Skip to content

Commit b1f31cd

Browse files
committedAug 27, 2018
Fix issue trekhleb#179.
1 parent bdf8a17 commit b1f31cd

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed
 

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ export default class Heap {
175175
leftChild !== null
176176
&& (
177177
parentItem === null
178-
|| !this.pairIsInCorrectOrder(parentItem, this.heapContainer[indexToRemove])
178+
|| this.pairIsInCorrectOrder(parentItem, this.heapContainer[indexToRemove])
179179
)
180180
) {
181181
this.heapifyDown(indexToRemove);

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

+22
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,26 @@ describe('MinHeap', () => {
169169
minHeap.remove('hey', comparator);
170170
expect(minHeap.toString()).toBe('a,bb,dddd');
171171
});
172+
173+
it('should remove values from heap and correctly re-order the tree', () => {
174+
const minHeap = new MinHeap();
175+
176+
minHeap.add(1);
177+
minHeap.add(2);
178+
minHeap.add(3);
179+
minHeap.add(4);
180+
minHeap.add(5);
181+
minHeap.add(6);
182+
minHeap.add(7);
183+
minHeap.add(8);
184+
minHeap.add(9);
185+
186+
expect(minHeap.toString()).toBe('1,2,3,4,5,6,7,8,9');
187+
188+
minHeap.remove(2);
189+
expect(minHeap.toString()).toBe('1,4,3,8,5,6,7,9');
190+
191+
minHeap.remove(4);
192+
expect(minHeap.toString()).toBe('1,5,3,8,9,6,7');
193+
});
172194
});

0 commit comments

Comments
 (0)
Please sign in to comment.