directed-graph-typed 1.53.6 → 1.53.7

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 (44) hide show
  1. package/dist/common/index.d.ts +12 -0
  2. package/dist/common/index.js +23 -0
  3. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +7 -10
  4. package/dist/data-structures/binary-tree/avl-tree.js +2 -2
  5. package/dist/data-structures/binary-tree/binary-tree.d.ts +54 -19
  6. package/dist/data-structures/binary-tree/binary-tree.js +100 -66
  7. package/dist/data-structures/binary-tree/bst.d.ts +100 -36
  8. package/dist/data-structures/binary-tree/bst.js +185 -66
  9. package/dist/data-structures/binary-tree/rb-tree.d.ts +4 -0
  10. package/dist/data-structures/binary-tree/rb-tree.js +6 -2
  11. package/dist/data-structures/binary-tree/tree-multi-map.js +2 -2
  12. package/dist/data-structures/heap/heap.d.ts +6 -6
  13. package/dist/data-structures/heap/heap.js +6 -6
  14. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +18 -8
  15. package/dist/data-structures/linked-list/doubly-linked-list.js +24 -10
  16. package/dist/data-structures/linked-list/singly-linked-list.d.ts +1 -1
  17. package/dist/data-structures/linked-list/singly-linked-list.js +1 -1
  18. package/dist/data-structures/trie/trie.d.ts +104 -4
  19. package/dist/data-structures/trie/trie.js +116 -12
  20. package/dist/index.d.ts +2 -1
  21. package/dist/index.js +2 -1
  22. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  23. package/dist/types/data-structures/binary-tree/bst.d.ts +3 -2
  24. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
  25. package/dist/types/utils/utils.d.ts +10 -6
  26. package/dist/utils/utils.js +4 -2
  27. package/package.json +2 -2
  28. package/src/common/index.ts +19 -0
  29. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +7 -9
  30. package/src/data-structures/binary-tree/avl-tree.ts +3 -2
  31. package/src/data-structures/binary-tree/binary-tree.ts +108 -64
  32. package/src/data-structures/binary-tree/bst.ts +190 -69
  33. package/src/data-structures/binary-tree/rb-tree.ts +6 -2
  34. package/src/data-structures/binary-tree/tree-multi-map.ts +3 -3
  35. package/src/data-structures/heap/heap.ts +39 -39
  36. package/src/data-structures/linked-list/doubly-linked-list.ts +111 -97
  37. package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
  38. package/src/data-structures/trie/trie.ts +116 -11
  39. package/src/index.ts +2 -1
  40. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  41. package/src/types/data-structures/binary-tree/bst.ts +3 -2
  42. package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
  43. package/src/types/utils/utils.ts +16 -10
  44. package/src/utils/utils.ts +4 -2
@@ -1,10 +1,11 @@
1
1
  import { BST, BSTNode } from '../../../data-structures';
2
2
  import type { BinaryTreeOptions } from './binary-tree';
3
- import { Comparator } from '../../common';
3
+ import { Comparable } from '../../utils';
4
4
  export type BSTNodeNested<K, V> = BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
5
5
  export type BSTNested<K, V, R, NODE extends BSTNode<K, V, NODE>> = BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
6
6
  export type BSTOptions<K, V, R> = BinaryTreeOptions<K, V, R> & {
7
- comparator?: Comparator<K>;
7
+ extractComparable?: (key: K) => Comparable;
8
+ isReverse?: boolean;
8
9
  };
9
10
  export type BSTNOptKey<K> = K | undefined;
10
11
  export type OptNode<NODE> = NODE | undefined;
@@ -3,4 +3,4 @@ import type { BSTOptions } from "./bst";
3
3
  export type RBTNColor = 'RED' | 'BLACK';
4
4
  export type RedBlackTreeNodeNested<K, V> = RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
5
5
  export type RedBlackTreeNested<K, V, R, NODE extends RedBlackTreeNode<K, V, NODE>> = RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
6
- export type RBTreeOptions<K, V, R> = BSTOptions<K, V, R> & {};
6
+ export type RBTreeOptions<K, V, R> = Omit<BSTOptions<K, V, R>, 'isReverse'> & {};
@@ -6,13 +6,17 @@ export type TrlFn<A extends any[] = any[], R = any> = (...args: A) => R;
6
6
  export type TrlAsyncFn = (...args: any[]) => any;
7
7
  export type SpecifyOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
8
8
  export type Any = string | number | bigint | boolean | symbol | undefined | object;
9
+ export type Arithmetic = number | bigint;
9
10
  export type ComparablePrimitive = number | bigint | string | boolean;
10
- export type ComparableObject = {
11
- [key in string]: any;
12
- } & ({
13
- valueOf: () => ComparablePrimitive | ComparableObject;
11
+ export interface BaseComparableObject {
12
+ [key: string]: unknown;
13
+ }
14
+ export interface ValueComparableObject extends BaseComparableObject {
15
+ valueOf: () => ComparablePrimitive | ValueComparableObject;
14
16
  toString?: () => string;
15
- } | {
17
+ }
18
+ export interface StringComparableObject extends BaseComparableObject {
16
19
  toString: () => string;
17
- });
20
+ }
21
+ export type ComparableObject = ValueComparableObject | StringComparableObject;
18
22
  export type Comparable = ComparablePrimitive | Date | ComparableObject;
@@ -213,7 +213,8 @@ exports.roundFixed = roundFixed;
213
213
  function isPrimitiveComparable(value) {
214
214
  const valueType = typeof value;
215
215
  if (valueType === 'number')
216
- return !Number.isNaN(value);
216
+ return true;
217
+ // if (valueType === 'number') return !Number.isNaN(value);
217
218
  return valueType === 'bigint' || valueType === 'string' || valueType === 'boolean';
218
219
  }
219
220
  /**
@@ -265,7 +266,8 @@ function isComparable(value, isForceObjectComparable = false) {
265
266
  if (typeof value !== 'object')
266
267
  return false;
267
268
  if (value instanceof Date)
268
- return !Number.isNaN(value.getTime());
269
+ return true;
270
+ // if (value instanceof Date) return !Number.isNaN(value.getTime());
269
271
  if (isForceObjectComparable)
270
272
  return true;
271
273
  const comparableValue = tryObjectToPrimitive(value);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "directed-graph-typed",
3
- "version": "1.53.6",
3
+ "version": "1.53.7",
4
4
  "description": "Directed Graph. Javascript & Typescript Data Structure.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -147,6 +147,6 @@
147
147
  "typescript": "^4.9.5"
148
148
  },
149
149
  "dependencies": {
150
- "data-structure-typed": "^1.53.6"
150
+ "data-structure-typed": "^1.53.7"
151
151
  }
152
152
  }
@@ -0,0 +1,19 @@
1
+ export enum DFSOperation {
2
+ VISIT = 0,
3
+ PROCESS = 1
4
+ }
5
+ export class Range<K> {
6
+ constructor(
7
+ public low: K,
8
+ public high: K,
9
+ public includeLow: boolean = true,
10
+ public includeHigh: boolean = true
11
+ ) {}
12
+
13
+ // Determine whether a key is within the range
14
+ isInRange(key: K, comparator: (a: K, b: K) => number): boolean {
15
+ const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
16
+ const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
17
+ return lowCheck && highCheck;
18
+ }
19
+ }
@@ -143,8 +143,9 @@ export class AVLTreeMultiMap<
143
143
  return new AVLTreeMultiMap<K, V, R, NODE, TREE>([], {
144
144
  iterationType: this.iterationType,
145
145
  isMapMode: this._isMapMode,
146
- comparator: this._comparator,
146
+ extractComparable: this._extractComparable,
147
147
  toEntryFn: this._toEntryFn,
148
+ isReverse: this._isReverse,
148
149
  ...options
149
150
  }) as TREE;
150
151
  }
@@ -187,17 +188,14 @@ export class AVLTreeMultiMap<
187
188
  return [this.createNode(key, finalValue, count), finalValue];
188
189
  }
189
190
 
190
- if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value, count), value];
191
-
192
191
  if (this.isRaw(keyNodeEntryOrRaw)) {
193
- if (this._toEntryFn) {
194
- const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw as R);
195
- const finalValue = value ?? entryValue;
196
- if (this.isKey(key)) return [this.createNode(key, finalValue, count), finalValue];
197
- }
198
- return [undefined, undefined];
192
+ const [key, entryValue] = this._toEntryFn!(keyNodeEntryOrRaw);
193
+ const finalValue = value ?? entryValue;
194
+ if (this.isKey(key)) return [this.createNode(key, finalValue, count), finalValue];
199
195
  }
200
196
 
197
+ if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value, count), value];
198
+
201
199
  return [undefined, undefined];
202
200
  }
203
201
 
@@ -113,8 +113,9 @@ export class AVLTree<
113
113
  return new AVLTree<K, V, R, NODE, TREE>([], {
114
114
  iterationType: this.iterationType,
115
115
  isMapMode: this._isMapMode,
116
- comparator: this._comparator,
116
+ extractComparable: this._extractComparable,
117
117
  toEntryFn: this._toEntryFn,
118
+ isReverse: this._isReverse,
118
119
  ...options
119
120
  }) as TREE;
120
121
  }
@@ -434,7 +435,7 @@ export class AVLTree<
434
435
  */
435
436
  protected _balancePath(node: BTNRep<K, V, NODE> | R): void {
436
437
  node = this.ensureNode(node);
437
- const path = this.getPathToRoot(node => node, node, false); // first O(log n) + O(log n)
438
+ const path = this.getPathToRoot(node, node => node, false); // first O(log n) + O(log n)
438
439
  for (let i = 0; i < path.length; i++) {
439
440
  // second O(log n)
440
441
  const A = path[i];
@@ -29,7 +29,7 @@ import { IBinaryTree } from '../../interfaces';
29
29
  import { isComparable, trampoline } from '../../utils';
30
30
  import { Queue } from '../queue';
31
31
  import { IterableEntryBase } from '../base';
32
- import { DFSOperation } from '../../constants';
32
+ import { DFSOperation, Range } from '../../common';
33
33
 
34
34
  /**
35
35
  * Represents a node in a binary tree.
@@ -233,17 +233,14 @@ export class BinaryTree<
233
233
  return [this.createNode(key, finalValue), finalValue];
234
234
  }
235
235
 
236
- if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value), value];
237
-
238
236
  if (this.isRaw(keyNodeEntryOrRaw)) {
239
- if (this._toEntryFn) {
240
- const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
241
- const finalValue = value ?? entryValue;
242
- if (this.isKey(key)) return [this.createNode(key, finalValue), finalValue];
243
- }
244
- return [undefined, undefined];
237
+ const [key, entryValue] = this._toEntryFn!(keyNodeEntryOrRaw);
238
+ const finalValue = value ?? entryValue;
239
+ if (this.isKey(key)) return [this.createNode(key, finalValue), finalValue];
245
240
  }
246
241
 
242
+ if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value), value];
243
+
247
244
  return [undefined, undefined];
248
245
  }
249
246
 
@@ -275,15 +272,15 @@ export class BinaryTree<
275
272
  const key = keyNodeEntryOrRaw[0];
276
273
  if (key === null) return null;
277
274
  if (key === undefined) return;
278
- return this.getNodeByKey(key, iterationType);
275
+ return this.getNode(key, this._root, iterationType);
279
276
  }
280
277
 
281
278
  if (this._toEntryFn) {
282
279
  const [key] = this._toEntryFn(keyNodeEntryOrRaw as R);
283
- if (this.isKey(key)) return this.getNodeByKey(key);
280
+ if (this.isKey(key)) return this.getNode(key);
284
281
  }
285
282
 
286
- if (this.isKey(keyNodeEntryOrRaw)) return this.getNodeByKey(keyNodeEntryOrRaw, iterationType);
283
+ if (this.isKey(keyNodeEntryOrRaw)) return this.getNode(keyNodeEntryOrRaw, this._root, iterationType);
287
284
  return;
288
285
  }
289
286
 
@@ -302,8 +299,15 @@ export class BinaryTree<
302
299
  return keyNodeEntryOrRaw instanceof BinaryTreeNode;
303
300
  }
304
301
 
302
+ /**
303
+ * The function `isRaw` checks if the input parameter is of type `R` by verifying if it is an object.
304
+ * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - BTNRep<K, V, NODE> | R
305
+ * @returns The function `isRaw` is checking if the `keyNodeEntryOrRaw` parameter is of type `R` by
306
+ * checking if it is an object. If the parameter is an object, the function will return `true`,
307
+ * indicating that it is of type `R`.
308
+ */
305
309
  isRaw(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is R {
306
- return typeof keyNodeEntryOrRaw === 'object';
310
+ return this._toEntryFn !== undefined && typeof keyNodeEntryOrRaw === 'object';
307
311
  }
308
312
 
309
313
  /**
@@ -345,6 +349,12 @@ export class BinaryTree<
345
349
  return keyNodeEntryOrRaw === this._NIL;
346
350
  }
347
351
 
352
+ isRange(
353
+ keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE> | Range<K>
354
+ ): keyNodeEntryRawOrPredicate is Range<K> {
355
+ return keyNodeEntryRawOrPredicate instanceof Range;
356
+ }
357
+
348
358
  /**
349
359
  * The function determines whether a given key, node, entry, or raw data is a leaf node in a binary
350
360
  * tree.
@@ -508,6 +518,18 @@ export class BinaryTree<
508
518
  return inserted;
509
519
  }
510
520
 
521
+ /**
522
+ * Time Complexity: O(k * n)
523
+ * Space Complexity: O(1)
524
+ *
525
+ * The `merge` function in TypeScript merges another binary tree into the current tree by adding all
526
+ * elements from the other tree.
527
+ * @param anotherTree - `BinaryTree<K, V, R, NODE, TREE>`
528
+ */
529
+ merge(anotherTree: BinaryTree<K, V, R, NODE, TREE>) {
530
+ this.addMany(anotherTree, []);
531
+ }
532
+
511
533
  /**
512
534
  * Time Complexity: O(k * n)
513
535
  * Space Complexity: O(1)
@@ -589,41 +611,45 @@ export class BinaryTree<
589
611
  * Time Complexity: O(n)
590
612
  * Space Complexity: O(k + log n)
591
613
  *
592
- * The function `getNodes` retrieves nodes from a binary tree based on a key, node, entry, raw data,
593
- * or predicate, with options for recursive or iterative traversal.
594
- * @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate
595
- * - The `getNodes` function you provided takes several parameters:
596
- * @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` function is a boolean flag that
597
- * determines whether to return only the first node that matches the criteria specified by the
598
- * `keyNodeEntryRawOrPredicate` parameter. If `onlyOne` is set to `true`, the function will
599
- * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
600
- * `getNodes` function is used to specify the starting point for traversing the binary tree. It
601
- * represents the root node of the binary tree or the node from which the traversal should begin. If
602
- * not provided, the default value is set to `this._root
603
- * @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` function
604
- * determines the type of iteration to be performed when traversing the nodes of a binary tree. It
605
- * can have two possible values:
606
- * @returns The `getNodes` function returns an array of nodes that satisfy the provided condition
607
- * based on the input parameters and the iteration type specified.
614
+ * The `search` function in TypeScript performs a depth-first or breadth-first search on a tree
615
+ * structure based on a given predicate or key, with options to return multiple results or just one.
616
+ * @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The
617
+ * `keyNodeEntryRawOrPredicate` parameter in the `search` function can accept three types of values:
618
+ * @param [onlyOne=false] - The `onlyOne` parameter in the `search` function is a boolean flag that
619
+ * determines whether the search should stop after finding the first matching node. If `onlyOne` is
620
+ * set to `true`, the search will return as soon as a matching node is found. If `onlyOne` is
621
+ * @param {C} callback - The `callback` parameter in the `search` function is a callback function
622
+ * that will be called on each node that matches the search criteria. It is of type `C`, which
623
+ * extends `NodeCallback<NODE>`. The default value for `callback` is `this._DEFAULT_NODE_CALLBACK` if
624
+ * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `search` function is
625
+ * used to specify the node from which the search operation should begin. It represents the starting
626
+ * point in the binary tree where the search will be performed. If no specific `startNode` is
627
+ * provided, the search operation will start from the root
628
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `search` function
629
+ * specifies the type of iteration to be used when searching for nodes in a binary tree. It can have
630
+ * two possible values:
631
+ * @returns The `search` function returns an array of values that match the provided criteria based
632
+ * on the search algorithm implemented within the function.
608
633
  */
609
- getNodes(
634
+ search<C extends NodeCallback<NODE>>(
610
635
  keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE>,
611
636
  onlyOne = false,
637
+ callback: C = this._DEFAULT_NODE_CALLBACK as C,
612
638
  startNode: BTNRep<K, V, NODE> | R = this._root,
613
639
  iterationType: IterationType = this.iterationType
614
- ): NODE[] {
640
+ ): ReturnType<C>[] {
615
641
  if (keyNodeEntryRawOrPredicate === undefined) return [];
616
642
  if (keyNodeEntryRawOrPredicate === null) return [];
617
643
  startNode = this.ensureNode(startNode);
618
644
  if (!startNode) return [];
619
- const callback = this._ensurePredicate(keyNodeEntryRawOrPredicate);
645
+ const predicate = this._ensurePredicate(keyNodeEntryRawOrPredicate);
620
646
 
621
- const ans: NODE[] = [];
647
+ const ans: ReturnType<C>[] = [];
622
648
 
623
649
  if (iterationType === 'RECURSIVE') {
624
650
  const dfs = (cur: NODE) => {
625
- if (callback(cur)) {
626
- ans.push(cur);
651
+ if (predicate(cur)) {
652
+ ans.push(callback(cur));
627
653
  if (onlyOne) return;
628
654
  }
629
655
  if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
@@ -637,8 +663,8 @@ export class BinaryTree<
637
663
  while (stack.length > 0) {
638
664
  const cur = stack.pop();
639
665
  if (this.isRealNode(cur)) {
640
- if (callback(cur)) {
641
- ans.push(cur);
666
+ if (predicate(cur)) {
667
+ ans.push(callback(cur));
642
668
  if (onlyOne) return ans;
643
669
  }
644
670
  if (this.isRealNode(cur.left)) stack.push(cur.left);
@@ -650,6 +676,36 @@ export class BinaryTree<
650
676
  return ans;
651
677
  }
652
678
 
679
+ /**
680
+ * Time Complexity: O(n)
681
+ * Space Complexity: O(k + log n)
682
+ *
683
+ * The function `getNodes` retrieves nodes from a binary tree based on a key, node, entry, raw data,
684
+ * or predicate, with options for recursive or iterative traversal.
685
+ * @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate
686
+ * - The `getNodes` function you provided takes several parameters:
687
+ * @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` function is a boolean flag that
688
+ * determines whether to return only the first node that matches the criteria specified by the
689
+ * `keyNodeEntryRawOrPredicate` parameter. If `onlyOne` is set to `true`, the function will
690
+ * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
691
+ * `getNodes` function is used to specify the starting point for traversing the binary tree. It
692
+ * represents the root node of the binary tree or the node from which the traversal should begin. If
693
+ * not provided, the default value is set to `this._root
694
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` function
695
+ * determines the type of iteration to be performed when traversing the nodes of a binary tree. It
696
+ * can have two possible values:
697
+ * @returns The `getNodes` function returns an array of nodes that satisfy the provided condition
698
+ * based on the input parameters and the iteration type specified.
699
+ */
700
+ getNodes(
701
+ keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE>,
702
+ onlyOne = false,
703
+ startNode: BTNRep<K, V, NODE> | R = this._root,
704
+ iterationType: IterationType = this.iterationType
705
+ ): NODE[] {
706
+ return this.search(keyNodeEntryRawOrPredicate, onlyOne, node => node, startNode, iterationType);
707
+ }
708
+
653
709
  /**
654
710
  * Time Complexity: O(n)
655
711
  * Space Complexity: O(log n).
@@ -675,24 +731,7 @@ export class BinaryTree<
675
731
  startNode: BTNRep<K, V, NODE> | R = this._root,
676
732
  iterationType: IterationType = this.iterationType
677
733
  ): OptNodeOrNull<NODE> {
678
- return this.getNodes(keyNodeEntryRawOrPredicate, true, startNode, iterationType)[0] ?? null;
679
- }
680
-
681
- /**
682
- * Time Complexity: O(n)
683
- * Space Complexity: O(log n)
684
- *
685
- * The function `getNodeByKey` retrieves a node by its key from a binary tree structure.
686
- * @param {K} key - The `key` parameter is the value used to search for a specific node in a data
687
- * structure.
688
- * @param {IterationType} iterationType - The `iterationType` parameter is a type of iteration that
689
- * specifies how the tree nodes should be traversed when searching for a node with the given key. It
690
- * is an optional parameter with a default value of `this.iterationType`.
691
- * @returns The `getNodeByKey` function is returning an optional binary tree node
692
- * (`OptNodeOrNull<NODE>`).
693
- */
694
- getNodeByKey(key: K, iterationType: IterationType = this.iterationType): OptNodeOrNull<NODE> {
695
- return this.getNode(key, this._root, iterationType);
734
+ return this.search(keyNodeEntryRawOrPredicate, true, node => node, startNode, iterationType)[0] ?? null;
696
735
  }
697
736
 
698
737
  /**
@@ -723,7 +762,7 @@ export class BinaryTree<
723
762
  iterationType: IterationType = this.iterationType
724
763
  ): V | undefined {
725
764
  if (this._isMapMode) {
726
- const key = this._getKey(keyNodeEntryRawOrPredicate);
765
+ const key = this._extractKey(keyNodeEntryRawOrPredicate);
727
766
  if (key === null || key === undefined) return;
728
767
  return this._store.get(key);
729
768
  }
@@ -756,7 +795,7 @@ export class BinaryTree<
756
795
  startNode: BTNRep<K, V, NODE> | R = this._root,
757
796
  iterationType: IterationType = this.iterationType
758
797
  ): boolean {
759
- return this.getNodes(keyNodeEntryRawOrPredicate, true, startNode, iterationType).length > 0;
798
+ return this.search(keyNodeEntryRawOrPredicate, true, node => node, startNode, iterationType).length > 0;
760
799
  }
761
800
 
762
801
  /**
@@ -1023,9 +1062,9 @@ export class BinaryTree<
1023
1062
  * parameter.
1024
1063
  */
1025
1064
  getPathToRoot<C extends NodeCallback<OptNodeOrNull<NODE>>>(
1026
- callback: C = this._DEFAULT_NODE_CALLBACK as C,
1027
1065
  beginNode: BTNRep<K, V, NODE> | R,
1028
- isReverse = true
1066
+ callback: C = this._DEFAULT_NODE_CALLBACK as C,
1067
+ isReverse = false
1029
1068
  ): ReturnType<C>[] {
1030
1069
  const result: ReturnType<C>[] = [];
1031
1070
  let beginNodeEnsured = this.ensureNode(beginNode);
@@ -1118,7 +1157,6 @@ export class BinaryTree<
1118
1157
  iterationType: IterationType = this.iterationType
1119
1158
  ): ReturnType<C> {
1120
1159
  if (this.isNIL(startNode)) return callback(undefined);
1121
- // TODO support get right most by passing key in
1122
1160
  startNode = this.ensureNode(startNode);
1123
1161
  if (!startNode) return callback(startNode);
1124
1162
 
@@ -2144,16 +2182,16 @@ export class BinaryTree<
2144
2182
  * Time Complexity: O(1)
2145
2183
  * Space Complexity: O(1)
2146
2184
  *
2147
- * The function `_getKey` in TypeScript returns the key from a given input, which can be a node,
2185
+ * The function `_extractKey` in TypeScript returns the key from a given input, which can be a node,
2148
2186
  * entry, raw data, or null/undefined.
2149
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `_getKey` method you provided is a
2187
+ * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `_extractKey` method you provided is a
2150
2188
  * TypeScript method that takes in a parameter `keyNodeEntryOrRaw` of type `BTNRep<K, V, NODE> | R`,
2151
2189
  * where `BTNRep` is a generic type with keys `K`, `V`, and `NODE`, and `
2152
- * @returns The `_getKey` method returns the key value extracted from the `keyNodeEntryOrRaw`
2190
+ * @returns The `_extractKey` method returns the key value extracted from the `keyNodeEntryOrRaw`
2153
2191
  * parameter. The return value can be a key value of type `K`, `null`, or `undefined`, depending on
2154
2192
  * the conditions checked in the method.
2155
2193
  */
2156
- protected _getKey(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): K | null | undefined {
2194
+ protected _extractKey(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): K | null | undefined {
2157
2195
  if (keyNodeEntryOrRaw === null) return null;
2158
2196
  if (keyNodeEntryOrRaw === undefined) return;
2159
2197
  if (keyNodeEntryOrRaw === this._NIL) return;
@@ -2193,6 +2231,9 @@ export class BinaryTree<
2193
2231
  }
2194
2232
 
2195
2233
  /**
2234
+ * Time Complexity: O(1)
2235
+ * Space Complexity: O(1)
2236
+ *
2196
2237
  * The _clearNodes function sets the root node to undefined and resets the size to 0.
2197
2238
  */
2198
2239
  protected _clearNodes() {
@@ -2201,6 +2242,9 @@ export class BinaryTree<
2201
2242
  }
2202
2243
 
2203
2244
  /**
2245
+ * Time Complexity: O(1)
2246
+ * Space Complexity: O(1)
2247
+ *
2204
2248
  * The _clearValues function clears all values stored in the _store object.
2205
2249
  */
2206
2250
  protected _clearValues() {