标签:
近日研习《计算机算法》,一点点弄明白了二叉搜索树。
/* @header BSTree @abstract BSTree @discussion Insert,delete,search and order @author WalterBright,2016 */ #include <iostream> using namespace std; template <class Type> class BSTree; //模板类作为友元类需提前声明 template <class Type> class TreeNode{ public: friend class BSTree<Type>; private: TreeNode *lChild; TreeNode *rChild; Type data; }; template <class Type> class BSTree{ public: BSTree(){ tree=NULL; } ~BSTree(){ delete tree; } TreeNode<Type>* Search(Type x); TreeNode<Type>* ISearch(Type x); void Insert(Type x); void Delete(Type x); void InOrder(); //中序遍历 void PreOrder(); //先序遍历 void PostOrder(); //后序遍历 (等价于先搜索右时的先序的反序) private: TreeNode<Type> *tree; TreeNode<Type>* Search(TreeNode<Type> *t,Type x); void Delete(TreeNode<Type> *t,Type x); void Visit(TreeNode<Type> *t); void InOrder(TreeNode<Type> *t); void PreOrder(TreeNode<Type> *t); void PostOrder(TreeNode<Type> *t); }; template <typename Type> TreeNode<Type>* BSTree<Type>::Search(Type x){ return Search(tree,x); } template <typename Type> TreeNode<Type>* BSTree<Type>::Search(TreeNode<Type> *t,Type x){ bool found=false; while(t&&!found){ if(x==t->data){ found=true; } else if(x<t->data){ t=t->lChild; } else{ t=t->rChild; } } if(found){ return t; } else{ return NULL; } } template <typename Type> void BSTree<Type>::Visit(TreeNode<Type> *t){ cout<<t->data<<‘ ‘; } template <typename Type> void BSTree<Type>::Insert(Type x){ bool found=false; TreeNode<Type> *p=tree,*q; //q是p的父节点 while((p)&&(!found)){ q=p; //保证了p接下来往下走 if(x==p->data){ found=true; } else if(x<p->data){ p=p->lChild; } else{ p=p->rChild; } } if(!found){ p=new TreeNode<Type>; p->lChild=p->rChild=NULL; p->data=x; if(tree){ if(x<q->data){ q->lChild=p; } else{ q->rChild=p; } } else{ tree=p; } } } template <typename Type> void BSTree<Type>::Delete(Type x){ Delete(tree,x); } template <typename Type> void BSTree<Type>::Delete(TreeNode<Type> *t,Type x){ bool found=false; TreeNode<Type> *p=t,*q; while((p)&&(!found)){ if(x==p->data){ found=true; break; } q=p; if(x<p->data){ p=p->lChild; } else{ p=p->rChild; } } if(found){ //用指针的引用可以节省代码量? if(p->lChild==NULL&&p->rChild==NULL){ //删除叶子 if(x<=q->data){ q->lChild=NULL; } else{ q->rChild=NULL; } p=NULL; } else if(p->lChild==NULL){ //删除只包含右节点的节点 if(x<=q->data){ q->lChild=p->rChild; } else{ q->rChild=p->rChild; } p=NULL; } else if(p->rChild==NULL){ //删除只包含左节点的节点 if(x<=q->data){ q->lChild=p->lChild; } else{ q->rChild=p->lChild; } p=NULL; } else{ //迭代删除 q=p; p=p->lChild; while(p->rChild){ p=p->rChild; } x=(q->data)=(p->data); q=q->lChild; Delete(q,x); } } } template <typename Type> void BSTree<Type>::InOrder(){ InOrder(tree); cout<<endl; } template <typename Type> void BSTree<Type>::PreOrder(){ PreOrder(tree); cout<<endl; } template <typename Type> void BSTree<Type>::PostOrder(){ PostOrder(tree); cout<<endl; } template <typename Type> void BSTree<Type>::InOrder(TreeNode<Type> *t){ if(t){ InOrder(t->lChild); Visit(t); InOrder(t->rChild); } } template <typename Type> void BSTree<Type>::PreOrder(TreeNode<Type> *t){ if(t){ Visit(t); PreOrder(t->lChild); PreOrder(t->rChild); } } template <typename Type> void BSTree<Type>::PostOrder(TreeNode<Type> *t){ if(t){ PostOrder(t->lChild); PostOrder(t->rChild); Visit(t); } } int main(){ BSTree<int> bst1; bst1.Insert(80);/* 80 */ bst1.Insert(90);/* / \ */ bst1.Insert(60);/* 60 90 */ bst1.Insert(70);/* / \ */ bst1.Insert(20);/* 20 70 */ bst1.Insert(10);/* / \ */ bst1.Insert(40);/* 10 40 */ bst1.Insert(30);/* / \ */ bst1.Insert(50);/* 30 50 */ bst1.InOrder(); bst1.PreOrder(); bst1.PostOrder(); cout<<endl; bool s1=bst1.Search(40);cout<<s1<<endl; bool s2=bst1.Search(55);cout<<s2<<endl; bst1.InOrder(); bst1.Delete(30); bst1.InOrder(); return 0; }
标签:
原文地址:http://www.cnblogs.com/shuiming/p/5786019.html