二叉排序树BST分析

先看一个需求:
给定一个数列, 要求能够对其高效的完成数据的查询和添加

对于数据的查询和添加, 我们之前使用的是数组或者链表
但是这两种数据结构是各自有缺陷的
在这里插入图片描述
那有没有一种数据结构
能够实现查询和添加数据都十分的高效
就是二叉排序树
BST (要求: 对于树中的每一个非叶子结点, 左节点要小于当前节点, 而右节点要大于当前节点)
一. 添加操作(将数列构建成二叉排序树)
在这里插入图片描述

public class ErchapaixushuTest {
    public static void main(String[] args) {
        int[] arr = {8,8,2,45,335,22,0,54,2,3};
        BSTTree bstTree = new BSTTree();
        for (int i = 0; i < arr.length; i++){
            bstTree.add(new BSTNode(arr[i]));
        }
        bstTree.zhongxu();
    }
}

//二叉排序树
class BSTTree{
    public BSTNode root;

    //添加节点
    public void add(BSTNode node){
        if (root == null){
            root = node;
            return;
        }
        root.addNewNode(node);
    }
    //遍历中序
    public void zhongxu(){
        if (root != null){
            root.zhongxu();
        }else{
            System.out.println("空树");
        }
    }
}

//树节点
class BSTNode{
    int value;
    BSTNode left;
    BSTNode right;

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

    //add
    public void addNewNode(BSTNode node){
        //判断和当前节点的大小关系
        if (node.value < this.value){
            if (this.left == null){
                this.left = node;
            }else{
                this.left.addNewNode(node);  //如果当前节点有左节点, 就用左节点来递归调用add
            }
        }else{
            if (this.right == null){
                this.right = node;
            }else{
                this.right.addNewNode(node);  //如果当前节点有右节点, 就用右节点来递归调用add
            }
        }
    }

    //中序遍历就刚好是升序
    public void zhongxu(){
        if (this.left!= null){
            this.left.zhongxu();
        }
        System.out.println(this);
        if (this.right!= null){
            this.right.zhongxu();
        }
    }

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

BSTNode{value=0}
BSTNode{value=2}
BSTNode{value=2}
BSTNode{value=3}
BSTNode{value=8}
BSTNode{value=8}
BSTNode{value=22}
BSTNode{value=45}
BSTNode{value=54}
BSTNode{value=335}

二. 删除操作 (分删除的是叶子节点, 是只有一个子树的节点, 是有两个子树的节点)
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
由以上思路分析可知, 有共同的步骤就是
找到要删除的节点和找到要删除节点的父节点

public class ErchapaixushuTest {
    public static void main(String[] args) {
        int[] arr = {8,8,2,45,335,22,0,54,2,3};
        BSTTree bstTree = new BSTTree();
        for (int i = 0; i < arr.length; i++){
            bstTree.add(new BSTNode(arr[i]));
        }
        System.out.println("删除前");
        bstTree.zhongxu();
        //测试删除
        bstTree.delNode(8);
        System.out.println("删除后");
        bstTree.zhongxu();
    }
}

//二叉排序树
class BSTTree{
    public BSTNode root;

    //添加节点
    public void add(BSTNode node){
        if (root == null){
            root = node;
            return;
        }
        root.addNewNode(node);
    }
    //遍历中序
    public void zhongxu(){
        if (root != null){
            root.zhongxu();
        }else{
            System.out.println("空树");
        }
    }

    //找到要删除的节点
    public BSTNode searchOfDel(int value){
        if (root == null){
            return null;
        }else{
            return root.searchOfDel(value);
        }
    }
    //找到要删除的节点的父节点
    public BSTNode searchOfDelParent(int value){
        if (root == null){
            return null;
        }else if (value == root.value){   /1
            //如果要删除的点是根节点, 也是没有父节点
            return null;
        }else{
            return root.searchOfDelParent(value);  //也就是说必须确保要删除的点不是根节点, 而且确实树中有要删除的节点, 才能return出一个父节点
        }
    }
    //找到要删除节点的右子树中的最小节点的值, 并删除该最小节点
    public int getMinOfTarget(BSTNode target){
        //创建一个指针
        BSTNode minNode = target.right;  //也可以在左子树找最大的
        while(minNode.left != null){
            minNode = minNode.left;
        }
        if (minNode.value == target.value){ //说明要删除的节点的值和它的右节点的值是一样的   //如果没有这个if, 有相等的值会死循环
            //直接让要删除的节点的右节点向下跳一位, //为什么可以这样呢, 因为此时该最小节点必定没有左子树
            target.right = minNode.right;
            minNode.right = null;      //2
        }else{
            delNode(minNode.value);
        }
        return minNode.value;
    }
    //删除节点
    public void delNode(int value){
        if (root == null){
            return;
        }else{
            //先找到要删除的节点
            BSTNode target = searchOfDel(value);
            if (target == null){   //树中没有目标节点
                return;
            }
            //再找到要删除节点的父节点 , 如果上面没有发生return, 说明target不为null, 说明确实有这个节点
            BSTNode targetParent = searchOfDelParent(value);
            if (targetParent != null){ //说明有父节点, 要删除的节点不是根节点
                if (target.left == null && target.right == null){  //说明要删除的节点没有子树, 也就是叶子结点
                    //那么我们就要判断该节点是父节点的右还是左
                    if (targetParent.left != null && targetParent.left.value == value){ //说明是左
                        targetParent.left = null;
                    }else if (targetParent.right != null && targetParent.right.value == value){ //说明是右
                        targetParent.right = null;
                    }
                    //到这里是叶子结点的情况就处理完毕
                }else if (target.left != null && target.right != null){ //说明有左右子树
                    //先找到替换当前节点的值, 同时删除那个最小节点
                    int temp = getMinOfTarget(target);
                    //重置当前节点的值
                    target.value = temp;
                }else{ //只有一个子树
                    if (target.left != null){ //是左子树
                        if (targetParent.left.value == value){  //是父节点的左节点
                            targetParent.left = target.left;
                        }else{
                            targetParent.right = target.left;
                        }
                    }else{  //是右子树
                        if (targetParent.left.value == value){ //是父节点的左节点
                            targetParent.left = target.right;
                        }else{
                            targetParent.right = target.right;
                        }
                    }
                }
            }else{  //没有父节点, 说明要删除的点正好是树的根节点,
                //判断有没有左右子树
                if (root.left == null && root.right == null){
                    //直接变空树
                    root = null;
                }else if (target.left != null && target.right != null){ //说明有左右子树
                    //先找到替换当前节点的值, 同时删除那个最小节点
                    int temp = getMinOfTarget(target);
                    //重置当前节点的值
                    target.value = temp;
                }else{ //只有一个子树
                    if (target.left != null){  //说明是有左子树
                        root = target.left;
                        target.left = null;
                    }else{
                        root = target.right;
                        target.right = null;  //断开联系好被回收
                    }
                }
            }
        }
    }
}

//树节点
class BSTNode{
    int value;
    BSTNode left;
    BSTNode right;

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

    //add
    public void addNewNode(BSTNode node){
        //判断和当前节点的大小关系
        if (node.value < this.value){
            if (this.left == null){
                this.left = node;
            }else{
                this.left.addNewNode(node);  //如果当前节点有左节点, 就用左节点来递归调用add
            }
        }else{
            if (this.right == null){
                this.right = node;
            }else{
                this.right.addNewNode(node);  //如果当前节点有右节点, 就用右节点来递归调用add
            }
        }
    }

    //中序遍历就刚好是升序
    public void zhongxu(){
        if (this.left!= null){
            this.left.zhongxu();
        }
        System.out.println(this);
        if (this.right!= null){
            this.right.zhongxu();
        }
    }

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

    //删除节点--------
    //找到要删除的节点
    public BSTNode searchOfDel(int value){
        if (this.value == value){
            return this;
        }else if (value < this.value){
            if (this.left == null){
                return null;
            }else{
                return this.left.searchOfDel(value);
            }
        }else{
            if (this.right == null){
                return null;
            }else{
                return this.right.searchOfDel(value);
            }
        }
    }
    //找到要删除的节点的父节点  //这里有一个隐藏的Bug : 就是如果根节点的右节点刚好是等于根节点的value那就会造成我们要删除的节点正好的根节点时, 根节点还是自己的父节点, 所以在树中要有一个判断才行
    public BSTNode searchOfDelParent(int value){
        if ((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value)){
            return this;
        }else if (value < this.value && this.left != null){
            return this.left.searchOfDelParent(value);
        }else if (value >= this.value && this.right != null){
            return this.right.searchOfDelParent(value);
        }else{
            return null;
        }
    }
}

注意: 这其中有两个地方是我自己经过排查之后, 弥补的两个BUg

删除前
BSTNode{value=0}
BSTNode{value=2}
BSTNode{value=2}
BSTNode{value=3}
BSTNode{value=8}
BSTNode{value=8}
BSTNode{value=22}
BSTNode{value=45}
BSTNode{value=54}
BSTNode{value=335}
删除后
BSTNode{value=0}
BSTNode{value=2}
BSTNode{value=2}
BSTNode{value=3}
BSTNode{value=8}
BSTNode{value=22}
BSTNode{value=45}
BSTNode{value=54}
BSTNode{value=335}

到这里我们学会了添加和遍历, 还有删除的方法
这也是BST比数组和链表有优势的地方, 添加和删除都要方便的多
接下来就要学习树的查找

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值