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

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

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

    so true

    心懷未來,開創未來!
    隨筆 - 160, 文章 - 0, 評論 - 40, 引用 - 0
    數據加載中……

    根據先序和中序得到后序遍歷的結果

    #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 閱讀(539) 評論(0)  編輯  收藏 所屬分類: C&C++

    主站蜘蛛池模板: 亚洲码欧美码一区二区三区| 黄视频在线观看免费| 人妻仑乱A级毛片免费看| 男男AV纯肉无码免费播放无码| 亚洲欧洲一区二区三区| 亚洲毛片免费视频| 久久毛片免费看一区二区三区| 国产成人午夜精品免费视频| 老司机亚洲精品影视www| 亚洲午夜精品久久久久久app| 午夜免费福利片观看| 国产亚洲人成网站在线观看| 精品国产亚洲AV麻豆| av无码免费一区二区三区| 亚洲一区二区三区高清| 国产婷婷成人久久Av免费高清| heyzo亚洲精品日韩| 亚洲性无码AV中文字幕| 成人免费777777| 91亚洲视频在线观看| 四虎最新永久免费视频| 亚洲成AV人片在| 中文字幕日本人妻久久久免费| 亚洲精品无码高潮喷水在线| 久久国产乱子伦免费精品| 亚洲国产精品自产在线播放| 亚洲精品国产专区91在线| 免费的黄色网页在线免费观看| 久久免费福利视频| 免费a在线观看播放| 久久久久亚洲AV片无码| 最好2018中文免费视频| 国产福利视精品永久免费| 亚洲国产精品无码久久青草| 免费A级毛片无码无遮挡内射| 亚洲专区中文字幕| 四虎在线播放免费永久视频| 成人电影在线免费观看| 亚洲欧美国产日韩av野草社区| 免费观看男人免费桶女人视频| baoyu777永久免费视频|