二叉树地遍历课程设计C++含源代码.docx
- 文档编号:24976420
- 上传时间:2023-06-03
- 格式:DOCX
- 页数:19
- 大小:145.27KB
二叉树地遍历课程设计C++含源代码.docx
《二叉树地遍历课程设计C++含源代码.docx》由会员分享,可在线阅读,更多相关《二叉树地遍历课程设计C++含源代码.docx(19页珍藏版)》请在冰豆网上搜索。
二叉树地遍历课程设计C++含源代码
南京理工大学
课程设计报告
作者:
相蒙蒙
学号:
054913221001
教学点:
苏州市职业大学
专业:
机电一体化
题目:
二叉树的遍历
指导者:
尚鲜莲
评阅者:
2014年4月
南京理工大学
课程设计报告评语
综合成绩:
指导者评语:
指导者(签字):
年月日
课程设计报告摘要
摘要:
本文主要说明如何实现二叉树的遍历。
此次二叉树的遍历基于二叉树的二叉链表存储结构。
遍历方式包括:
前序遍历,中序遍历,后续遍历,层序遍历。
其中前序遍历和后续遍历采用非递归算法实现。
编程环境为VC++,除了遍历操作外,还增加了求二叉树的深度,总结点数,每层结点数,以及最近共同祖先(LCA)问题的算法。
关键词:
二叉树遍历二叉树遍历
6、程序测试与调试............................12
1、问题描述
1.1问题描述:
创建二叉树并遍历
基本要求:
1、分别运用非递归的方式完成对二叉树的先序和后序遍历
2、输出二叉树的高度
3、输出每一层的结点数
4、查找结点P和结点Q的最近共同祖先
2、需求分析
1、本程序的功能包括二叉树的建立,二叉树的递归遍历,二叉树的非递归遍历,查询二叉树的深度,查询每层的结点数,查找两个结点的最近共同祖先,二叉树的打印。
2、程序运行后显现提示信息,等候用户输入0—6以进入相应的操作功能。
3、用户输入数据完毕,程序将输出运行结果。
4、测试数据应为字符型数据。
3、概要设计
3.1创建二叉树
输入数据不低于15个,用递归方法建立二叉树。
3.2二叉树的非递归前序遍历示意图
图3.2二叉树前序遍历示意图
3.3二叉树的后序非递归遍历示意图
图3.4二叉树后序遍历示意图
4、数据结构设计
4.1二叉树结点数据类型定义为:
template
structBiNode
{
BiNode
Tdata;//结点数据信息
};
4.2二叉树数据类型定义为:
template
classBiTree
{
template
friendostream&operator<<(ostream&os,BiTree
public:
BiTree();//无参构造函数
BiTree(intm){};//有参空构造函数
BiTree(Tary[],intnum,Tnone);//有参构造函数
~BiTree();//析构函数
voidpreorder();//递归前序遍历
voidinorder();//递归中序遍历
voidpostorder();//递归后续遍历
voidlevelorder();//层序遍历
intcount();//计算二叉树的结点数
intdepth();//计算二叉树的深度
voiddisplay(ostream&os);//打印二叉树,有层次
voidLevelNum();//计算每一层结点数
voidPreOrder();//非递归前序遍历
voidPostOrder();//非递归后序遍历
voidcreat();//创建二叉树
TleastCommanAncestor(Tva,Tvb);//求树中任意两结点最近共同祖先
protected:
//以下函数供上面函数调用
//对应相同功能
voidcreat(BiNode
voidrelease(BiNode
BiNode
voidPreOrder(BiNode
voidPostOrder(BiNode
voidLevelNum(BiNode
voidpreorder(BiNode
voidinorder(BiNode
voidpostorder(BiNode
voidlevelorder(BiNode
intcount(BiNode
intdepth(BiNode
voiddisplay(ostream&os,BiNode
staticboolleastCommanAncestor(BiNode
private:
BiNode
};
5、算法设计
5.1创建二叉树
//实现外部递归调用
voidBiTree
:
creat()
{
creat(rootptr);
}
//类体内递归创建二叉树
template
voidBiTree
:
creat(BiNode
{
Titem;
cin>>item;
if(item=='#')root=NULL;
else
{
root=newBiNode
root->data=item;
creat(root->lchild);
creat(root->rchild);
}
}
5.2非递归前序遍历
template
voidBiTree
:
PreOrder()
{
PreOrder(rootptr);
}
template
voidBiTree
:
PreOrder(BiNode
{
stack
while(root!
=NULL||!
s.empty())
{
while(root)
{
cout<
s.push(root);
root=root->lchild;
}
if(!
s.empty())
{
root=s.top();
s.pop();
root=root->rchild;
}
}
}
5.3非递归后序遍历
template
voidBiTree
:
PostOrder()
{
PostOrder(rootptr);
}
template
voidBiTree
:
PostOrder(BiNode
{
stack
BiNode
BiNode
while(p||!
s.empty())
{
//沿着左孩子方向走到最左下。
while(p)
{
s.push(p);
p=p->lchild;
}
//getthetopelementofthestack
p=s.top();
//如果p没有右孩子或者其右孩子刚刚被访问过
if(p->rchild==NULL||p->rchild==pre)
{
//visitthiselementandthenpopit
cout<
s.pop();
pre=p;
p=NULL;
}
else
{
p=p->rchild;
}
}//endofwhile(p||st.size()!
=0)
}
5.4求二叉树的高度
template
intBiTree
:
depth()
{
returndepth(rootptr);
}
template
intBiTree
:
depth(BiNode
{
intrdep,ldep;
if(root==NULL)
return0;
else
{
ldep=depth(root->lchild);
rdep=depth(root->rchild);
return(rdep>ldep?
rdep:
ldep)+1;
}
}
5.5求二叉树每一层的结点数
template
voidBiTree
:
LevelNum()
{
LevelNum(rootptr);
}
template
voidBiTree
:
LevelNum(BiNode
{
queue
intfront,rear,first,last,level;
front=rear=first=0;
last=level=1;
if(root)
{
q.push(root);
rear++;
while(front { root=q.front(); q.pop(); front++; if(root->lchild) { q.push(root->lchild); rear++; } if(root->rchild) { q.push(root->rchild); rear++; } if(front==last) { cout<<"第"< level++; last=rear; first=front; } } } } 5.6求两节点最近共同祖先 template TBiTree : leastCommanAncestor(Tn1,Tn2){ returnleastCommanAncestor(rootptr,n1,n2); } template TBiTree : leastCommanAncestor(BiNode { if(root==NULL||root->data==n1||root->data==n2) return-1; if((root->rchild! =NULL)&& (root->rchild->data==n1||root->rchild->data==n2)) returnroot->data; if((root->lchild! =NULL)&& (root->lchild->data==n1||root->lchild->data==n2)) returnroot->data; if(root->data>n1&&root->data returnroot->data; if(root->data>n1&&root->data>n2) returnleastCommanAncestor(root->lchild,n1,n2); if(root->data returnleastCommanAncestor(root->rchild,n1,n2); } 5.7算法流程图 6、程序测试与实现 6.1函数之间的调用关系 6.2主程序 voidmain() { BiTree (1); while (1){ cout<<"\t\t欢迎使用本系统! ! "< cout<<"\t\t########################################"< cout<<"\t\t##"< cout<<"\t\t#1--创建一颗二叉树并显示#"< cout<<"\t\t#2--遍历二叉树#"< cout<<"\t\t#3--查询二叉树的深度和结点数#"< cout<<"\t\t#4--查询每层结点数#"< cout<<"\t\t#5--查找两节点P和Q的最近共同祖先#"< cout<<"\t\t#6--退出#"< cout<<"\t\t##"< cout<<"\t\t########################################"< cout<<"请输入你的选择: "; intx; cin>>x; switch(x){ case1: { cout<<"请输入二叉树的前序遍历: "< cout<<"(以#作为分支结尾,例如: AB##C##)"< Tree.creat(); cout< cout< }break; case2: { cout< cout<<"前序遍历为: "; Tree.PreOrder(); cout< cout<<"中序遍历为: "; Tree.inorder(); cout< cout<<"后序遍历为: "; Tree.PostOrder(); cout< cout<<"层序遍历为: "; Tree.levelorder(); cout< cout< }break; case3: { cout<<"树的深度为: "< cout<<"树的结点数: "< cout< }break; case4: { Tree.LevelNum(); }break; case5: { charch1,ch2; cout<<"请输入P数据信息: "; cin>>ch1; cout<<"请输入Q数据信息: "; cin>>ch2; cout< }break; case6: return;break; default: cout<<"请输入正确的选择! ! ! "< } } } 6.3测试数据 AB#CD###E#FGH##K### 6.4测试结果 7、调试分析 创建二叉树: 依次输入二叉树前序遍历序列,构建相应的二叉树。 二叉树遍历: 递归算法、非递归算法测试,调用相应函数进行测试,结果正确。 求二叉树深度和结点数: 创建一个二叉树,调用相关函数,测试结果正确。 计算每层结点数: 调用levelNum()函数,测试结果正确。 求最近共同祖先: 调用LCA()函数,测试结果正确。 8、遇到的问题及解决办法 调试时遇到诸多问题,其中最主要的问题是死循环问题,在非递归遍历时,容易进入死循环,经过查找资料、分步调试最终找到循环结束条件,顺利解决各个难题。 9、心得体会 通过本次课程设计,我发现,有关一个课题的所有知识不仅仅是在课本上,多查阅一些资料能够更好的完成课题,这就需要一种能力,即自学能力。 本次课程设计还让我认识到自己的缺点。 本次选的课题是二叉树的遍历,因为本学期所学的就是二叉树等数据结构,所以认为比较适合。 刚开始认为会很简单,但到后来就出现一些难以解决的问题,就像老师请教,并查阅相关资料。 经过慢慢的调试,最终测试成功。 这次课程设计让我所学到的数据结构知识发挥的淋漓尽致,而且还拓展了我的知识面,使我更加熟练的掌握各种方法。 总之,这次课程设计增强了我的自学能力,拓展了我的知识面,让我对数据结构更加了解。 10、参考文献 [1]崔俊凯.计算机软件设计[M].机械工业出版社,2007.9 [2]吴乃陵,况迎辉.C++程序设计(第二版).高等教育出版社,2006.3 [3]王红梅.数据结构C++版[M].清华大学出版社,2011.6
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 二叉 遍历 课程设计 C+ 源代码
