数据结构与程序设计(王丽苹)24二叉树.ppt
《数据结构与程序设计(王丽苹)24二叉树.ppt》由会员分享,可在线阅读,更多相关《数据结构与程序设计(王丽苹)24二叉树.ppt(39页珍藏版)》请在三一办公上搜索。
1、5/27/2023,数据结构与程序设计,1,数据结构与程序设计(24)Chapter Binary Tree 二叉树,王丽苹,5/27/2023,数据结构与程序设计,2,Tree,树tree的定义(1)无结点的树 空树(2)非空树 仅有一个根结点 其余结点分为若干 互不相交的子树在树形结构中每个结点最多只有一个前驱,但可有多个后继的结构。它表示了一种具有层次的分支关系。,5/27/2023,数据结构与程序设计,3,subtree,Tree,Root(根):node without parent(A)Siblings(兄弟):nodes share the same parentInternal
2、 node:node with at least one child(A,B,C,F)External node(leaf):node without children(E,I,J,K,G,H,D)Ancestors of a node:parent,grandparent,grand-grandparent,etc.Descendant of a node:child,grandchild,grand-grandchild,etc.Depth of a node:number of ancestorsHeight of a tree:maximum depth of any node(3)D
3、egree of a node:the number of its childrenDegree of a tree:the maximum number of its node.,Subtree:tree consisting of a node and its descendants,5/27/2023,数据结构与程序设计,4,Tree Properties,PropertyValueNumber of nodes 9Height 4 Root Node ALeaves Interior nodesAncestors of H(G,E,B,A)Descendants of BSibling
4、s of ERight subtree of ADegree of this tree,5/27/2023,数据结构与程序设计,5,Chapter 10 Binary Tree,DEFINITION A binary tree is either empty,or it consists of a node called the root(根)together with two binary trees called the left subtree(左子树)and the right subtree(右子树)of the root.,5/27/2023,数据结构与程序设计,6,二叉树(Bin
5、ary Tree),二叉树的定义,二叉树的五种不同形态,一棵二叉树是结点的一个有限集合,该集合或者为空,或者是由一个根结点加上两棵分别称为左子树和右子树的、互不相交的二叉树组成。,5/27/2023,数据结构与程序设计,7,深度(Height)为k,结点至多2k+1-1个,深度为最大层数,第i层结点至多2i个 在本书,层数从0开始计算。,设有n2个2度点则有n2+1个叶片,A,B,C,D,E,F,G,H,I,J,5/27/2023,数据结构与程序设计,8,Binary Tree,满二叉树:如果一棵二叉树的深度为k,结点数2k+1-1,每层结点数都最大,则此二叉树称作满二叉树。完全二叉树:如果一
6、棵二叉树至多只有最下面的两层结点度数可以小于2,其余各层结点度数都必须为2,并且最下面一层的结点都集中在该层最左边的若干位置上,则此二叉树称为完全二叉树。即为,满二叉树去掉最下层最右边若干结点,5/27/2023,数据结构与程序设计,9,Binary Tree,5/27/2023,数据结构与程序设计,10,A,B,C,D,E,F,G,H,I,J,完全二叉树,K,L,M,n个结点,深度 k=log2(n)下取整,n个结点,深度为k:2k-1n2k+1-12k n 2k+1k log2n k+1k=log2(n)下取整,1个结点深度为0,2-3个结点深度为1,4-7个结点深度为2,8-15个结点深
7、度为3,5/27/2023,数据结构与程序设计,11,0,1,2,3,4,5,6,7,8,9,完全二叉树,10,11,12,结点i的左子结点是2i+1 右子结点是2i+2,结点i的父结点是(i-1)/2堆排序利用了这种特点。,5/27/2023,数据结构与程序设计,12,10.1.2 Traversal of Binary Trees,With preorder traversal(前序)we first visit a node,then traverse its left subtree,and then traverse its right subtree.With inorder tr
8、aversal(中序)we first traverse the left subtree,then visit the node,and then traverse its right subtree.With postorder traversal(后序)we first traverse the left subtree,then traverse the right subtree,and finally visit the node.,5/27/2023,数据结构与程序设计,13,写出这棵树的前序,后序,中序访问的结果,5/27/2023,数据结构与程序设计,14,前序遍历(Preo
9、rder Traversal),前序遍历二叉树算法的框架是若二叉树为空,则空操作;否则访问根结点(V);前序遍历左子树(L);前序遍历右子树(R)。遍历结果-+a*b-c d/e f,5/27/2023,数据结构与程序设计,15,中序遍历二叉树算法的框架是:若二叉树为空,则空操作;否则中序遍历左子树(L);访问根结点(V);中序遍历右子树(R)。遍历结果 a+b*c-d-e/f,中序遍历(Inorder Traversal),表达式语法树,5/27/2023,数据结构与程序设计,16,后序遍历(Postorder Traversal),后序遍历二叉树算法的框架是若二叉树为空,则空操作;否则后序
10、遍历左子树(L);后序遍历右子树(R);访问根结点(V)。遍历结果a b c d-*+e f/-,5/27/2023,数据结构与程序设计,17,完全二叉树的数组表示 一般二叉树的数组表示,二叉树的表示 1.数组表示,5/27/2023,数据结构与程序设计,18,二叉树的表示2.链表表示,5/27/2023,数据结构与程序设计,19,10.1.3 二叉树的链式实现,5/27/2023,数据结构与程序设计,20,Implement of Binary Tree链式实现方式-节点的数据结构,template struct Binary_node/data members:Entry data;Bin
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 程序设计 王丽苹 24 二叉

链接地址:https://www.31ppt.com/p-4980259.html