A binary heap data structure in JavaScript.
| Access | Search | Insertion | Deletion
------- | ------ | ------ | ---------- | --------
Average | Θ(1)
| Θ(n)
| Θ(1)
| Θ(log n)
Worst | O(1)
| O(n)
| O(log n)
| O(log n)
yarn add ds-binary-heap
Create a new binary heap.
Parameters
scorer
[Function] Function used to score an item.comparer
[Function] Function used to compare two items scores.
Push an item to the heap.
Parameters
item
Any
Examples
heap.push(1)
heap.push('foo')
heap.push({ foo: 'bar' })
Return the root of the heap and remove it.
Examples
heap.pop() // Hip-Hop! Ok...
// => 1
Returns Any item
Return the root of the heap.
Examples
heap.peek()
// => 1
Returns Any item
Remove all items form the heap.
Examples
heap.clear()
Return an array containing all the items.
Examples
heap.entries()
// => [ 1, 'foo', { foo: 'bar' }]
Returns Array
Return a string representation of the list.
Parameters
Examples
list.inspect()
// => [ 1, 'foo', { foo: 'bar' }]
Returns String
Return the number of items in the list.
Examples
list.length()
// => 3
Returns Number
Iterate over the list.
Examples
for (let item of list) {
console.log(item)
}
// => 1
// => 'foo'
// => { foo: 'bar' }
MIT © Nicolas Gryman