将写内容过程重要的一些内容备份一次,下面内容内容是关于C++ 二叉搜索树 模板的内容,希望能对大家也有帮助。
#pragma once
#include
#include
#include
using namespace std;
template
template
class TNode{
friend class STree_iterator
friend class STree_const_iterator
protected:
T data;
void copy(const TNode
left=rhs.left;
right=rhs.right;
parent=rhs.parent;
data=rhs.data;
}
public:
TNode() {left=NULL, right=NULL, parent=NULL;}
:data(item), left(lptr), right(rptr), parent(pptr) {}
TNode( const TNode
~TNode() {}
};
template
class STree {
friend class STree_iterator
friend class STree_const_iterator
protected:
int size;
public:
typedef T value_type;
STree(void);
STree(const STree
~STree(void);
connect the right subtree of R to its parent as the left sub tree
connect the left subtree of the deleted node to R as the left subtree link the right
subtree of the deleted node to R as its right sub tree, and the substitute the
void erase(iterator first, iterator last);
void LevelorderPrint();
bool empty() const {return root==NULL;}
int Size() const {return size;}
template
STree
root=NULL;
size=0;
}
template
root=NULL;
size=0;
while(first!=last) {
++first;
}
}
template
STree
root=copy( rhs.Root() );
size=rhs.Size();
}
template
STree
if(this==rhs) return this;
DelTree(root);
root=copy(rhs.Root());
size=rhs.Size();
return this;
}
template
STree
DelTree(root);
}
template
if(newnode==NULL) {cerr<<"allocate storage for new node failed!"< return newnode; } template if(rhs==NULL) return NULL; lptr=copy(rhs->left); newnode=GetNode(rhs->data, lptr, rptr, NULL); if(lptr!=NULL) lptr->parent=newnode; if(rptr!=NULL) rptr->parent=newnode; } template if(_root!=NULL){ DelTree(_root->left); DelTree(_root->right); delete _root; } } template while(cur!=NULL) { if(item==cur->data) return cur; else if(item else cur=cur->right; } return cur; } template int r_depth, l_depth, depth; if(cur==NULL) depth=-1; else { r_depth=height(cur->right); l_depth=height(cur->left); depth=1+(r_depth>l_depth?r_depth:l_depth); } return depth; } template typename STree cur=findNode(item); if(cur!=NULL) return STree else return this->end(); } template typename STree while(cur!=NULL) { par=cur; if(item==cur->data) return STree else if(item else cur=cur->right; } newnode=GetNode(item, NULL, NULL, par); if(par==NULL) root=newnode; else if(item else par->right=newnode; ++size; return STree } template bool STree STree if( pos!=end() ) erase(pos); return pos!= end(); } template void STree if(del==NULL) return; if(del->left==NULL || del->right==NULL) { if(del->left==NULL) subs=del->right; else subs=del->left; if(subs != NULL) subs->parent=par; } else { subs=del->right; while(subs->left!=NULL) { temp=subs; subs=subs->left; } if(temp==del) { subs->left=del->left; subs->parent=par; subs->left->parent=subs; } else { if(subs->right!=NULL) subs->right->parent=temp; subs->left->parent=subs; subs->right->parent=subs; } } if(par==NULL) root=subs; else if(par->left==del) par->left=subs; else par->right=subs; delete del; --size; } template void STree STree while(iter!=last) { erase(iter); ++iter; } } template void STree queue< TNode queue< TNode while(!parQ.empty()) { do{ parQ.pop(); cout< } while(!parQ.empty()); cout< parQ=chdQ; queue< TNode swap(chdQ, empty); } delete cur; cur=NULL; } template typename STree if(cur!=NULL) while(cur->left!=NULL) cur=cur->left; } template return STree } template friend class STree public: typedef STree_iterator typedef std::bidirectional_iterator_tag iterator_category; typedef T value_type; typedef Ref reference; typedef Ptr pointer; typedef ptrdiff_t difference_type; public: STree_iterator(){ pn=NULL; tree=NULL; } pn=rhs.pn; tree=rhs.tree; } bool operator==(const STree_iterator &rhs) const {return pn == rhs.pn;} bool operator!=(const STree_iterator &rhs) const {return pn != rhs.pn;} if(pn==NULL) {cerr<<"allocate storage for new node failed!"< return pn->data; } pn=tree->root; if(pn==NULL) { cerr<<"move on a empty tree!"< while(pn->left!=NULL) pn=pn->left; } pn=pn->right; while(pn->left!=NULL) pn=pn->left; } p=pn->parent; while(p!=NULL && pn==p->right) { pn=p; p=p->parent; } pn=p; } } self temp(pn, tree); } pn=tree->root; if(pn==NULL) {cerr<<"move on a empty tree!"< while(pn->right!=NULL) pn=pn->right; } pn=pn->left; while(pn->right!=NULL) pn=pn->right; } p=pn->parent; while(p!=NULL && pn==p->left) { pn=p; p=p->parent; } pn=p; } } self operator--(int) { self temp(pn, tree); } private: }; template class STree_const_iterator{ friend class STree public: typedef STree_const_iterator typedef std::bidirectional_iterator_tag iterator_category; typedef T value_type; typedef Ref reference; typedef Ptr pointer; typedef ptrdiff_t difference_type; public: STree_const_iterator(){ pn=NULL; tree=NULL; } pn=rhs.pn; tree=rhs.tree; } pn=rhs.pn; tree=rhs.tree; } bool operator==(const STree_const_iterator &rhs) const {return pn == rhs.pn;} bool operator!=(const STree_const_iterator &rhs) const {return pn != rhs.pn;} return pn->data; } pn=tree->root; if(pn==NULL) { cerr<<"STree STree_const_iterator operator++(): tree empty"< while(pn->left!=NULL) pn=pn->left; } pn=pn->right; while(pn->left!=NULL) pn=pn->left; } p=pn->parent; while(p!=NULL && pn==p->right) { pn=p; p=p->parent; } pn=p; } } STree } pn=tree->root; if(pn==NULL) throw underflowError ("STree STree_const_iterator operator++(): tree empty"); while(pn->right!=NULL) pn=pn->right; } pn=pn->left; while(pn->right!=NULL) pn=pn->right; } p=pn->parent; while(p!=NULL && pn==p->left) { pn=p; p=p->parent; } pn=p; } } STree_const_iterator operator--(int) { STree_const_iterator temp(pn, tree); } private: };