二叉查找樹又稱二叉搜索樹、有序二叉樹、排序二叉樹。它是特殊的二叉樹:對任意結點 x,其左子樹上所有結點的鍵值都不大於 key[x],右子樹上所有結點的鍵值都不小於 key[x]。
由此可歸納出四條性質:
1)左子樹非空時,左子樹上所有結點的值均小於根結點;
2)右子樹非空時,右子樹上所有結點的值均大於根結點;
3)左右子樹本身也是二叉查找樹;
4)不存在鍵值相等的結點。
template<class T>
class BSTNode
{
public:
T key; // 關鍵字(鍵值)
BSTNode *left; // 左孩子
BSTNode *right; // 右孩子
BSTNode *parent; // 父結點
BSTNode(T value, BSTNode *p, BSTNode *l, BSTNode *r):
key(value), parent(p), left(l), right(r) {}
};
template<class T>
class BSTree
{
private:
BSTNode<T> *root; // 根結點
public:
BSTree() {};
~BSTree() {};
void preOrder(); // 前序遍歷
void inOrder(); // 中序遍歷
void postOrder(); // 後序遍歷
BSTNode<T>* search(T key); // 遞歸查找
BSTNode<T>* iterativeSearch(T key); // 非遞歸查找
T minimum(); // 最小鍵值
T maximum(); // 最大鍵值
BSTNode<T>* successor(BSTNode<T> *x); // 後繼
BSTNode<T>* predecessor(BSTNode<T> *x); // 前驅
void insert(T key); // 插入
void remove(T key); // 刪除
void destroy(); // 銷毀
void print(); // 打印結構
private:
// 對外接口的內部實現(重載版本)
void preOrder(BSTNode<T> *tree) const;
void inOrder(BSTNode<T> *tree) const;
void postOrder(BSTNode<T> *tree) const;
BSTNode<T>* search(BSTNode<T> *x, T key) const;
BSTNode<T>* iterativeSearch(BSTNode<T> *x, T key) const;
BSTNode<T>* minimum(BSTNode<T> *tree);
BSTNode<T>* maximum(BSTNode<T> *tree);
void insert(BSTNode<T>* &tree, BSTNode<T> *z);
BSTNode<T>* remove(BSTNode<T>* &tree, BSTNode<T> *z);
void destroy(BSTNode<T>* &tree);
void print(BSTNode<T>* &tree, T key, int direction);
};
// 前序:根 → 左 → 右
template<class T>
void BSTree<T>::preOrder(BSTNode<T> *tree) const
{
if (tree != NULL)
{
cout << tree->key << " ";
preOrder(tree->left);
preOrder(tree->right);
}
}
// 中序:左 → 根 → 右(二叉查找樹中序即有序序列)
template<class T>
void BSTree<T>::inOrder(BSTNode<T> *tree) const
{
if (tree != NULL)
{
inOrder(tree->left);
cout << tree->key << " ";
inOrder(tree->right);
}
}
// 後序:左 → 右 → 根
template<class T>
void BSTree<T>::postOrder(BSTNode<T> *tree) const
{
if (tree != NULL)
{
postOrder(tree->left);
postOrder(tree->right);
cout << tree->key << " ";
}
}
// 遞歸
template<class T>
BSTNode<T>* BSTree<T>::search(BSTNode<T>* x, T key) const
{
if (x == NULL || x->key == key)
return x;
if (key < x->key)
return search(x->left, key);
else
return search(x->right, key);
}
// 非遞歸,通常效率更高
template<class T>
BSTNode<T>* BSTree<T>::iterativeSearch(BSTNode<T>* x, T key) const
{
while (x != NULL && x->key != key)
{
if (key < x->key)
x = x->left;
else
x = x->right;
}
return x;
}
一路往右到底就是最大值,一路往左到底就是最小值:
template<class T>
BSTNode<T>* BSTree<T>::maximum(BSTNode<T> *tree)
{
if (tree == NULL) return NULL;
while (tree->right != NULL)
tree = tree->right;
return tree;
}
template<class T>
BSTNode<T>* BSTree<T>::minimum(BSTNode<T> *tree)
{
if (tree == NULL) return NULL;
while (tree->left != NULL)
tree = tree->left;
return tree;
}
template<class T>
BSTNode<T>* BSTree<T>::predecessor(BSTNode<T> *x)
{
// 有左孩子:左子樹中的最大結點
if (x->left != NULL)
return maximum(x->left);
// 無左孩子:向上找到第一個「x 在其右子樹中」的祖先
BSTNode<T> *p = x->parent;
while (p != NULL && x == p->left)
{
x = p;
p = p->parent;
}
return p;
}
template<class T>
BSTNode<T>* BSTree<T>::successor(BSTNode<T> *x)
{
// 有右孩子:右子樹中的最小結點
if (x->right != NULL)
return minimum(x->right);
// 無右孩子:向上找到第一個「x 在其左子樹中」的祖先
BSTNode<T> *p = x->parent;
while (p != NULL && x == p->right)
{
x = p;
p = p->parent;
}
return p;
}
template<class T>
void BSTree<T>::insert(BSTNode<T>* &tree, BSTNode<T> *z)
{
BSTNode<T> *y = NULL;
BSTNode<T> *x = tree;
// 找到插入位置
while (x != NULL)
{
y = x;
if (z->key < x->key)
x = x->left;
else
x = x->right;
// 若禁止重複鍵值,可在此釋放 z 並返回
}
z->parent = y;
if (y == NULL)
tree = z; // 空樹,z 成為根
else if (z->key < y->key)
y->left = z;
else
y->right = z;
}
template<class T>
void BSTree<T>::insert(T key)
{
BSTNode<T> *z = new BSTNode<T>(key, NULL, NULL, NULL);
if (z == NULL) return;
insert(root, z);
}
刪除分三種情況:葉子結點直接刪;只有一個孩子則讓孩子頂上;左右孩子都存在時,用後繼結點 y 的鍵值覆蓋 z,再刪除 y(y 沒有左孩子,處理起來最省事)。
template<class T>
BSTNode<T>* BSTree<T>::remove(BSTNode<T>* &tree, BSTNode<T> *z)
{
BSTNode<T> *x = NULL;
BSTNode<T> *y = NULL;
if (z->left == NULL || z->right == NULL)
y = z;
else
y = successor(z);
if (y->left != NULL)
x = y->left;
else
x = y->right;
if (x != NULL)
x->parent = y->parent;
if (y->parent == NULL)
tree = x;
else if (y == y->parent->left)
y->parent->left = x;
else
y->parent->right = x;
if (y != z)
z->key = y->key; // 用後繼的鍵值覆蓋
return y;
}
template<class T>
void BSTree<T>::remove(T key)
{
BSTNode<T> *z, *node;
if ((z = search(root, key)) != NULL)
if ((node = remove(root, z)) != NULL)
delete node;
}
template<class T>
void BSTree<T>::print(BSTNode<T> *tree, T key, int direction)
{
if (tree != NULL)
{
if (direction == 0)
cout << setw(2) << tree->key << "is root" << endl;
else
cout << setw(2) << tree->key << "is" << setw(2) << key << "'s"
<< setw(12) << (direction == 1 ? "right child" : "left child") << endl;
print(tree->left, tree->key, -1);
print(tree->right, tree->key, 1);
}
}
template<class T>
void BSTree<T>::destroy(BSTNode<T> *&tree)
{
if (tree == NULL) return;
destroy(tree->left);
destroy(tree->right);
delete tree;
tree = NULL;
}
把上面各段拼進 BSTree.h(記得補上 #include<iomanip> 與頭文件衛哨),就得到完整的實現文件,此處不再重複貼一遍。
#include<iostream>
#include "BSTree.h"
using namespace std;
static int arr[] = {1, 5, 4, 3, 2, 6};
int main()
{
int i, len;
BSTree<int> *tree = new BSTree<int>();
cout << "依次添加:";
len = sizeof(arr) / sizeof(arr[0]);
for (i = 0; i < len; ++i)
{
cout << arr[i] << " ";
tree->insert(arr[i]);
}
cout << "\n前序遍歷:"; tree->preOrder();
cout << "\n中序遍歷:"; tree->inOrder();
cout << "\n後序遍歷:"; tree->postOrder();
cout << endl;
cout << "最小值:" << tree->minimum() << endl;
cout << "最大值:" << tree->maximum() << endl;
tree->print();
cout << "\n刪除節點:" << arr[3];
tree->remove(arr[3]);
cout << "\n中序遍歷:"; tree->inOrder();
cout << endl;
tree->destroy();
return 0;
}
中序遍歷的結果應該是升序序列,這也是驗證二叉查找樹是否正確建起來的最快方法。實用場景中要留意:二叉查找樹在插入有序數據時會退化成鏈表,此時需要 AVL、紅黑樹等自平衡方案。