二叉树中序遍历的实现
【C语言】
代码如下:
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *Lc,*Rc;
}
Bnode,*Btree;
void inordor(Btree T)
{
if(T!=NULL)
{
inordor(T->Lc);
printf("%4d",T->data);
inordor(T->Rc);
}
}
Btree insert(Btree T,int x)
{
if(T==NULL)
{
T=(Btree)malloc(sizeof(Bnode));
T->data=x;
T->Lc=T->Rc=NULL;
return T;
}
if(x<T->data)
T->Lc=insert(T->Lc,x);
else
T->Rc=insert(T->Rc,x);
return T;
}
int main()
{
int d,n,i;
Btree T=NULL;
printf("Enter the number oof data:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter data %d:",i);
scanf("%d",&d);
T=insert(T,d);
}
inordor(T);
printf("\n");
}
运行结果:
Enter the number oof data:6
Enter data 1:34
Enter data 2:6
Enter data 3:77
Enter data 4:32
Enter data 5:69
Enter data 6:89
6 32 34 69 77 89
Press any key to continue