@@ -47,7 +47,7 @@ export default class Heap {
47
47
* @return {boolean }
48
48
*/
49
49
hasParent ( childIndex ) {
50
- return this . getParentIndex ( childIndex ) >= 0 ;
50
+ return childIndex > 0 ;
51
51
}
52
52
53
53
/**
@@ -144,17 +144,16 @@ export default class Heap {
144
144
145
145
/**
146
146
* @param {* } item
147
- * @param {Comparator } [customFindingComparator ]
147
+ * @param {Comparator } [customComparator ]
148
148
* @return {Heap }
149
149
*/
150
- remove ( item , customFindingComparator ) {
150
+ remove ( item , customComparator = this . compare ) {
151
151
// Find number of items to remove.
152
- const customComparator = customFindingComparator || this . compare ;
153
152
const numberOfItemsToRemove = this . find ( item , customComparator ) . length ;
154
153
155
154
for ( let iteration = 0 ; iteration < numberOfItemsToRemove ; iteration += 1 ) {
156
155
// We need to find item index to remove each time after removal since
157
- // indices are being change after each heapify process.
156
+ // indices are being changed after each heapify process.
158
157
const indexToRemove = this . find ( item , customComparator ) . pop ( ) ;
159
158
160
159
// If we need to remove last child in the heap then just remove it.
@@ -165,16 +164,14 @@ export default class Heap {
165
164
// Move last element in heap to the vacant (removed) position.
166
165
this . heapContainer [ indexToRemove ] = this . heapContainer . pop ( ) ;
167
166
168
- // Get parent.
169
- const parentItem = this . hasParent ( indexToRemove ) ? this . parent ( indexToRemove ) : null ;
170
- const leftChild = this . hasLeftChild ( indexToRemove ) ? this . leftChild ( indexToRemove ) : null ;
167
+ const parentItem = this . parent ( indexToRemove ) ;
171
168
172
- // If there is no parent or parent is in incorrect order with the node
169
+ // If there is no parent or parent is in correct order with the node
173
170
// we're going to delete then heapify down. Otherwise heapify up.
174
171
if (
175
- leftChild !== null
172
+ this . hasLeftChild ( indexToRemove )
176
173
&& (
177
- parentItem === null
174
+ parentItem == null
178
175
|| this . pairIsInCorrectOrder ( parentItem , this . heapContainer [ indexToRemove ] )
179
176
)
180
177
) {
@@ -193,12 +190,11 @@ export default class Heap {
193
190
* @param {Comparator } [customComparator]
194
191
* @return {Number[] }
195
192
*/
196
- find ( item , customComparator ) {
193
+ find ( item , customComparator = this . compare ) {
197
194
const foundItemIndices = [ ] ;
198
- const comparator = customComparator || this . compare ;
199
195
200
196
for ( let itemIndex = 0 ; itemIndex < this . heapContainer . length ; itemIndex += 1 ) {
201
- if ( comparator . equal ( item , this . heapContainer [ itemIndex ] ) ) {
197
+ if ( customComparator . equal ( item , this . heapContainer [ itemIndex ] ) ) {
202
198
foundItemIndices . push ( itemIndex ) ;
203
199
}
204
200
}
@@ -224,9 +220,9 @@ export default class Heap {
224
220
* @param {number } [customStartIndex]
225
221
*/
226
222
heapifyUp ( customStartIndex ) {
227
- // Take last element (last in array or the bottom left in a tree) in
228
- // a heap container and lift him up until we find the parent element
229
- // that is less then the current new one .
223
+ // Take the last element (last in array or the bottom left in a tree)
224
+ // in the heap container and lift it up until it is in the correct
225
+ // order with respect to its parent element .
230
226
let currentIndex = customStartIndex || this . heapContainer . length - 1 ;
231
227
232
228
while (
@@ -241,10 +237,11 @@ export default class Heap {
241
237
/**
242
238
* @param {number } [customStartIndex]
243
239
*/
244
- heapifyDown ( customStartIndex ) {
245
- // Compare the root element to its children and swap root with the smallest
246
- // of children. Do the same for next children after swap.
247
- let currentIndex = customStartIndex || 0 ;
240
+ heapifyDown ( customStartIndex = 0 ) {
241
+ // Compare the parent element to its children and swap parent with the appropriate
242
+ // child (smallest child for MinHeap, largest child for MaxHeap).
243
+ // Do the same for next children after swap.
244
+ let currentIndex = customStartIndex ;
248
245
let nextIndex = null ;
249
246
250
247
while ( this . hasLeftChild ( currentIndex ) ) {
0 commit comments