This commit is contained in:
krahets
2026-04-02 03:08:50 +08:00
parent 09a136c9fa
commit aaf9f58eb3
157 changed files with 3002 additions and 2994 deletions
@@ -6,7 +6,7 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="description" content="Data Structures and Algorithms Crash Course with Animated Illustrations and Off-the-Shelf Code">
<meta name="description" content="Data structures and algorithms tutorial with animated illustrations and ready-to-run code">
<meta name="author" content="krahets">
@@ -576,7 +576,7 @@
<span class="md-ellipsis">
Chapter 1. Encounter With Algorithms
Chapter 1. Encounter with Algorithms
@@ -598,7 +598,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 1. Encounter With Algorithms
Chapter 1. Encounter with Algorithms
</label>
@@ -1183,7 +1183,7 @@
<span class="md-ellipsis">
Chapter 4. Array and Linked List
Chapter 4. Arrays and Linked Lists
@@ -1205,7 +1205,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 4. Array and Linked List
Chapter 4. Arrays and Linked Lists
</label>
@@ -1311,7 +1311,7 @@
<span class="md-ellipsis">
4.4 Memory and Cache *
4.4 Random-Access Memory and Cache *
@@ -1402,7 +1402,7 @@
<span class="md-ellipsis">
Chapter 5. Stack and Queue
Chapter 5. Stacks and Queues
@@ -1424,7 +1424,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 5. Stack and Queue
Chapter 5. Stacks and Queues
</label>
@@ -1502,7 +1502,7 @@
<span class="md-ellipsis">
5.3 Double-Ended Queue
5.3 Deque
@@ -1593,7 +1593,7 @@
<span class="md-ellipsis">
Chapter 6. Hashing
Chapter 6. Hash Table
@@ -1615,7 +1615,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 6. Hashing
Chapter 6. Hash Table
</label>
@@ -1899,7 +1899,7 @@
<span class="md-ellipsis">
7.3 Array Representation of Tree
7.3 Array Representation of Binary Trees
@@ -1917,7 +1917,7 @@
<span class="md-ellipsis">
7.3 Array Representation of Tree
7.3 Array Representation of Binary Trees
@@ -2190,7 +2190,7 @@
<span class="md-ellipsis">
8.2 Building a Heap
8.2 Heap Construction Operation
@@ -2218,7 +2218,7 @@
<span class="md-ellipsis">
8.3 Top-K Problem
8.3 Top-k Problem
@@ -2576,7 +2576,7 @@
<span class="md-ellipsis">
10.2 Binary Search Insertion
10.2 Binary Search Insertion Point
@@ -2604,7 +2604,7 @@
<span class="md-ellipsis">
10.3 Binary Search Edge Cases
10.3 Binary Search Boundaries
@@ -2660,7 +2660,7 @@
<span class="md-ellipsis">
10.5 Search Algorithms Revisited
10.5 Searching Algorithms Revisited
@@ -2809,7 +2809,7 @@
<span class="md-ellipsis">
11.1 Sorting Algorithms
11.1 Sorting Algorithm
@@ -3282,7 +3282,7 @@
<span class="md-ellipsis">
12.4 Hanoi Tower Problem
12.4 Hanota Problem
@@ -4194,7 +4194,7 @@
<span class="md-ellipsis">
16.3 Terminology Table
16.3 Glossary
@@ -4379,7 +4379,7 @@
<!-- Page content -->
<h1 id="73-array-representation-of-binary-trees">7.3 &nbsp; Array Representation of Binary Trees<a class="headerlink" href="#73-array-representation-of-binary-trees" title="Permanent link">&para;</a></h1>
<p>Under the linked list representation, the storage unit of a binary tree is a node <code>TreeNode</code>, and nodes are connected by pointers. The previous section introduced the basic operations of binary trees under the linked list representation.</p>
<p>In the linked-list representation, the storage unit of a binary tree is a node <code>TreeNode</code>, and nodes are connected by pointers. The previous section introduced the basic operations of binary trees in this representation.</p>
<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>
@@ -4394,7 +4394,7 @@
<p><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" /></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 Figure 7-14, 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 explicitly write out all <code>None</code> values in the level-order traversal sequence</strong>. As shown in Figure 7-14, once we do this, 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: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">
<div class="tabbed-block">
@@ -4477,17 +4477,17 @@
</div>
</div>
</div>
<p><img alt="Array representation of any type of binary tree" class="animation-figure" src="../array_representation_of_tree.assets/array_representation_with_empty.png" /></p>
<p align="center"> Figure 7-14 &nbsp; Array representation of any type of binary tree </p>
<p><img alt="Array representation of an arbitrary binary tree" class="animation-figure" src="../array_representation_of_tree.assets/array_representation_with_empty.png" /></p>
<p align="center"> Figure 7-14 &nbsp; Array representation of an arbitrary binary tree </p>
<p>It's worth noting that <strong>complete binary trees are very well-suited for array representation</strong>. Recalling the definition of a complete binary tree, <code>None</code> only appears at the bottom level and towards the right, <strong>meaning all <code>None</code> values must 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. Figure 7-15 gives an example.</p>
<p><img alt="Array representation of a complete binary tree" class="animation-figure" src="../array_representation_of_tree.assets/array_representation_complete_binary_tree.png" /></p>
<p align="center"> Figure 7-15 &nbsp; Array representation of a complete binary tree </p>
<p>The following code implements a binary tree based on array representation, including the following operations:</p>
<p>The following code implements a binary tree using an array representation, including the following operations:</p>
<ul>
<li>Given a certain node, obtain its value, left (right) child node, and parent node.</li>
<li>Given a node, obtain its value, left (right) child node, and parent node.</li>
<li>Obtain the preorder, inorder, postorder, and level-order traversal sequences.</li>
</ul>
<div class="tabbed-set tabbed-alternate" data-tabs="2:13"><input checked="checked" id="__tabbed_2_1" name="__tabbed_2" type="radio" /><input id="__tabbed_2_2" name="__tabbed_2" type="radio" /><input id="__tabbed_2_3" name="__tabbed_2" type="radio" /><input id="__tabbed_2_4" name="__tabbed_2" type="radio" /><input id="__tabbed_2_5" name="__tabbed_2" type="radio" /><input id="__tabbed_2_6" name="__tabbed_2" type="radio" /><input id="__tabbed_2_7" name="__tabbed_2" type="radio" /><input id="__tabbed_2_8" name="__tabbed_2" type="radio" /><input id="__tabbed_2_9" name="__tabbed_2" type="radio" /><input id="__tabbed_2_10" name="__tabbed_2" type="radio" /><input id="__tabbed_2_11" name="__tabbed_2" type="radio" /><input id="__tabbed_2_12" name="__tabbed_2" type="radio" /><input id="__tabbed_2_13" name="__tabbed_2" type="radio" /><div class="tabbed-labels"><label for="__tabbed_2_1">Python</label><label for="__tabbed_2_2">C++</label><label for="__tabbed_2_3">Java</label><label for="__tabbed_2_4">C#</label><label for="__tabbed_2_5">Go</label><label for="__tabbed_2_6">Swift</label><label for="__tabbed_2_7">JS</label><label for="__tabbed_2_8">TS</label><label for="__tabbed_2_9">Dart</label><label for="__tabbed_2_10">Rust</label><label for="__tabbed_2_11">C</label><label for="__tabbed_2_12">Kotlin</label><label for="__tabbed_2_13">Ruby</label></div>
+44 -44
View File
@@ -6,7 +6,7 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="description" content="Data Structures and Algorithms Crash Course with Animated Illustrations and Off-the-Shelf Code">
<meta name="description" content="Data structures and algorithms tutorial with animated illustrations and ready-to-run code">
<meta name="author" content="krahets">
@@ -39,7 +39,7 @@
<title>7.5 Avl Tree * - Hello Algo</title>
<title>7.5 AVL Tree * - Hello Algo</title>
@@ -154,7 +154,7 @@
<div class="md-header__topic" data-md-component="header-topic">
<span class="md-ellipsis">
7.5 &nbsp; Avl Tree *
7.5 &nbsp; AVL Tree *
</span>
</div>
@@ -576,7 +576,7 @@
<span class="md-ellipsis">
Chapter 1. Encounter With Algorithms
Chapter 1. Encounter with Algorithms
@@ -598,7 +598,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 1. Encounter With Algorithms
Chapter 1. Encounter with Algorithms
</label>
@@ -1183,7 +1183,7 @@
<span class="md-ellipsis">
Chapter 4. Array and Linked List
Chapter 4. Arrays and Linked Lists
@@ -1205,7 +1205,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 4. Array and Linked List
Chapter 4. Arrays and Linked Lists
</label>
@@ -1311,7 +1311,7 @@
<span class="md-ellipsis">
4.4 Memory and Cache *
4.4 Random-Access Memory and Cache *
@@ -1402,7 +1402,7 @@
<span class="md-ellipsis">
Chapter 5. Stack and Queue
Chapter 5. Stacks and Queues
@@ -1424,7 +1424,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 5. Stack and Queue
Chapter 5. Stacks and Queues
</label>
@@ -1502,7 +1502,7 @@
<span class="md-ellipsis">
5.3 Double-Ended Queue
5.3 Deque
@@ -1593,7 +1593,7 @@
<span class="md-ellipsis">
Chapter 6. Hashing
Chapter 6. Hash Table
@@ -1615,7 +1615,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 6. Hashing
Chapter 6. Hash Table
</label>
@@ -1890,7 +1890,7 @@
<span class="md-ellipsis">
7.3 Array Representation of Tree
7.3 Array Representation of Binary Trees
@@ -2003,12 +2003,12 @@
<a href="#751-common-terminology-in-avl-trees" class="md-nav__link">
<span class="md-ellipsis">
7.5.1 &nbsp; Common Terminology in Avl Trees
7.5.1 &nbsp; Common Terminology in AVL Trees
</span>
</a>
<nav class="md-nav" aria-label="7.5.1 Common Terminology in Avl Trees">
<nav class="md-nav" aria-label="7.5.1 Common Terminology in AVL Trees">
<ul class="md-nav__list">
<li class="md-nav__item">
@@ -2042,12 +2042,12 @@
<a href="#752-rotations-in-avl-trees" class="md-nav__link">
<span class="md-ellipsis">
7.5.2 &nbsp; Rotations in Avl Trees
7.5.2 &nbsp; Rotations in AVL Trees
</span>
</a>
<nav class="md-nav" aria-label="7.5.2 Rotations in Avl Trees">
<nav class="md-nav" aria-label="7.5.2 Rotations in AVL Trees">
<ul class="md-nav__list">
<li class="md-nav__item">
@@ -2114,12 +2114,12 @@
<a href="#753-common-operations-in-avl-trees" class="md-nav__link">
<span class="md-ellipsis">
7.5.3 &nbsp; Common Operations in Avl Trees
7.5.3 &nbsp; Common Operations in AVL Trees
</span>
</a>
<nav class="md-nav" aria-label="7.5.3 Common Operations in Avl Trees">
<nav class="md-nav" aria-label="7.5.3 Common Operations in AVL Trees">
<ul class="md-nav__list">
<li class="md-nav__item">
@@ -2164,7 +2164,7 @@
<a href="#754-typical-applications-of-avl-trees" class="md-nav__link">
<span class="md-ellipsis">
7.5.4 &nbsp; Typical Applications of Avl Trees
7.5.4 &nbsp; Typical Applications of AVL Trees
</span>
</a>
@@ -2329,7 +2329,7 @@
<span class="md-ellipsis">
8.2 Building a Heap
8.2 Heap Construction Operation
@@ -2357,7 +2357,7 @@
<span class="md-ellipsis">
8.3 Top-K Problem
8.3 Top-k Problem
@@ -2715,7 +2715,7 @@
<span class="md-ellipsis">
10.2 Binary Search Insertion
10.2 Binary Search Insertion Point
@@ -2743,7 +2743,7 @@
<span class="md-ellipsis">
10.3 Binary Search Edge Cases
10.3 Binary Search Boundaries
@@ -2799,7 +2799,7 @@
<span class="md-ellipsis">
10.5 Search Algorithms Revisited
10.5 Searching Algorithms Revisited
@@ -2948,7 +2948,7 @@
<span class="md-ellipsis">
11.1 Sorting Algorithms
11.1 Sorting Algorithm
@@ -3421,7 +3421,7 @@
<span class="md-ellipsis">
12.4 Hanoi Tower Problem
12.4 Hanota Problem
@@ -4333,7 +4333,7 @@
<span class="md-ellipsis">
16.3 Terminology Table
16.3 Glossary
@@ -4451,12 +4451,12 @@
<a href="#751-common-terminology-in-avl-trees" class="md-nav__link">
<span class="md-ellipsis">
7.5.1 &nbsp; Common Terminology in Avl Trees
7.5.1 &nbsp; Common Terminology in AVL Trees
</span>
</a>
<nav class="md-nav" aria-label="7.5.1 Common Terminology in Avl Trees">
<nav class="md-nav" aria-label="7.5.1 Common Terminology in AVL Trees">
<ul class="md-nav__list">
<li class="md-nav__item">
@@ -4490,12 +4490,12 @@
<a href="#752-rotations-in-avl-trees" class="md-nav__link">
<span class="md-ellipsis">
7.5.2 &nbsp; Rotations in Avl Trees
7.5.2 &nbsp; Rotations in AVL Trees
</span>
</a>
<nav class="md-nav" aria-label="7.5.2 Rotations in Avl Trees">
<nav class="md-nav" aria-label="7.5.2 Rotations in AVL Trees">
<ul class="md-nav__list">
<li class="md-nav__item">
@@ -4562,12 +4562,12 @@
<a href="#753-common-operations-in-avl-trees" class="md-nav__link">
<span class="md-ellipsis">
7.5.3 &nbsp; Common Operations in Avl Trees
7.5.3 &nbsp; Common Operations in AVL Trees
</span>
</a>
<nav class="md-nav" aria-label="7.5.3 Common Operations in Avl Trees">
<nav class="md-nav" aria-label="7.5.3 Common Operations in AVL Trees">
<ul class="md-nav__list">
<li class="md-nav__item">
@@ -4612,7 +4612,7 @@
<a href="#754-typical-applications-of-avl-trees" class="md-nav__link">
<span class="md-ellipsis">
7.5.4 &nbsp; Typical Applications of Avl Trees
7.5.4 &nbsp; Typical Applications of AVL Trees
</span>
</a>
@@ -4656,7 +4656,7 @@
<!-- 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>
<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 insertion and removal operations, a binary search tree may degenerate into a linked list. In this case, 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 Figure 7-24, after two node removal operations, this binary search tree will degrade into a linked list.</p>
<p><img alt="Degradation of an AVL tree after removing nodes" class="animation-figure" src="../avl_tree.assets/avltree_degradation_from_removing_node.png" /></p>
@@ -4666,8 +4666,8 @@
<p><img alt="Degradation of an AVL tree after inserting nodes" class="animation-figure" src="../avl_tree.assets/avltree_degradation_from_inserting_node.png" /></p>
<p align="center"> Figure 7-25 &nbsp; Degradation of an AVL tree after inserting nodes </p>
<p>In 1962, G. M. Adelson-Velsky and E. M. Landis proposed the <u>AVL tree</u> in their paper "An algorithm for the organization of information". The paper described in detail a series of operations ensuring that after continuously adding and removing nodes, the AVL tree does not degenerate, thus keeping the time complexity of various operations at the <span class="arithmatex">\(O(\log n)\)</span> level. In other words, in scenarios requiring frequent insertions, deletions, searches, and modifications, the AVL tree can always maintain efficient data operation performance, making it very valuable in applications.</p>
<h2 id="751-common-terminology-in-avl-trees">7.5.1 &nbsp; Common Terminology in Avl Trees<a class="headerlink" href="#751-common-terminology-in-avl-trees" title="Permanent link">&para;</a></h2>
<p>In 1962, G. M. Adelson-Velsky and E. M. Landis proposed the <u>AVL tree</u> in their paper "An algorithm for the organization of information". The paper describes a series of operations that prevent an AVL tree from degenerating as nodes are inserted and removed, thereby keeping the time complexity of various operations at <span class="arithmatex">\(O(\log n)\)</span>. In other words, in scenarios that require frequent insertion, deletion, lookup, and update operations, AVL trees can maintain consistently efficient performance and therefore have strong practical value.</p>
<h2 id="751-common-terminology-in-avl-trees">7.5.1 &nbsp; Common Terminology in AVL Trees<a class="headerlink" href="#751-common-terminology-in-avl-trees" title="Permanent link">&para;</a></h2>
<p>An AVL tree is both a binary search tree and a balanced binary tree, simultaneously satisfying all the properties of these two types of binary trees, hence it is a <u>balanced binary search tree</u>.</p>
<h3 id="1-node-height">1. &nbsp; Node Height<a class="headerlink" href="#1-node-height" title="Permanent link">&para;</a></h3>
<p>Since the operations related to AVL trees require obtaining node heights, we need to add a <code>height</code> variable to the node class:</p>
@@ -4857,7 +4857,7 @@
</div>
</div>
</div>
<p>The "node height" refers to the distance from that node to its farthest leaf node, i.e., the number of "edges" passed. It is important to note that the height of a leaf node is <span class="arithmatex">\(0\)</span>, and the height of a null node is <span class="arithmatex">\(-1\)</span>. We will create two utility functions for getting and updating the height of a node:</p>
<p>The "node height" refers to the distance from that node to its farthest leaf node, i.e., the number of edges on the path. It is important to note that the height of a leaf node is <span class="arithmatex">\(0\)</span>, and the height of a null node is <span class="arithmatex">\(-1\)</span>. We will create two utility functions for getting and updating the height of a node:</p>
<div class="tabbed-set tabbed-alternate" data-tabs="2:13"><input checked="checked" id="__tabbed_2_1" name="__tabbed_2" type="radio" /><input id="__tabbed_2_2" name="__tabbed_2" type="radio" /><input id="__tabbed_2_3" name="__tabbed_2" type="radio" /><input id="__tabbed_2_4" name="__tabbed_2" type="radio" /><input id="__tabbed_2_5" name="__tabbed_2" type="radio" /><input id="__tabbed_2_6" name="__tabbed_2" type="radio" /><input id="__tabbed_2_7" name="__tabbed_2" type="radio" /><input id="__tabbed_2_8" name="__tabbed_2" type="radio" /><input id="__tabbed_2_9" name="__tabbed_2" type="radio" /><input id="__tabbed_2_10" name="__tabbed_2" type="radio" /><input id="__tabbed_2_11" name="__tabbed_2" type="radio" /><input id="__tabbed_2_12" name="__tabbed_2" type="radio" /><input id="__tabbed_2_13" name="__tabbed_2" type="radio" /><div class="tabbed-labels"><label for="__tabbed_2_1">Python</label><label for="__tabbed_2_2">C++</label><label for="__tabbed_2_3">Java</label><label for="__tabbed_2_4">C#</label><label for="__tabbed_2_5">Go</label><label for="__tabbed_2_6">Swift</label><label for="__tabbed_2_7">JS</label><label for="__tabbed_2_8">TS</label><label for="__tabbed_2_9">Dart</label><label for="__tabbed_2_10">Rust</label><label for="__tabbed_2_11">C</label><label for="__tabbed_2_12">Kotlin</label><label for="__tabbed_2_13">Ruby</label></div>
<div class="tabbed-content">
<div class="tabbed-block">
@@ -5224,9 +5224,9 @@
<p class="admonition-title">Tip</p>
<p>Let the balance factor be <span class="arithmatex">\(f\)</span>, then the balance factor of any node in an AVL tree satisfies <span class="arithmatex">\(-1 \le f \le 1\)</span>.</p>
</div>
<h2 id="752-rotations-in-avl-trees">7.5.2 &nbsp; Rotations in Avl Trees<a class="headerlink" href="#752-rotations-in-avl-trees" title="Permanent link">&para;</a></h2>
<h2 id="752-rotations-in-avl-trees">7.5.2 &nbsp; Rotations in AVL Trees<a class="headerlink" href="#752-rotations-in-avl-trees" title="Permanent link">&para;</a></h2>
<p>The characteristic of AVL trees lies in the "rotation" operation, which can restore balance to unbalanced nodes without affecting the inorder traversal sequence of the binary tree. In other words, <strong>rotation operations can both maintain the property of a "binary search tree" and make the tree return to a "balanced binary tree"</strong>.</p>
<p>We call nodes with a balance factor absolute value <span class="arithmatex">\(&gt; 1\)</span> "unbalanced nodes". Depending on the imbalance situation, rotation operations are divided into four types: right rotation, left rotation, left rotation then right rotation, and right rotation then left rotation. Below we describe these rotation operations in detail.</p>
<p>We call nodes with a balance factor absolute value <span class="arithmatex">\(&gt; 1\)</span> "unbalanced nodes". Depending on the imbalance situation, rotation operations are divided into four types: right rotation, left rotation, right rotation then left rotation, and left rotation then right rotation. Below we describe these rotation operations in detail.</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 Figure 7-26, the value below the node is the balance factor. From bottom to top, the first unbalanced node in the binary tree is "node 3". We focus on the subtree with this unbalanced node as the root, denoting the node as <code>node</code> and its left child as <code>child</code>, and perform a "right rotation" operation. After the right rotation is completed, the subtree regains balance and still maintains 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>
@@ -6168,7 +6168,7 @@
</div>
</div>
</div>
<h2 id="753-common-operations-in-avl-trees">7.5.3 &nbsp; Common Operations in Avl Trees<a class="headerlink" href="#753-common-operations-in-avl-trees" title="Permanent link">&para;</a></h2>
<h2 id="753-common-operations-in-avl-trees">7.5.3 &nbsp; Common Operations in AVL Trees<a class="headerlink" href="#753-common-operations-in-avl-trees" title="Permanent link">&para;</a></h2>
<h3 id="1-node-insertion">1. &nbsp; Node Insertion<a class="headerlink" href="#1-node-insertion" title="Permanent link">&para;</a></h3>
<p>The node insertion operation in AVL trees is similar in principle to that in binary search trees. The only difference is that after inserting a node in an AVL tree, a series of unbalanced nodes may appear on the path from that node to the root. Therefore, <strong>we need to start from this node and perform rotation operations from bottom to top, restoring balance to all unbalanced nodes</strong>. The code is as follows:</p>
<div class="tabbed-set tabbed-alternate" data-tabs="8:13"><input checked="checked" id="__tabbed_8_1" name="__tabbed_8" type="radio" /><input id="__tabbed_8_2" name="__tabbed_8" type="radio" /><input id="__tabbed_8_3" name="__tabbed_8" type="radio" /><input id="__tabbed_8_4" name="__tabbed_8" type="radio" /><input id="__tabbed_8_5" name="__tabbed_8" type="radio" /><input id="__tabbed_8_6" name="__tabbed_8" type="radio" /><input id="__tabbed_8_7" name="__tabbed_8" type="radio" /><input id="__tabbed_8_8" name="__tabbed_8" type="radio" /><input id="__tabbed_8_9" name="__tabbed_8" type="radio" /><input id="__tabbed_8_10" name="__tabbed_8" type="radio" /><input id="__tabbed_8_11" name="__tabbed_8" type="radio" /><input id="__tabbed_8_12" name="__tabbed_8" type="radio" /><input id="__tabbed_8_13" name="__tabbed_8" type="radio" /><div class="tabbed-labels"><label for="__tabbed_8_1">Python</label><label for="__tabbed_8_2">C++</label><label for="__tabbed_8_3">Java</label><label for="__tabbed_8_4">C#</label><label for="__tabbed_8_5">Go</label><label for="__tabbed_8_6">Swift</label><label for="__tabbed_8_7">JS</label><label for="__tabbed_8_8">TS</label><label for="__tabbed_8_9">Dart</label><label for="__tabbed_8_10">Rust</label><label for="__tabbed_8_11">C</label><label for="__tabbed_8_12">Kotlin</label><label for="__tabbed_8_13">Ruby</label></div>
@@ -7100,7 +7100,7 @@
</div>
<h3 id="3-node-search">3. &nbsp; Node Search<a class="headerlink" href="#3-node-search" title="Permanent link">&para;</a></h3>
<p>The node search operation in AVL trees is consistent with that in binary search trees, and will not be elaborated here.</p>
<h2 id="754-typical-applications-of-avl-trees">7.5.4 &nbsp; Typical Applications of Avl Trees<a class="headerlink" href="#754-typical-applications-of-avl-trees" title="Permanent link">&para;</a></h2>
<h2 id="754-typical-applications-of-avl-trees">7.5.4 &nbsp; Typical Applications of AVL Trees<a class="headerlink" href="#754-typical-applications-of-avl-trees" title="Permanent link">&para;</a></h2>
<ul>
<li>Organizing and storing large-scale data, suitable for scenarios with high-frequency searches and low-frequency insertions and deletions.</li>
<li>Used to build index systems in databases.</li>
+29 -29
View File
@@ -6,7 +6,7 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="description" content="Data Structures and Algorithms Crash Course with Animated Illustrations and Off-the-Shelf Code">
<meta name="description" content="Data structures and algorithms tutorial with animated illustrations and ready-to-run code">
<meta name="author" content="krahets">
@@ -576,7 +576,7 @@
<span class="md-ellipsis">
Chapter 1. Encounter With Algorithms
Chapter 1. Encounter with Algorithms
@@ -598,7 +598,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 1. Encounter With Algorithms
Chapter 1. Encounter with Algorithms
</label>
@@ -1183,7 +1183,7 @@
<span class="md-ellipsis">
Chapter 4. Array and Linked List
Chapter 4. Arrays and Linked Lists
@@ -1205,7 +1205,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 4. Array and Linked List
Chapter 4. Arrays and Linked Lists
</label>
@@ -1311,7 +1311,7 @@
<span class="md-ellipsis">
4.4 Memory and Cache *
4.4 Random-Access Memory and Cache *
@@ -1402,7 +1402,7 @@
<span class="md-ellipsis">
Chapter 5. Stack and Queue
Chapter 5. Stacks and Queues
@@ -1424,7 +1424,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 5. Stack and Queue
Chapter 5. Stacks and Queues
</label>
@@ -1502,7 +1502,7 @@
<span class="md-ellipsis">
5.3 Double-Ended Queue
5.3 Deque
@@ -1593,7 +1593,7 @@
<span class="md-ellipsis">
Chapter 6. Hashing
Chapter 6. Hash Table
@@ -1615,7 +1615,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 6. Hashing
Chapter 6. Hash Table
</label>
@@ -1890,7 +1890,7 @@
<span class="md-ellipsis">
7.3 Array Representation of Tree
7.3 Array Representation of Binary Trees
@@ -2240,7 +2240,7 @@
<span class="md-ellipsis">
8.2 Building a Heap
8.2 Heap Construction Operation
@@ -2268,7 +2268,7 @@
<span class="md-ellipsis">
8.3 Top-K Problem
8.3 Top-k Problem
@@ -2626,7 +2626,7 @@
<span class="md-ellipsis">
10.2 Binary Search Insertion
10.2 Binary Search Insertion Point
@@ -2654,7 +2654,7 @@
<span class="md-ellipsis">
10.3 Binary Search Edge Cases
10.3 Binary Search Boundaries
@@ -2710,7 +2710,7 @@
<span class="md-ellipsis">
10.5 Search Algorithms Revisited
10.5 Searching Algorithms Revisited
@@ -2859,7 +2859,7 @@
<span class="md-ellipsis">
11.1 Sorting Algorithms
11.1 Sorting Algorithm
@@ -3332,7 +3332,7 @@
<span class="md-ellipsis">
12.4 Hanoi Tower Problem
12.4 Hanota Problem
@@ -4244,7 +4244,7 @@
<span class="md-ellipsis">
16.3 Terminology Table
16.3 Glossary
@@ -4490,7 +4490,7 @@
<h2 id="741-operations-on-a-binary-search-tree">7.4.1 &nbsp; Operations on a Binary Search Tree<a class="headerlink" href="#741-operations-on-a-binary-search-tree" title="Permanent link">&para;</a></h2>
<p>We encapsulate the binary search tree as a class <code>BinarySearchTree</code> and declare a member variable <code>root</code> pointing to the tree's root node.</p>
<h3 id="1-searching-for-a-node">1. &nbsp; Searching for a Node<a class="headerlink" href="#1-searching-for-a-node" title="Permanent link">&para;</a></h3>
<p>Given a target node value <code>num</code>, we can search according to the properties of the binary search tree. As shown in Figure 7-17, we declare a node <code>cur</code> and start from the binary tree's root node <code>root</code>, looping to compare the node value <code>cur.val</code> with <code>num</code>.</p>
<p>Given a target node value <code>num</code>, we can search according to the properties of the binary search tree. As shown in Figure 7-17, we declare a node <code>cur</code> and start from the binary search tree's root node <code>root</code>, looping to compare <code>cur.val</code> with <code>num</code>.</p>
<ul>
<li>If <code>cur.val &lt; num</code>, it means the target node is in <code>cur</code>'s right subtree, thus execute <code>cur = cur.right</code>.</li>
<li>If <code>cur.val &gt; num</code>, it means the target node is in <code>cur</code>'s left subtree, thus execute <code>cur = cur.left</code>.</li>
@@ -4514,7 +4514,7 @@
</div>
<p align="center"> Figure 7-17 &nbsp; Example of searching for a node in a binary search tree </p>
<p>The search operation in a binary search tree works on the same principle as the binary search algorithm, both eliminating half of the cases in each round. The number of loop iterations is at most the height of the binary tree. When the binary tree is balanced, it uses <span class="arithmatex">\(O(\log n)\)</span> time. The example code is as follows:</p>
<p>The search operation in a binary search tree follows the same principle as binary search: each round rules out half of the remaining cases. The number of loop iterations is at most the height of the tree. When the tree is balanced, the search takes <span class="arithmatex">\(O(\log n)\)</span> time. The example code is as follows:</p>
<div class="tabbed-set tabbed-alternate" data-tabs="2:13"><input checked="checked" id="__tabbed_2_1" name="__tabbed_2" type="radio" /><input id="__tabbed_2_2" name="__tabbed_2" type="radio" /><input id="__tabbed_2_3" name="__tabbed_2" type="radio" /><input id="__tabbed_2_4" name="__tabbed_2" type="radio" /><input id="__tabbed_2_5" name="__tabbed_2" type="radio" /><input id="__tabbed_2_6" name="__tabbed_2" type="radio" /><input id="__tabbed_2_7" name="__tabbed_2" type="radio" /><input id="__tabbed_2_8" name="__tabbed_2" type="radio" /><input id="__tabbed_2_9" name="__tabbed_2" type="radio" /><input id="__tabbed_2_10" name="__tabbed_2" type="radio" /><input id="__tabbed_2_11" name="__tabbed_2" type="radio" /><input id="__tabbed_2_12" name="__tabbed_2" type="radio" /><input id="__tabbed_2_13" name="__tabbed_2" type="radio" /><div class="tabbed-labels"><label for="__tabbed_2_1">Python</label><label for="__tabbed_2_2">C++</label><label for="__tabbed_2_3">Java</label><label for="__tabbed_2_4">C#</label><label for="__tabbed_2_5">Go</label><label for="__tabbed_2_6">Swift</label><label for="__tabbed_2_7">JS</label><label for="__tabbed_2_8">TS</label><label for="__tabbed_2_9">Dart</label><label for="__tabbed_2_10">Rust</label><label for="__tabbed_2_11">C</label><label for="__tabbed_2_12">Kotlin</label><label for="__tabbed_2_13">Ruby</label></div>
<div class="tabbed-content">
<div class="tabbed-block">
@@ -4794,14 +4794,14 @@
<p>Given an element <code>num</code> to be inserted, in order to maintain the property of the binary search tree "left subtree &lt; root node &lt; right subtree," the insertion process is as shown in Figure 7-18.</p>
<ol>
<li><strong>Finding the insertion position</strong>: Similar to the search operation, start from the root node and loop downward searching according to the size relationship between the current node value and <code>num</code>, until passing the leaf node (traversing to <code>None</code>) and then exit the loop.</li>
<li><strong>Insert the node at that position</strong>: Initialize node <code>num</code> and place it at the <code>None</code> position.</li>
<li><strong>Insert the node at that position</strong>: Create a node for <code>num</code> and place it at the <code>None</code> position.</li>
</ol>
<p><img alt="Inserting a node into a binary search tree" class="animation-figure" src="../binary_search_tree.assets/bst_insert.png" /></p>
<p align="center"> Figure 7-18 &nbsp; Inserting a node into a binary search tree </p>
<p>In the code implementation, note the following two points:</p>
<ul>
<li>Binary search trees do not allow duplicate nodes; otherwise, it would violate its definition. Therefore, if the node to be inserted already exists in the tree, the insertion is not performed and it returns directly.</li>
<li>Binary search trees do not allow duplicate nodes; otherwise, the tree would no longer satisfy its definition. Therefore, if the node to be inserted already exists in the tree, the insertion is skipped and the function returns directly.</li>
<li>To implement the node insertion, we need to use node <code>pre</code> to save the node from the previous loop iteration. This way, when traversing to <code>None</code>, we can obtain its parent node, thereby completing the node insertion operation.</li>
</ul>
<div class="tabbed-set tabbed-alternate" data-tabs="3:13"><input checked="checked" id="__tabbed_3_1" name="__tabbed_3" type="radio" /><input id="__tabbed_3_2" name="__tabbed_3" type="radio" /><input id="__tabbed_3_3" name="__tabbed_3" type="radio" /><input id="__tabbed_3_4" name="__tabbed_3" type="radio" /><input id="__tabbed_3_5" name="__tabbed_3" type="radio" /><input id="__tabbed_3_6" name="__tabbed_3" type="radio" /><input id="__tabbed_3_7" name="__tabbed_3" type="radio" /><input id="__tabbed_3_8" name="__tabbed_3" type="radio" /><input id="__tabbed_3_9" name="__tabbed_3" type="radio" /><input id="__tabbed_3_10" name="__tabbed_3" type="radio" /><input id="__tabbed_3_11" name="__tabbed_3" type="radio" /><input id="__tabbed_3_12" name="__tabbed_3" type="radio" /><input id="__tabbed_3_13" name="__tabbed_3" type="radio" /><div class="tabbed-labels"><label for="__tabbed_3_1">Python</label><label for="__tabbed_3_2">C++</label><label for="__tabbed_3_3">Java</label><label for="__tabbed_3_4">C#</label><label for="__tabbed_3_5">Go</label><label for="__tabbed_3_6">Swift</label><label for="__tabbed_3_7">JS</label><label for="__tabbed_3_8">TS</label><label for="__tabbed_3_9">Dart</label><label for="__tabbed_3_10">Rust</label><label for="__tabbed_3_11">C</label><label for="__tabbed_3_12">Kotlin</label><label for="__tabbed_3_13">Ruby</label></div>
@@ -5227,7 +5227,7 @@
</div>
<p>Similar to searching for a node, inserting a node uses <span class="arithmatex">\(O(\log n)\)</span> time.</p>
<h3 id="3-removing-a-node">3. &nbsp; Removing a Node<a class="headerlink" href="#3-removing-a-node" title="Permanent link">&para;</a></h3>
<p>First, find the target node in the binary tree, then remove it. Similar to node insertion, we need to ensure that after the removal operation is completed, the binary search tree's property of "left subtree <span class="arithmatex">\(&lt;\)</span> root node <span class="arithmatex">\(&lt;\)</span> right subtree" is still maintained. Therefore, depending on the number of child nodes the target node has, we divide it into 0, 1, and 2 three cases, and execute the corresponding node removal operations.</p>
<p>First, find the target node in the binary search tree, then remove it. Similar to node insertion, we need to ensure that after the removal operation is completed, the binary search tree's property of "left subtree <span class="arithmatex">\(&lt;\)</span> root node <span class="arithmatex">\(&lt;\)</span> right subtree" is still maintained. Therefore, depending on the number of child nodes the target node has, we consider three cases: degree <span class="arithmatex">\(0\)</span>, degree <span class="arithmatex">\(1\)</span>, and degree <span class="arithmatex">\(2\)</span>, and perform the corresponding removal operation.</p>
<p>As shown in Figure 7-19, when the degree of the node to be removed is <span class="arithmatex">\(0\)</span>, it means the node is a leaf node and can be directly removed.</p>
<p><img alt="Removing a node in a binary search tree (degree 0)" class="animation-figure" src="../binary_search_tree.assets/bst_remove_case1.png" /></p>
<p align="center"> Figure 7-19 &nbsp; Removing a node in a binary search tree (degree 0) </p>
@@ -5237,7 +5237,7 @@
<p align="center"> Figure 7-20 &nbsp; Removing a node in a binary search tree (degree 1) </p>
<p>When the degree of the node to be removed is <span class="arithmatex">\(2\)</span>, we cannot directly remove it; instead, we need to use a node to replace it. To maintain the binary search tree's property of "left subtree <span class="arithmatex">\(&lt;\)</span> root node <span class="arithmatex">\(&lt;\)</span> right subtree," <strong>this node can be either the smallest node in the right subtree or the largest node in the left subtree</strong>.</p>
<p>Assuming we choose the smallest node in the right subtree (the next node in the inorder traversal), the removal process is as shown in Figure 7-21.</p>
<p>Assuming we choose the smallest node in the right subtree, that is, the inorder successor, the removal process is as shown in Figure 7-21.</p>
<ol>
<li>Find the next node of the node to be removed in the "inorder traversal sequence," denoted as <code>tmp</code>.</li>
<li>Replace the value of the node to be removed with the value of <code>tmp</code>, and recursively remove node <code>tmp</code> in the tree.</li>
@@ -6018,7 +6018,7 @@
</tbody>
</table>
</div>
<p>In the ideal case, a binary search tree is "balanced," such that any node can be found within <span class="arithmatex">\(\log n\)</span> loop iterations.</p>
<p>In the ideal case, a binary search tree is balanced, so any node can be found within <span class="arithmatex">\(O(\log n)\)</span> loop iterations.</p>
<p>However, if we continuously insert and remove nodes in a binary search tree, it may degenerate into a linked list as shown in Figure 7-23, where the time complexity of various operations also degrades to <span class="arithmatex">\(O(n)\)</span>.</p>
<p><img alt="Degradation of a binary search tree" class="animation-figure" src="../binary_search_tree.assets/bst_degradation.png" /></p>
<p align="center"> Figure 7-23 &nbsp; Degradation of a binary search tree </p>
@@ -6051,7 +6051,7 @@ aria-label="Footer"
<a
href="../array_representation_of_tree/"
class="md-footer__link md-footer__link--prev"
aria-label="Previous: 7.3 Array Representation of Tree"
aria-label="Previous: 7.3 Array Representation of Binary Trees"
rel="prev"
>
<div class="md-footer__button md-icon">
@@ -6063,7 +6063,7 @@ aria-label="Footer"
Previous
</span>
<div class="md-ellipsis">
7.3 Array Representation of Tree
7.3 Array Representation of Binary Trees
</div>
</div>
</a>
+27 -27
View File
@@ -6,7 +6,7 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="description" content="Data Structures and Algorithms Crash Course with Animated Illustrations and Off-the-Shelf Code">
<meta name="description" content="Data structures and algorithms tutorial with animated illustrations and ready-to-run code">
<meta name="author" content="krahets">
@@ -576,7 +576,7 @@
<span class="md-ellipsis">
Chapter 1. Encounter With Algorithms
Chapter 1. Encounter with Algorithms
@@ -598,7 +598,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 1. Encounter With Algorithms
Chapter 1. Encounter with Algorithms
</label>
@@ -1183,7 +1183,7 @@
<span class="md-ellipsis">
Chapter 4. Array and Linked List
Chapter 4. Arrays and Linked Lists
@@ -1205,7 +1205,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 4. Array and Linked List
Chapter 4. Arrays and Linked Lists
</label>
@@ -1311,7 +1311,7 @@
<span class="md-ellipsis">
4.4 Memory and Cache *
4.4 Random-Access Memory and Cache *
@@ -1402,7 +1402,7 @@
<span class="md-ellipsis">
Chapter 5. Stack and Queue
Chapter 5. Stacks and Queues
@@ -1424,7 +1424,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 5. Stack and Queue
Chapter 5. Stacks and Queues
</label>
@@ -1502,7 +1502,7 @@
<span class="md-ellipsis">
5.3 Double-Ended Queue
5.3 Deque
@@ -1593,7 +1593,7 @@
<span class="md-ellipsis">
Chapter 6. Hashing
Chapter 6. Hash Table
@@ -1615,7 +1615,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 6. Hashing
Chapter 6. Hash Table
</label>
@@ -2060,7 +2060,7 @@
<span class="md-ellipsis">
7.3 Array Representation of Tree
7.3 Array Representation of Binary Trees
@@ -2279,7 +2279,7 @@
<span class="md-ellipsis">
8.2 Building a Heap
8.2 Heap Construction Operation
@@ -2307,7 +2307,7 @@
<span class="md-ellipsis">
8.3 Top-K Problem
8.3 Top-k Problem
@@ -2665,7 +2665,7 @@
<span class="md-ellipsis">
10.2 Binary Search Insertion
10.2 Binary Search Insertion Point
@@ -2693,7 +2693,7 @@
<span class="md-ellipsis">
10.3 Binary Search Edge Cases
10.3 Binary Search Boundaries
@@ -2749,7 +2749,7 @@
<span class="md-ellipsis">
10.5 Search Algorithms Revisited
10.5 Searching Algorithms Revisited
@@ -2898,7 +2898,7 @@
<span class="md-ellipsis">
11.1 Sorting Algorithms
11.1 Sorting Algorithm
@@ -3371,7 +3371,7 @@
<span class="md-ellipsis">
12.4 Hanoi Tower Problem
12.4 Hanota Problem
@@ -4283,7 +4283,7 @@
<span class="md-ellipsis">
16.3 Terminology Table
16.3 Glossary
@@ -4557,7 +4557,7 @@
<!-- Page content -->
<h1 id="71-binary-tree">7.1 &nbsp; Binary Tree<a class="headerlink" href="#71-binary-tree" title="Permanent link">&para;</a></h1>
<p>A <u>binary tree</u> is a non-linear data structure that represents the derivation relationship between "ancestors" and "descendants" and embodies the divide-and-conquer logic of "one divides into two". Similar to a linked list, the basic unit of a binary tree is a node, and each node contains a value, a reference to its left child node, and a reference to its right child node.</p>
<p>A <u>binary tree</u> is a non-linear data structure that models the hierarchical relationship between "ancestors" and "descendants" and embodies a divide-and-conquer pattern in which each split branches into two. Similar to a linked list, the basic unit of a binary tree is a node, and each node contains a value, a reference to its left child node, and a reference to its right child node.</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">
<div class="tabbed-block">
@@ -4736,7 +4736,7 @@
</div>
</div>
<p>Each node has two references (pointers), pointing respectively to the <u>left-child node</u> and <u>right-child node</u>. This node is called the <u>parent node</u> 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 below it the <u>left subtree</u> of this node. Similarly, the <u>right subtree</u> can be defined.</p>
<p><strong>In a binary tree, except leaf nodes, all other nodes contain child nodes and non-empty subtrees.</strong> As shown in Figure 7-1, if "Node 2" is regarded as a parent node, its left and right child nodes are "Node 4" and "Node 5" respectively. The left subtree is formed by "Node 4" and all nodes beneath it, while the right subtree is formed by "Node 5" and all nodes beneath it.</p>
<p><strong>In a binary tree, every non-leaf node has child nodes and therefore non-empty subtrees.</strong> As shown in Figure 7-1, if "Node 2" is regarded as a parent node, its left and right child nodes are "Node 4" and "Node 5" respectively. The left subtree is formed by "Node 4" and all nodes beneath it, while the right subtree is formed by "Node 5" and all nodes beneath it.</p>
<p><img alt="Parent Node, child Node, subtree" class="animation-figure" src="../binary_tree.assets/binary_tree_definition.png" /></p>
<p align="center"> Figure 7-1 &nbsp; Parent Node, child Node, subtree </p>
@@ -4757,7 +4757,7 @@
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>Please note that we usually define "height" and "depth" as "the number of edges traversed", but some questions or textbooks may define them as "the number of nodes traversed". In this case, both height and depth need to be incremented by 1.</p>
<p>We usually define "height" and "depth" as the number of edges traversed, but some textbooks and problem statements define them as the number of nodes on the path. In that case, both values are larger by 1.</p>
</div>
<h2 id="712-basic-operations-of-binary-trees">7.1.2 &nbsp; Basic Operations of Binary Trees<a class="headerlink" href="#712-basic-operations-of-binary-trees" title="Permanent link">&para;</a></h2>
<h3 id="1-initializing-a-binary-tree">1. &nbsp; Initializing a Binary Tree<a class="headerlink" href="#1-initializing-a-binary-tree" title="Permanent link">&para;</a></h3>
@@ -5104,11 +5104,11 @@
</details>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>It should be noted that inserting nodes may change the original logical structure of the binary tree, while removing nodes typically involves removing the node and all its subtrees. Therefore, in a binary tree, insertion and removal are usually performed through a set of operations to achieve meaningful outcomes.</p>
<p>Keep in mind that inserting a node can alter the original logical structure of a binary tree, while deleting a node usually entails removing that node together with its entire subtree. In practice, insertion and deletion in binary trees are therefore typically implemented as coordinated sequences of operations to achieve a meaningful result.</p>
</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 Figure 7-4, a <u>perfect binary tree</u> has all levels completely filled with nodes. In a perfect binary tree, leaf nodes have a degree of <span class="arithmatex">\(0\)</span>, while all other nodes have a degree of <span class="arithmatex">\(2\)</span>. If the tree height is <span class="arithmatex">\(h\)</span>, the total number of nodes is <span class="arithmatex">\(2^{h+1} - 1\)</span>, exhibiting a standard exponential relationship that reflects the common phenomenon of cell division in nature.</p>
<p>As shown in Figure 7-4, a <u>perfect binary tree</u> has every level completely filled. In a perfect binary tree, leaf nodes have a degree of <span class="arithmatex">\(0\)</span>, while all other nodes have a degree of <span class="arithmatex">\(2\)</span>. If the tree height is <span class="arithmatex">\(h\)</span>, the total number of nodes is <span class="arithmatex">\(2^{h+1} - 1\)</span>, following a standard exponential pattern that mirrors 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 <u>full binary tree</u>.</p>
@@ -5132,9 +5132,9 @@
<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>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" state; when all nodes are biased toward one side, the binary tree degenerates into a "linked list".</p>
<p>Figure 7-8 contrasts the ideal and degenerate structures of binary trees. When every level is filled, the tree becomes a "perfect binary tree"; when all nodes skew to one side, the binary tree degenerates into a "linked list".</p>
<ul>
<li>A perfect binary tree is the ideal case, fully leveraging the "divide and conquer" advantage of binary trees.</li>
<li>A perfect binary tree is the ideal case, fully leveraging the divide-and-conquer advantages of binary trees.</li>
<li>A linked list represents the other extreme, where all operations become linear operations with time complexity degrading to <span class="arithmatex">\(O(n)\)</span>.</li>
</ul>
<p><img alt="The Best and Worst Structures of Binary Trees" class="animation-figure" src="../binary_tree.assets/binary_tree_best_worst_cases.png" /></p>
@@ -6,7 +6,7 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="description" content="Data Structures and Algorithms Crash Course with Animated Illustrations and Off-the-Shelf Code">
<meta name="description" content="Data structures and algorithms tutorial with animated illustrations and ready-to-run code">
<meta name="author" content="krahets">
@@ -576,7 +576,7 @@
<span class="md-ellipsis">
Chapter 1. Encounter With Algorithms
Chapter 1. Encounter with Algorithms
@@ -598,7 +598,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 1. Encounter With Algorithms
Chapter 1. Encounter with Algorithms
</label>
@@ -1183,7 +1183,7 @@
<span class="md-ellipsis">
Chapter 4. Array and Linked List
Chapter 4. Arrays and Linked Lists
@@ -1205,7 +1205,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 4. Array and Linked List
Chapter 4. Arrays and Linked Lists
</label>
@@ -1311,7 +1311,7 @@
<span class="md-ellipsis">
4.4 Memory and Cache *
4.4 Random-Access Memory and Cache *
@@ -1402,7 +1402,7 @@
<span class="md-ellipsis">
Chapter 5. Stack and Queue
Chapter 5. Stacks and Queues
@@ -1424,7 +1424,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 5. Stack and Queue
Chapter 5. Stacks and Queues
</label>
@@ -1502,7 +1502,7 @@
<span class="md-ellipsis">
5.3 Double-Ended Queue
5.3 Deque
@@ -1593,7 +1593,7 @@
<span class="md-ellipsis">
Chapter 6. Hashing
Chapter 6. Hash Table
@@ -1615,7 +1615,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 6. Hashing
Chapter 6. Hash Table
</label>
@@ -2016,7 +2016,7 @@
<span class="md-ellipsis">
7.3 Array Representation of Tree
7.3 Array Representation of Binary Trees
@@ -2235,7 +2235,7 @@
<span class="md-ellipsis">
8.2 Building a Heap
8.2 Heap Construction Operation
@@ -2263,7 +2263,7 @@
<span class="md-ellipsis">
8.3 Top-K Problem
8.3 Top-k Problem
@@ -2621,7 +2621,7 @@
<span class="md-ellipsis">
10.2 Binary Search Insertion
10.2 Binary Search Insertion Point
@@ -2649,7 +2649,7 @@
<span class="md-ellipsis">
10.3 Binary Search Edge Cases
10.3 Binary Search Boundaries
@@ -2705,7 +2705,7 @@
<span class="md-ellipsis">
10.5 Search Algorithms Revisited
10.5 Searching Algorithms Revisited
@@ -2854,7 +2854,7 @@
<span class="md-ellipsis">
11.1 Sorting Algorithms
11.1 Sorting Algorithm
@@ -3327,7 +3327,7 @@
<span class="md-ellipsis">
12.4 Hanoi Tower Problem
12.4 Hanota Problem
@@ -4239,7 +4239,7 @@
<span class="md-ellipsis">
16.3 Terminology Table
16.3 Glossary
@@ -4473,7 +4473,7 @@
<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. Within each level, it visits nodes from left to right.</p>
<p>Level-order traversal is essentially <u>breadth-first traversal</u>, also known as <u>breadth-first search (BFS)</u>, which embodies a "expanding outward circle by circle" layer-by-layer traversal method.</p>
<p>Level-order traversal is essentially <u>breadth-first traversal</u>, also known as <u>breadth-first search (BFS)</u>, which proceeds outward level by level.</p>
<p><img alt="Level-order traversal of a binary tree" class="animation-figure" src="../binary_tree_traversal.assets/binary_tree_bfs.png" /></p>
<p align="center"> Figure 7-9 &nbsp; Level-order traversal of a binary tree </p>
@@ -4775,7 +4775,7 @@
<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 contains 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-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 traversals all belong to <u>depth-first traversal</u>, also known as <u>depth-first search (DFS)</u>, which embodies a "first go to the end, then backtrack and continue" traversal method.</p>
<p>Correspondingly, preorder, inorder, and postorder traversals all belong to <u>depth-first traversal</u>, also known as <u>depth-first search (DFS)</u>, which goes as deep as possible before backtracking.</p>
<p>Figure 7-10 shows how depth-first traversal works 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, inorder, and postorder traversal.</p>
<p><img alt="Preorder, inorder, and postorder traversal of a binary tree" class="animation-figure" src="../binary_tree_traversal.assets/binary_tree_dfs.png" /></p>
<p align="center"> Figure 7-10 &nbsp; Preorder, inorder, and postorder traversal of a binary tree </p>
@@ -5223,12 +5223,12 @@
</div>
<div class="admonition tip">
<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>
<p>Depth-first search can also be implemented iteratively, and interested readers can explore this on their own.</p>
</div>
<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>
<p>Figure 7-11 shows the recursive process of preorder traversal of a binary tree, which can be divided into two opposite phases: "descending" and "returning".</p>
<ol>
<li>"Recursion" means opening a new method, where the program accesses the next node in this process.</li>
<li>"Return" means the function returns, indicating that the current node has been fully visited.</li>
<li>"Descending" means making a new recursive call, during which the program visits the next node.</li>
<li>"Returning" means the function call returns, indicating that the current node has been fully processed.</li>
</ol>
<div class="tabbed-set tabbed-alternate" data-tabs="3:11"><input checked="checked" id="__tabbed_3_1" name="__tabbed_3" type="radio" /><input id="__tabbed_3_2" name="__tabbed_3" type="radio" /><input id="__tabbed_3_3" name="__tabbed_3" type="radio" /><input id="__tabbed_3_4" name="__tabbed_3" type="radio" /><input id="__tabbed_3_5" name="__tabbed_3" type="radio" /><input id="__tabbed_3_6" name="__tabbed_3" type="radio" /><input id="__tabbed_3_7" name="__tabbed_3" type="radio" /><input id="__tabbed_3_8" name="__tabbed_3" type="radio" /><input id="__tabbed_3_9" name="__tabbed_3" type="radio" /><input id="__tabbed_3_10" name="__tabbed_3" type="radio" /><input id="__tabbed_3_11" name="__tabbed_3" type="radio" /><div class="tabbed-labels"><label for="__tabbed_3_1">&lt;1&gt;</label><label for="__tabbed_3_2">&lt;2&gt;</label><label for="__tabbed_3_3">&lt;3&gt;</label><label for="__tabbed_3_4">&lt;4&gt;</label><label for="__tabbed_3_5">&lt;5&gt;</label><label for="__tabbed_3_6">&lt;6&gt;</label><label for="__tabbed_3_7">&lt;7&gt;</label><label for="__tabbed_3_8">&lt;8&gt;</label><label for="__tabbed_3_9">&lt;9&gt;</label><label for="__tabbed_3_10">&lt;10&gt;</label><label for="__tabbed_3_11">&lt;11&gt;</label></div>
<div class="tabbed-content">
@@ -5320,7 +5320,7 @@ aria-label="Footer"
<a
href="../array_representation_of_tree/"
class="md-footer__link md-footer__link--next"
aria-label="Next: 7.3 Array Representation of Tree"
aria-label="Next: 7.3 Array Representation of Binary Trees"
rel="next"
>
<div class="md-footer__title">
@@ -5328,7 +5328,7 @@ aria-label="Footer"
Next
</span>
<div class="md-ellipsis">
7.3 Array Representation of Tree
7.3 Array Representation of Binary Trees
</div>
</div>
<div class="md-footer__button md-icon">
+23 -23
View File
@@ -6,7 +6,7 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="description" content="Data Structures and Algorithms Crash Course with Animated Illustrations and Off-the-Shelf Code">
<meta name="description" content="Data structures and algorithms tutorial with animated illustrations and ready-to-run code">
<meta name="author" content="krahets">
@@ -576,7 +576,7 @@
<span class="md-ellipsis">
Chapter 1. Encounter With Algorithms
Chapter 1. Encounter with Algorithms
@@ -598,7 +598,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 1. Encounter With Algorithms
Chapter 1. Encounter with Algorithms
</label>
@@ -1183,7 +1183,7 @@
<span class="md-ellipsis">
Chapter 4. Array and Linked List
Chapter 4. Arrays and Linked Lists
@@ -1205,7 +1205,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 4. Array and Linked List
Chapter 4. Arrays and Linked Lists
</label>
@@ -1311,7 +1311,7 @@
<span class="md-ellipsis">
4.4 Memory and Cache *
4.4 Random-Access Memory and Cache *
@@ -1402,7 +1402,7 @@
<span class="md-ellipsis">
Chapter 5. Stack and Queue
Chapter 5. Stacks and Queues
@@ -1424,7 +1424,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 5. Stack and Queue
Chapter 5. Stacks and Queues
</label>
@@ -1502,7 +1502,7 @@
<span class="md-ellipsis">
5.3 Double-Ended Queue
5.3 Deque
@@ -1593,7 +1593,7 @@
<span class="md-ellipsis">
Chapter 6. Hashing
Chapter 6. Hash Table
@@ -1615,7 +1615,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 6. Hashing
Chapter 6. Hash Table
</label>
@@ -1890,7 +1890,7 @@
<span class="md-ellipsis">
7.3 Array Representation of Tree
7.3 Array Representation of Binary Trees
@@ -2109,7 +2109,7 @@
<span class="md-ellipsis">
8.2 Building a Heap
8.2 Heap Construction Operation
@@ -2137,7 +2137,7 @@
<span class="md-ellipsis">
8.3 Top-K Problem
8.3 Top-k Problem
@@ -2495,7 +2495,7 @@
<span class="md-ellipsis">
10.2 Binary Search Insertion
10.2 Binary Search Insertion Point
@@ -2523,7 +2523,7 @@
<span class="md-ellipsis">
10.3 Binary Search Edge Cases
10.3 Binary Search Boundaries
@@ -2579,7 +2579,7 @@
<span class="md-ellipsis">
10.5 Search Algorithms Revisited
10.5 Searching Algorithms Revisited
@@ -2728,7 +2728,7 @@
<span class="md-ellipsis">
11.1 Sorting Algorithms
11.1 Sorting Algorithm
@@ -3201,7 +3201,7 @@
<span class="md-ellipsis">
12.4 Hanoi Tower Problem
12.4 Hanota Problem
@@ -4113,7 +4113,7 @@
<span class="md-ellipsis">
16.3 Terminology Table
16.3 Glossary
@@ -4279,14 +4279,14 @@
<p><img alt="Tree" class="cover-image" src="../assets/covers/chapter_tree.jpg" /></p>
<div class="admonition abstract">
<p class="admonition-title">Abstract</p>
<p>Towering trees are full of vitality, with deep roots and lush leaves, spreading branches and flourishing.</p>
<p>They show us the vivid form of divide and conquer in data.</p>
<p>Towering trees are full of vitality, with deep roots, lush foliage, and sprawling branches.</p>
<p>They offer a vivid illustration of divide-and-conquer in data structures.</p>
</div>
<h2 id="chapter-contents">Chapter contents<a class="headerlink" href="#chapter-contents" title="Permanent link">&para;</a></h2>
<ul>
<li><a href="binary_tree/">7.1 &nbsp; Binary Tree</a></li>
<li><a href="binary_tree_traversal/">7.2 &nbsp; Binary Tree Traversal</a></li>
<li><a href="array_representation_of_tree/">7.3 &nbsp; Array Representation of Tree</a></li>
<li><a href="array_representation_of_tree/">7.3 &nbsp; Array Representation of Binary Trees</a></li>
<li><a href="binary_search_tree/">7.4 &nbsp; Binary Search Tree</a></li>
<li><a href="avl_tree/">7.5 &nbsp; AVL Tree *</a></li>
<li><a href="summary/">7.6 &nbsp; Summary</a></li>
+26 -26
View File
@@ -6,7 +6,7 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="description" content="Data Structures and Algorithms Crash Course with Animated Illustrations and Off-the-Shelf Code">
<meta name="description" content="Data structures and algorithms tutorial with animated illustrations and ready-to-run code">
<meta name="author" content="krahets">
@@ -576,7 +576,7 @@
<span class="md-ellipsis">
Chapter 1. Encounter With Algorithms
Chapter 1. Encounter with Algorithms
@@ -598,7 +598,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 1. Encounter With Algorithms
Chapter 1. Encounter with Algorithms
</label>
@@ -1183,7 +1183,7 @@
<span class="md-ellipsis">
Chapter 4. Array and Linked List
Chapter 4. Arrays and Linked Lists
@@ -1205,7 +1205,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 4. Array and Linked List
Chapter 4. Arrays and Linked Lists
</label>
@@ -1311,7 +1311,7 @@
<span class="md-ellipsis">
4.4 Memory and Cache *
4.4 Random-Access Memory and Cache *
@@ -1402,7 +1402,7 @@
<span class="md-ellipsis">
Chapter 5. Stack and Queue
Chapter 5. Stacks and Queues
@@ -1424,7 +1424,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 5. Stack and Queue
Chapter 5. Stacks and Queues
</label>
@@ -1502,7 +1502,7 @@
<span class="md-ellipsis">
5.3 Double-Ended Queue
5.3 Deque
@@ -1593,7 +1593,7 @@
<span class="md-ellipsis">
Chapter 6. Hashing
Chapter 6. Hash Table
@@ -1615,7 +1615,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 6. Hashing
Chapter 6. Hash Table
</label>
@@ -1890,7 +1890,7 @@
<span class="md-ellipsis">
7.3 Array Representation of Tree
7.3 Array Representation of Binary Trees
@@ -2179,7 +2179,7 @@
<span class="md-ellipsis">
8.2 Building a Heap
8.2 Heap Construction Operation
@@ -2207,7 +2207,7 @@
<span class="md-ellipsis">
8.3 Top-K Problem
8.3 Top-k Problem
@@ -2565,7 +2565,7 @@
<span class="md-ellipsis">
10.2 Binary Search Insertion
10.2 Binary Search Insertion Point
@@ -2593,7 +2593,7 @@
<span class="md-ellipsis">
10.3 Binary Search Edge Cases
10.3 Binary Search Boundaries
@@ -2649,7 +2649,7 @@
<span class="md-ellipsis">
10.5 Search Algorithms Revisited
10.5 Searching Algorithms Revisited
@@ -2798,7 +2798,7 @@
<span class="md-ellipsis">
11.1 Sorting Algorithms
11.1 Sorting Algorithm
@@ -3271,7 +3271,7 @@
<span class="md-ellipsis">
12.4 Hanoi Tower Problem
12.4 Hanota Problem
@@ -4183,7 +4183,7 @@
<span class="md-ellipsis">
16.3 Terminology Table
16.3 Glossary
@@ -4359,21 +4359,21 @@
<h1 id="76-summary">7.6 &nbsp; Summary<a class="headerlink" href="#76-summary" title="Permanent link">&para;</a></h1>
<h3 id="1-key-review">1. &nbsp; Key Review<a class="headerlink" href="#1-key-review" title="Permanent link">&para;</a></h3>
<ul>
<li>A binary tree is a non-linear data structure that embodies the divide-and-conquer logic of "one divides into two". Each binary tree node contains a value and two pointers, which respectively point to its left and right child nodes.</li>
<li>A binary tree is a non-linear data structure that embodies the divide-and-conquer logic of splitting into two. Each binary tree node contains a value and two pointers, which point to its left and right child nodes.</li>
<li>For a certain node in a binary tree, the tree formed by its left (right) child node and all nodes below is called the left (right) subtree of that node.</li>
<li>Related terminology of binary trees includes root node, leaf node, level, degree, edge, height, and depth.</li>
<li>The initialization, node insertion, and node removal operations of binary trees are similar to those of linked lists.</li>
<li>Common types of binary trees include perfect binary trees, complete binary trees, full binary trees, and balanced binary trees. The perfect binary tree is the ideal state, while the linked list is the worst state after degradation.</li>
<li>Common types of binary trees include perfect binary trees, complete binary trees, full binary trees, and balanced binary trees. A perfect binary tree is the ideal form, while a linked list represents the worst degenerate case.</li>
<li>A binary tree can be represented using an array by arranging node values and empty slots in level-order traversal sequence, and implementing pointers based on the index mapping relationship between parent and child nodes.</li>
<li>Level-order traversal of a binary tree is a breadth-first search method, embodying a layer-by-layer traversal approach of "expanding outward circle by circle", typically implemented using a queue.</li>
<li>Preorder, inorder, and postorder traversals all belong to depth-first search, embodying a traversal approach of "first go to the end, then backtrack and continue", typically implemented using recursion.</li>
<li>Level-order traversal of a binary tree is a breadth-first search method that proceeds level by level, typically implemented using a queue.</li>
<li>Preorder, inorder, and postorder traversals all belong to depth-first search, which proceeds by going as deep as possible before backtracking, typically using recursion.</li>
<li>A binary search tree is an efficient data structure for element searching, with search, insertion, and removal operations all having time complexity of <span class="arithmatex">\(O(\log n)\)</span>. When a binary search tree degenerates into a linked list, all time complexities degrade to <span class="arithmatex">\(O(n)\)</span>.</li>
<li>An AVL tree, also known as a balanced binary search tree, ensures the tree remains balanced after continuous node insertions and removals through rotation operations.</li>
<li>Rotation operations in AVL trees include right rotation, left rotation, left rotation then right rotation, and right rotation then left rotation. After inserting or removing nodes, AVL trees perform rotation operations from bottom to top to restore the tree to balance.</li>
<li>Rotation operations in AVL trees include right rotation, left rotation, right rotation followed by left rotation, and left rotation followed by right rotation. After inserting or removing nodes, AVL trees perform rotations from bottom to top to restore balance.</li>
</ul>
<h3 id="2-q-a">2. &nbsp; Q &amp; A<a class="headerlink" href="#2-q-a" title="Permanent link">&para;</a></h3>
<p><strong>Q</strong>: For a binary tree with only one node, are both the height of the tree and the depth of the root node <span class="arithmatex">\(0\)</span>?</p>
<p>Yes, because height and depth are typically defined as "the number of edges passed."</p>
<p>Yes, because height and depth are typically defined as the number of edges on the path.</p>
<p><strong>Q</strong>: The insertion and removal in a binary tree are generally accomplished by a set of operations. What does "a set of operations" refer to here? Does it imply releasing the resources of the child nodes?</p>
<p>Taking the binary search tree as an example, the operation of removing a node needs to be handled in three different scenarios, each requiring multiple steps of node operations.</p>
<p><strong>Q</strong>: Why does DFS traversal of binary trees have three orders: preorder, inorder, and postorder, and what are their uses?</p>