Class BinaryTree<K, V, R, NODE, TREE>

  1. Two Children Maximum: Each node has at most two children.
  2. Left and Right Children: Nodes have distinct left and right children.
  3. Depth and Height: Depth is the number of edges from the root to a node; height is the maximum depth in the tree.
  4. Subtrees: Each child of a node forms the root of a subtree.
  5. Leaf Nodes: Nodes without children are leaves.

Type Parameters

Hierarchy

Implements

  • IBinaryTree<K, V, R, NODE, TREE>

Constructors

  • The constructor function initializes a binary tree object with optional keysOrNodesOrEntriesOrRawElements and options.

    Type Parameters

    Parameters

    • Optional keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>> = []

      Optional iterable of KeyOrNodeOrEntry objects. These objects represent the nodes to be added to the binary tree.

    • Optional options: BinaryTreeOptions<K, V, R>

      The options parameter is an optional object that can contain additional configuration options for the binary tree. In this case, it is of type Partial<BinaryTreeOptions>, which means that not all properties of BinaryTreeOptions are required.

    Returns BinaryTree<K, V, R, NODE, TREE>

Accessors

  • get NIL(): NODE
  • The function returns the value of the _NIL property.

    Returns NODE

    The method is returning the value of the _NIL property.

  • get root(): undefined | null | NODE
  • The function returns the root node, which can be of type NODE, null, or undefined.

    Returns undefined | null | NODE

    The method is returning the value of the _root property, which can be of type NODE, null, or undefined.

  • get size(): number
  • The function returns the size of an object.

    Returns number

    The size of the object, which is a number.

  • get toEntryFn(): undefined | ((rawElement) => BTNEntry<K, V>)
  • The function returns the value of the _toEntryFn property.

    Returns undefined | ((rawElement) => BTNEntry<K, V>)

    The function being returned is this._toEntryFn.

Methods

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

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

    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 additional arguments to the _getIterator method.

    Returns IterableIterator<[K, undefined | V]>

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

    Parameters

    • node: undefined | null | NODE

      The node parameter represents a node in a binary tree. It can be of type NODE, null, or undefined.

    • options: BinaryTreePrintOptions

      The options parameter is an object that contains the following properties:

    Returns NodeDisplayLayout

    The function _displayAux returns a NodeDisplayLayout which is an array containing the following elements:

    1. mergedLines: An array of strings representing the lines of the node display.
    2. totalWidth: The total width of the node display.
    3. totalHeight: The total height of the node display.
    4. middleIndex: The index of the middle character
  • Time Complexity: O(1) Space Complexity: O(1)

    The function _ensureCallback ensures that a callback function is provided and returns it.

    Type Parameters

    • C extends BTNCallback<NODE>

    Parameters

    • identifier: undefined | null | ReturnType<C>

      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.

    • callback: C = ...

      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.

    Returns C

    the callback parameter.

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

    Parameters

    • node: undefined | null | NODE = ...

      The node parameter represents the current node in the binary search tree. It is initially set to the root node of the tree.

    Returns IterableIterator<[K, undefined | V]>

    an IterableIterator<[K, V | undefined]>.

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

    Parameters

    • oldNode: NODE

      The oldNode parameter represents the node that needs to be replaced in the tree.

    • newNode: NODE

      The newNode parameter is the node that will replace the oldNode in the tree.

    Returns NODE

    the newNode.

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

    The function sets the root property of an object to the provided value, and also updates the parent property of the new root.

    Parameters

    • v: undefined | null | NODE

      The parameter v is of type NODE | null | undefined. This means that it can accept a value of type NODE, null, or undefined.

    Returns void

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

    The function _swapProperties swaps the key-value properties between two nodes.

    Parameters

    • srcNode: R | KeyOrNodeOrEntry<K, V, NODE>

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

    • destNode: R | KeyOrNodeOrEntry<K, V, NODE>

      The destNode parameter is the node where the properties will be swapped with the srcNode.

    Returns undefined | NODE

    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(n) Space Complexity O(1)

    The add function is used to insert a new node into a binary tree, checking for duplicate keys and finding the appropriate insertion position.

    Parameters

    • keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>

      The keyOrNodeOrEntryOrRawElement parameter can accept a value of type R, which represents the key, node, entry, or raw element to be added to the tree. It can also accept a value of type KeyOrNodeOrEntry<K, V, NODE> @param {V} [value] - The valueparameter is an optional value that can be associated with the key being added to the tree. It represents the value that will be stored in the tree for the given key. @returns a boolean value. It returnstrueif the insertion is successful, andfalse` if the insertion position cannot be found or if there are duplicate keys.

    • Optional value: V

    Returns boolean

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

    The addMany function takes in an iterable of keys or nodes or entries or raw elements, and an optional iterable of values, and adds each key or node or entry with its corresponding value to a data structure, returning an array of booleans indicating whether each insertion was successful.

    Parameters

    • keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>>

      An iterable containing keys, nodes, entries, or raw elements. These elements will be added to the data structure.

    • Optional values: Iterable<undefined | V>

      An optional iterable of values that correspond to the keys or nodes or entries in the keysOrNodesOrEntriesOrRawElements parameter.

    Returns boolean[]

    The function addMany returns an array of booleans indicating whether each element was successfully added to the data structure.

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

    The bfs function performs a breadth-first search on a binary tree, calling a callback function on each node and returning an array of the results.

    Type Parameters

    • C extends BTNCallback<NODE>

    Parameters

    • Optional callback: C

      The callback parameter is a function that will be called for each node in the breadth-first search traversal. It takes a single argument, which is the current node being visited, and returns a value of any type.

    • Optional beginRoot: R | KeyOrNodeOrEntry<K, V, NODE>

      The beginRoot parameter represents the starting point of the breadth-first search. It can be either a root node of a tree or a key, node, or entry object. If no value is provided, the root property of the class is used as the default starting point.

    • Optional iterationType: IterationType

      The iterationType parameter determines the type of iteration to be performed. It can have two possible values:

    • Optional includeNull: false

      The includeNull parameter is a boolean value that determines whether or not to include null values in the breadth-first search (BFS) traversal. If includeNull is set to true, null values will be included in the traversal. If includeNull is set to false @returns The function bfsreturns an array of values that are the result of invoking thecallback` function on each node in the breadth-first order traversal of the binary tree.

    Returns ReturnType<C>[]

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

    The bfs function performs a breadth-first search on a binary tree, calling a callback function on each node and returning an array of the results.

    Type Parameters

    • C extends BTNCallback<null | NODE>

    Parameters

    • Optional callback: C

      The callback parameter is a function that will be called for each node in the breadth-first search traversal. It takes a single argument, which is the current node being visited, and returns a value of any type.

    • Optional beginRoot: R | KeyOrNodeOrEntry<K, V, NODE>

      The beginRoot parameter represents the starting point of the breadth-first search. It can be either a root node of a tree or a key, node, or entry object. If no value is provided, the root property of the class is used as the default starting point.

    • Optional iterationType: IterationType

      The iterationType parameter determines the type of iteration to be performed. It can have two possible values:

    • Optional includeNull: true

      The includeNull parameter is a boolean value that determines whether or not to include null values in the breadth-first search (BFS) traversal. If includeNull is set to true, null values will be included in the traversal. If includeNull is set to false @returns The function bfsreturns an array of values that are the result of invoking thecallback` function on each node in the breadth-first order traversal of the binary tree.

    Returns ReturnType<C>[]

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

    Clear the binary tree, removing all nodes.

    Returns void

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

    The clone function creates a deep copy of a tree object.

    Returns TREE

    The clone() method is returning a cloned instance of the TREE object.

  • Creates a new instance of BinaryTreeNode with the given key and value.

    Parameters

    • key: K

      The key for the new node.

    • Optional value: V

      The value for the new node.

    Returns NODE

    • The newly created BinaryTreeNode.
  • The function creates a binary tree with the given options.

    Parameters

    • Optional options: Partial<BinaryTreeOptions<K, V, R>>

      The options parameter is an optional object that allows you to customize the behavior of the BinaryTree class. It is of type Partial<BinaryTreeOptions>, which means that you can provide only a subset of the properties defined in the BinaryTreeOptions interface.

    Returns TREE

    a new instance of a binary tree.

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

    Type Parameters

    • C extends BTNCallback<NODE, K>

    Parameters

    • identifier: K

      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: C

      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`.

    Returns BinaryTreeDeleteResult<NODE>[]

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

    Type Parameters

    • C extends BTNCallback<NODE, NODE>

    Parameters

    • identifier: undefined | null | NODE

      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: C

      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`.

    Returns BinaryTreeDeleteResult<NODE>[]

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

    Type Parameters

    • C extends BTNCallback<NODE>

    Parameters

    • identifier: ReturnType<C>

      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.

    • callback: C

      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`.

    Returns BinaryTreeDeleteResult<NODE>[]

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

    The dfs function performs a depth-first search traversal on a binary tree, executing a callback function on each node according to a specified pattern and iteration type.

    Type Parameters

    • C extends BTNCallback<NODE>

    Parameters

    • Optional callback: C

      The callback parameter is a function that will be called for each node visited during the depth-first search. It takes a node as an argument and returns a value. The return type of the callback function is determined by the generic type C.

    • Optional pattern: DFSOrderPattern

      The pattern parameter determines the order in which the nodes are visited during the depth-first search. It can have one of the following values:

    • Optional beginRoot: R | KeyOrNodeOrEntry<K, V, NODE>

      The beginRoot parameter is the starting point of the depth-first search. It can be either a node object, a key-value pair, or a key. If it is a key or key-value pair, the method will find the corresponding node in the tree and start the search from there.

    • Optional iterationType: IterationType

      The iterationType parameter determines the type of iteration to use during the depth-first search. It can have two possible values:

    • Optional includeNull: false

      The includeNull parameter is a boolean value that determines whether or not to include null values in the depth-first search traversal. If includeNull is set to true, null values will be included in the traversal. If includeNull is set to false, null values will

    Returns ReturnType<C>[]

    an array of the return types of the callback function.

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

    The dfs function performs a depth-first search traversal on a binary tree, executing a callback function on each node according to a specified pattern and iteration type.

    Type Parameters

    • C extends BTNCallback<null | NODE>

    Parameters

    • Optional callback: C

      The callback parameter is a function that will be called for each node visited during the depth-first search. It takes a node as an argument and returns a value. The return type of the callback function is determined by the generic type C.

    • Optional pattern: DFSOrderPattern

      The pattern parameter determines the order in which the nodes are visited during the depth-first search. It can have one of the following values:

    • Optional beginRoot: R | KeyOrNodeOrEntry<K, V, NODE>

      The beginRoot parameter is the starting point of the depth-first search. It can be either a node object, a key-value pair, or a key. If it is a key or key-value pair, the method will find the corresponding node in the tree and start the search from there.

    • Optional iterationType: IterationType

      The iterationType parameter determines the type of iteration to use during the depth-first search. It can have two possible values:

    • Optional includeNull: true

      The includeNull parameter is a boolean value that determines whether or not to include null values in the depth-first search traversal. If includeNull is set to true, null values will be included in the traversal. If includeNull is set to false, null values will

    Returns ReturnType<C>[]

    an array of the return types of the callback function.

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

    The ensureNode function checks if the input is a valid node and returns it, or converts it to a node if it is a key or entry.

    Parameters

    • keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>

      The parameter keyOrNodeOrEntryOrRawElement can accept a value of type R, KeyOrNodeOrEntry<K, V, NODE>, or a raw element.

    • 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. It has a default value of 'ITERATIVE'.

    Returns undefined | null | NODE

    The function ensureNode returns either a NODE object, null, or undefined.

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

    The function returns an iterator that yields key-value pairs from the object, where the value can be undefined.

    Returns IterableIterator<[K, undefined | V]>

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

    The every function checks if every element in a collection satisfies a given condition.

    Parameters

    • predicate: EntryCallback<K, undefined | V, boolean>

      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: 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 first argument to the predicate function. If thisArg is not provided

    Returns boolean

    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.

    Parameters

    • predicate: EntryCallback<K, undefined | V, boolean>

      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: any

      The 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

    Returns TREE

    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.

    Parameters

    • callbackfn: EntryCallback<K, undefined | V, [K, undefined | V]>

      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: 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 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.

    Returns undefined | [K, undefined | V]

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

    Parameters

    • callbackfn: EntryCallback<K, undefined | V, void>

      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: any

      The 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, `

    Returns void

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

    Type Parameters

    • C extends BTNCallback<NODE, K>

    Parameters

    • identifier: K

      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: C

      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: IterationType

      The 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:

    Returns undefined | V

    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.

    Type Parameters

    • C extends BTNCallback<NODE, NODE>

    Parameters

    • identifier: undefined | null | NODE

      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: C

      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: IterationType

      The 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:

    Returns undefined | V

    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.

    Type Parameters

    • C extends BTNCallback<NODE>

    Parameters

    • identifier: ReturnType<C>

      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.

    • callback: C

      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: IterationType

      The 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:

    Returns undefined | V

    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.

    Parameters

    • dist: R | KeyOrNodeOrEntry<K, V, NODE>

      The dist parameter can be either a R (representing a root node), or a KeyOrNodeOrEntry<K, V, NODE> (representing a key, node, or entry).

    • beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = ...

      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

    Returns number

    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.

    Parameters

    • beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = ...

      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.

    • iterationType: IterationType = ...

      The iterationType parameter determines the type of iteration used to calculate the height of the tree. It can have two possible values:

    Returns number

    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.

    Parameters

    • beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = ...

      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.

    • iterationType: IterationType = ...

      The iterationType parameter is used to specify the type of iteration to be performed. It can have two possible values:

    Returns undefined | null | NODE

    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.

    Parameters

    • beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = ...

      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.

    • iterationType: IterationType = ...

      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:

    Returns number

    The function getMinHeight returns a number, which represents the minimum height of the binary tree.

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

    The function getNode returns the first node that matches the given identifier and callback, starting from the specified root node and using the specified iteration type.

    Type Parameters

    • C extends BTNCallback<NODE, K>

    Parameters

    • identifier: K

      The identifier parameter is the value used to identify the node you want to retrieve. It can be of any type that is the return type of the C callback function, or it can be null or undefined.

    • Optional callback: C

      The callback parameter is a function that will be used to determine if a node matches the desired criteria. It should return a value that can be used to identify the node.

    • Optional beginRoot: R | KeyOrNodeOrEntry<K, V, NODE>

      The beginRoot parameter is the starting point for searching nodes in a tree structure. It can be either a root node, a key-value pair, or a node entry. If not provided, the search will start from the root of the tree.

    • Optional iterationType: IterationType

      The iterationType parameter is used to specify the type of iteration to be performed when searching for nodes. It can have one of the following values:

    Returns undefined | null | NODE

    The method is returning a NODE object, or null, or undefined.

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

    The function getNode returns the first node that matches the given identifier and callback, starting from the specified root node and using the specified iteration type.

    Type Parameters

    • C extends BTNCallback<NODE, NODE>

    Parameters

    • identifier: undefined | null | NODE

      The identifier parameter is the value used to identify the node you want to retrieve. It can be of any type that is the return type of the C callback function, or it can be null or undefined.

    • Optional callback: C

      The callback parameter is a function that will be used to determine if a node matches the desired criteria. It should return a value that can be used to identify the node.

    • Optional beginRoot: R | KeyOrNodeOrEntry<K, V, NODE>

      The beginRoot parameter is the starting point for searching nodes in a tree structure. It can be either a root node, a key-value pair, or a node entry. If not provided, the search will start from the root of the tree.

    • Optional iterationType: IterationType

      The iterationType parameter is used to specify the type of iteration to be performed when searching for nodes. It can have one of the following values:

    Returns undefined | null | NODE

    The method is returning a NODE object, or null, or undefined.

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

    The function getNode returns the first node that matches the given identifier and callback, starting from the specified root node and using the specified iteration type.

    Type Parameters

    • C extends BTNCallback<NODE>

    Parameters

    • identifier: ReturnType<C>

      The identifier parameter is the value used to identify the node you want to retrieve. It can be of any type that is the return type of the C callback function, or it can be null or undefined.

    • callback: C

      The callback parameter is a function that will be used to determine if a node matches the desired criteria. It should return a value that can be used to identify the node.

    • Optional beginRoot: R | KeyOrNodeOrEntry<K, V, NODE>

      The beginRoot parameter is the starting point for searching nodes in a tree structure. It can be either a root node, a key-value pair, or a node entry. If not provided, the search will start from the root of the tree.

    • Optional iterationType: IterationType

      The iterationType parameter is used to specify the type of iteration to be performed when searching for nodes. It can have one of the following values:

    Returns undefined | null | NODE

    The method is returning a NODE object, or null, or undefined.

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

    The function getNodeByKey returns a node with a specific key value from a tree structure.

    Parameters

    • key: K

      The key parameter is the value that you want to search for in the tree. It is used to find the node with the matching key value.

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

    Returns undefined | null | NODE

    a value of type NODE, null, or undefined.

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

    The function getNodes returns an array of nodes that match a given identifier, using either a recursive or iterative approach.

    Type Parameters

    • C extends BTNCallback<NODE, K>

    Parameters

    • identifier: K

      The identifier parameter is the value that is used to identify the nodes. It can be of any type and is used to match against the result of the callback function for each node.

    • Optional callback: C

      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 a default callback function (this._DEFAULT_CALLBACK) if not provided.

    • Optional onlyOne: boolean

      A boolean value indicating whether to return only one node that matches the identifier or all nodes that match the identifier. 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.

    • Optional beginRoot: R | KeyOrNodeOrEntry<K, V, NODE>

      The beginRoot parameter is the starting point for the search. It can be either a node object, a key-value pair, or a key. If it is not provided, the root of the data structure is used as the starting point.

    • Optional iterationType: IterationType

      The iterationType parameter determines the type of iteration to be performed on the nodes of a binary tree. It can have two possible values:

    Returns NODE[]

    an array of NODE objects.

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

    The function getNodes returns an array of nodes that match a given identifier, using either a recursive or iterative approach.

    Type Parameters

    • C extends BTNCallback<NODE, NODE>

    Parameters

    • identifier: undefined | null | NODE

      The identifier parameter is the value that is used to identify the nodes. It can be of any type and is used to match against the result of the callback function for each node.

    • Optional callback: C

      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 a default callback function (this._DEFAULT_CALLBACK) if not provided.

    • Optional onlyOne: boolean

      A boolean value indicating whether to return only one node that matches the identifier or all nodes that match the identifier. 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.

    • Optional beginRoot: R | KeyOrNodeOrEntry<K, V, NODE>

      The beginRoot parameter is the starting point for the search. It can be either a node object, a key-value pair, or a key. If it is not provided, the root of the data structure is used as the starting point.

    • Optional iterationType: IterationType

      The iterationType parameter determines the type of iteration to be performed on the nodes of a binary tree. It can have two possible values:

    Returns NODE[]

    an array of NODE objects.

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

    The function getNodes returns an array of nodes that match a given identifier, using either a recursive or iterative approach.

    Type Parameters

    • C extends BTNCallback<NODE>

    Parameters

    • identifier: ReturnType<C>

      The identifier parameter is the value that is used to identify the nodes. It can be of any type and is used to match against the result of the callback function for each node.

    • callback: C

      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 a default callback function (this._DEFAULT_CALLBACK) if not provided.

    • Optional onlyOne: boolean

      A boolean value indicating whether to return only one node that matches the identifier or all nodes that match the identifier. 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.

    • Optional beginRoot: R | KeyOrNodeOrEntry<K, V, NODE>

      The beginRoot parameter is the starting point for the search. It can be either a node object, a key-value pair, or a key. If it is not provided, the root of the data structure is used as the starting point.

    • Optional iterationType: IterationType

      The iterationType parameter determines the type of iteration to be performed on the nodes of a binary tree. It can have two possible values:

    Returns NODE[]

    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.

    Parameters

    • beginNode: R | KeyOrNodeOrEntry<K, V, NODE>

      The beginNode parameter can be either of type R or KeyOrNodeOrEntry<K, V, NODE>.

    • Optional isReverse: boolean = true

      The 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

    Returns NODE[]

    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.

    Parameters

    • node: NODE

      The parameter "node" is of type "NODE", which represents a node in a binary tree.

    Returns NODE

    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.

    Parameters

    • beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = ...

      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.

    • iterationType: IterationType = ...

      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:

    Returns undefined | null | NODE

    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.

    Parameters

    • Optional x: null | K | NODE

      The parameter x can be of type K, NODE, or null.

    Returns undefined | null | NODE

    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.

    Type Parameters

    • C extends BTNCallback<NODE, K>

    Parameters

    • identifier: K

      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: C

      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: IterationType

      The iterationType parameter is used to specify the type of iteration to be performed. It is an optional parameter with a default value of IterationType.

    Returns boolean

    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.

    Type Parameters

    • C extends BTNCallback<NODE, NODE>

    Parameters

    • identifier: undefined | null | NODE

      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: C

      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: IterationType

      The iterationType parameter is used to specify the type of iteration to be performed. It is an optional parameter with a default value of IterationType.

    Returns boolean

    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.

    Type Parameters

    • C extends BTNCallback<NODE>

    Parameters

    • identifier: undefined | null | ReturnType<C>

      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.

    • callback: C

      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: IterationType

      The iterationType parameter is used to specify the type of iteration to be performed. It is an optional parameter with a default value of IterationType.

    Returns boolean

    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.

    Parameters

    • value: undefined | V

      The parameter "value" is the value that we want to check if it exists in the collection.

    Returns boolean

    a boolean value, either true or false.

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

    The function isBST checks if a binary search tree is valid, either recursively or iteratively.

    Parameters

    • beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = ...

      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

    • iterationType: IterationType = ...

      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:

    Returns boolean

    a boolean value.

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

    Check if the binary tree is empty.

    Returns boolean

    • True if the binary tree is empty, false otherwise.
  • The function checks if the input is an array with two elements, indicating it is a binary tree node entry.

    Parameters

    • keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>

      The parameter keyOrNodeOrEntryOrRawElement can be of type R or KeyOrNodeOrEntry<K, V, NODE>.

    Returns keyOrNodeOrEntryOrRawElement is BTNEntry<K, V>

    a boolean value.

  • The function checks if a given value is a valid key by evaluating its type and value.

    Parameters

    • key: any

      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 = true

      The 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().

    Returns key is K

    a boolean value.

  • The function checks if a given node is equal to the NIL value.

    Parameters

    • node: R | KeyOrNodeOrEntry<K, V, NODE>

      The parameter node can be of type R or KeyOrNodeOrEntry<K, V, NODE>.

    Returns boolean

    a boolean value.

  • The function checks if the input is an instance of the BinaryTreeNode class.

    Parameters

    • keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>

      The parameter keyOrNodeOrEntryOrRawElement can be of type R or KeyOrNodeOrEntry<K, V, NODE>.

    Returns keyOrNodeOrEntryOrRawElement is NODE

    a boolean value indicating whether the input parameter keyOrNodeOrEntryOrRawElement is an instance of the BinaryTreeNode class.

  • The function checks if a given node is a real node or null.

    Parameters

    • node: R | KeyOrNodeOrEntry<K, V, NODE>

      The parameter node can be of type R or KeyOrNodeOrEntry<K, V, NODE>.

    Returns node is null | NODE

    a boolean value.

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

    Parameters

    • beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = ...

      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

    Returns boolean

    a boolean value.

  • The function checks if a given node is a valid node in a binary search tree.

    Parameters

    • node: R | KeyOrNodeOrEntry<K, V, NODE>

      The parameter node can be of type R or KeyOrNodeOrEntry<K, V, NODE>.

    Returns node is NODE

    a boolean value.

  • The function keyValueOrEntryOrRawElementToNode converts a key-value pair, entry, or raw element into a node object.

    Parameters

    • keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>

      The parameter keyOrNodeOrEntryOrRawElement can be of type R or KeyOrNodeOrEntry<K, V, NODE>.

    • Optional value: V

      The value parameter is an optional value that can be passed to the keyValueOrEntryOrRawElementToNode function. It represents the value associated with a key in a key-value pair. If provided, it will be used to create a node with the specified key and value.

    Returns undefined | null | NODE

    The function keyValueOrEntryOrRawElementToNode returns either a NODE object, null, or undefined.

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

    The listLevels function returns an array of arrays, where each inner array represents a level in a binary tree and contains the results of applying a callback function to the nodes at that level.

    Type Parameters

    • C extends BTNCallback<NODE>

    Parameters

    • Optional callback: C

      The callback parameter is a function that will be called for each node in the tree. It takes a node as an argument and returns a value. The return type of the callback function is determined by the generic type C which extends BTNCallback<NODE | null>.

    • Optional beginRoot: R | KeyOrNodeOrEntry<K, V, NODE>

      The beginRoot parameter represents the starting point for traversing the tree. It can be either a root node, a key-value pair, or a node entry. If no value is provided, the root property of the class is used as the default starting point.

    • Optional iterationType: IterationType

      The iterationType parameter determines the type of iteration to be performed on the binary tree. It can have two possible values:

    • Optional includeNull: false

      The includeNull parameter is a boolean value that determines whether or not to include null values in the resulting levels. If includeNull is set to true, null values will be included in the levels. If includeNull is set to false, null values will be excluded

    Returns ReturnType<C>[][]

    The function listLevels returns a two-dimensional array of type ReturnType<C>[][].

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

    The listLevels function returns an array of arrays, where each inner array represents a level in a binary tree and contains the results of applying a callback function to the nodes at that level.

    Type Parameters

    • C extends BTNCallback<null | NODE>

    Parameters

    • Optional callback: C

      The callback parameter is a function that will be called for each node in the tree. It takes a node as an argument and returns a value. The return type of the callback function is determined by the generic type C which extends BTNCallback<NODE | null>.

    • Optional beginRoot: R | KeyOrNodeOrEntry<K, V, NODE>

      The beginRoot parameter represents the starting point for traversing the tree. It can be either a root node, a key-value pair, or a node entry. If no value is provided, the root property of the class is used as the default starting point.

    • Optional iterationType: IterationType

      The iterationType parameter determines the type of iteration to be performed on the binary tree. It can have two possible values:

    • Optional includeNull: true

      The includeNull parameter is a boolean value that determines whether or not to include null values in the resulting levels. If includeNull is set to true, null values will be included in the levels. If includeNull is set to false, null values will be excluded

    Returns ReturnType<C>[][]

    The function listLevels returns a two-dimensional array of type ReturnType<C>[][].

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

    Parameters

    • callback: EntryCallback<K, undefined | V, V>

      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: 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 TREE

    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.

    Type Parameters

    • C extends BTNCallback<NODE>

    Parameters

    • callback: C = ...

      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:

    • beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = ...

      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.

    Returns ReturnType<C>[]

    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 print function in TypeScript prints the binary tree structure with customizable options.

    Parameters

    • beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = ...

      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: BinaryTreePrintOptions

      The options parameter is an optional object that allows you to customize the printing behavior. It has the following properties:

    Returns void

    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.

    Type Parameters

    • U

    Parameters

    • callbackfn: ReduceEntryCallback<K, undefined | V, U>

      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.

    • initialValue: U

      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.

    Returns U

    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.

    Parameters

    • keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>>

      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.

    Returns void

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

    Parameters

    • predicate: EntryCallback<K, undefined | V, boolean>

      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: 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 first argument to the predicate function. If thisArg is

    Returns boolean

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

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

    The function returns an iterator that yields the values of a collection.

    Returns IterableIterator<undefined | V>

Generated using TypeDoc