Skip to content

Commit a1060c2

Browse files
committed
2 parents 6c892c4 + 38688f2 commit a1060c2

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

‎src/data-structures/heap/MinHeap.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ export default class MinHeap{
141141
*/
142142
remove(item,customFindingComparator){
143143
// Find number of items to remove.
144-
constnumberOfItemsToRemove=this.find(item).length;
145144
constcustomComparator=customFindingComparator||this.compare;
145+
constnumberOfItemsToRemove=this.find(item,customComparator).length;
146146

147147
for(letiteration=0;iteration<numberOfItemsToRemove;iteration+=1){
148148
// We need to find item index to remove each time after removal since

‎src/data-structures/heap/__test__/MinHeap.test.js‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
importMinHeapfrom'../MinHeap';
2+
importComparatorfrom'../../../utils/comparator/Comparator';
23

34
describe('MinHeap',()=>{
45
it('should create an empty min heap',()=>{
@@ -147,4 +148,25 @@ describe('MinHeap', () =>{
147148
expect(minHeap.remove(3).toString()).toEqual('4');
148149
expect(minHeap.remove(4).toString()).toEqual('');
149150
});
151+
152+
it('should be possible to remove items from heap with custom finding comparator',()=>{
153+
constminHeap=newMinHeap();
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+
constcomparator=newComparator((a,b)=>{
162+
if(a.length===b.length){
163+
return0;
164+
}
165+
166+
returna.length<b.length ? -1 : 1;
167+
});
168+
169+
minHeap.remove('hey',comparator);
170+
expect(minHeap.toString()).toBe('a,bb,dddd');
171+
});
150172
});

0 commit comments

Comments
(0)