已知二叉树先序、中序(中序、后序)遍历序列,输出后序(先序)遍历序列
2012-09-09 19:36
399 查看
1、已知二叉树的先序遍历序列为A B D F E G H C,中序遍历序列为D F B G E H A C,构建二叉树并输出二叉树的后序遍历序列。
思想:
先序遍历二叉树的顺序为 根 左子树 右子树
中序遍历二叉树的顺序为 左子树、根、右子树;
首先根据二叉树的先序遍历序列确定二叉树的根,再根据中序遍历序列确定二叉树的左子树和右子树,再递归的对左子树和右子树进行以上操作。
代码如下:
2、已知二叉树的中序遍历序列为D F B G E H A C,后序遍历序列为F D G H E B C A,构建二叉树并输出二叉树的先序遍历序列。
思想:
中序遍历二叉树的顺序为 左子树、根、右子树;
后序遍历二叉树的顺序为 左子树、右子树、根;
首先根据后序遍历序列确定二叉树的根,再根据中序遍历序列确定二叉树的左子树和右子树,再递归的对左子树和右子树进行以上操作。
代码如下:
思想:
先序遍历二叉树的顺序为 根 左子树 右子树
中序遍历二叉树的顺序为 左子树、根、右子树;
首先根据二叉树的先序遍历序列确定二叉树的根,再根据中序遍历序列确定二叉树的左子树和右子树,再递归的对左子树和右子树进行以上操作。
代码如下:
# include<iostream> using namespace std; typedef struct BinNode{ char data; BinNode *lchild,*rchild; }bin_tree,*tree; char pre_order[8]={'A','B','D','F','E','G','H','C'}; char mid_order[8]={'D','F','B','G','E','H','A','C'}; tree t; BinNode *creat_tree(char *pre,char *mid,int n){ char *it_mid; int left,right; tree p; if(n<=0) return NULL; p=new bin_tree; if(!p) exit(0); else{ p->data=*pre; if(*pre=='A') t=p; for(it_mid=mid;it_mid<mid+n;it_mid++){ if(*it_mid==*pre) break; } left=it_mid-mid; right=n-left-1; p->lchild=creat_tree(pre+1,mid,left); p->rchild=creat_tree(pre+left+1,it_mid+1,right); return(p); } } void post_order(tree &t){ if(t){ post_order(t->lchild); post_order(t->rchild); cout<<t->data<<" "; } } void main(){ creat_tree(pre_order,mid_order,8); post_order(t); cout<<endl; }
2、已知二叉树的中序遍历序列为D F B G E H A C,后序遍历序列为F D G H E B C A,构建二叉树并输出二叉树的先序遍历序列。
思想:
中序遍历二叉树的顺序为 左子树、根、右子树;
后序遍历二叉树的顺序为 左子树、右子树、根;
首先根据后序遍历序列确定二叉树的根,再根据中序遍历序列确定二叉树的左子树和右子树,再递归的对左子树和右子树进行以上操作。
代码如下:
# include<iostream> using namespace std; typedef struct BinNode{ char data; BinNode *lchild,*rchild; }bin_tree,*tree; char mid_order[8]={'D','F','B','G','E','H','A','C'}; char post_order[8]={'F','D','G','H','E','B','C','A'}; tree t; //保存二叉树的根,用作之后的先序遍历二叉树的参数 BinNode *creat_tree(char *mid,char *post,int n){ char *it_mid,*it_mid1,*it_post; int left,right; //保存二叉树左子树和右子树的节点个数 tree p; if(n<=0) return NULL; p=new bin_tree; if(!p) exit(0); else{ p->data=*post; if(*post=='A') t=p; for(it_mid=mid;it_mid>mid-n;it_mid--){ if(*it_mid==*post) break; } right=mid-it_mid; left=n-right-1; for(it_post=post-1;it_post>post-n+1;it_post--){ it_mid1=it_mid-1; while(*it_mid1!=*it_post&&it_mid1-1>it_mid-n+right){ it_mid1--; } if(*it_mid1==*it_post) break; } p->lchild=creat_tree(it_mid-1,it_post,left); p->rchild=creat_tree(it_mid+right,post-1,right); return(p); } } void pre_order(tree &t){ if(t){ cout<<t->data<<" "; pre_order(t->lchild); pre_order(t->rchild); } } void main(){ creat_tree(mid_order+7,post_order+7,8); pre_order(t); cout<<endl; }
相关文章推荐
- 已知二叉树的先序遍历序列和中序遍历序列,输出该二叉树的后序遍历序列
- 已知二叉树的先序遍历序列和中序遍历序列,输出该二叉树的后序遍历序列
- 已知二叉树的先序遍历序列和中序遍历序列,输出该二叉树的后序遍历序列
- 已知二叉树的先序遍历序列和中序遍历序列,输出该二叉树的后序遍历序列
- 已知二叉树的先序遍历序列和中序遍历序列,输出该二叉树的后序遍历序列
- 已知前序遍历序列和中序遍历序列,求二叉树的后序遍历
- 已知先序遍历和中序遍历,输出他的后序遍历序列.
- 【数据结构作业四】以二叉链表作存储结构,建立一棵二叉树,并输出该二叉树的先序、中序、后序遍历序列、高度和其叶子结点数。
- 已知一颗二叉树S的前序遍历和中序遍历序列,请编程输出二叉树S的后续遍历序列。
- 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列。这里假设键值都是互不相等的正整数。
- 已知二叉树的先序遍历和中序遍历序列,输出后序遍历序列
- 已知一颗二叉树S的前序遍历和中序遍历 序列,请编程输出二叉树S的后续遍历序列.
- 二叉树:根据二叉树的先序遍历序列和中序遍历序列,输出该二叉树的后序遍历序列
- 先序遍历序列和中序遍历序列,输出该二叉树的后序遍历序列
- 二叉树系列(一):已知先序遍历序列和中序遍历序列,求后序遍历序列
- 二叉树--已知前序遍历和中序遍历,输出后续遍历
- 由前序和中序遍历建立二叉树并后序遍历输出二叉树
- 二叉树中先序、中序、后序遍历序列
- 已知二叉树的前序遍历和中序遍历,如何得到它的后序遍历?
- 已知二叉树前序、后序遍历如何求中序