This commit is contained in:
krahets
2023-11-09 05:13:48 +08:00
parent 9701430089
commit 0105644232
83 changed files with 516 additions and 509 deletions

View File

@ -40,7 +40,7 @@ comments: true
图 12-4 展示了在数组中二分查找元素 $6$ 的分治过程。
![二分查找的分治过程](binary_search_recur.assets/binary_search_recur.png)
![二分查找的分治过程](binary_search_recur.assets/binary_search_recur.png){ class="animation-figure" }
<p align="center"> 图 12-4 &nbsp; 二分查找的分治过程 </p>

View File

@ -8,7 +8,7 @@ comments: true
给定一个二叉树的前序遍历 `preorder` 和中序遍历 `inorder` ,请从中构建二叉树,返回二叉树的根节点。假设二叉树中没有值重复的节点。
![构建二叉树的示例数据](build_binary_tree_problem.assets/build_tree_example.png)
![构建二叉树的示例数据](build_binary_tree_problem.assets/build_tree_example.png){ class="animation-figure" }
<p align="center"> 图 12-5 &nbsp; 构建二叉树的示例数据 </p>
@ -35,7 +35,7 @@ comments: true
2. 查找根节点 3 在 `inorder` 中的索引,利用该索引可将 `inorder` 划分为 `[ 9 | 3 1 2 7 ]`
3. 根据 `inorder` 划分结果,易得左子树和右子树的节点数量分别为 1 和 3 ,从而可将 `preorder` 划分为 `[ 3 | 9 | 2 1 7 ]`
![在前序和中序遍历中划分子树](build_binary_tree_problem.assets/build_tree_preorder_inorder_division.png)
![在前序和中序遍历中划分子树](build_binary_tree_problem.assets/build_tree_preorder_inorder_division.png){ class="animation-figure" }
<p align="center"> 图 12-6 &nbsp; 在前序和中序遍历中划分子树 </p>
@ -63,7 +63,7 @@ comments: true
请注意,右子树根节点索引中的 $(m-l)$ 的含义是“左子树的节点数量”,建议配合图 12-7 理解。
![根节点和左右子树的索引区间表示](build_binary_tree_problem.assets/build_tree_division_pointers.png)
![根节点和左右子树的索引区间表示](build_binary_tree_problem.assets/build_tree_division_pointers.png){ class="animation-figure" }
<p align="center"> 图 12-7 &nbsp; 根节点和左右子树的索引区间表示 </p>
@ -448,37 +448,37 @@ comments: true
图 12-8 展示了构建二叉树的递归过程,各个节点是在向下“递”的过程中建立的,而各条边(即引用)是在向上“归”的过程中建立的。
=== "<1>"
![构建二叉树的递归过程](build_binary_tree_problem.assets/built_tree_step1.png)
![构建二叉树的递归过程](build_binary_tree_problem.assets/built_tree_step1.png){ class="animation-figure" }
=== "<2>"
![built_tree_step2](build_binary_tree_problem.assets/built_tree_step2.png)
![built_tree_step2](build_binary_tree_problem.assets/built_tree_step2.png){ class="animation-figure" }
=== "<3>"
![built_tree_step3](build_binary_tree_problem.assets/built_tree_step3.png)
![built_tree_step3](build_binary_tree_problem.assets/built_tree_step3.png){ class="animation-figure" }
=== "<4>"
![built_tree_step4](build_binary_tree_problem.assets/built_tree_step4.png)
![built_tree_step4](build_binary_tree_problem.assets/built_tree_step4.png){ class="animation-figure" }
=== "<5>"
![built_tree_step5](build_binary_tree_problem.assets/built_tree_step5.png)
![built_tree_step5](build_binary_tree_problem.assets/built_tree_step5.png){ class="animation-figure" }
=== "<6>"
![built_tree_step6](build_binary_tree_problem.assets/built_tree_step6.png)
![built_tree_step6](build_binary_tree_problem.assets/built_tree_step6.png){ class="animation-figure" }
=== "<7>"
![built_tree_step7](build_binary_tree_problem.assets/built_tree_step7.png)
![built_tree_step7](build_binary_tree_problem.assets/built_tree_step7.png){ class="animation-figure" }
=== "<8>"
![built_tree_step8](build_binary_tree_problem.assets/built_tree_step8.png)
![built_tree_step8](build_binary_tree_problem.assets/built_tree_step8.png){ class="animation-figure" }
=== "<9>"
![built_tree_step9](build_binary_tree_problem.assets/built_tree_step9.png)
![built_tree_step9](build_binary_tree_problem.assets/built_tree_step9.png){ class="animation-figure" }
<p align="center"> 图 12-8 &nbsp; 构建二叉树的递归过程 </p>
每个递归函数内的前序遍历 `preorder` 和中序遍历 `inorder` 的划分结果如图 12-9 所示。
![每个递归函数中的划分结果](build_binary_tree_problem.assets/built_tree_overall.png)
![每个递归函数中的划分结果](build_binary_tree_problem.assets/built_tree_overall.png){ class="animation-figure" }
<p align="center"> 图 12-9 &nbsp; 每个递归函数中的划分结果 </p>

View File

@ -14,7 +14,7 @@ comments: true
1. **分**:递归地将原数组(原问题)划分为两个子数组(子问题),直到子数组只剩一个元素(最小子问题)。
2. **治**:从底至顶地将有序的子数组(子问题的解)进行合并,从而得到有序的原数组(原问题的解)。
![归并排序的分治策略](divide_and_conquer.assets/divide_and_conquer_merge_sort.png)
![归并排序的分治策略](divide_and_conquer.assets/divide_and_conquer_merge_sort.png){ class="animation-figure" }
<p align="center"> 图 12-1 &nbsp; 归并排序的分治策略 </p>
@ -46,7 +46,7 @@ $$
O(n + (\frac{n}{2})^2 \times 2 + n) = O(\frac{n^2}{2} + 2n)
$$
![划分数组前后的冒泡排序](divide_and_conquer.assets/divide_and_conquer_bubble_sort.png)
![划分数组前后的冒泡排序](divide_and_conquer.assets/divide_and_conquer_bubble_sort.png){ class="animation-figure" }
<p align="center"> 图 12-2 &nbsp; 划分数组前后的冒泡排序 </p>
@ -74,7 +74,7 @@ $$
比如在图 12-3 所示的“桶排序”中,我们将海量的数据平均分配到各个桶中,则可所有桶的排序任务分散到各个计算单元,完成后再进行结果合并。
![桶排序的并行计算](divide_and_conquer.assets/divide_and_conquer_parallel_computing.png)
![桶排序的并行计算](divide_and_conquer.assets/divide_and_conquer_parallel_computing.png){ class="animation-figure" }
<p align="center"> 图 12-3 &nbsp; 桶排序的并行计算 </p>

View File

@ -14,7 +14,7 @@ comments: true
2. 每次只能移动一个圆盘。
3. 小圆盘必须时刻位于大圆盘之上。
![汉诺塔问题示例](hanota_problem.assets/hanota_example.png)
![汉诺塔问题示例](hanota_problem.assets/hanota_example.png){ class="animation-figure" }
<p align="center"> 图 12-10 &nbsp; 汉诺塔问题示例 </p>
@ -25,10 +25,10 @@ comments: true
如图 12-11 所示,对于问题 $f(1)$ ,即当只有一个圆盘时,我们将它直接从 `A` 移动至 `C` 即可。
=== "<1>"
![规模为 1 问题的解](hanota_problem.assets/hanota_f1_step1.png)
![规模为 1 问题的解](hanota_problem.assets/hanota_f1_step1.png){ class="animation-figure" }
=== "<2>"
![hanota_f1_step2](hanota_problem.assets/hanota_f1_step2.png)
![hanota_f1_step2](hanota_problem.assets/hanota_f1_step2.png){ class="animation-figure" }
<p align="center"> 图 12-11 &nbsp; 规模为 1 问题的解 </p>
@ -39,16 +39,16 @@ comments: true
3. 最后将小圆盘从 `B` 移至 `C`
=== "<1>"
![规模为 2 问题的解](hanota_problem.assets/hanota_f2_step1.png)
![规模为 2 问题的解](hanota_problem.assets/hanota_f2_step1.png){ class="animation-figure" }
=== "<2>"
![hanota_f2_step2](hanota_problem.assets/hanota_f2_step2.png)
![hanota_f2_step2](hanota_problem.assets/hanota_f2_step2.png){ class="animation-figure" }
=== "<3>"
![hanota_f2_step3](hanota_problem.assets/hanota_f2_step3.png)
![hanota_f2_step3](hanota_problem.assets/hanota_f2_step3.png){ class="animation-figure" }
=== "<4>"
![hanota_f2_step4](hanota_problem.assets/hanota_f2_step4.png)
![hanota_f2_step4](hanota_problem.assets/hanota_f2_step4.png){ class="animation-figure" }
<p align="center"> 图 12-12 &nbsp; 规模为 2 问题的解 </p>
@ -65,16 +65,16 @@ comments: true
3.`C` 为目标柱、`A` 为缓冲柱,将两个圆盘从 `B` 移动至 `C`
=== "<1>"
![规模为 3 问题的解](hanota_problem.assets/hanota_f3_step1.png)
![规模为 3 问题的解](hanota_problem.assets/hanota_f3_step1.png){ class="animation-figure" }
=== "<2>"
![hanota_f3_step2](hanota_problem.assets/hanota_f3_step2.png)
![hanota_f3_step2](hanota_problem.assets/hanota_f3_step2.png){ class="animation-figure" }
=== "<3>"
![hanota_f3_step3](hanota_problem.assets/hanota_f3_step3.png)
![hanota_f3_step3](hanota_problem.assets/hanota_f3_step3.png){ class="animation-figure" }
=== "<4>"
![hanota_f3_step4](hanota_problem.assets/hanota_f3_step4.png)
![hanota_f3_step4](hanota_problem.assets/hanota_f3_step4.png){ class="animation-figure" }
<p align="center"> 图 12-13 &nbsp; 规模为 3 问题的解 </p>
@ -88,7 +88,7 @@ comments: true
对于这两个子问题 $f(n-1)$ **可以通过相同的方式进行递归划分**,直至达到最小子问题 $f(1)$ 。而 $f(1)$ 的解是已知的,只需一次移动操作即可。
![汉诺塔问题的分治策略](hanota_problem.assets/hanota_divide_and_conquer.png)
![汉诺塔问题的分治策略](hanota_problem.assets/hanota_divide_and_conquer.png){ class="animation-figure" }
<p align="center"> 图 12-14 &nbsp; 汉诺塔问题的分治策略 </p>
@ -485,7 +485,7 @@ comments: true
如图 12-15 所示,汉诺塔问题形成一个高度为 $n$ 的递归树,每个节点代表一个子问题、对应一个开启的 `dfs()` 函数,**因此时间复杂度为 $O(2^n)$ ,空间复杂度为 $O(n)$** 。
![汉诺塔问题的递归树](hanota_problem.assets/hanota_recursive_tree.png)
![汉诺塔问题的递归树](hanota_problem.assets/hanota_recursive_tree.png){ class="animation-figure" }
<p align="center"> 图 12-15 &nbsp; 汉诺塔问题的递归树 </p>

View File

@ -7,7 +7,7 @@ icon: material/set-split
<div class="center-table" markdown>
![分治](../assets/covers/chapter_divide_and_conquer.jpg){ width="600" }
![分治](../assets/covers/chapter_divide_and_conquer.jpg){ class="cover-image" }
</div>