您的位置:首页 > 其它

新手对二叉树先序,中序,后序的学习(POJ - 2255 (递归))

2020-05-11 04:10 183 查看

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!

Input
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.

Output
For each test case, recover Valentine’s binary tree and print one line containing the tree’s postorder traversal (left subtree, right subtree, root).
Sample Input
DBACEGF ABCDEFG
BCAD CBAD
Sample Output
ACBFGED
CDAB

1327654

此图 先序为 根左右 也就是 1 左(根左右 2 4 5)右(根 左右 3 6 7)
即 先序 1 2 4 5 3 6 7
后序 为 左右根 左(左 右 根 4 5 2)右(左 右 根 6 7 3)根 1
中序 左 根 右 左(4 2 5) 根 1 右(6 3 7)
出题有 给出中序 后序 求先序 ,给出 中序 先序 求 后序。
本题 为求后序
算法知识:递归
跟着流程 给出 先序 DBACEGF 中序 ABCDEFG 那么根据先序 先根 为 D 又 中序为左根右 那么 左边为ABC 右边为EFG 那么 树为

DEBFGCA

那么后序为左右根 即为
ACBGFED

#include<stdio.h>
#include<string.h>
using namespace std;
char a[1002],b[1002],flag;
void dfs(int l1,int r1,int l2,int r2)
{
if(l1>r1)
return ;
int i,k=-1;
for(i=l2;a[i]!=b[l1];i++);
dfs(l1+1,r1-r2+i,l2,i-1);
dfs(r1-r2+i+1,r1,i+1,r2);
printf("%c",b[l1]);
}
int main()
{
int n,i;
while(~scanf("%s",&b))
{
int l1,l2;
scanf("%s",&a);
l1=strlen(b);
l2=strlen(a);
dfs(0,l1-1,0,l2-1);
printf("\n");
}
return 0;
}
/**p=strchr(s2,s1[0]); *p里边存的是在s2中第一次出现s1[0]的位置开始一直到s2字符串结束,
p=strchr(s2,s1[0])-s2; p里边存的是s1[0]在s2中的位置,是一个数。&
DBACEGF ABCDEFG
BCAD CBAD
样例输出 :
ACBFGED
CDAB*/
#include <stdio.h>
#include <string.h>
char s1[20],s2[20],ans[20];
void build(int n,char* s1,char*s2,char *s)
{
if(n<=0)
return ;
int p=strchr(s2,s1[0])-s2; //找到根结点在s2中的位置
build(p,s1+1,s2,s);  //建立左子树的后序遍历
build(n-p-1,s1+p+1,s2+p+1,s+p);//建立右子树的后序遍历
s[n-1]=s1[0];//在最后的位置将根结点插入
}//整个遍历之后,一次递归将当前的根结点放在最后,递归完毕,后序遍历也就出来了。
int main (void)
{
int n,i;
while(scanf("%s%s",s1,s2)!=EOF)  //s1表示先序遍历,s2代表中序遍历
{
n=strlen(s1);
build(n,s1,s2,ans);  //构造后序遍历
ans[n]='\0';
printf("%s\n",ans); //输出后序遍历
}
return 0;
}
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
typedef struct node
{
char ch;
struct node *left, *right;
}node;         //定义节点的结构

node *creat(char *pre, char *in, int len)    //创建后序遍历的函数
{
int k;
if (len <= 0)
return NULL;
node *head = (node*)malloc(sizeof(node));
head -> ch = *pre;
char *p;
for (p = in; p != NULL; p++){
if (*p == *pre)    //在中序遍历的序列中得到与先序相同的节点
break;
}
k = p - in;
head -> left = creat(pre + 1, in, k);    //递归调用得到左子树
head -> right = creat(pre + k + 1, p + 1, len - k - 1);    //得到右子树
return head;
}

void print(node *head)    //打印后序遍历序列
{
if (head == NULL)
return ;
print(head -> left);
print(head -> right);
cout << head -> ch;
}

int main()
{
char pre[30], in[30];      //存储先序和中序遍历的序列
node *head;
head = (node*)malloc(sizeof(node));
while (cin >> pre >> in){
int len = strlen(pre);
head = creat(pre, in, len);
print(head);
cout << endl;
}
return 0;
}
Starry_Sky_Dream 原创文章 50获赞 4访问量 2368 关注 私信
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐