Heap.jsHeap data structure for JavaScript.
Easy to use, known interfaces, tested, and well documented JavaScript binary heap library.
Instances are integer min heap by default.
// Basic usage example
import Heap from 'heap-js';
const minHeap = new Heap();
const maxHeap = new Heap(Heap.maxComparator);
minHeap.init([5, 18, 1]);
minHeap.push(2);
console.log(minHeap.peek()); //> 1
console.log(minHeap.pop()); //> 1
console.log(minHeap.peek()); //> 2
// Priority Queue usage example
const { Heap } = require('heap-js');
const tasks = db.collection.find().toArray();
const customPriorityComparator = (a, b) => a.priority - b.priority;
const priorityQueue = new Heap(customPriorityComparator);
priorityQueue.init(tasks);
while (let task = priorityQueue.pop()) {
// Do something
}
// Python-like static methods example
import { Heap } from 'heap-js';
const numbers = [ 2, 3, 7, 5 ];
Heap.heapify(numbers);
console.log(numbers); //> [ 2, 3, 5, 7 ]
Heap.heappush(numbers, 1);
console.log(numbers); //> [ 1, 2, 5, 7, 3 ]
yarn add heap-js # if you use yarn
npm install --save heap-js # if you use npm
new Heap([comparator])
Basic comparators already included:
Heap.minComparator Integer min heap (default)Heap.maxComparator Integer max heaplength of the heaplimit amount of elements in the heap
pop() the top element
push(...elements) one or more elements to the heappushpop(element) faster than push & popreplace(element) faster than pop & push
top(number?) most valuable elements from the heap
bottom(number?) least valuable elements from the heapPriorityQueue interface:add(element) to the heapaddAll([elment, element, ... ]) to the heap, faster than loop addclear()clone()comparator()contains(element, fn?)element() alias of peek()isEmpty()offer(element) alias of add(element)peek()poll() alias of pop()remove(element?)removeAll() alias of clear()size() alias of lengthtoArray()toString()To do:
containsAllequalsiterator()retainAllheapq interface:Heap.heapify(array, comparator?) that converts an array to an array-heapHeap.heappop(heapArray, comparator?) that takes the peek of the array-heapHeap.heappush(heapArray, item, comparator?) that appends elements to the array-heapHeap.heappushpop(heapArray, item, comparator?) faster than heappush & heappopHeap.heapreplace(heapArray, item, comparator?) faster than heappop & heappushExtras:
Heap.heaptop(n, heapArray, comparator?) that returns the n most valuable elements of the array-heapHeap.heapbottom(n, heapArray, comparator?) that returns the n least valuable elements of the array-heapTo do:
merge(...iterables, comparator?)nlargest(n, iterable, comparator?)nsmallest(n, iterable, comparator?)Extensive documentation included at ./dist/docs. It'll be published to gh-pages in a next release.
Development of Heap.js happens in the open on GitHub, and I am grateful to the community for contributing bugfixes and improvements.
yarn # if you use yarn
npm install # if you use npm
yarn test # if you use yarn
npm test # if you use npm
Heap.js is BSD licensed.
Generated using TypeDoc