版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、實習(xí)報告一:需求分析1 基本要求a) 以回車('n')為輸入結(jié)束標志,輸入數(shù)列L,生成一棵二叉排 序樹T;b) 對二叉排序樹T作中序遍歷,輸出結(jié)果;c) 輸入元素x,查找二叉排序樹T,若存在含x的結(jié)點,則刪除該結(jié)點,并作中序遍歷(執(zhí)行操作2);否則輸出信息“無x”;2 數(shù)據(jù)類型要實現(xiàn)二叉排序數(shù),必須先定義數(shù)據(jù)類型,本設(shè)計的輸入數(shù)據(jù)為整型,輸出的數(shù)據(jù)也為整型。3 題目功能詳細說明 要生成一棵二叉排序數(shù)T,元素類型為ElemType 在二叉排序樹中查找其關(guān)鍵字等于給定值的結(jié)點是否存在 在二叉排序樹中插入一個新結(jié)點,中序遍歷所建二叉排序樹,將得到一個按關(guān)鍵字有序的元素序列 二叉排序樹
2、的查找,可多次查找,并輸出查找的結(jié)果4最后對輸出結(jié)構(gòu)進行分析二概要分析1.二叉樹是另一種樹型結(jié)構(gòu),他的特點是每個結(jié)點至多只有兩棵子樹,并且,二叉樹的子樹有左右之分,其次序不能任意顛倒。2.二叉樹的存儲結(jié)構(gòu)/-二叉樹的順序存儲表示-#define MAX-TREE-SIZE 100 /二叉樹的最大結(jié)點數(shù)Typedef TELem type sqbitreeMAX-TREE-SIZE;/0號單元存儲根結(jié)點sqBiTree bt3.在不同的存儲結(jié)構(gòu)中,實現(xiàn)二叉樹的操作方法也不同,如找結(jié)點X的雙親PARENT(T,E),在三叉鏈表中很容易實現(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); /*在左子樹中繼續(xù)查找*/else searchBST(t->rchild,key,t,p); /*在右子樹中繼續(xù)查找*/5calculateASL(node *t,int *s,int *j,int i) /*計算平均查找長度*/ if(*t) i+; /*i記錄當(dāng)前結(jié)點的在當(dāng)前樹中的深度*/ *s=*s+i; /*s記
4、錄已遍歷過的點的深度之和*/ if(calculateASL(&(*t)->lchild,s,j,i)/*計算左子樹的ASL*/三.詳細程序#include<stdio.h># include<stdlib.h>typedef struct Tnode int data; /*輸入的數(shù)據(jù)*/ struct Tnode *lchild,*rchild; /*結(jié)點的左右指針,分別指向結(jié)點的左右孩子*/ *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); /*在左子樹中繼續(xù)查找*/else searchBST(t->rchild,key,t,p); /*在右子樹中繼續(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é)點*s為新的根結(jié)點*/ else if(key<p->data) p->lchild=s;/*被插結(jié)點*s為左孩子*/ else p->rchild=s; /*被插結(jié)點*s為右孩子*/ return (1); else return (0);/*樹中已有關(guān)鍵字一樣的結(jié)點,不再插入*/ inorderTraverse(node *t) /*中序遍歷函數(shù)*/ if(*t) if(inorderTra
7、verse(&(*t)->lchild) /*中序遍歷根的左子樹*/ printf("%d ",(*t)->data); /*輸出根結(jié)點*/ if(inorderTraverse(&(*t)->rchild); /*中序遍歷根的右子樹*/ return(1) ; calculateASL(node *t,int *s,int *j,int i) /*計算平均查找長度*/ if(*t) i+; /*i記錄當(dāng)前結(jié)點的在當(dāng)前樹中的深度*/ *s=*s+i; /*s記錄已遍歷過的點的深度之和*/ if(calculateASL(&(*t)-
8、>lchild,s,j,i)/*計算左子樹的ASL*/ (*j)+; /*j記錄樹中結(jié)點的數(shù)目*/ if(calculateASL(&(*t)->rchild,s,j,i) /*計算右子樹的ASL*/ i-; return(1); else return(1); node Delete(node t,int key) /*刪除函數(shù)*/ node p=t,q=NULL,s,f; while(p!=NULL) /*查找要刪除的點*/ 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é)點*/ if(q=NULL) t=p->rchild; /*q指向要刪結(jié)點的父母*/ 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的左子樹*/ else f->rchild=s->lchild; /*重接f的右子樹*/ p->data=s->data; free (s); return t;int balanceBST(node t,int *i) /*判斷是否為平衡二叉樹的函數(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計算平均查找長度*/ printf(" ASL=%d/%d",s,j); break; case 3: printf(" Please input the number you want to delete:"); scanf("%d",&num); /*3刪除某個結(jié)點*/ 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); /*判斷是否為平衡二插樹*/ 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; /*輸入無效字符*/ 四.結(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. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 數(shù)學(xué)科組交流題目及答案
- 心理健康教育知識宣講
- 心理健康安全知識
- 水電站設(shè)備備品備件管理方案
- 糧庫任務(wù)分配與考核機制方案
- 標準化廠房空氣質(zhì)量監(jiān)測方案
- 溝通的基本知識
- 消防水泵選型與配置方案
- 儲備糧庫員工培訓(xùn)與管理方案
- 企業(yè)員工心理素質(zhì)提升計劃互動方案
- 法學(xué)本科畢業(yè)論文完整范文-大數(shù)據(jù)時代下電信網(wǎng)絡(luò)詐騙犯罪治理研究
- 初中物理八年級下冊第十一章《功和機械能》測試題(有答案解析)
- 立體圖形的展開與折疊-2024-2025學(xué)年人教版七年級數(shù)學(xué)上冊同步訓(xùn)練(含答案)
- 廣東省佛山市2023-2024學(xué)年高一上學(xué)期期末考試物理試題(含答案)
- DL∕T 5157-2012 電力系統(tǒng)調(diào)度通信交換網(wǎng)設(shè)計技術(shù)規(guī)程
- 【人效】人效儀表盤
- 未成年人侵害強制報告制度
- GLB-2防孤島保護裝置試驗報告
- 污水處理設(shè)備檢修規(guī)程
- 第十二章中國傳統(tǒng)倫理道德
- 醫(yī)學(xué)課件-發(fā)紺教學(xué)課件
評論
0/150
提交評論