今天看了算法导论的第12章《二叉搜索树》,觉得里面的过程的伪代码很精巧,过程讲解也很仔细易懂,所以就写下这个二叉搜索树模板。
树类数据结构的关键操作是插入和删除,查找和遍历相对而言技巧性和难度一般吧。
插入
在一个while循环里,从root开始不断往下走(根据左小右大的特点),找到要插入的位置,插进去就好了。
找要插入的位置的过程其实跟查找是一样的。
插入后不用管树平衡与否,所以还是很简单的。
一个小细节:注意插入第一个结点的时候,要更新root!
bool insert(const T& value) {
TreeNode<T> *lastNode = NULL, *now = m_root, *toInsert = new TreeNode<T>(value);
while (now != NULL) {
lastNode = now;
if (value == now->value)
return false;
else if (value < now->value)
now = now->left;
else
now = now->right;
}
toInsert->parent = lastNode;
if (lastNode == NULL) {
m_root = toInsert;
}
else if (value < lastNode->value) {
lastNode->left = toInsert;
}
else {
lastNode->right = toInsert;
}
return true;
}
删除
对要删除的结点node分四种情况(但是实现的时候不需要这样划分,具体见代码):
1. node是叶子结点,直接删了,不会影响搜索树的性质,要更新其父结点的孩子(也就是自己相当于变成NULL了);
2. node只有左孩子或只有右孩子,这个也很简单,删除相当于把左/右孩子提起来,放在自己原来的位置就好了;
3. node既有左孩子也有右孩子,需要找到树中比node的值大的结点中,值最小的一个来替代node(想想为什么),其实只需要从node的右孩子一直往左走就可以了(见getTreeMinimum函数),这也是二叉搜索树的特性决定的!
综合上面的分析,每一种情况都需要把某个结点来替换另外一个结点的位置,所以就产生了transplant辅助函数了!transplant的意思是手术、移植,一个结点其实代表了以它为根的子树,所以相当于把一棵子树移植到另一棵子树的位置上去,它的实现也很容易理解:
void transplant(TreeNode<T>* from, TreeNode<T>* to) {
if (from->parent == NULL) {
m_root = to;
} else if (from == from->parent->left) {
from->parent->left = to;
} else {
from->parent->right = to;
}
if (to != NULL) {
to->parent = from->parent;
}
}
下面是删除逻辑的实现:
bool deleteNode(TreeNode<T>* node) {
if (node == NULL)
return false;
if (node->left == NULL) {
transplant(node, node->right);
} else if (node->right == NULL) {
transplant(node, node->left);
} else {
TreeNode<T>* newSubRoot = getTreeMinimum(node->right);
if (newSubRoot->parent != node) {
transplant(newSubRoot, newSubRoot->right);
newSubRoot->right = node->right;
node->right->parent = newSubRoot;
}
transplant(node, newSubRoot);
newSubRoot->left = node->left;
node->left->parent = newSubRoot;
}
delete node;
return true;
}
所有代码
// BinarySearchTree.h
#ifndef __BINARY_SEARCH_TREE_H__
#define __BINARY_SEARCH_TREE_H__
#include <stdlib.h>
#include <iostream>
using namespace std;
template<typename T>
struct TreeNode
{
T value;
TreeNode *left, *right, *parent;
TreeNode(const T& value)
: value(value), left(NULL), right(NULL), parent(NULL) {}
};
template<typename T>
class BinarySearchTree
{
public:
BinarySearchTree() {
m_root = NULL;
}
size_t height() const {
return getHeight(m_root);
}
// 返回是否插入成功,若有重复则插入失败,则只保留已有的结点
bool insert(const T& value) {
TreeNode<T> *lastNode = NULL, *now = m_root, *toInsert = new TreeNode<T>(value);
while (now != NULL) {
lastNode = now;
if (value == now->value)
return false;
else if (value < now->value)
now = now->left;
else
now = now->right;
}
toInsert->parent = lastNode;
if (lastNode == NULL) {
m_root = toInsert;
}
else if (value < lastNode->value) {
lastNode->left = toInsert;
}
else {
lastNode->right = toInsert;
}
return true;
}
// 返回是否删除成功,不成功的原因是:没有这个值
bool deleteNode(const T& value) {
TreeNode<T> *now = m_root;
while (now != NULL) {
if (value == now->value)
return deleteNode(now);
else if (value < now->value)
now = now->left;
else
now = now->right;
}
return false;
}
bool deleteNode(TreeNode<T>* node) {
if (node == NULL)
return false;
if (node->left == NULL) {
transplant(node, node->right);
} else if (node->right == NULL) {
transplant(node, node->left);
} else {
TreeNode<T>* newSubRoot = getTreeMinimum(node->right);
if (newSubRoot->parent != node) {
transplant(newSubRoot, newSubRoot->right);
newSubRoot->right = node->right;
node->right->parent = newSubRoot;
}
transplant(node, newSubRoot);
newSubRoot->left = node->left;
node->left->parent = newSubRoot;
}
// delete node;
return true;
}
// 若返回NULL则表示找不到
TreeNode<T>* find(const T& value) const {
TreeNode<T> *now = m_root;
while (now != NULL) {
if (value == now->value)
return now;
else if (value < now->value)
now = now->left;
else
now = now->right;
}
return NULL;
}
// 前序输出
void display(char spliter=' ') const {
if (m_root == NULL) {
cout << "NULL" << endl;
return;
}
displayHelper(m_root, spliter);
cout << endl;
}
private:
void transplant(TreeNode<T>* from, TreeNode<T>* to) {
if (from->parent == NULL) {
m_root = to;
} else if (from == from->parent->left) {
from->parent->left = to;
} else {
from->parent->right = to;
}
if (to != NULL) {
to->parent = from->parent;
}
}
size_t getHeight(const TreeNode<T>* root) const {
if (root == NULL)
return 0;
size_t left_height = getHeight(root->left);
size_t right_height = getHeight(root->right);
return max(left_height, right_height) + 1;
}
void displayHelper(TreeNode<T>* root, char spliter) const {
if (root == NULL)
return;
displayHelper(root->left, spliter);
cout << root->value << spliter;
displayHelper(root->right, spliter);
}
TreeNode<T>* getTreeMinimum(TreeNode<T>* root) const {
TreeNode<T>* now = root;
while (now->left != NULL)
now = now->left;
return now;
}
private:
TreeNode<T>* m_root;
};
#endif
下面是测试和使用的例子:
#include "BinarySearchTree.h"
#include <iostream>
#include <stdlib.h>
#include <ctime>
using namespace std;
inline int randInt() {
static const int MAX = 9;
return rand() % MAX;
}
int main() {
srand(time(0));
BinarySearchTree<int> bst;
for (int i = 0; i < 10; ++i)
bst.insert(randInt());
bst.display();
cout << "tree height = " << bst.height() << endl << endl;
for (int i = 0; i < 10; ++i) {
bool result = bst.deleteNode(i);
if (result)
cout << "delete success " << i << endl;
else
cout << "delete fail " << i << endl;
bst.display();
cout << endl;
}
return 0;
}