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

下載本文檔

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

文檔簡介

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

2、ree t)功能:前序遍歷二叉樹void InOerderTraverse(BinTree t)功能:中序遍歷二叉樹ivoid LevelOrder(BinTree t)功能:層次遍歷二叉樹int BinTreeLeaf(BinTNode *t)功能:計算二叉樹的葉子個數三、源程序#include<stdio.h>#include<stdlib.h>int f=0;typedef struct BinTNodechar data;struct BinTNode *lch,*rch;專業資料整理WORD格式BinTNode,*BinTree;/ 創立二叉樹void Cre

3、atBinTree(BinTree &t)char ch;fflush(stdin);printf(" 請輸入二叉樹元素,用 * 表示空節點 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

4、 t)if (t)printf("%c ",t->data);PreOrderTraverse(t->lch);PreOrderTraverse(t->rch);/ 中序遍歷二叉樹void InOerderTraverse(BinTree t)BinTree s100;int top=0;int a=1;dowhile(t)stop=t;top+;t=t->lch;專業資料整理WORD格式if(top=0)a=0;elsetop-;t=stop;printf("%c",t->data);t=t->rch;while(a

5、);/ 層次遍歷二叉樹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)queue+rear = p->lch;if(p->rch)queue+rear = p->rch;/ 計算二叉樹的葉子個數int BinTreeLeaf(BinTNode *t)if(t)if

6、(t->lch=NULL&&t->rch=NULL)f=+1;elseBinTreeLeaf(t->lch);BinTreeLeaf(t->rch);專業資料整理WORD格式return f;void main()struct BinTNode *t=NULL;int k;do專業資料整理WORD格式printf(" -n ");printf("0.退出 n");printf(" 1.創立二叉樹 n");printf(" 2.先序遍歷 n");printf(" 3.

7、中序遍歷 n");printf(" 4.層次遍歷 n");printf(" 5.計算葉子個數 n");printf("-n ");專業資料整理WORD格式printf("please select the numscanf("%d",&k);switch(k):");專業資料整理WORD格式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(" 該二叉樹葉子節點個數為 :%dn",f); break;case 0:br

溫馨提示

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

評論

0/150

提交評論