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