资源预览内容
第1页 / 共26页
第2页 / 共26页
第3页 / 共26页
第4页 / 共26页
第5页 / 共26页
第6页 / 共26页
第7页 / 共26页
第8页 / 共26页
第9页 / 共26页
第10页 / 共26页
亲,该文档总共26页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
资料内容仅供您学习参考,如有不当之处,请联系改正或者删除。信息科学与技术学院数据结构课程设计报告题目名称: 二叉树的应用专业班级: 计算机科学与技术学生姓名: 陈 杰学生学号: 指导教师: 高 攀完成日期: -04目 录1、 课程设计的目的、 课程设计题目、 题目要求21.1课程设计的目的31.2课程设计的题目31.3题目要求32课程设计的实验报告内容:43课程设计的原程序代码:44运行结果165. 课程设计总结216参考书目221课程设计的目的1.1课程设计的目的:经过以前的学习以及查看相关资料,按着题目要求编写程序,进一步加强对编程的训练,使得自己掌握一些将书本知识转化为实际应用当中.在整个程序中,主要应用的是链表,可是也运用了类.经过两种方法解决现有问题.1.2课程设计的题目: 二叉树的应用1.3题目要求:1. 建立二叉树的二叉链表存储算法2. 二叉树的先序遍历, 中序遍历和后序遍历输出3. 非递归的先序遍历, 中序遍历4. 二叉树的层次遍历5. 判断此二叉树是否是完全二叉树6. 二叉树的左右孩子的交换2课程设计的实验报告内容:7. 经过递归对二叉树进行遍历。二叉树的非递归遍历主要采用利用队进行遍历。此后的判断此二叉树是否是完全二叉树也才采用队, 而二叉树的左右孩子的交换则采用的是一个简单的递归。3课程设计的原程序代码:#includeusing namespace std;#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_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(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 ; /空树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 Node(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 ; /递归调用的结束条件coutdataleft_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 stack100,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;while(!(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!=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_child); 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;
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号