![二叉樹應用之二叉樹基本算法的實現(xiàn)_第1頁](http://file4.renrendoc.com/view/6ee06c6a40e541fbd628d3d7c8dfbac3/6ee06c6a40e541fbd628d3d7c8dfbac31.gif)
![二叉樹應用之二叉樹基本算法的實現(xiàn)_第2頁](http://file4.renrendoc.com/view/6ee06c6a40e541fbd628d3d7c8dfbac3/6ee06c6a40e541fbd628d3d7c8dfbac32.gif)
![二叉樹應用之二叉樹基本算法的實現(xiàn)_第3頁](http://file4.renrendoc.com/view/6ee06c6a40e541fbd628d3d7c8dfbac3/6ee06c6a40e541fbd628d3d7c8dfbac33.gif)
![二叉樹應用之二叉樹基本算法的實現(xiàn)_第4頁](http://file4.renrendoc.com/view/6ee06c6a40e541fbd628d3d7c8dfbac3/6ee06c6a40e541fbd628d3d7c8dfbac34.gif)
![二叉樹應用之二叉樹基本算法的實現(xiàn)_第5頁](http://file4.renrendoc.com/view/6ee06c6a40e541fbd628d3d7c8dfbac3/6ee06c6a40e541fbd628d3d7c8dfbac35.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領
文檔簡介
1、二叉樹應用二叉樹基本算法的實現(xiàn)【功能要求】實現(xiàn) Create 方法,要求鍵盤輸入二叉樹結(jié)點序列,創(chuàng)建一棵二叉樹(提示:前 序遞歸)實現(xiàn) SwapTree 方法,以根結(jié)點為參數(shù),交換每個結(jié)點的左子樹和右子樹(提示: 前序遞歸)增加 InorderTree 方法,采用非遞歸方法實現(xiàn)二叉樹的中序遍歷 你可以選擇:對 BinaryTree 模板進行功能擴充; 自己定義并實現(xiàn)二叉樹類 要求鍵盤輸入二叉樹結(jié)點序列 結(jié)點序列可以是前序,也可以是層次 空結(jié)點以#表示【代碼實現(xiàn)】/ 二叉樹 01.cpp : Defines the entry point for the console application.
2、/#include stdafx.h#include using namespace std;templateclass BinaryTreeNode;templateclass BinaryTreepublic:BinaryTree()root=0;BinaryTree(const BinaryTree &Tree ) copy(Tree.root,root);BinaryTree();bool IsEmpty()constreturn (root)?false:true);void Creat();bool Root (T&x)const;void MakeTree(const T&ele
3、ment,BinaryTree&left,BinaryTree&right);void BreakTree( T&element,BinaryTree&left,BinaryTree&right);void PreOrder(void (*Visit)(BinaryTreeNode*u)PreOrder(Visit,root);void InOrder(void (*Visit)(BinaryTreeNode*u)InOrder(Visit,root);void PostOrder(void (*Visit)(BinaryTreeNode*u)PostOrder(Visit,root);voi
4、d LevelOrder(void(*Visit)(BinaryTreeNode*u);void PreOutput()PreOrder(Output,root);coutendl;void InOutput()InOrder(Output,root);coutendl;void Postput()PostOrder(Output,root);coutendl;void LevelOutPut()LevelOrder(Output);coutendl;void Delete()PostOrder(Free,root);root=0;int Height()constreturn Height(
5、root);int Size()constreturn Size(root);BinaryTreeNode*iCreat();bool equal(BinaryTree&Tree)return compare(root,Tree.root);void swap() swap(root);int leave()return leave(root);int noleave()return noleave(root);private:BinaryTreeNode*root;void PreOrder(void(*Visit)(BinaryTreeNode*u),BinaryTreeNode*t);v
6、oid InOrder(void(*Visit)(BinaryTreeNode*u),BinaryTreeNode*t);void PostOrder(void(*Visit)(BinaryTreeNode*u),BinaryTreeNode*t); static void Output(BinaryTreeNode* t) coutdata ;static void Free(BinaryTreeNode*t)delete t;int Height(BinaryTreeNode*t)const;int Size(BinaryTreeNode*t)const;bool compare(Bina
7、ryTreeNode*t1,BinaryTreeNode*t2);void copy(const BinaryTreeNode*t1,BinaryTreeNode*&t2);void swap(BinaryTreeNode*t);int leave(BinaryTreeNode*t);int noleave(BinaryTreeNode*t); template class LinkedQueue; template class Node friend LinkedQueue; private:T data;Node*link; template class LinkedQueue publi
8、c:LinkedQueue() front=rear=0; LinkedQueue(); bool IsEmpty()constreturn (front)?false:true);bool IsFull()const;T First()const;T Last()const;LinkedQueue&Add(const T &x);LinkedQueue&Delete(T&x); private:Node*front; Node*rear; template bool BinaryTree:Root(T&x)const if(root)x=root-data;return true;else
9、return false;templatevoid BinaryTree:MakeTree(const T&element ,BinaryTree&left,BinaryTree&right)root=new BinaryTreeNode(element,left.root,right.root); left.root=right.root=0;templatevoidBinaryTree:BreakTree(T&element ,BinaryTree&left,BinaryTree&right) element=root-data;left.root=root-LeftChild; righ
10、t.root=root-RightChild;delete root;root=0;templatevoidBinaryTree:PreOrder(void(*Visit)(BinaryTreeNode*u),BinaryTreeNode *t)if(t)Visit(t);PreOrder(Visit,t-LeftChild);PreOrder(Visit,t-RightChild);templatevoidBinaryTree:InOrder(void(*Visit)(BinaryTreeNode*u),BinaryTreeNode* t)if(t)InOrder(Visit,t-LeftC
11、hild);Visit(t);InOrder(Visit,t-RightChild);templatevoidBinaryTree:PostOrder(void(*Visit)(BinaryTreeNode*u),BinaryTreeNode*t)if(t)PostOrder(Visit,t-LeftChild);PostOrder(Visit,t-RightChild);Visit(t);templatevoid BinaryTree:LevelOrder(void(*Visit)(BinaryTreeNode*u)LinkedQueueBinaryTreeNode* Q;BinaryTre
12、eNode*t;t=root;while(t)Visit(t);if(t-LeftChild) Q.Add(t-LeftChild);if(t-RightChild) Q.Add(t-RightChild);if(Q.IsEmpty() return ;else Q.Delete(t);templateint BinaryTree:Height(BinaryTreeNode*t)constif(!t) return 0;int hl=Height(t-LeftChild);int hr=Height(t-RightChild);if(hlhr) return +hl;else return +
13、hr; template int BinaryTree:Size(BinaryTreeNode*t)const if(!t) return 0;int sl=Size(t-LeftChild);int sr=Size(t-RightChild);return (1+sl+sr); template BinaryTreeNode*BinaryTree:iCreat( )T ch;cinch;BinaryTreeNode * root;if(ch=#)root=NULL;elseroot=new BinaryTreeNode; root-data=ch;root-LeftChild=this-iC
14、reat(); root-RightChild=this-iCreat();return root;templatevoid BinaryTree:Creat()this-root = iCreat(); template bool BinaryTree:compare(BinaryTreeNode *t1, BinaryTreeNode *t2) if (t1=NULL&t2=NULL) return true;else if (t1!=NULL&t2=NULL) return false;else if (t1=NULL&t2!=NULL) return false;else if (t1
15、-data!=t2-data) return false;else return(compare(t1-leftChild,t2-leftChild)&compare(t1-RightChild,t2-RightChi ld); template void BinaryTree:copy(const BinaryTreeNode*t1,BinaryTreeNode*&t2) if(t1)t2=new BinaryTreeNode; t2-data=t1-data;copy(t1-LeftChild,t2-LeftChild); copy(t1-RightChild,t2-RightChild)
16、; template void BinaryTree:swap(BinaryTreeNode *t)BinaryTreeNode *temp;if(!t) return;elsetemp=t-LeftChild; t-LeftChild=t-RightChild; t-RightChild=temp;swap(t-leftChild); swap(t-RightChild); template int BinaryTree:leave(BinaryTreeNode*t)if(!t) return 0;if(t-LeftChild=0&t-RightChild=0)return 1;int le
17、afl=leave(t-LeftChild);int leafr=leave(t-RightChild);return leafl+leafr; template int BinaryTree:noleave(BinaryTreeNode *t) if(!t) return 0;if(!t-LeftChild&!t-RightChild)return 0;int leafl=noleave(t-LeftChild);int leafr=noleave(t-RightChild);return leafl+leafr+1; template class BinaryTree; template
18、class BinaryTreeNodefriend BinaryTree;public:BinaryTreeNode()LeftChild=RightChild=0;BinaryTreeNode(const T&e)data=e;LeftChild=RightChild=0;BinaryTreeNode(const T&e,BinaryTreeNode *l,BinaryTreeNode *r) data=e;LeftChild=l;RightChild=r;private:T data; BinaryTreeNode*LeftChild,*RightChild;template LinkedQueue:LinkedQueue()Node*next;while(front) next=front-link; delete front; front=next; template LinkedQueue&LinkedQueue:Add(const T &x) Node*p=new Node; p-data=x;p-link=0;if (front) rear-link=p;else front=p;
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- Pt-IV-M13-生命科學試劑-MCE-4429
- Frutinone-A-生命科學試劑-MCE-8513
- 2-Carbamimidoylsulfanyl-acetic-acid-hydrochloride-生命科學試劑-MCE-6335
- 二零二五年度茶葉品牌授權(quán)合作協(xié)議
- 2025年度籃球俱樂部賽事安全預案與責任承擔協(xié)議
- 二零二五年度中式餐廳合伙人合作協(xié)議
- 2025年度游艇碼頭租賃與船舶租賃稅務籌劃合同
- 二零二五年度表格合同管理系統(tǒng)在線培訓及售后服務協(xié)議
- 施工現(xiàn)場施工防化學事故威脅制度
- 科技創(chuàng)新在小學生課余生活中的重要性
- 北京四合院介紹課件
- 頁眉和頁腳基本知識課件
- 《國有企業(yè)采購操作規(guī)范》【2023修訂版】
- 土法吊裝施工方案
- BLM戰(zhàn)略規(guī)劃培訓與實戰(zhàn)
- GB/T 16475-2023變形鋁及鋁合金產(chǎn)品狀態(tài)代號
- 鎖骨遠端骨折伴肩鎖關(guān)節(jié)脫位的治療
- 教育心理學智慧樹知到答案章節(jié)測試2023年浙江師范大學
- 理論力學-運動學課件
- 計算機輔助工藝設計課件
- 汽車銷售流程與技巧培訓課件
評論
0/150
提交評論