zoukankan      html  css  js  c++  java
  • (三种遍历二叉树操作实例)(待解决)A

    Description

    Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes.
    This is an example of one of her creations:
                                                   D
    
                                                  / 
    
                                                 /   
    
                                                B     E
    
                                               /      
    
                                              /        
    
                                             A     C     G
    
                                                        /
    
                                                       /
    
                                                      F
    
    

    To record her trees for future generations, she wrote down two strings for each tree: a preorder traversal (root, left subtree, right subtree) and an inorder traversal (left subtree, root, right subtree). For the tree drawn above the preorder traversal is DBACEGF and the inorder traversal is ABCDEFG.
    She thought that such a pair of strings would give enough information to reconstruct the tree later (but she never tried it).

    Now, years later, looking again at the strings, she realized that reconstructing the trees was indeed possible, but only because she never had used the same letter twice in the same tree.
    However, doing the reconstruction by hand, soon turned out to be tedious.
    So now she asks you to write a program that does the job for her!

    Input

    The input will contain one or more test cases.
    Each test case consists of one line containing two strings preord and inord, representing the preorder traversal and inorder traversal of a binary tree. Both strings consist of unique capital letters. (Thus they are not longer than 26 characters.)
    Input is terminated by end of file.

    Output

    For each test case, recover Valentine's binary tree and print one line containing the tree's postorder traversal (left subtree, right subtree, root).

    Sample Input

    DBACEGF ABCDEFG
    BCAD CBAD
    

    Sample Output

    ACBFGED
    CDAB
    



    思路其实很简单,就是结合先根遍历(前序遍历)将中根遍历(中序遍历)分开每次找到相同的就将他分成左右两只大树枝,这样一直递归下去,就可以了

    下图摘自 http://blog.csdn.net/cbs612537/article/details/8529560

    题意:输入两组数据,分别是前序遍历序列和中序遍历序列,你需要编写程序通过这两组数据求出该树的后序遍历序列(前序序列 + 中序序列 = 后序序列

    解法:递归

    题目分析:

    可以先按照用笔和纸的形式去推导出后序序列。推导过程省略,在推导过程中我们会发现规律:

    假设 前序序列是 A B E H F C G I

     中序序列是 H E B F A C I G (图如下)


    每一次从前序序列里,按顺序抽取出字母就能将中序序列分割,并根据中序遍历的特性。分割后的两部分分别是 左子树 右子树(注意,他们也是二叉树!)

    就像这样:取A, 中序序列被分割为 左子树:H E B F  右子树 C I G

    继续取B,但是这次是对左子树:H E B F 进行分割。 分割结果是: 左子树:H E  右子树  B F

    直到不能再分割,递归会返回去到第一次使用 A 分割出来的 右子树 里继续分割

    上述整个过程都是递归,所以结合代码和用纸笔画一次会更好理解


    本代码十分精悍易懂,但是缺点就是在递归中开设了全局变量,这样就使得在理解当前状态的时候出现不清楚的情况,因此还是应该像前一个代码一样,在递归中尽量不使用全局变量,即使是需要用到这样的变量也应该如以上代码一样每次都将新的状态传递过来,以确保在每次递归中都能明确当前的各种状态

    在本代码中其实可以不再开辟新的数组last()数组,直接将其输出就行

    另外:之所以感觉这个代码递归中的全局变量不好是因为在理解本代码时,自己突然想这样输出“先输出右,在输出根,最后输出左”,但是在交换

      make_tree(i, k - 1);
      make_tree(k + 1, j);

    这两行代码的时候,却怎么也输不出来,加上条件也还是不好使,估计原因就出现在n这个全局变量上,因为是全局变量,所以状态也不明确,因此在修改时也是跟着感觉走,最后还是以失败告终,用了那么长时间竟然没有修改对,啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊

    有知道怎么修改的么,求解。。。。。

    #include<iostream>
    #include<cstring>
    using namespace std;
    char pre[27],mid[27];
    int n = -1;
    int count=0 ;
    char last[27];
    void make_tree(int i, int j)
    {
      int k;
      if(i > j)
          return;
      n++;
      for(k = i; k <= j; k++)
      {
        if(pre[n] == mid[k])
          break;
      }
      make_tree(i, k - 1);
      make_tree(k + 1, j);
      last[count++]=mid[k];
     // printf("%c", mid[k]);
    }
    
    int main(void)
    {   int len;
        while(scanf("%s%s", pre, mid) == 2) //当输入的是俩个字符串
        {
            len=strlen(pre);
            make_tree(0, len - 1);
            last[count]='';
            for(int i=0;i<len;i++)
                cout<<last[i];
            printf("
    ");
            n = -1;
            count=0;
        }
        return 0;
    
    }






    以下代码如果想实现以上目的就可以直接修改(将两个recover直接交换),并且输出结果是正确的,主要原因是他没有全局变量,恨死全局变量了........

    #include<iostream>
    #include <cstring>
    //#include<assert.h>
    using namespace std;
    char preord[30],midord[30];
    void recover(int preleft,int preright,int midleft,int midright)//分别是两种遍历的前后界限 
    {
        int root,leftsize,rightsize;
        if(preleft>preright)
            return ;                                    //条件不满足时,结束函数   
        for(root=midleft;root<=midright;root++)
            if(preord[preleft]==midord[root])
                break;
        leftsize =root-midleft;
        rightsize=midright-root;
        recover(preleft+1,preleft+leftsize,midleft,root-1);
        recover(preleft+leftsize+1,preright,root+1,midright);
        cout<<midord[root];                                 //后序遍历  
    }
    int main()
    {
        while(cin>>preord>>midord){
        int n=strlen(preord);
        recover(0,n-1,0,n-1);
        cout<<endl;
        }
        return 0;
    }


  • 相关阅读:
    关于Redis的介绍,使用以及集群
    关于RESTful详解
    关于Nginx——反向代理
    关于RabbitMQ
    关于Nginx——负载均衡
    关于Nginx——配置虚拟主机
    StringBuffer是字符串缓冲区
    数组的查找操作
    获取任意年的二月有多少天
    快速排序算法
  • 原文地址:https://www.cnblogs.com/zswbky/p/5432104.html
Copyright © 2011-2022 走看看