1 6 Non-Binary Trees. Contents 6.1 General Tree Definitions and Terminology 6.2 The Parent Pointer Implementation 6.3 General Tree Implementations 6.4.

Slides:



Advertisements
Similar presentations
首 页 首 页 上一页 下一页 本讲内容 投影法概述三视图形成及其投影规律平面立体三视图、尺寸标注 本讲内容 复习: P25~P31 、 P84~P85 作业: P7, P8, P14[2-32(2) A3 (1:1)]
Advertisements

纺纱学. 2 绪 论 基本要求:了解纺纱系统的类别 重点掌握:棉纺系统的工艺流程 3 一、纺纱原理与设备 纺纱:用物理或机械的方法将纺织纤维纺成纱 线的过程。 纺纱原理:初加工、原料的选配、开松除杂、 混和、梳理、精梳、并合、牵伸、加捻、卷绕等。 纺纱方法:传统纺纱方法、新型纺纱方法。 纺纱设备:开清棉联合机、梳棉机、精梳机、
第十二章 常微分方程 返回. 一、主要内容 基本概念 一阶方程 类 型 1. 直接积分法 2. 可分离变量 3. 齐次方程 4. 可化为齐次 方程 5. 全微分方程 6. 线性方程 类 型 1. 直接积分法 2. 可分离变量 3. 齐次方程 4. 可化为齐次 方程 5. 全微分方程 6. 线性方程.
概率统计( ZYH ) 节目录 2.1 随机变量与分布函数 2.2 离散型随机变量的概率分布 2.3 连续型随机变量的概率分布 第二章 随机变量及其分布.
在近年的高考地理试题中,考查地球上 两点间最短航线的方向问题经常出现,由于 很多学生对这类问题没有从本质上搞清楚, 又缺乏空间想象能力,只是机械地背一些结 论,造成解这类题目时经常出错。 地球上两点间的最短航线方向问题.
概率统计( ZYH ) 节目录 3.1 二维随机变量的概率分布 3.2 边缘分布 3.4 随机变量的独立性 第三章 随机向量及其分布 3.3 条件分布.
空间群 space groups 晶轴和直角坐标轴
基本知识和几何要素的投影 模块一: 字体练习 第一章 制图的基本知识与基本技能 题目提示返回.
数据挖掘实验 1 Apriori 算法编程实现. 数据挖掘实验一 (20’) 实验目的:了解关联规则在数据挖掘中的 应用,理解和掌握关联挖掘的经典算法 Apriori 算法的基本原理和执行过程并完成程 序设计。 实验内容:对给定数据集用 Apriori 算法进行 挖掘,找出其中的频繁集并生成关联规则。
实验:验证牛顿第二定律. 1 、实验目的:探究 a 与 F 、 m 的定量关系 2 、实验原理:控制变量法 A 、 m 一定时,探究 a 随 F 的变化关系 B 、 F 一定时, 探究 a 随 m 的变化关系.
细分曲面 傅孝明 SA 目录 细分曲面的基本思想 两个关键问题 一些基本概念 几种简单的细分曲面算法 细分曲面方法分类.
一、拟合优度检验 二、变量的显著性检验 三、参数的置信区间
第二章 质点组力学 质点组:许多(有限或无限)相 互联系的质点组成的系统 研究方法: 1. 分离体法 2. 从整体考虑 把质点的三个定理推广到质点组.
吉林大学远程教育课件 主讲人 : 杨凤杰学 时: 64 ( 第六十二讲 ) 离散数学. 最后,我们构造能识别 A 的 Kleene 闭包 A* 的自动机 M A* =(S A* , I , f A* , s A* , F A* ) , 令 S A* 包括所有的 S A 的状态以及一个 附加的状态 s.
1 为了更好的揭示随机现象的规律性并 利用数学工具描述其规律, 有必要引入随 机变量来描述随机试验的不同结果 例 电话总机某段时间内接到的电话次数, 可用一个变量 X 来描述 例 检测一件产品可能出现的两个结果, 也可以用一个变量来描述 第五章 随机变量及其分布函数.
数 学 系 University of Science and Technology of China DEPARTMENT OF MATHEMATICS 第 3 章 曲线拟合的最小二乘法 给出一组离散点,确定一个函数逼近原函数,插值是这样 的一种手段。在实际中,数据不可避免的会有误差,插值函 数会将这些误差也包括在内。
例9:例9: 第 n-1 行( -1 )倍加到第 n 行上,第( n-2 ) 行( -1 )倍加到第 n-1 行上,以此类推, 直到第 1 行( -1 )倍加到第 2 行上。
主讲教师:陈殿友 总课时: 124 第八讲 函数的极限. 第一章 机动 目录 上页 下页 返回 结束 § 3 函数的极限 在上一节我们学习数列的极限,数列 {x n } 可看作自变量 为 n 的函数: x n =f(n),n ∈ N +, 所以,数列 {x n } 的极限为 a, 就是 当自变量 n.
吉林大学远程教育课件 主讲人 : 杨凤杰学 时: 64 ( 第三十八讲 ) 离散数学. 第八章 格与布尔代数 §8.1 引 言 在第一章中我们介绍了关于集 合的理论。如果将 ρ ( S )看做 是集合 S 的所有子集组成的集合, 于是, ρ ( S )中两个集合的并 集 A ∪ B ,两个集合的交集.
吉林大学远程教育课件 主讲人 : 杨凤杰学 时: 64 ( 第四十八讲 ) 离散数学. 例 设 S 是一个非空集合, ρ ( s )是 S 的幂集合。 不难证明 :(ρ(S),∩, ∪,ˉ, ,S) 是一个布尔代数。 其中: A∩B 表示 A , B 的交集; A ∪ B 表示 A ,
第十一章 曲线回归 第一节 曲线的类型与特点 第二节 曲线方程的配置 第三节 多项式回归.
线性代数习题课 吉林大学 术洪亮 第一讲 行 列 式 前面我们已经学习了关 于行列式的概念和一些基本 理论,其主要内容可概括为:
吉林大学远程教育课件 主讲人 : 杨凤杰学 时: 64 ( 第二十五讲 ) 离散数学. 定理 群定义中的条件 ( 1 )和( 2 )可以减弱如下: ( 1 ) ’ G 中有一个元素左壹适合 1 · a=a; ( 2 ) ’ 对于任意 a ,有一个元素左逆 a -1 适 合 a -1 ·
Linux 内核源代码导读 中国科学技术大学计算机系 陈香兰( 0551 - ) Spring 2009.
数 学 系 University of Science and Technology of China DEPARTMENT OF MATHEMATICS 第 3 章 曲线拟合的最小二乘法 给出一组离散点,确定一个函数逼近原函数,插值是这样的一种手段。 在实际中,数据不可避免的会有误差,插值函数会将这些误差也包括在内。
吉林大学远程教育课件 主讲人 : 杨凤杰学 时: 64 ( 第三十九讲 ) 离散数学. 例 设 S 是一个集合, ρ ( S )是 S 的幂集合,集合 的交( ∩ ),并(∪)是 ρ ( S )上的两个代数运算, 于是,( ρ ( S ), ∩ ,∪) 是一个格。而由例 知.
吉林大学远程教育课件 主讲人 : 杨凤杰学 时: 64 ( 第四十五讲 ) 离散数学 模 格 定义 设( L , ≤ ) 是一个格,对任意 a , b , c ∈ L , 如果 a≤b ,都有 a  ( b×c ) = b× ( a  c ) 则称( L , ≤ )为模格。
第二章 贝叶斯决策理论 3学时.
非均相物系的分离 沉降速度 球形颗粒的 :一、自由沉降 二、沉降速度的计算 三、直径计算 1. 试差法 2. 摩擦数群法 四、非球形颗粒的自由沉降 1. 当量直径 de :与颗粒体积相等的圆球直径 V P — 颗粒的实际体积 2. 球形度  s : S—— 与颗粒实际体积相等的球形表面积.
量子化学 第四章 角动量与自旋 (Angular momentum and spin) 4.1 动量算符 4.2 角动量阶梯算符方法
数 学 系 University of Science and Technology of China DEPARTMENT OF MATHEMATICS 第 5 章 解线性方程组的直接法 实际中,存在大量的解线性方程组的问题。很多数值方 法到最后也会涉及到线性方程组的求解问题:如样条插值的 M 和.
主讲教师:陈殿友 总课时: 124 第十一讲 极限的运算法则. 第一章 二、 极限的四则运算法则 三、 复合函数的极限运算法则 一 、无穷小运算法则 机动 目录 上页 下页 返回 结束 §5 极限运算法则.
在发明中学习 线性代数 概念的引入 李尚志 中国科学技术大学. 随风潜入夜 : 知识的引入 之一、线性方程组的解法 加减消去法  方程的线性组合  原方程组的解是新方程的解 是否有 “ 增根 ” ?  互为线性组合 : 等价变形  初等变换  高斯消去法.
第一节 相图基本知识 1 三元相图的主要特点 (1)是立体图形,主要由曲面构成; (2)可发生四相平衡转变; (3)一、二、三相区为一空间。
9的乘法口诀 1 .把口诀说完全。 二八( ) 四六( ) 五八( ) 六八( ) 三七( ) 三八( ) 六七( ) 五七( ) 五六( ) 十六 四十八 四十二 二十四 二十一 三十五 四十 二十四 三十 2 .口算, 并说出用的是哪句口诀。 8×8= 4×6= 7×5= 6×8= 5×8=
最 小 公 倍 数最 小 公 倍 数 最 小 公 倍 数最 小 公 倍 数. 例题 顺次写出 4 的几个倍数和 6 的几个倍数,它们 公有的倍数是哪几个?其中最小的是多少? 4 的倍数有 : 4 , 8 , 12 , 16 , 20 , 24 , 28 , 32 , 36 , … 6 的倍数有 :
第 3 章 控制流分析 内容概述 – 定义一个函数式编程语言,变量可以指称函数 – 以 dynamic dispatch problem 为例(作为参数的 函数被调用时,究竟执行的是哪个函数) – 规范该控制流分析问题,定义什么是可接受的控 制流分析 – 定义可接受分析在语义模型上的可靠性 – 讨论分析算法.
吉林大学远程教育课件 主讲人 : 杨凤杰学 时: 64 ( 第五十三讲 ) 离散数学. 定义 设 G= ( V , T , S , P ) 是一个语法结构,由 G 产生的语言 (或者说 G 的语言)是由初始状态 S 演绎出来的所有终止符的集合, 记为 L ( G ) ={w  T *
平行线的平行公理与判定 九年制义务教育七年级几何 制作者:赵宁睿. 平行线的平行公理与判定 要点回顾 课堂练习 例题解析 课业小结 平行公理 平行判定.
卫生学(第 7 版) · 第十二章 直线相关与回归 1 直线相关与回归 第十一章. 卫生学(第 7 版) · 第十二章 直线相关与回归 2 主要内容 直线相关 直线回归 直线相关与回归的区别与联系 等级相关.
主要学习内容: 1、用描述法分析样品的风味特征 2 、学习味觉试验方法。 闽北职业技术学院食品与生物工程系.
§8-3 电 场 强 度 一、电场 近代物理证明:电场是一种物质。它具有能量、 动量、质量。 电荷 电场 电荷 电场对外的表现 : 1) 电场中的电荷要受到电场力的作用 ; 2) 电场力可移动电荷作功.
 符号表  标识符的作用: 声明部分:定义了各种对象及对应的属性和 使用规则。 程序体:对所定义的对象进行各种操作。 $ididname IdnameAttributeIR  必要性 Token : 新表-符号表(种类、类型等信息):
习题课( 1 ) 进程管理及调度. 复习 进程概念、描述及状态 进程概念、描述及状态 进程的同步与互斥及应用 进程的同步与互斥及应用 管程机制 管程机制 进程通信 进程通信 进程调度算法 进程调度算法 进程死锁 进程死锁 线程 线程.
Department of Mathematics 第二章 解析函数 第一节 解析函数的概念 与 C-R 条件 第二节 初等解析函数 第三节 初等多值函数.
首 页 首 页 上一页 下一页 本讲内容本讲内容 视图,剖视图(Ⅰ) 复习: P107 ~ P115 作业: P48(6-2,6-4), P49( 去 6-6) P50, P51(6-13), P52 P50, P51(6-13), P52 P53 (6-18,6-20) P53 (6-18,6-20)
1-4 节习题课 山东省淄博第一中学 物理组 阚方海. 2 、位移公式: 1 、速度公式: v = v 0 +at 匀变速直线运动规律: 4 、平均速度: 匀变速直线运动 矢量式 要规定正方向 统一单位 五个量知道了三 个量,就能求出 其余两个量 3 、位移与速度关系:
《 UML 分析与设计》 交互概述图 授课人:唐一韬. 知 识 图 谱知 识 图 谱知 识 图 谱知 识 图 谱.
Introduction to Automatic Control The Laplace Transform Li Huifeng Tel:
General Trees CS 400/600 – Data Structures. General Trees2.
Chapter 8 Algorithms. Understand the concept of an algorithm. Define and use the three constructs for developing algorithms: sequence, decision, and repetition.
1 、如果 x + 5 > 4 ,那么两边都 可得 x >- 1 2 、在- 3y >- 4 的两边都乘以 7 可得 3 、在不等式 — x≤5 的两边都乘以- 1 可得 4 、将- 7x — 6 < 8 移项可得 。 5 、将 5 + a >- 2 a 移项可得 。 6 、将- 8x < 0.
名探柯南在侦查一个特大盗窃集团过程 中,获得藏有宝物的密码箱,密码究竟 是什么呢?请看信息: ABCDEF( 每个字 母表示一个数字 ) A :是所有自然数的因数 B :既有因数 5 ,又是 5 的倍数 C :既是偶数又是质数 D :既是奇数又是合数 EF :是 2 、 3 、 5 的最小公倍数.
项目七: PLC 功能指令应用 带进位循环左移指令 XXXXX. 项目七: PLC 功能指令应用 FX2 系列可编程控制器移位控制指令有移位、循环移位、字移位 及先进先出 FIFO 指令等 10 条指令。 带进位循环右移指令 RCR 带进位循环左移指令 RCL 字右移位指令 WSFR 先入先出读出指令.
1 物体转动惯量的测量 南昌大学理学院
§10.2 对偶空间 一、对偶空间与对偶基 二、对偶空间的有关结果 三、例题讲析.
表单自定义 “ 表单自定义 ” 功能是用于制作表单的 工具,用数飞 OA 提供的表单自定义 功能能够快速制作出内容丰富、格 式规范、美观的表单。
7 生产费用在完工产品与在产 品之间分配的核算. 2 第七章 生产费用在完工产品与在产品之 间的分配  知识点 :  理解在产品的概念  掌握生产费用在完工产品与在产品之间的分 配.
力的合成 力的合成 一、力的合成 二、力的平行四边形 上一页下一页 目 录 退 出. 一、力的合成 O. O. 1. 合力与分力 我们常常用 一个力来代替几个力。如果这个 力单独作用在物体上的效果与原 来几个力共同作用在物体上的效 果完全一样,那么,这一个力就 叫做那几个力的合力,而那几个 力就是这个力的分力。
第四章 不定积分. 二、 第二类换元积分法 一、 第一类换元积分法 4.2 换元积分法 第二类换元法 第一类换元法 基本思路 设 可导, 则有.
CSCI 333 Data Structures Chapter 6 30 September and 2 and 4 October 2002.
个体 精子 卵细胞 父亲 受精卵 母亲 人类生活史 问题:人类产生配子(精、卵 细胞)是不是有丝分裂?
逻辑设计基础 1 第 7 章 多级与(或)非门电路 逻辑设计基础 多级门电路.
General Trees A tree T is a finite set of one or more nodes such that there is one designated node r called the root of T, and the remaining nodes in (T-{r})
八. 真核生物的转录 ㈠ 特点 ① 转录单元为单顺反子( single cistron ),每 个蛋白质基因都有自身的启动子,从而造成在功能 上相关而又独立的基因之间具有更复杂的调控系统。 ② RNA 聚合酶的高度分工,由 3 种不同的酶催化转 录不同的 RNA 。 ③ 需要基本转录因子与转录调控因子的参与,这.
人 有 悲 欢 离 合, 月有阴晴圆缺。月有阴晴圆缺。 华师大版七年级数学第二册 海口市第十中学 数学组 吴锐.
1 第三章 数列 数列的概念 考点 搜索 ●数列的概念 ●数列通项公式的求解方法 ●用函数的观点理解数列 高考 猜想 以递推数列、新情境下的 数列为载体, 重点考查数列的通 项及性质, 是近年来高考的热点, 也是考题难点之所在.
目录 上页 下页 返回 结束 二、无界函数反常积分的审敛法 * 第五节 反常积分 无穷限的反常积分 无界函数的反常积分 一、无穷限反常积分的审敛法 反常积分的审敛法  函数 第五章 第五章.
本章讨论有限自由度结构系统,在给定载荷和初始条件激励下的系统动力响应计算方法。 第 六 章
§7.2 估计量的评价标准 上一节我们看到,对于总体 X 的同一个 未知参数,由于采用的估计方法不同,可 能会产生多个不同的估计量.这就提出一 个问题,当总体的一个参数存在不同的估 计量时,究竟采用哪一个好呢?或者说怎 样评价一个估计量的统计性能呢?下面给 出几个常用的评价准则. 一.无偏性.
Presentation transcript:

1 6 Non-Binary Trees

Contents 6.1 General Tree Definitions and Terminology 6.2 The Parent Pointer Implementation 6.3 General Tree Implementations 6.4 K-ary Trees 6.5 Sequential Tree Implementations

Contents 6.1 General Tree Definitions and Terminology 6.2 The Parent Pointer Implementation 6.3 General Tree Implementations 6.4 K-ary Trees 6.5 Sequential Tree Implementations

4 6.1 General Tree Definitions and Terminology

General Trees A Tree is a finite set of n (n>0) nodes such that One and only one node R, is called the root of T. The remaining nodes are partitioned into m(m  0) disjoint subsets T 0, T 1 …..T m-1, each of which is a tree, and whose roots R 0, R 1 …..R m-1, respectively, are children of R. The subsets T i (0  i<m) are said to be subtrees of T. 6.1 General Tree Definitions and Terminology

General Trees 6.1 General Tree Definitions and Terminology

General Trees A node’s out degree is the number of children for that node. –Out degree of node D is 3. A forest is a collection of one or more trees. A BCD EFGHIJ MKL 6.1 General Tree Definitions and Terminology

General Tree Node // General tree node ADT template class GTNode { public: E value(); // Return value bool isLeaf(); // TRUE if is a leaf GTNode* parent(); // Return parent GTNode* leftmostChild(); // First child GTNode* rightSibling(); // Right sibling void setValue(E&); // Set value void insertFirst(GTNode *); void insertNext(GTNode *); void removeFirst(); // Remove first child void removeNext(); // Remove sibling }; 6.1 General Tree Definitions and Terminology

General Tree ADT // General tree ADT template class GenTree { public: void clear(); // Send nodes to free store GTNode * root(); // Return the root //Combine two subtrees void newroot(E&, GTNode *, GTNode *); void print(); // Print a tree }; 6.1 General Tree Definitions and Terminology

General Tree Traversal Preorder: First visit the root of the tree, then performs a preorder traversal of each subtree from left to right; Postorder: First performs a postorder traversal of the root’s subtrees from left to right, then visit the root; Inorder traversal does not have a natural defination. 6.1 General Tree Definitions and Terminology

A B C D E F G H I J K A B E F C D G H I J K E F B C I J K H G D A A B C D E F G H I J K Preorder traversals Level traversals Postorder traversals 6.1 General Tree Definitions and Terminology

General Tree Traversal //Print using a preorder traversal void printhelp(GTNode * root) { // 访问根 if (root->isLeaf()) cout << "Leaf: "; else cout << "Internal: "; cout value() << "\n"; // 从左到右访问各子树 for (GTNode * temp =root->leftmostChild(); temp != NULL; temp = temp->rightSibling()) printhelp(temp); } 6.1 General Tree Definitions and Terminology

Contents 6.1 General Tree Definitions and Terminology 6.2 The Parent Pointer Implementation 6.3 General Tree Implementations 6.4 K-ary Trees 6.5 Sequential Tree Implementations

The Parent Pointer Implementation

Parent Pointer Implementation 6.2 The Parent Pointer Implementation

Parent Pointer Implementation Advantages: Can answer the following question easily: Given two or more nodes, are they in the same tree? Disadvantages: It is inadequate for such important operations as finding the leftmost child or the right sibling for a node. 6.2 The Parent Pointer Implementation

ADT for parent pointer implementation Class ParPtrTree{ Private: int* array; int size: int FIND(int) const;//Find root Public: ParPtrTree(int); ~ParPtrTree(){delete[] array;} void UNION(int,int)//Merge equivalences bool differ(int,int)//TRUE if not in same tree }; 6.2 The Parent Pointer Implementation

Implementation of Find() int ParPtrTree::FIND(int curr) const{ while (array[curr]!=ROOT) curr = array[curr]; return curr; } 假设根的双亲值为 -1 , ROOT=-1. e.g. FIND(4) curr: 4->1->0 6.2 The Parent Pointer Implementation

Implementation of differ() The parent pointer representation is good for answering: –Are two elements in the same tree? // Return TRUE if nodes in different trees bool ParPtrTree::differ(int a, int b) { int root1 = FIND(a); // Find root for a int root2 = FIND(b); // Find root for b return root1 != root2; // Compare roots } 6.2 The Parent Pointer Implementation

Implementation of Union() //Merge two subtrees void ParPtrTree::UNION(int a, int b) { int root1 = FIND(a); // Find root for a int root2 = FIND(b); // Find root for b if (root1 != root2) array[root2] = root1; //root2 as a subtree of root1 } e.g. UNION(4,9) root1=0; root2=7; array[7]=0; 6.2 The Parent Pointer Implementation 0

Equivalence Classes( 并查集 ) Consider the problem of assigning the members of a set to disjoint subsets called equivalence classes. Examples of equivalence classes: –Connected components in graphs When two nodes are connected, they are said to be equivalent. –Point clustering 6.2 The Parent Pointer Implementation

Equiv Class Processing (1) (A, B), (C, H), (G, F), (D, E), and (I, F). (H, A) and (E, G) 6.2 The Parent Pointer Implementation

Equiv Class Processing (2) (H, E) 6.2 The Parent Pointer Implementation

Weighted union Keep the depth small. Weighted union rule: Join the tree with fewer nodes to the tree with more nodes by making the smaller tree’s root point to the root of the bigger tree. (A, B), (C, H), (G, F), (D, E), and (I, F). (H, A) and (E, G) 6.2 The Parent Pointer Implementation

Path Compression Path compression is a method that tends to create extremely shallow( 浅的 ) trees. –First, find the root of node X. Call this root R. –Reset the parent of every node on the path from X to R to point directly to R. Note: Path compression takes place during FIND. 6.2 The Parent Pointer Implementation

Path Compression int ParPtrTree::FIND(int curr) const { if (array[curr] == ROOT) return curr; array[curr] = FIND(array[curr]); return array[curr]; } //With path compression This FIND can: (1)returns the root of curr node, (2)makes all ancestors of curr node point to the root. e.g. UNION(7,4) 即合并 (H,E) 需要首先 FIND(7), FIND(4) The Parent Pointer Implementation

Path Compression e.g. UNION(7,4) (续) 最后,结点 0(A) 做为结点 5(F) 的孩子加入到 F 的子树中 。 6.2 The Parent Pointer Implementation

Application of Eqiv. Class(1) Problem 1: How many tables Today is Smith's birthday. He invites a lot of friends. Now it's dinner time. Smith wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want to stay with strangers. One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table. For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Smith needs 2 tables at least. 6.2 The Parent Pointer Implementation

Application of Eqiv. Class(2) Problem 2: 细胞统计 一矩形阵列由数字 0 到 9 组成,数字 1 到 9 代表细胞,细 胞的定义为沿细胞数字上下左右还是细胞数字则为 同一细胞,求给定矩形阵列的细胞个数。如阵列 : 有 4 个细胞。 The Parent Pointer Implementation

并查集实现 C(i-1,j) B(i,j-1)A(i,j) ◆ 逐行扫描,依次处理每一个点 ◆ 初始化:每个点的父亲指向本身 // 每个数是独立的一个细胞 ◆ 若点 A(i, j) 是细胞,分 3 种情况处理: (1)B 和 C 都是细胞,则合并 UNION(A, B), UNION(A, C) (2)B 是而 C 不是,则 UNION(A, B) (3)B 不是而 C 是,则 UNION(A, C) ◆ 统计父亲是自身 (find(i)=i) 的结点数即细胞的个数 6.2 The Parent Pointer Implementation

Application of Eqiv. Class(3) Problem 3: 房间问题 给出建筑平面图,计算: (1) 房间数量; (2) 最大的房间 有多大; (3) 拆除其中某一堵墙,以形成一个尽可能 大的房间。指出该墙。 该建筑分成m * n个方块( m≤50 , n≤50 ),每个方块 可有 0 ~ 4 堵墙( 0 表示无墙)。 6.2 The Parent Pointer Implementation

输入数据解释: ● 1 、 2 行分别是南北、东西方向的方块数。 ●后面的行中,一个数字表示一个方块,数字 P 描述 ( 0≤P≤15 )墙的情况。 P 是下面的可能取的数字之和: 1( 西墙 W) 、 2( 北墙 N) 、 4( 东墙 E) 、 8( 南墙 S) 室内的墙被定义两次: 例如方块 (1, 1) 中的南墙也被 位于其南面的方块 (2, 1) 定义了一次。 ●建筑中至少有两个房间。 样例输入: ,6,11,6,3,10,6 7,9,6,13,5,15,5 1,10,12,7,13,7,5 13,11,10,8,10,12,

算法分析: 类似细胞统计 第一问:房间数量; 逐行扫描,处理当前格子与上方和左方的格子。 第二问:最大的房间有多大; 父亲格子设置一个统计格子数量的变量,每扫描到 一个以当前格子为父亲的格子,数量加 1 。 第三问:拆除建筑中的某一堵墙,以形成一个尽可能大 的房间。指出该墙。 枚举每一堵墙。如果是竖直方向的墙:看左右是否 连通,如果连通(同父亲),拆除后不改变面积;不连 通则求左右两部分面积和,比较。如果水平的墙:看上 下两部分。

有 5 个房间; 最大房间的大小: 9 ; 拆除的墙: (3,3)(3,4) 之间的墙; 拆除后形成的房间面积为 16 输出: 5 9 (3,3)(3,4)

Contents 6.1 General Tree Definitions and Terminology 6.2 The Parent Pointer Implementation 6.3 General Tree Implementations 6.4 K-ary Trees 6.5 Sequential Tree Implementations

General Tree Implementation

Lists of Children 6.3 General Tree Implementation

Leftmost Child/Right Sibling (1) General Tree Implementation

Leftmost Child/Right Sibling (2) Combining two trees 6.3 General Tree Implementation

Dynamic Node Implementations (1) Allocate variable space for each node Each node stores an array-based list of child pointers 6.3 General Tree Implementation

Dynamic node Implementations (2) Each node stores an linked list of child pointers 6.3 General Tree Implementation

A BCD EF G A B C E D F G root A B C E D F G Dynamic Left-Child/Right-Sibling data leftChild rightSibling 6.3 General Tree Implementation

Converting to a Binary Tree Left child/right sibling representation essentially stores a binary tree. Use this process to convert any general tree or forest to a binary tree. A forest is a collection of one or more general trees. 6.3 General Tree Implementation

Contents 6.1 General Tree Definitions and Terminology 6.2 The Parent Pointer Implementation 6.3 General Tree Implementations 6.4 K-ary Trees 6.5 Sequential Tree Implementations

K-ary Trees K 叉树 6.4 K-ary Trees

K-ary Trees K-ary trees are trees whose nodes have K children.  A binary tree is a 2-ary tree. K-ary tree nodes have a fixed number of children. In general, K-ary trees bear many similarities to binary trees. 6.4 K-ary Trees

K-ary Trees Full and complete K-ary trees 6.4 K-ary Trees

Contents 6.1 General Tree Definitions and Terminology 6.2 The Parent Pointer Implementation 6.3 General Tree Implementations 6.4 K-ary Trees 6.5 Sequential Tree Implementations

Sequential Tree Implementations 树的顺序 ( 数组 ) 存储 6.5 Sequential Tree Implementations

Sequential Implementations (1) List node values in the order they would be visited by a preorder traversal. Saves space, but allows only sequential access. Need to retain tree structure for reconstruction. 6.5 Sequential Tree Implementations

Sequential Implementations (2) Example: For binary trees, use a symbol to mark null links. preorder traversal : ABDCEGFHI Sequential Implementation: AB/D//CEG///FH//I// A BC D EF GHI 6.5 Sequential Tree Implementations

Sequential Implementations (3) Explicitly list with each node whether it is an internal node or a leaf. Each internal node has two children (full) A BC D EF GHI Example: Take the tree as a full binary trees, mark nodes as leaf or internal. A’B’/DC’E’G/F’HI 6.5 Sequential Tree Implementations

Sequential Implementations (4) For general trees, use a special mark to indicate the end of each subtree. Example: RAC)D)E))BF))) 6.5 Sequential Tree Implementations

Summary 6.1 General Tree Definitions and Terminology 6.2 The Parent Pointer Implementation 6.3 General Tree Implementations 6.4 K-ary Trees 6.5 Sequential Tree Implementations

Homework P223~P