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>
+10 -10
View File
@@ -3844,11 +3844,11 @@
<!-- Page content -->
<h1 id="75-avl-tree">7.5 &nbsp; AVL tree *<a class="headerlink" href="#75-avl-tree" title="Permanent link">&para;</a></h1>
<p>In the "Binary Search Tree" section, we mentioned that after multiple insertions and removals, a binary search tree might degrade to a linked list. In such cases, the time complexity of all operations degrades from <span class="arithmatex">\(O(\log n)\)</span> to <span class="arithmatex">\(O(n)\)</span>.</p>
<p>As shown in the Figure 7-24 , after two node removal operations, this binary search tree will degrade into a linked list.</p>
<p>As shown in Figure 7-24, after two node removal operations, this binary search tree will degrade into a linked list.</p>
<p><a class="glightbox" href="../avl_tree.assets/avltree_degradation_from_removing_node.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Degradation of an AVL tree after removing nodes" class="animation-figure" src="../avl_tree.assets/avltree_degradation_from_removing_node.png" /></a></p>
<p align="center"> Figure 7-24 &nbsp; Degradation of an AVL tree after removing nodes </p>
<p>For example, in the perfect binary tree shown in the Figure 7-25 , after inserting two nodes, the tree will lean heavily to the left, and the time complexity of search operations will also degrade.</p>
<p>For example, in the perfect binary tree shown in Figure 7-25, after inserting two nodes, the tree will lean heavily to the left, and the time complexity of search operations will also degrade.</p>
<p><a class="glightbox" href="../avl_tree.assets/avltree_degradation_from_inserting_node.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Degradation of an AVL tree after inserting nodes" class="animation-figure" src="../avl_tree.assets/avltree_degradation_from_inserting_node.png" /></a></p>
<p align="center"> Figure 7-25 &nbsp; Degradation of an AVL tree after inserting nodes </p>
@@ -4432,7 +4432,7 @@
<p>The characteristic feature of an AVL tree is the "rotation" operation, which can restore balance to an unbalanced node without affecting the in-order traversal sequence of the binary tree. In other words, <strong>the rotation operation can maintain the property of a "binary search tree" while also turning the tree back into a "balanced binary tree"</strong>.</p>
<p>We call nodes with an absolute balance factor <span class="arithmatex">\(&gt; 1\)</span> "unbalanced nodes". Depending on the type of imbalance, there are four kinds of rotations: right rotation, left rotation, right-left rotation, and left-right rotation. Below, we detail these rotation operations.</p>
<h3 id="1-right-rotation">1. &nbsp; Right rotation<a class="headerlink" href="#1-right-rotation" title="Permanent link">&para;</a></h3>
<p>As shown in the Figure 7-26 , the first unbalanced node from the bottom up in the binary tree is "node 3". Focusing on the subtree with this unbalanced node as the root, denoted as <code>node</code>, and its left child as <code>child</code>, perform a "right rotation". After the right rotation, the subtree is balanced again while still maintaining the properties of a binary search tree.</p>
<p>As shown in Figure 7-26, the first unbalanced node from the bottom up in the binary tree is "node 3". Focusing on the subtree with this unbalanced node as the root, denoted as <code>node</code>, and its left child as <code>child</code>, perform a "right rotation". After the right rotation, the subtree is balanced again while still maintaining the properties of a binary search tree.</p>
<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">&lt;1&gt;</label><label for="__tabbed_4_2">&lt;2&gt;</label><label for="__tabbed_4_3">&lt;3&gt;</label><label for="__tabbed_4_4">&lt;4&gt;</label></div>
<div class="tabbed-content">
<div class="tabbed-block">
@@ -4451,7 +4451,7 @@
</div>
<p align="center"> Figure 7-26 &nbsp; Steps of right rotation </p>
<p>As shown in the Figure 7-27 , when the <code>child</code> node has a right child (denoted as <code>grand_child</code>), a step needs to be added in the right rotation: set <code>grand_child</code> as the left child of <code>node</code>.</p>
<p>As shown in Figure 7-27, when the <code>child</code> node has a right child (denoted as <code>grand_child</code>), a step needs to be added in the right rotation: set <code>grand_child</code> as the left child of <code>node</code>.</p>
<p><a class="glightbox" href="../avl_tree.assets/avltree_right_rotate_with_grandchild.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Right rotation with grand_child" class="animation-figure" src="../avl_tree.assets/avltree_right_rotate_with_grandchild.png" /></a></p>
<p align="center"> Figure 7-27 &nbsp; Right rotation with grand_child </p>
@@ -4690,11 +4690,11 @@
</div>
</div>
<h3 id="2-left-rotation">2. &nbsp; Left rotation<a class="headerlink" href="#2-left-rotation" title="Permanent link">&para;</a></h3>
<p>Correspondingly, if considering the "mirror" of the above unbalanced binary tree, the "left rotation" operation shown in the Figure 7-28 needs to be performed.</p>
<p>Correspondingly, if considering the "mirror" of the above unbalanced binary tree, the "left rotation" operation shown in Figure 7-28 needs to be performed.</p>
<p><a class="glightbox" href="../avl_tree.assets/avltree_left_rotate.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Left rotation operation" class="animation-figure" src="../avl_tree.assets/avltree_left_rotate.png" /></a></p>
<p align="center"> Figure 7-28 &nbsp; Left rotation operation </p>
<p>Similarly, as shown in the Figure 7-29 , when the <code>child</code> node has a left child (denoted as <code>grand_child</code>), a step needs to be added in the left rotation: set <code>grand_child</code> as the right child of <code>node</code>.</p>
<p>Similarly, as shown in Figure 7-29, when the <code>child</code> node has a left child (denoted as <code>grand_child</code>), a step needs to be added in the left rotation: set <code>grand_child</code> as the right child of <code>node</code>.</p>
<p><a class="glightbox" href="../avl_tree.assets/avltree_left_rotate_with_grandchild.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Left rotation with grand_child" class="animation-figure" src="../avl_tree.assets/avltree_left_rotate_with_grandchild.png" /></a></p>
<p align="center"> Figure 7-29 &nbsp; Left rotation with grand_child </p>
@@ -4933,21 +4933,21 @@
</div>
</div>
<h3 id="3-right-left-rotation">3. &nbsp; Right-left rotation<a class="headerlink" href="#3-right-left-rotation" title="Permanent link">&para;</a></h3>
<p>For the unbalanced node 3 shown in the Figure 7-30 , using either left or right rotation alone cannot restore balance to the subtree. In this case, a "left rotation" needs to be performed on <code>child</code> first, followed by a "right rotation" on <code>node</code>.</p>
<p>For the unbalanced node 3 shown in Figure 7-30, using either left or right rotation alone cannot restore balance to the subtree. In this case, a "left rotation" needs to be performed on <code>child</code> first, followed by a "right rotation" on <code>node</code>.</p>
<p><a class="glightbox" href="../avl_tree.assets/avltree_left_right_rotate.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Right-left rotation" class="animation-figure" src="../avl_tree.assets/avltree_left_right_rotate.png" /></a></p>
<p align="center"> Figure 7-30 &nbsp; Right-left rotation </p>
<h3 id="4-left-right-rotation">4. &nbsp; Left-right rotation<a class="headerlink" href="#4-left-right-rotation" title="Permanent link">&para;</a></h3>
<p>As shown in the Figure 7-31 , for the mirror case of the above unbalanced binary tree, a "right rotation" needs to be performed on <code>child</code> first, followed by a "left rotation" on <code>node</code>.</p>
<p>As shown in Figure 7-31, for the mirror case of the above unbalanced binary tree, a "right rotation" needs to be performed on <code>child</code> first, followed by a "left rotation" on <code>node</code>.</p>
<p><a class="glightbox" href="../avl_tree.assets/avltree_right_left_rotate.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Left-right rotation" class="animation-figure" src="../avl_tree.assets/avltree_right_left_rotate.png" /></a></p>
<p align="center"> Figure 7-31 &nbsp; Left-right rotation </p>
<h3 id="5-choice-of-rotation">5. &nbsp; Choice of rotation<a class="headerlink" href="#5-choice-of-rotation" title="Permanent link">&para;</a></h3>
<p>The four kinds of imbalances shown in the Figure 7-32 correspond to the cases described above, respectively requiring right rotation, left-right rotation, right-left rotation, and left rotation.</p>
<p>The four kinds of imbalances shown in Figure 7-32 correspond to the cases described above, respectively requiring right rotation, left-right rotation, right-left rotation, and left rotation.</p>
<p><a class="glightbox" href="../avl_tree.assets/avltree_rotation_cases.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="The four rotation cases of AVL tree" class="animation-figure" src="../avl_tree.assets/avltree_rotation_cases.png" /></a></p>
<p align="center"> Figure 7-32 &nbsp; The four rotation cases of AVL tree </p>
<p>As shown in the Table 7-3 , we determine which of the above cases an unbalanced node belongs to by judging the sign of the balance factor of the unbalanced node and its higher-side child's balance factor.</p>
<p>As shown in Table 7-3, we determine which of the above cases an unbalanced node belongs to by judging the sign of the balance factor of the unbalanced node and its higher-side child's balance factor.</p>
<p align="center"> Table 7-3 &nbsp; Conditions for Choosing Among the Four Rotation Cases </p>
<div class="center-table">
File diff suppressed because one or more lines are too long
+9 -9
View File
@@ -3933,12 +3933,12 @@
</div>
</div>
<p>Each node has two references (pointers), pointing to the "left-child node" and "right-child node," respectively. This node is called the "parent node" of these two child nodes. When given a node of a binary tree, we call the tree formed by this node's left child and all nodes under it the "left subtree" of this node. Similarly, the "right subtree" can be defined.</p>
<p><strong>In a binary tree, except for leaf nodes, all other nodes contain child nodes and non-empty subtrees.</strong> As shown in the Figure 7-1 , if "Node 2" is considered as the parent node, then its left and right child nodes are "Node 4" and "Node 5," respectively. The left subtree is "the tree formed by Node 4 and all nodes under it," and the right subtree is "the tree formed by Node 5 and all nodes under it."</p>
<p><strong>In a binary tree, except for leaf nodes, all other nodes contain child nodes and non-empty subtrees.</strong> As shown in Figure 7-1, if "Node 2" is considered as the parent node, then its left and right child nodes are "Node 4" and "Node 5," respectively. The left subtree is "the tree formed by Node 4 and all nodes under it," and the right subtree is "the tree formed by Node 5 and all nodes under it."</p>
<p><a class="glightbox" href="../binary_tree.assets/binary_tree_definition.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Parent Node, child Node, subtree" class="animation-figure" src="../binary_tree.assets/binary_tree_definition.png" /></a></p>
<p align="center"> Figure 7-1 &nbsp; Parent Node, child Node, subtree </p>
<h2 id="711-common-terminology-of-binary-trees">7.1.1 &nbsp; Common terminology of binary trees<a class="headerlink" href="#711-common-terminology-of-binary-trees" title="Permanent link">&para;</a></h2>
<p>The commonly used terminology of binary trees is shown in the following figure.</p>
<p>The commonly used terminology of binary trees is shown in Figure 7-2.</p>
<ul>
<li>"Root node": The node at the top level of the binary tree, which has no parent node.</li>
<li>"Leaf node": A node with no children, both of its pointers point to <code>None</code>.</li>
@@ -4152,7 +4152,7 @@
<p>https://pythontutor.com/render.html#code=class%20TreeNode%3A%0A%20%20%20%20%22%22%22%E4%BA%8C%E5%8F%89%E6%A0%91%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.left%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%B7%A6%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%20%20%20%20%20%20%20%20self.right%3A%20TreeNode%20%7C%20None%20%3D%20None%20%23%20%E5%8F%B3%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E4%BA%8C%E5%8F%89%E6%A0%91%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E8%8A%82%E7%82%B9%0A%20%20%20%20n1%20%3D%20TreeNode%28val%3D1%29%0A%20%20%20%20n2%20%3D%20TreeNode%28val%3D2%29%0A%20%20%20%20n3%20%3D%20TreeNode%28val%3D3%29%0A%20%20%20%20n4%20%3D%20TreeNode%28val%3D4%29%0A%20%20%20%20n5%20%3D%20TreeNode%28val%3D5%29%0A%20%20%20%20%23%20%E6%9E%84%E5%BB%BA%E8%8A%82%E7%82%B9%E4%B9%8B%E9%97%B4%E7%9A%84%E5%BC%95%E7%94%A8%EF%BC%88%E6%8C%87%E9%92%88%EF%BC%89%0A%20%20%20%20n1.left%20%3D%20n2%0A%20%20%20%20n1.right%20%3D%20n3%0A%20%20%20%20n2.left%20%3D%20n4%0A%20%20%20%20n2.right%20%3D%20n5&amp;cumulative=false&amp;curInstr=3&amp;heapPrimitives=nevernest&amp;mode=display&amp;origin=opt-frontend.js&amp;py=311&amp;rawInputLstJSON=%5B%5D&amp;textReferences=false</p>
</details>
<h3 id="2-inserting-and-removing-nodes">2. &nbsp; Inserting and removing nodes<a class="headerlink" href="#2-inserting-and-removing-nodes" title="Permanent link">&para;</a></h3>
<p>Similar to a linked list, inserting and removing nodes in a binary tree can be achieved by modifying pointers. The Figure 7-3 provides an example.</p>
<p>Similar to a linked list, inserting and removing nodes in a binary tree can be achieved by modifying pointers. Figure 7-3 provides an example.</p>
<p><a class="glightbox" href="../binary_tree.assets/binary_tree_add_remove.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Inserting and removing nodes in a binary tree" class="animation-figure" src="../binary_tree.assets/binary_tree_add_remove.png" /></a></p>
<p align="center"> Figure 7-3 &nbsp; Inserting and removing nodes in a binary tree </p>
@@ -4294,7 +4294,7 @@
</div>
<h2 id="713-common-types-of-binary-trees">7.1.3 &nbsp; Common types of binary trees<a class="headerlink" href="#713-common-types-of-binary-trees" title="Permanent link">&para;</a></h2>
<h3 id="1-perfect-binary-tree">1. &nbsp; Perfect binary tree<a class="headerlink" href="#1-perfect-binary-tree" title="Permanent link">&para;</a></h3>
<p>As shown in the Figure 7-4 , in a "perfect binary tree," all levels of nodes are fully filled. In a perfect binary tree, the degree of leaf nodes is <span class="arithmatex">\(0\)</span>, and the degree of all other nodes is <span class="arithmatex">\(2\)</span>; if the tree's height is <span class="arithmatex">\(h\)</span>, then the total number of nodes is <span class="arithmatex">\(2^{h+1} - 1\)</span>, showing a standard exponential relationship, reflecting the common phenomenon of cell division in nature.</p>
<p>As shown in Figure 7-4, in a "perfect binary tree," all levels of nodes are fully filled. In a perfect binary tree, the degree of leaf nodes is <span class="arithmatex">\(0\)</span>, and the degree of all other nodes is <span class="arithmatex">\(2\)</span>; if the tree's height is <span class="arithmatex">\(h\)</span>, then the total number of nodes is <span class="arithmatex">\(2^{h+1} - 1\)</span>, showing a standard exponential relationship, reflecting the common phenomenon of cell division in nature.</p>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>Please note that in the Chinese community, a perfect binary tree is often referred to as a "full binary tree."</p>
@@ -4303,22 +4303,22 @@
<p align="center"> Figure 7-4 &nbsp; Perfect binary tree </p>
<h3 id="2-complete-binary-tree">2. &nbsp; Complete binary tree<a class="headerlink" href="#2-complete-binary-tree" title="Permanent link">&para;</a></h3>
<p>As shown in the Figure 7-5 , a "complete binary tree" has only the bottom level nodes not fully filled, and the bottom level nodes are filled as far left as possible.</p>
<p>As shown in Figure 7-5, a "complete binary tree" has only the bottom level nodes not fully filled, and the bottom level nodes are filled as far left as possible.</p>
<p><a class="glightbox" href="../binary_tree.assets/complete_binary_tree.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Complete binary tree" class="animation-figure" src="../binary_tree.assets/complete_binary_tree.png" /></a></p>
<p align="center"> Figure 7-5 &nbsp; Complete binary tree </p>
<h3 id="3-full-binary-tree">3. &nbsp; Full binary tree<a class="headerlink" href="#3-full-binary-tree" title="Permanent link">&para;</a></h3>
<p>As shown in the Figure 7-6 , a "full binary tree" has all nodes except leaf nodes having two children.</p>
<p>As shown in Figure 7-6, a "full binary tree" has all nodes except leaf nodes having two children.</p>
<p><a class="glightbox" href="../binary_tree.assets/full_binary_tree.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Full binary tree" class="animation-figure" src="../binary_tree.assets/full_binary_tree.png" /></a></p>
<p align="center"> Figure 7-6 &nbsp; Full binary tree </p>
<h3 id="4-balanced-binary-tree">4. &nbsp; Balanced binary tree<a class="headerlink" href="#4-balanced-binary-tree" title="Permanent link">&para;</a></h3>
<p>As shown in the Figure 7-7 , in a "balanced binary tree," the absolute difference in height between the left and right subtrees of any node does not exceed 1.</p>
<p>As shown in Figure 7-7, in a "balanced binary tree," the absolute difference in height between the left and right subtrees of any node does not exceed 1.</p>
<p><a class="glightbox" href="../binary_tree.assets/balanced_binary_tree.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Balanced binary tree" class="animation-figure" src="../binary_tree.assets/balanced_binary_tree.png" /></a></p>
<p align="center"> Figure 7-7 &nbsp; Balanced binary tree </p>
<h2 id="714-degeneration-of-binary-trees">7.1.4 &nbsp; Degeneration of binary trees<a class="headerlink" href="#714-degeneration-of-binary-trees" title="Permanent link">&para;</a></h2>
<p>The Figure 7-8 shows the ideal and degenerate structures of binary trees. When every level of a binary tree is filled, it reaches the "perfect binary tree"; when all nodes are biased towards one side, the binary tree degenerates into a "linked list".</p>
<p>Figure 7-8 shows the ideal and degenerate structures of binary trees. When every level of a binary tree is filled, it reaches the "perfect binary tree"; when all nodes are biased towards one side, the binary tree degenerates into a "linked list".</p>
<ul>
<li>The perfect binary tree is the ideal situation, fully leveraging the "divide and conquer" advantage of binary trees.</li>
<li>A linked list is another extreme, where operations become linear, degrading the time complexity to <span class="arithmatex">\(O(n)\)</span>.</li>
@@ -4326,7 +4326,7 @@
<p><a class="glightbox" href="../binary_tree.assets/binary_tree_best_worst_cases.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="The Best and Worst Structures of Binary Trees" class="animation-figure" src="../binary_tree.assets/binary_tree_best_worst_cases.png" /></a></p>
<p align="center"> Figure 7-8 &nbsp; The Best and Worst Structures of Binary Trees </p>
<p>As shown in the Table 7-1 , in the best and worst structures, the number of leaf nodes, total number of nodes, and height of the binary tree reach their maximum or minimum values.</p>
<p>As shown in Table 7-1, in the best and worst structures, the number of leaf nodes, total number of nodes, and height of the binary tree reach their maximum or minimum values.</p>
<p align="center"> Table 7-1 &nbsp; The Best and Worst Structures of Binary Trees </p>
<div class="center-table">
@@ -3690,7 +3690,7 @@
<p>From the perspective of physical structure, a tree is a data structure based on linked lists, hence its traversal method involves accessing nodes one by one through pointers. However, a tree is a non-linear data structure, which makes traversing a tree more complex than traversing a linked list, requiring the assistance of search algorithms to achieve.</p>
<p>Common traversal methods for binary trees include level-order traversal, preorder traversal, inorder traversal, and postorder traversal, among others.</p>
<h2 id="721-level-order-traversal">7.2.1 &nbsp; Level-order traversal<a class="headerlink" href="#721-level-order-traversal" title="Permanent link">&para;</a></h2>
<p>As shown in the Figure 7-9 , "level-order traversal" traverses the binary tree from top to bottom, layer by layer, and accesses nodes in each layer in a left-to-right order.</p>
<p>As shown in Figure 7-9, "level-order traversal" traverses the binary tree from top to bottom, layer by layer, and accesses nodes in each layer in a left-to-right order.</p>
<p>Level-order traversal essentially belongs to "breadth-first traversal", also known as "breadth-first search (BFS)", which embodies a "circumferentially outward expanding" layer-by-layer traversal method.</p>
<p><a class="glightbox" href="../binary_tree_traversal.assets/binary_tree_bfs.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Level-order traversal of a binary tree" class="animation-figure" src="../binary_tree_traversal.assets/binary_tree_bfs.png" /></a></p>
<p align="center"> Figure 7-9 &nbsp; Level-order traversal of a binary tree </p>
@@ -4029,7 +4029,7 @@
</ul>
<h2 id="722-preorder-inorder-and-postorder-traversal">7.2.2 &nbsp; Preorder, inorder, and postorder traversal<a class="headerlink" href="#722-preorder-inorder-and-postorder-traversal" title="Permanent link">&para;</a></h2>
<p>Correspondingly, preorder, inorder, and postorder traversal all belong to "depth-first traversal", also known as "depth-first search (DFS)", which embodies a "proceed to the end first, then backtrack and continue" traversal method.</p>
<p>The Figure 7-10 shows the working principle of performing a depth-first traversal on a binary tree. <strong>Depth-first traversal is like walking around the perimeter of the entire binary tree</strong>, encountering three positions at each node, corresponding to preorder traversal, inorder traversal, and postorder traversal.</p>
<p>Figure 7-10 shows the working principle of performing a depth-first traversal on a binary tree. <strong>Depth-first traversal is like walking around the perimeter of the entire binary tree</strong>, encountering three positions at each node, corresponding to preorder traversal, inorder traversal, and postorder traversal.</p>
<p><a class="glightbox" href="../binary_tree_traversal.assets/binary_tree_dfs.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Preorder, inorder, and postorder traversal of a binary search tree" class="animation-figure" src="../binary_tree_traversal.assets/binary_tree_dfs.png" /></a></p>
<p align="center"> Figure 7-10 &nbsp; Preorder, inorder, and postorder traversal of a binary search tree </p>
@@ -4496,7 +4496,7 @@
<p class="admonition-title">Tip</p>
<p>Depth-first search can also be implemented based on iteration, interested readers can study this on their own.</p>
</div>
<p>The Figure 7-11 shows the recursive process of preorder traversal of a binary tree, which can be divided into two opposite parts: "recursion" and "return".</p>
<p>Figure 7-11 shows the recursive process of preorder traversal of a binary tree, which can be divided into two opposite parts: "recursion" and "return".</p>
<ol>
<li>"Recursion" means starting a new method, the program accesses the next node in this process.</li>
<li>"Return" means the function returns, indicating the current node has been fully accessed.</li>