From 261fd8c4ae5619ab1d4a39c1385c788978950520 Mon Sep 17 00:00:00 2001 From: Jeongjin Oh Date: Sat, 13 Apr 2024 01:55:32 +0900 Subject: [PATCH] fix(heap): this is undefined --- src/data-structures/heap/MaxHeapAdhoc.js | 2 +- src/data-structures/heap/MinHeapAdhoc.js | 2 +- .../heap/__test__/MaxHeapAdhoc.test.js | 18 +++++++++++++----- .../heap/__test__/MinHeapAdhoc.test.js | 8 ++++++++ 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/data-structures/heap/MaxHeapAdhoc.js b/src/data-structures/heap/MaxHeapAdhoc.js index b9d69c59..9291f0c6 100644 --- a/src/data-structures/heap/MaxHeapAdhoc.js +++ b/src/data-structures/heap/MaxHeapAdhoc.js @@ -7,7 +7,7 @@ class MaxHeapAdhoc { constructor(heap = []) { this.heap = []; - heap.forEach(this.add); + heap.forEach((value) => this.add(value)); } add(num) { diff --git a/src/data-structures/heap/MinHeapAdhoc.js b/src/data-structures/heap/MinHeapAdhoc.js index c70692f3..771c8794 100644 --- a/src/data-structures/heap/MinHeapAdhoc.js +++ b/src/data-structures/heap/MinHeapAdhoc.js @@ -7,7 +7,7 @@ class MinHeapAdhoc { constructor(heap = []) { this.heap = []; - heap.forEach(this.add); + heap.forEach((value) => this.add(value)); } add(num) { diff --git a/src/data-structures/heap/__test__/MaxHeapAdhoc.test.js b/src/data-structures/heap/__test__/MaxHeapAdhoc.test.js index 0217a98d..8ddc9f8a 100644 --- a/src/data-structures/heap/__test__/MaxHeapAdhoc.test.js +++ b/src/data-structures/heap/__test__/MaxHeapAdhoc.test.js @@ -1,8 +1,8 @@ -import MaxHeap from '../MaxHeapAdhoc'; +import MaxHeapAdhoc from '../MaxHeapAdhoc'; describe('MaxHeapAdhoc', () => { it('should create an empty max heap', () => { - const maxHeap = new MaxHeap(); + const maxHeap = new MaxHeapAdhoc(); expect(maxHeap).toBeDefined(); expect(maxHeap.peek()).toBe(undefined); @@ -10,7 +10,7 @@ describe('MaxHeapAdhoc', () => { }); it('should add items to the heap and heapify it up', () => { - const maxHeap = new MaxHeap(); + const maxHeap = new MaxHeapAdhoc(); maxHeap.add(5); expect(maxHeap.isEmpty()).toBe(false); @@ -44,7 +44,7 @@ describe('MaxHeapAdhoc', () => { }); it('should poll items from the heap and heapify it down', () => { - const maxHeap = new MaxHeap(); + const maxHeap = new MaxHeapAdhoc(); maxHeap.add(5); maxHeap.add(3); @@ -74,7 +74,7 @@ describe('MaxHeapAdhoc', () => { }); it('should heapify down through the right branch as well', () => { - const maxHeap = new MaxHeap(); + const maxHeap = new MaxHeapAdhoc(); maxHeap.add(3); maxHeap.add(12); @@ -88,4 +88,12 @@ describe('MaxHeapAdhoc', () => { expect(maxHeap.poll()).toBe(12); expect(maxHeap.toString()).toBe('11,3,10'); }); + + it('should create the max heap filled', () => { + const maxHeap = new MaxHeapAdhoc([3, 12, 10]); + + expect(maxHeap).toBeDefined(); + expect(maxHeap.peek()).toBe(12); + expect(maxHeap.isEmpty()).toBe(false); + }); }); diff --git a/src/data-structures/heap/__test__/MinHeapAdhoc.test.js b/src/data-structures/heap/__test__/MinHeapAdhoc.test.js index 766b307f..5b170c28 100644 --- a/src/data-structures/heap/__test__/MinHeapAdhoc.test.js +++ b/src/data-structures/heap/__test__/MinHeapAdhoc.test.js @@ -88,4 +88,12 @@ describe('MinHeapAdhoc', () => { expect(minHeap.poll()).toBe(3); expect(minHeap.toString()).toBe('10,11,12'); }); + + it('should create the min heap filled', () => { + const minHeap = new MinHeapAdhoc([3, 12, 10]); + + expect(minHeap).toBeDefined(); + expect(minHeap.peek()).toBe(3); + expect(minHeap.isEmpty()).toBe(false); + }); });