#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define Status void
#define Teletype char
#define NULL 0
typedef struct bitnode
{
Teletype data;
struct bitnode *rchild;
struct bitnode *lchild;
}bitnode,*bitree;
bitree jj;
bitree *bin=&jj;
/*构造二叉树链表表示的二叉树的函数-以先根序方式构造*/
Status createbitree(bitree *b)
{
char ch;
fflush(stdin); /*清除键盘缓冲区*/
scanf("%c",&ch);
if(ch==' '){*b=NULL;return;}
else{
*b=(bitree)malloc(sizeof(bitnode));
if(!*b)exit(1);
(*b)->data=ch;
createbitree(&((*b)->lchild));
createbitree(&((*b)->rchild));
}
}
/*二叉树的先根序遍历函数*/
Status pretra(bitree *b)
{
if(*b==NULL)return;
else{
printf("%c ",(*b)->data);
pretra(&((*b)->lchild));
pretra(&((*b)->rchild));
}
}
/*二叉树的中根序遍历函数*/
Status intra(bitree *b)
{
if(*b==NULL)return;
else{
intra(&((*b)->lchild));
printf("%c ",(*b)->data);
intra(&((*b)->rchild));
}
}
/*二叉树的后根序遍历函数*/
Status posttra(bitree *b)
{
if(*b==NULL)return;
else{
posttra(&((*b)->lchild));
posttra(&((*b)->rchild));
printf("%c ",(*b)->data);
}
}
void main()
{
printf("按先根序输入二叉树(空格表示空树):/n");
createbitree(bin);
printf("/n按先根序输出二叉树:/n");
pretra(bin);
printf("/n按中根序输出二叉树:/n");
intra(bin);
printf("/n按后根序输出二叉树:/n");
posttra(bin);
printf("/n");
}