平衡二叉树AVL分析

在这里插入图片描述
这样一来, 我们在二叉排序树的基础上, 再来实现平衡二叉树
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
以下是单旋转的情况分析

public class AVLtest {
    
    
    public static void main(String[] args) {
    
    
        //int[] arr = {4,3,6,5,7,8};
        int[] arr = {
    
    10,12,8,9,7,6};
        //创建一个Avl数对象
        AvlTree avlTree = new AvlTree();
        for (int i = 0; i < arr.length; i++){
    
    
            avlTree.add(new Avlnode(arr[i]));
        }
        //遍历
        avlTree.zhongxu();
        /*System.out.println("在没有做平衡处理前");
        System.out.println("树的高度为:"+avlTree.getRoot().getHeight());
        System.out.println("根节点的左子树的高度为:"+avlTree.getRoot().left.getHeight());
        System.out.println("根节点的右子树的高度为:"+avlTree.getRoot().right.getHeight());*/

        System.out.println("在做平衡处理后");  //平衡处理是在添加时一并处理的
        System.out.println("树的高度为:"+avlTree.getRoot().getHeight());
        System.out.println("根节点的左子树的高度为:"+avlTree.getRoot().left.getHeight());
        System.out.println("根节点的右子树的高度为:"+avlTree.getRoot().right.getHeight());
        avlTree.zhongxu();
    }
}

//平衡二叉树类
class AvlTree {
    
    
    public Avlnode root;
    //添加节点
    public void add(Avlnode node){
    
    
        if (root == null){
    
    
            root = node;
            return;
        }
        root.addNewNode(node);
    }
    //遍历中序
    public void zhongxu(){
    
    
        if (root != null){
    
    
            root.zhongxu();
        }else{
    
    
            System.out.println("空树");
        }
    }

    //找到要删除的节点
    public Avlnode searchOfDel(int value){
    
    
        if (root == null){
    
    
            return null;
        }else{
    
    
            return root.searchOfDel(value);
        }
    }
    //找到要删除的节点的父节点
    public Avlnode 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(Avlnode target){
    
    
        //创建一个指针
        Avlnode 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{
    
    
            //先找到要删除的节点
            Avlnode target = searchOfDel(value);
            if (target == null){
    
       //树中没有目标节点
                return;
            }
            //再找到要删除节点的父节点 , 如果上面没有发生return, 说明target不为null, 说明确实有这个节点
            Avlnode 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;  //断开联系好被回收
                    }
                }
            }
        }
    }

    //以上是二叉排序树的基础------------------------------------------------------------------------------------------------------------
    public Avlnode getRoot() {
    
    
        return root;
    }

}

//节点类
class Avlnode {
    
    
    int value;
    Avlnode left;
    Avlnode right;

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

    //add
    public void addNewNode(Avlnode 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
            }
        }
        //平衡处理
        //每添加一个节点就做一次平衡处理
        if (this.getLeftHeight() + 1 < this.getRightHeight()){
    
    
            this.leftxuanzhuan();
        }
        if (this.getRightHeight() + 1 < this.getLeftHeight()){
    
    
            this.rightxuanzhuan();
        }
    }

    //中序遍历就刚好是升序
    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 "Avlnode{" +
                "value=" + value +
                '}';
    }

    //删除节点--------
    //找到要删除的节点
    public Avlnode 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 Avlnode 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;
        }
    }

    //以上都是二叉排序树的基础------------------------------------------------------------------------------------------------------------
    //得到this节点作为根节点的数的高度
    public int getHeight(){
    
    
        return Math.max(left == null ? 0 : left.getHeight(), right == null ? 0 : right.getHeight())+1;  //为什么要加1, 因为当前节点就是一个高度
    }
    //
    public int getLeftHeight (){
    
    
        if (this.left == null){
    
    
            return 0;
        }
        return this.left.getHeight();
    }
    public int getRightHeight (){
    
    
        if (this.right == null){
    
    
            return 0;
        }
        return this.right.getHeight();
    }
    //左旋转
    public void leftxuanzhuan(){
    
    
        //1. 创建一个新的节点, 值为当前节点的值
        Avlnode newavlnode = new Avlnode(this.value);
        //2. 把新的节点的左子树设置成当前节点的右子树
        newavlnode.left = this.left;
        //3. 把新的节点的右子树设置成当前节点的左子树
        newavlnode.right = this.right.left;
        //4. 把当前节点的值换成当前节点的右子节点的值
        this.value = this.right.value;
        //5. 把当前节点的左子树设置成新建的那个节点
        this.left = newavlnode;
        //6. 把当前节点的右子树设置成当前节点右节点的右节点
        //this.right = this.right.right;
        //优化写法, 节省空间, 替换第六步
        Avlnode temp = this.right.right;
        this.right.right = null;
        this.right = temp;
    }
    //右旋转
    public void rightxuanzhuan(){
    
    
        Avlnode newavlnode = new Avlnode(this.value);
        newavlnode.right = this.right;
        newavlnode.left = this.left.right;
        this.value = this.left.value;
        this.right = newavlnode;
        Avlnode temp = this.left.left;
        this.left.left = null;
        this.left = temp;
    }
}

Avlnode{
    
    value=3}
Avlnode{
    
    value=4}
Avlnode{
    
    value=5}
Avlnode{
    
    value=6}
Avlnode{
    
    value=7}
Avlnode{
    
    value=8}
在没有做平衡处理前
树的高度为:4
根节点的左子树的高度为:1
根节点的右子树的高度为:3
Avlnode{
    
    value=3}
Avlnode{
    
    value=4}
Avlnode{
    
    value=5}
Avlnode{
    
    value=6}
Avlnode{
    
    value=7}
Avlnode{
    
    value=8}
Avlnode{
    
    value=3}
Avlnode{
    
    value=4}
Avlnode{
    
    value=5}
Avlnode{
    
    value=6}
Avlnode{
    
    value=7}
Avlnode{
    
    value=8}
在做平衡处理后
树的高度为:3
根节点的左子树的高度为:2
根节点的右子树的高度为:2
Avlnode{
    
    value=3}
Avlnode{
    
    value=4}
Avlnode{
    
    value=5}
Avlnode{
    
    value=6}
Avlnode{
    
    value=7}
Avlnode{
    
    value=8}
Avlnode{
    
    value=6}
Avlnode{
    
    value=7}
Avlnode{
    
    value=8}
Avlnode{
    
    value=9}
Avlnode{
    
    value=10}
Avlnode{
    
    value=12}
在没有做平衡处理前
树的高度为:4
根节点的左子树的高度为:3
根节点的右子树的高度为:1
Avlnode{
    
    value=6}
Avlnode{
    
    value=7}
Avlnode{
    
    value=8}
Avlnode{
    
    value=9}
Avlnode{
    
    value=10}
Avlnode{
    
    value=12}
Avlnode{
    
    value=6}
Avlnode{
    
    value=7}
Avlnode{
    
    value=8}
Avlnode{
    
    value=9}
Avlnode{
    
    value=10}
Avlnode{
    
    value=12}
在没有做平衡处理后
树的高度为:3
根节点的左子树的高度为:2
根节点的右子树的高度为:2
Avlnode{
    
    value=6}
Avlnode{
    
    value=7}
Avlnode{
    
    value=8}
Avlnode{
    
    value=9}
Avlnode{
    
    value=10}
Avlnode{
    
    value=12}

由此我们可以看到
int[] arr = {4,3,6,5,7,8};
int[] arr = {10,12,8,9,7,6}
这两个数组去创建AVL树时, 都是单旋转就可以
但是
如果此时换成是这个数组呢
int[] arr = {10,11,7,6,8,9};
以上代码不变测试结果如下

Avlnode{
    
    value=6}
Avlnode{
    
    value=7}
Avlnode{
    
    value=8}
Avlnode{
    
    value=9}
Avlnode{
    
    value=10}
Avlnode{
    
    value=11}
在做平衡处理后
树的高度为:4
根节点的左子树的高度为:1
根节点的右子树的高度为:3
Avlnode{
    
    value=6}
Avlnode{
    
    value=7}
Avlnode{
    
    value=8}
Avlnode{
    
    value=9}
Avlnode{
    
    value=10}
Avlnode{
    
    value=11}

可以看到, 依然没有平衡
原因和解决方法应该是怎么样的呢
在这里插入图片描述

也就是说此时节点类的add方法中

		//平衡处理
        //每添加一个节点就做一次平衡处理
        if (this.getLeftHeight() + 1 < this.getRightHeight()){
    
    
            this.leftxuanzhuan();
        }
        if (this.getRightHeight() + 1 < this.getLeftHeight()){
    
    
            this.rightxuanzhuan();
        }

这样的代码是不够的用了
也就是说, 在进行一次旋转操作之前, 还要完善条件, 说白了
就是, 右旋转之前要不要先将当前节点的左子树先左旋, 左旋转之前是不是需要将当前节点的右子树先右旋

		//平衡处理
        //每添加一个节点就做一次平衡处理
        if (this.getLeftHeight() + 1 < this.getRightHeight()){
    
    
            if (this.right != null && this.right.getLeftHeight() > this.right.getRightHeight()){
    
      //这种情况直接左旋转还是不平衡, 要先将当前节点的左子树先右旋转
                this.right.rightxuanzhuan();
            }
            this.leftxuanzhuan();
        }
        if (this.getRightHeight() + 1 < this.getLeftHeight()){
    
    
            if (this.left != null && this.left.getRightHeight() > this.left.getLeftHeight()){
    
     //这种情况直接右旋转还是不平衡, 要先将当前节点的左子树先左旋转
                this.left.leftxuanzhuan();
            }
            this.rightxuanzhuan();
        }

但是这又会带来一个问题,那就是平衡二叉树的定义过于严格,导致每次插入或者删除一个元素之后,都要去维护二叉树整体的平衡,这样产生额外的代价又太大了。
右该怎么办呢?

猜你喜欢

转载自blog.csdn.net/weixin_45032905/article/details/121583246