C++中二叉樹(shù)實(shí)現(xiàn)方法_第1頁(yè)
C++中二叉樹(shù)實(shí)現(xiàn)方法_第2頁(yè)
C++中二叉樹(shù)實(shí)現(xiàn)方法_第3頁(yè)
C++中二叉樹(shù)實(shí)現(xiàn)方法_第4頁(yè)
C++中二叉樹(shù)實(shí)現(xiàn)方法_第5頁(yè)
已閱讀5頁(yè),還剩5頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論