二叉排序树创建,查找,删除

代码

public class Main {
    public static void main(String[] args) {
        int[] arr = {7,3,10,12,5,1,9,2};
        BinarySortTree binarySortTree = new BinarySortTree();
        for (int i = 0;i < arr.length;i++){
            binarySortTree.add(new Node(arr[i]));
        }

        binarySortTree.infixList();
        binarySortTree.deleteNode(7);
        binarySortTree.infixList();
    }
}
class BinarySortTree{
    Node root;
    //找到要删除的节点
    public Node searchNode(int value){
        if (root == null){
            System.out.println("二叉排序树为空");
            return null;
        }else {
            return root.searchNode(value);
        }
    }

    //找到要删除节点的父节点
    public Node searchParentNode(int value){
        if (root == null){
            System.out.println("二叉排序树为空");
            return null;
        }else {
            return root.searchParentNode(value);
        }
    }

    //删除节点
    public void deleteNode(int value){
        if (root == null){
            System.out.println("二叉排序树为空");
            return;
        }

        //先找到要删除的节点
        Node targetNode = searchNode(value);
        //如果没找到 则返回
        if (targetNode == null){
            System.out.println("没找到要删除的节点");
            return;
        }
        //走到这说明找到了,那么如果只有一个节点,即根节点就是
        if (root.left == null && root.right == null){
            //说明只有一个节点即根节点,且根节点就是我们需要删除的
            root = null;
        }

        //说明要删除的不是 只有一个根节点的树的根
        //但是有可能  要删除的点是只有一个根节点和其子节点的 树的根节点,此时parentNode就是null
        Node parentNode = searchParentNode(value);
        System.out.println("parentNode是"+parentNode);
        //首先判断是不是叶子节点
        if (targetNode.left == null && targetNode.right == null){
            //说明是叶子节点
            //然后找该节点是父节点的左子节点还是右子节点
            if (parentNode.left != null && parentNode.left.value == value){
                //是左子节点
                parentNode.left = null;
            }else if (parentNode.right != null && parentNode.right.value ==value){
                parentNode.right = null;
            }
        }else if (targetNode.left != null && targetNode.right != null){
            //说明要删除的节点有两个子节点

            //找到右子树的最小节点的值,把它删除并将最小值赋给targetNode
            Node temp = targetNode.right;
            while (temp.left != null){
                temp = temp.left;
            }
            //退出的时候,temp就是指向的最小值了
            deleteNode(temp.value);//把他删除
            targetNode.value = temp.value;

        }else {
            //说明要删除的节点有一个子节点
            //如果该子节点是左子节点
            if (targetNode.left != null){
                //如果目标节点是父节点的左子节点
                if (parentNode != null){
                    if (parentNode.left != null && parentNode.left.value == value){
                        parentNode.left = targetNode.left;
                    }else {
                        //如果目标节点是父节点的右子节点
                        parentNode.right = targetNode.left;
                    }
                }else {
                    root = targetNode.left;
                }
            }else {
                //说明该子节点是右子节点
                if (parentNode != null){
                    //如果目标节点是父节点的左子节点
                    if (parentNode.left != null && parentNode.left.value == value){
                        parentNode.left = targetNode.right;
                    }else {
                        //如果目标节点是父节点的右子节点
                        parentNode.right = targetNode.right;
                    }
                }else {
                    root = parentNode.right;
                }
            }
        }
    }
    public void add(Node node){
        if (root == null){
            root = node;
        }else {
            root.add(node);
        }
    }

    public void infixList(){
        if (root == null){
            return;
        }else {
            root.infixList();
        }
    }
}
class Node{
    int value;
    Node left;
    Node right;

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

    //添加元素
    public void add(Node node){
        if (node == null){
            return;
        }

        if (node.value < this.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);
            }
        }
    }

    //查找要删除的节点
    public Node searchNode(int value){
        if (this.value == value){
            return this;
        }else if (value < this.value){//往左边递归
            if (this.left == null){
                return null;
            }
            return this.left.searchNode(value);
        }else{
            if (this.right == null){
                return null;
            }
            return this.right.searchNode(value);
        }
    }

    //查找要删除的节点的父节点
    public Node searchParentNode(int value){
        if ((this.left != null && this.left.value == value)
        || (this.right != null && this.right.value == value)){
            return this;
        }else if (value < this.value){
            if (this.left == null){
                return null;
            }
            return this.left.searchParentNode(value);
        }else if (value >= this.value){
            if (this.right == null){
                return null;
            }
            return this.right.searchParentNode(value);
        }else {
            return null;
        }
    }

    //中序遍历
    public void infixList(){
        if (this.left != null){
            this.left.infixList();
        }

        System.out.println(this);

        if (this.right != null){
            this.right.infixList();
        }
    }
    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }
}

结果

Node{value=1}
Node{value=2}
Node{value=3}
Node{value=5}
Node{value=7}
Node{value=9}
Node{value=10}
Node{value=12}
parentNode是null
parentNode是Node{value=10}
Node{value=1}
Node{value=2}
Node{value=3}
Node{value=5}
Node{value=9}
Node{value=10}
Node{value=12}

注意点

这里注意 要删除根节点的时候有三种情况

①正常情况

在这里插入图片描述
对应程序的这里(此时没有用到parentNode
在这里插入图片描述

②单个情况

在这里插入图片描述
对应程序的这里,此时还没有走到给parentNode赋值的那里
在这里插入图片描述

③双个情况

在这里插入图片描述
对应程序的这里(此时parentNodenull
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值