實現由先序、中序序列構造二叉樹的演算法,實現由後序、中序序列構造二叉樹的演算法

來源:互聯網
上載者:User

一道上機題 思想是遞迴

如果不理解原理可以看這個部落格 注釋裡的說明很詳細:http://blog.csdn.net/yunzhongguwu005/article/details/9270085

在代碼的注釋部分有測試範例

先序和中序:


#include<algorithm>#include<iostream>#include<cstdlib>#include<cstring>#include<cstdio>#include<string>#include<stack>#include<queue>#include<cmath>#include<stack>#include<list>#include<map>#include<set>#define up(i,x,y) for(i=x;i<=y;i++)#define down(i,x,y) for(i=x;i>=y;i--)#define mem(a,x) memset(a,x,sizeof(a))#define gi(x) scanf("%d",&x)#define gi2(x,y) scanf("%d%d",&x,&y)#define gll(x) scanf("%lld",&x)#define gll2(x,y) scanf("%lld%lld",&x,&y)#define gc(x) scanf("%c",&x)#define gc2(x,y) scanf("%c%c",&x,&y)using namespace std;const double eps=1e-8; typedef long long ll;const int MAXN=100005;const int mod=1000000007;const ll llinf = (ll)(1e18) + 500;const int inf=0x3f3f3f3f;struct Node{char c;Node *lson;Node *rson;};void build(Node* &t,string prestr,string instr){if(prestr.size()==0){t=NULL;return;}char rootch=prestr[0];int index=instr.find(rootch);//下標string lsoninstr=instr.substr(0,index);string rsoninstr=instr.substr(index+1);int lsonlen=lsoninstr.size();//int rsonlen=rsoninstr.size();string lsonprestr=prestr.substr(1,lsonlen);string rsonprestr=prestr.substr(lsonlen+1);t=new Node;if(t){t->c=rootch;build(t->lson, lsonprestr, lsoninstr);build(t->rson, rsonprestr, rsoninstr);}}void preorderTraverse(Node* & t)   {    if(t!=NULL)    {  cout<<t->c;  preorderTraverse(t->lson);  preorderTraverse(t->rson);    }   }  void inorderTraverse(Node* & t)   {    if(t!=NULL)    {  inorderTraverse(t->lson);cout<<t->c;  inorderTraverse(t->rson);    }   } void postorderTraverse(Node* & t)   {    if(t!=NULL)    {  postorderTraverse(t->lson); postorderTraverse(t->rson);  cout<<t->c;   }   }  //string pre = "1234567";//string in = "3241657";//後序 3 4 2 6 7 5 1 int main(){int i,j,k;string prestr,instr;//prestr="ABCDEFG";//instr="CBEDAFG";prestr="1234567";instr="3241657";Node *root;build(root,prestr,instr);preorderTraverse(root);cout<<endl;inorderTraverse(root);cout<<endl;postorderTraverse(root);cout<<endl;return 0;}



中序和後序:

#include<algorithm>#include<iostream>#include<cstdlib>#include<cstring>#include<cstdio>#include<string>#include<stack>#include<queue>#include<cmath>#include<stack>#include<list>#include<map>#include<set>#define up(i,x,y) for(i=x;i<=y;i++)#define down(i,x,y) for(i=x;i>=y;i--)#define mem(a,x) memset(a,x,sizeof(a))#define gi(x) scanf("%d",&x)#define gi2(x,y) scanf("%d%d",&x,&y)#define gll(x) scanf("%lld",&x)#define gll2(x,y) scanf("%lld%lld",&x,&y)#define gc(x) scanf("%c",&x)#define gc2(x,y) scanf("%c%c",&x,&y)using namespace std;const double eps=1e-8; typedef long long ll;const int MAXN=100005;const int mod=1000000007;const ll llinf = (ll)(1e18) + 500;const int inf=0x3f3f3f3f;struct Node{char c;Node *lson;Node *rson;};void build(Node* &t,string poststr,string instr){if(poststr.size()==0){t=NULL;return;}char rootch=poststr[poststr.size()-1];int index=instr.find(rootch);//下標string lsoninstr=instr.substr(0,index);string rsoninstr=instr.substr(index+1);int lsonlen=lsoninstr.size();int rsonlen=rsoninstr.size();string lsonpoststr=poststr.substr(0,lsonlen);string rsonpoststr=poststr.substr(lsonlen,poststr.size()-1-lsonlen);//string rsonpoststr=rsonpoststrtmp.substr(0,);t=new Node;if(t){t->c=rootch;build(t->lson, lsonpoststr, lsoninstr);build(t->rson, rsonpoststr, rsoninstr);}}void preorderTraverse(Node* & t)   {    if(t!=NULL)    {  cout<<t->c;  preorderTraverse(t->lson);  preorderTraverse(t->rson);    }  }  void inorderTraverse(Node* & t)   {    if(t!=NULL)    {  inorderTraverse(t->lson);cout<<t->c;  inorderTraverse(t->rson);    }   } void postorderTraverse(Node* & t)   {    if(t!=NULL)    {  postorderTraverse(t->lson); postorderTraverse(t->rson);  cout<<t->c;   }   } //string pre = "1234567";//string in = "3241657";//後序 3 4 2 6 7 5 1 int main(){int i,j,k;string poststr,instr;//prestr="ABCDEFG";//instr="CBEDAFG";poststr="3426751";instr="3241657";Node *root;build(root,poststr,instr);preorderTraverse(root);cout<<endl;inorderTraverse(root);cout<<endl;postorderTraverse(root);cout<<endl;return 0;}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.