码迷,mamicode.com
首页 > 其他好文 > 详细

树 | 根据前序、中序生成后序,根据后序、中序生成前序

时间:2018-02-01 22:04:42      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:play   while   put   --   space   math   stdio.h   text   col   

使用的全局变量:

int in[LEN]={4,2,5,1,6,3,7};
int pre[LEN]={1,2,4,5,3,6,7};
int post[LEN];
int t=0;

 

根据前序、中序生成后序:

//caution: should using initialize code: " t=0; "
//pre in -> post
//        pre_start pre_end in_start in_end
void setPost(int ps,int pe,int is,int ie){
    if(ps>pe)return;//null
    if(ps==pe){
        post[t++]=pre[ps];
    }else{
        //find the elem is the pair of preOrder (ps)
        int i=is;
        while(in[i]!=pre[ps] && i<ie) i++;//redirect
        //left
        setPost(ps+1, ps+i-is, is, i-1);
        //right
        setPost(ps+i-is+1, pe, i+1, ie);
        //root
        post[t++]=pre[ps];
    }
}

 

根据后序、中序生成前序:

//caution: should using initialize code: " t=0; "
//post in -> pre
//        post_start post_end in_start in_end
void setPre(int ps,int pe,int is,int ie){
    if(ps>pe)return;//null
    if(ps==pe){
        pre[t++]=post[ps];
    }else{
        //find the elem is the pair of preOrder (ps)
        int i=is;
        while(in[i]!=post[pe] && i<ie) i++;//redirect
        //root
        pre[t++]=post[pe];
        //left
        setPre(ps, ps+i-is-1, is, i-1);
        //right
        setPre(ps+i-is, pe-1, i+1, ie);
    }
}

 

完整代码:

技术分享图片
#include <stdio.h>
#include <memory.h>
#include <math.h>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <map>


#define I scanf
#define OL puts
#define O printf
#define F(a,b,c) for(a=b;a<c;a++)
#define FF(a,b) for(a=0;a<b;a++)
#define FG(a,b) for(a=b-1;a>=0;a--)
#define LEN 7
#define MAX 0x06FFFFFF
#define V vector<int>

using namespace std;

int in[LEN]={4,2,5,1,6,3,7};
int pre[LEN]={1,2,4,5,3,6,7};
int post[LEN];
int t=0;

//caution: should using initialize code: " t=0; "
//pre in -> post
//        pre_start pre_end in_start in_end
void setPost(int ps,int pe,int is,int ie){
    if(ps>pe)return;//null
    if(ps==pe){
        post[t++]=pre[ps];
    }else{
        //find the elem is the pair of preOrder (ps)
        int i=is;
        while(in[i]!=pre[ps] && i<ie) i++;//redirect
        //left
        setPost(ps+1, ps+i-is, is, i-1);
        //right
        setPost(ps+i-is+1, pe, i+1, ie);
        //root
        post[t++]=pre[ps];
    }
}

//caution: should using initialize code: " t=0; "
//post in -> pre
//        post_start post_end in_start in_end
void setPre(int ps,int pe,int is,int ie){
    if(ps>pe)return;//null
    if(ps==pe){
        pre[t++]=post[ps];
    }else{
        //find the elem is the pair of preOrder (ps)
        int i=is;
        while(in[i]!=post[pe] && i<ie) i++;//redirect
        //root
        pre[t++]=post[pe];
        //left
        setPre(ps, ps+i-is-1, is, i-1);
        //right
        setPre(ps+i-is, pe-1, i+1, ie);
    }
}

int main(){
//    freopen("d:/input/A1128.txt","r",stdin);
    t=0;
    setPost(0,LEN-1,0,LEN-1);
    FF(t,LEN) O("%d ",post[t]);
    puts("");
    t=0;
    setPre(0,LEN-1,0,LEN-1);
    FF(t,LEN) O("%d ",pre[t]);
    return 0;
}
View Code

 


测试的数据结构:

技术分享图片

 

  

技术分享图片

 

:测试效果

 

树 | 根据前序、中序生成后序,根据后序、中序生成前序

标签:play   while   put   --   space   math   stdio.h   text   col   

原文地址:https://www.cnblogs.com/TQCAI/p/8401355.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!