min-heap-typed 1.42.7 → 1.42.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +88 -23
  2. package/dist/data-structures/binary-tree/avl-tree.js +88 -23
  3. package/dist/data-structures/binary-tree/binary-tree.d.ts +180 -74
  4. package/dist/data-structures/binary-tree/binary-tree.js +388 -201
  5. package/dist/data-structures/binary-tree/bst.d.ts +121 -66
  6. package/dist/data-structures/binary-tree/bst.js +121 -67
  7. package/dist/data-structures/binary-tree/rb-tree.d.ts +72 -5
  8. package/dist/data-structures/binary-tree/rb-tree.js +95 -18
  9. package/dist/data-structures/binary-tree/tree-multimap.d.ts +82 -43
  10. package/dist/data-structures/binary-tree/tree-multimap.js +82 -43
  11. package/dist/data-structures/graph/abstract-graph.d.ts +139 -36
  12. package/dist/data-structures/graph/abstract-graph.js +147 -36
  13. package/dist/data-structures/graph/directed-graph.d.ts +126 -0
  14. package/dist/data-structures/graph/directed-graph.js +126 -0
  15. package/dist/data-structures/graph/undirected-graph.d.ts +63 -0
  16. package/dist/data-structures/graph/undirected-graph.js +63 -0
  17. package/dist/data-structures/heap/heap.d.ts +175 -12
  18. package/dist/data-structures/heap/heap.js +175 -12
  19. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +203 -0
  20. package/dist/data-structures/linked-list/doubly-linked-list.js +203 -0
  21. package/dist/data-structures/linked-list/singly-linked-list.d.ts +182 -0
  22. package/dist/data-structures/linked-list/singly-linked-list.js +182 -0
  23. package/dist/data-structures/linked-list/skip-linked-list.d.ts +64 -0
  24. package/dist/data-structures/linked-list/skip-linked-list.js +64 -0
  25. package/dist/data-structures/queue/deque.d.ts +113 -3
  26. package/dist/data-structures/queue/deque.js +113 -3
  27. package/dist/data-structures/queue/queue.d.ts +87 -0
  28. package/dist/data-structures/queue/queue.js +87 -0
  29. package/dist/data-structures/stack/stack.d.ts +42 -0
  30. package/dist/data-structures/stack/stack.js +42 -0
  31. package/dist/data-structures/trie/trie.d.ts +76 -0
  32. package/dist/data-structures/trie/trie.js +76 -1
  33. package/package.json +2 -2
  34. package/src/data-structures/binary-tree/avl-tree.ts +97 -23
  35. package/src/data-structures/binary-tree/binary-tree.ts +419 -204
  36. package/src/data-structures/binary-tree/bst.ts +130 -68
  37. package/src/data-structures/binary-tree/rb-tree.ts +106 -19
  38. package/src/data-structures/binary-tree/tree-multimap.ts +88 -44
  39. package/src/data-structures/graph/abstract-graph.ts +133 -7
  40. package/src/data-structures/graph/directed-graph.ts +145 -1
  41. package/src/data-structures/graph/undirected-graph.ts +72 -0
  42. package/src/data-structures/heap/heap.ts +201 -12
  43. package/src/data-structures/linked-list/doubly-linked-list.ts +232 -0
  44. package/src/data-structures/linked-list/singly-linked-list.ts +208 -0
  45. package/src/data-structures/linked-list/skip-linked-list.ts +74 -0
  46. package/src/data-structures/queue/deque.ts +127 -3
  47. package/src/data-structures/queue/queue.ts +99 -0
  48. package/src/data-structures/stack/stack.ts +48 -0
  49. package/src/data-structures/trie/trie.ts +87 -4
@@ -35,10 +35,9 @@ export declare class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>
35
35
  }
36
36
  export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>> extends BinaryTree<V, N> implements IBinaryTree<V, N> {
37
37
  /**
38
- * The constructor function initializes a binary search tree object with an optional comparator
39
- * function.
40
- * @param {BSTOptions} [options] - An optional object that contains configuration options for the
41
- * binary search tree.
38
+ * The constructor function initializes a binary search tree with an optional comparator function.
39
+ * @param {BSTOptions} [options] - An optional object that contains additional configuration options
40
+ * for the binary search tree.
42
41
  */
43
42
  constructor(options?: BSTOptions);
44
43
  protected _root?: N;
@@ -56,51 +55,79 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
56
55
  */
57
56
  createNode(key: BTNKey, value?: V): N;
58
57
  /**
59
- * The `add` function in a binary search tree class inserts a new node with a given key and value
60
- * into the tree.
61
- * @param {BTNKey | N | undefined} keyOrNode - The `keyOrNode` parameter can be either a
62
- * `BTNKey` (which can be a number or a string), a `BSTNode` object, or `undefined`.
63
- * @param [value] - The `value` parameter is the value to be assigned to the new node being added to the
64
- * binary search tree.
65
- * @returns the inserted node (N) if it was successfully added to the binary search tree. If the node
66
- * was not added or if the parameters were invalid, it returns undefined or undefined.
58
+ * Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
59
+ * Space Complexity: O(1) - Constant space is used.
60
+ */
61
+ /**
62
+ * Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
63
+ * Space Complexity: O(1) - Constant space is used.
64
+ *
65
+ * The `add` function adds a new node to a binary search tree based on the provided key and value.
66
+ * @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
67
+ * following types:
68
+ * @param {V} [value] - The `value` parameter is an optional value that can be associated with the
69
+ * key or node being added to the binary search tree.
70
+ * @returns The method `add` returns a node (`N`) that was inserted into the binary search tree. If
71
+ * no node was inserted, it returns `undefined`.
67
72
  */
68
73
  add(keyOrNode: BTNKey | N | null | undefined, value?: V): N | undefined;
69
74
  /**
70
- * The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
71
- * maintaining balance.
72
- * @param {[BTNKey | N, V][]} keysOrNodes - The `arr` parameter in the `addMany` function
73
- * represents an array of keys or nodes that need to be added to the binary search tree. It can be an
74
- * array of `BTNKey` or `N` (which represents the node type in the binary search tree) or
75
- * `undefined
76
- * @param {V[]} data - The values of tree nodes
77
- * @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
78
- * @param iterationType - The `iterationType` parameter determines the type of iteration to be used.
79
- * It can have two possible values:
80
- * @returns The `addMany` function returns an array of `N`, `undefined`, or `undefined` values.
75
+ * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
76
+ * Space Complexity: O(n) - Additional space is required for the sorted array.
77
+ */
78
+ /**
79
+ * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
80
+ * Space Complexity: O(n) - Additional space is required for the sorted array.
81
+ *
82
+ * The `addMany` function is used to efficiently add multiple keys or nodes with corresponding data
83
+ * to a binary search tree.
84
+ * @param {(BTNKey | N | undefined)[]} keysOrNodes - An array of keys or nodes to be added to the
85
+ * binary search tree. Each element can be of type `BTNKey` (binary tree node key), `N` (binary tree
86
+ * node), or `undefined`.
87
+ * @param {(V | undefined)[]} [data] - An optional array of values to associate with the keys or
88
+ * nodes being added. If provided, the length of the `data` array must be the same as the length of
89
+ * the `keysOrNodes` array.
90
+ * @param [isBalanceAdd=true] - A boolean flag indicating whether the tree should be balanced after
91
+ * adding the nodes. The default value is `true`.
92
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
93
+ * type of iteration to use when adding multiple keys or nodes to the binary search tree. It has a
94
+ * default value of `this.iterationType`, which means it will use the iteration type specified in the
95
+ * current instance of the binary search tree
96
+ * @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
81
97
  */
82
98
  addMany(keysOrNodes: (BTNKey | N | undefined)[], data?: (V | undefined)[], isBalanceAdd?: boolean, iterationType?: IterationType): (N | undefined)[];
83
99
  /**
84
- * The function `lastKey` returns the key of the rightmost node if the comparison result is less
85
- * than, the key of the leftmost node if the comparison result is greater than, and the key of the
86
- * rightmost node otherwise.
87
- * @param {N | undefined} beginRoot - The `beginRoot` parameter is the starting point for finding the last
88
- * key in a binary tree. It represents the root node of the subtree from which the search for the
89
- * last key should begin. If no specific `beginRoot` is provided, the search will start from the root
90
- * of the entire binary
100
+ * Time Complexity: O(log n) - Average case for a balanced tree.
101
+ * Space Complexity: O(1) - Constant space is used.
102
+ */
103
+ /**
104
+ * Time Complexity: O(log n) - Average case for a balanced tree.
105
+ * Space Complexity: O(1) - Constant space is used.
106
+ *
107
+ * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
108
+ * leftmost node if the comparison result is greater than.
109
+ * @param {BTNKey | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
110
+ * type `BTNKey`, `N`, or `undefined`. It represents the starting point for finding the last key in
111
+ * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
91
112
  * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
92
- * be performed when finding the last key. It determines whether the iteration should be performed in
93
- * pre-order, in-order, or post-order.
113
+ * be performed. It can have one of the following values:
94
114
  * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
95
115
  * the key of the leftmost node if the comparison result is greater than, and the key of the
96
116
  * rightmost node otherwise. If no node is found, it returns 0.
97
117
  */
98
118
  lastKey(beginRoot?: BTNKey | N | undefined, iterationType?: IterationType): BTNKey;
99
119
  /**
120
+ * Time Complexity: O(log n) - Average case for a balanced tree.
121
+ * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
122
+ */
123
+ /**
124
+ * Time Complexity: O(log n) - Average case for a balanced tree.
125
+ * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
126
+ *
100
127
  * The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
101
128
  * either recursive or iterative methods.
102
129
  * @param {BTNKey} key - The `key` parameter is the key value that we are searching for in the tree.
103
- * It is used to find the node with the matching key value.
130
+ * It is used to identify the node that we want to retrieve.
104
131
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
105
132
  * type of iteration to use when searching for a node in the binary tree. It can have two possible
106
133
  * values:
@@ -119,43 +146,57 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
119
146
  */
120
147
  ensureNotKey(key: BTNKey | N | undefined, iterationType?: IterationType): N | undefined;
121
148
  /**
122
- * The function `getNodes` retrieves nodes from a binary tree based on a given node property or key,
123
- * using either recursive or iterative traversal.
124
- * @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter represents the property
125
- * of the binary tree node that you want to search for. It can be either a `BTNKey` or a
126
- * generic type `N`.
127
- * @param callback - The `callback` parameter is a function that takes a node as input and returns a
128
- * value. This value is compared with the `nodeProperty` parameter to determine if the node should be
129
- * included in the result. The default value for `callback` is `this._defaultOneParamCallback`, which is
130
- * a
131
- * @param [onlyOne=false] - A boolean value indicating whether to stop the traversal after finding
132
- * the first node that matches the nodeProperty. If set to true, the function will return an array
133
- * containing only that node. If set to false (default), the function will continue the traversal and
134
- * return an array containing all nodes that match the node
135
- * @param {N | undefined} beginRoot - The `beginRoot` parameter is the starting node for the traversal. It
136
- * specifies the root node of the binary tree from which the traversal should begin. If `beginRoot`
137
- * is `undefined`, an empty array will be returned.
138
- * @param iterationType - The `iterationType` parameter determines the type of iteration used to
139
- * traverse the binary tree. It can have one of the following values:
140
- * @returns an array of nodes (N[]).
149
+ * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
150
+ * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
151
+ */
152
+ /**
153
+ * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
154
+ * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
155
+ *
156
+ * The function `getNodes` returns an array of nodes that match a given identifier, using either a
157
+ * recursive or iterative approach.
158
+ * @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
159
+ * want to search for in the nodes of the binary tree. It can be of any type that is returned by the
160
+ * callback function `C`.
161
+ * @param {C} callback - The `callback` parameter is a function that takes a node of type `N` as its
162
+ * argument and returns a value of type `ReturnType<C>`. The `C` type parameter represents a callback
163
+ * function type that extends the `BTNCallback<N>` type. The `BTNCallback<N>` type is
164
+ * @param [onlyOne=false] - A boolean flag indicating whether to stop searching after finding the
165
+ * first node that matches the identifier. If set to true, the function will return an array
166
+ * containing only the first matching node. If set to false (default), the function will continue
167
+ * searching for all nodes that match the identifier and return an array containing
168
+ * @param {BTNKey | N | undefined} beginRoot - The `beginRoot` parameter represents the starting node
169
+ * for the traversal. It can be either a key value or a node object. If it is undefined, the
170
+ * traversal will start from the root of the tree.
171
+ * @param iterationType - The `iterationType` parameter determines the type of iteration to be
172
+ * performed on the binary tree. It can have two possible values:
173
+ * @returns The method returns an array of nodes (`N[]`).
141
174
  */
142
175
  getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: BTNKey | N | undefined, iterationType?: IterationType): N[];
143
176
  /**
144
- * The `lesserOrGreaterTraverse` function traverses a binary tree and applies a callback function to
145
- * nodes that have a key value lesser or greater than a target key value.
146
- * @param callback - The `callback` parameter is a function that will be called for each node that
147
- * meets the condition specified by the `lesserOrGreater` parameter. It takes a node as an argument
148
- * and returns a value.
177
+ * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
178
+ * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
179
+ */
180
+ /**
181
+ * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
182
+ * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
183
+ *
184
+ * The `lesserOrGreaterTraverse` function traverses a binary tree and returns an array of nodes that
185
+ * are either lesser or greater than a target node, depending on the specified comparison type.
186
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node
187
+ * that satisfies the condition specified by the `lesserOrGreater` parameter. It takes a single
188
+ * parameter of type `N` (the node type) and returns a value of any type.
149
189
  * @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
150
- * traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It can take one
151
- * of the following values:
152
- * @param {BTNKey | N | undefined} targetNode - The `targetNode` parameter in the
153
- * `lesserOrGreaterTraverse` function is used to specify the node from which the traversal should
154
- * start. It can be either a reference to a specific node (`N`), the key of a node
155
- * (`BTNKey`), or `undefined` to
156
- * @param iterationType - The `iterationType` parameter determines whether the traversal should be
157
- * done recursively or iteratively. It can have two possible values:
158
- * @returns The function `lesserOrGreaterTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
190
+ * traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It is of type
191
+ * `CP`, which is a custom type representing the comparison operator. The possible values for
192
+ * `lesserOrGreater` are
193
+ * @param {BTNKey | N | undefined} targetNode - The `targetNode` parameter represents the node in the
194
+ * binary tree that you want to traverse from. It can be specified either by its key, by the node
195
+ * object itself, or it can be left undefined to start the traversal from the root of the tree.
196
+ * @param iterationType - The `iterationType` parameter determines the type of traversal to be
197
+ * performed on the binary tree. It can have two possible values:
198
+ * @returns The function `lesserOrGreaterTraverse` returns an array of values of type
199
+ * `ReturnType<C>`, which is the return type of the callback function passed as an argument.
159
200
  */
160
201
  lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BTNKey | N | undefined, iterationType?: IterationType): ReturnType<C>[];
161
202
  /**
@@ -168,6 +209,13 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
168
209
  * AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
169
210
  */
170
211
  /**
212
+ * Time Complexity: O(n) - Building a balanced tree from a sorted array.
213
+ * Space Complexity: O(n) - Additional space is required for the sorted array.
214
+ */
215
+ /**
216
+ * Time Complexity: O(n) - Building a balanced tree from a sorted array.
217
+ * Space Complexity: O(n) - Additional space is required for the sorted array.
218
+ *
171
219
  * The `perfectlyBalance` function balances a binary search tree by adding nodes in a way that
172
220
  * ensures the tree is perfectly balanced.
173
221
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
@@ -177,6 +225,13 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
177
225
  */
178
226
  perfectlyBalance(iterationType?: IterationType): boolean;
179
227
  /**
228
+ * Time Complexity: O(n) - Visiting each node once.
229
+ * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
230
+ */
231
+ /**
232
+ * Time Complexity: O(n) - Visiting each node once.
233
+ * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
234
+ *
180
235
  * The function checks if a binary tree is AVL balanced using either recursive or iterative approach.
181
236
  * @param iterationType - The `iterationType` parameter is used to determine the method of iteration
182
237
  * to check if the AVL tree is balanced. It can have two possible values:
@@ -47,10 +47,9 @@ class BSTNode extends binary_tree_1.BinaryTreeNode {
47
47
  exports.BSTNode = BSTNode;
48
48
  class BST extends binary_tree_1.BinaryTree {
49
49
  /**
50
- * The constructor function initializes a binary search tree object with an optional comparator
51
- * function.
52
- * @param {BSTOptions} [options] - An optional object that contains configuration options for the
53
- * binary search tree.
50
+ * The constructor function initializes a binary search tree with an optional comparator function.
51
+ * @param {BSTOptions} [options] - An optional object that contains additional configuration options
52
+ * for the binary search tree.
54
53
  */
55
54
  constructor(options) {
56
55
  super(options);
@@ -81,14 +80,20 @@ class BST extends binary_tree_1.BinaryTree {
81
80
  return new BSTNode(key, value);
82
81
  }
83
82
  /**
84
- * The `add` function in a binary search tree class inserts a new node with a given key and value
85
- * into the tree.
86
- * @param {BTNKey | N | undefined} keyOrNode - The `keyOrNode` parameter can be either a
87
- * `BTNKey` (which can be a number or a string), a `BSTNode` object, or `undefined`.
88
- * @param [value] - The `value` parameter is the value to be assigned to the new node being added to the
89
- * binary search tree.
90
- * @returns the inserted node (N) if it was successfully added to the binary search tree. If the node
91
- * was not added or if the parameters were invalid, it returns undefined or undefined.
83
+ * Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
84
+ * Space Complexity: O(1) - Constant space is used.
85
+ */
86
+ /**
87
+ * Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
88
+ * Space Complexity: O(1) - Constant space is used.
89
+ *
90
+ * The `add` function adds a new node to a binary search tree based on the provided key and value.
91
+ * @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
92
+ * following types:
93
+ * @param {V} [value] - The `value` parameter is an optional value that can be associated with the
94
+ * key or node being added to the binary search tree.
95
+ * @returns The method `add` returns a node (`N`) that was inserted into the binary search tree. If
96
+ * no node was inserted, it returns `undefined`.
92
97
  */
93
98
  add(keyOrNode, value) {
94
99
  if (keyOrNode === null)
@@ -168,17 +173,28 @@ class BST extends binary_tree_1.BinaryTree {
168
173
  return inserted;
169
174
  }
170
175
  /**
171
- * The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
172
- * maintaining balance.
173
- * @param {[BTNKey | N, V][]} keysOrNodes - The `arr` parameter in the `addMany` function
174
- * represents an array of keys or nodes that need to be added to the binary search tree. It can be an
175
- * array of `BTNKey` or `N` (which represents the node type in the binary search tree) or
176
- * `undefined
177
- * @param {V[]} data - The values of tree nodes
178
- * @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
179
- * @param iterationType - The `iterationType` parameter determines the type of iteration to be used.
180
- * It can have two possible values:
181
- * @returns The `addMany` function returns an array of `N`, `undefined`, or `undefined` values.
176
+ * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
177
+ * Space Complexity: O(n) - Additional space is required for the sorted array.
178
+ */
179
+ /**
180
+ * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
181
+ * Space Complexity: O(n) - Additional space is required for the sorted array.
182
+ *
183
+ * The `addMany` function is used to efficiently add multiple keys or nodes with corresponding data
184
+ * to a binary search tree.
185
+ * @param {(BTNKey | N | undefined)[]} keysOrNodes - An array of keys or nodes to be added to the
186
+ * binary search tree. Each element can be of type `BTNKey` (binary tree node key), `N` (binary tree
187
+ * node), or `undefined`.
188
+ * @param {(V | undefined)[]} [data] - An optional array of values to associate with the keys or
189
+ * nodes being added. If provided, the length of the `data` array must be the same as the length of
190
+ * the `keysOrNodes` array.
191
+ * @param [isBalanceAdd=true] - A boolean flag indicating whether the tree should be balanced after
192
+ * adding the nodes. The default value is `true`.
193
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
194
+ * type of iteration to use when adding multiple keys or nodes to the binary search tree. It has a
195
+ * default value of `this.iterationType`, which means it will use the iteration type specified in the
196
+ * current instance of the binary search tree
197
+ * @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
182
198
  */
183
199
  addMany(keysOrNodes, data, isBalanceAdd = true, iterationType = this.iterationType) {
184
200
  // TODO this addMany function is inefficient, it should be optimized
@@ -250,16 +266,20 @@ class BST extends binary_tree_1.BinaryTree {
250
266
  return inserted;
251
267
  }
252
268
  /**
253
- * The function `lastKey` returns the key of the rightmost node if the comparison result is less
254
- * than, the key of the leftmost node if the comparison result is greater than, and the key of the
255
- * rightmost node otherwise.
256
- * @param {N | undefined} beginRoot - The `beginRoot` parameter is the starting point for finding the last
257
- * key in a binary tree. It represents the root node of the subtree from which the search for the
258
- * last key should begin. If no specific `beginRoot` is provided, the search will start from the root
259
- * of the entire binary
269
+ * Time Complexity: O(log n) - Average case for a balanced tree.
270
+ * Space Complexity: O(1) - Constant space is used.
271
+ */
272
+ /**
273
+ * Time Complexity: O(log n) - Average case for a balanced tree.
274
+ * Space Complexity: O(1) - Constant space is used.
275
+ *
276
+ * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
277
+ * leftmost node if the comparison result is greater than.
278
+ * @param {BTNKey | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
279
+ * type `BTNKey`, `N`, or `undefined`. It represents the starting point for finding the last key in
280
+ * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
260
281
  * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
261
- * be performed when finding the last key. It determines whether the iteration should be performed in
262
- * pre-order, in-order, or post-order.
282
+ * be performed. It can have one of the following values:
263
283
  * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
264
284
  * the key of the leftmost node if the comparison result is greater than, and the key of the
265
285
  * rightmost node otherwise. If no node is found, it returns 0.
@@ -274,10 +294,17 @@ class BST extends binary_tree_1.BinaryTree {
274
294
  return (_f = (_e = this.getRightMost(beginRoot, iterationType)) === null || _e === void 0 ? void 0 : _e.key) !== null && _f !== void 0 ? _f : 0;
275
295
  }
276
296
  /**
297
+ * Time Complexity: O(log n) - Average case for a balanced tree.
298
+ * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
299
+ */
300
+ /**
301
+ * Time Complexity: O(log n) - Average case for a balanced tree.
302
+ * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
303
+ *
277
304
  * The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
278
305
  * either recursive or iterative methods.
279
306
  * @param {BTNKey} key - The `key` parameter is the key value that we are searching for in the tree.
280
- * It is used to find the node with the matching key value.
307
+ * It is used to identify the node that we want to retrieve.
281
308
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
282
309
  * type of iteration to use when searching for a node in the binary tree. It can have two possible
283
310
  * values:
@@ -328,25 +355,31 @@ class BST extends binary_tree_1.BinaryTree {
328
355
  return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
329
356
  }
330
357
  /**
331
- * The function `getNodes` retrieves nodes from a binary tree based on a given node property or key,
332
- * using either recursive or iterative traversal.
333
- * @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter represents the property
334
- * of the binary tree node that you want to search for. It can be either a `BTNKey` or a
335
- * generic type `N`.
336
- * @param callback - The `callback` parameter is a function that takes a node as input and returns a
337
- * value. This value is compared with the `nodeProperty` parameter to determine if the node should be
338
- * included in the result. The default value for `callback` is `this._defaultOneParamCallback`, which is
339
- * a
340
- * @param [onlyOne=false] - A boolean value indicating whether to stop the traversal after finding
341
- * the first node that matches the nodeProperty. If set to true, the function will return an array
342
- * containing only that node. If set to false (default), the function will continue the traversal and
343
- * return an array containing all nodes that match the node
344
- * @param {N | undefined} beginRoot - The `beginRoot` parameter is the starting node for the traversal. It
345
- * specifies the root node of the binary tree from which the traversal should begin. If `beginRoot`
346
- * is `undefined`, an empty array will be returned.
347
- * @param iterationType - The `iterationType` parameter determines the type of iteration used to
348
- * traverse the binary tree. It can have one of the following values:
349
- * @returns an array of nodes (N[]).
358
+ * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
359
+ * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
360
+ */
361
+ /**
362
+ * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
363
+ * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
364
+ *
365
+ * The function `getNodes` returns an array of nodes that match a given identifier, using either a
366
+ * recursive or iterative approach.
367
+ * @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
368
+ * want to search for in the nodes of the binary tree. It can be of any type that is returned by the
369
+ * callback function `C`.
370
+ * @param {C} callback - The `callback` parameter is a function that takes a node of type `N` as its
371
+ * argument and returns a value of type `ReturnType<C>`. The `C` type parameter represents a callback
372
+ * function type that extends the `BTNCallback<N>` type. The `BTNCallback<N>` type is
373
+ * @param [onlyOne=false] - A boolean flag indicating whether to stop searching after finding the
374
+ * first node that matches the identifier. If set to true, the function will return an array
375
+ * containing only the first matching node. If set to false (default), the function will continue
376
+ * searching for all nodes that match the identifier and return an array containing
377
+ * @param {BTNKey | N | undefined} beginRoot - The `beginRoot` parameter represents the starting node
378
+ * for the traversal. It can be either a key value or a node object. If it is undefined, the
379
+ * traversal will start from the root of the tree.
380
+ * @param iterationType - The `iterationType` parameter determines the type of iteration to be
381
+ * performed on the binary tree. It can have two possible values:
382
+ * @returns The method returns an array of nodes (`N[]`).
350
383
  */
351
384
  getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
352
385
  beginRoot = this.ensureNotKey(beginRoot);
@@ -404,23 +437,30 @@ class BST extends binary_tree_1.BinaryTree {
404
437
  }
405
438
  return ans;
406
439
  }
407
- // --- start additional functions
408
440
  /**
409
- * The `lesserOrGreaterTraverse` function traverses a binary tree and applies a callback function to
410
- * nodes that have a key value lesser or greater than a target key value.
411
- * @param callback - The `callback` parameter is a function that will be called for each node that
412
- * meets the condition specified by the `lesserOrGreater` parameter. It takes a node as an argument
413
- * and returns a value.
441
+ * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
442
+ * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
443
+ */
444
+ /**
445
+ * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
446
+ * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
447
+ *
448
+ * The `lesserOrGreaterTraverse` function traverses a binary tree and returns an array of nodes that
449
+ * are either lesser or greater than a target node, depending on the specified comparison type.
450
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node
451
+ * that satisfies the condition specified by the `lesserOrGreater` parameter. It takes a single
452
+ * parameter of type `N` (the node type) and returns a value of any type.
414
453
  * @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
415
- * traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It can take one
416
- * of the following values:
417
- * @param {BTNKey | N | undefined} targetNode - The `targetNode` parameter in the
418
- * `lesserOrGreaterTraverse` function is used to specify the node from which the traversal should
419
- * start. It can be either a reference to a specific node (`N`), the key of a node
420
- * (`BTNKey`), or `undefined` to
421
- * @param iterationType - The `iterationType` parameter determines whether the traversal should be
422
- * done recursively or iteratively. It can have two possible values:
423
- * @returns The function `lesserOrGreaterTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
454
+ * traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It is of type
455
+ * `CP`, which is a custom type representing the comparison operator. The possible values for
456
+ * `lesserOrGreater` are
457
+ * @param {BTNKey | N | undefined} targetNode - The `targetNode` parameter represents the node in the
458
+ * binary tree that you want to traverse from. It can be specified either by its key, by the node
459
+ * object itself, or it can be left undefined to start the traversal from the root of the tree.
460
+ * @param iterationType - The `iterationType` parameter determines the type of traversal to be
461
+ * performed on the binary tree. It can have two possible values:
462
+ * @returns The function `lesserOrGreaterTraverse` returns an array of values of type
463
+ * `ReturnType<C>`, which is the return type of the callback function passed as an argument.
424
464
  */
425
465
  lesserOrGreaterTraverse(callback = this._defaultOneParamCallback, lesserOrGreater = types_1.CP.lt, targetNode = this.root, iterationType = this.iterationType) {
426
466
  targetNode = this.ensureNotKey(targetNode);
@@ -472,6 +512,13 @@ class BST extends binary_tree_1.BinaryTree {
472
512
  * AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
473
513
  */
474
514
  /**
515
+ * Time Complexity: O(n) - Building a balanced tree from a sorted array.
516
+ * Space Complexity: O(n) - Additional space is required for the sorted array.
517
+ */
518
+ /**
519
+ * Time Complexity: O(n) - Building a balanced tree from a sorted array.
520
+ * Space Complexity: O(n) - Additional space is required for the sorted array.
521
+ *
475
522
  * The `perfectlyBalance` function balances a binary search tree by adding nodes in a way that
476
523
  * ensures the tree is perfectly balanced.
477
524
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
@@ -517,6 +564,13 @@ class BST extends binary_tree_1.BinaryTree {
517
564
  }
518
565
  }
519
566
  /**
567
+ * Time Complexity: O(n) - Visiting each node once.
568
+ * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
569
+ */
570
+ /**
571
+ * Time Complexity: O(n) - Visiting each node once.
572
+ * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
573
+ *
520
574
  * The function checks if a binary tree is AVL balanced using either recursive or iterative approach.
521
575
  * @param iterationType - The `iterationType` parameter is used to determine the method of iteration
522
576
  * to check if the AVL tree is balanced. It can have two possible values: