二分查詢樹(Binary Search Tree)的基本操作有搜尋、求最大值、求最小值、求前繼、求後繼、插入及刪除。

對二分查詢樹的進行基本操作所花費的時間與樹的高度成比例。例如有n個節點的完全二叉樹,對它進行的基本操作的時間複雜度為O(logn)。然而,如果樹是一個有n個節點的線性的鏈,則在這種情況下的時間複雜度為O(n)。

1、什麼是二分查詢樹

二分查詢樹是一種有組織的二叉樹。我們可以通過連結節點表示這樣一棵樹。每個節點包含鍵(key),資料(data),左子節點(left),右子節點(right),父節點(parent)這五種屬性。

如果一個節點沒有父節點或某個子結點,則其對應的屬性的值為null。

建立一個Node.java檔案,用如下程式碼表示節點:

public class Node {
public int key;
public int data;
public Node left;
public Node right;
public Node parent; public Node(){} public Node(int key) {
this.key = key;
}
}

建立一個BinarySearchTree.java檔案,用如下程式碼表示二分查詢樹:

public class BinarySearchTree {
public Node root;
}

接下來,會逐步補充程式碼。

對於儲存在二分查詢樹中的鍵,必須滿足下面這個條件(二分查詢樹特點)

對於二叉樹中的任一節點n,如果left是n左子樹中的任何一個節點,有left.key <= n.key;如果right是n右子樹中的任一節點,則有n.key<=right.key。

如果我們中序遍歷二分查詢樹,能升序打印出所有的鍵(key)。

可在BinarySearchTree.java檔案中加上如下程式碼實現中序遍歷:

	private void innerWalk(Node node) {
if(node != null) {
innerWalk(node.left);
System.out.print(node.key + " ");
innerWalk(node.right);
}
} public void innerWalk() {
this.innerWalk(this.root);
System.out.println();
}

2、查詢二分查詢樹

2.1、搜尋

在二分查詢樹中搜索鍵為key的結點,如果找到,則返回該結點,否則,返回null。可利用二分查詢樹的特性來查詢。

可在BinarySearchTree.java檔案中加上如下程式碼實現搜尋:

	public Node search(int key) {
Node pointer = this.root;
while (pointer != null && pointer.key != key) {
pointer = key < pointer.key ? pointer.left : pointer.right;
}
return pointer;
}

2.2、最小值與最大值

求鍵最小的節點,可根據二叉樹的特點,從根結點開始,遍歷跟蹤其左子節點,直到最後一個。

可在Node.java檔案中加上如下程式碼實現:

    public Node minimum() {
Node pointer = this;
while(pointer.left != null){
pointer = pointer.left;
}
return pointer;
}

在BinarySearchTree.java檔案中加上如下程式碼:

 	public Node minimum() {
return this.root.minimum();
}

求鍵最大的節點,可根據二叉樹的特點,從根結點開始,遍歷跟蹤其右子節點,直到最後一個。

可在Node.java檔案中加上如下程式碼實現:

    public Node maximum() {
Node pointer = this;
while(pointer.right != null) {
pointer = pointer.right;
}
return pointer;
}

在BinarySearchTree.java檔案中加上如下程式碼:

 	public Node minimum() {
return this.root.maximum();
}

2.3、前驅與後繼

給定一個二分查詢樹中的節點,有時我們需要發現它在樹中所有結點按鍵升序排序的情況下的直接後繼(後面第一個)。

如果樹中所有的鍵是不同的,結點n的直接後繼是大於n.key的最小結點。

如果該結點有右節點,則其直接後繼是其右子樹鍵最小的那個結點。如果該結點則判斷該結點是其父節點的左子節點還是其右子節點。

如果是左子節點,則返回其父節點。如果是右子節點,判斷其父節點的其父節點的父節點的左子節點還是右子節點,以此類推。

如果找不到,則返回null。

可在Node.java檔案中加上如下實現程式碼:

    public Node successor() {
Node pointer = this;
if (pointer.right != null)
return pointer.right.minimum();
Node parentPointer = pointer.parent;
while (parentPointer != null && parentPointer.right == pointer) {
pointer = parentPointer;
parentPointer = parentPointer.parent;
}
return pointer;
}

一個節點的直接前繼是指樹中所有結點按鍵升序排序的情況下,該節點的前面第一個。求直接前繼的與求直接後繼是對稱的。

可在Node.java檔案中加上如下實現程式碼:

    public Node predecessor() {
Node pointer = this;
if (pointer.left != null)
return pointer.left.maximum();
Node parentPointer = pointer.parent;
while (parentPointer != null && parentPointer.left == pointer) {
pointer = parentPointer;
parentPointer = parentPointer.parent;
}
return pointer;
}

3、插入與刪除

插入與刪除會導致二分查詢樹發生改變,但必須保持其特點。

3.1、插入

在樹中找到一個鍵(key)最接近要插入結點鍵(key)的結點,且有可以插入的位置,然後插入合適的位置。

可在BinarySearchTree.java檔案中加上如下實現程式碼:

    public void insert(Node node) {
Node pointer = this.root;
Node parentPointer = null;
while (pointer != null) {
parentPointer = pointer;
pointer = node.key < pointer.key ? pointer.left : pointer.right;
}
node.parent = parentPointer;
if (parentPointer == null)
this.root = node;
else if (node.key < parentPointer.key) {
parentPointer.left = node;
} else {
parentPointer.right = node;
}
}

3.2、刪除

刪除節點後,我門要保持二分查詢樹的特點。

如果要刪除節點沒有任何子節點,直接可以刪除它,不用做任何處理。

如果要刪除節點只有一個左子樹,可以用左子樹的根節點代替要刪除節點,也可以用左子樹中鍵(key)最大的節點來代替要刪除節點。

如果要刪除結點只有一個右子樹,可以用右子樹的根節點代替要刪除節點,也可以用右子樹中鍵(key)最小的結點來代替要刪除結點。

如果要刪除結點既有左子樹,又有右子樹,可以用左子樹中鍵(key)最大的結點代替要刪除結點,也可以用右子樹中鍵最小的結點代替要刪除結點。

假設要從二分查詢樹tree中刪除節點node,一種刪除方法的具體步驟如下:

  1. 如果node沒有左子節點,然後我們可以用node的右子節node.right點代替node,node.right可能是或可能不是null。
  2. 如果node只有左子節點node.left,則我們用node.left代替node。
  3. 如果node有左子節點node.left和右子節點node.right。我麼要發現node的直接後繼s(node右子樹中鍵最小的結點,它沒有左子節點),s在node右子樹中,然後用s代替node。這裡要考慮下面兩種情況:
    1. 如果s是node的右子節點,可以用s代替node。
    2. 否則,用s的右子節點代替s,然後再用s代替node。

由於我們需要在二分查詢樹中替換子樹的方法,可以先寫一個替換子樹的方法。

可在BinarySearchTree.java檔案中加上如下實現程式碼:

    /**
* 用子樹node2代替代替子樹node1
*
* @param node1
* @param node2
*/
private void transplant(Node node1, Node node2) {
if (node1.parent == null) {
this.root = node2;
} else if (node1.parent.left == node1) {
node1.parent.left = node2;
} else {
node1.parent.right = node2;
} if (node2 != null)
node2.parent = node1.parent;
node1.parent = null;
}

接下來解釋刪除節點操作的程式碼實現。

可在BinarySearchTree.java檔案中加上如下實現程式碼:

    public void delete(Node node) {
if (node.left == null) {
this.transplant(node, node.right);
} else if (node.right == null) {
this.transplant(node, node.left);
} else {
Node successor = node.successor();
if (successor.parent != node) {
this.transplant(successor, successor.right);
successor.right = node.right;
successor.right.parent = successor;
}
this.transplant(node, successor);
successor.left = node.left;
successor.left.parent = successor;
}
}

4、完整程式碼

Node.java檔案

public class Node {
public int key;
public int data;
public Node left;
public Node right;
public Node parent; public Node() {
} public Node(int key) {
this.key = key;
} public Node minimum() {
Node pointer = this;
while (pointer.left != null)
pointer = pointer.left;
return pointer;
} public Node maximum() {
Node pointer = this;
while (pointer.right != null)
pointer = pointer.right;
return pointer;
} public Node successor() {
Node pointer = this;
if (pointer.right != null)
return pointer.right.minimum();
Node parentPointer = pointer.parent;
while (parentPointer != null && parentPointer.right == pointer) {
pointer = parentPointer;
parentPointer = parentPointer.parent;
}
return pointer;
} public Node predecessor() {
Node pointer = this;
if (pointer.left != null)
return pointer.left.maximum();
Node parentPointer = pointer.parent;
while (parentPointer != null && parentPointer.left == pointer) {
pointer = parentPointer;
parentPointer = parentPointer.parent;
}
return pointer;
}
}

BinarySearchTree.java檔案

public class BinarySearchTree {
public Node root; private void innerWalk(Node node) {
if (node != null) {
innerWalk(node.left);
System.out.print(node.key + " ");
innerWalk(node.right);
}
} public void innerWalk() {
this.innerWalk(this.root);
System.out.println();
} public Node search(int key) {
Node pointer = this.root;
while (pointer != null && pointer.key != key) {
pointer = key < pointer.key ? pointer.left : pointer.right;
}
return pointer;
} public Node minimum() {
return this.root.minimum();
} public Node maximum() {
return this.root.maximum();
} public void insert(Node node) {
Node pointer = this.root;
Node parentPointer = null;
while (pointer != null) {
parentPointer = pointer;
pointer = node.key < pointer.key ? pointer.left : pointer.right;
}
node.parent = parentPointer;
if (parentPointer == null)
this.root = node;
else if (node.key < parentPointer.key) {
parentPointer.left = node;
} else {
parentPointer.right = node;
}
} /**
* 用子樹node2代替代替子樹node1
*
* @param node1
* @param node2
*/
private void transplant(Node node1, Node node2) {
if (node1.parent == null) {
this.root = node2;
} else if (node1.parent.left == node1) {
node1.parent.left = node2;
} else {
node1.parent.right = node2;
} if (node2 != null)
node2.parent = node1.parent;
node1.parent = null;
} public void delete(Node node) {
if (node.left == null) {
this.transplant(node, node.right);
} else if (node.right == null) {
this.transplant(node, node.left);
} else {
Node successor = node.successor();
if (successor.parent != node) {
this.transplant(successor, successor.right);
successor.right = node.right;
successor.right.parent = successor;
}
this.transplant(node, successor);
successor.left = node.left;
successor.left.parent = successor;
}
}
}

5、演示

演示程式碼

public class Test01 {
public static void main(String[] args) {
Node n1 = new Node(1);
Node n2 = new Node(2);
Node n3 = new Node(3);
Node n4 = new Node(4);
Node n5 = new Node(5); BinarySearchTree bst = new BinarySearchTree();
bst.insert(n3);
bst.insert(n4);
bst.insert(n2);
bst.insert(n1);
bst.insert(n5); System.out.println("bst.minimum().key: " + bst.minimum().key);
System.out.println("bst.maximum().key: " + bst.maximum().key);
System.out.println("n3.successor().key: " + n3.successor().key);
System.out.println("n3.predecessor().key: " + n3.predecessor().key);
System.out.println("bst.search(4).key: " + bst.search(4).key); System.out.print("tree: ");
bst.innerWalk();
System.out.print("delete n3: ");
bst.delete(n3);
bst.innerWalk();
System.out.print("delete n2: ");
bst.delete(n2);
bst.innerWalk();
System.out.print("delete n1: ");
bst.delete(n1);
bst.innerWalk();
System.out.print("delete n4: ");
bst.delete(n4);
bst.innerWalk();
System.out.print("delete n5: ");
bst.delete(n5);
bst.innerWalk();
}
}

結果

bst.minimum().key: 1
bst.maximum().key: 5
n3.successor().key: 4
n3.predecessor().key: 2
bst.search(4).key: 4
tree: 1 2 3 4 5
delete n3: 1 2 4 5
delete n2: 1 4 5
delete n1: 4 5
delete n4: 5
delete n5: