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

View File

@@ -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>