排序二叉樹和平衡二叉樹


概述

對於一組元素 [7, 3, 10, 12, 5, 1, 9] 能夠有不少種存儲方式,但不管使用哪一種數據結構,都或多或少有缺陷。好比使用線性結構存儲,排序方便,但查找效率低。二叉排序樹的特色就是能在保證元素有序的同時,提升查找的效率。java


二叉排序樹的定義

二叉排序樹,也叫二叉查找樹,二叉搜索樹,英文名 Binary Sort Tree(BST)。它或者是一顆空樹,或者是一顆具備如下性質的二叉樹node

  • 若左子樹不爲空,則左子樹上全部結點的值均小於它的根結點的值
  • 若右子樹不爲空,則右子樹上全部結點的值均大於它的根結點的值
  • 左、右子樹也分別爲二叉排序樹

序列 [7, 3, 10, 12, 5, 1, 9] 以二叉排序樹存儲的結構如圖:數據結構


建立二叉排序樹 & 添加 & 查找 & 遍歷

值得注意的是,對二叉排序樹做中序遍歷,結果正好是一個有序序列。this

public class Node {

    int value;
    Node left;
    Node right;

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

    /**
     * 向子樹添加結點
     * @param node 要添加的結點
     */
    public void add(Node node) {
        if (node != null) {
            // 添加的結點比當前結點的值小
            if (node.value < this.value) {
                // 左結點爲空
                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 midShow() {
        // 輸出左結點內容
        if (left != null) {
            left.midShow();
        }
        // 輸出當前結點內容
        System.out.println(value);
        // 輸出右結點內容
        if (right != null) {
            right.midShow();
        }
    }

    /**
     * 查找結點
     * @param value 目標結點的值
     * @return  目標結點
     */
    public Node search(int value) {
        if (this.value == value) {
            return this;
        } else if (value < this.value) {
            if (left == null) {
                return null;
            }
            return left.search(value);
        } else {
            if (right == null) {
                return null;
            }
            return right.search(value);
        }
    }
}
public class BinarySortTree {

    private Node root;

    /**
     * 向二叉排序樹添加結點
     * @param node
     */
    public void add(Node node) {
        if (root == null) {
            root = node;
        } else {
            root.add(node);
        }
    }

    /**
     * 中序遍歷
     */
    public void midShow() {
        if (root != null) {
            root.midShow();
        }
    }

    /**
     * 查找結點
     * @param value 目標結點的值
     * @return  目標結點
     */
    public Node search(int value) {
        if (root == null) {
            return null;
        } else {
            return root.search(value);
        }
    }
}

刪除結點

二叉排序樹的刪除操做相對麻煩些,咱們不能像之前那樣直接刪除結點對應的整個子樹,而是要把子結點保留下來,並從新拼接成新的排序二叉樹。針對不一樣的狀況,也有不一樣的應對策略:3d

  • 刪除葉子結點。直接砍掉就行了,不會對其餘結點有影響。
  • 刪除只有一個子結點的結點。子結點代替原結點的位置。
  • 刪除有兩個子結點的結點。被刪除結點同時也是對應二叉排序子樹的根結點,根據二叉排序樹的性質,根結點就是序列的中間值,因此要補上中間值的位置,要用中間值的後一位的元素(對應右子樹的最小結點)或前一位元素(對應左子樹的最大結點)
public class BinarySortTree {

    private Node root;

	......

    /**
     * 刪除結點
     * @param value 要刪除結點的值
     */
    public void delete(int value) {
        if (root != null) {
            // 找到目標結點
            Node target = search(value);
            if (target != null) {
                // 找到目標結點的父結點
                Node parent = searchParent(value);
                // 要刪除的結點是葉子結點
                if (target.left == null && target.right == null) {
                    // 要刪除的結點是父結點的左子結點
                    if (parent.left.value() == value) {
                        parent.left = null;
                    // 要刪除的結點是父結點的右子結點
                    } else {
                        parent.right = null;
                    }
                // 要刪除的結點有兩個子結點
                } else if (target.left != null && target.right != null) {
                    // 刪除右子樹中值最小的結點,並獲取該結點的值
                    int min = deleteMin(target.right);
                    // 替換目標結點的值
                    target.value = min;
                // 要刪除的結點只有一個子結點
                } else {
                    // 有左子結點
                    if (target.left != null) {
                        // 要刪除的結點是父結點的左子結點
                        if (parent.left.value() == value) {
                            // 父結點的左子結點指向目標結點的左子結點
                            parent.left = target.left;
                        // 要刪除的結點是父結點的右子結點
                        } else {
                            // 父結點的右子結點指向目標結點的左子結點
                            parent.right = target.left;
                        }
                    // 有右子結點
                    } else {
                        // 要刪除的結點是父結點的左子結點
                        if (parent.left.value == value) {
                            // 父結點的左子結點指向目標結點的左子結點
                            parent.left = target.right;
                        // 要刪除的結點是父結點的右子結點
                        } else {
                            parent.right = target.right;
                        }
                    }
                }
            }
        }
    }

    /**
     * 刪除最小值結點
     * @param node  目標二叉樹的根結點
     * @return 最小值
     */
    public int deleteMin(Node node) {

        Node target = node;
        while (target.left != null) {
            target = target.left();
        }
        delete(target.value);
        return target.value;
    }

    /**
     * 查找父結點
     * @param value 目標父結點的子結點的值
     * @return 目標父結點
     */
    public Node searchParent(int value) {
        if (root == null) {
            return null;
        } else {
            return root.searchParent(value);
        }
    }
}
public class Node {

    int value;
    Node left;
    Node right;

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

	......

    /**
     * 查找結點
     * @param value 目標結點的值
     * @return  目標結點
     */
    public Node search(int value) {
        if (this.value == value) {
            return this;
        } else if (value < this.value) {
            if (left == null) {
                return null;
            }
            return left.search(value);
        } else {
            if (right == null) {
                return null;
            }
            return right.search(value);
        }
    }

    /**
     * 查找父結點
     * @param value 目標父結點的子結點的值
     * @return 目標父結點
     */
    public Node searchParent(int value) {
        if ((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value)) {
            return this;
        } else {
            if (this.left != null && this.value > value) {
                return this.left.searchParent(value);
            } else if (this.right != null && this.value < value) {
                return this.right.searchParent(value);
            } else {
                return null;
            }
        }
    }
}

平衡二叉樹

上述的排序二叉樹,若是結構良好,那麼檢索數據時的時間開銷爲 O(logn),其中 n 爲結點個數。但若是排序二叉樹的結構畸形,那麼最壞時間開銷可能爲 O(n),以下圖所示:code

這樣也知足二叉排序樹的定義,但和單鏈表無異,若是是這樣的話,那使用排序二叉樹還有什麼意義呢?因此咱們下一步要思考的是如何保證一顆排序二叉樹結構良好blog

平衡二叉樹,也叫 AVL 樹,除了具備二叉排序樹的性質之外,它要求每個結點的左右子樹的高度之差的絕對值不超過一排序

每一次插入新元素後,樹的平衡都有可能被破壞,所以每次插入時都要經過旋轉來維持平衡二叉樹的結構。假設需平衡的結點爲 8,那麼破壞平衡的狀況有四種:get

  • 左左:對 8 的左兒子的左子樹進行一次插入
  • 左右:對 8 的左兒子的右子樹進行一次插入
  • 右左:對 8 的右兒子的左子樹進行一次插入
  • 右右:對 8 的右兒子的右子樹進行一次插入

要解決上述的問題,找到距離新插入結點最近的不平衡子樹進行旋轉,對於左左狀況使用右旋轉,右右狀況使用左旋轉,能夠統稱爲單旋轉。左右和右左狀況則使用雙旋轉。左旋轉和右旋的旋轉方式是互爲鏡像的,掌握其中一個,另外一個天然也會了。左右、右左也是如此class

以左左爲例講解單旋轉:

  • 找到最近的不平衡子樹 8

  • 建立一個新結點,值等於當前結點的值,便是 8

  • 把新結點的右子樹設置爲當前結點的右子樹,便是 9

  • 把新結點的左子樹設置爲當前結點的左子樹的右子樹,便是 7,到這裏得出下面結果

  • 再接下來目的就很明確了,將 6 看成根結點,新結點做爲 6 的右兒子,這樣就完成了一次右旋轉,能夠想象成 8 向右轉了一個角度

雙旋轉其實就是作兩次旋轉,對於左右狀況,先對 6 作一次左旋轉,而後纔是 8 作一次右旋轉;對於右左狀況,先對 8 作一次右旋轉,而後纔是 5 作一次左旋轉

代碼實現以下:

public class AVLNode {

    int value;
    AVLNode left;
    AVLNode right;

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

    /**
     * 返回當前結點的高度
     * @return 當前結點的高度
     */
    public int getHeight() {
        return Math.max(left == null ? 0 : left.getHeight(),
                right == null ? 0 : right.getHeight()) + 1;
    }

    /**
     * 獲取左子樹的高度
     * @return 左子樹的高度
     */
    public int getLeftHeight() {
        if (left == null) {
            return 0;
        }
        return left.getHeight();
    }

    /**
     * 獲取右子樹的高度
     * @return 右子樹的高度
     */
    public int getRightHeight() {
        if (right == null) {
            return 0;
        }
        return right.getHeight();
    }

    /**
     * 向子樹添加結點
     * @param node 要添加的結點
     */
    public void add(AVLNode node) {
        if (node != null) {
            // 添加的結點比當前結點的值小
            if (node.value < this.value) {
                // 左結點爲空
                if (this.left == null) {
                    this.left = node;
                    // 左結點不爲空
                } else {
                    this.left.add(node);
                }
                // 添加的結點比當前結點的值大
            } else {
                // 右結點爲空
                if (this.right == null) {
                    this.right = node;
                    // 右結點不爲空
                } else {
                    this.right.add(node);
                }
            }
        }
        // 判斷是否平衡
        // 進行右旋轉
        if (getLeftHeight() - getRightHeight() >= 2) {
            // 雙旋轉
            if (left != null && left.getLeftHeight() < left.getRightHeight()) {
                // 先左旋轉
                left.leftRotate();
                // 再右旋轉
                rightRotate();
            } else {
                // 單旋轉
                rightRotate();
            }
        }
        // 進行左旋轉
        if(getLeftHeight() - getRightHeight() <= -2) {
            if (right != null && right.getRightHeight() < right.getLeftHeight()) {
                // 先右旋轉
                right.rightRotate();
                // 再左旋轉
                leftRotate();
            } else {
                // 單旋轉
                leftRotate();
            }
        }
    }

    /**
     * 左旋轉
     */
    private void leftRotate() {
        AVLNode node = new AVLNode(value);
        node.left = left;
        node.right = right.left;
        value = right.value;
        right = right.right;
        left = node;
    }

    /**
     * 右旋轉
     */
    private void rightRotate() {
        // 建立一個新結點,值等於當前結點的值
        AVLNode node = new AVLNode(value);
        // 把新結點的右子樹設置爲當前結點的右子樹
        node.right = right;
        // 把新結點的左子樹設置爲當前結點的左子樹的右子樹
        node.left = left.right;
        // 把當前結點的值換爲左子結點的值
        value = left.value;
        // 把當前結點的左子樹設置爲左子樹的左子樹
        left = left.left;
        // 把當前結點的右子樹設置爲新結點
        right = node;
    }

    /**
     * 查找結點
     * @param value 目標結點的值
     * @return  目標結點
     */
    public AVLNode search(int value) {
        if (this.value == value) {
            return this;
        } else if (value < this.value) {
            if (left == null) {
                return null;
            }
            return left.search(value);
        } else {
            if (right == null) {
                return null;
            }
            return right.search(value);
        }
    }

    /**
     * 查找父結點
     * @param value 目標父結點的子結點的值
     * @return 目標父結點
     */
    public AVLNode searchParent(int value) {
        if ((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value)) {
            return this;
        } else {
            if (this.left != null && this.value > value) {
                return this.left.searchParent(value);
            } else if (this.right != null && this.value < value) {
                return this.right.searchParent(value);
            } else {
                return null;
            }
        }
    }
}
public class AVLTree {

    private AVLNode root;

    /**
     * 向二叉排序樹添加結點
     * @param node
     */
    public void add(AVLNode node) {
        if (root == null) {
            root = node;
        } else {
            root.add(node);
        }
    }

    /**
     * 查找結點
     * @param value 目標結點的值
     * @return  目標結點
     */
    public AVLNode search(int value) {
        if (root == null) {
            return null;
        } else {
            return root.search(value);
        }
    }

    /**
     * 刪除結點
     * @param value 要刪除結點的值
     */
    public void delete(int value) {
        if (root != null) {
            // 找到目標結點
            AVLNode target = search(value);
            if (target != null) {
                // 找到目標結點的父結點
                AVLNode parent = searchParent(value);
                // 要刪除的結點是葉子結點
                if (target.left == null && target.right == null) {
                    // 要刪除的結點是父結點的左子結點
                    if (parent.left.value == value) {
                        parent.left = null;
                        // 要刪除的結點是父結點的右子結點
                    } else {
                        parent.right = null;
                    }
                    // 要刪除的結點有兩個子結點
                } else if (target.left != null && target.right != null) {
                    // 刪除右子樹中值最小的結點,並獲取該結點的值
                    int min = deleteMin(target.right);
                    // 替換目標結點的值
                    target.value = min;
                    // 要刪除的結點只有一個子結點
                } else {
                    // 有左子結點
                    if (target.left != null) {
                        // 要刪除的結點是父結點的左子結點
                        if (parent.left.value == value) {
                            // 父結點的左子結點指向目標結點的左子結點
                            parent.left = target.left;
                            // 要刪除的結點是父結點的右子結點
                        } else {
                            // 父結點的右子結點指向目標結點的左子結點
                            parent.right = target.left;
                        }
                        // 有右子結點
                    } else {
                        // 要刪除的結點是父結點的左子結點
                        if (parent.left.value == value) {
                            // 父結點的左子結點指向目標結點的左子結點
                            parent.left = target.right;
                            // 要刪除的結點是父結點的右子結點
                        } else {
                            parent.right = target.right;
                        }
                    }
                }
            }
        }
    }

    /**
     * 刪除最小值結點
     * @param node  目標二叉樹的根結點
     * @return
     */
    public int deleteMin(AVLNode node) {

        AVLNode target = node;
        while (target.left != null) {
            target = target.left;
        }
        delete(target.value);
        return target.value;
    }

    /**
     * 查找父結點
     * @param value 目標父結點的子結點的值
     * @return 目標父結點
     */
    public AVLNode searchParent(int value) {
        if (root == null) {
            return null;
        } else {
            return root.searchParent(value);
        }
    }
}
相關文章
相關標籤/搜索