This is the constructor function for a Binary Search Tree class in TypeScript.
The keysOrNodesOrEntriesOrRawElements
parameter is an
iterable that can contain either keys, nodes, entries, or raw elements. These elements will be
added to the binary search tree during the construction of the object.
Optional
options: BSTOptions<K, V, R>An optional object that contains additional options for the Binary Search Tree. It can include a comparator function that defines the order of the elements in the tree.
The function returns the value of the _NIL property.
The method is returning the value of the _NIL
property.
The function returns the value of the _comparator property.
The _comparator
property is being returned.
The function returns the root node of a tree structure.
The _root
property of the object, which is of type NODE
or undefined
.
The function returns the size of an object.
The size of the object, which is a number.
The function returns the value of the _toEntryFn property.
The function being returned is this._toEntryFn
.
Time Complexity: O(n) Space Complexity: O(1)
The function is an implementation of the Symbol.iterator method that returns an iterable iterator.
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 additional arguments to the _getIterator
method.
Protected
_displayTime Complexity: O(n) Space Complexity: O(n)
The _displayAux
function is responsible for generating the display layout of a binary tree node,
taking into account various options such as whether to show null, undefined, or NaN nodes.
The node
parameter represents a node in a binary tree.
It can be of type NODE
, null
, or undefined
.
The options
parameter is an object that contains the
following properties:
The function _displayAux
returns a NodeDisplayLayout
which is an array containing the
following elements:
mergedLines
: An array of strings representing the lines of the node display.totalWidth
: The total width of the node display.totalHeight
: The total height of the node display.middleIndex
: The index of the middle characterProtected
_ensureTime Complexity: O(1) Space Complexity: O(1)
The function _ensureCallback
ensures that a callback function is provided and returns it.
The identifier
parameter is of type
ReturnType<C> | null | undefined
. This means it can accept a value that is the return type of
the generic type C
, or it can be null
or undefined
.
The callback
parameter is a function that takes a node
as an argument
and returns a value. It is of type C
, which is a generic type that extends the
BTNCallback<NODE>
type.
the callback parameter.
Protected
_getTime Complexity: O(1) Space Complexity: O(1)
The function _getIterator
is a generator function that returns an iterator for the key-value
pairs in a binary search tree.
The node
parameter represents the current node in the binary search tree. It is
initially set to the root node of the tree.
an IterableIterator<[K, V | undefined]>.
Protected
_replaceTime Complexity: O(1) Space Complexity: O(1)
The function replaces a node in a binary tree with a new node, updating the parent, left child, right child, and root if necessary.
The oldNode parameter represents the node that needs to be replaced in the tree.
The newNode
parameter is the node that will replace the oldNode
in the
tree.
the newNode.
Protected
_setProtected
_swapTime Complexity: O(1) Space Complexity: O(1)
The function _swapProperties
swaps the key-value properties between two nodes.
The source node that will be swapped with the
destination node. It can be either an instance of the class R
, or an object of type
KeyOrNodeOrEntry<K, V, NODE>
.
The destNode
parameter is the node where
the properties will be swapped with the srcNode
.
either the destNode
object with its properties swapped with the srcNode
object's
properties, or undefined
if either srcNode
or destNode
is falsy.
Time Complexity: O(log n) Space Complexity: O(1)
The add
function in TypeScript adds a new node to a binary search tree based on the key value.
The parameter
keyOrNodeOrEntryOrRawElement
can accept a value of type R
or KeyOrNodeOrEntry<K, V, NODE>
.
Optional
value: VThe value
parameter is an optional value that can be associated with the
key in the binary search tree. If provided, it will be stored in the node along with the key.
a boolean value.
Time Complexity: O(k log n) Space Complexity: O(k + log n)
The addMany
function in TypeScript adds multiple keys or nodes to a data structure and returns
an array indicating whether each key or node was successfully inserted.
An iterable containing keys, nodes, entries, or raw elements to be added to the data structure.
Optional
values: Iterable<undefined | V>An optional iterable of values to be associated with the keys or nodes being added. If provided, the values will be assigned to the corresponding keys or nodes in the same order. If not provided, undefined will be assigned as the value for each key or node.
Optional
isBalanceAdd: boolean = trueA boolean flag indicating whether the tree should be balanced after adding the elements. If set to true, the tree will be balanced using a binary search tree algorithm. If set to false, the elements will be added without balancing the tree. The default value is true.
The iterationType
parameter is an optional parameter that
specifies the type of iteration to use when adding multiple keys or nodes to the binary search
tree. It can have two possible values:
The function addMany
returns an array of booleans indicating whether each element was
successfully inserted into the data structure.
Time complexity: O(n) Space complexity: O(n)
The function overrides the breadth-first search method and returns an array of the return types of the callback function.
The callback
parameter is a function that will be called for each node
visited during the breadth-first search. It should take a single argument, which is the current
node being visited, and it can return a value of any type.
The beginRoot
parameter is the starting
point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
object. If no value is provided, the default value is the root of the tree.
The iterationType
parameter is used to specify the type
of iteration to be performed during the breadth-first search (BFS) traversal. It can have one of
the following values:
an array of the return type of the callback function.
The function creates a new BSTNode with the given key and value and returns it.
The key parameter is of type K, which represents the type of the key for the node being created.
Optional
value: VThe "value" parameter is an optional parameter of type V. It represents the value associated with the key in the node being created.
The method is returning a new instance of the BSTNode class, casted as the NODE type.
The function creates a new binary search tree with the specified options.
Optional
options: Partial<BSTOptions<K, V, R>>The options
parameter is an optional object that allows you to customize the
behavior of the createTree
method. It accepts a partial BSTOptions
object, which has the
following properties:
a new instance of the BST class with the provided options.
Time Complexity: O(n) Space Complexity: O(1)
The above function is a TypeScript implementation of deleting a node from a binary tree, returning the deleted node and the node that needs to be balanced.
The identifier
parameter is the value
used to identify the node that needs to be deleted from the binary tree. It can be of any type
that is returned by the callback function.
Optional
callback: CThe callback
parameter is a function that is used to determine the
identifier of the node to be deleted. It is of type C
, which extends the BTNCallback<NODE>
interface. The BTNCallback<NODE>
interface represents a callback function that takes a node of
type NODE @returns an array of
BinaryTreeDeleteResult
Time Complexity: O(n) Space Complexity: O(1)
The above function is a TypeScript implementation of deleting a node from a binary tree, returning the deleted node and the node that needs to be balanced.
The identifier
parameter is the value
used to identify the node that needs to be deleted from the binary tree. It can be of any type
that is returned by the callback function.
Optional
callback: CThe callback
parameter is a function that is used to determine the
identifier of the node to be deleted. It is of type C
, which extends the BTNCallback<NODE>
interface. The BTNCallback<NODE>
interface represents a callback function that takes a node of
type NODE @returns an array of
BinaryTreeDeleteResult
Time Complexity: O(n) Space Complexity: O(1)
The above function is a TypeScript implementation of deleting a node from a binary tree, returning the deleted node and the node that needs to be balanced.
The identifier
parameter is the value
used to identify the node that needs to be deleted from the binary tree. It can be of any type
that is returned by the callback function.
The callback
parameter is a function that is used to determine the
identifier of the node to be deleted. It is of type C
, which extends the BTNCallback<NODE>
interface. The BTNCallback<NODE>
interface represents a callback function that takes a node of
type NODE @returns an array of
BinaryTreeDeleteResult
Time complexity: O(n) Space complexity: O(n)
The function overrides the depth-first search method and returns an array of the return types of the callback function.
The callback
parameter is a function that will be called for each node
during the depth-first search traversal. It is an optional parameter and defaults to
this._DEFAULT_CALLBACK
. The type C
represents the type of the callback function.
Optional
pattern: DFSOrderPattern = 'IN'The "pattern" parameter in the code snippet refers to the order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can take one of the following values:
The beginRoot
parameter is the starting
point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
node entry. If not specified, the default value is the root of the tree.
Optional
iterationType: IterationType = 'ITERATIVE'The iterationType
parameter specifies the
type of iteration to be used during the Depth-First Search (DFS) traversal. It can have one of the
following values:
The method is returning an array of the return type of the callback function.
Time Complexity: O(log n) Space Complexity: O(log n)
The function ensures the existence of a node in a data structure and returns it, or undefined if it doesn't exist.
The parameter
keyOrNodeOrEntryOrRawElement
can accept a value of type R
, which represents the key, node,
entry, or raw element that needs to be ensured in the tree.
Optional
iterationType: IterationType = 'ITERATIVE'The iterationType
parameter is an optional
parameter that specifies the type of iteration to be used when ensuring a node. It has a default
value of 'ITERATIVE'
.
The method is returning either the node that was ensured or undefined
if the node could
not be ensured.
Time Complexity: O(n) Space Complexity: O(1)
The every
function checks if every element in a collection satisfies a given condition.
The predicate
parameter is a callback function that takes three arguments:
value
, key
, and index
. It should return a boolean value indicating whether the condition is
met for the current element in the iteration.
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 first argument to the predicate
function. If thisArg
is not provided
The every
method is returning a boolean value. It returns true
if every element in
the collection satisfies the provided predicate function, and false
otherwise.
Time Complexity: O(n) Space Complexity: O(n)
The filter
function creates a new tree with entries that pass a given predicate function.
The predicate
parameter is a callback function that is used to test each
element in the tree. It takes three arguments: value
, key
, and index
. The value
argument
represents the value of the current element being processed, the key
argument represents the key
of the
Optional
thisArg: anyThe thisArg
parameter is an optional argument that allows you to
specify the value of this
within the predicate
function. When the predicate
function is
called, thisArg
will be used as the value of this
within the function. If thisArg
The filter
method is returning a new tree object that contains the entries that pass
the given predicate function.
Time Complexity: O(n) Space Complexity: O(1)
The find
function iterates over the entries of a collection and returns the first value for
which the callback function returns true.
The callback function that will be called for each entry in the collection. It takes three arguments: the value of the entry, the key of the entry, and the index of the entry in the collection. It should return a boolean value indicating whether the current entry 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 method
findreturns the value of the first element in the iterable 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 key-value pair in a collection and executes a callback
function for each pair.
The callback function that will be called for each element in the collection. It takes four parameters: the value of the current element, the key of the current element, the index of the current element, and the collection itself.
Optional
thisArg: anyThe thisArg
parameter is an optional argument that allows you to
specify the value of this
within the callback function. If thisArg
is provided, it will be
used as the this
value when calling the callback function. If thisArg
is not provided, `
Time Complexity: O(n) Space Complexity: O(log n)
The function get
in TypeScript overrides the base class method and returns the value associated
with the given identifier.
The identifier
parameter is the value
used to identify the node in the binary tree. It can be of any type that is returned by the
callback function C
. It can also be null
or undefined
if no identifier is provided.
Optional
callback: CThe callback
parameter is a function that will be used to determine if a
node matches the given identifier. It is optional and defaults to this._DEFAULT_CALLBACK
.
Optional
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE>The beginRoot
parameter is the starting
point for the search in the binary tree. It can be either a root node of the tree or a key, node,
or entry object that exists in the tree. If no specific starting point is provided, the search
will begin from the root of the
Optional
iterationType: IterationTypeThe iterationType
parameter is used to specify the type
of iteration to be performed when searching for a node in the tree. It can have one of the
following values:
The method is returning the value associated with the specified identifier in the binary tree.
Time Complexity: O(n) Space Complexity: O(log n)
The function get
in TypeScript overrides the base class method and returns the value associated
with the given identifier.
The identifier
parameter is the value
used to identify the node in the binary tree. It can be of any type that is returned by the
callback function C
. It can also be null
or undefined
if no identifier is provided.
Optional
callback: CThe callback
parameter is a function that will be used to determine if a
node matches the given identifier. It is optional and defaults to this._DEFAULT_CALLBACK
.
Optional
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE>The beginRoot
parameter is the starting
point for the search in the binary tree. It can be either a root node of the tree or a key, node,
or entry object that exists in the tree. If no specific starting point is provided, the search
will begin from the root of the
Optional
iterationType: IterationTypeThe iterationType
parameter is used to specify the type
of iteration to be performed when searching for a node in the tree. It can have one of the
following values:
The method is returning the value associated with the specified identifier in the binary tree.
Time Complexity: O(n) Space Complexity: O(log n)
The function get
in TypeScript overrides the base class method and returns the value associated
with the given identifier.
The identifier
parameter is the value
used to identify the node in the binary tree. It can be of any type that is returned by the
callback function C
. It can also be null
or undefined
if no identifier is provided.
The callback
parameter is a function that will be used to determine if a
node matches the given identifier. It is optional and defaults to this._DEFAULT_CALLBACK
.
Optional
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE>The beginRoot
parameter is the starting
point for the search in the binary tree. It can be either a root node of the tree or a key, node,
or entry object that exists in the tree. If no specific starting point is provided, the search
will begin from the root of the
Optional
iterationType: IterationTypeThe iterationType
parameter is used to specify the type
of iteration to be performed when searching for a node in the tree. It can have one of the
following values:
The method is returning the value associated with the specified identifier in the binary tree.
Time Complexity: O(n) Space Complexity: O(1)
The function calculates the depth of a given node or key in a tree-like data structure.
The dist
parameter can be either a R
(representing a root node), or a KeyOrNodeOrEntry<K, V, NODE>
(representing a key, node, or
entry).
The beginRoot
parameter is optional and
represents the starting point from which to calculate the depth. It can be either a reference to a
node in the tree or a key-value pair or an entry object. If not provided, the default value is
this.root
, which refers to the root node
the depth of a node in a tree structure.
Time Complexity: O(n) Space Complexity: O(1)
The getHeight
function calculates the maximum height of a binary tree using either a recursive
or iterative approach.
The beginRoot
parameter represents the
starting point for calculating the height of a tree. It can be either a root node (R
), a key or
node or entry (KeyOrNodeOrEntry<K, V, NODE>
), or it defaults to the root of the current tree.
The iterationType
parameter determines the type of
iteration used to calculate the height of the tree. It can have two possible values:
the maximum height of the binary tree.
Time Complexity: O(log n) Space Complexity: O(1)
The getLeftMost
function returns the leftmost node in a binary tree, either using recursive or
iterative traversal.
The beginRoot
parameter represents the
starting point for finding the leftmost node in a binary tree. It can be either a root node (R
),
a key or node or entry (KeyOrNodeOrEntry<K, V, NODE>
), or null
or undefined
.
The iterationType
parameter is used to specify the type
of iteration to be performed. It can have two possible values:
The function getLeftMost
returns the leftmost node in a binary tree.
Time Complexity: O(n) Space Complexity: O(log n)
The getMinHeight
function calculates the minimum height of a binary tree using either a
recursive or iterative approach.
The beginRoot
parameter represents the
starting point for calculating the minimum height of a tree. It can be either a root node (R
), a
key or node or entry (KeyOrNodeOrEntry<K, V, NODE>
), or it defaults to the root of the current
tree.
The iterationType
parameter determines the type of
iteration to be used when calculating the minimum height of the tree. It can have two possible
values:
The function getMinHeight
returns a number, which represents the minimum height of the
binary tree.
Time Complexity: O(log n) Space Complexity: O(1)
The function getNode
returns the first node that matches the given identifier and callback
function in a binary search tree.
The identifier
parameter is the value that you
want to search for in the binary search tree. It can be of any type that is compatible with the
type returned by the callback function.
The callback
parameter is a function that will be used to determine if a
node matches the desired criteria. It should be a function that takes a node as an argument and
returns a boolean value indicating whether the node matches the criteria or not. If no callback is
provided, the default callback will be
The beginRoot
parameter is the starting point for the search in the binary
search tree. It can be either a key or a node. If it is a key, the search will start from the node
with that key. If it is a node, the search will start from that node.
The iterationType
parameter is used to specify the type
of iteration to be performed when searching for nodes in the binary search tree. It can have one
of the following values:
The method is returning a NODE object or undefined.
Time Complexity: O(log n) Space Complexity: O(1)
The function getNodeByKey
returns a node with a specific key from a tree data structure.
The key parameter is the value used to search for a specific node in the tree. It is typically a unique identifier or a value that can be used to determine the position of the node in the tree structure.
Optional
iterationType: IterationType = 'ITERATIVE'The iterationType
parameter is an optional
parameter that specifies the type of iteration to be used when searching for a node in the tree.
It has a default value of 'ITERATIVE'
.
The method is returning a NODE object or undefined.
Time Complexity: O(log n) Space Complexity: O(k + log n)
The getNodes
function in TypeScript retrieves nodes from a binary tree based on a given
identifier and callback function.
The identifier
parameter is the value that you
want to search for in the binary tree. It can be of any type that is returned by the callback
function.
The callback
parameter is a function that takes a node as input and
returns a value. This value is used to identify the nodes that match the given identifier. The
callback
function is optional and defaults to this._DEFAULT_CALLBACK
.
Optional
onlyOne: boolean = falseA boolean value indicating whether to return only the first matching node or all matching nodes. If set to true, only the first matching node will be returned. If set to false, all matching nodes will be returned. The default value is false.
The beginRoot
parameter is the starting
point for the search in the binary tree. It can be either a node object, a key-value pair, or an
entry object. If it is not provided, the root
of the binary tree is used as the starting point.
The iterationType
parameter determines the type of
iteration to be performed. It can have two possible values:
The method getNodes
returns an array of NODE
objects.
Time Complexity: O(log n) Space Complexity: O(log n)
The function getPathToRoot
returns an array of nodes starting from a given node and traversing
up to the root node, with an option to reverse the order of the nodes.
The beginNode
parameter can be either of
type R
or KeyOrNodeOrEntry<K, V, NODE>
.
Optional
isReverse: boolean = trueThe isReverse
parameter is a boolean flag that determines whether the
resulting path should be reversed or not. If isReverse
is set to true
, the path will be
reversed before returning it. If isReverse
is set to false
or not provided, the path will
The function getPathToRoot
returns an array of NODE
objects.
Time Complexity: O(log n) Space Complexity: O(1)
The function returns the predecessor node of a given node in a binary tree.
The parameter "node" is of type "NODE", which represents a node in a binary tree.
the predecessor node of the given node.
Time Complexity: O(log n) Space Complexity: O(1)
The getRightMost
function returns the rightmost node in a binary tree, either recursively or
iteratively.
The beginRoot
parameter represents the
starting point for finding the rightmost node in a binary tree. It can be either a root node
(R
), a key or node or entry (KeyOrNodeOrEntry<K, V, NODE>
), or null
or undefined
.
The iterationType
parameter is used to specify the type
of iteration to be performed when finding the rightmost node in a binary tree. It can have two
possible values:
The function getRightMost
returns a NODE object, null
, or undefined
.
Time Complexity: O(log n) Space Complexity: O(1)
The function getSuccessor
returns the next node in a binary tree given a current node.
Optional
x: null | K | NODEThe parameter x
can be of type K
, NODE
, or null
.
The function getSuccessor
returns a NODE
object if a successor exists, null
if
there is no successor, and undefined
if the input x
is not a valid node.
Time Complexity: O(n) Space Complexity: O(log n)
The has
function checks if a given identifier exists in the data structure and returns a boolean
value.
The identifier
parameter is the value
used to identify a specific node or entry in the data structure. It can be of any type that is
returned by the callback function C
. It can also be null
or undefined
if no specific
identifier is provided.
Optional
callback: CThe callback
parameter is a function that will be used to determine
whether a node should be included in the result or not. It is of type C
, which extends the
BTNCallback<NODE>
type.
Optional
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE>The beginRoot
parameter is the starting
point for the iteration in the data structure. It can be either a root node, a key-value pair, or
a node entry. If not specified, it defaults to the root of the data structure.
Optional
iterationType: IterationTypeThe iterationType
parameter is used to specify the type
of iteration to be performed. It is an optional parameter with a default value of IterationType
.
The method is returning a boolean value.
Time Complexity: O(n) Space Complexity: O(log n)
The has
function checks if a given identifier exists in the data structure and returns a boolean
value.
The identifier
parameter is the value
used to identify a specific node or entry in the data structure. It can be of any type that is
returned by the callback function C
. It can also be null
or undefined
if no specific
identifier is provided.
Optional
callback: CThe callback
parameter is a function that will be used to determine
whether a node should be included in the result or not. It is of type C
, which extends the
BTNCallback<NODE>
type.
Optional
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE>The beginRoot
parameter is the starting
point for the iteration in the data structure. It can be either a root node, a key-value pair, or
a node entry. If not specified, it defaults to the root of the data structure.
Optional
iterationType: IterationTypeThe iterationType
parameter is used to specify the type
of iteration to be performed. It is an optional parameter with a default value of IterationType
.
The method is returning a boolean value.
Time Complexity: O(n) Space Complexity: O(log n)
The has
function checks if a given identifier exists in the data structure and returns a boolean
value.
The identifier
parameter is the value
used to identify a specific node or entry in the data structure. It can be of any type that is
returned by the callback function C
. It can also be null
or undefined
if no specific
identifier is provided.
The callback
parameter is a function that will be used to determine
whether a node should be included in the result or not. It is of type C
, which extends the
BTNCallback<NODE>
type.
Optional
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE>The beginRoot
parameter is the starting
point for the iteration in the data structure. It can be either a root node, a key-value pair, or
a node entry. If not specified, it defaults to the root of the data structure.
Optional
iterationType: IterationTypeThe iterationType
parameter is used to specify the type
of iteration to be performed. It is an optional parameter with a default value of IterationType
.
The method is returning a boolean value.
Time Complexity: O(n) Space Complexity: O(1)
The function checks if a given value exists in a collection.
The parameter "value" is the value that we want to check if it exists in the collection.
a boolean value, either true or false.
Time Complexity: O(n) Space Complexity: O(log n)
The function isAVLBalanced
checks if a binary tree is AVL balanced using either a recursive or
iterative approach.
The iterationType
parameter is an optional parameter that
specifies the type of iteration to use when checking if the AVL tree is balanced. It has a default
value of this.iterationType
, which means it will use the iteration type specified in the current
instance of the AVL tree.
a boolean value.
Time Complexity: O(n) Space Complexity: O(1)
The function isBST
checks if a binary search tree is valid, either recursively or iteratively.
The beginRoot
parameter represents the
starting point for checking if a binary search tree (BST) is valid. It can be either a root node
of the BST, a key value of a node in the BST, or an entry object containing both the key and value
of a node in the BST
The iterationType
parameter is used to determine the type
of iteration to be performed while checking if the binary search tree (BST) is valid. It can have
two possible values:
a boolean value.
The function checks if the input is an array with two elements, indicating it is a binary tree node entry.
The parameter
keyOrNodeOrEntryOrRawElement
can be of type R
or KeyOrNodeOrEntry<K, V, NODE>
.
a boolean value.
The function checks if a given value is a valid key by evaluating its type and value.
The key
parameter can be of any type. It is the value that we want to check
if it is a valid key.
Optional
isCheckValueOf: boolean = trueThe isCheckValueOf
parameter is a boolean flag that determines
whether the function should check the valueOf() method of an object when the key is of type
'object'. If isCheckValueOf
is true, the function will recursively call itself with the value
returned by key.valueOf().
a boolean value.
The function checks if the input is an instance of the BSTNode class.
The parameter
keyOrNodeOrEntryOrRawElement
can be of type R
or KeyOrNodeOrEntry<K, V, NODE>
.
a boolean value indicating whether the input parameter keyOrNodeOrEntryOrRawElement
is
an instance of the BSTNode
class.
Time Complexity: O(n) Space Complexity: O(log n)
The function checks if a binary tree is perfectly balanced by comparing the minimum height and the height of the tree.
The parameter beginRoot
is optional and
has a default value of this.root
. It represents the starting point for checking if the tree is
perfectly balanced. It can be either a root node (R
), a key or node or entry
(`KeyOrNodeOrEntry<K, V, NODE
a boolean value.
The function overrides a method and converts a key, value pair or entry or raw element to a node.
A variable that can be of type R or KeyOrNodeOrEntry<K, V, NODE>. It represents either a key, a node, an entry, or a raw element.
Optional
value: VThe value
parameter is an optional value of type V
. It represents the
value associated with a key in a key-value pair.
either a NODE object or undefined.
Time complexity: O(n) Space complexity: O(n)
The lesserOrGreaterTraverse
function traverses a binary tree and applies a callback function to
each node that meets a certain condition based on a target node and a comparison value.
The callback
parameter is a function that will be called for each node
that meets the condition specified by the lesserOrGreater
parameter. It takes a single argument,
which is the current node being traversed, and returns a value of any type.
The lesserOrGreater
parameter is used to determine whether to
traverse nodes that are lesser, greater, or both than the targetNode
. It accepts the values -1,
0, or 1, where:
The targetNode
parameter is the node in
the binary tree that you want to start traversing from. It can be specified either by providing
the key of the node, the node itself, or an entry containing the key and value of the node. If no
targetNode
is provided,
The iterationType
parameter determines the type of
traversal to be performed on the binary tree. It can have two possible values:
The function lesserOrGreaterTraverse
returns an array of values of type
ReturnType<C>
, which is the return type of the callback function passed as an argument.
Time complexity: O(n) Space complexity: O(n)
The function overrides the listLevels method from the superclass and returns an array of arrays containing the results of the callback function applied to each level of the tree.
The callback
parameter is a generic type C
that extends
BTNCallback<NODE>
. It represents a callback function that will be called for each node in the
tree during the iteration process.
The beginRoot
parameter is the starting
point for listing the levels of the binary tree. It can be either a root node of the tree, a
key-value pair representing a node in the tree, or a key representing a node in the tree. If no
value is provided, the root of
The iterationType
parameter is used to specify the type
of iteration to be performed on the tree. It can have one of the following values:
The method is returning a two-dimensional array of the return type of the callback function.
Time Complexity: O(n) Space Complexity: O(n)
The map
function creates a new tree by applying a callback function to each entry in the current
tree.
The callback parameter is a function that will be called for each entry in the tree. It takes three arguments: value, key, and index. The value argument represents the value of the current entry, the key argument represents the key of the current entry, and the index argument represents the index of the
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 map
method is returning a new tree object.
Time complexity: O(n) Space complexity: O(n)
The morris
function performs a depth-first traversal on a binary tree using the Morris traversal
algorithm.
The callback
parameter is a function that will be called for each node in
the tree. It takes a single argument, which is the current node, and can return any value. The
return type of the callback
function is determined by the ReturnType<C>
type, which represents
the return
Optional
pattern: DFSOrderPattern = 'IN'The pattern
parameter in the morris
function is used
to specify the order in which the nodes of a binary tree are traversed. It can take one of the
following values:
The beginRoot
parameter is the starting
point for the traversal. It can be either a node object, a key, or an entry object. If no value is
provided, the root
of the tree is used as the starting point.
The function morris
returns an array of values that are the return values of the
callback function callback
.
Time complexity: O(n) Space complexity: O(n)
The perfectlyBalance
function takes an optional iterationType
parameter and returns true
if
the binary search tree is perfectly balanced, otherwise it returns false
.
The iterationType
parameter is an optional parameter that
specifies the type of iteration to use when building a balanced binary search tree. It has a
default value of this.iterationType
, which means it will use the iteration type specified in the
current instance of the class.
The function perfectlyBalance
returns a boolean value.
Time Complexity: O(n) Space Complexity: O(n)
The print
function in TypeScript prints the binary tree structure with customizable options.
The beginRoot
parameter is the starting
point for printing the binary tree. It can be either a node of the binary tree or a key or entry
that exists in the binary tree. If no value is provided, the root of the binary tree will be used
as the starting point.
Optional
options: BinaryTreePrintOptionsThe options
parameter is an optional object that
allows you to customize the printing behavior. It has the following properties:
Nothing is being returned. The function has a return type of void
, which means it does
not return any value.
Time Complexity: O(n) Space Complexity: O(1)
The reduce
function iterates over key-value pairs and applies a callback function to each pair,
accumulating a single value.
The callback function that will be called for each element in the collection. It takes four arguments: the current accumulator value, the current value of the element, the key of the element, and the index of the element in the collection. It should return the updated accumulator value.
The initialValue
parameter is the initial value of the accumulator. It
is the value that will be used as the first argument to the callbackfn
function when reducing
the elements of the collection.
The reduce
method is returning the final value of the accumulator after iterating over
all the elements in the collection.
Time Complexity: O(k * n) Space Complexity: O(1)
The refill
function clears the current data and adds new data to the collection.
An iterable collection of keys, nodes, entries, or raw elements. These can be of any type (R) or a specific type (KeyOrNodeOrEntry<K, V, NODE>).
Optional
values: Iterable<undefined | V>The values
parameter is an optional iterable of values that will be associated
with the keys or nodes being added. If provided, the values will be assigned to the corresponding
keys or nodes. If not provided, the values will be set to undefined
.
Time Complexity: O(n) Space Complexity: O(1)
The "some" function iterates over a collection and returns true if at least one element satisfies a given predicate.
The predicate
parameter is a callback function that takes three arguments:
value
, key
, and index
. It should return a boolean value indicating whether the condition is
met for the current element in the iteration.
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 first argument to the predicate
function. If thisArg
is
a boolean value. It returns true if the predicate function returns true for any pair in the collection, and false otherwise.
Generated using TypeDoc