二叉树地遍历算法.doc

上传人:李司机 文档编号:1173332 上传时间:2022-07-13 格式:DOC 页数:13 大小:100.50KB
返回 下载 相关 举报
二叉树地遍历算法.doc_第1页
第1页 / 共13页
二叉树地遍历算法.doc_第2页
第2页 / 共13页
二叉树地遍历算法.doc_第3页
第3页 / 共13页
二叉树地遍历算法.doc_第4页
第4页 / 共13页
二叉树地遍历算法.doc_第5页
第5页 / 共13页
点击查看更多>>
资源描述

《二叉树地遍历算法.doc》由会员分享,可在线阅读,更多相关《二叉树地遍历算法.doc(13页珍藏版)》请在三一办公上搜索。

1、实验三 二叉树遍历算法一、 实验目的1 进一步理解掌握二叉树二叉链表存储结构。2 掌握二叉树遍历的递归与非递归算法。二、 实验要求1 认真阅读和掌握先序、中序、后序和层次遍历的递归与非递归算法。2 上机调试先序、中序、后序和层次遍历的递归与非递归算法。3 保存和打印出程序的运行结果,并结合程序进展分析。4 上机后,认真整理源程序与其注释,完成实验报告包括源程序、实验结果、算法分析、心得体会等。三、 实验内容先序、中序、后序遍历的递归与非递归算法和层次遍历的算法实现程序:#include stdio.h#include stdlib.h#include conio.h#define maxsiz

2、e 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;SeqQueue,*PSeqQueue;typedef structbtree da

3、tamaxsize;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(PSeqStack1 s,btree sq)if(s-top=maxsize-1)printf(栈满不得入栈);elses-top+;s-datas-top=sq;/

4、出栈操作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);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|

5、s-top!=-1)if(p)printf(%c,p-data);push1(s,p);p=p-lchild;elsepop1(s,&p);p=p-rchild;/中序遍历的递归算法void InOrder1(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

6、)push1(s,p);p=p-lchild;elsepop1(s,&p);printf(%c,p-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-datas-

7、top=sq;/出栈操作void pop(PSeqStack s,DataType *sq)if(s-top=-1)printf(栈空不得出栈);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)malloc(sizeof(SeqStack);sq.flag=0;sq.node=p;push

8、(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=0;return q;/判断队空int empty(PSeqQueue q)if(q&q-front=q-rear)return 1;else return

9、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)printf(队空);elseq-front=(q-front+1)%maxsize;*x=q-dataq-front;/按照层次遍历二叉树void LevelOrder1(btree t)PSeqQueue q;btree p=t;q=init();input(

10、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-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(此二叉树的后序递归遍历输出为:);PostOrder2(t);printf(n);printf(此二叉树的层次遍历输出为:);LevelOrder1(t);printf(n);程序结果:五:实验心得、体会:熟练地掌握递归算法的核心内容,能够较为熟练地使用递归算法的思想,了解二叉树的各种遍历的特点,了解各种遍历算法的递归算法和非递归算法。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号