二叉查詢樹
定義
-
二叉查詢樹(亦稱二叉搜尋樹、二叉排序樹)是一棵二叉樹,且各結點關鍵詞互異,其中根序列按其關鍵詞遞增排列。
-
等價描述:二叉查詢樹中任一結點 P,其左子樹中結點的關鍵詞都小於 P 的關鍵詞,右子樹中結點的關鍵詞都大於 P 的關鍵詞,且結點 P 的左右子樹也都是二叉查詢樹
節點結構
1️⃣ key:關鍵字的值
2️⃣ value:關鍵字的儲存資訊
3️⃣ left:左節點的引用
4️⃣ right:右節點的引用
class BSTNode<K extends Comparable<K>,V>{
public K key;
public V value;
public BSTNode<K,V> left;
public BSTNode<K,V> right;
}
為了程式碼簡潔,本文不考慮屬性的封裝,一律設為 public
查詢演算法
思想:利用二叉查詢樹的特性,左子樹值小於根節點值,右子樹值大於根節點值,從根節點開始搜尋
- 如果目標值等於某節點值,返回該節點
- 如果目標值小於某節點值,搜尋該節點的左子樹
- 如果目標值大於某節點值,搜尋該節點的右子樹
1️⃣ 遞迴版本
public BSTNode<K, V> searchByRecursion(K key) {
return searchByRecursion(root, key);
}
private BSTNode<K, V> searchByRecursion(BSTNode<K, V> t, K key) {
if (t == null || t.key == key) return t;
else if (key.compareTo(t.key) < 0) return searchByRecursion(t.left, key);
else return searchByRecursion(t.right, key);
}
2️⃣ 迭代版本
public BSTNode<K,V> searchByIteration(K key) {
BSTNode<K,V> p = this.root;
while(p != null) {
if(key.compareTo(p.key) < 0) p = p.left;
else if(key.compareTo(p.key) > 0) p = p.right;
else return p;
}
return null;
}
插入演算法
- 在以
t
為根的二叉查詢樹中插入關鍵詞為key
的結點 - 在
t
中查詢key
,在查詢失敗處插入
1️⃣ 遞迴版本
public void insertByRecursion(K key, V value) {
this.root = insertByRecursion(root, key, value);
}
private BSTNode<K, V> insertByRecursion(BSTNode<K, V> t, K key, V value) {
if (t == null) {
return new BSTNode<>(key, value);
}
else if (key.compareTo(t.key) < 0) t.left = insertByRecursion(t.left, key, value);
else if (key.compareTo(t.key) > 0) t.right = insertByRecursion(t.right, key, value);
else {
t.value = value; // 如果二叉查詢樹中已經存在關鍵字,則替換該結點的值
}
return t;
}
2️⃣ 迭代版本
public void insertByIteration(K key, V value) {
BSTNode<K, V> p = root;
if (p == null) {
root = new BSTNode<>(key, value);
return;
}
BSTNode<K, V> pre = null;
while (p != null) {
pre = p;
if (key.compareTo(p.key) < 0) p = p.left;
else if (key.compareTo(p.key) > 0) p = p.right;
else {
p.value = value; // 如果二叉查詢樹中已經存在關鍵字,則替換該結點的值
return;
}
}
if(key.compareTo(pre.key) < 0) {
pre.left = new BSTNode<>(key, value);
} else {
pre.right = new BSTNode<>(key, value);
}
}
刪除演算法
-
在以
t
為根的二叉查詢樹中刪除關鍵詞值為key
的結點 -
在
t
中找到關鍵詞為key
的結點,分三種情況刪除key
-
若
key
是葉子節點,則直接刪除
-
若
key
只有一棵子樹,則子承父業
-
若
key
既有左子樹也有右子樹,則找到key
的後繼結點,替換key
和後繼節點的值,然後刪除後繼節點(後繼節點只有一棵子樹,轉化為第二種情況)。
後繼結點是當前結點的右子樹的最左結點,如果右子樹沒有左子樹,則後繼節點就是右子樹的根節點。
-
public void removeByRecursion(K key) {
this.root = removeByRecursion(root, key);
}
private BSTNode<K, V> removeByRecursion(BSTNode<K, V> t, K key) {
if(t == null) return root;
else if(t.key.compareTo(key) < 0) t.right = removeByRecursion(t.right, key); // key大,遞迴處理右子樹
else if(t.key.compareTo(key) > 0) t.left = removeByRecursion(t.left, key); // key小,遞迴處理左子樹
else {
if(t.right == null) return t.left; // 情況一、二一起處理
if(t.left == null) return t.right; // 情況一、二一起處理
BSTNode<K, V> node = t.right; // 情況三:右子樹沒有左子樹
if (node.left == null) {
node.left = t.left;
} else { // 情況三:右子樹有左子樹
BSTNode<K, V> pre = null;
while (node.left != null) {
pre = node;
node = node.left;
}
t.key = node.key;
t.value = node.value;
pre.left = node.right;
}
}
return t;
}
完整程式碼
class BSTNode<K extends Comparable<K>, V> {
public K key;
public V value;
public BSTNode<K, V> left;
public BSTNode<K, V> right;
public BSTNode(K key, V value) {
this.key = key;
this.value = value;
}
}
class BSTree<K extends Comparable<K>, V> {
public BSTNode<K, V> root;
private void inorder(BSTNode<K, V> root) {
if (root != null) {
inorder(root.left);
System.out.print("(key: " + root.key + " , value: " + root.value + ") ");
inorder(root.right);
}
}
private void preorder(BSTNode<K, V> root) {
if (root != null) {
System.out.print("(key: " + root.key + " , value: " + root.value + ") ");
preorder(root.left);
preorder(root.right);
}
}
private void postorder(BSTNode<K, V> root) {
if (root != null) {
postorder(root.left);
postorder(root.right);
System.out.print("(key: " + root.key + " , value: " + root.value + ") ");
}
}
public void postorderTraverse() {
System.out.print("後序遍歷:");
postorder(root);
System.out.println();
}
public void preorderTraverse() {
System.out.print("先序遍歷:");
preorder(root);
System.out.println();
}
public void inorderTraverse() {
System.out.print("中序遍歷:");
inorder(root);
System.out.println();
}
public BSTNode<K, V> searchByRecursion(K key) {
return searchByRecursion(root, key);
}
private BSTNode<K, V> searchByRecursion(BSTNode<K, V> t, K key) {
if (t == null || t.key == key) return t;
else if (key.compareTo(t.key) < 0) return searchByRecursion(t.left, key);
else return searchByRecursion(t.right, key);
}
public BSTNode<K, V> searchByIteration(K key) {
BSTNode<K, V> p = this.root;
while (p != null) {
if (key.compareTo(p.key) < 0) p = p.left;
else if (key.compareTo(p.key) > 0) p = p.right;
else return p;
}
return null;
}
public void insertByRecursion(K key, V value) {
this.root = insertByRecursion(root, key, value);
}
private BSTNode<K, V> insertByRecursion(BSTNode<K, V> t, K key, V value) {
if (t == null) {
return new BSTNode<>(key, value);
} else if (key.compareTo(t.key) < 0) t.left = insertByRecursion(t.left, key, value);
else if (key.compareTo(t.key) > 0) t.right = insertByRecursion(t.right, key, value);
else {
t.value = value;
}
return t;
}
public void insertByIteration(K key, V value) {
BSTNode<K, V> p = root;
if (p == null) {
root = new BSTNode<>(key, value);
return;
}
BSTNode<K, V> pre = null;
while (p != null) {
pre = p;
if (key.compareTo(p.key) < 0) p = p.left;
else if (key.compareTo(p.key) > 0) p = p.right;
else {
p.value = value; // 如果二叉查詢樹中已經存在關鍵字,則替換該結點的值
return;
}
}
if (key.compareTo(pre.key) < 0) {
pre.left = new BSTNode<>(key, value);
} else {
pre.right = new BSTNode<>(key, value);
}
}
public void removeByRecursion(K key) {
this.root = removeByRecursion(root, key);
}
private BSTNode<K, V> removeByRecursion(BSTNode<K, V> t, K key) {
if(t == null) return root;
else if(t.key.compareTo(key) < 0) t.right = removeByRecursion(t.right, key); // key大,遞迴處理右子樹
else if(t.key.compareTo(key) > 0) t.left = removeByRecursion(t.left, key); // key小,遞迴處理左子樹
else {
if(t.right == null) return t.left; // 情況一、二一起處理
if(t.left == null) return t.right; // 情況一、二一起處理
BSTNode<K, V> node = t.right; // 情況三:右子樹沒有左子樹
if (node.left == null) {
node.left = t.left;
} else { // 情況三:右子樹有左子樹
BSTNode<K, V> pre = null;
while (node.left != null) {
pre = node;
node = node.left;
}
t.key = node.key;
t.value = node.value;
pre.left = node.right;
}
}
return t;
}
}
? 方法測試:
public class Main {
public static void main(String[] args) {
BSTree<Integer, Integer> tree = new BSTree<>();
// tree.insertByRecursion(1, 1);
// tree.insertByRecursion(5, 1);
// tree.insertByRecursion(2, 1);
// tree.insertByRecursion(4, 1);
// tree.insertByRecursion(3, 1);
// tree.insertByRecursion(1, 2);
tree.insertByIteration(1, 1);
tree.insertByIteration(5, 1);
tree.insertByIteration(2, 1);
tree.insertByIteration(4, 1);
tree.insertByIteration(3, 1);
tree.insertByIteration(1, 5);
tree.removeByRecursion(4);
tree.inorderTraverse();
tree.postorderTraverse();
tree.preorderTraverse();
BSTNode<Integer, Integer> node = tree.searchByIteration(1);
System.out.println(node.key + " " + node.value);
}
}
中序遍歷:(key: 1 , value: 5) (key: 2 , value: 1) (key: 3 , value: 1) (key: 5 , value: 1)
後序遍歷:(key: 3 , value: 1) (key: 2 , value: 1) (key: 5 , value: 1) (key: 1 , value: 5)
先序遍歷:(key: 1 , value: 5) (key: 5 , value: 1) (key: 2 , value: 1) (key: 3 , value: 1)
1 5