diff --git a/docs/chapter_array_and_linkedlist/array.assets/array_insert_remove_element.png b/docs/chapter_array_and_linkedlist/array.assets/array_insert_element.png similarity index 100% rename from docs/chapter_array_and_linkedlist/array.assets/array_insert_remove_element.png rename to docs/chapter_array_and_linkedlist/array.assets/array_insert_element.png diff --git a/docs/chapter_array_and_linkedlist/array.assets/array_remove_element.png b/docs/chapter_array_and_linkedlist/array.assets/array_remove_element.png new file mode 100644 index 000000000..6e02d129b Binary files /dev/null and b/docs/chapter_array_and_linkedlist/array.assets/array_remove_element.png differ diff --git a/docs/chapter_array_and_linkedlist/array.md b/docs/chapter_array_and_linkedlist/array.md index f5e80f211..e2c42dfe6 100755 --- a/docs/chapter_array_and_linkedlist/array.md +++ b/docs/chapter_array_and_linkedlist/array.md @@ -243,96 +243,134 @@ elementAddr = firtstElementAddr + elementLength * elementIndex [class]{}-[func]{extend} ``` -**数组中插入或删除元素效率低下**。假设我们想要在数组中间某位置插入一个元素,由于数组元素在内存中是“紧挨着的”,它们之间没有空间再放任何数据。因此,我们不得不将此索引之后的所有元素都向后移动一位,然后再把元素赋值给该索引。删除元素也是类似,需要把此索引之后的元素都向前移动一位。总体看有以下缺点: +**数组中插入或删除元素效率低下**。如果我们想要在数组中间插入一个元素,由于数组元素在内存中是“紧挨着的”,它们之间没有空间再放任何数据。因此,我们不得不将此索引之后的所有元素都向后移动一位,然后再把元素赋值给该索引。 -- **时间复杂度高**:数组的插入和删除的平均时间复杂度均为 $O(N)$ ,其中 $N$ 为数组长度。 -- **丢失元素**:由于数组的长度不可变,因此在插入元素后,超出数组长度范围的元素会被丢失。 -- **内存浪费**:我们一般会初始化一个比较长的数组,只用前面一部分,这样在插入数据时,丢失的末尾元素都是我们不关心的,但这样做同时也会造成内存空间的浪费。 - -![array_insert_remove_element](array.assets/array_insert_remove_element.png) - -
Fig. 在数组中插入与删除元素
+![array_insert_element](array.assets/array_insert_element.png) === "Java" ```java title="array.java" [class]{array}-[func]{insert} - - [class]{array}-[func]{remove} ``` === "C++" ```cpp title="array.cpp" [class]{}-[func]{insert} - - [class]{}-[func]{remove} ``` === "Python" ```python title="array.py" [class]{}-[func]{insert} - - [class]{}-[func]{remove} ``` === "Go" ```go title="array.go" [class]{}-[func]{insert} - - [class]{}-[func]{remove} ``` === "JavaScript" ```javascript title="array.js" [class]{}-[func]{insert} - - [class]{}-[func]{remove} ``` === "TypeScript" ```typescript title="array.ts" [class]{}-[func]{insert} - - [class]{}-[func]{remove} ``` === "C" ```c title="array.c" [class]{}-[func]{insert} - - [class]{}-[func]{removeItem} ``` === "C#" ```csharp title="array.cs" [class]{array}-[func]{insert} - - [class]{array}-[func]{remove} ``` === "Swift" ```swift title="array.swift" [class]{}-[func]{insert} + ``` +删除元素也是类似,如果我们想要删除索引 $i$ 处的元素,则需要把索引 $i$ 之后的元素都向前移动一位。值得注意的是,删除元素后,原先末尾的元素变得“无意义”了,我们无需特意去修改它。 + +![array_remove_element](array.assets/array_remove_element.png) + +=== "Java" + + ```java title="array.java" + [class]{array}-[func]{remove} + ``` + +=== "C++" + + ```cpp title="array.cpp" + [class]{}-[func]{remove} + ``` + +=== "Python" + + ```python title="array.py" + [class]{}-[func]{remove} + ``` + +=== "Go" + + ```go title="array.go" + [class]{}-[func]{remove} + ``` + +=== "JavaScript" + + ```javascript title="array.js" + [class]{}-[func]{remove} + ``` + +=== "TypeScript" + + ```typescript title="array.ts" + [class]{}-[func]{remove} + ``` + +=== "C" + + ```c title="array.c" + [class]{}-[func]{removeItem} + ``` + +=== "C#" + + ```csharp title="array.cs" + [class]{array}-[func]{remove} + ``` + +=== "Swift" + + ```swift title="array.swift" [class]{}-[func]{remove} ``` === "Zig" ```zig title="array.zig" - [class]{}-[func]{insert} - [class]{}-[func]{remove} ``` +总结来看,数组的插入与删除操作有以下缺点: + +- **时间复杂度高**:数组的插入和删除的平均时间复杂度均为 $O(N)$ ,其中 $N$ 为数组长度。 +- **丢失元素**:由于数组的长度不可变,因此在插入元素后,超出数组长度范围的元素会被丢失。 +- **内存浪费**:我们一般会初始化一个比较长的数组,只用前面一部分,这样在插入数据时,丢失的末尾元素都是我们不关心的,但这样做同时也会造成内存空间的浪费。 + ## 数组常用操作 **数组遍历**。以下介绍两种常用的遍历方法。 diff --git a/docs/chapter_array_and_linkedlist/linked_list.assets/linkedlist_insert_remove_node.png b/docs/chapter_array_and_linkedlist/linked_list.assets/linkedlist_insert_node.png similarity index 100% rename from docs/chapter_array_and_linkedlist/linked_list.assets/linkedlist_insert_remove_node.png rename to docs/chapter_array_and_linkedlist/linked_list.assets/linkedlist_insert_node.png diff --git a/docs/chapter_array_and_linkedlist/linked_list.assets/linkedlist_remove_node.png b/docs/chapter_array_and_linkedlist/linked_list.assets/linkedlist_remove_node.png new file mode 100644 index 000000000..3ab4da38c Binary files /dev/null and b/docs/chapter_array_and_linkedlist/linked_list.assets/linkedlist_remove_node.png differ diff --git a/docs/chapter_array_and_linkedlist/linked_list.md b/docs/chapter_array_and_linkedlist/linked_list.md index 834be28e1..bf8c3b05e 100755 --- a/docs/chapter_array_and_linkedlist/linked_list.md +++ b/docs/chapter_array_and_linkedlist/linked_list.md @@ -316,89 +316,131 @@ comments: true ## 链表优点 -**在链表中,插入与删除结点的操作效率高**。例如,如果想在链表中间的两个结点 `A` , `B` 之间插入一个新结点 `P` ,我们只需要改变两个结点指针即可,时间复杂度为 $O(1)$ ,相比数组的插入操作高效很多。在链表中删除某个结点也很方便,只需要改变一个结点指针即可。 +**在链表中,插入与删除结点的操作效率高**。比如,如果我们想在链表中间的两个结点 `A` , `B` 之间插入一个新结点 `P` ,我们只需要改变两个结点指针即可,时间复杂度为 $O(1)$ ,相比数组的插入操作高效很多。 -![linkedlist_insert_remove_node](linked_list.assets/linkedlist_insert_remove_node.png) - -Fig. 在链表中插入与删除结点
+![linkedlist_insert_node](linked_list.assets/linkedlist_insert_node.png) === "Java" ```java title="linked_list.java" [class]{linked_list}-[func]{insert} - - [class]{linked_list}-[func]{remove} ``` === "C++" ```cpp title="linked_list.cpp" [class]{}-[func]{insert} - - [class]{}-[func]{remove} ``` === "Python" ```python title="linked_list.py" [class]{}-[func]{insert} - - [class]{}-[func]{remove} ``` === "Go" ```go title="linked_list.go" [class]{}-[func]{insertNode} - - [class]{}-[func]{removeNode} ``` === "JavaScript" ```javascript title="linked_list.js" [class]{}-[func]{insert} - - [class]{}-[func]{remove} ``` === "TypeScript" ```typescript title="linked_list.ts" [class]{}-[func]{insert} - - [class]{}-[func]{remove} ``` === "C" ```c title="linked_list.c" [class]{}-[func]{insertNode} - - [class]{}-[func]{removeNode} ``` === "C#" ```csharp title="linked_list.cs" [class]{linked_list}-[func]{insert} - - [class]{linked_list}-[func]{remove} ``` === "Swift" ```swift title="linked_list.swift" [class]{}-[func]{insert} - - [class]{}-[func]{remove} ``` === "Zig" ```zig title="linked_list.zig" [class]{}-[func]{insert} + ``` + +在链表中删除结点也很方便,只需要改变一个结点指针即可。如下图所示,虽然在完成删除后结点 `P` 仍然指向 `n2` ,但实际上 `P` 已经不属于此链表了,因为遍历此链表是无法访问到 `P` 的。 + +![linkedlist_remove_node](linked_list.assets/linkedlist_remove_node.png) + +=== "Java" + + ```java title="linked_list.java" + [class]{linked_list}-[func]{remove} + ``` + +=== "C++" + + ```cpp title="linked_list.cpp" + [class]{}-[func]{remove} + ``` + +=== "Python" + + ```python title="linked_list.py" + [class]{}-[func]{remove} + ``` +=== "Go" + + ```go title="linked_list.go" + [class]{}-[func]{removeNode} + ``` + +=== "JavaScript" + + ```javascript title="linked_list.js" + [class]{}-[func]{remove} + ``` + +=== "TypeScript" + + ```typescript title="linked_list.ts" + [class]{}-[func]{remove} + ``` + +=== "C" + + ```c title="linked_list.c" + [class]{}-[func]{removeNode} + ``` + +=== "C#" + + ```csharp title="linked_list.cs" + [class]{linked_list}-[func]{remove} + ``` + +=== "Swift" + + ```swift title="linked_list.swift" + [class]{}-[func]{remove} + ``` + +=== "Zig" + + ```zig title="linked_list.zig" [class]{}-[func]{remove} ``` diff --git a/docs/chapter_computational_complexity/time_complexity.assets/time_complexity_first_example.png b/docs/chapter_computational_complexity/time_complexity.assets/time_complexity_simple_example.png similarity index 100% rename from docs/chapter_computational_complexity/time_complexity.assets/time_complexity_first_example.png rename to docs/chapter_computational_complexity/time_complexity.assets/time_complexity_simple_example.png diff --git a/docs/chapter_computational_complexity/time_complexity.md b/docs/chapter_computational_complexity/time_complexity.md index f4c410e22..162bcbd57 100755 --- a/docs/chapter_computational_complexity/time_complexity.md +++ b/docs/chapter_computational_complexity/time_complexity.md @@ -369,7 +369,7 @@ $$ ``` -![time_complexity_first_example](time_complexity.assets/time_complexity_first_example.png) +![time_complexity_simple_example](time_complexity.assets/time_complexity_simple_example.png)Fig. 算法 A, B, C 的时间增长趋势
diff --git a/docs/chapter_data_structure/data_and_memory.assets/IEEE-754-float.png b/docs/chapter_data_structure/data_and_memory.assets/ieee_754_float.png similarity index 100% rename from docs/chapter_data_structure/data_and_memory.assets/IEEE-754-float.png rename to docs/chapter_data_structure/data_and_memory.assets/ieee_754_float.png diff --git a/docs/chapter_data_structure/data_and_memory.md b/docs/chapter_data_structure/data_and_memory.md index 4dd649b8c..78f4f7e87 100644 --- a/docs/chapter_data_structure/data_and_memory.md +++ b/docs/chapter_data_structure/data_and_memory.md @@ -80,7 +80,7 @@ $$ \end{aligned} $$ -![IEEE-754-float](data_and_memory.assets/IEEE-754-float.png) +![ieee_754_float](data_and_memory.assets/ieee_754_float.png) 以上图为例,$\mathrm{S} = 0$ , $\mathrm{E} = 124$ ,$\mathrm{N} = 2^{-2} + 2^{-3} = 0.375$ ,易得 diff --git a/docs/chapter_heap/heap.assets/heapify_count.png b/docs/chapter_heap/heap.assets/heapify_operations_count.png similarity index 100% rename from docs/chapter_heap/heap.assets/heapify_count.png rename to docs/chapter_heap/heap.assets/heapify_operations_count.png diff --git a/docs/chapter_heap/heap.md b/docs/chapter_heap/heap.md index 09f9a15a9..1b016aa7e 100644 --- a/docs/chapter_heap/heap.md +++ b/docs/chapter_heap/heap.md @@ -793,7 +793,7 @@ $$ T(h) = 2^0h + 2^1(h-1) + 2^2(h-2) + \cdots + 2^{(h-1)}\times1 $$ -![heapify_count](heap.assets/heapify_count.png) +![heapify_operations_count](heap.assets/heapify_operations_count.png) 化简上式需要借助中学的数列知识,先对 $T(h)$ 乘以 $2$ ,易得 diff --git a/docs/chapter_preface/about_the_book.assets/mindmap.png b/docs/chapter_preface/about_the_book.assets/hello_algo_mindmap.png similarity index 100% rename from docs/chapter_preface/about_the_book.assets/mindmap.png rename to docs/chapter_preface/about_the_book.assets/hello_algo_mindmap.png diff --git a/docs/chapter_preface/about_the_book.md b/docs/chapter_preface/about_the_book.md index 62b1eb435..a7c219f92 100644 --- a/docs/chapter_preface/about_the_book.md +++ b/docs/chapter_preface/about_the_book.md @@ -36,7 +36,7 @@ comments: true 本书主要内容分为复杂度分析、数据结构、算法三个部分。 -![mindmap](about_the_book.assets/mindmap.png) +![hello_algo_mindmap](about_the_book.assets/hello_algo_mindmap.png)Fig. 知识点思维导图
diff --git a/docs/chapter_sorting/bubble_sort.assets/bubble_sort.png b/docs/chapter_sorting/bubble_sort.assets/bubble_sort_overview.png similarity index 100% rename from docs/chapter_sorting/bubble_sort.assets/bubble_sort.png rename to docs/chapter_sorting/bubble_sort.assets/bubble_sort_overview.png diff --git a/docs/chapter_sorting/bubble_sort.md b/docs/chapter_sorting/bubble_sort.md index 79861d62b..3b22c33ba 100755 --- a/docs/chapter_sorting/bubble_sort.md +++ b/docs/chapter_sorting/bubble_sort.md @@ -43,7 +43,7 @@ comments: true 2. 同理,对剩余 $n - 1$ 个元素执行「冒泡」,可将第二大元素交换至正确位置,因而待排序元素只剩 $n - 2$ 个。 3. 以此类推…… **循环 $n - 1$ 轮「冒泡」,即可完成整个数组的排序**。 -![bubble_sort](bubble_sort.assets/bubble_sort.png) +![bubble_sort_overview](bubble_sort.assets/bubble_sort_overview.png)Fig. 冒泡排序流程
diff --git a/docs/chapter_sorting/insertion_sort.assets/insertion_sort.png b/docs/chapter_sorting/insertion_sort.assets/insertion_sort_overview.png similarity index 100% rename from docs/chapter_sorting/insertion_sort.assets/insertion_sort.png rename to docs/chapter_sorting/insertion_sort.assets/insertion_sort_overview.png diff --git a/docs/chapter_sorting/insertion_sort.md b/docs/chapter_sorting/insertion_sort.md index 4e3dd2b6a..e660e3e0b 100755 --- a/docs/chapter_sorting/insertion_sort.md +++ b/docs/chapter_sorting/insertion_sort.md @@ -20,7 +20,7 @@ comments: true 2. 第 2 轮选取 **第 3 个元素** 为 `base` ,执行「插入操作」后,**数组前 3 个元素已完成排序**。 3. 以此类推……最后一轮选取 **数组尾元素** 为 `base` ,执行「插入操作」后,**所有元素已完成排序**。 -![insertion_sort](insertion_sort.assets/insertion_sort.png) +![insertion_sort_overview](insertion_sort.assets/insertion_sort_overview.png)Fig. 插入排序流程
diff --git a/docs/chapter_sorting/merge_sort.assets/merge_sort_preview.png b/docs/chapter_sorting/merge_sort.assets/merge_sort_overview.png similarity index 100% rename from docs/chapter_sorting/merge_sort.assets/merge_sort_preview.png rename to docs/chapter_sorting/merge_sort.assets/merge_sort_overview.png diff --git a/docs/chapter_sorting/merge_sort.md b/docs/chapter_sorting/merge_sort.md index 9933896f3..57e1c18ec 100755 --- a/docs/chapter_sorting/merge_sort.md +++ b/docs/chapter_sorting/merge_sort.md @@ -9,7 +9,7 @@ comments: true 1. **划分阶段**:通过递归不断 **将数组从中点位置划分开**,将长数组的排序问题转化为短数组的排序问题; 2. **合并阶段**:划分到子数组长度为 1 时,开始向上合并,不断将 **左、右两个短排序数组** 合并为 **一个长排序数组**,直至合并至原数组时完成排序; -![merge_sort_preview](merge_sort.assets/merge_sort_preview.png) +![merge_sort_overview](merge_sort.assets/merge_sort_overview.png)Fig. 归并排序两阶段:划分与合并
diff --git a/docs/chapter_sorting/quick_sort.assets/quick_sort.png b/docs/chapter_sorting/quick_sort.assets/quick_sort_overview.png similarity index 100% rename from docs/chapter_sorting/quick_sort.assets/quick_sort.png rename to docs/chapter_sorting/quick_sort.assets/quick_sort_overview.png diff --git a/docs/chapter_sorting/quick_sort.md b/docs/chapter_sorting/quick_sort.md index 9b1200a0c..ef299d450 100755 --- a/docs/chapter_sorting/quick_sort.md +++ b/docs/chapter_sorting/quick_sort.md @@ -129,7 +129,7 @@ comments: true 观察发现,快速排序和「二分查找」的原理类似,都是以对数阶的时间复杂度来缩小处理区间。 -![quick_sort](quick_sort.assets/quick_sort.png) +![quick_sort_overview](quick_sort.assets/quick_sort_overview.png)Fig. 快速排序流程
diff --git a/docs/chapter_tree/avl_tree.assets/degradation_from_inserting_node.png b/docs/chapter_tree/avl_tree.assets/avltree_degradation_from_inserting_node.png similarity index 100% rename from docs/chapter_tree/avl_tree.assets/degradation_from_inserting_node.png rename to docs/chapter_tree/avl_tree.assets/avltree_degradation_from_inserting_node.png diff --git a/docs/chapter_tree/avl_tree.assets/degradation_from_removing_node.png b/docs/chapter_tree/avl_tree.assets/avltree_degradation_from_removing_node.png similarity index 100% rename from docs/chapter_tree/avl_tree.assets/degradation_from_removing_node.png rename to docs/chapter_tree/avl_tree.assets/avltree_degradation_from_removing_node.png diff --git a/docs/chapter_tree/avl_tree.assets/left_right_rotate.png b/docs/chapter_tree/avl_tree.assets/avltree_left_right_rotate.png similarity index 100% rename from docs/chapter_tree/avl_tree.assets/left_right_rotate.png rename to docs/chapter_tree/avl_tree.assets/avltree_left_right_rotate.png diff --git a/docs/chapter_tree/avl_tree.assets/left_rotate.png b/docs/chapter_tree/avl_tree.assets/avltree_left_rotate.png similarity index 100% rename from docs/chapter_tree/avl_tree.assets/left_rotate.png rename to docs/chapter_tree/avl_tree.assets/avltree_left_rotate.png diff --git a/docs/chapter_tree/avl_tree.assets/left_rotate_with_grandchild.png b/docs/chapter_tree/avl_tree.assets/avltree_left_rotate_with_grandchild.png similarity index 100% rename from docs/chapter_tree/avl_tree.assets/left_rotate_with_grandchild.png rename to docs/chapter_tree/avl_tree.assets/avltree_left_rotate_with_grandchild.png diff --git a/docs/chapter_tree/avl_tree.assets/right_left_rotate.png b/docs/chapter_tree/avl_tree.assets/avltree_right_left_rotate.png similarity index 100% rename from docs/chapter_tree/avl_tree.assets/right_left_rotate.png rename to docs/chapter_tree/avl_tree.assets/avltree_right_left_rotate.png diff --git a/docs/chapter_tree/avl_tree.assets/right_rotate_step1.png b/docs/chapter_tree/avl_tree.assets/avltree_right_rotate_step1.png similarity index 100% rename from docs/chapter_tree/avl_tree.assets/right_rotate_step1.png rename to docs/chapter_tree/avl_tree.assets/avltree_right_rotate_step1.png diff --git a/docs/chapter_tree/avl_tree.assets/right_rotate_step2.png b/docs/chapter_tree/avl_tree.assets/avltree_right_rotate_step2.png similarity index 100% rename from docs/chapter_tree/avl_tree.assets/right_rotate_step2.png rename to docs/chapter_tree/avl_tree.assets/avltree_right_rotate_step2.png diff --git a/docs/chapter_tree/avl_tree.assets/right_rotate_step3.png b/docs/chapter_tree/avl_tree.assets/avltree_right_rotate_step3.png similarity index 100% rename from docs/chapter_tree/avl_tree.assets/right_rotate_step3.png rename to docs/chapter_tree/avl_tree.assets/avltree_right_rotate_step3.png diff --git a/docs/chapter_tree/avl_tree.assets/right_rotate_step4.png b/docs/chapter_tree/avl_tree.assets/avltree_right_rotate_step4.png similarity index 100% rename from docs/chapter_tree/avl_tree.assets/right_rotate_step4.png rename to docs/chapter_tree/avl_tree.assets/avltree_right_rotate_step4.png diff --git a/docs/chapter_tree/avl_tree.assets/right_rotate_with_grandchild.png b/docs/chapter_tree/avl_tree.assets/avltree_right_rotate_with_grandchild.png similarity index 100% rename from docs/chapter_tree/avl_tree.assets/right_rotate_with_grandchild.png rename to docs/chapter_tree/avl_tree.assets/avltree_right_rotate_with_grandchild.png diff --git a/docs/chapter_tree/avl_tree.assets/rotation_cases.png b/docs/chapter_tree/avl_tree.assets/avltree_rotation_cases.png similarity index 100% rename from docs/chapter_tree/avl_tree.assets/rotation_cases.png rename to docs/chapter_tree/avl_tree.assets/avltree_rotation_cases.png diff --git a/docs/chapter_tree/avl_tree.md b/docs/chapter_tree/avl_tree.md index 34b681762..5d2c7e0d9 100644 --- a/docs/chapter_tree/avl_tree.md +++ b/docs/chapter_tree/avl_tree.md @@ -8,11 +8,11 @@ comments: true 如下图所示,执行两步删除结点后,该二叉搜索树就会退化为链表。 -![degradation_from_removing_node](avl_tree.assets/degradation_from_removing_node.png) +![avltree_degradation_from_removing_node](avl_tree.assets/avltree_degradation_from_removing_node.png) 再比如,在以下完美二叉树中插入两个结点后,树严重向左偏斜,查找操作的时间复杂度也随之发生劣化。 -![degradation_from_inserting_node](avl_tree.assets/degradation_from_inserting_node.png) +![avltree_degradation_from_inserting_node](avl_tree.assets/avltree_degradation_from_inserting_node.png) G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorithm for the organization of information" 中提出了「AVL 树」。**论文中描述了一系列操作,使得在不断添加与删除结点后,AVL 树仍然不会发生退化**,进而使得各种操作的时间复杂度均能保持在 $O(\log n)$ 级别。 @@ -314,20 +314,20 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影 如下图所示(结点下方为「平衡因子」),从底至顶看,二叉树中首个失衡结点是 **结点 3**。我们聚焦在以该失衡结点为根结点的子树上,将该结点记为 `node` ,将其左子结点记为 `child` ,执行「右旋」操作。完成右旋后,该子树已经恢复平衡,并且仍然为二叉搜索树。 === "<1>" - ![right_rotate_step1](avl_tree.assets/right_rotate_step1.png) + ![avltree_right_rotate_step1](avl_tree.assets/avltree_right_rotate_step1.png) === "<2>" - ![right_rotate_step2](avl_tree.assets/right_rotate_step2.png) + ![avltree_right_rotate_step2](avl_tree.assets/avltree_right_rotate_step2.png) === "<3>" - ![right_rotate_step3](avl_tree.assets/right_rotate_step3.png) + ![avltree_right_rotate_step3](avl_tree.assets/avltree_right_rotate_step3.png) === "<4>" - ![right_rotate_step4](avl_tree.assets/right_rotate_step4.png) + ![avltree_right_rotate_step4](avl_tree.assets/avltree_right_rotate_step4.png) 进而,如果结点 `child` 本身有右子结点(记为 `grandChild` ),则需要在「右旋」中添加一步:将 `grandChild` 作为 `node` 的左子结点。 -![right_rotate_with_grandchild](avl_tree.assets/right_rotate_with_grandchild.png) +![avltree_right_rotate_with_grandchild](avl_tree.assets/avltree_right_rotate_with_grandchild.png) “向右旋转”是一种形象化的说法,实际需要通过修改结点指针实现,代码如下所示。 @@ -395,11 +395,11 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影 类似地,如果将取上述失衡二叉树的“镜像”,那么则需要「左旋」操作。 -![left_rotate](avl_tree.assets/left_rotate.png) +![avltree_left_rotate](avl_tree.assets/avltree_left_rotate.png) 同理,若结点 `child` 本身有左子结点(记为 `grandChild` ),则需要在「左旋」中添加一步:将 `grandChild` 作为 `node` 的右子结点。 -![left_rotate_with_grandchild](avl_tree.assets/left_rotate_with_grandchild.png) +![avltree_left_rotate_with_grandchild](avl_tree.assets/avltree_left_rotate_with_grandchild.png) 观察发现,**「左旋」和「右旋」操作是镜像对称的,两者对应解决的两种失衡情况也是对称的**。根据对称性,我们可以很方便地从「右旋」推导出「左旋」。具体地,只需将「右旋」代码中的把所有的 `left` 替换为 `right` 、所有的 `right` 替换为 `left` ,即可得到「左旋」代码。 @@ -467,19 +467,19 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影 对于下图的失衡结点 3 ,**单一使用左旋或右旋都无法使子树恢复平衡**,此时需要「先左旋后右旋」,即先对 `child` 执行「左旋」,再对 `node` 执行「右旋」。 -![left_right_rotate](avl_tree.assets/left_right_rotate.png) +![avltree_left_right_rotate](avl_tree.assets/avltree_left_right_rotate.png) ### Case 4 - 先右后左 同理,取以上失衡二叉树的镜像,则需要「先右旋后左旋」,即先对 `child` 执行「右旋」,然后对 `node` 执行「左旋」。 -![right_left_rotate](avl_tree.assets/right_left_rotate.png) +![avltree_right_left_rotate](avl_tree.assets/avltree_right_left_rotate.png) ### 旋转的选择 下图描述的四种失衡情况与上述 Cases 逐个对应,分别需采用 **右旋、左旋、先右后左、先左后右** 的旋转操作。 -![rotation_cases](avl_tree.assets/rotation_cases.png) +![avltree_rotation_cases](avl_tree.assets/avltree_rotation_cases.png) 具体地,在代码中使用 **失衡结点的平衡因子、较高一侧子结点的平衡因子** 来确定失衡结点属于上图中的哪种情况。 diff --git a/docs/chapter_tree/binary_search_tree.assets/bst_remove_case3_1.png b/docs/chapter_tree/binary_search_tree.assets/bst_remove_case3_step1.png similarity index 100% rename from docs/chapter_tree/binary_search_tree.assets/bst_remove_case3_1.png rename to docs/chapter_tree/binary_search_tree.assets/bst_remove_case3_step1.png diff --git a/docs/chapter_tree/binary_search_tree.assets/bst_remove_case3_2.png b/docs/chapter_tree/binary_search_tree.assets/bst_remove_case3_step2.png similarity index 100% rename from docs/chapter_tree/binary_search_tree.assets/bst_remove_case3_2.png rename to docs/chapter_tree/binary_search_tree.assets/bst_remove_case3_step2.png diff --git a/docs/chapter_tree/binary_search_tree.assets/bst_remove_case3_3.png b/docs/chapter_tree/binary_search_tree.assets/bst_remove_case3_step3.png similarity index 100% rename from docs/chapter_tree/binary_search_tree.assets/bst_remove_case3_3.png rename to docs/chapter_tree/binary_search_tree.assets/bst_remove_case3_step3.png diff --git a/docs/chapter_tree/binary_search_tree.assets/bst_remove_case3_4.png b/docs/chapter_tree/binary_search_tree.assets/bst_remove_case3_step4.png similarity index 100% rename from docs/chapter_tree/binary_search_tree.assets/bst_remove_case3_4.png rename to docs/chapter_tree/binary_search_tree.assets/bst_remove_case3_step4.png diff --git a/docs/chapter_tree/binary_search_tree.assets/bst_search_1.png b/docs/chapter_tree/binary_search_tree.assets/bst_search_step1.png similarity index 100% rename from docs/chapter_tree/binary_search_tree.assets/bst_search_1.png rename to docs/chapter_tree/binary_search_tree.assets/bst_search_step1.png diff --git a/docs/chapter_tree/binary_search_tree.assets/bst_search_2.png b/docs/chapter_tree/binary_search_tree.assets/bst_search_step2.png similarity index 100% rename from docs/chapter_tree/binary_search_tree.assets/bst_search_2.png rename to docs/chapter_tree/binary_search_tree.assets/bst_search_step2.png diff --git a/docs/chapter_tree/binary_search_tree.assets/bst_search_3.png b/docs/chapter_tree/binary_search_tree.assets/bst_search_step3.png similarity index 100% rename from docs/chapter_tree/binary_search_tree.assets/bst_search_3.png rename to docs/chapter_tree/binary_search_tree.assets/bst_search_step3.png diff --git a/docs/chapter_tree/binary_search_tree.assets/bst_search_4.png b/docs/chapter_tree/binary_search_tree.assets/bst_search_step4.png similarity index 100% rename from docs/chapter_tree/binary_search_tree.assets/bst_search_4.png rename to docs/chapter_tree/binary_search_tree.assets/bst_search_step4.png diff --git a/docs/chapter_tree/binary_search_tree.md b/docs/chapter_tree/binary_search_tree.md index dcfcbb350..8c6dbb829 100755 --- a/docs/chapter_tree/binary_search_tree.md +++ b/docs/chapter_tree/binary_search_tree.md @@ -22,16 +22,16 @@ comments: true - 若 `cur.val = num` ,说明找到目标结点,跳出循环并返回该结点即可; === "<1>" - ![bst_search_1](binary_search_tree.assets/bst_search_1.png) + ![bst_search_step1](binary_search_tree.assets/bst_search_step1.png) === "<2>" - ![bst_search_2](binary_search_tree.assets/bst_search_2.png) + ![bst_search_step2](binary_search_tree.assets/bst_search_step2.png) === "<3>" - ![bst_search_3](binary_search_tree.assets/bst_search_3.png) + ![bst_search_step3](binary_search_tree.assets/bst_search_step3.png) === "<4>" - ![bst_search_4](binary_search_tree.assets/bst_search_4.png) + ![bst_search_step4](binary_search_tree.assets/bst_search_step4.png) 二叉搜索树的查找操作和二分查找算法如出一辙,也是在每轮排除一半情况。循环次数最多为二叉树的高度,当二叉树平衡时,使用 $O(\log n)$ 时间。 @@ -189,16 +189,16 @@ comments: true 3. 使用 `nex` 替换待删除结点; === "<1>" - ![bst_remove_case3_1](binary_search_tree.assets/bst_remove_case3_1.png) + ![bst_remove_case3_step1](binary_search_tree.assets/bst_remove_case3_step1.png) === "<2>" - ![bst_remove_case3_2](binary_search_tree.assets/bst_remove_case3_2.png) + ![bst_remove_case3_step2](binary_search_tree.assets/bst_remove_case3_step2.png) === "<3>" - ![bst_remove_case3_3](binary_search_tree.assets/bst_remove_case3_3.png) + ![bst_remove_case3_step3](binary_search_tree.assets/bst_remove_case3_step3.png) === "<4>" - ![bst_remove_case3_4](binary_search_tree.assets/bst_remove_case3_4.png) + ![bst_remove_case3_step4](binary_search_tree.assets/bst_remove_case3_step4.png) 删除结点操作也使用 $O(\log n)$ 时间,其中查找待删除结点 $O(\log n)$ ,获取中序遍历后继结点 $O(\log n)$ 。