diff --git a/data_structures/heap/max_heap.ts b/data_structures/heap/max_heap.ts index e5307bfc..34f2068f 100644 --- a/data_structures/heap/max_heap.ts +++ b/data_structures/heap/max_heap.ts @@ -32,7 +32,7 @@ export class MaxHeap extends Heap { leftChildIndex: number, rightChildIndex: number ): number { - return (this.heap[leftChildIndex] || -Infinity) > + return this.heap[leftChildIndex] > (this.heap[rightChildIndex] || -Infinity) ? leftChildIndex : rightChildIndex; diff --git a/data_structures/heap/min_heap.ts b/data_structures/heap/min_heap.ts index fa50a0a0..ec71572a 100644 --- a/data_structures/heap/min_heap.ts +++ b/data_structures/heap/min_heap.ts @@ -32,8 +32,8 @@ export class MinHeap extends Heap { leftChildIndex: number, rightChildIndex: number ): number { - return (this.heap[leftChildIndex] || -Infinity) < - (this.heap[rightChildIndex] || -Infinity) + return this.heap[leftChildIndex] < + (this.heap[rightChildIndex] || Infinity) ? leftChildIndex : rightChildIndex; } diff --git a/data_structures/heap/test/max_heap.test.ts b/data_structures/heap/test/max_heap.test.ts index 0f49f876..9a251de9 100644 --- a/data_structures/heap/test/max_heap.test.ts +++ b/data_structures/heap/test/max_heap.test.ts @@ -2,11 +2,11 @@ import { MaxHeap } from "../max_heap"; describe("MaxHeap", () => { let heap: MaxHeap; + const elements: number[] = [ + 12, 4, 43, 42, 9, 7, 39, 16, 55, 1, 51, 34, 81, 18, + ]; - beforeAll(() => { - const elements: number[] = [ - 12, 4, 43, 42, 9, 7, 39, 16, 55, 1, 51, 34, 81, 18, - ]; + beforeEach(() => { heap = new MaxHeap(elements); }); @@ -26,4 +26,26 @@ describe("MaxHeap", () => { heap.insert(61); heap.check(); }); + + const extract_all = (heap: MaxHeap) => { + [...elements].sort((a, b) => b - a).forEach((element: number) => { + expect(heap.extract()).toEqual(element); + }); + heap.check(); + expect(heap.size()).toEqual(0); + } + + it("should remove and return the max elements in order", () => { + extract_all(heap); + }); + + it("should insert all, then remove and return the max elements in order", () => { + heap = new MaxHeap(); + elements.forEach((element: number) => { + heap.insert(element); + }); + heap.check(); + expect(heap.size()).toEqual(elements.length); + extract_all(heap); + }); }); diff --git a/data_structures/heap/test/min_heap.test.ts b/data_structures/heap/test/min_heap.test.ts index 8132a05c..07f10b1e 100644 --- a/data_structures/heap/test/min_heap.test.ts +++ b/data_structures/heap/test/min_heap.test.ts @@ -2,11 +2,11 @@ import { MinHeap } from "../min_heap"; describe("MinHeap", () => { let heap: MinHeap; + const elements: number[] = [ + 12, 4, 43, 42, 9, 7, 39, 16, 55, 1, 51, 34, 81, 18, + ]; - beforeAll(() => { - const elements: number[] = [ - 12, 4, 43, 42, 9, 7, 39, 16, 55, 1, 51, 34, 81, 18, - ]; + beforeEach(() => { heap = new MinHeap(elements); }); @@ -26,4 +26,26 @@ describe("MinHeap", () => { heap.insert(24); heap.check(); }); + + const extract_all = (heap: MinHeap) => { + [...elements].sort((a, b) => a - b).forEach((element: number) => { + expect(heap.extract()).toEqual(element); + }); + heap.check(); + expect(heap.size()).toEqual(0); + } + + it("should remove and return the min elements in order", () => { + extract_all(heap); + }); + + it("should insert all, then remove and return the min elements in order", () => { + heap = new MinHeap(); + elements.forEach((element: number) => { + heap.insert(element); + }); + heap.check(); + expect(heap.size()).toEqual(elements.length); + extract_all(heap); + }); });