red-black-tree-typed
Advanced tools
Comparing version 1.49.5 to 1.49.6
@@ -9,3 +9,3 @@ /** | ||
import { BST, BSTNode } from './bst'; | ||
import type { AVLTreeNested, AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, BTNExemplar, BTNKeyOrNode } from '../../types'; | ||
import type { AVLTreeNested, AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, KeyOrNodeOrEntry } from '../../types'; | ||
import { IBinaryTree } from '../../interfaces'; | ||
@@ -27,5 +27,5 @@ export declare class AVLTreeNode<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeNodeNested<K, V>> extends BSTNode<K, V, N> { | ||
/** | ||
* The constructor function initializes an AVLTree object with optional elements and options. | ||
* @param [elements] - The `elements` parameter is an optional iterable of `BTNExemplar<K, V, N>` | ||
* objects. It represents a collection of elements that will be added to the AVL tree during | ||
* The constructor function initializes an AVLTree object with optional nodes and options. | ||
* @param [nodes] - The `nodes` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>` | ||
* objects. It represents a collection of nodes that will be added to the AVL tree during | ||
* initialization. | ||
@@ -36,3 +36,3 @@ * @param [options] - The `options` parameter is an optional object that allows you to customize the | ||
*/ | ||
constructor(elements?: Iterable<BTNExemplar<K, V, N>>, options?: Partial<AVLTreeOptions<K>>); | ||
constructor(nodes?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: Partial<AVLTreeOptions<K>>); | ||
/** | ||
@@ -57,7 +57,7 @@ * The function creates a new AVL tree node with the specified key and value. | ||
/** | ||
* The function checks if an exemplar is an instance of AVLTreeNode. | ||
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`. | ||
* @returns a boolean value indicating whether the exemplar is an instance of the AVLTreeNode class. | ||
* The function checks if an keyOrNodeOrEntry is an instance of AVLTreeNode. | ||
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`. | ||
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeNode class. | ||
*/ | ||
isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N; | ||
isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N; | ||
/** | ||
@@ -69,10 +69,11 @@ * The function "isNotNodeInstance" checks if a potential key is a K. | ||
*/ | ||
isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K; | ||
isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K; | ||
/** | ||
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity. | ||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size. | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity. | ||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -87,10 +88,10 @@ * The function overrides the add method of a binary tree node and balances the tree after inserting | ||
*/ | ||
add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V): N | undefined; | ||
add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): boolean; | ||
/** | ||
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity. | ||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (BST) has logarithmic time complexity. | ||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -121,8 +122,9 @@ * The function overrides the delete method of a binary tree, performs the deletion, and then | ||
/** | ||
* Time Complexity: O(1) - constant time, as it performs a fixed number of operations. | ||
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* constant time, as it performs a fixed number of operations. constant space, as it only uses a constant amount of memory. | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time, as it performs a fixed number of operations. | ||
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -136,8 +138,9 @@ * The function calculates the balance factor of a node in a binary tree. | ||
/** | ||
* Time Complexity: O(1) - constant time, as it performs a fixed number of operations. | ||
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* constant time, as it performs a fixed number of operations. constant space, as it only uses a constant amount of memory. | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time, as it performs a fixed number of operations. | ||
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -150,8 +153,9 @@ * The function updates the height of a node in a binary tree based on the heights of its left and | ||
/** | ||
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The method traverses the path from the inserted node to the root. | ||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* logarithmic time, where "n" is the number of nodes in the tree. The method traverses the path from the inserted node to the root. constant space, as it doesn't use additional data structures that scale with input size. | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The method traverses the path from the inserted node to the root. | ||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -163,10 +167,11 @@ * The `_balancePath` function is used to update the heights of nodes and perform rotation operations | ||
*/ | ||
protected _balancePath(node: N): void; | ||
protected _balancePath(node: KeyOrNodeOrEntry<K, V, N>): void; | ||
/** | ||
* Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations. | ||
* Space Complexity: O(1) - constant space, as they only use a constant amount of memory. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* constant time, as these methods perform a fixed number of operations. constant space, as they only use a constant amount of memory. | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations. | ||
* Space Complexity: O(1) - constant space, as they only use a constant amount of memory. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -178,8 +183,8 @@ * The function `_balanceLL` performs a left-left rotation to balance a binary tree. | ||
/** | ||
* Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations. | ||
* Space Complexity: O(1) - constant space, as they only use a constant amount of memory. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations. | ||
* Space Complexity: O(1) - constant space, as they only use a constant amount of memory. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -191,8 +196,8 @@ * The `_balanceLR` function performs a left-right rotation to balance a binary tree. | ||
/** | ||
* Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations. | ||
* Space Complexity: O(1) - constant space, as they only use a constant amount of memory. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations. | ||
* Space Complexity: O(1) - constant space, as they only use a constant amount of memory. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -204,8 +209,8 @@ * The function `_balanceRR` performs a right-right rotation to balance a binary tree. | ||
/** | ||
* Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations. | ||
* Space Complexity: O(1) - constant space, as they only use a constant amount of memory. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations. | ||
* Space Complexity: O(1) - constant space, as they only use a constant amount of memory. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -212,0 +217,0 @@ * The function `_balanceRL` performs a right-left rotation to balance a binary tree. |
@@ -30,5 +30,5 @@ "use strict"; | ||
/** | ||
* The constructor function initializes an AVLTree object with optional elements and options. | ||
* @param [elements] - The `elements` parameter is an optional iterable of `BTNExemplar<K, V, N>` | ||
* objects. It represents a collection of elements that will be added to the AVL tree during | ||
* The constructor function initializes an AVLTree object with optional nodes and options. | ||
* @param [nodes] - The `nodes` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>` | ||
* objects. It represents a collection of nodes that will be added to the AVL tree during | ||
* initialization. | ||
@@ -39,6 +39,6 @@ * @param [options] - The `options` parameter is an optional object that allows you to customize the | ||
*/ | ||
constructor(elements, options) { | ||
constructor(nodes, options) { | ||
super([], options); | ||
if (elements) | ||
super.addMany(elements); | ||
if (nodes) | ||
super.addMany(nodes); | ||
} | ||
@@ -68,8 +68,8 @@ /** | ||
/** | ||
* The function checks if an exemplar is an instance of AVLTreeNode. | ||
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`. | ||
* @returns a boolean value indicating whether the exemplar is an instance of the AVLTreeNode class. | ||
* The function checks if an keyOrNodeOrEntry is an instance of AVLTreeNode. | ||
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`. | ||
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeNode class. | ||
*/ | ||
isNode(exemplar) { | ||
return exemplar instanceof AVLTreeNode; | ||
isNode(keyOrNodeOrEntry) { | ||
return keyOrNodeOrEntry instanceof AVLTreeNode; | ||
} | ||
@@ -86,8 +86,9 @@ /** | ||
/** | ||
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity. | ||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size. | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity. | ||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -104,15 +105,15 @@ * The function overrides the add method of a binary tree node and balances the tree after inserting | ||
if (keyOrNodeOrEntry === null) | ||
return undefined; | ||
return false; | ||
const inserted = super.add(keyOrNodeOrEntry, value); | ||
if (inserted) | ||
this._balancePath(inserted); | ||
this._balancePath(keyOrNodeOrEntry); | ||
return inserted; | ||
} | ||
/** | ||
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity. | ||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (BST) has logarithmic time complexity. | ||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -171,8 +172,9 @@ * The function overrides the delete method of a binary tree, performs the deletion, and then | ||
/** | ||
* Time Complexity: O(1) - constant time, as it performs a fixed number of operations. | ||
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* constant time, as it performs a fixed number of operations. constant space, as it only uses a constant amount of memory. | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time, as it performs a fixed number of operations. | ||
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -195,8 +197,9 @@ * The function calculates the balance factor of a node in a binary tree. | ||
/** | ||
* Time Complexity: O(1) - constant time, as it performs a fixed number of operations. | ||
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* constant time, as it performs a fixed number of operations. constant space, as it only uses a constant amount of memory. | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time, as it performs a fixed number of operations. | ||
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -220,8 +223,9 @@ * The function updates the height of a node in a binary tree based on the heights of its left and | ||
/** | ||
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The method traverses the path from the inserted node to the root. | ||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* logarithmic time, where "n" is the number of nodes in the tree. The method traverses the path from the inserted node to the root. constant space, as it doesn't use additional data structures that scale with input size. | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The method traverses the path from the inserted node to the root. | ||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -234,2 +238,3 @@ * The `_balancePath` function is used to update the heights of nodes and perform rotation operations | ||
_balancePath(node) { | ||
node = this.ensureNode(node); | ||
const path = this.getPathToRoot(node, false); // first O(log n) + O(log n) | ||
@@ -274,8 +279,9 @@ for (let i = 0; i < path.length; i++) { | ||
/** | ||
* Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations. | ||
* Space Complexity: O(1) - constant space, as they only use a constant amount of memory. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* constant time, as these methods perform a fixed number of operations. constant space, as they only use a constant amount of memory. | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations. | ||
* Space Complexity: O(1) - constant space, as they only use a constant amount of memory. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -316,8 +322,8 @@ * The function `_balanceLL` performs a left-left rotation to balance a binary tree. | ||
/** | ||
* Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations. | ||
* Space Complexity: O(1) - constant space, as they only use a constant amount of memory. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations. | ||
* Space Complexity: O(1) - constant space, as they only use a constant amount of memory. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -373,8 +379,8 @@ * The `_balanceLR` function performs a left-right rotation to balance a binary tree. | ||
/** | ||
* Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations. | ||
* Space Complexity: O(1) - constant space, as they only use a constant amount of memory. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations. | ||
* Space Complexity: O(1) - constant space, as they only use a constant amount of memory. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -416,8 +422,8 @@ * The function `_balanceRR` performs a right-right rotation to balance a binary tree. | ||
/** | ||
* Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations. | ||
* Space Complexity: O(1) - constant space, as they only use a constant amount of memory. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations. | ||
* Space Complexity: O(1) - constant space, as they only use a constant amount of memory. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -424,0 +430,0 @@ * The function `_balanceRL` performs a right-left rotation to balance a binary tree. |
@@ -8,3 +8,3 @@ /** | ||
*/ | ||
import type { BinaryTreeDeleteResult, BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BinaryTreePrintOptions, BTNCallback, BTNEntry, BTNExemplar, BTNKeyOrNode, DFSOrderPattern, EntryCallback, NodeDisplayLayout } from '../../types'; | ||
import type { BinaryTreeDeleteResult, BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BinaryTreePrintOptions, BTNCallback, BTNEntry, DFSOrderPattern, EntryCallback, KeyOrNodeOrEntry, NodeDisplayLayout } from '../../types'; | ||
import { FamilyPosition, IterationType } from '../../types'; | ||
@@ -45,5 +45,5 @@ import { IBinaryTree } from '../../interfaces'; | ||
/** | ||
* The constructor function initializes a binary tree object with optional elements and options. | ||
* @param [elements] - An optional iterable of BTNExemplar objects. These objects represent the | ||
* elements to be added to the binary tree. | ||
* The constructor function initializes a binary tree object with optional nodes and options. | ||
* @param [nodes] - An optional iterable of KeyOrNodeOrEntry objects. These objects represent the | ||
* nodes to be added to the binary tree. | ||
* @param [options] - The `options` parameter is an optional object that can contain additional | ||
@@ -54,3 +54,3 @@ * configuration options for the binary tree. In this case, it is of type | ||
*/ | ||
constructor(elements?: Iterable<BTNExemplar<K, V, N>>, options?: Partial<BinaryTreeOptions<K>>); | ||
constructor(nodes?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: Partial<BinaryTreeOptions<K>>); | ||
protected _extractor: (key: K) => number; | ||
@@ -78,24 +78,73 @@ get extractor(): (key: K) => number; | ||
/** | ||
* The function "isNode" checks if an exemplar is an instance of the BinaryTreeNode class. | ||
* @param exemplar - The `exemplar` parameter is a variable of type `BTNExemplar<K, V,N>`. | ||
* @returns a boolean value indicating whether the exemplar is an instance of the class N. | ||
*/ | ||
isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N; | ||
/** | ||
* The function `exemplarToNode` converts an exemplar object into a node object. | ||
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`. | ||
* The function `exemplarToNode` converts an keyOrNodeOrEntry object into a node object. | ||
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`. | ||
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the | ||
* `exemplarToNode` function. It represents the value associated with the exemplar node. If no value | ||
* `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If no value | ||
* is provided, it will be `undefined`. | ||
* @returns a value of type N (node), or null, or undefined. | ||
*/ | ||
exemplarToNode(exemplar: BTNExemplar<K, V, N>, value?: V): N | null | undefined; | ||
exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): N | null | undefined; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(log n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(log n) | ||
* | ||
* The function `ensureNode` returns the node corresponding to the given key if it is a valid node | ||
* key, otherwise it returns the key itself. | ||
* @param {K | N | null | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `N`, | ||
* `null`, or `undefined`. It represents a key used to identify a node in a binary tree. | ||
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the | ||
* type of iteration to be used when searching for a node by key. It has a default value of | ||
* `IterationType.ITERATIVE`. | ||
* @returns either the node corresponding to the given key if it is a valid node key, or the key | ||
* itself if it is not a valid node key. | ||
*/ | ||
ensureNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): N | null | undefined; | ||
/** | ||
* The function "isNode" checks if an keyOrNodeOrEntry is an instance of the BinaryTreeNode class. | ||
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V,N>`. | ||
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the class N. | ||
*/ | ||
isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N; | ||
/** | ||
* The function checks if a given value is an entry in a binary tree node. | ||
* @param kne - BTNExemplar<K, V,N> - A generic type representing a node in a binary tree. It has | ||
* @param keyOrNodeOrEntry - KeyOrNodeOrEntry<K, V,N> - A generic type representing a node in a binary tree. It has | ||
* two type parameters V and N, representing the value and node type respectively. | ||
* @returns a boolean value. | ||
*/ | ||
isEntry(kne: BTNExemplar<K, V, N>): kne is BTNEntry<K, V>; | ||
isEntry(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is BTNEntry<K, V>; | ||
/** | ||
* Time complexity: O(n) | ||
* Space complexity: O(log n) | ||
*/ | ||
/** | ||
* The function checks if a given node is a real node by verifying if it is an instance of | ||
* BinaryTreeNode and its key is not NaN. | ||
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type. | ||
* @returns a boolean value. | ||
*/ | ||
isRealNode(node: KeyOrNodeOrEntry<K, V, N>): node is N; | ||
/** | ||
* The function checks if a given node is a BinaryTreeNode instance and has a key value of NaN. | ||
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type. | ||
* @returns a boolean value. | ||
*/ | ||
isNIL(node: KeyOrNodeOrEntry<K, V, N>): boolean; | ||
/** | ||
* The function checks if a given node is a real node or null. | ||
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type. | ||
* @returns a boolean value. | ||
*/ | ||
isNodeOrNull(node: KeyOrNodeOrEntry<K, V, N>): node is N | null; | ||
/** | ||
* The function "isNotNodeInstance" checks if a potential key is a K. | ||
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any | ||
* data type. | ||
* @returns a boolean value indicating whether the potentialKey is of type number or not. | ||
*/ | ||
isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K; | ||
/** | ||
* Time Complexity O(log n) - O(n) | ||
@@ -114,3 +163,3 @@ * Space Complexity O(1) | ||
*/ | ||
add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V): N | null | undefined; | ||
add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): boolean; | ||
/** | ||
@@ -125,18 +174,32 @@ * Time Complexity: O(k log n) - O(k * n) | ||
* | ||
* The `addMany` function takes in a collection of nodes and an optional collection of values, and | ||
* The `addMany` function takes in a collection of keysOrNodesOrEntries and an optional collection of values, and | ||
* adds each node with its corresponding value to the data structure. | ||
* @param nodes - An iterable collection of BTNExemplar objects. | ||
* @param keysOrNodesOrEntries - An iterable collection of KeyOrNodeOrEntry objects. | ||
* @param [values] - An optional iterable of values that will be assigned to each node being added. | ||
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values. | ||
*/ | ||
addMany(nodes: Iterable<BTNExemplar<K, V, N>>, values?: Iterable<V | undefined>): (N | null | undefined)[]; | ||
addMany(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>>, values?: Iterable<V | undefined>): boolean[]; | ||
/** | ||
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. | ||
* Time Complexity: O(k * n) | ||
* Space Complexity: O(1) | ||
* "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. | ||
*/ | ||
refill(nodesOrKeysOrEntries: Iterable<BTNExemplar<K, V, N>>, values?: Iterable<V | undefined>): void; | ||
/** | ||
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. | ||
* Time Complexity: O(k * n) | ||
* Space Complexity: O(1) | ||
* | ||
* The `refill` function clears the current data and adds new key-value pairs to the data structure. | ||
* @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries. These can be of type | ||
* KeyOrNodeOrEntry<K, V, N>. | ||
* @param [values] - The `values` parameter is an optional iterable that contains the values to be | ||
* associated with the keys or nodes or entries in the `keysOrNodesOrEntries` parameter. If provided, | ||
* the values will be associated with the corresponding keys or nodes or entries in the | ||
* `keysOrNodesOrEntries` iterable | ||
*/ | ||
refill(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>>, values?: Iterable<V | undefined>): void; | ||
/** | ||
* Time Complexity: O(k * n) | ||
* Space Complexity: O(1) | ||
* "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. | ||
*/ | ||
delete<C extends BTNCallback<N, K>>(identifier: K, callback?: C): BinaryTreeDeleteResult<N>[]; | ||
@@ -154,3 +217,3 @@ delete<C extends BTNCallback<N, N>>(identifier: N | null | undefined, callback?: C): BinaryTreeDeleteResult<N>[]; | ||
* The function calculates the depth of a given node in a binary tree. | ||
* @param {K | N | null | undefined} distNode - The `distNode` parameter represents the node in | ||
* @param {K | N | null | undefined} dist - The `dist` parameter represents the node in | ||
* the binary tree whose depth we want to find. It can be of type `K`, `N`, `null`, or | ||
@@ -161,5 +224,5 @@ * `undefined`. | ||
* `N` (binary tree node) or `null` or `undefined`. If no value is provided for `beginRoot | ||
* @returns the depth of the `distNode` relative to the `beginRoot`. | ||
* @returns the depth of the `dist` relative to the `beginRoot`. | ||
*/ | ||
getDepth(distNode: BTNKeyOrNode<K, N>, beginRoot?: BTNKeyOrNode<K, N>): number; | ||
getDepth(dist: KeyOrNodeOrEntry<K, V, N>, beginRoot?: KeyOrNodeOrEntry<K, V, N>): number; | ||
/** | ||
@@ -183,3 +246,3 @@ * Time Complexity: O(n) | ||
*/ | ||
getHeight(beginRoot?: BTNKeyOrNode<K, N>, iterationType?: IterationType): number; | ||
getHeight(beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): number; | ||
/** | ||
@@ -203,3 +266,3 @@ * Time Complexity: O(n) | ||
*/ | ||
getMinHeight(beginRoot?: BTNKeyOrNode<K, N>, iterationType?: IterationType): number; | ||
getMinHeight(beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): number; | ||
/** | ||
@@ -221,3 +284,3 @@ * Time Complexity: O(n) | ||
*/ | ||
isPerfectlyBalanced(beginRoot?: BTNKeyOrNode<K, N>): boolean; | ||
isPerfectlyBalanced(beginRoot?: KeyOrNodeOrEntry<K, V, N>): boolean; | ||
/** | ||
@@ -227,5 +290,5 @@ * Time Complexity: O(n) | ||
*/ | ||
getNodes<C extends BTNCallback<N, K>>(identifier: K, callback?: C, onlyOne?: boolean, beginRoot?: BTNKeyOrNode<K, N>, iterationType?: IterationType): N[]; | ||
getNodes<C extends BTNCallback<N, N>>(identifier: N | null | undefined, callback?: C, onlyOne?: boolean, beginRoot?: BTNKeyOrNode<K, N>, iterationType?: IterationType): N[]; | ||
getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, onlyOne?: boolean, beginRoot?: BTNKeyOrNode<K, N>, iterationType?: IterationType): N[]; | ||
getNodes<C extends BTNCallback<N, K>>(identifier: K, callback?: C, onlyOne?: boolean, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): N[]; | ||
getNodes<C extends BTNCallback<N, N>>(identifier: N | null | undefined, callback?: C, onlyOne?: boolean, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): N[]; | ||
getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, onlyOne?: boolean, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): N[]; | ||
/** | ||
@@ -235,5 +298,5 @@ * Time Complexity: O(n) | ||
*/ | ||
has<C extends BTNCallback<N, K>>(identifier: K, callback?: C, beginRoot?: BTNKeyOrNode<K, N>, iterationType?: IterationType): boolean; | ||
has<C extends BTNCallback<N, N>>(identifier: N | null | undefined, callback?: C, beginRoot?: BTNKeyOrNode<K, N>, iterationType?: IterationType): boolean; | ||
has<C extends BTNCallback<N>>(identifier: ReturnType<C> | null | undefined, callback: C, beginRoot?: BTNKeyOrNode<K, N>, iterationType?: IterationType): boolean; | ||
has<C extends BTNCallback<N, K>>(identifier: K, callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): boolean; | ||
has<C extends BTNCallback<N, N>>(identifier: N | null | undefined, callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): boolean; | ||
has<C extends BTNCallback<N>>(identifier: ReturnType<C> | null | undefined, callback: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): boolean; | ||
/** | ||
@@ -243,5 +306,5 @@ * Time Complexity: O(n) | ||
*/ | ||
getNode<C extends BTNCallback<N, K>>(identifier: K, callback?: C, beginRoot?: BTNKeyOrNode<K, N>, iterationType?: IterationType): N | null | undefined; | ||
getNode<C extends BTNCallback<N, N>>(identifier: N | null | undefined, callback?: C, beginRoot?: BTNKeyOrNode<K, N>, iterationType?: IterationType): N | null | undefined; | ||
getNode<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, beginRoot?: BTNKeyOrNode<K, N>, iterationType?: IterationType): N | null | undefined; | ||
getNode<C extends BTNCallback<N, K>>(identifier: K, callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): N | null | undefined; | ||
getNode<C extends BTNCallback<N, N>>(identifier: N | null | undefined, callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): N | null | undefined; | ||
getNode<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): N | null | undefined; | ||
/** | ||
@@ -266,30 +329,24 @@ * Time Complexity: O(n) | ||
getNodeByKey(key: K, iterationType?: IterationType): N | undefined; | ||
get<C extends BTNCallback<N, K>>(identifier: K, callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): V | undefined; | ||
get<C extends BTNCallback<N, N>>(identifier: N | null | undefined, callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): V | undefined; | ||
get<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): V | undefined; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(log n) | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* The function `ensureNode` returns the node corresponding to the given key if it is a valid node | ||
* key, otherwise it returns the key itself. | ||
* @param {K | N | null | undefined} key - The `key` parameter can be of type `K`, `N`, | ||
* `null`, or `undefined`. It represents a key used to identify a node in a binary tree. | ||
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the | ||
* type of iteration to be used when searching for a node by key. It has a default value of | ||
* `IterationType.ITERATIVE`. | ||
* @returns either the node corresponding to the given key if it is a valid node key, or the key | ||
* itself if it is not a valid node key. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* Clear the binary tree, removing all nodes. | ||
*/ | ||
ensureNode(key: BTNKeyOrNode<K, N>, iterationType?: IterationType): N | null | undefined; | ||
get<C extends BTNCallback<N, K>>(identifier: K, callback?: C, beginRoot?: BTNKeyOrNode<K, N>, iterationType?: IterationType): V | undefined; | ||
get<C extends BTNCallback<N, N>>(identifier: N | null | undefined, callback?: C, beginRoot?: BTNKeyOrNode<K, N>, iterationType?: IterationType): V | undefined; | ||
get<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, beginRoot?: BTNKeyOrNode<K, N>, iterationType?: IterationType): V | undefined; | ||
clear(): void; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(log n) | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Clear the binary tree, removing all nodes. | ||
*/ | ||
clear(): void; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* Check if the binary tree is empty. | ||
@@ -313,6 +370,6 @@ * @returns {boolean} - True if the binary tree is empty, false otherwise. | ||
*/ | ||
getPathToRoot(beginRoot: BTNKeyOrNode<K, N>, isReverse?: boolean): N[]; | ||
getPathToRoot(beginRoot: KeyOrNodeOrEntry<K, V, N>, isReverse?: boolean): N[]; | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
*/ | ||
@@ -333,3 +390,3 @@ /** | ||
*/ | ||
getLeftMost(beginRoot?: BTNKeyOrNode<K, N>, iterationType?: IterationType): N | null | undefined; | ||
getLeftMost(beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): N | null | undefined; | ||
/** | ||
@@ -354,3 +411,3 @@ * Time Complexity: O(log n) | ||
*/ | ||
getRightMost(beginRoot?: BTNKeyOrNode<K, N>, iterationType?: IterationType): N | null | undefined; | ||
getRightMost(beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): N | null | undefined; | ||
/** | ||
@@ -372,66 +429,20 @@ * Time Complexity: O(log n) | ||
*/ | ||
isSubtreeBST(beginRoot: BTNKeyOrNode<K, N>, iterationType?: IterationType): boolean; | ||
isBST(beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): boolean; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
* The function checks if a binary tree is a binary search tree. | ||
* @param iterationType - The parameter "iterationType" is used to specify the type of iteration to | ||
* be used when checking if the binary tree is a binary search tree (BST). It is an optional | ||
* parameter with a default value of "this.iterationType". The value of "this.iterationType" is | ||
* expected to be | ||
* @returns a boolean value. | ||
*/ | ||
isBST(iterationType?: IterationType): boolean; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
*/ | ||
subTreeTraverse<C extends BTNCallback<N>>(callback?: C, beginRoot?: BTNKeyOrNode<K, N>, iterationType?: IterationType, includeNull?: false): ReturnType<C>[]; | ||
subTreeTraverse<C extends BTNCallback<N>>(callback?: C, beginRoot?: BTNKeyOrNode<K, N>, iterationType?: IterationType, includeNull?: undefined): ReturnType<C>[]; | ||
subTreeTraverse<C extends BTNCallback<N | null | undefined>>(callback?: C, beginRoot?: BTNKeyOrNode<K, N>, iterationType?: IterationType, includeNull?: true): ReturnType<C>[]; | ||
/** | ||
* Time complexity: O(n) | ||
* Space complexity: O(log n) | ||
*/ | ||
subTreeTraverse<C extends BTNCallback<N>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: false): ReturnType<C>[]; | ||
subTreeTraverse<C extends BTNCallback<N>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: undefined): ReturnType<C>[]; | ||
subTreeTraverse<C extends BTNCallback<N | null | undefined>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: true): ReturnType<C>[]; | ||
dfs<C extends BTNCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: false): ReturnType<C>[]; | ||
dfs<C extends BTNCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: undefined): ReturnType<C>[]; | ||
dfs<C extends BTNCallback<N | null | undefined>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: true): ReturnType<C>[]; | ||
/** | ||
* The function checks if a given node is a real node by verifying if it is an instance of | ||
* BinaryTreeNode and its key is not NaN. | ||
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type. | ||
* @returns a boolean value. | ||
*/ | ||
isRealNode(node: BTNExemplar<K, V, N>): node is N; | ||
/** | ||
* The function checks if a given node is a BinaryTreeNode instance and has a key value of NaN. | ||
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type. | ||
* @returns a boolean value. | ||
*/ | ||
isNIL(node: BTNExemplar<K, V, N>): boolean; | ||
/** | ||
* The function checks if a given node is a real node or null. | ||
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type. | ||
* @returns a boolean value. | ||
*/ | ||
isNodeOrNull(node: BTNExemplar<K, V, N>): node is N | null; | ||
/** | ||
* The function "isNotNodeInstance" checks if a potential key is a K. | ||
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any | ||
* data type. | ||
* @returns a boolean value indicating whether the potentialKey is of type number or not. | ||
*/ | ||
isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K; | ||
dfs<C extends BTNCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: BTNKeyOrNode<K, N>, iterationType?: IterationType, includeNull?: false): ReturnType<C>[]; | ||
dfs<C extends BTNCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: BTNKeyOrNode<K, N>, iterationType?: IterationType, includeNull?: undefined): ReturnType<C>[]; | ||
dfs<C extends BTNCallback<N | null | undefined>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: BTNKeyOrNode<K, N>, iterationType?: IterationType, includeNull?: true): ReturnType<C>[]; | ||
/** | ||
* Time complexity: O(n) | ||
* Space complexity: O(n) | ||
*/ | ||
bfs<C extends BTNCallback<N>>(callback?: C, beginRoot?: BTNKeyOrNode<K, N>, iterationType?: IterationType, includeNull?: false): ReturnType<C>[]; | ||
bfs<C extends BTNCallback<N>>(callback?: C, beginRoot?: BTNKeyOrNode<K, N>, iterationType?: IterationType, includeNull?: undefined): ReturnType<C>[]; | ||
bfs<C extends BTNCallback<N | null | undefined>>(callback?: C, beginRoot?: BTNKeyOrNode<K, N>, iterationType?: IterationType, includeNull?: true): ReturnType<C>[]; | ||
bfs<C extends BTNCallback<N>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: false): ReturnType<C>[]; | ||
bfs<C extends BTNCallback<N>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: undefined): ReturnType<C>[]; | ||
bfs<C extends BTNCallback<N | null | undefined>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: true): ReturnType<C>[]; | ||
/** | ||
@@ -441,5 +452,5 @@ * Time complexity: O(n) | ||
*/ | ||
listLevels<C extends BTNCallback<N>>(callback?: C, beginRoot?: BTNKeyOrNode<K, N>, iterationType?: IterationType, includeNull?: false): ReturnType<C>[][]; | ||
listLevels<C extends BTNCallback<N>>(callback?: C, beginRoot?: BTNKeyOrNode<K, N>, iterationType?: IterationType, includeNull?: undefined): ReturnType<C>[][]; | ||
listLevels<C extends BTNCallback<N | null | undefined>>(callback?: C, beginRoot?: BTNKeyOrNode<K, N>, iterationType?: IterationType, includeNull?: true): ReturnType<C>[][]; | ||
listLevels<C extends BTNCallback<N>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: false): ReturnType<C>[][]; | ||
listLevels<C extends BTNCallback<N>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: undefined): ReturnType<C>[][]; | ||
listLevels<C extends BTNCallback<N | null | undefined>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: true): ReturnType<C>[][]; | ||
/** | ||
@@ -468,3 +479,8 @@ * Time Complexity: O(log n) | ||
* Time complexity: O(n) | ||
* Space complexity: O(1) | ||
* Space complexity: O(n) | ||
*/ | ||
/** | ||
* Time complexity: O(n) | ||
* Space complexity: O(n) | ||
* | ||
* The `morris` function performs a depth-first traversal on a binary tree using the Morris traversal | ||
@@ -482,6 +498,6 @@ * algorithm. | ||
* @returns The function `morris` returns an array of values that are the result of invoking the | ||
* `callback` function on each node in the binary tree. The type of the array elements is determined | ||
* `callback` function on each node in the binary tree. The type of the array nodes is determined | ||
* by the return type of the `callback` function. | ||
*/ | ||
morris<C extends BTNCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: BTNKeyOrNode<K, N>): ReturnType<C>[]; | ||
morris<C extends BTNCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: KeyOrNodeOrEntry<K, V, N>): ReturnType<C>[]; | ||
/** | ||
@@ -508,4 +524,4 @@ * Time complexity: O(n) | ||
* | ||
* The `filter` function creates a new tree by iterating over the elements of the current tree and | ||
* adding only the elements that satisfy the given predicate function. | ||
* The `filter` function creates a new tree by iterating over the nodes of the current tree and | ||
* adding only the nodes that satisfy the given predicate function. | ||
* @param predicate - The `predicate` parameter is a function that takes three arguments: `value`, | ||
@@ -542,2 +558,9 @@ * `key`, and `index`. It should return a boolean value indicating whether the pair should be | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `print` function is used to display a binary tree structure in a visually appealing way. | ||
@@ -549,3 +572,3 @@ * @param {K | N | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `K | N | null | | ||
*/ | ||
print(beginRoot?: BTNKeyOrNode<K, N>, options?: BinaryTreePrintOptions): void; | ||
print(beginRoot?: KeyOrNodeOrEntry<K, V, N>, options?: BinaryTreePrintOptions): void; | ||
protected _getIterator(node?: N | null | undefined): IterableIterator<[K, V | undefined]>; | ||
@@ -560,3 +583,3 @@ protected _displayAux(node: N | null | undefined, options: BinaryTreePrintOptions): NodeDisplayLayout; | ||
*/ | ||
protected _swapProperties(srcNode: BTNKeyOrNode<K, N>, destNode: BTNKeyOrNode<K, N>): N | undefined; | ||
protected _swapProperties(srcNode: KeyOrNodeOrEntry<K, V, N>, destNode: KeyOrNodeOrEntry<K, V, N>): N | undefined; | ||
/** | ||
@@ -563,0 +586,0 @@ * The function replaces an old node with a new node in a binary tree. |
@@ -8,3 +8,3 @@ /** | ||
*/ | ||
import type { BSTNested, BSTNKeyOrNode, BSTNodeNested, BSTOptions, BTNCallback, BTNExemplar, BTNKeyOrNode } from '../../types'; | ||
import type { BSTNested, BSTNodeNested, BSTOptions, BTNCallback, KeyOrNodeOrEntry } from '../../types'; | ||
import { BSTVariant, CP, IterationType } from '../../types'; | ||
@@ -49,4 +49,4 @@ import { BinaryTree, BinaryTreeNode } from './binary-tree'; | ||
* This is the constructor function for a binary search tree class in TypeScript, which initializes | ||
* the tree with optional elements and options. | ||
* @param [elements] - An optional iterable of BTNExemplar objects that will be added to the | ||
* the tree with optional nodes and options. | ||
* @param [nodes] - An optional iterable of KeyOrNodeOrEntry objects that will be added to the | ||
* binary search tree. | ||
@@ -56,3 +56,3 @@ * @param [options] - The `options` parameter is an optional object that can contain additional | ||
*/ | ||
constructor(elements?: Iterable<BTNExemplar<K, V, N>>, options?: Partial<BSTOptions<K>>); | ||
constructor(nodes?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: Partial<BSTOptions<K>>); | ||
protected _root?: N; | ||
@@ -80,24 +80,50 @@ get root(): N | undefined; | ||
/** | ||
* The function checks if an exemplar is an instance of BSTNode. | ||
* @param exemplar - The `exemplar` parameter is a variable of type `BTNExemplar<K, V, N>`. | ||
* @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class. | ||
*/ | ||
isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N; | ||
/** | ||
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid, | ||
* The function `exemplarToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid, | ||
* otherwise it returns undefined. | ||
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, where: | ||
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, where: | ||
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the | ||
* `exemplarToNode` function. It represents the value associated with the exemplar node. | ||
* `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node. | ||
* @returns a node of type N or undefined. | ||
*/ | ||
exemplarToNode(exemplar: BTNExemplar<K, V, N>, value?: V): N | undefined; | ||
exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): N | undefined; | ||
/** | ||
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n). | ||
* Space Complexity: O(1) - Constant space is used. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(log n) | ||
* Average case for a balanced tree. Space for the recursive call stack in the worst case. | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n). | ||
* Space Complexity: O(1) - Constant space is used. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(log n) | ||
* | ||
* The function `ensureNode` returns the node corresponding to the given key if it is a node key, | ||
* otherwise it returns the key itself. | ||
* @param {K | N | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `N`, or | ||
* `undefined`. | ||
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the | ||
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`. | ||
* @returns either a node object (N) or undefined. | ||
*/ | ||
ensureNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): N | undefined; | ||
/** | ||
* The function "isNotNodeInstance" checks if a potential key is a K. | ||
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any | ||
* data type. | ||
* @returns a boolean value indicating whether the potentialKey is of type number or not. | ||
*/ | ||
isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K; | ||
/** | ||
* The function checks if an keyOrNodeOrEntry is an instance of BSTNode. | ||
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, N>`. | ||
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the BSTNode class. | ||
*/ | ||
isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N; | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n). | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* | ||
* The `add` function adds a new node to a binary tree, updating the value if the key already exists | ||
@@ -111,10 +137,11 @@ * or inserting a new node if the key is unique. | ||
*/ | ||
add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V): N | undefined; | ||
add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): boolean; | ||
/** | ||
* Time Complexity: O(k log n) - Adding each element individually in a balanced tree. | ||
* Space Complexity: O(k) - Additional space is required for the sorted array. | ||
* Time Complexity: O(k log n) | ||
* Space Complexity: O(k) | ||
* Adding each element individually in a balanced tree. Additional space is required for the sorted array. | ||
*/ | ||
/** | ||
* Time Complexity: O(k log n) - Adding each element individually in a balanced tree. | ||
* Space Complexity: O(k) - Additional space is required for the sorted array. | ||
* Time Complexity: O(k log n) | ||
* Space Complexity: O(k) | ||
* | ||
@@ -130,3 +157,3 @@ * The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally | ||
* balanced or not. If set to true, the add operation will be balanced using a binary search tree | ||
* algorithm. If set to false, the add operation will not be balanced and the elements will be added | ||
* algorithm. If set to false, the add operation will not be balanced and the nodes will be added | ||
* in the order they appear in the input. | ||
@@ -138,10 +165,11 @@ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the | ||
*/ | ||
addMany(keysOrNodesOrEntries: Iterable<BTNExemplar<K, V, N>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): (N | undefined)[]; | ||
addMany(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): boolean[]; | ||
/** | ||
* Time Complexity: O(n log n) - Adding each element individually in a balanced tree. | ||
* Space Complexity: O(n) - Additional space is required for the sorted array. | ||
* Time Complexity: O(n log n) | ||
* Space Complexity: O(n) | ||
* Adding each element individually in a balanced tree. Additional space is required for the sorted array. | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) - Average case for a balanced tree. | ||
* Space Complexity: O(1) - Constant space is used. | ||
* Time Complexity: O(n log n) | ||
* Space Complexity: O(n) | ||
* | ||
@@ -153,4 +181,2 @@ * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the | ||
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`). | ||
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to | ||
* be performed. It can have one of the following values: | ||
* @returns the key of the rightmost node in the binary tree if the comparison result is less than, | ||
@@ -160,10 +186,10 @@ * the key of the leftmost node if the comparison result is greater than, and the key of the | ||
*/ | ||
lastKey(beginRoot?: BSTNKeyOrNode<K, N>): K | undefined; | ||
lastKey(beginRoot?: KeyOrNodeOrEntry<K, V, N>): K | undefined; | ||
/** | ||
* Time Complexity: O(log n) - Average case for a balanced tree. | ||
* Space Complexity: O(1) - Constant space is used. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) - Average case for a balanced tree. | ||
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -182,25 +208,10 @@ * The function `getNodeByKey` searches for a node in a binary tree based on a given key, using | ||
/** | ||
* The function "isNotNodeInstance" checks if a potential key is a K. | ||
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any | ||
* data type. | ||
* @returns a boolean value indicating whether the potentialKey is of type number or not. | ||
*/ | ||
isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K; | ||
/** | ||
* Time Complexity: O(log n) - Average case for a balanced tree. | ||
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case. | ||
*/ | ||
/** | ||
* The function `ensureNode` returns the node corresponding to the given key if it is a node key, | ||
* otherwise it returns the key itself. | ||
* @param {K | N | undefined} key - The `key` parameter can be of type `K`, `N`, or | ||
* `undefined`. | ||
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the | ||
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`. | ||
* @returns either a node object (N) or undefined. | ||
*/ | ||
ensureNode(key: BSTNKeyOrNode<K, N>, iterationType?: IterationType): N | undefined; | ||
/** | ||
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. | ||
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(log n) | ||
* Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case. | ||
* / | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(log n) | ||
* | ||
@@ -226,10 +237,11 @@ * The function `getNodes` returns an array of nodes that match a given identifier, using either a | ||
*/ | ||
getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: BSTNKeyOrNode<K, N>, iterationType?: IterationType): N[]; | ||
getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): N[]; | ||
/** | ||
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. | ||
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(log n) | ||
* Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case. | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. | ||
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(log n) | ||
* | ||
@@ -253,10 +265,10 @@ * The `lesserOrGreaterTraverse` function traverses a binary tree and returns an array of nodes that | ||
*/ | ||
lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BSTNKeyOrNode<K, N>, iterationType?: IterationType): ReturnType<C>[]; | ||
lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): ReturnType<C>[]; | ||
/** | ||
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. | ||
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(log n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) - Building a balanced tree from a sorted array. | ||
* Space Complexity: O(n) - Additional space is required for the sorted array. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(log n) | ||
* | ||
@@ -285,4 +297,4 @@ * The `perfectlyBalance` function balances a binary search tree by adding nodes in a way that | ||
/** | ||
* Time Complexity: O(n) - Visiting each node once. | ||
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(log n) | ||
* | ||
@@ -289,0 +301,0 @@ * The function checks if a binary tree is AVL balanced using either recursive or iterative approach. |
@@ -60,4 +60,4 @@ "use strict"; | ||
* This is the constructor function for a binary search tree class in TypeScript, which initializes | ||
* the tree with optional elements and options. | ||
* @param [elements] - An optional iterable of BTNExemplar objects that will be added to the | ||
* the tree with optional nodes and options. | ||
* @param [nodes] - An optional iterable of KeyOrNodeOrEntry objects that will be added to the | ||
* binary search tree. | ||
@@ -67,5 +67,5 @@ * @param [options] - The `options` parameter is an optional object that can contain additional | ||
*/ | ||
constructor(elements, options) { | ||
constructor(nodes, options) { | ||
super([], options); | ||
this._variant = types_1.BSTVariant.MIN; | ||
this._variant = types_1.BSTVariant.STANDARD; | ||
if (options) { | ||
@@ -78,4 +78,4 @@ const { variant } = options; | ||
this._root = undefined; | ||
if (elements) | ||
this.addMany(elements); | ||
if (nodes) | ||
this.addMany(nodes); | ||
} | ||
@@ -110,27 +110,19 @@ get root() { | ||
/** | ||
* The function checks if an exemplar is an instance of BSTNode. | ||
* @param exemplar - The `exemplar` parameter is a variable of type `BTNExemplar<K, V, N>`. | ||
* @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class. | ||
*/ | ||
isNode(exemplar) { | ||
return exemplar instanceof BSTNode; | ||
} | ||
/** | ||
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid, | ||
* The function `exemplarToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid, | ||
* otherwise it returns undefined. | ||
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, where: | ||
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, where: | ||
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the | ||
* `exemplarToNode` function. It represents the value associated with the exemplar node. | ||
* `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node. | ||
* @returns a node of type N or undefined. | ||
*/ | ||
exemplarToNode(exemplar, value) { | ||
exemplarToNode(keyOrNodeOrEntry, value) { | ||
let node; | ||
if (exemplar === null || exemplar === undefined) { | ||
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) { | ||
return; | ||
} | ||
else if (this.isNode(exemplar)) { | ||
node = exemplar; | ||
else if (this.isNode(keyOrNodeOrEntry)) { | ||
node = keyOrNodeOrEntry; | ||
} | ||
else if (this.isEntry(exemplar)) { | ||
const [key, value] = exemplar; | ||
else if (this.isEntry(keyOrNodeOrEntry)) { | ||
const [key, value] = keyOrNodeOrEntry; | ||
if (key === undefined || key === null) { | ||
@@ -143,4 +135,4 @@ return; | ||
} | ||
else if (this.isNotNodeInstance(exemplar)) { | ||
node = this.createNode(exemplar, value); | ||
else if (this.isNotNodeInstance(keyOrNodeOrEntry)) { | ||
node = this.createNode(keyOrNodeOrEntry, value); | ||
} | ||
@@ -153,9 +145,59 @@ else { | ||
/** | ||
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n). | ||
* Space Complexity: O(1) - Constant space is used. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(log n) | ||
* Average case for a balanced tree. Space for the recursive call stack in the worst case. | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n). | ||
* Space Complexity: O(1) - Constant space is used. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(log n) | ||
* | ||
* The function `ensureNode` returns the node corresponding to the given key if it is a node key, | ||
* otherwise it returns the key itself. | ||
* @param {K | N | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `N`, or | ||
* `undefined`. | ||
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the | ||
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`. | ||
* @returns either a node object (N) or undefined. | ||
*/ | ||
ensureNode(keyOrNodeOrEntry, iterationType = types_1.IterationType.ITERATIVE) { | ||
let res; | ||
if (this.isRealNode(keyOrNodeOrEntry)) { | ||
res = keyOrNodeOrEntry; | ||
} | ||
else if (this.isEntry(keyOrNodeOrEntry)) { | ||
if (keyOrNodeOrEntry[0]) | ||
res = this.getNodeByKey(keyOrNodeOrEntry[0], iterationType); | ||
} | ||
else { | ||
if (keyOrNodeOrEntry) | ||
res = this.getNodeByKey(keyOrNodeOrEntry, iterationType); | ||
} | ||
return res; | ||
} | ||
/** | ||
* The function "isNotNodeInstance" checks if a potential key is a K. | ||
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any | ||
* data type. | ||
* @returns a boolean value indicating whether the potentialKey is of type number or not. | ||
*/ | ||
isNotNodeInstance(potentialKey) { | ||
return !(potentialKey instanceof BSTNode); | ||
} | ||
/** | ||
* The function checks if an keyOrNodeOrEntry is an instance of BSTNode. | ||
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, N>`. | ||
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the BSTNode class. | ||
*/ | ||
isNode(keyOrNodeOrEntry) { | ||
return keyOrNodeOrEntry instanceof BSTNode; | ||
} | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n). | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* | ||
* The `add` function adds a new node to a binary tree, updating the value if the key already exists | ||
@@ -172,7 +214,7 @@ * or inserting a new node if the key is unique. | ||
if (newNode === undefined) | ||
return; | ||
return false; | ||
if (this.root === undefined) { | ||
this._setRoot(newNode); | ||
this._size++; | ||
return this.root; | ||
return true; | ||
} | ||
@@ -185,3 +227,3 @@ let current = this.root; | ||
this._replaceNode(current, newNode); | ||
return newNode; | ||
return true; | ||
// } else { | ||
@@ -198,3 +240,3 @@ // The key value is the same and the reference is the same, replace the entire node | ||
this._size++; | ||
return newNode; | ||
return true; | ||
} | ||
@@ -208,3 +250,3 @@ current = current.left; | ||
this._size++; | ||
return newNode; | ||
return true; | ||
} | ||
@@ -214,11 +256,12 @@ current = current.right; | ||
} | ||
return undefined; | ||
return false; | ||
} | ||
/** | ||
* Time Complexity: O(k log n) - Adding each element individually in a balanced tree. | ||
* Space Complexity: O(k) - Additional space is required for the sorted array. | ||
* Time Complexity: O(k log n) | ||
* Space Complexity: O(k) | ||
* Adding each element individually in a balanced tree. Additional space is required for the sorted array. | ||
*/ | ||
/** | ||
* Time Complexity: O(k log n) - Adding each element individually in a balanced tree. | ||
* Space Complexity: O(k) - Additional space is required for the sorted array. | ||
* Time Complexity: O(k log n) | ||
* Space Complexity: O(k) | ||
* | ||
@@ -234,3 +277,3 @@ * The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally | ||
* balanced or not. If set to true, the add operation will be balanced using a binary search tree | ||
* algorithm. If set to false, the add operation will not be balanced and the elements will be added | ||
* algorithm. If set to false, the add operation will not be balanced and the nodes will be added | ||
* in the order they appear in the input. | ||
@@ -317,8 +360,9 @@ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the | ||
/** | ||
* Time Complexity: O(n log n) - Adding each element individually in a balanced tree. | ||
* Space Complexity: O(n) - Additional space is required for the sorted array. | ||
* Time Complexity: O(n log n) | ||
* Space Complexity: O(n) | ||
* Adding each element individually in a balanced tree. Additional space is required for the sorted array. | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) - Average case for a balanced tree. | ||
* Space Complexity: O(1) - Constant space is used. | ||
* Time Complexity: O(n log n) | ||
* Space Complexity: O(n) | ||
* | ||
@@ -330,4 +374,2 @@ * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the | ||
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`). | ||
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to | ||
* be performed. It can have one of the following values: | ||
* @returns the key of the rightmost node in the binary tree if the comparison result is less than, | ||
@@ -341,3 +383,3 @@ * the key of the leftmost node if the comparison result is greater than, and the key of the | ||
return undefined; | ||
if (this._variant === types_1.BSTVariant.MIN) { | ||
if (this._variant === types_1.BSTVariant.STANDARD) { | ||
// For BSTVariant.MIN, find the rightmost node | ||
@@ -357,8 +399,8 @@ while (current.right !== undefined) { | ||
/** | ||
* Time Complexity: O(log n) - Average case for a balanced tree. | ||
* Space Complexity: O(1) - Constant space is used. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) - Average case for a balanced tree. | ||
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -407,29 +449,10 @@ * The function `getNodeByKey` searches for a node in a binary tree based on a given key, using | ||
/** | ||
* The function "isNotNodeInstance" checks if a potential key is a K. | ||
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any | ||
* data type. | ||
* @returns a boolean value indicating whether the potentialKey is of type number or not. | ||
*/ | ||
isNotNodeInstance(potentialKey) { | ||
return !(potentialKey instanceof BSTNode); | ||
} | ||
/** | ||
* Time Complexity: O(log n) - Average case for a balanced tree. | ||
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case. | ||
*/ | ||
/** | ||
* The function `ensureNode` returns the node corresponding to the given key if it is a node key, | ||
* otherwise it returns the key itself. | ||
* @param {K | N | undefined} key - The `key` parameter can be of type `K`, `N`, or | ||
* `undefined`. | ||
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the | ||
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`. | ||
* @returns either a node object (N) or undefined. | ||
*/ | ||
ensureNode(key, iterationType = types_1.IterationType.ITERATIVE) { | ||
return this.isNotNodeInstance(key) ? this.getNodeByKey(key, iterationType) : key; | ||
} | ||
/** | ||
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. | ||
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(log n) | ||
* Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case. | ||
* / | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(log n) | ||
* | ||
@@ -512,8 +535,9 @@ * The function `getNodes` returns an array of nodes that match a given identifier, using either a | ||
/** | ||
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. | ||
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(log n) | ||
* Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case. | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. | ||
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(log n) | ||
* | ||
@@ -578,8 +602,8 @@ * The `lesserOrGreaterTraverse` function traverses a binary tree and returns an array of nodes that | ||
/** | ||
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. | ||
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(log n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) - Building a balanced tree from a sorted array. | ||
* Space Complexity: O(n) - Additional space is required for the sorted array. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(log n) | ||
* | ||
@@ -643,4 +667,4 @@ * The `perfectlyBalance` function balances a binary search tree by adding nodes in a way that | ||
/** | ||
* Time Complexity: O(n) - Visiting each node once. | ||
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(log n) | ||
* | ||
@@ -715,3 +739,3 @@ * The function checks if a binary tree is AVL balanced using either recursive or iterative approach. | ||
const extractedB = this.extractor(b); | ||
const compared = this.variant === types_1.BSTVariant.MIN ? extractedA - extractedB : extractedB - extractedA; | ||
const compared = this.variant === types_1.BSTVariant.STANDARD ? extractedA - extractedB : extractedB - extractedA; | ||
return compared > 0 ? types_1.CP.gt : compared < 0 ? types_1.CP.lt : types_1.CP.eq; | ||
@@ -718,0 +742,0 @@ } |
@@ -8,3 +8,3 @@ /** | ||
*/ | ||
import { BinaryTreeDeleteResult, BTNCallback, BTNExemplar, BTNKeyOrNode, IterationType, RBTNColor, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types'; | ||
import { BinaryTreeDeleteResult, BTNCallback, IterationType, KeyOrNodeOrEntry, RBTNColor, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types'; | ||
import { BST, BSTNode } from './bst'; | ||
@@ -27,7 +27,7 @@ import { IBinaryTree } from '../../interfaces'; | ||
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which | ||
* initializes the tree with optional elements and options. | ||
* @param [elements] - The `elements` parameter is an optional iterable of `BTNExemplar<K, V, N>` | ||
* objects. It represents the initial elements that will be added to the RBTree during its | ||
* initializes the tree with optional nodes and options. | ||
* @param [nodes] - The `nodes` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>` | ||
* objects. It represents the initial nodes that will be added to the RBTree during its | ||
* construction. If this parameter is provided, the `addMany` method is called to add all the | ||
* elements to the | ||
* nodes to the | ||
* @param [options] - The `options` parameter is an optional object that allows you to customize the | ||
@@ -37,3 +37,3 @@ * behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide | ||
*/ | ||
constructor(elements?: Iterable<BTNExemplar<K, V, N>>, options?: Partial<RBTreeOptions<K>>); | ||
constructor(nodes?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: Partial<RBTreeOptions<K>>); | ||
protected _root: N; | ||
@@ -65,9 +65,23 @@ get root(): N; | ||
/** | ||
* The function checks if an exemplar is an instance of the RedBlackTreeNode class. | ||
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`. | ||
* @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode | ||
* The function `exemplarToNode` takes an keyOrNodeOrEntry and converts it into a node object if possible. | ||
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, where: | ||
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the | ||
* `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If a value | ||
* is provided, it will be used when creating the new node. If no value is provided, the new node | ||
* @returns a node of type N or undefined. | ||
*/ | ||
exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): N | undefined; | ||
/** | ||
* The function checks if an keyOrNodeOrEntry is an instance of the RedBlackTreeNode class. | ||
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`. | ||
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the RedBlackTreeNode | ||
* class. | ||
*/ | ||
isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N; | ||
isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N; | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* Space Complexity: O(1) | ||
*/ | ||
isRealNode(node: N | undefined): node is N; | ||
/** | ||
* The function "isNotNodeInstance" checks if a potential key is a K. | ||
@@ -78,18 +92,10 @@ * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any | ||
*/ | ||
isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K; | ||
isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K; | ||
/** | ||
* The function `exemplarToNode` takes an exemplar and converts it into a node object if possible. | ||
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, where: | ||
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the | ||
* `exemplarToNode` function. It represents the value associated with the exemplar node. If a value | ||
* is provided, it will be used when creating the new node. If no value is provided, the new node | ||
* @returns a node of type N or undefined. | ||
*/ | ||
exemplarToNode(exemplar: BTNExemplar<K, V, N>, value?: V): N | undefined; | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* on average (where n is the number of nodes in the tree) | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
@@ -105,9 +111,10 @@ * | ||
*/ | ||
add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V): N | undefined; | ||
add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): boolean; | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* on average (where n is the number of nodes in the tree) | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
@@ -127,7 +134,2 @@ * | ||
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null | undefined, callback?: C): BinaryTreeDeleteResult<N>[]; | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* Space Complexity: O(1) | ||
*/ | ||
isRealNode(node: N | undefined): node is N; | ||
getNode<C extends BTNCallback<N, K>>(identifier: K, callback?: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined; | ||
@@ -151,3 +153,3 @@ getNode<C extends BTNCallback<N, N>>(identifier: N | undefined, callback?: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined; | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
@@ -183,7 +185,7 @@ */ | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
@@ -209,7 +211,7 @@ * | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
@@ -216,0 +218,0 @@ * |
@@ -30,7 +30,7 @@ "use strict"; | ||
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which | ||
* initializes the tree with optional elements and options. | ||
* @param [elements] - The `elements` parameter is an optional iterable of `BTNExemplar<K, V, N>` | ||
* objects. It represents the initial elements that will be added to the RBTree during its | ||
* initializes the tree with optional nodes and options. | ||
* @param [nodes] - The `nodes` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>` | ||
* objects. It represents the initial nodes that will be added to the RBTree during its | ||
* construction. If this parameter is provided, the `addMany` method is called to add all the | ||
* elements to the | ||
* nodes to the | ||
* @param [options] - The `options` parameter is an optional object that allows you to customize the | ||
@@ -40,3 +40,3 @@ * behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide | ||
*/ | ||
constructor(elements, options) { | ||
constructor(nodes, options) { | ||
super([], options); | ||
@@ -46,4 +46,4 @@ this.Sentinel = new RedBlackTreeNode(NaN); | ||
this._root = this.Sentinel; | ||
if (elements) | ||
super.addMany(elements); | ||
if (nodes) | ||
super.addMany(nodes); | ||
} | ||
@@ -82,37 +82,19 @@ get root() { | ||
/** | ||
* The function checks if an exemplar is an instance of the RedBlackTreeNode class. | ||
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`. | ||
* @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode | ||
* class. | ||
*/ | ||
isNode(exemplar) { | ||
return exemplar instanceof RedBlackTreeNode; | ||
} | ||
/** | ||
* The function "isNotNodeInstance" checks if a potential key is a K. | ||
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any | ||
* data type. | ||
* @returns a boolean value indicating whether the potentialKey is of type number or not. | ||
*/ | ||
isNotNodeInstance(potentialKey) { | ||
return !(potentialKey instanceof RedBlackTreeNode); | ||
} | ||
/** | ||
* The function `exemplarToNode` takes an exemplar and converts it into a node object if possible. | ||
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, where: | ||
* The function `exemplarToNode` takes an keyOrNodeOrEntry and converts it into a node object if possible. | ||
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, where: | ||
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the | ||
* `exemplarToNode` function. It represents the value associated with the exemplar node. If a value | ||
* `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If a value | ||
* is provided, it will be used when creating the new node. If no value is provided, the new node | ||
* @returns a node of type N or undefined. | ||
*/ | ||
exemplarToNode(exemplar, value) { | ||
exemplarToNode(keyOrNodeOrEntry, value) { | ||
let node; | ||
if (exemplar === null || exemplar === undefined) { | ||
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) { | ||
return; | ||
} | ||
else if (this.isNode(exemplar)) { | ||
node = exemplar; | ||
else if (this.isNode(keyOrNodeOrEntry)) { | ||
node = keyOrNodeOrEntry; | ||
} | ||
else if (this.isEntry(exemplar)) { | ||
const [key, value] = exemplar; | ||
else if (this.isEntry(keyOrNodeOrEntry)) { | ||
const [key, value] = keyOrNodeOrEntry; | ||
if (key === undefined || key === null) { | ||
@@ -125,4 +107,4 @@ return; | ||
} | ||
else if (this.isNotNodeInstance(exemplar)) { | ||
node = this.createNode(exemplar, value, types_1.RBTNColor.RED); | ||
else if (this.isNotNodeInstance(keyOrNodeOrEntry)) { | ||
node = this.createNode(keyOrNodeOrEntry, value, types_1.RBTNColor.RED); | ||
} | ||
@@ -135,8 +117,36 @@ else { | ||
/** | ||
* The function checks if an keyOrNodeOrEntry is an instance of the RedBlackTreeNode class. | ||
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`. | ||
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the RedBlackTreeNode | ||
* class. | ||
*/ | ||
isNode(keyOrNodeOrEntry) { | ||
return keyOrNodeOrEntry instanceof RedBlackTreeNode; | ||
} | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* Space Complexity: O(1) | ||
*/ | ||
isRealNode(node) { | ||
if (node === this.Sentinel || node === undefined) | ||
return false; | ||
return node instanceof RedBlackTreeNode; | ||
} | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* The function "isNotNodeInstance" checks if a potential key is a K. | ||
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any | ||
* data type. | ||
* @returns a boolean value indicating whether the potentialKey is of type number or not. | ||
*/ | ||
isNotNodeInstance(potentialKey) { | ||
return !(potentialKey instanceof RedBlackTreeNode); | ||
} | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* on average (where n is the number of nodes in the tree) | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -154,3 +164,3 @@ * The `add` function adds a new node to a binary search tree and performs necessary rotations and | ||
if (newNode === undefined) | ||
return; | ||
return false; | ||
newNode.left = this.Sentinel; | ||
@@ -173,3 +183,3 @@ newNode.right = this.Sentinel; | ||
} | ||
return; | ||
return false; | ||
} | ||
@@ -191,17 +201,19 @@ } | ||
this._size++; | ||
return; | ||
return false; | ||
} | ||
if (newNode.parent.parent === undefined) { | ||
this._size++; | ||
return; | ||
return false; | ||
} | ||
this._fixInsert(newNode); | ||
this._size++; | ||
return true; | ||
} | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* on average (where n is the number of nodes in the tree) | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
@@ -279,17 +291,8 @@ * | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
*/ | ||
isRealNode(node) { | ||
if (node === this.Sentinel || node === undefined) | ||
return false; | ||
return node instanceof RedBlackTreeNode; | ||
} | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -345,3 +348,3 @@ * The function `getNode` retrieves a single node from a binary tree based on a given identifier and | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
@@ -427,7 +430,7 @@ */ | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
@@ -527,7 +530,7 @@ * | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
@@ -534,0 +537,0 @@ * |
@@ -8,3 +8,3 @@ /** | ||
*/ | ||
import type { BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, BTNExemplar, BTNKeyOrNode, TreeMultimapNested, TreeMultimapNodeNested, TreeMultimapOptions } from '../../types'; | ||
import type { BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, KeyOrNodeOrEntry, TreeMultimapNested, TreeMultimapNodeNested, TreeMultimapOptions } from '../../types'; | ||
import { IterationType } from '../../types'; | ||
@@ -31,3 +31,3 @@ import { IBinaryTree } from '../../interfaces'; | ||
export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N> = TreeMultimapNode<K, V, TreeMultimapNodeNested<K, V>>, TREE extends TreeMultimap<K, V, N, TREE> = TreeMultimap<K, V, N, TreeMultimapNested<K, V, N>>> extends AVLTree<K, V, N, TREE> implements IBinaryTree<K, V, N, TREE> { | ||
constructor(elements?: Iterable<BTNExemplar<K, V, N>>, options?: Partial<TreeMultimapOptions<K>>); | ||
constructor(nodes?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: Partial<TreeMultimapOptions<K>>); | ||
private _count; | ||
@@ -47,8 +47,20 @@ get count(): number; | ||
/** | ||
* The function checks if an exemplar is an instance of the TreeMultimapNode class. | ||
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`. | ||
* @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode | ||
* The function `exemplarToNode` converts an keyOrNodeOrEntry object into a node object. | ||
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, which means it | ||
* can be one of the following: | ||
* @param {V} [value] - The `value` parameter is an optional argument that represents the value | ||
* associated with the node. It is of type `V`, which can be any data type. If no value is provided, | ||
* it defaults to `undefined`. | ||
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of | ||
* times the value should be added to the node. If not provided, it defaults to 1. | ||
* @returns a node of type `N` or `undefined`. | ||
*/ | ||
exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V, count?: number): N | undefined; | ||
/** | ||
* The function checks if an keyOrNodeOrEntry is an instance of the TreeMultimapNode class. | ||
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`. | ||
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the TreeMultimapNode | ||
* class. | ||
*/ | ||
isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N; | ||
isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N; | ||
/** | ||
@@ -60,22 +72,11 @@ * The function "isNotNodeInstance" checks if a potential key is a K. | ||
*/ | ||
isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K; | ||
isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K; | ||
/** | ||
* The function `exemplarToNode` converts an exemplar object into a node object. | ||
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, which means it | ||
* can be one of the following: | ||
* @param {V} [value] - The `value` parameter is an optional argument that represents the value | ||
* associated with the node. It is of type `V`, which can be any data type. If no value is provided, | ||
* it defaults to `undefined`. | ||
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of | ||
* times the value should be added to the node. If not provided, it defaults to 1. | ||
* @returns a node of type `N` or `undefined`. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size. | ||
*/ | ||
exemplarToNode(exemplar: BTNExemplar<K, V, N>, value?: V, count?: number): N | undefined; | ||
/** | ||
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. | ||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. | ||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -94,10 +95,11 @@ * The function overrides the add method of a binary tree node and adds a new node to the tree. | ||
*/ | ||
add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V, count?: number): N | undefined; | ||
add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V, count?: number): boolean; | ||
/** | ||
* Time Complexity: O(k log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. | ||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
* Time Complexity: O(k log n) | ||
* Space Complexity: O(1) | ||
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size. | ||
*/ | ||
/** | ||
* Time Complexity: O(k log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. | ||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
* Time Complexity: O(k log n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -110,10 +112,11 @@ * The function overrides the addMany method to add multiple keys, nodes, or entries to a data | ||
*/ | ||
addMany(keysOrNodesOrEntries: Iterable<BTNExemplar<K, V, N>>): (N | undefined)[]; | ||
addMany(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>>): boolean[]; | ||
/** | ||
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments. | ||
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory. | ||
* Time Complexity: O(n log n) | ||
* Space Complexity: O(n) | ||
* logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node. linear space, as it creates an array to store the sorted nodes. | ||
*/ | ||
/** | ||
* Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node. | ||
* Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes. | ||
* Time Complexity: O(n log n) | ||
* Space Complexity: O(n) | ||
* | ||
@@ -129,8 +132,9 @@ * The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search | ||
/** | ||
* Time Complexity: O(k log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each. | ||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
* Time Complexity: O(k log n) | ||
* Space Complexity: O(1) | ||
* logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each. constant space, as it doesn't use additional data structures that scale with input size. | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (AVLTree) has logarithmic time complexity. | ||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
* Time Complexity: O(k log n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -154,6 +158,9 @@ * The `delete` function in TypeScript is used to remove a node from a binary tree, taking into | ||
/** | ||
* Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node. | ||
* Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The clear() function clears the contents of a data structure and sets the count to zero. | ||
@@ -160,0 +167,0 @@ */ |
@@ -27,7 +27,7 @@ "use strict"; | ||
class TreeMultimap extends avl_tree_1.AVLTree { | ||
constructor(elements, options) { | ||
constructor(nodes, options) { | ||
super([], options); | ||
this._count = 0; | ||
if (elements) | ||
this.addMany(elements); | ||
if (nodes) | ||
this.addMany(nodes); | ||
} | ||
@@ -56,22 +56,4 @@ // TODO the _count is not accurate after nodes count modified | ||
/** | ||
* The function checks if an exemplar is an instance of the TreeMultimapNode class. | ||
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`. | ||
* @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode | ||
* class. | ||
*/ | ||
isNode(exemplar) { | ||
return exemplar instanceof TreeMultimapNode; | ||
} | ||
/** | ||
* The function "isNotNodeInstance" checks if a potential key is a K. | ||
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any | ||
* data type. | ||
* @returns a boolean value indicating whether the potentialKey is of type number or not. | ||
*/ | ||
isNotNodeInstance(potentialKey) { | ||
return !(potentialKey instanceof TreeMultimapNode); | ||
} | ||
/** | ||
* The function `exemplarToNode` converts an exemplar object into a node object. | ||
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, which means it | ||
* The function `exemplarToNode` converts an keyOrNodeOrEntry object into a node object. | ||
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, which means it | ||
* can be one of the following: | ||
@@ -85,12 +67,12 @@ * @param {V} [value] - The `value` parameter is an optional argument that represents the value | ||
*/ | ||
exemplarToNode(exemplar, value, count = 1) { | ||
exemplarToNode(keyOrNodeOrEntry, value, count = 1) { | ||
let node; | ||
if (exemplar === undefined || exemplar === null) { | ||
if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) { | ||
return; | ||
} | ||
else if (this.isNode(exemplar)) { | ||
node = exemplar; | ||
else if (this.isNode(keyOrNodeOrEntry)) { | ||
node = keyOrNodeOrEntry; | ||
} | ||
else if (this.isEntry(exemplar)) { | ||
const [key, value] = exemplar; | ||
else if (this.isEntry(keyOrNodeOrEntry)) { | ||
const [key, value] = keyOrNodeOrEntry; | ||
if (key === undefined || key === null) { | ||
@@ -103,4 +85,4 @@ return; | ||
} | ||
else if (this.isNotNodeInstance(exemplar)) { | ||
node = this.createNode(exemplar, value, count); | ||
else if (this.isNotNodeInstance(keyOrNodeOrEntry)) { | ||
node = this.createNode(keyOrNodeOrEntry, value, count); | ||
} | ||
@@ -113,8 +95,27 @@ else { | ||
/** | ||
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. | ||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
* The function checks if an keyOrNodeOrEntry is an instance of the TreeMultimapNode class. | ||
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`. | ||
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the TreeMultimapNode | ||
* class. | ||
*/ | ||
isNode(keyOrNodeOrEntry) { | ||
return keyOrNodeOrEntry instanceof TreeMultimapNode; | ||
} | ||
/** | ||
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. | ||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
* The function "isNotNodeInstance" checks if a potential key is a K. | ||
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any | ||
* data type. | ||
* @returns a boolean value indicating whether the potentialKey is of type number or not. | ||
*/ | ||
isNotNodeInstance(potentialKey) { | ||
return !(potentialKey instanceof TreeMultimapNode); | ||
} | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size. | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -136,3 +137,3 @@ * The function overrides the add method of a binary tree node and adds a new node to the tree. | ||
if (newNode === undefined) | ||
return; | ||
return false; | ||
const orgNodeCount = (newNode === null || newNode === void 0 ? void 0 : newNode.count) || 0; | ||
@@ -143,11 +144,12 @@ const inserted = super.add(newNode); | ||
} | ||
return inserted; | ||
return true; | ||
} | ||
/** | ||
* Time Complexity: O(k log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. | ||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
* Time Complexity: O(k log n) | ||
* Space Complexity: O(1) | ||
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size. | ||
*/ | ||
/** | ||
* Time Complexity: O(k log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. | ||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
* Time Complexity: O(k log n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -164,8 +166,9 @@ * The function overrides the addMany method to add multiple keys, nodes, or entries to a data | ||
/** | ||
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments. | ||
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory. | ||
* Time Complexity: O(n log n) | ||
* Space Complexity: O(n) | ||
* logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node. linear space, as it creates an array to store the sorted nodes. | ||
*/ | ||
/** | ||
* Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node. | ||
* Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes. | ||
* Time Complexity: O(n log n) | ||
* Space Complexity: O(n) | ||
* | ||
@@ -216,8 +219,9 @@ * The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search | ||
/** | ||
* Time Complexity: O(k log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each. | ||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
* Time Complexity: O(k log n) | ||
* Space Complexity: O(1) | ||
* logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each. constant space, as it doesn't use additional data structures that scale with input size. | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (AVLTree) has logarithmic time complexity. | ||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
* Time Complexity: O(k log n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -298,6 +302,9 @@ * The `delete` function in TypeScript is used to remove a node from a binary tree, taking into | ||
/** | ||
* Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node. | ||
* Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The clear() function clears the contents of a data structure and sets the count to zero. | ||
@@ -304,0 +311,0 @@ */ |
import { BinaryTree, BinaryTreeNode } from '../data-structures'; | ||
import { BinaryTreeDeleteResult, BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BTNCallback, BTNExemplar } from '../types'; | ||
import { BinaryTreeDeleteResult, BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BTNCallback, KeyOrNodeOrEntry } from '../types'; | ||
export interface IBinaryTree<K = number, V = any, N extends BinaryTreeNode<K, V, N> = BinaryTreeNodeNested<K, V>, TREE extends BinaryTree<K, V, N, TREE> = BinaryTreeNested<K, V, N>> { | ||
createNode(key: K, value?: N['value']): N; | ||
createTree(options?: Partial<BinaryTreeOptions<K>>): TREE; | ||
add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V, count?: number): N | null | undefined; | ||
addMany(nodes: Iterable<BTNExemplar<K, V, N>>, values?: Iterable<V | undefined>): (N | null | undefined)[]; | ||
add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V, count?: number): boolean; | ||
addMany(nodes: Iterable<KeyOrNodeOrEntry<K, V, N>>, values?: Iterable<V | undefined>): boolean[]; | ||
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeleteResult<N>[]; | ||
} |
export declare enum BSTVariant { | ||
MIN = "MIN", | ||
MAX = "MAX" | ||
STANDARD = "STANDARD", | ||
INVERSE = "INVERSE" | ||
} | ||
@@ -47,3 +47,3 @@ export declare enum CP { | ||
export type BTNKeyOrNode<K, N> = K | null | undefined | N; | ||
export type BTNExemplar<K, V, N> = BTNEntry<K, V> | BTNKeyOrNode<K, N>; | ||
export type KeyOrNodeOrEntry<K, V, N> = BTNEntry<K, V> | BTNKeyOrNode<K, N>; | ||
export type BTNodePureKeyOrNode<K, N> = K | N; | ||
@@ -50,0 +50,0 @@ export type BTNodePureExemplar<K, V, N> = [K, V | undefined] | BTNodePureKeyOrNode<K, N>; |
@@ -6,4 +6,4 @@ "use strict"; | ||
(function (BSTVariant) { | ||
BSTVariant["MIN"] = "MIN"; | ||
BSTVariant["MAX"] = "MAX"; | ||
BSTVariant["STANDARD"] = "STANDARD"; | ||
BSTVariant["INVERSE"] = "INVERSE"; | ||
})(BSTVariant = exports.BSTVariant || (exports.BSTVariant = {})); | ||
@@ -10,0 +10,0 @@ var CP; |
{ | ||
"name": "red-black-tree-typed", | ||
"version": "1.49.5", | ||
"version": "1.49.6", | ||
"description": "RedBlackTree. Javascript & Typescript Data Structure.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -16,4 +16,3 @@ /** | ||
BTNCallback, | ||
BTNExemplar, | ||
BTNKeyOrNode | ||
KeyOrNodeOrEntry | ||
} from '../../types'; | ||
@@ -53,5 +52,5 @@ import { IBinaryTree } from '../../interfaces'; | ||
/** | ||
* The constructor function initializes an AVLTree object with optional elements and options. | ||
* @param [elements] - The `elements` parameter is an optional iterable of `BTNExemplar<K, V, N>` | ||
* objects. It represents a collection of elements that will be added to the AVL tree during | ||
* The constructor function initializes an AVLTree object with optional nodes and options. | ||
* @param [nodes] - The `nodes` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>` | ||
* objects. It represents a collection of nodes that will be added to the AVL tree during | ||
* initialization. | ||
@@ -62,5 +61,5 @@ * @param [options] - The `options` parameter is an optional object that allows you to customize the | ||
*/ | ||
constructor(elements?: Iterable<BTNExemplar<K, V, N>>, options?: Partial<AVLTreeOptions<K>>) { | ||
constructor(nodes?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: Partial<AVLTreeOptions<K>>) { | ||
super([], options); | ||
if (elements) super.addMany(elements); | ||
if (nodes) super.addMany(nodes); | ||
} | ||
@@ -97,8 +96,8 @@ | ||
/** | ||
* The function checks if an exemplar is an instance of AVLTreeNode. | ||
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`. | ||
* @returns a boolean value indicating whether the exemplar is an instance of the AVLTreeNode class. | ||
* The function checks if an keyOrNodeOrEntry is an instance of AVLTreeNode. | ||
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`. | ||
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeNode class. | ||
*/ | ||
override isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N { | ||
return exemplar instanceof AVLTreeNode; | ||
override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N { | ||
return keyOrNodeOrEntry instanceof AVLTreeNode; | ||
} | ||
@@ -112,3 +111,3 @@ | ||
*/ | ||
override isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K { | ||
override isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K { | ||
return !(potentialKey instanceof AVLTreeNode); | ||
@@ -118,9 +117,10 @@ } | ||
/** | ||
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity. | ||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size. | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity. | ||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -135,6 +135,6 @@ * The function overrides the add method of a binary tree node and balances the tree after inserting | ||
*/ | ||
override add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V): N | undefined { | ||
if (keyOrNodeOrEntry === null) return undefined; | ||
override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): boolean { | ||
if (keyOrNodeOrEntry === null) return false; | ||
const inserted = super.add(keyOrNodeOrEntry, value); | ||
if (inserted) this._balancePath(inserted); | ||
if (inserted) this._balancePath(keyOrNodeOrEntry); | ||
return inserted; | ||
@@ -144,9 +144,9 @@ } | ||
/** | ||
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity. | ||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (BST) has logarithmic time complexity. | ||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -214,9 +214,10 @@ * The function overrides the delete method of a binary tree, performs the deletion, and then | ||
/** | ||
* Time Complexity: O(1) - constant time, as it performs a fixed number of operations. | ||
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* constant time, as it performs a fixed number of operations. constant space, as it only uses a constant amount of memory. | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time, as it performs a fixed number of operations. | ||
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -239,9 +240,10 @@ * The function calculates the balance factor of a node in a binary tree. | ||
/** | ||
* Time Complexity: O(1) - constant time, as it performs a fixed number of operations. | ||
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* constant time, as it performs a fixed number of operations. constant space, as it only uses a constant amount of memory. | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time, as it performs a fixed number of operations. | ||
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -262,9 +264,10 @@ * The function updates the height of a node in a binary tree based on the heights of its left and | ||
/** | ||
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The method traverses the path from the inserted node to the root. | ||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* logarithmic time, where "n" is the number of nodes in the tree. The method traverses the path from the inserted node to the root. constant space, as it doesn't use additional data structures that scale with input size. | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The method traverses the path from the inserted node to the root. | ||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -276,3 +279,4 @@ * The `_balancePath` function is used to update the heights of nodes and perform rotation operations | ||
*/ | ||
protected _balancePath(node: N): void { | ||
protected _balancePath(node: KeyOrNodeOrEntry<K, V, N>): void { | ||
node = this.ensureNode(node); | ||
const path = this.getPathToRoot(node, false); // first O(log n) + O(log n) | ||
@@ -317,9 +321,10 @@ for (let i = 0; i < path.length; i++) { | ||
/** | ||
* Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations. | ||
* Space Complexity: O(1) - constant space, as they only use a constant amount of memory. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* constant time, as these methods perform a fixed number of operations. constant space, as they only use a constant amount of memory. | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations. | ||
* Space Complexity: O(1) - constant space, as they only use a constant amount of memory. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -356,9 +361,9 @@ * The function `_balanceLL` performs a left-left rotation to balance a binary tree. | ||
/** | ||
* Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations. | ||
* Space Complexity: O(1) - constant space, as they only use a constant amount of memory. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations. | ||
* Space Complexity: O(1) - constant space, as they only use a constant amount of memory. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -413,9 +418,9 @@ * The `_balanceLR` function performs a left-right rotation to balance a binary tree. | ||
/** | ||
* Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations. | ||
* Space Complexity: O(1) - constant space, as they only use a constant amount of memory. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations. | ||
* Space Complexity: O(1) - constant space, as they only use a constant amount of memory. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -457,9 +462,9 @@ * The function `_balanceRR` performs a right-right rotation to balance a binary tree. | ||
/** | ||
* Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations. | ||
* Space Complexity: O(1) - constant space, as they only use a constant amount of memory. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations. | ||
* Space Complexity: O(1) - constant space, as they only use a constant amount of memory. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -466,0 +471,0 @@ * The function `_balanceRL` performs a right-left rotation to balance a binary tree. |
@@ -10,9 +10,7 @@ /** | ||
BSTNested, | ||
BSTNKeyOrNode, | ||
BSTNodeNested, | ||
BSTOptions, | ||
BTNCallback, | ||
BTNExemplar, | ||
BTNKeyOrNode, | ||
BTNodePureExemplar | ||
BTNodePureExemplar, | ||
KeyOrNodeOrEntry | ||
} from '../../types'; | ||
@@ -98,4 +96,4 @@ import { BSTVariant, CP, IterationType } from '../../types'; | ||
* This is the constructor function for a binary search tree class in TypeScript, which initializes | ||
* the tree with optional elements and options. | ||
* @param [elements] - An optional iterable of BTNExemplar objects that will be added to the | ||
* the tree with optional nodes and options. | ||
* @param [nodes] - An optional iterable of KeyOrNodeOrEntry objects that will be added to the | ||
* binary search tree. | ||
@@ -105,3 +103,3 @@ * @param [options] - The `options` parameter is an optional object that can contain additional | ||
*/ | ||
constructor(elements?: Iterable<BTNExemplar<K, V, N>>, options?: Partial<BSTOptions<K>>) { | ||
constructor(nodes?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: Partial<BSTOptions<K>>) { | ||
super([], options); | ||
@@ -118,3 +116,3 @@ | ||
if (elements) this.addMany(elements); | ||
if (nodes) this.addMany(nodes); | ||
} | ||
@@ -128,3 +126,3 @@ | ||
protected _variant = BSTVariant.MIN; | ||
protected _variant = BSTVariant.STANDARD; | ||
@@ -163,26 +161,17 @@ get variant() { | ||
/** | ||
* The function checks if an exemplar is an instance of BSTNode. | ||
* @param exemplar - The `exemplar` parameter is a variable of type `BTNExemplar<K, V, N>`. | ||
* @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class. | ||
*/ | ||
override isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N { | ||
return exemplar instanceof BSTNode; | ||
} | ||
/** | ||
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid, | ||
* The function `exemplarToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid, | ||
* otherwise it returns undefined. | ||
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, where: | ||
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, where: | ||
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the | ||
* `exemplarToNode` function. It represents the value associated with the exemplar node. | ||
* `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node. | ||
* @returns a node of type N or undefined. | ||
*/ | ||
override exemplarToNode(exemplar: BTNExemplar<K, V, N>, value?: V): N | undefined { | ||
override exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): N | undefined { | ||
let node: N | undefined; | ||
if (exemplar === null || exemplar === undefined) { | ||
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) { | ||
return; | ||
} else if (this.isNode(exemplar)) { | ||
node = exemplar; | ||
} else if (this.isEntry(exemplar)) { | ||
const [key, value] = exemplar; | ||
} else if (this.isNode(keyOrNodeOrEntry)) { | ||
node = keyOrNodeOrEntry; | ||
} else if (this.isEntry(keyOrNodeOrEntry)) { | ||
const [key, value] = keyOrNodeOrEntry; | ||
if (key === undefined || key === null) { | ||
@@ -193,4 +182,4 @@ return; | ||
} | ||
} else if (this.isNotNodeInstance(exemplar)) { | ||
node = this.createNode(exemplar, value); | ||
} else if (this.isNotNodeInstance(keyOrNodeOrEntry)) { | ||
node = this.createNode(keyOrNodeOrEntry, value); | ||
} else { | ||
@@ -203,10 +192,63 @@ return; | ||
/** | ||
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n). | ||
* Space Complexity: O(1) - Constant space is used. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(log n) | ||
* Average case for a balanced tree. Space for the recursive call stack in the worst case. | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n). | ||
* Space Complexity: O(1) - Constant space is used. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(log n) | ||
* | ||
* The function `ensureNode` returns the node corresponding to the given key if it is a node key, | ||
* otherwise it returns the key itself. | ||
* @param {K | N | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `N`, or | ||
* `undefined`. | ||
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the | ||
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`. | ||
* @returns either a node object (N) or undefined. | ||
*/ | ||
override ensureNode( | ||
keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, | ||
iterationType = IterationType.ITERATIVE | ||
): N | undefined { | ||
let res: N | undefined; | ||
if (this.isRealNode(keyOrNodeOrEntry)) { | ||
res = keyOrNodeOrEntry; | ||
} else if (this.isEntry(keyOrNodeOrEntry)) { | ||
if (keyOrNodeOrEntry[0]) res = this.getNodeByKey(keyOrNodeOrEntry[0], iterationType); | ||
} else { | ||
if (keyOrNodeOrEntry) res = this.getNodeByKey(keyOrNodeOrEntry, iterationType); | ||
} | ||
return res; | ||
} | ||
/** | ||
* The function "isNotNodeInstance" checks if a potential key is a K. | ||
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any | ||
* data type. | ||
* @returns a boolean value indicating whether the potentialKey is of type number or not. | ||
*/ | ||
override isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K { | ||
return !(potentialKey instanceof BSTNode); | ||
} | ||
/** | ||
* The function checks if an keyOrNodeOrEntry is an instance of BSTNode. | ||
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, N>`. | ||
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the BSTNode class. | ||
*/ | ||
override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N { | ||
return keyOrNodeOrEntry instanceof BSTNode; | ||
} | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n). | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* | ||
* The `add` function adds a new node to a binary tree, updating the value if the key already exists | ||
@@ -220,5 +262,5 @@ * or inserting a new node if the key is unique. | ||
*/ | ||
override add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V): N | undefined { | ||
override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): boolean { | ||
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value); | ||
if (newNode === undefined) return; | ||
if (newNode === undefined) return false; | ||
@@ -228,3 +270,3 @@ if (this.root === undefined) { | ||
this._size++; | ||
return this.root; | ||
return true; | ||
} | ||
@@ -238,3 +280,3 @@ | ||
this._replaceNode(current, newNode); | ||
return newNode; | ||
return true; | ||
@@ -252,3 +294,3 @@ // } else { | ||
this._size++; | ||
return newNode; | ||
return true; | ||
} | ||
@@ -261,3 +303,3 @@ current = current.left; | ||
this._size++; | ||
return newNode; | ||
return true; | ||
} | ||
@@ -268,13 +310,14 @@ current = current.right; | ||
return undefined; | ||
return false; | ||
} | ||
/** | ||
* Time Complexity: O(k log n) - Adding each element individually in a balanced tree. | ||
* Space Complexity: O(k) - Additional space is required for the sorted array. | ||
* Time Complexity: O(k log n) | ||
* Space Complexity: O(k) | ||
* Adding each element individually in a balanced tree. Additional space is required for the sorted array. | ||
*/ | ||
/** | ||
* Time Complexity: O(k log n) - Adding each element individually in a balanced tree. | ||
* Space Complexity: O(k) - Additional space is required for the sorted array. | ||
* Time Complexity: O(k log n) | ||
* Space Complexity: O(k) | ||
* | ||
@@ -290,3 +333,3 @@ * The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally | ||
* balanced or not. If set to true, the add operation will be balanced using a binary search tree | ||
* algorithm. If set to false, the add operation will not be balanced and the elements will be added | ||
* algorithm. If set to false, the add operation will not be balanced and the nodes will be added | ||
* in the order they appear in the input. | ||
@@ -299,8 +342,8 @@ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the | ||
override addMany( | ||
keysOrNodesOrEntries: Iterable<BTNExemplar<K, V, N>>, | ||
keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>>, | ||
values?: Iterable<V | undefined>, | ||
isBalanceAdd = true, | ||
iterationType = this.iterationType | ||
): (N | undefined)[] { | ||
const inserted: (N | undefined)[] = []; | ||
): boolean[] { | ||
const inserted: boolean[] = []; | ||
@@ -324,3 +367,3 @@ let valuesIterator: Iterator<V | undefined> | undefined; | ||
const isRealBTNExemplar = (kve: BTNExemplar<K, V, N>): kve is BTNodePureExemplar<K, V, N> => { | ||
const isRealBTNExemplar = (kve: KeyOrNodeOrEntry<K, V, N>): kve is BTNodePureExemplar<K, V, N> => { | ||
if (kve === undefined || kve === null) return false; | ||
@@ -387,9 +430,10 @@ return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null)); | ||
/** | ||
* Time Complexity: O(n log n) - Adding each element individually in a balanced tree. | ||
* Space Complexity: O(n) - Additional space is required for the sorted array. | ||
* Time Complexity: O(n log n) | ||
* Space Complexity: O(n) | ||
* Adding each element individually in a balanced tree. Additional space is required for the sorted array. | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) - Average case for a balanced tree. | ||
* Space Complexity: O(1) - Constant space is used. | ||
* Time Complexity: O(n log n) | ||
* Space Complexity: O(n) | ||
* | ||
@@ -401,4 +445,2 @@ * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the | ||
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`). | ||
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to | ||
* be performed. It can have one of the following values: | ||
* @returns the key of the rightmost node in the binary tree if the comparison result is less than, | ||
@@ -408,7 +450,7 @@ * the key of the leftmost node if the comparison result is greater than, and the key of the | ||
*/ | ||
lastKey(beginRoot: BSTNKeyOrNode<K, N> = this.root): K | undefined { | ||
lastKey(beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root): K | undefined { | ||
let current = this.ensureNode(beginRoot); | ||
if (!current) return undefined; | ||
if (this._variant === BSTVariant.MIN) { | ||
if (this._variant === BSTVariant.STANDARD) { | ||
// For BSTVariant.MIN, find the rightmost node | ||
@@ -428,9 +470,9 @@ while (current.right !== undefined) { | ||
/** | ||
* Time Complexity: O(log n) - Average case for a balanced tree. | ||
* Space Complexity: O(1) - Constant space is used. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) - Average case for a balanced tree. | ||
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -473,32 +515,10 @@ * The function `getNodeByKey` searches for a node in a binary tree based on a given key, using | ||
/** | ||
* The function "isNotNodeInstance" checks if a potential key is a K. | ||
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any | ||
* data type. | ||
* @returns a boolean value indicating whether the potentialKey is of type number or not. | ||
*/ | ||
override isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K { | ||
return !(potentialKey instanceof BSTNode); | ||
} | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(log n) | ||
* Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case. | ||
* / | ||
/** | ||
* Time Complexity: O(log n) - Average case for a balanced tree. | ||
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case. | ||
*/ | ||
/** | ||
* The function `ensureNode` returns the node corresponding to the given key if it is a node key, | ||
* otherwise it returns the key itself. | ||
* @param {K | N | undefined} key - The `key` parameter can be of type `K`, `N`, or | ||
* `undefined`. | ||
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the | ||
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`. | ||
* @returns either a node object (N) or undefined. | ||
*/ | ||
override ensureNode(key: BSTNKeyOrNode<K, N>, iterationType = IterationType.ITERATIVE): N | undefined { | ||
return this.isNotNodeInstance(key) ? this.getNodeByKey(key, iterationType) : key; | ||
} | ||
/** | ||
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. | ||
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case. | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(log n) | ||
* | ||
@@ -528,3 +548,3 @@ * The function `getNodes` returns an array of nodes that match a given identifier, using either a | ||
onlyOne = false, | ||
beginRoot: BSTNKeyOrNode<K, N> = this.root, | ||
beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root, | ||
iterationType = this.iterationType | ||
@@ -582,9 +602,10 @@ ): N[] { | ||
/** | ||
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. | ||
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(log n) | ||
* Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case. | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. | ||
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(log n) | ||
* | ||
@@ -611,3 +632,3 @@ * The `lesserOrGreaterTraverse` function traverses a binary tree and returns an array of nodes that | ||
lesserOrGreater: CP = CP.lt, | ||
targetNode: BSTNKeyOrNode<K, N> = this.root, | ||
targetNode: KeyOrNodeOrEntry<K, V, N> = this.root, | ||
iterationType = this.iterationType | ||
@@ -651,9 +672,9 @@ ): ReturnType<C>[] { | ||
/** | ||
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. | ||
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(log n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) - Building a balanced tree from a sorted array. | ||
* Space Complexity: O(n) - Additional space is required for the sorted array. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(log n) | ||
* | ||
@@ -720,4 +741,4 @@ * The `perfectlyBalance` function balances a binary search tree by adding nodes in a way that | ||
/** | ||
* Time Complexity: O(n) - Visiting each node once. | ||
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(log n) | ||
* | ||
@@ -791,3 +812,3 @@ * The function checks if a binary tree is AVL balanced using either recursive or iterative approach. | ||
const extractedB = this.extractor(b); | ||
const compared = this.variant === BSTVariant.MIN ? extractedA - extractedB : extractedB - extractedA; | ||
const compared = this.variant === BSTVariant.STANDARD ? extractedA - extractedB : extractedB - extractedA; | ||
@@ -794,0 +815,0 @@ return compared > 0 ? CP.gt : compared < 0 ? CP.lt : CP.eq; |
@@ -13,5 +13,4 @@ /** | ||
BTNCallback, | ||
BTNExemplar, | ||
BTNKeyOrNode, | ||
IterationType, | ||
KeyOrNodeOrEntry, | ||
RBTNColor, | ||
@@ -57,7 +56,7 @@ RBTreeOptions, | ||
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which | ||
* initializes the tree with optional elements and options. | ||
* @param [elements] - The `elements` parameter is an optional iterable of `BTNExemplar<K, V, N>` | ||
* objects. It represents the initial elements that will be added to the RBTree during its | ||
* initializes the tree with optional nodes and options. | ||
* @param [nodes] - The `nodes` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>` | ||
* objects. It represents the initial nodes that will be added to the RBTree during its | ||
* construction. If this parameter is provided, the `addMany` method is called to add all the | ||
* elements to the | ||
* nodes to the | ||
* @param [options] - The `options` parameter is an optional object that allows you to customize the | ||
@@ -67,7 +66,7 @@ * behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide | ||
*/ | ||
constructor(elements?: Iterable<BTNExemplar<K, V, N>>, options?: Partial<RBTreeOptions<K>>) { | ||
constructor(nodes?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: Partial<RBTreeOptions<K>>) { | ||
super([], options); | ||
this._root = this.Sentinel; | ||
if (elements) super.addMany(elements); | ||
if (nodes) super.addMany(nodes); | ||
} | ||
@@ -119,38 +118,18 @@ | ||
/** | ||
* The function checks if an exemplar is an instance of the RedBlackTreeNode class. | ||
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`. | ||
* @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode | ||
* class. | ||
*/ | ||
override isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N { | ||
return exemplar instanceof RedBlackTreeNode; | ||
} | ||
/** | ||
* The function "isNotNodeInstance" checks if a potential key is a K. | ||
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any | ||
* data type. | ||
* @returns a boolean value indicating whether the potentialKey is of type number or not. | ||
*/ | ||
override isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K { | ||
return !(potentialKey instanceof RedBlackTreeNode); | ||
} | ||
/** | ||
* The function `exemplarToNode` takes an exemplar and converts it into a node object if possible. | ||
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, where: | ||
* The function `exemplarToNode` takes an keyOrNodeOrEntry and converts it into a node object if possible. | ||
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, where: | ||
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the | ||
* `exemplarToNode` function. It represents the value associated with the exemplar node. If a value | ||
* `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If a value | ||
* is provided, it will be used when creating the new node. If no value is provided, the new node | ||
* @returns a node of type N or undefined. | ||
*/ | ||
override exemplarToNode(exemplar: BTNExemplar<K, V, N>, value?: V): N | undefined { | ||
override exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): N | undefined { | ||
let node: N | undefined; | ||
if (exemplar === null || exemplar === undefined) { | ||
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) { | ||
return; | ||
} else if (this.isNode(exemplar)) { | ||
node = exemplar; | ||
} else if (this.isEntry(exemplar)) { | ||
const [key, value] = exemplar; | ||
} else if (this.isNode(keyOrNodeOrEntry)) { | ||
node = keyOrNodeOrEntry; | ||
} else if (this.isEntry(keyOrNodeOrEntry)) { | ||
const [key, value] = keyOrNodeOrEntry; | ||
if (key === undefined || key === null) { | ||
@@ -161,4 +140,4 @@ return; | ||
} | ||
} else if (this.isNotNodeInstance(exemplar)) { | ||
node = this.createNode(exemplar, value, RBTNColor.RED); | ||
} else if (this.isNotNodeInstance(keyOrNodeOrEntry)) { | ||
node = this.createNode(keyOrNodeOrEntry, value, RBTNColor.RED); | ||
} else { | ||
@@ -171,2 +150,12 @@ return; | ||
/** | ||
* The function checks if an keyOrNodeOrEntry is an instance of the RedBlackTreeNode class. | ||
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`. | ||
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the RedBlackTreeNode | ||
* class. | ||
*/ | ||
override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N { | ||
return keyOrNodeOrEntry instanceof RedBlackTreeNode; | ||
} | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
@@ -176,5 +165,26 @@ * Space Complexity: O(1) | ||
override isRealNode(node: N | undefined): node is N { | ||
if (node === this.Sentinel || node === undefined) return false; | ||
return node instanceof RedBlackTreeNode; | ||
} | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* The function "isNotNodeInstance" checks if a potential key is a K. | ||
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any | ||
* data type. | ||
* @returns a boolean value indicating whether the potentialKey is of type number or not. | ||
*/ | ||
override isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K { | ||
return !(potentialKey instanceof RedBlackTreeNode); | ||
} | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* on average (where n is the number of nodes in the tree) | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -189,5 +199,5 @@ * The `add` function adds a new node to a binary search tree and performs necessary rotations and | ||
*/ | ||
override add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V): N | undefined { | ||
override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): boolean { | ||
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value); | ||
if (newNode === undefined) return; | ||
if (newNode === undefined) return false; | ||
@@ -211,3 +221,3 @@ newNode.left = this.Sentinel; | ||
} | ||
return; | ||
return false; | ||
} | ||
@@ -229,3 +239,3 @@ } | ||
this._size++; | ||
return; | ||
return false; | ||
} | ||
@@ -235,3 +245,3 @@ | ||
this._size++; | ||
return; | ||
return false; | ||
} | ||
@@ -241,11 +251,13 @@ | ||
this._size++; | ||
return true; | ||
} | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* on average (where n is the number of nodes in the tree) | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
@@ -325,12 +337,2 @@ * | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* Space Complexity: O(1) | ||
*/ | ||
override isRealNode(node: N | undefined): node is N { | ||
if (node === this.Sentinel || node === undefined) return false; | ||
return node instanceof RedBlackTreeNode; | ||
} | ||
getNode<C extends BTNCallback<N, K>>( | ||
@@ -358,3 +360,3 @@ identifier: K, | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
@@ -364,3 +366,3 @@ */ | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
@@ -425,3 +427,3 @@ * | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
@@ -508,3 +510,3 @@ */ | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
@@ -514,3 +516,3 @@ */ | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
@@ -606,3 +608,3 @@ * | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
@@ -612,3 +614,3 @@ */ | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
@@ -615,0 +617,0 @@ * |
@@ -12,4 +12,3 @@ /** | ||
BTNCallback, | ||
BTNExemplar, | ||
BTNKeyOrNode, | ||
KeyOrNodeOrEntry, | ||
TreeMultimapNested, | ||
@@ -57,5 +56,5 @@ TreeMultimapNodeNested, | ||
implements IBinaryTree<K, V, N, TREE> { | ||
constructor(elements?: Iterable<BTNExemplar<K, V, N>>, options?: Partial<TreeMultimapOptions<K>>) { | ||
constructor(nodes?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: Partial<TreeMultimapOptions<K>>) { | ||
super([], options); | ||
if (elements) this.addMany(elements); | ||
if (nodes) this.addMany(nodes); | ||
} | ||
@@ -94,24 +93,4 @@ | ||
/** | ||
* The function checks if an exemplar is an instance of the TreeMultimapNode class. | ||
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`. | ||
* @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode | ||
* class. | ||
*/ | ||
override isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N { | ||
return exemplar instanceof TreeMultimapNode; | ||
} | ||
/** | ||
* The function "isNotNodeInstance" checks if a potential key is a K. | ||
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any | ||
* data type. | ||
* @returns a boolean value indicating whether the potentialKey is of type number or not. | ||
*/ | ||
override isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K { | ||
return !(potentialKey instanceof TreeMultimapNode); | ||
} | ||
/** | ||
* The function `exemplarToNode` converts an exemplar object into a node object. | ||
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, which means it | ||
* The function `exemplarToNode` converts an keyOrNodeOrEntry object into a node object. | ||
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, which means it | ||
* can be one of the following: | ||
@@ -125,10 +104,10 @@ * @param {V} [value] - The `value` parameter is an optional argument that represents the value | ||
*/ | ||
override exemplarToNode(exemplar: BTNExemplar<K, V, N>, value?: V, count = 1): N | undefined { | ||
override exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V, count = 1): N | undefined { | ||
let node: N | undefined; | ||
if (exemplar === undefined || exemplar === null) { | ||
if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) { | ||
return; | ||
} else if (this.isNode(exemplar)) { | ||
node = exemplar; | ||
} else if (this.isEntry(exemplar)) { | ||
const [key, value] = exemplar; | ||
} else if (this.isNode(keyOrNodeOrEntry)) { | ||
node = keyOrNodeOrEntry; | ||
} else if (this.isEntry(keyOrNodeOrEntry)) { | ||
const [key, value] = keyOrNodeOrEntry; | ||
if (key === undefined || key === null) { | ||
@@ -139,4 +118,4 @@ return; | ||
} | ||
} else if (this.isNotNodeInstance(exemplar)) { | ||
node = this.createNode(exemplar, value, count); | ||
} else if (this.isNotNodeInstance(keyOrNodeOrEntry)) { | ||
node = this.createNode(keyOrNodeOrEntry, value, count); | ||
} else { | ||
@@ -149,9 +128,30 @@ return; | ||
/** | ||
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. | ||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
* The function checks if an keyOrNodeOrEntry is an instance of the TreeMultimapNode class. | ||
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`. | ||
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the TreeMultimapNode | ||
* class. | ||
*/ | ||
override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N { | ||
return keyOrNodeOrEntry instanceof TreeMultimapNode; | ||
} | ||
/** | ||
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. | ||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
* The function "isNotNodeInstance" checks if a potential key is a K. | ||
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any | ||
* data type. | ||
* @returns a boolean value indicating whether the potentialKey is of type number or not. | ||
*/ | ||
override isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K { | ||
return !(potentialKey instanceof TreeMultimapNode); | ||
} | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size. | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -170,5 +170,5 @@ * The function overrides the add method of a binary tree node and adds a new node to the tree. | ||
*/ | ||
override add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V, count = 1): N | undefined { | ||
override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V, count = 1): boolean { | ||
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value, count); | ||
if (newNode === undefined) return; | ||
if (newNode === undefined) return false; | ||
@@ -180,13 +180,14 @@ const orgNodeCount = newNode?.count || 0; | ||
} | ||
return inserted; | ||
return true; | ||
} | ||
/** | ||
* Time Complexity: O(k log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. | ||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
* Time Complexity: O(k log n) | ||
* Space Complexity: O(1) | ||
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size. | ||
*/ | ||
/** | ||
* Time Complexity: O(k log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. | ||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
* Time Complexity: O(k log n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -199,3 +200,3 @@ * The function overrides the addMany method to add multiple keys, nodes, or entries to a data | ||
*/ | ||
override addMany(keysOrNodesOrEntries: Iterable<BTNExemplar<K, V, N>>): (N | undefined)[] { | ||
override addMany(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>>): boolean[] { | ||
return super.addMany(keysOrNodesOrEntries); | ||
@@ -205,9 +206,10 @@ } | ||
/** | ||
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments. | ||
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory. | ||
* Time Complexity: O(n log n) | ||
* Space Complexity: O(n) | ||
* logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node. linear space, as it creates an array to store the sorted nodes. | ||
*/ | ||
/** | ||
* Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node. | ||
* Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes. | ||
* Time Complexity: O(n log n) | ||
* Space Complexity: O(n) | ||
* | ||
@@ -260,9 +262,10 @@ * The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search | ||
/** | ||
* Time Complexity: O(k log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each. | ||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
* Time Complexity: O(k log n) | ||
* Space Complexity: O(1) | ||
* logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each. constant space, as it doesn't use additional data structures that scale with input size. | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (AVLTree) has logarithmic time complexity. | ||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
* Time Complexity: O(k log n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -345,7 +348,10 @@ * The `delete` function in TypeScript is used to remove a node from a binary tree, taking into | ||
/** | ||
* Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node. | ||
* Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The clear() function clears the contents of a data structure and sets the count to zero. | ||
@@ -352,0 +358,0 @@ */ |
@@ -8,3 +8,3 @@ import { BinaryTree, BinaryTreeNode } from '../data-structures'; | ||
BTNCallback, | ||
BTNExemplar | ||
KeyOrNodeOrEntry | ||
} from '../types'; | ||
@@ -22,7 +22,7 @@ | ||
add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V, count?: number): N | null | undefined; | ||
add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V, count?: number): boolean; | ||
addMany(nodes: Iterable<BTNExemplar<K, V, N>>, values?: Iterable<V | undefined>): (N | null | undefined)[]; | ||
addMany(nodes: Iterable<KeyOrNodeOrEntry<K, V, N>>, values?: Iterable<V | undefined>): boolean[]; | ||
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeleteResult<N>[]; | ||
} |
export enum BSTVariant { | ||
MIN = 'MIN', | ||
MAX = 'MAX' | ||
STANDARD = 'STANDARD', | ||
INVERSE = 'INVERSE' | ||
} | ||
@@ -57,3 +57,3 @@ | ||
export type BTNExemplar<K, V, N> = BTNEntry<K, V> | BTNKeyOrNode<K, N>; | ||
export type KeyOrNodeOrEntry<K, V, N> = BTNEntry<K, V> | BTNKeyOrNode<K, N>; | ||
@@ -60,0 +60,0 @@ export type BTNodePureKeyOrNode<K, N> = K | N; |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
35765
1990523