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

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

如何判斷兩顆二叉樹(shù)同構(gòu)

2019-11-08 20:31:12
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

#define MaxTree 10#define ElementType char#define Tree int#define Null -1struct TreeNode{ElementType Element; Tree Left;Tree Right; }T1[MaxTree],T2[MaxTree],T[MaxTree];int main(){Tree R1,R2;R1=BuildTree(T1);R2=BuildTree(T2);if(Isomorphic(R1,R2)) PRintf("Yes/n");else printf("No/n"); return 0;} Tree BuildTree(struct TreeNode T[])//如何建二叉樹(shù) { if(N){for(i=0;i<N;i++) check[i]=0;for(i=0;i<N;i++){scanf("%c %c %c/n",&T[i].Element,&cl,&cr);if(cl!='-'){T[i].Left=cl-'0';check[T[i].Left]=1;}else T[i].Left=Null;if(cr!='-'){T[i].Right=cr-'0';check[T[i].Right]=1;}else T[i].Right=Null;//對(duì)cr的對(duì)應(yīng)處理 }for(i=0;i<N;i++)if(!check[i]) break;Root=i;}return Root;}int Isomorphic(Tree R1,Tree R2)//如何判別兩二叉樹(shù)同構(gòu) {if((R1==Null)&&(R2==Null))//both emptyreturn 1;if((R1==Null)&&(R2==Null)||((R1!=Null)&&(R2!=Null)))return 0;//one of them is emptyif(T1[R1].Element!=T2[R2].Element)return 0;//roots are differentif((T1[R1].Left==Null)&&(T2[R2].Left==Null))//both have no left subtreereturn Isomorphic(T1[R1].Right,T2[R2].Right);if(((T1[R1].Left!=Null)&&(T2[R2].Left!=Null))&&  ((T1[T1[R1].Left].Element)==(T2[T2[R2].Left].Element)))  //no need to swap the left and the right  return(Isomorphic(T1[R1].Left,T2[R2].Left)&&    Isomorphic(T1[R1].Right,T2[R2].Right));else//need to swap the left and the rightreturn(Isomorphic(T1[R1].Left,T2[R2].Right)&&   Isomorphic(T1[R1].Right,T2[R2].Left));} 


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 德庆县| 柏乡县| 枣强县| 连南| 晴隆县| 海淀区| 红桥区| 深泽县| 阜康市| 鲁甸县| 江陵县| 青铜峡市| 灵川县| 赤城县| 池州市| 夏河县| 孙吴县| 鲁山县| 昌乐县| 冷水江市| 星座| 峨边| 新宾| 礼泉县| 台江县| 阳山县| 临洮县| 沂水县| 霍山县| 东丰县| 大洼县| 三穗县| 治多县| 晋城| 桓台县| 永仁县| 合江县| 达孜县| 赤壁市| 微山县| 建宁县|