//////二叉搜索树.
#include <iostream>
using namespace std;
template<typename Type>
class BSTNode{
public:
Type data;
BSTNode<Type> *left;
BSTNode<Type> *right;
BSTNode(Type val = Type()):data(val),left(NULL),right(NULL){}
};
template<typename Type>
class BSTtree
{
public:
friend istream& operator>>(istream &in,BSTtree<Type> &bst)
{
Type item;
while( in>>item , item != bst.vef)
{
bst.Insert(bst.root,item);
}
return in;
}
BSTtree():vef(-1),root(NULL){}
BSTtree(const BSTtree<Type> &bst)
{
copy(this->root,bst.root);
}
BSTtree& operator=(const BSTtree<Type> &bst)
{
if(this!=&bst)
{
if(root!=NULL)
_destory(root);//判断原来是否存在节点,存在则删除该树所有节点,再进行赋值构造.
copy(root,bst.root);
}
return *this;
}
~BSTtree()
{
_destory(root);
}
bool Insert(Type a[],int n)
{
for(int i=0;i<n;++i)
{
Insert(root,a[i]);
}
}
void Printf()
{
Printf(root);
}
bool Find(int val)
{
if(Find(root,val))return true;
else return false;
}
bool Remove(Type val)
{
return Remove(root,val);
}
bool Delete(Type val)
{
return Delete(root,val);
}
Type Parent(Type val)
{
BSTNode<Type> *p = Parent(root,val);
if(p!=NULL)
return GetVal(p);
else return -1;
}
Type GetVal(BSTNode<Type> *t)
{
return t->data;
}
private:
BSTNode<Type> *Parent(BSTNode<Type> *&t,Type val)//找val的父亲节点.
{
if(t==NULL || t->data==val)return NULL;
if(t->left!=NULL && t->left->data==val)return t;
if(t->right!=NULL && t->right->data==val)return t;
if(t->data>val)
Parent(t->left,val);
else
{
Parent(t->right,val);
}
}
bool Remove(BSTNode<Type> *&t,Type val)//这里删除是搜索一个值代替删除位置的值.
{
if(t==NULL)return false;
else
{
if(t->data>val)
Remove(t->left,val);
else if(t->data<val)
Remove(t->right,val);
else
{
if(t->left!=NULL && t->right!=NULL)
{
BSTNode<Type> *tmp = t;
BSTNode<Type> *p = t->right;
while(p->left!=NULL)
p=p->left;
t->data = p->data;
if(t->right!=NULL)
Remove(t->right,p->data);
}
else
{
if(t->left==NULL)
{
BSTNode<Type> *tmp = t;
t = tmp->right;
delete tmp;
tmp = NULL;
}
else if(t->right==NULL)
{
BSTNode<Type> *tmp = t;
t = tmp->left;
delete tmp;
tmp = NULL;
}
else
{
delete t;
t=NULL;
}
}
}
}
}
BSTNode<Type>* Find(BSTNode<Type> *&t,Type val)//按值val查找节点.
{
if(t==NULL)return NULL;
if(t!=NULL)
{
if(t->data>val)
Find(t->left,val);
else if(t->data<val)
Find(t->right,val);
else
return t;
}
}
bool _destory(BSTNode<Type> *&t)//摧毁搜索二叉树.
{
if(t!=NULL)
{
_destory(t->left);
_destory(t->right);
delete t;
t = NULL;
}
}
bool copy(BSTNode<Type> *&t,BSTNode<Type> *e)//赋值搜索二叉树.
{
if(e==NULL)return false;
if(e!=NULL)
{
t = new BSTNode<Type>(e->data);
copy(t->left,e->left);
copy(t->right,e->right);
}
return true;
}
bool Insert(BSTNode<Type> *&t,Type val)//插入val。
{
if(t==NULL)
{
t = new BSTNode<Type>(val);
}
else
{
if(t->data>val)
Insert(t->left,val);
else if(t->data<val)
Insert(t->right,val);
else
return false;
}
return true;
}
void Printf(BSTNode<Type> *t)//排序打印.
{
if(t!=NULL)
{
Printf(t->left);
cout<<t->data<<" ";
Printf(t->right);
}
}
public:
Type Min()//求最小值.
{
BSTNode<Type> *p = root;
if(p==NULL)return vef;
while(p->left!=NULL)
p=p->left;
return p->data;
}
Type Max()//求最大值.
{
BSTNode<Type> *p = root;
if(p==NULL)return vef;
while(p->right!=NULL)
p=p->right;
return p->data;
}
private:
Type vef;
BSTNode<Type> *root;
};
int main()
{
BSTtree<int> bst;
cin>>bst;
// cout<<bst.Parent(3)<<endl;
// cout<<bst.Find(2)<<endl;
// int a[]={6,52,4,2,7,86,7,1};
// bst.Insert(a,8);
// bst.Printf();
// cout<<bst.Max()<<endl;
bst.Printf();
// fst.Printf();//这里都是测试调用.
return 0;
}
感悟:我本来想将Remove()函数的实现用要删除节点的左分支挂在右边分支的最左端,也可以形成二叉搜索树,只是找寻父节点链接十分麻烦,且增加了二叉树深度,所以我放弃了那个想法,选择了数据删除的方法.
原文地址:http://blog.csdn.net/liuhuiyan_2014/article/details/45625141