@datastructures-js/heap

a javascript implementation for Heap data structure & Heap Sort algorithm.
Min Heap | Max Heap |
---|
|
|
Contents
install
npm install --save @datastructures-js/heap
require
const { MinHeap, MaxHeap, CustomHeap } = require('@datastructures-js/heap');
import
import { MinHeap, MaxHeap, CustomHeap, HeapNode } from '@datastructures-js/heap';
API
constructor
creates an empty heap. use CustomHeap when you need advanced comparison logic between heap nodes. For primitive comparisons, use MinHeap/MaxHeap.
JS
const minHeap = new MinHeap();
const maxHeap = new MaxHeap();
const customMinHeap = new CustomHeap((a, b) => a.count - b.count);
const customMaxHeap = new CustomHeap((a, b) => a.name < b.name ? 1 : -1);
TS
const minHeap = new MinHeap<number, [number, number]>();
const maxHeap = new MaxHeap<string, { name: string }>();
const customMinHeap = new CustomHeap<{ count: number }>(
(a, b) => a.count - b.count
);
const customMaxHeap = new CustomHeap<{ name: string }>(
(a, b) => a.name < b.name ? 1 : -1
);
.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). For CustomHeap, anything can be inserted as a comparator is provided to compare nodes.
MinHeap/MaxHeap
params | return | runtime |
---|
key: T (number | string)
value: U (any)
| MinHeap<T, U> | MaxHeap<T, U> | O(log(n)) |
minHeap
.insert(50)
.insert(80)
.insert(30, 'something')
.insert(90)
.insert(60, null)
.insert(40)
.insert(20, { name: 'test' });
maxHeap
.insert('m')
.insert('x')
.insert('f', 'something')
.insert('b')
.insert('z', null)
.insert('k')
.insert('c', { name: 'test' });
CustomHeap
params | return | runtime |
---|
key: T
| CustomHeap<T> | O(log(n)) |
customMaxHeap
.insert({ name: 'm' })
.insert({ name: 'x' })
.insert({ name: 'f' })
.insert({ name: 'b' })
.insert({ name: 'z' })
.insert({ name: 'k' })
.insert({ name: 'c' });
removes and returns the root node in the heap.
MinHeap/MaxHeap
return | runtime |
---|
number | string | HeapNode | O(log(n)) |
console.log(minHeap.extractRoot());
console.log(minHeap.extractRoot());
console.log(minHeap.extractRoot());
console.log(maxHeap.extractRoot());
console.log(maxHeap.extractRoot());
console.log(maxHeap.extractRoot());
CustomHeap
console.log(customMaxHeap.extractRoot());
console.log(customMaxHeap.extractRoot());
console.log(customMaxHeap.extractRoot());
.root()
returns the root node without removing it.
MinHeap/MaxHeap
return | runtime |
---|
number | string | HeapNode | O(1) |
console.log(minHeap.root());
console.log(maxHeap.root());
CustomHeap
console.log(customMaxHeap.root());
.leaf()
returns a node with max key in MinHeap, or with min key in MaxHeap.
MinHeap/MaxHeap
return | runtime |
---|
number | string | HeapNode | O(1) |
console.log(minHeap.leaf());
console.log(maxHeap.leaf());
CustomHeap
console.log(customMaxHeap.leaf());
.size()
returns the number of nodes in the heap.
console.log(minHeap.size());
console.log(maxHeap.size());
console.log(customMaxHeap.size());
.clone()
creates a shallow copy of the heap.
return | runtime |
---|
MinHeap | MaxHeap | CustomHeap | O(n) |
const minHeapClone = minHeap.clone();
minHeapClone.extractRoot();
console.log(minHeapClone.root());
console.log(minHeap.root());
const maxHeapClone = maxHeap.clone();
maxHeapClone.extractRoot();
console.log(maxHeapClone.root());
console.log(maxHeap.root());
const customMaxHeapClone = customMaxHeap.clone();
customMaxHeap.extractRoot();
console.log(customMaxHeap.root());
console.log(customMaxHeapClone.root());
.isValid()
checks if the heap is valid.
return | runtime |
---|
boolean | O(log(n)) |
console.log(minHeap.isValid());
console.log(maxHeap.isValid());
console.log(customMaxHeap.isValid());
.fix()
fixes a heap by making the necessary changes in node positions.
return | runtime |
---|
MinHeap | MaxHeap | CustomHeap | O(log(n)) |
console.log(minHeap.fix().isValid());
console.log(maxHeap.fix().isValid());
console.log(customMaxHeap.fix().isValid());
.sort()
implements Heap Sort and sorts a Max Heap in ascending order or a Min Heap in descending order.
MinHeap/MaxHeap
return | runtime |
---|
array<number|string|HeapNode> | O(n*log(n)) |
note: calling .sort() directly on a heap will mutate its nodes location. To avoid that, you might either sort a shallow copy. You can also use use .fix() to fix the mutated heap positions
console.log(maxHeap.clone().sort());
console.log(maxHeap.root());
console.log(minHeap.sort());
console.log(minHeap.isValid());
minHeap.fix();
console.log(minHeap.isValid());
To sort a list of elements directtly using Heap Sort, it can be done like:
const ascSorted = MaxHeap.heapify([3, 7, 2, 10, 4, 9, 8, 5, 1, 6]).sort();
const descSorted = MinHeap.heapify([3, 7, 2, 10, 4, 9, 8, 5, 1, 6]).sort();
CustomHeap
return | runtime |
---|
array<T> | O(n*log(n)) |
console.log(customMaxHeap.clone().sort());
.clear()
clears the nodes in the heap.
minHeap.clear();
maxHeap.clear();
console.log(minHeap.size());
console.log(minHeap.root());
console.log(customMaxHeap.size());
console.log(customMaxHeap.root());
Heap.heapify(list)
Heapifies an existing list. It returns a heap instance as well as changing the list positions properly.
MinHeap/MaxHeap
params | return | runtime |
---|
list: array<number | string | HeapNode> | MinHeap | MaxHeap | O(n) |
JS
const numList = [50, 80, 30, 90, 60, 40, 20];
MinHeap.heapify(numList);
console.log(numList);
const strList = ['m', 'x', 'f', 'b', 'z', 'k', 'c'];
const maxHeap = MaxHeap.heapify(strList);
console.log(strList);
console.log(maxHeap.isValid());
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);
TS
const numList = [50, 80, 30, 90, 60, 40, 20];
MinHeap.heapify<number>(numList);
console.log(numList);
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<number, string>(objList);
console.log(objList);
CustomHeap
params | return | runtime |
---|
list: array<T>
comparator: (a: T, b: T) => number
| CustomHeap<T> | O(n) |
const counts = [
{ count: 50 },
{ count: 80 },
{ count: 30 },
{ count: 90 },
{ count: 60 },
{ count: 40 },
{ count: 20 }
];
CustomHeap.heapify<{ count: number }>(counts, (a, b) => a.count - b.count);
console.log(counts);
Heap.isHeapified(list)
Checks if a given list is heapified.
MinHeap/MaxHeap
params | return | runtime |
---|
list: array<number | string | HeapNode>
| boolean | O(log(n)) |
JS
MinHeap.isHeapified([50, 80, 30, 90, 60, 40, 20]);
MinHeap.isHeapified([20, 60, 30, 90, 80, 50, 40]);
MaxHeap.isHeapified(['m', 'x', 'f', 'b', 'z', 'k', 'c']);
MaxHeap.isHeapified(['z', 'x', 'k', 'b', 'm', 'f', 'c']);
TS
MinHeap.isHeapified<number>([20, 60, 30, 90, 80, 50, 40]);
MaxHeap.isHeapified<string>(['z', 'x', 'k', 'b', 'm', 'f', 'c']);
CustomHeap
params | return | runtime |
---|
list: array<T>
comparator: (a: T, b: T) => number
| boolean | O(log(n)) |
const counts = [
{ count: 20 },
{ count: 60 },
{ count: 30 },
{ count: 90 },
{ count: 80 },
{ count: 50 },
{ count: 40 }
];
console.log(CustomHeap.isHeapified<{ count: number }>(counts, (a, b) => a.count - b.count));
Build
grunt build
License
The MIT License. Full License is here