分享
分享赚钱 收藏 举报 版权申诉 / 23

类型九-树.ppt

  • 上传人:HR专家
  • 文档编号:7435841
  • 上传时间:2019-05-17
  • 格式:PPT
  • 页数:23
  • 大小:238KB
  • 配套讲稿:

    如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。

    特殊限制:

    部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。

    关 键  词:
    九-树.ppt
    资源描述:

    1、第1页,We have already discussed arrays, stacks, queues and linked lists which are called linear data structures. The reason for this is that the elements are arranged in a linear fashion (i.e., one-dimensional representation) In this chapter, we are going to discuss a very useful data structure call t

    2、ree. A tree is called non- linear data structure because the elements of this data structure are arranged in a non-linear fashion i.e., they use two-dimensional representation.,trees,第2页,Familiar examples of trees are genealogies(家谱) and organization charts .trees are also used to organize informati

    3、on in database system and to represent the syntactic(句法) structure of source programs in compilers.Advantages of this data structure is that insertion. deletion and searching ,etc., are more efficient than linear data structures.,第3页,definition,A tree is a collection called nodes, one of which is de

    4、signated as root along with a relation that places a hierarchical structure on the nodes.,第4页,Terminologies used,Node This is the main component of any tree structure. each individual element of a tree can be called a node. Parent The parent of a node is the immediate predecessor of that node. Child

    5、 The immediate successors of a node are called child nodes, the left child and the right child. Link( branch and edge) A link is a pointer to a node in the tree. A node can have more than one link.,第5页,RootA root is a specially designated node in a tree. it is a node which has no parent. there can b

    6、e only one root node in a tree. Patheach node has to be reachable from the root through a unique sequence of arcs or links called a path.length of the path: the number of links in a path is called the length of the path. Leaf nodes Leaf node is a node which does not have any child node. Level The le

    7、vel of a node in the tree is the rank of the hierarchy. Height The height of a non-empty tree is the maximum level of a node in the tree.,第6页,Degree(arity)The maximum number of children that is possible for a node is known as the degree of a node . Siblings(兄弟)Nodes which have the same parent are ca

    8、lled siblings .,第7页,6.1 树的概念与表示,6.1.1 树的定义 树(Tree)是n个(n0)个结点组成的有限集合T,当T为空集时称为空树,否则它满足如下两个条件:(1) 有且仅有一个特定的称为根(Root)的结点。,(2) 除根结点外的其余结点可分为 m(m0)个互不相交的子集 T1,T2,Tm,其中每个子集Ti本身又是一棵树,并称其为根的子树。,第8页,树具有下面两个特点:(1)树的根结点没有前驱结点,除根结点之外的所有结点有且只有一个前驱结点。(2)树中所有结点可以有零个或多个后继结点。,第9页,6.1.2 树的相关术语结点:一个数据元素及若干个指向其子树的分支。结点

    9、的度:结点所拥有的子树的个数称为该结点的度。树的度:树中结点度的最大值。,叶子结点:度为0的结点称为叶子结点,或者称为终端结点。分支结点:度不为0的结点称为分支结点或非终端结点。,孩子、双亲和兄弟结点:树中结点子树的根称为该结点的孩子。相应地,该结点称为孩子结点的双亲。,第10页,具有同一个双亲的孩子结点互称为兄弟。祖先、子孙:从树中根结点到某一结点所经过的分支上的所有结点称为该结点的祖先。反之,以某结点为根的子树中的任一结点都称为该结点的子孙。 结点的层数:规定树的根结点的层数为1,其余结点的层数等于它的双亲结点的层数加1。树的深度:树中所有结点的最大层数称为树的深度或高度。,有序树和无序树

    10、:如果一棵树中结点的各子树从左到右是有次序的有序树;反之,无序树。,森林:m(m0)棵互不相交的树的集合称为森林。,第11页,6.1.3 树的表示树的表示有四种方法,直观表示法、嵌套集合表示法、凹入表示法、广义表表示法。,(A(B(D,E(H),F),C(G(I) 广义表表示法,第12页,Binary treeA binary tree T is a finite set of nodes such that1)T is empty (called empty binary tree),or 2)T contains a specially designated node called the

    11、 root of T, and the remaining nodes of T form two disjoint binary trees which are called left sub-tree and right sub-tree respectively.,第13页,binary tree,Difference between a general tree and a binary tree1) A tree can never be empty but a binary tree may be empty. 2) In the case of a binary tree, a

    12、node can have a maximum of two children whereas in the case of a general tree, a node can have any number of children. A binary tree can be any of the following: 1)Full binary tree 2)Complete binary tree,第14页,binary tree,A binary tree can be any of the following : Full binary treeA binary tree is a

    13、full binary tree if it contains maximum number of nodes in all level. complete binary treeif all its levels except possibly the last level have the maximum number of nodes and all nodes at the last level appear as far left as possible.,第15页,6.2 二叉树的概念与性质,6.2.1 二叉树的基本概念 定义:二叉树是n个(n0)结点的集合,该集合或者为空、或者由

    14、一个根结点和两个分别称为左子树和右子树的互不相交的二叉树组成,当集合为空时,称该二叉树为空二叉树。二叉树具有五种基本形态:空二叉树、仅有根结点的二叉树、右子树为空的二叉树、左子树为空的二叉树和左右子树均不为空的二叉树。,第16页,6.2.2 二叉树的重要性质 二叉树有以下重要性质。性质1 二叉树的第i(i1)层上最多有2 i -1个结点。对非空二叉树,第一层为根结点,最多只有一个结点,即20个;显然第二层最多有21个;以此类推,第 i 层上最多只有2i-1个结点。性质2 在一棵深度为k的二叉树中,最多具有2 k1个结点。,第17页,性质2 在一棵深度为k的二叉树中,最多具有2 k1个结点。 证

    15、明:由性质1可知,第i层的结点数最多为2 i-1(1ik),则深度为k的二叉树的结点数最多为:,性质3 对于一棵非空的二叉树,如果叶子结点数为n0,度数为2的结点数为n2,则有: n0n21。,第18页,证明:设度为i的结点有ni 个(其中:i=0,1,2),总结点个数为 n,总分支数为e,则根据二叉树的定义,有如下的式子: n = n0 + n1 + n2 e = 2n2 + n1 = n 1 ? 除了根节点,其余都有一个分支 将两式结合有: 2n2 + n1 = n0 + n1 + n2 - 1即有 n0 = n2 + 1,一棵深度为k且有2 k1个结点的二叉树称为满二叉树。 当深度为k,

    16、含有n个结点的二叉树,当且仅当其每个结点的编号与相应满二叉树结点顺序编号从1n互相对应时,则称此二叉树为完全二叉树。,第19页,(c) 非完全二叉树,第20页,6.3 二叉树的存储结构,二叉树有两种存储结构:顺序存储结构(linear representation)和链式存储结构(link representation) 。 6.3.1 顺序存储结构二叉树的顺序存储,就是把所有结点按照一定的顺序存放到一组连续的存储单元中。一般是按照完全二叉树中结点编号将结点自上而下、自左至右的顺序存储。可以看出,数组下标和结点的编号相对应,即结点在数组中的位置隐含了结点之间的关系。,数组下标,第21页,6.3

    17、.2 链式存储结构二叉树的链式存储结构是指用链表的形式来存储二叉树,即用指针来指示元素之间的逻辑关系。最常用的有二叉链表和三叉链表。1. 二叉链表存储每个结点由三个域组成,一个数据域和两个指针域。两个指针lchild和rchild分别指向其左孩子和右孩子结点。,结点结构描述为: typedef struct BiTNodeElemType data; /*数据域*/struct BiTNode *lchild, *rchild; /*左右指针域*/BTNode;,第22页,2. 三叉链表存储每个结点由四个域组成,具体结构如图6-6所示:,其中data、lchild以及rchild三个域的意义同二叉链表结构相同,parent为指向该结点双亲结点的指针。这种存储结构既便于查找孩子结点,又便于查找双亲结点;但是,相对于二叉链表存储结构而言,它增加了空间开销。typedef struct BiTNodeElemType data; /*数据域*/struct BiTNode *lchild, *rchild,*parent; /*左、右及双亲指针域*/BTNode_p;,第23页,二叉树T的二叉链表表示,其中bt是一个*BTNode类型的变量;三叉链表表示,其中bt_p是*BTNode_p类型的变量。,二叉树,bt,二叉链表,

    展开阅读全文
    提示  道客多多所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。
    关于本文
    本文标题:九-树.ppt
    链接地址:https://www.docduoduo.com/p-7435841.html
    关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

    道客多多用户QQ群:832276834  微博官方号:道客多多官方   知乎号:道客多多

    Copyright© 2025 道客多多 docduoduo.com 网站版权所有世界地图

    经营许可证编号:粤ICP备2021046453号    营业执照商标

    1.png 2.png 3.png 4.png 5.png 6.png 7.png 8.png 9.png 10.png



    收起
    展开