Compare commits

...

2 Commits

Author SHA1 Message Date
Jeongjin Oh
ce2ba60f2d
Merge 261fd8c4ae into ca3d16dcce 2024-07-14 15:51:39 +09:00
Jeongjin Oh
261fd8c4ae fix(heap): this is undefined 2024-04-13 01:55:32 +09:00
4 changed files with 23 additions and 7 deletions

View File

@ -7,7 +7,7 @@
class MaxHeapAdhoc { class MaxHeapAdhoc {
constructor(heap = []) { constructor(heap = []) {
this.heap = []; this.heap = [];
heap.forEach(this.add); heap.forEach((value) => this.add(value));
} }
add(num) { add(num) {

View File

@ -7,7 +7,7 @@
class MinHeapAdhoc { class MinHeapAdhoc {
constructor(heap = []) { constructor(heap = []) {
this.heap = []; this.heap = [];
heap.forEach(this.add); heap.forEach((value) => this.add(value));
} }
add(num) { add(num) {

View File

@ -1,8 +1,8 @@
import MaxHeap from '../MaxHeapAdhoc'; import MaxHeapAdhoc from '../MaxHeapAdhoc';
describe('MaxHeapAdhoc', () => { describe('MaxHeapAdhoc', () => {
it('should create an empty max heap', () => { it('should create an empty max heap', () => {
const maxHeap = new MaxHeap(); const maxHeap = new MaxHeapAdhoc();
expect(maxHeap).toBeDefined(); expect(maxHeap).toBeDefined();
expect(maxHeap.peek()).toBe(undefined); expect(maxHeap.peek()).toBe(undefined);
@ -10,7 +10,7 @@ describe('MaxHeapAdhoc', () => {
}); });
it('should add items to the heap and heapify it up', () => { it('should add items to the heap and heapify it up', () => {
const maxHeap = new MaxHeap(); const maxHeap = new MaxHeapAdhoc();
maxHeap.add(5); maxHeap.add(5);
expect(maxHeap.isEmpty()).toBe(false); expect(maxHeap.isEmpty()).toBe(false);
@ -44,7 +44,7 @@ describe('MaxHeapAdhoc', () => {
}); });
it('should poll items from the heap and heapify it down', () => { it('should poll items from the heap and heapify it down', () => {
const maxHeap = new MaxHeap(); const maxHeap = new MaxHeapAdhoc();
maxHeap.add(5); maxHeap.add(5);
maxHeap.add(3); maxHeap.add(3);
@ -74,7 +74,7 @@ describe('MaxHeapAdhoc', () => {
}); });
it('should heapify down through the right branch as well', () => { it('should heapify down through the right branch as well', () => {
const maxHeap = new MaxHeap(); const maxHeap = new MaxHeapAdhoc();
maxHeap.add(3); maxHeap.add(3);
maxHeap.add(12); maxHeap.add(12);
@ -88,4 +88,12 @@ describe('MaxHeapAdhoc', () => {
expect(maxHeap.poll()).toBe(12); expect(maxHeap.poll()).toBe(12);
expect(maxHeap.toString()).toBe('11,3,10'); 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);
});
}); });

View File

@ -88,4 +88,12 @@ describe('MinHeapAdhoc', () => {
expect(minHeap.poll()).toBe(3); expect(minHeap.poll()).toBe(3);
expect(minHeap.toString()).toBe('10,11,12'); 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);
});
}); });