Package Exports
- heap-js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (heap-js) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Heap.js
Heap data structure for JavaScript.
Easy to use, known interfaces, tested, and well documented JavaScript binary heap library.
Instances are integer min heap by default.
Examples
// 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 ]Installation
yarn add heap-js # if you use yarn
npm install --save heap-js # if you use npmConstructor
new Heap([comparator])Basic comparators already included:
Heap.minComparatorInteger min heap (default)Heap.maxComparatorInteger max heap
Implements JavaScript style methods
lengthof the heaplimitnumber of elements in the heappop()top elementpush(...elements)to the heappushpop(element)faster thanpush&popreplace(element)faster thanpop&pushtop(number?)elements from the heap
Implements Java's PriorityQueue interface:
add(element)to the heapaddAll([elment, element, ... ])to the heap, faster than loopaddclear()clone()comparator()contains(element, fn?)element()alias ofpeek()isEmpty()offer(element)alias ofadd(element)peek()poll()alias ofpop()remove(element?)removeAll()alias ofclear()size()alias oflengthtoArray()toString()
To do:
- containsAll
- equals
- iterator()
- retainAll
Implements static Python's heapq interface:
Heap.heapify(array)that converts an array to an array-heapHeap.heappop(heapArray)that takes the peek of the array-heapHeap.heappush(heapArray, item)that appends elements to the array-heapHeap.heappushpop(heapArray, item)faster thanheappush&heappopHeap.heapreplace(heapArray, item)faster thanheappop&heappush
To do:
- merge(*iterables, key=None, reverse=False)
- nlargest(n, iterable, key=None)
- nsmallest(n, iterable, key=None)
Documentation
Extensive documentation included at ./dist/docs. It'll be published to gh-pages in a next release.
Contributing
Development of Heap.js happens in the open on GitHub, and I am grateful to the community for contributing bugfixes and improvements.
Dev setup
yarn # if you use yarn
npm install # if you use npmTests
yarn test # if you use yarn
npm test # if you use npmLicense
Heap.js is BSD licensed.
