Class MaxHeap<E, R>


Type Parameters

  • E = any
  • R = any

    Max-oriented binary heap. Notes and typical use-cases are documented in Heap.

    1. Complete Binary Tree: Heaps are typically complete binary trees, meaning every level is fully filled except possibly for the last level, which has nodes as far left as possible.
    2. Heap Properties: The value of each parent node is greater than or equal to the value of its children.
    3. Root Node Access: In a heap, the largest element (in a max heap) or the smallest element (in a min heap) is always at the root of the tree.
    4. Efficient Insertion and Deletion: Due to its structure, a heap allows for insertion and deletion operations in logarithmic time (O(log n)).
    5. Managing Dynamic Data Sets: Heaps effectively manage dynamic data sets, especially when frequent access to the largest or smallest elements is required.
    6. Non-linear Search: While a heap allows rapid access to its largest or smallest element, it is less efficient for other operations, such as searching for a specific element, as it is not designed for these tasks.
    7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
    8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum-spanning tree algorithm, which use heaps to improve performance.

Hierarchy (view full)

Constructors

  • Create a max-heap. For objects, supply a custom comparator.

    Type Parameters

    • E = any
    • R = any

    Parameters

    • elements: Iterable<E, any, any> | Iterable<R, any, any> = []

      Optional initial elements.

    • Optionaloptions: HeapOptions<E, R>

      Optional configuration.

    Returns MaxHeap<E, R>

Properties

_toElementFn?: ((rawElement: R) => E)

The converter used to transform a raw element (R) into a public element (E).

Time O(1), Space O(1).

Accessors

  • get comparator(): Comparator<E>
  • Get the comparator used to order elements.

    Returns Comparator<E>

    Comparator function.

    Time O(1), Space O(1)

  • get elements(): E[]
  • Get the backing array of the heap.

    Returns E[]

    Internal elements array.

    Time O(1), Space O(1)

  • get leaf(): undefined | E
  • Get the last leaf element.

    Returns undefined | E

    Last element or undefined.

    Time O(1), Space O(1)

  • get size(): number
  • Get the number of elements.

    Returns number

    Heap size.

    Time O(1), Space O(1)

  • get toElementFn(): undefined | ((rawElement: R) => E)
  • Exposes the current toElementFn, if configured.

    Returns undefined | ((rawElement: R) => E)

    The converter function or undefined when not set.

    Time O(1), Space O(1).

Methods

  • (Protected) Create an empty instance of the same concrete class.

    Parameters

    • Optionaloptions: HeapOptions<E, R>

      Options to override comparator or toElementFn.

    Returns this

    A like-kind empty heap instance.

    Time O(1), Space O(1)

  • (Protected) Create a like-kind instance seeded by elements.

    Type Parameters

    • EM
    • RM

    Parameters

    • Optionalelements: Iterable<EM, any, any> | Iterable<RM, any, any> = []

      Iterable of elements or raw values to seed.

    • Optionaloptions: HeapOptions<EM, RM>

      Options forwarded to the constructor.

    Returns Heap<EM, RM>

    A like-kind heap instance.

    Time O(N log N), Space O(N)

  • Internal iterator factory used by the default iterator.

    Returns IterableIterator<E, any, any>

    An iterator over elements.

    Implementations should yield in O(1) per element with O(1) extra space when possible.

  • (Protected) Spawn an empty like-kind heap instance.

    Type Parameters

    • EM
    • RM

    Parameters

    • Optionaloptions: HeapOptions<EM, RM>

      Options forwarded to the constructor.

    Returns Heap<EM, RM>

    An empty like-kind heap instance.

    Time O(1), Space O(1)

  • Returns an iterator over the structure's elements.

    Parameters

    • Rest...args: unknown[]

      Optional iterator arguments forwarded to the internal iterator.

    Returns IterableIterator<E, any, any>

    An IterableIterator<E> that yields the elements in traversal order.

    Producing the iterator is O(1); consuming the entire iterator is Time O(n) with O(1) extra space.

  • Insert an element.

    Parameters

    • element: E

      Element to insert.

    Returns boolean

    True.

    Time O(1) amortized, Space O(1)

  • Insert many elements from an iterable.

    Parameters

    • elements: Iterable<E | R, any, any>

      Iterable of elements or raw values.

    Returns boolean[]

    Array of per-element success flags.

    Time O(N log N), Space O(1)

  • Delete one occurrence of an element.

    Parameters

    • element: E

      Element to delete.

    Returns boolean

    True if an element was removed.

    Time O(N), Space O(1)

  • Delete the first element that matches a predicate.

    Parameters

    • predicate: ((element: E, index: number, heap: this) => boolean)

      Function (element, index, heap) → boolean.

        • (element, index, heap): boolean
        • Parameters

          • element: E
          • index: number
          • heap: this

          Returns boolean

    Returns boolean

    True if an element was removed.

    Time O(N), Space O(1)

  • Traverse the binary heap as a complete binary tree and collect elements.

    Parameters

    • Optionalorder: DFSOrderPattern = 'PRE'

      Traversal order: 'PRE' | 'IN' | 'POST'.

    Returns E[]

    Array of visited elements.

    Time O(N), Space O(H)

  • Tests whether all elements satisfy the predicate.

    Parameters

    • predicate: ElementCallback<E, R, boolean>

      Function invoked for each element with signature (value, index, self).

    • OptionalthisArg: unknown

      Optional this binding for the predicate.

    Returns boolean

    true if every element passes; otherwise false.

    Time O(n) in the worst case; may exit early when the first failure is found. Space O(1).

  • Filter elements into a new heap of the same class.

    Parameters

    • callback: ElementCallback<E, R, boolean>

      Predicate (element, index, heap) → boolean to keep element.

    • OptionalthisArg: unknown

      Value for this inside the callback.

    Returns this

    A new heap with the kept elements.

    Time O(N log N), Space O(N)

  • Finds the first element that satisfies the predicate and returns it.

    Finds the first element of type S (a subtype of E) that satisfies the predicate and returns it.

    Type Parameters

    • S

    Parameters

    • predicate: ElementCallback<E, R, S>

      Type-guard predicate: (value, index, self) => value is S.

    • OptionalthisArg: unknown

      Optional this binding for the predicate.

    Returns undefined | S

    The matched element typed as S, or undefined if not found.

    Time O(n) in the worst case; may exit early on the first match. Space O(1).

  • Restore heap order bottom-up (heapify in-place).

    Returns boolean[]

    Array of per-node results from fixing steps.

    Time O(N), Space O(1)

  • Invokes a callback for each element in iteration order.

    Parameters

    • callbackfn: ElementCallback<E, R, void>

      Function invoked per element with signature (value, index, self).

    • OptionalthisArg: unknown

      Optional this binding for the callback.

    Returns void

    void.

    Time O(n), Space O(1).

  • Check if an equal element exists in the heap.

    Parameters

    • element: E

      Element to search for.

    Returns boolean

    True if found.

    Time O(N), Space O(1)

  • Map elements into a new heap of possibly different element type.

    Type Parameters

    • EM
    • RM

    Parameters

    • callback: ElementCallback<E, R, EM>

      Mapping function (element, index, heap) → newElement.

    • options: IterableElementBaseOptions<EM, RM> & {
          comparator?: Comparator<EM>;
      } & {
          comparator: Comparator<EM>;
      }

      Options for the output heap, including comparator for EM.

    • OptionalthisArg: unknown

      Value for this inside the callback.

    Returns Heap<EM, RM>

    A new heap with mapped elements.

    Time O(N log N), Space O(N)

  • Map elements into a new heap of the same element type.

    Parameters

    • callback: ElementCallback<E, R, E>

      Mapping function (element, index, heap) → element.

    • OptionalthisArg: unknown

      Value for this inside the callback.

    Returns this

    A new heap with mapped elements.

    Time O(N log N), Space O(N)

  • Get the current top element without removing it.

    Returns undefined | E

    Top element or undefined.

    Time O(1), Space O(1)

  • Replace the backing array and rebuild the heap.

    Parameters

    • elements: Iterable<E, any, any>

      Iterable used to refill the heap.

    Returns boolean[]

    Array of per-node results from fixing steps.

    Time O(N), Space O(N)

  • Set the equality comparator used by has/delete operations.

    Parameters

    • equals: ((a: E, b: E) => boolean)

      Equality predicate (a, b) → boolean.

        • (a, b): boolean
        • Parameters

          Returns boolean

    Returns this

    This heap.

    Time O(1), Space O(1)

  • Tests whether at least one element satisfies the predicate.

    Parameters

    • predicate: ElementCallback<E, R, boolean>

      Function invoked for each element with signature (value, index, self).

    • OptionalthisArg: unknown

      Optional this binding for the predicate.

    Returns boolean

    true if any element passes; otherwise false.

    Time O(n) in the worst case; may exit early on first success. Space O(1).

  • Return all elements in ascending order by repeatedly polling.

    Returns E[]

    Sorted array of elements.

    Time O(N log N), Space O(N)

  • Returns a representation of the structure suitable for quick visualization. Defaults to an array of elements; subclasses may override to provide richer visuals.

    Returns E[]

    A visual representation (array by default).

    Time O(n), Space O(n).

  • Returns an iterator over the values (alias of the default iterator).

    Returns IterableIterator<E, any, any>

    An IterableIterator<E> over all elements.

    Creating the iterator is O(1); full iteration is Time O(n), Space O(1).

  • Create a heap of the same class from an iterable.

    Type Parameters

    Parameters

    • this: Object
    • Optionalelements: Iterable<T | R, any, any>

      Iterable of elements or raw records.

    • Optionaloptions: HeapOptions<T, R>

      Heap options including comparator.

    Returns S

    A new heap instance of this class.

    Time O(N), Space O(N)

  • Build a Heap from an iterable in linear time given a comparator.

    Type Parameters

    • EE = unknown
    • RR = never

    Parameters

    • elements: Iterable<EE, any, any>

      Iterable of elements.

    • options: HeapOptions<EE, RR>

      Heap options including comparator.

    Returns Heap<EE, RR>

    A new Heap built from elements.

    Time O(N), Space O(N)