不会写的,算导上有意外收获
#include<iostream>
template <class E,class K>
struct BSTNode
{
E data;
BSTNode<E,K> *left,*right;
BSTNode():left(NULL),right(NULL){}
BSTNode(const E d,BSTNode<E,K> *L=NULL,BSTNode<E,K> *R=NULL):data(d),left(L),right(R){}
~BSTNode(){}
void SetData(E d){data = d;}
E GetData(){return data;}
bool operator <(const E& x){return (data.key<x.key)?true:false;}
bool operator >(const E& x){return (data.key>x.key)?true:false;}
bool operator ==(const E& x){return (data.key==x.key)?true:false;}
};
template <class E,class K>
class BST{
public:
BST():root(NULL){}
BST(K value);
~BST(){};
bool Search(const K x)const
{return (Search(x,root)!=NULL)?true:false;}
BST<E,K>& operator =(const BST<E,K>& R);
void makeEmpty()
{makeEmpty(root);root = NULL;}
void PrintTree()const
{PrintTree(root);}
E Min()
{return Min(root)->data;}
E Max()
{return Max(root)->data;}
bool Insert(const E& e)
{return Insert(e,root);}
bool Remove(const E x)
{return Remove(x,root);}
private:
BSTNode<E,K> *root;
K Refvalue; //标识输入停止
BSTNode<E,K> *Search(const K x,BSTNode<E,K> *ptr);
void makeEmpty(BSTNode<E,K> *&ptr);
void PrintTree(BSTNode<E,K> *ptr)const;
BSTNode<E,K> *Copy(const BSTNode<E,K> *ptr);
BSTNode<E,K> *Min(BSTNode<E,K> *ptr) const;
BSTNode<E,K> *Max(BSTNode<E,K> *ptr) const;
bool Insert(const E& e,BSTNode<E,K> *&ptr);
bool Remove(const E x,BSTNode<E,K> *&ptr);
};
template <class E,class K>
BSTNode<E,K> *BST<E,K>::Search(const K x,BSTNode<E,K> *ptr){
if (ptr==NULL)
return NULL;
if (x<ptr->data)
return Search(x,ptr->left);
else if (x>ptr->data)
return Search(x,ptr->right);
else
return ptr;
};
template <class E,class K>
bool BST<E,K>::Insert(const E& e,BSTNode<E,K> *&ptr){
if (ptr==NULL){
ptr = new BSTNode<E,K>(e);
if (ptr==NULL)
{ cerr<<"空间溢出"<<endl;exit(1);}
return true;
}
if (e>ptr->data)
return Insert(e,ptr->right);
else if (e<ptr->data)
return Insert(e,ptr->left);
else return false;
};
template <class E,class K>//建立一颗二叉搜索树,边输入边建
BST<E,K>::BST(K value){
E x;
root = NULL;
Refvalue = value;
cin>>x;
while (x.key!=Refvalue)
Insert(x,root);cin>>x;
};
template <class E,class K>
bool BST<E,K>::Remove(const E x,BSTNode<E,K> *&ptr){
if (ptr==NULL)
return false;
BSTNode<E,K> *temp;
if (x<ptr->data)
return Remove(x,ptr->left);
else if (x>ptr->data)
return Remove(x,ptr->right);
else{
if (ptr->left!=NULL&&ptr->right!=NULL){
temp = ptr->right;
while (temp->left!=NULL)
temp = temp->left;
ptr->data = temp->data;
Remove(ptr->data,ptr->right);
}
else{
temp = ptr;
if (temp->left==NULL)
ptr = ptr->right;
else ptr = ptr->left;
delete temp;
return true;
}
}
return false;
};
template <class E,class K>
void BST<E,K>::makeEmpty(BSTNode<E,K> *&ptr){
if (ptr!=NULL){
BSTNode<E,K> *temp=ptr;
makeEmpty(ptr->left);
makeEmpty(ptr->right);
delete temp;
}
};
template <class E,class K>
void BST<E,K>::PrintTree(BSTNode<E,K> *ptr)const{
if (ptr!=NULL){
PrintTree(ptr->left);
cout<<ptr->data<<endl;
PrintTree(ptr->right);
}
};
template <class E,class K>
BSTNode<E,K> *BST<E,K>::Min(BSTNode<E,K> *ptr)const{
if (ptr->left==NULL)
return ptr;
if (ptr->left!=NULL)
return Min(ptr->left);
return NULL;
};
template <class E,class K>
BSTNode<E,K> *BST<E,K>::Max(BSTNode<E,K> *ptr)const{
if (ptr->right==NULL)
return ptr;
if (ptr->right!=NULL)
return Max(ptr->right);
return NULL;
};
template <class E,class K>
BSTNode<E,K> *BST<E,K>::Copy(const BSTNode<E,K> *ptr){
if (ptr!=NULL){
BSTNode<E,K> *temp=new BSTNode<E,K>(ptr->data);
temp->left = Copy(ptr->left);
temp->right = Copy(ptr->right);
return temp;
}
};
template <class E,class K>
BST<E,K>& BST<E,K>::operator =(const BST<E,K>& R){
BST<E,K> b;
b.root = R.Copy(R.root);
return b;
};