长春建筑学院《数据结构》课程设计(论文)基于二叉树遍历系统设计与实现Binary tree traversal System Design and Implementation年级:学号:姓名:专业:指导老师:二零一三年十二月摘要针对现实世界中许多关系复杂的数据,如人类社会的家谱,各种社会组织机构,博弈交通等复杂事物或过程以及客观世界中广泛存在的具有分支关系或层次特性的对象.如操作系统的文件构成、人工智能和算法分析的模型表示以及数据库系统的信息组织形式等,用线性结构难以把其中的逻辑关系表达出来,必须借助于数和图这样的非线性结构,因此在以模拟客观世界问题,解决客观世界问题为主要任务的计算机领域中树型结构是信息的一种重要组织形式,树有着广泛应用。
在树型结构的应用中又以二叉树最为常用。
二叉树是一种非常重要的非线性结构,所描述的数据有明显的层次关系,其中的每个元素只有一个前驱,二叉树是最为常用的数据结构,它的实际应用非常广泛,二叉树的遍历方式有三种,前序遍历,中序遍历,后序遍历,先序遍历的顺序为:NLR 先根结点,然后左子树,右子树;中序遍历顺序为;LNR先左子树,然后根结点,右子树;后序遍历顺序为:LRN先左子树,然后右子树,根结点。
由前序和中序遍历,有中序和后序遍历序列可以唯一确定一棵二叉树。
对于给几个数据的排序或在已知的几个数据中进行查找,二叉树均能提供一种十分有效的方法,比如在查找问题上,任何借助于比较法查找长度为Ⅳ的一个序表的算法,都可以表示成一株二叉树。
反之,任何二叉树都对应一个查找有序表的有效方法根据树的数学理论,对于算法分析的某些最有启发性的应用,是与给出用于计算各种类型中不同树的数目的公式有关的。
本文对二叉树以及二叉树的各种功能做介绍以及写出一些基本的程序,让读者对二叉树的理解有更好的效果。
关键词:二叉树;左子树;右子树AbstractIn many real world of complex data, such as the human society family, social organization, widespread game traffic complex thing or process and the objective world with a branch or level characteristics of the object. If the operating system file analysis, artificial intelligence and algorithm model representation and database information system the form of organization, with a linear structure to express the logic relationship among them, must depend on the number and the diagram of such nonlinear structure, so in order to simulate the objective world, solve problems as the main task of the computer field in the tree structure is an important organization form of information, the tree has a broad application. In the application of tree structure in which the two fork tree is the most commonly used.Two binary tree is a kind of very important nonlinear structure, hiberarchy description of the data, where each element is only a precursor, two fork tree is the most commonly used data structure, its application is very extensive, there are three kinds of two binary tree traversal, preorder traversal, in the traversal, postorder traversal, preorder traversal sequence: NLR to the root node, and then the left subtree, right subtree; in order traversal sequence; LNR before the left sub tree, then the root node, the right subtree; after the traversal order: LRN first and then the left subtree, right subtree, root node. By preorder traversal and traversal, with the order and post order traversal sequence can be uniquely identified a two binary tree.For several data sorting or searching in several data known, two fork tree can provide a very effective method, such as search problems, any by the comparison method to find the length of a sequential algorithm of Table IV, can be expressed as a two fork tree. Conversely, any two fork tree corresponds to an effective method to find the ordered list according to the mathematical theory of tree, for some algorithm analysis of the application of heuristic, is given for the number and different types of tree calculation formula.Various functions of two binary tree and binary tree in this paper two introduces and write some of the basic procedures, to allow readers to understand the two fork tree has a better effect.Keywords:Two tree; the left subtree; right subtree目录摘要 (I)ABSTRACT (Ⅱ)第1章绪论 ................................................................................................................ - 1 -1.1设计目的 (1)1.2设计内容 (1)1.3设计要求 (1)1.4设计思想 (2)1.5系统模块划分 (2)1.6主要功能模块设计 (2)第2章系统总体设计 (3)2.1基本理论 (3)2.2概要设计 (3)第3章详细设计 (4)3.1建立二叉树 (4)3.2二叉树的层次遍历和中序遍历 (4)3.3求二叉树的深度 (7)3.4将二叉树中所有结点的左右子树相互交换 (7)3.5求二叉树中叶子结点的数目 (9)第4章流程分析图 (11)4.1流程图 (11)4.2主要功能模块设计 (11)4.3模块设计 (12)4.4函数主要调用关系图 (13)第5章系统测试 (14)5.1调试分析 (14)5.2实验结果 (15)结论 (17)致谢 (18)参考文献 (18)第1章绪论引言:树型结构是一类重要的非线性数据结构,其中一树和二叉树最重要。
树结构在客观世界中广泛存在,如人类社会的族谱和各种社会组织机构够可以用树来形象表示。
树在计算机领域中也得到了广泛应用,如在编译程序中,可以用树来表示源程序的语法结构。
二叉树是一种非线性数据结构,对它进行操作时总需要对每个数据逐一进行操作,这样就存在一个操作顺序问题,由此提出了二叉树的遍历操作问题,所谓遍历二叉树就是按某种顺序访问二叉树中某个结点一次且仅一次的过程,这里的访问可以是输出.比较.更新.查看元素内容等各种操作。
1.1 设计目的1.掌握二叉树结点结构的建立。
2.掌握先序、中序和后序遍历的基本操作。
1.2 设计内容利用二叉树特点和功能实现先序、中序和后序遍历系统的实现,具体功能:输入、输出遍历结果、先序遍历、中序遍历和后序遍历,并能在屏幕上输出操作前后的结果。
1.3 设计要求1.写出系统需求分析,并建模。
2.编程实现,界面友好。
3.输出操作前后的结果。
4.提供测试报告。
1.4 设计思想1.建立二叉树采用一个一个输入的方式。
2.对二叉树进中序遍历采用递归函数和非递归函数分别实现多种遍历的方式。
另外还有层次遍历,来充分实现本书对树的遍历。
3.删除结点函数,采用边查找边删除的方式。
如果没有查找到,则不对树做任何的修改;如果查找到结点则删除。
1.5 系统模块划分1.二叉树是一种动态树表。
2.开辟一个空间建立一个节点,逐个加入,逐个建立。
3.利用查找函数,对数进行插入删除。
4.利用书中所学知识进行各种遍历,包括将所有方法归并在一起,还要建立查看界面一边有系统的视觉效果。
1.6 主要功能模块设计程序主要设计了几个功能:首先是创建二叉排序树,完成后出现任务菜单,菜单中设计了八个模块:树状输出二叉树,前序遍历二叉树,中序遍历二叉树,后序遍历二叉树,输出叶子结点,输出叶子结点个数,输出二叉树的深度,退出。
第2章系统总体设计2.1 基本理论(1)建立二叉树的操作就是用递归算法以先序遍历的次序从根结点开始建立一棵二叉树。