Additional heap/heapsort flashcards.

c-declarations
Joshua Potter 2024-04-29 09:11:05 -06:00
parent d04bd101ad
commit b800dc2ab8
4 changed files with 294 additions and 2 deletions

View File

@ -116,7 +116,9 @@
"perfect-tree.png", "perfect-tree.png",
"non-complete-tree.png", "non-complete-tree.png",
"max-heap-tree.png", "max-heap-tree.png",
"max-heap-array.png" "max-heap-array.png",
"max-heapify-1.png",
"max-heapify-2.png"
], ],
"File Hashes": { "File Hashes": {
"algorithms/index.md": "3ac071354e55242919cc574eb43de6f8", "algorithms/index.md": "3ac071354e55242919cc574eb43de6f8",
@ -373,7 +375,7 @@
"_journal/2024-04/2024-04-23.md": "20514052da91b06b979cacb3da758837", "_journal/2024-04/2024-04-23.md": "20514052da91b06b979cacb3da758837",
"_journal/2024-04-25.md": "10c98531cb90a6bc940ea7ae3342f98b", "_journal/2024-04-25.md": "10c98531cb90a6bc940ea7ae3342f98b",
"_journal/2024-04/2024-04-24.md": "4cb04e0dea56e0b471fc0e428471a390", "_journal/2024-04/2024-04-24.md": "4cb04e0dea56e0b471fc0e428471a390",
"algorithms/heaps.md": "b12c70ec85e514ce912821d133d116d4", "algorithms/heaps.md": "ed37002a7600a05794f668e092265522",
"_journal/2024-04-26.md": "3ce37236a9e09e74b547a4f7231df5f0", "_journal/2024-04-26.md": "3ce37236a9e09e74b547a4f7231df5f0",
"_journal/2024-04/2024-04-25.md": "5a81123af29f8ebf0a0d28f820a3a52e", "_journal/2024-04/2024-04-25.md": "5a81123af29f8ebf0a0d28f820a3a52e",
"_journal/2024-04-28.md": "46726bf76a594b987c63ba8b9b6d13d3", "_journal/2024-04-28.md": "46726bf76a594b987c63ba8b9b6d13d3",

View File

@ -11,6 +11,8 @@ tags:
The **binary heap** data structure is an array object that can be viewed as a [[trees#Positional Trees|complete binary tree]]. The **binary heap** data structure is an array object that can be viewed as a [[trees#Positional Trees|complete binary tree]].
The primary function used to maintain the max-heap property is `MAX_HEAPIFY_DOWN`. This function assumes the left and right- subtrees at a given node are max heaps but that the current node may be smaller than its children. An analagous function and assumptions exist for `MIN_HEAPIFY_DOWN`.
%%ANKI %%ANKI
Cloze Cloze
A binary heap is an {array} that can be viewed as a {binary tree}. A binary heap is an {array} that can be viewed as a {binary tree}.
@ -147,6 +149,294 @@ Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (
<!--ID: 1714356546616--> <!--ID: 1714356546616-->
END%% END%%
%%ANKI
Basic
What preconditions must hold before invoking `MAX_HEAPIFY_DOWN` on a node?
Back: The node's left and right subtrees must be max-heaps.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1714399155389-->
END%%
%%ANKI
Basic
When is `MAX_HEAPIFY_DOWN` a no-op?
Back: When the current node is already larger than both its children.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1714399155419-->
END%%
%%ANKI
Basic
If not a no-op, which child should `MAX_HEAPIFY_DOWN` swap its current value with?
Back: The larger of its two children.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1714399155425-->
END%%
%%ANKI
Basic
Given a heap of height $h$, *why* is `MAX_HEAPIFY_DOWN`'s worst case runtime $O(h)$?
Back: Each invocation may violate the max-heap property of a child node.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1714399155432-->
END%%
%%ANKI
Basic
What is the runtime of `MAX_HEAPIFY_DOWN`?
Back: $O(h)$ where $h$ is the height of the heap.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1714403425256-->
END%%
%%ANKI
Basic
What is the result of calling `MAX_HEAPIFY_DOWN` on the highlighted node?
![[max-heapify-1.png]]
Back:
![[max-heapify-2.png]]
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1714399155438-->
END%%
%%ANKI
Basic
What is the runtime of `MIN_HEAPIFY_DOWN`?
Back: $O(h)$ where $h$ is the height of the heap.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1714403425286-->
END%%
%%ANKI
Basic
What preconditions must hold before invoking `MIN_HEAPIFY_DOWN` on a node?
Back: The node's left and right subtrees must be min-heaps.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1714399155443-->
END%%
%%ANKI
Basic
When is `Min_HEAPIFY_DOWN` a no-op?
Back: When the current node is already smaller than both its children.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1714399155448-->
END%%
%%ANKI
Basic
If not a no-op, which child should `MIN_HEAPIFY_DOWN` swap its current value with?
Back: The smaller of its two children.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1714399155453-->
END%%
%%ANKI
Basic
Given a heap of height $h$, *why* is `MIN_HEAPIFY_DOWN`'s worst case runtime $O(h)$?
Back: Each invocation may violate the min-heap property of a child node.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1714399155459-->
END%%
%%ANKI
Basic
What does the "heapify" operation of a heap refer to?
Back: Repeatedly swapping a node's value with a child until the heap property is achieved.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1714399155469-->
END%%
%%ANKI
Basic
How many internal nodes does a binary heap of size $n$ have?
Back: $\lfloor n / 2 \rfloor$
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1714403425292-->
END%%
%%ANKI
Basic
How many internal nodes precede the first external node of a heap of size $n$?
Back: $\lfloor n / 2 \rfloor$
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1714403425296-->
END%%
%%ANKI
Basic
What is the height of a binary heap?
Back: The height of the heap's root when viewed as a complete binary tree.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1714403425300-->
END%%
%%ANKI
Basic
What is the input of `MAX_HEAPIFY_DOWN`?
Back: The index of a node in the target heap.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1714403425304-->
END%%
%%ANKI
Basic
What is the input of `BUILD_MAX_HEAP`?
Back: An array.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1714403425309-->
END%%
%%ANKI
Basic
What is the runtime of `BUILD_MAX_HEAP` on an array of $n$ elements?
Back: $O(n)$
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1714403425314-->
END%%
%%ANKI
Basic
How is the `BUILD_MAX_HEAP` function usually implemented?
Back: As calling heapify on each internal node.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1714403425320-->
END%%
%%ANKI
Basic
Which node does `BUILD_MAX_HEAP` start iterating on?
Back: The last internal node.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1714403425326-->
END%%
%%ANKI
Basic
Why does `BUILD_MAX_HEAP` "ignore" the external nodes of a heap?
Back: Because they are already max-heaps of size $1$.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1714403425331-->
END%%
%%ANKI
Basic
Given heap $H[0{..}n{-}1]$, what is `BUILD_MAX_HEAP`'s loop invariant?
Back: Each node in $H[i{+}1{..}n{-}1]$ is the root of a max-heap.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1714403425336-->
END%%
%%ANKI
Basic
What is initialization of `BUILD_MAX_HEAP`'s loop invariant?
Back: Every external node is the root of a max-heap.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1714403425340-->
END%%
%%ANKI
Basic
What is maintenance of `BUILD_MAX_HEAP`'s loop invariant?
Back: Calling `MAX_HEAPIFY_DOWN` maintains the max-heap property of the current node.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1714403425344-->
END%%
%%ANKI
Basic
In pseudocode, how is `BUILD_MAX_HEAP` implemented?
Back:
```c
void BUILD_MAX_HEAP(int n, int H[static n]) {
for (int i = (n / 2) - 1; i >= 0; --i) {
MAX_HEAPIFY_DOWN(i, H);
}
}
```
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1714403425348-->
END%%
%%ANKI
Basic
What is the input of `BUILD_MIN_HEAP`?
Back: An array.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1714403425351-->
END%%
%%ANKI
Basic
What is the runtime of `BUILD_MIN_HEAP` on an array of $n$ elements?
Back: $O(n)$
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1714403425355-->
END%%
%%ANKI
Basic
How is the `BUILD_MIN_HEAP` function usually implemented?
Back: As calling heapify on each internal node.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1714403425359-->
END%%
%%ANKI
Basic
Which node does `BUILD_MIN_HEAP` start iterating on?
Back: The last internal node.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1714403425363-->
END%%
%%ANKI
Basic
Why does `BUILD_MIN_HEAP` "ignore" the external nodes of a heap?
Back: Because they are already max-heaps of size $1$.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1714403425367-->
END%%
%%ANKI
Basic
Given heap $H[0{..}n{-}1]$, what is `BUILD_MIN_HEAP`'s loop invariant?
Back: Each node in $H[i{+}1{..}n{-}1]$ is the root of a min-heap.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1714403425372-->
END%%
%%ANKI
Basic
What is initialization of `BUILD_MIN_HEAP`'s loop invariant?
Back: Every external node is the root of a min-heap.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1714403425376-->
END%%
%%ANKI
Basic
What is maintenance of `BUILD_MIN_HEAP`'s loop invariant?
Back: Calling `MIN_HEAPIFY_DOWN` maintains the min-heap property of the current node.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1714403425381-->
END%%
%%ANKI
Basic
In pseudocode, how is `BUILD_MIN_HEAP` implemented?
Back:
```c
void BUILD_MIN_HEAP(int n, int H[static n]) {
for (int i = (n / 2) - 1; i >= 0; --i) {
MIN_HEAPIFY_DOWN(i, H);
}
}
```
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1714403425386-->
END%%
## Bibliography ## Bibliography
* Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022). * Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB