This commit is contained in:
Jeongjin Oh 2024-07-14 15:51:39 +09:00 committed by GitHub
commit ce2ba60f2d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 23 additions and 7 deletions

View File

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

View File

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

View File

@ -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);
});
});

View File

@ -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);
});
});