二叉樹子系統(tǒng)_第1頁(yè)
二叉樹子系統(tǒng)_第2頁(yè)
二叉樹子系統(tǒng)_第3頁(yè)
二叉樹子系統(tǒng)_第4頁(yè)
二叉樹子系統(tǒng)_第5頁(yè)
已閱讀5頁(yè),還剩2頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、#include<stdio.h>#include <stdlib.h>#define TREEMAX 100typedef struct BT/ 定義二叉樹結(jié)構(gòu)體char data;struct BT* lchild;struct BT* rchild;BT;BT *CreateTree();void ShowTree(BT *T);void Preorder(BT *T);void Postorder(BT *T);void Levelorder(BT *T);void Inorder(BT *T);void Leafnum(BT *T);void Nodenum(

2、BT *T);int TreeDepth(BT *T);int count=0;/ 定義計(jì)算結(jié)點(diǎn)個(gè)數(shù)的變量void main()BT *T=NULL;char ch1,ch2,a;ch1='y'while(ch1='y'|ch1='Y') printf("n");printf("ntt二叉樹子系統(tǒng)");printf("ntt*");printf("ntt*1 建二叉樹*")printf("ntt*2 凹入顯示*")printf("ntt*

3、3 先序遍歷*")printf("ntt*4 中序遍歷*")printf("ntt*5 后序遍歷*")printf("ntt*6 層次遍歷*")printf("ntt*7 求葉子數(shù)*")printf("ntt*8 求結(jié)點(diǎn)數(shù)*")printf("ntt*9 求樹深度*")printf("ntt*0 返回*")printf("ntt*");printf("ntt 請(qǐng)選擇菜單號(hào)(0-9) : ");scanf(&

4、quot;%c",&ch2);getchar();printf("n");switch(ch2) case '1':printf("ntt 請(qǐng)按先序序列輸入二叉樹的結(jié)點(diǎn): n");printf("ntt 說明:輸入結(jié)點(diǎn)('0'表示后繼結(jié)點(diǎn)為空)后按回車 .n");printf("ntt 請(qǐng)輸入根結(jié)點(diǎn):");T=CreateTree();printf("ntt 二叉樹成功建立!n");break;case'2':ShowTree(T

5、);break;case'3':printf("ntt 該二叉樹的先序遍歷序列為: ");Preorder(T);break;case'4':printf("ntt 該二叉樹的中序遍歷序列為: ");Inorder(T);break;case'5':printf("ntt 該二叉樹的后序遍歷序列為: ");Postorder(T);break;case'6':printf("ntt 該二叉樹的層次遍歷序列為: ");Levelorder(T);brea

6、k;case'7':count=0;Leafnum(T);printf("ntt 該二叉樹有%d 個(gè)葉子。 n",count);break;case'8':count=0;Nodenum(T);printf("ntt 該二叉樹總共有%d 個(gè)結(jié)點(diǎn)。 n",count);break;case'9':printf("ntt 該樹的深度是: %d",TreeDepth(T);break;case'0':ch1='n'break;default:printf(&qu

7、ot;ntt* 請(qǐng)注意:輸入有誤 !*");if(ch2!='0') printf("nntt 按回車鍵繼續(xù),按任意鍵返回主菜單!n");a=getchar();if(a!='xA') getchar();ch1='n' BT *CreateTree()/ 建立二叉樹 BT *t;char x;scanf("%c",&x);getchar();if(x='0')t=NULL;else t=(BT *)malloc(sizeof(BT);t->data=x;printf

8、("ntt 請(qǐng)輸入%c 結(jié)點(diǎn)的左子結(jié)點(diǎn):",t->data)t->lchild=CreateTree();printf("ntt 請(qǐng)輸入%c 結(jié)點(diǎn)的右子結(jié)點(diǎn):",t->data)t->rchild=CreateTree();return t;void Preorder(BT *T) if(T) printf("%3c",T->data); Preorder(T->lchild); Preorder(T->rchild);void Inorder(BT *T) if(T) Inorder(T-&

9、gt;lchild);printf("%3c",T->data); Inorder(T->rchild);void Postorder(BT *T) if(T) Postorder(T->lchild);Postorder(T->rchild);printf("%3c",T->data);void Levelorder(BT *T) int i,j;BT *q100,*p;p=T;if(p!=NULL) i=1;qi=p; j=2;while(i!=j) p=qi;printf("%3c",p->d

10、ata); if(p->lchild!=NULL)/ 先序遍歷/ 中序遍歷/ 后序遍歷/ 層次遍歷 qj=p->lchild;j+; if(p->rchild!=NULL) qj=p->rchild; j+;i+;void Leafnum(BT *T)/ 求葉子數(shù) if(T) if(T->lchild=NULL&&T->rchild=NULL) count+;Leafnum(T->lchild); Leafnum(T->rchild);void Nodenum(BT *T)/ 求結(jié)點(diǎn)數(shù) if(T) count+;Nodenum(T

11、->lchild); Nodenum(T->rchild);int TreeDepth(BT *T)/ 求樹深度 int ldep,rdep;if(T=NULL) return 0;else ldep=TreeDepth(T->lchild); rdep=TreeDepth(T->rchild); if(ldep>rdep)return ldep+1; else return rdep+1;void ShowTree(BT *T)/ 凹入法顯示二叉樹 BT *stackTREEMAX,*p;int levelTREEMAX2,top,n,i,width=4;if(T!=NULL) printf("ntt 凹入表示法: ntt"); top=1;stacktop=T;leveltop0=width;while(top>0) p=stacktop;n=leveltop0;for(i=1;i<=n;i+)printf(" ");printf("%c",p->data);for(i=n+1;i<30;i+=2)printf(" ");prin

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝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ù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論