1. 程式人生 > >二叉搜尋樹中序迭代器的實現

二叉搜尋樹中序迭代器的實現

#include<iostream>
#include<queue>
#include<stack>
#include<stdlib.h>
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:
class const_iterator
{
protected:
BTNode *current;
T& retrieve()const{return (current->data);}
const_iterator(BTNode* t){current=GoFarLeft(t);}
stack<BTNode*> St;
BTNode* GoFarLeft(BTNode* t)
{
if(!t)
return (NULL);
while(t->left)
{
St.push(t);
t=t->left;
}
return t;
}
friend class BSTree<T>;
public:
const_iterator():current(NULL){}
const T& operator*()const{return retrieve();}
const_iterator& operator++()
{
if(current->right)
{
current=GoFarLeft(current->right);
}
else if(!St.empty())
{
current=St.top();
St.pop();
}
else
{
current=NULL;
}
return *this;
}
bool operator==(const const_iterator& rhs)const{return (current==rhs.current);}
bool operator!=(const const_iterator& rhs)const{return (current!=rhs.current);}
};
class iterator:public const_iterator
{
protected:
iterator(BTNode* t):const_iterator(t){}
friend class BSTree<T>;
public:
iterator(){}
T& operator*(){return retrieve();};
const T& operator*()const{return const_iterator::operator*();}
iterator& operator++()
{
if(current->right)
{
current=GoFarLeft(current->right);
}
else if(!St.empty())
{
current=St.top();
St.pop();
}
else
{
current=NULL;
}
return *this;
}
};
const_iterator begin()const{return const_iterator(root);}
const_iterator end()const{return NULL;}
iterator begin(){return iterator(root);}
iterator end(){return NULL;}
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;
}

template<class Iterator>
void Display(Iterator first,Iterator last)
{
for(;first!=last;++first)
{
cout<<(*first);
}
cout<<endl;
}

int main()
{
BSTree<char> L;
char a[8]={'H','K','J','C','B','F','D','E'};
for(int i=0;i<8;i++)
{
L.Insert(a[i]);
}
L.PrintBTree(40);
Display(L.begin(),L.end());
return 0;
}

 

因為編譯器的不同,這篇程式碼不能在g++中編譯成功,只能在VC6.0中執行成功