UNPKG

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
export = MaxHeap; /** * MaxHeap implementation * A binary heap where the parent node is always larger than its children */ declare class MaxHeap { /** * Build heap from array in O(n) time * @param {Array} array - Array to build heap from * @param {Function} compareFn - Optional comparison function * @returns {MaxHeap} New MaxHeap instance */ static buildHeap(array: any[], compareFn?: Function): MaxHeap; 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 maximum element (root) without removing it * @returns {*} The maximum element * @throws {Error} If heap is empty */ peek(): any; /** * Extract maximum element (root) * @returns {*} The maximum element * @throws {Error} If heap is empty */ extractMax(): 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; }