#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
char value;
struct Node *leftChild;
struct Node *rightChild;
}TreeNode;
{
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;
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 *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;
}
{
if(root==NULL)
return;
MidTraverse(root->leftChild);
printf("%c",root->value);
MidTraverse(root->rightChild);
return;
}
{
if(root==NULL)
return;
PstTraverse(root->leftChild);
PstTraverse(root->rightChild);
printf("%c",root->value);
return;
}
{
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;
}
#include <stdlib.h>
typedef struct Node
{
char value;
struct Node *leftChild;
struct Node *rightChild;
}TreeNode;
//根据中序和后序序列构造二叉树
{
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 *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;
}
//中序遍历二叉树
{
if(root==NULL)
return;
MidTraverse(root->leftChild);
printf("%c",root->value);
MidTraverse(root->rightChild);
return;
}
//后序遍历二叉树
{
if(root==NULL)
return;
PstTraverse(root->leftChild);
PstTraverse(root->rightChild);
printf("%c",root->value);
return;
}
{
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;
}