Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(heap): this is undefined #1124

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/data-structures/heap/MaxHeapAdhoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class MaxHeapAdhoc {
constructor(heap = []) {
this.heap = [];
heap.forEach(this.add);
heap.forEach((value) => this.add(value));
}

add(num) {
Expand Down
2 changes: 1 addition & 1 deletion src/data-structures/heap/MinHeapAdhoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class MinHeapAdhoc {
constructor(heap = []) {
this.heap = [];
heap.forEach(this.add);
heap.forEach((value) => this.add(value));
}

add(num) {
Expand Down
18 changes: 13 additions & 5 deletions src/data-structures/heap/__test__/MaxHeapAdhoc.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
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);
expect(maxHeap.isEmpty()).toBe(true);
});

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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
});
});
8 changes: 8 additions & 0 deletions src/data-structures/heap/__test__/MinHeapAdhoc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});