#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int num;
struct node *l;
struct node *r;
}LN;
void Creat(LN *L);
void Change(LN *L);
int Print(int n);
void main()
{
LN *L;
int n;
ii:
printf("Press 0 to end: \n");
printf("Press 1 to Creat a Linklist: \n");
printf("Press 2 to Change the child: \n");
scanf("%d",&n);
switch(n)
{
case 0:
system("CLS");
goto cc;
case 1:
system("CLS");
printf("Input the root number: \n");
Creat(L);
system("CLS");
break;
case 2:
Change(L);
break;
}
goto ii;
cc:;
}
void Creat(LN *L)
{
int n;
scanf("%d",&n);
if(n==0)
{
L=NULL;
}
else
{
L=(LN*)malloc(sizeof(LN));
L->num=n;
printf("Creat %d's leftchlid: \n",n);
Creat(L->l);
printf("Creat %d's rightchild: \n",n);
Creat(L->r);
}
}
void Change(LN *L)
{
if (L!=NULL)
{
printf("%d",L->num);
Change(L->l);
Change(L->r);
}
}
我的Change函数是先序遍历,小弟刚学实在不知道错在哪里。怎么从先序遍历算法中交换左右孩子,谢谢大神门!!!!!
还有怎么按层次遍历,感觉按层次没有办法用递归算法
如果我要想从大到小的顺序输出二叉排序树的各结点的算法,怎么实现,跪求大神门的指教,感谢!