mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-05 11:41:22 +08:00
deploy
This commit is contained in:
@@ -60,7 +60,18 @@
|
||||
|
||||
|
||||
|
||||
</head>
|
||||
<link href="../../assets/stylesheets/glightbox.min.css" rel="stylesheet"/><style>
|
||||
html.glightbox-open { overflow: initial; height: 100%; }
|
||||
.gslide-title { margin-top: 0px; user-select: text; }
|
||||
.gslide-desc { color: #666; user-select: text; }
|
||||
.gslide-image img { background: white; }
|
||||
|
||||
.gscrollbar-fixer { padding-right: 15px; }
|
||||
.gdesc-inner { font-size: 0.75rem; }
|
||||
body[data-md-color-scheme="slate"] .gdesc-inner { background: var(--md-default-bg-color);}
|
||||
body[data-md-color-scheme="slate"] .gslide-title { color: var(--md-default-fg-color);}
|
||||
body[data-md-color-scheme="slate"] .gslide-desc { color: var(--md-default-fg-color);}
|
||||
</style> <script src="../../assets/javascripts/glightbox.min.js"></script></head>
|
||||
|
||||
|
||||
|
||||
@@ -1972,14 +1983,6 @@
|
||||
10.2 二分查找插入点
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -2000,14 +2003,6 @@
|
||||
10.3 二分查找边界
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -2427,14 +2422,6 @@
|
||||
第 12 章 分治
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
|
||||
@@ -2466,14 +2453,6 @@
|
||||
12.1 分治算法
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -2494,14 +2473,6 @@
|
||||
12.2 分治搜索策略
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -2522,14 +2493,6 @@
|
||||
12.3 构建树问题
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -2550,14 +2513,6 @@
|
||||
12.4 汉诺塔问题
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -2578,14 +2533,6 @@
|
||||
12.5 小结
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -2817,14 +2764,6 @@
|
||||
第 14 章 动态规划
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
|
||||
@@ -2856,14 +2795,6 @@
|
||||
14.1 初探动态规划
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -2884,14 +2815,6 @@
|
||||
14.2 DP 问题特性
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -2912,14 +2835,6 @@
|
||||
14.3 DP 解题思路
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -2940,14 +2855,6 @@
|
||||
14.4 0-1 背包问题
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -2968,14 +2875,6 @@
|
||||
14.5 完全背包问题
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -2996,14 +2895,6 @@
|
||||
14.6 编辑距离问题
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -3024,14 +2915,6 @@
|
||||
14.7 小结
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -3090,14 +2973,6 @@
|
||||
第 15 章 贪心
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
|
||||
@@ -3129,14 +3004,6 @@
|
||||
15.1 贪心算法
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -3157,14 +3024,6 @@
|
||||
15.2 分数背包问题
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -3185,14 +3044,6 @@
|
||||
15.3 最大容量问题
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -3213,14 +3064,6 @@
|
||||
15.4 最大切分乘积问题
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -3241,14 +3084,6 @@
|
||||
15.5 小结
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -3561,7 +3396,7 @@
|
||||
<li>对于根节点,左子树中所有节点的值 <span class="arithmatex">\(<\)</span> 根节点的值 <span class="arithmatex">\(<\)</span> 右子树中所有节点的值。</li>
|
||||
<li>任意节点的左、右子树也是二叉搜索树,即同样满足条件 <code>1.</code> 。</li>
|
||||
</ol>
|
||||
<p><img alt="二叉搜索树" src="../binary_search_tree.assets/binary_search_tree.png" /></p>
|
||||
<p><a class="glightbox" href="../binary_search_tree.assets/binary_search_tree.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="二叉搜索树" src="../binary_search_tree.assets/binary_search_tree.png" /></a></p>
|
||||
<p align="center"> 图 7-16 二叉搜索树 </p>
|
||||
|
||||
<h2 id="741">7.4.1 二叉搜索树的操作<a class="headerlink" href="#741" title="Permanent link">¶</a></h2>
|
||||
@@ -3576,16 +3411,16 @@
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="1:4"><input checked="checked" id="__tabbed_1_1" name="__tabbed_1" type="radio" /><input id="__tabbed_1_2" name="__tabbed_1" type="radio" /><input id="__tabbed_1_3" name="__tabbed_1" type="radio" /><input id="__tabbed_1_4" name="__tabbed_1" type="radio" /><div class="tabbed-labels"><label for="__tabbed_1_1"><1></label><label for="__tabbed_1_2"><2></label><label for="__tabbed_1_3"><3></label><label for="__tabbed_1_4"><4></label></div>
|
||||
<div class="tabbed-content">
|
||||
<div class="tabbed-block">
|
||||
<p><img alt="二叉搜索树查找节点示例" src="../binary_search_tree.assets/bst_search_step1.png" /></p>
|
||||
<p><a class="glightbox" href="../binary_search_tree.assets/bst_search_step1.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="二叉搜索树查找节点示例" src="../binary_search_tree.assets/bst_search_step1.png" /></a></p>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<p><img alt="bst_search_step2" src="../binary_search_tree.assets/bst_search_step2.png" /></p>
|
||||
<p><a class="glightbox" href="../binary_search_tree.assets/bst_search_step2.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="bst_search_step2" src="../binary_search_tree.assets/bst_search_step2.png" /></a></p>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<p><img alt="bst_search_step3" src="../binary_search_tree.assets/bst_search_step3.png" /></p>
|
||||
<p><a class="glightbox" href="../binary_search_tree.assets/bst_search_step3.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="bst_search_step3" src="../binary_search_tree.assets/bst_search_step3.png" /></a></p>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<p><img alt="bst_search_step4" src="../binary_search_tree.assets/bst_search_step4.png" /></p>
|
||||
<p><a class="glightbox" href="../binary_search_tree.assets/bst_search_step4.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="bst_search_step4" src="../binary_search_tree.assets/bst_search_step4.png" /></a></p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -3855,7 +3690,7 @@
|
||||
<li><strong>查找插入位置</strong>:与查找操作相似,从根节点出发,根据当前节点值和 <code>num</code> 的大小关系循环向下搜索,直到越过叶节点(遍历至 <span class="arithmatex">\(\text{None}\)</span> )时跳出循环。</li>
|
||||
<li><strong>在该位置插入节点</strong>:初始化节点 <code>num</code> ,将该节点置于 <span class="arithmatex">\(\text{None}\)</span> 的位置。</li>
|
||||
</ol>
|
||||
<p><img alt="在二叉搜索树中插入节点" src="../binary_search_tree.assets/bst_insert.png" /></p>
|
||||
<p><a class="glightbox" href="../binary_search_tree.assets/bst_insert.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="在二叉搜索树中插入节点" src="../binary_search_tree.assets/bst_insert.png" /></a></p>
|
||||
<p align="center"> 图 7-18 在二叉搜索树中插入节点 </p>
|
||||
|
||||
<p>在代码实现中,需要注意以下两点。</p>
|
||||
@@ -4256,11 +4091,11 @@
|
||||
<p>与插入节点类似,我们需要保证在删除操作完成后,二叉搜索树的“左子树 < 根节点 < 右子树”的性质仍然满足。</p>
|
||||
<p>因此,我们需要根据目标节点的子节点数量,共分为 0、1 和 2 这三种情况,执行对应的删除节点操作。</p>
|
||||
<p>如图 7-19 所示,当待删除节点的度为 <span class="arithmatex">\(0\)</span> 时,表示该节点是叶节点,可以直接删除。</p>
|
||||
<p><img alt="在二叉搜索树中删除节点(度为 0 )" src="../binary_search_tree.assets/bst_remove_case1.png" /></p>
|
||||
<p><a class="glightbox" href="../binary_search_tree.assets/bst_remove_case1.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="在二叉搜索树中删除节点(度为 0 )" src="../binary_search_tree.assets/bst_remove_case1.png" /></a></p>
|
||||
<p align="center"> 图 7-19 在二叉搜索树中删除节点(度为 0 ) </p>
|
||||
|
||||
<p>如图 7-20 所示,当待删除节点的度为 <span class="arithmatex">\(1\)</span> 时,将待删除节点替换为其子节点即可。</p>
|
||||
<p><img alt="在二叉搜索树中删除节点(度为 1 )" src="../binary_search_tree.assets/bst_remove_case2.png" /></p>
|
||||
<p><a class="glightbox" href="../binary_search_tree.assets/bst_remove_case2.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="在二叉搜索树中删除节点(度为 1 )" src="../binary_search_tree.assets/bst_remove_case2.png" /></a></p>
|
||||
<p align="center"> 图 7-20 在二叉搜索树中删除节点(度为 1 ) </p>
|
||||
|
||||
<p>当待删除节点的度为 <span class="arithmatex">\(2\)</span> 时,我们无法直接删除它,而需要使用一个节点替换该节点。由于要保持二叉搜索树“左 <span class="arithmatex">\(<\)</span> 根 <span class="arithmatex">\(<\)</span> 右”的性质,<strong>因此这个节点可以是右子树的最小节点或左子树的最大节点</strong>。</p>
|
||||
@@ -4272,16 +4107,16 @@
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="4:4"><input checked="checked" id="__tabbed_4_1" name="__tabbed_4" type="radio" /><input id="__tabbed_4_2" name="__tabbed_4" type="radio" /><input id="__tabbed_4_3" name="__tabbed_4" type="radio" /><input id="__tabbed_4_4" name="__tabbed_4" type="radio" /><div class="tabbed-labels"><label for="__tabbed_4_1"><1></label><label for="__tabbed_4_2"><2></label><label for="__tabbed_4_3"><3></label><label for="__tabbed_4_4"><4></label></div>
|
||||
<div class="tabbed-content">
|
||||
<div class="tabbed-block">
|
||||
<p><img alt="在二叉搜索树中删除节点(度为 2 )" src="../binary_search_tree.assets/bst_remove_case3_step1.png" /></p>
|
||||
<p><a class="glightbox" href="../binary_search_tree.assets/bst_remove_case3_step1.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="在二叉搜索树中删除节点(度为 2 )" src="../binary_search_tree.assets/bst_remove_case3_step1.png" /></a></p>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<p><img alt="bst_remove_case3_step2" src="../binary_search_tree.assets/bst_remove_case3_step2.png" /></p>
|
||||
<p><a class="glightbox" href="../binary_search_tree.assets/bst_remove_case3_step2.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="bst_remove_case3_step2" src="../binary_search_tree.assets/bst_remove_case3_step2.png" /></a></p>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<p><img alt="bst_remove_case3_step3" src="../binary_search_tree.assets/bst_remove_case3_step3.png" /></p>
|
||||
<p><a class="glightbox" href="../binary_search_tree.assets/bst_remove_case3_step3.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="bst_remove_case3_step3" src="../binary_search_tree.assets/bst_remove_case3_step3.png" /></a></p>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<p><img alt="bst_remove_case3_step4" src="../binary_search_tree.assets/bst_remove_case3_step4.png" /></p>
|
||||
<p><a class="glightbox" href="../binary_search_tree.assets/bst_remove_case3_step4.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="bst_remove_case3_step4" src="../binary_search_tree.assets/bst_remove_case3_step4.png" /></a></p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -4942,7 +4777,7 @@
|
||||
<p>如图 7-22 所示,二叉树的中序遍历遵循“左 <span class="arithmatex">\(\rightarrow\)</span> 根 <span class="arithmatex">\(\rightarrow\)</span> 右”的遍历顺序,而二叉搜索树满足“左子节点 <span class="arithmatex">\(<\)</span> 根节点 <span class="arithmatex">\(<\)</span> 右子节点”的大小关系。</p>
|
||||
<p>这意味着在二叉搜索树中进行中序遍历时,总是会优先遍历下一个最小节点,从而得出一个重要性质:<strong>二叉搜索树的中序遍历序列是升序的</strong>。</p>
|
||||
<p>利用中序遍历升序的性质,我们在二叉搜索树中获取有序数据仅需 <span class="arithmatex">\(O(n)\)</span> 时间,无须进行额外的排序操作,非常高效。</p>
|
||||
<p><img alt="二叉搜索树的中序遍历序列" src="../binary_search_tree.assets/bst_inorder_traversal.png" /></p>
|
||||
<p><a class="glightbox" href="../binary_search_tree.assets/bst_inorder_traversal.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="二叉搜索树的中序遍历序列" src="../binary_search_tree.assets/bst_inorder_traversal.png" /></a></p>
|
||||
<p align="center"> 图 7-22 二叉搜索树的中序遍历序列 </p>
|
||||
|
||||
<h2 id="742">7.4.2 二叉搜索树的效率<a class="headerlink" href="#742" title="Permanent link">¶</a></h2>
|
||||
@@ -4979,7 +4814,7 @@
|
||||
</div>
|
||||
<p>在理想情况下,二叉搜索树是“平衡”的,这样就可以在 <span class="arithmatex">\(\log n\)</span> 轮循环内查找任意节点。</p>
|
||||
<p>然而,如果我们在二叉搜索树中不断地插入和删除节点,可能导致二叉树退化为图 7-23 所示的链表,这时各种操作的时间复杂度也会退化为 <span class="arithmatex">\(O(n)\)</span> 。</p>
|
||||
<p><img alt="二叉搜索树的退化" src="../binary_search_tree.assets/bst_degradation.png" /></p>
|
||||
<p><a class="glightbox" href="../binary_search_tree.assets/bst_degradation.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="二叉搜索树的退化" src="../binary_search_tree.assets/bst_degradation.png" /></a></p>
|
||||
<p align="center"> 图 7-23 二叉搜索树的退化 </p>
|
||||
|
||||
<h2 id="743">7.4.3 二叉搜索树常见应用<a class="headerlink" href="#743" title="Permanent link">¶</a></h2>
|
||||
@@ -5151,10 +4986,15 @@ aria-label="页脚"
|
||||
<div class="md-copyright">
|
||||
|
||||
<div class="md-copyright__highlight">
|
||||
Copyright © 2023 Krahets
|
||||
Copyright © 2022 - 2023 Krahets
|
||||
</div>
|
||||
|
||||
|
||||
Made with
|
||||
<a href="https://squidfunk.github.io/mkdocs-material/" target="_blank" rel="noopener">
|
||||
Material for MkDocs
|
||||
</a>
|
||||
|
||||
</div>
|
||||
|
||||
<!-- Social links -->
|
||||
@@ -5223,5 +5063,5 @@ aria-label="页脚"
|
||||
|
||||
|
||||
|
||||
</body>
|
||||
<script>document$.subscribe(() => {const lightbox = GLightbox({"touchNavigation": true, "loop": false, "zoomable": true, "draggable": false, "openEffect": "zoom", "closeEffect": "zoom", "slideEffect": "none"});})</script></body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user