- 1、本文档共7页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
- 5、该文档为VIP文档,如果想要下载,成为VIP会员后,下载免费。
- 6、成为VIP后,下载本文档将扣除1次下载权益。下载后,不支持退款、换文档。如有疑问请联系我们。
- 7、成为VIP后,您将拥有八大权益,权益包括:VIP文档下载权益、阅读免打扰、文档格式转换、高级专利检索、专属身份标志、高级客服、多端互通、版权登记。
- 8、VIP文档为合作方或网友上传,每下载1次, 网站将根据用户上传文档的质量评分、类型等,对文档贡献者给予高额补贴、流量扶持。如果你也想贡献VIP文档。上传文档
查看更多
二叉树的非递归遍历
福建师大福清分校计算机实验报告
院/系: 数计系 课程名称:数据结构与高级语言实验 日期:
专业 ?08信本 组号 ? 学号 ?118632008034 实验室 ?C204 班级 08信息 姓名 谢佩珍 老师
签名 ?刘应平 实验名称 二叉树的非递归遍历 成绩
评定 ? 实验环境 ?Windows XP,VC或TC
? 实
验
内
容
1:利用栈来实现前序,中序遍历二叉树的非递归算法
2:求二叉树的结点数
3:叶子结点数和深度
4:转换二叉树
(写不完时,可另加附页。) 测
试
报
告
与
分
析 利用栈来实现前序遍历二叉树时,用到循环,如果二叉树非空先访问它的根结点,并输出该结点且访问它的左孩子,若左孩子为空,则访问它的右孩子。
用到的函数:
1:前序遍历非递归算法: void preorder2(BTNode *bt)
2:中序遍历非递归算法:void inorder2(BTNode *bt)
3:计算节点个数: void Node_count(btnode *bt)
4:计算叶子节点的个数: void leaf_btnode(btnode *bt)
5:交换左右孩子: void exchange(btnode *bt)
6:计算二叉树的深度: int depth(btnode *bt)
运行结果如下:
1.preoreder the tree.
2.inorder the tree .
3.count the nodes number.
4.count the leafs number.
5.count the trees deep.
6.Exchange the lchild and rchild.
please input the information,if you put # the doing is ove
ABC##D##EF##G##
please input the number0----5:1
The qian xu bian li is:A B C D E F G
Are you went to continue(0---1)1
please input the number0----5:5
The trees deep is: 3
Are you went to continue(0---1)1
please input the number0----5:3
the nodes number is: 7
Are you went to continue(0---1)1
please input the number0----5:6
Exchange the lchild and rchild,the result is :A E G F B D C
Are you went to continue(0---1)1
please input the number0----5:3
the nodes number is: 14
Are you went to continue(0---1)1
please input the number0----5:4
The leafs number is: 4 思
考
与
心
得
对二叉树的遍历查询,节点的深度,个数及叶子的计算机操作有了一定的认识,但是对于具体的程序的完整性及细节的地方仍需加强.
附源码 #include stdio.h
#define Maxsize 20
typedef struct btnode
{
char data;
struct btnode *lchild,*rchild;
}btnode;
int count1,count2,deep;
btnode *Creatbtnode2()
{
btnode *t;
char c;
c=getchar();
if(c==#) return NULL;
else
{
t=(btnode*)malloc(sizeof(btnode));
t-data=c;
t-lchild=Creatbtnode2();
t-rchild=Creatbtnode2();
}
return t;
}
void preorder2(btnode *bt)
{
btnode *p=bt,*s[Maxsize];
int top=-1;
while(p||top-1)
{
if(p)
{
printf(%c ,p-data);
++top;
s[top]=p;
p=p-lchild;
}
else
{
p=s[top];
-
您可能关注的文档
最近下载
- Q∕GDW 11894-2018 -电力监控系统网络安全监测装置检测规范.pdf VIP
- 电工基础:数字电路概述.pptx VIP
- 师德师风培训类师德讲座.pdf VIP
- 工艺装置蒸汽伴热管的设计与计算.pdf.pdf VIP
- 一种便携式紧凑型双作用丝杆螺母式手轮机构.pdf VIP
- 临边防护安全检查要点.pptx VIP
- 2025北京市知识产权局所属事业单位下半年招聘4人备考题库及答案解析.docx VIP
- 行业分析报告:高端装备制造产业-先进轨道交通装备行业_轨道交通电气化与供电系统.docx VIP
- 铝合金黑色微弧陶瓷氧化 .pdf VIP
- 中小学校教师师德师风专题培训讲座PPT课件.pptx VIP
文档评论(0)