feat: Revised the book (#978)

* Sync recent changes to the revised Word.

* Revised the preface chapter

* Revised the introduction chapter

* Revised the computation complexity chapter

* Revised the chapter data structure

* Revised the chapter array and linked list

* Revised the chapter stack and queue

* Revised the chapter hashing

* Revised the chapter tree

* Revised the chapter heap

* Revised the chapter graph

* Revised the chapter searching

* Reivised the sorting chapter

* Revised the divide and conquer chapter

* Revised the chapter backtacking

* Revised the DP chapter

* Revised the greedy chapter

* Revised the appendix chapter

* Revised the preface chapter doubly

* Revised the figures
This commit is contained in:
Yudong Jin
2023-12-02 06:21:34 +08:00
committed by GitHub
parent b824d149cb
commit e720aa2d24
404 changed files with 1537 additions and 1558 deletions

View File

@@ -31,7 +31,7 @@
=== "<4>"
![bst_search_step4](binary_search_tree.assets/bst_search_step4.png)
二叉搜索树的查找操作与二分查找算法的工作原理一致,都是每轮排除一半情况。循环次数最多为二叉树的高度,当二叉树平衡时,使用 $O(\log n)$ 时间。
二叉搜索树的查找操作与二分查找算法的工作原理一致,都是每轮排除一半情况。循环次数最多为二叉树的高度,当二叉树平衡时,使用 $O(\log n)$ 时间。示例代码如下:
```src
[file]{binary_search_tree}-[class]{binary_search_tree}-[func]{search}
@@ -59,11 +59,11 @@
### 删除节点
先在二叉树中查找到目标节点,再将其从二叉树中删除。
先在二叉树中查找到目标节点,再将其删除。
与插入节点类似,我们需要保证在删除操作完成后,二叉搜索树的“左子树 < 根节点 < 右子树”的性质仍然满足。
因此,我们需要根据目标节点的子节点数量,共分为 0、1 和 2 三种情况,执行对应的删除节点操作。
因此,我们根据目标节点的子节点数量, 0、1 和 2 三种情况,执行对应的删除节点操作。
如下图所示,当待删除节点的度为 $0$ 时,表示该节点是叶节点,可以直接删除。
@@ -73,12 +73,12 @@
![在二叉搜索树中删除节点(度为 1 ](binary_search_tree.assets/bst_remove_case2.png)
当待删除节点的度为 $2$ 时,我们无法直接删除它,而需要使用一个节点替换该节点。由于要保持二叉搜索树“左 $<$ 根 $<$ 右”的性质,**因此这个节点可以是右子树的最小节点或左子树的最大节点**。
当待删除节点的度为 $2$ 时,我们无法直接删除它,而需要使用一个节点替换该节点。由于要保持二叉搜索树“左子树 $<$ 根节点 $<$ 右子树”的性质,**因此这个节点可以是右子树的最小节点或左子树的最大节点**。
假设我们选择右子树的最小节点(中序遍历的下一个节点),则删除操作流程如下图所示。
假设我们选择右子树的最小节点(中序遍历的下一个节点),则删除操作流程如下图所示。
1. 找到待删除节点在“中序遍历序列”中的下一个节点,记为 `tmp`
2. `tmp` 的值覆盖待删除节点的值,并在树中递归删除节点 `tmp`
2. `tmp` 的值覆盖待删除节点的值,并在树中递归删除节点 `tmp`
=== "<1>"
![在二叉搜索树中删除节点(度为 2 ](binary_search_tree.assets/bst_remove_case3_step1.png)
@@ -92,7 +92,7 @@
=== "<4>"
![bst_remove_case3_step4](binary_search_tree.assets/bst_remove_case3_step4.png)
删除节点操作同样使用 $O(\log n)$ 时间,其中查找待删除节点需要 $O(\log n)$ 时间,获取中序遍历后继节点需要 $O(\log n)$ 时间。
删除节点操作同样使用 $O(\log n)$ 时间,其中查找待删除节点需要 $O(\log n)$ 时间,获取中序遍历后继节点需要 $O(\log n)$ 时间。示例代码如下:
```src
[file]{binary_search_tree}-[class]{binary_search_tree}-[func]{remove}
@@ -110,7 +110,7 @@
## 二叉搜索树的效率
给定一组数据,我们考虑使用数组或二叉搜索树存储。观察下表,二叉搜索树的各项操作的时间复杂度都是对数阶,具有稳定且高效的性能表现。只有在高频添加、低频查找删除数据适用场景下,数组比二叉搜索树的效率更高。
给定一组数据,我们考虑使用数组或二叉搜索树存储。观察下表,二叉搜索树的各项操作的时间复杂度都是对数阶,具有稳定且高效的性能。只有在高频添加、低频查找删除数据场景下,数组比二叉搜索树的效率更高。
<p align="center"> 表 <id> &nbsp; 数组与搜索树的效率对比 </p>
@@ -124,7 +124,7 @@
然而,如果我们在二叉搜索树中不断地插入和删除节点,可能导致二叉树退化为下图所示的链表,这时各种操作的时间复杂度也会退化为 $O(n)$ 。
![二叉搜索树退化](binary_search_tree.assets/bst_degradation.png)
![二叉搜索树退化](binary_search_tree.assets/bst_degradation.png)
## 二叉搜索树常见应用