这里写代码片#include <iostream>
#include <iomanip>
using namespace std;
template<typename Type>
class BSTNode
{
public:
Type data;
BSTNode<Type> *left;
BSTNode<Type> *right;
BSTNode<Type> *parent;//带父亲节点的搜索二叉树。
BSTNode(Type d = Type()):data(d),left(NULL),right(NULL),parent(NULL){}
};
template<typename Type>
class BSTtree
{
public:
BSTtree():root(NULL),ref(-1){}
BSTtree(const BSTtree &bst):root(NULL),ref(-1)//拷贝构造
{
copy(this->root,(BSTNode<Type> *)NULL,bst.root);
}
BSTtree& operator=(const BSTNode<Type> *bst)//赋值构造
{
if(this!=bst)
{
if(root!=NULL)
{
__destory(root);
}
copy(root,(BSTNode<Type>*)NULL,bst.root);
}
return *this;
}
void Insert(Type a[],int n)
{
for(int i=0;i<n;i++){
Insert(root,(BSTNode<Type> *)NULL,a[i]);
}
}
void Printf()
{
Printf(root);
}
Type Parent(Type val)//求父亲节点值.
{
return Getval(Find(root,val)->parent);
}
Type Min()//求最小值。
{
BSTNode<Type> *p = root;
if(p==NULL)return ref;
while(p->left!=NULL)p=p->left;
return p->data;
}
Type Max()//求最大值
{
BSTNode<Type> *p = root;
if(p==NULL)return ref;
while(p->right!=NULL)p=p->right;
return p->data;
}
Type Find(Type val)
{
return Getval(Find(root,val));
}
Type Getval(BSTNode<Type> *t)
{
if(t==NULL)return ref;
return t->data;
}
void Remove(Type val)
{
Remove(root,val);
}
private:
void Remove(BSTNode<Type> *&t,Type val)//着实费劲,将要删除节点的左分支挂到该删除节点右分支的最左端.
{
if(t==NULL)return ;
BSTNode<Type> *s = Find(t,val);
if(s==NULL)return;
BSTNode<Type> *p = Find(t,val)->parent;
if(p==NULL)
{
BSTNode<Type> *q1 = root->right;
BSTNode<Type> *q2 = root->left;
if(q1==NULL && q2==NULL)
{delete t;return;}
else if(q1!=NULL)
{
BSTNode<Type> *save = NULL;
while(q1!=NULL)
{
save = q1;
q1=q1->left;
}
BSTNode<Type> *tmp = save;
tmp->left = q2;
if(q2!=NULL)
q2->parent = tmp;
t->left = NULL;
delete t;
t = NULL;
root = tmp;
return ;
}
else if(q1==NULL && q2!=NULL)
{
delete t;
t = NULL;
root = q2;
return ;
}
}
else
{
p = Find(t,val);
BSTNode<Type> *q2 = p->left;
BSTNode<Type> *q1 = p->right;
BSTNode<Type> *par = p->parent;
if(q1!=NULL)
{
BSTNode<Type>* save = NULL;
if(par->left==p)
{
par->left = q1;
}
else if(par->right==p)
{
par->right = q1;
}
while(q1!=NULL)
{
save = q1;
q1 = q1->left;
}
save->left = q1;
q1->parent = save;
delete p;
p = NULL;
}
else if(q2!=0)
{
par->left = q2;
q2 -> parent = par;
delete p;
p = NULL;
}
else
{
if(p->parent->left == p)
{
p->parent->left = NULL;
}
else if(p->parent->right == p)
{
p->parent->right = NULL;
}
delete p;
p = NULL;
}
}
}
/*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;
}
}
}
}
}*/
void __destory(BSTNode<Type> *&t)//销毁
{
if(t!=NULL)
{
__destory(t->left);
__destory(t->right);
delete t;
t = NULL;
}
}
void copy(BSTNode<Type> *&t , BSTNode<Type> *p , BSTNode<Type> *e)//节点复制。
{
if(e==NULL)return ;
if(t==NULL)
{
t = new BSTNode<Type>(e->data);
t->parent = p;
}
copy(t->left,t,e->left);
copy(t->right,t,e->right);
}
BSTNode<Type>* Find(BSTNode<Type>* &t,Type val)//寻找节点
{
if(t==NULL)return NULL;
else
{
if(t->data>val)
Find(t->left,val);
else if(t->data<val)
Find(t->right,val);
else{
return t;
}
}
}
void Printf(BSTNode<Type> *&t)//排序打印。
{
if(t!=NULL)
{
Printf(t->left);
cout<<setw(4)<<t->data;
Printf(t->right);
}
}
void Insert(BSTNode<Type> *&t,BSTNode<Type> *p,Type val)//插入数值,带有parent指针.
{
if(t==NULL)
{
t = new BSTNode<Type>(val);
t->parent = p;
}
else{
if(t->data>val)
Insert(t->left,t,val);
if(t->data<val)
Insert(t->right,t,val);
}
}
Type ref;
BSTNode<Type> *root;
};
int main()
{
int a[]={5,4,6,2,1,7};
BSTtree<int> bst;
bst.Insert(a,6);
// bst.Printf();
bst.Remove(11);
bst.Printf();
// cout<<endl;
// cout<<bst.Parent(5)<<endl;
// bst.Parent();
// cout<<bst.Min()<<endl;
// cout<<bst.Max()<<endl;
return 0;
}
原文地址:http://blog.csdn.net/liuhuiyan_2014/article/details/45641181