-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
790d45b
commit 3baaf49
Showing
1 changed file
with
71 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,71 @@ | ||
# priority-queue | ||
A typescript package for priority queue data structure | ||
# 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 | ||
``` | ||
|
||
# Usage | ||
```js | ||
const binaryHeap = new BinaryHeap( | ||
(x) =>x | ||
) | ||
|
||
binaryHeap.insert(3); | ||
binaryHeap.insert(5); | ||
binaryHeap.insert(7); | ||
|
||
binaryHeap.pop() // 7 | ||
binaryHeap.pop() // 5 | ||
binaryHeap.pop() // 3 | ||
binaryHeap.pop() // undefined | ||
``` | ||
|
||
# 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) | | ||
| iterator | IterableIterator | false| | Used to provide the heap with initial elements | | ||
MaxHeap | boolean | false | true | determines the order of the heap (maximum or minimum) | ||
|
||
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 binaryHeapOne = new BinaryHeap( | ||
(x) =>x, | ||
arr.values(), | ||
false | ||
); | ||
|
||
const binaryHeapOne = new BinaryHeap( | ||
(x) =>x, | ||
sets.values(), | ||
false | ||
); | ||
|
||
const binaryHeapOne = new BinaryHeap( | ||
(x) =>x, | ||
map.keys().values(), | ||
false | ||
) | ||
``` | ||
Works with complex objects as long as you provide the right extractor | ||
|
||
```js | ||
const arr = [{id:1},{id:2},{id:3}]; | ||
|
||
const binaryHeap = new BinaryHeap( | ||
(x) =>x.id, | ||
arr.values(), | ||
false | ||
) | ||
``` |