国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

SDUTOJ 2173 數(shù)據(jù)結(jié)構(gòu)實驗之求二叉樹后序遍歷和層次遍歷

2019-11-10 21:37:01
字體:
供稿:網(wǎng)友
[cpp] view plain copy #include<iostream>  #include<stdio.h>  #include<queue>  #include<string.h>  #include<stdlib.h>  using namespace std;  char s1[100],s2[100],ans[100];  typedef struct BiTNode  {      char data;      struct BiTNode *lchild,*rchild;  }BiTNode,*BiTree;  void build(BiTree &T,char *s1,char *s2,int n)//利用前序遍歷和中序遍歷恢復(fù)二叉樹  {      if(n<=0) T=NULL;      else      {          int p=strchr(s2,s1[0])-s2;          T=new BiTNode;          T->data=s1[0];          build(T->lchild,s1+1,s2,p);//遞歸創(chuàng)建左子樹          build(T->rchild,s1+p+1,s2+p+1,n-p-1);//遞歸創(chuàng)建右子樹      }  }  void build1(int n,char *s1,char *s2,char *s)//根據(jù)前序遍歷和中序遍歷求后序遍歷  {       if(n<=0) return ;       else       {           int p=strchr(s2,s1[0])-s2;           build1(p,s1+1,s2,s);           build1(n-p-1,s1+p+1,s2+p+1,s+p);           s[n-1]=s1[0];       }  }  void levelOrder(BiTree T)//bfs算法完成層次遍歷,使用隊列存儲數(shù)據(jù)。  {      BiTree p=T;      queue<BiTree>queue;      queue.push(p);      while(!queue.empty())      {          p=queue.front();          cout<<p->data;          queue.pop();          if(p->lchild!=NULL)          {              queue.push(p->lchild);          }          if(p->rchild!=NULL)          {              queue.push(p->rchild);          }      }  }  int main()  {      BiTree T;      int n,len;      cin>>n;      while(n--)      {          cin>>s1>>s2;          len=strlen(s1);          build(T,s1,s2,len);          build1(len,s1,s2,ans);          ans[len]='/0';          cout<<ans<<endl;          levelOrder(T);          cout<<endl;      }      return 0;  }  轉(zhuǎn)載地址http://blog.csdn.net/r_misaya/article/details/40396607
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 通化市| 北海市| 始兴县| 崇信县| 精河县| 黄梅县| 宾阳县| 苏尼特左旗| 乐昌市| 新疆| 新源县| 长泰县| 彭水| 土默特左旗| 勃利县| 页游| 台州市| 来凤县| 祥云县| 潞西市| 开鲁县| 时尚| 兴业县| 邢台县| 永宁县| 五大连池市| 沙田区| 肇庆市| 页游| 毕节市| 彩票| 大足县| 石林| 察雅县| 凌海市| 泊头市| 新巴尔虎左旗| 嘉兴市| 大竹县| 陵川县| 崇明县|