https://github.com/ngryman/ds-binary-heap
A binary heap data structure in JavaScript.
https://github.com/ngryman/ds-binary-heap
binary-heap datastructures heap
Last synced: 11 months ago
JSON representation
A binary heap data structure in JavaScript.
- Host: GitHub
- URL: https://github.com/ngryman/ds-binary-heap
- Owner: ngryman
- Created: 2017-02-19T17:34:46.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-10-12T23:40:04.000Z (over 8 years ago)
- Last Synced: 2025-01-28T05:28:24.465Z (about 1 year ago)
- Topics: binary-heap, datastructures, heap
- Language: JavaScript
- Size: 63.5 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
A binary heap data structure in JavaScript.
* * *
## Big-O
| Access | Search | Insertion | Deletion
------- | ------ | ------ | ---------- | --------
Average | `Θ(1)` | `Θ(n)` | `Θ(1)` | `Θ(log n)`
Worst | `O(1)` | `O(n)` | `O(log n)` | `O(log n)`
## Install
```bash
yarn add ds-binary-heap
```
## API
### constructor
Create a new binary heap.
**Parameters**
- `scorer` **\[[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)]** Function used to score an item.
- `comparer` **\[[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)]** Function used to compare two items scores.
### push
Push an item to the heap.
**Parameters**
- `item` **Any**
**Examples**
```javascript
heap.push(1)
heap.push('foo')
heap.push({ foo: 'bar' })
```
### pop
Return the root of the heap and remove it.
**Examples**
```javascript
heap.pop() // Hip-Hop! Ok...
// => 1
```
Returns **Any** item
### peek
Return the root of the heap.
**Examples**
```javascript
heap.peek()
// => 1
```
Returns **Any** item
### clear
Remove all items form the heap.
**Examples**
```javascript
heap.clear()
```
### entries
Return an array containing all the items.
**Examples**
```javascript
heap.entries()
// => [ 1, 'foo', { foo: 'bar' }]
```
Returns **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)**
### inspect
Return a string representation of the list.
**Parameters**
- `depth` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)**
- `opts` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
**Examples**
```javascript
list.inspect()
// => [ 1, 'foo', { foo: 'bar' }]
```
Returns **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)**
### length
Return the number of items in the list.
**Examples**
```javascript
list.length()
// => 3
```
Returns **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)**
### iterator
Iterate over the list.
**Examples**
```javascript
for (let item of list) {
console.log(item)
}
// => 1
// => 'foo'
// => { foo: 'bar' }
```
## License
MIT © [Nicolas Gryman](http://ngryman.sh)