【笔记】第5章 二叉树

A. 树

一、动机

  1. 应用:层次结构表示,如RNP,文件系统,url…
  2. 综合性:数据结构,Vector静态操作,list的动态操作
  3. 半线性:不再是简单的线性结构,但在确定某种次序后,仍具有线性特征

二、有根树、有序数

在这里插入图片描述

三、特点

  • 路径(连通性) + 环路(无环性)
    1. v中的k+1个节点,通过v中的k条边依次相联,构成一条路径(path,通路)
    2. 路径长度即所含边数:| π | = k
    3. 环路(cycle/loop):vk = v0(如果覆盖所有节点各一次,称作周游tour)
  • 连通 + 无环
    1. 连通图:节点之间均有路径(connected);无环图(acyclic):不含环路
    2. 树:无环连通图、极小连通图、极大无环图
    3. 任一节点v与很之间存在唯一路径path(v,r) = path(v),以| path(v) |为指标可对所有节点做等价类划分
  • 深度 + 层次
    1. 路径、节点和子树可以相互指代:path(v) ~ v ~ subtree(v)
    2. 祖先与后代
    · path(v) 上节点,均为v的祖先(ancestor),v是他们的后代(descendent),除自身以外是真(proper)祖先/后代
    · 半线性:在任一深度,v的祖先/后代若存在,则必然/未必唯一
    · 根节点是所有节点的公共祖先,深度为0
    · 没有后代的节点(必然存在)称作叶子(leaf)
    3. v的深度
    · depth(v) = | path(v) |
    · 所有叶子深度中的最大者,称作(子)树(根)的高度:height(v) = height( subtree(v))
    · 特别的,空树的高度取做-1
    · depth(v) + height(v) ≤ height(T)
    在这里插入图片描述

四、作业

  1. Which of the following data structures can efficiently balance static operations with dynamic operations:下列哪种数据结构可以高效地兼顾静态操作和动态操作:tree树
  2. How many edges of the tree have n vertices?n个顶点的树有多少条边? n-1
  3. The tree is:树是:Connected Acyclic Graph连通无环图
  4. 在一棵树中,顶点p是顶点v的父亲,则它们的高度的关系是:height(v) < height§

B. 树的表示

一、接口

在这里插入图片描述

二、父节点与孩子节点

1. 父节点

  1. 特点:除根外,任一节点,有且仅有一个父节点
  2. 构思:将节点组织为序列,各节点分别记录
  3. 空间性能:O(n)
  4. 时间性能:
    · parent() : O(1)
    · root() : O(n) 或 O(1) 【rank0即为根节点】
    · firstChild() : O(n)
    · nextSilbing() : O(n)

2. 孩子节点

  1. 同一节点的所有孩子,组织为一个序列;序列的长度,分别等于对应节点的度数
  2. parent()慢
    在这里插入图片描述

三、父节点+孩子节点

· 每一个节点child引用所指向的数据集(长度Σdi = e = n - 1),在规模相差悬殊。平均而言,所有数据集的规模应该为O(1),但目前为O(n)
在这里插入图片描述

四、长子+兄弟

· 对于任一节点,均只需设两个引用:纵firstChild();横nextSibling()
在这里插入图片描述

五、作业

  1. Save the tree of n nodes using the parent + child node method. The space required is:用父节点+孩子节点的方法存储n个节点的树,需要的空间是:O(n)
  2. The above tree is represented on the computer as follows:以上树在计算机中表示如下:
    在这里插入图片描述
    The content of parent[] in the third line should be:第三行中parent[]的内容应该是: -1, 5, 5, 7, 0, 4, 5, 0, 0, 7

C. 有根有序树 = 二叉树binary tree

一、二叉树

  1. 节点度数不超过2,子树可以左、右区分(隐含有序,左为先右为后)
  2. 深度为k的节点,至多2k
  3. n个节点、高h的二叉树满足:h+1 ≤ n ≤ 2h+1-1
  4. 特殊情况
    · 单链:n = h+1
    · 满二叉树(full binary tree):n = 2h+1-1

二、基数与真二叉树

  • 设度数为0,1,2的节点,各有n0,n1,n2
    · 边数: e = n1 + 2n2
    · 叶节点数:n0 = n2 + 1
    · 节点数:n = n0 + n1 + n2 = 1 + n1 + 2n2
    · 特别地,当n1=0时,e=2n2和n0=n2+1=(n+z)/2
  • 当节点度数均为偶数时,称为真二叉树(proper binary tree)
    · 通过引入n1 + 2n0个外部节点,可是原有节点度数统一为2,即可将任一二叉树转化为真二叉树
    · 全树自身的复杂度并为实质增加

三、描述多叉树

  1. 有根且有序时,多叉树均可转化并表示为二叉树
  2. 长子—左孩子;兄弟—右孩子

四、作业

  1. How many nodes are in full binary trees of height h?高度为h的满二叉树有多少个节点? 2h+1-1
  2. A true binary tree with height h and node n is characterized by:一棵高度为h,节点数为n的真二叉树的特点是:There is no node with only one child不存在只有一个孩子的节点
  3. In the eldest son-brother representation, what will the eldest child of a node in the tree be equivalent to in the binary tree:在长子-兄弟表示法中,树中某节点的长子相当于二叉树中的:left child左子

D. 二叉树的实现

一、BinNode

在这里插入图片描述

二、BinTree模板类

在这里插入图片描述

三、动态操作

  1. 高度更新
    在这里插入图片描述
  2. 节点插入
    在这里插入图片描述
  3. 子树接入与删除
    在这里插入图片描述
  4. 子树分离
    在这里插入图片描述

四、作业

  1. Consider a binary tree have n nodes with a height of h. Insert a new node in it, and the number of nodes whose height has changed is:设二叉树有n个节点,高度为h.在其中插入一个新的节点,高度发生改变的节点个数为: O(h) 【The height of all nodes (ie the ancestors of the new node) on the newly inserted node to the root node may change.新插入节点到根节点的路径上所有节点(即新节点的祖先)高度都有可能变化.】

E. 先序遍历

一、遍历traversal

  • 按照某种次序访问树中各节点,每个节点被访问恰好一次
  • 遍历结果 — 遍历过程 — 遍历次序 — 遍历策略
    在这里插入图片描述

二、递归实现

在这里插入图片描述

三、迭代实现1

  • 正确性:无遗漏、根优先、左先右后
  • 复杂度:O(n)
  • 缺点:不易推广
    在这里插入图片描述

四、迭代实现2

  • 藤缠树
    1. 自上而下访问藤上节点,再自下而上遍历右子树
    2. 各右子树的遍历彼此独立自成一个子任务
  • 实现
    在这里插入图片描述

五、作业

  1. Pre-order traversal of the following binary tree:对以下二叉树进行先序遍历:When you have just finished accessing node d (Implementation 2), the elements in the stack from the top of the stack to the bottom of the stack are:刚访问完节点d时(迭代实现2)栈中的元素从栈顶到栈底依次为: f
    在这里插入图片描述
  2. The binary tree is:二叉树是:Semi-linear structure半线性结构
  3. If in the pre-order traversal the access to the root node is followed by accessing the right subtree first and then the left subtree, then the stacking order of the left and right subtrees is:若在先序遍历中规定访问完根节点后先访问右子树再访问左子树,则左、右子树的入栈顺序是:Left first and then right先左后右
  4. Pre-order traversal is:先序遍历的顺序是:Visit the nodes on the left side chain from top to bottom and access their right subtree from bottom to top先自上而下访问左侧链上的节点,再自下而上访问它们的右子树

F. 中序遍历

一、递归实现

在这里插入图片描述

二、迭代实现

  • 观察:不同尺度下,对一系列左侧分支的逐步处理
  • 藤缠树:
    1. 沿着左侧藤,遍历可自底而上分解d+1步迭代:访问藤上节点,再遍历右子树
    2. 各右子树的遍历彼此独立,自成一个子任务
  • 分摊分析Amortized analysis:复杂度O(n)
    1. 每次迭代,都恰有一个节点出栈并被访问
    2. 每个节点入栈且仅一次
    3. 每个节点进行push的操作仅为n次
  • 实现:
    在这里插入图片描述

三、后继与前驱

在这里插入图片描述

四、作业

  1. The first node visited in the in-order traversal is:中序遍历中第一个被访问的节点是:Leftmost node最左的节点
  2. Perform inorder traversal on the following binary tree:对以下二叉树进行中序遍历:When the node c has just been accessed, the elements in the stack from the top of the stack to the bottom of the stack are:节点c刚被访问完毕时栈中的元素从栈顶到栈底为:d,f
    在这里插入图片描述

G. 后序遍历

一、递归实现

在这里插入图片描述

二、迭代算法

  • 藤缠树
    1. 从根出发下行:尽可能沿左分支,实不得已才沿右分支
    2. 最后一个节点,必是叶子,是按中序遍历次序最靠左者,也是递归版中visit()首次执行处
  • 分摊分析:复杂度O(n)
  • 表达式树(Expression Tree) - 后序遍历(Post-order) - 逆波兰表达式(RPN)
  • 实现
    在这里插入图片描述

H. 层次遍历

一、实现

  • 条件:有跟有序树
  • 要点:顺序,deep深度
  • 分析:
    · 任何时刻,队列中各节点按深度单调排序,而且(相邻)节点的深度相差不超过1层
    · 所有节点都会入队,且更高/低的节点,更早/晚入队;更左/右的节点,更早/晚入队。
    · 每次迭代,都恰好一个节点出队并接受访问,同时不超过两个节点入队
  • 实现:
    在这里插入图片描述

二、完全二叉树*

  1. 完全二叉树 - 紧凑表示 - 以向量实现
  2. 叶节点仅限于最低两层
    · 底层叶子,均居于次底层叶子左侧(相对于LCA)
    · 除末节点的父亲,内部节点均有双子
  3. 叶节点:不致少于内部节点,但至多多出一个
  4. LCA(最近公共祖先,Lowest common anccestor)【资料:https://www.cnblogs.com/shadowland/p/5930429.html】

三、作业

  1. The order of hierarchy traversal is:层次遍历的次序是:Top-down access to each node in depth, from left to right in nodes of the same depth自上而下访问各个深度的节点,同样深度的节点中自左向右
  2. Perform hierarchical traversal of the following binary tree:对以下二叉树进行层次遍历:The element in the queue when node F is about to dequeue is from the head to the end of the queue:节点F正欲出队时队列中的元素从队头到队尾为:F,G
    在这里插入图片描述

I. 重构

已知序列,如何还原出二叉树:

一、【先序 + 后序】| 中序

以先序为例:

  • 通过先序,确定根root;再根据中序遍历序列确定- 左右子树
  • 单使用先序序列,当左子树或右子树为空时,出现歧义
    在这里插入图片描述

二、【先序 + 后序】* 真二叉树

在这里插入图片描述

三、作业

  1. The last node in the post-order traversal sequence is:后序遍历序列中最后一个节点是:Root node根节点

J. Huffman树

一、编码

  1. 编码
    · 二进制编码:组成字符来源于字符集Σ;字符为互异的二进制串
    · 文件大小取决于:字符的数量 * 各字符编码的长短
  2. PFC编码(prefix-free code)
    · 字符集Σ组织为二叉树,以0/1表示左右子树
    · 字符编码串由根到V(x)的通路(root path)决定
    · 不同字符的编码互不为前缀
  3. 对于特定的Σ,ald()最小者即为最优编码树Topt,但不见得唯一

二、编码算法

  1. 带权编码长度 vs. 叶节点平均带权深度
    在这里插入图片描述
  2. 最优带权编码树
    在这里插入图片描述

三、构造编码树*

Huffman算法
1. 双子性:真二叉树
2. 不唯一性:明确左右子树频率高低关系
3. 层次性:出现频率最低的字符,必在某棵最优编码树的最底层,且相互之间互为兄弟
4. 步骤:
· Huffman(超)字符
· 树与森林
· 构造编码树
· 查找最小字符(先、中、后均可)
· 构造编码表
5. 实现
在这里插入图片描述

XA. 应用*

  1. Graph/Tree:diameter、eccentricity,radius,center
  2. TreeDiameter
  3. Knights of the Round Table
  4. Traveling Knight

测验

  1. A binary tree has n nodes and its height is h. In which a new node is inserted, the maximum number of nodes that have changed in height is:某二叉树有n个节点,高度为h。在其中插入一个新的节点,高度发生改变的节点个数最多为:O(h)
  2. How many nodes could be in complete binary tree of height h?高度为 h 的完全二叉树可能有多少个节点? 2h 【The full binary tree node with a height of h is the most full binary tree, and the least case is more than 1 with a full binary tree of height h-1.高度为h的完全二叉树节点最多的情况即满二叉树,最少的情况比高度为h-1的满二叉树多1。】
  3. The following proposition of the tree, which is wrong下列关于树的命题中错误的是:Delete any edge in the tree to get the tree. 在树中删除任一条边得到的还是树。【Delete two edges after deleting one edge删除一条边后得到两个连通分量】
  4. A lookup set is a data structure used to represent disjoint sets and supports the following operations:并查集是一种用于表示不相交集合的数据结构,支持以下操作:A basic implementation is to organize the elements of each set into a rooted tree. The elements in the set are the nodes in the tree. The root of the collection is the representative element of the set. The entire search set consists of several trees. Composition of the forest. The interface implementation method is:一种基本的实现是将每一个集合中的元素组织成一棵有根树,集合中的元素即树中的节点,选取树根为该集合的代表元,而整个并查集就是由若干棵树组成的森林。接口实现的方法是:Example: In the figure below, we look at the tree {c, h, b, e} and {f, d, g} that originally represented the two collections. After calling Union(h, f), we get the right tree. At this point, calling Find(e) returns f.例子:下图中的并查集原先有两棵表示集合的树{c,h,b,e}和{f,d,g},调用Union(h, f)后得到了右边的树,如果此时再调用Find(e)会返回f。What is the best way to look for the most concentrated tree?并查集中的树最适合用什么方法表示:Parent node method父节点法 【父节点法能够高效定位父亲而不能高效地定位孩子,而并查集中的树只需要能够定位父节点。】
    在这里插入图片描述
  5. From the node node u of the binary tree of n nodes to the root node node by node, the following mistakes are:从n个节点的二叉树的叶节点u逐个节点地上溯到根节点的过程中,以下说法中错误的是:Each time it goes up one level, the depth of the current node decreases by one and the height increases by one.每上溯一层,当前节点的深度减小1,而高度增加1。【Each layer goes up one layer, and the depth decreases by one, but the increase in height may be greater than one, because the height of a node is determined by the higher of its left and right subtrees.每上溯一层,深度减小1,但高度的增加可能大于1,因为节点的高度由其左、右子树中较高者决定。】
  6. In order to traverse the binary tree, the successor of the node v in the order traversal is (assuming that the successor of v exists):对二叉树进行中序遍历,节点v在中序遍历下的后继为(假设v的后继存在):The first visited node in its right subtree or an ancestor of v其右子树中第一个被访问的节点或v的某个祖先【When v does not have a right subtree, its successor is an ancestor, specifically the ancestor that first turned right along the parent pointer.当v没有右子树时它的后继为某个祖先,具体地说是在沿着parent指针向上的过程中第一次向右的祖先。】
  7. Similar to pre-order and in-order traversal, accessing the binary tree in the order of left child->right child->root node is called post-order traversal. The first visited node in the post-order traversal is与先序、中序遍历类似,以左子->右子->根节点的顺序来访问二叉树称为后序遍历。后序遍历中第一个被访问的节点是:None of the above以上皆不是 【Starting from the root node, for each node in the middle, you can go left to the left and not to the left. If there is no way to go, then the node is the first node to be visited in the subsequent traversal.从根节点开始,对于中途每个节点,能往左就往左,不能往左就往右,若左右都无路可走,则该节点是后序遍历中第一个被访问的节点。】
  8. The binary tree is traversed in order. u and v are two nodes on the left chain, and u is the ancestor of v. x and y are the right children of u and v. The order in which these four nodes are accessed is:对二叉树进行先序遍历,u和v是左侧链上两个节点,且u是v的祖先,x、y分别是u和v的右子,试问这四个节点被访问的顺序是:u,v,y,x
  9. The misconception about the relationship between binary tree traversal sequences is:关于二叉树遍历序列之间关系的说法错误的是:Knowing pre-order traversal and post-order traversal sequences, we can determine the sequence of mid-order traversal已知先序遍历序列和后序遍历序列可以确定中序遍历序列
    10.When using a queue to hierarchically traverse a binary tree, the nodes in the queue at any time satisfy:借助队列对二叉树进行层次遍历时,任意时刻队列中的节点满足: The difference in depth does not exceed 1深度相差不超过1
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

啊有礼貌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值