二叉樹(shù)的建立和遍歷C語(yǔ)言實(shí)現(xiàn)

上傳人:tia****g98 文檔編號(hào):108013560 上傳時(shí)間:2022-06-15 格式:DOC 頁(yè)數(shù):4 大?。?4.01KB
收藏 版權(quán)申訴 舉報(bào) 下載
二叉樹(shù)的建立和遍歷C語(yǔ)言實(shí)現(xiàn)_第1頁(yè)
第1頁(yè) / 共4頁(yè)
二叉樹(shù)的建立和遍歷C語(yǔ)言實(shí)現(xiàn)_第2頁(yè)
第2頁(yè) / 共4頁(yè)
二叉樹(shù)的建立和遍歷C語(yǔ)言實(shí)現(xiàn)_第3頁(yè)
第3頁(yè) / 共4頁(yè)

下載文檔到電腦,查找使用更方便

9.9 積分

下載資源

還剩頁(yè)未讀,繼續(xù)閱讀

資源描述:

《二叉樹(shù)的建立和遍歷C語(yǔ)言實(shí)現(xiàn)》由會(huì)員分享,可在線閱讀,更多相關(guān)《二叉樹(shù)的建立和遍歷C語(yǔ)言實(shí)現(xiàn)(4頁(yè)珍藏版)》請(qǐng)?jiān)谘b配圖網(wǎng)上搜索。

1、 #include #include typedef struct node{ int data; struct node *ltree; struct node *rtree; }TNode; //ADT of the Binary tree TNode * create(TNode *); int depth(TNode *); int findmax(int,int); void postorder(TNode *); void inorder(TNode *); void preorder(TNode *); i

2、nt main() { TNode *tree; printf("BinaryTree (By Recursion)\n\n"); tree=create(tree); printf("\npreorder:\n"); preorder(tree); printf("\ninorder:\n"); inorder(tree); printf("\npostorder:\n"); postorder(tree); printf("\nThe depth o the binary tree is: %d.\n",depth(tree))

3、; return 0; }//main TNode * create(TNode * p) {//創(chuàng)建一棵二叉樹(shù),返回根節(jié)點(diǎn)地址 int num; scanf("%d",&num); if(num==0){ return NULL; }//if p=(TNode *)malloc(sizeof(TNode)); p->data=num;//把數(shù)據(jù)賦值給數(shù)據(jù)域 p->ltree=create(p->ltree);//遞歸算法,創(chuàng)建左子樹(shù) p->rtree=create(p->rtree); return p; }//create vo

4、id preorder(TNode * p) { TNode* tree=p; if(tree){ printf("%5d",p->data); preorder(p->ltree); preorder(p->rtree); } }//preorder void inorder(TNode * p) { TNode* tree=p; if(tree){ inorder(p->ltree); printf("%5d",p->data); inorder(p->rtree); } }//inorder void postord

5、er(TNode * p) { TNode* tree=p; if(tree){ postorder(p->ltree); postorder(p->rtree); printf("%5d",p->data); } }//postorder int findmax(int x,int y) { return (x>y?x:y); }//findmax int depth(TNode * p) { TNode * tree=p; int dep=0; int depl,depr; if(!p){ return 0; } depl=depth(p->ltree); depr=depth(p->rtree); dep=1+findmax(depl,depr); return dep; }//depth

展開(kāi)閱讀全文
溫馨提示:
1: 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
2: 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
3.本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
5. 裝配圖網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

相關(guān)資源

更多
正為您匹配相似的精品文檔
關(guān)于我們 - 網(wǎng)站聲明 - 網(wǎng)站地圖 - 資源地圖 - 友情鏈接 - 網(wǎng)站客服 - 聯(lián)系我們

copyright@ 2023-2025  zhuangpeitu.com 裝配圖網(wǎng)版權(quán)所有   聯(lián)系電話:18123376007

備案號(hào):ICP2024067431號(hào)-1 川公網(wǎng)安備51140202000466號(hào)


本站為文檔C2C交易模式,即用戶上傳的文檔直接被用戶下載,本站只是中間服務(wù)平臺(tái),本站所有文檔下載所得的收益歸上傳人(含作者)所有。裝配圖網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)上載內(nèi)容本身不做任何修改或編輯。若文檔所含內(nèi)容侵犯了您的版權(quán)或隱私,請(qǐng)立即通知裝配圖網(wǎng),我們立即給予刪除!