The elements
parameter is an iterable object that contains the initial
elements to be added to the priority queue. It is optional and defaults to an empty array if not
provided.
Optional
options: PriorityQueueOptions<E, R>The options
parameter is an object that contains additional configuration
options for the priority queue. In this case, it has a property called comparator,
which is a
function used to compare elements in the priority queue. The comparator
function takes two
parameters a
and b
Get the size (number of elements) of the heap.
Protected
_bubbleProtected
_getProtected
_sinkTime Complexity: O(n) Space Complexity: O(1)
The function is an implementation of the Symbol.iterator method that returns an IterableIterator.
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.
Time Complexity: O(log n) Space Complexity: O(1)
The add function pushes an element into an array and then triggers a bubble-up operation.
The element
parameter represents the element that you want to add to the
data structure.
The add
method is returning a boolean value, which is the result of calling the
_bubbleUp
method with the index this.elements.length - 1
as an argument.
Time Complexity: O(k log n) Space Complexity: O(1)
The addMany
function iterates over elements and adds them to a collection, returning an array of
boolean values indicating success or failure.
The elements
parameter in the addMany
method is
an iterable containing elements of type E
or R
. The method iterates over each element in the
iterable and adds them to the data structure. If a transformation function _toElementFn
is
provided, it transforms the element
The addMany
method returns an array of boolean values indicating whether each element
in the input iterable was successfully added to the data structure.
The clone
function returns a new instance of the MinPriorityQueue
class with the same
comparator and toElementFn as the original instance.
The method is returning a new instance of the MinPriorityQueue
class with the same
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.
The element
parameter represents the element that you want to delete from
the array this.elements
.
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。
Traverse order parameter: 'IN' (in-order), 'PRE' (pre-order) or 'POST' (post-order).
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.
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: anyThe 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
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 MinPriorityQueue object containing elements that pass a given callback
function.
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: anyThe 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
The filter
method is returning a new MinPriorityQueue
object that contains the elements that pass
the filter condition specified by the callback
function.
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.
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: anyThe 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
Time Complexity: O(n) Space Complexity: O(1)
Use a comparison function to check whether a binary heap contains a specific element.
the element to check.
Returns true if the specified element is contained; otherwise, returns 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.
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
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: RM) => 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
Optional
thisArg: anyThe 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
a new instance of the MinPriorityQueue
class with the mapped elements.
Time Complexity: O(n) Space Complexity: O(n)
Clear and add elements of the heap
Time Complexity: O(n) Space Complexity: O(1)
The "some" function checks if at least one element in a collection satisfies a given predicate.
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: anyThe 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
a boolean value. It returns true if the predicate function returns true for any element in the collection, and false otherwise.
Static
heapify
The constructor initializes a PriorityQueue with optional elements and options, including a comparator function.