mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-05 11:41:22 +08:00
deploy
This commit is contained in:
@@ -3439,7 +3439,7 @@
|
||||
<li>对于根节点,左子树中所有节点的值 <span class="arithmatex">\(<\)</span> 根节点的值 <span class="arithmatex">\(<\)</span> 右子树中所有节点的值。</li>
|
||||
<li>任意节点的左、右子树也是二叉搜索树,即同样满足条件 <code>1.</code> 。</li>
|
||||
</ol>
|
||||
<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><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="二叉搜索树" class="animation-figure" 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>
|
||||
@@ -3454,16 +3454,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><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>
|
||||
<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="二叉搜索树查找节点示例" class="animation-figure" src="../binary_search_tree.assets/bst_search_step1.png" /></a></p>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<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>
|
||||
<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" class="animation-figure" src="../binary_search_tree.assets/bst_search_step2.png" /></a></p>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<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>
|
||||
<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" class="animation-figure" src="../binary_search_tree.assets/bst_search_step3.png" /></a></p>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<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>
|
||||
<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" class="animation-figure" src="../binary_search_tree.assets/bst_search_step4.png" /></a></p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -3733,7 +3733,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><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><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="在二叉搜索树中插入节点" class="animation-figure" src="../binary_search_tree.assets/bst_insert.png" /></a></p>
|
||||
<p align="center"> 图 7-18 在二叉搜索树中插入节点 </p>
|
||||
|
||||
<p>在代码实现中,需要注意以下两点。</p>
|
||||
@@ -4134,11 +4134,11 @@
|
||||
<p>与插入节点类似,我们需要保证在删除操作完成后,二叉搜索树的“左子树 < 根节点 < 右子树”的性质仍然满足。</p>
|
||||
<p>因此,我们需要根据目标节点的子节点数量,共分为 0、1 和 2 这三种情况,执行对应的删除节点操作。</p>
|
||||
<p>如图 7-19 所示,当待删除节点的度为 <span class="arithmatex">\(0\)</span> 时,表示该节点是叶节点,可以直接删除。</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><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 )" class="animation-figure" 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><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><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 )" class="animation-figure" 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>
|
||||
@@ -4150,16 +4150,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><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>
|
||||
<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 )" class="animation-figure" src="../binary_search_tree.assets/bst_remove_case3_step1.png" /></a></p>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<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>
|
||||
<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" class="animation-figure" src="../binary_search_tree.assets/bst_remove_case3_step2.png" /></a></p>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<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>
|
||||
<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" class="animation-figure" src="../binary_search_tree.assets/bst_remove_case3_step3.png" /></a></p>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<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>
|
||||
<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" class="animation-figure" src="../binary_search_tree.assets/bst_remove_case3_step4.png" /></a></p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -4820,7 +4820,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><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><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="二叉搜索树的中序遍历序列" class="animation-figure" 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>
|
||||
@@ -4857,7 +4857,7 @@
|
||||
</div>
|
||||
<p>在理想情况下,二叉搜索树是“平衡”的,这样就可以在 <span class="arithmatex">\(\log n\)</span> 轮循环内查找任意节点。</p>
|
||||
<p>然而,如果我们在二叉搜索树中不断地插入和删除节点,可能导致二叉树退化为图 7-23 所示的链表,这时各种操作的时间复杂度也会退化为 <span class="arithmatex">\(O(n)\)</span> 。</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><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="二叉搜索树的退化" class="animation-figure" 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>
|
||||
|
||||
Reference in New Issue
Block a user