#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
char value;
struct Node *leftChild;
struct Node *rightChild;
}TreeNode;
//根據中序和後序序列構造二叉樹
TreeNode *CreateFromMidPst(char mid[],int midLeft,int midRight,char pst[],int pstLeft,int pstRight)
{
TreeNode *node = (TreeNode*)malloc(sizeof(TreeNode));
int midLeft1,midRight1,pstLeft1,pstRight1,rootPos=0;
if(midRight-midLeft != pstRight-pstLeft)
{
printf("錯誤:輸入的中序和後序序列結點個數不一樣!\n");
return node;
}
for(;mid[rootPos]!=pst[pstRight];rootPos++);
node->value = pst[pstRight];;
node->leftChild = NULL;
node->rightChild = NULL;
if(rootPos!=midLeft)
{
midRight1 = rootPos-1;
pstRight1 = pstLeft+(midRight1-midLeft);
node->leftChild = CreateFromMidPst(mid,midLeft,midRight1,pst,pstLeft,pstRight1);
}
if(rootPos!=midRight)
{
midLeft1 = rootPos+1;
pstLeft1 = pstLeft+rootPos-midLeft;
node->rightChild = CreateFromMidPst(mid,midLeft1,midRight,pst,pstLeft1,pstRight-1);
}
return node;
}
//根據中序和前序序列構造二叉樹
TreeNode *CreateFromMidPre(char mid[],int midLeft,int midRight,char pre[],int preLeft,int preRight)
{
TreeNode *node = (TreeNode*)malloc(sizeof(TreeNode));
int midLeft1,midRight1,preLeft1,preRight1,rootPos=0;
if(midRight-midLeft != preRight-preLeft)
{
printf("錯誤:輸入的中序和前序序列結點個數不一樣!\n");
return node;
}
node->value = pre[preLeft];
node->leftChild = NULL;
node->rightChild = NULL;
for(;mid[rootPos]!=pre[preLeft];rootPos++);
if(rootPos!=midLeft)
{
midRight1 = rootPos-1;
preRight1 = rootPos-midLeft+preLeft;
node->leftChild = CreateFromMidPre(mid,midLeft,midRight1,pre,preLeft+1,preRight1);
}
if(rootPos!=midRight)
{
midLeft1 = rootPos+1;
preLeft1 = rootPos-midLeft+preLeft+1;
node->rightChild = CreateFromMidPre(mid,midLeft1,midRight,pre,preLeft1,preRight);
}
return node;
}
//前序走訪二叉樹
void PreTraverse(TreeNode *root)
{
if(root==NULL)
return;
printf("%c",root->value);
PreTraverse(root->leftChild);
PreTraverse(root->rightChild);
return;
}
//中序遍曆二叉樹
void MidTraverse(TreeNode *root)
{
if(root==NULL)
return;
MidTraverse(root->leftChild);
printf("%c",root->value);
MidTraverse(root->rightChild);
return;
}
//後序遍曆二叉樹
void PstTraverse(TreeNode *root)
{
if(root==NULL)
return;
PstTraverse(root->leftChild);
PstTraverse(root->rightChild);
printf("%c",root->value);
return;
}
int main()
{
TreeNode *root1,*root2;
char preSeq[20],midSeq[20],pstSeq[20];
int i=0,numNode;
printf("請輸入節點總數:\n");
scanf("%d",&numNode);
fflush(stdin);
printf("請輸入先序序列:\n");
do
{
scanf("%c",preSeq+i);
i++;
}
while(i!=numNode);
fflush(stdin);
i=0;
printf("請輸入中序序列:\n");
do
{
scanf("%c",midSeq+i);
i++;
}
while(i!=numNode);
fflush(stdin);
i=0;
printf("請輸入後序序列:\n");
do
{
scanf("%c",pstSeq+i);
i++;
}
while(i!=numNode);
fflush(stdin);
root1=CreateFromMidPre(midSeq,0,i-1,preSeq,0,i-1);
printf("\n");
printf("前序序列:\n");
PreTraverse(root1);
printf("\n");
printf("中序序列:\n");
MidTraverse(root1);
printf("\n");
printf("後序序列:\n");
PstTraverse(root1);
printf("\n");
root2=CreateFromMidPst(midSeq,0,i-1,pstSeq,0,i-1);
printf("\n");
printf("前序序列:\n");
PreTraverse(root2);
printf("\n");
printf("中序序列:\n");
MidTraverse(root2);
printf("\n");
printf("後序序列:\n");
PstTraverse(root2);
printf("\n");
return 1;
}