Addressable Binary Heaps - v1.3.0
    Preparing search index...

    Class MaxHeap<N>

    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.

    Type Parameters

    • N extends IHeapNode = IHeapNode

      The type of nodes in the heap, must implement IHeapNode interface.

    Hierarchy (View Summary)

    Index
    • Creates a new MaxHeap instance.

      Type Parameters

      • N extends IHeapNode = IHeapNode

        The type of nodes in the heap, must implement IHeapNode interface.

      Parameters

      • OptionalinitialNodes: N[] | readonly N[]

        Optional array of elements to initialize the heap with. An element that appears more than once is added once. A value that is not an array is ignored.

      Returns MaxHeap<N>

    • get size(): number

      Gets the current number of elements in the max-heap.

      Returns number

      The number of elements in the heap.

    • Makes the MaxHeap iterable.

      Note: The traversal follows the order of the underlying array, not the priority order.

      Parameters

      • Optionalreversed: boolean = false

        If true, the iterator will traverse the heap in reverse order.

      Returns Generator<N, void, unknown>

      An iterator yielding heap elements in the specified order.

    • Adds a new element to the heap while maintaining the max-heap property. An element the heap already holds is ignored, and the heap is unchanged.

      Parameters

      • node: N

        The element to add to the heap.

      Returns void

    • Decreases the key value of a heap element by a specified amount.

      Parameters

      • node: N

        The element to modify.

      • decreaseValue: number

        Amount to decrease the key by.

      Returns boolean

      true if element was found and modified, false if the heap does not hold it, the amount is not a number, or the new key would be NaN (key is then unchanged).

      TypeError if node.key is not writable, as on a frozen node or a key with a getter and no setter. The heap and key are then unchanged.

    • 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.

      Parameters

      • Optionalreversed: boolean = false

        If true, the iterator will traverse the heap in reverse order.

      Returns Generator<N, void, unknown>

      An iterator yielding heap elements in the specified order.

    • Executes a callback function for each element in the heap.

      Parameters

      • callback: (node: N, index: number, heapInstance: MaxHeap<N>) => void
          • (node: N, index: number, heapInstance: MaxHeap<N>): void
          • Parameters

            • node: N

              Element of each iteration.

            • index: number

              The index of the current element being processed in the heap.

            • heapInstance: MaxHeap<N>

              The heap instance being iterated.

            Returns void

      • OptionalthisArg: any

        A value to use as this when executing callback.

      Returns void

    • Increases the key value of a heap element by a specified amount.

      Parameters

      • node: N

        The element to modify.

      • increaseValue: number

        Amount to increase the key by.

      Returns boolean

      true if element was found and modified, false if the heap does not hold it, the amount is not a number, or the new key would be NaN (key is then unchanged).

      TypeError if node.key is not writable, as on a frozen node or a key with a getter and no setter. The heap and key are then unchanged.

    • 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.

      Parameters

      • Optionalreversed: boolean = false

        If true, the iterator will traverse the heap in reverse order.

      Returns Generator<number, void, unknown>

      An iterator yielding heap element keys in the specified order.

    • Returns the maximum element (root) of the max-heap without removing it.

      Returns N | undefined

      The maximum element or undefined if the heap is empty.

    • Removes and returns the maximum element (root) from the max-heap.

      Returns N | undefined

      The maximum element or undefined if the heap is empty.

    • Removes a specific element from anywhere in the max-heap.

      Parameters

      • node: N

        The element to remove.

      Returns boolean

      true if element was found and removed, false otherwise.