2023-02-28 20:03:53 +08:00
|
|
|
|
# 二叉树
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2022-12-21 01:37:58 +08:00
|
|
|
|
「二叉树 Binary Tree」是一种非线性数据结构,代表着祖先与后代之间的派生关系,体现着“一分为二”的分治逻辑。类似于链表,二叉树也是以结点为单位存储的,结点包含「值」和两个「指针」。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```java title=""
|
2023-03-12 18:49:52 +08:00
|
|
|
|
/* 二叉树结点类 */
|
2022-11-22 17:47:26 +08:00
|
|
|
|
class TreeNode {
|
|
|
|
|
int val; // 结点值
|
|
|
|
|
TreeNode left; // 左子结点指针
|
|
|
|
|
TreeNode right; // 右子结点指针
|
|
|
|
|
TreeNode(int x) { val = x; }
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2022-11-29 02:21:49 +08:00
|
|
|
|
=== "C++"
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```cpp title=""
|
2023-03-12 18:49:52 +08:00
|
|
|
|
/* 二叉树结点结构体 */
|
2022-11-29 02:21:49 +08:00
|
|
|
|
struct TreeNode {
|
|
|
|
|
int val; // 结点值
|
|
|
|
|
TreeNode *left; // 左子结点指针
|
|
|
|
|
TreeNode *right; // 右子结点指针
|
|
|
|
|
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```python title=""
|
2023-03-12 18:49:52 +08:00
|
|
|
|
""" 二叉树结点类 """
|
2022-11-29 02:21:49 +08:00
|
|
|
|
class TreeNode:
|
2023-03-12 18:49:52 +08:00
|
|
|
|
def __init__(self, val: int):
|
|
|
|
|
self.val: int = val # 结点值
|
|
|
|
|
self.left: Optional[TreeNode] = None # 左子结点指针
|
|
|
|
|
self.right: Optional[TreeNode] = None # 右子结点指针
|
2022-11-29 02:21:49 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```go title=""
|
2023-03-12 18:49:52 +08:00
|
|
|
|
/* 二叉树结点结构体 */
|
2022-12-03 20:25:24 +08:00
|
|
|
|
type TreeNode struct {
|
|
|
|
|
Val int
|
|
|
|
|
Left *TreeNode
|
|
|
|
|
Right *TreeNode
|
|
|
|
|
}
|
2022-12-28 12:29:29 +08:00
|
|
|
|
/* 结点初始化方法 */
|
2022-12-03 20:25:24 +08:00
|
|
|
|
func NewTreeNode(v int) *TreeNode {
|
|
|
|
|
return &TreeNode{
|
|
|
|
|
Left: nil,
|
|
|
|
|
Right: nil,
|
|
|
|
|
Val: v,
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
2023-02-08 04:27:55 +08:00
|
|
|
|
```javascript title=""
|
2023-03-12 18:49:52 +08:00
|
|
|
|
/* 二叉树结点类 */
|
2022-12-04 12:26:09 +08:00
|
|
|
|
function TreeNode(val, left, right) {
|
2022-12-13 21:44:14 +08:00
|
|
|
|
this.val = (val === undefined ? 0 : val); // 结点值
|
|
|
|
|
this.left = (left === undefined ? null : left); // 左子结点指针
|
|
|
|
|
this.right = (right === undefined ? null : right); // 右子结点指针
|
2022-12-05 00:13:48 +08:00
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title=""
|
2023-03-12 18:49:52 +08:00
|
|
|
|
/* 二叉树结点类 */
|
2022-12-15 00:00:40 +08:00
|
|
|
|
class TreeNode {
|
|
|
|
|
val: number;
|
|
|
|
|
left: TreeNode | null;
|
|
|
|
|
right: TreeNode | null;
|
2022-12-21 17:19:39 +08:00
|
|
|
|
|
2022-12-15 00:00:40 +08:00
|
|
|
|
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
|
|
|
|
|
this.val = val === undefined ? 0 : val; // 结点值
|
|
|
|
|
this.left = left === undefined ? null : left; // 左子结点指针
|
|
|
|
|
this.right = right === undefined ? null : right; // 右子结点指针
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title=""
|
2022-12-21 17:19:39 +08:00
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title=""
|
2023-03-12 18:49:52 +08:00
|
|
|
|
/* 二叉树结点类 */
|
2022-12-23 15:42:02 +08:00
|
|
|
|
class TreeNode {
|
2022-12-24 17:05:58 +08:00
|
|
|
|
int val; // 结点值
|
|
|
|
|
TreeNode? left; // 左子结点指针
|
|
|
|
|
TreeNode? right; // 右子结点指针
|
2022-12-23 15:42:02 +08:00
|
|
|
|
TreeNode(int x) { val = x; }
|
|
|
|
|
}
|
2022-11-29 02:21:49 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-01-08 19:41:05 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title=""
|
2023-03-12 18:49:52 +08:00
|
|
|
|
/* 二叉树结点类 */
|
2023-01-18 21:34:44 +08:00
|
|
|
|
class TreeNode {
|
|
|
|
|
var val: Int // 结点值
|
|
|
|
|
var left: TreeNode? // 左子结点指针
|
|
|
|
|
var right: TreeNode? // 右子结点指针
|
2023-01-08 19:41:05 +08:00
|
|
|
|
|
2023-01-18 21:34:44 +08:00
|
|
|
|
init(x: Int) {
|
|
|
|
|
val = x
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-08 19:41:05 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-02-01 22:03:04 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title=""
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2023-04-06 22:03:46 +08:00
|
|
|
|
结点的两个指针分别指向「左子结点」和「右子结点」,并且称该结点为两个子结点的「父结点」。给定二叉树某结点,将“左子结点及其以下结点形成的树”称为该结点的「左子树」,右子树同理。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-04-06 22:03:46 +08:00
|
|
|
|
除了叶结点外,每个结点都有子结点和子树。例如,若将下图的“结点 2”看作父结点,那么其左子结点和右子结点分别为“结点 4”和“结点 5”,左子树和右子树分别为“结点 4 及其以下结点形成的树”和“结点 5 及其以下结点形成的树”。
|
2022-12-25 14:05:42 +08:00
|
|
|
|
|
2023-02-26 18:18:34 +08:00
|
|
|
|
![父结点、子结点、子树](binary_tree.assets/binary_tree_definition.png)
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-02-16 03:39:01 +08:00
|
|
|
|
## 二叉树常见术语
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2022-12-18 15:42:06 +08:00
|
|
|
|
二叉树的术语较多,建议尽量理解并记住。后续可能遗忘,可以在需要使用时回来查看确认。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2022-12-18 15:42:06 +08:00
|
|
|
|
- 「根结点 Root Node」:二叉树最顶层的结点,其没有父结点;
|
|
|
|
|
- 「叶结点 Leaf Node」:没有子结点的结点,其两个指针都指向 $\text{null}$ ;
|
2023-01-04 19:27:35 +08:00
|
|
|
|
- 结点所处「层 Level」:从顶至底依次增加,根结点所处层为 1 ;
|
2022-12-28 12:29:29 +08:00
|
|
|
|
- 结点「度 Degree」:结点的子结点数量。二叉树中,度的范围是 0, 1, 2 ;
|
2022-12-18 15:42:06 +08:00
|
|
|
|
- 「边 Edge」:连接两个结点的边,即结点指针;
|
2022-12-18 15:59:34 +08:00
|
|
|
|
- 二叉树「高度」:二叉树中根结点到最远叶结点走过边的数量;
|
2022-12-18 15:42:06 +08:00
|
|
|
|
- 结点「深度 Depth」 :根结点到该结点走过边的数量;
|
|
|
|
|
- 结点「高度 Height」:最远叶结点到该结点走过边的数量;
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-02-26 18:18:34 +08:00
|
|
|
|
![二叉树的常用术语](binary_tree.assets/binary_tree_terminology.png)
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2022-12-18 15:59:34 +08:00
|
|
|
|
!!! tip "高度与深度的定义"
|
|
|
|
|
|
|
|
|
|
值得注意,我们通常将「高度」和「深度」定义为“走过边的数量”,而有些题目或教材会将其定义为“走过结点的数量”,此时高度或深度都需要 + 1 。
|
|
|
|
|
|
2023-02-16 03:39:01 +08:00
|
|
|
|
## 二叉树基本操作
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-01-07 20:34:32 +08:00
|
|
|
|
**初始化二叉树**。与链表类似,先初始化结点,再构建引用指向(即指针)。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="binary_tree.java"
|
|
|
|
|
// 初始化结点
|
|
|
|
|
TreeNode n1 = new TreeNode(1);
|
|
|
|
|
TreeNode n2 = new TreeNode(2);
|
|
|
|
|
TreeNode n3 = new TreeNode(3);
|
|
|
|
|
TreeNode n4 = new TreeNode(4);
|
|
|
|
|
TreeNode n5 = new TreeNode(5);
|
|
|
|
|
// 构建引用指向(即指针)
|
|
|
|
|
n1.left = n2;
|
|
|
|
|
n1.right = n3;
|
|
|
|
|
n2.left = n4;
|
|
|
|
|
n2.right = n5;
|
|
|
|
|
```
|
|
|
|
|
|
2022-11-29 02:21:49 +08:00
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="binary_tree.cpp"
|
|
|
|
|
/* 初始化二叉树 */
|
|
|
|
|
// 初始化结点
|
|
|
|
|
TreeNode* n1 = new TreeNode(1);
|
|
|
|
|
TreeNode* n2 = new TreeNode(2);
|
|
|
|
|
TreeNode* n3 = new TreeNode(3);
|
|
|
|
|
TreeNode* n4 = new TreeNode(4);
|
|
|
|
|
TreeNode* n5 = new TreeNode(5);
|
|
|
|
|
// 构建引用指向(即指针)
|
|
|
|
|
n1->left = n2;
|
|
|
|
|
n1->right = n3;
|
|
|
|
|
n2->left = n4;
|
|
|
|
|
n2->right = n5;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="binary_tree.py"
|
2022-12-27 19:33:58 +08:00
|
|
|
|
""" 初始化二叉树 """
|
2023-02-07 04:43:52 +08:00
|
|
|
|
# 初始化结点
|
2022-12-20 15:52:00 +08:00
|
|
|
|
n1 = TreeNode(val=1)
|
|
|
|
|
n2 = TreeNode(val=2)
|
|
|
|
|
n3 = TreeNode(val=3)
|
|
|
|
|
n4 = TreeNode(val=4)
|
|
|
|
|
n5 = TreeNode(val=5)
|
|
|
|
|
# 构建引用指向(即指针)
|
|
|
|
|
n1.left = n2
|
|
|
|
|
n1.right = n3
|
|
|
|
|
n2.left = n4
|
|
|
|
|
n2.right = n5
|
2022-11-29 02:21:49 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="binary_tree.go"
|
2022-12-03 20:25:24 +08:00
|
|
|
|
/* 初始化二叉树 */
|
|
|
|
|
// 初始化结点
|
|
|
|
|
n1 := NewTreeNode(1)
|
|
|
|
|
n2 := NewTreeNode(2)
|
|
|
|
|
n3 := NewTreeNode(3)
|
|
|
|
|
n4 := NewTreeNode(4)
|
|
|
|
|
n5 := NewTreeNode(5)
|
|
|
|
|
// 构建引用指向(即指针)
|
|
|
|
|
n1.Left = n2
|
|
|
|
|
n1.Right = n3
|
|
|
|
|
n2.Left = n4
|
|
|
|
|
n2.Right = n5
|
2022-11-29 02:21:49 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
2023-02-08 04:27:55 +08:00
|
|
|
|
```javascript title="binary_tree.js"
|
2022-12-04 12:26:09 +08:00
|
|
|
|
/* 初始化二叉树 */
|
|
|
|
|
// 初始化结点
|
|
|
|
|
let n1 = new TreeNode(1),
|
2022-12-13 21:44:14 +08:00
|
|
|
|
n2 = new TreeNode(2),
|
|
|
|
|
n3 = new TreeNode(3),
|
|
|
|
|
n4 = new TreeNode(4),
|
|
|
|
|
n5 = new TreeNode(5);
|
2022-12-04 12:26:09 +08:00
|
|
|
|
// 构建引用指向(即指针)
|
|
|
|
|
n1.left = n2;
|
|
|
|
|
n1.right = n3;
|
|
|
|
|
n2.left = n4;
|
|
|
|
|
n2.right = n5;
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="binary_tree.ts"
|
2022-12-15 00:00:40 +08:00
|
|
|
|
/* 初始化二叉树 */
|
|
|
|
|
// 初始化结点
|
|
|
|
|
let n1 = new TreeNode(1),
|
|
|
|
|
n2 = new TreeNode(2),
|
|
|
|
|
n3 = new TreeNode(3),
|
|
|
|
|
n4 = new TreeNode(4),
|
|
|
|
|
n5 = new TreeNode(5);
|
|
|
|
|
// 构建引用指向(即指针)
|
|
|
|
|
n1.left = n2;
|
|
|
|
|
n1.right = n3;
|
|
|
|
|
n2.left = n4;
|
|
|
|
|
n2.right = n5;
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="binary_tree.c"
|
2022-12-21 17:19:39 +08:00
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="binary_tree.cs"
|
2022-12-23 15:42:02 +08:00
|
|
|
|
/* 初始化二叉树 */
|
|
|
|
|
// 初始化结点
|
|
|
|
|
TreeNode n1 = new TreeNode(1);
|
|
|
|
|
TreeNode n2 = new TreeNode(2);
|
|
|
|
|
TreeNode n3 = new TreeNode(3);
|
|
|
|
|
TreeNode n4 = new TreeNode(4);
|
|
|
|
|
TreeNode n5 = new TreeNode(5);
|
|
|
|
|
// 构建引用指向(即指针)
|
|
|
|
|
n1.left = n2;
|
|
|
|
|
n1.right = n3;
|
|
|
|
|
n2.left = n4;
|
|
|
|
|
n2.right = n5;
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-01-08 19:41:05 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="binary_tree.swift"
|
2023-01-18 21:34:44 +08:00
|
|
|
|
// 初始化结点
|
|
|
|
|
let n1 = TreeNode(x: 1)
|
|
|
|
|
let n2 = TreeNode(x: 2)
|
|
|
|
|
let n3 = TreeNode(x: 3)
|
|
|
|
|
let n4 = TreeNode(x: 4)
|
|
|
|
|
let n5 = TreeNode(x: 5)
|
|
|
|
|
// 构建引用指向(即指针)
|
|
|
|
|
n1.left = n2
|
|
|
|
|
n1.right = n3
|
|
|
|
|
n2.left = n4
|
|
|
|
|
n2.right = n5
|
2023-01-08 19:41:05 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-02-01 22:03:04 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="binary_tree.zig"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2023-01-07 20:34:32 +08:00
|
|
|
|
**插入与删除结点**。与链表类似,插入与删除结点都可以通过修改指针实现。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-02-26 18:18:34 +08:00
|
|
|
|
![在二叉树中插入与删除结点](binary_tree.assets/binary_tree_add_remove.png)
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2022-11-29 02:21:49 +08:00
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="binary_tree.java"
|
|
|
|
|
TreeNode P = new TreeNode(0);
|
|
|
|
|
// 在 n1 -> n2 中间插入结点 P
|
|
|
|
|
n1.left = P;
|
|
|
|
|
P.left = n2;
|
|
|
|
|
// 删除结点 P
|
|
|
|
|
n1.left = n2;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="binary_tree.cpp"
|
|
|
|
|
/* 插入与删除结点 */
|
|
|
|
|
TreeNode* P = new TreeNode(0);
|
|
|
|
|
// 在 n1 -> n2 中间插入结点 P
|
|
|
|
|
n1->left = P;
|
|
|
|
|
P->left = n2;
|
|
|
|
|
// 删除结点 P
|
|
|
|
|
n1->left = n2;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="binary_tree.py"
|
2022-12-27 19:33:58 +08:00
|
|
|
|
""" 插入与删除结点 """
|
2022-12-20 15:52:00 +08:00
|
|
|
|
p = TreeNode(0)
|
|
|
|
|
# 在 n1 -> n2 中间插入结点 P
|
|
|
|
|
n1.left = p
|
|
|
|
|
p.left = n2
|
2023-02-07 04:43:52 +08:00
|
|
|
|
# 删除结点 P
|
2022-12-20 15:52:00 +08:00
|
|
|
|
n1.left = n2
|
2022-11-29 02:21:49 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="binary_tree.go"
|
2022-12-03 20:25:24 +08:00
|
|
|
|
/* 插入与删除结点 */
|
|
|
|
|
// 在 n1 -> n2 中间插入结点 P
|
|
|
|
|
p := NewTreeNode(0)
|
|
|
|
|
n1.Left = p
|
|
|
|
|
p.Left = n2
|
|
|
|
|
// 删除结点 P
|
|
|
|
|
n1.Left = n2
|
2022-11-29 02:21:49 +08:00
|
|
|
|
```
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
2023-02-08 04:27:55 +08:00
|
|
|
|
```javascript title="binary_tree.js"
|
2022-12-04 12:26:09 +08:00
|
|
|
|
/* 插入与删除结点 */
|
|
|
|
|
let P = new TreeNode(0);
|
|
|
|
|
// 在 n1 -> n2 中间插入结点 P
|
|
|
|
|
n1.left = P;
|
|
|
|
|
P.left = n2;
|
|
|
|
|
// 删除结点 P
|
|
|
|
|
n1.left = n2;
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="binary_tree.ts"
|
2022-12-15 00:00:40 +08:00
|
|
|
|
/* 插入与删除结点 */
|
|
|
|
|
const P = new TreeNode(0);
|
|
|
|
|
// 在 n1 -> n2 中间插入结点 P
|
|
|
|
|
n1.left = P;
|
|
|
|
|
P.left = n2;
|
|
|
|
|
// 删除结点 P
|
|
|
|
|
n1.left = n2;
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="binary_tree.c"
|
2022-12-21 17:19:39 +08:00
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="binary_tree.cs"
|
2022-12-23 15:42:02 +08:00
|
|
|
|
/* 插入与删除结点 */
|
|
|
|
|
TreeNode P = new TreeNode(0);
|
|
|
|
|
// 在 n1 -> n2 中间插入结点 P
|
|
|
|
|
n1.left = P;
|
|
|
|
|
P.left = n2;
|
|
|
|
|
// 删除结点 P
|
|
|
|
|
n1.left = n2;
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-01-08 19:41:05 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="binary_tree.swift"
|
2023-01-18 21:34:44 +08:00
|
|
|
|
let P = TreeNode(x: 0)
|
|
|
|
|
// 在 n1 -> n2 中间插入结点 P
|
|
|
|
|
n1.left = P
|
|
|
|
|
P.left = n2
|
|
|
|
|
// 删除结点 P
|
|
|
|
|
n1.left = n2
|
2023-01-08 19:41:05 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-02-01 22:03:04 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="binary_tree.zig"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2022-11-22 17:47:26 +08:00
|
|
|
|
!!! note
|
|
|
|
|
|
|
|
|
|
插入结点会改变二叉树的原有逻辑结构,删除结点往往意味着删除了该结点的所有子树。因此,二叉树中的插入与删除一般都是由一套操作配合完成的,这样才能实现有意义的操作。
|
|
|
|
|
|
2023-02-16 03:39:01 +08:00
|
|
|
|
## 常见二叉树类型
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2022-12-21 17:19:39 +08:00
|
|
|
|
### 完美二叉树
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-04-06 22:03:46 +08:00
|
|
|
|
「完美二叉树 Perfect Binary Tree」的所有层的结点都被完全填满。在完美二叉树中,叶结点的度为 $0$ ,其余所有结点的度都为 $2$ ;若树高度 $= h$ ,则结点总数 $= 2^{h+1} - 1$ ,呈标准的指数级关系,反映着自然界中常见的细胞分裂。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2022-12-21 17:19:39 +08:00
|
|
|
|
!!! tip
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2022-12-21 17:19:39 +08:00
|
|
|
|
在中文社区中,完美二叉树常被称为「满二叉树」,请注意与完满二叉树区分。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-02-26 18:18:34 +08:00
|
|
|
|
![完美二叉树](binary_tree.assets/perfect_binary_tree.png)
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2022-12-21 17:19:39 +08:00
|
|
|
|
### 完全二叉树
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2022-12-21 17:19:39 +08:00
|
|
|
|
「完全二叉树 Complete Binary Tree」只有最底层的结点未被填满,且最底层结点尽量靠左填充。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2022-12-21 17:19:39 +08:00
|
|
|
|
**完全二叉树非常适合用数组来表示**。如果按照层序遍历序列的顺序来存储,那么空结点 `null` 一定全部出现在序列的尾部,因此我们就可以不用存储这些 null 了。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-02-26 18:18:34 +08:00
|
|
|
|
![完全二叉树](binary_tree.assets/complete_binary_tree.png)
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2022-12-21 17:19:39 +08:00
|
|
|
|
### 完满二叉树
|
2022-11-29 02:21:49 +08:00
|
|
|
|
|
2022-12-21 17:19:39 +08:00
|
|
|
|
「完满二叉树 Full Binary Tree」除了叶结点之外,其余所有结点都有两个子结点。
|
2022-11-29 02:21:49 +08:00
|
|
|
|
|
2023-02-26 18:18:34 +08:00
|
|
|
|
![完满二叉树](binary_tree.assets/full_binary_tree.png)
|
2022-11-29 02:21:49 +08:00
|
|
|
|
|
2022-12-21 17:19:39 +08:00
|
|
|
|
### 平衡二叉树
|
2022-11-29 02:21:49 +08:00
|
|
|
|
|
2022-12-21 17:19:39 +08:00
|
|
|
|
「平衡二叉树 Balanced Binary Tree」中任意结点的左子树和右子树的高度之差的绝对值 $\leq 1$ 。
|
2022-11-29 02:21:49 +08:00
|
|
|
|
|
2023-02-26 18:18:34 +08:00
|
|
|
|
![平衡二叉树](binary_tree.assets/balanced_binary_tree.png)
|
2022-11-29 02:21:49 +08:00
|
|
|
|
|
2023-02-16 03:39:01 +08:00
|
|
|
|
## 二叉树的退化
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2022-12-21 17:19:39 +08:00
|
|
|
|
当二叉树的每层的结点都被填满时,达到「完美二叉树」;而当所有结点都偏向一边时,二叉树退化为「链表」。
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2022-12-21 17:19:39 +08:00
|
|
|
|
- 完美二叉树是一个二叉树的“最佳状态”,可以完全发挥出二叉树“分治”的优势;
|
|
|
|
|
- 链表则是另一个极端,各项操作都变为线性操作,时间复杂度退化至 $O(n)$ ;
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2023-02-26 18:18:34 +08:00
|
|
|
|
![二叉树的最佳与最二叉树的最佳和最差结构差情况](binary_tree.assets/binary_tree_corner_cases.png)
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2022-12-21 17:19:39 +08:00
|
|
|
|
如下表所示,在最佳和最差结构下,二叉树的叶结点数量、结点总数、高度等达到极大或极小值。
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2022-11-22 17:47:26 +08:00
|
|
|
|
<div class="center-table" markdown>
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2022-12-21 17:19:39 +08:00
|
|
|
|
| | 完美二叉树 | 链表 |
|
|
|
|
|
| ----------------------------- | ---------- | ---------- |
|
|
|
|
|
| 第 $i$ 层的结点数量 | $2^{i-1}$ | $1$ |
|
|
|
|
|
| 树的高度为 $h$ 时的叶结点数量 | $2^h$ | $1$ |
|
|
|
|
|
| 树的高度为 $h$ 时的结点总数 | $2^{h+1} - 1$ | $h + 1$ |
|
|
|
|
|
| 树的结点总数为 $n$ 时的高度 | $\log_2 (n+1) - 1$ | $n - 1$ |
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2022-11-22 17:47:26 +08:00
|
|
|
|
</div>
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2023-02-16 03:39:01 +08:00
|
|
|
|
## 二叉树表示方式 *
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2022-12-23 00:44:40 +08:00
|
|
|
|
我们一般使用二叉树的「链表表示」,即存储单位为结点 `TreeNode` ,结点之间通过指针(引用)相连接。本文前述示例代码展示了二叉树在链表表示下的各项基本操作。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2022-12-23 00:44:40 +08:00
|
|
|
|
那能否可以用「数组表示」二叉树呢?答案是肯定的。先来分析一个简单案例,给定一个「完美二叉树」,将结点按照层序遍历的顺序编号(从 0 开始),那么可以推导得出父结点索引与子结点索引之间的「映射公式」:**设结点的索引为 $i$ ,则该结点的左子结点索引为 $2i + 1$ 、右子结点索引为 $2i + 2$** 。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2022-12-23 00:44:40 +08:00
|
|
|
|
**本质上,映射公式的作用就是链表中的指针**。对于层序遍历序列中的任意结点,我们都可以使用映射公式来访问子结点。因此,可以直接使用层序遍历序列(即数组)来表示完美二叉树。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-02-26 18:18:34 +08:00
|
|
|
|
![完美二叉树的数组表示](binary_tree.assets/array_representation_mapping.png)
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2022-12-23 00:44:40 +08:00
|
|
|
|
然而,完美二叉树只是个例,二叉树中间层往往存在许多空结点(即 `null` ),而层序遍历序列并不包含这些空结点,并且我们无法单凭序列来猜测空结点的数量和分布位置,**即理论上存在许多种二叉树都符合该层序遍历序列**。显然,这种情况无法使用数组来存储二叉树。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-02-26 18:18:34 +08:00
|
|
|
|
![给定数组对应多种二叉树可能性](binary_tree.assets/array_representation_without_empty.png)
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2022-12-23 00:44:40 +08:00
|
|
|
|
为了解决此问题,考虑按照完美二叉树的形式来表示所有二叉树,**即在序列中使用特殊符号来显式地表示“空位”**。如下图所示,这样处理后,序列(数组)就可以唯一表示二叉树了。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
2022-12-23 00:44:40 +08:00
|
|
|
|
```java title=""
|
|
|
|
|
/* 二叉树的数组表示 */
|
|
|
|
|
// 使用 int 的包装类 Integer ,就可以使用 null 来标记空位
|
|
|
|
|
Integer[] tree = { 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 };
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-11-29 02:21:49 +08:00
|
|
|
|
=== "C++"
|
|
|
|
|
|
2022-12-23 00:44:40 +08:00
|
|
|
|
```cpp title=""
|
|
|
|
|
/* 二叉树的数组表示 */
|
|
|
|
|
// 为了符合数据类型为 int ,使用 int 最大值标记空位
|
|
|
|
|
// 该方法的使用前提是没有结点的值 = INT_MAX
|
|
|
|
|
vector<int> tree = { 1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15 };
|
2022-11-29 02:21:49 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
2022-12-23 00:44:40 +08:00
|
|
|
|
```python title=""
|
2022-12-27 19:33:58 +08:00
|
|
|
|
""" 二叉树的数组表示 """
|
2022-12-23 00:44:40 +08:00
|
|
|
|
# 直接使用 None 来表示空位
|
|
|
|
|
tree = [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]
|
2022-11-29 02:21:49 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
2022-12-23 00:44:40 +08:00
|
|
|
|
```go title=""
|
2023-03-16 18:42:06 +08:00
|
|
|
|
/* 二叉树的数组表示 */
|
|
|
|
|
// 使用 any 类型的切片, 就可以使用 nil 来标记空位
|
|
|
|
|
tree := []any{1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15}
|
2022-11-29 02:21:49 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
2023-02-08 04:27:55 +08:00
|
|
|
|
```javascript title=""
|
2022-12-24 17:05:58 +08:00
|
|
|
|
/* 二叉树的数组表示 */
|
|
|
|
|
// 直接使用 null 来表示空位
|
2022-12-27 11:34:41 +08:00
|
|
|
|
let tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
2022-12-23 00:44:40 +08:00
|
|
|
|
```typescript title=""
|
2022-12-24 17:05:58 +08:00
|
|
|
|
/* 二叉树的数组表示 */
|
|
|
|
|
// 直接使用 null 来表示空位
|
2022-12-27 11:51:04 +08:00
|
|
|
|
let tree: (number | null)[] = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
2022-12-23 00:44:40 +08:00
|
|
|
|
```c title=""
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
2022-12-23 00:44:40 +08:00
|
|
|
|
```csharp title=""
|
2022-12-23 15:42:02 +08:00
|
|
|
|
/* 二叉树的数组表示 */
|
2022-12-24 17:05:58 +08:00
|
|
|
|
// 使用 int? 可空类型 ,就可以使用 null 来标记空位
|
2022-12-23 15:42:02 +08:00
|
|
|
|
int?[] tree = { 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 };
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-01-08 19:41:05 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title=""
|
2023-01-18 21:34:44 +08:00
|
|
|
|
/* 二叉树的数组表示 */
|
|
|
|
|
// 使用 Int? 可空类型 ,就可以使用 nil 来标记空位
|
|
|
|
|
let tree: [Int?] = [1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15]
|
2023-01-08 19:41:05 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-02-01 22:03:04 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title=""
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-26 18:18:34 +08:00
|
|
|
|
![任意类型二叉树的数组表示](binary_tree.assets/array_representation_with_empty.png)
|
2022-12-23 00:44:40 +08:00
|
|
|
|
|
2023-01-07 20:34:32 +08:00
|
|
|
|
回顾「完全二叉树」的定义,其只有最底层有空结点,并且最底层的结点尽量靠左,因而所有空结点都一定出现在层序遍历序列的末尾。**因为我们先验地确定了空位的位置,所以在使用数组表示完全二叉树时,可以省略存储“空位”**。因此,完全二叉树非常适合使用数组来表示。
|
2022-12-23 00:44:40 +08:00
|
|
|
|
|
2023-02-26 18:18:34 +08:00
|
|
|
|
![完全二叉树的数组表示](binary_tree.assets/array_representation_complete_binary_tree.png)
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2022-12-23 00:44:40 +08:00
|
|
|
|
数组表示有两个优点: 一是不需要存储指针,节省空间;二是可以随机访问结点。然而,当二叉树中的“空位”很多时,数组中只包含很少结点的数据,空间利用率很低。
|