ImageVerifierCode 换一换
格式:DOCX , 页数:14 ,大小:16.82KB ,
资源ID:8648939      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/8648939.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(二叉树的遍历.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

二叉树的遍历.docx

1、二叉树的遍历设计4 二叉树的遍历设计一个程序演示在二叉树上进行三种遍历的过程。一、需求分析(1)从键盘上输入二叉树的每一个结点,演示二叉树T的建立过程。(2)演示各种遍历的遍历过程。二、概要设计1.二叉树以及栈的存储表示#define stack_init_size 100#define stackincreament 10#define maxlength 10#define overflow -2typedef char telemtype;typedef struct bitnode telemtype data; struct bitnode *lchild,*rchild;bitno

2、de,*bitree;/定义树的类型typedef bitree selemtype;typedef struct selemtype *base; selemtype *top; int stacksize;Sqstack;/定义栈的类型2.栈的基本函数声明void initstack(Sqstack &s); /初始化栈void push(Sqstack &s,selemtype e); /进栈bool gettop(Sqstack &s,selemtype &e); /提取栈顶元素void pop(Sqstack &s,selemtype &e); /出栈bool stackempty(

3、Sqstack s); /判断栈是否非空3树创建和遍历函数声明void createbitree(bitree &bt); /树的创建void preorderbitree(bitree bt); /先序非递归遍历void inorderbitree(bitree bt); /中序非递归遍历void postorderbitree(bitree bt); /后序非递归遍历void levelorderbitree(bitree bt); /层序遍历三、详细设计1. 栈的初始化函数名:initstack(Sqstack &s)参数:(传入)Sqstack &s顺序栈作用:对栈进行初始化算法:vo

4、id initstack(Sqstack &s) /初始化栈 s.base=(selemtype*)malloc (stack_init_size*sizeof(selemtype); if(!s.base) cout=s.stacksize) s.base=(selemtype*)realloc(s.base, (s.stacksize+stackincreament)*sizeof(selemtype); if(!s.base) exit(overflow); s.stacksize+=stackincreament; *s.top+=e;3.提取栈顶元素函数名:gettop(Sqstac

5、k &s,selemtype &e)参数:Sqstack &s,selemtype &e作用:提取栈顶元素算法:bool gettop(Sqstack &s,selemtype &e) /提取栈顶元素 if(s.top=s.base) return false; e=*(s.top-1); return true;4.出栈函数名:pop(Sqstack &s,selemtype &e)参数:Sqstack &s,selemtype &e作用:删除栈顶元素,用e返回其值算法:void pop(Sqstack &s,selemtype &e) /出栈 if(s.top=s.base) coutda

6、ta=ch; createbitree(bt-lchild); createbitree(bt-rchild); 7.先序遍历二叉树函数名:preorderbitree(bitree bt)参数:bitree bt作用:先序遍历一棵二叉树算法:void preorderbitree(bitree bt) /先序非递归遍历 bitree p;p=bt; Sqstack s; initstack(s); push(s,bt); while(!stackempty(s) while(gettop(s,p)&p) coutdatalchild); pop(s,p); if(!stackempty(s)

7、 pop(s,p); push(s,p-rchild); 8.中序遍历二叉树函数名:inorderbitree(bitree bt)参数:bitree bt作用:中序非递归遍历二叉树算法:void inorderbitree(bitree bt) /中序非递归遍历 Sqstack s;initstack(s); bitree p; p=bt; while(p|!stackempty(s) if(p) push(s,p); p=p-lchild; else pop(s,p); coutdatarchild; 9.后序遍历二叉树函数名:postorderbitree(bitree bt)参数:bi

8、tree bt作用:后序非递归遍历二叉树算法:void postorderbitree(bitree bt) /后序非递归遍历 Sqstack s;initstack(s); bitree p,q;p=bt; push(s,p); while(p|!stackempty(s) while(gettop(s,p)&p) push(s,p-lchild); pop(s,p); if(!stackempty(s) gettop(s,p); if(p-rchild=NULL|q=p-rchild) coutdatarchild); 10.层序遍历二叉树函数名:levelorderbitree(bitr

9、ee bt)参数:bitree bt作用:层序遍历二叉树算法: void levelorderbitree(bitree bt) /层序遍历 bitree queuemaxlength; int front=0,rear=0; bitree p; if(bt) queuerear=bt; rear=(rear+1)%maxlength; while(front!=rear) p=queuefront; front=(front+1)%maxlength; coutdatalchild) queuerear=p-lchild; rear=(rear+1)%maxlength; if(p-rchi

10、ld)queuerear=p-rchild; rear=(rear+1)%maxlength; 四、运行结果及分析附:主要源代码#include#include#define stack_init_size 100#define stackincreament 10#define maxlength 10#define overflow -2typedef char telemtype;typedef struct bitnode telemtype data; struct bitnode *lchild,*rchild;bitnode,*bitree;/定义树的类型typedef bitr

11、ee selemtype;typedef struct selemtype *base; selemtype *top; int stacksize;Sqstack;/定义栈的类型/栈的基本函数声明void initstack(Sqstack &s);void push(Sqstack &s,selemtype e);bool gettop(Sqstack &s,selemtype &e);void pop(Sqstack &s,selemtype &e);bool stackempty(Sqstack s);/树创建和遍历函数声明void createbitree(bitree &bt);v

12、oid preorderbitree(bitree bt);void inorderbitree(bitree bt);void postorderbitree(bitree bt);void levelorderbitree(bitree bt);void main() bitree bt; cout请按先序的输入树的各个结点(空格表示空树):endl; createbitree(bt); cout递归算法略!endl; cout该树的先序非递归遍历为:; preorderbitree(bt); coutendl; cout该树的中序非递归遍历为:; inorderbitree(bt); c

13、outendl; cout该树的后序非递归遍历为:; postorderbitree(bt); coutendl; cout该树的层序遍历为: ; levelorderbitree(bt); coutendl;void initstack(Sqstack &s) /初始化栈 s.base=(selemtype*)malloc (stack_init_size*sizeof(selemtype); if(!s.base) cout=s.stacksize) s.base=(selemtype*)realloc(s.base, (s.stacksize+stackincreament)*sizeo

14、f(selemtype); if(!s.base) exit(overflow); s.stacksize+=stackincreament; *s.top+=e;bool gettop(Sqstack &s,selemtype &e) /提取栈顶元素 if(s.top=s.base) return false; e=*(s.top-1); return true;void pop(Sqstack &s,selemtype &e) /出栈 if(s.top=s.base) coutdata=ch; createbitree(bt-lchild); createbitree(bt-rchild)

15、; /*/树的非递归遍历函数*void preorderbitree(bitree bt) /先序非递归遍历 bitree p;p=bt; Sqstack s; initstack(s); push(s,bt); while(!stackempty(s) while(gettop(s,p)&p) coutdatalchild); pop(s,p); if(!stackempty(s) pop(s,p); push(s,p-rchild); void inorderbitree(bitree bt) /中序非递归遍历 Sqstack s;initstack(s); bitree p; p=bt;

16、 while(p|!stackempty(s) if(p) push(s,p); p=p-lchild; else pop(s,p); coutdatarchild; void postorderbitree(bitree bt) /后序非递归遍历 Sqstack s;initstack(s); bitree p,q;p=bt; push(s,p); while(p|!stackempty(s) while(gettop(s,p)&p) push(s,p-lchild); pop(s,p); if(!stackempty(s) gettop(s,p); if(p-rchild=NULL|q=p

17、-rchild) coutdatarchild); void levelorderbitree(bitree bt) /层序遍历*bitree queuemaxlength; int front=0,rear=0; bitree p; if(bt) queuerear=bt; rear=(rear+1)%maxlength; while(front!=rear) p=queuefront; front=(front+1)%maxlength; coutdatalchild) queuerear=p-lchild; rear=(rear+1)%maxlength; if(p-rchild)queuerear=p-rchild; rear=(rear+1)%maxlength; /*

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1