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

    主站蜘蛛池模板: 19禁啪啪无遮挡免费网站| 亚洲免费二区三区| 美女被羞羞网站免费下载| 免费人成网站在线播放| 亚洲不卡视频在线观看| 亚洲AV日韩综合一区尤物| 亚洲av日韩专区在线观看| 免费在线看黄网站| 乱爱性全过程免费视频| 日批日出水久久亚洲精品tv| 国产亚洲午夜高清国产拍精品| 免费一区二区三区在线视频| 亚洲精品国产日韩无码AV永久免费网| 美女羞羞喷液视频免费| 亚洲精品视频免费| 三年片免费高清版 | 100000免费啪啪18免进| 亚洲一区中文字幕| 夫妻免费无码V看片| 含羞草国产亚洲精品岁国产精品| 91香焦国产线观看看免费| 国产免费人成视频在线观看| 国产亚洲精品美女久久久久| 国产成人亚洲综合| 亚洲中文字幕久久精品无码VA| 成人免费a级毛片无码网站入口| 亚洲日韩一中文字暮| 免费人成无码大片在线观看| 爱丫爱丫影院在线观看免费| 亚洲成aⅴ人片在线影院八| 久久久久久免费一区二区三区| 久久精品国产亚洲av影院| 深夜福利在线视频免费| 亚洲va国产va天堂va久久| 亚洲免费在线视频播放| 国产精品亚洲精品久久精品| 国产成人综合亚洲亚洲国产第一页 | 久久亚洲国产精品一区二区| 国产电影午夜成年免费视频| 亚洲中文字幕AV每天更新| 久久亚洲色一区二区三区|