A javascript binary heap implementation for Node.js and the browser.
As npm for Node.js:
$ npm install bheap
var BinaryHeap = require('bheap');
var heap = new BinaryHeap(function(a, b) {
return a.cash - b.cash;
});
heap.push({ cash: 250, name: 'Valentina' });
heap.push({ cash: 300, name: 'Jano' });
heap.push({ cash: 150, name: 'Fran' );
heap.size; // 3
heap.top(); // { cash: 300, name: 'Jano' }
heap.pop(); // { cash: 300, name: 'Jano' }
heap.size; // 2
It creates a new instance of BinaryHeap
based on its parameters.
Time complexity: O(n) such that n === array.length
- Type: Array
- Default: []
Elements that are inserted in binary heap.
- Type: Function
- Default: [BinaryHeap.DEFAULT_COMPARATOR](a, b)
- Returns:
- positive if
a
is great thanb
- negative if
a
is less thanb
- zero if
a
is equal tob
- positive if
It is a function that heap uses internally to sort its elements.
It is default comparator if any is passed and compares two Number
or String
objects. It is static member of BinaryHeap
.
- Type: Function
- Returns: Boolean
Returns whether the binary heap is empty or not.
Time complexity: O(1)
- Type: Number
The size of the binary heap.
Time complexity: O(1)
- Type: Function
- Returns: Element of instancce of
BinaryHeap
Gets the top element of the binary heap.
Throws an Error
when the heap is empty.
Time complexity: O(1)
- Type: Function
- Returns: Element of instancce of
BinaryHeap
Pops the top element of instance of binary heap.
Throws an Error
when the heap is empty.
Time complexity: O(log(n)) such that n === this.size
- Type: Function
- Returns: Integer
Push the element
at the binary heap and returns its new size.
Time complexity: O(log(n)) such that n === this.size
As npm package:
$ npm test
MIT