data-structure-typed 1.42.8 → 1.43.0

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 (108) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +18 -18
  3. package/benchmark/report.html +12 -12
  4. package/benchmark/report.json +106 -106
  5. package/dist/cjs/src/data-structures/binary-tree/avl-tree.d.ts +88 -23
  6. package/dist/cjs/src/data-structures/binary-tree/avl-tree.js +88 -23
  7. package/dist/cjs/src/data-structures/binary-tree/avl-tree.js.map +1 -1
  8. package/dist/cjs/src/data-structures/binary-tree/binary-tree.d.ts +180 -74
  9. package/dist/cjs/src/data-structures/binary-tree/binary-tree.js +415 -236
  10. package/dist/cjs/src/data-structures/binary-tree/binary-tree.js.map +1 -1
  11. package/dist/cjs/src/data-structures/binary-tree/bst.d.ts +121 -66
  12. package/dist/cjs/src/data-structures/binary-tree/bst.js +121 -67
  13. package/dist/cjs/src/data-structures/binary-tree/bst.js.map +1 -1
  14. package/dist/cjs/src/data-structures/binary-tree/rb-tree.d.ts +72 -5
  15. package/dist/cjs/src/data-structures/binary-tree/rb-tree.js +95 -18
  16. package/dist/cjs/src/data-structures/binary-tree/rb-tree.js.map +1 -1
  17. package/dist/cjs/src/data-structures/binary-tree/tree-multimap.d.ts +82 -43
  18. package/dist/cjs/src/data-structures/binary-tree/tree-multimap.js +82 -43
  19. package/dist/cjs/src/data-structures/binary-tree/tree-multimap.js.map +1 -1
  20. package/dist/cjs/src/data-structures/graph/abstract-graph.d.ts +139 -36
  21. package/dist/cjs/src/data-structures/graph/abstract-graph.js +147 -36
  22. package/dist/cjs/src/data-structures/graph/abstract-graph.js.map +1 -1
  23. package/dist/cjs/src/data-structures/graph/directed-graph.d.ts +126 -0
  24. package/dist/cjs/src/data-structures/graph/directed-graph.js +126 -0
  25. package/dist/cjs/src/data-structures/graph/directed-graph.js.map +1 -1
  26. package/dist/cjs/src/data-structures/graph/undirected-graph.d.ts +63 -0
  27. package/dist/cjs/src/data-structures/graph/undirected-graph.js +63 -0
  28. package/dist/cjs/src/data-structures/graph/undirected-graph.js.map +1 -1
  29. package/dist/cjs/src/data-structures/heap/heap.d.ts +175 -12
  30. package/dist/cjs/src/data-structures/heap/heap.js +175 -12
  31. package/dist/cjs/src/data-structures/heap/heap.js.map +1 -1
  32. package/dist/cjs/src/data-structures/linked-list/doubly-linked-list.d.ts +203 -0
  33. package/dist/cjs/src/data-structures/linked-list/doubly-linked-list.js +203 -0
  34. package/dist/cjs/src/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  35. package/dist/cjs/src/data-structures/linked-list/singly-linked-list.d.ts +182 -0
  36. package/dist/cjs/src/data-structures/linked-list/singly-linked-list.js +182 -0
  37. package/dist/cjs/src/data-structures/linked-list/singly-linked-list.js.map +1 -1
  38. package/dist/cjs/src/data-structures/linked-list/skip-linked-list.d.ts +64 -0
  39. package/dist/cjs/src/data-structures/linked-list/skip-linked-list.js +64 -0
  40. package/dist/cjs/src/data-structures/linked-list/skip-linked-list.js.map +1 -1
  41. package/dist/cjs/src/data-structures/queue/deque.d.ts +113 -3
  42. package/dist/cjs/src/data-structures/queue/deque.js +113 -3
  43. package/dist/cjs/src/data-structures/queue/deque.js.map +1 -1
  44. package/dist/cjs/src/data-structures/queue/queue.d.ts +87 -0
  45. package/dist/cjs/src/data-structures/queue/queue.js +87 -0
  46. package/dist/cjs/src/data-structures/queue/queue.js.map +1 -1
  47. package/dist/cjs/src/data-structures/stack/stack.d.ts +42 -0
  48. package/dist/cjs/src/data-structures/stack/stack.js +42 -0
  49. package/dist/cjs/src/data-structures/stack/stack.js.map +1 -1
  50. package/dist/cjs/src/data-structures/trie/trie.d.ts +76 -0
  51. package/dist/cjs/src/data-structures/trie/trie.js +76 -1
  52. package/dist/cjs/src/data-structures/trie/trie.js.map +1 -1
  53. package/dist/mjs/src/data-structures/binary-tree/avl-tree.d.ts +88 -23
  54. package/dist/mjs/src/data-structures/binary-tree/avl-tree.js +88 -23
  55. package/dist/mjs/src/data-structures/binary-tree/binary-tree.d.ts +180 -74
  56. package/dist/mjs/src/data-structures/binary-tree/binary-tree.js +415 -236
  57. package/dist/mjs/src/data-structures/binary-tree/bst.d.ts +121 -66
  58. package/dist/mjs/src/data-structures/binary-tree/bst.js +121 -67
  59. package/dist/mjs/src/data-structures/binary-tree/rb-tree.d.ts +72 -5
  60. package/dist/mjs/src/data-structures/binary-tree/rb-tree.js +95 -18
  61. package/dist/mjs/src/data-structures/binary-tree/tree-multimap.d.ts +82 -43
  62. package/dist/mjs/src/data-structures/binary-tree/tree-multimap.js +82 -43
  63. package/dist/mjs/src/data-structures/graph/abstract-graph.d.ts +139 -36
  64. package/dist/mjs/src/data-structures/graph/abstract-graph.js +147 -36
  65. package/dist/mjs/src/data-structures/graph/directed-graph.d.ts +126 -0
  66. package/dist/mjs/src/data-structures/graph/directed-graph.js +126 -0
  67. package/dist/mjs/src/data-structures/graph/undirected-graph.d.ts +63 -0
  68. package/dist/mjs/src/data-structures/graph/undirected-graph.js +63 -0
  69. package/dist/mjs/src/data-structures/heap/heap.d.ts +175 -12
  70. package/dist/mjs/src/data-structures/heap/heap.js +175 -12
  71. package/dist/mjs/src/data-structures/linked-list/doubly-linked-list.d.ts +203 -0
  72. package/dist/mjs/src/data-structures/linked-list/doubly-linked-list.js +203 -0
  73. package/dist/mjs/src/data-structures/linked-list/singly-linked-list.d.ts +182 -0
  74. package/dist/mjs/src/data-structures/linked-list/singly-linked-list.js +182 -0
  75. package/dist/mjs/src/data-structures/linked-list/skip-linked-list.d.ts +64 -0
  76. package/dist/mjs/src/data-structures/linked-list/skip-linked-list.js +64 -0
  77. package/dist/mjs/src/data-structures/queue/deque.d.ts +113 -3
  78. package/dist/mjs/src/data-structures/queue/deque.js +113 -3
  79. package/dist/mjs/src/data-structures/queue/queue.d.ts +87 -0
  80. package/dist/mjs/src/data-structures/queue/queue.js +87 -0
  81. package/dist/mjs/src/data-structures/stack/stack.d.ts +42 -0
  82. package/dist/mjs/src/data-structures/stack/stack.js +42 -0
  83. package/dist/mjs/src/data-structures/trie/trie.d.ts +76 -0
  84. package/dist/mjs/src/data-structures/trie/trie.js +76 -1
  85. package/dist/umd/data-structure-typed.min.js +1 -1
  86. package/dist/umd/data-structure-typed.min.js.map +1 -1
  87. package/package.json +1 -1
  88. package/src/data-structures/binary-tree/avl-tree.ts +97 -23
  89. package/src/data-structures/binary-tree/binary-tree.ts +465 -256
  90. package/src/data-structures/binary-tree/bst.ts +130 -68
  91. package/src/data-structures/binary-tree/rb-tree.ts +106 -19
  92. package/src/data-structures/binary-tree/tree-multimap.ts +88 -44
  93. package/src/data-structures/graph/abstract-graph.ts +133 -7
  94. package/src/data-structures/graph/directed-graph.ts +145 -1
  95. package/src/data-structures/graph/undirected-graph.ts +72 -0
  96. package/src/data-structures/heap/heap.ts +201 -12
  97. package/src/data-structures/linked-list/doubly-linked-list.ts +232 -0
  98. package/src/data-structures/linked-list/singly-linked-list.ts +208 -0
  99. package/src/data-structures/linked-list/skip-linked-list.ts +74 -0
  100. package/src/data-structures/queue/deque.ts +127 -3
  101. package/src/data-structures/queue/queue.ts +99 -0
  102. package/src/data-structures/stack/stack.ts +48 -0
  103. package/src/data-structures/trie/trie.ts +87 -4
  104. package/test/integration/index.html +24 -2
  105. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +2 -1
  106. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +73 -5
  107. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +0 -1
  108. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +0 -1
@@ -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:
@@ -50,10 +50,9 @@ class BSTNode extends binary_tree_1.BinaryTreeNode {
50
50
  exports.BSTNode = BSTNode;
51
51
  class BST extends binary_tree_1.BinaryTree {
52
52
  /**
53
- * The constructor function initializes a binary search tree object with an optional comparator
54
- * function.
55
- * @param {BSTOptions} [options] - An optional object that contains configuration options for the
56
- * binary search tree.
53
+ * The constructor function initializes a binary search tree with an optional comparator function.
54
+ * @param {BSTOptions} [options] - An optional object that contains additional configuration options
55
+ * for the binary search tree.
57
56
  */
58
57
  constructor(options) {
59
58
  super(options);
@@ -84,14 +83,20 @@ class BST extends binary_tree_1.BinaryTree {
84
83
  return new BSTNode(key, value);
85
84
  }
86
85
  /**
87
- * The `add` function in a binary search tree class inserts a new node with a given key and value
88
- * into the tree.
89
- * @param {BTNKey | N | undefined} keyOrNode - The `keyOrNode` parameter can be either a
90
- * `BTNKey` (which can be a number or a string), a `BSTNode` object, or `undefined`.
91
- * @param [value] - The `value` parameter is the value to be assigned to the new node being added to the
92
- * binary search tree.
93
- * @returns the inserted node (N) if it was successfully added to the binary search tree. If the node
94
- * was not added or if the parameters were invalid, it returns undefined or undefined.
86
+ * Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
87
+ * Space Complexity: O(1) - Constant space is used.
88
+ */
89
+ /**
90
+ * Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
91
+ * Space Complexity: O(1) - Constant space is used.
92
+ *
93
+ * The `add` function adds a new node to a binary search tree based on the provided key and value.
94
+ * @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
95
+ * following types:
96
+ * @param {V} [value] - The `value` parameter is an optional value that can be associated with the
97
+ * key or node being added to the binary search tree.
98
+ * @returns The method `add` returns a node (`N`) that was inserted into the binary search tree. If
99
+ * no node was inserted, it returns `undefined`.
95
100
  */
96
101
  add(keyOrNode, value) {
97
102
  if (keyOrNode === null)
@@ -171,17 +176,28 @@ class BST extends binary_tree_1.BinaryTree {
171
176
  return inserted;
172
177
  }
173
178
  /**
174
- * The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
175
- * maintaining balance.
176
- * @param {[BTNKey | N, V][]} keysOrNodes - The `arr` parameter in the `addMany` function
177
- * represents an array of keys or nodes that need to be added to the binary search tree. It can be an
178
- * array of `BTNKey` or `N` (which represents the node type in the binary search tree) or
179
- * `undefined
180
- * @param {V[]} data - The values of tree nodes
181
- * @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
182
- * @param iterationType - The `iterationType` parameter determines the type of iteration to be used.
183
- * It can have two possible values:
184
- * @returns The `addMany` function returns an array of `N`, `undefined`, or `undefined` values.
179
+ * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
180
+ * Space Complexity: O(n) - Additional space is required for the sorted array.
181
+ */
182
+ /**
183
+ * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
184
+ * Space Complexity: O(n) - Additional space is required for the sorted array.
185
+ *
186
+ * The `addMany` function is used to efficiently add multiple keys or nodes with corresponding data
187
+ * to a binary search tree.
188
+ * @param {(BTNKey | N | undefined)[]} keysOrNodes - An array of keys or nodes to be added to the
189
+ * binary search tree. Each element can be of type `BTNKey` (binary tree node key), `N` (binary tree
190
+ * node), or `undefined`.
191
+ * @param {(V | undefined)[]} [data] - An optional array of values to associate with the keys or
192
+ * nodes being added. If provided, the length of the `data` array must be the same as the length of
193
+ * the `keysOrNodes` array.
194
+ * @param [isBalanceAdd=true] - A boolean flag indicating whether the tree should be balanced after
195
+ * adding the nodes. The default value is `true`.
196
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
197
+ * type of iteration to use when adding multiple keys or nodes to the binary search tree. It has a
198
+ * default value of `this.iterationType`, which means it will use the iteration type specified in the
199
+ * current instance of the binary search tree
200
+ * @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
185
201
  */
186
202
  addMany(keysOrNodes, data, isBalanceAdd = true, iterationType = this.iterationType) {
187
203
  // TODO this addMany function is inefficient, it should be optimized
@@ -253,16 +269,20 @@ class BST extends binary_tree_1.BinaryTree {
253
269
  return inserted;
254
270
  }
255
271
  /**
256
- * The function `lastKey` returns the key of the rightmost node if the comparison result is less
257
- * than, the key of the leftmost node if the comparison result is greater than, and the key of the
258
- * rightmost node otherwise.
259
- * @param {N | undefined} beginRoot - The `beginRoot` parameter is the starting point for finding the last
260
- * key in a binary tree. It represents the root node of the subtree from which the search for the
261
- * last key should begin. If no specific `beginRoot` is provided, the search will start from the root
262
- * of the entire binary
272
+ * Time Complexity: O(log n) - Average case for a balanced tree.
273
+ * Space Complexity: O(1) - Constant space is used.
274
+ */
275
+ /**
276
+ * Time Complexity: O(log n) - Average case for a balanced tree.
277
+ * Space Complexity: O(1) - Constant space is used.
278
+ *
279
+ * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
280
+ * leftmost node if the comparison result is greater than.
281
+ * @param {BTNKey | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
282
+ * type `BTNKey`, `N`, or `undefined`. It represents the starting point for finding the last key in
283
+ * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
263
284
  * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
264
- * be performed when finding the last key. It determines whether the iteration should be performed in
265
- * pre-order, in-order, or post-order.
285
+ * be performed. It can have one of the following values:
266
286
  * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
267
287
  * the key of the leftmost node if the comparison result is greater than, and the key of the
268
288
  * rightmost node otherwise. If no node is found, it returns 0.
@@ -276,10 +296,17 @@ class BST extends binary_tree_1.BinaryTree {
276
296
  return this.getRightMost(beginRoot, iterationType)?.key ?? 0;
277
297
  }
278
298
  /**
299
+ * Time Complexity: O(log n) - Average case for a balanced tree.
300
+ * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
301
+ */
302
+ /**
303
+ * Time Complexity: O(log n) - Average case for a balanced tree.
304
+ * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
305
+ *
279
306
  * The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
280
307
  * either recursive or iterative methods.
281
308
  * @param {BTNKey} key - The `key` parameter is the key value that we are searching for in the tree.
282
- * It is used to find the node with the matching key value.
309
+ * It is used to identify the node that we want to retrieve.
283
310
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
284
311
  * type of iteration to use when searching for a node in the binary tree. It can have two possible
285
312
  * values:
@@ -330,25 +357,31 @@ class BST extends binary_tree_1.BinaryTree {
330
357
  return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
331
358
  }
332
359
  /**
333
- * The function `getNodes` retrieves nodes from a binary tree based on a given node property or key,
334
- * using either recursive or iterative traversal.
335
- * @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter represents the property
336
- * of the binary tree node that you want to search for. It can be either a `BTNKey` or a
337
- * generic type `N`.
338
- * @param callback - The `callback` parameter is a function that takes a node as input and returns a
339
- * value. This value is compared with the `nodeProperty` parameter to determine if the node should be
340
- * included in the result. The default value for `callback` is `this._defaultOneParamCallback`, which is
341
- * a
342
- * @param [onlyOne=false] - A boolean value indicating whether to stop the traversal after finding
343
- * the first node that matches the nodeProperty. If set to true, the function will return an array
344
- * containing only that node. If set to false (default), the function will continue the traversal and
345
- * return an array containing all nodes that match the node
346
- * @param {N | undefined} beginRoot - The `beginRoot` parameter is the starting node for the traversal. It
347
- * specifies the root node of the binary tree from which the traversal should begin. If `beginRoot`
348
- * is `undefined`, an empty array will be returned.
349
- * @param iterationType - The `iterationType` parameter determines the type of iteration used to
350
- * traverse the binary tree. It can have one of the following values:
351
- * @returns an array of nodes (N[]).
360
+ * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
361
+ * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
362
+ */
363
+ /**
364
+ * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
365
+ * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
366
+ *
367
+ * The function `getNodes` returns an array of nodes that match a given identifier, using either a
368
+ * recursive or iterative approach.
369
+ * @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
370
+ * want to search for in the nodes of the binary tree. It can be of any type that is returned by the
371
+ * callback function `C`.
372
+ * @param {C} callback - The `callback` parameter is a function that takes a node of type `N` as its
373
+ * argument and returns a value of type `ReturnType<C>`. The `C` type parameter represents a callback
374
+ * function type that extends the `BTNCallback<N>` type. The `BTNCallback<N>` type is
375
+ * @param [onlyOne=false] - A boolean flag indicating whether to stop searching after finding the
376
+ * first node that matches the identifier. If set to true, the function will return an array
377
+ * containing only the first matching node. If set to false (default), the function will continue
378
+ * searching for all nodes that match the identifier and return an array containing
379
+ * @param {BTNKey | N | undefined} beginRoot - The `beginRoot` parameter represents the starting node
380
+ * for the traversal. It can be either a key value or a node object. If it is undefined, the
381
+ * traversal will start from the root of the tree.
382
+ * @param iterationType - The `iterationType` parameter determines the type of iteration to be
383
+ * performed on the binary tree. It can have two possible values:
384
+ * @returns The method returns an array of nodes (`N[]`).
352
385
  */
353
386
  getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
354
387
  beginRoot = this.ensureNotKey(beginRoot);
@@ -406,23 +439,30 @@ class BST extends binary_tree_1.BinaryTree {
406
439
  }
407
440
  return ans;
408
441
  }
409
- // --- start additional functions
410
442
  /**
411
- * The `lesserOrGreaterTraverse` function traverses a binary tree and applies a callback function to
412
- * nodes that have a key value lesser or greater than a target key value.
413
- * @param callback - The `callback` parameter is a function that will be called for each node that
414
- * meets the condition specified by the `lesserOrGreater` parameter. It takes a node as an argument
415
- * and returns a value.
443
+ * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
444
+ * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
445
+ */
446
+ /**
447
+ * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
448
+ * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
449
+ *
450
+ * The `lesserOrGreaterTraverse` function traverses a binary tree and returns an array of nodes that
451
+ * are either lesser or greater than a target node, depending on the specified comparison type.
452
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node
453
+ * that satisfies the condition specified by the `lesserOrGreater` parameter. It takes a single
454
+ * parameter of type `N` (the node type) and returns a value of any type.
416
455
  * @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
417
- * traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It can take one
418
- * of the following values:
419
- * @param {BTNKey | N | undefined} targetNode - The `targetNode` parameter in the
420
- * `lesserOrGreaterTraverse` function is used to specify the node from which the traversal should
421
- * start. It can be either a reference to a specific node (`N`), the key of a node
422
- * (`BTNKey`), or `undefined` to
423
- * @param iterationType - The `iterationType` parameter determines whether the traversal should be
424
- * done recursively or iteratively. It can have two possible values:
425
- * @returns The function `lesserOrGreaterTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
456
+ * traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It is of type
457
+ * `CP`, which is a custom type representing the comparison operator. The possible values for
458
+ * `lesserOrGreater` are
459
+ * @param {BTNKey | N | undefined} targetNode - The `targetNode` parameter represents the node in the
460
+ * binary tree that you want to traverse from. It can be specified either by its key, by the node
461
+ * object itself, or it can be left undefined to start the traversal from the root of the tree.
462
+ * @param iterationType - The `iterationType` parameter determines the type of traversal to be
463
+ * performed on the binary tree. It can have two possible values:
464
+ * @returns The function `lesserOrGreaterTraverse` returns an array of values of type
465
+ * `ReturnType<C>`, which is the return type of the callback function passed as an argument.
426
466
  */
427
467
  lesserOrGreaterTraverse(callback = this._defaultOneParamCallback, lesserOrGreater = types_1.CP.lt, targetNode = this.root, iterationType = this.iterationType) {
428
468
  targetNode = this.ensureNotKey(targetNode);
@@ -474,6 +514,13 @@ class BST extends binary_tree_1.BinaryTree {
474
514
  * 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).
475
515
  */
476
516
  /**
517
+ * Time Complexity: O(n) - Building a balanced tree from a sorted array.
518
+ * Space Complexity: O(n) - Additional space is required for the sorted array.
519
+ */
520
+ /**
521
+ * Time Complexity: O(n) - Building a balanced tree from a sorted array.
522
+ * Space Complexity: O(n) - Additional space is required for the sorted array.
523
+ *
477
524
  * The `perfectlyBalance` function balances a binary search tree by adding nodes in a way that
478
525
  * ensures the tree is perfectly balanced.
479
526
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
@@ -519,6 +566,13 @@ class BST extends binary_tree_1.BinaryTree {
519
566
  }
520
567
  }
521
568
  /**
569
+ * Time Complexity: O(n) - Visiting each node once.
570
+ * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
571
+ */
572
+ /**
573
+ * Time Complexity: O(n) - Visiting each node once.
574
+ * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
575
+ *
522
576
  * The function checks if a binary tree is AVL balanced using either recursive or iterative approach.
523
577
  * @param iterationType - The `iterationType` parameter is used to determine the method of iteration
524
578
  * to check if the AVL tree is balanced. It can have two possible values: