# binary-heap.js

> A package for binary heap (heap tree) data structure

Latest version **3.3.0** (published 2021-03-09) · ISC license · 0 weekly downloads

## Install

```sh
npm install binary-heap.js
pnpm add binary-heap.js
yarn add binary-heap.js
bun add binary-heap.js
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.3.0 |
| Published | 2021-03-09 |
| First published | 2021-02-24 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 21.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | khaled osama |
| Maintainers | khaledosama |
| Keywords | data, structure, trees, sort, iteratable, heap, max, min, array, set, map, typescript |

## Links

- npm: https://www.npmjs.com/package/binary-heap.js
- Repository: https://github.com/khaledosama999/binary-heap.js
- Homepage: https://github.com/khaledosama999/binary-heap.js#readme
- Issues: https://github.com/khaledosama999/binary-heap.js/issues
- npm.io page: https://npm.io/package/binary-heap.js

## Alternatives

- [@lexical/table](https://npm.io/package/@lexical/table.md) — 3.0M weekly downloads
- [mantine-datatable](https://npm.io/package/mantine-datatable.md) — 98.2K weekly downloads
- [react-native-collapsible-tab-view](https://npm.io/package/react-native-collapsible-tab-view.md) — 70.6K weekly downloads
- [@handsontable/vue3](https://npm.io/package/@handsontable/vue3.md) — 16.1K weekly downloads
- [vuewordcloud](https://npm.io/package/vuewordcloud.md) — 7.2K weekly downloads

## Recent versions

- 3.3.0 (latest) — 2021-03-09
- 3.2.2 — 2021-03-09
- 3.2.1 — 2021-03-09
- 3.2.0 — 2021-03-09
- 3.1.1 — 2021-03-05
- 3.1.0 — 2021-03-05
- 3.0.0 — 2021-03-01
- 2.0.0 — 2021-02-28
- 1.2.0 — 2021-02-28
- 1.1.0 — 2021-02-25
- 1.0.0 — 2021-02-24

## README

# Binary heap
A package for general purpose binary heap data structure that can contain any type of data 

# Installation
```bash
npm i binary-heap.js
```
# Constructor

| Parameter   | Type | Required| Default| Description     |
| :---        |    :----:   | :---: | :---: |         ---: |
| extractor      | function |    false |   | function that extracts the key used for sorting elements in the heap (should be equal to identity for primitive data types)   |

# Methods

| Name   | Description | Return |
| :---        |    :----:   | ---: | 
| Insert      | Insert a single element into the heap  |    void |  
| InsertMany   | Inserts Many elements into the heap  | void| 
| pop   | Pops the top element (maximum or minimum depending on the type of the heap)| type T (type of elements inserted)| 
| isEmpty | Checks if the heap is empty or not | boolean

# Usage
```js
const maxHeap = new MaxHeap( (x) =>x );
maxHeap.insert(3);
maxHeap.insertMany([5,7].values())

maxHeap.pop() // 7
maxHeap.pop() // 5
maxHeap.pop() // 3
maxHeap.pop() // undefined

const minHeap = new MinHeap( (x) =>x );

maxHeap.insert(5);
maxHeap.insertMany([3,7].values())

maxHeap.pop() // 3
maxHeap.pop() // 5
maxHeap.pop() // 7
maxHeap.pop() // undefined
```

Works with any data structure that implements the iteratable interface to provide it's elements

```js
// Works with arrays, maps, sets
const arr = [1,2,3];
const set = new Set([1,2,3])
const map = new Map();
map.set(1,1)
map.set(2,2)
map.set(3,3);

const maxHeapOne = new MaxHeap((x) =>x);
maxHeapOne.insertMany(arr.values());

const maxHeapTwo = new MaxHeap((x) =>x);
maxHeapTwo.insertMany(set.values());

const maxHeapThree = new MaxHeap((x) =>x);
maxHeapThree.insertMany(map.keys().values());

```
Works with complex objects as long as you provide the right extractor

```js
const arr = [{id:1},{id:2},{id:3}];

const maxHeap = new MaxHeap((x) =>x.id);
maxHeap.insertMany(arr.values());

```

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