mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-24 23:50:45 +08:00
Book pages keep placeholders only; all code now lives under each edition's codes/. - The 32 inline 13-language tab groups (17 pages) and the 18 PythonTutor URLs written directly in `??? pythontutor` blocks move, per edition, to `codes/snippets/<chapter>/<page>.md`. Pages refer to them with a `snippet` fence that holds the entry ID and with `@tutor <ID>`; the workspace build restores the exact previous Markdown, so the generated docs and site are unchanged. - `codes/code_links.json`, the `src` mapping shared by every edition, moves here from the workspace repository unchanged, so a new `src` reference needs only a source PR. Snippet code is kept byte for byte, including one Rust line with trailing spaces in space_complexity. zh-hant is regenerated by OpenCC. Co-authored-by: Claude Opus 5.5 <[email protected]>
115 lines
5.6 KiB
Markdown
115 lines
5.6 KiB
Markdown
# 二叉树
|
||
|
||
<u>二叉树(binary tree)</u>是一种非线性数据结构,代表“祖先”与“后代”之间的派生关系,体现了“一分为二”的分治逻辑。与链表类似,二叉树的基本单元是节点,每个节点包含值、左子节点引用和右子节点引用。
|
||
|
||
```snippet
|
||
tree_node_class
|
||
```
|
||
|
||
每个节点都有两个引用(指针),分别指向<u>左子节点(left-child node)</u>和<u>右子节点(right-child node)</u>,该节点被称为这两个子节点的<u>父节点(parent node)</u>。当给定一个二叉树的节点时,我们将该节点的左子节点及其以下节点形成的树称为该节点的<u>左子树(left subtree)</u>,同理可得<u>右子树(right subtree)</u>。
|
||
|
||
**在二叉树中,除叶节点外,其他所有节点都包含子节点和非空子树**。如下图所示,如果将“节点 2”视为父节点,则其左子节点和右子节点分别是“节点 4”和“节点 5”,左子树是“节点 4 及其以下节点形成的树”,右子树是“节点 5 及其以下节点形成的树”。
|
||
|
||

|
||
|
||
## 二叉树常见术语
|
||
|
||
二叉树的常用术语如下图所示。
|
||
|
||
- <u>根节点(root node)</u>:位于二叉树顶层的节点,没有父节点。
|
||
- <u>叶节点(leaf node)</u>:没有子节点的节点,其两个指针均指向 `None` 。
|
||
- <u>边(edge)</u>:连接两个节点的线段,即节点引用(指针)。
|
||
- 节点所在的<u>层(level)</u>:从顶至底递增,根节点所在层为 1 。
|
||
- 节点的<u>度(degree)</u>:节点的子节点的数量。在二叉树中,度的取值范围是 0、1、2 。
|
||
- 二叉树的<u>高度(height)</u>:从根节点到最远叶节点所经过的边的数量。
|
||
- 节点的<u>深度(depth)</u>:从根节点到该节点所经过的边的数量。
|
||
- 节点的<u>高度(height)</u>:从距离该节点最远的叶节点到该节点所经过的边的数量。
|
||
|
||

|
||
|
||
!!! tip
|
||
|
||
请注意,我们通常将“高度”和“深度”定义为“经过的边的数量”,但有些题目或教材可能会将其定义为“经过的节点的数量”。在这种情况下,高度和深度都需要加 1 。
|
||
|
||
## 二叉树基本操作
|
||
|
||
### 初始化二叉树
|
||
|
||
与链表类似,首先初始化节点,然后构建引用(指针)。
|
||
|
||
```snippet
|
||
init_binary_tree
|
||
```
|
||
|
||
??? pythontutor "可视化运行"
|
||
|
||
@tutor init_binary_tree_tutor
|
||
|
||
### 插入与删除节点
|
||
|
||
与链表类似,在二叉树中插入与删除节点可以通过修改指针来实现。下图给出了一个示例。
|
||
|
||

|
||
|
||
```snippet
|
||
insert_remove_node
|
||
```
|
||
|
||
??? pythontutor "可视化运行"
|
||
|
||
@tutor insert_remove_node_tutor
|
||
|
||
!!! tip
|
||
|
||
需要注意的是,插入节点可能会改变二叉树的原有逻辑结构,而删除节点通常意味着删除该节点及其所有子树。因此,在二叉树中,插入与删除通常是由一套操作配合完成的,以实现有实际意义的操作。
|
||
|
||
## 常见二叉树类型
|
||
|
||
### 完美二叉树
|
||
|
||
如下图所示,<u>完美二叉树(perfect binary tree)</u>所有层的节点都被完全填满。在完美二叉树中,叶节点的度为 $0$ ,其余所有节点的度都为 $2$ ;若树的高度为 $h$ ,则节点总数为 $2^{h+1} - 1$ ,呈现标准的指数级关系,反映了自然界中常见的细胞分裂现象。
|
||
|
||
!!! tip
|
||
|
||
请注意,在中文社区中,完美二叉树常被称为<u>满二叉树</u>。
|
||
|
||

|
||
|
||
### 完全二叉树
|
||
|
||
如下图所示,<u>完全二叉树(complete binary tree)</u>仅允许最底层的节点不完全填满,且最底层的节点必须从左至右依次连续填充。请注意,完美二叉树也是一棵完全二叉树。
|
||
|
||

|
||
|
||
### 完满二叉树
|
||
|
||
如下图所示,<u>完满二叉树(full binary tree)</u>除了叶节点之外,其余所有节点都有两个子节点。
|
||
|
||

|
||
|
||
### 平衡二叉树
|
||
|
||
如下图所示,<u>平衡二叉树(balanced binary tree)</u>中任意节点的左子树和右子树的高度之差的绝对值不超过 1 。
|
||
|
||

|
||
|
||
## 二叉树的退化
|
||
|
||
下图展示了二叉树的理想结构与退化结构。当二叉树的每层节点都被填满时,达到“完美二叉树”;而当所有节点都偏向一侧时,二叉树退化为“链表”。
|
||
|
||
- 完美二叉树是理想情况,可以充分发挥二叉树“分治”的优势。
|
||
- 链表则是另一个极端,各项操作都变为线性操作,时间复杂度退化至 $O(n)$ 。
|
||
|
||

|
||
|
||
如下表所示,在最佳结构和最差结构下,二叉树的叶节点数量、节点总数、高度等达到极大值或极小值。
|
||
|
||
<p align="center"> 表 <id> 二叉树的最佳结构与最差结构 </p>
|
||
|
||
| | 完美二叉树 | 链表 |
|
||
| --------------------------- | ------------------ | ------- |
|
||
| 第 $i$ 层的节点数量 | $2^{i-1}$ | $1$ |
|
||
| 高度为 $h$ 的树的叶节点数量 | $2^h$ | $1$ |
|
||
| 高度为 $h$ 的树的节点总数 | $2^{h+1} - 1$ | $h + 1$ |
|
||
| 节点总数为 $n$ 的树的高度 | $\log_2 (n+1) - 1$ | $n - 1$ |
|