




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、實驗三 二叉樹遍歷算法一、 實驗目的1 進一步理解掌握二叉樹二叉鏈表存儲結構。2 掌握二叉樹遍歷的遞歸與非遞歸算法。二、 實驗要求1 認真閱讀和掌握(先序、中序、后序和層次)遍歷的遞歸與非遞歸算法。2 上機調(diào)試(先序、中序、后序和層次)遍歷的遞歸與非遞歸算法。3 保存和打印出程序的運行結果,并結合程序進行分析。4 上機后,認真整理源程序及其注釋,完成實驗報告(包括源程序、實驗結果、算法分析、心得體會等)。三、 實驗內(nèi)容先序、中序、后序遍歷的遞歸與非遞歸算法和層次遍歷的算法實現(xiàn)程序:#include "stdio.h"#include "stdlib.h"
2、#include "conio.h"#define maxsize 100typedef int datatype;typedef struct bnodedatatype data;struct bnode *lchild,*rchild;bnode,*btree;typedef struct bnode *node;int flag;DataType;typedef structDataType datamaxsize;int top;SeqStack,*PSeqStack;typedef struct btree datamaxsize;int front,rear;
3、SeqQueue,*PSeqQueue;typedef structbtree datamaxsize;int top;SeqStack1,*PSeqStack1;/建立一個二叉樹btree create()btree t;char ch;scanf("%c",&ch);if(ch='?')t=NULL;elset=(btree)malloc(sizeof(bnode);t->data=ch;t->lchild=create();t->rchild=create();return t;/先序遍歷/入棧操作void push1(PSe
4、qStack1 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("棧空不得出棧");else*sq=s->datas->top;s->top-;/先序遍歷的遞歸算法void PreOrder1(btree t)if(t)printf("%c",t->data);P
5、reOrder1(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)printf("%c",p->data);push1(s,p);p=p->lchild;elsepop1(s,&p);p=p->rchild;/中序遍歷的遞歸算法void InOrder1
6、(btree t)if(t)InOrder1(t->lchild);printf("%c",t->data);InOrder1(t->rchild);/中序遍歷的非遞歸算法void InOrder2(btree t)PSeqStack1 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);printf("%c",p-&g
7、t;data);p=p->rchild;/后序遍歷的遞歸算法void PostOrder1(btree t)if(t)/t=(btree)malloc(sizeof(bnode);PostOrder1(t->lchild);PostOrder1(t->rchild);printf("%c",t->data);/后序遍歷的非遞歸算法/入棧操作void push(PSeqStack s,DataType sq)if(s->top=maxsize-1)printf("棧滿不得入棧");elses->top+;s->da
8、tas->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(p)/s=(PSeqStack)mall
9、oc(sizeof(SeqStack);sq.flag=0;sq.node=p;push(s,sq);p=p->lchild;elsepop(s,&sq);p=sq.node;if(sq.flag=0)sq.flag=1;push(s,sq);p=p->rchild;elseprintf("%c",p->data);p=NULL;/按照層次遍歷二叉樹/隊列的初始化PSeqQueue init()PSeqQueue q;q=(PSeqQueue)malloc(sizeof(SeqQueue);if(q)q->front=0;q->rear
10、=0;return q;/判斷隊空int empty(PSeqQueue q)if(q&&q->front=q->rear)return 1;else return 0;/入隊void input(PSeqQueue q,btree x)if(q->rear+1)%maxsize=q->front)printf("隊滿");elseq->rear=(q->rear+1)%maxsize;q->dataq->rear=x;/出隊void output(PSeqQueue q,btree *x)if(empty(q
11、)printf("隊空");elseq->front=(q->front+1)%maxsize;*x=q->dataq->front;/按照層次遍歷二叉樹void LevelOrder1(btree t)PSeqQueue q;btree p=t;q=init();input(q,p);while(!empty(q)output(q,&p);printf("%c",p->data);if(p->lchild)input(q,p->lchild);if(p->rchild)input(q,p->
12、rchild);void main()btree t;t=create();printf("此二叉樹的先序遞歸遍歷輸出為:");PreOrder1(t);printf("n");printf("此二叉樹的先序非遞歸遍歷輸出為:");PreOrder2(t);printf("n");printf("此二叉樹的中序遞歸遍歷輸出為:");InOrder1(t);printf("n");printf("此二叉樹的中序遞歸遍歷輸出為:");InOrder2(t);printf("n");printf("此二叉樹的后序遞歸遍歷輸出為:");PostOrder1(t);printf("n");printf("此二叉樹的后序遞歸遍歷輸出為:&
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 理財天賦測試題及答案
- 高德java面試題及答案
- 航運知識考試題及答案
- 環(huán)境工程風險評估與管理試題集匯編
- 未來西方政治制度與非正式政治活動試題及答案
- 學習方法多樣化2025年信息系統(tǒng)項目管理師試題及答案
- 軟件測試專家技能要求試題及答案
- 西方國家選舉制度的未來趨勢試題及答案
- 軟件設計師考試情商提升及試題答案
- 軟件測試工程師日常工作試題及答案
- 附錄B-回彈法檢測泵送混凝土測區(qū)強度換算表
- GB/T 3620.1-2016鈦及鈦合金牌號和化學成分
- GB/T 13295-2013水及燃氣用球墨鑄鐵管、管件和附件
- GB 17565-2007防盜安全門通用技術條件
- 新生放棄入學資格申請表(模板)
- 社區(qū)工作聯(lián)系函700字
- 供應商服務商管理辦法
- 天然氣管道運輸外文文獻
- 新教材 人教B版高中數(shù)學必修第四冊 第十一章 立體幾何初步 精品教學案(知識點考點匯總)
- 營銷策劃工作項目內(nèi)容明細表
- 人教版六年級畢業(yè)考試卷數(shù)學講解學習
評論
0/150
提交評論