二叉树的应用数据结构课程设计样本

上传人:公**** 文档编号:560499485 上传时间:2023-09-04 格式:DOC 页数:26 大小:220KB
返回 下载 相关 举报
二叉树的应用数据结构课程设计样本_第1页
第1页 / 共26页
二叉树的应用数据结构课程设计样本_第2页
第2页 / 共26页
二叉树的应用数据结构课程设计样本_第3页
第3页 / 共26页
二叉树的应用数据结构课程设计样本_第4页
第4页 / 共26页
二叉树的应用数据结构课程设计样本_第5页
第5页 / 共26页
点击查看更多>>
资源描述

《二叉树的应用数据结构课程设计样本》由会员分享,可在线阅读,更多相关《二叉树的应用数据结构课程设计样本(26页珍藏版)》请在金锄头文库上搜索。

1、资料内容仅供您学习参考,如有不当之处,请联系改正或者删除。信息科学与技术学院数据结构课程设计报告题目名称: 二叉树的应用专业班级: 计算机科学与技术学生姓名: 陈 杰学生学号: 指导教师: 高 攀完成日期: -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课程设计的原程序代码:#includeusing namespace std;

3、#define MAXSIZE 100int sign=0;void menu();/typedef struct BiTNodechar data;BiTNode *left_child,*right_child;BiTNode,*BiTree;int CreateBiTree(BiTree &T)/创立二叉树 char ch; coutch; if(ch=#) T=NULL; else if(!(T=new BiTNode) coutdata=ch; CreateBiTree(T-left_child);/create leftchild CreateBiTree(T-right_chil

4、d); /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(p-left_child=NULL)&(p-right_child)sign=1;if(p-left_child)rear+;stackrear=p-left_child;if(p-right_child)rear+;stackrea

5、r=p-right_child;return 1;void Output(BiTree &T) /输出二叉树if(!T) cout空树!n;return ; /空树coutdataleft_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 (ij?i:j) + 1;int Nod

6、e(BiTree &T)/求结点数if(!T) return 0;return 1+Node(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 ; /递归调用的结束条件coutdatalef

7、t_child);/先序递归遍历T的左子树PreOrder(T-right_child);/先序递归遍历T的右子树void InOrder(BiTree &T)/中序遍历算法 LDRif(!T) return ;InOrder(T-left_child);coutdataright_child);void PostOrder(BiTree &T)/后序遍历算法 LRDif(!T) return ;PostOrder(T-left_child);PostOrder(T-right_child);coutdata ;/非递归先序遍历int NRPreOrder(BiTree &T)BiTree s

8、tack100,p;int top;if(T=NULL)return 1;top=-1;p=T;while(!(p=NULL&top=-1)while(p!=NULL)coutdata ;if(top100-1)top+;stacktop=p;else cout栈溢出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;wh

9、ile(!(p=NULL&top=-1)while(p!=NULL)if(top100-1)top+;stacktop=p;else cout栈溢出left_child;if(top=-1)return 1;elsep=stacktop;coutdataright_child;return 1;/层次遍历void LevelOrder(BiTree &T)BiTree queue100;int front,rear;if(T=NULL)return;front=-1;rear=0;queuerear=T;while(front!=rear)front+;coutdataleft_child!=

10、NULL)rear+;queuerear=queuefront-left_child;if(queuefront-right_child!=NULL)rear+;queuerear=queuefront-right_child;/*左右子树交换*/*将结点的左右子树交换*/*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_c

11、hild); t-left_child=t2; t-right_child=t1; return(t); void print(BiTNode &T) if(T!=NULL) printf(%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-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;

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 工作计划

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号