#include<iostream>
using namespace std;
struct node
{
int info;
struct node *Llink,*Rlink;
};
void change(struct node *t)
{
struct node *s;
s=t->Llink;
t->Llink=t->Rlink;
t->Rlink=s;
if(t->Llink!=0)change(t->Llink);
if(t->Rlink!=0)change(t->Rlink);
}
void make(struct node *s,struct node *t)//s是单个结点,t是树
{
if(s->info<=t->info)//s->info<=t->就放在t左边,否则就放在t的右边
{
if(t->Llink==0)
t->Llink=s;
else
make(s,t->Llink);
}
else
{
if(t->Rlink==0)
t->Rlink=s;
else
make(s,t->Rlink);
}
}
void circuit(struct node *t)
{
if(t->Llink!=0)circuit(t->Llink);
cout<<t->info<<" ";
if(t->Rlink!=0)circuit(t->Rlink);
}
void dele(struct node *t)
{
if(t->Llink!=0)dele(t->Llink);
if(t->Rlink!=0)dele(t->Rlink);
delete t;
}
void main()
{
int a[50],i,j;
cout<<"请输入一组数,以0结尾:"<<endl;
for(i=0,j=0;i<100;i++,j++)
{
cin>>a[i];
if(a[i]==0)break;
}
struct node *s,*t;//t为树根
t=new struct node;
t->info=a[0];
t->Llink=0;
t->Rlink=0;
for(i=1;i<j;i++)//构造二叉树
{
s=new struct node;
s->info=a[i];
s->Llink=0;
s->Rlink=0;
make(s,t);
}
cout<<"交换前中序周游顺序:"<<endl;
circuit(t);
cout<<endl;
change(t);
cout<<"交换后中序周游顺序:"<<endl;
circuit(t);
cout<<endl;
dele(t);//释放空间
}
自己改下就可以了
我没时间改了