//日常记录 C语言
#include
#include
#include
#define MaxSize 100
typedef struct Node
{
Node * lchild , * rchild;
char data;
}Node;
typedef struct Stack
{
Node * val;
int flag ;
}Stack;
Node Tree[MaxSize];
int a=0;
char Str1[100];
char Str2[100];
Node * Creat()
{
Tree[a].lchild = NULL;
Tree[a].rchild = NULL;
return &Tree[a++] ;
}
Node * Build(int q1,int q2,int z1,int z2)
{
Node * T = Creat();
T->data = Str1[q1];
int flag = z1;
for(int j= z1 ; jlchild = Build(q1+1, q1+flag-z1 , z1, flag-1);
}
if(flag != z2 )
{
T->rchild = Build(q1+flag-z1+1 ,q2 , flag+1 ,z2);
}
return T;
}
void Post( Node * H) //非递归
{
Stack S[MaxSize];
int top = -1;
while (H!=NULL || top != -1)
{
while(H!= NULL)
{
top++;
S[top].val = H;
S[top].flag = 1;
H = H->lchild ;
}
while(top != -1 && S[top].flag == 2)
{
H = S[top--].val;
printf("%c ",H->data);
}
if(top!= -1)
{
S[top].flag = 2;
H = S[top].val->rchild;
}
else H = NULL;
}
}
//void Post(Node *H) //递归
//{
// if(H->lchild!=NULL)
// {
// Post(H->lchild);
// }
// if(H->rchild!=NULL)
// {
// Post(H->rchild);
// }
// printf("%c",H->data);
//}
int main()
{
printf("请输入前序遍历序列和后序遍历序列 中间用空格隔开~n");
scanf("%s%s",&Str1,&Str2);
int q2= strlen(Str1);
int z2= strlen(Str2);
Node * T = Build(0,q2-1,0,z2-1);
printf("n遍历结果为:");
Post(T);
// Post(T);
}
//测试数据
// A
// B C
// D E F G
// 输入 ABDECFG DBEAFCG
// 输出
//请输入前序遍历序列和后序遍历数列 中间用空格隔开~
//ABDECFG DBEAFCG
//遍历结果为:D E B F G C A
//--------------------------------