二叉樹應用之二叉樹基本算法的實現(xiàn)_第1頁
二叉樹應用之二叉樹基本算法的實現(xiàn)_第2頁
二叉樹應用之二叉樹基本算法的實現(xiàn)_第3頁
二叉樹應用之二叉樹基本算法的實現(xiàn)_第4頁
二叉樹應用之二叉樹基本算法的實現(xiàn)_第5頁
已閱讀5頁,還剩3頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論