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
+31 -31
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>
@@ -1888,7 +1888,7 @@
<span class="md-ellipsis">
7.3 Array Representation of Tree
7.3 Array Representation of Binary Trees
@@ -2118,7 +2118,7 @@
<span class="md-ellipsis">
8.2 Building a Heap
8.2 Heap Construction Operation
@@ -2136,7 +2136,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
@@ -4381,7 +4381,7 @@
<h1 id="82-heap-construction-operation">8.2 &nbsp; Heap Construction Operation<a class="headerlink" href="#82-heap-construction-operation" title="Permanent link">&para;</a></h1>
<p>In some cases, we want to build a heap using all elements of a list, and this process is called "heap construction operation."</p>
<h2 id="821-implementing-with-element-insertion">8.2.1 &nbsp; Implementing with Element Insertion<a class="headerlink" href="#821-implementing-with-element-insertion" title="Permanent link">&para;</a></h2>
<p>We first create an empty heap, then iterate through the list, performing the "element insertion operation" on each element in sequence. This means adding the element to the bottom of the heap and then performing "bottom-to-top" heapify on that element.</p>
<p>We first create an empty heap, then iterate through the list, performing the "element insertion operation" on each element in sequence. This means appending the element to the end of the heap and then performing "bottom-to-top" heapify on that element.</p>
<p>Each time an element is inserted into the heap, the heap's length increases by one. Since nodes are added to the binary tree sequentially from top to bottom, the heap is constructed "from top to bottom."</p>
<p>Given <span class="arithmatex">\(n\)</span> elements, each element's insertion operation takes <span class="arithmatex">\(O(\log{n})\)</span> time, so the time complexity of this heap construction method is <span class="arithmatex">\(O(n \log n)\)</span>.</p>
<h2 id="822-implementing-through-heapify-traversal">8.2.2 &nbsp; Implementing Through Heapify Traversal<a class="headerlink" href="#822-implementing-through-heapify-traversal" title="Permanent link">&para;</a></h2>
@@ -4391,8 +4391,8 @@
<li>Traverse the heap in reverse order (reverse of level-order traversal), performing "top-to-bottom heapify" on each non-leaf node in sequence.</li>
</ol>
<p><strong>After heapifying a node, the subtree rooted at that node becomes a valid sub-heap</strong>. Since we traverse in reverse order, the heap is constructed "from bottom to top."</p>
<p>The reason for choosing reverse order traversal is that it ensures the subtree below the current node is already a valid sub-heap, making the heapification of the current node effective.</p>
<p>It's worth noting that <strong>since leaf nodes have no children, they are naturally valid sub-heaps and do not require heapification</strong>. As shown in the code below, the last non-leaf node is the parent of the last node; we start from it and traverse in reverse order to perform heapification:</p>
<p>The reason for choosing reverse-order traversal is that it ensures the subtrees beneath the current node are already valid sub-heaps, so heapifying the current node is effective.</p>
<p>It's worth noting that <strong>since leaf nodes have no children, they are naturally valid sub-heaps and do not require heapification</strong>. As shown in the code below, the last non-leaf node is the parent of the last node; we start from that node and heapify while traversing in reverse order:</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">
@@ -4670,25 +4670,25 @@
<p>Next, let's attempt to derive the time complexity of this second heap construction method.</p>
<ul>
<li>Assuming the complete binary tree has <span class="arithmatex">\(n\)</span> nodes, then the number of leaf nodes is <span class="arithmatex">\((n + 1) / 2\)</span>, where <span class="arithmatex">\(/\)</span> is floor division. Therefore, the number of nodes that need heapification is <span class="arithmatex">\((n - 1) / 2\)</span>.</li>
<li>In the top-to-bottom heapify process, each node is heapified at most to the leaf nodes, so the maximum number of iterations is the binary tree height <span class="arithmatex">\(\log n\)</span>.</li>
<li>In the top-to-bottom heapify process, each node can sink at most to a leaf node, so the maximum number of iterations is the height of the binary tree, <span class="arithmatex">\(\log n\)</span>.</li>
</ul>
<p>Multiplying these two together, we get a time complexity of <span class="arithmatex">\(O(n \log n)\)</span> for the heap construction process. <strong>However, this estimate is not accurate because it doesn't account for the property that binary trees have far more nodes at lower levels than at upper levels</strong>.</p>
<p>Let's perform a more accurate calculation. To reduce calculation difficulty, assume a "perfect binary tree" with <span class="arithmatex">\(n\)</span> nodes and height <span class="arithmatex">\(h\)</span>; this assumption does not affect the correctness of the result.</p>
<p>Let's perform a more accurate calculation. To simplify the analysis, assume a "perfect binary tree" with <span class="arithmatex">\(n\)</span> nodes and height <span class="arithmatex">\(h\)</span>; this assumption does not affect the correctness of the result.</p>
<p><img alt="Node count at each level of a perfect binary tree" class="animation-figure" src="../build_heap.assets/heapify_operations_count.png" /></p>
<p align="center"> Figure 8-5 &nbsp; Node count at each level of a perfect binary tree </p>
<p>As shown in Figure 8-5, the maximum number of iterations for a node's "top-to-bottom heapify" equals the distance from that node to the leaf nodes, which is precisely the "node height." Therefore, we can sum the "number of nodes <span class="arithmatex">\(\times\)</span> node height" at each level to <strong>obtain the total number of heapify iterations for all nodes</strong>.</p>
<p>As shown in Figure 8-5, the maximum number of iterations for a node's "top-to-bottom heapify" equals the distance from that node to a leaf node, which is precisely the node's height. Therefore, we can sum the "number of nodes <span class="arithmatex">\(\times\)</span> node height" at each level to <strong>obtain the total number of heapify iterations for all nodes</strong>.</p>
<div class="arithmatex">\[
T(h) = 2^0h + 2^1(h-1) + 2^2(h-2) + \dots + 2^{(h-1)}\times1
\]</div>
<p>To simplify the above expression, we need to use sequence knowledge from high school. First, multiply <span class="arithmatex">\(T(h)\)</span> by <span class="arithmatex">\(2\)</span> to get:</p>
<p>Simplifying the expression above requires some high-school sequence algebra. First, multiply <span class="arithmatex">\(T(h)\)</span> by <span class="arithmatex">\(2\)</span> to get:</p>
<div class="arithmatex">\[
\begin{aligned}
T(h) &amp; = 2^0h + 2^1(h-1) + 2^2(h-2) + \dots + 2^{h-1}\times1 \newline
2 T(h) &amp; = 2^1h + 2^2(h-1) + 2^3(h-2) + \dots + 2^{h}\times1 \newline
\end{aligned}
\]</div>
<p>Using the method of differences, subtract the first equation <span class="arithmatex">\(T(h)\)</span> from the second equation <span class="arithmatex">\(2 T(h)\)</span> to get:</p>
<p>Using subtraction of shifted sums, subtract the first equation <span class="arithmatex">\(T(h)\)</span> from the second equation <span class="arithmatex">\(2 T(h)\)</span> to get:</p>
<div class="arithmatex">\[
2T(h) - T(h) = T(h) = -2^0h + 2^1 + 2^2 + \dots + 2^{h-1} + 2^h
\]</div>
@@ -4747,7 +4747,7 @@ aria-label="Footer"
<a
href="../top_k/"
class="md-footer__link md-footer__link--next"
aria-label="Next: 8.3 Top-K Problem"
aria-label="Next: 8.3 Top-k Problem"
rel="next"
>
<div class="md-footer__title">
@@ -4755,7 +4755,7 @@ aria-label="Footer"
Next
</span>
<div class="md-ellipsis">
8.3 Top-K Problem
8.3 Top-k Problem
</div>
</div>
<div class="md-footer__button md-icon">
+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>
@@ -1888,7 +1888,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
@@ -4494,7 +4494,7 @@
<li>For max heaps (min heaps), the value of the heap top element (root node) is the largest (smallest).</li>
</ul>
<h2 id="811-common-heap-operations">8.1.1 &nbsp; Common Heap Operations<a class="headerlink" href="#811-common-heap-operations" title="Permanent link">&para;</a></h2>
<p>It should be noted that many programming languages provide a <u>priority queue</u>, which is an abstract data structure defined as a queue with priority sorting.</p>
<p>It should be noted that many programming languages provide a <u>priority queue</u>, an abstract data structure defined as a queue whose elements are ordered by priority.</p>
<p>In fact, <strong>heaps are typically used to implement priority queues, with max heaps corresponding to priority queues where elements are dequeued in descending order</strong>. From a usage perspective, we can regard "priority queue" and "heap" as equivalent data structures. Therefore, this book does not make a special distinction between the two and uniformly refers to them as "heap."</p>
<p>Common heap operations are shown in Table 8-1, and method names need to be determined based on the programming language.</p>
<p align="center"> Table 8-1 &nbsp; Efficiency of Heap Operations </p>
@@ -4896,10 +4896,10 @@
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=import%20heapq%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%B0%8F%E9%A1%B6%E5%A0%86%0A%20%20%20%20min_heap,%20flag%20%3D%20%5B%5D,%201%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%A4%A7%E9%A1%B6%E5%A0%86%0A%20%20%20%20max_heap,%20flag%20%3D%20%5B%5D,%20-1%0A%20%20%20%20%0A%20%20%20%20%23%20Python%20%E7%9A%84%20heapq%20%E6%A8%A1%E5%9D%97%E9%BB%98%E8%AE%A4%E5%AE%9E%E7%8E%B0%E5%B0%8F%E9%A1%B6%E5%A0%86%0A%20%20%20%20%23%20%E8%80%83%E8%99%91%E5%B0%86%E2%80%9C%E5%85%83%E7%B4%A0%E5%8F%96%E8%B4%9F%E2%80%9D%E5%90%8E%E5%86%8D%E5%85%A5%E5%A0%86%EF%BC%8C%E8%BF%99%E6%A0%B7%E5%B0%B1%E5%8F%AF%E4%BB%A5%E5%B0%86%E5%A4%A7%E5%B0%8F%E5%85%B3%E7%B3%BB%E9%A2%A0%E5%80%92%EF%BC%8C%E4%BB%8E%E8%80%8C%E5%AE%9E%E7%8E%B0%E5%A4%A7%E9%A1%B6%E5%A0%86%0A%20%20%20%20%23%20%E5%9C%A8%E6%9C%AC%E7%A4%BA%E4%BE%8B%E4%B8%AD%EF%BC%8Cflag%20%3D%201%20%E6%97%B6%E5%AF%B9%E5%BA%94%E5%B0%8F%E9%A1%B6%E5%A0%86%EF%BC%8Cflag%20%3D%20-1%20%E6%97%B6%E5%AF%B9%E5%BA%94%E5%A4%A7%E9%A1%B6%E5%A0%86%0A%20%20%20%20%0A%20%20%20%20%23%20%E5%85%83%E7%B4%A0%E5%85%A5%E5%A0%86%0A%20%20%20%20heapq.heappush%28max_heap,%20flag%20*%201%29%0A%20%20%20%20heapq.heappush%28max_heap,%20flag%20*%203%29%0A%20%20%20%20heapq.heappush%28max_heap,%20flag%20*%202%29%0A%20%20%20%20heapq.heappush%28max_heap,%20flag%20*%205%29%0A%20%20%20%20heapq.heappush%28max_heap,%20flag%20*%204%29%0A%20%20%20%20%0A%20%20%20%20%23%20%E8%8E%B7%E5%8F%96%E5%A0%86%E9%A1%B6%E5%85%83%E7%B4%A0%0A%20%20%20%20peek%20%3D%20flag%20*%20max_heap%5B0%5D%20%23%205%0A%20%20%20%20%0A%20%20%20%20%23%20%E5%A0%86%E9%A1%B6%E5%85%83%E7%B4%A0%E5%87%BA%E5%A0%86%0A%20%20%20%20%23%20%E5%87%BA%E5%A0%86%E5%85%83%E7%B4%A0%E4%BC%9A%E5%BD%A2%E6%88%90%E4%B8%80%E4%B8%AA%E4%BB%8E%E5%A4%A7%E5%88%B0%E5%B0%8F%E7%9A%84%E5%BA%8F%E5%88%97%0A%20%20%20%20val%20%3D%20flag%20*%20heapq.heappop%28max_heap%29%20%23%205%0A%20%20%20%20val%20%3D%20flag%20*%20heapq.heappop%28max_heap%29%20%23%204%0A%20%20%20%20val%20%3D%20flag%20*%20heapq.heappop%28max_heap%29%20%23%203%0A%20%20%20%20val%20%3D%20flag%20*%20heapq.heappop%28max_heap%29%20%23%202%0A%20%20%20%20val%20%3D%20flag%20*%20heapq.heappop%28max_heap%29%20%23%201%0A%20%20%20%20%0A%20%20%20%20%23%20%E8%8E%B7%E5%8F%96%E5%A0%86%E5%A4%A7%E5%B0%8F%0A%20%20%20%20size%20%3D%20len%28max_heap%29%0A%20%20%20%20%0A%20%20%20%20%23%20%E5%88%A4%E6%96%AD%E5%A0%86%E6%98%AF%E5%90%A6%E4%B8%BA%E7%A9%BA%0A%20%20%20%20is_empty%20%3D%20not%20max_heap%0A%20%20%20%20%0A%20%20%20%20%23%20%E8%BE%93%E5%85%A5%E5%88%97%E8%A1%A8%E5%B9%B6%E5%BB%BA%E5%A0%86%0A%20%20%20%20min_heap%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%20%20%20%20heapq.heapify%28min_heap%29&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=3&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">Full Screen &gt;</a></div></p>
</details>
<h2 id="812-implementation-of-the-heap">8.1.2 &nbsp; Implementation of the Heap<a class="headerlink" href="#812-implementation-of-the-heap" title="Permanent link">&para;</a></h2>
<p>The following implementation is of a max heap. To convert it to a min heap, simply invert all size logic comparisons (for example, replace <span class="arithmatex">\(\geq\)</span> with <span class="arithmatex">\(\leq\)</span>). Interested readers are encouraged to implement this on their own.</p>
<p>The following implementation is for a max heap. To convert it to a min heap, simply reverse all comparison logic related to ordering (for example, replace <span class="arithmatex">\(\geq\)</span> with <span class="arithmatex">\(\leq\)</span>). Interested readers are encouraged to implement this on their own.</p>
<h3 id="1-heap-storage-and-representation">1. &nbsp; Heap Storage and Representation<a class="headerlink" href="#1-heap-storage-and-representation" title="Permanent link">&para;</a></h3>
<p>As mentioned in the "Binary Tree" chapter, complete binary trees are well-suited for array representation. Since heaps are a type of complete binary tree, <strong>we will use arrays to store heaps</strong>.</p>
<p>When representing a binary tree with an array, elements represent node values, and indexes represent node positions in the binary tree. <strong>Node pointers are implemented through index mapping formulas</strong>.</p>
<p>When representing a binary tree with an array, elements represent node values, and indexes represent node positions in the binary tree. <strong>Parent-child relationships are represented through index-mapping formulas</strong>.</p>
<p>As shown in Figure 8-2, given an index <span class="arithmatex">\(i\)</span>, the index of its left child is <span class="arithmatex">\(2i + 1\)</span>, the index of its right child is <span class="arithmatex">\(2i + 2\)</span>, and the index of its parent is <span class="arithmatex">\((i - 1) / 2\)</span> (floor division). When an index is out of bounds, it indicates a null node or that the node does not exist.</p>
<p><img alt="Representation and storage of heaps" class="animation-figure" src="../heap.assets/representation_of_heap.png" /></p>
<p align="center"> Figure 8-2 &nbsp; Representation and storage of heaps </p>
@@ -5225,8 +5225,8 @@
</div>
</div>
<h3 id="3-inserting-an-element-into-the-heap">3. &nbsp; Inserting an Element Into the Heap<a class="headerlink" href="#3-inserting-an-element-into-the-heap" title="Permanent link">&para;</a></h3>
<p>Given an element <code>val</code>, we first add it to the bottom of the heap. After addition, since <code>val</code> may be larger than other elements in the heap, the heap's property may be violated. <strong>Therefore, it's necessary to repair the path from the inserted node to the root node</strong>. This operation is called <u>heapify</u>.</p>
<p>Starting from the inserted node, <strong>perform heapify from bottom to top</strong>. As shown in Figure 8-3, we compare the inserted node with its parent node, and if the inserted node is larger, swap them. Then continue this operation, repairing nodes in the heap from bottom to top until we pass the root node or encounter a node that does not need swapping.</p>
<p>Given an element <code>val</code>, we first add it to the bottom of the heap. After insertion, because <code>val</code> may be larger than other elements in the heap, the heap property may be violated. <strong>Therefore, we need to restore the heap property along the path from the inserted node to the root</strong>. This operation is called <u>heapify</u>.</p>
<p>Starting from the inserted node, <strong>perform heapify from bottom to top</strong>. As shown in Figure 8-3, we compare the inserted node with its parent, and if the inserted node is larger, we swap them. We continue this process from bottom to top until we move past the root or reach a node that no longer needs to be swapped.</p>
<div class="tabbed-set tabbed-alternate" data-tabs="4:9"><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" /><input id="__tabbed_4_5" name="__tabbed_4" type="radio" /><input id="__tabbed_4_6" name="__tabbed_4" type="radio" /><input id="__tabbed_4_7" name="__tabbed_4" type="radio" /><input id="__tabbed_4_8" name="__tabbed_4" type="radio" /><input id="__tabbed_4_9" 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><label for="__tabbed_4_5">&lt;5&gt;</label><label for="__tabbed_4_6">&lt;6&gt;</label><label for="__tabbed_4_7">&lt;7&gt;</label><label for="__tabbed_4_8">&lt;8&gt;</label><label for="__tabbed_4_9">&lt;9&gt;</label></div>
<div class="tabbed-content">
<div class="tabbed-block">
@@ -6135,9 +6135,9 @@
</div>
<h2 id="813-common-applications-of-heaps">8.1.3 &nbsp; Common Applications of Heaps<a class="headerlink" href="#813-common-applications-of-heaps" title="Permanent link">&para;</a></h2>
<ul>
<li><strong>Priority queue</strong>: Heaps are typically the preferred data structure for implementing priority queues, with both enqueue and dequeue operations having a time complexity of <span class="arithmatex">\(O(\log n)\)</span>, and the heap construction operation having <span class="arithmatex">\(O(n)\)</span>, all of which are highly efficient.</li>
<li><strong>Priority queue</strong>: Heaps are typically the preferred data structure for implementing priority queues. The time complexity of both enqueue and dequeue operations is <span class="arithmatex">\(O(\log n)\)</span>, and heap construction has a time complexity of <span class="arithmatex">\(O(n)\)</span>, making these operations highly efficient.</li>
<li><strong>Heap sort</strong>: Given a set of data, we can build a heap with them and then continuously perform element removal operations to obtain sorted data. However, we usually use a more elegant approach to implement heap sort, as detailed in the "Heap Sort" chapter.</li>
<li><strong>Getting the largest <span class="arithmatex">\(k\)</span> elements</strong>: This is a classic algorithm problem and also a typical application, such as selecting the top 10 trending news for Weibo hot search, selecting the top 10 best-selling products, etc.</li>
<li><strong>Getting the largest <span class="arithmatex">\(k\)</span> elements</strong>: This is a classic algorithm problem and also a typical application, such as selecting the top 10 trending news items for Weibo Hot Search or the top 10 best-selling products.</li>
</ul>
<!-- Source file information -->
@@ -6185,7 +6185,7 @@ aria-label="Footer"
<a
href="../build_heap/"
class="md-footer__link md-footer__link--next"
aria-label="Next: 8.2 Building a Heap"
aria-label="Next: 8.2 Heap Construction Operation"
rel="next"
>
<div class="md-footer__title">
@@ -6193,7 +6193,7 @@ aria-label="Footer"
Next
</span>
<div class="md-ellipsis">
8.2 Building a Heap
8.2 Heap Construction Operation
</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>
@@ -1888,7 +1888,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="Heap" class="cover-image" src="../assets/covers/chapter_heap.jpg" /></p>
<div class="admonition abstract">
<p class="admonition-title">Abstract</p>
<p>Heaps are like mountain peaks, layered and undulating, each with its unique form.</p>
<p>Heaps are like mountain peaks, rising layer upon layer, each with a distinct shape.</p>
<p>The peaks rise and fall at varying heights, yet the tallest peak always catches the eye first.</p>
</div>
<h2 id="chapter-contents">Chapter contents<a class="headerlink" href="#chapter-contents" title="Permanent link">&para;</a></h2>
<ul>
<li><a href="heap/">8.1 &nbsp; Heap</a></li>
<li><a href="build_heap/">8.2 &nbsp; Building a Heap</a></li>
<li><a href="top_k/">8.3 &nbsp; Top-K Problem</a></li>
<li><a href="build_heap/">8.2 &nbsp; Heap Construction Operation</a></li>
<li><a href="top_k/">8.3 &nbsp; Top-k Problem</a></li>
<li><a href="summary/">8.4 &nbsp; Summary</a></li>
</ul>
+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>
@@ -1888,7 +1888,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
@@ -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,17 +4359,17 @@
<h1 id="84-summary">8.4 &nbsp; Summary<a class="headerlink" href="#84-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 heap is a complete binary tree that can be categorized as a max heap or min heap based on its property. The heap top element of a max heap (min heap) is the largest (smallest).</li>
<li>A priority queue is defined as a queue with priority sorting, typically implemented using heaps.</li>
<li>Common heap operations and their corresponding time complexities include: element insertion <span class="arithmatex">\(O(\log n)\)</span>, heap top element removal <span class="arithmatex">\(O(\log n)\)</span>, and accessing the heap top element <span class="arithmatex">\(O(1)\)</span>.</li>
<li>A heap is a complete binary tree. Depending on the property it satisfies, it can be classified as either a max heap or a min heap. The top element of a max heap (min heap) is the largest (smallest) element.</li>
<li>A priority queue is a queue in which elements are dequeued according to priority, and it is typically implemented using a heap.</li>
<li>Common heap operations and their corresponding time complexities include inserting an element <span class="arithmatex">\(O(\log n)\)</span>, removing the top element <span class="arithmatex">\(O(\log n)\)</span>, and accessing the top element <span class="arithmatex">\(O(1)\)</span>.</li>
<li>Complete binary trees are well-suited for array representation, so we typically use arrays to store heaps.</li>
<li>Heapify operations are used to maintain the heap property and are employed in both element insertion and removal operations.</li>
<li>The time complexity of building a heap with <span class="arithmatex">\(n\)</span> input elements can be optimized to <span class="arithmatex">\(O(n)\)</span>, which is highly efficient.</li>
<li>Top-k is a classic algorithm problem that can be efficiently solved using the heap data structure, with a time complexity of <span class="arithmatex">\(O(n \log k)\)</span>.</li>
<li>Building a heap from <span class="arithmatex">\(n\)</span> input elements can be optimized to <span class="arithmatex">\(O(n)\)</span>, which is highly efficient.</li>
<li>Top-k is a classic algorithmic problem that can be solved efficiently using a heap, with a time complexity of <span class="arithmatex">\(O(n \log k)\)</span>.</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>: Are the "heap" in data structures and the "heap" in memory management the same concept?</p>
<p>The two are not the same concept; they just happen to share the name "heap." The heap in computer system memory is part of dynamic memory allocation, where programs can use it to store data during runtime. Programs can request a certain amount of heap memory to store complex structures such as objects and arrays. When this data is no longer needed, the program needs to release this memory to prevent memory leaks. Compared to stack memory, heap memory management and usage require more caution, as improper use can lead to issues such as memory leaks and dangling pointers.</p>
<p><strong>Q</strong>: Does the term "heap" in data structures mean the same thing as "heap" in memory management?</p>
<p>They are not the same concept; they simply share the same name. In computer systems, the heap is part of dynamic memory allocation, and programs can use it to store data at runtime. A program can request a certain amount of heap memory to store complex structures such as objects and arrays. When the data is no longer needed, the program must release that memory to prevent memory leaks. Compared with stack memory, heap memory requires more careful management and use; improper handling can lead to problems such as memory leaks and dangling pointers.</p>
<!-- Source file information -->
@@ -4392,7 +4392,7 @@ aria-label="Footer"
<a
href="../top_k/"
class="md-footer__link md-footer__link--prev"
aria-label="Previous: 8.3 Top-K Problem"
aria-label="Previous: 8.3 Top-k Problem"
rel="prev"
>
<div class="md-footer__button md-icon">
@@ -4404,7 +4404,7 @@ aria-label="Footer"
Previous
</span>
<div class="md-ellipsis">
8.3 Top-K Problem
8.3 Top-k Problem
</div>
</div>
</a>
+31 -31
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>8.3 Top-K Problem - Hello Algo</title>
<title>8.3 Top-k Problem - Hello Algo</title>
@@ -154,7 +154,7 @@
<div class="md-header__topic" data-md-component="header-topic">
<span class="md-ellipsis">
8.3 &nbsp; Top-K Problem
8.3 &nbsp; Top-k Problem
</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>
@@ -1888,7 +1888,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
@@ -2146,7 +2146,7 @@
<span class="md-ellipsis">
8.3 Top-K Problem
8.3 Top-k Problem
@@ -2164,7 +2164,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
@@ -4378,15 +4378,15 @@
<!-- Page content -->
<h1 id="83-top-k-problem">8.3 &nbsp; Top-K Problem<a class="headerlink" href="#83-top-k-problem" title="Permanent link">&para;</a></h1>
<h1 id="83-top-k-problem">8.3 &nbsp; Top-k Problem<a class="headerlink" href="#83-top-k-problem" title="Permanent link">&para;</a></h1>
<div class="admonition question">
<p class="admonition-title">Question</p>
<p>Given an unordered array <code>nums</code> of length <span class="arithmatex">\(n\)</span>, return the largest <span class="arithmatex">\(k\)</span> elements in the array.</p>
</div>
<p>For this problem, we'll first introduce two solutions with relatively straightforward approaches, then introduce a more efficient heap-based solution.</p>
<p>For this problem, we will first introduce two relatively straightforward solutions, followed by a more efficient heap-based solution.</p>
<h2 id="831-method-1-iterative-selection">8.3.1 &nbsp; Method 1: Iterative Selection<a class="headerlink" href="#831-method-1-iterative-selection" title="Permanent link">&para;</a></h2>
<p>We can perform <span class="arithmatex">\(k\)</span> rounds of traversal as shown in Figure 8-6, extracting the <span class="arithmatex">\(1^{st}\)</span>, <span class="arithmatex">\(2^{nd}\)</span>, <span class="arithmatex">\(\dots\)</span>, <span class="arithmatex">\(k^{th}\)</span> largest elements in each round, with a time complexity of <span class="arithmatex">\(O(nk)\)</span>.</p>
<p>This method is only suitable when <span class="arithmatex">\(k \ll n\)</span>, because when <span class="arithmatex">\(k\)</span> is close to <span class="arithmatex">\(n\)</span>, the time complexity approaches <span class="arithmatex">\(O(n^2)\)</span>, which is very time-consuming.</p>
<p>This method is only suitable when <span class="arithmatex">\(k \ll n\)</span>, because when <span class="arithmatex">\(k\)</span> is close to <span class="arithmatex">\(n\)</span>, the time complexity approaches <span class="arithmatex">\(O(n^2)\)</span>, making it very inefficient.</p>
<p><img alt="Traversing to find the largest k elements" class="animation-figure" src="../top_k.assets/top_k_traversal.png" /></p>
<p align="center"> Figure 8-6 &nbsp; Traversing to find the largest k elements </p>
@@ -4396,12 +4396,12 @@
</div>
<h2 id="832-method-2-sorting">8.3.2 &nbsp; Method 2: Sorting<a class="headerlink" href="#832-method-2-sorting" title="Permanent link">&para;</a></h2>
<p>As shown in Figure 8-7, we can first sort the array <code>nums</code>, then return the rightmost <span class="arithmatex">\(k\)</span> elements, with a time complexity of <span class="arithmatex">\(O(n \log n)\)</span>.</p>
<p>Clearly, this method "overachieves" the task, as we only need to find the largest <span class="arithmatex">\(k\)</span> elements, without needing to sort the other elements.</p>
<p>Clearly, this method does more work than necessary, because we only need to find the largest <span class="arithmatex">\(k\)</span> elements rather than sort the other elements.</p>
<p><img alt="Sorting to find the largest k elements" class="animation-figure" src="../top_k.assets/top_k_sorting.png" /></p>
<p align="center"> Figure 8-7 &nbsp; Sorting to find the largest k elements </p>
<h2 id="833-method-3-heap">8.3.3 &nbsp; Method 3: Heap<a class="headerlink" href="#833-method-3-heap" title="Permanent link">&para;</a></h2>
<p>We can solve the Top-k problem more efficiently using heaps, with the process shown in Figure 8-8.</p>
<p>We can solve the Top-k problem more efficiently with a heap, as shown in Figure 8-8.</p>
<ol>
<li>Initialize a min heap, where the heap top element is the smallest.</li>
<li>First, insert the first <span class="arithmatex">\(k\)</span> elements of the array into the heap in sequence.</li>
@@ -4808,7 +4808,7 @@
</div>
</div>
<p>A total of <span class="arithmatex">\(n\)</span> rounds of heap insertions and removals are performed, with the heap's maximum length being <span class="arithmatex">\(k\)</span>, so the time complexity is <span class="arithmatex">\(O(n \log k)\)</span>. This method is very efficient; when <span class="arithmatex">\(k\)</span> is small, the time complexity approaches <span class="arithmatex">\(O(n)\)</span>; when <span class="arithmatex">\(k\)</span> is large, the time complexity does not exceed <span class="arithmatex">\(O(n \log n)\)</span>.</p>
<p>Additionally, this method is suitable for dynamic data stream scenarios. By continuously adding data, we can maintain the elements in the heap, thus achieving dynamic updates of the largest <span class="arithmatex">\(k\)</span> elements.</p>
<p>Additionally, this method is well suited to dynamic data streams. As new data arrives, we can continuously maintain the elements in the heap, enabling dynamic updates to the largest <span class="arithmatex">\(k\)</span> elements.</p>
<!-- Source file information -->
@@ -4831,7 +4831,7 @@ aria-label="Footer"
<a
href="../build_heap/"
class="md-footer__link md-footer__link--prev"
aria-label="Previous: 8.2 Building a Heap"
aria-label="Previous: 8.2 Heap Construction Operation"
rel="prev"
>
<div class="md-footer__button md-icon">
@@ -4843,7 +4843,7 @@ aria-label="Footer"
Previous
</span>
<div class="md-ellipsis">
8.2 Building a Heap
8.2 Heap Construction Operation
</div>
</div>
</a>