版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、信息科学与技术学院数据结构课程设计报告题目名称:二叉树的应用专业班级:计算机科学与技术学生姓名:陈 杰学生学号:200808261指导教师:高 攀完成日期:2010-04目 录1、课程设计的目的、课程设计题目、题目要求21.1课程设计的目的31.2课程设计的题目31.3题目要求32课程设计的实验报告内容:43课程设计的原程序代码:44运行结果165. 课程设计总结216参考书目221课程设计的目的1.1课程设计的目的:通过以前的学习以及查看相关资料,按着题目要求编写程序,进一步加强对编程的训练,使得自己掌握一些将书本知识转化为实际应用当中.在整个程序中,主要应用的是链表,但是也运用了类.通过两
2、种方法解决现有问题.1.2课程设计的题目: 二叉树的应用1.3题目要求:1. 建立二叉树的二叉链表存储算法2. 二叉树的先序遍历,中序遍历和后序遍历输出3. 非递归的先序遍历,中序遍历4. 二叉树的层次遍历5. 判断此二叉树是否是完全二叉树6. 二叉树的左右孩子的交换2课程设计的实验报告内容:7. 通过递归对二叉树进行遍历。二叉树的非递归遍历主要采用利用队进行遍历。此后的判断此二叉树是否是完全二叉树也才采用队,而二叉树的左右孩子的交换则采用的是一个简单的递归。3课程设计的原程序代码:#include<iostream>using namespace std;#define MAXS
3、IZE 100int sign=0;void menu();/typedef struct BiTNodechar data;BiTNode *left_child,*right_child;BiTNode,*BiTree;int CreateBiTree(BiTree &T)/创建二叉树 char ch; cout<<"请输入数据(#为结束): " cin>>ch; if(ch='#') T=NULL; else if(!(T=new BiTNode) cout<<"Overflow!"/no
4、 alloction return 0; T->data=ch; CreateBiTree(T->left_child);/create leftchild CreateBiTree(T->right_child); /create rightchild return 1;/判断此树是否是完全二叉树int LevelOrder1(BiTree &T)BiTree stackMAXSIZE;BiTreep;int front,rear;front=-1,rear=0;stackrear=T;while(rear!=front)front+;p=stackfront;if
5、(p->left_child=NULL)&&(p->right_child)sign=1;if(p->left_child)rear+;stackrear=p->left_child;if(p->right_child)rear+;stackrear=p->right_child;return 1;void Output(BiTree &T) /输出二叉树if(!T) cout<<"空树!n"return ; /空树cout<<T->data<<" "/
6、 输出根结点if(T->left_child) Output(T->left_child); /输出左子树if(T->right_child)Output(T->right_child);/输出右子树int Depth(BiTree &T) /求树深int i,j;if(!T) return 0;i = Depth(T->left_child);j = Depth(T->right_child);return (i>j?i:j) + 1;int Node(BiTree &T)/求结点数if(!T) return 0;return 1+N
7、ode(T->left_child)+Node(T->right_child);int Leaf(BiTree &T) /求叶子结点if(!T) return 0;if(!T->left_child&&!T->right_child) return 1;/仅有根结点return Leaf(T->left_child)+Leaf(T->right_child);/返回叶子结点的数目void PreOrder(BiTree &T) /先序遍历算法 DLRif(!T) return ; /递归调用的结束条件cout<<T
8、->data<<" " / 访问根结点的数据域PreOrder(T->left_child);/先序递归遍历T的左子树PreOrder(T->right_child);/先序递归遍历T的右子树void InOrder(BiTree &T)/中序遍历算法 LDRif(!T) return ;InOrder(T->left_child);cout<<T->data<<" "InOrder(T->right_child);void PostOrder(BiTree &T)/
9、后序遍历算法 LRDif(!T) return ;PostOrder(T->left_child);PostOrder(T->right_child);cout<<T->data<<" "/非递归先序遍历int NRPreOrder(BiTree &T)BiTree stack100,p;int top;if(T=NULL)return 1;top=-1;p=T;while(!(p=NULL&&top=-1)while(p!=NULL)cout<<p->data<<"
10、"if(top<100-1)top+;stacktop=p;else cout<<"栈溢出"<<endl; return 0; p=p->left_child;if(top=-1)return 1;elsep=stacktop;top-;p=p->right_child; return 1;/非递归中序遍历int NRInOrder(BiTree &T)BiTree stack100,p;int top;if(T=NULL)return 1;top=-1;p=T;while(!(p=NULL&&to
11、p=-1)while(p!=NULL)if(top<100-1)top+;stacktop=p;else cout<<"栈溢出"<<endl; return 0; p=p->left_child;if(top=-1)return 1;elsep=stacktop;cout<<p->data<<" "top-;p=p->right_child;return 1;/层次遍历void LevelOrder(BiTree &T)BiTree queue100;int front,re
12、ar;if(T=NULL)return;front=-1;rear=0;queuerear=T;while(front!=rear)front+;cout<<queuefront->data<<" "if(queuefront->left_child!=NULL)rear+;queuerear=queuefront->left_child;if(queuefront->right_child!=NULL)rear+;queuerear=queuefront->right_child;/*左右子树交换*/*将结点的左右子树
13、交换*/*BiTNode *swap(BiTNode &T) BiTNode *t,*t1,*t2; if(T=NULL) t=NULL; else t=(BiTNode *)malloc(sizeof(BiTNode); t->data=T->data; t1=swap(T->left_child); t2=swap(T->right_child); t->left_child=t2; t->right_child=t1; return(t); void print(BiTNode &T) if(T!=NULL) printf("
14、%c",T->data); if(T->left_child!=NULL|T->right_child!=NULL) printf("("); print(b->left_child); if(b->right_child!=NULL)printf(", "); print(b->right_child); printf(")"); */int PreOrderTraverse(BiTree T) if(!T)return 0;BiTree t;if (T->left_child|T
15、->right_child) t=T->left_child;T->left_child=T->right_child;T->right_child=t;PreOrderTraverse(T->left_child);PreOrderTraverse(T->right_child);return 0;/菜单void menu() cout<<"*"<<endl; cout<<"<< (主菜单) >>"<<endl; cout<<&
16、quot;*"<<endl; cout<<"<< 0.建立二叉树 >>"<<endl; cout<<"<< 1.二叉树树深 >>"<<endl; cout<<"<< 2.二叉树结点数 >>"<<endl; cout<<"<< 3.二叉树的叶子结点 >>"<<endl; cout<<"
17、<< 4.二叉树的先序遍历 >>"<<endl; cout<<"<< 5.二叉树的中序遍历 >>"<<endl; cout<<"<< 6.二叉树的后序遍历 >>"<<endl; cout<<"<< 7.二叉树的非递归先序遍历 >>"<<endl; cout<<"<< 8.二叉树的非递归中序遍历 >>&q
18、uot;<<endl; cout<<"<< 9.二叉树的层次遍历 >>"<<endl; cout<<"<< 10.判断此树是否是完全二叉树 >>"<<endl; cout<<"<< 11.左右孩子交换 >>"<<endl; cout<<"<< 12.退出 >>"<<endl; cout<<"*
19、"<<endl;/主函数void main() int br,a;BiTree T;br=CreateBiTree(T);while(1)menu();cout<<"请输入选择的命令->"cin>>a;if(a>=0|a<=12)switch (a)case 0:cout<<"建立后的二叉树为:n"Output(T);cout<<endl;system("pause");break;case 1:cout<<"该树的树深为:
20、"<<Depth(T)<<endl;system("pause");break;case 2:cout<<"该树的结点数:"<<Node(T)<<endl;system("pause");break;case 3:cout<<"该树的叶子结点为:"<<Leaf(T)<<endl;system("pause");break;case 4:cout<<"该树以先序遍历输出为
21、:n"PreOrder(T);cout<<endl;system("pause");break;case 5:cout<<"该树以中序遍历输出为:n"InOrder(T);cout<<endl;system("pause");break;case 6:cout<<"该树以后序遍历输出为:n"PostOrder(T);cout<<endl;system("pause");break;case 7:cout<<"该树的非递归先序遍历输出为:n" NRPreOrder(T);cout<<endl;system("pause");break;cas
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 企业沟通标准化模板分享
- 团队建设活动策划与评估方案
- 办公场所信息安全事情事后恢复预案
- 描述家乡四季之美写景文章(11篇)
- 客户满意度与质量追溯承诺书3篇
- 技术人员工作流程管理模板
- 快速规划自动化设备调整清单
- 农业科技领域的承诺书(6篇)
- 单位债务及时偿付责任承诺书7篇
- 高品质生活领域承诺函3篇范文
- 注塑岗位安全培训课件
- 2025年高职(城市轨道交通机电技术)设备调试阶段测试题及答案
- 2026年考试题库北汽集团高管知识水平测试
- 核电防异物管理指南(核心版)
- 电厂防汛课件
- 人工智能在高职机械专业教学中的应用研究
- 高标准农田建设项目操作方案指南
- 2026年上饶职业技术学院单招职业技能考试必刷测试卷附答案
- 野战生存课件军用
- 环卫车辆安全行驶培训课件
- 刷漆搭架施工方案
评论
0/150
提交评论