




已阅读5页,还剩11页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
数据结构课程设计数据结构课程设计报告设计题目: 二叉树的遍历 姓 名: 陈 雷 学 号: 211001047 专 业: 计算机科学与技术 院 系: 计算机科学与技术 班 级: 1002 指导教师: 吴克力 2012年 3 月 1日 摘要:本文主要说明如何实现二叉树的遍历。此次二叉树的遍历基于二叉树的二叉链表存储结构。遍历方式包括:前序遍历,中序遍历,后续遍历,层序遍历。其中前序遍历和后续遍历采用非递归算法实现。编程环境为vc+,除了遍历操作外,还增加了求二叉树的深度,总结点数,每层结点数,以及最近共同祖先(lca)问题的算法。关键字:二叉树 遍历 非递归 c+ lca abstract: this paper mainly describes how to implement binary tree traversal. the binary tree traversal is based on binary tree binary storage structure. traversal method includes: preorder traversal,inorder traversal, postorder traversal, levelorder traversal. the former preorder traversal and postorder use of non - recursive algorithm. programming environment is vc + +, in addition to traversal operation, also increased for solving the binary tree depth 、 summary points and each layer of nodes, as well as the most recent common ancestor ( lca ) algorithm.keywords: binary tree traversal non-recursive c+ lca目 录一、问题描述4问题描述:创建二叉树并遍历4基本要求:4二、需求分析4三、概要设计41创建二叉树42二叉树的非递归前序遍历示意图43二叉树的后序非递归遍历示意图5四、数据结构设计51二叉树结点数据类型定义为:52二叉树数据类型定义为:5五、算法设计61、创建二叉树62、非递归前序遍历73、非递归后序遍历74、求二叉树的高度85、求二叉树每一层的结点数96、求两节点最近共同祖先96、算法流程图10六、程序测试与实现111、函数之间的调用关系112、主程序113、测试数据134、测试结果13七、调试分析14八、遇到的问题及解决办法15九、心得体会15十、参考文献15一、问题描述问题描述:创建二叉树并遍历基本要求:1、 分别运用非递归的方式完成对二叉树的先序和后序遍历2、 输出二叉树的高度3、 输出每一层的结点数4、 查找结点p 和结点q的最近共同祖先二、需求分析1 本程序的功能包括二叉树的建立,二叉树的递归遍历,二叉树的非递归遍历,查询二叉树的深度,查询每层的结点数,查找两个结点的最近共同祖先,二叉树的打印。2 程序运行后显现提示信息,等候用户输入06以进入相应的操作功能。3 用户输入数据完毕,程序将输出运行结果。4 测试数据应为字符型数据。三、概要设计1创建二叉树输入数据不低于15个,用递归方法建立二叉树。2二叉树的非递归前序遍历示意图图3.2二叉树前序遍历示意图3二叉树的后序非递归遍历示意图图3.4二叉树后序遍历示意图四、数据结构设计1 二叉树结点数据类型定义为:template structbinodebinode *rchild,*lchild;/指向左右孩子的指针t data; /结点数据信息;2 二叉树数据类型定义为:template class bitreetemplate friend ostream & operator (ostream &os ,bitree &bt);public:bitree();/无参构造函数bitree(int m);/有参空构造函数bitree(t ary,int num,t none);/有参构造函数bitree();/析构函数void preorder();/递归前序遍历void inorder();/递归中序遍历void postorder();/递归后续遍历void levelorder();/层序遍历int count();/计算二叉树的结点数int depth();/计算二叉树的深度void display(ostream &os);/打印二叉树,有层次void levelnum();/计算每一层结点数void preorder();/非递归前序遍历void postorder();/非递归后序遍历void creat();/创建二叉树t leastcommanancestor(t va, t vb);/求树中任意两结点最近共同祖先protected:/以下函数供上面函数调用/对应相同功能void creat(binode * &root);/创建void release(binode* &root);/删除binode * build(t ary,int num,t none,int idx);/用数组创建二叉树void preorder(binode* root);/前序遍历void postorder(binode* root);/后续遍历void levelnum(binode* root);/层序遍历void preorder(binode* root);/递归前序遍历void inorder(binode* root);/递归中序遍历void postorder(binode* root);/递归后续遍历void levelorder(binode*root);/层序遍历int count(binode* root);/计算结点数int depth(binode* root);/计算深度void display(ostream& os,binode* root,int dep);/打印static bool leastcommanancestor(binode *root, t va, t vb, binode *&result, binode* parrent);/求最近共同祖先private:binode *rootptr;五、算法设计1、创建二叉树/实现外部递归调用void bitree:creat()creat(rootptr);/类体内递归创建二叉树template void bitree:creat(binode * & root)t item;cinitem;if(item=#) root=null;elseroot=new binode;root-data=item;creat(root-lchild);creat(root-rchild);2、非递归前序遍历template void bitree:preorder()preorder(rootptr);template void bitree:preorder(binode* root)stack binode * s;while(root!=null|!s.empty()while(root)coutdata;s.push(root);root=root-lchild;if(!s.empty()root=s.top();s.pop();root=root-rchild;3、非递归后序遍历template void bitree:postorder()postorder(rootptr);template void bitree:postorder(binode *root) stackbinode* s;/定义栈,节点类型为treenode binode *p = root; binode *pre = null;/pre表示最近一次访问的结点 while(p|!s.empty() /沿着左孩子方向走到最左下。 while(p) s.push(p); p = p-lchild; /get the top element of the stack p = s.top(); /如果p没有右孩子或者其右孩子刚刚被访问过 if(p-rchild= null| p-rchild = pre) /visit this element and then pop it cout data; s.pop(); pre = p; p = null; else p = p-rchild; /end of while(p | st.size()!=0)4、求二叉树的高度template int bitree:depth()return depth(rootptr);template int bitree:depth(binode *root)int rdep,ldep;if(root=null)return 0;else ldep=depth(root-lchild);rdep=depth(root-rchild);return (rdepldep?rdep:ldep)+1;5、求二叉树每一层的结点数template void bitree:levelnum()levelnum(rootptr);template void bitree:levelnum(binode * root)queue binode * q;int front,rear,first,last,level;front=rear=first=0;last=level=1;if(root)q.push(root);rear+;while(frontlchild)q.push(root-lchild);rear+;if(root-rchild)q.push(root-rchild);rear+;if(front=last)cout第level层有last-first个结点endl;level+;last=rear;first=front;6、求两节点最近共同祖先template t bitree:leastcommanancestor(t n1, t n2)return leastcommanancestor(rootptr,n1,n2);template t bitree:leastcommanancestor(binode * root, t n1, t n2) if(root = null | root-data = n1 | root-data = n2) return -1; if(root-rchild!= null) & (root-rchild-data = n1 | root-rchild-data = n2) return root-data; if(root-lchild != null) & (root-lchild-data = n1 | root-lchild-data = n2) return root-data; if(root-data n1 & root-data data; if(root-data n1 & root-data n2) return leastcommanancestor(root-lchild, n1, n2); if(root-data data rchild, n1, n2); 6、算法流程图程序初始化用户交互用户输入选择求共同祖先求每层结点数求深度遍历二叉树创建二叉树结束处理并输出结果六、程序测试与实现1、函数之间的调用关系main()每层结点数求lca求节点数深度遍历创建lca()levelnum()creat()count()depth()inorder()postorder()preorder()2、主程序void main()bitree tree(1);while(1)couttt 欢迎使用本系统! endl;couttt# endl;couttt# # endl;couttt# 1-创建一颗二叉树并显示 # endl;couttt# 2-遍历二叉树 # endl;couttt# 3-查询二叉树的深度和结点数 # endl;couttt# 4-查询每层结点数 # endl;couttt# 5-查找两节点p和q的最近共同祖先 # endl;couttt# 6-退出 # endl;couttt# # endl;couttt# endl;coutx;switch(x)case 1:cout请输入二叉树的前序遍历:endl;cout(以#作为分支结尾,例如:a b # # c # #)endl;tree.creat();couttree;coutendl; break;case 2:coutendl;cout前序遍历为:;tree.preorder();coutendl;cout中序遍历为:;tree.inorder();coutendl;cout后序遍历为:;tree.postorder();coutendl;cout层序遍历为:;tree.levelorder();coutendl;coutendl; break;case 3: cout树的深度为:tree.depth()endl;cout树的结点数:tree.count()endl;coutendl;break;case 4:tree.levelnum(); break;case 5:char ch1,ch2;coutch1;coutch2;coutch1和ch2的最近共同祖先是tree.leastcommanancestor(ch1, ch2)endl; brea
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 邮政快递智能技术专业教学标准(高等职业教育专科)2025修订
- 2025年中国家用光脱毛器具行业市场全景分析及前景机遇研判报告
- 中国鞋面横织机行业市场竞争格局及投资前景展望报告
- 中医培训课件 哪些
- 2025年中国车床行业市场深度评估及投资策略咨询报告
- 中国幕墙装饰板市场规模预测及投资战略咨询报告
- 2025年 重庆市长寿区教育事业单位定向招聘考试笔试试题附答案
- 2025年 新疆铁道职业技术学院招聘考试笔试试题附答案
- 2025年 楚雄州楚雄市紧密型医共体编制外职工招聘笔试试题附答案
- 中国蔬菜种场运植市场竞争格局及行业投资前景预测报告
- 沟通与演讲2023学习通超星课后章节答案期末考试题库2023年
- 健康生活方式基本的知识讲座
- 制造执行系统SMT MES解决方案
- 高二区域地理 撒哈拉以南的非洲课件
- 数字化精密加工车间项目可行性研究报告建议书
- 2022年《内蒙古自治区建设工程费用定额》取费说明
- Q∕GDW 10799.6-2018 国家电网有限公司电力安全工作规程 第6部分:光伏电站部分
- 宁波市建设工程资料统一用表(2022版)1 通用分册
- 危险化学品安全技术说明书MSDS—汽油
- 三甲医院必备医疗设备清单大全
- 暴雨产流计算(推理公式_四川省)
评论
0/150
提交评论