




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
實(shí)驗報告題目:平衡二叉樹操作的演示一.需求分析利用平衡二叉樹實(shí)現(xiàn)一個動態(tài)查找表。實(shí)現(xiàn)動態(tài)查找表的三種基本功能:查找、插入和刪除。測試數(shù)據(jù):13,24,37,90,53詳細(xì)設(shè)計#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>#defineSUCCESS0#defineERROR-1#definemax(a,b)((a)>(b)?(a):(b))#defineNOT_FOUND1usingnamespacestd;typedefintStatus;typedefstruct_AVL{ intdata,count; inth; struct_AVL*lch,*rch; StatusInit(intd) { data=d; h=count=1; lch=rch=NULL; returnSUCCESS; }}AVL,*PAVL;intGetHeight(PAVLp){ if(p==NULL) return0; returnp->h;}voidUpdateHeight(PAVL&p){ returnERROR; RightBalance(p); } elseif(elem<p->data) { if(AVLInsert(p->lch,elem)==ERROR) returnERROR; LeftBalance(p); } else p->count++; } UpdateHeight(p); returnSUCCESS;}PAVLFindSuccession(PAVL&p){ PAVLr=p; if(p->lch!=NULL) { r=FindSuccession(p->lch); UpdateHeight(p); RightBalance(p); } else p=p->rch; returnr;}StatusAVLDelete(PAVL&p,intelem){ Statusstatus=SUCCESS; if(NULL==p) returnNOT_FOUND; if(elem>p->data) { status=AVLDelete(p->rch,elem); if(status==SUCCESS) { UpdateHeight(p); LeftBalance(p); } } elseif(elem<p->data) { status=AVLDelete(p->lch,elem); if(status==SUCCESS) { UpdateHeight(p); RightBalance(p); } } else//找到待刪除元素 { if(p->lch!=NULL&&p->rch!=NULL) { PAVLsucc=FindSuccession(p->rch);//尋找后繼結(jié)點(diǎn) PAVLtmp=p; succ->lch=p->lch; succ->rch=p->rch; p=succ;//移動后繼結(jié)點(diǎn)到當(dāng)前結(jié)點(diǎn) UpdateHeight(p); deletetmp; } else { if(p->lch!=NULL) p=p->lch; else p=p->rch; } LeftBalance(p); } returnstatus;}StatusAVLFind(PAVLT,intelem,int&depth){ depth=1; while(T!=NULL) { if(elem>T->data) T=T->rch; elseif(elem<T->data) T=T->lch; else returnSUCCESS; depth++; } returnNOT_FOUND;}intMsg(){ intr; cout<<"*****************************"<<endl; cout<<"*1.插入元素*"<<endl; cout<<"*2.刪除元素*"<<endl; cout<<"*3.查找元素*"<<endl; cout<<"*0.退出程序*"<<endl; cout<<"*****************************"<<endl; cout<<"請選擇:"; cin>>r; returnr;}voidprint_proc(PAVLT,int&now,intdepth,intm[][100]){ if(NULL==T) return; print_proc(T->rch,now,depth+1,m); m[now++][depth]=T->data; print_proc(T->lch,now,depth+1,m);}voidprint_avl(PAVLT){ intm[100][100],num=0; memset(m,0xFF,sizeof(m)); print_proc(T,num,0,m); for(inti=0;;i++) { intlev_num=0; for(intj=0;j<num;j++) if(m[i][j]!=-1) { lev_num++; printf("%3d",m[i][j]); } else printf(""); printf("\n"); if(lev_num==0) break; }}voidcmd_insert(PAVL&T){ intd; cout<<"輸入待插入的元素值:"; cin>>d; if(AVLInsert(T,d)==ERROR) cout<<"插入元素失敗!"<<endl; else { cout<<"插入元素成功!"<<endl; print_avl(T); }}voidcmd_delete(PAVL&T){ intd,status; cout<<"輸入待刪除的元素值:"; cin>>d; status=AVLDelete(T,d); switch(status) { caseSUCCESS: cout<<"刪除元素成功!"<<endl; print_avl(T); break; caseNOT_FOUND: cout<<"沒有找到輸入的元素"<<endl; break; caseERROR: cout<<"刪除元素失敗!"<<endl; break; }}voidcmd_find(PAVL&T){ intd,status,depth; cout<<"輸入待查找的元素值:"; cin>>d; status=AVLFind(T,d,depth); switch(status) { caseSUCCESS: cout<<"在深度"<<depth<<"找到該元素!"<<endl; break; caseNOT_FOUND: cout<<"沒有找到輸入的元素"<<endl; break; caseERROR: cout<<"查找元素失敗!"<<endl; break; }}intmain(){ intcmd; PAVLavl=NULL; while((cmd=Msg())) { switch(c
溫馨提示
- 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 移動頻道合作協(xié)議模板
- 公路 可行性研究報告
- 科技生活與健康飲食打造亞健康預(yù)防新模式
- 發(fā)票咨詢合同范本
- 農(nóng)機(jī)播種合同范本
- 科技行業(yè)與股票投資的風(fēng)險管理策略
- 科技產(chǎn)品推廣中的美團(tuán)外賣營銷技巧
- 印刷設(shè)備購買合同范本
- 英語品牌加盟合同范本
- oem銷售合作合同范本
- 皮膚科常見診療技術(shù)操作規(guī)范
- 成都中醫(yī)藥大學(xué)公共管理專業(yè)考研復(fù)試面試問題整理附面試技巧自我介紹
- 合肥的文化民俗
- 傷口的延續(xù)性護(hù)理
- 藥品批發(fā)公司培訓(xùn)課件模板
- 《教科版一國兩制》課件
- 急性腎挫裂傷護(hù)理查房課件
- 腦出血個案護(hù)理計劃
- 小學(xué)生電力科普小講座(課件)-小學(xué)常識科普主題班會
- 第八次課-冶金考古
- 臨床醫(yī)生如何進(jìn)行臨床科研-2
評論
0/150
提交評論