https://github.com/codebox/js-heap
https://github.com/codebox/js-heap
algorithms data-structures javascript
Last synced: 2 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/codebox/js-heap
- Owner: codebox
- Created: 2020-06-26T16:31:04.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-06-27T14:15:20.000Z (almost 6 years ago)
- Last Synced: 2025-10-04T12:40:07.838Z (9 months ago)
- Topics: algorithms, data-structures, javascript
- Language: JavaScript
- Homepage: https://codebox.net/pages/javascript-binary-heap
- Size: 64.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# JavaScript Binary Heap
Here's a simple JavaScript implementation of a Binary Heap,
a data structure that can be used as a [Priority Queue](https://en.wikipedia.org/wiki/Priority_queue) storing items and returning
them in priority order. The code uses a number of ECMAScript 6 features and therefore will not work on older browsers.
I've implemented `insert`, `extract` and `peek` methods because that was all I needed at the time, but
it should be simple to extend if required:
Method|Time Complexity (Worst Case)
------|-----------
insert()|O(log n)
extract()|O(log n)
peek()|O(1)
To create a heap object, just call the `buildHeap()` function and add new items using the `insert()` method.
By default a 'min-heap' is created, where the smallest remaining item will be returned next:
const heap = buildHeap();
heap.insert(3);
heap.insert(2);
heap.insert(1);
heap.extract(); // 1
heap.extract(); // 2
heap.extract(); // 3
heap.extract(); // undefined - heap is now empty
`buildHeap()` accepts an optional argument which will be used as a mapping function, transforming values before comparing them
with each other. In this way custom ordering can be implemented, for example to create a heap which stores objects and always returns the
one with the highest `score` property, do this:
const maxScoreHeap = buildHeap(v => -v.score);