From a48c55aa743c071df2b1cb64b58c89efa1d012c1 Mon Sep 17 00:00:00 2001 From: Renan Pimentel Date: Tue, 6 Oct 2026 10:16:30 -0300 Subject: [PATCH] test(heap): cover heapify up when removing an item The heapifyUp branch of Heap.remove could be deleted without any test failing: no existing case moves a smaller item into the removed slot below a larger parent. Add one that observes the heap right after the removal. --- .../heap/__test__/MinHeap.test.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/data-structures/heap/__test__/MinHeap.test.js b/src/data-structures/heap/__test__/MinHeap.test.js index b963a41ff8..b9fe854819 100644 --- a/src/data-structures/heap/__test__/MinHeap.test.js +++ b/src/data-structures/heap/__test__/MinHeap.test.js @@ -191,4 +191,20 @@ describe('MinHeap', () => { minHeap.remove(4); expect(minHeap.toString()).toBe('1,5,3,8,9,6,7'); }); + it('should heapify up when the item moved into the removed position is smaller than its parent', () => { + const minHeap = new MinHeap(); + + minHeap.add(1); + minHeap.add(5); + minHeap.add(2); + minHeap.add(6); + minHeap.add(7); + minHeap.add(3); + minHeap.add(4); + + expect(minHeap.toString()).toBe('1,5,2,6,7,3,4'); + + minHeap.remove(6); + expect(minHeap.toString()).toBe('1,4,2,5,7,3'); + }); });