天天看點

一篇搞懂二叉排序樹與平衡二叉樹

文章目錄

  • ​​思維導圖​​
  • ​​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 +
                '}';
    }
}