二叉樹的遍歷算法_第1頁
二叉樹的遍歷算法_第2頁
二叉樹的遍歷算法_第3頁
已閱讀5頁,還剩2頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)

文檔簡介

1、實驗三二叉樹遍歷算法一、實驗?zāi)康? 進一步理解掌握二叉樹二叉鏈表存儲結(jié)構(gòu)。2.掌握二叉樹遍歷的遞歸與非遞歸算法。二、實驗要求1. 認(rèn)真閱讀和掌握(先序、中序、后序和層次)遍歷的遞歸與非遞歸 算法2. 上機調(diào)試(先序、中序、后序和層次)遍歷的遞歸與非遞歸 算法。3. 保存和打印出程序的運行結(jié)果,并結(jié)合程序進行分析。4. 上機后,認(rèn)真整理源程序及其注釋,完成實驗報告(包括源程序、實驗 結(jié)果、算法分析、心得體會等)。三、實驗容先序、中序、后序遍歷的遞歸與非遞歸算法和層次遍歷的算法實現(xiàn)程序:#i nclude "stdio.h"#in elude "stdlib.h&qu

2、ot;#i nclude "con io.h"#defi ne maxsize 100typedef int datatype;typedef struct bno dedatatype data;struct bnode *lchild,*rchild;bno de,*btree;typedef struct bnode *node;int flag;DataType;typedef structDataType datamaxsize;int top;SeqStack,*PSeqStack;typedef struct btree datamaxsize;int fro

3、n t,rear;SeqQueue,*PSeqQueue;typedef structbtree datamaxsize;int top;SeqStack1,*PSeqStack1;/建立一個二叉樹btree create()btree t;char ch; scan f("%c",&ch);if(ch=?)t=NULL;elset=(btree)malloc(sizeof(b no de); t->data=ch;t->lchild=create(); t->rchild=create(); return t;/先序遍歷/入棧操作void pus

4、h1(PSeqStack1 s,btree sq)if(s->top=maxsize-1)printf("棧滿不得入棧");elses_>top+; s_>datas_>top=sq;/出棧操作void pop1(PSeqStack1 s,btree *sq)if(s->top=-1)printf("??詹坏贸鰲?quot;);else*sq=s->datas->top; s->top-;/先序遍歷的遞歸算法void PreOrder1(btree t)if(t)prin tf("%c",t-&g

5、t;data);PreOrder1(t->lchild); PreOrder1(t->rchild);/先序遍歷的非遞歸算法void PreOrder2(btree t)PSeqStack1 s; s=(PSeqStack1)malloc(sizeof(SeqStack1); btree p=t;s->top=-1;while(p|s->top!=-1)if(p)prin tf("%c",p->data);push1(s,p);p=p->lchild;elsepop1(s,&p); p=p->rchild;/中序遍歷的遞歸算

6、法void In Order1(btree t)if(t)In Order1(t->lchild);prin tf("%c",t->data);In Order1(t->rchild);/中序遍歷的非遞歸算法void In Order2(btree t)PSeqStackl s; s=(PSeqStack1)malloc(sizeof(SeqStack1); btree p=t;s->top=-1;while(p|s->top!=-1)if(p)push1(s,p); p=p->lchild;elsepop1(s,&p);prin

7、 tf("%c",p->data);p=p->rchild;/后序遍歷的遞歸算法void PostOrder1(btree t)if(t)t=(btree)malloc(sizeof(b no de);PostOrder1(t->lchild);PostOrder1(t->rchild); prin tf("%c",t->data);/后序遍歷的非遞歸算法/入棧操作void push(PSeqStack s,DataType sq)if(s->top=maxsize-1)printf(”棧滿不得入棧”);elses_&

8、gt;top+; s_>datas_>top=sq;/出棧操作void pop(PSeqStack s,DataType *sq)if(s->top=-1) printf(" ??詹坏贸鰲?quot;);else*sq=s->datas->top; s->top-;/后序遍歷的非遞歸算法void PostOrder2(btree t)PSeqStack s;DataType sq;btree p=t;s=(PSeqStack)malloc(sizeof(SeqStack);s->top=-1;while(p|s->top!=-1)if(

9、p) s=(PSeqStack)malloc(sizeof(SeqStack); sq.flag=O;sq .no de=p;push(s,sq);p=p->lchild;elsepop(s,& sq);p=sq .no de;if(sq.flag=0)sq.flag=1;push(s,sq);p=p->rchild; elseprin tf("%c",p->data); p=NULL;/按照層次遍歷二叉樹/隊列的初始化PSeqQueue init()PSeqQueue q; q=(PSeqQueue)malloc(sizeof(SeqQueue)

10、; if(q)q->fro nt=O; q->rear=0;return q;/判斷隊空int empty(PSeqQueue q)if(q&&q_>fron t=q->rear)return 1;else return 0;/入隊void in put(PSeqQueue q,btree x)if(q->rear+1)%maxsize=q->front)prin tf("隊滿”);elseq_>rear=(q_>rear+1)%maxsize; q_>dataq_>rear=x;/出隊void output

11、(PSeqQueue q,btree *x)if(empty(q)prin tf("隊空");elseq->fron t=(q- >fron t+1)%maxsize; *x=q->dataq->fr on t;/按照層次遍歷二叉樹void LevelOrder1(btree t)PSeqQueue q;btree p=t;q=in it();in put(q,p);while(!empty(q)output(q,&p);prin tf("%c",p->data);if(p->lchild)in put(q,p

12、->lchild);if(p->rchild)in put(q,p->rchild);void mai n()btree t;t=create();printf(”此二叉樹的先序遞歸遍歷輸出為:”);PreOrder1(t);prin tf("n");printf(”此二叉樹的先序非遞歸遍歷輸出為:");PreOrder2(t);prin tf("n");printf("此二叉樹的中序遞歸遍歷輸出為:");InO rder1(t);prin tf("n");printf("此二

13、叉樹的中序遞歸遍歷輸出為:");InO rder2(t);prin tf("n");printf(”此二叉樹的后序遞歸遍歷輸出為:”);PostOrder1(t);prin tf("n");printf(”此二叉樹的后序遞歸遍歷輸出為:”);PostOrder2(t);prin tf("n");printf("此二叉樹的層次遍歷輸出為:");LevelOrderl(t);prin tf("n");程序結(jié)果: st如DegklDpXDebugWhujLMn 70420-OX征二叉初爲(wèi)羌序遞歸遍歷輸出為ABCDE 此二叉樹的先序菲遞歸遍歷輸出為:ABCDE 此二叉

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論