前言
一、二叉排序樹
1.1 先看一個需求
給你一個數列 (7, 3, 10, 12, 5, 1, 9),要求能夠高效的完成對數據的 查詢和添加
。
1.2 解決方案分析
- 使用數組
- 數組未排序, 優點:直接在數組尾添加,速度快。 缺點:查找速度慢. [示意圖]
- 數組排序,優點:可以使用二分查找,查找速度快,缺點:爲了保證數組有序,在添加新數據時,找到插入位置後,後面的數據需整體移動,速度慢。[示意圖]
-
使用鏈式存儲-鏈表不管鏈表是否有序,查找速度都慢,添加數據速度比數組快,不需要數據整體移動。
-
使用二叉排序樹
1.3 叉排序樹介紹
二叉排序樹
:BST: (Binary Sort(Search) Tree), 對於二叉排序樹的任何一個非葉子節點,要求左子節點的值比當前節點的值小,右子節點的值比當前節點的值大。
特別說明
:如果有相同的值,可以將該節點放在左子節點或右子節點
比如針對前面的數據 (7, 3, 10, 12, 5, 1, 9) ,對應的二叉排序樹爲:
1.4 二叉排序樹創建和遍歷
一個數組創建成對應的二叉排序樹,並使用中序遍歷二叉排序樹,比如: 數組爲 Array(7, 3, 10, 12, 5, 1, 9) , 創建成對應的二叉排序樹爲 :
1.5 二叉排序樹的刪除
二叉排序樹的刪除情況比較複雜,有下面三種情況需要考慮
- 刪除葉子節點 (比如:2, 5, 9, 12)
- 刪除只有一顆子樹的節點 (比如:1)
- 刪除有兩顆子樹的節點. (比如:7, 3,10 )
- 操作思路分析
圖中文字:
第一種情況: 刪除葉子節點 (比如:2, 5, 9, 12)
思路
(1) 需求先去找到要刪除的結點 targetNode
(2) 找到targetNode 的 父結點 parent
(3) 確定 targetNode 是 parent的左子結點 還是右子結點
(4) 根據前面的情況來對應刪除
左子結點 parent.left = null
右子結點 parent.right = null;
第二種情況: 刪除只有一顆子樹的節點 比如 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
情況三 : 刪除有兩顆子樹的節點. (比如:7, 3,10 )
思路
(1) 需求先去找到要刪除的結點 targetNode = 10
(2) 找到targetNode 的 父結點 parent
(3) 從targetNode 的右子樹找到最小的結點
(4) 用一個臨時變量,將 最小結點的值保存 temp = 11
(5) 刪除該最小結點
(6) targetNode.value = temp
1.6 代碼實現
1.6.1 Node結點類
package com.feng.ch15_binarysorttree;
/*
* 實體 Node 結點
* */
public class Node {
private int value;
private Node left;
private Node right;
public Node(int value) {
this.value = value;
}
@Override
public String toString() {
return "Node{" +
"value=" + value +
'}';
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
// 添加節點
/*
* 添加節點
* 遞歸形式 添加結點,注意需要滿足二叉排序樹的要求
* */
public void addNode(Node node) {
if (null == node) {
return;
}
// 判斷傳入的結點的值,和當前子樹的根結點的值關係
if (node.getValue() < this.value) {
// 如果當前結點左子結點爲null
if (this.left == null) {
this.left = node;
} else {
// 遞歸向左子樹 添加
this.left.addNode(node);
}
} else { // 添加結點值 大於 當前結點值
if (this.right == null) {
this.right = node;
} else {
this.right.addNode(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 searchWillDeleteNode(int value) {
if (value == this.value) { // 找到就是該結點
return this;
} else if (value < this.value) { // 如果查找的值小於當前結點,向左子樹遞歸查找
// 如果左子節點爲空
if (this.left == null) {
return null;
}
return this.left.searchWillDeleteNode(value);
} else { // 如果查找的值不小於當前結點,向右子樹遞歸查找
if (this.right == null) {
return null;
}
return this.right.searchWillDeleteNode(value);
}
}
// 查找要刪除結點的父結點
/**
* @param value 要刪除的結點的值
* @return 返回的是要刪除的結點的父結點,如果沒有就返回 null
*/
public Node searchWillDeleteNodeParent(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.searchWillDeleteNodeParent(value);//向左子樹遞歸查找
} else if (value >= this.value && this.right != null) {
return this.right.searchWillDeleteNodeParent(value); // 向右子樹遞歸查找
} else {
return null; // m沒有找到父結點
}
}
}
}
1.6.2 BinarySortTree 二叉排序樹類
package com.feng.ch15_binarysorttree;
/*
* 創建二叉排序樹
* */
public class BinarySortTree {
private Node root;
// 添加
public void addNode(Node node) {
if (root == null) {
root = node;
} else {
root.addNode(node);
}
}
// 中序遍歷
public void infixOrder() {
if (root != null) {
root.infixOrder();
} else {
System.out.println("二叉排序樹爲空~~~");
}
}
// 查找要刪除的 結點
public Node searchWillDeleteNode(int value) {
if (root == null) {
return null;
} else {
return root.searchWillDeleteNode(value);
}
}
// 查找要刪除的結點的 父結點
public Node searchWillDeleteNodeParent(int value) {
if (root == null) {
return null;
} else {
return root.searchWillDeleteNodeParent(value);
}
}
// 刪除結點
/*
* 刪除結點需要分三種情況
* 一、第一種情況: 刪除葉子節點 (比如:2, 5, 9, 12)
* 思路
* (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
*
* 二、第二種情況: 刪除只有一顆子樹的節點 比如 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
*
* 三、情況三 : 刪除有兩顆子樹的節點. (比如:7, 3,10 )
* 思路
* (1) 需求先去找到要刪除的結點 targetNode = 10
* (2) 找到targetNode 的 父結點 parent
* (3) 從targetNode 的右子樹找到最小的結點
* (4) 用一個臨時變量,將 最小結點的值保存 temp = 11
* (5) 刪除該最小結點
* (6) targetNode.value = temp
*
* */
public void deleteNode(int value) {
if (root == null) {
return;
} else {
//1、需求先去找到要刪除的結點 targetNode
Node targetNode = searchWillDeleteNode(value);
if (targetNode == null) {
return;
}
// 如果發現 root 根結點 沒有左右結點,說明要刪除的就是根結點,
if (root.getLeft() == null && root.getRight() == null) {
root = null;
return;
}
// 去找到 targetNode 的父結點
Node parentNode = searchWillDeleteNodeParent(value);
/*
* 第一種情況:如果要刪除的結點是葉子結點
* */
if (targetNode.getLeft() == null && targetNode.getRight() == null) {
// 判斷 targetNode 是父結點的左子結點,還是右子結點
if (parentNode.getLeft() != null && parentNode.getLeft().getValue() == value) { // 是左子結點
parentNode.setLeft(null);
} else if (parentNode.getRight() != null && parentNode.getRight().getValue() == value) { // 是右子結點
parentNode.setRight(null);
}
}
/*
* 第二種情況:如果要刪除的結點 是 只有一顆子樹的節點(僅有左子結點或僅有右子結點)
* */
if (targetNode.getLeft() != null && targetNode.getRight() == null) { // targetNode 僅有 左子結點
// 判斷 targetNode 是父結點的左子結點,還是右子結點
if (parentNode != null){
if (parentNode.getLeft() != null && parentNode.getLeft().getValue() == value) { // targetNode是 parentNode 的左子結點
parentNode.setLeft(targetNode.getLeft());
} else if (parentNode.getRight() != null && parentNode.getRight().getValue() == value) {// targetNode是 parentNode 的右子結點
parentNode.setRight(targetNode.getLeft());
}
} else {
root = targetNode.getLeft();
}
} else if (targetNode.getLeft() == null && targetNode.getRight() != null) {// targetNode 僅有 右子結點
if (parentNode != null){
if (parentNode.getLeft() != null && parentNode.getLeft().getValue() == value) {
parentNode.setLeft(targetNode.getRight());
} else if (parentNode.getRight() == null && parentNode.getRight().getValue() == value) {
parentNode.setRight(targetNode.getRight());
}
} else{
root = targetNode.getRight();
}
}
/*
* 第三種情況:刪除有兩顆子樹的節點. (比如:7, 3,10 )
* */
if (targetNode.getLeft() != null && targetNode.getRight() != null) {
/*
* 執行兩個功能:
* 1、返回的以 node 爲根結點的二叉排序樹 的最小結點的值
* 2、刪除 node 爲 根結點的二叉排序樹的最小結點
* */
int minValue = deleteRightTreeMin(targetNode.getRight());
// 將返回的最小值 賦值給 要刪除的 目標結點
targetNode.setValue(minValue);
}
}
}
/*
* 方法功能:
* 1、返回的以 node 爲根結點的二叉排序樹 的最小結點的值
* 2、刪除 node 爲 根結點的二叉排序樹的最小結點
*
* @param node // 傳入的結點 (當做二叉排序樹的根結點)
* @return 返回的 以 node 爲根結點的二叉排序樹的最小結點的值
* */
public int deleteRightTreeMin(Node node) {
Node target = node;
while (target.getLeft() != null) {
target = target.getLeft();
}
// 這是 target 指向了最小值
// 刪除最小結點
deleteNode(target.getValue()); // 這一步 就是刪除 葉子結點
return target.getValue();
}
}
1.6.3 BinarySortTreeMain測試類
package com.feng.ch15_binarysorttree;
/*
* 二叉排序樹測試類
* 主要學習
* 1、二叉排序樹的定義: BST: (Binary Sort(Search) Tree), 對於二叉排序樹的任何一個非葉子節點,
* 要求左子節點的值比當前節點的值小,右子節點的值比當前節點的值大。
* 2、二叉排序樹的添加
* 3、二叉排序樹的刪除:刪除分三種情況
* 3.1 刪除葉子結點的情況
* 3.2 刪除僅有一顆子樹的情況
* 3.3 刪除有兩顆子樹的情況
*
* 最後有個bug 需要注意一點,在刪除 僅有一顆子樹的請求,如果刪除的爲根結點,其返回的 父結點是爲 null 的,所以要對其父結點 判斷 是否爲空。
* */
public class BinarySortTreeMain {
public static void main(String[] args) {
int[] array = {7, 3, 10, 12, 5, 1, 9, 2};
BinarySortTree binarySortTree = new BinarySortTree();
// 循環添加結點 到 二叉排序樹
for (int i = 0; i < array.length; i++) {
binarySortTree.addNode(new Node(array[i]));
}
// 中序遍歷二叉樹
System.out.println("中序遍歷二叉樹:");
binarySortTree.infixOrder(); // 1 3 5 7 9 10 12 說明 : 中序遍歷後 就是升序排列的
// 刪除葉子結點
binarySortTree.deleteNode(2);
binarySortTree.deleteNode(5);
binarySortTree.deleteNode(9);
binarySortTree.deleteNode(12);
System.out.println("刪除葉子結點 後的中序遍歷:");
binarySortTree.infixOrder();
// 刪除有一顆子樹的結點
// binarySortTree.deleteNode(1);
// System.out.println("刪除只有一個子樹的 結點後的中序遍歷:");
// binarySortTree.infixOrder();
// 刪除兩顆子樹的結點
// binarySortTree.deleteNode(3);
// System.out.println("刪除有兩個結點的目標結點 後的中序遍歷:");
// binarySortTree.infixOrder();
// 刪除所有結點
binarySortTree.deleteNode(2);
binarySortTree.deleteNode(5);
binarySortTree.deleteNode(9);
binarySortTree.deleteNode(12);
binarySortTree.deleteNode(7);
binarySortTree.deleteNode(3);
binarySortTree.deleteNode(10);
binarySortTree.deleteNode(1);
System.out.println("刪除所有結點 後的中序遍歷:");
binarySortTree.infixOrder();
}
}
1.7 測試結果
二、二叉平衡樹
2.1二叉排序樹存在的問題
-
給你一個數列{1,2,3,4,5,6},要求創建一顆二叉排序樹(BST), 並分析問題所在.
-
創建的二叉排序樹如下
-
上邊BST 存在的問題分析:
左子樹全部爲空,從形式上看,更像一個單鏈表.插入速度沒有影響
查詢速度明顯降低(因爲需要依次比較), 不能發揮BST的優勢,因爲每次還需要比較左子樹,其查詢速度比單鏈表還慢 -
解決方案-
平衡二叉樹
(AVL)
2.2 二叉平衡樹 基本介紹
-
平衡二叉樹也叫平衡二叉搜索樹(Self-balancing binary search tree)又被稱爲AVL樹, 可以保證查詢效率較高。
-
具有以下特點:它是一 棵空樹或它的左右兩個子樹的高度差的絕對值不超過1,並且左右兩個子樹都是一棵平衡二叉樹。平衡二叉樹的常用實現方法有紅黑樹、AVL、替罪羊樹、Treap、伸展樹等。
-
舉例說明, 看看下面哪些AVL樹, 爲什麼?
2.3 應用案例-單旋轉(左旋轉)
2.3.1 思路分析
-
要求: 給你一個數列,創建出對應的平衡二叉樹.數列 {4,3,6,5,7,8}
-
思路分析(示意圖)
-
圖中文字:
問題:當插入8 時
rightHeight() - leftHeight() > 1 成立,此時,不再是一顆avl樹了.
怎麼處理–進行左旋轉.
- 創建一個新的節點 newNode (以4這個值創建)
,創建一個新的節點,值等於當前根節點的值
//把新節點的左子樹設置了當前節點的左子樹- newNode.left = left
//把新節點的右子樹設置爲當前節點的右子樹的左子樹- newNode.right =right.left;
////把當前節點的值換爲右子節點的值
4.value=right.value;
//把當前節點的右子樹設置成右子樹的右子樹- right=right.right;
////把當前節點的左子樹設置爲新節點- left=newLeft;
2.3.2 代碼實現
//左旋轉方法
private void leftRotate() {
//創建新的結點,以當前根結點的值
Node newNode = new Node(value);
//把新的結點的左子樹設置成當前結點的左子樹
newNode.left = left;
//把新的結點的右子樹設置成帶你過去結點的右子樹的左子樹
newNode.right = right.left;
//把當前結點的值替換成右子結點的值
value = right.value;
//把當前結點的右子樹設置成當前結點右子樹的右子樹
right = right.right;
//把當前結點的左子樹(左子結點)設置成新的結點
left = newNode;
}
2.4 應用案例-單旋轉(右旋轉)
2.4.1 思路分析
要求: 給你一個數列,創建出對應的平衡二叉樹.數列 {10,12, 8, 9, 7, 6}
2.4.2 代碼實現
//右旋轉
private void rightRotate() {
Node newNode = new Node(value);
newNode.right = right;
newNode.left = left.right;
value = left.value;
left = left.left;
right = newNode;
}
2.5 全部代碼
2.5.1 Node節點類(包含左右旋轉)
package com.feng.ch16_avl;
/*
* 實體 Node 結點
* 複製的 ch15_binarysorttree 包中的 Node 類
* 新添加方法:
* leftHeight() 左子樹高度
* rightHeight() 右子樹高度
* height() 高度 遞歸
* leftRotate() 左旋轉
* rightRotate() 右旋轉
*
* */
public class Node {
private int value;
private Node left;
private Node right;
public Node(int value) {
this.value = value;
}
@Override
public String toString() {
return "Node{" +
"value=" + value +
'}';
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
// 返回左子樹的高度
public int leftHeight() {
if (left == null) {
return 0;
}
return left.height();
}
// 返回右子樹的高度
public int rightHeight() {
if (right == null) {
return 0;
}
return right.height();
}
// 返回 以該結點爲根結點的樹的高度
public int height() {
return Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height()) + 1;
}
//左旋轉方法
private void leftRotate() {
//創建新的結點,以當前根結點的值
Node newNode = new Node(value);
//把新的結點的左子樹設置成當前結點的左子樹
newNode.left = left;
//把新的結點的右子樹設置成帶你過去結點的右子樹的左子樹
newNode.right = right.left;
//把當前結點的值替換成右子結點的值
value = right.value;
//把當前結點的右子樹設置成當前結點右子樹的右子樹
right = right.right;
//把當前結點的左子樹(左子結點)設置成新的結點
left = newNode;
}
//右旋轉
private void rightRotate() {
Node newNode = new Node(value);
newNode.right = right;
newNode.left = left.right;
value = left.value;
left = left.left;
right = newNode;
}
// 添加節點
/*
* 添加節點
* 遞歸形式 添加結點,注意需要滿足二叉排序樹的要求
* */
public void addNode(Node node) {
if (null == node) {
return;
}
// 判斷傳入的結點的值,和當前子樹的根結點的值關係
if (node.getValue() < this.value) {
// 如果當前結點左子結點爲null
if (this.left == null) {
this.left = node;
} else {
// 遞歸向左子樹 添加
this.left.addNode(node);
}
} else { // 添加結點值 大於 當前結點值
if (this.right == null) {
this.right = node;
} else {
this.right.addNode(node);
}
}
/*
* 當添加完一個結點後,如果: (右子樹的高度-左子樹的高度) > 1 , 左旋轉
* */
if (rightHeight() - leftHeight() > 1) {
//如果它的右子樹的左子樹的高度大於它的右子樹的右子樹的高度
if (right != null && right.leftHeight() > right.rightHeight()) {
//先對右子結點進行右旋轉
right.rightRotate();
//然後在對當前結點進行左旋轉
leftRotate(); //左旋轉..
} else {
//直接進行左旋轉即可
leftRotate();
}
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();
}
}
/**
* 查找要刪除的結點
*
* @param value 要刪除的結點的值
* @return 如果找到返回該結點,否則返回 null
*/
public Node searchWillDeleteNode(int value) {
if (value == this.value) { // 找到就是該結點
return this;
} else if (value < this.value) { // 如果查找的值小於當前結點,向左子樹遞歸查找
// 如果左子節點爲空
if (this.left == null) {
return null;
}
return this.left.searchWillDeleteNode(value);
} else { // 如果查找的值不小於當前結點,向右子樹遞歸查找
if (this.right == null) {
return null;
}
return this.right.searchWillDeleteNode(value);
}
}
// 查找要刪除結點的父結點
/**
* @param value 要刪除的結點的值
* @return 返回的是要刪除的結點的父結點,如果沒有就返回 null
*/
public Node searchWillDeleteNodeParent(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.searchWillDeleteNodeParent(value);//向左子樹遞歸查找
} else if (value >= this.value && this.right != null) {
return this.right.searchWillDeleteNodeParent(value); // 向右子樹遞歸查找
} else {
return null; // m沒有找到父結點
}
}
}
}
2.5.2 AVLTree二叉平衡樹類
package com.feng.ch16_avl;
/*
* 複製的 二叉排序樹 的 tree 代碼
* 這裏沒有變動,主要添加 在 Node 節點實體類中
* */
public class AVLTree {
private Node root;
public Node getRoot() {
return root;
}
// 添加
public void addNode(Node node) {
if (root == null) {
root = node;
} else {
root.addNode(node);
}
}
// 中序遍歷
public void infixOrder() {
if (root != null) {
root.infixOrder();
} else {
System.out.println("二叉排序樹爲空~~~");
}
}
// 查找要刪除的 結點
public Node searchWillDeleteNode(int value) {
if (root == null) {
return null;
} else {
return root.searchWillDeleteNode(value);
}
}
// 查找要刪除的結點的 父結點
public Node searchWillDeleteNodeParent(int value) {
if (root == null) {
return null;
} else {
return root.searchWillDeleteNodeParent(value);
}
}
// 刪除結點
/*
* 刪除結點需要分三種情況
* 一、第一種情況: 刪除葉子節點 (比如:2, 5, 9, 12)
* 思路
* (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
*
* 二、第二種情況: 刪除只有一顆子樹的節點 比如 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
*
* 三、情況三 : 刪除有兩顆子樹的節點. (比如:7, 3,10 )
* 思路
* (1) 需求先去找到要刪除的結點 targetNode = 10
* (2) 找到targetNode 的 父結點 parent
* (3) 從targetNode 的右子樹找到最小的結點
* (4) 用一個臨時變量,將 最小結點的值保存 temp = 11
* (5) 刪除該最小結點
* (6) targetNode.value = temp
*
* */
public void deleteNode(int value) {
if (root == null) {
return;
} else {
//1、需求先去找到要刪除的結點 targetNode
Node targetNode = searchWillDeleteNode(value);
if (targetNode == null) {
return;
}
// 如果發現 root 根結點 沒有左右結點,說明要刪除的就是根結點,
if (root.getLeft() == null && root.getRight() == null) {
root = null;
return;
}
// 去找到 targetNode 的父結點
Node parentNode = searchWillDeleteNodeParent(value);
/*
* 第一種情況:如果要刪除的結點是葉子結點
* */
if (targetNode.getLeft() == null && targetNode.getRight() == null) {
// 判斷 targetNode 是父結點的左子結點,還是右子結點
if (parentNode.getLeft() != null && parentNode.getLeft().getValue() == value) { // 是左子結點
parentNode.setLeft(null);
} else if (parentNode.getRight() != null && parentNode.getRight().getValue() == value) { // 是右子結點
parentNode.setRight(null);
}
}
/*
* 第二種情況:如果要刪除的結點 是 只有一顆子樹的節點(僅有左子結點或僅有右子結點)
* */
if (targetNode.getLeft() != null && targetNode.getRight() == null) { // targetNode 僅有 左子結點
// 判斷 targetNode 是父結點的左子結點,還是右子結點
if (parentNode != null){
if (parentNode.getLeft() != null && parentNode.getLeft().getValue() == value) { // targetNode是 parentNode 的左子結點
parentNode.setLeft(targetNode.getLeft());
} else if (parentNode.getRight() != null && parentNode.getRight().getValue() == value) {// targetNode是 parentNode 的右子結點
parentNode.setRight(targetNode.getLeft());
}
} else {
root = targetNode.getLeft();
}
} else if (targetNode.getLeft() == null && targetNode.getRight() != null) {// targetNode 僅有 右子結點
if (parentNode != null){
if (parentNode.getLeft() != null && parentNode.getLeft().getValue() == value) {
parentNode.setLeft(targetNode.getRight());
} else if (parentNode.getRight() == null && parentNode.getRight().getValue() == value) {
parentNode.setRight(targetNode.getRight());
}
} else{
root = targetNode.getRight();
}
}
/*
* 第三種情況:刪除有兩顆子樹的節點. (比如:7, 3,10 )
* */
if (targetNode.getLeft() != null && targetNode.getRight() != null) {
/*
* 執行兩個功能:
* 1、返回的以 node 爲根結點的二叉排序樹 的最小結點的值
* 2、刪除 node 爲 根結點的二叉排序樹的最小結點
* */
int minValue = deleteRightTreeMin(targetNode.getRight());
// 將返回的最小值 賦值給 要刪除的 目標結點
targetNode.setValue(minValue);
}
}
}
/*
* 方法功能:
* 1、返回的以 node 爲根結點的二叉排序樹 的最小結點的值
* 2、刪除 node 爲 根結點的二叉排序樹的最小結點
*
* @param node // 傳入的結點 (當做二叉排序樹的根結點)
* @return 返回的 以 node 爲根結點的二叉排序樹的最小結點的值
* */
public int deleteRightTreeMin(Node node) {
Node target = node;
while (target.getLeft() != null) {
target = target.getLeft();
}
// 這是 target 指向了最小值
// 刪除最小結點
deleteNode(target.getValue()); // 這一步 就是刪除 葉子結點
return target.getValue();
}
}
2.5.3 AVLTreeMain測試類
package com.feng.ch16_avl;
/*
* 二叉平衡樹
* */
public class AVLTreeMain {
public static void main(String[] args) {
// int[] array = {4, 3, 6, 5, 7, 8}; // 右子樹 高於 左子樹 ,需要左旋轉
// int[] array = { 10, 12, 8, 9, 7, 6 }; // 左子樹 高於 右子樹,需要右旋轉
int[] array = { 10, 11, 7, 6, 8, 9 };
// 創建一個 AVLTree 對象
AVLTree avlTree = new AVLTree();
// 添加節點
for (int i = 0; i < array.length ; i++){
avlTree.addNode(new Node(array[i]));
}
// 遍歷
System.out.println("中序遍歷:");
avlTree.infixOrder();
// System.out.println("在沒有平衡處理前:");
// System.out.println("樹的高度="+avlTree.getRoot().height()); //4
// System.out.println("右子樹的高度="+avlTree.getRoot().rightHeight()); //1
// System.out.println("左子樹的高度="+avlTree.getRoot().leftHeight()); //3
// System.out.println("當前根結點:"+avlTree.getRoot());
System.out.println("平衡處理後:");
System.out.println("樹的高度="+avlTree.getRoot().height()); //
System.out.println("右子樹的高度="+avlTree.getRoot().rightHeight()); //
System.out.println("左子樹的高度="+avlTree.getRoot().leftHeight()); //
System.out.println("當前根結點:"+avlTree.getRoot());
}
}