flag9交换左右子树

# include <stdio.h>
# include <malloc.h>
typedef struct tree
{
int data;
struct tree* lchild;
struct tree* rchild;
}Tree,*Ptree;
void travel(Ptree T);
void exchange(Ptree T);
void main()
{
Ptree a=(Ptree)malloc(sizeof(Tree));
Ptree b=(Ptree)malloc(sizeof(Tree));
Ptree c=(Ptree)malloc(sizeof(Tree));
Ptree d=(Ptree)malloc(sizeof(Tree));
Ptree e=(Ptree)malloc(sizeof(Tree));
a->data=8;
b->data=1;
c->data=2;
d->data=6;
e->data=5;
a->lchild=b;
a->rchild=c;
b->lchild=NULL;
b->rchild=d;
c->lchild=e;
c->rchild=NULL;
d->lchild=NULL;
d->rchild=NULL;
e->lchild=NULL;
e->rchild=NULL;
//travel(a);
exchange(a);
travel(a);
}
void travel(Ptree T)
{
if(T->lchild!=NULL)
travel(T->lchild);
printf("%d\n",T->data);
if(T->rchild!=NULL)
travel(T->rchild);
}
void exchange(Ptree T)
{
if(T->lchild!=NULL)
exchange(T->lchild);
if(T->rchild!=NULL)
exchange(T->rchild);
if(T!=NULL)
{
Ptree t;
t=T->lchild;
T->lchild=T->rchild;
T->rchild=t;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值