數(shù)據結構實驗報告樹(3)董華偉_第1頁
數(shù)據結構實驗報告樹(3)董華偉_第2頁
數(shù)據結構實驗報告樹(3)董華偉_第3頁
數(shù)據結構實驗報告樹(3)董華偉_第4頁
數(shù)據結構實驗報告樹(3)董華偉_第5頁
已閱讀5頁,還剩2頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、 數(shù)據結構實驗報告院系名稱: 信息學院 專業(yè)班級: 計科1001班 姓 名: 董華偉 學 號: 201046830104 一.需求分析:掌握二叉樹的存儲結構以及其各種操作,包括二叉樹的建立,二叉樹的前序遍歷,中序遍歷和后序遍歷。二.概要設計存儲結構的定義如下:typedef struct BinTNodechar data;struct BinTNode *lch,*rch; BinTNode,*BinTree功能函數(shù):void CreatBinTree(BinTree &t)功能:創(chuàng)建二叉樹void PreOrderTraverse(BinTree t)功能:前序遍歷二叉樹void InOe

2、rderTraverse(BinTree t)功能:中序遍歷二叉樹ivoid LevelOrder(BinTree t)功能:層次遍歷二叉樹 int BinTreeLeaf(BinTNode *t)功能:計算二叉樹的葉子個數(shù)3、 源程序#include#includeint f=0;typedef struct BinTNodechar data;struct BinTNode *lch,*rch; BinTNode,*BinTree;/創(chuàng)建二叉樹void CreatBinTree(BinTree &t)char ch;fflush(stdin);printf(請輸入二叉樹元素,用*表示空節(jié)點

3、n);scanf(%c,&ch);if(ch=*)t=NULL;elseif(!(t=(BinTNode *)malloc(sizeof(BinTNode)exit(-2);t-data=ch;CreatBinTree(t-lch);CreatBinTree(t-rch);/前序遍歷二叉樹void PreOrderTraverse(BinTree t) if (t)printf(%c ,t-data); PreOrderTraverse(t-lch);PreOrderTraverse(t-rch); /中序遍歷二叉樹void InOerderTraverse(BinTree t)BinTree

4、 s100;int top=0; int a=1;do while(t)stop=t;top+; t=t-lch; if(top=0)a=0;elsetop-; t=stop;printf(%c,t-data);t=t-rch;while(a);/層次遍歷二叉樹void LevelOrder(BinTree t)BinTree queue100; int front = -1; int rear = 0; BinTree p = t; queuerear = p; while(front != rear) p = queue+front; printf(%cn,p-data);if(p-lch

5、) queue+rear = p-lch; if(p-rch) queue+rear = p-rch; /計算二叉樹的葉子個數(shù)int BinTreeLeaf(BinTNode *t)if(t)if(t-lch=NULL&t-rch=NULL)f=+1;elseBinTreeLeaf(t-lch);BinTreeLeaf(t-rch);return f;void main()struct BinTNode *t=NULL;int k;doprintf( -n ); printf(0.退出n);printf( 1.創(chuàng)建二叉樹n); printf( 2.先序遍歷n); printf( 3.中序遍歷n); printf( 4.層次遍歷n); printf( 5.計算葉子個數(shù)n); printf(-n );printf(please select the num:);scanf(%d,&k);switch(k)case 1:CreatBinTree(t);break;case 2:PreOrderTraverse(t);break;case 3:InOerderTraverse(t);break;case 4:LevelOrder(t); break;case 5:BinTreeLeaf(t); printf(該二叉樹葉子節(jié)點個數(shù)為:%

溫馨提示

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

評論

0/150

提交評論