# binaryheap-array

> Binary Heap Data Structure using an array like implementation, super fast and easy to use

Latest version **2.1.0** (published 2019-11-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install binaryheap-array
pnpm add binaryheap-array
yarn add binaryheap-array
bun add binaryheap-array
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2019-11-02 |
| First published | 2016-04-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 10 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Khaled Mohamed |
| Maintainers | khaledmohamedp |
| Keywords | data, structure, binary, tree, binary, heap, binaryheap, priority, queue |

## Links

- npm: https://www.npmjs.com/package/binaryheap-array
- Repository: https://github.com/KhaledMohamedP/BinaryHeap
- Homepage: https://github.com/KhaledMohamedP/BinaryHeap#readme
- Issues: https://github.com/KhaledMohamedP/BinaryHeap/issues
- npm.io page: https://npm.io/package/binaryheap-array

## Dependencies (1)

- [nyc](https://npm.io/package/nyc.md) 14.1.1

## Alternatives

- [cron](https://npm.io/package/cron.md) — 4.9M weekly downloads
- [@vercel/queue](https://npm.io/package/@vercel/queue.md) — 731.6K weekly downloads
- [create-sonicjs](https://npm.io/package/create-sonicjs.md) — 1.6K weekly downloads
- [@exellix/jobs-api](https://npm.io/package/@exellix/jobs-api.md) — 941 weekly downloads
- [@forwardimpact/libskill](https://npm.io/package/@forwardimpact/libskill.md) — 575 weekly downloads

## Recent versions

- 2.1.0 (latest) — 2019-11-02
- 1.0.3 — 2016-05-02
- 1.0.2 — 2016-04-29
- 1.0.1 — 2016-04-29
- 1.0.0 — 2016-04-29

## README

# BinaryHeap
Binary Heap Data Structure using an array implementation

[![Istanbul Coverage](https://img.shields.io/codecov/c/github/KhaledMohamedP/BinaryHeap.svg?style=flat-square)](https://github.com/KhaledMohamedP/BinaryHeap)
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
[![Version](https://img.shields.io/npm/v/binaryheap-array.svg?style=flat-square)](https://www.npmjs.com/package/binaryheap-array)
[![License](https://img.shields.io/npm/l/binaryheap-array.svg?style=flat-square)](https://www.npmjs.com/package/binaryheap-array)
[![Downloads](http://img.shields.io/npm/dm/binaryheap-array.svg?style=flat-square)](https://www.npmjs.com/package/binaryheap-array)

## Example

#### Import via NPM
```javascript
var BinaryHeap = require("binaryheap-array");
```

#### || Single element
``` javascript
var ch = new BinaryHeap();
ch.insert('T');
ch.insert('S');

// You can also chain inserts :)
ch.insert('R').insert('P');

// Removes the largest element first
ch.remove(); // 'T'

// You can also peak before you remove
ch.peak();    // 'S'
ch.remove();  // 'S'
```

#### || Object
```javascript
// Use the 'comparable' property to choose what you need to compare ahead of time
// In our example we want to compare the age
var list = new BinaryHeap({ comparable: function(elm) { return elm.age; } });
list.insert({ 'name': 'John', 'age': 25 });
list.insert({ 'name': 'Mike', 'age': 21 });
list.insert({ 'name': 'Aisha', 'age': 33 });
list.insert({ 'name': 'Sarah', 'age': 20 });

list.remove(); // { name: 'Aisha', age: 33 }
list.size(); // 3
list.remove(); // { name: 'John', age: 25 }
```

#### || Priority Queue
Create a maximum or minimum priority queue on the fly

```javascript
// Choose the order of the binaryheap ascending, or descending
var maximumPQ = new BinaryHeap({ order:'asc' });
var minimumPQ = new BinaryHeap({ order:'dec' });
```

## API
| Method| Returns Type| Description
|-------|------------ |-------------
|size   | `number`    | The size of the heap
|insert | `object`    | Adds an element to the heap
|remove | `object`    | Removes the root element (could be the max or min based on the [configuration setting](#setting))
|print  | `undefined` | Prints the tree structure of the binary heap
|peak   | `object`    | Peak on the root element, or the element that will get remove first

### Setting
| Object     | Type      | Description
|------------|-----------|------------
| order      | `string`  | The order of the BinaryHeap either 'ascending' or 'descending'
| comparable | `function`| Choose what you need to compare, default to the inserted value [see object example](#-object)
| data       | `array`   | Pass in the data as an array ahead of time and we will handle the insertion for you

### O(n)

| Type     | Worst     | Average
| -------- | --------- | --------
| insert   |  O(log n) |  O(log n)
| remove   |  O(log n) |  O(log n)
| peak     |  O(1)     |  O(1)

## Graph

This graph is a representation of the algorithm used in this module

```

               *-( (2 * i) + 1 )-˅
               *-( 2 * i )-˅     ˅
[ 'ø',  'T',  'S',  'R',  'P',  'N',  'O',  'A', ...]
  Empty  *------^     ^
         (2 * i)      ^
         *------------^
         (2 * i) + 1

```

## Reach out

Feel free to reach out with feedback via [github](https://github.com/KhaledMohamedP/BinaryHeap/issues): `issue`, `feature`, `bug`, or `enhancement` inputs are greatly appreciated

____

↳ Links: [NPM](https://www.npmjs.com/package/binaryheap-array) | [GitHub](https://github.com/KhaledMohamedP/BinaryHeap)

© MIT

---
_Source: https://npm.io/package/binaryheap-array · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
