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

    主站蜘蛛池模板: 亚洲AV无码一区东京热| 狠狠亚洲狠狠欧洲2019| 亚洲国产精品午夜电影| 午夜爽爽爽男女免费观看影院| 久久精品亚洲男人的天堂| 最好2018中文免费视频| 国产jizzjizz视频全部免费| 亚洲色精品三区二区一区| 国内自产少妇自拍区免费| 亚洲av永久无码精品秋霞电影秋| 免费观看成人毛片a片2008| 在线观看亚洲AV日韩A∨| 真实乱视频国产免费观看| 亚洲AV永久无码精品放毛片 | 桃子视频在线观看高清免费视频| 亚洲国产精品嫩草影院在线观看| 久久国产免费观看精品| 久久精品亚洲一区二区三区浴池 | 波多野结衣在线免费观看| 亚洲人色大成年网站在线观看| 亚洲精品视频免费在线观看| 亚洲AV成人无码天堂| 日本久久久免费高清| 一级毛片免费全部播放| 国产亚洲精品精华液| 30岁的女人韩剧免费观看| 亚洲综合偷自成人网第页色| 精品久久洲久久久久护士免费| 亚洲JIZZJIZZ妇女| 中文字幕影片免费在线观看| 亚洲AV永久无码天堂影院| 亚洲国产香蕉人人爽成AV片久久| 永久免费AV无码网站国产| 亚洲一级毛片免费在线观看| 黄网址在线永久免费观看| 国产精品青草视频免费播放| 亚洲狠狠久久综合一区77777| 大地资源二在线观看免费高清| 老外毛片免费视频播放| 亚洲AV色香蕉一区二区| 女人张开腿给人桶免费视频 |