您的位置:首页 > 理论基础 > 数据结构算法

数据结构实验之求二叉树后序遍历和层次遍历

2014-10-09 23:55 204 查看

数据结构实验之求二叉树后序遍历和层次遍历


Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^

题目描述

已知一棵二叉树的前序遍历和中序遍历,求二叉树的后序遍历。

输入

输入数据有多组,第一行是一个整数t (t<1000),代表有t组测试数据。每组包括两个长度小于50 的字符串,第一个字符串表示二叉树的先序遍历序列,第二个字符串表示二叉树的中序遍历序列。

输出

每组第一行输出二叉树的后序遍历序列,第二行输出二叉树的层次遍历序列

示例输入

2
abdegcf
dbgeafc
xnliu
lnixu


示例输出

dgebfca
abcdefg
linux
xnuli


提示

来源

ma6174

示例程序

被这些指针链表啥的给搞哭了。。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node
{
char data;
struct node *l,*r;
};
struct node *creat(int n,char *str1,char *str2)//二叉树的先序遍历的第一个节点是子树的根,中序遍历中根节点的左边全都是左子树的中序,右边是右子树的中序
{
struct node *root;
char *p;
if(n==0)//当节点为零时表明数据全部进入二叉树;
return NULL;
root=(struct node *)malloc (sizeof(struct node));
root->data=str1[0];//先序的第一个为根节点
for(p=str2;p!='\0';p++)
{
if(*p==str1[0])//在中序中找到根节点,从而分为左右两个部分;
break;
}
int t=p-str2;//左子树节点的个数
root->l=creat(t,str1+1,str2);//递归创建左子树;
root->r=creat(n-t-1,str1+t+1,p+1);//递归创建右子树
return root;
};
void houxu(struct node *root)
{
if(root)
{
houxu(root->l);
houxu(root->r);
printf("%c",root->data);
}
}
void bianli(struct node *root)
{
int cnt=0;
int r1=1;
struct node *q[60];//指针控制
q[0]=root;
while(cnt<r1)//每一个q[cnt]都是子数树根;
{
if(q[cnt])//为二叉树所以最多有2个子树,且只有当二叉树不为空时才可能有数据,为空时即可跳过,不打印
{
printf("%c",q[cnt]->data);
q[r1++]=q[cnt]->l;
q[r1++]=q[cnt]->r;
}
cnt++;
}
}
int main()
{
int n;
char str1[60],str2[60];
struct node *root;
root=(struct node *)malloc(sizeof(struct node));
scanf("%d",&n);
getchar();
while(n--)
{
scanf("%s %s",str1,str2);
int n=strlen(str1);
root=creat(n,str1,str2);
houxu(root);
printf("\n");
bianli(root);
printf("\n");
}
return 0;
}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: