Skip to content

Commit

Permalink
Merge pull request #11 from datastructures-js/development
Browse files Browse the repository at this point in the history
add typescript
  • Loading branch information
eyas-ranjous authored Jun 15, 2021
2 parents 9092d5b + 9773cf7 commit 89e6665
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 169 deletions.
4 changes: 2 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"rules": {
max-len: ["error", { "code": 80, "ignoreComments": true }],
"max-len": ["error", { "code": 80, "ignoreComments": true }],
"comma-dangle": ["error", {
"functions": "ignore"
}],
no-underscore-dangle: [
"no-underscore-dangle": [
"error",
{
"allowAfterThis": true,
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]


## [3.1.0] - 2021-06-15

### Added
- typescript.

## [3.0.1] - 2021-03-28

### Fixed
Expand Down
158 changes: 69 additions & 89 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
[![npm](https://img.shields.io/npm/v/@datastructures-js/heap.svg)](https://www.npmjs.com/package/@datastructures-js/heap)
[![npm](https://img.shields.io/npm/dm/@datastructures-js/heap.svg)](https://www.npmjs.com/package/@datastructures-js/heap) [![npm](https://img.shields.io/badge/node-%3E=%206.0-blue.svg)](https://www.npmjs.com/package/@datastructures-js/heap)

a complete javascript implementation for the Min/Max Heap data structures & Heap Sort algorithm.
a javascript implementation for Heap data structure & Heap Sort algorithm.

<img src="https://user-images.githubusercontent.com/6517308/121813242-859a9700-cc6b-11eb-99c0-49e5bb63005b.jpg">

<table>
<tr><th>Min Heap</th><th>Max Heap</th></tr>
Expand All @@ -17,12 +19,12 @@ a complete javascript implementation for the Min/Max Heap data structures & Heap
</tr>
</table>

# Table of Contents
# Contents
* [Install](#install)
* [require](#require)
* [import](#import)
* [API](#api)
* [new](#new)
* [constructor](#constructor)
* [.insert(key[, value])](#insertkey-value)
* [.extractRoot()](#extractroot)
* [.root()](#root)
Expand Down Expand Up @@ -50,20 +52,29 @@ const { MinHeap, MaxHeap } = require('@datastructures-js/heap');

### import
```js
import { MinHeap, MaxHeap } from '@datastructures-js/heap';
import { MinHeap, MaxHeap, HeapNode } from '@datastructures-js/heap';
// HeapNode is the key/value interface
```

## API

### new
### constructor
creates an empty heap.

##### JS
```js
const minHeap = new MinHeap();

const maxHeap = new MaxHeap();
```

##### TS
```js
const minHeap = new MinHeap<number, [number, number]>();

const maxHeap = new MaxHeap<string, { name: string }>();
```

### .insert(key[, value])
insert a node into the heap. If value is provided (anything except undefined), the node is stored as `{key: ..., value: ...}` otherwise, the node is the key (number or string).

Expand Down Expand Up @@ -113,7 +124,7 @@ removes and returns the root node in the heap.
<th align="center">runtime</th>
</tr>
<tr>
<td align="center">number | string | object</td>
<td align="center">number | string | { key, value }</td>
<td align="center">O(log(n))</td>
</tr>
</table>
Expand All @@ -137,7 +148,7 @@ returns the root node without removing it.
<th align="center">runtime</th>
</tr>
<tr>
<td align="center">number | string | object</td>
<td align="center">number | string | { key, value }</td>
<td align="center">O(1)</td>
</tr>
</table>
Expand All @@ -157,7 +168,7 @@ returns a node with max key in MinHeap, or with min key in MaxHeap.
<th align="center">runtime</th>
</tr>
<tr>
<td align="center">number | string | object</td>
<td align="center">number | string | { key, value }</td>
<td align="center">O(1)</td>
</tr>
</table>
Expand Down Expand Up @@ -334,59 +345,52 @@ Heapifies an existing list. It returns a heap instance as well as changing the l
<th>runtime</th>
</tr>
<tr>
<td>list: array&lt;number|string|object&gt;</td>
<td>list: array&lt;number | string | { key, value }&gt;</td>
<td>MinHeap | MaxHeap</td>
<td>O(n)</td>
</tr>
</table>

##### JS
```js
const numList = [
50,
80,
30,
90,
60,
40,
20
];
const numList = [50, 80, 30, 90, 60, 40, 20];
MinHeap.heapify(numList);
console.log(numList);
/*
[
20,
60,
30,
90,
80,
50,
40
]
*/
console.log(numList); // [20, 60, 30, 90, 80, 50, 40]

const strList = [
'm',
'x',
'f',
'b',
'z',
'k',
'c'
];
const strList = ['m', 'x', 'f', 'b', 'z', 'k', 'c'];
const maxHeap = MaxHeap.heapify(strList);
console.log(strList);
console.log(strList); // ['z', 'x', 'k', 'b', 'm', 'f', 'c']
console.log(maxHeap.isValid()); // true

const objList = [
{ key: 50, value: 't1' },
{ key: 80, value: 't2' },
{ key: 30, value: 't3' },
{ key: 90, value: 't4' },
{ key: 60, value: 't5' },
{ key: 40, value: 't6' },
{ key: 20, value: 't7' }
];
MinHeap.heapify(objList);
console.log(objList);
/*
[
'z',
'x',
'k',
'b',
'm',
'f',
'c'
{ key: 20, value: 't7' },
{ key: 60, value: 't5' },
{ key: 30, value: 't3' },
{ key: 90, value: 't4' },
{ key: 80, value: 't2' },
{ key: 50, value: 't1' },
{ key: 40, value: 't6' }
]
*/
console.log(maxHeap.isValid()); // true
```

##### TS
```js
const numList = [50, 80, 30, 90, 60, 40, 20];
MinHeap.heapify<number>(numList);
console.log(numList); // [20, 60, 30, 90, 80, 50, 40]

const objList = [
{ key: 50, value: 't1' },
Expand All @@ -397,7 +401,7 @@ const objList = [
{ key: 40, value: 't6' },
{ key: 20, value: 't7' }
];
MinHeap.heapify(objList);
MinHeap.heapify<number, string>(objList);
console.log(objList);
/*
[
Expand All @@ -422,52 +426,28 @@ Checks if a given list is heapified.
<th>runtime</th>
</tr>
<tr>
<td>list: array&lt;number|string|object&gt;</td>
<td>list: array&lt;number | string | { key, value }&gt;</td>
<td>boolean</td>
<td>O(log(n))</td>
</tr>
</table>

##### JS
```js
MinHeap.isHeapified([
50,
80,
30,
90,
60,
40,
20
]); // false

MinHeap.isHeapified([
20,
60,
30,
90,
80,
50,
40
]); // true

MaxHeap.isHeapified([
'm',
'x',
'f',
'b',
'z',
'k',
'c'
]); // false

MaxHeap.isHeapified([
'z',
'x',
'k',
'b',
'm',
'f',
'c'
]); // true
MinHeap.isHeapified([50, 80, 30, 90, 60, 40, 20]); // false

MinHeap.isHeapified([20, 60, 30, 90, 80, 50, 40]); // true

MaxHeap.isHeapified(['m', 'x', 'f', 'b', 'z', 'k', 'c']); // false

MaxHeap.isHeapified(['z', 'x', 'k', 'b', 'm', 'f', 'c']); // true
```

##### TS
```js
MinHeap.isHeapified<number>([20, 60, 30, 90, 80, 50, 40]); // true

MaxHeap.isHeapified<string>(['z', 'x', 'k', 'b', 'm', 'f', 'c']); // true
```

## Build
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const MinHeap = require('./src/minHeap');
const MaxHeap = require('./src/maxHeap');
const { MinHeap } = require('./src/minHeap');
const { MaxHeap } = require('./src/maxHeap');

exports.MinHeap = MinHeap;
exports.MaxHeap = MaxHeap;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@datastructures-js/heap",
"version": "3.0.1",
"version": "3.1.0",
"description": "Min/Max Heap & Heap Sort implementation in javascript",
"main": "index.js",
"scripts": {
Expand Down
18 changes: 18 additions & 0 deletions src/heap.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export interface HeapNode<T extends number|string, U> {
key: T;
value: U;
}

export abstract class Heap<T extends number|string, U = undefined> {
constructor(elements?: (HeapNode<T, U> | T)[], leaf?: (HeapNode<T, U> | T));
extractRoot(): HeapNode<T, U> | T;
insert(key: T, value?: U): Heap<T, U>;
sort(): (HeapNode<T, U> | T)[];
fix(): void;
isValid(): boolean;
root(): HeapNode<T, U> | T;
leaf(): HeapNode<T, U> | T;
size(): number;
isEmpty(): boolean;
clear(): void;
}
Loading

0 comments on commit 89e6665

Please sign in to comment.