重建二叉树 的 非递归算法(已知前序和中序)输出 层续和后续
2010-06-17 16:14
337 查看
//以下代码仅供参考,有什么可以优化的请指教指教 //联系QQ 344590697
#include <stdio.h> #include <malloc.h> int toprc=-1,tobase=-1;; int before[10000],nowing[10000]; int n; int a=0; int AP=0; int now=1; int AAAA=0; typedef struct btree { int date; struct btree *lchrild,*rchrild; }BT; typedef struct RootCollection { int root[10000]; int rootI[10000]; int top; int topI; }RC; RC rc; BT *btrc[2000]; BT *creat(); BT *RebuildBT(BT *T); int Check();//检验是否是左子树 或 右子树 void Push(int a);//压入前序中当前节点在中序中的位置 void PUSH(int root);//压入前序当前节点数据 void LaverTrave(BT *T);//层序遍历 void LateTrave(BT *T);//后续遍历 void Push(int a) { if(rc.topI<10000) { rc.topI++; rc.rootI[rc.topI]=a; } else printf("栈已满/n"); } void PUSH(int root) { if(rc.top<10000) { rc.top++; rc.root[rc.top]=root; } else printf("栈已满/n"); } BT *creat() { BT *bitree; bitree=(BT*) malloc (sizeof(BT)); bitree->date=before[0]; bitree->lchrild = NULL; bitree->rchrild = NULL; PUSH(before[0]); Push(a); return bitree; } int Check() { int anwser=-1; int i; int rt=0; int j; AP=rc.topI; for(i=0;i<n;i++)///新的节点入栈 if(nowing[i]==before[now]) { Push(i); break; } if(rc.rootI[rc.topI]<rc.rootI[rc.topI-1])///判断左子树 anwser=0; else for(i=rc.topI-1;i>=0;i--)//判断右子树 { if(rc.rootI[rc.topI]<rc.rootI[i]) { for(j=i+1;j<rc.topI-1;j++) if(rc.rootI[j]>rc.rootI[j+1]) break; AP=j; toprc=AP; rc.rootI[AP+1]=rc.rootI[rc.topI]; rc.topI=AP+1; rc.top=AP+1; anwser=1; break; } else if(rc.rootI[i]==AAAA) { AAAA=rc.rootI[rc.topI]; anwser=1; toprc=i; rc.rootI[i+1]=rc.rootI[rc.topI]; rc.topI=i+1; rc.top=i+1; } } return anwser; } BT *RebuildBT(BT *T) { BT *t; int anwser; toprc++; btrc[toprc]=T; while(now<n) { anwser=Check(); if(!anwser) { t=btrc[toprc]; t->lchrild=(BT *) malloc (sizeof(BT)); t->lchrild->date=before[now]; T=btrc[toprc]; PUSH(before[now]); now++; toprc++; btrc[toprc]=T->lchrild; T=btrc[toprc]; T->lchrild = NULL; T->rchrild = NULL; } if(anwser==1) { t=btrc[toprc]; t->rchrild=(BT *) malloc (sizeof(BT)); t->rchrild->date=before[now]; T=btrc[toprc];PUSH(before[now]); now++; toprc++; btrc[toprc]=T->rchrild; T=btrc[toprc]; T->lchrild = NULL; T->rchrild = NULL; } } return btrc[0]; } void LaverTrave(BT *T) { tobase++; toprc++; btrc[toprc]=T; while(tobase<=toprc) { printf("%d ", btrc[tobase]->date); if(btrc[tobase]->lchrild!=NULL) { toprc++; btrc[toprc]=btrc[tobase]->lchrild; } if(btrc[tobase]->rchrild!=NULL) { toprc++; btrc[toprc]=btrc[tobase]->rchrild; } tobase++; } } void LateTrave(BT *T) { if(T==NULL) return ; else { LateTrave(T->lchrild); LateTrave(T->rchrild); printf("%d ",T->date); } } void main () { BT *bitree; int i; scanf("%d",&n); rc.top=-1; rc.topI=-1; for(i=0;i<n;i++) scanf("%d",&before[i]); for(i=0;i<n;i++) { scanf("%d",&nowing[i]); if(nowing[i]==before[0]) { a=i; AAAA=a; } } bitree=creat(); bitree=RebuildBT(bitree); toprc=-1; LaverTrave(bitree); printf("/n");// LateTrave(bitree);//后续遍历 }
相关文章推荐
- 已知二叉树的先序(前序)遍历及中序遍历,还原该二叉树并输出其后续遍历
- 已知一颗二叉树S的前序遍历和中序遍历序列,请编程输出二叉树S的后续遍历序列。
- 二叉树--已知前序遍历和中序遍历,输出后续遍历
- 二叉树--已知前序遍历和中序遍历,输出后续遍历
- 已知一颗二叉树S的前序遍历和中序遍历 序列,请编程输出二叉树S的后续遍历序列.
- 牛客_剑指offer_重建二叉树,再后续遍历_递归思想_分两端
- NYOJ221二叉树重建(已知先序和中序求后序)
- Java实现由前序序列和中序序列重建二叉树,并后序输出。
- 数据结构,二叉树已知后续中序,建树,层次遍历;
- 已知二叉树的前序和中序序列,不建立二叉树来输出后序序列
- 已知先序和中序构建二叉树并且以层序输出
- sicily1935 树的重建(已知树的先序遍历和中序遍历输出树的广度优先遍历)
- 已知二叉树的先序排列和中序排列,重构该二叉树,并输出该树的后序遍历
- 已知前序中序,重建二叉树(加7种遍历的方式)
- 已知二叉树先序、中序(中序、后序)遍历序列,输出后序(先序)遍历序列
- 已知二叉树的两种遍历序列重建树
- 已知二叉树的某两种遍历结果,输出另一种遍历的结果(附源码)
- 以二叉链表的方式创建一棵二叉树,并以非递归算法中序输出;计算二叉树的繁茂度,并判断二叉树是否为完全二叉树
- 已知二叉树的后序遍历和中序遍历重建二叉树(二叉树)
- 已知二叉树先序遍历,中序遍历创建二叉树并输出后序遍历