二叉树查找树

#include "BinaryNode.h"
#include "Dictionary.h"

template <class Key,class Elem,class KEComp,class EEComp>
class BST
:public Dictionary<Key,Elem,KEComp,EEComp>
{
private:
BinNode<Elem>* root;
int nodecount;
void clearhelp(BinNode<Elem>*);
BinNode<Elem>* inserthelp(BinNode<Elem>*,const Elem&);
BinNode<Elem>* deletemin(BinNode<Elem>*,BinNode<Elem>*&);
BinNode<Elem>* removehelp(BinNode<Elem>*,const Key&,BinNode<Elem>*&);

bool findhelp(BinNode<Elem>*,const Key&,Elem&) const;
void printhelp(BinNode<Elem>*,int) const;
public:
BST()
{
root=NULL;nodecount=0;
}
~BST()
{
clearhelp(root);
}
void clear()
{
clearhelp(root);root=NULL;nodecount=0;
}
bool insert(const Elem& e)
{
root=inserthelp(root,e);
nodecount++;
return true;
}
bool remove(const Key& K,Elem &e)
{
BinNode<Elem> *t=NULL;
root=removehelp(root,K,t);
if(t==NULL) return false;
e=t->val();
nodecount--;
delete t;
return true;
}
bool removeAny(Elem &e)
{
if(root==NULL) return false;
BinNode<Elem>* t;
root=deletemin(root,t);
e=t->val();
delete t;
nodecount--;
return true;
}
bool find(const Key& K,Elem &e) const
{
return findhelp(root,K,e);
}
int size()
{
return nodecount;
}
void print() const
{
if(root==NULL) cout<<"The BST is empty.\n";
else
printhelp(root,0);
}
};

template<class Key,class Elem,class KEComp,class EEComp>
bool BST<Key,Elem,KEComp,EEComp>::findhelp(BinNode<Elem>* subroot,const Key &K,Elem& e) const
{
if(subroot==NULL) return false;
else if(KEComp::lt(K,subroot->val()))
return findhelp(subroot->left(),K,e);
else if(KEComp::gt(K,subroot->val()))
return findhelp(subroot->right(),K,e);
else
{
e=subroot->val();return true;
}
}

template<class Key,class Elem,class KEComp,class EEComp>
BinNode<Elem>* BST<Key,Elem,KEComp,EEComp>::inserthelp(BinNode<Elem>* subroot,const Elem& val)
{
if(subroot==NULL)
return (new BinNodePtr<Elem>(val,NULL,NULL));
if(EEComp::lt(val,subroot->val()))
subroot->setLeft(inserthelp(subroot->left (),val));
else subroot->setRight(inserthelp(subroot->right(),val));
return subroot;
}

template<class Key,class Elem,class KEComp,class EEComp>
BinNode<Elem>* BST<Key,Elem,KEComp,EEComp>::deletemin(BinNode<Elem>* subroot,BinNode<Elem>*& min)
{
if(subroot->left()==NULL)
{
min=subroot;
return subroot->right();
}
else
{
subroot->setLeft(deletemin(subroot->left(),min));
return subroot;
}
}

template<class Key,class Elem,class KEComp,class EEComp>
BinNode<Elem>* BST<Key,Elem,KEComp,EEComp>::removehelp(BinNode<Elem>* subroot,const Key& K,BinNode<Elem> *& t)
{
if(subroot==NULL) return NULL;
else if(KEComp::lt(K,subroot->val()))
subroot->setLeft(removehelp(subroot->left(),K,t));
else if(KEComp::gt(K,subroot->val()))
subroot->setRight(removehelp(subroot->right(),K,t);
else
{
BinNode<Elem> *temp;
t=subroot;
if(subroot->left==NULL)
subroot=subroot->right();
else if(subroot->right()==NULL)
subroot=subroot->left();
else
{
subroot->setRight(deletemin(subroot->right(),temp));
Elem te=subroot->val();
subroot->setVal(temp->val());
temp->setVal(te);
t=temp;
}
}
return subroot;
}

template <class Key,class Elem,class KEComp,class EEComp>
void BST<Key,Elem,KEComp,EEComp>::clearhelp(BinNode<Elem>* subroot)
{
if(subroot==NULL) return;
clearhelp(subroot->left());
clearhelp(subroot->right());
delete subroot;
}


template<class Key,class Elem,class KEComp,class EEComp>
void BST<Key,Elem,KEComp,EEComp>::printhelp(BinNode<Elem>* subroot,int level)const
{
if(subroot==NULL) return;
printhelp(subroot->left(),level+1);
for(int i=0;i<level;i++)
cout<<"";
cout<<subroot->val()<<"\n";
printhelp(subroot->right(),level+1);
}
Live together,or Die alone!
原文地址:https://www.cnblogs.com/hzhida/p/2354749.html