addressable-binary-heaps
Version:
A versatile TypeScript library for addressable binary heaps, delivering optimized and scalable min-heap and max-heap implementations, seamlessly supporting both object-oriented and functional paradigms.
115 lines (112 loc) • 4.21 kB
TypeScript
import { IHeapNode } from '../types.js';
import { AbstractHeap } from './AbstractHeap.js';
/**
* A concrete implementation of a max-heap data structure.
*
* In a max-heap, for any given node, the node's key value is greater than or equal
* to the key values of its children.
*
* @template N - The type of nodes in the heap, must implement `IHeapNode` interface.
*/
declare class MaxHeap<N extends IHeapNode = IHeapNode> extends AbstractHeap<N> {
#private;
/**
* Creates a new `MaxHeap` instance.
*
* @param [initialNodes] - Optional array of elements to initialize the heap with.
*/
constructor(initialNodes?: N[] | Readonly<N[]>);
/**
* Gets the current number of elements in the max-heap.
*
* @returns The number of elements in the heap.
*/
get size(): number;
/**
* Makes the `MaxHeap` iterable.
*
* *Note*: The traversal follows the order of the underlying array, not the priority order.
*
* @param [reversed=false] - If `true`, the iterator will traverse the heap in reverse order.
* @returns An iterator yielding heap elements in the specified order.
*/
[Symbol.iterator](reversed?: boolean): Generator<N, void, unknown>;
/**
* Adds a new element to the heap while maintaining the max-heap property.
*
* @param node - The element to add to the heap.
*/
add(node: N): void;
/**
* Clears the heap by removing all elements.
*/
clear(): void;
/**
* Decreases the key value of a heap element by a specified amount.
*
* @param node - The element to modify.
* @param decreaseValue - Amount to decrease the key by.
* @returns `true` if element was found and modified, `false` otherwise.
*/
decrease(node: N, decreaseValue: number): boolean;
/**
* Returns an iterator for traversing all elements in the max-heap.
*
* *Note*: The traversal follows the order of the underlying array, not the priority order.
*
* @param [reversed=false] - If `true`, the iterator will traverse the heap in reverse order.
* @returns An iterator yielding heap elements in the specified order.
*/
entries(reversed?: boolean): Generator<N, void, unknown>;
/**
* Executes a callback function for each element in the heap.
*/
forEach(
/**
* @param node - Element of each iteration.
* @param index - The index of the current element being processed in the heap.
* @param heapInstance - The heap instance being iterated.
*/
callback: (node: N, index: number, heapInstance: typeof this) => void,
/**
* @param [thisArg] - A value to use as `this` when executing `callback`.
*/
thisArg?: any): void;
/**
* Increases the key value of a heap element by a specified amount.
*
* @param node - The element to modify.
* @param increaseValue - Amount to increase the key by.
* @returns `true` if element was found and modified, `false` otherwise.
*/
increase(node: N, increaseValue: number): boolean;
/**
* Returns an iterator for traversing just the key values in the max-heap.
*
* *Note*: The traversal follows the order of the underlying array, not the priority order.
*
* @param [reversed=false] - If `true`, the iterator will traverse the heap in reverse order.
* @returns An iterator yielding heap element keys in the specified order.
*/
keys(reversed?: boolean): Generator<number, void, unknown>;
/**
* Returns the maximum element (root) of the max-heap without removing it.
*
* @returns The maximum element or `undefined` if the heap is empty.
*/
peek(): N | undefined;
/**
* Removes and returns the maximum element (root) from the max-heap.
*
* @returns The maximum element or `undefined` if the heap is empty.
*/
pop(): N | undefined;
/**
* Removes a specific element from anywhere in the max-heap.
*
* @param node - The element to remove.
* @returns `true` if element was found and removed, `false` otherwise.
*/
remove(node: N): boolean;
}
export { MaxHeap };