已知中序和后序,求前序

例子,后序遍历为 gbdehfca,中序遍历为 dgbaechf 
后序遍历中的最后一个元素是根节点,a,然后查找中序中a的位置 
把中序遍历分成 dgb a echf,而因为节点个数要对应 
后序遍历分为 gbd ehfc a,gbd为左子树,ehfc为右子树,这样又可以递归计算了
最后形成的二叉树如下图片所示:

 

具体代码如下:

递归:

  1. #include<iostream.h>  
  2.   
  3. #include<string.h>  
  4.   
  5. #include<stdlib.h>  
  6.   
  7. #define MAX 20 /*预定义字符数组的最大长度*/  
  8.   
  9. typedef struct tnode /*该结构体类型为树结点的类型*/  
  10.   
  11. {  
  12.   
  13.        char data;  
  14.   
  15.        struct tnode *lchild;  
  16.   
  17.        struct tnode *rchild;  
  18.   
  19. } *bt;  
  20.   
  21. void in_post_to_bt(char *in,char *post,int len,bt &T) /*由长度为len的中序序列in和后序序列post唯一确定一棵二叉树T*/  
  22.   
  23. {  
  24.   
  25.        int k;  
  26.   
  27.        if(len<=0)  
  28.   
  29.        {  
  30.   
  31.               T=NULL;  
  32.   
  33.               return;  
  34.   
  35.        }  
  36.   
  37.        for(char *temp=in;temp<in+len;temp++) /*在中序序列in中找到根节点所在的位置*/  
  38.   
  39.               if(*(post+len-1)==*temp)  
  40.   
  41.               {  
  42.   
  43.                      k=temp-in;  /*k为根结点在中序序列中的下标*/  
  44.   
  45.                      T=(bt)malloc(sizeof(struct tnode));  
  46.   
  47.                      T->data =*temp;  
  48.   
  49.                      break;  
  50.   
  51.               }  
  52.   
  53.        in_post_to_bt(in,post,k,T->lchild ); /*建立左子树*/  
  54.   
  55.        in_post_to_bt(in+k+1,post+k,len-k-1,T->rchild ); /*建立右子树*/  
  56.   
  57. }  
  58.   
  59. void in_visit(bt T)/*中序遍历树T*/  
  60.   
  61. {  
  62.   
  63.        if(T)  
  64.   
  65.        {  
  66.   
  67.               in_visit(T->lchild );  
  68.   
  69.               cout<<T->data ;  
  70.   
  71.               in_visit(T->rchild );  
  72.   
  73.        }  
  74.   
  75. }  
  76.   
  77. void post_visit(bt T)/*后序遍历树*/  
  78.   
  79. {  
  80.   
  81.        if(T)  
  82.   
  83.        {  
  84.   
  85.               post_visit(T->lchild );  
  86.   
  87.               post_visit(T->rchild );  
  88.   
  89.               cout<<T->data ;  
  90.   
  91.        }  
  92.   
  93. }  
  94.   
  95. main()  
  96.   
  97. {  
  98.   
  99.        char in[MAX+1],post[MAX+1];  
  100.   
  101.        cout<<"输入中序序列:";  
  102.   
  103.        cin>>in;  
  104.   
  105.        cout<<"输入后序序列:";  
  106.   
  107.        cin>>post;  
  108.   
  109.        bt T;  
  110.   
  111.        int len_in=strlen(in),len_post=strlen(post);  
  112.   
  113.        if(len_in<=MAX&&len_post<=MAX)  
  114.   
  115.               in_post_to_bt(in,post,len_in,T);  
  116.   
  117.   
  118.        cout<<endl<<"输出中序序列:";  
  119.   
  120.        in_visit(T);  
  121.   
  122.        cout<<endl<<"输出后序序列:";  
  123.   
  124.        post_visit(T);  
  125.   
  126.        cout<<endl;  
  127.   
  128. }  





原文地址:http://blog.csdn.net/wuchuanpingstone/article/details/6860453
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值