An implementation of MinHeap data structure.
npm install @utilityjs/min-heapA min-heap data structure where the smallest element is at the root.
- Min Heap Property: Smallest element always at the root
- Custom Comparator: Support for custom element comparison
- Standard Operations: Add, poll, peek, remove, find
- Extends Heap: Built on the abstract Heap class
``bash`
npm install @utilityjs/min-heap
or
`bash`
pnpm add @utilityjs/min-heap
`typescript
import { MinHeap } from "@utilityjs/min-heap";
const heap = new MinHeap
heap.add(5);
heap.add(3);
heap.add(7);
heap.add(1);
heap.peek(); // 1
heap.poll(); // 1
heap.poll(); // 3
`
`typescript
import { MinHeap } from "@utilityjs/min-heap";
interface Task {
name: string;
priority: number;
}
const heap = new MinHeap
if (a.priority === b.priority) return 0;
return a.priority < b.priority ? -1 : 1;
});
heap.add({ name: "Low", priority: 3 });
heap.add({ name: "High", priority: 1 });
heap.add({ name: "Medium", priority: 2 });
heap.poll(); // { name: "High", priority: 1 }
`
Extends Heap
#### Constructor
- new MinHeap - Creates a min-heap
with optional comparator
#### Inherited Methods
- isEmpty(): boolean - Check if the heap is emptypeek(): T | null
- - View the minimum element without removingpoll(): T | null
- - Remove and return the minimum elementadd(item: T): void
- - Add an element to the heapremove(item: T, comparator?): void
- - Remove all occurrences of an elementfind(item: T, comparator?): number[]
- - Find all indices of an elementtoString(): string` - String representation of the heap
-
Read the
contributing guide
to learn about our development process, how to propose bug fixes and
improvements, and how to build and test your changes.
This project is licensed under the terms of the
MIT license.