#include <myhead.h>
typedef struct node
{
char data;
struct node *lchild;
struct node *rchild;
}Tree,*Ptree;
int i=0;
Ptree creat_tree(char a[])
{
char e=a[i];
i++;
if(e=='#')
{
return NULL;
}
Ptree P=malloc(sizeof(Tree));
if(P==NULL)
{
printf("创建失败\n");
}
P->data=e;
P->lchild=creat_tree(a);
P->rchild=creat_tree(a);
return P;
}
void mid(Ptree T)
{
if(T!=NULL)
{
mid(T->lchild);
printf("%c\t",T->data);
mid(T->rchild);
}
}
void last(Ptree T)
{
if(T!=NULL)
{
last(T->lchild);
last(T->rchild);
printf("%c\t",T->data);
}
}
void first(Ptree T)
{
if(T!=NULL)
{
printf("%c\t",T->data);
first(T->lchild);
first(T->rchild);
}
}
int main(int argc, const char *argv[])
{ char a[]={'A','B','D','#','#','#','C','E','#','#','#'};
Ptree L=creat_tree(a);
printf("先序:");
first(L);
printf("\n中序:");
mid(L);
printf("\n后序:");
last(L);
return 0;
}