




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、cpp HYPERLINK /u012967763/article/details/51898281 o view plain view plain HYPERLINK /u012967763/article/details/51898281 o copy copy#ifndef_BINARYTREE_H_#define_BINARYTREE_H_constintMaxSize=100;templatestructBTNodeTdata;BTNode*lchild;BTNode*rchild;templateclassBinaryTreepublic:BinaryTree();/構(gòu)造函數(shù)Bin
2、aryTree();/析構(gòu)函數(shù)voidPreOrder()PreOrder(r);/遞歸前序遍歷voidInOrder()InOrder(r);/遞歸中序遍歷voidPostOrder()PostOrder1(r);/遞歸后序遍歷voidPreOrder1()PreOrder1(r);/非遞歸前序遍歷voidInOrder1()InOrder1(r);/非遞歸中序遍歷voidPostOrder1()PostOrder(r);/非遞歸后序遍歷voidLevelOrder()LevelOrder(r);/層序遍歷BTNode*FindNode(Tx)FindNode(r,x);/查找結(jié)點(diǎn)intBT
3、NodeHeigth()BTNodeHeigth(r);/樹(shù)的高度intNodeCount1()NodeCount1(r);/基于前序遍歷求結(jié)點(diǎn)個(gè)數(shù)intNodeCount2()NodeCount2(r);/基于中序遍歷求結(jié)點(diǎn)個(gè)數(shù)intNodeCount3()NodeCount3(r);/基于后序遍歷求結(jié)點(diǎn)個(gè)數(shù)intNodeCount4()NodeCount4(r);/遞歸求結(jié)點(diǎn)個(gè)數(shù)voidDispLeaf()DispLeaf(r);/輸出樹(shù)的葉子結(jié)點(diǎn)voidprintRouteLength()printLeavesDepth(r,0);/輸出樹(shù)的葉子結(jié)點(diǎn)到根結(jié)點(diǎn)的路徑長(zhǎng)度boolprintA
4、ncestor(Tx)printAncestor(r,x);/輸出值為x的結(jié)點(diǎn)的祖先結(jié)點(diǎn)private:BTNode*r;BTNode*CreateTree(BTNode*t);/構(gòu)造函數(shù)調(diào)用voidDestroyTree(BTNode*t);/析構(gòu)函數(shù)調(diào)用voidPreOrder(BTNode*t);/遞歸前序遍歷調(diào)用voidInOrder(BTNode*t);/遞歸中序遍歷調(diào)用voidPostOrder(BTNode*t);/遞歸后序遍歷調(diào)用voidPreOrder1(BTNode*t);/非遞歸前序遍歷調(diào)用voidInOrder1(BTNode*t);/非遞歸中序遍歷調(diào)用voidPost
5、Order1(BTNode*t);/非遞歸后序遍歷調(diào)用voidLevelOrder(BTNode*t);/層序遍歷調(diào)用BTNode*FindNode(BTNode*t,Tx);intBTNodeHeigth(BTNode*t);intNodeCount1(BTNode*t);/前序遍歷求結(jié)點(diǎn)個(gè)數(shù)調(diào)用intNodeCount2(BTNode*t);/中序遍歷求結(jié)點(diǎn)個(gè)數(shù)調(diào)用intNodeCount3(BTNode*t);/后序遍歷求結(jié)點(diǎn)個(gè)數(shù)調(diào)用intNodeCount4(BTNode*t);/遞歸求結(jié)點(diǎn)個(gè)數(shù)調(diào)用voidDispLeaf(BTNode*t);voidprintLeavesDepth(
6、BTNode*t,intdepth);boolprintAncestor(BTNode*t,Tx);/templateBinaryTree:BinaryTree()r=CreateTree(r);/templateBinaryTree:BinaryTree()DestroyTree(r);/templatevoidBinaryTree:DestroyTree(BTNode*t)if(t!=NULL)DestroyTree(t-lchild);DestroyTree(t-rchild);deletet;/templateBTNode*BinaryTree:CreateTree(BTNode*t)
7、Tch;std:cinch;if(ch=#)t=NULL;elset=newBTNode;t-data=ch;t-lchild=CreateTree(t-lchild);t-rchild=CreateTree(t-rchild);returnt;/templatevoidBinaryTree:PreOrder(BTNode*t)if(t!=NULL)std:coutdatalchild);PreOrder(t-rchild);/templatevoidBinaryTree:InOrder(BTNode*t)if(t!=NULL)InOrder(t-lchild);std:coutdatarch
8、ild);/templatevoidBinaryTree:PostOrder(BTNode*t)if(t!=NULL)PostOrder(t-lchild);PostOrder(t-rchild);std:coutdata;/templateBTNode*BinaryTree:FindNode(BTNode*t,Tx)BTNode*p;if(t=NULL)returnNULL;elseif(t-data=x)returnt;elsep=FindNode(t-lchild,x);if(p!=NULL)returnp;returnFindNode(t-rchild,x);/templateintB
9、inaryTree:BTNodeHeigth(BTNode*t)intlchildh,rchildh;if(t=NULL)return0;elselchildh=BTNodeHeigth(t-lchild);rchildh=BTNodeHeigth(t-rchild);return(lchildhrchildh)?(lchildh+1):(rchildh+1);/templateintBinaryTree:NodeCount4(BTNode*t)if(t=NULL)return0;elsereturn(NodeCount4(t-lchild)+NodeCount4(t-rchild)+1);/
10、templateintBinaryTree:NodeCount1(BTNode*t)intm,n,k;if(t!=NULL)m=NodeCount1(t-lchild);k=1;n=NodeCount1(t-rchild);returnm+n+k;return0;/templateintBinaryTree:NodeCount2(BTNode*t)intm,n,k;if(t!=NULL)m=NodeCount2(t-lchild);n=NodeCount2(t-rchild);k=1;returnm+n+k;return0;/templateintBinaryTree:NodeCount3(B
11、TNode*t)intm,n,k;if(t!=NULL)k=1;m=NodeCount3(t-lchild);n=NodeCount3(t-rchild);returnm+n+k;return0;/templatevoidBinaryTree:DispLeaf(BTNode*t)if(t!=NULL)if(t-lchild=NULL)&(t-rchild=NULL)std:coutdatalchild);DispLeaf(t-rchild);/template/輸出路徑長(zhǎng)度voidBinaryTree:printLeavesDepth(BTNode*t,intdepth)if(t=NULL)r
12、eturn;if(t-lchild=NULL&t-rchild=NULL)std:coutdata:depthlchild,depth+1);printLeavesDepth(t-rchild,depth+1);/templateboolBinaryTree:printAncestor(BTNode*t,Tx)if(t=NULL)returnfalse;if(t-lchild!=NULL)&(t-lchild-data=x)std:coutdatarchild!=NULL)&(t-rchild-data=x)std:coutdatalchild,x)|(printAncestor(t-rchi
13、ld,x)std:coutdata;returntrue;returnfalse;/templatevoidBinaryTree:PreOrder1(BTNode*t)BTNode*stMaxSize;inttop=-1;BTNode*p;top+;sttop=t;/將根結(jié)點(diǎn)入棧while(top!=-1)/棧非空p=sttop;top-;std:coutdatarchild!=NULL)/右孩子先進(jìn)棧top+;sttop=p-rchild;if(p-lchild!=NULL)/左孩子再進(jìn)棧top+;sttop=p-lchild;/中序遍歷非遞歸算法templatevoidBinaryTree
14、:InOrder1(BTNode*t)BTNode*stMaxSize;BTNode*p;inttop=-1;p=t;while(top!=-1)|(p!=NULL)/棧不空或p不為空時(shí)while(p!=NULL)/掃描所有左結(jié)點(diǎn)并進(jìn)棧top+;sttop=p;p=p-lchild;if(top-1)/若棧不為空p=sttop;top-;std:coutdatarchild;/轉(zhuǎn)向處理右子樹(shù)/templatevoidBinaryTree:PostOrder1(BTNode*t)BTNode*stMaxSize,*p,*q;p=t;inttop=-1;boolflag;dowhile(p!=NU
15、LL)/將p結(jié)點(diǎn)及所有的左下結(jié)點(diǎn)進(jìn)棧top+;sttop=p;p=p-lchild;q=NULL;/q指向棧頂結(jié)點(diǎn)的前一個(gè)已經(jīng)訪問(wèn)的結(jié)點(diǎn)flag=true;/表示p結(jié)點(diǎn)的左子樹(shù)已經(jīng)遍歷完while(top!=-1)&(flag=true)/若p結(jié)點(diǎn)及其右子樹(shù)已訪問(wèn)或?yàn)榭誴=sttop;if(p-rchild=q)std:coutdatarchild;flag=false;while(top!=-1);/templatevoidBinaryTree:LevelOrder(BTNode*t)BTNode*quMaxSize,*p;intfront=0,rear=0;rear+;qurear=t;w
16、hile(front!=rear)/隊(duì)列不為空f(shuō)ront=(front+1)%MaxSize;p=qufront;std:coutdatalchild!=NULL)rear=(rear+1)%MaxSize;qurear=p-lchild;if(p-rchild!=NULL)rear=(rear+1)%MaxSize;qurear=p-rchild;#endif/_BINARYREE_H_源文件cpp HYPERLINK /u012967763/article/details/51898281 o view plain view plain HYPERLINK /u012967763/article/details/51898281 o copy copy#include#includeBinaryTree.husingnamespacestd;intmain()BinaryTreea;cout前序遍歷:;a.PreOrder();coutendl;
溫馨提示
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 車輛買賣賒購(gòu)合同
- 倉(cāng)房買賣合同協(xié)議書
- 的聘用合同年
- 銀行解除借款合同
- 《蘇武傳》教學(xué)設(shè)計(jì) 2024-2025學(xué)年統(tǒng)編版高中語(yǔ)文選擇性必修中冊(cè)
- Unit 1 How can I get there?PartA(教學(xué)設(shè)計(jì))-2024-2025學(xué)年人教PEP版英語(yǔ)六年級(jí)上冊(cè)
- 山東醫(yī)學(xué)高等??茖W(xué)校《物理化學(xué)B(下)》2023-2024學(xué)年第二學(xué)期期末試卷
- 山東華宇工學(xué)院《土木工程材料》2023-2024學(xué)年第二學(xué)期期末試卷
- 山西藥科職業(yè)學(xué)院《財(cái)務(wù)大數(shù)據(jù)決策》2023-2024學(xué)年第二學(xué)期期末試卷
- 內(nèi)蒙古電子信息職業(yè)技術(shù)學(xué)院《有色冶金設(shè)備》2023-2024學(xué)年第二學(xué)期期末試卷
- 2024-2025學(xué)年第二學(xué)期教學(xué)教研工作安排表 第二版
- 開(kāi)展課外讀物負(fù)面清單管理的具體實(shí)施舉措方案
- 人體的免疫系統(tǒng)課件
- 六年級(jí)下學(xué)期開(kāi)學(xué)第一課
- 體育原理課件
- 生物化學(xué)緒論
- 公路工程項(xiàng)目隱隱蔽工程管理辦法
- PCCP安裝與水壓試驗(yàn)
- 中國(guó)民間藝術(shù) ppt課件
- 景觀生態(tài)學(xué)教學(xué)大綱(共10頁(yè))
- 電子舌技術(shù)在普洱茶品質(zhì)識(shí)別中應(yīng)用的研究
評(píng)論
0/150
提交評(píng)論