A generic binary heap implementation that provides both Min Heap and Max Heap variants with efficient operations.
import { MinHeap, MaxHeap } from 'jsr:@mskr/data-structures';
const minHeap = new MinHeap<number>();
const maxHeap = new MaxHeap<number>();
size: number
- Number of elements in the heapisEmpty(): boolean
- Whether the heap is empty
// Insert element - O(log n)
heap.insert(value);
// Remove root element - O(log n)
const root = heap.remove();
// Peek at root element - O(1)
const top = heap.peek();
// Check if element exists - O(n)
const exists = heap.contains(value);
// Remove all elements - O(1)
heap.clear();
// Convert to array (level-order) - O(n)
const array = heap.toArray();
// Level-order traversal
for (const value of heap) {
console.log(value);
}
const minHeap = new MinHeap<number>();
// Insert elements
minHeap.insert(5);
minHeap.insert(3);
minHeap.insert(7);
console.log(minHeap.peek()); // 3 (minimum element)
console.log(minHeap.remove()); // 3
console.log(minHeap.peek()); // 5 (new minimum)
const maxHeap = new MaxHeap<number>();
// Insert elements
maxHeap.insert(5);
maxHeap.insert(3);
maxHeap.insert(7);
console.log(maxHeap.peek()); // 7 (maximum element)
console.log(maxHeap.remove()); // 7
console.log(maxHeap.peek()); // 5 (new maximum)
interface Person {
name: string;
age: number;
}
const byAge = (a: Person, b: Person) => a.age - b.age;
const minHeap = new MinHeap<Person>(byAge);
minHeap.insert({ name: 'Alice', age: 25 });
minHeap.insert({ name: 'Bob', age: 20 });
minHeap.insert({ name: 'Charlie', age: 30 });
console.log(minHeap.peek()); // { name: "Bob", age: 20 }
// Create a heap with initial elements O(n)
const minHeap = new MinHeap<number>(null, [5, 3, 8, 1, 7]);
console.log(minHeap.peek()); // 1 (minimum element)
console.log(minHeap.size); // 5
// Similarly for MaxHeap
const maxHeap = new MaxHeap<number>(null, [5, 3, 8, 1]);
console.log(maxHeap.peek()); // 8
console.log(maxHeap.size); // 4
class ComparablePerson implements Comparable<ComparablePerson> {
constructor(public name: string, public age: number) {}
compareTo(other: ComparablePerson): number {
return this.age - other.age;
}
}
const heap = new MinHeap<ComparablePerson>();
heap.insert(new ComparablePerson('Alice', 25));
try {
const empty = new MinHeap<number>();
empty.remove(); // Throws EmptyStructureError
} catch (error) {
if (error instanceof EmptyStructureError) {
console.log('Heap is empty!');
}
}
- Insert: O(log n)
- Remove root: O(log n)
- Peek: O(1)
- Contains: O(n)
- Space complexity: O(n)
- Build Heap from Array: O(n)
The heap is implemented as a complete binary tree stored in an array, where for any given node at index i:
- Left child is at index: 2i + 1
- Right child is at index: 2i + 2
- Parent is at index: floor((i-1)/2)
When creating a heap with an initial array of elements, the construction is optimized to use an O(n) algorithm. This means that initializing a heap with an array is significantly more efficient than inserting elements one by one(takes O(n log n)), providing a performant way to create heaps from existing collections.
- Generic type support with type safety
- Custom comparator support
- Built-in support for numbers, strings, and Comparable objects
- Efficient heap property maintenance
- Iterator implementation for level-order traversal