binary-tree-typed 1.53.7 → 1.54.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/dist/common/index.js +5 -0
  2. package/dist/data-structures/base/iterable-entry-base.js +4 -4
  3. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +34 -27
  4. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +63 -53
  5. package/dist/data-structures/binary-tree/avl-tree.d.ts +3 -14
  6. package/dist/data-structures/binary-tree/avl-tree.js +22 -25
  7. package/dist/data-structures/binary-tree/binary-tree.d.ts +33 -17
  8. package/dist/data-structures/binary-tree/binary-tree.js +45 -26
  9. package/dist/data-structures/binary-tree/bst.d.ts +67 -46
  10. package/dist/data-structures/binary-tree/bst.js +87 -54
  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} +75 -16
  14. package/dist/data-structures/binary-tree/{rb-tree.js → red-black-tree.js} +93 -60
  15. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +20 -16
  16. package/dist/data-structures/binary-tree/tree-multi-map.js +41 -28
  17. package/dist/data-structures/graph/abstract-graph.js +2 -2
  18. package/dist/data-structures/hash/hash-map.d.ts +31 -1
  19. package/dist/data-structures/hash/hash-map.js +35 -5
  20. package/dist/data-structures/heap/heap.d.ts +20 -3
  21. package/dist/data-structures/heap/heap.js +31 -11
  22. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +46 -11
  23. package/dist/data-structures/linked-list/doubly-linked-list.js +68 -21
  24. package/dist/data-structures/linked-list/singly-linked-list.d.ts +44 -11
  25. package/dist/data-structures/linked-list/singly-linked-list.js +70 -26
  26. package/dist/data-structures/queue/deque.d.ts +37 -8
  27. package/dist/data-structures/queue/deque.js +73 -29
  28. package/dist/data-structures/queue/queue.d.ts +41 -1
  29. package/dist/data-structures/queue/queue.js +51 -9
  30. package/dist/data-structures/stack/stack.d.ts +27 -10
  31. package/dist/data-structures/stack/stack.js +39 -20
  32. package/dist/data-structures/trie/trie.d.ts +8 -3
  33. package/dist/data-structures/trie/trie.js +8 -3
  34. package/dist/interfaces/binary-tree.d.ts +1 -1
  35. package/dist/types/data-structures/base/base.d.ts +1 -1
  36. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
  37. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -2
  38. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -2
  39. package/dist/types/data-structures/binary-tree/bst.d.ts +3 -3
  40. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -3
  41. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +4 -4
  42. package/package.json +2 -2
  43. package/src/common/index.ts +7 -1
  44. package/src/data-structures/base/iterable-entry-base.ts +4 -4
  45. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +84 -65
  46. package/src/data-structures/binary-tree/avl-tree.ts +40 -34
  47. package/src/data-structures/binary-tree/binary-tree.ts +76 -32
  48. package/src/data-structures/binary-tree/bst.ts +121 -68
  49. package/src/data-structures/binary-tree/index.ts +1 -1
  50. package/src/data-structures/binary-tree/{rb-tree.ts → red-black-tree.ts} +116 -71
  51. package/src/data-structures/binary-tree/tree-multi-map.ts +59 -33
  52. package/src/data-structures/graph/abstract-graph.ts +2 -2
  53. package/src/data-structures/hash/hash-map.ts +37 -7
  54. package/src/data-structures/heap/heap.ts +33 -10
  55. package/src/data-structures/linked-list/doubly-linked-list.ts +75 -21
  56. package/src/data-structures/linked-list/singly-linked-list.ts +77 -27
  57. package/src/data-structures/queue/deque.ts +72 -28
  58. package/src/data-structures/queue/queue.ts +50 -7
  59. package/src/data-structures/stack/stack.ts +39 -20
  60. package/src/data-structures/trie/trie.ts +8 -3
  61. package/src/interfaces/binary-tree.ts +4 -1
  62. package/src/types/data-structures/base/base.ts +1 -1
  63. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +2 -2
  64. package/src/types/data-structures/binary-tree/avl-tree.ts +2 -2
  65. package/src/types/data-structures/binary-tree/binary-tree.ts +2 -2
  66. package/src/types/data-structures/binary-tree/bst.ts +3 -3
  67. package/src/types/data-structures/binary-tree/rb-tree.ts +3 -3
  68. package/src/types/data-structures/binary-tree/tree-multi-map.ts +4 -4
@@ -19,6 +19,9 @@ const common_1 = require("../../common");
19
19
  */
20
20
  class BinaryTreeNode {
21
21
  constructor(key, value) {
22
+ this._height = 0;
23
+ this._color = 'BLACK';
24
+ this._count = 1;
22
25
  this.key = key;
23
26
  this.value = value;
24
27
  }
@@ -40,6 +43,24 @@ class BinaryTreeNode {
40
43
  }
41
44
  this._right = v;
42
45
  }
46
+ get height() {
47
+ return this._height;
48
+ }
49
+ set height(value) {
50
+ this._height = value;
51
+ }
52
+ get color() {
53
+ return this._color;
54
+ }
55
+ set color(value) {
56
+ this._color = value;
57
+ }
58
+ get count() {
59
+ return this._count;
60
+ }
61
+ set count(value) {
62
+ this._count = value;
63
+ }
43
64
  get familyPosition() {
44
65
  const that = this;
45
66
  if (!this.parent) {
@@ -113,6 +134,9 @@ class BinaryTree extends base_1.IterableEntryBase {
113
134
  return this._toEntryFn;
114
135
  }
115
136
  /**
137
+ * Time Complexity: O(1)
138
+ * Space Complexity: O(1)
139
+ *
116
140
  * The function creates a new binary tree node with a specified key and optional value.
117
141
  * @param {K} key - The `key` parameter is the key of the node being created in the binary tree.
118
142
  * @param {V} [value] - The `value` parameter in the `createNode` function is optional, meaning it is
@@ -150,7 +174,7 @@ class BinaryTree extends base_1.IterableEntryBase {
150
174
  * input parameter (`keyNodeEntryOrRaw`) and processes it accordingly to return a node or null
151
175
  * value.
152
176
  */
153
- keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value) {
177
+ _keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value) {
154
178
  if (keyNodeEntryOrRaw === undefined)
155
179
  return [undefined, undefined];
156
180
  if (keyNodeEntryOrRaw === null)
@@ -346,7 +370,7 @@ class BinaryTree extends base_1.IterableEntryBase {
346
370
  * key was found and the node was replaced instead of inserted.
347
371
  */
348
372
  add(keyNodeEntryOrRaw, value) {
349
- const [newNode, newValue] = this.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
373
+ const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
350
374
  if (newNode === undefined)
351
375
  return false;
352
376
  // If the tree is empty, directly set the new node as the root node
@@ -1499,7 +1523,7 @@ class BinaryTree extends base_1.IterableEntryBase {
1499
1523
  const newTree = this.createTree();
1500
1524
  let index = 0;
1501
1525
  for (const [key, value] of this) {
1502
- if (predicate.call(thisArg, value, key, index++, this)) {
1526
+ if (predicate.call(thisArg, key, value, index++, this)) {
1503
1527
  newTree.add([key, value]);
1504
1528
  }
1505
1529
  }
@@ -1509,34 +1533,29 @@ class BinaryTree extends base_1.IterableEntryBase {
1509
1533
  * Time Complexity: O(n)
1510
1534
  * Space Complexity: O(n)
1511
1535
  *
1512
- * The `map` function iterates over key-value pairs in a tree data structure, applies a callback
1513
- * function to each value, and returns a new tree with the updated values.
1514
- * @param callback - The `callback` parameter in the `map` method is a function that will be called
1515
- * on each entry in the tree. It takes four arguments:
1516
- * @param {any} [thisArg] - The `thisArg` parameter in the `map` function is an optional parameter
1517
- * that specifies the value to be passed as `this` when executing the callback function. If provided,
1518
- * the `thisArg` value will be used as the `this` value within the callback function. If `thisArg
1519
- * @returns The `map` method is returning a new tree with the entries modified by the provided
1520
- * callback function. Each entry in the original tree is passed to the callback function, and the
1521
- * result of the callback function is added to the new tree.
1536
+ * The `map` function in TypeScript creates a new BinaryTree by applying a callback function to each
1537
+ * entry in the original BinaryTree.
1538
+ * @param callback - A function that will be called for each entry in the current binary tree. It
1539
+ * takes the key, value (which can be undefined), and an array containing the mapped key and value as
1540
+ * arguments.
1541
+ * @param [options] - The `options` parameter in the `map` method is of type `BinaryTreeOptions<MK,
1542
+ * MV, MR>`. It is an optional parameter that allows you to specify additional options for the binary
1543
+ * tree being created during the mapping process. These options could include things like custom
1544
+ * comparators, initial
1545
+ * @param {any} [thisArg] - The `thisArg` parameter in the `map` method is used to specify the value
1546
+ * of `this` when executing the `callback` function. It allows you to set the context (value of
1547
+ * `this`) within the callback function. If `thisArg` is provided, it will be passed
1548
+ * @returns The `map` function is returning a new `BinaryTree` instance filled with entries that are
1549
+ * the result of applying the provided `callback` function to each entry in the original tree.
1522
1550
  */
1523
- map(callback, thisArg) {
1524
- const newTree = this.createTree();
1551
+ map(callback, options, thisArg) {
1552
+ const newTree = new BinaryTree([], options);
1525
1553
  let index = 0;
1526
1554
  for (const [key, value] of this) {
1527
- newTree.add([key, callback.call(thisArg, value, key, index++, this)]);
1555
+ newTree.add(callback.call(thisArg, key, value, index++, this));
1528
1556
  }
1529
1557
  return newTree;
1530
1558
  }
1531
- // // TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
1532
- // // map<NV>(callback: (entry: [K, V | undefined], tree: this) => NV) {
1533
- // // const newTree = this.createTree();
1534
- // // for (const [key, value] of this) {
1535
- // // newTree.add(key, callback([key, value], this));
1536
- // // }
1537
- // // return newTree;
1538
- // // }
1539
- //
1540
1559
  /**
1541
1560
  * Time Complexity: O(n)
1542
1561
  * Space Complexity: O(n)
@@ -1568,7 +1587,7 @@ class BinaryTree extends base_1.IterableEntryBase {
1568
1587
  if (opts.isShowRedBlackNIL)
1569
1588
  output += `S for Sentinel Node(NIL)\n`;
1570
1589
  const display = (root) => {
1571
- const [lines, , ,] = this._displayAux(root, opts);
1590
+ const [lines, ,] = this._displayAux(root, opts);
1572
1591
  let paragraph = '';
1573
1592
  for (const line of lines) {
1574
1593
  paragraph += line + '\n';
@@ -5,38 +5,38 @@
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, Comparable, Comparator, CP, DFSOrderPattern, IterationType, NodeCallback, NodePredicate, OptNode } from '../../types';
8
+ import { BSTNested, BSTNodeNested, BSTNOptKeyOrNode, BSTOptions, BTNRep, Comparable, Comparator, CP, DFSOrderPattern, EntryCallback, IterationType, NodeCallback, NodePredicate, OptNode, OptNodeOrNull } from '../../types';
9
9
  import { BinaryTree, BinaryTreeNode } from './binary-tree';
10
10
  import { IBinaryTree } from '../../interfaces';
11
11
  import { Range } from '../../common';
12
12
  export declare class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNodeNested<K, V>> extends BinaryTreeNode<K, V, NODE> {
13
13
  parent?: NODE;
14
14
  constructor(key: K, value?: V);
15
- protected _left?: NODE;
15
+ _left?: OptNodeOrNull<NODE>;
16
16
  /**
17
17
  * The function returns the value of the `_left` property.
18
18
  * @returns The `_left` property of the current object is being returned.
19
19
  */
20
- get left(): OptNode<NODE>;
20
+ get left(): OptNodeOrNull<NODE>;
21
21
  /**
22
22
  * The function sets the left child of a node and updates the parent reference of the child.
23
23
  * @param {OptNode<NODE>} v - The parameter `v` is of type `OptNode<NODE>`. It can either be an
24
24
  * instance of the `NODE` class or `undefined`.
25
25
  */
26
- set left(v: OptNode<NODE>);
27
- protected _right?: NODE;
26
+ set left(v: OptNodeOrNull<NODE>);
27
+ _right?: OptNodeOrNull<NODE>;
28
28
  /**
29
29
  * The function returns the right node of a binary tree or undefined if there is no right node.
30
30
  * @returns The method is returning the value of the `_right` property, which is of type `NODE` or
31
31
  * `undefined`.
32
32
  */
33
- get right(): OptNode<NODE>;
33
+ get right(): OptNodeOrNull<NODE>;
34
34
  /**
35
35
  * The function sets the right child of a node and updates the parent reference of the child.
36
36
  * @param {OptNode<NODE>} v - The parameter `v` is of type `OptNode<NODE>`. It can either be a
37
37
  * `NODE` object or `undefined`.
38
38
  */
39
- set right(v: OptNode<NODE>);
39
+ set right(v: OptNodeOrNull<NODE>);
40
40
  }
41
41
  /**
42
42
  * 1. Node Order: Each node's left child has a lesser value, and the right child has a greater value.
@@ -47,32 +47,48 @@ export declare class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE>
47
47
  * 6. Balance Variability: Can become unbalanced; special types maintain balance.
48
48
  * 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
49
49
  * @example
50
- * // Find kth smallest element
51
- * // Create a BST with some elements
52
- * const bst = new BST<number>([5, 3, 7, 1, 4, 6, 8]);
53
- * const sortedKeys = bst.dfs(node => node.key, 'IN');
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
+ * ]);
54
64
  *
55
- * // Helper function to find kth smallest
56
- * const findKthSmallest = (k: number): number | undefined => {
57
- * return sortedKeys[k - 1];
58
- * };
65
+ * // Merge datasets into a single BinarySearchTree
66
+ * const merged = new BST<number, string>(dataset1);
67
+ * merged.addMany(dataset2);
68
+ * merged.merge(dataset3);
59
69
  *
60
- * // Assertions
61
- * console.log(findKthSmallest(1)); // 1
62
- * console.log(findKthSmallest(3)); // 4
63
- * console.log(findKthSmallest(7)); // 8
70
+ * // Verify merged dataset is in sorted order
71
+ * console.log([...merged.values()]); // ['A', 'B', 'C', 'D', 'E', 'F', 'G']
64
72
  * @example
65
73
  * // Find elements in a range
66
74
  * const bst = new BST<number>([10, 5, 15, 3, 7, 12, 18]);
67
75
  * console.log(bst.search(new Range(5, 10))); // [10, 5, 7]
68
- * console.log(bst.search(new Range(4, 12))); // [10, 12, 5, 7]
76
+ * console.log(bst.rangeSearch([4, 12], node => node.key.toString())); // ['10', '12', '5', '7']
69
77
  * console.log(bst.search(new Range(4, 12, true, false))); // [10, 5, 7]
70
- * console.log(bst.search(new Range(15, 20))); // [15, 18]
78
+ * console.log(bst.rangeSearch([15, 20])); // [15, 18]
71
79
  * console.log(bst.search(new Range(15, 20, false))); // [18]
72
80
  * @example
73
81
  * // Find lowest common ancestor
74
82
  * const bst = new BST<number>([20, 10, 30, 5, 15, 25, 35, 3, 7, 12, 18]);
75
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
+ *
76
92
  * function findFirstCommon(arr1: number[], arr2: number[]): number | undefined {
77
93
  * for (const num of arr1) {
78
94
  * if (arr2.indexOf(num) !== -1) {
@@ -82,20 +98,12 @@ export declare class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE>
82
98
  * return undefined;
83
99
  * }
84
100
  *
85
- * // LCA helper function
86
- * const findLCA = (num1: number, num2: number): number | undefined => {
87
- * const path1 = bst.getPathToRoot(num1);
88
- * const path2 = bst.getPathToRoot(num2);
89
- * // Find the first common ancestor
90
- * return findFirstCommon(path1, path2);
91
- * };
92
- *
93
101
  * // Assertions
94
102
  * console.log(findLCA(3, 10)); // 7
95
103
  * console.log(findLCA(5, 35)); // 15
96
104
  * console.log(findLCA(20, 30)); // 25
97
105
  */
98
- 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> {
106
+ export declare class BST<K = any, V = any, R = object, MK = any, MV = any, MR = object, NODE extends BSTNode<K, V, NODE> = BSTNode<K, V, BSTNodeNested<K, V>>, TREE extends BST<K, V, R, MK, MV, MR, NODE, TREE> = BST<K, V, R, MK, MV, MR, NODE, BSTNested<K, V, R, MK, MV, MR, NODE>>> extends BinaryTree<K, V, R, MK, MV, MR, NODE, TREE> implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE> {
99
107
  /**
100
108
  * This is the constructor function for a Binary Search Tree class in TypeScript.
101
109
  * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
@@ -144,7 +152,7 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
144
152
  * value associated with a key in a key-value pair.
145
153
  * @returns either a NODE object or undefined.
146
154
  */
147
- 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];
148
156
  /**
149
157
  * Time Complexity: O(log n)
150
158
  * Space Complexity: O(log n)
@@ -212,16 +220,6 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
212
220
  * successfully inserted into the data structure.
213
221
  */
214
222
  addMany(keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, NODE>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): boolean[];
215
- /**
216
- * Time Complexity: O(n)
217
- * Space Complexity: O(1)
218
- *
219
- * The `merge` function overrides the base class method by adding elements from another
220
- * binary search tree.
221
- * @param anotherTree - `anotherTree` is an instance of a Binary Search Tree (BST) with key type `K`,
222
- * value type `V`, return type `R`, node type `NODE`, and tree type `TREE`.
223
- */
224
- merge(anotherTree: BST<K, V, R, NODE, TREE>): void;
225
223
  /**
226
224
  * Time Complexity: O(log n)
227
225
  * Space Complexity: O(k + log n)
@@ -251,6 +249,28 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
251
249
  * collected in an array and returned as the output of the method.
252
250
  */
253
251
  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>[];
252
+ /**
253
+ * Time Complexity: O(log n)
254
+ * Space Complexity: O(n)
255
+ *
256
+ * The `rangeSearch` function searches for nodes within a specified range in a binary search tree.
257
+ * @param {Range<K> | [K, K]} range - The `range` parameter in the `rangeSearch` function can be
258
+ * either a `Range` object or an array of two elements representing the range boundaries.
259
+ * @param {C} callback - The `callback` parameter in the `rangeSearch` function is a callback
260
+ * function that is used to process each node that is found within the specified range during the
261
+ * search operation. It is of type `NodeCallback<NODE>`, where `NODE` is the type of nodes in the
262
+ * data structure.
263
+ * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `rangeSearch`
264
+ * function represents the node from which the search for nodes within the specified range will
265
+ * begin. It is the starting point for the range search operation.
266
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `rangeSearch` function
267
+ * is used to specify the type of iteration to be performed during the search operation. It has a
268
+ * default value of `this.iterationType`, which suggests that it is likely a property of the class or
269
+ * object that the `rangeSearch`
270
+ * @returns The `rangeSearch` function is returning the result of calling the `search` method with
271
+ * the specified parameters.
272
+ */
273
+ rangeSearch<C extends NodeCallback<NODE>>(range: Range<K> | [K, K], callback?: C, startNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType): ReturnType<C>[];
254
274
  /**
255
275
  * Time Complexity: O(log n)
256
276
  * Space Complexity: O(1)
@@ -384,13 +404,13 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
384
404
  * @returns The `_comparator` property is being returned.
385
405
  */
386
406
  get comparator(): Comparator<K>;
387
- protected _extractComparable?: (key: K) => Comparable;
407
+ protected _specifyComparable?: (key: K) => Comparable;
388
408
  /**
389
- * This function returns the value of the `_extractComparable` property.
390
- * @returns The method `extractComparable()` is being returned, which is a getter method for the
391
- * `_extractComparable` property.
409
+ * This function returns the value of the `_specifyComparable` property.
410
+ * @returns The method `specifyComparable()` is being returned, which is a getter method for the
411
+ * `_specifyComparable` property.
392
412
  */
393
- get extractComparable(): ((key: K) => Comparable) | undefined;
413
+ get specifyComparable(): ((key: K) => Comparable) | undefined;
394
414
  /**
395
415
  * The function sets the root of a tree-like structure and updates the parent property of the new
396
416
  * root.
@@ -398,4 +418,5 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
398
418
  */
399
419
  protected _setRoot(v: OptNode<NODE>): void;
400
420
  protected _compare(a: K, b: K): number;
421
+ map(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: BSTOptions<MK, MV, MR>, thisArg?: any): BST<MK, MV, MR>;
401
422
  }
@@ -4,6 +4,7 @@ exports.BST = exports.BSTNode = void 0;
4
4
  const binary_tree_1 = require("./binary-tree");
5
5
  const queue_1 = require("../queue");
6
6
  const utils_1 = require("../../utils");
7
+ const common_1 = require("../../common");
7
8
  class BSTNode extends binary_tree_1.BinaryTreeNode {
8
9
  constructor(key, value) {
9
10
  super(key, value);
@@ -59,32 +60,48 @@ exports.BSTNode = BSTNode;
59
60
  * 6. Balance Variability: Can become unbalanced; special types maintain balance.
60
61
  * 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
61
62
  * @example
62
- * // Find kth smallest element
63
- * // Create a BST with some elements
64
- * const bst = new BST<number>([5, 3, 7, 1, 4, 6, 8]);
65
- * const sortedKeys = bst.dfs(node => node.key, 'IN');
63
+ * // Merge 3 sorted datasets
64
+ * const dataset1 = new BST<number, string>([
65
+ * [1, 'A'],
66
+ * [7, 'G']
67
+ * ]);
68
+ * const dataset2 = [
69
+ * [2, 'B'],
70
+ * [6, 'F']
71
+ * ];
72
+ * const dataset3 = new BST<number, string>([
73
+ * [3, 'C'],
74
+ * [5, 'E'],
75
+ * [4, 'D']
76
+ * ]);
66
77
  *
67
- * // Helper function to find kth smallest
68
- * const findKthSmallest = (k: number): number | undefined => {
69
- * return sortedKeys[k - 1];
70
- * };
78
+ * // Merge datasets into a single BinarySearchTree
79
+ * const merged = new BST<number, string>(dataset1);
80
+ * merged.addMany(dataset2);
81
+ * merged.merge(dataset3);
71
82
  *
72
- * // Assertions
73
- * console.log(findKthSmallest(1)); // 1
74
- * console.log(findKthSmallest(3)); // 4
75
- * console.log(findKthSmallest(7)); // 8
83
+ * // Verify merged dataset is in sorted order
84
+ * console.log([...merged.values()]); // ['A', 'B', 'C', 'D', 'E', 'F', 'G']
76
85
  * @example
77
86
  * // Find elements in a range
78
87
  * const bst = new BST<number>([10, 5, 15, 3, 7, 12, 18]);
79
88
  * console.log(bst.search(new Range(5, 10))); // [10, 5, 7]
80
- * console.log(bst.search(new Range(4, 12))); // [10, 12, 5, 7]
89
+ * console.log(bst.rangeSearch([4, 12], node => node.key.toString())); // ['10', '12', '5', '7']
81
90
  * console.log(bst.search(new Range(4, 12, true, false))); // [10, 5, 7]
82
- * console.log(bst.search(new Range(15, 20))); // [15, 18]
91
+ * console.log(bst.rangeSearch([15, 20])); // [15, 18]
83
92
  * console.log(bst.search(new Range(15, 20, false))); // [18]
84
93
  * @example
85
94
  * // Find lowest common ancestor
86
95
  * const bst = new BST<number>([20, 10, 30, 5, 15, 25, 35, 3, 7, 12, 18]);
87
96
  *
97
+ * // LCA helper function
98
+ * const findLCA = (num1: number, num2: number): number | undefined => {
99
+ * const path1 = bst.getPathToRoot(num1);
100
+ * const path2 = bst.getPathToRoot(num2);
101
+ * // Find the first common ancestor
102
+ * return findFirstCommon(path1, path2);
103
+ * };
104
+ *
88
105
  * function findFirstCommon(arr1: number[], arr2: number[]): number | undefined {
89
106
  * for (const num of arr1) {
90
107
  * if (arr2.indexOf(num) !== -1) {
@@ -94,14 +111,6 @@ exports.BSTNode = BSTNode;
94
111
  * return undefined;
95
112
  * }
96
113
  *
97
- * // LCA helper function
98
- * const findLCA = (num1: number, num2: number): number | undefined => {
99
- * const path1 = bst.getPathToRoot(num1);
100
- * const path2 = bst.getPathToRoot(num2);
101
- * // Find the first common ancestor
102
- * return findFirstCommon(path1, path2);
103
- * };
104
- *
105
114
  * // Assertions
106
115
  * console.log(findLCA(3, 10)); // 7
107
116
  * console.log(findLCA(5, 35)); // 15
@@ -128,22 +137,22 @@ class BST extends binary_tree_1.BinaryTree {
128
137
  return -1;
129
138
  return 0;
130
139
  }
131
- if (this._extractComparable) {
132
- if (this._extractComparable(a) > this._extractComparable(b))
140
+ if (this._specifyComparable) {
141
+ if (this._specifyComparable(a) > this._specifyComparable(b))
133
142
  return 1;
134
- if (this._extractComparable(a) < this._extractComparable(b))
143
+ if (this._specifyComparable(a) < this._specifyComparable(b))
135
144
  return -1;
136
145
  return 0;
137
146
  }
138
147
  if (typeof a === 'object' || typeof b === 'object') {
139
- throw TypeError(`When comparing object types, a custom extractComparable must be defined in the constructor's options parameter.`);
148
+ throw TypeError(`When comparing object types, a custom specifyComparable must be defined in the constructor's options parameter.`);
140
149
  }
141
150
  return 0;
142
151
  };
143
152
  if (options) {
144
- const { extractComparable, isReverse } = options;
145
- if (typeof extractComparable === 'function')
146
- this._extractComparable = extractComparable;
153
+ const { specifyComparable, isReverse } = options;
154
+ if (typeof specifyComparable === 'function')
155
+ this._specifyComparable = specifyComparable;
147
156
  if (isReverse !== undefined)
148
157
  this._isReverse = isReverse;
149
158
  }
@@ -184,7 +193,7 @@ class BST extends binary_tree_1.BinaryTree {
184
193
  * @returns a new instance of the BST class with the provided options.
185
194
  */
186
195
  createTree(options) {
187
- return new BST([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, extractComparable: this._extractComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options));
196
+ return new BST([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options));
188
197
  }
189
198
  /**
190
199
  * The function overrides a method and converts a key, value pair or entry or raw element to a node.
@@ -195,8 +204,8 @@ class BST extends binary_tree_1.BinaryTree {
195
204
  * value associated with a key in a key-value pair.
196
205
  * @returns either a NODE object or undefined.
197
206
  */
198
- keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value) {
199
- const [node, entryValue] = super.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
207
+ _keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value) {
208
+ const [node, entryValue] = super._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
200
209
  if (node === null)
201
210
  return [undefined, undefined];
202
211
  return [node, value !== null && value !== void 0 ? value : entryValue];
@@ -239,7 +248,7 @@ class BST extends binary_tree_1.BinaryTree {
239
248
  * this._DEFAULT_COMPARATOR`.
240
249
  */
241
250
  isKey(key) {
242
- return (0, utils_1.isComparable)(key, this._extractComparable !== undefined);
251
+ return (0, utils_1.isComparable)(key, this._specifyComparable !== undefined);
243
252
  }
244
253
  /**
245
254
  * Time Complexity: O(log n)
@@ -253,7 +262,7 @@ class BST extends binary_tree_1.BinaryTree {
253
262
  * @returns a boolean value.
254
263
  */
255
264
  add(keyNodeEntryOrRaw, value) {
256
- const [newNode, newValue] = this.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
265
+ const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
257
266
  if (newNode === undefined)
258
267
  return false;
259
268
  if (this._root === undefined) {
@@ -279,7 +288,8 @@ class BST extends binary_tree_1.BinaryTree {
279
288
  this._size++;
280
289
  return true;
281
290
  }
282
- current = current.left;
291
+ if (current.left !== null)
292
+ current = current.left;
283
293
  }
284
294
  else {
285
295
  if (current.right === undefined) {
@@ -289,7 +299,8 @@ class BST extends binary_tree_1.BinaryTree {
289
299
  this._size++;
290
300
  return true;
291
301
  }
292
- current = current.right;
302
+ if (current.right !== null)
303
+ current = current.right;
293
304
  }
294
305
  }
295
306
  return false;
@@ -396,18 +407,6 @@ class BST extends binary_tree_1.BinaryTree {
396
407
  }
397
408
  return inserted;
398
409
  }
399
- /**
400
- * Time Complexity: O(n)
401
- * Space Complexity: O(1)
402
- *
403
- * The `merge` function overrides the base class method by adding elements from another
404
- * binary search tree.
405
- * @param anotherTree - `anotherTree` is an instance of a Binary Search Tree (BST) with key type `K`,
406
- * value type `V`, return type `R`, node type `NODE`, and tree type `TREE`.
407
- */
408
- merge(anotherTree) {
409
- this.addMany(anotherTree, [], false);
410
- }
411
410
  /**
412
411
  * Time Complexity: O(log n)
413
412
  * Space Complexity: O(k + log n)
@@ -547,6 +546,31 @@ class BST extends binary_tree_1.BinaryTree {
547
546
  }
548
547
  return ans;
549
548
  }
549
+ /**
550
+ * Time Complexity: O(log n)
551
+ * Space Complexity: O(n)
552
+ *
553
+ * The `rangeSearch` function searches for nodes within a specified range in a binary search tree.
554
+ * @param {Range<K> | [K, K]} range - The `range` parameter in the `rangeSearch` function can be
555
+ * either a `Range` object or an array of two elements representing the range boundaries.
556
+ * @param {C} callback - The `callback` parameter in the `rangeSearch` function is a callback
557
+ * function that is used to process each node that is found within the specified range during the
558
+ * search operation. It is of type `NodeCallback<NODE>`, where `NODE` is the type of nodes in the
559
+ * data structure.
560
+ * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `rangeSearch`
561
+ * function represents the node from which the search for nodes within the specified range will
562
+ * begin. It is the starting point for the range search operation.
563
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `rangeSearch` function
564
+ * is used to specify the type of iteration to be performed during the search operation. It has a
565
+ * default value of `this.iterationType`, which suggests that it is likely a property of the class or
566
+ * object that the `rangeSearch`
567
+ * @returns The `rangeSearch` function is returning the result of calling the `search` method with
568
+ * the specified parameters.
569
+ */
570
+ rangeSearch(range, callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
571
+ const searchRange = range instanceof common_1.Range ? range : new common_1.Range(range[0], range[1]);
572
+ return this.search(searchRange, false, callback, startNode, iterationType);
573
+ }
550
574
  /**
551
575
  * Time Complexity: O(log n)
552
576
  * Space Complexity: O(1)
@@ -783,7 +807,8 @@ class BST extends binary_tree_1.BinaryTree {
783
807
  while (stack.length > 0 || node) {
784
808
  if (node) {
785
809
  stack.push(node);
786
- node = node.left;
810
+ if (node.left !== null)
811
+ node = node.left;
787
812
  }
788
813
  else {
789
814
  node = stack[stack.length - 1];
@@ -814,12 +839,12 @@ class BST extends binary_tree_1.BinaryTree {
814
839
  return this._comparator;
815
840
  }
816
841
  /**
817
- * This function returns the value of the `_extractComparable` property.
818
- * @returns The method `extractComparable()` is being returned, which is a getter method for the
819
- * `_extractComparable` property.
842
+ * This function returns the value of the `_specifyComparable` property.
843
+ * @returns The method `specifyComparable()` is being returned, which is a getter method for the
844
+ * `_specifyComparable` property.
820
845
  */
821
- get extractComparable() {
822
- return this._extractComparable;
846
+ get specifyComparable() {
847
+ return this._specifyComparable;
823
848
  }
824
849
  /**
825
850
  * The function sets the root of a tree-like structure and updates the parent property of the new
@@ -835,5 +860,13 @@ class BST extends binary_tree_1.BinaryTree {
835
860
  _compare(a, b) {
836
861
  return this._isReverse ? -this._comparator(a, b) : this._comparator(a, b);
837
862
  }
863
+ map(callback, options, thisArg) {
864
+ const newTree = new BST([], options);
865
+ let index = 0;
866
+ for (const [key, value] of this) {
867
+ newTree.add(callback.call(thisArg, key, value, index++, this));
868
+ }
869
+ return newTree;
870
+ }
838
871
  }
839
872
  exports.BST = BST;
@@ -3,6 +3,6 @@ export * from './bst';
3
3
  export * from './binary-indexed-tree';
4
4
  export * from './segment-tree';
5
5
  export * from './avl-tree';
6
- export * from './rb-tree';
6
+ export * from './red-black-tree';
7
7
  export * from './avl-tree-multi-map';
8
8
  export * from './tree-multi-map';
@@ -19,6 +19,6 @@ __exportStar(require("./bst"), exports);
19
19
  __exportStar(require("./binary-indexed-tree"), exports);
20
20
  __exportStar(require("./segment-tree"), exports);
21
21
  __exportStar(require("./avl-tree"), exports);
22
- __exportStar(require("./rb-tree"), exports);
22
+ __exportStar(require("./red-black-tree"), exports);
23
23
  __exportStar(require("./avl-tree-multi-map"), exports);
24
24
  __exportStar(require("./tree-multi-map"), exports);