All Projects → ignlg → heap-js

ignlg / heap-js

Licence: BSD-3-Clause license
Efficient Binary heap (priority queue, binary tree) data structure for JavaScript / TypeScript. Includes JavaScript methods, Python's heapq module methods, and Java's PriorityQueue methods.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to heap-js

cs.js
Computer Science Data Structures and Algorithms in JavaScript ( Node.JS, ES ) in simple, clean, reusable code
Stars: ✭ 86 (+30.3%)
Mutual labels:  heap, binary-heap
Data Structures
A collection of powerful data structures
Stars: ✭ 2,534 (+3739.39%)
Mutual labels:  priority-queue, heap
needle
📌📚 An extensive standalone data structure library for JavaScript.
Stars: ✭ 25 (-62.12%)
Mutual labels:  heap, binary-trees
Binarytree
Python Library for Studying Binary Trees
Stars: ✭ 1,694 (+2466.67%)
Mutual labels:  heap, binary-trees
DEPRECATED-data-structures
A collection of powerful data structures
Stars: ✭ 2,648 (+3912.12%)
Mutual labels:  priority-queue, heap
Heapify
The fastest JavaScript priority queue out there. Zero dependencies.
Stars: ✭ 520 (+687.88%)
Mutual labels:  priority-queue, heap
jheaps
Master repository for the JHeaps project
Stars: ✭ 34 (-48.48%)
Mutual labels:  priority-queue, heap
Fastpriorityqueue.js
a fast heap-based priority queue in JavaScript
Stars: ✭ 240 (+263.64%)
Mutual labels:  priority-queue, heap
Kue
Kue is a priority job queue backed by redis, built for node.js.
Stars: ✭ 9,316 (+14015.15%)
Mutual labels:  priority-queue
Data-Structure-Algorithm-Programs
This Repo consists of Data structures and Algorithms
Stars: ✭ 464 (+603.03%)
Mutual labels:  heap
Buckets Js
A complete, fully tested and documented data structure library written in pure JavaScript.
Stars: ✭ 1,128 (+1609.09%)
Mutual labels:  priority-queue
Flatqueue
A very fast and simple JavaScript priority queue
Stars: ✭ 98 (+48.48%)
Mutual labels:  priority-queue
algoexpert
AlgoExpert is an online platform that helps software engineers to prepare for coding and technical interviews.
Stars: ✭ 8 (-87.88%)
Mutual labels:  binary-trees
Advancedasynctask
Enhanced AsyncTask library for Android
Stars: ✭ 77 (+16.67%)
Mutual labels:  priority-queue
NALib
General purpose C sourcecode collection
Stars: ✭ 16 (-75.76%)
Mutual labels:  heap
ctl
My variant of the C Template Library
Stars: ✭ 105 (+59.09%)
Mutual labels:  priority-queue
DrawRacket4Me
DrawRacket4Me draws trees and graphs from your code, making it easier to check if the structure is what you wanted.
Stars: ✭ 43 (-34.85%)
Mutual labels:  binary-trees
Sidekiq Priority
Prioritize Sidekiq jobs within queues
Stars: ✭ 47 (-28.79%)
Mutual labels:  priority-queue
Libgenerics
libgenerics is a minimalistic and generic library for C basic data structures.
Stars: ✭ 42 (-36.36%)
Mutual labels:  priority-queue
Rxjavapriorityscheduler
RxPS - RxJavaPriorityScheduler - A RxJava Priority Scheduler library for Android and Java applications
Stars: ✭ 138 (+109.09%)
Mutual labels:  priority-queue

Heap.js Heap.js

npm version Build Status Coverage Status Dependencies devDependency Status

Efficient Binary heap (priority queue, binary tree) data structure for JavaScript / TypeScript.

Includes JavaScript methods, Python's heapq module methods, and Java's PriorityQueue methods.

Easy to use, known interfaces, tested, and well documented JavaScript binary heap library.

Instances are integer min heap by default.

Open in Gitpod

Is it faster than sorting an array?

It depends on your usage, but for some scenarios it is much faster:

heap vs array: push + pop/unshift 50
	heap  x 72,130 ops/sec ±0.50% (93 runs sampled)
	array x 121 ops/sec ±78.09% (5 runs sampled)

heap vs array: push + peek 20
	heap  x 622,332 ops/sec ±27.93% (63 runs sampled)
	array x 207 ops/sec ±78.18% (5 runs sampled)

heap vs array: push + top(1) of 100
	heap  x 124,835 ops/sec ±40.37% (61 runs sampled)
	array x 123 ops/sec ±78.49% (5 runs sampled)

heap vs array: push + top(50) of 100
	heap  x 59,210 ops/sec ±17.66% (82 runs sampled)
	array x 125 ops/sec ±78.79% (5 runs sampled)

Changelog

2.2

Notice that using the heap directly as an iteraror will consume the heap, as Python's heapq implementation does.

2.1

  • Adds Heap.nlargest as heapq.
  • Adds Heap.nsmallest as heapq.
  • Sanitizes top / bottom input to force an integer.
  • Linted with Eslint.

2.0

The main breaking change is that now top(N) does NOT sort the output. It should not be part of the spec for a priority queue, the output should be the top N elements. It will be partially ordered with the peek at index 0 by definition, that is all.

  • top(N) is unordered, only the first element is guaranteed to be the top priority element.
  • Fixes custom heap issue #31.
  • Performance improvements.
  • More tests, including those for custom heaps.
  • Auxiliary experimental topN algorithms.
  • (wip) Benchmarks.

1.5

  • Adds Iterator interface and iterator() method.

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

// Iterator, that will consume the heap
maxHeap.init([3, 4, 1, 12, 8]);
for (const value of maxHeap) {
  console.log('Next top value is', value);
}
// 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);

// Iterator, the Java way, that will not consume the heap but does not guarantee
// to traverse the elements of the heap in any particular order. Barely useful.
for (const taks of priorityQueue.iterator()) {
  // Do something
}

// Iterator, the JavaScript and Python way, that will consume the heap
for (const task of priorityQueue) {
  // 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 npm

Constructor

new Heap([comparator]);

Basic comparators already included:

  • Heap.minComparator Integer min heap (default)
  • Heap.maxComparator Integer max heap

Implements JavaScript style methods

  • for (const value of heap) directly usable as an Iterator, consumes the heap
  • length of the heap
  • limit amount of elements in the heap
  • pop() the top element
  • push(...elements) one or more elements to the heap
  • pushpop(element) faster than push & pop
  • replace(element) faster than pop & push
  • top(number?) most valuable elements from the heap
  • bottom(number?) least valuable elements from the heap

Implements Java's PriorityQueue interface:

  • add(element) to the heap
  • addAll([elment, element, ... ]) to the heap, faster than loop add
  • clear()
  • clone()
  • comparator()
  • contains(element, fn?)
  • element() alias of peek()
  • isEmpty()
  • iterator() returns the same as toArray() because it is iterable and follows Java's implementation
  • offer(element) alias of add(element)
  • peek()
  • poll() alias of pop()
  • remove(element?)
  • removeAll() alias of clear()
  • size() alias of length
  • toArray()
  • toString()

To do:

  • containsAll
  • equals
  • retainAll

Implements static Python's heapq interface:

  • Heap.heapify(array, comparator?) that converts an array to an array-heap
  • Heap.heappop(heapArray, comparator?) that takes the peek of the array-heap
  • Heap.heappush(heapArray, item, comparator?) that appends elements to the array-heap
  • Heap.heappushpop(heapArray, item, comparator?) faster than heappush & heappop
  • Heap.heapreplace(heapArray, item, comparator?) faster than heappop & heappush
  • Heap.nlargest(n, iterable, comparator?) that gets the n most valuable elements of an iterable
  • Heap.nsmallest(n, iterable, comparator?) that gets the n least valuable elements of an iterable

Extras:

  • Heap.heaptop(n, heapArray, comparator?) that returns the n most valuable elements of the array-heap
  • Heap.heapbottom(n, heapArray, comparator?) that returns the n least valuable elements of the array-heap

To do:

  • merge(...iterables, comparator?)

Documentation

https://ignlg.github.io/heap-js/

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

Tests

npm run test

Benchmarks

npm run benchmarks

License

Heap.js is BSD licensed.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].