Java实现二叉排序树的创建、遍历与结点的删除

package com.xiejianjun.day10;

/**
 * @author bilibilidick
 * @version 2022 04
 * @date 2022/4/25 12:38
 */

public class BinarySortTree {

    public static Node root;

    public static void main(String[] args) {
        int[] tree = {7, 3, 10, 12, 5, 1, 9, 2};
        for (int i = 0; i < tree.length; i++) {
            if (root == null){
                root = new Node(tree[i]);
                continue;
            }
            root.add(new Node(tree[i]));
        }
        BinarySortTree sortTree = new BinarySortTree();
        System.out.println("==========================中序遍历二叉排序树====================================");
        sortTree.inOrder(root);
        sortTree.deleteNode(2);
        System.out.println("==========================删除2结点后遍历排序树=================================");
        sortTree.inOrder(root);
        sortTree.deleteNode(5);
        System.out.println("==========================删除5结点后遍历排序树=================================");
        sortTree.inOrder(root);
        sortTree.deleteNode(9);
        System.out.println("==========================删除9结点后遍历排序树=================================");
        sortTree.inOrder(root);
        sortTree.deleteNode(12);
        System.out.println("==========================删除12结点后遍历排序树=================================");
        sortTree.inOrder(root);
        sortTree.deleteNode(7);
        System.out.println("==========================删除7结点后遍历排序树=================================");
        sortTree.inOrder(root);
        sortTree.deleteNode(3);
        System.out.println("==========================删除3结点后遍历排序树=================================");
        sortTree.inOrder(root);
        sortTree.deleteNode(10);
        System.out.println("==========================删除10结点后遍历排序树=================================");
        sortTree.inOrder(root);
        sortTree.deleteNode(1);
        System.out.println("==========================删除1结点后遍历排序树=================================");
        sortTree.inOrder(root);
    }


    static class Node {
        int value;
        Node left;
        Node right;


        Node(int value) {
            this.value = value;
        }

        void add(Node node) {
            if (this.value > node.value) {
                if (this.left == null) this.left = node;
                else this.left.add(node);
            } else {
                if (this.right == null) this.right = node;
                else this.right.add(node);
            }
        }

        Node search(int value) {
            if (this.value == value) {
                return this;
            }
            if (this.left != null && value < this.value) return this.left.search(value);
            else if (this.right != null) return this.right.search(value);
            return null;
        }

        Node searchParent(int value) {
            if (this.left != null && this.left.value == value) {
                return this;
            } else if (this.right != null && this.right.value == value) {
                return this;
            }
            if (this.left != null && value < this.value) {
                return this.left.searchParent(value);
            } else if (this.right != null && value > this.value) {
                return this.right.searchParent(value);
            }
            return null;
        }



        @Override
        public String toString() {
            return "Node{" +
                    "value=" + value +
                    '}';
        }
    }

    // 找到左子树的最小值的结点,删除它,并返回被删除结点的值
    int delRightTreeMin(Node node) {
        int result = 0;
        if (node != null) {
            while (node.left != null) {
                node = node.left;
            }
            result = node.value;
            deleteNode(node.value);
        }
        return result;
    }

    public void inOrder(Node root) {

        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.println(root.value);
        inOrder(root.right);
    }

    public void deleteNode(int value) {
        if (root == null)return;
        Node target = root.search(value);
        if (target == null)return;
        // 1、删除结点正好为根节点:
        if (root.left == null && root.right == null) {
            root = null;
            return;
        }
        // 2、删除结点不为根节点的情况:
        Node parent = root.searchParent(value);
        // 2.1、删除结点为叶子结点
        if (target.left == null && target.right == null) {
            if (parent.left != null && parent.left.value == value) {
                parent.left = null;
            } else if (parent.right != null && parent.right.value == value) {
                parent.right = null;
            }
        // 2.2、删除结点为带两个子节点的结点
        } else if (target.left != null && target.right != null) {
            // 删除被删除结点的左子树的最小值的结点,并将最小值放入被删除结点处
            target.value = delRightTreeMin(target.right);
        }
        // 2.3、删除结点为带一个子节点的结点
        else {
            if (target.left != null) {
                if (parent != null) {
                    if (parent.left != null && parent.left.value == target.value) {
                        parent.left = target.left;
                    } else if (parent.right != null && parent.right.value == target.value) {
                        parent.right = target.left;
                    }
                } else {
                    root = target.left;
                }
            } else {
                if (parent != null) {
                    if (parent.left != null && parent.left.value == target.value) {
                        parent.left = target.right;
                    } else if (parent.right != null && parent.right.value == target.value) {
                        parent.right = target.right;
                    }
                } else {
                    root = target.right;
                }
            }
        }
    }




}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值