quig
v1.1.1
Published
Quick and dirty heap implementation
Downloads
10
Readme
quig
Quick and dirty heap implementation
For alternate solutions see Heapify, TinyQueue, and FlatQueue.
Getting Started
npm install -S quig
import { Heap } from 'quig'
const heap = Heap.of()
heap.push(3)
heap.push(5)
heap.push(1)
heap.peek() // 1
heap.pop() // 1
heap.pop() // 3
heap.pop() // 5
Configuration
The Heap constructor accepts a configuration object:
{
data: <Array<any>>,
comparator: <Function<any, any> -> Boolean>
}
A heap can be initialised with values:
const heap = Heap.of({
data: [0, 10, 4, 7]
})
The comparator function can be used to change the behaviour of the heap, for example, to implement a max heap:
const heap = Heap.of({
comparator: (a, b) => a > b
})
The comparator will be furnished with two nodes to compare and is expected to return a boolean denoting their priority i.e. returning true from the comparator will bubble the node to the head of the heap.
Heap can accept any sort of data (for faster implementations using integers see Heapify).
const heap = Heap.of({
comparator: (a, b) => a.cost < b.cost
})
heap.push({ path: path, cost: 10 })
heap.push({ path: path, cost: 5 })
heap.pop() // { path: path, cost: 5 }
Running tests
npm install
npm test
Contributing
Pull requests are always welcome, the project uses the standard code style. Please run npm test
to ensure all tests are passing and add tests for any new features or updates.
For bugs and feature requests, please create an issue.
License
MIT