二叉排序树
#include<iostream>using namespace std;
typedef struct tree
{
int data;
struct tree *l;
struct tree *r;
};
tree *charu(tree *&t,int x)
{
if(t==NULL)
{
t=new tree;
t->l=t->r=NULL;
t->data=x;
}
else
if(x>t->data)
t->r=charu(t->r,x);
if(x<t->data)
t->l=charu(t->l,x);
return t;
}
void show(tree *t)
{
if(t!=NULL)
{
show(t->l);
cout<<t->data<<endl;
show(t->r);
}
}
void chazhao(tree *t,int x)
{ int f=0;
if(t!=NULL)
{
if(x==t->data)
{
cout<<"找到该数据:"<<t->data<<endl;
f=1;
}
if(x>t->data)
chazhao(t->r,x);
if(x<t->data)
chazhao(t->l,x);
}
if(f=0)
cout<<"没到到该数据"<<endl;
charu(t,x);
show(t);
}
void delet(tree *t,int k)
{
tree *q,*s;
chazhao(t,k);
if (!t->r)
{
q=t;
t=t->l;
free(q);
}
else if(!t->l)
{
q=t;
t=t->r;
free(q);
}
else
{
q=t;
s=t->l;
while(s->r)
{
q=s;
s=s->r;
}
t->data=s->data;
if(q!=t)
q->r=s->l;
else
q->l=s->l;
delete s;
}
}
void main()
{ tree*t;
int n,x,m,k;
cout<<"输入结点个数"<<endl;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>x;
charu(t,x);
}
show(t);
cout<<"输入要查找的数据"<<endl;
cin>>m;
chazhao(t,m);
cout<<"输入你要删除的节点:"<<endl;
cin>>k;
delet(t,k);
}
这是二叉排序树的建立,查找,删除,为什么不能实现删除呢?输入结点个数这句话为什么运行时无法输出