/**/ /****************************************** * 文件名:BSTree.cpp * 功能:1,定义二叉查找树结点 2,定义二叉查找树 3,测试二叉查找树******************************************/ #include < iostream > using namespace std;template < typename T > class BSTnode ... {public: BSTnode():pare(0),left(0),right(0) ...{ } BSTnode(const T &v, BSTnode<T> *p = 0, BSTnode<T> *lef = 0, BSTnode<T> *rig = 0):value(v),pare(p),left(lef),right(rig) ...{ }public: T value; BSTnode<T> *pare, *left, *right;} ; // 以上是二叉查找树结点的定义 // 以下是二叉查找树的定义 template < typename T > class BSTree ... {public: BSTnode<T> *root;public: BSTree():root(0) ...{} BSTree(const BSTnode<T> &r):root(&r) ...{} BSTree(const T &v) ...{ root = new BSTnode<T>(v); } void Insert_BST(const T &v) ...{ if( root == 0 ) root = new BSTnode<T>(v); else ...{ BSTnode<T> *curr = root; BSTnode<T> *p = 0; while(curr != 0) ...{ p = curr; curr = (v > curr->value) ? curr->right : curr->left; } curr = new BSTnode<T>(v); curr->pare = p; if( curr->value > p->value ) p->right = curr; else p->left = curr; } // else } // Insert_BST BSTnode<T> *search_BST(const T &v) ...{ BSTnode<T> *curr = root; while( (curr != 0) && (v != curr->value) ) ...{ curr = (v > curr->value) ? curr->right : curr->left; } return curr; } // search_BST T mini_BST(BSTnode<T> *r) const ...{ BSTnode<T> *curr = r; while( curr->left != 0 ) curr = curr->left; return curr->value; } T max_BST(BSTnode<T> *r) const ...{ BSTnode<T> *curr = r; while(curr->right != 0) curr = curr->right; return curr->value; } T next_BST(const T &v) ...{ BSTnode<T> *curr; curr = search_BST(v); if(curr->right != 0) return mini_BST(curr->right); BSTnode<T> *p = curr->pare; while((p != 0) && (curr == p->right)) ...{ curr = p; p = p->pare; } return p->value; } T prev_BST(const T &v) ...{ BSTnode<T> *curr; curr = search_BST(v); if(curr->left != 0) return max_BST(curr->left); BSTnode<T> *p = curr->pare; while((p != 0) && (curr == p->left)) ...{ curr = p; p = curr->pare; } return p->value; } BSTnode<T> *del_BST(BSTnode<T> *r, const T &v) ...{ BSTnode<T> *curr, *y, *x; curr = 0; y = 0; x = 0; curr = search_BST(v); if( curr == 0 ) return 0; if((curr->left == 0) || (curr->right == 0)) y = curr; else y = search_BST(next_BST(v)); if( y->left != 0 ) x = y->left; else x = y->right; if(x != 0) x->pare = y->pare; if(y->pare == 0) r = x; else if( y == y->pare->left ) y->pare->left = x; else y->pare->right = x; if( y != curr ) curr->value = y->value; return y; } // 中序遍历BSTree 输出每个结点值 void InOrder_BST(BSTnode<T> *r) ...{ if(r != 0) ...{ InOrder_BST(r->left); cout << r->value << ' '; InOrder_BST(r->right); } }} ; /**/ /*************************************************** 14 / 3 17 / / 2 5 15 18 / 4 7***************************************************/ int main() ... { BSTree<int> a(14); a.Insert_BST(3); a.Insert_BST(2); a.Insert_BST(5); a.Insert_BST(4); a.Insert_BST(7); a.Insert_BST(17); a.Insert_BST(15); a.Insert_BST(18); a.InOrder_BST(a.root); cout << endl; // 2 3 4 5 7 14 15 17 18 cout << a.prev_BST(5) << " <- 5 -> " << a.next_BST(5) << endl; // 4 <- 5 -> 7 cout << a.search_BST(5)->value << endl; // 5 cout << a.del_BST(a.root, 3)->value << endl; // 7 a.InOrder_BST(a.root); // 2 3 4 7 14 15 17 18 return 0;}