This commit is contained in:
krahets
2024-09-28 09:26:59 +08:00
parent 55b91eb967
commit 8ffc9616e9
34 changed files with 548 additions and 591 deletions
@@ -3687,11 +3687,11 @@
<!-- Page content -->
<h1 id="72-binary-tree-traversal">7.2 &nbsp; Binary tree traversal<a class="headerlink" href="#72-binary-tree-traversal" title="Permanent link">&para;</a></h1>
<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, pre-order traversal, in-order traversal, and post-order traversal, among others.</p>
<p>From a physical structure perspective, 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.</p>
<p>The common traversal methods for binary trees include level-order traversal, pre-order traversal, in-order traversal, and post-order traversal.</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 Figure 7-9, <u>level-order traversal</u> 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 <u>breadth-first traversal</u>, also known as <u>breadth-first search (BFS)</u>, which embodies a "circumferentially outward expanding" layer-by-layer traversal method.</p>
<p>As shown in Figure 7-9, <u>level-order traversal</u> traverses the binary tree from top to bottom, layer by layer. Within each level, it visits nodes from left to right.</p>
<p>Level-order traversal is essentially a type of <u>breadth-first traversal</u>, also known as <u>breadth-first search (BFS)</u>, 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>
@@ -3806,12 +3806,12 @@
</div>
<h3 id="2-complexity-analysis">2. &nbsp; Complexity analysis<a class="headerlink" href="#2-complexity-analysis" title="Permanent link">&para;</a></h3>
<ul>
<li><strong>Time complexity is <span class="arithmatex">\(O(n)\)</span></strong>: All nodes are visited once, using <span class="arithmatex">\(O(n)\)</span> time, where <span class="arithmatex">\(n\)</span> is the number of nodes.</li>
<li><strong>Space complexity is <span class="arithmatex">\(O(n)\)</span></strong>: In the worst case, i.e., a full binary tree, before traversing to the lowest level, the queue can contain at most <span class="arithmatex">\((n + 1) / 2\)</span> nodes at the same time, occupying <span class="arithmatex">\(O(n)\)</span> space.</li>
<li><strong>Time complexity is <span class="arithmatex">\(O(n)\)</span></strong>: All nodes are visited once, taking <span class="arithmatex">\(O(n)\)</span> time, where <span class="arithmatex">\(n\)</span> is the number of nodes.</li>
<li><strong>Space complexity is <span class="arithmatex">\(O(n)\)</span></strong>: In the worst case, i.e., a full binary tree, before traversing to the bottom level, the queue can contain at most <span class="arithmatex">\((n + 1) / 2\)</span> nodes simultaneously, occupying <span class="arithmatex">\(O(n)\)</span> space.</li>
</ul>
<h2 id="722-preorder-in-order-and-post-order-traversal">7.2.2 &nbsp; Preorder, in-order, and post-order traversal<a class="headerlink" href="#722-preorder-in-order-and-post-order-traversal" title="Permanent link">&para;</a></h2>
<p>Correspondingly, pre-order, in-order, and post-order traversal all belong to <u>depth-first traversal</u>, also known as <u>depth-first search (DFS)</u>, which embodies a "proceed to the end first, then backtrack and continue" traversal method.</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 pre-order traversal, in-order traversal, and post-order 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 entire binary tree</strong>, encountering three positions at each node, corresponding to pre-order, in-order, and post-order 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, in-order, and post-order 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, in-order, and post-order traversal of a binary search tree </p>
@@ -4053,7 +4053,7 @@
<h3 id="2-complexity-analysis_1">2. &nbsp; Complexity analysis<a class="headerlink" href="#2-complexity-analysis_1" title="Permanent link">&para;</a></h3>
<ul>
<li><strong>Time complexity is <span class="arithmatex">\(O(n)\)</span></strong>: All nodes are visited once, using <span class="arithmatex">\(O(n)\)</span> time.</li>
<li><strong>Space complexity is <span class="arithmatex">\(O(n)\)</span></strong>: In the worst case, i.e., the tree degrades into a linked list, the recursion depth reaches <span class="arithmatex">\(n\)</span>, the system occupies <span class="arithmatex">\(O(n)\)</span> stack frame space.</li>
<li><strong>Space complexity is <span class="arithmatex">\(O(n)\)</span></strong>: In the worst case, i.e., the tree degenerates into a linked list, the recursion depth reaches <span class="arithmatex">\(n\)</span>, the system occupies <span class="arithmatex">\(O(n)\)</span> stack frame space.</li>
</ul>
<!-- Source file information -->