The constructor initializes a priority queue with optional elements and options.
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:
Get the size (number of elements) of the heap.
The function returns the _toElementFn property, which is a function that converts a raw element to a specific type.
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
.
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)
Insert an element into the heap and maintain the heap properties.
The element to be inserted.
The clone
function returns a new instance of the PriorityQueue
class with the same comparator
and toElementFn as the original instance.
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.
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 PriorityQueue 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 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.
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: 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 @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.
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 PriorityQueue
class with the mapped elements.
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.
The callbackfn parameter is a function that will be called for each element in the array. It takes four arguments:
The initialValue parameter is the initial value of the accumulator. It is the value that the accumulator starts with before the reduction operation begins.
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
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