UNPKG

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.

149 lines (145 loc) 4.83 kB
'use strict'; var minHeap = require('../core/min-heap.js'); var AbstractHeap = require('./AbstractHeap.js'); /** * A concrete implementation of a min-heap data structure. * * In a min-heap, for any given node, the node's key value is less than or equal * to the key values of its children. * * @template N - The type of nodes in the heap, must implement `IHeapNode` interface. */ class MinHeap extends AbstractHeap.AbstractHeap { /** Private field holding the internal heap data structure */ #heap; /** * Creates a new `MinHeap` instance. * * @param [initialNodes] - Optional array of elements to initialize the heap with. */ constructor(initialNodes) { super(); this.#heap = minHeap.create(initialNodes); } /** * Gets the current number of elements in the min-heap. * * @returns The number of elements in the heap. */ get size() { return minHeap.size(this.#heap); } /** * Makes the `MinHeap` 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 = false) { return minHeap.entries(this.#heap, reversed); } /** * Adds a new element to the heap while maintaining the min-heap property. * * @param node - The element to add to the heap. */ add(node) { return minHeap.add(this.#heap, node); } /** * Clears the heap by removing all elements. */ clear() { return minHeap.clear(this.#heap); } /** * 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, decreaseValue) { return minHeap.decrease(this.#heap, node, decreaseValue); } /** * Returns an iterator for traversing all elements in the min-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 = false) { return minHeap.entries(this.#heap, reversed); } /** * 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, /** * @param [thisArg] - A value to use as `this` when executing `callback`. */ thisArg) { let index = 0; for (const node of this.entries()) { callback.call(thisArg, node, index, this); index += 1; } } /** * 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, increaseValue) { return minHeap.increase(this.#heap, node, increaseValue); } /** * Returns an iterator for traversing just the key values in the min-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 = false) { return minHeap.keys(this.#heap, reversed); } /** * Returns the minimum element (root) of the min-heap without removing it. * * @returns The minimum element or `undefined` if the heap is empty. */ peek() { return minHeap.peek(this.#heap); } /** * Removes and returns the minimum element (root) from the min-heap. * * @returns The minimum element or `undefined` if the heap is empty. */ pop() { return minHeap.pop(this.#heap); } /** * Removes a specific element from anywhere in the min-heap. * * @param node - The element to remove. * @returns `true` if element was found and removed, `false` otherwise. */ remove(node) { return minHeap.remove(this.#heap, node); } } exports.MinHeap = MinHeap;