mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-09-20 15:53:02 +08:00
Compare commits
3 Commits
ce2ba60f2d
...
a05d4e6420
Author | SHA1 | Date | |
---|---|---|---|
|
a05d4e6420 | ||
|
564bbd4325 | ||
|
261fd8c4ae |
@ -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) {
|
||||||
|
@ -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) {
|
||||||
|
@ -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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user