




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、實習報告一:需求分析1 基本要求a) 以回車('n')為輸入結束標志,輸入數列L,生成一棵二叉排 序樹T;b) 對二叉排序樹T作中序遍歷,輸出結果;c) 輸入元素x,查找二叉排序樹T,若存在含x的結點,則刪除該結點,并作中序遍歷(執行操作2);否則輸出信息“無x”;2 數據類型要實現二叉排序數,必須先定義數據類型,本設計的輸入數據為整型,輸出的數據也為整型。3 題目功能詳細說明 要生成一棵二叉排序數T,元素類型為ElemType 在二叉排序樹中查找其關鍵字等于給定值的結點是否存在 在二叉排序樹中插入一個新結點,中序遍歷所建二叉排序樹,將得到一個按關鍵字有序的元素序列 二叉排序樹
2、的查找,可多次查找,并輸出查找的結果4最后對輸出結構進行分析二概要分析1.二叉樹是另一種樹型結構,他的特點是每個結點至多只有兩棵子樹,并且,二叉樹的子樹有左右之分,其次序不能任意顛倒。2.二叉樹的存儲結構/-二叉樹的順序存儲表示-#define MAX-TREE-SIZE 100 /二叉樹的最大結點數Typedef TELem type sqbitreeMAX-TREE-SIZE;/0號單元存儲根結點sqBiTree bt3.在不同的存儲結構中,實現二叉樹的操作方法也不同,如找結點X的雙親PARENT(T,E),在三叉鏈表中很容易實現,而在二叉鏈表中則需從根指針出發巡查.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); /*在左子樹中繼續查找*/else searchBST(t->rchild,key,t,p); /*在右子樹中繼續查找*/5calculateASL(node *t,int *s,int *j,int i) /*計算平均查找長度*/ if(*t) i+; /*i記錄當前結點的在當前樹中的深度*/ *s=*s+i; /*s記
4、錄已遍歷過的點的深度之和*/ if(calculateASL(&(*t)->lchild,s,j,i)/*計算左子樹的ASL*/三.詳細程序#include<stdio.h># include<stdlib.h>typedef struct Tnode int data; /*輸入的數據*/ struct Tnode *lchild,*rchild; /*結點的左右指針,分別指向結點的左右孩子*/ *node,BSTnode;searchBST(node t,int key,node f,node *p) /*查找函數*/ 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); /*在左子樹中繼續查找*/else searchBST(t->rchild,key,t,p); /*在右子樹中繼續查找*/insertBST(node *t,int key) /*插入函數*/ 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; /*被插結點*s為新的根結點*/ else if(key<p->data) p->lchild=s;/*被插結點*s為左孩子*/ else p->rchild=s; /*被插結點*s為右孩子*/ return (1); else return (0);/*樹中已有關鍵字一樣的結點,不再插入*/ inorderTraverse(node *t) /*中序遍歷函數*/ if(*t) if(inorderTra
7、verse(&(*t)->lchild) /*中序遍歷根的左子樹*/ printf("%d ",(*t)->data); /*輸出根結點*/ if(inorderTraverse(&(*t)->rchild); /*中序遍歷根的右子樹*/ return(1) ; calculateASL(node *t,int *s,int *j,int i) /*計算平均查找長度*/ if(*t) i+; /*i記錄當前結點的在當前樹中的深度*/ *s=*s+i; /*s記錄已遍歷過的點的深度之和*/ if(calculateASL(&(*t)-
8、>lchild,s,j,i)/*計算左子樹的ASL*/ (*j)+; /*j記錄樹中結點的數目*/ if(calculateASL(&(*t)->rchild,s,j,i) /*計算右子樹的ASL*/ i-; return(1); else return(1); node Delete(node t,int key) /*刪除函數*/ 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指向當前要刪除的結點*/ if(q=NULL) t=p->rchild; /*q指向要刪結點的父母*/ 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) /*判斷是否為平衡二叉樹的函數*/ 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值記錄是否存在不平衡現象*/ 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刪除某個結點*/ 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; /*輸入無效字符*/ 四.結果輸出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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- GB/T 45498.2-2025中華人民共和國社會保障卡一卡通規范第2部分:應用規范
- GB/T 45454-2025壓縮模和注射模澆注系統零件
- 課題申報書超字怎么辦
- 證券分析師的職責與技能試題及答案
- 高通過率:微生物檢驗技師試題及答案
- 項目管理中的法律合規要求試題及答案
- 微生物檢驗技師證書考試中備考的試題
- 微生物檢驗新研究成果的試題與答案
- 小班兒童安全守則教育計劃
- 創造思想的碰撞計劃
- 養殖業勞動合同樣本
- 保險公司增額終身壽主講課件
- 上海市2023-2024學年五年級下冊第1-3單元期中模擬測試數學試卷(滬教版)
- 廠房屋頂分布式光伏電站工程日常質量巡查記錄表
- 中考語文真題雙向細目表
- 老年護理中的跌倒風險評估與干預計劃
- 《小兒支氣管炎肺炎》課件
- 基于時序數據的深度學習異常檢測技術
- 第六章 內輪廓加工
- 工程力學答案
- 2023年新高考生物江蘇卷試題真題答案解析版(精校打印)
評論
0/150
提交評論