Release reviewed gh-pages candidate

This commit is contained in:
krahets
2026-11-18 16:34:49 +08:00
parent 8966617032
commit 451a6b692c
822 changed files with 3585 additions and 3541 deletions

View File

@@ -1935,14 +1935,14 @@
<h2 id="731-representing-a-perfect-binary-tree">7.3.1   Representing a perfect binary tree<a class="headerlink" href="#731-representing-a-perfect-binary-tree" title="Permanent link"></a></h2>
<p>Start with a simple case. Store every node of a perfect binary tree in an array in level order. Each node then corresponds to a unique array index.</p>
<p>Level-order traversal gives us a mapping between parent and child indices: <strong>if a node has index <span class="arithmatex">\(i\)</span>, its left child has index <span class="arithmatex">\(2i + 1\)</span> and its right child has index <span class="arithmatex">\(2i + 2\)</span></strong>. Figure 7-12 illustrates this relationship.</p>
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_tree/array_representation_of_tree.assets/array_representation_binary_tree.png" data-dark-src="/assets/figure-svg/figures/b392d437519f272b1b6877e4.json" data-src="/assets/figure-svg/figures/812e6e2d195c9a115f8c75a2.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_tree/array_representation_of_tree.assets/array_representation_binary_tree.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Array representation of a perfect binary tree</object><object data="/en/chapter_tree/array_representation_of_tree.assets/array_representation_binary_tree.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Array representation of a perfect binary tree</object></ha-svg-figure></p>
<p align="center"> Figure 7-12   Array representation of a perfect binary tree </p>
<p><ha-svg-figure aria-label="Array representation of a perfect binary tree" class="animation-figure" data-book-key="en/chapter_tree/array_representation_of_tree.assets/array_representation_binary_tree.png" data-dark-src="/assets/figure-svg/figures/b392d437519f272b1b6877e4.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/812e6e2d195c9a115f8c75a2.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_tree/array_representation_of_tree.assets/array_representation_binary_tree.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">Array representation of a perfect binary tree</object><object aria-hidden="true" data="/en/chapter_tree/array_representation_of_tree.assets/array_representation_binary_tree.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">Array representation of a perfect binary tree</object></ha-svg-figure></p>
<p align="center"> Figure 7-12   Array representation of a perfect binary tree </p><div class="ha-sr-only figure-description"><h3>Description of Figure 7-12: Array representation of a perfect binary tree</h3><p>Store a perfect binary tree in level order in a zero-based array, and locate left and right children with index formulas 2i+1 and 2i+2.</p><p>A 4-level perfect binary tree has 15 nodes with values 1 through 15 in level order (root 1 on level 1, 2 and 3 on level 2, 4 through 7 on level 3, and leaf nodes 8 through 15 on level 4), stored sequentially at array indices 0 through 14. The table below lists every node's index and its left and right child positions.</p><table><caption>Node indices and left and right child positions</caption><thead><tr><th scope="col">Current index</th><th scope="col">Node value</th><th scope="col">Left child: index and value</th><th scope="col">Right child: index and value</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: out of bounds, no child</td><td>16: out of bounds, no child</td></tr><tr><th scope="row">8</th><td>9</td><td>17: out of bounds, no child</td><td>18: out of bounds, no child</td></tr><tr><th scope="row">9</th><td>10</td><td>19: out of bounds, no child</td><td>20: out of bounds, no child</td></tr><tr><th scope="row">10</th><td>11</td><td>21: out of bounds, no child</td><td>22: out of bounds, no child</td></tr><tr><th scope="row">11</th><td>12</td><td>23: out of bounds, no child</td><td>24: out of bounds, no child</td></tr><tr><th scope="row">12</th><td>13</td><td>25: out of bounds, no child</td><td>26: out of bounds, no child</td></tr><tr><th scope="row">13</th><td>14</td><td>27: out of bounds, no child</td><td>28: out of bounds, no child</td></tr><tr><th scope="row">14</th><td>15</td><td>29: out of bounds, no child</td><td>30: out of bounds, no child</td></tr></tbody></table><p>Substitute the array index i—not the node value—into the formulas. For example, node 6 is at index 5, so its left and right children are at index 11 (value 12) and index 12 (value 13).</p><p>Index formulas replace child pointers, but array bounds must still be checked on access: in this figure, indices 7 through 14 (nodes 8 through 15) are leaf nodes, so their computed child positions 15 through 30 are all out of bounds, indicating no children.</p></div>
<p><strong>The mapping formulas serve the same role as node references or pointers in a linked representation</strong>. Given any node in the array, we can use them to access its left or right child.</p>
<h2 id="732-representing-an-arbitrary-binary-tree">7.3.2   Representing an arbitrary binary tree<a class="headerlink" href="#732-representing-an-arbitrary-binary-tree" title="Permanent link"></a></h2>
<p>A perfect binary tree is a special case. Other binary trees often contain many missing nodes, <code>None</code>, at intermediate levels. Ordinary level-order traversal omits these positions, so its sequence alone cannot tell us their number or locations. <strong>Multiple tree structures can therefore produce the same level-order sequence</strong>.</p>
<p>As shown below, the array representation above no longer works for a non-perfect binary tree.</p>
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_tree/array_representation_of_tree.assets/array_representation_without_empty.png" data-dark-src="/assets/figure-svg/figures/2d35f41eec6a811b5df1d477.json" data-src="/assets/figure-svg/figures/ea60507806a2a353335fe574.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_tree/array_representation_of_tree.assets/array_representation_without_empty.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Multiple binary trees can share a level-order sequence</object><object data="/en/chapter_tree/array_representation_of_tree.assets/array_representation_without_empty.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Multiple binary trees can share a level-order sequence</object></ha-svg-figure></p>
<p align="center"> Figure 7-13   Multiple binary trees can share a level-order sequence </p>
<p><ha-svg-figure aria-label="Level-order array omitting intermediate empty slots" class="animation-figure" data-book-key="en/chapter_tree/array_representation_of_tree.assets/array_representation_without_empty.png" data-dark-src="/assets/figure-svg/figures/2d35f41eec6a811b5df1d477.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/ea60507806a2a353335fe574.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_tree/array_representation_of_tree.assets/array_representation_without_empty.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">Multiple binary trees can share a level-order sequence</object><object aria-hidden="true" data="/en/chapter_tree/array_representation_of_tree.assets/array_representation_without_empty.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">Multiple binary trees can share a level-order sequence</object></ha-svg-figure></p>
<p align="center"> Figure 7-13   Multiple binary trees can share a level-order sequence </p><div class="ha-sr-only figure-description"><h3>Description of Figure 7-13: Level-order array omitting intermediate empty slots</h3><p>When empty slots inside a tree are omitted, the compacted level-order array shifts indices out of alignment, allowing the same value sequence to match different tree shapes.</p><p>In the table, "Original tree position" refers to the zero-based level-order position when empty slots are kept, with the root at position 0; "Compacted array index" refers to the array index after skipping empty slots. Only the original tree below is drawn in the figure.</p><p>The original tree has 10 nodes: root 1 (position 0) has left and right children 2 (position 1) and 3 (position 2); 2 has only left child 4 (position 3); 3 has left and right children 6 (position 5) and 7 (position 6); 4 has left and right children 8 (position 7) and 9 (position 8); 6 has only left child 12 (position 11); 7 has only right child 15 (position 14); and 8, 9, 12, and 15 are leaf nodes. The table below maps compacted array indices 0 through 9 to their original tree positions.</p><table><caption>Correspondence between compacted array indices and original tree positions</caption><thead><tr><th scope="col">Compacted array index</th><th scope="col">Node value</th><th scope="col">Original tree position</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>The original tree is missing nodes at positions 4, 9, 10, 12, and 13; skipping empty slots to compact the sequence directly into [1, 2, 3, 4, 6, 7, 8, 9, 12, 15] shifts node 6 forward from original position 5 to compacted index 4. Applying 2i+2 then misidentifies 6 at index 4 as the right child of node 2 at index 1, whereas in the original tree 2 has no right child and 6 is the left child of 3. Deriving a whole tree from the compacted array via the index formulas yields: 1 with left 2 and right 3; 2 with left 4 and right 6; 3 with left 7 and right 8; 4 with left 9 and right 12; 6 with only left 15; and all remaining nodes as leaves. That derived tree has different connections from the original tree despite sharing the same level-order value sequence; it is an analytical comparison and is not drawn separately in the figure.</p><p>Once intermediate empty slots are omitted, the same level-order value sequence can correspond to differently connected trees. This issue arises only when empty slots occur in the middle; in a complete binary tree, all empty slots lie at the end of level order, so omitting trailing empty slots preserves the index mapping.</p></div>
<p>To solve this, <strong>include every <code>None</code> position explicitly in the level-order sequence</strong>. The sequence then represents the tree uniquely, as shown below. Example code follows:</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">
@@ -2026,12 +2026,12 @@
</div>
</div>
</div>
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_tree/array_representation_of_tree.assets/array_representation_with_empty.png" data-dark-src="/assets/figure-svg/figures/ff671a8377bed4aefcd361da.json" data-src="/assets/figure-svg/figures/d59f75336599c6424576064b.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_tree/array_representation_of_tree.assets/array_representation_with_empty.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Array representation of an arbitrary binary tree</object><object data="/en/chapter_tree/array_representation_of_tree.assets/array_representation_with_empty.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Array representation of an arbitrary binary tree</object></ha-svg-figure></p>
<p align="center"> Figure 7-14   Array representation of an arbitrary binary tree </p>
<p><ha-svg-figure aria-label="Binary tree array keeping empty slots" class="animation-figure" data-book-key="en/chapter_tree/array_representation_of_tree.assets/array_representation_with_empty.png" data-dark-src="/assets/figure-svg/figures/ff671a8377bed4aefcd361da.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/d59f75336599c6424576064b.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_tree/array_representation_of_tree.assets/array_representation_with_empty.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">Array representation of an arbitrary binary tree</object><object aria-hidden="true" data="/en/chapter_tree/array_representation_of_tree.assets/array_representation_with_empty.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">Array representation of an arbitrary binary tree</object></ha-svg-figure></p>
<p align="center"> Figure 7-14   Array representation of an arbitrary binary tree </p><div class="ha-sr-only figure-description"><h3>Description of Figure 7-14: Binary tree array keeping empty slots</h3><p>Explicitly keep empty slots for missing intermediate nodes in the level-order array so the 2i+1 and 2i+2 mappings reconstruct the tree uniquely.</p><p>The 10 solid nodes are connected as follows: root 1 connects to left child 2 and right child 3; 2 connects only to left child 4; 3 connects to left child 6 and right child 7; 4 connects to left child 8 and right child 9; 6 connects only to left child 12; 7 connects only to right child 15; and 8, 9, 12, and 15 are leaf nodes. Array indices 0 through 14 span 15 cells holding 10 node values and five empty slots at indices 4, 9, 10, 12, and 13; the table below lists every cell.</p><table><caption>Array indices and node values</caption><thead><tr><th scope="col">Index</th><th scope="col">Node value or empty slot</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>Empty</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>Empty</td></tr><tr><th scope="row">10</th><td>Empty</td></tr><tr><th scope="row">11</th><td>12</td></tr><tr><th scope="row">12</th><td>Empty</td></tr><tr><th scope="row">13</th><td>Empty</td></tr><tr><th scope="row">14</th><td>15</td></tr></tbody></table><p>Even though index 4 is an empty slot with no node, its two child positions 9 and 10 on the next level must also be left empty so later nodes do not shift forward out of place. For instance, node 6 at index 5 stores 12 at left child position 11 and has an empty right child position 12.</p><p>Keeping intermediate empty slots explicitly lets child positions 2i+1 and 2i+2 for index i reconstruct this tree uniquely. An empty slot means no node exists there (often written None or null in code), not the numeric value 0.</p></div>
<p><strong>Complete binary trees are particularly well suited to array representation</strong>. By definition, missing positions occur only at the right end of the lowest level, <strong>so all <code>None</code> entries appear at the end of the level-order sequence</strong>.</p>
<p>We can therefore omit all <code>None</code> entries when storing a complete binary tree in an array. An example is shown below.</p>
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_tree/array_representation_of_tree.assets/array_representation_complete_binary_tree.png" data-dark-src="/assets/figure-svg/figures/a070e69d1ad1772032577cca.json" data-src="/assets/figure-svg/figures/dc96b54b5128bb22f8e30e4a.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_tree/array_representation_of_tree.assets/array_representation_complete_binary_tree.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Array representation of a complete binary tree</object><object data="/en/chapter_tree/array_representation_of_tree.assets/array_representation_complete_binary_tree.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Array representation of a complete binary tree</object></ha-svg-figure></p>
<p align="center"> Figure 7-15   Array representation of a complete binary tree </p>
<p><ha-svg-figure aria-label="Array representation of a complete binary tree can omit trailing empty slots: Omitting the five trailing empty slots in the level-order array of a complete binary tree" class="animation-figure" data-book-key="en/chapter_tree/array_representation_of_tree.assets/array_representation_complete_binary_tree.png" data-dark-src="/assets/figure-svg/figures/a070e69d1ad1772032577cca.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/dc96b54b5128bb22f8e30e4a.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_tree/array_representation_of_tree.assets/array_representation_complete_binary_tree.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">Array representation of a complete binary tree</object><object aria-hidden="true" data="/en/chapter_tree/array_representation_of_tree.assets/array_representation_complete_binary_tree.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">Array representation of a complete binary tree</object></ha-svg-figure></p>
<p align="center"> Figure 7-15   Array representation of a complete binary tree </p><div class="ha-sr-only figure-description"><h3>Description of Figure 7-15: Array representation of a complete binary tree can omit trailing empty slots</h3><p>In a complete binary tree, empty slots appear only at the end of the level-order array and can be omitted without changing existing node indices.</p><p>In a complete binary tree, every level except possibly the last is completely filled, and nodes on the last level are packed contiguously from left to right. Array indices start at 0 here, so the ten actual nodes occupy indices 0 through 9.</p><p>On the left, the four-level complete binary tree has 10 solid nodes: root 1 has left and right children 2 and 3; 2 has left and right children 4 and 5; 3 has left and right children 6 and 7; 4 has left and right children 8 and 9; 5 has only left child 10; and 6, 7, 8, 9, and 10 are leaf nodes. On the fourth level, to the right of node 10, five gray dashed empty circles represent the unfilled trailing positions.</p><p>At upper right, "Level-order traversal sequence (array)" first shows [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] together with the five trailing dashed empty cells. A downward arrow in the middle, paired with the gray note "For a complete binary tree, empty slots need not be stored", points to the final array [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] at lower right. Because all five omitted empty slots lie at the very end of the sequence, the array indices of the first ten actual nodes remain completely unchanged.</p><p>For example, node 5 is at index 4: left child position 2×4+1 = 9 stores node 10, whereas right child position 2×4+2 = 10 falls past the end of the length-10 array, indicating no right child.</p><p>After omitting trailing empty slots, the 2i+1 and 2i+2 mappings still hold; check array bounds when accessing a computed child position.</p></div>
<p>The following code implements an array-based binary tree with these operations.</p>
<ul>
<li>Given a node, get its value, left or right child, and parent.</li>
@@ -3329,5 +3329,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/a704d8e80a80ddc71f2a03a7.js"></script><script defer="" src="/assets/figure-svg/js/bff17b8259339758cb1c4a06.js"></script></body>
<script defer="" src="/assets/figure-svg/js/00174ff861e190f5c1f09e34.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/70ae82c6406c9bdbda8e8362.js"></script></body>
</html>