# Tree 二叉树的建立 和遍历

http://acm.nyist.net/JudgeOnline/problem.php?pid=221
Tree

Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes.
This is an example of one of her creations:
D

/ \

/   \

B     E

/ \     \

/   \     \

A     C     G

/

/

F

To record her trees for future generations, she wrote down two strings for each tree: a preorder traversal (root, left subtree, right subtree) and an inorder traversal (left subtree, root, right subtree). For the tree drawn above the preorder traversal is DBACEGF and the inorder traversal is ABCDEFG.
She thought that such a pair of strings would give enough information to reconstruct the tree later (but she never tried it).

Now, years later, looking again at the strings, she realized that reconstructing the trees was indeed possible, but only because she never had used the same letter twice in the same tree.
However, doing the reconstruction by hand, soon turned out to be tedious.
So now she asks you to write a program that does the job for her!

The input will contain one or more test cases.
Each test case consists of one line containing two strings preord and inord, representing the preorder traversal and inorder traversal of a binary tree. Both strings consist of unique capital letters. (Thus they are not longer than 26 characters.)
Input is terminated by end of file.

For each test case, recover Valentine's binary tree and print one line containing the tree's postorder traversal (left subtree, right subtree, root).

DBACEGF ABCDEFG

ACBFGED
CDAB

typedef struct node//定义结点
{
char data;
struct node *lchild,*rchild;
} Node,*BitTree;

int search(char ino[],char c)//在中序序列中查找先序中该元素所在位置
{
int i=0;
while(ino[i]!=c&&ino[i])  i++;
if(ino[i]==c)   return i;
}

void CrtBT(BitTree &T,char pre[],char ino[],int ps,int is,int n)/*递归算法构造函数，建立二叉链表*/
{
int k;
if(n==0)  T=NULL;
else
{
k=search(ino,pre[ps]);//找到在中序中的位置  分为两部分继续递归
T=(BitTree)malloc(sizeof(Node));
T->data=pre[ps];
if(k==is)     T->lchild=NULL;//如果中序中 字符左为空的话  左子树即为空
//先序前进一个  中序不变  字符程度为总长度 k减去  is  既 左边剩下的个数
else     CrtBT(T->lchild,pre,ino,ps+1,is,k-is);//
if(k==is+n-1)     T->rchild=NULL;
//同理
else     CrtBT(T->rchild,pre,ino,ps+1+(k-is),k+1,n-(k-is)-1);
}
}

void PostOrder(BitTree T)
{
if(T)
{
PostOrder(T->lchild);
PostOrder(T->rchild);
printf("%c",T->data);
}
}

#include<stdio.h>
#include<stdlib.h>
#include<cstring>
#define size 100
typedef struct node//定义结点
{
char data;
struct node *lchild,*rchild;
} Node,*BitTree;
int search(char ino[],char c)//在中序序列中查找先序中该元素所在位置
{
int i=0;
while(ino[i]!=c&&ino[i])  i++;
if(ino[i]==c)   return i;
}
void CrtBT(BitTree &T,char pre[],char ino[],int ps,int is,int n)/*递归算法构造函数，建立二叉链表*/
{
int k;
if(n==0)  T=NULL;
else
{
k=search(ino,pre[ps]);//找到在中序中的位置  分为两部分继续递归
T=(BitTree)malloc(sizeof(Node));
T->data=pre[ps];
if(k==is)     T->lchild=NULL;//如果中序中 字符左为空的话  左子树即为空
//先序前进一个  中序不变  字符程度为总长度 k减去  is  既 左边剩下的个数
else     CrtBT(T->lchild,pre,ino,ps+1,is,k-is);//
if(k==is+n-1)     T->rchild=NULL;
//同理
else     CrtBT(T->rchild,pre,ino,ps+1+(k-is),k+1,n-(k-is)-1);
}
}
void PostOrder(BitTree T)
{
if(T)
{
PostOrder(T->lchild);
PostOrder(T->rchild);
printf("%c",T->data);
}
}
int main()
{
char pre[size],ino[size];
while(scanf("%s%s",pre,ino)!=EOF)
{
BitTree T=NULL;
CrtBT(T,pre,ino,0,0,strlen(pre));
PostOrder(T);
printf("\n");
}
}



• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120