Skip to content

Commit

Permalink
docs(readme.md): update the usage docs to use the two subclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
khaledosama999 committed Feb 28, 2021
1 parent b5aac24 commit 53c9ee3
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,26 @@ npm i binary-heap.js

# Usage
```js
const binaryHeap = new BinaryHeap(
(x) =>x
)
const maxHeap = new MaxHeap( (x) =>x );
maxHeap.insert(3);
maxHeap.insert(5);
maxHeap.insert(7);

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

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

binaryHeap.insert(3);
binaryHeap.insert(5);
binaryHeap.insert(7);
maxHeap.insert(5);
maxHeap.insert(3);
maxHeap.insert(7);

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

# Constructor
Expand All @@ -28,7 +36,6 @@ binaryHeap.pop() // undefined
| :--- | :----: | :---: | :---: | ---: |
| 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
Expand All @@ -40,19 +47,19 @@ map.set(1,1)
map.set(2,2)
map.set(3,3);

const binaryHeapOne = new BinaryHeap(
const maxHeapOne = new MaxHeap(
(x) =>x,
arr.values(),
false
);

const binaryHeapOne = new BinaryHeap(
const maxHeapTwo = new MaxHeap(
(x) =>x,
sets.values(),
false
);

const binaryHeapOne = new BinaryHeap(
const maxHeapThree = new MaxHeap(
(x) =>x,
map.keys().values(),
false
Expand All @@ -63,7 +70,7 @@ 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(
const maxHeap = new MaxHeap(
(x) =>x.id,
arr.values(),
false
Expand Down

0 comments on commit 53c9ee3

Please sign in to comment.