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