File tree 2 files changed +23
-1
lines changed
2 files changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -141,8 +141,8 @@ export default class MinHeap {
141
141
*/
142
142
remove ( item , customFindingComparator ) {
143
143
// Find number of items to remove.
144
- const numberOfItemsToRemove = this . find ( item ) . length ;
145
144
const customComparator = customFindingComparator || this . compare ;
145
+ const numberOfItemsToRemove = this . find ( item , customComparator ) . length ;
146
146
147
147
for ( let iteration = 0 ; iteration < numberOfItemsToRemove ; iteration += 1 ) {
148
148
// We need to find item index to remove each time after removal since
Original file line number Diff line number Diff line change 1
1
import MinHeap from '../MinHeap' ;
2
+ import Comparator from '../../../utils/comparator/Comparator' ;
2
3
3
4
describe ( 'MinHeap' , ( ) => {
4
5
it ( 'should create an empty min heap' , ( ) => {
@@ -147,4 +148,25 @@ describe('MinHeap', () => {
147
148
expect ( minHeap . remove ( 3 ) . toString ( ) ) . toEqual ( '4' ) ;
148
149
expect ( minHeap . remove ( 4 ) . toString ( ) ) . toEqual ( '' ) ;
149
150
} ) ;
151
+
152
+ it ( 'should be possible to remove items from heap with custom finding comparator' , ( ) => {
153
+ const minHeap = new MinHeap ( ) ;
154
+ minHeap . add ( 'dddd' ) ;
155
+ minHeap . add ( 'ccc' ) ;
156
+ minHeap . add ( 'bb' ) ;
157
+ minHeap . add ( 'a' ) ;
158
+
159
+ expect ( minHeap . toString ( ) ) . toBe ( 'a,bb,ccc,dddd' ) ;
160
+
161
+ const comparator = new Comparator ( ( a , b ) => {
162
+ if ( a . length === b . length ) {
163
+ return 0 ;
164
+ }
165
+
166
+ return a . length < b . length ? - 1 : 1 ;
167
+ } ) ;
168
+
169
+ minHeap . remove ( 'hey' , comparator ) ;
170
+ expect ( minHeap . toString ( ) ) . toBe ( 'a,bb,dddd' ) ;
171
+ } ) ;
150
172
} ) ;
You can’t perform that action at this time.
0 commit comments