2023-02-28 20:03:53 +08:00
|
|
|
|
# 二叉树
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-04-10 23:59:22 +08:00
|
|
|
|
「二叉树 Binary Tree」是一种非线性数据结构,代表着祖先与后代之间的派生关系,体现着“一分为二”的分治逻辑。与链表类似,二叉树的基本单元是节点,每个节点包含一个「值」和两个「指针」。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```java title=""
|
2023-04-09 04:32:17 +08:00
|
|
|
|
/* 二叉树节点类 */
|
2022-11-22 17:47:26 +08:00
|
|
|
|
class TreeNode {
|
2023-04-09 04:32:17 +08:00
|
|
|
|
int val; // 节点值
|
|
|
|
|
TreeNode left; // 左子节点指针
|
|
|
|
|
TreeNode right; // 右子节点指针
|
2022-11-22 17:47:26 +08:00
|
|
|
|
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-04-09 04:32:17 +08:00
|
|
|
|
/* 二叉树节点结构体 */
|
2022-11-29 02:21:49 +08:00
|
|
|
|
struct TreeNode {
|
2023-04-09 04:32:17 +08:00
|
|
|
|
int val; // 节点值
|
|
|
|
|
TreeNode *left; // 左子节点指针
|
|
|
|
|
TreeNode *right; // 右子节点指针
|
2022-11-29 02:21:49 +08:00
|
|
|
|
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```python title=""
|
2022-11-29 02:21:49 +08:00
|
|
|
|
class TreeNode:
|
2023-04-09 05:30:02 +08:00
|
|
|
|
"""二叉树节点类"""
|
2023-03-12 18:49:52 +08:00
|
|
|
|
def __init__(self, val: int):
|
2023-04-09 04:32:17 +08:00
|
|
|
|
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-04-09 04:32:17 +08:00
|
|
|
|
/* 二叉树节点结构体 */
|
2022-12-03 20:25:24 +08:00
|
|
|
|
type TreeNode struct {
|
|
|
|
|
Val int
|
|
|
|
|
Left *TreeNode
|
|
|
|
|
Right *TreeNode
|
|
|
|
|
}
|
2023-04-09 04:32:17 +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-04-09 04:32:17 +08:00
|
|
|
|
/* 二叉树节点类 */
|
2022-12-04 12:26:09 +08:00
|
|
|
|
function TreeNode(val, left, right) {
|
2023-04-09 04:32:17 +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-04-09 04:32:17 +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) {
|
2023-04-09 04:32:17 +08:00
|
|
|
|
this.val = val === undefined ? 0 : val; // 节点值
|
|
|
|
|
this.left = left === undefined ? null : left; // 左子节点指针
|
|
|
|
|
this.right = right === undefined ? null : right; // 右子节点指针
|
2022-12-15 00:00:40 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
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-04-09 04:32:17 +08:00
|
|
|
|
/* 二叉树节点类 */
|
2022-12-23 15:42:02 +08:00
|
|
|
|
class TreeNode {
|
2023-04-09 04:32:17 +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-04-09 04:32:17 +08:00
|
|
|
|
/* 二叉树节点类 */
|
2023-01-18 21:34:44 +08:00
|
|
|
|
class TreeNode {
|
2023-04-09 04:32:17 +08:00
|
|
|
|
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-10 23:59:22 +08:00
|
|
|
|
节点的两个指针分别指向「左子节点」和「右子节点」,同时该节点被称为这两个子节点的「父节点」。当给定一个二叉树的节点时,我们将该节点的左子节点及其以下节点形成的树称为该节点的「左子树」,同理可得「右子树」。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-04-10 23:59:22 +08:00
|
|
|
|
**在二叉树中,除叶节点外,其他所有节点都包含子节点和非空子树**。例如,在以下示例中,若将“节点 2”视为父节点,则其左子节点和右子节点分别是“节点 4”和“节点 5”,左子树是“节点 4 及其以下节点形成的树”,右子树是“节点 5 及其以下节点形成的树”。
|
2022-12-25 14:05:42 +08:00
|
|
|
|
|
2023-04-09 04:32:17 +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
|
|
|
|
|
2023-04-10 23:59:22 +08:00
|
|
|
|
二叉树涉及的术语较多,建议尽量理解并记住。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-04-10 23:59:22 +08:00
|
|
|
|
- 「根节点 Root Node」:位于二叉树顶层的节点,没有父节点;
|
|
|
|
|
- 「叶节点 Leaf Node」:没有子节点的节点,其两个指针均指向 $\text{null}$ ;
|
|
|
|
|
- 节点的「层 Level」:从顶至底递增,根节点所在层为 1 ;
|
|
|
|
|
- 节点的「度 Degree」:节点的子节点的数量。在二叉树中,度的范围是 0, 1, 2 ;
|
|
|
|
|
- 「边 Edge」:连接两个节点的线段,即节点指针;
|
|
|
|
|
- 二叉树的「高度」:从根节点到最远叶节点所经过的边的数量;
|
|
|
|
|
- 节点的「深度 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 "高度与深度的定义"
|
|
|
|
|
|
2023-04-10 23:59:22 +08:00
|
|
|
|
请注意,我们通常将「高度」和「深度」定义为“走过边的数量”,但有些题目或教材可能会将其定义为“走过节点的数量”。在这种情况下,高度和深度都需要加 1 。
|
2022-12-18 15:59:34 +08:00
|
|
|
|
|
2023-02-16 03:39:01 +08:00
|
|
|
|
## 二叉树基本操作
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-04-10 23:59:22 +08:00
|
|
|
|
**初始化二叉树**。与链表类似,首先初始化节点,然后构建引用指向(即指针)。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="binary_tree.java"
|
2023-04-09 04:32:17 +08:00
|
|
|
|
// 初始化节点
|
2022-11-22 17:47:26 +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-11-29 02:21:49 +08:00
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="binary_tree.cpp"
|
|
|
|
|
/* 初始化二叉树 */
|
2023-04-09 04:32:17 +08:00
|
|
|
|
// 初始化节点
|
2022-11-29 02:21:49 +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;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="binary_tree.py"
|
2023-04-09 05:30:02 +08:00
|
|
|
|
# 初始化二叉树
|
2023-04-09 04:32:17 +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
|
|
|
|
/* 初始化二叉树 */
|
2023-04-09 04:32:17 +08:00
|
|
|
|
// 初始化节点
|
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
|
|
|
|
/* 初始化二叉树 */
|
2023-04-09 04:32:17 +08:00
|
|
|
|
// 初始化节点
|
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
|
|
|
|
/* 初始化二叉树 */
|
2023-04-09 04:32:17 +08:00
|
|
|
|
// 初始化节点
|
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
|
|
|
|
/* 初始化二叉树 */
|
2023-04-09 04:32:17 +08:00
|
|
|
|
// 初始化节点
|
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-04-09 04:32:17 +08:00
|
|
|
|
// 初始化节点
|
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-04-10 23:59:22 +08:00
|
|
|
|
**插入与删除节点**。与链表类似,通过修改指针来实现插入与删除节点。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-04-09 04:32:17 +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);
|
2023-04-09 04:32:17 +08:00
|
|
|
|
// 在 n1 -> n2 中间插入节点 P
|
2022-11-29 02:21:49 +08:00
|
|
|
|
n1.left = P;
|
|
|
|
|
P.left = n2;
|
2023-04-09 04:32:17 +08:00
|
|
|
|
// 删除节点 P
|
2022-11-29 02:21:49 +08:00
|
|
|
|
n1.left = n2;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="binary_tree.cpp"
|
2023-04-09 04:32:17 +08:00
|
|
|
|
/* 插入与删除节点 */
|
2022-11-29 02:21:49 +08:00
|
|
|
|
TreeNode* P = new TreeNode(0);
|
2023-04-09 04:32:17 +08:00
|
|
|
|
// 在 n1 -> n2 中间插入节点 P
|
2022-11-29 02:21:49 +08:00
|
|
|
|
n1->left = P;
|
|
|
|
|
P->left = n2;
|
2023-04-09 04:32:17 +08:00
|
|
|
|
// 删除节点 P
|
2022-11-29 02:21:49 +08:00
|
|
|
|
n1->left = n2;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="binary_tree.py"
|
2023-04-09 05:30:02 +08:00
|
|
|
|
# 插入与删除节点
|
2022-12-20 15:52:00 +08:00
|
|
|
|
p = TreeNode(0)
|
2023-04-09 04:32:17 +08:00
|
|
|
|
# 在 n1 -> n2 中间插入节点 P
|
2022-12-20 15:52:00 +08:00
|
|
|
|
n1.left = p
|
|
|
|
|
p.left = n2
|
2023-04-09 04:32:17 +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"
|
2023-04-09 04:32:17 +08:00
|
|
|
|
/* 插入与删除节点 */
|
|
|
|
|
// 在 n1 -> n2 中间插入节点 P
|
2022-12-03 20:25:24 +08:00
|
|
|
|
p := NewTreeNode(0)
|
|
|
|
|
n1.Left = p
|
|
|
|
|
p.Left = n2
|
2023-04-09 04:32:17 +08:00
|
|
|
|
// 删除节点 P
|
2022-12-03 20:25:24 +08:00
|
|
|
|
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"
|
2023-04-09 04:32:17 +08:00
|
|
|
|
/* 插入与删除节点 */
|
2022-12-04 12:26:09 +08:00
|
|
|
|
let P = new TreeNode(0);
|
2023-04-09 04:32:17 +08:00
|
|
|
|
// 在 n1 -> n2 中间插入节点 P
|
2022-12-04 12:26:09 +08:00
|
|
|
|
n1.left = P;
|
|
|
|
|
P.left = n2;
|
2023-04-09 04:32:17 +08:00
|
|
|
|
// 删除节点 P
|
2022-12-04 12:26:09 +08:00
|
|
|
|
n1.left = n2;
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="binary_tree.ts"
|
2023-04-09 04:32:17 +08:00
|
|
|
|
/* 插入与删除节点 */
|
2022-12-15 00:00:40 +08:00
|
|
|
|
const P = new TreeNode(0);
|
2023-04-09 04:32:17 +08:00
|
|
|
|
// 在 n1 -> n2 中间插入节点 P
|
2022-12-15 00:00:40 +08:00
|
|
|
|
n1.left = P;
|
|
|
|
|
P.left = n2;
|
2023-04-09 04:32:17 +08:00
|
|
|
|
// 删除节点 P
|
2022-12-15 00:00:40 +08:00
|
|
|
|
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"
|
2023-04-09 04:32:17 +08:00
|
|
|
|
/* 插入与删除节点 */
|
2022-12-23 15:42:02 +08:00
|
|
|
|
TreeNode P = new TreeNode(0);
|
2023-04-09 04:32:17 +08:00
|
|
|
|
// 在 n1 -> n2 中间插入节点 P
|
2022-12-23 15:42:02 +08:00
|
|
|
|
n1.left = P;
|
|
|
|
|
P.left = n2;
|
2023-04-09 04:32:17 +08:00
|
|
|
|
// 删除节点 P
|
2022-12-23 15:42:02 +08:00
|
|
|
|
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)
|
2023-04-09 04:32:17 +08:00
|
|
|
|
// 在 n1 -> n2 中间插入节点 P
|
2023-01-18 21:34:44 +08:00
|
|
|
|
n1.left = P
|
|
|
|
|
P.left = n2
|
2023-04-09 04:32:17 +08:00
|
|
|
|
// 删除节点 P
|
2023-01-18 21:34:44 +08:00
|
|
|
|
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-04-10 23:59:22 +08:00
|
|
|
|
需要注意的是,插入节点可能会改变二叉树的原有逻辑结构,而删除节点通常意味着删除该节点及其所有子树。因此,在二叉树中,插入与删除操作通常是由一套操作配合完成的,以实现有实际意义的操作。
|
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-21 17:19:39 +08:00
|
|
|
|
### 完美二叉树
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-04-10 23:59:22 +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
|
|
|
|
|
2023-04-10 23:59:22 +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
|
|
|
|
|
2023-04-09 04:32:17 +08:00
|
|
|
|
「完全二叉树 Complete Binary Tree」只有最底层的节点未被填满,且最底层节点尽量靠左填充。
|
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
|
|
|
|
|
2023-04-09 04:32:17 +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
|
|
|
|
|
2023-04-10 23:59:22 +08:00
|
|
|
|
「平衡二叉树 Balanced Binary Tree」中任意节点的左子树和右子树的高度之差的绝对值不超过 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
|
|
|
|
|
2023-04-10 23:59:22 +08:00
|
|
|
|
当二叉树的每层节点都被填满时,达到「完美二叉树」;而当所有节点都偏向一侧时,二叉树退化为「链表」。
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2023-04-10 23:59:22 +08:00
|
|
|
|
- 完美二叉树是理想情况,可以充分发挥二叉树“分治”的优势;
|
2022-12-21 17:19:39 +08:00
|
|
|
|
- 链表则是另一个极端,各项操作都变为线性操作,时间复杂度退化至 $O(n)$ ;
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2023-04-13 22:44:59 +08:00
|
|
|
|
![二叉树的最佳与最差结构](binary_tree.assets/binary_tree_corner_cases.png)
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2023-04-09 04:32:17 +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
|
|
|
|
| | 完美二叉树 | 链表 |
|
|
|
|
|
| ----------------------------- | ---------- | ---------- |
|
2023-04-09 04:32:17 +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>
|