数据结构课程设计报告二叉树的遍历
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
《数据结构》课程设计报告
设计题目:___二叉树的遍历_____
姓名:__________王伦__________
学号:________211113206_______
专业:__________物联网________
院系:___计算机科学与技术学院_
班级:___________1104__________
指导教师:_______高秀梅_______
2013 年 3 月 22 日
摘要:本课程设计主要说明如何在C++编程环境下实现二叉树的遍历,遍历方式包括:二叉树的前序非递归遍历、二叉树的后续非递归遍历。同时,此次课程设计还包括了求二叉树每层节点数和求解任意俩点最近的共同祖先以及计算二叉树深度的功能。
英文摘要:Abstract: this course design mainly shows how in C + + programming environment to achieve binary tree traversal, traversal methods include: the preamble of binary tree non-recursive traversal, subsequent non-recursive traversal of binary tree. At the same time, the curriculum design includes for binary tree each layer node number and the solution of arbitrary two points in recent common ancestor and calculating the function of the depth of a binary tree.
目录
一、问题描述 (4)
二、需求分析 (4)
三、概要设计 (4)
四、数据结构设计 (5)
五、算法设计 (5)
六、程序测试与实现 (10)
七、调试分析 (13)
八、遇到的问题与解决方法 (13)
九、心得体会 (13)
一、问题描述
问题描述:创建二叉树并遍历
基本要求:
1、分别运用非递归的方式完成对二叉树的先序和后序遍历
2、输出二叉树的高度
3、输出每一层的结点数
4、查找结点P 和结点Q的最近共同祖先
二、需求分析
1.本程序的功能包括建立二叉树、前序遍历二叉树、后序遍历二叉树、求二叉树的深度、求每层节点的个数、求任意俩个节点的共同祖先等。
2.程序运行后显现提示信息,等候用户输入0—7以进入相应的操作功能。
3.用户输入数据完毕,程序将运行相应的程序并输出运行结束。
4.测试数据应为char型数据。
三、概要设计
1、建立任意一个节点数不超过100二叉树;
2
3后序遍历
四、数据结构设计
struct BiNode{ //节点声明T data; //节点数据
BiNode
class BiTree{ //树类型声明template
friend ostream & operator<<(ostream & os,BiTree
public:
BiTree(); //构造函数
~BiTree(void); //析构函数
BiNode
void PreOrder(BiNode
void PostOrder(BiNode
int Depth(BiNode
void ZJzx(BiNode
BiNode
BiNode
void Release(BiNode
void Print(ostream & os);
void Println(ostream &os,BiNode
};
五、算法设计
1、二叉树的构造
BiNode
BiNode
T ch;
cout<<"请输入一个二叉树的节点(以#作为结束标记)"< cin>>ch; if(ch=='#') bt=NULL; else{ bt=new BiNode bt->data=ch; bt->lchild=Creat(); bt->rchild=Creat(); } return bt; } template BiTree cout<<"请先建立一个二叉树"< cout< cout< this->root=Creat(); } 2、输出函数 template void BiTree Println(os,root->rchild,depth+1); for(int i=0;i<4*(depth-1);i++) os<<" "; os<<"*----"< Println(os,root->lchild,depth+1); } } template void BiTree Println(os,root,1); } template ostream & operator<<(ostream & os,BiTree bt.Print(os); return os; } 3、前序遍历