Trees¶
Abstract
- 树的表示法 first-child-next-sibling
- 二叉树
- 性质
- 树的遍历:前序、中序、后序、层序
- 线索二叉树
- 应用:文件系统、表达式树
- 二叉搜索树
- 插入
- 删除
int binary_search(List Tbl, ElementType K){
int left, right, mid;
int NotFound = -1;
left = 1;
right = Tbl -> Length;
while(left <= right){
mid = (left + right) / 2;
if(K < Tbl -> Element[mid])
right = mid - 1;
else if(K > Tbl -> Element[mid])
left = mid + 1;
else
return mid;
}
return NotFound;
}
Time Complexity: \(O(log N)\)
Definitions¶
- 树(trees)
- 包含1个根节点(root) \(r\)
- 有0个或k个子树(subtrees) \(T_1,……,T_k\)
Tip
- 子树间不会相互连接,因此每个节点都是某个子树的根节点
- 对一棵有\(N\)个节点的树,其有 \(N - 1\)条边
-
度(degree)
- 对于 一个节点 :它 所有子树的个数
- 对于 一棵树 :它 所有子树中最大的度 即为此树的度
-
父节点(parent)
- 有子树的节点
- 孩子节点(children)
- 父节点子树的根节点
-
兄弟节点(siblings)
- 有共同父节点的孩子节点
-
叶子节点(leaf)
- 度为0的节点(即没有子树)
-
路径(path)
- 从 \(n_1\) 到 \(n_k\),一个包含节点 \(n_1,n_2,……,n_k\) 唯一的序列,满足 \(n_i\) 是 \(n_{i+1}\) 的父节点(一路顺下来的意思)
-
路径长度(length)
- 路径上边的条数
-
深度
- 对于顶点 \(n_i\) ,从根节点出发到\(n_i\)的路径长度
- \(depth(root) = 0\)
-
高度
- 对于顶点 \(n_i\) ,从\(n_i\)到叶子节点的最长路径长度
- \(height(leaf) = 0\)
根节点的高度 = 最深的叶子节点的深度
- 祖先 (ancestor):从该节点到根节点的路径上所有的节点
- 后代 (descendant):该节点所有子树的节点
- 内部节点 (internal vertices):有孩子节点的顶点
Implementation¶
-
List Representation
每个节点的空间大小取决于它有多少个子树实现起来麻烦
-
FirstChild-NextSibling Representation
对于同一棵树表示不唯一,因为孩子的顺序可任意
Binary Trees¶
二叉树
Expression Trees / syntax trees¶
(表达式树/语法树)
- 中缀表达式 → 后缀表达式
- 类似后缀表达式求解
Tree Traversals¶
树的遍历:
- 前序遍历(preorder traversals)
- 后序遍历(postorder traversals)
- 层序遍历(levelorder traversals)
“之”形遍历
- 中序遍历(inorder traversals)
例题
Threaded Binary Trees¶
线索二叉树
一些特例 - 完全二叉树 (complete binary trees)
- 歪斜二叉树 (skewed binary trees)
BST Tree¶
Implementations⚓︎¶
- Find
- FindMin
- FindMax
- Insert
- Delete
AVL Tree¶
Segment Tree¶
线段树(segment tree)-- 区间树(interval tree)
leading: 频繁区间查询 - 区间查询 Range Query 更快 - 单点修改 Point Update 也不能太慢
把一个大区间不断二分成小区间,每个节点保存一个区间的信息。
线段树 = 区间递归二分 + 节点保存区间答案 + 查询/更新时只走必要分支
结构
示例
给定数组A = [7, 2, 5, 8, 3]
- 每个节点代表一个区间,存储这个区间的统计结果 - 根节点:整个数组区间 - 中间节点:某个子区间 - 叶节点:单个元素- 求和线段树 (sum segment tree)
- 最小值线段树(min segment tree)
- 最大值线段树(max segment tree)
区间最小值查询 / Range Minimum Query
操作集¶
Build 建树 Query 区间查询 Update 单点更新 PushDown 懒惰标记下推 RangeUpdate 区间更新
- 建树
- 时间复杂度 \(O(N)\)
- 空间复杂度 \(O(N)\)
如果有 \(N\) 个叶节点,则线段树的总节点数为 \(2N - 1\),因此空间复杂度为 \(O(2N - 1)\),简化后为 \(O(N)\)。
- 区间查询
- 时间复杂度 \(O(log N)\)
- 空间复杂度 \(O(1)\)
- No Overlap
- Total Overlap
- Partial Overlap
- Unvisited
- 单点更新
- 时间复杂度 \(O(log N)\)
- 空间复杂度 \(O(1)\)
单点更新只影响 从根到目标叶节点的一条路径,不会影响整棵树
补充:区间更新
- 时间复杂度 \(O(log N)\)
- 空间复杂度 \(O(1)\)
- 需要使用懒惰标记(lazy propagation)来实现区间更新
Lazy Tag / Lazy Propagation
HW4 -- tree¶
True-False Questions¶
-
It is always possible to represent a tree by a one-dimensional integer array.
-
There exists a binary tree with 2016 nodes in total, and with 16 nodes having only one child.
Multiple-Choice Questions¶
-
Given a tree of degree 3. Suppose that there are 3 nodes of degree 2 and 2 nodes of degree 3. Then the number of leaf nodes must be ____.
-
If a general tree \(T\) is converted into a binary tree \(BT\), then which of the following \(BT\) traversals gives the same sequence as that of the post-order traversal of \(T\)?
-
Given the shape of a binary tree shown by the figure below. If its inorder traversal sequence is { E, A, D, B, F, H, C, G }, then the node on the same level of C must be:

-
Among the following threaded binary trees (the threads are represented by dotted curves), which one is the postorder threaded tree?
ZigZagging on a Tree
```C
HW5 -- binary search tree¶
True-False Questions¶
-
In a binary search tree, the keys on the same level from left to right must be in sorted (non-decreasing) order.
-
In a binary search tree which contains several integer keys including 4, 5, and 6, if 4 and 6 are on the same level, then 5 must be their parent.
Multiple-Choice Questions¶
-
Given a binary search tree as shown in the following figure. Which of the following relationships is correct with respect to the given tree?
-
Given the structure of a binary search tree (as shown in the figure), which one of the following insertion sequences is impossible?
-
Given a binary search tree with its preorder traversal sequence { 8, 2, 15, 10, 12, 21 }. If 8 is deleted from the tree, which one of the following statements is FALSE?
-
A.One possible preorder traversal sequence of the resulting tree may be { 2, 15, 10, 12, 21 }
-
B.One possible preorder traversal sequence of the resulting tree may be { 10, 2, 15, 12, 21 }
-
C.One possible preorder traversal sequence of the resulting tree may be { 15, 10, 2, 12, 21 }
-
D.It is possible that the new root may have 2 children
-
-
Insert {5, 2, 7, 3, 4, 1, 6} one by one into an initially empty binary search tree. The postorder traversal sequence of the resulting tree is:
-
A.1, 2, 3, 4, 6, 7, 5
-
B.1, 4, 2, 6, 3, 7, 5
-
C.1, 4, 3, 2, 6, 7, 5
-
D.5, 4, 3, 7, 6, 2, 1
-
-
Among the following binary trees, which one can possibly be the decision tree (the external nodes are excluded) for binary search?
-
For a binary search tree, in which order of traversal that we can obtain a non-decreasing sequence?
-
A.preorder traversal
-
B.postorder traversal
-
C.inorder traversal
-
D.level-order traversal
-
HW7 -- segment tree¶
True-False Questions¶
-
A segment tree can be used to find the greatest common divisor (GCD) of any index range.
-
In Union/Find algorithm, if Unions are done by size, the depth of any node must be no more than \(N/2\), but not \(O(logN)\).
Multiple-Choice Questions¶
-
The array representation of a disjoint set containing numbers 0 to 8 is given by { 1, -4, 1, 1, -3, 4, 4, 8, -2 }. Then to union the two sets which contain 6 and 8 (with union-by-size), the index of the resulting root and the value stored at the root are:
-
A.1 and -6
-
B.4 and -5
-
C.8 and -5
-
D.8 and -6
-
A relation R is defined on a set S. If for every element e in S, "e R e" is always true, then R is said to be __ over S.
-
A.consistent
-
B.symmetric
-
C.transitive
-
D.reflexive
-
Let T be a tree created by union-by-size with N nodes, then the height of T can be .
-
A.at most \(log_2(N)+1\)
-
B.at least \(log_2(N)+1\)
-
C.as large as \(N\)
-
D.anything that is greater than 1
-
What is a segment tree primarily used for?
-
A.Sorting elements
-
B.Finding minimum/maximum in a range
-
C.Finding the sum of a given set of integers
-
D.Finding a given string from a set of strings
Find
Please fill in the blanks in the program which performs Find as a Union/Find operation with path compression.