js-heap-data-structures
Version:
A comprehensive JavaScript implementation of Min Heap and Max Heap data structures with practical utilities
77 lines (76 loc) • 2.12 kB
TypeScript
export = MinHeap;
/**
* MinHeap implementation
* A binary heap where the parent node is always smaller than its children
*/
declare class MinHeap {
/**
* Build heap from array in O(n) time
* @param {Array} array - Array to build heap from
* @param {Function} compareFn - Optional comparison function
* @returns {MinHeap} New MinHeap instance
*/
static buildHeap(array: any[], compareFn?: Function): MinHeap;
constructor(compareFn?: null);
heap: any[];
compare: (a: any, b: any) => boolean;
getParentIndex(index: any): number;
getLeftChildIndex(index: any): number;
getRightChildIndex(index: any): number;
hasParent(index: any): boolean;
hasLeftChild(index: any): boolean;
hasRightChild(index: any): boolean;
parent(index: any): any;
leftChild(index: any): any;
rightChild(index: any): any;
swap(index1: any, index2: any): void;
/**
* Get minimum element (root) without removing it
* @returns {*} The minimum element
* @throws {Error} If heap is empty
*/
peek(): any;
/**
* Extract minimum element (root)
* @returns {*} The minimum element
* @throws {Error} If heap is empty
*/
extractMin(): any;
/**
* Insert new element
* @param {*} value - The value to insert
*/
insert(value: any): void;
/**
* Heapify up - maintain heap property after insertion
*/
heapifyUp(): void;
/**
* Heapify down - maintain heap property after extraction
*/
heapifyDown(): void;
/**
* Get size of heap
* @returns {number} Size of heap
*/
size(): number;
/**
* Check if heap is empty
* @returns {boolean} True if heap is empty
*/
isEmpty(): boolean;
/**
* Get array representation of heap
* @returns {Array} Array representation
*/
toArray(): any[];
/**
* Clear the heap
*/
clear(): void;
/**
* Heapify down from specific index (for buildHeap)
* @param {number} index - Starting index
*/
heapifyDownFrom(index: number): void;
}