Skip to content

Commit 1eea663

Browse files
authored
[Heap] Update implementation details section in docs (apple#84)
- Use a persistent DOI-based link rather than linking to ephemeral course material. - Replace the example heap (borrowed from Wikipedia) with a better one.
1 parent c04384a commit 1eea663

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

Documentation/Heap.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -134,29 +134,30 @@ The above graph was generated in release mode on a MacBook Pro (16-inch, 2019) w
134134

135135
## Implementation Details
136136

137-
The implementation is based off [Atkinson et al. Min-Max Heaps and Generalized Priority Queues (1986)](http://akira.ruc.dk/~keld/teaching/algoritmedesign_f03/Artikler/02/Atkinson86.pdf).
137+
The implementation is based on the min-max heap data structure as introduced by [Atkinson et al. 1986].
138138

139-
In a min-max heap, each node at an even level in the tree is less than all its descendants, while each node at an odd level in the tree is greater than all of its descendants.
139+
[Atkinson et al. 1986]: https://doi.org/10.1145/6617.6621
140140

141-
["A min-max heap is a complete binary tree data structure."](https://en.wikipedia.org/wiki/Min-max_heap) This means that all the levels in the tree are filled except the last and that they are filled from left to right. The tree can be stored in an array:
141+
Min-max heaps are complete binary trees represented implicitly as an array of their elements. Each node at an even level in the tree is less than or equal to all its descendants, while each node at an odd level in the tree is greater or equal to all of its descendants.
142142

143143
```
144144
// Min-max heap:
145-
level 0: 8
146-
level 1: 71 41
147-
level 2: 31 10 11 16
148-
level 3: 46 51 31 21 13
145+
level 0 (min): ┌────── A ──────┐
146+
level 1 (max): ┌── J ──┐ ┌── G ──┐
147+
level 2 (min): ┌ D ┐ ┌ B F C
148+
level 3 (max): I E H
149149
150150
// Array representation:
151-
[8, 71, 41, 31, 10, 11, 16, 46, 51, 31, 21, 13]
151+
["A", "J", "G", "D", "B", "F", "C", "I", "E", "H"]
152152
```
153153

154-
**Heap property**: Each element in an even level in the tree is less than all its descendants; each element in an odd level in the tree is greater than all its descendants. _Mutations to the heap **must** maintain this heap property._
154+
By the min-max property above, the root node is an on even level, so its value ("A" in this example) must be the minimum of the entire heap. Its two children are on an odd level, so they hold the maximum value for their respective subtrees; it follows that one of them holds the maximum value for the whole tree -- in this case, "J". Accessing the minimum and maximum values in the heap can therefore be done in O(1) comparisons.
155155

156-
The levels start at 0 (even), so the smallest element in the tree is at the root. In the example above, the root of the tree is 8, which is the smallest element. The next level in the tree (containing 71 and 41) is a max level, so those elements are greater than all their respective descendants. Because this is the first max level in the tree, the largest element in the tree is one of the two elements (71).
157-
158-
Note that the comparisons only take into account descendants — it is possible, for example, for elements at a lower level to be larger than elements in a different branch that are higher up the tree. In the example above, 41 isn't the second-largest element in the tree, but _it is_ the largest element in the right branch of the root node.
156+
Mutations of the heap (insertions, removals) must ensure that items remain arranged in a way that maintain the min-max property. Inserting a single new element or removing the current minimum/maximum can be done by rearranging items on a single path in the tree; accordingly, these operations execute O(log(`count`)) comparisons/swaps.
159157

160158
---
161159

162-
M.D. Atkinson, J.-R. Sack, N. Santoro, T. Strothotte. October 1986. Min-Max Heaps and Generalized Priority Queues. Communications of the ACM. 29(10):996-1000.
160+
M.D. Atkinson, J.-R. Sack, N. Santoro, T. Strothotte.
161+
"Min-Max Heaps and Generalized Priority Queues."
162+
*Communications of the ACM*, vol. 29, no. 10, Oct. 1986., pp. 996-1000,
163+
doi:[10.1145/6617.6621](https://doi.org/10.1145/6617.6621)

Sources/PriorityQueueModule/Heap.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111

1212
/// A [Min-Max Heap](https://en.wikipedia.org/wiki/Min-max_heap) data structure.
1313
///
14-
/// In a min-max heap, each node at an even level in the tree is less than all
15-
/// its descendants, while each node at an odd level in the tree is greater than
16-
/// all of its descendants.
14+
/// In a min-max heap, each node at an even level in the tree is less than or
15+
/// equal to all its descendants, while each node at an odd level in the tree is
16+
/// greater than or equal to all of its descendants.
1717
///
18-
/// The implementation is based on [Atkinson 1986]:
18+
/// The implementation is based on [Atkinson et al. 1986]:
1919
///
20-
/// [Atkinson 1986]: https://doi.org/10.1145/6617.6621
20+
/// [Atkinson et al. 1986]: https://doi.org/10.1145/6617.6621
2121
///
2222
/// M.D. Atkinson, J.-R. Sack, N. Santoro, T. Strothotte.
2323
/// "Min-Max Heaps and Generalized Priority Queues."

0 commit comments

Comments
 (0)