1.先看一個需求
給你一個數列 (7, 3, 10, 12, 5, 1, 9),要求能夠高效的完成對數據的查詢和添加
1.1 使用數組
數組未排序, 優(yōu)點:直接在數組尾添加,速度快。 缺點:查找速度慢. [示意圖]
數組排序,優(yōu)點:可以使用二分查找,查找速度快,缺點:為了保證數組有序,在添加新數據時,找到插入位
1.2 使用鏈式存儲-鏈表
不管鏈表是否有序,查找速度都慢,添加數據速度比數組快,不需要數據整體移動。[示意圖]
1.3 使用二叉排序樹
二叉排序樹:BST: (Binary Sort(Search) Tree), 對于二叉排序樹的任何一個非葉子節(jié)點,要求左子節(jié)點的值比當前節(jié)點的值小,右子節(jié)點的值比當前節(jié)點的值大。
特別說明:如果有相同的值,可以將該節(jié)點放在左子節(jié)點或右子節(jié)點
比如針對前面的數據 (7, 3, 10, 12, 5, 1, 9) ,對應的二叉排序樹為:

2.二叉排序樹創(chuàng)建和遍歷
一個數組創(chuàng)建成對應的二叉排序樹,并使用中序遍歷二叉排序樹,比如: 數組為 Array(7, 3, 10, 12, 5, 1, 9) , 創(chuàng)建成對應的二叉排序樹為 :

3. 二叉排序樹的刪除
二叉排序樹的刪除情況比較復雜,有下面三種情況需要考慮

- 刪除葉子節(jié)點 (比如:2, 5, 9, 12)
- (1) 需求先去找到要刪除的結點targetNode
- (2) 找到 targetNode 的 父結點 parent
- (3) 確定 targetNode 是 parent 的左子結點 還是右子結點
- (4) 根據前面的情況來對應刪除
左子結點 parent.left = null
右子結點 parent.right = null;
- 刪除只有一顆子樹的節(jié)點 (比如: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
- 刪除有兩顆子樹的節(jié)點. (比如:7, 3,10 )
(1) 需求先去找到要刪除的結點 targetNode
(2)找到 targetNode 的 父結點 parent
(3)從 targetNode 的右子樹找到最小的結點
(4) 用一個臨時變量,將 最小結點的值保存 temp = 11
(5)刪除該最小結點
(6)targetNode.value = temp
4. 代碼實現
1.創(chuàng)建Node結點
class Node {
int value;
Node left;
Node right;
public Node(int value) {
this.value = value;
}
//查找要刪除的結點
/**
*
* @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 + "]";
}
//添加結點的方法
//遞歸的形式添加結點,注意需要滿足二叉排序樹的要求
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();
}
}
}
2.創(chuàng)建二叉排序樹
class BinarySortTree {
private Node root;
public Node getRoot() {
return 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;
//循環(huán)的查找左子節(jié)點,就會找到最小值
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;
}
//如果我們發(fā)現當前這顆二叉排序樹只有一個結點
if(root.left == null && root.right == null) {
root = null;
return;
}
//去找到targetNode的父結點
Node parent = searchParent(value);
//如果要刪除的結點是葉子結點
if(targetNode.left == null && targetNode.right == null) {
//判斷targetNode 是父結點的左子結點,還是右子結點
if(parent.left != null && parent.left.value == value) { //是左子結點
parent.left = null;
} else if (parent.right != null && parent.right.value == value) {//是由子結點
parent.right = null;
}
} else if (targetNode.left != null && targetNode.right != null) { //刪除有兩顆子樹的節(jié)點
int minVal = delRightTreeMin(targetNode.right);
targetNode.value = minVal;
} else { // 刪除只有一顆子樹的結點
//如果要刪除的結點有左子結點
if(targetNode.left != null) {
if(parent != null) {
//如果 targetNode 是 parent 的左子結點
if(parent.left.value == value) {
parent.left = targetNode.left;
} else { // targetNode 是 parent 的右子結點
parent.right = targetNode.left;
}
} else {
root = targetNode.left;
}
} else { //如果要刪除的結點有右子結點
if(parent != null) {
//如果 targetNode 是 parent 的左子結點
if(parent.left.value == 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("二叉排序樹為空,不能遍歷");
}
}
}
3.測試
public static void main(String[] args) {
int[] arr = {7, 3, 10, 12, 5, 1, 9, 2};
BinarySortTree binarySortTree = new BinarySortTree();
//循環(huán)的添加結點到二叉排序樹
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
//測試一下刪除葉子結點
binarySortTree.delNode(12);
binarySortTree.delNode(5);
binarySortTree.delNode(10);
binarySortTree.delNode(2);
binarySortTree.delNode(3);
binarySortTree.delNode(9);
binarySortTree.delNode(1);
binarySortTree.delNode(7);
System.out.println("root=" + binarySortTree.getRoot());
System.out.println("刪除結點后");
binarySortTree.infixOrder();
}