版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、實(shí)習(xí)報(bào)告一:需求分析1 基本要求a) 以回車(chē)('n')為輸入結(jié)束標(biāo)志,輸入數(shù)列L,生成一棵二叉排 序樹(shù)T;b) 對(duì)二叉排序樹(shù)T作中序遍歷,輸出結(jié)果;c) 輸入元素x,查找二叉排序樹(shù)T,若存在含x的結(jié)點(diǎn),則刪除該結(jié)點(diǎn),并作中序遍歷(執(zhí)行操作2);否則輸出信息“無(wú)x”;2 數(shù)據(jù)類型要實(shí)現(xiàn)二叉排序數(shù),必須先定義數(shù)據(jù)類型,本設(shè)計(jì)的輸入數(shù)據(jù)為整型,輸出的數(shù)據(jù)也為整型。3 題目功能詳細(xì)說(shuō)明 要生成一棵二叉排序數(shù)T,元素類型為ElemType 在二叉排序樹(shù)中查找其關(guān)鍵字等于給定值的結(jié)點(diǎn)是否存在 在二叉排序樹(shù)中插入一個(gè)新結(jié)點(diǎn),中序遍歷所建二叉排序樹(shù),將得到一個(gè)按關(guān)鍵字有序的元素序列 二叉排序樹(shù)
2、的查找,可多次查找,并輸出查找的結(jié)果4最后對(duì)輸出結(jié)構(gòu)進(jìn)行分析二概要分析1.二叉樹(shù)是另一種樹(shù)型結(jié)構(gòu),他的特點(diǎn)是每個(gè)結(jié)點(diǎn)至多只有兩棵子樹(shù),并且,二叉樹(shù)的子樹(shù)有左右之分,其次序不能任意顛倒。2.二叉樹(shù)的存儲(chǔ)結(jié)構(gòu)/-二叉樹(shù)的順序存儲(chǔ)表示-#define MAX-TREE-SIZE 100 /二叉樹(shù)的最大結(jié)點(diǎn)數(shù)Typedef TELem type sqbitreeMAX-TREE-SIZE;/0號(hào)單元存儲(chǔ)根結(jié)點(diǎn)sqBiTree bt3.在不同的存儲(chǔ)結(jié)構(gòu)中,實(shí)現(xiàn)二叉樹(shù)的操作方法也不同,如找結(jié)點(diǎn)X的雙親PARENT(T,E),在三叉鏈表中很容易實(shí)現(xiàn),而在二叉鏈表中則需從根指針出發(fā)巡查.4. if(!t) *
3、p=f;return (0); /*查找不成功*/else if(key=t->data) *p=t;return (1); /*查找成功*/ else if(key<t->data) searchBST(t->lchild,key,t,p); /*在左子樹(shù)中繼續(xù)查找*/else searchBST(t->rchild,key,t,p); /*在右子樹(shù)中繼續(xù)查找*/5calculateASL(node *t,int *s,int *j,int i) /*計(jì)算平均查找長(zhǎng)度*/ if(*t) i+; /*i記錄當(dāng)前結(jié)點(diǎn)的在當(dāng)前樹(shù)中的深度*/ *s=*s+i; /*s記
4、錄已遍歷過(guò)的點(diǎn)的深度之和*/ if(calculateASL(&(*t)->lchild,s,j,i)/*計(jì)算左子樹(shù)的ASL*/三.詳細(xì)程序#include<stdio.h># include<stdlib.h>typedef struct Tnode int data; /*輸入的數(shù)據(jù)*/ struct Tnode *lchild,*rchild; /*結(jié)點(diǎn)的左右指針,分別指向結(jié)點(diǎn)的左右孩子*/ *node,BSTnode;searchBST(node t,int key,node f,node *p) /*查找函數(shù)*/ if(!t) *p=f;retu
5、rn (0); /*查找不成功*/else if(key=t->data) *p=t;return (1); /*查找成功*/ else if(key<t->data) searchBST(t->lchild,key,t,p); /*在左子樹(shù)中繼續(xù)查找*/else searchBST(t->rchild,key,t,p); /*在右子樹(shù)中繼續(xù)查找*/insertBST(node *t,int key) /*插入函數(shù)*/ node p=NULL,s=NULL; if(!searchBST(*t,key,NULL,&p) /*查找不成功*/ s=(node)m
6、alloc(sizeof(BSTnode); s->data=key; s->lchild=s->rchild=NULL; if(!p) *t=s; /*被插結(jié)點(diǎn)*s為新的根結(jié)點(diǎn)*/ else if(key<p->data) p->lchild=s;/*被插結(jié)點(diǎn)*s為左孩子*/ else p->rchild=s; /*被插結(jié)點(diǎn)*s為右孩子*/ return (1); else return (0);/*樹(shù)中已有關(guān)鍵字一樣的結(jié)點(diǎn),不再插入*/ inorderTraverse(node *t) /*中序遍歷函數(shù)*/ if(*t) if(inorderTra
7、verse(&(*t)->lchild) /*中序遍歷根的左子樹(shù)*/ printf("%d ",(*t)->data); /*輸出根結(jié)點(diǎn)*/ if(inorderTraverse(&(*t)->rchild); /*中序遍歷根的右子樹(shù)*/ return(1) ; calculateASL(node *t,int *s,int *j,int i) /*計(jì)算平均查找長(zhǎng)度*/ if(*t) i+; /*i記錄當(dāng)前結(jié)點(diǎn)的在當(dāng)前樹(shù)中的深度*/ *s=*s+i; /*s記錄已遍歷過(guò)的點(diǎn)的深度之和*/ if(calculateASL(&(*t)-
8、>lchild,s,j,i)/*計(jì)算左子樹(shù)的ASL*/ (*j)+; /*j記錄樹(shù)中結(jié)點(diǎn)的數(shù)目*/ if(calculateASL(&(*t)->rchild,s,j,i) /*計(jì)算右子樹(shù)的ASL*/ i-; return(1); else return(1); node Delete(node t,int key) /*刪除函數(shù)*/ node p=t,q=NULL,s,f; while(p!=NULL) /*查找要?jiǎng)h除的點(diǎn)*/ if(p->data=key) break; q=p; if(p->data>key) p=p->lchild; else
9、 p=p->rchild; if(p=NULL) return t; /*查找失敗*/ if(p->lchild=NULL) /*p指向當(dāng)前要?jiǎng)h除的結(jié)點(diǎn)*/ if(q=NULL) t=p->rchild; /*q指向要?jiǎng)h結(jié)點(diǎn)的父母*/ else if(q->lchild=p) q->lchild=p->rchild; /*p為q的左孩子*/ else q->rchild=p->rchild;/*p為q的右孩子*/ free(p); else /*p的左孩子不為空*/ f=p; s=p->lchild; while(s->rchild)
10、 /*左拐后向右走到底*/ f=s; s=s->rchild; if(f=p) f->lchild=s->lchild; /*重接f的左子樹(shù)*/ else f->rchild=s->lchild; /*重接f的右子樹(shù)*/ p->data=s->data; free (s); return t;int balanceBST(node t,int *i) /*判斷是否為平衡二叉樹(shù)的函數(shù)*/ int dep1,dep2; if(!t) return(0); else dep1=balanceBST(t->lchild,i); dep2=balanceB
11、ST(t->rchild,i); if(dep1-dep2)>1|(dep1-dep2)<-1) *i=dep1-dep2;/*用i值記錄是否存在不平衡現(xiàn)象*/ if(dep1>dep2) return(dep1+1); else return(dep2+1);void main() node T=NULL; int num; int s=0,j=0,i=0; int ch=0; node p=NULL; printf("please input a list of numbers end with zero:"); do scanf("%
12、d",&num); if(!num) printf("you have finished your input!n"); else insertBST(&T,num); while(num); printf("nn-the menu of the opperation-n"); /*主程序菜單*/ printf("n 0: exit" ); printf("n 1: inorder travel the tree"); printf("n 2: the average searc
13、h length for the tree"); printf("n 3: delete"); printf("n 4: judge the balance"); while(ch=ch) printf("n choose the opperation to continue:"); scanf("%d",&ch); switch(ch) case 0: exit(0); /*0退出*/ case 1: printf(" The result of the inorder travers
14、e is:n "); inorderTraverse(&T); /*1中序遍歷*/ break; case 2: s=0;j=0;i=0; calculateASL(&T,&s,&j,i); /*2計(jì)算平均查找長(zhǎng)度*/ printf(" ASL=%d/%d",s,j); break; case 3: printf(" Please input the number you want to delete:"); scanf("%d",&num); /*3刪除某個(gè)結(jié)點(diǎn)*/ if(searc
15、hBST(T,num,NULL,&p) T=Delete(T,num); printf(" You have delete the number successfully!n "); inorderTraverse(&T); else printf(" No node %d you want to delete!",num); break; case 4: i=0; balanceBST(T,&i); /*判斷是否為平衡二插樹(shù)*/ if(i=0) printf(" OK!The tree is a balanced tr
16、ee!"); else printf(" NO!"); break; default: printf("Your input is wrong!please input again!n"); break; /*輸入無(wú)效字符*/ 四.結(jié)果輸出112 5 4 1 6 0You have finished your input!-the menu of the opperation-0: exit1: inorder travel the tree2: the average search length for the tree3:delete4:
17、judge the balanceChoose the opperation to continue:1The result of the inorder traverse is:1 4 5 6 113Choose the opperation to continue2ASL=13/5Choose the opperation to continue:3Please input the number you want to delete:6You have delete the number successfully!1 4 5 113Choose the opperation to continue:4NO!Choose the opperation to con
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝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ù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026年正常人體結(jié)構(gòu)試卷
- 花樣墻施工方案(3篇)
- 外墻填充施工方案(3篇)
- 應(yīng)急水箱施工方案(3篇)
- 水泵線路施工方案(3篇)
- 施工方案交底封面(3篇)
- 材料搬運(yùn)施工方案(3篇)
- 清淤砂石施工方案(3篇)
- 液體聚氯化鋁項(xiàng)目投資計(jì)劃書(shū)
- 煤炭洗選項(xiàng)目商業(yè)計(jì)劃書(shū)
- 化工和危險(xiǎn)化學(xué)品重大隱患考試試題(后附答案)
- 西方經(jīng)濟(jì)學(xué)考試題庫(kù)(含參考答案)
- 國(guó)企集團(tuán)公司各崗位廉潔風(fēng)險(xiǎn)點(diǎn)防控表格(廉政)范本
- 涉密人員考試試題庫(kù)(保密資格標(biāo)準(zhǔn))
- 個(gè)人防護(hù)用品培訓(xùn)課件
- 員工伙食提升方案
- 模擬電子技術(shù)基礎(chǔ)-華中科技大學(xué)中國(guó)大學(xué)mooc課后章節(jié)答案期末考試題庫(kù)2023年
- 輔助生殖技術(shù)及護(hù)理人工授精
- 把未來(lái)點(diǎn)亮歌詞打印版
- 華南理工大學(xué)模擬電子技術(shù)基礎(chǔ)試卷及答案
- GB/T 18369-2022玻璃纖維無(wú)捻粗紗
評(píng)論
0/150
提交評(píng)論