二叉搜索樹的實現
#include<iostream>
#include<queue>
using namespace std;
struct Location
{
int xindent,ylevel;
};
void Gotoxy(int x,int y)
{
static int level=0,indent=0;
if(y==0)
{
indent=0;level=0;
}
if(level!=y)
{
cout<<endl;
indent=0;
level++;
}
cout.width(x-indent);
indent=x;
}
template<class T>
class BSTree
{
private:
struct BTNode
{
T data;
BTNode *left,*right;
BTNode(const T& item,BTNode* lptr=NULL,BTNode* rptr=NULL):data(item),left(lptr),right(rptr){}
};
BTNode* root;
int size;
void Insert(const T& x,BTNode* &t);
void Remove(const T& x,BTNode* &t);
BTNode* FindMin(BTNode* t)const;
BTNode* FindMax(BTNode* t)const;
BTNode* FindNode(const T& x,BTNode* t)const;
void Clear(BTNode* &t);
void PrintBTree(const BTNode* t,int w)const;
public:
BSTree():root(NULL),size(0){};
~BSTree(){Clear();}
T& FindMin()const{return (FindMin(root)->data);}
T& FindMax()const{return (FindMax(root)->data);}
bool Find(const T& x)const{return FindNode(x,root)!=NULL;}
bool Find(T& x)const;
bool Empty()const{return (size==0);}
int Size(){return size;}
void Clear(){Clear(root);}
void Update(const T& x);
void Insert(const T& x){Insert(x,root);}
void Remove(const T& x){Remove(x,root);}
void PrintBTree(int w)const{PrintBTree(root,w);}
};
template<class T>
BSTree<T>::BTNode* BSTree<T>::FindMin(BTNode* t)const
{
if(t!=NULL)
{
while(t->left!=NULL)
{
t=t->left;
}
}
return t;
}
template<class T>
BSTree<T>::BTNode* BSTree<T>::FindMax(BTNode* t)const
{
if(t!=NULL)
{
while(t->right!=NULL)
{
t=t->right;
}
}
return t;
}
template<class T>
BSTree<T>::BTNode* BSTree<T>::FindNode(const T& x,BTNode *t)const
{
if(t==NULL)
return NULL;
while(t)
{
if(x<t->data)
{
t=t->left;
}
else if(x>t->data)
{
t=t->right;
}
else
{
return t;
}
}
return NULL;
}
template<class T>
void BSTree<T>::Clear(BTNode* &t)
{
if(t==NULL)
return;
Clear(t->left);
Clear(t->right);
delete t;
t=NULL;
}
template<class T>
bool BSTree<T>::Find(T& x)const
{
BTNode* p=FindNode(x,root);
if(p)
{
x=p->data;
return 1;
}
return 0;
}
template<class T>
void BSTree<T>::Update(const T& x)
{
BTNode* p=FindNode(x,root);
if(p)
{
p->data=x;
}
else
{
Insert(x);
}
}
template<class T>
void BSTree<T>::Insert(const T& x,BTNode* &t)
{
if(t==NULL)
{
t=new BTNode(x);
size++;
}
else if(x<t->data)
{
Insert(x,t->left);
}
else if(x>t->data)
{
Insert(x,t->right);
}
}
template<class T>
void BSTree<T>::Remove(const T& x,BTNode* &t)
{
if(t==NULL)
return ;
if(x<t->data)
{
Remove(x,t->left);
}
else if(x>t->data)
{
Remove(x,t->right);
}
else if(t->left!=NULL&&t->right!=NULL)
{
t->data=FindMin(t->right)->data;
Remove(t->data,t->right);
}
else
{
BTNode* old=t;
t=(t->left!=NULL)?t->left:t->right;
delete old;
size--;
}
}
template<class T>
void BSTree<T>::PrintBTree(const BTNode *t,int w)const
{
if(t==NULL)
return ;
int level=0,offset=w/2;
Location fLoc,cLoc;
queue<const BTNode *> Q;
queue<Location> LQ;
fLoc.xindent=offset;
fLoc.ylevel=level;
Q.push(t);
LQ.push(fLoc);
while(!Q.empty())
{
t=Q.front();
Q.pop();
fLoc=LQ.front();
LQ.pop();
Gotoxy(fLoc.xindent,fLoc.ylevel);
cout<<t->data;
if(fLoc.ylevel!=level)
{
level++;
offset/=2;
}
if(t->left)
{
Q.push(t->left);
cLoc.ylevel=fLoc.ylevel+1;
cLoc.xindent=fLoc.xindent-offset/2;
LQ.push(cLoc);
}
if(t->right)
{
Q.push(t->right);
cLoc.ylevel=fLoc.ylevel+1;
cLoc.xindent=fLoc.xindent+offset/2;
LQ.push(cLoc);
}
}
cout<<endl;
}
int main()
{
BSTree<char> L;
int i;
char a[8]={‘H‘,‘K‘,‘J‘,‘C‘,‘B‘,‘F‘,‘D‘,‘E‘};
for(i=0;i<8;i++)
{
L.Insert(a[i]);
}
L.PrintBTree(40);
cout<<"after deleting C:\n"<<endl;
L.Remove(‘C‘);
L.PrintBTree(40);
cout<<"after inserting C:\n"<<endl;
L.Insert(‘C‘);
L.PrintBTree(40);
return 0;
}
二叉搜索樹的實現