#include<stdio.h>
#include<stdlib.h>
typedef struct TNode
{
char data;
struct TNode *LChild,*RChild;
}TNode,*Tree;
void CreateTree(Tree *T)
{
char ch;
scanf("%c",&ch);
if(ch == '#')
*T = NULL;
else
{
*T = (TNode *)malloc(sizeof(TNode *));
if(*T == NULL)
exit(-1);
(*T)->data = ch;
CreateTree(&((*T)->LChild));
CreateTree(&((*T)->RChild));
}
}
//先序遍历
void PreOrder(Tree T)
{
if(T != NULL)
{
printf("%c ",T->data);
PreOrder(T->LChild);
PreOrder(T->RChild);
}
}
void MidOrder(Tree T)
{
if(T != NULL)
{
MidOrder(T->LChild);
printf("%c ",T->data);
MidOrder(T->RChild);
}
}
void PosOrder(Tree T)
{
if(T != NULL)
{
PosOrder(T->LChild);
PosOrder(T->RChild);
printf("%c ",T->data);
}
}
int main()
{
Tree T;
T = NULL;
CreateTree(&T);
printf("先序遍历:\n");
PreOrder(T);
printf("\n");
printf("中序遍历:\n");
MidOrder(T);
printf("\n");
printf("后序遍历:\n");
PosOrder(T);
printf("\n");
return 0;
}
//运行结果如下