mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-15 02:40:58 +08:00
deploy
This commit is contained in:
@@ -1788,7 +1788,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
8.3 Top-K 问题
|
||||
8.3 Top-k 问题
|
||||
</span>
|
||||
|
||||
|
||||
@@ -3543,12 +3543,12 @@
|
||||
|
||||
<p><strong>映射公式的角色相当于链表中的指针</strong>。给定数组中的任意一个节点,我们都可以通过映射公式来访问它的左(右)子节点。</p>
|
||||
<h2 id="732">7.3.2 表示任意二叉树<a class="headerlink" href="#732" title="Permanent link">¶</a></h2>
|
||||
<p>完美二叉树是一个特例,在二叉树的中间层通常存在许多 <span class="arithmatex">\(\text{None}\)</span> 。由于层序遍历序列并不包含这些 <span class="arithmatex">\(\text{None}\)</span> ,因此我们无法仅凭该序列来推测 <span class="arithmatex">\(\text{None}\)</span> 的数量和分布位置。<strong>这意味着存在多种二叉树结构都符合该层序遍历序列</strong>。</p>
|
||||
<p>完美二叉树是一个特例,在二叉树的中间层通常存在许多 <code>None</code> 。由于层序遍历序列并不包含这些 <code>None</code> ,因此我们无法仅凭该序列来推测 <code>None</code> 的数量和分布位置。<strong>这意味着存在多种二叉树结构都符合该层序遍历序列</strong>。</p>
|
||||
<p>如图 7-13 所示,给定一棵非完美二叉树,上述数组表示方法已经失效。</p>
|
||||
<p><a class="glightbox" href="../array_representation_of_tree.assets/array_representation_without_empty.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="层序遍历序列对应多种二叉树可能性" class="animation-figure" src="../array_representation_of_tree.assets/array_representation_without_empty.png" /></a></p>
|
||||
<p align="center"> 图 7-13 层序遍历序列对应多种二叉树可能性 </p>
|
||||
|
||||
<p>为了解决此问题,<strong>我们可以考虑在层序遍历序列中显式地写出所有 <span class="arithmatex">\(\text{None}\)</span></strong> 。如图 7-14 所示,这样处理后,层序遍历序列就可以唯一表示二叉树了。示例代码如下:</p>
|
||||
<p>为了解决此问题,<strong>我们可以考虑在层序遍历序列中显式地写出所有 <code>None</code></strong> 。如图 7-14 所示,这样处理后,层序遍历序列就可以唯一表示二叉树了。示例代码如下:</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="1:12"><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" /><input id="__tabbed_1_5" name="__tabbed_1" type="radio" /><input id="__tabbed_1_6" name="__tabbed_1" type="radio" /><input id="__tabbed_1_7" name="__tabbed_1" type="radio" /><input id="__tabbed_1_8" name="__tabbed_1" type="radio" /><input id="__tabbed_1_9" name="__tabbed_1" type="radio" /><input id="__tabbed_1_10" name="__tabbed_1" type="radio" /><input id="__tabbed_1_11" name="__tabbed_1" type="radio" /><input id="__tabbed_1_12" name="__tabbed_1" type="radio" /><div class="tabbed-labels"><label for="__tabbed_1_1">Python</label><label for="__tabbed_1_2">C++</label><label for="__tabbed_1_3">Java</label><label for="__tabbed_1_4">C#</label><label for="__tabbed_1_5">Go</label><label for="__tabbed_1_6">Swift</label><label for="__tabbed_1_7">JS</label><label for="__tabbed_1_8">TS</label><label for="__tabbed_1_9">Dart</label><label for="__tabbed_1_10">Rust</label><label for="__tabbed_1_11">C</label><label for="__tabbed_1_12">Zig</label></div>
|
||||
<div class="tabbed-content">
|
||||
<div class="tabbed-block">
|
||||
@@ -3626,8 +3626,8 @@
|
||||
<p><a class="glightbox" href="../array_representation_of_tree.assets/array_representation_with_empty.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="任意类型二叉树的数组表示" class="animation-figure" src="../array_representation_of_tree.assets/array_representation_with_empty.png" /></a></p>
|
||||
<p align="center"> 图 7-14 任意类型二叉树的数组表示 </p>
|
||||
|
||||
<p>值得说明的是,<strong>完全二叉树非常适合使用数组来表示</strong>。回顾完全二叉树的定义,<span class="arithmatex">\(\text{None}\)</span> 只出现在最底层且靠右的位置,<strong>因此所有 <span class="arithmatex">\(\text{None}\)</span> 一定出现在层序遍历序列的末尾</strong>。</p>
|
||||
<p>这意味着使用数组表示完全二叉树时,可以省略存储所有 <span class="arithmatex">\(\text{None}\)</span> ,非常方便。图 7-15 给出了一个例子。</p>
|
||||
<p>值得说明的是,<strong>完全二叉树非常适合使用数组来表示</strong>。回顾完全二叉树的定义,<code>None</code> 只出现在最底层且靠右的位置,<strong>因此所有 <code>None</code> 一定出现在层序遍历序列的末尾</strong>。</p>
|
||||
<p>这意味着使用数组表示完全二叉树时,可以省略存储所有 <code>None</code> ,非常方便。图 7-15 给出了一个例子。</p>
|
||||
<p><a class="glightbox" href="../array_representation_of_tree.assets/array_representation_complete_binary_tree.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="完全二叉树的数组表示" class="animation-figure" src="../array_representation_of_tree.assets/array_representation_complete_binary_tree.png" /></a></p>
|
||||
<p align="center"> 图 7-15 完全二叉树的数组表示 </p>
|
||||
|
||||
@@ -4645,7 +4645,7 @@
|
||||
<ul>
|
||||
<li>数组存储需要连续内存空间,因此不适合存储数据量过大的树。</li>
|
||||
<li>增删节点需要通过数组插入与删除操作实现,效率较低。</li>
|
||||
<li>当二叉树中存在大量 <span class="arithmatex">\(\text{None}\)</span> 时,数组中包含的节点数据比重较低,空间利用率较低。</li>
|
||||
<li>当二叉树中存在大量 <code>None</code> 时,数组中包含的节点数据比重较低,空间利用率较低。</li>
|
||||
</ul>
|
||||
|
||||
<!-- Source file information -->
|
||||
|
||||
Reference in New Issue
Block a user