1. 程式人生 > >排序二叉樹和平衡二叉樹

排序二叉樹和平衡二叉樹


## 概述 對於一組元素 [7, 3, 10, 12, 5, 1, 9] 可以有很多種儲存方式,但無論使用哪種資料結構,都或多或少有缺陷。比如使用線性結構儲存,排序方便,但查詢效率低。二叉排序樹的特點就是能在保證元素有序的同時,提高查詢的效率。
## 二叉排序樹的定義 二叉排序樹,也叫二叉查詢樹,二叉搜尋樹,英文名 Binary Sort Tree(BST)。它或者是一顆空樹,或者是一顆具有以下性質的二叉樹 - 若左子樹不為空,則左子樹上所有結點的值均小於它的根結點的值 - 若右子樹不為空,則右子樹上所有結點的值均大於它的根結點的值 - 左、右子樹也分別為二叉排序樹 序列 [7, 3, 10, 12, 5, 1, 9] 以二叉排序樹儲存的結構如圖: ![](https://img2020.cnblogs.com/blog/1759254/202009/1759254-20200929160332024-1587150925.png)
## 建立二叉排序樹 & 新增 & 查詢 & 遍歷 值得注意的是,對二叉排序樹作中序遍歷,結果正好是一個有序序列。 ```java 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); } } } ``` ```java 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); } } } ```
## 刪除結點 二叉排序樹的刪除操作相對麻煩些,我們不能像以前那樣直接刪除結點對應的整個子樹,而是要把子結點保留下來,並重新拼接成新的排序二叉樹。針對不同的情況,也有不同的應對策略: - 刪除葉子結點。直接砍掉就好了,不會對其他結點有影響。 - 刪除只有一個子結點的結點。子結點代替原結點的位置。 - 刪除有兩個子結點的結點。被刪除結點同時也是對應二叉排序子樹的根結點,根據二叉排序樹的性質,根結點就是序列的中間值,所以要補上中間值的位置,要用中間值的後一位的元素(對應右子樹的最小結點)或前一位元素(對應左子樹的最大結點) ```java 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); } } } ``` ```java 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),如下圖所示: ![](https://img2020.cnblogs.com/blog/1759254/202009/1759254-20200929160424650-1935797918.png) 這樣也滿足二叉排序樹的定義,但和單鏈表無異,如果是這樣的話,那使用排序二叉樹還有什麼意義呢?所以我們下一步要思考的是如何保證一顆排序二叉樹結構良好 平衡二叉樹,也叫 AVL 樹,除了具有二叉排序樹的性質以外,它要求每一個結點的左右子樹的高度之差的絕對值不超過一 每一次插入新元素後,樹的平衡都有可能被破壞,因此每次插入時都要通過旋轉來維持平衡二叉樹的結構。假設需平衡的結點為 8,那麼破壞平衡的情況有四種: - 左左:對 8 的左兒子的左子樹進行一次插入 - 左右:對 8 的左兒子的右子樹進行一次插入 - 右左:對 8 的右兒子的左子樹進行一次插入 - 右右:對 8 的右兒子的右子樹進行一次插入 ![](https://img2020.cnblogs.com/blog/1759254/202009/1759254-20200929160438573-55445249.png) 要解決上述的問題,找到距離新插入結點最近的不平衡子樹進行旋轉,對於左左情況使用右旋轉,右右情況使用左旋轉,可以統稱為單旋轉。左右和右左情況則使用雙旋轉。左旋轉和右旋的旋轉方式是互為映象的,掌握其中一個,另一個自然也會了。左右、右左也是如此 以左左為例講解單旋轉: - 找到最近的不平衡子樹 8 - 建立一個新結點,值等於當前結點的值,即是 8 - 把新結點的右子樹設定為當前結點的右子樹,即是 9 - 把新結點的左子樹設定為當前結點的左子樹的右子樹,即是 7,到這裡得出下面結果 ![](https://img2020.cnblogs.com/blog/1759254/202009/1759254-20200929160456117-1073950325.png) - 再接下來目的就很明確了,將 6 當作根結點,新結點作為 6 的右兒子,這樣就完成了一次右旋轉,可以想象成 8 向右轉了一個角度 ![](https://img2020.cnblogs.com/blog/1759254/202009/1759254-20200929160516538-868151419.png) 雙旋轉其實就是做兩次旋轉,對於左右情況,先對 6 做一次左旋轉,然後才是 8 做一次右旋轉;對於右左情況,先對 8 做一次右旋轉,然後才是 5 做一次左旋轉 程式碼實現如下: ```java 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; } } } } ``` ```java 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); } } } ``