heap-typed 1.53.6 → 1.53.8

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 (62) hide show
  1. package/README.md +6 -6
  2. package/dist/common/index.d.ts +12 -0
  3. package/dist/common/index.js +28 -0
  4. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
  5. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +9 -12
  6. package/dist/data-structures/binary-tree/avl-tree.js +2 -2
  7. package/dist/data-structures/binary-tree/binary-tree.d.ts +55 -20
  8. package/dist/data-structures/binary-tree/binary-tree.js +102 -68
  9. package/dist/data-structures/binary-tree/bst.d.ts +131 -37
  10. package/dist/data-structures/binary-tree/bst.js +222 -69
  11. package/dist/data-structures/binary-tree/index.d.ts +1 -1
  12. package/dist/data-structures/binary-tree/index.js +1 -1
  13. package/dist/data-structures/binary-tree/{rb-tree.d.ts → red-black-tree.d.ts} +53 -0
  14. package/dist/data-structures/binary-tree/{rb-tree.js → red-black-tree.js} +56 -3
  15. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
  16. package/dist/data-structures/binary-tree/tree-multi-map.js +7 -7
  17. package/dist/data-structures/hash/hash-map.d.ts +30 -0
  18. package/dist/data-structures/hash/hash-map.js +30 -0
  19. package/dist/data-structures/heap/heap.d.ts +26 -9
  20. package/dist/data-structures/heap/heap.js +37 -17
  21. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +54 -9
  22. package/dist/data-structures/linked-list/doubly-linked-list.js +80 -19
  23. package/dist/data-structures/linked-list/singly-linked-list.d.ts +35 -2
  24. package/dist/data-structures/linked-list/singly-linked-list.js +55 -11
  25. package/dist/data-structures/queue/deque.d.ts +37 -8
  26. package/dist/data-structures/queue/deque.js +73 -29
  27. package/dist/data-structures/queue/queue.d.ts +41 -1
  28. package/dist/data-structures/queue/queue.js +51 -9
  29. package/dist/data-structures/stack/stack.d.ts +27 -10
  30. package/dist/data-structures/stack/stack.js +39 -20
  31. package/dist/data-structures/trie/trie.d.ts +111 -6
  32. package/dist/data-structures/trie/trie.js +123 -14
  33. package/dist/index.d.ts +2 -1
  34. package/dist/index.js +2 -1
  35. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  36. package/dist/types/data-structures/binary-tree/bst.d.ts +3 -2
  37. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
  38. package/dist/types/utils/utils.d.ts +10 -6
  39. package/dist/utils/utils.js +4 -2
  40. package/package.json +2 -2
  41. package/src/common/index.ts +25 -0
  42. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +9 -11
  43. package/src/data-structures/binary-tree/avl-tree.ts +3 -2
  44. package/src/data-structures/binary-tree/binary-tree.ts +110 -66
  45. package/src/data-structures/binary-tree/bst.ts +232 -72
  46. package/src/data-structures/binary-tree/index.ts +1 -1
  47. package/src/data-structures/binary-tree/{rb-tree.ts → red-black-tree.ts} +56 -3
  48. package/src/data-structures/binary-tree/tree-multi-map.ts +6 -6
  49. package/src/data-structures/hash/hash-map.ts +30 -0
  50. package/src/data-structures/heap/heap.ts +72 -49
  51. package/src/data-structures/linked-list/doubly-linked-list.ts +173 -105
  52. package/src/data-structures/linked-list/singly-linked-list.ts +61 -11
  53. package/src/data-structures/queue/deque.ts +72 -28
  54. package/src/data-structures/queue/queue.ts +50 -7
  55. package/src/data-structures/stack/stack.ts +39 -20
  56. package/src/data-structures/trie/trie.ts +123 -13
  57. package/src/index.ts +2 -1
  58. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  59. package/src/types/data-structures/binary-tree/bst.ts +3 -2
  60. package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
  61. package/src/types/utils/utils.ts +16 -10
  62. package/src/utils/utils.ts +4 -2
@@ -11,7 +11,7 @@ exports.BinaryTree = exports.BinaryTreeNode = void 0;
11
11
  const utils_1 = require("../../utils");
12
12
  const queue_1 = require("../queue");
13
13
  const base_1 = require("../base");
14
- const constants_1 = require("../../constants");
14
+ const common_1 = require("../../common");
15
15
  /**
16
16
  * Represents a node in a binary tree.
17
17
  * @template V - The type of data stored in the node.
@@ -150,7 +150,7 @@ class BinaryTree extends base_1.IterableEntryBase {
150
150
  * input parameter (`keyNodeEntryOrRaw`) and processes it accordingly to return a node or null
151
151
  * value.
152
152
  */
153
- keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value) {
153
+ _keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value) {
154
154
  if (keyNodeEntryOrRaw === undefined)
155
155
  return [undefined, undefined];
156
156
  if (keyNodeEntryOrRaw === null)
@@ -166,17 +166,14 @@ class BinaryTree extends base_1.IterableEntryBase {
166
166
  const finalValue = value !== null && value !== void 0 ? value : entryValue;
167
167
  return [this.createNode(key, finalValue), finalValue];
168
168
  }
169
- if (this.isKey(keyNodeEntryOrRaw))
170
- return [this.createNode(keyNodeEntryOrRaw, value), value];
171
169
  if (this.isRaw(keyNodeEntryOrRaw)) {
172
- if (this._toEntryFn) {
173
- const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
174
- const finalValue = value !== null && value !== void 0 ? value : entryValue;
175
- if (this.isKey(key))
176
- return [this.createNode(key, finalValue), finalValue];
177
- }
178
- return [undefined, undefined];
170
+ const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
171
+ const finalValue = value !== null && value !== void 0 ? value : entryValue;
172
+ if (this.isKey(key))
173
+ return [this.createNode(key, finalValue), finalValue];
179
174
  }
175
+ if (this.isKey(keyNodeEntryOrRaw))
176
+ return [this.createNode(keyNodeEntryOrRaw, value), value];
180
177
  return [undefined, undefined];
181
178
  }
182
179
  /**
@@ -209,15 +206,15 @@ class BinaryTree extends base_1.IterableEntryBase {
209
206
  return null;
210
207
  if (key === undefined)
211
208
  return;
212
- return this.getNodeByKey(key, iterationType);
209
+ return this.getNode(key, this._root, iterationType);
213
210
  }
214
211
  if (this._toEntryFn) {
215
212
  const [key] = this._toEntryFn(keyNodeEntryOrRaw);
216
213
  if (this.isKey(key))
217
- return this.getNodeByKey(key);
214
+ return this.getNode(key);
218
215
  }
219
216
  if (this.isKey(keyNodeEntryOrRaw))
220
- return this.getNodeByKey(keyNodeEntryOrRaw, iterationType);
217
+ return this.getNode(keyNodeEntryOrRaw, this._root, iterationType);
221
218
  return;
222
219
  }
223
220
  /**
@@ -234,8 +231,15 @@ class BinaryTree extends base_1.IterableEntryBase {
234
231
  isNode(keyNodeEntryOrRaw) {
235
232
  return keyNodeEntryOrRaw instanceof BinaryTreeNode;
236
233
  }
234
+ /**
235
+ * The function `isRaw` checks if the input parameter is of type `R` by verifying if it is an object.
236
+ * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - BTNRep<K, V, NODE> | R
237
+ * @returns The function `isRaw` is checking if the `keyNodeEntryOrRaw` parameter is of type `R` by
238
+ * checking if it is an object. If the parameter is an object, the function will return `true`,
239
+ * indicating that it is of type `R`.
240
+ */
237
241
  isRaw(keyNodeEntryOrRaw) {
238
- return typeof keyNodeEntryOrRaw === 'object';
242
+ return this._toEntryFn !== undefined && typeof keyNodeEntryOrRaw === 'object';
239
243
  }
240
244
  /**
241
245
  * The function `isRealNode` checks if a given input is a valid node in a binary tree.
@@ -274,6 +278,9 @@ class BinaryTree extends base_1.IterableEntryBase {
274
278
  isNIL(keyNodeEntryOrRaw) {
275
279
  return keyNodeEntryOrRaw === this._NIL;
276
280
  }
281
+ isRange(keyNodeEntryRawOrPredicate) {
282
+ return keyNodeEntryRawOrPredicate instanceof common_1.Range;
283
+ }
277
284
  /**
278
285
  * The function determines whether a given key, node, entry, or raw data is a leaf node in a binary
279
286
  * tree.
@@ -339,7 +346,7 @@ class BinaryTree extends base_1.IterableEntryBase {
339
346
  * key was found and the node was replaced instead of inserted.
340
347
  */
341
348
  add(keyNodeEntryOrRaw, value) {
342
- const [newNode, newValue] = this.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
349
+ const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
343
350
  if (newNode === undefined)
344
351
  return false;
345
352
  // If the tree is empty, directly set the new node as the root node
@@ -429,6 +436,17 @@ class BinaryTree extends base_1.IterableEntryBase {
429
436
  }
430
437
  return inserted;
431
438
  }
439
+ /**
440
+ * Time Complexity: O(k * n)
441
+ * Space Complexity: O(1)
442
+ *
443
+ * The `merge` function in TypeScript merges another binary tree into the current tree by adding all
444
+ * elements from the other tree.
445
+ * @param anotherTree - `BinaryTree<K, V, R, NODE, TREE>`
446
+ */
447
+ merge(anotherTree) {
448
+ this.addMany(anotherTree, []);
449
+ }
432
450
  /**
433
451
  * Time Complexity: O(k * n)
434
452
  * Space Complexity: O(1)
@@ -511,24 +529,27 @@ class BinaryTree extends base_1.IterableEntryBase {
511
529
  * Time Complexity: O(n)
512
530
  * Space Complexity: O(k + log n)
513
531
  *
514
- * The function `getNodes` retrieves nodes from a binary tree based on a key, node, entry, raw data,
515
- * or predicate, with options for recursive or iterative traversal.
516
- * @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate
517
- * - The `getNodes` function you provided takes several parameters:
518
- * @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` function is a boolean flag that
519
- * determines whether to return only the first node that matches the criteria specified by the
520
- * `keyNodeEntryRawOrPredicate` parameter. If `onlyOne` is set to `true`, the function will
521
- * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
522
- * `getNodes` function is used to specify the starting point for traversing the binary tree. It
523
- * represents the root node of the binary tree or the node from which the traversal should begin. If
524
- * not provided, the default value is set to `this._root
525
- * @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` function
526
- * determines the type of iteration to be performed when traversing the nodes of a binary tree. It
527
- * can have two possible values:
528
- * @returns The `getNodes` function returns an array of nodes that satisfy the provided condition
529
- * based on the input parameters and the iteration type specified.
532
+ * The `search` function in TypeScript performs a depth-first or breadth-first search on a tree
533
+ * structure based on a given predicate or key, with options to return multiple results or just one.
534
+ * @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The
535
+ * `keyNodeEntryRawOrPredicate` parameter in the `search` function can accept three types of values:
536
+ * @param [onlyOne=false] - The `onlyOne` parameter in the `search` function is a boolean flag that
537
+ * determines whether the search should stop after finding the first matching node. If `onlyOne` is
538
+ * set to `true`, the search will return as soon as a matching node is found. If `onlyOne` is
539
+ * @param {C} callback - The `callback` parameter in the `search` function is a callback function
540
+ * that will be called on each node that matches the search criteria. It is of type `C`, which
541
+ * extends `NodeCallback<NODE>`. The default value for `callback` is `this._DEFAULT_NODE_CALLBACK` if
542
+ * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `search` function is
543
+ * used to specify the node from which the search operation should begin. It represents the starting
544
+ * point in the binary tree where the search will be performed. If no specific `startNode` is
545
+ * provided, the search operation will start from the root
546
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `search` function
547
+ * specifies the type of iteration to be used when searching for nodes in a binary tree. It can have
548
+ * two possible values:
549
+ * @returns The `search` function returns an array of values that match the provided criteria based
550
+ * on the search algorithm implemented within the function.
530
551
  */
531
- getNodes(keyNodeEntryRawOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
552
+ search(keyNodeEntryRawOrPredicate, onlyOne = false, callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
532
553
  if (keyNodeEntryRawOrPredicate === undefined)
533
554
  return [];
534
555
  if (keyNodeEntryRawOrPredicate === null)
@@ -536,12 +557,12 @@ class BinaryTree extends base_1.IterableEntryBase {
536
557
  startNode = this.ensureNode(startNode);
537
558
  if (!startNode)
538
559
  return [];
539
- const callback = this._ensurePredicate(keyNodeEntryRawOrPredicate);
560
+ const predicate = this._ensurePredicate(keyNodeEntryRawOrPredicate);
540
561
  const ans = [];
541
562
  if (iterationType === 'RECURSIVE') {
542
563
  const dfs = (cur) => {
543
- if (callback(cur)) {
544
- ans.push(cur);
564
+ if (predicate(cur)) {
565
+ ans.push(callback(cur));
545
566
  if (onlyOne)
546
567
  return;
547
568
  }
@@ -559,8 +580,8 @@ class BinaryTree extends base_1.IterableEntryBase {
559
580
  while (stack.length > 0) {
560
581
  const cur = stack.pop();
561
582
  if (this.isRealNode(cur)) {
562
- if (callback(cur)) {
563
- ans.push(cur);
583
+ if (predicate(cur)) {
584
+ ans.push(callback(cur));
564
585
  if (onlyOne)
565
586
  return ans;
566
587
  }
@@ -573,6 +594,30 @@ class BinaryTree extends base_1.IterableEntryBase {
573
594
  }
574
595
  return ans;
575
596
  }
597
+ /**
598
+ * Time Complexity: O(n)
599
+ * Space Complexity: O(k + log n)
600
+ *
601
+ * The function `getNodes` retrieves nodes from a binary tree based on a key, node, entry, raw data,
602
+ * or predicate, with options for recursive or iterative traversal.
603
+ * @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate
604
+ * - The `getNodes` function you provided takes several parameters:
605
+ * @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` function is a boolean flag that
606
+ * determines whether to return only the first node that matches the criteria specified by the
607
+ * `keyNodeEntryRawOrPredicate` parameter. If `onlyOne` is set to `true`, the function will
608
+ * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
609
+ * `getNodes` function is used to specify the starting point for traversing the binary tree. It
610
+ * represents the root node of the binary tree or the node from which the traversal should begin. If
611
+ * not provided, the default value is set to `this._root
612
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` function
613
+ * determines the type of iteration to be performed when traversing the nodes of a binary tree. It
614
+ * can have two possible values:
615
+ * @returns The `getNodes` function returns an array of nodes that satisfy the provided condition
616
+ * based on the input parameters and the iteration type specified.
617
+ */
618
+ getNodes(keyNodeEntryRawOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
619
+ return this.search(keyNodeEntryRawOrPredicate, onlyOne, node => node, startNode, iterationType);
620
+ }
576
621
  /**
577
622
  * Time Complexity: O(n)
578
623
  * Space Complexity: O(log n).
@@ -595,23 +640,7 @@ class BinaryTree extends base_1.IterableEntryBase {
595
640
  */
596
641
  getNode(keyNodeEntryRawOrPredicate, startNode = this._root, iterationType = this.iterationType) {
597
642
  var _a;
598
- return (_a = this.getNodes(keyNodeEntryRawOrPredicate, true, startNode, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
599
- }
600
- /**
601
- * Time Complexity: O(n)
602
- * Space Complexity: O(log n)
603
- *
604
- * The function `getNodeByKey` retrieves a node by its key from a binary tree structure.
605
- * @param {K} key - The `key` parameter is the value used to search for a specific node in a data
606
- * structure.
607
- * @param {IterationType} iterationType - The `iterationType` parameter is a type of iteration that
608
- * specifies how the tree nodes should be traversed when searching for a node with the given key. It
609
- * is an optional parameter with a default value of `this.iterationType`.
610
- * @returns The `getNodeByKey` function is returning an optional binary tree node
611
- * (`OptNodeOrNull<NODE>`).
612
- */
613
- getNodeByKey(key, iterationType = this.iterationType) {
614
- return this.getNode(key, this._root, iterationType);
643
+ return (_a = this.search(keyNodeEntryRawOrPredicate, true, node => node, startNode, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
615
644
  }
616
645
  /**
617
646
  * Time Complexity: O(n)
@@ -638,7 +667,7 @@ class BinaryTree extends base_1.IterableEntryBase {
638
667
  get(keyNodeEntryRawOrPredicate, startNode = this._root, iterationType = this.iterationType) {
639
668
  var _a;
640
669
  if (this._isMapMode) {
641
- const key = this._getKey(keyNodeEntryRawOrPredicate);
670
+ const key = this._extractKey(keyNodeEntryRawOrPredicate);
642
671
  if (key === null || key === undefined)
643
672
  return;
644
673
  return this._store.get(key);
@@ -667,7 +696,7 @@ class BinaryTree extends base_1.IterableEntryBase {
667
696
  * Otherwise, it returns `false`.
668
697
  */
669
698
  has(keyNodeEntryRawOrPredicate, startNode = this._root, iterationType = this.iterationType) {
670
- return this.getNodes(keyNodeEntryRawOrPredicate, true, startNode, iterationType).length > 0;
699
+ return this.search(keyNodeEntryRawOrPredicate, true, node => node, startNode, iterationType).length > 0;
671
700
  }
672
701
  /**
673
702
  * Time Complexity: O(1)
@@ -926,7 +955,7 @@ class BinaryTree extends base_1.IterableEntryBase {
926
955
  * array is either in reverse order or in the original order based on the value of the `isReverse`
927
956
  * parameter.
928
957
  */
929
- getPathToRoot(callback = this._DEFAULT_NODE_CALLBACK, beginNode, isReverse = true) {
958
+ getPathToRoot(beginNode, callback = this._DEFAULT_NODE_CALLBACK, isReverse = false) {
930
959
  const result = [];
931
960
  let beginNodeEnsured = this.ensureNode(beginNode);
932
961
  if (!beginNodeEnsured)
@@ -1009,7 +1038,6 @@ class BinaryTree extends base_1.IterableEntryBase {
1009
1038
  getRightMost(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
1010
1039
  if (this.isNIL(startNode))
1011
1040
  return callback(undefined);
1012
- // TODO support get right most by passing key in
1013
1041
  startNode = this.ensureNode(startNode);
1014
1042
  if (!startNode)
1015
1043
  return callback(startNode);
@@ -1655,20 +1683,20 @@ class BinaryTree extends base_1.IterableEntryBase {
1655
1683
  dfs(startNode);
1656
1684
  }
1657
1685
  else {
1658
- const stack = [{ opt: constants_1.DFSOperation.VISIT, node: startNode }];
1686
+ const stack = [{ opt: common_1.DFSOperation.VISIT, node: startNode }];
1659
1687
  const pushLeft = (cur) => {
1660
1688
  var _a;
1661
1689
  if (shouldVisitLeft(cur.node))
1662
- stack.push({ opt: constants_1.DFSOperation.VISIT, node: (_a = cur.node) === null || _a === void 0 ? void 0 : _a.left });
1690
+ stack.push({ opt: common_1.DFSOperation.VISIT, node: (_a = cur.node) === null || _a === void 0 ? void 0 : _a.left });
1663
1691
  };
1664
1692
  const pushRight = (cur) => {
1665
1693
  var _a;
1666
1694
  if (shouldVisitRight(cur.node))
1667
- stack.push({ opt: constants_1.DFSOperation.VISIT, node: (_a = cur.node) === null || _a === void 0 ? void 0 : _a.right });
1695
+ stack.push({ opt: common_1.DFSOperation.VISIT, node: (_a = cur.node) === null || _a === void 0 ? void 0 : _a.right });
1668
1696
  };
1669
1697
  const pushRoot = (cur) => {
1670
1698
  if (shouldVisitRoot(cur.node))
1671
- stack.push({ opt: constants_1.DFSOperation.PROCESS, node: cur.node });
1699
+ stack.push({ opt: common_1.DFSOperation.PROCESS, node: cur.node });
1672
1700
  };
1673
1701
  while (stack.length > 0) {
1674
1702
  const cur = stack.pop();
@@ -1676,7 +1704,7 @@ class BinaryTree extends base_1.IterableEntryBase {
1676
1704
  continue;
1677
1705
  if (!shouldVisitRoot(cur.node))
1678
1706
  continue;
1679
- if (cur.opt === constants_1.DFSOperation.PROCESS) {
1707
+ if (cur.opt === common_1.DFSOperation.PROCESS) {
1680
1708
  if (shouldProcessRoot(cur.node))
1681
1709
  ans.push(callback(cur.node));
1682
1710
  }
@@ -1948,16 +1976,16 @@ class BinaryTree extends base_1.IterableEntryBase {
1948
1976
  * Time Complexity: O(1)
1949
1977
  * Space Complexity: O(1)
1950
1978
  *
1951
- * The function `_getKey` in TypeScript returns the key from a given input, which can be a node,
1979
+ * The function `_extractKey` in TypeScript returns the key from a given input, which can be a node,
1952
1980
  * entry, raw data, or null/undefined.
1953
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `_getKey` method you provided is a
1981
+ * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `_extractKey` method you provided is a
1954
1982
  * TypeScript method that takes in a parameter `keyNodeEntryOrRaw` of type `BTNRep<K, V, NODE> | R`,
1955
1983
  * where `BTNRep` is a generic type with keys `K`, `V`, and `NODE`, and `
1956
- * @returns The `_getKey` method returns the key value extracted from the `keyNodeEntryOrRaw`
1984
+ * @returns The `_extractKey` method returns the key value extracted from the `keyNodeEntryOrRaw`
1957
1985
  * parameter. The return value can be a key value of type `K`, `null`, or `undefined`, depending on
1958
1986
  * the conditions checked in the method.
1959
1987
  */
1960
- _getKey(keyNodeEntryOrRaw) {
1988
+ _extractKey(keyNodeEntryOrRaw) {
1961
1989
  if (keyNodeEntryOrRaw === null)
1962
1990
  return null;
1963
1991
  if (keyNodeEntryOrRaw === undefined)
@@ -1999,6 +2027,9 @@ class BinaryTree extends base_1.IterableEntryBase {
1999
2027
  return this._store.set(key, value);
2000
2028
  }
2001
2029
  /**
2030
+ * Time Complexity: O(1)
2031
+ * Space Complexity: O(1)
2032
+ *
2002
2033
  * The _clearNodes function sets the root node to undefined and resets the size to 0.
2003
2034
  */
2004
2035
  _clearNodes() {
@@ -2006,6 +2037,9 @@ class BinaryTree extends base_1.IterableEntryBase {
2006
2037
  this._size = 0;
2007
2038
  }
2008
2039
  /**
2040
+ * Time Complexity: O(1)
2041
+ * Space Complexity: O(1)
2042
+ *
2009
2043
  * The _clearValues function clears all values stored in the _store object.
2010
2044
  */
2011
2045
  _clearValues() {
@@ -5,9 +5,10 @@
5
5
  * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { BSTNested, BSTNodeNested, BSTNOptKeyOrNode, BSTOptions, BTNRep, Comparator, CP, DFSOrderPattern, IterationType, NodeCallback, NodePredicate, OptNode } from '../../types';
8
+ import type { BSTNested, BSTNodeNested, BSTNOptKeyOrNode, BSTOptions, BTNRep, Comparable, Comparator, CP, DFSOrderPattern, IterationType, NodeCallback, NodePredicate, OptNode } from '../../types';
9
9
  import { BinaryTree, BinaryTreeNode } from './binary-tree';
10
10
  import { IBinaryTree } from '../../interfaces';
11
+ import { Range } from '../../common';
11
12
  export declare class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNodeNested<K, V>> extends BinaryTreeNode<K, V, NODE> {
12
13
  parent?: NODE;
13
14
  constructor(key: K, value?: V);
@@ -45,6 +46,62 @@ export declare class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE>
45
46
  * 5. Logarithmic Operations: Ideal operations like insertion, deletion, and searching are O(log n) time-efficient.
46
47
  * 6. Balance Variability: Can become unbalanced; special types maintain balance.
47
48
  * 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
49
+ * @example
50
+ * // Merge 3 sorted datasets
51
+ * const dataset1 = new BST<number, string>([
52
+ * [1, 'A'],
53
+ * [7, 'G']
54
+ * ]);
55
+ * const dataset2 = [
56
+ * [2, 'B'],
57
+ * [6, 'F']
58
+ * ];
59
+ * const dataset3 = new BST<number, string>([
60
+ * [3, 'C'],
61
+ * [5, 'E'],
62
+ * [4, 'D']
63
+ * ]);
64
+ *
65
+ * // Merge datasets into a single BinarySearchTree
66
+ * const merged = new BST<number, string>(dataset1);
67
+ * merged.addMany(dataset2);
68
+ * merged.merge(dataset3);
69
+ *
70
+ * // Verify merged dataset is in sorted order
71
+ * console.log([...merged.values()]); // ['A', 'B', 'C', 'D', 'E', 'F', 'G']
72
+ * @example
73
+ * // Find elements in a range
74
+ * const bst = new BST<number>([10, 5, 15, 3, 7, 12, 18]);
75
+ * console.log(bst.search(new Range(5, 10))); // [10, 5, 7]
76
+ * console.log(bst.rangeSearch([4, 12], node => node.key.toString())); // ['10', '12', '5', '7']
77
+ * console.log(bst.search(new Range(4, 12, true, false))); // [10, 5, 7]
78
+ * console.log(bst.rangeSearch([15, 20])); // [15, 18]
79
+ * console.log(bst.search(new Range(15, 20, false))); // [18]
80
+ * @example
81
+ * // Find lowest common ancestor
82
+ * const bst = new BST<number>([20, 10, 30, 5, 15, 25, 35, 3, 7, 12, 18]);
83
+ *
84
+ * // LCA helper function
85
+ * const findLCA = (num1: number, num2: number): number | undefined => {
86
+ * const path1 = bst.getPathToRoot(num1);
87
+ * const path2 = bst.getPathToRoot(num2);
88
+ * // Find the first common ancestor
89
+ * return findFirstCommon(path1, path2);
90
+ * };
91
+ *
92
+ * function findFirstCommon(arr1: number[], arr2: number[]): number | undefined {
93
+ * for (const num of arr1) {
94
+ * if (arr2.indexOf(num) !== -1) {
95
+ * return num;
96
+ * }
97
+ * }
98
+ * return undefined;
99
+ * }
100
+ *
101
+ * // Assertions
102
+ * console.log(findLCA(3, 10)); // 7
103
+ * console.log(findLCA(5, 35)); // 15
104
+ * console.log(findLCA(20, 30)); // 25
48
105
  */
49
106
  export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V, NODE> = BSTNode<K, V, BSTNodeNested<K, V>>, TREE extends BST<K, V, R, NODE, TREE> = BST<K, V, R, NODE, BSTNested<K, V, R, NODE>>> extends BinaryTree<K, V, R, NODE, TREE> implements IBinaryTree<K, V, R, NODE, TREE> {
50
107
  /**
@@ -62,6 +119,13 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
62
119
  * @returns The `_root` property of the object, which is of type `NODE` or `undefined`.
63
120
  */
64
121
  get root(): OptNode<NODE>;
122
+ protected _isReverse: boolean;
123
+ /**
124
+ * The above function is a getter method in TypeScript that returns the value of the private property
125
+ * `_isReverse`.
126
+ * @returns The `isReverse` property of the object, which is a boolean value.
127
+ */
128
+ get isReverse(): boolean;
65
129
  /**
66
130
  * The function creates a new BSTNode with the given key and value and returns it.
67
131
  * @param {K} key - The key parameter is of type K, which represents the type of the key for the node
@@ -88,7 +152,7 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
88
152
  * value associated with a key in a key-value pair.
89
153
  * @returns either a NODE object or undefined.
90
154
  */
91
- keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): [OptNode<NODE>, V | undefined];
155
+ protected _keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): [OptNode<NODE>, V | undefined];
92
156
  /**
93
157
  * Time Complexity: O(log n)
94
158
  * Space Complexity: O(log n)
@@ -118,7 +182,7 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
118
182
  * @param {any} key - The `key` parameter is a value that will be checked to determine if it is of
119
183
  * type `K`.
120
184
  * @returns The `override isKey(key: any): key is K` function is returning a boolean value based on
121
- * the result of the `isComparable` function with the condition `this.comparator !==
185
+ * the result of the `isComparable` function with the condition `this._compare !==
122
186
  * this._DEFAULT_COMPARATOR`.
123
187
  */
124
188
  isKey(key: any): key is K;
@@ -156,30 +220,67 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
156
220
  * successfully inserted into the data structure.
157
221
  */
158
222
  addMany(keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, NODE>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): boolean[];
223
+ /**
224
+ * Time Complexity: O(n)
225
+ * Space Complexity: O(1)
226
+ *
227
+ * The `merge` function overrides the base class method by adding elements from another
228
+ * binary search tree.
229
+ * @param anotherTree - `anotherTree` is an instance of a Binary Search Tree (BST) with key type `K`,
230
+ * value type `V`, return type `R`, node type `NODE`, and tree type `TREE`.
231
+ */
232
+ merge(anotherTree: BST<K, V, R, NODE, TREE>): void;
159
233
  /**
160
234
  * Time Complexity: O(log n)
161
235
  * Space Complexity: O(k + log n)
162
236
  *
163
- * The function `getNodes` in TypeScript overrides the base class method to retrieve nodes based on a
164
- * given keyNodeEntryRawOrPredicate and iteration type.
165
- * @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The `keyNodeEntryRawOrPredicate`
166
- * parameter in the `getNodes` method is used to filter the nodes that will be returned. It can be a
167
- * key, a node, an entry, or a custom keyNodeEntryRawOrPredicate function that determines whether a node should be
168
- * included in the result.
169
- * @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` method is a boolean flag that
170
- * determines whether to return only the first node that matches the keyNodeEntryRawOrPredicate (`true`) or all nodes
171
- * that match the keyNodeEntryRawOrPredicate (`false`). If `onlyOne` is set to `true`, the method will stop iterating
172
- * and
173
- * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
174
- * `getNodes` method is used to specify the starting point for traversing the tree when searching for
175
- * nodes that match a given keyNodeEntryRawOrPredicate. It represents the root node of the subtree where the search
176
- * should begin. If not explicitly provided, the default value for `begin
177
- * @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` method
178
- * specifies the type of iteration to be performed when traversing the nodes of a binary tree. It can
179
- * have two possible values:
180
- * @returns The `getNodes` method returns an array of nodes that satisfy the given keyNodeEntryRawOrPredicate.
237
+ * The function `search` in TypeScript overrides the search behavior in a binary tree structure based
238
+ * on specified criteria.
239
+ * @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The
240
+ * `keyNodeEntryRawOrPredicate` parameter in the `override search` method can accept one of the
241
+ * following types:
242
+ * @param [onlyOne=false] - The `onlyOne` parameter is a boolean flag that determines whether the
243
+ * search should stop after finding the first matching node. If `onlyOne` is set to `true`, the
244
+ * search will return as soon as a matching node is found. If `onlyOne` is set to `false`, the
245
+ * @param {C} callback - The `callback` parameter in the `override search` function is a function
246
+ * that will be called on each node that matches the search criteria. It is of type `C`, which
247
+ * extends `NodeCallback<NODE>`. The callback function should accept a node of type `NODE` as its
248
+ * argument and
249
+ * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `override search`
250
+ * method represents the node from which the search operation will begin. It is the starting point
251
+ * for searching within the tree data structure. The method ensures that the `startNode` is a valid
252
+ * node before proceeding with the search operation. If the `
253
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `override search`
254
+ * function determines the type of iteration to be used during the search operation. It can have two
255
+ * possible values:
256
+ * @returns The `override search` method returns an array of values that match the search criteria
257
+ * specified by the input parameters. The method performs a search operation on a binary tree
258
+ * structure based on the provided key, predicate, and other options. The search results are
259
+ * collected in an array and returned as the output of the method.
181
260
  */
182
- getNodes(keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE>, onlyOne?: boolean, startNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType): NODE[];
261
+ search<C extends NodeCallback<NODE>>(keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE> | Range<K>, onlyOne?: boolean, callback?: C, startNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType): ReturnType<C>[];
262
+ /**
263
+ * Time Complexity: O(log n)
264
+ * Space Complexity: O(n)
265
+ *
266
+ * The `rangeSearch` function searches for nodes within a specified range in a binary search tree.
267
+ * @param {Range<K> | [K, K]} range - The `range` parameter in the `rangeSearch` function can be
268
+ * either a `Range` object or an array of two elements representing the range boundaries.
269
+ * @param {C} callback - The `callback` parameter in the `rangeSearch` function is a callback
270
+ * function that is used to process each node that is found within the specified range during the
271
+ * search operation. It is of type `NodeCallback<NODE>`, where `NODE` is the type of nodes in the
272
+ * data structure.
273
+ * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `rangeSearch`
274
+ * function represents the node from which the search for nodes within the specified range will
275
+ * begin. It is the starting point for the range search operation.
276
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `rangeSearch` function
277
+ * is used to specify the type of iteration to be performed during the search operation. It has a
278
+ * default value of `this.iterationType`, which suggests that it is likely a property of the class or
279
+ * object that the `rangeSearch`
280
+ * @returns The `rangeSearch` function is returning the result of calling the `search` method with
281
+ * the specified parameters.
282
+ */
283
+ rangeSearch<C extends NodeCallback<NODE>>(range: Range<K> | [K, K], callback?: C, startNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType): ReturnType<C>[];
183
284
  /**
184
285
  * Time Complexity: O(log n)
185
286
  * Space Complexity: O(1)
@@ -201,20 +302,6 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
201
302
  * returns the first node found or `undefined` if no node is found.
202
303
  */
203
304
  getNode(keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE>, startNode?: R | BSTNOptKeyOrNode<K, NODE>, iterationType?: IterationType): OptNode<NODE>;
204
- /**
205
- * Time Complexity: O(log n)
206
- * Space Complexity: O(1)
207
- *
208
- * The function `getNodeByKey` returns a node with a specific key from a tree data structure.
209
- * @param {K} key - The key parameter is the value used to search for a specific node in the tree. It
210
- * is typically a unique identifier or a value that can be used to determine the position of the node
211
- * in the tree structure.
212
- * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
213
- * parameter that specifies the type of iteration to be used when searching for a node in the tree.
214
- * It has a default value of `'ITERATIVE'`.
215
- * @returns The method is returning a NODE object or undefined.
216
- */
217
- getNodeByKey(key: K, iterationType?: IterationType): OptNode<NODE>;
218
305
  /**
219
306
  * Time complexity: O(n)
220
307
  * Space complexity: O(n)
@@ -321,17 +408,24 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
321
408
  * @returns a boolean value.
322
409
  */
323
410
  isAVLBalanced(iterationType?: IterationType): boolean;
324
- protected _DEFAULT_COMPARATOR: (a: K, b: K) => number;
325
411
  protected _comparator: Comparator<K>;
326
412
  /**
327
413
  * The function returns the value of the _comparator property.
328
414
  * @returns The `_comparator` property is being returned.
329
415
  */
330
416
  get comparator(): Comparator<K>;
417
+ protected _extractComparable?: (key: K) => Comparable;
418
+ /**
419
+ * This function returns the value of the `_extractComparable` property.
420
+ * @returns The method `extractComparable()` is being returned, which is a getter method for the
421
+ * `_extractComparable` property.
422
+ */
423
+ get extractComparable(): ((key: K) => Comparable) | undefined;
331
424
  /**
332
425
  * The function sets the root of a tree-like structure and updates the parent property of the new
333
426
  * root.
334
427
  * @param {OptNode<NODE>} v - v is a parameter of type NODE or undefined.
335
428
  */
336
429
  protected _setRoot(v: OptNode<NODE>): void;
430
+ protected _compare(a: K, b: K): number;
337
431
  }