mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-11-10 11:09:43 +08:00
Merge 261fd8c4ae
into ca3d16dcce
This commit is contained in:
commit
ce2ba60f2d
@ -7,7 +7,7 @@
|
||||
class MaxHeapAdhoc {
|
||||
constructor(heap = []) {
|
||||
this.heap = [];
|
||||
heap.forEach(this.add);
|
||||
heap.forEach((value) => this.add(value));
|
||||
}
|
||||
|
||||
add(num) {
|
||||
|
@ -7,7 +7,7 @@
|
||||
class MinHeapAdhoc {
|
||||
constructor(heap = []) {
|
||||
this.heap = [];
|
||||
heap.forEach(this.add);
|
||||
heap.forEach((value) => this.add(value));
|
||||
}
|
||||
|
||||
add(num) {
|
||||
|
@ -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);
|
||||
});
|
||||
});
|
||||
|
@ -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);
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user