namastey-min-heap
v1.0.0
Published
A JavaScript package implementing the Min-Heap data structure for efficient priority queue operations.
Downloads
7
Maintainers
Readme
namastey-min-heap
Brief Description
namastey-min-heap
is a JavaScript package that implements the Min-Heap data structure. It provides essential methods for inserting, removing, and accessing elements while maintaining the heap property.
Features
- insert(value): Inserts a new value into the heap while maintaining the heap property.
- extractMin(): Removes and returns the minimum value (the root) from the heap.
- heapifyUp(index): Ensures the heap property is maintained after an insertion.
- heapifyDown(index): Ensures the heap property is maintained after extraction.
- peek(): Returns the minimum value without removing it from the heap.
- size(): Returns the number of elements in the heap.
- isEmpty(): Returns
true
if the heap is empty,false
otherwise.
Installation
To install the namastey-min-heap
package globally, run the following command:
npm install -g namastey-min-heap
Examples
const MinHeap = require('namastey-min-heap');
const heap = new MinHeap();
heap.insert(10);
heap.insert(5);
heap.insert(30);
heap.insert(2);
console.log('Min element:', heap.peek()); // Output: Min element: 2
console.log('Extracted min:', heap.extractMin()); // Output: Extracted min: 2
console.log('Min element:', heap.peek()); // Output: Min element: 5
console.log('Heap size:', heap.size()); // Output: Heap size: 3