二叉树建立的基本思路

来源:互联网 发布:heatmap制作软件 编辑:程序博客网 时间:2024/06/06 06:47

#include<stdio.h>
#include<stdlib.h>
typedef struct node                                        //建立结点
{
char data;
struct node *lchild,*rchild;                             //左右孩子
}lnode,*binode;

 

binode creat()                                     //建立结点的数据
{
binode p;

char ch;
scanf("%c",&ch);
if(ch=='*')                                    //当读入的数据为* 时结束
{
p=NULL;


}
else
{p=(binode)malloc(sizeof(node)); 
p->data=ch;
p->lchild=creat();                            //为右孩子赋值
p->rchild=creat();

}
return p;                                             //返回,注意这时候的p为第一次的p

 

}

 


void print(binode n)                             //按照中序遍历输出,可调换 下列顺序变为前序,后序
{
if(n)
{print(n->lchild);
printf("%c",n->data);

print(n->rchild);
}

}

 

void  main()      
{
binode b;
b=creat();
print(b);
}

原创粉丝点击