




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)報(bào)告 設(shè)計(jì)題目:_二叉樹(shù)的遍歷_ 姓名:_王倫_ 學(xué)號(hào):_211113206_ 專業(yè):_物聯(lián)網(wǎng)_ 院系:_計(jì)算機(jī)科學(xué)與技術(shù)學(xué)院_ 班級(jí):_1104_ 指導(dǎo)教師:_高秀梅_2013 年 3 月 22 日 摘要:本課程設(shè)計(jì)主要說(shuō)明如何在c+編程環(huán)境下實(shí)現(xiàn)二叉樹(shù)的遍歷,遍歷方式包括:二叉樹(shù)的前序非遞歸遍歷、二叉樹(shù)的后續(xù)非遞歸遍歷。同時(shí),此次課程設(shè)計(jì)還包括了求二叉樹(shù)每層節(jié)點(diǎn)數(shù)和求解任意倆點(diǎn)最近的共同祖先以及計(jì)算二叉樹(shù)深度的功能。英文摘要:abstract: this course design mainly shows how in c + + programming
2、environment to achieve binary tree traversal, traversal methods include: the preamble of binary tree non-recursive traversal, subsequent non-recursive traversal of binary tree. at the same time, the curriculum design includes for binary tree each layer node number and the solution of arbitrary two p
3、oints in recent common ancestor and calculating the function of the depth of a binary tree. 目 錄1、 問(wèn)題描述.42、 需求分析.43、 概要設(shè)計(jì).44、 數(shù)據(jù)結(jié)構(gòu)設(shè)計(jì).55、 算法設(shè)計(jì).56、 程序測(cè)試與實(shí)現(xiàn).107、 調(diào)試分析.138、 遇到的問(wèn)題與解決方法.139、 心得體會(huì).13一、問(wèn)題描述問(wèn)題描述:創(chuàng)建二叉樹(shù)并遍歷基本要求:1、 分別運(yùn)用非遞歸的方式完成對(duì)二叉樹(shù)的先序和后序遍歷2、 輸出二叉樹(shù)的高度3、 輸出每一層的結(jié)點(diǎn)數(shù)4、 查找結(jié)點(diǎn)p 和結(jié)點(diǎn)q的最近共同祖先二、需求分析1 本程序的功能包
4、括建立二叉樹(shù)、前序遍歷二叉樹(shù)、后序遍歷二叉樹(shù)、求二叉樹(shù)的深度、求每層節(jié)點(diǎn)的個(gè)數(shù)、求任意倆個(gè)節(jié)點(diǎn)的共同祖先等。2 程序運(yùn)行后顯現(xiàn)提示信息,等候用戶輸入07以進(jìn)入相應(yīng)的操作功能。3 用戶輸入數(shù)據(jù)完畢,程序?qū)⑦\(yùn)行相應(yīng)的程序并輸出運(yùn)行結(jié)束。4 測(cè)試數(shù)據(jù)應(yīng)為char型數(shù)據(jù)。三、概要設(shè)計(jì)1、建立任意一個(gè)節(jié)點(diǎn)數(shù)不超過(guò)100二叉樹(shù);2、前序遍歷節(jié)點(diǎn)1節(jié)點(diǎn)3節(jié)點(diǎn)2節(jié)點(diǎn)4節(jié)點(diǎn)53后序遍歷節(jié)點(diǎn)1節(jié)點(diǎn)3 節(jié)點(diǎn)2節(jié)點(diǎn)4 節(jié)點(diǎn)54、 數(shù)據(jù)結(jié)構(gòu)設(shè)計(jì)struct binode/節(jié)點(diǎn)聲明t data;/節(jié)點(diǎn)數(shù)據(jù)binode *lchild,*rchild;/二叉樹(shù)左右節(jié)點(diǎn);class bitree/樹(shù)類型聲明templatef
5、riend ostream & operator(ostream & os,bitree & bt);/輸出函數(shù)public:bitree();/構(gòu)造函數(shù)bitree(void);/析構(gòu)函數(shù)binode *getroot();/返回根節(jié)點(diǎn)void preorder(binode *root);/前序遍歷void postorder(binode *root);/后序遍歷int depth(binode *root);/二叉樹(shù)深度void zjzx(binode *root,t q,t w);/倆點(diǎn)最近共同祖先void levelnum(binode *root,int hig);/每層節(jié)點(diǎn)個(gè)數(shù)
6、private:binode *root;/根節(jié)點(diǎn)binode *creat();void release(binode *root);void print(ostream & os);void println(ostream &os,binode *root,int death);5、 算法設(shè)計(jì)1、二叉樹(shù)的構(gòu)造templatebinode *bitree:creat()binode *bt;t ch;cout請(qǐng)輸入一個(gè)二叉樹(shù)的節(jié)點(diǎn)(以#作為結(jié)束標(biāo)記)ch;if(ch=#) bt=null;elsebt=new binode;bt-data=ch;bt-lchild=creat();bt-rc
7、hild=creat();return bt;templatebitree:bitree()cout請(qǐng)先建立一個(gè)二叉樹(shù)endl;coutendl;coutroot=creat();2、輸出函數(shù)templatevoid bitree:println(ostream & os,binode *root,int depth)if(root!=null)println(os,root-rchild,depth+1);for(int i=0;i4*(depth-1);i+)os ;os*-datalchild,depth+1);templatevoid bitree:print(ostream & os
8、)println(os,root,1);templateostream & operator(ostream & os,bitree &bt)bt.print(os);return os;3、 前序遍歷templatevoid bitree:preorder(binode *root)binode *s100;int top=-1;cout前序遍歷結(jié)果為endl;while(top!=-1 | root)while(root)coutdatalchild;if(top!=-1)root=stop-;root=root-rchild;4、 后序遍歷templatevoid bitree:post
9、order(binode *root)binode *s100;int top=-1;cout后序遍歷結(jié)果為lchild;if(top!=-1)root=stop-;coutdatarchild;5、 求二叉樹(shù)深度templateint bitree:depth(binode *root)int high,hl,hr;if(root=null) return 0;elsehl=depth(root-lchild);hr=depth(root-rchild);high=(hlhr)?(hl+1):(hr+1);return high;6、 求每層節(jié)點(diǎn)個(gè)數(shù)templatevoid bitree:l
10、evelnum(binode *root,int hig)int y=1;binode *q100;binode *s100;s0=root;if(root=null)cout此二叉樹(shù)為空二叉樹(shù)endl;elsecout此二叉樹(shù)第一層有1個(gè)節(jié)點(diǎn)endl;while(yhig)int k=-1,m=0,j,x,i,p=0;for(i=0;ilchild;if(qk!=null) p+;k+;qk=si-rchild;if(qk!=null) p+;if(p=0) break;elsej=k;i=-1;for(x=0;xj;x+)if(qx!=null)i+;si=qx;y+;cout第“y層的節(jié)
11、點(diǎn)個(gè)數(shù)為p個(gè)endl;7、 求倆點(diǎn)最近的共同祖先templatevoid bitree:zjzx(binode *root,t q,t w)binode *s100;int top=-1;while(top!=-1 | root)while(root)s+top=root;root=root-lchild;if(top!=-1)root=stop-;binode *m100,*t;int g=0,h=0;t=root;int top=-1;while(top!=-1 | t)while(t)if(t-data=q)g=1;if(t-data=w)w=1;if(g=1 & w=1)coutdat
12、alchild;if(top!=-1)t=mtop-;t=t-rchild;root=root-rchild;初始化程序3、 算法流程圖構(gòu)建二叉樹(shù)人機(jī)交互用戶輸入最近共同祖先輸出二叉樹(shù)每層個(gè)數(shù)二叉樹(shù)深度后序遍歷前序遍歷處理并輸出結(jié)果結(jié)束六、程序測(cè)試與實(shí)現(xiàn)1、函數(shù)之間的調(diào)用關(guān)系 main caidandepthpostorderpostorderlevelnum zjzxpostorderpostorderpostorderpostorderpostorderpostorderpreorder2、主程序 int main()int xuanxiang;bitree bt;binode* root
13、=bt.getroot();caidan();while(1)cout請(qǐng)輸入需要執(zhí)行的功能的選項(xiàng)(阿拉伯?dāng)?shù)字)xuanxiang;switch(xuanxiang)case 1:coutbtendl;break; case 2:bt.preorder(root);coutendl;break; case 3:bt.postorder(root);coutendl;break; case 4:cout樹(shù)骸?的?深?度為a:阰bt.depth(root)endl;break; case 5:bt.levelnum(root,bt.depth(root); break; case 6:char q,
14、w;cout請(qǐng)?輸?入?需要癮查找的?兩?個(gè)?變?量?q;cinw;coutq和w最?近的?共2同?祖?先是?:阰 ;bt.zjzx(root,q,w);break; case 7: cout退?出?程序成功|!?endl; return 0; 3、 測(cè)試數(shù)據(jù)1 2 # 5 # 3 4 #4、 測(cè)試結(jié)果1、創(chuàng)建二叉樹(shù)2、 輸出二叉樹(shù)3、 前序遍歷4、 后序遍歷5、 求二叉樹(shù)深度6、 求任意倆點(diǎn)最近的共同祖先7、 退出程序7、 調(diào)試分析 通過(guò)前序遍歷的方式創(chuàng)建一個(gè)二叉樹(shù),通過(guò)非遞歸的方式前序遍歷和后序遍歷此二叉樹(shù),調(diào)試結(jié)果正確;用遞歸的方式求解二叉樹(shù)深度,調(diào)試結(jié)果正確;調(diào)用levelnum()求解每層的節(jié)點(diǎn)個(gè)數(shù);調(diào)用zjzx()求解任意倆點(diǎn)最近的共同祖先;八、遇到的問(wèn)題及解決辦法1、在實(shí)現(xiàn)求解二叉樹(shù)每層的節(jié)點(diǎn)個(gè)數(shù)時(shí),生成總是出現(xiàn)“=”或“”等符號(hào)無(wú)用的出錯(cuò)提示,后來(lái)經(jīng)過(guò)同學(xué)的提示才知道自己的錯(cuò)誤在將for循環(huán)中的語(yǔ)句順序?qū)戝e(cuò)了,應(yīng)該先比較再+而不是先+再比較;2、在求解最近的共同祖先時(shí),求得的結(jié)果總是是根節(jié)點(diǎn),后來(lái)對(duì)照課本重新檢查時(shí)發(fā)現(xiàn)自己在沒(méi)
溫馨提示
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 新塘鎮(zhèn)元宵花燈活動(dòng)方案
- 春季植物大賽活動(dòng)方案
- 新年禮物教育活動(dòng)方案
- 春游區(qū)域活動(dòng)方案
- 春日小班活動(dòng)方案
- 文明購(gòu)物活動(dòng)方案
- 日語(yǔ)朗誦活動(dòng)方案
- 數(shù)學(xué)函數(shù)活動(dòng)方案
- 明月漸進(jìn)活動(dòng)方案
- 硅基新材料產(chǎn)業(yè)園項(xiàng)目環(huán)境影響評(píng)估報(bào)告
- GB/T 23806-2009精細(xì)陶瓷斷裂韌性試驗(yàn)方法單邊預(yù)裂紋梁(SEPB)法
- 2022年04月四川宜賓市敘州區(qū)面向區(qū)內(nèi)外考試選調(diào)在編在職教師136人考試押題庫(kù)【1000題】含答案附帶詳解析
- FZ/T 74001-2020紡織品針織運(yùn)動(dòng)護(hù)具
- 圖解“雙均線雙交叉”期貨、股票操作系統(tǒng)課件
- 宮外孕右輸卵管妊娠腹腔鏡下盆腔粘連分解術(shù)、右輸卵管妊娠開(kāi)窗取胚術(shù)手術(shù)記錄模板
- 教科版 科學(xué)小學(xué)二年級(jí)下冊(cè)期末測(cè)試卷及參考答案(基礎(chǔ)題)
- 美軍標(biāo)電子裝備環(huán)境試驗(yàn)-mil-std-810g
- 混凝土重力壩設(shè)計(jì)說(shuō)明書(shū)
- 應(yīng)用回歸分析(第三版)何曉群_劉文卿_課后習(xí)題答案_完整版
- 道路及兩側(cè)便道保潔方案.docx
- 旅游開(kāi)發(fā)公司組織架構(gòu)
評(píng)論
0/150
提交評(píng)論