二叉查找树的性质:对于树中的每个节点x,它的左子树中所有项的值不大于x的值,它的右子树中所有项的值不小于x的值。
二叉查找树应具有以下操作
① 寻找最小项 FIND_MIN
② 寻找最大项 FIND_MAX
③ 是否包含 CONTAINS
④ 树是否为空 IS_EMPTY
⑤ 清空树 MAKE_EMPTY
⑥ 插入节点 INSERT
⑦ 移除节点 REMOVE
⑧ 打印树 PRINT_TREE (中序遍历)
⑨ 深拷贝 DEEP_CLONE
二叉查找树(binary search tree)的抽象数据类型(abstract data type)见如下类模板
1 #include <iostream> 2 3 using namespace std; 4 5 template <typename Comparable> 6 class BinarySearchTree 7 { 8 public: 9 BinarySearchTree() { root = NULL; } 10 BinarySearchTree(const BinarySearchTree &rhs) { root = deepClone(rhs.root); } 11 ~BinarySearchTree() { makeEmpty(); } 12 13 bool contains(const Comparable &x) const { return contains(x, root); } 14 const Comparable& findMin() const { return findMin(root); } 15 16 const Comparable& findMax() const { return findMax(root); } 17 bool isEmpty() const { return (!root)?true:false; } 18 19 void makeEmpty() { makeEmpty(root); } 20 void insert(const Comparable &x) { insert(x, root); } 21 void remove(const Comparable &x) { remove(x, root); } 22 23 void printTree() const { printTree(root); cout << endl; } 24 25 const BinarySearchTree& operator=(const BinarySearchTree &rhs) 26 { 27 if (this != &rhs) 28 { 29 makeEmpty(); 30 root = deepClone(rhs.root); 31 } 32 return *this; 33 } 34 35 private: 36 struct BinaryNode 37 { 38 Comparable element; 39 BinaryNode *left; 40 BinaryNode *right; 41 42 BinaryNode(const Comparable &theElement, BinaryNode *lt, BinaryNode *rt): element(theElement),left(lt),right(rt) { } 43 }; 44 45 BinaryNode *root; 46 47 48 BinaryNode * findMin(BinaryNode *t) const 49 { 50 if (!t) 51 return NULL; 52 if (!t->left) 53 return t; 54 return findMin(t->left); 55 } 56 57 BinaryNode * findMax(BinaryNode *t) const 58 { 59 if (!t) 60 while (!t->right) 61 t = t->right; 62 return t; 63 } 64 65 bool contains(const Comparable &x, BinaryNode *t) const 66 { 67 if (!t) 68 return false; 69 else if (x < t->element) 70 return contains(x, t->left); 71 else if (x > t->element) 72 return contains(x, t->right); 73 else 74 return true; 75 } 76 77 void makeEmpty(BinaryNode *&t) 78 { 79 if (t) 80 { 81 makeEmpty(t->left); 82 makeEmpty(t->right); 83 delete t; 84 } 85 t = NULL; 86 } 87 88 void insert(const Comparable &x, BinaryNode *&t) const 89 { 90 if (!t) 91 t = new BinaryNode(x, NULL, NULL); 92 else if (x < t->element) 93 insert(x, t->left); 94 else if (x > t->element) 95 insert(x, t->right); 96 else 97 ; 98 } 99 100 void remove(const Comparable &x, BinaryNode *&t) const 101 { 102 if (!t) 103 return; 104 if (x < t->element) 105 remove(x, t->left); 106 else if (x > t->element) 107 remove(x, t->right); 108 else if (!t->left && !t->right) 109 { 110 t->element = findMin(t->right)->element; 111 remove(t->element, t->right); 112 } 113 else 114 { 115 BinaryNode *oldNode = t; 116 t = (!t->left)?t->left:t->right; 117 delete oldNode; 118 } 119 } 120 121 void printTree(BinaryNode *t) const 122 { 123 if (!t) 124 return; 125 printTree(t->left); 126 cout << t->element << " "; 127 printTree(t->right); 128 } 129 130 BinaryNode * deepClone(BinaryNode *t) const 131 { 132 if (!t) 133 return NULL; 134 return new BinaryNode(t->element, deepClone(t->left), deepClone(t->right)); 135 } 136 };
main.cpp 如下
#include <ctime> #include "BinarySearchTree.hpp" using namespace std; const int LENGTH = 18; void generateTree(BinarySearchTree<int> *tree) { srand((unsigned int)time(NULL)); int i = LENGTH; while(i--) { tree->insert(rand() % 100); } } int main() { BinarySearchTree<int> *tree = new BinarySearchTree<int>(); generateTree(tree); tree->printTree(); }
结果: