@@ -47,7 +47,7 @@ export default class Heap {
47
47
* @return {boolean }
48
48
*/
49
49
hasParent ( childIndex ) {
50
- return childIndex > 0 ;
50
+ return this . getParentIndex ( childIndex ) >= 0 ;
51
51
}
52
52
53
53
/**
@@ -144,17 +144,17 @@ export default class Heap {
144
144
145
145
/**
146
146
* @param {* } item
147
- * @param {Comparator } [customComparator ]
147
+ * @param {Comparator } [comparator ]
148
148
* @return {Heap }
149
149
*/
150
- remove ( item , customComparator = this . compare ) {
150
+ remove ( item , comparator = this . compare ) {
151
151
// Find number of items to remove.
152
- const numberOfItemsToRemove = this . find ( item , customComparator ) . length ;
152
+ const numberOfItemsToRemove = this . find ( item , comparator ) . length ;
153
153
154
154
for ( let iteration = 0 ; iteration < numberOfItemsToRemove ; iteration += 1 ) {
155
155
// We need to find item index to remove each time after removal since
156
156
// indices are being changed after each heapify process.
157
- const indexToRemove = this . find ( item , customComparator ) . pop ( ) ;
157
+ const indexToRemove = this . find ( item , comparator ) . pop ( ) ;
158
158
159
159
// If we need to remove last child in the heap then just remove it.
160
160
// There is no need to heapify the heap afterwards.
@@ -164,14 +164,15 @@ export default class Heap {
164
164
// Move last element in heap to the vacant (removed) position.
165
165
this . heapContainer [ indexToRemove ] = this . heapContainer . pop ( ) ;
166
166
167
+ // Get parent.
167
168
const parentItem = this . parent ( indexToRemove ) ;
168
169
169
170
// If there is no parent or parent is in correct order with the node
170
171
// we're going to delete then heapify down. Otherwise heapify up.
171
172
if (
172
173
this . hasLeftChild ( indexToRemove )
173
174
&& (
174
- parentItem == null
175
+ ! parentItem
175
176
|| this . pairIsInCorrectOrder ( parentItem , this . heapContainer [ indexToRemove ] )
176
177
)
177
178
) {
@@ -187,14 +188,14 @@ export default class Heap {
187
188
188
189
/**
189
190
* @param {* } item
190
- * @param {Comparator } [customComparator ]
191
+ * @param {Comparator } [comparator ]
191
192
* @return {Number[] }
192
193
*/
193
- find ( item , customComparator = this . compare ) {
194
+ find ( item , comparator = this . compare ) {
194
195
const foundItemIndices = [ ] ;
195
196
196
197
for ( let itemIndex = 0 ; itemIndex < this . heapContainer . length ; itemIndex += 1 ) {
197
- if ( customComparator . equal ( item , this . heapContainer [ itemIndex ] ) ) {
198
+ if ( comparator . equal ( item , this . heapContainer [ itemIndex ] ) ) {
198
199
foundItemIndices . push ( itemIndex ) ;
199
200
}
200
201
}
0 commit comments