初次做二叉树的题目--给定中序和前序求后序
来源:互联网 发布:淘宝装修平台 编辑:程序博客网 时间:2024/06/07 19:05
Farmer John takes the heritage of his cows very seriously. He is not, however, a truly fine bookkeeper. He keeps his cow genealogies as binary trees and, instead of writing them in graphic form, he records them in the more linear `tree in-order' and `tree pre-order' notations.
Your job is to create the `tree post-order' notation of a cow's heritage after being given the in-order and pre-order notations. Each cow name is encoded as a unique letter. (You may already know that you can frequently reconstruct a tree from any two of the ordered traversals.) Obviously, the trees will have no more than 26 nodes.
Here is a graphical representation of the tree used in the sample input and output:
C / \ / \ B G / \ / A D H / \ E FThe in-order traversal of this tree prints the left sub-tree, the root, and the right sub-tree.
The pre-order traversal of this tree prints the root, the left sub-tree, and the right sub-tree.
The post-order traversal of this tree print the left sub-tree, the right sub-tree, and the root.
PROGRAM NAME: heritageINPUT FORMAT#include<stdio.h>#include<string.h>#include<malloc.h>char root;FILE *fin,*fout;char pre_order[28],in_order[28];struct BNode{ char data; BNode * ld,* rd;};int pos=0;void createtree(BNode * &b,int first,int last) //这里用了引用类型{ int i; if(first>last) return ; b=(BNode *)malloc(sizeof(BNode)); for(i=first;i<=last;i++) if(in_order[i]==pre_order[pos]) break; b->data=pre_order[pos]; pos++;
b->ld=NULL;
b->rd=NULL; createtree(b->ld,first,i-1); createtree(b->rd,i+1,last);}void post_traversal(BNode *b){ if(b->ld) post_traversal(b->ld); if(b->rd) post_traversal(b->rd); fprintf(fout,"%c",b->data);}int main(){ int len; BNode *BTree; fin=fopen("heritage.in","r"); fout=fopen("heritage.out","w"); fscanf(fin,"%s%s",in_order,pre_order); len=strlen(in_order); createtree(BTree,0,len-1); post_traversal(BTree); fprintf(fout,"\n"); return 0;}
ABEDFCHG
CBADEFGH
------- test 2 -------
F
F
------- test 3 -------
BCAD
ABCD
------- test 4 -------
GOLEAFS
SFAELOG
------- test 5 -------
GSHBAQTPM
ABGHSPQTM
------- test 6 -------
AUBYCVDZEWFXGTH
ZYUABVCDXWEFTGH
------- test 7 -------
ABDCJHKILMNPOQFEGRS
ABCDEFHJIKLMNOPQGRS
------- test 8 -------
GFDIHKLJMBNESRTPOQAUCWVZYX
ABDFGHIJKLMENOPRSTQCUVWXYZ
------- test 9 -------
EHGDIFJLKMBNCOQSPRAWUXZYTV
ABDEGHFIJKLMCNOPQSRTUWXYZV
- 初次做二叉树的题目--给定中序和前序求后序
- 二叉树的建立、遍历,以及给定二叉树前序遍历和中序遍历重建二叉树问题。
- Binary Tree——给定二叉树的前序遍历和中序遍历重建二叉树
- 题目:二叉树的中序遍历
- 给定二叉树的先序遍历和中序遍历,输出它的后序遍历序列
- 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回
- 二叉树中找出和为给定值得所有路径
- 给定二叉树的前序和中序,判断是否可以构成一颗二叉树,如果可以输出后序
- 给定先序和后续,构造出一颗二叉树并输出中序序列
- 给定中序-先序,中序-后序序列构建二叉树的算法
- 二叉搜索树中求得给定元素的下界
- 给定一棵二叉树,和一个数值。求二叉树的路径和等于给定值的所有路径
- 给出二叉树,将二叉树进行中序线索化,在根据中序线索化二叉树,找出给定节点的前序后继节点,和给出节点的后序后继节点
- 给定二叉树的先序遍历中序遍历,求后序遍历
- 二叉查找树(4) - 中序查找一个给定值的前驱以及后继
- 试编写一个函数,返回一颗给定二叉树在中序遍历下的最后一个节点(分别用递归和非递归实现)
- 题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。
- 二叉树中根到叶子路径中节点和等于给定值的路径
- BFC规范
- 最小生成树——普里姆算法(Prim)和克鲁斯卡尔算法(Kruskal)
- 【HDU2196】【树形DP】
- 运放的轨到轨与偏置电压
- Android6.0存储中加入总内存和系统内存项和在西语下把,换成.
- 初次做二叉树的题目--给定中序和前序求后序
- 【GDOI2017模拟11.2】相位幻击
- 自定义饼状图
- HDU 2458 Kindergarten (最大独立集)
- Java面向对象-打印和比较对象-对象生命周期-匿名对象
- 在 RHEL、CentOS 和 Fedora 上安装 Git 及设置 Git 账号的技巧
- static
- 学习资源
- mysql 命令行登录