You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- renamed "elements" to "nodes" to clarify the difference between array and heap. This also unifies code and comments, which were sometimes referring to the array as "nodes".
- made nodes publicly readable but only settable within the heap itself. To change the nodes/operate on the heap, the provided functions should be used, to guarantee the heaps structure.
- renamed isOrderedBefore to orderCriteria. The variable name isOrderedBefore did not clearly explain what the variable is for/what the closure stored within does
- added method to get node at a specific index
- replace method should not force the replaced value to be higher/lower than its parent. Instead replace should remove the node to be replaced and insert the given value as a new one in order to preserve the heap's structure. Asserting crashes the whole program.
- added @discardableResult to removeAt. If remove is @discardableResult removeAt should be as well in for consistency.
- replaced while true in shiftDown with a recursive call. while true is bad programming style.
- simplified indexOf method
- added a removeNode method for heaps with equatable data type
- improved, adjusted and added descriptions
- made functions private where fileprivate was not needed
- added tests for new functions and tested everything
- updated README where necessary (to conform to new code, only a few changes)
@@ -38,7 +38,7 @@ A heap is not a replacement for a binary search tree, and there are similarities
38
38
39
39
**Balancing.** A binary search tree must be "balanced" so that most operations have **O(log n)** performance. You can either insert and delete your data in a random order or use something like an [AVL tree](../AVL%20Tree/) or [red-black tree](../Red-Black%20Tree/), but with heaps we don't actually need the entire tree to be sorted. We just want the heap property to be fulfilled, so balancing isn't an issue. Because of the way the heap is structured, heaps can guarantee **O(log n)** performance.
40
40
41
-
**Searching.** Whereas searching is fast in a binary tree, it is slow in a heap. Searching isn't a top priority in a heap since the purpose of a heap is to put the largest (or smallest) node at the front and to allow relatively fast inserts and deletes.
41
+
**Searching.** Whereas searching is fast in a binary tree, it is slow in a heap. Searching isn't a top priority in a heap since the purpose of a heap is to put the largest (or smallest) node at the front and to allow relatively fast inserts and deletes.
42
42
43
43
## The tree inside an array
44
44
@@ -148,7 +148,7 @@ There are two primitive operations necessary to make sure the heap is a valid ma
148
148
149
149
Shifting up or down is a recursive procedure that takes **O(log n)** time.
150
150
151
-
Here are other operations that are built on primitive operations:
151
+
Here are other operations that are built on primitive operations:
152
152
153
153
-`insert(value)`: Adds the new element to the end of the heap and then uses `shiftUp()` to fix the heap.
154
154
@@ -190,7 +190,7 @@ The `(16)` was added to the first available space on the last row.
190
190
191
191
Unfortunately, the heap property is no longer satisfied because `(2)` is above `(16)`, and we want higher numbers above lower numbers. (This is a max-heap.)
192
192
193
-
To restore the heap property, we swap `(16)` and `(2)`.
193
+
To restore the heap property, we swap `(16)` and `(2)`.
194
194
195
195

196
196
@@ -214,7 +214,7 @@ What happens to the empty spot at the top?
214
214
215
215

216
216
217
-
When inserting, we put the new value at the end of the array. Here, we do the opposite: we take the last object we have, stick it up on top of the tree, and restore the heap property.
217
+
When inserting, we put the new value at the end of the array. Here, we do the opposite: we take the last object we have, stick it up on top of the tree, and restore the heap property.
218
218
219
219

220
220
@@ -226,7 +226,7 @@ Keep shifting down until the node does not have any children or it is larger tha
226
226
227
227

228
228
229
-
The time required for shifting all the way down is proportional to the height of the tree which takes **O(log n)** time.
229
+
The time required for shifting all the way down is proportional to the height of the tree which takes **O(log n)** time.
230
230
231
231
> **Note:**`shiftUp()` and `shiftDown()` can only fix one out-of-place element at a time. If there are multiple elements in the wrong place, you need to call these functions once for each of those elements.
0 commit comments