min-heap-typed
v2.6.0
Published
Min Heap
Maintainers
Keywords
Readme
What
Brief
This is a standalone Min Heap data structure from the data-structure-typed collection. If you wish to access more data structures or advanced features, you can transition to directly installing the complete data-structure-typed package
How
install
npm
npm i min-heap-typed --saveyarn
yarn add min-heap-typedsnippet
TS
import {MinHeap} from 'data-structure-typed';
// /* or if you prefer */ import {MinHeap} from 'heap-typed';
const minNumHeap = new MinHeap<number>();
minNumHeap.add(1).add(6).add(2).add(0).add(5).add(9);
minNumHeap.has(1) // true
minNumHeap.has(2) // true
minNumHeap.poll() // 0
minNumHeap.poll() // 1
minNumHeap.peek() // 2
minNumHeap.has(1); // false
minNumHeap.has(2); // true
const arrFromHeap = minNumHeap.toArray();
arrFromHeap.length // 4
arrFromHeap[0] // 2
arrFromHeap[1] // 5
arrFromHeap[2] // 9
arrFromHeap[3] // 6
minNumHeap.sort() // [2, 5, 6, 9]JS
const {MinHeap} = require('data-structure-typed');
// /* or if you prefer */ const {MinHeap} = require('heap-typed');
const minNumHeap = new MinHeap();
minNumHeap.add(1).add(6).add(2).add(0).add(5).add(9);
minNumHeap.has(1) // true
minNumHeap.has(2) // true
minNumHeap.poll() // 0
minNumHeap.poll() // 1
minNumHeap.peek() // 2
minNumHeap.has(1); // false
minNumHeap.has(2); // true
const arrFromHeap = minNumHeap.toArray();
arrFromHeap.length // 4
arrFromHeap[0] // 2
arrFromHeap[1] // 5
arrFromHeap[2] // 9
arrFromHeap[3] // 6
minNumHeap.sort() // [2, 5, 6, 9]Merge K sorted arrays
const arrays = [
[1, 4, 7],
[2, 5, 8],
[3, 6, 9]
];
// Use min heap to merge: track (value, arrayIndex, elementIndex)
const heap = new MinHeap<[number, number, number]>([], {
comparator: (a, b) => a[0] - b[0]
});
// Initialize with first element of each array
arrays.forEach((arr, i) => heap.add([arr[0], i, 0]));
const merged: number[] = [];
while (heap.size > 0) {
const [val, arrIdx, elemIdx] = heap.poll()!;
merged.push(val);
if (elemIdx + 1 < arrays[arrIdx].length) {
heap.add([arrays[arrIdx][elemIdx + 1], arrIdx, elemIdx + 1]);
}
}
console.log(merged); // [1, 2, 3, 4, 5, 6, 7, 8, 9];Dijkstra-style shortest distance tracking
// Simulating distance updates: (distance, nodeId)
const heap = new MinHeap<[number, string]>([], {
comparator: (a, b) => a[0] - b[0]
});
heap.add([0, 'start']);
heap.add([10, 'A']);
heap.add([5, 'B']);
heap.add([3, 'C']);
// Process nearest node first
console.log(heap.poll()); // [0, 'start'];
console.log(heap.poll()); // [3, 'C'];
console.log(heap.poll()); // [5, 'B'];
console.log(heap.poll()); // [10, 'A'];Running median with min heap (upper half)
const upperHalf = new MinHeap<number>();
// Add larger numbers to min heap
for (const n of [5, 8, 3, 9, 1]) {
upperHalf.add(n);
}
// Smallest of the upper half is always accessible
console.log(upperHalf.peek()); // 1;
console.log(upperHalf.size); // 5;
// Remove smallest repeatedly
console.log(upperHalf.poll()); // 1;
console.log(upperHalf.poll()); // 3;
console.log(upperHalf.peek()); // 5;API docs & Examples
Examples Repository
