This commit is contained in:
Oleksii Trekhleb 2018-08-27 16:30:18 +03:00
parent bdf8a174eb
commit b1f31cd067
2 changed files with 23 additions and 1 deletions

View File

@ -175,7 +175,7 @@ export default class Heap {
leftChild !== null leftChild !== null
&& ( && (
parentItem === null parentItem === null
|| !this.pairIsInCorrectOrder(parentItem, this.heapContainer[indexToRemove]) || this.pairIsInCorrectOrder(parentItem, this.heapContainer[indexToRemove])
) )
) { ) {
this.heapifyDown(indexToRemove); this.heapifyDown(indexToRemove);

View File

@ -169,4 +169,26 @@ describe('MinHeap', () => {
minHeap.remove('hey', comparator); minHeap.remove('hey', comparator);
expect(minHeap.toString()).toBe('a,bb,dddd'); expect(minHeap.toString()).toBe('a,bb,dddd');
}); });
it('should remove values from heap and correctly re-order the tree', () => {
const minHeap = new MinHeap();
minHeap.add(1);
minHeap.add(2);
minHeap.add(3);
minHeap.add(4);
minHeap.add(5);
minHeap.add(6);
minHeap.add(7);
minHeap.add(8);
minHeap.add(9);
expect(minHeap.toString()).toBe('1,2,3,4,5,6,7,8,9');
minHeap.remove(2);
expect(minHeap.toString()).toBe('1,4,3,8,5,6,7,9');
minHeap.remove(4);
expect(minHeap.toString()).toBe('1,5,3,8,9,6,7');
});
}); });