顺序树。
#ifndef TREE_H_INCLUDED #define TREE_H_INCLUDED #include <memory> using namespace std; template<typename T> class TreeNode { public: TreeNode(const T& v,const shared_ptr<TreeNode>& _leftc,const shared_ptr<TreeNode>& _rightc,const shared_ptr<TreeNode>& _fatherC): data(v),leftC(_leftc),rightC(_rightc),fatherC(_fatherC){} //private T data; shared_ptr<TreeNode> leftC; shared_ptr<TreeNode> rightC; shared_ptr<TreeNode> fatherC; }; template<typename T> class BST { public: BST():size(0),RootNode(shared_ptr<TreeNode<T>>()){} void ListTree(const shared_ptr<TreeNode<T>>& r) { if(r!=0) { if(r->leftC!=0) { ListTree(r->leftC); } cout<<r->data<<endl; if(r->rightC!=0) { ListTree(r->rightC); } } } shared_ptr<TreeNode<T>> Insert(const T& v) { shared_ptr<TreeNode<T>> tempNode=0; if(RootNode==0) { //new node.set ret=rootnode;size=1 tempNode=shared_ptr<TreeNode<T>>(new TreeNode<T>(v,shared_ptr<TreeNode<T>>(),shared_ptr<TreeNode<T>>(),shared_ptr<TreeNode<T>>())); RootNode=tempNode; } else { //new node.form rootnode big ,left; litter:right. size++; // shared_ptr<TreeNode<T>> compareNode=RootNode; bool isBigger=v>=compareNode->data?true:false; while( (isBigger && compareNode->rightC!=0)||(isBigger==false && compareNode->leftC!=0) ) { if(isBigger) { compareNode=compareNode->rightC; } else { compareNode=compareNode->leftC; } isBigger=v>=compareNode->data?true:false; } tempNode=shared_ptr<TreeNode<T>>(new TreeNode<T>(v,shared_ptr<TreeNode<T>>(),shared_ptr<TreeNode<T>>(),compareNode)); if(isBigger) { compareNode->rightC=tempNode; } else { compareNode->leftC=tempNode; } } ++size; return tempNode; } // shared_ptr<TreeNode<T>> MaxMinNode(bool isMax,shared_ptr<TreeNode<T>> nodeT) { shared_ptr<TreeNode<T>> temp=nodeT; if(isMax) { while(temp->rightC!=0) { temp=temp->rightC; } } else { while(temp->leftC!=0) { temp=temp->leftC; } } return temp; } shared_ptr<TreeNode<T>> NextNode(const shared_ptr<TreeNode<T>>& tNode) { if(tNode->rightC!=0) { return MaxMinNode(false,tNode->rightC); } else { shared_ptr<TreeNode<T>> tempc=tNode; shared_ptr<TreeNode<T>> tempf=tNode->fatherC; //tempf->leftC!=tempc这里有个小技巧了.虽然这里返回的结果是一致,但是下面的语句就免去了空left的判断. while(tempf->rightC==tempc && tempf!=0) { tempc=tempf; tempf=tempc->fatherC; } return tempf; } } void Transplant(const shared_ptr<TreeNode<T>>& des,const shared_ptr<TreeNode<T>>& source) { if(des->fatherC==0) { RootNode=source; } else if(des==des->fatherC->leftC) { des->fatherC->leftC=source; } else { des->fatherC->rightC=source; } if(source!=0) { source->fatherC=des->fatherC; } } void DelNode(const shared_ptr<TreeNode<T>>& tNode) { if(tNode->leftC==0&& tNode->rightC==0) { Transplant(tNode,0); } else if(tNode->leftC==0&& tNode->rightC!=0) { Transplant(tNode,tNode->rightC); } else if(tNode->leftC!=0&& tNode->rightC==0) { Transplant(tNode,tNode->leftC); } else { shared_ptr<TreeNode<T>> nextN=NextNode(tNode); if(nextN->fatherC!=tNode) { Transplant(nextN,nextN->rightC); nextN->rightC=tNode->rightC; nextN->rightC->fatherC=nextN; } Transplant(tNode,nextN); nextN->leftC=tNode->leftC; nextN->rightC->fatherC=nextN; } } //private int size; shared_ptr<TreeNode<T>> RootNode; }; #endif // TREE_H_INCLUDED