天天看点

一篇搞懂二叉排序树与平衡二叉树

文章目录

  • ​​思维导图​​
  • ​​1.二叉排序树​​
  • ​​1.1 二叉排序树介绍​​
  • ​​1.2 二叉排序树(BST)的创建和遍历​​
  • ​​1.3 二叉排序树删除结点思路图解​​
  • ​​1.3.1 二叉排序树删除叶子结点​​
  • ​​1.3.2 二叉排序树删除只有一棵子树的节点​​
  • ​​1.3.3 二叉排序树删除有二棵子树的结点​​
  • ​​2.平衡二叉树(AVL树)​​
  • ​​2.1 平衡二叉树介绍​​
  • ​​2.2 AVL树左旋转思路图解​​
  • ​​2.3 AVL树高度求解​​
  • ​​2.4 AVL树左旋转代码实现​​
  • ​​2.5 AVL树右旋转思路图解​​
  • ​​2.5 AVL树右旋转代码实现​​
  • ​​2.6 AVL树双旋转思路图解​​
  • ​​2.7 AVL树双旋转代码实现​​

思维导图

一篇搞懂二叉排序树与平衡二叉树

1.二叉排序树

需求:给你一个数列 (7, 3, 10, 12, 5, 1, 9),要求能够高效的完成对数据的查询和添加。

解决方案分析:

  • 使用数组
  • 数组未排序, 优点:​

    ​直接在数组尾添加​

    ​​,速度快。 缺点:​

    ​查找速度慢​

  • 数组排序,优点:可以使用二分查找,查找速度快,缺点:为了保证数组有序,在添加新数据时,找到插入位置后,​

    ​后面的数据需整体移动,速度慢​

    ​。
  • 使用链式存储-链表
  • 不管链表是否有序,​

    ​查找速度都慢​

    ​,添加数据速度比数组快,不需要数据整体移动。
  • ​使用二叉排序树(接下来详细介绍)​

1.1 二叉排序树介绍

  • 二叉排序树:​

    ​BST (Binary Sort(Search) Tree)​

    ​​, 对于二叉排序树的任何一个​

    ​非叶子节点​

    ​​,​

    ​要求左子节点的值比当前节点的值小,右子节点的值比当前节点的值大​

    ​。
  • ​特别说明​

    ​:如果有相同的值,可以将该节点放在左子节点或右子节点
  • 比如针对前面的数据 (7, 3, 10, 12, 5, 1, 9) ,对应的二叉排序树为:
一篇搞懂二叉排序树与平衡二叉树

1.2 二叉排序树(BST)的创建和遍历

一个数组创建成对应的二叉排序树,并使用​

​中序遍历二叉排序树​

​,比如: 数组为 Array (7, 3, 10, 12, 5, 1, 9) ,创建成对应的二叉排序树为 :
一篇搞懂二叉排序树与平衡二叉树
/**
 * @author xiexu
 * @create 2020-11-18 11:40 下午
 */
public class BinarySortTreeDemo {

    public static void main(String[] args) {
        int arr[] = {7, 3, 10, 12, 5, 1, 9};
        BinarySortTree binarySortTree = new BinarySortTree();
        //循环的添加结点到二叉排序树
        for (int i = 0; i < arr.length; i++) {
            binarySortTree.add(new Node(arr[i]));
        }

        //中序遍历二叉排序树
        System.out.println("中序遍历此树:");
        binarySortTree.infixOrder();    //1,3,5,7,9,10,12
    }
}

//创建二叉排序树
class BinarySortTree {
    Node root;

    //添加节点的方法
    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 void add(Node node) {
        if (node == null) {
            return;
        }
        //判断传入的节点的值,和当前子树的根节点的值的关系
        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();
        }
    }

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

1.3 二叉排序树删除结点思路图解

一篇搞懂二叉排序树与平衡二叉树

二叉排序树的删除情况比较复杂,有下面​

​三种情况​

​需要考虑

  • 删除叶子节点 (比如:2, 5, 9, 12)
  • 删除只有一颗子树的节点 (比如:1)
  • 删除有两颗子树的节点. (比如:7, 3,10 )

1.3.1 二叉排序树删除叶子结点

一篇搞懂二叉排序树与平衡二叉树
第一种情况: 删除叶子节点 (比如:2, 5, 9, 12)
思路
(1) 需要先找到要删除的结点  targetNode
(2) 找到targetNode 的 父结点 parent 
(3) 确定 targetNode 是 parent的左子结点 还是右子结点
(4) 根据前面的情况来对应删除
  左子结点 parent.left = null
  右子结点 parent.right = null;      
代码实现
/**
 * @author xiexu
 * @create 2020-11-18 11:40 下午
 */
public class BinarySortTreeDemo {

    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]));
        }

        //中序遍历二叉排序树
        System.out.println("中序遍历此树:");
        binarySortTree.infixOrder();   //1,2,3,5,7,9,10,12

        //测试一下删除叶子节点
        binarySortTree.delNode(2);
        binarySortTree.delNode(5);
        binarySortTree.delNode(9);
        binarySortTree.delNode(1);
        System.out.println("删除节点后:");
        binarySortTree.infixOrder();
    }
}

//创建二叉排序树
class BinarySortTree {
    Node 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);
        }
    }

    //删除节点
    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 的父结点 parent
            Node parent = searchParent(value);
            //如果要删除的节点为叶子节点
            if (targetNode.left == null && targetNode.right == null) {
                //判断 targetNode 是 parent的左子结点 还是右子结点
                if (parent.left != null && parent.left.value == targetNode.value) { //左子节点
                    parent.left = null;
                } else if (parent.right != null && parent.right.value == targetNode.value) { //右子节点
                    parent.right = null;
                }
            }

        }
    }

    //添加节点的方法
    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 void add(Node node) {
        if (node == null) {
            return;
        }
        //判断传入的节点的值,和当前子树的根节点的值的关系
        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();
        }
    }

    //查找要删除的节点
    /**
     * @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 返回的是要删除的结点的父结点, 如果没有就返回null
     */
    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 +
                '}';
    }
}      

1.3.2 二叉排序树删除只有一棵子树的节点

一篇搞懂二叉排序树与平衡二叉树
第二种情况: 删除只有一棵子树的节点 比如 1 这个节点
思路
(1) 需要先找到要删除的结点  targetNode
(2) 找到targetNode 的 父结点 parent 
(3) 确定targetNode 的子结点是左子结点还是右子结点
(4) targetNode 是 parent 的左子结点还是右子结点
(5) 如果targetNode 有左子结点
  5.1 如果 targetNode 是 parent 的左子结点
    parent.left = targetNode.left;
  5.2 如果 targetNode 是 parent 的右子结点
    parent.right = targetNode.left;
(6) 如果targetNode 有右子结点
  6.1 如果 targetNode 是 parent 的左子结点
    parent.left = targetNode.right;
  6.2 如果 targetNode 是 parent 的右子结点
    parent.right = targetNode.right      
代码实现
/**
 * @author xiexu
 * @create 2020-11-18 11:40 下午
 */
public class BinarySortTreeDemo {

    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]));
        }

        //中序遍历二叉排序树
        System.out.println("中序遍历此树:");
        binarySortTree.infixOrder();   //1,2,3,5,7,9,10,12

        //测试一下删除叶子节点
        binarySortTree.delNode(1);
        System.out.println("删除节点后:");
        binarySortTree.infixOrder();
    }
}

//创建二叉排序树
class BinarySortTree {
    Node 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);
        }
    }

    //删除节点
    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 的父结点 parent
            Node parent = searchParent(value);
            //如果要删除的节点为叶子节点
            if (targetNode.left == null && targetNode.right == null) {
                //判断 targetNode 是 parent的左子结点 还是右子结点
                if (parent.left != null && parent.left.value == targetNode.value) { //左子节点
                    parent.left = null;
                } else if (parent.right != null && parent.right.value == targetNode.value) { //右子节点
                    parent.right = null;
                }
            }else if (targetNode.left != null && targetNode.right != null) { //删除有两棵子树的节点

            }else { //删除只有一棵子树的节点
                //如果要删除的结点有左子结点
                if (targetNode.left != null) {
                    //如果 targetNode 是 parent 的左子结点
                    if (parent.left.value == targetNode.value) {
                        parent.left = targetNode.left;
                    }else { //如果 targetNode 是 parent 的右子结点
                        parent.right = targetNode.left;
                    }
                }else { //如果targetNode 有右子结点
                    //如果 targetNode 是 parent 的左子结点
                    if (parent.left.value == targetNode.value) {
                        parent.left = targetNode.right;
                    }else { //如果 targetNode 是 parent 的右子结点
                        parent.right = 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 void add(Node node) {
        if (node == null) {
            return;
        }
        //判断传入的节点的值,和当前子树的根节点的值的关系
        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();
        }
    }

    //查找要删除的节点
    /**
     * @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 返回的是要删除的结点的父结点, 如果没有就返回null
     */
    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 +
                '}';
    }
}      

1.3.3 二叉排序树删除有二棵子树的结点

一篇搞懂二叉排序树与平衡二叉树
第三种情况: 删除有两棵子树的节点. (比如:7, 3,10)
思路
(1) 需求先去找到要删除的结点  targetNode,这里以节点"10"为例
(2) 找到targetNode 的 父结点 parent 
(3) 从targetNode 的右子树找到最小的结点,也就是"12"这个节点
(4) 用一个临时变量,将 最小结点的值保存 temp = 12
(5) 删除该最小结点,也就是删除"12"这个节点
(6) targetNode.value =      
代码实现
/**
 * @author xiexu
 * @create 2020-11-18 11:40 下午
 */
public class BinarySortTreeDemo {

    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]));
        }

        //中序遍历二叉排序树
        System.out.println("中序遍历此树:");
        binarySortTree.infixOrder();   //1,2,3,5,7,9,10,12

        //测试一下删除叶子节点
        binarySortTree.delNode(7);
        binarySortTree.delNode(3);
        binarySortTree.delNode(10);
        System.out.println("删除节点后:");
        binarySortTree.infixOrder();
    }
}

//创建二叉排序树
class BinarySortTree {
    Node 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为根节点的二叉排序树的右子树的左子节点
    /**
     * @param node 传入的节点(为二叉排序树的根节点)
     * @return 返回的是以node为根节点的二叉排序树的右子树的左子节点(最小节点的值)
     */
    public int delRightTreeMin(Node node) {
        Node target = node;
        //循环的查找左子节点,就会找到最小值
        while (target.left != null) {
            target = target.left;
        }
        //这是target就指向了最小节点
        //删除最小节点
        delNode(target.value);
        return target.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 的父结点 parent
            Node parent = searchParent(value);
            //如果要删除的节点为叶子节点
            if (targetNode.left == null && targetNode.right == null) {
                //判断 targetNode 是 parent的左子结点 还是右子结点
                if (parent.left != null && parent.left.value == targetNode.value) { //左子节点
                    parent.left = null;
                } else if (parent.right != null && parent.right.value == targetNode.value) { //右子节点
                    parent.right = null;
                }
            } else if (targetNode.left != null && targetNode.right != null) { //删除有两棵子树的节点
                int minValue = delRightTreeMin(targetNode.right);
                targetNode.value = minValue;
            } else { //删除只有一棵子树的节点
                //如果要删除的结点有左子结点
                if (targetNode.left != null) {
                    if (parent != null) { //如果targetNode的父节点不为空
                        //如果 targetNode 是 parent 的左子结点
                        if (parent.left.value == targetNode.value) {
                            parent.left = targetNode.left;
                        } else { //如果 targetNode 是 parent 的右子结点
                            parent.right = targetNode.left;
                        }
                    } else { //如果父节点为空
                        root = targetNode.left;
                    }
                } else { //如果targetNode 有右子结点
                    if (parent != null) { //如果targetNode的父节点不为空
                        //如果 targetNode 是 parent 的左子结点
                        if (parent.left.value == targetNode.value) {
                            parent.left = targetNode.right;
                        } else { //如果 targetNode 是 parent 的右子结点
                            parent.right = 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 void add(Node node) {
        if (node == null) {
            return;
        }
        //判断传入的节点的值,和当前子树的根节点的值的关系
        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();
        }
    }

    //查找要删除的节点
    /**
     * @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 返回的是要删除的结点的父结点, 如果没有就返回null
     */
    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 +
                '}';
    }
}      

2.平衡二叉树(AVL树)

看一个案例(说明二叉排序树可能的问题)
  • 给你一个数列{1,2,3,4,5,6},要求创建一颗二叉排序树(BST),并分析问题所在。
一篇搞懂二叉排序树与平衡二叉树
上图二叉排序树存在的问题
  • 左子树全部为空,从形式上看,更像一个单链表.
  • 插入速度没有影响
  • 查询速度明显降低(因为需要依次比较), 不能发挥BST

    的优势,因为每次还需要比较左子树,其查询速度比

    单链表还慢

  • 解决方案 ->​

    ​平衡二叉树(AVL)​

2.1 平衡二叉树介绍

  • 平衡二叉树也叫​

    ​平衡二叉排序树​

    ​​(Self-balancing binary search tree)又被称为AVL树, 可以​

    ​保证查询效率较高​

    ​。
  • 具有以下​

    ​特点​

    ​​:它是一 棵​

    ​空树​

    ​​或它的​

    ​左右两个子树的高度差的绝对值不超过1​

    ​​,并且左右两个子树都是一棵平衡二叉树。平衡二叉树的常用实现方法有​

    ​红黑树​

    ​​、​

    ​AVL(算法)​

    ​​、​

    ​替罪羊树​

    ​​、​

    ​Treap​

    ​​、​

    ​伸展树​

    ​等。
  • 举例说明, 看看下面哪些是AVL树, 为什么?
一篇搞懂二叉排序树与平衡二叉树

2.2 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; 
//把当前节点的右子树 设置成 当前节点的右子树的右子树      
一篇搞懂二叉排序树与平衡二叉树
5. right=right.right;
//把当前节点的左子树 设置为 新节点
6. left=newLeft;      
一篇搞懂二叉排序树与平衡二叉树
左旋动态图
一篇搞懂二叉排序树与平衡二叉树

2.3 AVL树高度求解

/**
 * @author xiexu
 * @create 2020-11-19 5:32 下午
 */
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();    //3,4,5,6,7,8

        System.out.println("未经过平衡处理的树:");
        System.out.println("树的高度:" + avlTree.root.height());   //4
        System.out.println("树的左子树高度:" + avlTree.root.leftHeight()); // 1
        System.out.println("树的右子树高度:" + avlTree.root.rightHeight()); // 3
    }
}

//创建AVL树
class AVLTree {
    Node 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为根节点的二叉排序树的右子树的左子节点

    /**
     * @param node 传入的节点(为二叉排序树的根节点)
     * @return 返回的是以node为根节点的二叉排序树的右子树的左子节点(最小节点的值)
     */
    public int delRightTreeMin(Node node) {
        Node target = node;
        //循环的查找左子节点,就会找到最小值
        while (target.left != null) {
            target = target.left;
        }
        //这是target就指向了最小节点
        //删除最小节点
        delNode(target.value);
        return target.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 的父结点 parent
            Node parent = searchParent(value);
            //如果要删除的节点为叶子节点
            if (targetNode.left == null && targetNode.right == null) {
                //判断 targetNode 是 parent的左子结点 还是右子结点
                if (parent.left != null && parent.left.value == targetNode.value) { //左子节点
                    parent.left = null;
                } else if (parent.right != null && parent.right.value == targetNode.value) { //右子节点
                    parent.right = null;
                }
            } else if (targetNode.left != null && targetNode.right != null) { //删除有两棵子树的节点
                int minValue = delRightTreeMin(targetNode.right);
                targetNode.value = minValue;
            } else { //删除只有一棵子树的节点
                //如果要删除的结点有左子结点
                if (targetNode.left != null) {
                    if (parent != null) { //如果targetNode的父节点不为空
                        //如果 targetNode 是 parent 的左子结点
                        if (parent.left.value == targetNode.value) {
                            parent.left = targetNode.left;
                        } else { //如果 targetNode 是 parent 的右子结点
                            parent.right = targetNode.left;
                        }
                    } else { //如果父节点为空
                        root = targetNode.left;
                    }
                } else { //如果targetNode 有右子结点
                    if (parent != null) { //如果targetNode的父节点不为空
                        //如果 targetNode 是 parent 的左子结点
                        if (parent.left.value == targetNode.value) {
                            parent.left = targetNode.right;
                        } else { //如果 targetNode 是 parent 的右子结点
                            parent.right = 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 height() {
        return Math.max(this.left == null ? 0 : this.left.height(), this.right == null ? 0 : this.right.height()) + 1;
    }

    //height()方法的拆分版,便于理解
    public int height2(Node node) {
        int hLeft, hRight, maxH;
        if (node != null) {
            hLeft = height2(node.left);
            hRight = height2(node.right);
            maxH = hLeft > hRight ? hLeft : hRight;
            return maxH + 1;
        } else {
            return 0;
        }
    }

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

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

    //添加节点的方法
    //递归的形式添加结点,注意需要满足二叉排序树的要求
    public void add(Node node) {
        if (node == null) {
            return;
        }
        //判断传入的节点的值,和当前子树的根节点的值的关系
        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();
        }
    }

    //查找要删除的节点

    /**
     * @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 返回的是要删除的结点的父结点, 如果没有就返回null
     */
    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 +
                '}';
    }
}      

2.4 AVL树左旋转代码实现

/**
 * @author xiexu
 * @create 2020-11-19 5:32 下午
 */
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();    //3,4,5,6,7,8

        System.out.println("经过平衡处理的树:");
        System.out.println("树的高度:" + avlTree.root.height());   //3
        System.out.println("树的左子树高度:" + avlTree.root.leftHeight()); // 2
        System.out.println("树的右子树高度:" + avlTree.root.rightHeight()); // 2
    }
}

//创建AVL树
class AVLTree {
    Node 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为根节点的二叉排序树的右子树的左子节点

    /**
     * @param node 传入的节点(为二叉排序树的根节点)
     * @return 返回的是以node为根节点的二叉排序树的右子树的左子节点(最小节点的值)
     */
    public int delRightTreeMin(Node node) {
        Node target = node;
        //循环的查找左子节点,就会找到最小值
        while (target.left != null) {
            target = target.left;
        }
        //这是target就指向了最小节点
        //删除最小节点
        delNode(target.value);
        return target.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 的父结点 parent
            Node parent = searchParent(value);
            //如果要删除的节点为叶子节点
            if (targetNode.left == null && targetNode.right == null) {
                //判断 targetNode 是 parent的左子结点 还是右子结点
                if (parent.left != null && parent.left.value == targetNode.value) { //左子节点
                    parent.left = null;
                } else if (parent.right != null && parent.right.value == targetNode.value) { //右子节点
                    parent.right = null;
                }
            } else if (targetNode.left != null && targetNode.right != null) { //删除有两棵子树的节点
                int minValue = delRightTreeMin(targetNode.right);
                targetNode.value = minValue;
            } else { //删除只有一棵子树的节点
                //如果要删除的结点有左子结点
                if (targetNode.left != null) {
                    if (parent != null) { //如果targetNode的父节点不为空
                        //如果 targetNode 是 parent 的左子结点
                        if (parent.left.value == targetNode.value) {
                            parent.left = targetNode.left;
                        } else { //如果 targetNode 是 parent 的右子结点
                            parent.right = targetNode.left;
                        }
                    } else { //如果父节点为空
                        root = targetNode.left;
                    }
                } else { //如果targetNode 有右子结点
                    if (parent != null) { //如果targetNode的父节点不为空
                        //如果 targetNode 是 parent 的左子结点
                        if (parent.left.value == targetNode.value) {
                            parent.left = targetNode.right;
                        } else { //如果 targetNode 是 parent 的右子结点
                            parent.right = 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 height() {
        return Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height()) + 1;
    }

    //height()方法的拆分版,便于理解
    public int height2(Node node) {
        int hLeft, hRight, maxH;
        if (node != null) {
            hLeft = height2(node.left);
            hRight = height2(node.right);
            maxH = hLeft > hRight ? hLeft : hRight;
            return maxH + 1;
        } else {
            return 0;
        }
    }

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

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

    //添加节点的方法
    //递归的形式添加结点,注意需要满足二叉排序树的要求
    public void add(Node node) {
        if (node == null) {
            return;
        }
        //判断传入的节点的值,和当前子树的根节点的值的关系
        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(); //左旋转
        }
    }

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

    //左旋转方法
    public void leftRotate() {
        //创建新的节点,值等于当前根节点的值
        Node newNode = new Node(value);
        //把新节点的左子树设置为当前节点的左子树
        newNode.left = left;
        //把新节点的右子树设置为当前节点的右子树的左子树
        newNode.right = right.left;
        //把当前节点的值 替换为 当前节点的右子节点的值
        value = right.value;
        //把当前节点的右子树 设置成 当前节点的右子树的右子树
        right = right.right;
        //把当前节点的左子树 设置为 新节点
        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 返回的是要删除的结点的父结点, 如果没有就返回null
     */
    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 +
                '}';
    }
}      

2.5 AVL树右旋转思路图解

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

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

2.思路分析(示意图)

一篇搞懂二叉排序树与平衡二叉树
问题:当插入6时
leftHeight()  - rightHeight()  > 1 成立,此时,不再是一棵AVL树了.

怎么处理 --> 进行右旋转[目的就是降低左子树的高度]      
1.创建一个新的节点 newNode (以10这个值创建), 值等于当前根节点的值.

//把新节点的右子树 设置为 当前节点的右子树      
一篇搞懂二叉排序树与平衡二叉树
2. newNode.right = right 
//把新节点的左子树 设置为 当前节点的左子树的右子树      
一篇搞懂二叉排序树与平衡二叉树
3. newNode.left = left.right;
//把当前节点的值 替换为 左子节点的值      
一篇搞懂二叉排序树与平衡二叉树
4.value = left.value; 
//把当前节点的左子树 设置成 左子树的左子树      
一篇搞懂二叉排序树与平衡二叉树
5. left = left.left;
//把当前节点的右子树 设置为 新节点
6. right = newLeft;      
一篇搞懂二叉排序树与平衡二叉树
右旋动态图
一篇搞懂二叉排序树与平衡二叉树

2.5 AVL树右旋转代码实现

/**
 * @author xiexu
 * @create 2020-11-19 5:32 下午
 */
public class AVLTreeDemo {
    public static void main(String[] args) {
        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("中序遍历:");
        avlTree.infixOrder();    //6,7,8,9,10,12

        System.out.println("经过平衡处理的树:");
        System.out.println("树的高度:" + avlTree.root.height());   //3
        System.out.println("树的左子树高度:" + avlTree.root.leftHeight()); // 2
        System.out.println("树的右子树高度:" + avlTree.root.rightHeight()); // 2
        System.out.println("当前的根节点:" + avlTree.root); //8
    }
}

//创建AVL树
class AVLTree {
    Node 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为根节点的二叉排序树的右子树的左子节点
    /**
     * @param node 传入的节点(为二叉排序树的根节点)
     * @return 返回的是以node为根节点的二叉排序树的右子树的左子节点(最小节点的值)
     */
    public int delRightTreeMin(Node node) {
        Node target = node;
        //循环的查找左子节点,就会找到最小值
        while (target.left != null) {
            target = target.left;
        }
        //这是target就指向了最小节点
        //删除最小节点
        delNode(target.value);
        return target.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 的父结点 parent
            Node parent = searchParent(value);
            //如果要删除的节点为叶子节点
            if (targetNode.left == null && targetNode.right == null) {
                //判断 targetNode 是 parent的左子结点 还是右子结点
                if (parent.left != null && parent.left.value == targetNode.value) { //左子节点
                    parent.left = null;
                } else if (parent.right != null && parent.right.value == targetNode.value) { //右子节点
                    parent.right = null;
                }
            } else if (targetNode.left != null && targetNode.right != null) { //删除有两棵子树的节点
                int minValue = delRightTreeMin(targetNode.right);
                targetNode.value = minValue;
            } else { //删除只有一棵子树的节点
                //如果要删除的结点有左子结点
                if (targetNode.left != null) {
                    if (parent != null) { //如果targetNode的父节点不为空
                        //如果 targetNode 是 parent 的左子结点
                        if (parent.left.value == targetNode.value) {
                            parent.left = targetNode.left;
                        } else { //如果 targetNode 是 parent 的右子结点
                            parent.right = targetNode.left;
                        }
                    } else { //如果父节点为空
                        root = targetNode.left;
                    }
                } else { //如果targetNode 有右子结点
                    if (parent != null) { //如果targetNode的父节点不为空
                        //如果 targetNode 是 parent 的左子结点
                        if (parent.left.value == targetNode.value) {
                            parent.left = targetNode.right;
                        } else { //如果 targetNode 是 parent 的右子结点
                            parent.right = 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 height() {
        return Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height()) + 1;
    }

    //height()方法的拆分版,便于理解
    public int height2(Node node) {
        int hLeft, hRight, maxH;
        if (node != null) {
            hLeft = height2(node.left);
            hRight = height2(node.right);
            maxH = hLeft > hRight ? hLeft : hRight;
            return maxH + 1;
        } else {
            return 0;
        }
    }

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

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

    //添加节点的方法
    //递归的形式添加结点,注意需要满足二叉排序树的要求
    public void add(Node node) {
        if (node == null) {
            return;
        }
        //判断传入的节点的值,和当前子树的根节点的值的关系
        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(); //右旋转
        }
    }

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

    //左旋转方法
    public void leftRotate() {
        //创建新的节点,值等于当前根节点的值
        Node newNode = new Node(value);
        //把新节点的左子树设置为当前节点的左子树
        newNode.left = left;
        //把新节点的右子树设置为当前节点的右子树的左子树
        newNode.right = right.left;
        //把当前节点的值 替换为 当前节点的右子节点的值
        value = right.value;
        //把当前节点的右子树 设置成 当前节点的右子树的右子树
        right = right.right;
        //把当前节点的左子树 设置为 新节点
        left = newNode;
    }

    //右旋转方法
    public void rightRotate() {
        //创建新的节点,值等于当前根节点的值
        Node newNode = new Node(value);
        //把新节点的右子树 设置为 当前节点的右子树
        newNode.right = right;
        //把新节点的左子树 设置为 当前节点的左子树的右子树
        newNode.left = left.right;
        //把当前节点的值 替换为 左子节点的值
        value = left.value;
        //把当前节点的左子树 设置成 左子树的左子树
        left = left.left;
        //把当前节点的右子树 设置为 新节点
        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 返回的是要删除的结点的父结点, 如果没有就返回null
     */
    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 +
                '}';
    }
}      

2.6 AVL树双旋转思路图解

应用案例 - 双旋转

前面的两个数列,进行单旋转(即一次旋转) 就可以将非平衡二叉树转成平衡二叉树, 但是在某些情况下,单旋转不能完成平衡二叉树的转换。比如数列

int[ ] arr = { 10, 11, 7, 6, 8, 9 }; // 运行原来的代码可以看到,并没有转成AVL树

int[ ]arr= {2,1,6,5,7,3}; // 运行原来的代码可以看到,并没有转成 AVL树

一篇搞懂二叉排序树与平衡二叉树
问题分析:  
在满足右旋转条件时,要判断:
(1)如果它的左子树的右子树高度(高度为2) 大于 它的左子树的左子树高度(高度为1)
(2)就先对当前根节点的左子树,先进行左旋转,目的就是降低左子树的右子树的高度
(3)然后,再对当前根节点进行右旋转即可

否则,直接对当前节点(根节点)进行右旋转.      
具体步骤
1.先对当前节点的左子树,进行左旋转      
一篇搞懂二叉排序树与平衡二叉树
一篇搞懂二叉排序树与平衡二叉树
一篇搞懂二叉排序树与平衡二叉树
一篇搞懂二叉排序树与平衡二叉树
2.再对当前根节点,进行右旋转      
一篇搞懂二叉排序树与平衡二叉树
一篇搞懂二叉排序树与平衡二叉树
一篇搞懂二叉排序树与平衡二叉树

2.7 AVL树双旋转代码实现

/**
 * @author xiexu
 * @create 2020-11-19 5:32 下午
 */
public class AVLTreeDemo {
    public static void main(String[] args) {
        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("中序遍历:");
        avlTree.infixOrder();    //6,7,8,9,10,11

        System.out.println("经过平衡处理的树:");
        System.out.println("树的高度:" + avlTree.root.height());   //3
        System.out.println("树的左子树高度:" + avlTree.root.leftHeight()); // 2
        System.out.println("树的右子树高度:" + avlTree.root.rightHeight()); // 2
        System.out.println("当前的根节点:" + avlTree.root); //8
    }
}

//创建AVL树
class AVLTree {
    Node 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为根节点的二叉排序树的右子树的左子节点
    /**
     * @param node 传入的节点(为二叉排序树的根节点)
     * @return 返回的是以node为根节点的二叉排序树的右子树的左子节点(最小节点的值)
     */
    public int delRightTreeMin(Node node) {
        Node target = node;
        //循环的查找左子节点,就会找到最小值
        while (target.left != null) {
            target = target.left;
        }
        //这是target就指向了最小节点
        //删除最小节点
        delNode(target.value);
        return target.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 的父结点 parent
            Node parent = searchParent(value);
            //如果要删除的节点为叶子节点
            if (targetNode.left == null && targetNode.right == null) {
                //判断 targetNode 是 parent的左子结点 还是右子结点
                if (parent.left != null && parent.left.value == targetNode.value) { //左子节点
                    parent.left = null;
                } else if (parent.right != null && parent.right.value == targetNode.value) { //右子节点
                    parent.right = null;
                }
            } else if (targetNode.left != null && targetNode.right != null) { //删除有两棵子树的节点
                int minValue = delRightTreeMin(targetNode.right);
                targetNode.value = minValue;
            } else { //删除只有一棵子树的节点
                //如果要删除的结点有左子结点
                if (targetNode.left != null) {
                    if (parent != null) { //如果targetNode的父节点不为空
                        //如果 targetNode 是 parent 的左子结点
                        if (parent.left.value == targetNode.value) {
                            parent.left = targetNode.left;
                        } else { //如果 targetNode 是 parent 的右子结点
                            parent.right = targetNode.left;
                        }
                    } else { //如果父节点为空
                        root = targetNode.left;
                    }
                } else { //如果targetNode 有右子结点
                    if (parent != null) { //如果targetNode的父节点不为空
                        //如果 targetNode 是 parent 的左子结点
                        if (parent.left.value == targetNode.value) {
                            parent.left = targetNode.right;
                        } else { //如果 targetNode 是 parent 的右子结点
                            parent.right = 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 height() {
        return Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height()) + 1;
    }

    //height()方法的拆分版,便于理解
    public int height2(Node node) {
        int hLeft, hRight, maxH;
        if (node != null) {
            hLeft = height2(node.left);
            hRight = height2(node.right);
            maxH = hLeft > hRight ? hLeft : hRight;
            return maxH + 1;
        } else {
            return 0;
        }
    }

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

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

    //添加节点的方法
    //递归的形式添加结点,注意需要满足二叉排序树的要求
    public void add(Node node) {
        if (node == null) {
            return;
        }
        //判断传入的节点的值,和当前子树的根节点的值的关系
        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();
            }
        }
    }

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

    //左旋转方法
    public void leftRotate() {
        //创建新的节点,值等于当前根节点的值
        Node newNode = new Node(value);
        //把新节点的左子树设置为当前节点的左子树
        newNode.left = left;
        //把新节点的右子树设置为当前节点的右子树的左子树
        newNode.right = right.left;
        //把当前节点的值 替换为 当前节点的右子节点的值
        value = right.value;
        //把当前节点的右子树 设置成 当前节点的右子树的右子树
        right = right.right;
        //把当前节点的左子树 设置为 新节点
        left = newNode;
    }

    //右旋转方法
    public void rightRotate() {
        //创建新的节点,值等于当前根节点的值
        Node newNode = new Node(value);
        //把新节点的右子树 设置为 当前节点的右子树
        newNode.right = right;
        //把新节点的左子树 设置为 当前节点的左子树的右子树
        newNode.left = left.right;
        //把当前节点的值 替换为 左子节点的值
        value = left.value;
        //把当前节点的左子树 设置成 左子树的左子树
        left = left.left;
        //把当前节点的右子树 设置为 新节点
        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 返回的是要删除的结点的父结点, 如果没有就返回null
     */
    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 +
                '}';
    }
}