This commit is contained in:
krahets
2024-05-01 07:30:15 +08:00
parent 85f0bc4ed1
commit d246e08cc6
68 changed files with 220 additions and 220 deletions
@@ -3613,18 +3613,18 @@
<p>So, can we use an array to represent a binary tree? The answer is yes.</p>
<h2 id="731-representing-perfect-binary-trees">7.3.1 &nbsp; Representing perfect binary trees<a class="headerlink" href="#731-representing-perfect-binary-trees" title="Permanent link">&para;</a></h2>
<p>Let's analyze a simple case first. Given a perfect binary tree, we store all nodes in an array according to the order of level-order traversal, where each node corresponds to a unique array index.</p>
<p>Based on the characteristics of level-order traversal, we can deduce a "mapping formula" between the index of a parent node and its children: <strong>If a node's index is <span class="arithmatex">\(i\)</span>, then the index of its left child is <span class="arithmatex">\(2i + 1\)</span> and the right child is <span class="arithmatex">\(2i + 2\)</span></strong>. The Figure 7-12 shows the mapping relationship between the indices of various nodes.</p>
<p>Based on the characteristics of level-order traversal, we can deduce a "mapping formula" between the index of a parent node and its children: <strong>If a node's index is <span class="arithmatex">\(i\)</span>, then the index of its left child is <span class="arithmatex">\(2i + 1\)</span> and the right child is <span class="arithmatex">\(2i + 2\)</span></strong>. Figure 7-12 shows the mapping relationship between the indices of various nodes.</p>
<p><a class="glightbox" href="../array_representation_of_tree.assets/array_representation_binary_tree.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Array representation of a perfect binary tree" class="animation-figure" src="../array_representation_of_tree.assets/array_representation_binary_tree.png" /></a></p>
<p align="center"> Figure 7-12 &nbsp; Array representation of a perfect binary tree </p>
<p><strong>The mapping formula plays a role similar to the node references (pointers) in linked lists</strong>. Given any node in the array, we can access its left (right) child node using the mapping formula.</p>
<h2 id="732-representing-any-binary-tree">7.3.2 &nbsp; Representing any binary tree<a class="headerlink" href="#732-representing-any-binary-tree" title="Permanent link">&para;</a></h2>
<p>Perfect binary trees are a special case; there are often many <code>None</code> values in the middle levels of a binary tree. Since the sequence of level-order traversal does not include these <code>None</code> values, we cannot solely rely on this sequence to deduce the number and distribution of <code>None</code> values. <strong>This means that multiple binary tree structures can match the same level-order traversal sequence</strong>.</p>
<p>As shown in the Figure 7-13 , given a non-perfect binary tree, the above method of array representation fails.</p>
<p>As shown in Figure 7-13, given a non-perfect binary tree, the above method of array representation fails.</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="Level-order traversal sequence corresponds to multiple binary tree possibilities" class="animation-figure" src="../array_representation_of_tree.assets/array_representation_without_empty.png" /></a></p>
<p align="center"> Figure 7-13 &nbsp; Level-order traversal sequence corresponds to multiple binary tree possibilities </p>
<p>To solve this problem, <strong>we can consider explicitly writing out all <code>None</code> values in the level-order traversal sequence</strong>. As shown in the following figure, after this treatment, the level-order traversal sequence can uniquely represent a binary tree. Example code is as follows:</p>
<p>To solve this problem, <strong>we can consider explicitly writing out all <code>None</code> values in the level-order traversal sequence</strong>. As shown in Figure 7-14, after this treatment, the level-order traversal sequence can uniquely represent a binary tree. Example code is as follows:</p>
<div class="tabbed-set tabbed-alternate" data-tabs="1:14"><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" /><input id="__tabbed_1_14" 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><label for="__tabbed_1_14">Zig</label></div>
<div class="tabbed-content">
<div class="tabbed-block">
@@ -3713,7 +3713,7 @@
<p align="center"> Figure 7-14 &nbsp; Array representation of any type of binary tree </p>
<p>It's worth noting that <strong>complete binary trees are very suitable for array representation</strong>. Recalling the definition of a complete binary tree, <code>None</code> appears only at the bottom level and towards the right, <strong>meaning all <code>None</code> values definitely appear at the end of the level-order traversal sequence</strong>.</p>
<p>This means that when using an array to represent a complete binary tree, it's possible to omit storing all <code>None</code> values, which is very convenient. The Figure 7-15 gives an example.</p>
<p>This means that when using an array to represent a complete binary tree, it's possible to omit storing all <code>None</code> values, which is very convenient. Figure 7-15 gives an example.</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="Array representation of a complete binary tree" class="animation-figure" src="../array_representation_of_tree.assets/array_representation_complete_binary_tree.png" /></a></p>
<p align="center"> Figure 7-15 &nbsp; Array representation of a complete binary tree </p>