Class MaxPriorityQueue<E, R>

Max-oriented priority queue (max-heap) built on PriorityQueue. The default comparator orders primitive values in descending order. If you store objects, you must provide a custom comparator via PriorityQueueOptions.


Type Parameters

  • E = any

    Element type stored in the queue.

  • R = any

    Extra record/metadata associated with each element.

Hierarchy (view full)

Constructors

  • Creates a max-priority queue.

    Type Parameters

    • E = any
    • R = any

    Parameters

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

      Optional initial elements to insert.

    • Optionaloptions: PriorityQueueOptions<E, R>

      Optional configuration (e.g., comparator, toElementFn).

    Returns MaxPriorityQueue<E, R>

    Thrown when using the default comparator with object elements (provide a custom comparator).

    Complexity — Time: O(n log n) when inserting n elements incrementally; Space: O(n).

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.

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

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

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

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

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