![實驗三二叉樹的操作及應用_第1頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/17/d0ddc1c7-6e76-4dcd-9d6c-586c768de1f5/d0ddc1c7-6e76-4dcd-9d6c-586c768de1f51.gif)
![實驗三二叉樹的操作及應用_第2頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/17/d0ddc1c7-6e76-4dcd-9d6c-586c768de1f5/d0ddc1c7-6e76-4dcd-9d6c-586c768de1f52.gif)
![實驗三二叉樹的操作及應用_第3頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/17/d0ddc1c7-6e76-4dcd-9d6c-586c768de1f5/d0ddc1c7-6e76-4dcd-9d6c-586c768de1f53.gif)
![實驗三二叉樹的操作及應用_第4頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/17/d0ddc1c7-6e76-4dcd-9d6c-586c768de1f5/d0ddc1c7-6e76-4dcd-9d6c-586c768de1f54.gif)
![實驗三二叉樹的操作及應用_第5頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/17/d0ddc1c7-6e76-4dcd-9d6c-586c768de1f5/d0ddc1c7-6e76-4dcd-9d6c-586c768de1f55.gif)
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、實驗三 二叉樹的操作及應用實驗課程名: 數(shù)據結構與算法成績:專業(yè)班級: 15計科1班 學號: 201540410109 姓名: 劉江 實驗時間: 2016.10.24-10.31 實驗地點: K4-102 指導教師: 馮珊 一、實驗目的及要求1、進一步掌握指針變量、動態(tài)變量的含義。2、掌握二叉樹的結構特性,以及各種存儲結構的特點和適用范圍。3、掌握用指針類型描述、訪問和處理二叉樹的運算。二、實驗內容任務一:完成下列程序,該程序以二叉鏈表作存儲結構,構建如圖1所示的二叉樹,并依次進行二叉樹的前序、中序、后序及層次遍歷。圖1解答:(1)源代碼:#include<stdio.h>#inc
2、lude<stdlib.h>#define OK 1#define ERROR 0typedef char DataType;/* 二叉樹節(jié)點的存儲類型 */typedef struct Node/define stricture BiTree DataType data; struct Node *lchild,*rchild;Node, *BiTree;/*按先序序列建立一棵二叉樹*/BiTree CreatBiTree(BiTree &T)DataType ch;scanf("%c",&ch);if(ch=' ') T=NU
3、LL;elseif(!(T=(Node*)malloc(sizeof(Node)printf("Overflow.n");exit(0);T->data =ch;CreatBiTree(T->lchild );CreatBiTree(T->rchild );return T;void PrintData(DataType x)printf("%c",x);void PreOrder(BiTree T, void (*Visit)( DataType item)/*前序遍歷二叉樹T,訪問操作為Visit()函數(shù)*/if(T!= NULL)
4、Visit(T->data);PreOrder(T->lchild, Visit);PreOrder(T->rchild, Visit);void InOrder(BiTree T, void (*Visit)( DataType item) /*中序t */if(T!= NULL)InOrder(T->lchild, Visit);Visit(T->data);InOrder(T->rchild, Visit); void PostOrder(BiTree T, void (*Visit)( DataType item) /*后序 */if(T!= NUL
5、L)PostOrder(T->lchild, Visit);PostOrder(T->rchild, Visit);Visit(T->data);int main()int choice;BiTree root;printf("下面建立一棵二叉樹,結點數(shù)據輸入按先序次序。n");printf("輸入數(shù)據請注意,每一個非空結點的所有孩子數(shù)據都要給出n");printf("若孩子為空,請用空格符表示。n");printf("請輸入二叉樹結點的數(shù)據,這里設定為字符:n");CreatBiTree(roo
6、t);printf("=n");printf("1:先序序列:n");printf("2:中序序列:n");printf("3:后序序列:n");printf("其它值則退出:n");printf("=n");doprintf("請輸入對應的操作碼:");scanf("%d",&choice);switch(choice)case 1:printf("n先序序列為:"); PreOrder(root,Prin
7、tData);printf("n");break;case 2:printf("n中序序列為:"); InOrder(root,PrintData);printf("n");break;case 3:printf("n后序序列為:"); PostOrder(root,PrintData);printf("n");while(choice>0&&choice<4); return 0;(2) 運行結果: (3)運行結果分析: 運用遍歷方法實現(xiàn)二叉樹的建立任務二:完成下列
8、程序,該程序以二叉鏈表作存儲結構,構建如圖2所示二叉樹,計算二叉樹深度、所有結點總數(shù)、葉子結點數(shù)、雙孩子結點個數(shù)、單孩子結點個數(shù)。圖2解答:(1)源代碼:#include<stdio.h>#include<stdlib.h>#define OK 1#define ERROR 0typedef char TElemType;/* 二叉樹節(jié)點的存儲類型 */typedef struct BiTNode/define stricture BiTree TElemType data; struct BiTNode *lchild,*rchild;BiTNode, *BiTree
9、;/*按先序序列建立一棵二叉樹*/void CreatBiTree(BiTree &T)TElemType ch;scanf("%c",&ch);if(ch=' ') T=NULL;return;elseif(!(T=(BiTNode*)malloc(sizeof(BiTNode)printf("Overflow.n");exit(0);(T)->data =ch;CreatBiTree(T->lchild );CreatBiTree(T->rchild );void PrintData(TElemTyp
10、e x)printf("%c",x);void PreOrder(BiTree T, void (*Visit)( TElemType item)/*前序遍歷二叉樹T,訪問操作為Visit()函數(shù)*/if(T!= NULL)Visit(T->data);PreOrder(T->lchild, Visit);PreOrder(T->rchild, Visit);void InOrder(BiTree T, void (*Visit)( TElemType item) /*中序t */if(T!= NULL)InOrder(T->lchild, Visi
11、t);Visit(T->data);InOrder(T->rchild, Visit); void PostOrder(BiTree T, void (*Visit)( TElemType item) /*后序 */if(T!= NULL)PostOrder(T->lchild, Visit);PostOrder(T->rchild, Visit);Visit(T->data);/以上代碼與任務一的完全相同,下面是完成任務二的代碼/注意這些函數(shù)算法的相似性int BTreeDepth(BiTree BT) int leftdep,rightdep; if (BT=
12、NULL) return(0); else leftdep=BTreeDepth(BT->lchild); rightdep=BTreeDepth(BT->rchild); if (leftdep>rightdep) return(leftdep+1); else return(rightdep+1); int NodeCount(BiTree BT) if (BT=NULL) return(0); else return(NodeCount (BT-> lchild)+ NodeCount (BT-> rchild)+1); int LeafCount(BiTr
13、ee BT) if (BT=NULL) return(0); else if (BT-> lchild =NULL && BT-> rchild =NULL) return(1); else return(LeafCount (BT-> lchild)+ LeafCount (BT-> rchild); int NotLeafCount(BiTree BT) if (BT=NULL) return(0); else if (BT-> lchild =NULL && BT-> rchild =NULL) return(0); e
14、lse return(NotLeafCount (BT-> lchild)+ NotLeafCount (BT-> rchild)+1); int OneChildCount(BiTree BT) if (BT=NULL) return(0); else if (BT-> lchild =NULL && BT-> rchild!=NULL) |(BT-> lchild!=NULL && BT-> rchild =NULL) return(OneChildCount (BT-> lchild)+ OneChildCount
15、 (BT-> rchild)+1); else return(OneChildCount (BT-> lchild)+ OneChildCount (BT-> rchild); int TwoChildCount(BiTree BT) if (BT=NULL) return(0); else if (BT-> lchild =NULL | BT-> rchild =NULL) return(TwoChildCount (BT-> lchild)+ TwoChildCount (BT-> rchild); else if (BT-> lchild!
16、=NULL && BT-> rchild!=NULL) return(TwoChildCount (BT-> lchild)+ TwoChildCount (BT-> rchild)+1);/主函數(shù)在任務一的基礎上進行適當?shù)脑鎏韎nt main()int choice;BiTree root;printf("下面建立一棵二叉樹,結點數(shù)據輸入按先序次序。n");printf("輸入數(shù)據請注意,每一個非空結點的所有孩子數(shù)據都要給出n");printf("若孩子為空,請用空格符表示。n");printf(&
17、quot;請輸入二叉樹結點的數(shù)據,這里設定為字符:n");CreatBiTree(root);printf("=n");printf("1:先序序列:n");printf("2:中序序列:n");printf("3:后序序列:n");printf("4:二叉樹的深(高)度:n");printf("5:二叉樹的結點數(shù):n");printf("6:二叉樹的葉子結點數(shù):n");printf("7:二叉樹的度為2的結點數(shù):n");pr
18、intf("8:二叉樹的度為1的結點數(shù):n");printf("其它值則退出:n");printf("=n");doprintf("請輸入對應的操作碼:");scanf("%d",&choice);switch(choice)case 1:printf("n先序序列為:"); PreOrder(root,PrintData);printf("n");break;case 2:printf("n中序序列為:"); InOrder(root,PrintData);
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 電動車專賣店銷售協(xié)議書
- 教育技術專業(yè)工具操作作業(yè)指導書
- 2025年貴陽貨運資格證題目答案
- 2024-2025學年三年級語文下冊第三單元12一幅名揚中外的畫作業(yè)設計新人教版
- 2024年高中歷史第一單元古代中國的政治制度易混易錯高考體驗含解析新人教版必修1
- 四年級混合運算計算題100題
- 五年級蘇教版數(shù)學下冊《質數(shù)與合數(shù)》聽評課記錄(校內大組)
- 2022-2023學年第二學期高一中職數(shù)學期末考試模擬測試題
- 粵教版道德與法治八年級下冊8.1《社會合作與公平》聽課評課記錄2
- 空壓機維修及保養(yǎng)合同范本
- 高考語文閱讀兒童視角的作用專項訓練(含答案)
- 繼承人股權轉讓協(xié)議書
- 保障性住房配套公建工程項目可研報告
- 【基于杜邦分析的雅戈爾盈利能力分析9900字】
- 服務人員隊伍穩(wěn)定措施
- 橋面系小型構件遮板等預制場施工方案
- 高二語文早讀材料積累(1-20周)課件159張
- 規(guī)劃收費標準
- 支氣管鏡護理測試題
- 大連理工大學信封紙
- 圖形創(chuàng)意(高職藝術設計)PPT完整全套教學課件
評論
0/150
提交評論