<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    so true

    心懷未來(lái),開(kāi)創(chuàng)未來(lái)!
    隨筆 - 160, 文章 - 0, 評(píng)論 - 40, 引用 - 0
    數(shù)據(jù)加載中……

    根據(jù)先序和中序得到后序遍歷的結(jié)果

    #include <iostream>
    #include <assert.h>
    using namespace std;
     
    #define FIRST "ABCDEFGH"
    #define MIDDLE "CBEDAGHF"

    //function description: get the last-root order from the first-root order and middle-root order
    //the recursive form of the needed function as follows.
    char* getFirstRootOrder(const char* first,const char* middle,int len,char* last){
     if(0==len)return last+1;//return the position where the character has been filled.
     char root=*first;
     int root_pos=-1;
     while(middle[++root_pos]!=root);//get the position in the middle-order string.
     *last=root;
     char * rev=getFirstRootOrder(&first[root_pos+1],&middle[root_pos+1],len-root_pos-1,last-1);
     rev=getFirstRootOrder(first+1,middle,root_pos,rev-1);//'rev-1' is a position that any character hasn't filled.
     return rev;
    }

    //the non-recursive form of the needed function as follows.
    struct Node{
     const char* f;//the pointer of the first-order string
     const char* m;//the pointer of the middle-order string
     int len;//the length of the processing string
    };

    void getFRONR(const char* first, const char* middle,char* last){
     assert(first!=NULL && middle!=NULL);
     assert(strlen(first)==strlen(middle));

     int len=strlen(first);
     const char *pf=first;
     const char *pm=middle;

     Node *pstack=new Node[len];
     int index=0;
     int I_pos=len-1;

     do{
      while(len!=0){
       int root_pos=0;
       while(pm[root_pos++]!=*pf);
       last[I_pos--]=*pf;
       
       //push operation of the stack
       //the left-hand sub-tree is pushed into the stack
       pstack[index].len=root_pos-1;
       pstack[index].f=pf+1;
       pstack[index++].m=pm;

       //update the new right-hand sub-tree
       len=len-root_pos;
       pf=pf+root_pos;
       pm=pm+root_pos;
      }
      if(index>0){//pop operation of the stack
       len=pstack[--index].len;
       pf=pstack[index].f;
       pm=pstack[index].m;
      }
     }while(index>0 || len!=0);
     delete [] pstack;
    }

    void main(){
     int len=strlen(FIRST);
     assert(strlen(MIDDLE)==len);
     char *first=new char[len+1];
     strcpy(first,FIRST);
     char *middle=new char[len+1];
     strcpy(middle,MIDDLE);
     char *last=new char[len+1];
     memset(last,0,len+1);

     getFirstRootOrder(first,middle,len,last+len-1);
     cout<<last<<endl;

     getFRONR(first,middle,last);
     cout<<last<<endl;

     delete [] first;
     delete [] middle;
     delete [] last;
    }

    posted on 2008-10-11 18:26 so true 閱讀(535) 評(píng)論(0)  編輯  收藏 所屬分類: C&C++

    主站蜘蛛池模板: 亚洲黄色激情视频| 亚洲人成网站18禁止一区| 精品久久久久久亚洲| 亚洲国产精品无码久久久| 久草视频在线免费看| 亚洲国产精品自在线一区二区| 香蕉免费一级视频在线观看| 亚洲成网777777国产精品| 美女又黄又免费的视频| 亚洲不卡无码av中文字幕| 久久久久久久岛国免费播放| 亚洲黄色免费电影| 久久笫一福利免费导航| 亚洲欧美综合精品成人导航| 精品剧情v国产在免费线观看| 国产成人人综合亚洲欧美丁香花| 国产一区二区三区免费视频| 九九久久精品国产免费看小说| 综合亚洲伊人午夜网| 无码精品一区二区三区免费视频| 久久精品国产亚洲AV电影网| 亚洲天堂中文字幕| 亚洲人成网站观看在线播放| 丰满妇女做a级毛片免费观看| 亚洲狠狠狠一区二区三区| 国产无遮挡裸体免费视频| 一区二区在线视频免费观看| 亚洲成人激情小说| 亚洲裸男gv网站| 成人毛片免费在线观看| 一级一片免费视频播放| 亚洲人成电影网站免费| 亚洲经典在线中文字幕| 国产AV无码专区亚洲AV男同| 亚洲第一区在线观看| 免费看的一级毛片| 无码免费午夜福利片在线| 日亚毛片免费乱码不卡一区| 亚洲网址在线观看你懂的| 在线观看亚洲天天一三视| 亚洲第一成人影院|