算法与数据结构问题(C语言版):证明通过先根序列和中序序列可以构造出2叉树 求数据结构设计:C语言由先序遍历和中序遍历序列构造一颗二叉树...
\u6570\u636e\u7ed3\u6784(C\u8bed\u8a00\u7248\uff09\u4e2d2\u53c9\u6811\u7684\u95ee\u9898#include #include #define MaxSize 100 #define MaxWidth 40 typedef char ElemType; typedef struct tnode { ElemType data; struct tnode *lchild,*rchild; } BTNode; void CreateBTree(BTNode * &bt,char *str) /*\u7531str\u521b\u5efa\u4e8c\u53c9\u94febt*/ {BTNode *St[MaxSize],*p=NULL; int top=-1,k,j=0; char ch;bt=NULL; ch=str[j]; bt=NULL; ch=str[j]; while(ch!='\0') { switch(ch) { case'(':top++;St[top]=p;k=1;break; case')':top--;break; case',':k=2;break; default:p=(BTNode *)malloc(sizeof(BTNode)); p->data=ch;p->lchild=p->rchild=NULL; if(bt==NULL) bt=p; else { switch(k) { case 1:St[top]->lchild=p;break; case 2:St[top]->rchild=p;break; } } } j++; ch=str[j]; }/*\u8bf7\u5728\u6b64\u5904\u586b\u5199\u4ee3\u7801*/ } int BTHeight(BTNode *bt) /*\u6c42\u4e8c\u53c9\u6811\u9ad8\u5ea6*/ {int lchilddep,rchilddep; if(bt==NULL) return(0); else {lchilddep=BTHeight(bt->lchild); rchilddep=BTHeight(bt->rchild); return(lchilddep>rchilddep)?(lchilddep+1):(rchilddep+1); }/*\u8bf7\u5728\u6b64\u5904\u586b\u5199\u4ee3\u7801*/ } int NodeCount(BTNode *bt) /*\u6c42\u4e8c\u53c9\u6811bt\u7684\u7ed3\u70b9\u4e2a\u6570*/ {int num1,num2; if(bt==NULL) return 0; else { num1=NodeCount(bt->lchild); num2=NodeCount(bt->rchild); return (num1+num2+1); }/*\u8bf7\u5728\u6b64\u5904\u586b\u5199\u4ee3\u7801*/ } int LeafCount(BTNode *bt) /*\u6c42\u4e8c\u53c9\u6811bt\u7684\u53f6\u5b50\u7ed3\u70b9\u4e2a\u6570*/ {int num1,num2; if(bt==NULL) return 0; else if(bt->lchild==NULL&&bt->rchild==NULL) return 1; else {num1=LeafCount(bt->lchild); num2=LeafCount(bt->rchild); return(num1+num2); }/*\u8bf7\u5728\u6b64\u5904\u586b\u5199\u4ee3\u7801*/ } void DispBTree(BTNode *bt) /*\u4ee5\u62ec\u53f7\u8868\u793a\u6cd5\u8f93\u51fa\u4e8c\u53c9\u6811*/ {if (bt!=NULL) {printf("&c",bt->data); if(bt->lchild!=NULL||bt->rchild!=NULL) {printf("("); DispBTree(bt->lchild); if(bt->rchild!=NULL) printf(","); DispBTree(bt->rchild); printf(")"); } }/*\u8bf7\u5728\u6b64\u5904\u586b\u5199\u4ee3\u7801*/ } void main() { BTNode *bt; CreateBTree(bt,"A(B(C,D(E(G),F)))"); printf("\u4e8c\u53c9\u6811bt:");DispBTree(bt);printf("\n"); printf("bt\u7684\u9ad8\u5ea6:%d\n",BTHeight(bt)); printf("bt\u7684\u7ed3\u70b9\u6570:%d\n",NodeCount(bt)); printf("bt\u7684\u53f6\u5b50\u7ed3\u70b9\u6570:%d\n",LeafCount(bt)); }
\u6ee1\u610f\u8bf7\u91c7\u7eb3
\u4f60\u5148\u5199\u4e00\u4e2a\u9012\u5f52\u7248\u672c\u7684,\u975e\u9012\u5f52\u53ef\u4ee5\u7528\u624b\u5199\u6808\u6765\u5b9e\u73b0
可以用递归来证先序的第一个结点A为根结点 在中序中找到A,此结点左strA为左树,右strB为右树 同时可以将先序除A以外的结点分为strA和strB
再来证明strA strB分别确定两棵树 方法与上相同
举个例子画画就清楚了
绛旓細鎴戠湅浜嗕竴涓嬶紝濂藉儚娌℃湁浠g爜锛屽叏绉版槸锛氥鏁版嵁缁撴瀯(C璇█鐗)銆嬮厤涔﹀厜鐩 DSDemo 鏁版嵁缁撴瀯绠楁硶婕旂ず(Windows鐗)浣 鐢 鎵 鍐 涓銆 鍔熻兘绠浠 鏈浠舵槸涓涓姩鎬佹紨绀烘暟鎹粨鏋勭畻娉曟墽琛岃繃绋嬬殑杈呭姪鏁欏杞欢, 瀹冨彲閫傚簲璇昏呭绠楁硶鐨勮緭鍏ユ暟鎹拰杩囩▼鎵ц鐨勬帶鍒舵柟寮忕殑涓嶅悓闇姹, 鍦ㄨ绠楁満鐨勫睆骞曚笂鏄剧ず绠楁硶鎵ц杩囩▼涓暟鎹...
绛旓細銆绠楁硶涓庢暟鎹粨鏋:C璇█鎻忚堪(绗2鐗)銆嬫槸涓鏈互鏁版嵁缁撴瀯涓轰富绾匡紝绠楁硶涓鸿緟鐨勬暀鏉愶紝鏃ㄥ湪鎻愪緵鍏ㄩ潰鐨勬暀瀛﹀唴瀹广傚畠鍖呭惈鍗佷釜绔犺妭锛氱华璁恒佺嚎鎬ц〃銆佸瓧绗︿覆銆佹爤涓庨槦鍒椼佷簩鍙夋爲涓庢爲銆侀泦鍚堜笌瀛楀吀銆侀珮绾у瓧鍏哥粨鏋勩佹帓搴忋佸浘浠ュ強绠楁硶鍒嗘瀽涓庤璁°傝涔︿綋绯讳弗璋紝姒傚康娓呮櫚锛屽唴瀹逛赴瀵岋紝閫夋潗绮捐壇锛屾浘琚瘎涓衡滈潰鍚21涓栫邯璇剧▼...
绛旓細娉ㄩ噴闈炲父璇︾粏锛屽笇鏈涘浣犳湁鎵甯姪銆俰nclude include defineM15 defineN15 structmark//瀹氫箟杩峰鍐呯偣鐨勫潗鏍囩被鍨 { intx;inty;};structElement//鎭嬫爤鍏冪礌锛屽樋鍢裤傘倇 intx,y;//x琛,y鍒 intd;//d涓嬩竴姝ョ殑鏂瑰悜 };typedefstructLStack//閾炬爤 { Elementelem;structLStack*next;}*PLStack;/***鏍堝嚱鏁*...
绛旓細include <stdio.h>//N鐨囧悗闂#include <stdlib.h>#include <stdio.h>#include <iostream.h>#include #include <dos.h>#include<malloc.h>typedef struct {int *elem;int length;int listsize;}Sqlist;int InitList(Sqlist & L){//鍒濆鍖朙.elem=(int *)malloc(100*sizeof(int));if(!L...
绛旓細20 void MP_Ellipse(int xc , int yc , int a, int b)21 { 22 double sqa = a * a;23 double sqb = b * b;24 25 double d = sqb + sqa * (-b + 0.25);26 int x = 0;27 int y = b;28 EllipsePlot(xc, yc, x, y);29 while( sqb * (x ...
绛旓細4]锛屽伓鏁颁负1锛屼綅缃负[2]锛屽洜姝よ鐢熸垚鐨勬暟鍒椾腑鍓4涓繀瀹氫负濂囨暟锛岃岄鐩腑娌℃湁瀵瑰ぇ灏忕殑瑕佹眰锛屽洜姝ゅ彧鐢ㄥ皢鍋舵暟涓庢渶鍚庨潰鐨勫鏁板鎹綅缃嵆鍙傚鎹㈢殑娆℃暟鍗充负鍋舵暟鐨勪釜鏁般傚ぇ姒傛濊矾濡傛锛屼笉杩囨湁寰堝鏂规硶鍙互楂樻晥鐨勫瓨鍌鍜璁$畻锛屽叿浣撳疄鐜帮紝甯屾湜浣犺兘浜茶嚜鐞㈢(涓嬶紝杩樺彲浠ュ珐鍥轰竴涓婥鎶宸с傜濂斤紝鏈闂鍙互鎺㈣銆
绛旓細鐧惧害缃戠洏 鏁版嵁缁撴瀯涓绠楁硶锛圕++璇█鐗堬級楂樻竻鍦ㄧ嚎瑙傜湅 https://pan.baidu.com/s/1zUo6r49eSozsQRR_qJwDow pwd=1234 鎻愬彇鐮侊細1234 鍐呭绠浠 銆婃暟鎹粨鏋勪笌绠楁硶鍒嗘瀽锛C璇█鎻忚堪锛堣嫳鏂囩増路鍘熶功绗2鐗堬級銆嬫槸鍥藉鏁版嵁缁撴瀯涓庣畻娉曞垎鏋愭柟闈㈢殑缁忓吀鏁欐潗锛屽師涔︽浘琚瘎涓20涓栫邯30閮ㄨ绠楁満钁椾綔涔嬩竴銆傘婃暟鎹粨鏋勪笌...
绛旓細54321锛1~5杩5涓暟杩炵画杩涙爤鍚庡啀5涓暟杩炵画鍑烘爤锛岀敤2涓惊鐜氨鍙互浜 32145锛1~3杩3涓暟杩炵画杩涙爤鍚庡啀3涓暟杩炵画鍑烘爤锛屼篃鍙互鐢2涓惊鐜紝鐒跺悗鏄4杩4鍑猴紝5杩5鍑
绛旓細绠楁硶濡備笅锛歩nt strReplace(SString S,SString T, SString V){/*鐢ㄤ覆V鏇挎崲S涓殑鎵鏈夊瓙涓睺 */ int pos,i;pos=strIndex(S,1,T); /*姹係涓瓙涓睺绗竴娆″嚭鐜扮殑浣嶇疆*/ if(pos = = 0) return(0);while(pos!=0) /*鐢ㄤ覆V鏇挎崲S涓殑鎵鏈夊瓙涓睺 */ { switch(T.len-V.len)...
绛旓細銆鏁版嵁缁撴瀯涓绠楁硶:C璇█瀹炵幇銆嬫槸涓鏈笓娉ㄤ簬鏁版嵁缁撴瀯鍩虹涓庣畻娉曞疄鐜扮殑鏁欐潗锛屼笓涓衡滄暟鎹粨鏋勨濊绋嬭璁°傛湰涔﹁灏藉湴浠嬬粛浜嗗悇绉嶆牳蹇冩暟鎹粨鏋勶紝鍖呮嫭瀹冧滑鐨勫師鐞嗐佺畻娉曟濇兂鍜孋璇█鐨勫疄鎴樺簲鐢ㄣ傛瘡涓畻娉曠珷鑺傦紝浣滆呴兘鎻愪緵浜嗚灏界殑瀹炰緥瑙f瀽锛屽寘鎷畻娉曠殑鍔熻兘璇存槑銆佺畻娉曟濇兂鍒嗘瀽锛屼互鍙婂彲浠ョ洿鎺ョ紪璇戝苟杩愯鐨勫畬鏁碈璇█绋嬪簭锛...