Class PriorityQueue<E, R>

  1. Element Priority: In a PriorityQueue, elements are sorted according to their priority. Each dequeue (element removal) operation removes the element with the highest priority. The priority can be determined based on the natural ordering of the elements or through a provided comparator (Comparator).
  2. Heap-Based Implementation: PriorityQueue is typically implemented using a binary heap, allowing both insertion and removal operations to be completed in O(log n) time, where n is the number of elements in the queue.
  3. Task Scheduling: In systems where tasks need to be processed based on the urgency of tasks rather than the order of arrival.
  4. Dijkstra's Algorithm: In shortest path algorithms for graphs, used to select the next shortest edge to visit.
  5. Huffman Coding: Used to select the smallest node combination when constructing a Huffman tree.
  6. Kth Largest Element in a Data Stream: Used to maintain a min-heap of size K for quickly finding the Kth largest element in stream data

Type Parameters

  • E = any

  • R = any

Hierarchy

Constructors

  • The constructor initializes a priority queue with optional elements and options.

    Type Parameters

    • E = any

    • R = any

    Parameters

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

      The elements parameter is an iterable object that contains the initial elements to be added to the priority queue. It is an optional parameter, and if not provided, the priority queue will be initialized as empty.

    • Optional options: PriorityQueueOptions<E, R>

      The options parameter is an optional object that can be used to customize the behavior of the priority queue. It can contain the following properties:

    Returns PriorityQueue<E, R>

Accessors

  • get comparator(): Comparator<E>
  • The function returns the value of the _comparator property.

    Returns Comparator<E>

    The _comparator property is being returned.

  • get elements(): E[]
  • The function returns an array of elements.

    Returns E[]

    The element array is being returned.

  • get leaf(): undefined | E
  • Get the last element in the heap, which is not necessarily a leaf node.

    Returns undefined | E

    The last element or undefined if the heap is empty.

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

    Returns number

  • get toElementFn(): undefined | ((rawElement) => E)
  • The function returns the _toElementFn property, which is a function that converts a raw element to a specific type.

    Returns undefined | ((rawElement) => E)

    The function get toElementFn() is returning either a function that takes a raw element rawElement of type R and returns an element E, or undefined if no function is assigned to _toElementFn.

Methods

  • Time Complexity: O(n) Space Complexity: O(1)

    The function is an implementation of the Symbol.iterator method that returns an IterableIterator.

    Parameters

    • Rest ...args: any[]

      The args parameter in the code snippet represents a rest parameter. It allows the function to accept any number of arguments as an array. In this case, the args parameter is used to pass any number of arguments to the _getIterator method.

    Returns IterableIterator<E>

  • Time Complexity: O(log n) Space Complexity: O(1)

    Float operation to maintain heap properties after adding an element.

    Parameters

    • index: number

      The index of the newly added element.

    Returns boolean

  • The function _getIterator returns an iterable iterator for the elements in the class.

    Returns IterableIterator<E>

  • Time Complexity: O(log n) Space Complexity: O(1)

    Sinking operation to maintain heap properties after removing the top element.

    Parameters

    • index: number

      The index from which to start sinking.

    • halfLength: number

    Returns boolean

  • Time Complexity: O(log n) Space Complexity: O(1)

    Insert an element into the heap and maintain the heap properties.

    Parameters

    • element: E

      The element to be inserted.

    Returns boolean

  • Reset the elements of the heap. Make the elements empty.

    Returns void

  • The clone function returns a new instance of the PriorityQueue class with the same comparator and toElementFn as the original instance.

    Returns PriorityQueue<E, R>

    The method is returning a new instance of the PriorityQueue class with the same elements and properties as the current instance.

  • Time Complexity: O(n) Space Complexity: O(1)

    The delete function removes an element from an array-like data structure, maintaining the order and structure of the remaining elements.

    Parameters

    • element: E

      The element parameter represents the element that you want to delete from the array this.elements.

    Returns boolean

    The delete function is returning a boolean value. It returns true if the element was successfully deleted from the array, and false if the element was not found in the array.

  • Time Complexity: O(n) Space Complexity: O(log n)

    Depth-first search (DFS) method, different traversal orders can be selected。

    Parameters

    • order: DFSOrderPattern = 'PRE'

      Traverse order parameter: 'IN' (in-order), 'PRE' (pre-order) or 'POST' (post-order).

    Returns E[]

    An array containing elements traversed in the specified order.

  • Time Complexity: O(n) Space Complexity: O(1)

    The every function checks if every element in the array satisfies a given predicate.

    Parameters

    • predicate: ElementCallback<E, R, boolean, Heap<E, R>>

      The predicate parameter is a callback function that takes three arguments: the current element being processed, its index, and the array it belongs to. It should return a boolean value indicating whether the element satisfies a certain condition or not.

    • Optional thisArg: any

      The thisArg parameter is an optional argument that specifies the value to be used as this when executing the predicate function. If thisArg is provided, it will be passed as the this value to the predicate function. If thisArg is

    Returns boolean

    The every method is returning a boolean value. It returns true if every element in the array satisfies the provided predicate function, and false otherwise.

  • Time Complexity: O(n) Space Complexity: O(n)

    The filter function creates a new PriorityQueue object containing elements that pass a given callback function.

    Parameters

    • callback: ElementCallback<E, R, boolean, PriorityQueue<E, R>>

      The callback parameter is a function that will be called for each element in the heap. It takes three arguments: the current element, the index of the current element, and the heap itself. The callback function should return a boolean value indicating whether the current element should be included in the filtered list

    • Optional thisArg: any

      The thisArg parameter is an optional argument that specifies the value to be used as this when executing the callback function. If thisArg is provided, it will be passed as the this value to the callback function. If thisArg is

    Returns PriorityQueue<E, R>

    The filter method is returning a new PriorityQueue object that contains the elements that pass the filter condition specified by the callback function.

  • Time Complexity: O(n) Space Complexity: O(1)

    The find function iterates over the elements of an array-like object and returns the first element that satisfies the provided callback function.

    Parameters

    • callbackfn: ElementCallback<E, R, boolean, Heap<E, R>>

      The callbackfn parameter is a function that will be called for each element in the array. It takes three arguments: the current element being processed, the index of the current element, and the array itself. The function should return a boolean value indicating whether the current element matches the desired condition.

    • Optional thisArg: any

      The thisArg parameter is an optional argument that specifies the value to be used as this when executing the callbackfn function. If thisArg is provided, it will be passed as the this value to the callbackfn function. If thisArg @returns The findmethod returns the first element in the array that satisfies the provided callback function. If no element satisfies the callback function,undefined` is returned.

    Returns undefined | E

  • Time Complexity: O(n log n) Space Complexity: O(n)

    Fix the entire heap to maintain heap properties.

    Returns boolean[]

  • Time Complexity: O(n) Space Complexity: O(1)

    The forEach function iterates over each element in an array-like object and calls a callback function for each element.

    Parameters

    • callbackfn: ElementCallback<E, R, void, Heap<E, R>>

      The callbackfn parameter is a function that will be called for each element in the array. It takes three arguments: the current element being processed, the index of the current element, and the array that forEach was called upon.

    • Optional thisArg: any

      The thisArg parameter is an optional argument that specifies the value to be used as this when executing the callbackfn function. If thisArg is provided, it will be passed as the this value to the callbackfn function. If `thisArg

    Returns void

  • Time Complexity: O(n) Space Complexity: O(1)

    Use a comparison function to check whether a binary heap contains a specific element.

    Parameters

    • element: E

      the element to check.

    Returns boolean

    Returns true if the specified element is contained; otherwise, returns false.

  • Check if the heap is empty.

    Returns boolean

    True if the heap is empty, otherwise false.

  • Time Complexity: O(n log n) Space Complexity: O(n)

    The map function creates a new heap by applying a callback function to each element of the original heap.

    Type Parameters

    • EM

    • RM

    Parameters

    • callback: ElementCallback<E, R, EM, PriorityQueue<E, R>>

      The callback parameter is a function that will be called for each element in the heap. It takes three arguments: el (the current element), index (the index of the current element), and this (the heap itself). The callback function should return a value of

    • comparator: Comparator<EM>

      The comparator parameter is a function that defines the order of the elements in the heap. It takes two elements a and b as arguments and returns a negative number if a should be placed before b, a positive number if a should be placed after

    • Optional toElementFn: ((rawElement) => EM)

      The toElementFn parameter is an optional function that converts the raw element RR to the desired type T. It takes a single argument rawElement of type RR and returns a value of type T. This function is used to transform the elements of the original

        • (rawElement): EM
        • Parameters

          • rawElement: RM

          Returns EM

    • Optional thisArg: any

      The thisArg parameter is an optional argument that allows you to specify the value of this within the callback function. It is used to set the context or scope in which the callback function will be executed. If thisArg is provided, it will be used as the value of

    Returns PriorityQueue<EM, RM>

    a new instance of the PriorityQueue class with the mapped elements.

  • Time Complexity: O(1) Space Complexity: O(1)

    Peek at the top element of the heap without removing it.

    Returns undefined | E

    The top element or undefined if the heap is empty.

  • Time Complexity: O(log n) Space Complexity: O(1)

    Remove and return the top element (the smallest or largest element) from the heap.

    Returns undefined | E

    The top element or undefined if the heap is empty.

  • Time Complexity: O(n) Space Complexity: O(1)

    The reduce function iterates over the elements of an array-like object and applies a callback function to reduce them into a single value.

    Type Parameters

    • U

    Parameters

    • callbackfn: ReduceElementCallback<E, R, U, Heap<E, R>>

      The callbackfn parameter is a function that will be called for each element in the array. It takes four arguments:

    • initialValue: U

      The initialValue parameter is the initial value of the accumulator. It is the value that the accumulator starts with before the reduction operation begins.

    Returns U

    The reduce method is returning the final value of the accumulator after iterating over all the elements in the array and applying the callback function to each element.

  • Time Complexity: O(n) Space Complexity: O(n)

    Clear and add elements of the heap

    Parameters

    • elements: E[]

    Returns boolean[]

  • Time Complexity: O(n) Space Complexity: O(1)

    The "some" function checks if at least one element in a collection satisfies a given predicate.

    Parameters

    • predicate: ElementCallback<E, R, boolean, Heap<E, R>>

      The predicate parameter is a callback function that takes three arguments: value, index, and array. It should return a boolean value indicating whether the current element satisfies the condition.

    • Optional thisArg: any

      The thisArg parameter is an optional argument that specifies the value to be used as the this value when executing the predicate function. If thisArg is provided, it will be passed as the this value to the predicate function. If `thisArg

    Returns boolean

    a boolean value. It returns true if the predicate function returns true for any element in the collection, and false otherwise.

  • Time Complexity: O(n log n) Space Complexity: O(n)

    Sort the elements in the heap and return them as an array.

    Returns E[]

    An array containing the elements sorted in ascending order.

  • Time Complexity: O(n) Space Complexity: O(n)

    Convert the heap to an array.

    Returns E[]

    An array containing the elements of the heap.

  • Time Complexity: O(n) Space Complexity: O(n)

    The function returns an iterator that yields all the values in the object.

    Returns IterableIterator<E>

  • Static method that creates a binary heap from an array of elements and a comparison function.

    Type Parameters

    • E = any

    • R = any

    Parameters

    • elements: Iterable<E>
    • options: HeapOptions<E, R>

    Returns Heap<E, any>

    A new Heap instance.

Generated using TypeDoc