min-heap-typed 1.51.8 → 1.51.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 (50) hide show
  1. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +104 -66
  2. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +119 -87
  3. package/dist/data-structures/binary-tree/avl-tree.d.ts +80 -60
  4. package/dist/data-structures/binary-tree/avl-tree.js +78 -59
  5. package/dist/data-structures/binary-tree/binary-tree.d.ts +316 -224
  6. package/dist/data-structures/binary-tree/binary-tree.js +471 -361
  7. package/dist/data-structures/binary-tree/bst.d.ts +198 -200
  8. package/dist/data-structures/binary-tree/bst.js +215 -249
  9. package/dist/data-structures/binary-tree/rb-tree.d.ts +71 -72
  10. package/dist/data-structures/binary-tree/rb-tree.js +107 -98
  11. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +90 -73
  12. package/dist/data-structures/binary-tree/tree-multi-map.js +105 -93
  13. package/dist/data-structures/graph/abstract-graph.d.ts +10 -15
  14. package/dist/data-structures/graph/abstract-graph.js +10 -15
  15. package/dist/data-structures/hash/hash-map.d.ts +31 -38
  16. package/dist/data-structures/hash/hash-map.js +40 -55
  17. package/dist/data-structures/queue/deque.d.ts +2 -3
  18. package/dist/data-structures/queue/deque.js +2 -3
  19. package/dist/data-structures/trie/trie.d.ts +1 -1
  20. package/dist/data-structures/trie/trie.js +1 -1
  21. package/dist/interfaces/binary-tree.d.ts +6 -6
  22. package/dist/types/common.d.ts +1 -2
  23. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
  24. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -2
  25. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +5 -4
  26. package/dist/types/data-structures/binary-tree/bst.d.ts +4 -4
  27. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -2
  28. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -3
  29. package/dist/utils/utils.js +3 -5
  30. package/package.json +2 -2
  31. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +142 -92
  32. package/src/data-structures/binary-tree/avl-tree.ts +94 -66
  33. package/src/data-structures/binary-tree/binary-tree.ts +530 -398
  34. package/src/data-structures/binary-tree/bst.ts +251 -270
  35. package/src/data-structures/binary-tree/rb-tree.ts +121 -100
  36. package/src/data-structures/binary-tree/tree-multi-map.ts +125 -99
  37. package/src/data-structures/graph/abstract-graph.ts +10 -10
  38. package/src/data-structures/hash/hash-map.ts +42 -49
  39. package/src/data-structures/queue/deque.ts +2 -2
  40. package/src/data-structures/queue/queue.ts +1 -1
  41. package/src/data-structures/trie/trie.ts +2 -2
  42. package/src/interfaces/binary-tree.ts +8 -7
  43. package/src/types/common.ts +1 -2
  44. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +2 -2
  45. package/src/types/data-structures/binary-tree/avl-tree.ts +2 -2
  46. package/src/types/data-structures/binary-tree/binary-tree.ts +5 -4
  47. package/src/types/data-structures/binary-tree/bst.ts +4 -4
  48. package/src/types/data-structures/binary-tree/rb-tree.ts +2 -2
  49. package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -3
  50. package/src/utils/utils.ts +3 -3
@@ -60,31 +60,42 @@ exports.BSTNode = BSTNode;
60
60
  */
61
61
  class BST extends binary_tree_1.BinaryTree {
62
62
  /**
63
- * This is the constructor function for a Binary Search Tree class in TypeScript, which initializes
64
- * the tree with keys, nodes, or entries and optional options.
65
- * @param keysOrNodesOrEntries - The `keysOrNodesOrEntries` parameter is an iterable object that can
66
- * contain keys, nodes, or entries. It is used to initialize the binary search tree with the provided
67
- * keys, nodes, or entries.
68
- * @param [options] - The `options` parameter is an optional object that can contain additional
69
- * configuration options for the binary search tree. It can have the following properties:
70
- */
71
- constructor(keysOrNodesOrEntries = [], options) {
63
+ * This is the constructor function for a Binary Search Tree class in TypeScript.
64
+ * @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
65
+ * iterable that can contain either keys, nodes, entries, or raw elements. These elements will be
66
+ * added to the binary search tree during the construction of the object.
67
+ * @param [options] - An optional object that contains additional options for the Binary Search Tree.
68
+ * It can include a comparator function that defines the order of the elements in the tree.
69
+ */
70
+ constructor(keysOrNodesOrEntriesOrRawElements = [], options) {
72
71
  super([], options);
73
72
  this._root = undefined;
74
- this._comparator = (a, b) => {
73
+ /**
74
+ * Time complexity: O(n)
75
+ * Space complexity: O(n)
76
+ */
77
+ this._DEFAULT_COMPARATOR = (a, b) => {
78
+ if (typeof a === 'object' && typeof b === 'object' && this.comparator === this._DEFAULT_COMPARATOR) {
79
+ throw TypeError('When comparing two object types, it is necessary to customize a [comparator] function of options parameter during the instantiation of the data structure.');
80
+ }
75
81
  if (a > b)
76
82
  return 1;
77
83
  if (a < b)
78
84
  return -1;
79
85
  return 0;
80
86
  };
87
+ /**
88
+ * Time complexity: O(n)
89
+ * Space complexity: O(n)
90
+ */
91
+ this._comparator = this._DEFAULT_COMPARATOR;
81
92
  if (options) {
82
93
  const { comparator } = options;
83
94
  if (comparator)
84
95
  this._comparator = comparator;
85
96
  }
86
- if (keysOrNodesOrEntries)
87
- this.addMany(keysOrNodesOrEntries);
97
+ if (keysOrNodesOrEntriesOrRawElements)
98
+ this.addMany(keysOrNodesOrEntriesOrRawElements);
88
99
  }
89
100
  /**
90
101
  * The function returns the root node of a tree structure.
@@ -93,13 +104,6 @@ class BST extends binary_tree_1.BinaryTree {
93
104
  get root() {
94
105
  return this._root;
95
106
  }
96
- /**
97
- * The function returns the value of the _comparator property.
98
- * @returns The `_comparator` property is being returned.
99
- */
100
- get comparator() {
101
- return this._comparator;
102
- }
103
107
  /**
104
108
  * The function creates a new BSTNode with the given key and value and returns it.
105
109
  * @param {K} key - The key parameter is of type K, which represents the type of the key for the node
@@ -114,104 +118,68 @@ class BST extends binary_tree_1.BinaryTree {
114
118
  /**
115
119
  * The function creates a new binary search tree with the specified options.
116
120
  * @param [options] - The `options` parameter is an optional object that allows you to customize the
117
- * behavior of the `createTree` method. It is of type `Partial<BSTOptions<K>>`, which means it is a
118
- * partial object of type `BSTOptions<K>`.
119
- * @returns a new instance of the BST class, with the provided options merged with the default
120
- * options. The returned value is casted as TREE.
121
+ * behavior of the `createTree` method. It accepts a partial `BSTOptions` object, which has the
122
+ * following properties:
123
+ * @returns a new instance of the BST class with the provided options.
121
124
  */
122
125
  createTree(options) {
123
126
  return new BST([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
124
127
  }
125
128
  /**
126
- * The function `keyValueOrEntryToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid,
127
- * otherwise it returns undefined.
128
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`, where:
129
- * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
130
- * `keyValueOrEntryToNode` function. It represents the value associated with the keyOrNodeOrEntry node.
131
- * @returns a node of type NODE or undefined.
132
- */
133
- keyValueOrEntryToNode(keyOrNodeOrEntry, value) {
134
- let node;
135
- if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
136
- return;
137
- }
138
- else if (this.isNode(keyOrNodeOrEntry)) {
139
- node = keyOrNodeOrEntry;
140
- }
141
- else if (this.isEntry(keyOrNodeOrEntry)) {
142
- const [key, value] = keyOrNodeOrEntry;
143
- if (key === undefined || key === null) {
144
- return;
145
- }
146
- else {
147
- node = this.createNode(key, value);
148
- }
149
- }
150
- else if (!this.isNode(keyOrNodeOrEntry)) {
151
- node = this.createNode(keyOrNodeOrEntry, value);
152
- }
153
- else {
154
- return;
155
- }
156
- return node;
157
- }
158
- /**
159
- * Time Complexity: O(log n)
160
- * Space Complexity: O(log n)
129
+ * The function overrides a method and converts a key, value pair or entry or raw element to a node.
130
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - A variable that can be of
131
+ * type R or KeyOrNodeOrEntry<K, V, NODE>. It represents either a key, a node, an entry, or a raw
132
+ * element.
133
+ * @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
134
+ * value associated with a key in a key-value pair.
135
+ * @returns either a NODE object or undefined.
161
136
  */
137
+ keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value) {
138
+ var _a;
139
+ return (_a = super.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value)) !== null && _a !== void 0 ? _a : undefined;
140
+ }
162
141
  /**
163
142
  * Time Complexity: O(log n)
164
143
  * Space Complexity: O(log n)
165
144
  *
166
- * The function `ensureNode` returns the node corresponding to the given key if it is a node key,
167
- * otherwise it returns the key itself.
168
- * @param {K | NODE | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `NODE`, or
169
- * `undefined`.
170
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
171
- * type of iteration to be performed. It has a default value of `'ITERATIVE'`.
172
- * @returns either a node object (NODE) or undefined.
173
- */
174
- ensureNode(keyOrNodeOrEntry, iterationType = 'ITERATIVE') {
175
- if (keyOrNodeOrEntry === this.NIL)
176
- return;
177
- if (this.isRealNode(keyOrNodeOrEntry)) {
178
- return keyOrNodeOrEntry;
179
- }
180
- if (this.isEntry(keyOrNodeOrEntry)) {
181
- const key = keyOrNodeOrEntry[0];
182
- if (key === null || key === undefined)
183
- return;
184
- return this.getNodeByKey(key, iterationType);
185
- }
186
- const key = keyOrNodeOrEntry;
187
- if (key === null || key === undefined)
188
- return;
189
- return this.getNodeByKey(key, iterationType);
145
+ * The function ensures the existence of a node in a data structure and returns it, or undefined if
146
+ * it doesn't exist.
147
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
148
+ * `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, which represents the key, node,
149
+ * entry, or raw element that needs to be ensured in the tree.
150
+ * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
151
+ * parameter that specifies the type of iteration to be used when ensuring a node. It has a default
152
+ * value of `'ITERATIVE'`.
153
+ * @returns The method is returning either the node that was ensured or `undefined` if the node could
154
+ * not be ensured.
155
+ */
156
+ ensureNode(keyOrNodeOrEntryOrRawElement, iterationType = 'ITERATIVE') {
157
+ var _a;
158
+ return (_a = super.ensureNode(keyOrNodeOrEntryOrRawElement, iterationType)) !== null && _a !== void 0 ? _a : undefined;
190
159
  }
191
160
  /**
192
- * The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
193
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, NODE>`.
194
- * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the BSTNode class.
161
+ * The function checks if the input is an instance of the BSTNode class.
162
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
163
+ * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
164
+ * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
165
+ * an instance of the `BSTNode` class.
195
166
  */
196
- isNode(keyOrNodeOrEntry) {
197
- return keyOrNodeOrEntry instanceof BSTNode;
167
+ isNode(keyOrNodeOrEntryOrRawElement) {
168
+ return keyOrNodeOrEntryOrRawElement instanceof BSTNode;
198
169
  }
199
- /**
200
- * Time Complexity: O(log n)
201
- * Space Complexity: O(1)
202
- */
203
170
  /**
204
171
  * Time Complexity: O(log n)
205
172
  * Space Complexity: O(1)
206
173
  *
207
- * The `add` function in TypeScript adds a new node to a binary search tree based on the key value,
208
- * updating the value if the key already exists.
209
- * @param keyOrNodeOrEntry - It is a parameter that can accept three types of values:
210
- * @param {V} [value] - The value to be added to the binary search tree.
211
- * @returns The method returns a boolean value.
212
- */
213
- add(keyOrNodeOrEntry, value) {
214
- const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
174
+ * The `add` function in TypeScript adds a new node to a binary search tree based on the key value.
175
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
176
+ * `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
177
+ * @param {V} [value] - The `value` parameter is an optional value that can be associated with the
178
+ * key in the binary search tree. If provided, it will be stored in the node along with the key.
179
+ * @returns a boolean value.
180
+ */
181
+ add(keyOrNodeOrEntryOrRawElement, value) {
182
+ const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
215
183
  if (newNode === undefined)
216
184
  return false;
217
185
  if (this.root === undefined) {
@@ -222,15 +190,8 @@ class BST extends binary_tree_1.BinaryTree {
222
190
  let current = this.root;
223
191
  while (current !== undefined) {
224
192
  if (this.comparator(current.key, newNode.key) === 0) {
225
- // if (current !== newNode) {
226
- // The key value is the same but the reference is different, update the value of the existing node
227
193
  this._replaceNode(current, newNode);
228
194
  return true;
229
- // } else {
230
- // The key value is the same and the reference is the same, replace the entire node
231
- // this._replaceNode(current, newNode);
232
- // return;
233
- // }
234
195
  }
235
196
  else if (this.comparator(current.key, newNode.key) > 0) {
236
197
  if (current.left === undefined) {
@@ -252,18 +213,17 @@ class BST extends binary_tree_1.BinaryTree {
252
213
  return false;
253
214
  }
254
215
  /**
255
- * Time Complexity: O(k log n)
256
- * Space Complexity: O(k + log n)
216
+ * Time Complexity: O(log n)
217
+ * Space Complexity: O(log n)
257
218
  */
258
219
  /**
259
220
  * Time Complexity: O(k log n)
260
221
  * Space Complexity: O(k + log n)
261
222
  *
262
- * The `addMany` function in TypeScript adds multiple keys or nodes to a data structure, balancing
263
- * the structure if specified, and returns an array indicating whether each key or node was
264
- * successfully inserted.
265
- * @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries to be added to the
266
- * data structure.
223
+ * The `addMany` function in TypeScript adds multiple keys or nodes to a data structure and returns
224
+ * an array indicating whether each key or node was successfully inserted.
225
+ * @param keysOrNodesOrEntriesOrRawElements - An iterable containing keys, nodes, entries, or raw
226
+ * elements to be added to the data structure.
267
227
  * @param [values] - An optional iterable of values to be associated with the keys or nodes being
268
228
  * added. If provided, the values will be assigned to the corresponding keys or nodes in the same
269
229
  * order. If not provided, undefined will be assigned as the value for each key or node.
@@ -272,20 +232,19 @@ class BST extends binary_tree_1.BinaryTree {
272
232
  * algorithm. If set to false, the elements will be added without balancing the tree. The default
273
233
  * value is true.
274
234
  * @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
275
- * specifies the type of iteration to use when adding multiple keys or nodes to the binary tree. It
276
- * has a default value of `this.iterationType`, which means it will use the iteration type specified
277
- * in the binary tree instance.
278
- * @returns The function `addMany` returns an array of booleans indicating whether each key or node
279
- * or entry was successfully inserted into the data structure.
235
+ * specifies the type of iteration to use when adding multiple keys or nodes to the binary search
236
+ * tree. It can have two possible values:
237
+ * @returns The function `addMany` returns an array of booleans indicating whether each element was
238
+ * successfully inserted into the data structure.
280
239
  */
281
- addMany(keysOrNodesOrEntries, values, isBalanceAdd = true, iterationType = this.iterationType) {
240
+ addMany(keysOrNodesOrEntriesOrRawElements, values, isBalanceAdd = true, iterationType = this.iterationType) {
282
241
  const inserted = [];
283
242
  let valuesIterator;
284
243
  if (values) {
285
244
  valuesIterator = values[Symbol.iterator]();
286
245
  }
287
246
  if (!isBalanceAdd) {
288
- for (const kve of keysOrNodesOrEntries) {
247
+ for (const kve of keysOrNodesOrEntriesOrRawElements) {
289
248
  const value = valuesIterator === null || valuesIterator === void 0 ? void 0 : valuesIterator.next().value;
290
249
  const nn = this.add(kve, value);
291
250
  inserted.push(nn);
@@ -298,25 +257,32 @@ class BST extends binary_tree_1.BinaryTree {
298
257
  return false;
299
258
  return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null));
300
259
  };
301
- for (const kve of keysOrNodesOrEntries) {
260
+ for (const kve of keysOrNodesOrEntriesOrRawElements) {
302
261
  isRealBTNExemplar(kve) && realBTNExemplars.push(kve);
303
262
  }
304
263
  let sorted = [];
305
264
  sorted = realBTNExemplars.sort((a, b) => {
306
265
  let keyA, keyB;
307
- if (this.isEntry(a)) {
266
+ if (this.isEntry(a))
308
267
  keyA = a[0];
309
- }
310
268
  else if (this.isRealNode(a))
311
269
  keyA = a.key;
312
- else
270
+ else if (this.toEntryFn) {
271
+ keyA = this.toEntryFn(a)[0];
272
+ }
273
+ else {
313
274
  keyA = a;
275
+ }
314
276
  if (this.isEntry(b))
315
277
  keyB = b[0];
316
278
  else if (this.isRealNode(b))
317
279
  keyB = b.key;
318
- else
280
+ else if (this.toEntryFn) {
281
+ keyB = this.toEntryFn(b)[0];
282
+ }
283
+ else {
319
284
  keyB = b;
285
+ }
320
286
  if (keyA !== undefined && keyA !== null && keyB !== undefined && keyB !== null) {
321
287
  return this.comparator(keyA, keyB);
322
288
  }
@@ -357,32 +323,26 @@ class BST extends binary_tree_1.BinaryTree {
357
323
  return inserted;
358
324
  }
359
325
  /**
360
- * Time Complexity: O(log n)
361
- * Space Complexity: O(k + log n)
362
- * /
363
-
364
- /**
365
326
  * Time Complexity: O(log n)
366
327
  * Space Complexity: O(k + log n)
367
328
  *
368
- * The function `getNodes` returns an array of nodes that match a given identifier, using either a
369
- * recursive or iterative approach.
329
+ * The `getNodes` function in TypeScript retrieves nodes from a binary tree based on a given
330
+ * identifier and callback function.
370
331
  * @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
371
- * want to search for in the nodes of the binary tree. It can be of any type that is returned by the
372
- * callback function `C`.
373
- * @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as its
374
- * argument and returns a value of type `ReturnType<C>`. The `C` type parameter represents a callback
375
- * function type that extends the `BTNCallback<NODE>` type. The `BTNCallback<NODE>` type is
376
- * @param [onlyOne=false] - A boolean flag indicating whether to stop searching after finding the
377
- * first node that matches the identifier. If set to true, the function will return an array
378
- * containing only the first matching node. If set to false (default), the function will continue
379
- * searching for all nodes that match the identifier and return an array containing
380
- * @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter represents the starting node
381
- * for the traversal. It can be either a key value or a node object. If it is undefined, the
382
- * traversal will start from the root of the tree.
383
- * @param iterationType - The `iterationType` parameter determines the type of iteration to be
384
- * performed on the binary tree. It can have two possible values:
385
- * @returns The method returns an array of nodes (`NODE[]`).
332
+ * want to search for in the binary tree. It can be of any type that is returned by the callback
333
+ * function.
334
+ * @param {C} callback - The `callback` parameter is a function that takes a node as input and
335
+ * returns a value. This value is used to identify the nodes that match the given identifier. The
336
+ * `callback` function is optional and defaults to `this._DEFAULT_CALLBACK`.
337
+ * @param [onlyOne=false] - A boolean value indicating whether to return only the first matching node
338
+ * or all matching nodes. If set to true, only the first matching node will be returned. If set to
339
+ * false, all matching nodes will be returned. The default value is false.
340
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
341
+ * point for the search in the binary tree. It can be either a node object, a key-value pair, or an
342
+ * entry object. If it is not provided, the `root` of the binary tree is used as the starting point.
343
+ * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
344
+ * iteration to be performed. It can have two possible values:
345
+ * @returns The method `getNodes` returns an array of `NODE` objects.
386
346
  */
387
347
  getNodes(identifier, callback = this._DEFAULT_CALLBACK, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
388
348
  beginRoot = this.ensureNode(beginRoot);
@@ -453,51 +413,50 @@ class BST extends binary_tree_1.BinaryTree {
453
413
  * Time Complexity: O(log n)
454
414
  * Space Complexity: O(1)
455
415
  *
456
- * The `getNode` function retrieves a node from a Red-Black Tree based on the provided identifier and
457
- * callback function.
458
- * @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value or key
459
- * that you want to search for in the binary search tree. It can be of any type that is compatible
460
- * with the type of nodes in the tree.
461
- * @param {C} callback - The `callback` parameter is a function that will be called for each node in
462
- * the tree. It is used to determine whether a node matches the given identifier. The `callback`
463
- * function should take a node as its parameter and return a value that can be compared to the
464
- * `identifier` parameter.
416
+ * The function `getNode` returns the first node that matches the given identifier and callback
417
+ * function in a binary search tree.
418
+ * @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
419
+ * want to search for in the binary search tree. It can be of any type that is compatible with the
420
+ * type returned by the callback function.
421
+ * @param {C} callback - The `callback` parameter is a function that will be used to determine if a
422
+ * node matches the desired criteria. It should be a function that takes a node as an argument and
423
+ * returns a boolean value indicating whether the node matches the criteria or not. If no callback is
424
+ * provided, the default callback will be
465
425
  * @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
466
- * search tree. It can be either a key or a node. If it is a key, it will be converted to a node
467
- * using the `ensureNode` method. If it is not provided, the `root`
468
- * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
469
- * be performed when searching for nodes in the binary search tree. It is an optional parameter and
470
- * its default value is taken from the `iterationType` property of the class.
471
- * @returns The method is returning a value of type `NODE | null | undefined`.
426
+ * search tree. It can be either a key or a node. If it is a key, the search will start from the node
427
+ * with that key. If it is a node, the search will start from that node.
428
+ * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
429
+ * of iteration to be performed when searching for nodes in the binary search tree. It can have one
430
+ * of the following values:
431
+ * @returns The method is returning a NODE object or undefined.
472
432
  */
473
433
  getNode(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
474
434
  var _a;
475
435
  return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
476
436
  }
477
437
  /**
478
- * Time Complexity: O(log n)
479
- * Space Complexity: O(1)
438
+ * Time Complexity: O(k log n)
439
+ * Space Complexity: O(k + log n)
480
440
  */
481
441
  /**
482
442
  * Time Complexity: O(log n)
483
443
  * Space Complexity: O(1)
484
444
  *
485
- * The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
486
- * either recursive or iterative methods.
487
- * @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
488
- * It is used to identify the node that we want to retrieve.
489
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
490
- * type of iteration to use when searching for a node in the binary tree. It can have two possible
491
- * values:
492
- * @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
493
- * found in the binary tree. If no node is found, it returns `undefined`.
445
+ * The function `getNodeByKey` returns a node with a specific key from a tree data structure.
446
+ * @param {K} key - The key parameter is the value used to search for a specific node in the tree. It
447
+ * is typically a unique identifier or a value that can be used to determine the position of the node
448
+ * in the tree structure.
449
+ * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
450
+ * parameter that specifies the type of iteration to be used when searching for a node in the tree.
451
+ * It has a default value of `'ITERATIVE'`.
452
+ * @returns The method is returning a NODE object or undefined.
494
453
  */
495
454
  getNodeByKey(key, iterationType = 'ITERATIVE') {
496
455
  return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
497
456
  }
498
457
  /**
499
- * Time complexity: O(n)
500
- * Space complexity: O(n)
458
+ * Time Complexity: O(log n)
459
+ * Space Complexity: O(k + log n)
501
460
  */
502
461
  /**
503
462
  * Time complexity: O(n)
@@ -506,15 +465,16 @@ class BST extends binary_tree_1.BinaryTree {
506
465
  * The function overrides the depth-first search method and returns an array of the return types of
507
466
  * the callback function.
508
467
  * @param {C} callback - The `callback` parameter is a function that will be called for each node
509
- * during the depth-first search traversal. It is an optional parameter and if not provided, a
510
- * default callback function will be used.
511
- * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter specifies the order in which the
512
- * nodes are visited during the depth-first search. It can have one of the following values:
513
- * @param beginRoot - The `beginRoot` parameter is used to specify the starting point for the
514
- * Depth-First Search (DFS) traversal. It can be either a key, a node, or an entry in the tree. If no
515
- * value is provided, the DFS traversal will start from the root of the tree.
516
- * @param {IterationType} iterationType - The `iterationType` parameter specifies the type of
517
- * iteration to be used during the Depth-First Search (DFS) traversal. It can have one of the
468
+ * during the depth-first search traversal. It is an optional parameter and defaults to
469
+ * `this._DEFAULT_CALLBACK`. The type `C` represents the type of the callback function.
470
+ * @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
471
+ * order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
472
+ * take one of the following values:
473
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
474
+ * point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
475
+ * node entry. If not specified, the default value is the root of the tree.
476
+ * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
477
+ * type of iteration to be used during the Depth-First Search (DFS) traversal. It can have one of the
518
478
  * following values:
519
479
  * @returns The method is returning an array of the return type of the callback function.
520
480
  */
@@ -522,8 +482,8 @@ class BST extends binary_tree_1.BinaryTree {
522
482
  return super.dfs(callback, pattern, beginRoot, iterationType, false);
523
483
  }
524
484
  /**
525
- * Time complexity: O(n)
526
- * Space complexity: O(n)
485
+ * Time Complexity: O(log n)
486
+ * Space Complexity: O(1)
527
487
  */
528
488
  /**
529
489
  * Time complexity: O(n)
@@ -532,38 +492,38 @@ class BST extends binary_tree_1.BinaryTree {
532
492
  * The function overrides the breadth-first search method and returns an array of the return types of
533
493
  * the callback function.
534
494
  * @param {C} callback - The `callback` parameter is a function that will be called for each node
535
- * visited during the breadth-first search traversal. It is an optional parameter and if not
536
- * provided, a default callback function will be used.
537
- * @param beginRoot - The `beginRoot` parameter is the starting point for the breadth-first search
538
- * traversal. It can be either a key, a node, or an entry in the tree. If not specified, the root of
539
- * the tree is used as the starting point.
540
- * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
541
- * be performed during the breadth-first search (BFS) traversal. It determines the order in which the
542
- * nodes are visited.
543
- * @returns The method is returning an array of the return type of the callback function.
495
+ * visited during the breadth-first search. It should take a single argument, which is the current
496
+ * node being visited, and it can return a value of any type.
497
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
498
+ * point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
499
+ * object. If no value is provided, the default value is the root of the tree.
500
+ * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
501
+ * of iteration to be performed during the breadth-first search (BFS) traversal. It can have one of
502
+ * the following values:
503
+ * @returns an array of the return type of the callback function.
544
504
  */
545
505
  bfs(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
546
506
  return super.bfs(callback, beginRoot, iterationType, false);
547
507
  }
548
508
  /**
549
- * Time complexity: O(n)
550
- * Space complexity: O(n)
509
+ * Time Complexity: O(log n)
510
+ * Space Complexity: O(1)
551
511
  */
552
512
  /**
553
513
  * Time complexity: O(n)
554
514
  * Space complexity: O(n)
555
515
  *
556
- * The function overrides the listLevels method and returns an array of arrays containing the return
557
- * type of the callback function for each level of the tree.
516
+ * The function overrides the listLevels method from the superclass and returns an array of arrays
517
+ * containing the results of the callback function applied to each level of the tree.
558
518
  * @param {C} callback - The `callback` parameter is a generic type `C` that extends
559
- * `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the tree
560
- * during the level listing process.
561
- * @param beginRoot - The `beginRoot` parameter is used to specify the starting point for listing the
562
- * levels of a binary tree. It can be either a key, a node, or an entry in the binary tree. If not
563
- * provided, the root of the binary tree is used as the starting point.
564
- * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
565
- * be performed on the tree. It determines the order in which the nodes are visited during the
566
- * iteration.
519
+ * `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the
520
+ * tree during the iteration process.
521
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
522
+ * point for listing the levels of the binary tree. It can be either a root node of the tree, a
523
+ * key-value pair representing a node in the tree, or a key representing a node in the tree. If no
524
+ * value is provided, the root of
525
+ * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
526
+ * of iteration to be performed on the tree. It can have one of the following values:
567
527
  * @returns The method is returning a two-dimensional array of the return type of the callback
568
528
  * function.
569
529
  */
@@ -571,27 +531,27 @@ class BST extends binary_tree_1.BinaryTree {
571
531
  return super.listLevels(callback, beginRoot, iterationType, false);
572
532
  }
573
533
  /**
574
- * Time Complexity: O(log n)
575
- * Space Complexity: O(log n)
534
+ * Time complexity: O(n)
535
+ * Space complexity: O(n)
576
536
  */
577
537
  /**
578
- * Time Complexity: O(log n)
579
- * Space Complexity: O(log n)
538
+ * Time complexity: O(n)
539
+ * Space complexity: O(n)
580
540
  *
581
- * The `lesserOrGreaterTraverse` function traverses a binary tree and returns an array of nodes that
582
- * are either lesser or greater than a target node, depending on the specified comparison type.
541
+ * The `lesserOrGreaterTraverse` function traverses a binary tree and applies a callback function to
542
+ * each node that meets a certain condition based on a target node and a comparison value.
583
543
  * @param {C} callback - The `callback` parameter is a function that will be called for each node
584
- * that satisfies the condition specified by the `lesserOrGreater` parameter. It takes a single
585
- * parameter of type `NODE` (the node type) and returns a value of any type.
544
+ * that meets the condition specified by the `lesserOrGreater` parameter. It takes a single argument,
545
+ * which is the current node being traversed, and returns a value of any type.
586
546
  * @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
587
- * traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It is of type
588
- * `CP`, which is a custom type representing the comparison operator. The possible values for
589
- * `lesserOrGreater` are
590
- * @param {K | NODE | undefined} targetNode - The `targetNode` parameter represents the node in the
591
- * binary tree that you want to traverse from. It can be specified either by its key, by the node
592
- * object itself, or it can be left undefined to start the traversal from the root of the tree.
593
- * @param iterationType - The `iterationType` parameter determines the type of traversal to be
594
- * performed on the binary tree. It can have two possible values:
547
+ * traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
548
+ * 0, or 1, where:
549
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} targetNode - The `targetNode` parameter is the node in
550
+ * the binary tree that you want to start traversing from. It can be specified either by providing
551
+ * the key of the node, the node itself, or an entry containing the key and value of the node. If no
552
+ * `targetNode` is provided,
553
+ * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
554
+ * traversal to be performed on the binary tree. It can have two possible values:
595
555
  * @returns The function `lesserOrGreaterTraverse` returns an array of values of type
596
556
  * `ReturnType<C>`, which is the return type of the callback function passed as an argument.
597
557
  */
@@ -634,18 +594,19 @@ class BST extends binary_tree_1.BinaryTree {
634
594
  }
635
595
  }
636
596
  /**
637
- * Time Complexity: O(log n)
638
- * Space Complexity: O(log n)
597
+ * Time complexity: O(n)
598
+ * Space complexity: O(n)
639
599
  */
640
600
  /**
641
- * Time Complexity: O(log n)
642
- * Space Complexity: O(log n)
601
+ * Time complexity: O(n)
602
+ * Space complexity: O(n)
643
603
  *
644
- * The `perfectlyBalance` function balances a binary search tree by adding nodes in a way that
645
- * ensures the tree is perfectly balanced.
646
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
647
- * type of iteration to use when building a balanced binary search tree. It can have two possible
648
- * values:
604
+ * The `perfectlyBalance` function takes an optional `iterationType` parameter and returns `true` if
605
+ * the binary search tree is perfectly balanced, otherwise it returns `false`.
606
+ * @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
607
+ * specifies the type of iteration to use when building a balanced binary search tree. It has a
608
+ * default value of `this.iterationType`, which means it will use the iteration type specified in the
609
+ * current instance of the class.
649
610
  * @returns The function `perfectlyBalance` returns a boolean value.
650
611
  */
651
612
  perfectlyBalance(iterationType = this.iterationType) {
@@ -685,25 +646,19 @@ class BST extends binary_tree_1.BinaryTree {
685
646
  }
686
647
  }
687
648
  /**
688
- * Balancing Adjustment:
689
- * Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.
690
- * AVL Tree: After insertion or deletion operations, an AVL tree performs rotation adjustments based on the balance factor of nodes to restore the tree's balance. These rotations can be left rotations, right rotations, left-right rotations, or right-left rotations, performed as needed.
691
- *
692
- * Use Cases and Efficiency:
693
- * Perfectly Balanced Binary Tree: Perfectly balanced binary trees are typically used in specific scenarios such as complete binary heaps in heap sort or certain types of Huffman trees. However, they are not suitable for dynamic operations requiring frequent insertions and deletions, as these operations often necessitate full tree reconstruction.
694
- * 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).
695
- */
696
- /**
697
- * Time Complexity: O(n)
698
- * Space Complexity: O(log n)
649
+ * Time complexity: O(n)
650
+ * Space complexity: O(n)
699
651
  */
700
652
  /**
701
653
  * Time Complexity: O(n)
702
654
  * Space Complexity: O(log n)
703
655
  *
704
- * The function checks if a binary tree is AVL balanced using either recursive or iterative approach.
705
- * @param iterationType - The `iterationType` parameter is used to determine the method of iteration
706
- * to check if the AVL tree is balanced. It can have two possible values:
656
+ * The function `isAVLBalanced` checks if a binary tree is AVL balanced using either a recursive or
657
+ * iterative approach.
658
+ * @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
659
+ * specifies the type of iteration to use when checking if the AVL tree is balanced. It has a default
660
+ * value of `this.iterationType`, which means it will use the iteration type specified in the current
661
+ * instance of the AVL tree.
707
662
  * @returns a boolean value.
708
663
  */
709
664
  isAVLBalanced(iterationType = this.iterationType) {
@@ -753,9 +708,20 @@ class BST extends binary_tree_1.BinaryTree {
753
708
  return balanced;
754
709
  }
755
710
  /**
756
- * The function sets the root property of an object and updates the parent property of the new root.
757
- * @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. This means that it
758
- * can either be an object of type `NODE` or it can be `undefined`.
711
+ * Time Complexity: O(n)
712
+ * Space Complexity: O(log n)
713
+ */
714
+ /**
715
+ * The function returns the value of the _comparator property.
716
+ * @returns The `_comparator` property is being returned.
717
+ */
718
+ get comparator() {
719
+ return this._comparator;
720
+ }
721
+ /**
722
+ * The function sets the root of a tree-like structure and updates the parent property of the new
723
+ * root.
724
+ * @param {NODE | undefined} v - v is a parameter of type NODE or undefined.
759
725
  */
760
726
  _setRoot(v) {
761
727
  if (v) {