




已閱讀5頁,還剩8頁未讀, 繼續(xù)免費閱讀
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
數(shù)據(jù)結構實驗報告實驗序號:6 實驗項目名稱:樹和二叉樹的操作學號姓名專業(yè)、班實驗地點指導教師實驗時間一、實驗目的及要求1、進一步掌握指針變量、動態(tài)變量的含義。2、掌握二叉樹的結構特征,以及各種存儲結構的特點及適用范圍。3、掌握用指針類型描述、訪問和處理二叉樹的運算。4、掌握用二叉樹前序、中序、后序、層次遍歷的方法。二、實驗設備(環(huán)境)及要求微型計算機;windows 操作系統(tǒng);Microsoft Visual Studio 6.0集成開發(fā)環(huán)境。三、實驗內容與步驟1.根據(jù)下圖中的樹回答問題-。 列出所有的葉子結點; K,L,F,M,H,I,J 列出G結點的雙親; B 列出E結點的孩子; K.L 列出I結點所有的堂兄弟; E,F,G,H 列出B結點所有的子孫; E,F,G,K,L,M 結點E的度是多少; 2 樹的度是多少; 3 結點E的層次是多少; 3 樹的深度是多少; 42根據(jù)P129的方法,將a*b-(c+d*e/f)+g)轉化為表達式二叉樹(繪圖),并寫出表達式二叉樹的前序、中序和后序遍歷順序。-*+ab+gc/f*de先序:-*ab+c/*defg中序:a*b-c+d*e/f+g后序:ab*cde*f/+g+-3. 畫出和下列二叉樹相應的森林:4. 鏈式表表示和實現(xiàn)二叉排序樹如下:#include #include typedef int TElemType;typedef struct BiTNodeTElemType data;struct BiTNode *lchild,*rchild;BiNode, *Bitree;Bitree root;/定義根結點 void insert_data(int x) /*生成二叉排序樹*/ Bitree p,q,s;s=(Bitree)malloc(sizeof(BiNode); /創(chuàng)建結點s-data=x; /結點賦值s-lchild=NULL;s-rchild=NULL;if(!root)root=s; elsep=root; while(p) /*如何接入二叉排序樹的適當位置*/q=p;if(p-data=x) /相同結點不能重復插入printf(data already exist! n);return;else if(xdata)p=p-lchild; else p=p-rchild;if(xdata)q-lchild=s;else q-rchild=s;void main() /*先生成二叉排序樹*/int i=1,x; /i記錄結點個數(shù),x存放結點值root=NULL; /*千萬別忘了賦初值給root!*/printf(請輸入數(shù)據(jù),-9999表示輸入結束n);doprintf(please input data %d:,i);i+;scanf(%d,&x); /*從鍵盤采集數(shù)據(jù),以-9999表示輸入結束*/if(x=-9999) printf(nNow output data value:n); else insert_data(x); /*調用插入數(shù)據(jù)元素的函數(shù)*/while(x!=-9999); 改寫以上程序,實現(xiàn)功能如下(任選三題):1). 編寫函數(shù)實現(xiàn)前序、中序和后序遍歷。2). 編寫函數(shù)實現(xiàn)計算葉節(jié)點個數(shù)。3). 編寫函數(shù)實現(xiàn)層序遍歷。4). 編寫函數(shù)實現(xiàn)查詢二叉樹中的某個結點(分查到和查不到兩種情況)。5)編寫函數(shù)實現(xiàn)求二叉樹的深度6). 編寫函數(shù)實現(xiàn)中序非遞歸遍歷(利用棧)以下題目為選做題:5. 如果通訊字符a,b,c,d出現(xiàn)頻度分別為7,5,2,4某同學設計了如下程序,請根據(jù)程序畫出對應的二叉樹,計算所畫出的二叉樹的帶權路徑長度;if(input=c) printf(%c,c); else if(input=d) printf(%c,d); else if(input=a) printf(%c,a); elseprintf(%c,b); WPL=7*3+5*3+4*2+2*1=46請畫出對應的赫夫曼(哈弗曼)樹;計算赫夫曼樹的帶權路徑長度;WPL=2*3+4*3+5*2+7*1=35根據(jù)赫夫曼樹,用if-else語句修改中的程序,寫出最佳判定算法。if(input=a) printf(%c,a); else if(input=b) printf(%c,b); else if(input=c) printf(%c,c); elseprintf(%c,d);四、實驗結果與數(shù)據(jù)處理詳細記錄程序在調試過程中出現(xiàn)的問題及解決方法。記錄程序執(zhí)行的結果(貼圖)。五、分析與討論對上機實踐結果進行分析,上機的心得體會。六、教師評語簽名:日期:成績附源程序清單:1、#include #include typedef int TElemType;typedef struct BiTNodeTElemType data;struct BiTNode *lchild,*rchild;BiNode, *Bitree;DLR( Bitree root ) if (root !=NULL) /非空二叉樹 printf(%d,root-data); /訪問D DLR(root-lchild); /遞歸遍歷左子樹 DLR(root-rchild); /遞歸遍歷右子樹 return(0); LDR(Bitree root) if(root !=NULL) LDR(root-lchild); printf(%d,root-data); LDR(root-rchild); return(0);LRD (Bitree root) if(root !=NULL) LRD(root-lchild); LRD(root-rchild); printf(%d,root-data); return(0);Bitree root;/定義根結點 void insert_data(int x) /*生成/樹*/ Bitree p,q,s;s=(Bitree)malloc(sizeof(BiNode); /創(chuàng)建結點s-data=x; /結點賦值s-lchild=NULL;s-rchild=NULL;if(!root)root=s; elsep=root; while(p) /*如何接入二叉排序樹的適當位置*/q=p;if(p-data=x) /相同結點不能重復插入printf(data already exist! n);return;else if(xdata)p=p-lchild; else p=p-rchild;if(xdata)q-lchild=s;else q-rchild=s;void main() /*先生成二叉排序樹*/int i=1,x; /i記錄結點個數(shù),x存放結點值root=NULL; /*千萬別忘了賦初值給root!*/printf(請輸入數(shù)據(jù),-9999表示輸入結束n);doprintf(please input data %d:,i);i+;scanf(%d,&x); /*從鍵盤采集數(shù)據(jù),以-9999表示輸入結束*/if(x=-9999) printf(nNow output data value:n); else insert_data(x); /*調用插入數(shù)據(jù)元素的函數(shù)*/while(x!=-9999); printf(nDLR:); DLR(root); printf(nLDR:); LDR(root); printf(nLRD:); LRD(root);printf(n);2、#include #include typedef int TElemType;typedef struct BiTNodeTElemType data;struct BiTNode *lchild,*rchild;BiNode, *Bitree;Bitree root;/定義根結點 int CountLeaf (Bitree root) /返回指針T所指二叉樹中所有葉子結點個數(shù)int m,n; if (!root ) return 0; if (!root-lchild & !root-rchild) return 1; else m = CountLeaf( root-lchild); n = CountLeaf( root-rchild); return (m+n); /else / CountLeafvoid insert_data(int x) /*生成/樹*/ Bitree p,q,s;s=(Bitree)malloc(sizeof(BiNode); /創(chuàng)建結點s-data=x; /結點賦值s-lchild=NULL;s-rchild=NULL;if(!root)root=s; elsep=root; while(p) /*如何接入二叉排序樹的適當位置*/q=p;if(p-data=x) /相同結點不能重復插入printf(data already exist! n);return;else if(xdata)p=p-lchild; else p=p-rchild;if(xdata)q-lchild=s;else q-rchild=s;void main() /*先生成二叉排序樹*/int i=1,x; /i記錄結點個數(shù),x存放結點值int sum;root=NULL; /*千萬別忘了賦初值給root!*/printf(請輸入數(shù)據(jù),-9999表示輸入結束n);doprintf(please input data %d:,i);i+;scanf(%d,&x); /*從鍵盤采集數(shù)據(jù),以-9999表示輸入結束*/if(x=-9999) printf(nNow output data value:n); else insert_data(x); /*調用插入數(shù)據(jù)元素的函數(shù)*/while(x!=-9999);printf( n葉節(jié)點個數(shù)=);sum=CountLeaf (root);printf(%dn,sum);3、#include #include typedef int TElemType;typedef struct BiTNodeTElemType data;struct BiTNode *lchild,*rchild;BiNode, *Bitree;Bitree root;/定義根結點 int Depth (Bitree root ) / 返回二叉樹的深度 int depthval,depthLeft,depthRight;if (root=NULL) depthval = 0; else depthLeft = Depth( root-lchild ); depthRight= Depth( root-rchild ); depthval = 1+(depthLeftdepthRight?depthLeft:depthRight); return depthval;void insert_data(int x) /*生成/樹*/ Bitree p,q,s;s=(Bitree)malloc(sizeof(BiNode); /創(chuàng)建結點s-data=x; /結點賦值s-lchild=NULL;s-rchild=NULL;if(!root)root=s; elsep=root; while(p) /*如何接入二叉排序樹的適當位置*/q=p;if(p-data=x) /相同結點不能重復插入printf(data already exist! n);return;else if(xdata)p=p-lchild; else p=p-rchild;if(xdata)q-lchild=s;else q-rchild=s;void main() /*先生成二叉排序樹*/int i=1,x; /i記錄結點個數(shù),x存放結點值int d;root=NULL; /*千萬別
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 國家思政課題申報書
- 高職省級課題申報書
- 黨建雙創(chuàng)課題申報書
- 醫(yī)學婦科課題申報書范文
- 養(yǎng)殖設備銷售合同范本
- ai生成課題申報書
- 合同范本封面彩色設計
- 課題如何寫申報書
- 信用保證保險合同范本
- 印刷租賃合同范本
- 國企治理三會一層詳解
- 部編人教版道德與法治五年級下冊全冊課時練習講解課件
- 仿真技術在車架防腐性能開發(fā)中的應用
- 初一平面直角坐標系集體備課
- 高一年級英語必修二學科導學案全冊
- 胡菊仁愛版九年級英語上教學計劃及教學進度表
- 國家職業(yè)技能標準 (2020年版) 航空發(fā)動機制造工
- 徹卻----劉立千居士文集
- 安全保證體系新
- 油氣產品標準實驗室管理規(guī)范
- 物業(yè)裝修押金協(xié)議
評論
0/150
提交評論