mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-29 15:54:44 +08:00
Release reviewed gh-pages candidate
This commit is contained in:
@@ -47,7 +47,7 @@
|
||||
set(key, value, storage, key === "__palette" ? shared : scope);
|
||||
})();
|
||||
</script>
|
||||
<link href="/assets/figure-svg/css/ca08fd1c3a121ac517cf0998.css" rel="stylesheet"/><link href="/assets/figure-svg/css/9f909651eeceaf5c7dda140d.css" rel="stylesheet"/></head>
|
||||
<link href="/assets/figure-svg/css/ca08fd1c3a121ac517cf0998.css" rel="stylesheet"/><link href="/assets/figure-svg/css/f41655ef506ff5ce3cf0fd42.css" rel="stylesheet"/></head>
|
||||
<body data-md-color-accent="teal" data-md-color-primary="white" data-md-color-scheme="default" dir="ltr">
|
||||
<input autocomplete="off" class="md-toggle" data-md-toggle="drawer" id="__drawer" type="checkbox"/>
|
||||
<input autocomplete="off" class="md-toggle" data-md-toggle="search" id="__search" type="checkbox"/>
|
||||
@@ -1963,14 +1963,14 @@
|
||||
<h2 id="731">7.3.1 表示完美二叉树<a class="headerlink" href="#731" title="Permanent link">¶</a></h2>
|
||||
<p>先分析一个简单案例。给定一棵完美二叉树,我们将所有节点按照层序遍历的顺序存储在一个数组中,则每个节点都对应唯一的数组索引。</p>
|
||||
<p>根据层序遍历的特性,我们可以推导出父节点索引与子节点索引之间的“映射公式”:<strong>若某节点的索引为 <span class="arithmatex">\(i\)</span> ,则该节点的左子节点索引为 <span class="arithmatex">\(2i + 1\)</span> ,右子节点索引为 <span class="arithmatex">\(2i + 2\)</span></strong> 。图 7-12 展示了各个节点索引之间的映射关系。</p>
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="chapter_tree/array_representation_of_tree.assets/array_representation_binary_tree.png" data-dark-src="/assets/figure-svg/figures/dbbe37dbebc2d908f4c36372.json" data-src="/assets/figure-svg/figures/db52a14ab8484eb6a9dfb4e0.json" style=";--figure-ratio:960.0/540.0"><object data="/chapter_tree/array_representation_of_tree.assets/array_representation_binary_tree.svg" data-theme="light" tabindex="-1" type="image/svg+xml">完美二叉树的数组表示</object><object data="/chapter_tree/array_representation_of_tree.assets/array_representation_binary_tree.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">完美二叉树的数组表示</object></ha-svg-figure></p>
|
||||
<p align="center"> 图 7-12 完美二叉树的数组表示 </p>
|
||||
<p><ha-svg-figure aria-label="完美二叉树的数组表示" class="animation-figure" data-book-key="chapter_tree/array_representation_of_tree.assets/array_representation_binary_tree.png" data-dark-src="/assets/figure-svg/figures/dbbe37dbebc2d908f4c36372.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/db52a14ab8484eb6a9dfb4e0.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/chapter_tree/array_representation_of_tree.assets/array_representation_binary_tree.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">完美二叉树的数组表示</object><object aria-hidden="true" data="/chapter_tree/array_representation_of_tree.assets/array_representation_binary_tree.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">完美二叉树的数组表示</object></ha-svg-figure></p>
|
||||
<p align="center"> 图 7-12 完美二叉树的数组表示 </p><div class="ha-sr-only figure-description"><h3>图解 7-12:完美二叉树的数组表示</h3><p>将完美二叉树按层序存入零基数组,用索引算式 2i+1 与 2i+2 定位左右子节点。</p><p>4 层完美二叉树共 15 个节点,节点值按层序为 1 至 15(第 1 层根为 1,第 2 层为 2、3,第 3 层为 4 至 7,第 4 层叶节点为 8 至 15),依次存入索引 0 至 14 的数组。全部节点的索引与左右子节点位置见下表。</p><table><caption>节点索引与左右子节点位置</caption><thead><tr><th scope="col">当前索引</th><th scope="col">节点值</th><th scope="col">左子节点:索引和值</th><th scope="col">右子节点:索引和值</th></tr></thead><tbody><tr><th scope="row">0</th><td>1</td><td>1:2</td><td>2:3</td></tr><tr><th scope="row">1</th><td>2</td><td>3:4</td><td>4:5</td></tr><tr><th scope="row">2</th><td>3</td><td>5:6</td><td>6:7</td></tr><tr><th scope="row">3</th><td>4</td><td>7:8</td><td>8:9</td></tr><tr><th scope="row">4</th><td>5</td><td>9:10</td><td>10:11</td></tr><tr><th scope="row">5</th><td>6</td><td>11:12</td><td>12:13</td></tr><tr><th scope="row">6</th><td>7</td><td>13:14</td><td>14:15</td></tr><tr><th scope="row">7</th><td>8</td><td>15:越界,无子节点</td><td>16:越界,无子节点</td></tr><tr><th scope="row">8</th><td>9</td><td>17:越界,无子节点</td><td>18:越界,无子节点</td></tr><tr><th scope="row">9</th><td>10</td><td>19:越界,无子节点</td><td>20:越界,无子节点</td></tr><tr><th scope="row">10</th><td>11</td><td>21:越界,无子节点</td><td>22:越界,无子节点</td></tr><tr><th scope="row">11</th><td>12</td><td>23:越界,无子节点</td><td>24:越界,无子节点</td></tr><tr><th scope="row">12</th><td>13</td><td>25:越界,无子节点</td><td>26:越界,无子节点</td></tr><tr><th scope="row">13</th><td>14</td><td>27:越界,无子节点</td><td>28:越界,无子节点</td></tr><tr><th scope="row">14</th><td>15</td><td>29:越界,无子节点</td><td>30:越界,无子节点</td></tr></tbody></table><p>公式代入的是数组索引 i,而非节点值。例如节点 6 位于索引 5,其左、右子节点分别在索引 11(值 12)和索引 12(值 13)。</p><p>索引算式可替代子节点指针,但访问时仍须检查数组边界:本图索引 7 至 14(节点 8 至 15)是叶节点,算出的子位置 15 至 30 全部越界,表示无子节点。</p></div>
|
||||
<p><strong>映射公式的角色相当于链表中的节点引用(指针)</strong>。给定数组中的任意一个节点,我们都可以通过映射公式来访问它的左(右)子节点。</p>
|
||||
<h2 id="732">7.3.2 表示任意二叉树<a class="headerlink" href="#732" title="Permanent link">¶</a></h2>
|
||||
<p>完美二叉树是一个特例,在二叉树的中间层通常存在许多 <code>None</code> 。由于层序遍历序列并不包含这些 <code>None</code> ,因此我们无法仅凭该序列来推测 <code>None</code> 的数量和分布位置。<strong>这意味着存在多种二叉树结构都符合该层序遍历序列</strong>。</p>
|
||||
<p>如图 7-13 所示,给定一棵非完美二叉树,上述数组表示方法已经失效。</p>
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="chapter_tree/array_representation_of_tree.assets/array_representation_without_empty.png" data-dark-src="/assets/figure-svg/figures/ffdc894b7592dcc5ec27c31b.json" data-src="/assets/figure-svg/figures/ae64dcd97a6b6f9e070727ab.json" style=";--figure-ratio:960.0/540.0"><object data="/chapter_tree/array_representation_of_tree.assets/array_representation_without_empty.svg" data-theme="light" tabindex="-1" type="image/svg+xml">层序遍历序列对应多种二叉树可能性</object><object data="/chapter_tree/array_representation_of_tree.assets/array_representation_without_empty.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">层序遍历序列对应多种二叉树可能性</object></ha-svg-figure></p>
|
||||
<p align="center"> 图 7-13 层序遍历序列对应多种二叉树可能性 </p>
|
||||
<p><ha-svg-figure aria-label="省略中间空位的层序数组" class="animation-figure" data-book-key="chapter_tree/array_representation_of_tree.assets/array_representation_without_empty.png" data-dark-src="/assets/figure-svg/figures/ffdc894b7592dcc5ec27c31b.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/ae64dcd97a6b6f9e070727ab.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/chapter_tree/array_representation_of_tree.assets/array_representation_without_empty.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">层序遍历序列对应多种二叉树可能性</object><object aria-hidden="true" data="/chapter_tree/array_representation_of_tree.assets/array_representation_without_empty.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">层序遍历序列对应多种二叉树可能性</object></ha-svg-figure></p>
|
||||
<p align="center"> 图 7-13 层序遍历序列对应多种二叉树可能性 </p><div class="ha-sr-only figure-description"><h3>图解 7-13:省略中间空位的层序数组</h3><p>省略树中间的空位后,压紧的层序数组会使索引错位,同一数值序列可对应不同树形。</p><p>表中的“原树位置编号”指保留空位时的零基层序位置,根为位置 0;“压紧索引”指跳过空位后的数组索引。图中只画了下面这棵原树。</p><p>原树共 10 个节点:根为 1(位置 0),左右子节点为 2(位置 1)、3(位置 2);2 只有左子节点 4(位置 3);3 的左右子节点为 6(位置 5)、7(位置 6);4 的左右子节点为 8(位置 7)、9(位置 8);6 只有左子节点 12(位置 11);7 只有右子节点 15(位置 14);8、9、12、15 为叶节点。压紧数组索引 0 至 9 与原树位置编号的对应见下表。</p><table><caption>压紧数组与原树位置的对应</caption><thead><tr><th scope="col">压紧数组索引</th><th scope="col">节点值</th><th scope="col">原树位置编号</th></tr></thead><tbody><tr><th scope="row">0</th><td>1</td><td>0</td></tr><tr><th scope="row">1</th><td>2</td><td>1</td></tr><tr><th scope="row">2</th><td>3</td><td>2</td></tr><tr><th scope="row">3</th><td>4</td><td>3</td></tr><tr><th scope="row">4</th><td>6</td><td>5</td></tr><tr><th scope="row">5</th><td>7</td><td>6</td></tr><tr><th scope="row">6</th><td>8</td><td>7</td></tr><tr><th scope="row">7</th><td>9</td><td>8</td></tr><tr><th scope="row">8</th><td>12</td><td>11</td></tr><tr><th scope="row">9</th><td>15</td><td>14</td></tr></tbody></table><p>原树缺少位置 4、9、10、12、13 的节点;若跳过空位直接压紧为 [1, 2, 3, 4, 6, 7, 8, 9, 12, 15],节点 6 会从原位置 5 前移到压紧索引 4。此时套用 2i+2 会把索引 4 的 6 误判为索引 1 的节点 2 的右子节点,但原树中 2 无右子节点、6 是 3 的左子节点。进一步按索引公式从压紧数组推导另一棵树:1 左接 2、右接 3;2 左接 4、右接 6;3 左接 7、右接 8;4 左接 9、右接 12;6 仅左接 15,其余为叶节点。它与原树连接不同,却有相同的层序数值序列;这棵对照树是推导结果,图中没有另画。</p><p>省略中间空位后,同一层序数值序列可对应不同连接的树。该问题针对中间存在空位的情形;完全二叉树的空位都在层序末尾,省略末尾空位不会破坏索引映射。</p></div>
|
||||
<p>为了解决此问题,<strong>我们可以考虑在层序遍历序列中显式地写出所有 <code>None</code></strong> 。如图 7-14 所示,这样处理后,层序遍历序列就可以唯一表示二叉树了。示例代码如下:</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="1:13"><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"/><input id="__tabbed_1_13" 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">Kotlin</label><label for="__tabbed_1_13">Ruby</label></div>
|
||||
<div class="tabbed-content">
|
||||
@@ -2054,12 +2054,12 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="chapter_tree/array_representation_of_tree.assets/array_representation_with_empty.png" data-dark-src="/assets/figure-svg/figures/488c08d0be33c1ee7e4338be.json" data-src="/assets/figure-svg/figures/e57863c3d63e43b9a0508f3b.json" style=";--figure-ratio:960.0/540.0"><object data="/chapter_tree/array_representation_of_tree.assets/array_representation_with_empty.svg" data-theme="light" tabindex="-1" type="image/svg+xml">任意类型二叉树的数组表示</object><object data="/chapter_tree/array_representation_of_tree.assets/array_representation_with_empty.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">任意类型二叉树的数组表示</object></ha-svg-figure></p>
|
||||
<p align="center"> 图 7-14 任意类型二叉树的数组表示 </p>
|
||||
<p><ha-svg-figure aria-label="保留空位的二叉树数组" class="animation-figure" data-book-key="chapter_tree/array_representation_of_tree.assets/array_representation_with_empty.png" data-dark-src="/assets/figure-svg/figures/488c08d0be33c1ee7e4338be.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/e57863c3d63e43b9a0508f3b.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/chapter_tree/array_representation_of_tree.assets/array_representation_with_empty.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">任意类型二叉树的数组表示</object><object aria-hidden="true" data="/chapter_tree/array_representation_of_tree.assets/array_representation_with_empty.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">任意类型二叉树的数组表示</object></ha-svg-figure></p>
|
||||
<p align="center"> 图 7-14 任意类型二叉树的数组表示 </p><div class="ha-sr-only figure-description"><h3>图解 7-14:保留空位的二叉树数组</h3><p>在层序数组中显式保留中间缺失节点的空位,使 2i+1 与 2i+2 映射能唯一还原树形。</p><p>10 个实心节点连接为:根 1 连左 2、右 3;2 仅连左 4;3 连左 6、右 7;4 连左 8、右 9;6 仅连左 12;7 仅连右 15;8、9、12、15 为叶节点。数组索引 0 至 14 共 15 格,存放 10 个节点值和索引 4、9、10、12、13 五个空位;逐格内容见下表。</p><table><caption>数组索引与节点值</caption><thead><tr><th scope="col">索引</th><th scope="col">节点值或空位</th></tr></thead><tbody><tr><th scope="row">0</th><td>1</td></tr><tr><th scope="row">1</th><td>2</td></tr><tr><th scope="row">2</th><td>3</td></tr><tr><th scope="row">3</th><td>4</td></tr><tr><th scope="row">4</th><td>空</td></tr><tr><th scope="row">5</th><td>6</td></tr><tr><th scope="row">6</th><td>7</td></tr><tr><th scope="row">7</th><td>8</td></tr><tr><th scope="row">8</th><td>9</td></tr><tr><th scope="row">9</th><td>空</td></tr><tr><th scope="row">10</th><td>空</td></tr><tr><th scope="row">11</th><td>12</td></tr><tr><th scope="row">12</th><td>空</td></tr><tr><th scope="row">13</th><td>空</td></tr><tr><th scope="row">14</th><td>15</td></tr></tbody></table><p>索引 4 的空位虽无节点,它在下一层的两个子位置 9、10 也必须留空,才能保证更后的节点不向前错位。例如索引 5 的节点 6,左子位置 11 存 12,右子位置 12 为空。</p><p>显式保留中间空位后,可按索引 i 的子位置 2i+1、2i+2 唯一还原这棵树。空位表示该处不存在节点(代码中常记为 None 或 null),不是数值 0。</p></div>
|
||||
<p>值得说明的是,<strong>完全二叉树非常适合使用数组来表示</strong>。回顾完全二叉树的定义,<code>None</code> 只出现在最底层且靠右的位置,<strong>因此所有 <code>None</code> 一定出现在层序遍历序列的末尾</strong>。</p>
|
||||
<p>这意味着使用数组表示完全二叉树时,可以省略存储所有 <code>None</code> ,非常方便。图 7-15 给出了一个例子。</p>
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="chapter_tree/array_representation_of_tree.assets/array_representation_complete_binary_tree.png" data-dark-src="/assets/figure-svg/figures/10e96ebd591650ca2e0e50f9.json" data-src="/assets/figure-svg/figures/56ea0519e480ed3790d57400.json" style=";--figure-ratio:960.0/540.0"><object data="/chapter_tree/array_representation_of_tree.assets/array_representation_complete_binary_tree.svg" data-theme="light" tabindex="-1" type="image/svg+xml">完全二叉树的数组表示</object><object data="/chapter_tree/array_representation_of_tree.assets/array_representation_complete_binary_tree.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">完全二叉树的数组表示</object></ha-svg-figure></p>
|
||||
<p align="center"> 图 7-15 完全二叉树的数组表示 </p>
|
||||
<p><ha-svg-figure aria-label="完全二叉树的数组表示可省略末尾空位:省略完全二叉树层序数组末尾的五个空位" class="animation-figure" data-book-key="chapter_tree/array_representation_of_tree.assets/array_representation_complete_binary_tree.png" data-dark-src="/assets/figure-svg/figures/10e96ebd591650ca2e0e50f9.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/56ea0519e480ed3790d57400.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/chapter_tree/array_representation_of_tree.assets/array_representation_complete_binary_tree.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">完全二叉树的数组表示</object><object aria-hidden="true" data="/chapter_tree/array_representation_of_tree.assets/array_representation_complete_binary_tree.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">完全二叉树的数组表示</object></ha-svg-figure></p>
|
||||
<p align="center"> 图 7-15 完全二叉树的数组表示 </p><div class="ha-sr-only figure-description"><h3>图解 7-15:完全二叉树的数组表示可省略末尾空位</h3><p>完全二叉树的空位集中在层序数组末尾,可以省略而不改变已有节点的索引。</p><p>完全二叉树除最后一层外各层均被填满,且最后一层的节点必须从左到右连续紧密排列。本图数组索引从 0 开始,十个实际节点占用索引 0 至 9。</p><p>左侧四层完全二叉树共有 10 个实心节点:根为 1,左、右子节点为 2、3;2 的左、右子节点为 4、5;3 的左、右子节点为 6、7;4 的左、右子节点为 8、9;5 只有左子节点 10;6、7、8、9、10 为叶节点。第四层节点 10 的右侧依次画出五个灰色虚线空圆,代表末尾未填满的空位置。</p><p>右上方的“层序遍历序列(数组)”先完整画出 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 以及末尾附带的五个虚线空格。中间向下的箭头配合灰色说明框“若为完全二叉树 则可不存储空位”,指向右下方的最终数组 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]。由于省去的五个空位全都位于序列最末端,前十个实际节点的数组索引丝毫未变。</p><p>例如节点 5 位于索引 4:左子位置 2×4+1 = 9,存着节点 10;右子位置 2×4+2 = 10,已越出长度为 10 的数组,表示没有右子节点。</p><p>省略末尾空位后,2i+1 与 2i+2 的映射仍成立;访问计算出的子位置时,需要检查数组边界。</p></div>
|
||||
<p>以下代码实现了一棵基于数组表示的二叉树,包括以下几种操作。</p>
|
||||
<ul>
|
||||
<li>给定某节点,获取它的值、左(右)子节点、父节点。</li>
|
||||
@@ -3357,5 +3357,5 @@
|
||||
<script src="../../javascripts/mathjax.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.min.js"></script>
|
||||
<script src="../../javascripts/animation_player.js"></script>
|
||||
<script defer="" src="/assets/figure-svg/js/bcdea376f01bc2bc0cac7c6c.js"></script><script defer="" src="/assets/figure-svg/js/e84f59519170c0f5ef238548.js"></script><script defer="" src="/assets/figure-svg/js/99a707e9f0a14d8280f16808.js"></script><script defer="" src="/assets/figure-svg/js/b789dc8b01e43e756fc89964.js"></script></body>
|
||||
<script defer="" src="/assets/figure-svg/js/bcdea376f01bc2bc0cac7c6c.js"></script><script defer="" src="/assets/figure-svg/js/e84f59519170c0f5ef238548.js"></script><script defer="" src="/assets/figure-svg/js/a704d8e80a80ddc71f2a03a7.js"></script><script defer="" src="/assets/figure-svg/js/bff17b8259339758cb1c4a06.js"></script></body>
|
||||
</html>
|
||||
Binary file not shown.
Reference in New Issue
Block a user