directed-graph-typed 1.52.6 → 1.52.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 (77) hide show
  1. package/dist/constants/index.d.ts +4 -0
  2. package/dist/constants/index.js +8 -0
  3. package/dist/data-structures/base/iterable-element-base.d.ts +8 -1
  4. package/dist/data-structures/base/iterable-element-base.js +10 -1
  5. package/dist/data-structures/base/iterable-entry-base.d.ts +8 -1
  6. package/dist/data-structures/base/iterable-entry-base.js +10 -10
  7. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +31 -32
  8. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +43 -44
  9. package/dist/data-structures/binary-tree/avl-tree.d.ts +23 -24
  10. package/dist/data-structures/binary-tree/avl-tree.js +71 -64
  11. package/dist/data-structures/binary-tree/binary-indexed-tree.js +2 -2
  12. package/dist/data-structures/binary-tree/binary-tree.d.ts +591 -402
  13. package/dist/data-structures/binary-tree/binary-tree.js +690 -604
  14. package/dist/data-structures/binary-tree/bst.d.ts +72 -65
  15. package/dist/data-structures/binary-tree/bst.js +122 -125
  16. package/dist/data-structures/binary-tree/rb-tree.d.ts +21 -24
  17. package/dist/data-structures/binary-tree/rb-tree.js +42 -39
  18. package/dist/data-structures/binary-tree/segment-tree.d.ts +2 -2
  19. package/dist/data-structures/binary-tree/segment-tree.js +2 -2
  20. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +27 -31
  21. package/dist/data-structures/binary-tree/tree-multi-map.js +45 -43
  22. package/dist/data-structures/graph/abstract-graph.d.ts +2 -2
  23. package/dist/data-structures/graph/abstract-graph.js +7 -4
  24. package/dist/data-structures/graph/directed-graph.d.ts +2 -2
  25. package/dist/data-structures/graph/directed-graph.js +4 -2
  26. package/dist/data-structures/graph/undirected-graph.d.ts +2 -2
  27. package/dist/data-structures/hash/hash-map.d.ts +2 -2
  28. package/dist/data-structures/hash/hash-map.js +1 -1
  29. package/dist/data-structures/heap/heap.js +3 -3
  30. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
  31. package/dist/data-structures/linked-list/doubly-linked-list.js +7 -7
  32. package/dist/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  33. package/dist/data-structures/linked-list/singly-linked-list.js +6 -6
  34. package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -2
  35. package/dist/data-structures/matrix/matrix.d.ts +2 -2
  36. package/dist/data-structures/matrix/navigator.d.ts +2 -2
  37. package/dist/data-structures/matrix/navigator.js +4 -2
  38. package/dist/data-structures/queue/deque.d.ts +3 -3
  39. package/dist/data-structures/queue/deque.js +29 -29
  40. package/dist/data-structures/queue/queue.d.ts +1 -1
  41. package/dist/data-structures/stack/stack.d.ts +2 -2
  42. package/dist/data-structures/trie/trie.d.ts +2 -2
  43. package/dist/data-structures/trie/trie.js +1 -1
  44. package/dist/index.d.ts +1 -0
  45. package/dist/index.js +1 -0
  46. package/dist/interfaces/binary-tree.d.ts +2 -2
  47. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -4
  48. package/dist/types/data-structures/binary-tree/binary-tree.js +0 -6
  49. package/package.json +2 -2
  50. package/src/constants/index.ts +4 -0
  51. package/src/data-structures/base/iterable-element-base.ts +11 -1
  52. package/src/data-structures/base/iterable-entry-base.ts +11 -19
  53. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +46 -50
  54. package/src/data-structures/binary-tree/avl-tree.ts +68 -71
  55. package/src/data-structures/binary-tree/binary-indexed-tree.ts +2 -2
  56. package/src/data-structures/binary-tree/binary-tree.ts +716 -748
  57. package/src/data-structures/binary-tree/bst.ts +137 -146
  58. package/src/data-structures/binary-tree/rb-tree.ts +46 -46
  59. package/src/data-structures/binary-tree/segment-tree.ts +2 -2
  60. package/src/data-structures/binary-tree/tree-multi-map.ts +49 -49
  61. package/src/data-structures/graph/abstract-graph.ts +6 -6
  62. package/src/data-structures/graph/directed-graph.ts +4 -4
  63. package/src/data-structures/graph/undirected-graph.ts +2 -2
  64. package/src/data-structures/hash/hash-map.ts +3 -3
  65. package/src/data-structures/heap/heap.ts +3 -3
  66. package/src/data-structures/linked-list/doubly-linked-list.ts +9 -9
  67. package/src/data-structures/linked-list/singly-linked-list.ts +8 -8
  68. package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
  69. package/src/data-structures/matrix/matrix.ts +2 -2
  70. package/src/data-structures/matrix/navigator.ts +4 -4
  71. package/src/data-structures/queue/deque.ts +31 -31
  72. package/src/data-structures/queue/queue.ts +1 -1
  73. package/src/data-structures/stack/stack.ts +2 -2
  74. package/src/data-structures/trie/trie.ts +3 -3
  75. package/src/index.ts +2 -1
  76. package/src/interfaces/binary-tree.ts +3 -3
  77. package/src/types/data-structures/binary-tree/binary-tree.ts +3 -5
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * data-structure-typed
3
3
  *
4
- * @author Tyler Zeng
5
- * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
4
+ * @author Pablo Zeng
5
+ * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
8
  import type {
@@ -11,15 +11,15 @@ import type {
11
11
  BSTNodeNested,
12
12
  BSTOptions,
13
13
  BTNCallback,
14
+ BTNEntry,
14
15
  BTNKeyOrNodeOrEntry,
15
- BTNPureKeyOrNodeOrEntry,
16
+ BTNPredicate,
16
17
  Comparator,
17
18
  CP,
18
19
  DFSOrderPattern,
19
20
  IterationType,
20
21
  OptBSTN
21
22
  } from '../../types';
22
- import { BTNEntry } from '../../types';
23
23
  import { BinaryTree, BinaryTreeNode } from './binary-tree';
24
24
  import { IBinaryTree } from '../../interfaces';
25
25
  import { Queue } from '../queue';
@@ -106,14 +106,14 @@ export class BST<
106
106
  {
107
107
  /**
108
108
  * This is the constructor function for a Binary Search Tree class in TypeScript.
109
- * @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
109
+ * @param keysOrNodesOrEntriesOrRaws - The `keysOrNodesOrEntriesOrRaws` parameter is an
110
110
  * iterable that can contain either keys, nodes, entries, or raw elements. These elements will be
111
111
  * added to the binary search tree during the construction of the object.
112
112
  * @param [options] - An optional object that contains additional options for the Binary Search Tree.
113
113
  * It can include a comparator function that defines the order of the elements in the tree.
114
114
  */
115
115
  constructor(
116
- keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
116
+ keysOrNodesOrEntriesOrRaws: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
117
117
  options?: BSTOptions<K, V, R>
118
118
  ) {
119
119
  super([], options);
@@ -123,7 +123,7 @@ export class BST<
123
123
  if (comparator) this._comparator = comparator;
124
124
  }
125
125
 
126
- if (keysOrNodesOrEntriesOrRawElements) this.addMany(keysOrNodesOrEntriesOrRawElements);
126
+ if (keysOrNodesOrEntriesOrRaws) this.addMany(keysOrNodesOrEntriesOrRaws);
127
127
  }
128
128
 
129
129
  protected override _root?: NODE = undefined;
@@ -155,17 +155,18 @@ export class BST<
155
155
  * following properties:
156
156
  * @returns a new instance of the BST class with the provided options.
157
157
  */
158
- override createTree(options?: Partial<BSTOptions<K, V, R>>): TREE {
158
+ override createTree(options?: BSTOptions<K, V, R>): TREE {
159
159
  return new BST<K, V, R, NODE, TREE>([], {
160
160
  iterationType: this.iterationType,
161
- comparator: this.comparator,
161
+ comparator: this._comparator,
162
+ toEntryFn: this._toEntryFn,
162
163
  ...options
163
164
  }) as TREE;
164
165
  }
165
166
 
166
167
  /**
167
168
  * The function overrides a method and converts a key, value pair or entry or raw element to a node.
168
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - A variable that can be of
169
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - A variable that can be of
169
170
  * type R or BTNKeyOrNodeOrEntry<K, V, NODE>. It represents either a key, a node, an entry, or a raw
170
171
  * element.
171
172
  * @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
@@ -173,10 +174,10 @@ export class BST<
173
174
  * @returns either a NODE object or undefined.
174
175
  */
175
176
  override keyValueOrEntryOrRawElementToNode(
176
- keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
177
+ keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
177
178
  value?: V
178
179
  ): OptBSTN<NODE> {
179
- return super.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value) ?? undefined;
180
+ return super.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw, value) ?? undefined;
180
181
  }
181
182
 
182
183
  /**
@@ -185,8 +186,8 @@ export class BST<
185
186
  *
186
187
  * The function ensures the existence of a node in a data structure and returns it, or undefined if
187
188
  * it doesn't exist.
188
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
189
- * `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, which represents the key, node,
189
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
190
+ * `keyOrNodeOrEntryOrRaw` can accept a value of type `R`, which represents the key, node,
190
191
  * entry, or raw element that needs to be ensured in the tree.
191
192
  * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
192
193
  * parameter that specifies the type of iteration to be used when ensuring a node. It has a default
@@ -195,25 +196,31 @@ export class BST<
195
196
  * not be ensured.
196
197
  */
197
198
  override ensureNode(
198
- keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
199
+ keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
199
200
  iterationType: IterationType = this.iterationType
200
201
  ): OptBSTN<NODE> {
201
- return super.ensureNode(keyOrNodeOrEntryOrRawElement, iterationType) ?? undefined;
202
+ return super.ensureNode(keyOrNodeOrEntryOrRaw, iterationType) ?? undefined;
202
203
  }
203
204
 
204
205
  /**
205
206
  * The function checks if the input is an instance of the BSTNode class.
206
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
207
- * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
208
- * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
207
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
208
+ * `keyOrNodeOrEntryOrRaw` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
209
+ * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRaw` is
209
210
  * an instance of the `BSTNode` class.
210
211
  */
211
- override isNode(
212
- keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>
213
- ): keyOrNodeOrEntryOrRawElement is NODE {
214
- return keyOrNodeOrEntryOrRawElement instanceof BSTNode;
212
+ override isNode(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): keyOrNodeOrEntryOrRaw is NODE {
213
+ return keyOrNodeOrEntryOrRaw instanceof BSTNode;
215
214
  }
216
215
 
216
+ /**
217
+ * The function "override isKey" checks if a key is comparable based on a given comparator.
218
+ * @param {any} key - The `key` parameter is a value that will be checked to determine if it is of
219
+ * type `K`.
220
+ * @returns The `override isKey(key: any): key is K` function is returning a boolean value based on
221
+ * the result of the `isComparable` function with the condition `this.comparator !==
222
+ * this._DEFAULT_COMPARATOR`.
223
+ */
217
224
  override isKey(key: any): key is K {
218
225
  return isComparable(key, this.comparator !== this._DEFAULT_COMPARATOR);
219
226
  }
@@ -223,23 +230,23 @@ export class BST<
223
230
  * Space Complexity: O(1)
224
231
  *
225
232
  * The `add` function in TypeScript adds a new node to a binary search tree based on the key value.
226
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
227
- * `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
233
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
234
+ * `keyOrNodeOrEntryOrRaw` can accept a value of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
228
235
  * @param {V} [value] - The `value` parameter is an optional value that can be associated with the
229
236
  * key in the binary search tree. If provided, it will be stored in the node along with the key.
230
237
  * @returns a boolean value.
231
238
  */
232
- override add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
233
- const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
239
+ override add(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R, value?: V): boolean {
240
+ const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw, value);
234
241
  if (newNode === undefined) return false;
235
242
 
236
- if (this.root === undefined) {
243
+ if (this._root === undefined) {
237
244
  this._setRoot(newNode);
238
245
  this._size++;
239
246
  return true;
240
247
  }
241
248
 
242
- let current = this.root;
249
+ let current = this._root;
243
250
  while (current !== undefined) {
244
251
  if (this.comparator(current.key, newNode.key) === 0) {
245
252
  this._replaceNode(current, newNode);
@@ -270,7 +277,7 @@ export class BST<
270
277
  *
271
278
  * The `addMany` function in TypeScript adds multiple keys or nodes to a data structure and returns
272
279
  * an array indicating whether each key or node was successfully inserted.
273
- * @param keysOrNodesOrEntriesOrRawElements - An iterable containing keys, nodes, entries, or raw
280
+ * @param keysOrNodesOrEntriesOrRaws - An iterable containing keys, nodes, entries, or raw
274
281
  * elements to be added to the data structure.
275
282
  * @param [values] - An optional iterable of values to be associated with the keys or nodes being
276
283
  * added. If provided, the values will be assigned to the corresponding keys or nodes in the same
@@ -286,7 +293,7 @@ export class BST<
286
293
  * successfully inserted into the data structure.
287
294
  */
288
295
  override addMany(
289
- keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>>,
296
+ keysOrNodesOrEntriesOrRaws: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>>,
290
297
  values?: Iterable<V | undefined>,
291
298
  isBalanceAdd = true,
292
299
  iterationType: IterationType = this.iterationType
@@ -300,43 +307,41 @@ export class BST<
300
307
  }
301
308
 
302
309
  if (!isBalanceAdd) {
303
- for (const kve of keysOrNodesOrEntriesOrRawElements) {
310
+ for (const kve of keysOrNodesOrEntriesOrRaws) {
304
311
  const value = valuesIterator?.next().value;
305
- const nn = this.add(kve, value);
306
- inserted.push(nn);
312
+ inserted.push(this.add(kve, value));
307
313
  }
308
314
  return inserted;
309
315
  }
310
316
 
311
- const realBTNExemplars: (R | BTNPureKeyOrNodeOrEntry<K, V, NODE>)[] = [];
312
-
313
- const isRealBTNExemplar = (
314
- kve: R | BTNKeyOrNodeOrEntry<K, V, NODE>
315
- ): kve is BTNPureKeyOrNodeOrEntry<K, V, NODE> => {
316
- if (kve === undefined || kve === null) return false;
317
- return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null));
318
- };
317
+ const realBTNExemplars: {
318
+ key: R | BTNKeyOrNodeOrEntry<K, V, NODE>;
319
+ value: V | undefined;
320
+ orgIndex: number;
321
+ }[] = [];
319
322
 
320
- for (const kve of keysOrNodesOrEntriesOrRawElements) {
321
- isRealBTNExemplar(kve) && realBTNExemplars.push(kve);
323
+ let i = 0;
324
+ for (const kve of keysOrNodesOrEntriesOrRaws) {
325
+ realBTNExemplars.push({ key: kve, value: valuesIterator?.next().value, orgIndex: i });
326
+ i++;
322
327
  }
323
328
 
324
- let sorted: (R | BTNPureKeyOrNodeOrEntry<K, V, NODE>)[] = [];
329
+ let sorted: { key: R | BTNKeyOrNodeOrEntry<K, V, NODE>; value: V | undefined; orgIndex: number }[] = [];
325
330
 
326
- sorted = realBTNExemplars.sort((a, b) => {
331
+ sorted = realBTNExemplars.sort(({ key: a }, { key: b }) => {
327
332
  let keyA: K | undefined | null, keyB: K | undefined | null;
328
333
  if (this.isEntry(a)) keyA = a[0];
329
334
  else if (this.isRealNode(a)) keyA = a.key;
330
- else if (this.toEntryFn) {
331
- keyA = this.toEntryFn(a as R)[0];
335
+ else if (this._toEntryFn) {
336
+ keyA = this._toEntryFn(a as R)[0];
332
337
  } else {
333
338
  keyA = a as K;
334
339
  }
335
340
 
336
341
  if (this.isEntry(b)) keyB = b[0];
337
342
  else if (this.isRealNode(b)) keyB = b.key;
338
- else if (this.toEntryFn) {
339
- keyB = this.toEntryFn(b as R)[0];
343
+ else if (this._toEntryFn) {
344
+ keyB = this._toEntryFn(b as R)[0];
340
345
  } else {
341
346
  keyB = b as K;
342
347
  }
@@ -347,12 +352,12 @@ export class BST<
347
352
  return 0;
348
353
  });
349
354
 
350
- const _dfs = (arr: (R | BTNPureKeyOrNodeOrEntry<K, V, NODE>)[]) => {
355
+ const _dfs = (arr: { key: R | BTNKeyOrNodeOrEntry<K, V, NODE>; value: V | undefined; orgIndex: number }[]) => {
351
356
  if (arr.length === 0) return;
352
357
 
353
358
  const mid = Math.floor((arr.length - 1) / 2);
354
- const newNode = this.add(arr[mid]);
355
- inserted.push(newNode);
359
+ const { key, value, orgIndex } = arr[mid];
360
+ inserted[orgIndex] = this.add(key, value);
356
361
  _dfs(arr.slice(0, mid));
357
362
  _dfs(arr.slice(mid + 1));
358
363
  };
@@ -366,8 +371,8 @@ export class BST<
366
371
  const [l, r] = popped;
367
372
  if (l <= r) {
368
373
  const m = l + Math.floor((r - l) / 2);
369
- const newNode = this.add(sorted[m]);
370
- inserted.push(newNode);
374
+ const { key, value, orgIndex } = sorted[m];
375
+ inserted[orgIndex] = this.add(key, value);
371
376
  stack.push([m + 1, r]);
372
377
  stack.push([l, m - 1]);
373
378
  }
@@ -388,54 +393,52 @@ export class BST<
388
393
  * Time Complexity: O(log n)
389
394
  * Space Complexity: O(k + log n)
390
395
  *
391
- * The `getNodes` function in TypeScript retrieves nodes from a binary tree based on a given
392
- * identifier and callback function.
393
- * @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
394
- * want to search for in the binary tree. It can be of any type that is returned by the callback
395
- * function.
396
- * @param {C} callback - The `callback` parameter is a function that takes a node as input and
397
- * returns a value. This value is used to identify the nodes that match the given identifier. The
398
- * `callback` function is optional and defaults to `this._DEFAULT_CALLBACK`.
399
- * @param [onlyOne=false] - A boolean value indicating whether to return only the first matching node
400
- * or all matching nodes. If set to true, only the first matching node will be returned. If set to
401
- * false, all matching nodes will be returned. The default value is false.
402
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
403
- * point for the search in the binary tree. It can be either a node object, a key-value pair, or an
404
- * entry object. If it is not provided, the `root` of the binary tree is used as the starting point.
405
- * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
406
- * iteration to be performed. It can have two possible values:
407
- * @returns The method `getNodes` returns an array of `NODE` objects.
396
+ * The function `getNodes` in TypeScript overrides the base class method to retrieve nodes based on a
397
+ * given predicate and iteration type.
398
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} predicate - The `predicate`
399
+ * parameter in the `getNodes` method is used to filter the nodes that will be returned. It can be a
400
+ * key, a node, an entry, or a custom predicate function that determines whether a node should be
401
+ * included in the result.
402
+ * @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` method is a boolean flag that
403
+ * determines whether to return only the first node that matches the predicate (`true`) or all nodes
404
+ * that match the predicate (`false`). If `onlyOne` is set to `true`, the method will stop iterating
405
+ * and
406
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
407
+ * `getNodes` method is used to specify the starting point for traversing the tree when searching for
408
+ * nodes that match a given predicate. It represents the root node of the subtree where the search
409
+ * should begin. If not explicitly provided, the default value for `begin
410
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` method
411
+ * specifies the type of iteration to be performed when traversing the nodes of a binary tree. It can
412
+ * have two possible values:
413
+ * @returns The `getNodes` method returns an array of nodes that satisfy the given predicate.
408
414
  */
409
- override getNodes<C extends BTNCallback<NODE>>(
410
- identifier: ReturnType<C> | undefined,
411
- callback: C = this._DEFAULT_CALLBACK as C,
415
+ override getNodes(
416
+ predicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>,
412
417
  onlyOne = false,
413
- beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
418
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
414
419
  iterationType: IterationType = this.iterationType
415
420
  ): NODE[] {
416
- if (identifier === undefined) return [];
417
- if (identifier === null) return [];
421
+ if (predicate === undefined) return [];
422
+ if (predicate === null) return [];
418
423
  beginRoot = this.ensureNode(beginRoot);
419
424
  if (!beginRoot) return [];
420
- callback = this._ensureCallback(identifier, callback);
425
+ const callback = this._ensurePredicate(predicate);
421
426
  const ans: NODE[] = [];
422
427
 
423
428
  if (iterationType === 'RECURSIVE') {
424
429
  const dfs = (cur: NODE) => {
425
- const callbackResult = callback(cur);
426
- if (callbackResult === identifier) {
430
+ if (callback(cur)) {
427
431
  ans.push(cur);
428
432
  if (onlyOne) return;
429
433
  }
430
434
 
431
435
  if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
432
- // TODO potential bug
433
- if (callback === this._DEFAULT_CALLBACK) {
434
- if (this.isRealNode(cur.left) && this.comparator(cur.key, identifier as K) > 0) dfs(cur.left);
435
- if (this.isRealNode(cur.right) && this.comparator(cur.key, identifier as K) < 0) dfs(cur.right);
436
+ if (this.isKey(predicate)) {
437
+ if (this.isRealNode(cur.left) && this.comparator(cur.key, predicate) > 0) dfs(cur.left);
438
+ if (this.isRealNode(cur.right) && this.comparator(cur.key, predicate) < 0) dfs(cur.right);
436
439
  } else {
437
- this.isRealNode(cur.left) && dfs(cur.left);
438
- this.isRealNode(cur.right) && dfs(cur.right);
440
+ if (this.isRealNode(cur.left)) dfs(cur.left);
441
+ if (this.isRealNode(cur.right)) dfs(cur.right);
439
442
  }
440
443
  };
441
444
 
@@ -444,26 +447,16 @@ export class BST<
444
447
  const stack = [beginRoot];
445
448
  while (stack.length > 0) {
446
449
  const cur = stack.pop()!;
447
- const callbackResult = callback(cur);
448
- if (callbackResult === identifier) {
450
+ if (callback(cur)) {
449
451
  ans.push(cur);
450
452
  if (onlyOne) return ans;
451
453
  }
452
- // TODO potential bug
453
- if (callback === this._DEFAULT_CALLBACK) {
454
- if (this.isRealNode(cur.right) && this.comparator(cur.key, identifier as K) < 0) stack.push(cur.right);
455
- if (this.isRealNode(cur.left) && this.comparator(cur.key, identifier as K) > 0) stack.push(cur.left);
456
-
457
- // if (this.isRealNode(cur.right) && this._lt(cur.key, identifier as K)) stack.push(cur.right);
458
- // if (this.isRealNode(cur.left) && this._gt(cur.key, identifier as K)) stack.push(cur.left);
459
-
460
- // // @ts-ignore
461
- // if (this.isRealNode(cur.right) && cur.key > identifier) stack.push(cur.right);
462
- // // @ts-ignore
463
- // if (this.isRealNode(cur.left) && cur.key < identifier) stack.push(cur.left);
454
+ if (this.isKey(predicate)) {
455
+ if (this.isRealNode(cur.right) && this.comparator(cur.key, predicate) < 0) stack.push(cur.right);
456
+ if (this.isRealNode(cur.left) && this.comparator(cur.key, predicate) > 0) stack.push(cur.left);
464
457
  } else {
465
- this.isRealNode(cur.right) && stack.push(cur.right);
466
- this.isRealNode(cur.left) && stack.push(cur.left);
458
+ if (this.isRealNode(cur.right)) stack.push(cur.right);
459
+ if (this.isRealNode(cur.left)) stack.push(cur.left);
467
460
  }
468
461
  }
469
462
  }
@@ -475,30 +468,28 @@ export class BST<
475
468
  * Time Complexity: O(log n)
476
469
  * Space Complexity: O(1)
477
470
  *
478
- * The function `getNode` returns the first node that matches the given identifier and callback
479
- * function in a binary search tree.
480
- * @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
481
- * want to search for in the binary search tree. It can be of any type that is compatible with the
482
- * type returned by the callback function.
483
- * @param {C} callback - The `callback` parameter is a function that will be used to determine if a
484
- * node matches the desired criteria. It should be a function that takes a node as an argument and
485
- * returns a boolean value indicating whether the node matches the criteria or not. If no callback is
486
- * provided, the default callback will be
487
- * @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
488
- * search tree. It can be either a key or a node. If it is a key, the search will start from the node
489
- * with that key. If it is a node, the search will start from that node.
490
- * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
491
- * of iteration to be performed when searching for nodes in the binary search tree. It can have one
492
- * of the following values:
493
- * @returns The method is returning a NODE object or undefined.
471
+ * This function retrieves a node based on a given predicate within a binary search tree structure.
472
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} predicate - The `predicate`
473
+ * parameter can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>`, `R`, or `BTNPredicate<NODE>`.
474
+ * @param {R | BSTNKeyOrNode<K, NODE>} beginRoot - The `beginRoot` parameter in the `getNode` method
475
+ * is used to specify the starting point for searching nodes in the binary search tree. If no
476
+ * specific starting point is provided, the default value is set to `this._root`, which is the root
477
+ * node of the binary search tree.
478
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `getNode` method is a
479
+ * parameter that specifies the type of iteration to be used. It has a default value of
480
+ * `this.iterationType`, which means it will use the iteration type defined in the class instance if
481
+ * no value is provided when calling the method.
482
+ * @returns The `getNode` method is returning an optional binary search tree node (`OptBSTN<NODE>`).
483
+ * It is using the `getNodes` method to find the node based on the provided predicate, beginning at
484
+ * the specified root node (`beginRoot`) and using the specified iteration type. The method then
485
+ * returns the first node found or `undefined` if no node is found.
494
486
  */
495
- override getNode<C extends BTNCallback<NODE>>(
496
- identifier: ReturnType<C> | undefined,
497
- callback: C = this._DEFAULT_CALLBACK as C,
498
- beginRoot: R | BSTNKeyOrNode<K, NODE> = this.root,
487
+ override getNode(
488
+ predicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>,
489
+ beginRoot: R | BSTNKeyOrNode<K, NODE> = this._root,
499
490
  iterationType: IterationType = this.iterationType
500
491
  ): OptBSTN<NODE> {
501
- return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
492
+ return this.getNodes(predicate, true, beginRoot, iterationType)[0] ?? undefined;
502
493
  }
503
494
 
504
495
  /**
@@ -515,7 +506,7 @@ export class BST<
515
506
  * @returns The method is returning a NODE object or undefined.
516
507
  */
517
508
  override getNodeByKey(key: K, iterationType: IterationType = this.iterationType): OptBSTN<NODE> {
518
- return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
509
+ return this.getNode(key, this._root, iterationType);
519
510
  }
520
511
 
521
512
  /**
@@ -526,11 +517,11 @@ export class BST<
526
517
  * the callback function.
527
518
  * @param {C} callback - The `callback` parameter is a function that will be called for each node
528
519
  * during the depth-first search traversal. It is an optional parameter and defaults to
529
- * `this._DEFAULT_CALLBACK`. The type `C` represents the type of the callback function.
520
+ * `this._DEFAULT_BTN_CALLBACK`. The type `C` represents the type of the callback function.
530
521
  * @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
531
522
  * order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
532
523
  * take one of the following values:
533
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
524
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter is the starting
534
525
  * point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
535
526
  * node entry. If not specified, the default value is the root of the tree.
536
527
  * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
@@ -539,9 +530,9 @@ export class BST<
539
530
  * @returns The method is returning an array of the return type of the callback function.
540
531
  */
541
532
  override dfs<C extends BTNCallback<NODE>>(
542
- callback: C = this._DEFAULT_CALLBACK as C,
533
+ callback: C = this._DEFAULT_BTN_CALLBACK as C,
543
534
  pattern: DFSOrderPattern = 'IN',
544
- beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
535
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
545
536
  iterationType: IterationType = this.iterationType
546
537
  ): ReturnType<C>[] {
547
538
  return super.dfs(callback, pattern, beginRoot, iterationType);
@@ -556,7 +547,7 @@ export class BST<
556
547
  * @param {C} callback - The `callback` parameter is a function that will be called for each node
557
548
  * visited during the breadth-first search. It should take a single argument, which is the current
558
549
  * node being visited, and it can return a value of any type.
559
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
550
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter is the starting
560
551
  * point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
561
552
  * object. If no value is provided, the default value is the root of the tree.
562
553
  * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
@@ -565,8 +556,8 @@ export class BST<
565
556
  * @returns an array of the return type of the callback function.
566
557
  */
567
558
  override bfs<C extends BTNCallback<NODE>>(
568
- callback: C = this._DEFAULT_CALLBACK as C,
569
- beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
559
+ callback: C = this._DEFAULT_BTN_CALLBACK as C,
560
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
570
561
  iterationType: IterationType = this.iterationType
571
562
  ): ReturnType<C>[] {
572
563
  return super.bfs(callback, beginRoot, iterationType, false);
@@ -581,7 +572,7 @@ export class BST<
581
572
  * @param {C} callback - The `callback` parameter is a generic type `C` that extends
582
573
  * `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the
583
574
  * tree during the iteration process.
584
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
575
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter is the starting
585
576
  * point for listing the levels of the binary tree. It can be either a root node of the tree, a
586
577
  * key-value pair representing a node in the tree, or a key representing a node in the tree. If no
587
578
  * value is provided, the root of
@@ -591,8 +582,8 @@ export class BST<
591
582
  * function.
592
583
  */
593
584
  override listLevels<C extends BTNCallback<NODE>>(
594
- callback: C = this._DEFAULT_CALLBACK as C,
595
- beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
585
+ callback: C = this._DEFAULT_BTN_CALLBACK as C,
586
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
596
587
  iterationType: IterationType = this.iterationType
597
588
  ): ReturnType<C>[][] {
598
589
  return super.listLevels(callback, beginRoot, iterationType, false);
@@ -610,7 +601,7 @@ export class BST<
610
601
  * @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
611
602
  * traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
612
603
  * 0, or 1, where:
613
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} targetNode - The `targetNode` parameter is the node in
604
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} targetNode - The `targetNode` parameter is the node in
614
605
  * the binary tree that you want to start traversing from. It can be specified either by providing
615
606
  * the key of the node, the node itself, or an entry containing the key and value of the node. If no
616
607
  * `targetNode` is provided,
@@ -620,15 +611,15 @@ export class BST<
620
611
  * `ReturnType<C>`, which is the return type of the callback function passed as an argument.
621
612
  */
622
613
  lesserOrGreaterTraverse<C extends BTNCallback<NODE>>(
623
- callback: C = this._DEFAULT_CALLBACK as C,
614
+ callback: C = this._DEFAULT_BTN_CALLBACK as C,
624
615
  lesserOrGreater: CP = -1,
625
- targetNode: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
616
+ targetNode: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
626
617
  iterationType: IterationType = this.iterationType
627
618
  ): ReturnType<C>[] {
628
619
  const targetNodeEnsured = this.ensureNode(targetNode);
629
620
  const ans: ReturnType<BTNCallback<NODE>>[] = [];
621
+ if (!this._root) return ans;
630
622
  if (!targetNodeEnsured) return ans;
631
- if (!this.root) return ans;
632
623
 
633
624
  const targetKey = targetNodeEnsured.key;
634
625
 
@@ -641,10 +632,10 @@ export class BST<
641
632
  if (this.isRealNode(cur.right)) dfs(cur.right);
642
633
  };
643
634
 
644
- dfs(this.root);
635
+ dfs(this._root);
645
636
  return ans;
646
637
  } else {
647
- const queue = new Queue<NODE>([this.root]);
638
+ const queue = new Queue<NODE>([this._root]);
648
639
  while (queue.size > 0) {
649
640
  const cur = queue.shift();
650
641
  if (this.isRealNode(cur)) {
@@ -721,7 +712,7 @@ export class BST<
721
712
  * @returns a boolean value.
722
713
  */
723
714
  isAVLBalanced(iterationType: IterationType = this.iterationType): boolean {
724
- if (!this.root) return true;
715
+ if (!this._root) return true;
725
716
 
726
717
  let balanced = true;
727
718
 
@@ -733,10 +724,10 @@ export class BST<
733
724
  if (Math.abs(leftHeight - rightHeight) > 1) balanced = false;
734
725
  return Math.max(leftHeight, rightHeight) + 1;
735
726
  };
736
- _height(this.root);
727
+ _height(this._root);
737
728
  } else {
738
729
  const stack: NODE[] = [];
739
- let node: OptBSTN<NODE> = this.root,
730
+ let node: OptBSTN<NODE> = this._root,
740
731
  last: OptBSTN<NODE> = undefined;
741
732
  const depths: Map<NODE, number> = new Map();
742
733
 
@@ -749,8 +740,8 @@ export class BST<
749
740
  if (!node.right || last === node.right) {
750
741
  node = stack.pop();
751
742
  if (node) {
752
- const left = node.left ? (depths.get(node.left) ?? -1) : -1;
753
- const right = node.right ? (depths.get(node.right) ?? -1) : -1;
743
+ const left = node.left ? depths.get(node.left)! : -1;
744
+ const right = node.right ? depths.get(node.right)! : -1;
754
745
  if (Math.abs(left - right) > 1) return false;
755
746
  depths.set(node, 1 + Math.max(left, right));
756
747
  last = node;