Java数据结构与算法-树结构实际应用Ⅳ(平衡二叉树AVL、左旋转、右旋转)[day10]

平衡二叉树(AVL树)

看一个案例(说明二叉排序树可能的问题)

给你一个数列{1,2,3,4,5,6},要求创建一颗二叉排序树(BST), 并分析问题所在。

在这里插入图片描述
左边BST 存在的问题分析:

  • 左子树全部为空,从形式上看,更像一个单链表.

  • 插入速度没有影响

  • 查询速度明显降低(因为需要依次比较), 不能发挥BST 的优势,因为每次还需要比较左子树,其查询速度比 单链表还慢

  • 解决方案 ->平衡二叉树(AVL)

基本介绍

  • 平衡二叉树也叫平衡二叉搜索树(Self-balancing binary search tree)又被称为AVL树, 可以保证查询效率较高。
  • 具有以下特点:它是一 棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。平衡二叉树的常用实现方法- 有红黑树、AVL、替罪羊树、Treap、伸展树等。
  • 举例说明, 看看下面哪些AVL树, 为什么?
    在这里插入图片描述
    ①②为AVL树

AVL树左旋转思路图解

应用案例-单旋转(左旋转)

1.要求: 给你一个数列,创建出对应的平衡二叉树.数列 {4,3,6,5,7,8}

2.思路分析(示意图)

在这里插入图片描述

  • 问题:当插入8 时,rightHeight() - leftHeight() > 1 成立,此时,不再是一颗avl树了. 怎么处理才能保证为AVL树 --> 进行左旋转.

  • 具体步骤图解:

1.创建一个新的节点 newNode (以4这个值创建),创建一个新的节点,值等于当前根节点的值.

//把新节点的左子树设置了当前节点的左子树
在这里插入图片描述
2. newNode.left = left
//把新节点的右子树设置为当前节点的右子树的左子树
在这里插入图片描述
3. newNode.right =right.left;
//把当前节点的值换为右子节点的值

在这里插入图片描述

4.value=right.value;
//把当前节点的右子树设置成右子树的右子树

在这里插入图片描述

  1. right=right.right;
    //把当前节点的左子树设置为新节点
    在这里插入图片描述
  2. left=newLeft;
    在这里插入图片描述
    在这里插入图片描述

求AVL树的高度

public class AVLTreeDemo {
    
    
    public static void main(String[] args) {
    
    
        int arr[] = {
    
    4,3,6,5,7,8};
        //创建一个AVLTree对象
        AVLTree avlTree = new AVLTree();
        //添加结点
        for (int i = 0; i < arr.length; i++) {
    
    
            avlTree.add(new Node(arr[i]));
        }

        //遍历
        System.out.println("中序遍历");
        avlTree.infixOrder();

        System.out.println("在没有平衡处理前~");
        if (Math.abs(avlTree.getRoot().left.height()- avlTree.getRoot().right.height()) > 1){
    
    
            
        }

    }
}

//创建AVLTree
class AVLTree {
    
    
    private Node root;

    public Node getRoot() {
    
    
        return root;
    }

    //查找要删除的结点
    public Node search(int value) {
    
    
        if (root == null) {
    
    
            return null;
        } else {
    
    
            return root.search(value);
        }
    }

    //查找父节点
    public Node searchParent(int value) {
    
    
        if (root == null) {
    
    
            return null;
        } else {
    
    
            return root.searchParent(value);
        }
    }

    /*
    编写方法
    1. 返回以node为根结点的,二叉排序树的最小结点的值。
    2. 删除以node为根结点的,二叉排序树的最小结点。
     */

    /**
     * 1.返回这个值
     * 2.删除这个值
     *
     * @param node 传入的结点(当作二叉排序树的根节点)
     * @return 返回的 以node为根结点的,二叉排序树的最小结点的值
     */
    public int delRightTreeMin(Node node) {
    
    
        Node target = node;
        //循环的查找左子节点,就会找到最小值
        while (target.left != null) {
    
     //当while循环结束后,target就指向以node为根节点的最小值结点
            target = target.left;
        }
        //这是target就指向了最小节点
        //删除最小结点
        delNode(target.value);
        return target.value;
    }

    //课后练习:从左子树找到最大的结点,然后删除节点
    public int delLeftTreeMax(Node node) {
    
    
        while (node.right != null) {
    
    
            node = node.right;
        }
        delNode(node.value);
        return node.value;
    }

    //删除结点
    public void delNode(int value) {
    
    
        if (root == null) {
    
    
            return;
        } else {
    
    
            //1.先找到要删除的结点 targetNode
            Node targetNode = search(value);
            //如果没有找到要删除的结点,下面不用走了。
            if (targetNode == null) {
    
    
                return;
            }
            //如果我们发现当前这棵二叉排序树 没有父节点,说明根结点就是我们要删除的结点,直接删除根结点即可
            if (root.left == null && root.right == null) {
    
    
                root = null;
                return;
            }
            //去找targetNode的父节点
            Node parent = searchParent(value);
            //第一种情况:如果要删除的结点是叶子结点
            if (targetNode.left == null && targetNode.right == null) {
    
    
                //判断targetNode是父节点的左子节点,还是右子结点。
                if (parent.left != null && parent.left.value == value) {
    
     //是左子节点
                    parent.left = null;
                } else if (parent.right != null && parent.right.value == value) {
    
     //是右子结点
                    parent.right = null;
                }
            } else if (targetNode.left != null && targetNode.right != null) {
    
     //删除有两棵子树的结点
                int minVal = delRightTreeMin(targetNode.right);
                //int maxVal = delLeftTreeMax(targetNode.left);
                targetNode.value = minVal;
            } else {
    
     //删除有一棵子树的结点
                //如果要删除的结点有左子节点
                if (targetNode.left != null) {
    
    
                    if (parent != null) {
    
    
                        //如果targetNode 是 parent 的左子节点
                        if (parent.left.value == value) {
    
    
                            parent.left = targetNode.left;
                        } else {
    
     //如果targetNode 是 parent 的右子节点
                            parent.right = targetNode.left;
                        }
                    } else {
    
    
                        root = targetNode.left;
                    }
                } else {
    
     //如果要删除的结点有右子节点
                    if (parent != null) {
    
    
                        //如果targetNode 是 parent 的左子节点
                        if (parent.right.value == value) {
    
    
                            parent.right = targetNode.right;
                        } else {
    
     //如果targetNode 是 parent 的右子节点
                            parent.left = targetNode.right;
                        }
                    } else {
    
    
                        root = targetNode.right;
                    }
                }
            }
        }
    }

    //添加结点的方法
    public void add(Node node) {
    
    
        if (root == null) {
    
    
            root = node;//如果root为空,则直接让root指向node
        } else {
    
    
            root.add(node);
        }
    }

    //中序遍历方法
    public void infixOrder() {
    
    
        if (root != null) {
    
    
            root.infixOrder();
        } else {
    
    
            System.out.println("该树为空,无法遍历!");
        }
    }
}

//创建Node结点
class Node {
    
    
    int value;
    Node left;
    Node right;

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

    //返回左子树的高度
    public int leftHeight() {
    
    
        if (left == null) {
    
    
            return 0;
        }
        return left.height();
    }

    //返回右子树的高度
    public int rightHeight() {
    
    
        if (right == null) {
    
    
            return 0;
        }
        return right.height();
    }

    //返回该结点为根结点的树的高度
    public int height() {
    
    
        return Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height()) + 1;
    }


    //查找要删除的结点

    /**
     * @param value 希望删除的结点的值
     * @return 如果找到返回该结点, 否则返回null
     */
    public Node search(int value) {
    
    
        if (value == this.value) {
    
    //找到
            return this;
        } else if (value < this.value) {
    
    //如果查找的值小于当前结点,向左子树递归查找
            if (this.left == null) {
    
    
                return null;
            }
            return this.left.search(value);
        } else {
    
    //如果查找的值大于等于当前结点,向右子树递归查找
            if (this.right == null) {
    
    
                return null;
            }
            return this.right.search(value);
        }
    }

    //查找要删除结点的父节点

    /**
     * @param value 要找的结点的值
     * @return 返回的是要删除的结点的父节点
     */
    public Node searchParent(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.searchParent(value);
            } else if (value >= this.value && this.right != null) {
    
    //如果查找的值大于当前结点的值,并且当前结点的右子节点不为空
                return this.right.searchParent(value);
            } else {
    
    
                return null;
            }
        }
    }

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

    //添加结点的方法
    //递归的形式添加结点,注意需要满足二叉排序树
    public void add(Node node) {
    
    
        if (node == null) {
    
    
            return;
        }

        //判断传入的node结点值,和当前子树的根结点的值关系
        if (node.value < this.value) {
    
    
            //如果当前结点的左子节点为null
            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 void infixOrder() {
    
    
        if (this.left != null) {
    
    
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null) {
    
    
            this.right.infixOrder();
        }
    }
}

AVL树左旋转代码实现

public class AVLTreeDemo {
    
    
    public static void main(String[] args) {
    
    
        int arr[] = {
    
    4,3,6,5,7,8};
        //创建一个AVLTree对象
        AVLTree avlTree = new AVLTree();
        //添加结点
        for (int i = 0; i < arr.length; i++) {
    
    
            avlTree.add(new Node(arr[i]));
        }

        //遍历
        System.out.println("中序遍历");
        System.out.println("平衡处理后");
        avlTree.infixOrder();
        System.out.println("树的高度=" + avlTree.getRoot().height());
        System.out.println("树的左子树高度=" + avlTree.getRoot().leftHeight());
        System.out.println("树的左子树高度=" + avlTree.getRoot().rightHeight());


    }
}

//创建AVLTree
class AVLTree {
    
    
    private Node root;

    public Node getRoot() {
    
    
        return root;
    }

    //查找要删除的结点
    public Node search(int value) {
    
    
        if (root == null) {
    
    
            return null;
        } else {
    
    
            return root.search(value);
        }
    }

    //查找父节点
    public Node searchParent(int value) {
    
    
        if (root == null) {
    
    
            return null;
        } else {
    
    
            return root.searchParent(value);
        }
    }

    /*
    编写方法
    1. 返回以node为根结点的,二叉排序树的最小结点的值。
    2. 删除以node为根结点的,二叉排序树的最小结点。
     */

    /**
     * 1.返回这个值
     * 2.删除这个值
     *
     * @param node 传入的结点(当作二叉排序树的根节点)
     * @return 返回的 以node为根结点的,二叉排序树的最小结点的值
     */
    public int delRightTreeMin(Node node) {
    
    
        Node target = node;
        //循环的查找左子节点,就会找到最小值
        while (target.left != null) {
    
     //当while循环结束后,target就指向以node为根节点的最小值结点
            target = target.left;
        }
        //这是target就指向了最小节点
        //删除最小结点
        delNode(target.value);
        return target.value;
    }

    //课后练习:从左子树找到最大的结点,然后删除节点
    public int delLeftTreeMax(Node node) {
    
    
        while (node.right != null) {
    
    
            node = node.right;
        }
        delNode(node.value);
        return node.value;
    }

    //删除结点
    public void delNode(int value) {
    
    
        if (root == null) {
    
    
            return;
        } else {
    
    
            //1.先找到要删除的结点 targetNode
            Node targetNode = search(value);
            //如果没有找到要删除的结点,下面不用走了。
            if (targetNode == null) {
    
    
                return;
            }
            //如果我们发现当前这棵二叉排序树 没有父节点,说明根结点就是我们要删除的结点,直接删除根结点即可
            if (root.left == null && root.right == null) {
    
    
                root = null;
                return;
            }
            //去找targetNode的父节点
            Node parent = searchParent(value);
            //第一种情况:如果要删除的结点是叶子结点
            if (targetNode.left == null && targetNode.right == null) {
    
    
                //判断targetNode是父节点的左子节点,还是右子结点。
                if (parent.left != null && parent.left.value == value) {
    
     //是左子节点
                    parent.left = null;
                } else if (parent.right != null && parent.right.value == value) {
    
     //是右子结点
                    parent.right = null;
                }
            } else if (targetNode.left != null && targetNode.right != null) {
    
     //删除有两棵子树的结点
                int minVal = delRightTreeMin(targetNode.right);
                //int maxVal = delLeftTreeMax(targetNode.left);
                targetNode.value = minVal;
            } else {
    
     //删除有一棵子树的结点
                //如果要删除的结点有左子节点
                if (targetNode.left != null) {
    
    
                    if (parent != null) {
    
    
                        //如果targetNode 是 parent 的左子节点
                        if (parent.left.value == value) {
    
    
                            parent.left = targetNode.left;
                        } else {
    
     //如果targetNode 是 parent 的右子节点
                            parent.right = targetNode.left;
                        }
                    } else {
    
    
                        root = targetNode.left;
                    }
                } else {
    
     //如果要删除的结点有右子节点
                    if (parent != null) {
    
    
                        //如果targetNode 是 parent 的左子节点
                        if (parent.right.value == value) {
    
    
                            parent.right = targetNode.right;
                        } else {
    
     //如果targetNode 是 parent 的右子节点
                            parent.left = targetNode.right;
                        }
                    } else {
    
    
                        root = targetNode.right;
                    }
                }
            }
        }
    }

    //添加结点的方法
    public void add(Node node) {
    
    
        if (root == null) {
    
    
            root = node;//如果root为空,则直接让root指向node
        } else {
    
    
            root.add(node);
        }
    }

    //中序遍历方法
    public void infixOrder() {
    
    
        if (root != null) {
    
    
            root.infixOrder();
        } else {
    
    
            System.out.println("该树为空,无法遍历!");
        }
    }


}

//创建Node结点
class Node {
    
    
    int value;
    Node left;
    Node right;

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

    //返回左子树的高度
    public int leftHeight() {
    
    
        if (left == null) {
    
    
            return 0;
        }
        return left.height();
    }

    //返回右子树的高度
    public int rightHeight() {
    
    
        if (right == null) {
    
    
            return 0;
        }
        return right.height();
    }

    //返回该结点为根结点的树的高度
    public int height() {
    
    
        return Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height()) + 1;
    }

    //左旋转方法
    private void leftRotate(){
    
    

        //1.创建新的结点,以当前根节点的值创建
        Node newNode = new Node(value);
        //2.把新结点的左子树设置为当前结点的左字节
        newNode.left = left;
        //3.把新的结点的右子树设置成当前结点的右子树的左子树
        newNode.right = right.left;
        //4.把当前结点的值换成右子结点的值
        value = right.value;
        //5.把当前结点的右子树设置成当前结点的右子树的右子树
        right = right.right;
        //6.把当前结点的左子树(左子结点)设置成新的结点
        left = newNode;
    }

    //查找要删除的结点

    /**
     * @param value 希望删除的结点的值
     * @return 如果找到返回该结点, 否则返回null
     */
    public Node search(int value) {
    
    
        if (value == this.value) {
    
    //找到
            return this;
        } else if (value < this.value) {
    
    //如果查找的值小于当前结点,向左子树递归查找
            if (this.left == null) {
    
    
                return null;
            }
            return this.left.search(value);
        } else {
    
    //如果查找的值大于等于当前结点,向右子树递归查找
            if (this.right == null) {
    
    
                return null;
            }
            return this.right.search(value);
        }
    }

    //查找要删除结点的父节点

    /**
     * @param value 要找的结点的值
     * @return 返回的是要删除的结点的父节点
     */
    public Node searchParent(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.searchParent(value);
            } else if (value >= this.value && this.right != null) {
    
    //如果查找的值大于当前结点的值,并且当前结点的右子节点不为空
                return this.right.searchParent(value);
            } else {
    
    
                return null;
            }
        }
    }

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

    //添加结点的方法
    //递归的形式添加结点,注意需要满足二叉排序树
    public void add(Node node) {
    
    
        if (node == null) {
    
    
            return;
        }

        //判断传入的node结点值,和当前子树的根结点的值关系
        if (node.value < this.value) {
    
    
            //如果当前结点的左子节点为null
            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);
            }
        }
        if (rightHeight() - leftHeight() > 1){
    
    

            leftRotate();//左旋转

        }
    }

    //当添加完一个结点后,如果 (右子树的高度-左子树的高度) > 1

    //中序遍历
    public void infixOrder() {
    
    
        if (this.left != null) {
    
    
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null) {
    
    
            this.right.infixOrder();
        }
    }


}

此旋转考虑并不完整,完整的旋转代码,参考下方的双旋转。

AVL树右旋转图解和实现

  • 要求: 给你一个数列,创建出对应的平衡二叉树
    数列 {10,12, 8, 9, 7, 6}

  • 问题:当插入6 时,leftHeight() - rightHeight() > 1 成立,此时,不再是一颗avl树了.

  • 怎么处理 --> 进行右旋转.[就是降低左子树的高度], 这里是将9 这个节点,通过右旋转,到右子树

思路分析(示意图):
在这里插入图片描述

  1. 创建一个新的节点 newNode (以10这个值创建),创建一个新的节点,值等于当前根节点的值
    把新节点的右子树设置了当前节点的右子树
    在这里插入图片描述
  1. newNode.right = right 把新节点的左子树设置为当前节点的左子树的右子树
    在这里插入图片描述
  1. newNode.left =left.right; 把当前节点的值换为左子节点的值
    在这里插入图片描述

4.value=left.value;
把当前节点的左子树设置成左子树的左子树
在这里插入图片描述

  1. left=left.left;
    把当前节点的右子树设置为新节点
    在这里插入图片描述
  1. right=newLeft; 在这里插入图片描述

在这里插入图片描述
代码实现:

public class AVLTreeDemo {
    
    
    public static void main(String[] args) {
    
    
//        int arr[] = {4,3,6,5,7,8};
        int arr[] = {
    
    10,12,8,9,7,6};
        //创建一个AVLTree对象
        AVLTree avlTree = new AVLTree();
        //添加结点
        for (int i = 0; i < arr.length; i++) {
    
    
            avlTree.add(new Node(arr[i]));
        }

        //遍历
        System.out.println("中序遍历");
        System.out.println("平衡处理后");
        avlTree.infixOrder();
        System.out.println("树的高度=" + avlTree.getRoot().height());//4
        System.out.println("树的左子树高度=" + avlTree.getRoot().leftHeight());//3
        System.out.println("树的左子树高度=" + avlTree.getRoot().rightHeight());//1
        System.out.println("当前的根结点=" + avlTree.getRoot());
		System.out.println("根节点的左子节点=" +avlTree.getRoot().left );//7
        System.out.println("根节点的右子节点=" +avlTree.getRoot().right );//10

    }
}

//创建AVLTree
class AVLTree {
    
    
    private Node root;

    public Node getRoot() {
    
    
        return root;
    }

    //查找要删除的结点
    public Node search(int value) {
    
    
        if (root == null) {
    
    
            return null;
        } else {
    
    
            return root.search(value);
        }
    }

    //查找父节点
    public Node searchParent(int value) {
    
    
        if (root == null) {
    
    
            return null;
        } else {
    
    
            return root.searchParent(value);
        }
    }

    /*
    编写方法
    1. 返回以node为根结点的,二叉排序树的最小结点的值。
    2. 删除以node为根结点的,二叉排序树的最小结点。
     */

    /**
     * 1.返回这个值
     * 2.删除这个值
     *
     * @param node 传入的结点(当作二叉排序树的根节点)
     * @return 返回的 以node为根结点的,二叉排序树的最小结点的值
     */
    public int delRightTreeMin(Node node) {
    
    
        Node target = node;
        //循环的查找左子节点,就会找到最小值
        while (target.left != null) {
    
     //当while循环结束后,target就指向以node为根节点的最小值结点
            target = target.left;
        }
        //这是target就指向了最小节点
        //删除最小结点
        delNode(target.value);
        return target.value;
    }

    //课后练习:从左子树找到最大的结点,然后删除节点
    public int delLeftTreeMax(Node node) {
    
    
        while (node.right != null) {
    
    
            node = node.right;
        }
        delNode(node.value);
        return node.value;
    }

    //删除结点
    public void delNode(int value) {
    
    
        if (root == null) {
    
    
            return;
        } else {
    
    
            //1.先找到要删除的结点 targetNode
            Node targetNode = search(value);
            //如果没有找到要删除的结点,下面不用走了。
            if (targetNode == null) {
    
    
                return;
            }
            //如果我们发现当前这棵二叉排序树 没有父节点,说明根结点就是我们要删除的结点,直接删除根结点即可
            if (root.left == null && root.right == null) {
    
    
                root = null;
                return;
            }
            //去找targetNode的父节点
            Node parent = searchParent(value);
            //第一种情况:如果要删除的结点是叶子结点
            if (targetNode.left == null && targetNode.right == null) {
    
    
                //判断targetNode是父节点的左子节点,还是右子结点。
                if (parent.left != null && parent.left.value == value) {
    
     //是左子节点
                    parent.left = null;
                } else if (parent.right != null && parent.right.value == value) {
    
     //是右子结点
                    parent.right = null;
                }
            } else if (targetNode.left != null && targetNode.right != null) {
    
     //删除有两棵子树的结点
                int minVal = delRightTreeMin(targetNode.right);
                //int maxVal = delLeftTreeMax(targetNode.left);
                targetNode.value = minVal;
            } else {
    
     //删除有一棵子树的结点
                //如果要删除的结点有左子节点
                if (targetNode.left != null) {
    
    
                    if (parent != null) {
    
    
                        //如果targetNode 是 parent 的左子节点
                        if (parent.left.value == value) {
    
    
                            parent.left = targetNode.left;
                        } else {
    
     //如果targetNode 是 parent 的右子节点
                            parent.right = targetNode.left;
                        }
                    } else {
    
    
                        root = targetNode.left;
                    }
                } else {
    
     //如果要删除的结点有右子节点
                    if (parent != null) {
    
    
                        //如果targetNode 是 parent 的左子节点
                        if (parent.right.value == value) {
    
    
                            parent.right = targetNode.right;
                        } else {
    
     //如果targetNode 是 parent 的右子节点
                            parent.left = targetNode.right;
                        }
                    } else {
    
    
                        root = targetNode.right;
                    }
                }
            }
        }
    }

    //添加结点的方法
    public void add(Node node) {
    
    
        if (root == null) {
    
    
            root = node;//如果root为空,则直接让root指向node
        } else {
    
    
            root.add(node);
        }
    }

    //中序遍历方法
    public void infixOrder() {
    
    
        if (root != null) {
    
    
            root.infixOrder();
        } else {
    
    
            System.out.println("该树为空,无法遍历!");
        }
    }


}

//创建Node结点
class Node {
    
    
    int value;
    Node left;
    Node right;

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

    //返回左子树的高度
    public int leftHeight() {
    
    
        if (left == null) {
    
    
            return 0;
        }
        return left.height();
    }

    //返回右子树的高度
    public int rightHeight() {
    
    
        if (right == null) {
    
    
            return 0;
        }
        return right.height();
    }

    //返回该结点为根结点的树的高度
    public int height() {
    
    
        return Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height()) + 1;
    }

    //左旋转方法
    private void leftRotate(){
    
    

        //1.创建新的结点,以当前根节点的值创建
        Node newNode = new Node(value);
        //2.把新结点的左子树设置为当前结点的左字节
        newNode.left = left;
        //3.把新的结点的右子树设置成当前结点的右子树的左子树
        newNode.right = right.left;
        //4.把当前结点的值换成右子结点的值
        value = right.value;
        //5.把当前结点的右子树设置成当前结点的右子树的右子树
        right = right.right;
        //6.把当前结点的左子树(左子结点)设置成新的结点
        left = newNode;
    }

    //右旋转
    private void rightRotate(){
    
    
        //1.创建一个以当前结点的值为值的结点
        Node newNode = new Node(value);
        //2.让新结点的右子结点指向右子结点
        newNode.right = right;
        //3.让新结点的左子结点指向左子节点的右子节点
        newNode.left = left.right;
        //4.将当前结点的值改为左子结点的值
        value = left.value;
        //5.让当前结点的左子节点指向当前结点的左子节点的左子节点
        left = left.left;
        //6.让当前结点的右子结点指向新结点
        right = newNode;

    }

    //查找要删除的结点

    /**
     * @param value 希望删除的结点的值
     * @return 如果找到返回该结点, 否则返回null
     */
    public Node search(int value) {
    
    
        if (value == this.value) {
    
    //找到
            return this;
        } else if (value < this.value) {
    
    //如果查找的值小于当前结点,向左子树递归查找
            if (this.left == null) {
    
    
                return null;
            }
            return this.left.search(value);
        } else {
    
    //如果查找的值大于等于当前结点,向右子树递归查找
            if (this.right == null) {
    
    
                return null;
            }
            return this.right.search(value);
        }
    }

    //查找要删除结点的父节点

    /**
     * @param value 要找的结点的值
     * @return 返回的是要删除的结点的父节点
     */
    public Node searchParent(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.searchParent(value);
            } else if (value >= this.value && this.right != null) {
    
    //如果查找的值大于当前结点的值,并且当前结点的右子节点不为空
                return this.right.searchParent(value);
            } else {
    
    
                return null;
            }
        }
    }

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

    //添加结点的方法
    //递归的形式添加结点,注意需要满足二叉排序树
    public void add(Node node) {
    
    
        if (node == null) {
    
    
            return;
        }

        //判断传入的node结点值,和当前子树的根结点的值关系
        if (node.value < this.value) {
    
    
            //如果当前结点的左子节点为null
            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);
            }
        }
        //当添加完一个结点后,如果:(右子树的高度 - 左子树的高度) > 1,左旋转
        if (rightHeight() - leftHeight() > 1){
    
    

            leftRotate();//左旋转

        }
        //当添加完一个接单后,如果:(左子树的高度 - 后字数的高度) > 1,右旋转
        if (leftHeight() - rightHeight() > 1){
    
    
            rightRotate();//右旋转
        }
    }

    //当添加完一个结点后,如果 (右子树的高度-左子树的高度) > 1

    //中序遍历
    public void infixOrder() {
    
    
        if (this.left != null) {
    
    
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null) {
    
    
            this.right.infixOrder();
        }
    }


}

此上右旋转考虑并不周全,完整的旋转代码参考下方双旋转。

AVL树双旋转图解和实现

应用案例-双旋转

  • 前面的两个数列,进行单旋转(即一次旋转)就可以将非平衡二叉树转成平衡二叉树,但是在某些情况下,单旋转不能完成平衡二叉树的转换。
  • 比如数列int[] arr = { 10, 11, 7, 6, 8, 9 }; 运行原来的代码可以看到,并没有转成AVL树.
  • int[]arr= {2,1,6,5,7,3}; // 运行原来的代码可以看到,并没有转成 AVL树

在这里插入图片描述

问题分析:
在满足右旋转条件时,要判断:

  • 如果 是 左子树的 右子树高度 大于左子树的左子树时:
    就是 对 当前根节点的左子树,先进行 左旋转

  • 然后, 再对当前根节点进行右旋转即可

  • 否则,直接对当前节点(根节点)进行右旋转.即可.

1.先对当前节点的左子树,进行左旋转
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  1. 再对当前节点,进行右旋转
    在这里插入图片描述
    在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
代码实现:

public class AVLTreeDemo {
    
    
   public static void main(String[] args) {
    
    
//        int arr[] = {4,3,6,5,7,8};
       int arr[] = {
    
    10, 11, 7, 6, 8, 9};
       //创建一个AVLTree对象
       AVLTree avlTree = new AVLTree();
       //添加结点
       for (int i = 0; i < arr.length; i++) {
    
    
           avlTree.add(new Node(arr[i]));
       }

       //遍历
       System.out.println("中序遍历");
       System.out.println("平衡处理后");
       avlTree.infixOrder();
       System.out.println("树的高度=" + avlTree.getRoot().height());//4
       System.out.println("树的左子树高度=" + avlTree.getRoot().leftHeight());//3
       System.out.println("树的左子树高度=" + avlTree.getRoot().rightHeight());//1
       System.out.println("当前的根结点=" + avlTree.getRoot());


   }
}

//创建AVLTree
class AVLTree {
    
    
   private Node root;

   public Node getRoot() {
    
    
       return root;
   }

   //查找要删除的结点
   public Node search(int value) {
    
    
       if (root == null) {
    
    
           return null;
       } else {
    
    
           return root.search(value);
       }
   }

   //查找父节点
   public Node searchParent(int value) {
    
    
       if (root == null) {
    
    
           return null;
       } else {
    
    
           return root.searchParent(value);
       }
   }

   /*
   编写方法
   1. 返回以node为根结点的,二叉排序树的最小结点的值。
   2. 删除以node为根结点的,二叉排序树的最小结点。
    */

   /**
    * 1.返回这个值
    * 2.删除这个值
    *
    * @param node 传入的结点(当作二叉排序树的根节点)
    * @return 返回的 以node为根结点的,二叉排序树的最小结点的值
    */
   public int delRightTreeMin(Node node) {
    
    
       Node target = node;
       //循环的查找左子节点,就会找到最小值
       while (target.left != null) {
    
     //当while循环结束后,target就指向以node为根节点的最小值结点
           target = target.left;
       }
       //这是target就指向了最小节点
       //删除最小结点
       delNode(target.value);
       return target.value;
   }

   //课后练习:从左子树找到最大的结点,然后删除节点
   public int delLeftTreeMax(Node node) {
    
    
       while (node.right != null) {
    
    
           node = node.right;
       }
       delNode(node.value);
       return node.value;
   }

   //删除结点
   public void delNode(int value) {
    
    
       if (root == null) {
    
    
           return;
       } else {
    
    
           //1.先找到要删除的结点 targetNode
           Node targetNode = search(value);
           //如果没有找到要删除的结点,下面不用走了。
           if (targetNode == null) {
    
    
               return;
           }
           //如果我们发现当前这棵二叉排序树 没有父节点,说明根结点就是我们要删除的结点,直接删除根结点即可
           if (root.left == null && root.right == null) {
    
    
               root = null;
               return;
           }
           //去找targetNode的父节点
           Node parent = searchParent(value);
           //第一种情况:如果要删除的结点是叶子结点
           if (targetNode.left == null && targetNode.right == null) {
    
    
               //判断targetNode是父节点的左子节点,还是右子结点。
               if (parent.left != null && parent.left.value == value) {
    
     //是左子节点
                   parent.left = null;
               } else if (parent.right != null && parent.right.value == value) {
    
     //是右子结点
                   parent.right = null;
               }
           } else if (targetNode.left != null && targetNode.right != null) {
    
     //删除有两棵子树的结点
               int minVal = delRightTreeMin(targetNode.right);
               //int maxVal = delLeftTreeMax(targetNode.left);
               targetNode.value = minVal;
           } else {
    
     //删除有一棵子树的结点
               //如果要删除的结点有左子节点
               if (targetNode.left != null) {
    
    
                   if (parent != null) {
    
    
                       //如果targetNode 是 parent 的左子节点
                       if (parent.left.value == value) {
    
    
                           parent.left = targetNode.left;
                       } else {
    
     //如果targetNode 是 parent 的右子节点
                           parent.right = targetNode.left;
                       }
                   } else {
    
    
                       root = targetNode.left;
                   }
               } else {
    
     //如果要删除的结点有右子节点
                   if (parent != null) {
    
    
                       //如果targetNode 是 parent 的左子节点
                       if (parent.right.value == value) {
    
    
                           parent.right = targetNode.right;
                       } else {
    
     //如果targetNode 是 parent 的右子节点
                           parent.left = targetNode.right;
                       }
                   } else {
    
    
                       root = targetNode.right;
                   }
               }
           }
       }
   }

   //添加结点的方法
   public void add(Node node) {
    
    
       if (root == null) {
    
    
           root = node;//如果root为空,则直接让root指向node
       } else {
    
    
           root.add(node);
       }
   }

   //中序遍历方法
   public void infixOrder() {
    
    
       if (root != null) {
    
    
           root.infixOrder();
       } else {
    
    
           System.out.println("该树为空,无法遍历!");
       }
   }


}

//创建Node结点
class Node {
    
    
   int value;
   Node left;
   Node right;

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

   //返回左子树的高度
   public int leftHeight() {
    
    
       if (left == null) {
    
    
           return 0;
       }
       return left.height();
   }

   //返回右子树的高度
   public int rightHeight() {
    
    
       if (right == null) {
    
    
           return 0;
       }
       return right.height();
   }

   //返回该结点为根结点的树的高度
   public int height() {
    
    
       return Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height()) + 1;
   }

   //左旋转方法
   private void leftRotate() {
    
    

       //1.创建新的结点,以当前根节点的值创建
       Node newNode = new Node(value);
       //2.把新结点的左子树设置为当前结点的左字节
       newNode.left = left;
       //3.把新的结点的右子树设置成当前结点的右子树的左子树
       newNode.right = right.left;
       //4.把当前结点的值换成右子结点的值
       value = right.value;
       //5.把当前结点的右子树设置成当前结点的右子树的右子树
       right = right.right;
       //6.把当前结点的左子树(左子结点)设置成新的结点
       left = newNode;
   }

   //右旋转
   private void rightRotate() {
    
    
       //1.创建一个以当前结点的值为值的结点
       Node newNode = new Node(value);
       //2.让新结点的右子结点指向右子结点
       newNode.right = right;
       //3.让新结点的左子结点指向左子节点的右子节点
       newNode.left = left.right;
       //4.将当前结点的值改为左子结点的值
       value = left.value;
       //5.让当前结点的左子节点指向当前结点的左子节点的左子节点
       left = left.left;
       //6.让当前结点的右子结点指向新结点
       right = newNode;

   }

   //查找要删除的结点

   /**
    * @param value 希望删除的结点的值
    * @return 如果找到返回该结点, 否则返回null
    */
   public Node search(int value) {
    
    
       if (value == this.value) {
    
    //找到
           return this;
       } else if (value < this.value) {
    
    //如果查找的值小于当前结点,向左子树递归查找
           if (this.left == null) {
    
    
               return null;
           }
           return this.left.search(value);
       } else {
    
    //如果查找的值大于等于当前结点,向右子树递归查找
           if (this.right == null) {
    
    
               return null;
           }
           return this.right.search(value);
       }
   }

   //查找要删除结点的父节点

   /**
    * @param value 要找的结点的值
    * @return 返回的是要删除的结点的父节点
    */
   public Node searchParent(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.searchParent(value);
           } else if (value >= this.value && this.right != null) {
    
    //如果查找的值大于当前结点的值,并且当前结点的右子节点不为空
               return this.right.searchParent(value);
           } else {
    
    
               return null;
           }
       }
   }

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

   //添加结点的方法
   //递归的形式添加结点,注意需要满足二叉排序树
   public void add(Node node) {
    
    
       if (node == null) {
    
    
           return;
       }

       //判断传入的node结点值,和当前子树的根结点的值关系
       if (node.value < this.value) {
    
    
           //如果当前结点的左子节点为null
           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);
           }
       }
       //当添加完一个结点后,如果:(右子树的高度 - 左子树的高度) > 1,左旋转
       if (rightHeight() - leftHeight() > 1) {
    
    
           //如果它的右子树的左子树高度>它的右子树的右子树高度
           if (right != null && right.leftHeight() > right.rightHeight()){
    
    
               //先进行右子树的右旋转
               right.rightRotate();
               //再进行当前结点的左旋转
               leftRotate();
           }else {
    
    
               leftRotate();
           }
           //!!此处注意,处理完一个马上return,不然它还会继续往下走,会很危险
           return;//必须要!!!->已经处理完,已经平衡,没必要往下走,可能会有问题。
       }
       //当添加完一个接单后,如果:(左子树的高度 - 后字数的高度) > 1,右旋转
       if (leftHeight() - rightHeight() > 1) {
    
    
           //如果它的左子树的右子树高度大于它的左子树的左子树高度
           if (left != null && left.rightHeight() > left.leftHeight()){
    
    
               //先对当前结点的左节点(左子树)->左旋转
               left.leftRotate();//!!一定是当前结点的 *左节点* 进行左旋转
               //再对当前结点进行右旋转
               rightRotate();
           }else {
    
    
               //直接进行右旋转即可
               rightRotate();
           }


       }
   }

   //当添加完一个结点后,如果 (右子树的高度-左子树的高度) > 1

   //中序遍历
   public void infixOrder() {
    
    
       if (this.left != null) {
    
    
           this.left.infixOrder();
       }
       System.out.println(this);
       if (this.right != null) {
    
    
           this.right.infixOrder();
       }
   }


}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/SwaeLeeUknow/article/details/109020542
今日推荐