#include<stdio.h>
#include<stdlib.h>
typedef struct BiTNode{
char data;
struct BiTNode *L;
struct BiTNode *R;
}BiTNode,*BiTree;
//前序创建二叉树
void PreCreatBiTree(BiTree*t);
//前序遍历二叉树
void PreShowBiTree(BiTree t);
//中序遍历二叉树
void MidShowBiTree(BiTree t);
//后序遍历二叉树
void RearShowBiTree(BiTree t);
int main(){
//定义二叉树
BiTree t;
printf("请输入任意字符,#代表空\n");
PreCreatBiTree(&t);
printf("前序遍历结果为:\n");
PreShowBiTree(t);
printf("\n");
printf("中序遍历结果为:\n");
MidShowBiTree(t);
printf("\n");
printf("后序遍历结果为:\n");
RearShowBiTree(t);
printf("\n");
}
void PreCreatBiTree(BiTree*t){
char ch;
scanf("%c",&ch);
if(ch == '#')
{
*t = NULL;
}
else
{
*t = (BiTree)malloc(sizeof(BiTNode));
if(NULL == (*t))return;
(*t) -> data = ch;
PreCreatBiTree(&(*t)->L);
PreCreatBiTree(&(*t)->R);
}
}
void PreShowBiTree(BiTree t){
if(t != NULL)
{
printf("%c",t->data);
PreShowBiTree(t->L);
PreShowBiTree(t->R);
}
}
void MidShowBiTree(BiTree t){
if(t != NULL)
{
MidShowBiTree(t->L);
printf("%c",t ->data);
MidShowBiTree(t->R);
}
}
void RearShowBiTree(BiTree t){
if(t != NULL)
{
RearShowBiTree(t->L);
RearShowBiTree(t->R);
printf("%c",t->data);
}
}