binary-tree-typed 1.53.8 → 1.54.1

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 (96) hide show
  1. package/dist/data-structures/base/iterable-entry-base.js +4 -4
  2. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +213 -0
  3. package/dist/data-structures/binary-tree/avl-tree-counter.js +407 -0
  4. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +71 -170
  5. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +133 -328
  6. package/dist/data-structures/binary-tree/avl-tree.d.ts +103 -69
  7. package/dist/data-structures/binary-tree/avl-tree.js +130 -70
  8. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +3 -0
  9. package/dist/data-structures/binary-tree/binary-indexed-tree.js +3 -0
  10. package/dist/data-structures/binary-tree/binary-tree.d.ts +268 -202
  11. package/dist/data-structures/binary-tree/binary-tree.js +311 -263
  12. package/dist/data-structures/binary-tree/bst.d.ts +145 -121
  13. package/dist/data-structures/binary-tree/bst.js +195 -145
  14. package/dist/data-structures/binary-tree/index.d.ts +2 -0
  15. package/dist/data-structures/binary-tree/index.js +2 -0
  16. package/dist/data-structures/binary-tree/red-black-tree.d.ts +100 -72
  17. package/dist/data-structures/binary-tree/red-black-tree.js +127 -107
  18. package/dist/data-structures/binary-tree/tree-counter.d.ts +212 -0
  19. package/dist/data-structures/binary-tree/tree-counter.js +444 -0
  20. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +78 -170
  21. package/dist/data-structures/binary-tree/tree-multi-map.js +140 -362
  22. package/dist/data-structures/graph/abstract-graph.js +2 -2
  23. package/dist/data-structures/graph/directed-graph.d.ts +3 -0
  24. package/dist/data-structures/graph/directed-graph.js +3 -0
  25. package/dist/data-structures/graph/map-graph.d.ts +3 -0
  26. package/dist/data-structures/graph/map-graph.js +3 -0
  27. package/dist/data-structures/graph/undirected-graph.d.ts +3 -0
  28. package/dist/data-structures/graph/undirected-graph.js +3 -0
  29. package/dist/data-structures/hash/hash-map.d.ts +1 -1
  30. package/dist/data-structures/hash/hash-map.js +5 -5
  31. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +10 -10
  32. package/dist/data-structures/linked-list/doubly-linked-list.js +12 -12
  33. package/dist/data-structures/linked-list/singly-linked-list.d.ts +13 -10
  34. package/dist/data-structures/linked-list/singly-linked-list.js +19 -16
  35. package/dist/data-structures/linked-list/skip-linked-list.d.ts +3 -0
  36. package/dist/data-structures/linked-list/skip-linked-list.js +3 -0
  37. package/dist/data-structures/matrix/matrix.d.ts +3 -0
  38. package/dist/data-structures/matrix/matrix.js +3 -0
  39. package/dist/data-structures/matrix/navigator.d.ts +3 -0
  40. package/dist/data-structures/matrix/navigator.js +3 -0
  41. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +3 -0
  42. package/dist/data-structures/priority-queue/max-priority-queue.js +3 -0
  43. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +3 -0
  44. package/dist/data-structures/priority-queue/min-priority-queue.js +3 -0
  45. package/dist/data-structures/trie/trie.d.ts +0 -4
  46. package/dist/data-structures/trie/trie.js +0 -4
  47. package/dist/interfaces/binary-tree.d.ts +8 -8
  48. package/dist/types/data-structures/base/base.d.ts +1 -1
  49. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
  50. package/dist/types/data-structures/binary-tree/avl-tree-counter.js +2 -0
  51. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -4
  52. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -3
  53. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -3
  54. package/dist/types/data-structures/binary-tree/bst.d.ts +4 -4
  55. package/dist/types/data-structures/binary-tree/index.d.ts +2 -0
  56. package/dist/types/data-structures/binary-tree/index.js +2 -0
  57. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -5
  58. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
  59. package/dist/types/data-structures/binary-tree/tree-counter.js +2 -0
  60. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -5
  61. package/package.json +2 -2
  62. package/src/data-structures/base/iterable-entry-base.ts +4 -4
  63. package/src/data-structures/binary-tree/avl-tree-counter.ts +463 -0
  64. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +151 -370
  65. package/src/data-structures/binary-tree/avl-tree.ts +162 -105
  66. package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -0
  67. package/src/data-structures/binary-tree/binary-tree.ts +488 -416
  68. package/src/data-structures/binary-tree/bst.ts +270 -234
  69. package/src/data-structures/binary-tree/index.ts +2 -0
  70. package/src/data-structures/binary-tree/red-black-tree.ts +170 -145
  71. package/src/data-structures/binary-tree/tree-counter.ts +504 -0
  72. package/src/data-structures/binary-tree/tree-multi-map.ts +159 -401
  73. package/src/data-structures/graph/abstract-graph.ts +2 -2
  74. package/src/data-structures/graph/directed-graph.ts +3 -0
  75. package/src/data-structures/graph/map-graph.ts +3 -0
  76. package/src/data-structures/graph/undirected-graph.ts +3 -0
  77. package/src/data-structures/hash/hash-map.ts +7 -7
  78. package/src/data-structures/linked-list/doubly-linked-list.ts +13 -13
  79. package/src/data-structures/linked-list/singly-linked-list.ts +20 -17
  80. package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
  81. package/src/data-structures/matrix/matrix.ts +3 -0
  82. package/src/data-structures/matrix/navigator.ts +3 -0
  83. package/src/data-structures/priority-queue/max-priority-queue.ts +3 -0
  84. package/src/data-structures/priority-queue/min-priority-queue.ts +3 -0
  85. package/src/data-structures/trie/trie.ts +0 -4
  86. package/src/interfaces/binary-tree.ts +10 -21
  87. package/src/types/data-structures/base/base.ts +1 -1
  88. package/src/types/data-structures/binary-tree/avl-tree-counter.ts +3 -0
  89. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -6
  90. package/src/types/data-structures/binary-tree/avl-tree.ts +0 -5
  91. package/src/types/data-structures/binary-tree/binary-tree.ts +0 -5
  92. package/src/types/data-structures/binary-tree/bst.ts +6 -6
  93. package/src/types/data-structures/binary-tree/index.ts +2 -0
  94. package/src/types/data-structures/binary-tree/rb-tree.ts +2 -7
  95. package/src/types/data-structures/binary-tree/tree-counter.ts +3 -0
  96. package/src/types/data-structures/binary-tree/tree-multi-map.ts +2 -7
@@ -6,43 +6,33 @@ const queue_1 = require("../queue");
6
6
  const utils_1 = require("../../utils");
7
7
  const common_1 = require("../../common");
8
8
  class BSTNode extends binary_tree_1.BinaryTreeNode {
9
+ /**
10
+ * This TypeScript constructor function initializes an instance with a key and an optional value.
11
+ * @param {K} key - The `key` parameter is typically used to uniquely identify an object or element
12
+ * within a data structure. It serves as a reference or identifier for accessing or manipulating the
13
+ * associated value.
14
+ * @param {V} [value] - The `value` parameter in the constructor is optional, meaning it does not
15
+ * have to be provided when creating an instance of the class. If a value is not provided, it will
16
+ * default to `undefined`.
17
+ */
9
18
  constructor(key, value) {
10
19
  super(key, value);
11
20
  this.parent = undefined;
12
21
  this._left = undefined;
13
22
  this._right = undefined;
14
23
  }
15
- /**
16
- * The function returns the value of the `_left` property.
17
- * @returns The `_left` property of the current object is being returned.
18
- */
19
24
  get left() {
20
25
  return this._left;
21
26
  }
22
- /**
23
- * The function sets the left child of a node and updates the parent reference of the child.
24
- * @param {OptNode<NODE>} v - The parameter `v` is of type `OptNode<NODE>`. It can either be an
25
- * instance of the `NODE` class or `undefined`.
26
- */
27
27
  set left(v) {
28
28
  if (v) {
29
29
  v.parent = this;
30
30
  }
31
31
  this._left = v;
32
32
  }
33
- /**
34
- * The function returns the right node of a binary tree or undefined if there is no right node.
35
- * @returns The method is returning the value of the `_right` property, which is of type `NODE` or
36
- * `undefined`.
37
- */
38
33
  get right() {
39
34
  return this._right;
40
35
  }
41
- /**
42
- * The function sets the right child of a node and updates the parent reference of the child.
43
- * @param {OptNode<NODE>} v - The parameter `v` is of type `OptNode<NODE>`. It can either be a
44
- * `NODE` object or `undefined`.
45
- */
46
36
  set right(v) {
47
37
  if (v) {
48
38
  v.parent = this;
@@ -118,12 +108,13 @@ exports.BSTNode = BSTNode;
118
108
  */
119
109
  class BST extends binary_tree_1.BinaryTree {
120
110
  /**
121
- * This is the constructor function for a Binary Search Tree class in TypeScript.
122
- * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
123
- * iterable that can contain either keys, nodes, entries, or raw elements. These elements will be
124
- * added to the binary search tree during the construction of the object.
125
- * @param [options] - An optional object that contains additional options for the Binary Search Tree.
126
- * It can include a comparator function that defines the order of the elements in the tree.
111
+ * This TypeScript constructor initializes a binary search tree with optional options and adds
112
+ * elements if provided.
113
+ * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
114
+ * iterable that can contain elements of type `BTNRep<K, V, BSTNode<K, V>>` or `R`. It is used to
115
+ * initialize the binary search tree with keys, nodes, entries, or raw data.
116
+ * @param [options] - The `options` parameter is an optional object that can contain the following
117
+ * properties:
127
118
  */
128
119
  constructor(keysNodesEntriesOrRaws = [], options) {
129
120
  super([], options);
@@ -137,55 +128,58 @@ class BST extends binary_tree_1.BinaryTree {
137
128
  return -1;
138
129
  return 0;
139
130
  }
140
- if (this._extractComparable) {
141
- if (this._extractComparable(a) > this._extractComparable(b))
131
+ if (this._specifyComparable) {
132
+ if (this._specifyComparable(a) > this._specifyComparable(b))
142
133
  return 1;
143
- if (this._extractComparable(a) < this._extractComparable(b))
134
+ if (this._specifyComparable(a) < this._specifyComparable(b))
144
135
  return -1;
145
136
  return 0;
146
137
  }
147
138
  if (typeof a === 'object' || typeof b === 'object') {
148
- throw TypeError(`When comparing object types, a custom extractComparable must be defined in the constructor's options parameter.`);
139
+ throw TypeError(`When comparing object types, a custom specifyComparable must be defined in the constructor's options parameter.`);
149
140
  }
150
141
  return 0;
151
142
  };
152
143
  if (options) {
153
- const { extractComparable, isReverse } = options;
154
- if (typeof extractComparable === 'function')
155
- this._extractComparable = extractComparable;
144
+ const { specifyComparable, isReverse } = options;
145
+ if (typeof specifyComparable === 'function')
146
+ this._specifyComparable = specifyComparable;
156
147
  if (isReverse !== undefined)
157
148
  this._isReverse = isReverse;
158
149
  }
159
150
  if (keysNodesEntriesOrRaws)
160
151
  this.addMany(keysNodesEntriesOrRaws);
161
152
  }
162
- /**
163
- * The function returns the root node of a tree structure.
164
- * @returns The `_root` property of the object, which is of type `NODE` or `undefined`.
165
- */
166
153
  get root() {
167
154
  return this._root;
168
155
  }
169
- /**
170
- * The above function is a getter method in TypeScript that returns the value of the private property
171
- * `_isReverse`.
172
- * @returns The `isReverse` property of the object, which is a boolean value.
173
- */
174
156
  get isReverse() {
175
157
  return this._isReverse;
176
158
  }
159
+ get comparator() {
160
+ return this._comparator;
161
+ }
162
+ get specifyComparable() {
163
+ return this._specifyComparable;
164
+ }
177
165
  /**
166
+ * Time Complexity: O(1)
167
+ * Space Complexity: O(1)
168
+ *
178
169
  * The function creates a new BSTNode with the given key and value and returns it.
179
170
  * @param {K} key - The key parameter is of type K, which represents the type of the key for the node
180
171
  * being created.
181
172
  * @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
182
173
  * value associated with the key in the node being created.
183
- * @returns The method is returning a new instance of the BSTNode class, casted as the NODE type.
174
+ * @returns The method is returning a new instance of the BSTNode class, casted as the BSTNode<K, V> type.
184
175
  */
185
176
  createNode(key, value) {
186
177
  return new BSTNode(key, this._isMapMode ? undefined : value);
187
178
  }
188
179
  /**
180
+ * Time Complexity: O(1)
181
+ * Space Complexity: O(1)
182
+ *
189
183
  * The function creates a new binary search tree with the specified options.
190
184
  * @param [options] - The `options` parameter is an optional object that allows you to customize the
191
185
  * behavior of the `createTree` method. It accepts a partial `BSTOptions` object, which has the
@@ -193,22 +187,7 @@ class BST extends binary_tree_1.BinaryTree {
193
187
  * @returns a new instance of the BST class with the provided options.
194
188
  */
195
189
  createTree(options) {
196
- return new BST([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, extractComparable: this._extractComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options));
197
- }
198
- /**
199
- * The function overrides a method and converts a key, value pair or entry or raw element to a node.
200
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - A variable that can be of
201
- * type R or BTNRep<K, V, NODE>. It represents either a key, a node, an entry, or a raw
202
- * element.
203
- * @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
204
- * value associated with a key in a key-value pair.
205
- * @returns either a NODE object or undefined.
206
- */
207
- _keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value) {
208
- const [node, entryValue] = super._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
209
- if (node === null)
210
- return [undefined, undefined];
211
- return [node, value !== null && value !== void 0 ? value : entryValue];
190
+ return new BST([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options));
212
191
  }
213
192
  /**
214
193
  * Time Complexity: O(log n)
@@ -216,8 +195,8 @@ class BST extends binary_tree_1.BinaryTree {
216
195
  *
217
196
  * The function ensures the existence of a node in a data structure and returns it, or undefined if
218
197
  * it doesn't exist.
219
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
220
- * `keyNodeEntryOrRaw` can accept a value of type `R`, which represents the key, node,
198
+ * @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - The parameter
199
+ * `keyNodeOrEntry` can accept a value of type `R`, which represents the key, node,
221
200
  * entry, or raw element that needs to be ensured in the tree.
222
201
  * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
223
202
  * parameter that specifies the type of iteration to be used when ensuring a node. It has a default
@@ -225,44 +204,50 @@ class BST extends binary_tree_1.BinaryTree {
225
204
  * @returns The method is returning either the node that was ensured or `undefined` if the node could
226
205
  * not be ensured.
227
206
  */
228
- ensureNode(keyNodeEntryOrRaw, iterationType = this.iterationType) {
207
+ ensureNode(keyNodeOrEntry, iterationType = this.iterationType) {
229
208
  var _a;
230
- return (_a = super.ensureNode(keyNodeEntryOrRaw, iterationType)) !== null && _a !== void 0 ? _a : undefined;
209
+ return (_a = super.ensureNode(keyNodeOrEntry, iterationType)) !== null && _a !== void 0 ? _a : undefined;
231
210
  }
232
211
  /**
212
+ * Time Complexity: O(1)
213
+ * Space Complexity: O(1)
214
+ *
233
215
  * The function checks if the input is an instance of the BSTNode class.
234
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
235
- * `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
236
- * @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
216
+ * @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - The parameter
217
+ * `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, BSTNode<K, V>>`.
218
+ * @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
237
219
  * an instance of the `BSTNode` class.
238
220
  */
239
- isNode(keyNodeEntryOrRaw) {
240
- return keyNodeEntryOrRaw instanceof BSTNode;
221
+ isNode(keyNodeOrEntry) {
222
+ return keyNodeOrEntry instanceof BSTNode;
241
223
  }
242
224
  /**
243
- * The function "override isKey" checks if a key is comparable based on a given comparator.
225
+ * Time Complexity: O(1)
226
+ * Space Complexity: O(1)
227
+ *
228
+ * The function "override isValidKey" checks if a key is comparable based on a given comparator.
244
229
  * @param {any} key - The `key` parameter is a value that will be checked to determine if it is of
245
230
  * type `K`.
246
- * @returns The `override isKey(key: any): key is K` function is returning a boolean value based on
231
+ * @returns The `override isValidKey(key: any): key is K` function is returning a boolean value based on
247
232
  * the result of the `isComparable` function with the condition `this._compare !==
248
233
  * this._DEFAULT_COMPARATOR`.
249
234
  */
250
- isKey(key) {
251
- return (0, utils_1.isComparable)(key, this._extractComparable !== undefined);
235
+ isValidKey(key) {
236
+ return (0, utils_1.isComparable)(key, this._specifyComparable !== undefined);
252
237
  }
253
238
  /**
254
239
  * Time Complexity: O(log n)
255
- * Space Complexity: O(1)
240
+ * Space Complexity: O(log n)
256
241
  *
257
242
  * The `add` function in TypeScript adds a new node to a binary search tree based on the key value.
258
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
259
- * `keyNodeEntryOrRaw` can accept a value of type `R` or `BTNRep<K, V, NODE>`.
243
+ * @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - The parameter
244
+ * `keyNodeOrEntry` can accept a value of type `R` or `BTNRep<K, V, BSTNode<K, V>>`.
260
245
  * @param {V} [value] - The `value` parameter is an optional value that can be associated with the
261
246
  * key in the binary search tree. If provided, it will be stored in the node along with the key.
262
247
  * @returns a boolean value.
263
248
  */
264
- add(keyNodeEntryOrRaw, value) {
265
- const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
249
+ add(keyNodeOrEntry, value) {
250
+ const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
266
251
  if (newNode === undefined)
267
252
  return false;
268
253
  if (this._root === undefined) {
@@ -288,7 +273,8 @@ class BST extends binary_tree_1.BinaryTree {
288
273
  this._size++;
289
274
  return true;
290
275
  }
291
- current = current.left;
276
+ if (current.left !== null)
277
+ current = current.left;
292
278
  }
293
279
  else {
294
280
  if (current.right === undefined) {
@@ -298,7 +284,8 @@ class BST extends binary_tree_1.BinaryTree {
298
284
  this._size++;
299
285
  return true;
300
286
  }
301
- current = current.right;
287
+ if (current.right !== null)
288
+ current = current.right;
302
289
  }
303
290
  }
304
291
  return false;
@@ -331,8 +318,10 @@ class BST extends binary_tree_1.BinaryTree {
331
318
  valuesIterator = values[Symbol.iterator]();
332
319
  }
333
320
  if (!isBalanceAdd) {
334
- for (const kve of keysNodesEntriesOrRaws) {
321
+ for (let kve of keysNodesEntriesOrRaws) {
335
322
  const value = valuesIterator === null || valuesIterator === void 0 ? void 0 : valuesIterator.next().value;
323
+ if (this.isRaw(kve))
324
+ kve = this._toEntryFn(kve);
336
325
  inserted.push(this.add(kve, value));
337
326
  }
338
327
  return inserted;
@@ -346,23 +335,21 @@ class BST extends binary_tree_1.BinaryTree {
346
335
  let sorted = [];
347
336
  sorted = realBTNExemplars.sort(({ key: a }, { key: b }) => {
348
337
  let keyA, keyB;
349
- if (this.isEntry(a))
338
+ if (this.isRaw(a))
339
+ keyA = this._toEntryFn(a)[0];
340
+ else if (this.isEntry(a))
350
341
  keyA = a[0];
351
342
  else if (this.isRealNode(a))
352
343
  keyA = a.key;
353
- else if (this._toEntryFn) {
354
- keyA = this._toEntryFn(a)[0];
355
- }
356
344
  else {
357
345
  keyA = a;
358
346
  }
359
- if (this.isEntry(b))
347
+ if (this.isRaw(b))
348
+ keyB = this._toEntryFn(b)[0];
349
+ else if (this.isEntry(b))
360
350
  keyB = b[0];
361
351
  else if (this.isRealNode(b))
362
352
  keyB = b.key;
363
- else if (this._toEntryFn) {
364
- keyB = this._toEntryFn(b)[0];
365
- }
366
353
  else {
367
354
  keyB = b;
368
355
  }
@@ -372,15 +359,23 @@ class BST extends binary_tree_1.BinaryTree {
372
359
  return 0;
373
360
  });
374
361
  const _dfs = (arr) => {
362
+ var _a;
375
363
  if (arr.length === 0)
376
364
  return;
377
365
  const mid = Math.floor((arr.length - 1) / 2);
378
- const { key, value, orgIndex } = arr[mid];
366
+ let { key, value } = arr[mid];
367
+ const { orgIndex } = arr[mid];
368
+ if (this.isRaw(key)) {
369
+ const entry = this._toEntryFn(key);
370
+ key = entry[0];
371
+ value = (_a = entry[1]) !== null && _a !== void 0 ? _a : value;
372
+ }
379
373
  inserted[orgIndex] = this.add(key, value);
380
374
  _dfs(arr.slice(0, mid));
381
375
  _dfs(arr.slice(mid + 1));
382
376
  };
383
377
  const _iterate = () => {
378
+ var _a;
384
379
  const n = sorted.length;
385
380
  const stack = [[0, n - 1]];
386
381
  while (stack.length > 0) {
@@ -389,7 +384,13 @@ class BST extends binary_tree_1.BinaryTree {
389
384
  const [l, r] = popped;
390
385
  if (l <= r) {
391
386
  const m = l + Math.floor((r - l) / 2);
392
- const { key, value, orgIndex } = sorted[m];
387
+ let { key, value } = sorted[m];
388
+ const { orgIndex } = sorted[m];
389
+ if (this.isRaw(key)) {
390
+ const entry = this._toEntryFn(key);
391
+ key = entry[0];
392
+ value = (_a = entry[1]) !== null && _a !== void 0 ? _a : value;
393
+ }
393
394
  inserted[orgIndex] = this.add(key, value);
394
395
  stack.push([m + 1, r]);
395
396
  stack.push([l, m - 1]);
@@ -405,35 +406,23 @@ class BST extends binary_tree_1.BinaryTree {
405
406
  }
406
407
  return inserted;
407
408
  }
408
- /**
409
- * Time Complexity: O(n)
410
- * Space Complexity: O(1)
411
- *
412
- * The `merge` function overrides the base class method by adding elements from another
413
- * binary search tree.
414
- * @param anotherTree - `anotherTree` is an instance of a Binary Search Tree (BST) with key type `K`,
415
- * value type `V`, return type `R`, node type `NODE`, and tree type `TREE`.
416
- */
417
- merge(anotherTree) {
418
- this.addMany(anotherTree, [], false);
419
- }
420
409
  /**
421
410
  * Time Complexity: O(log n)
422
411
  * Space Complexity: O(k + log n)
423
412
  *
424
413
  * The function `search` in TypeScript overrides the search behavior in a binary tree structure based
425
414
  * on specified criteria.
426
- * @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The
427
- * `keyNodeEntryRawOrPredicate` parameter in the `override search` method can accept one of the
415
+ * @param {BTNRep<K, V, BSTNode<K, V>> | NodePredicate<BSTNode<K, V>>} keyNodeEntryOrPredicate - The
416
+ * `keyNodeEntryOrPredicate` parameter in the `override search` method can accept one of the
428
417
  * following types:
429
418
  * @param [onlyOne=false] - The `onlyOne` parameter is a boolean flag that determines whether the
430
419
  * search should stop after finding the first matching node. If `onlyOne` is set to `true`, the
431
420
  * search will return as soon as a matching node is found. If `onlyOne` is set to `false`, the
432
421
  * @param {C} callback - The `callback` parameter in the `override search` function is a function
433
422
  * that will be called on each node that matches the search criteria. It is of type `C`, which
434
- * extends `NodeCallback<NODE>`. The callback function should accept a node of type `NODE` as its
423
+ * extends `NodeCallback<BSTNode<K, V>>`. The callback function should accept a node of type `BSTNode<K, V>` as its
435
424
  * argument and
436
- * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `override search`
425
+ * @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter in the `override search`
437
426
  * method represents the node from which the search operation will begin. It is the starting point
438
427
  * for searching within the tree data structure. The method ensures that the `startNode` is a valid
439
428
  * node before proceeding with the search operation. If the `
@@ -445,26 +434,26 @@ class BST extends binary_tree_1.BinaryTree {
445
434
  * structure based on the provided key, predicate, and other options. The search results are
446
435
  * collected in an array and returned as the output of the method.
447
436
  */
448
- search(keyNodeEntryRawOrPredicate, onlyOne = false, callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
449
- if (keyNodeEntryRawOrPredicate === undefined)
437
+ search(keyNodeEntryOrPredicate, onlyOne = false, callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
438
+ if (keyNodeEntryOrPredicate === undefined)
450
439
  return [];
451
- if (keyNodeEntryRawOrPredicate === null)
440
+ if (keyNodeEntryOrPredicate === null)
452
441
  return [];
453
442
  startNode = this.ensureNode(startNode);
454
443
  if (!startNode)
455
444
  return [];
456
445
  let predicate;
457
- const isRange = this.isRange(keyNodeEntryRawOrPredicate);
446
+ const isRange = this.isRange(keyNodeEntryOrPredicate);
458
447
  // Set predicate based on parameter type
459
448
  if (isRange) {
460
- predicate = node => keyNodeEntryRawOrPredicate.isInRange(node.key, this._comparator);
449
+ predicate = node => keyNodeEntryOrPredicate.isInRange(node.key, this._comparator);
461
450
  }
462
451
  else {
463
- predicate = this._ensurePredicate(keyNodeEntryRawOrPredicate);
452
+ predicate = this._ensurePredicate(keyNodeEntryOrPredicate);
464
453
  }
465
454
  const isToLeftByRange = (cur) => {
466
455
  if (isRange) {
467
- const range = keyNodeEntryRawOrPredicate;
456
+ const range = keyNodeEntryOrPredicate;
468
457
  const leftS = this.isReverse ? range.high : range.low;
469
458
  const leftI = this.isReverse ? range.includeHigh : range.includeLow;
470
459
  return (leftI && this._compare(cur.key, leftS) >= 0) || (!leftI && this._compare(cur.key, leftS) > 0);
@@ -473,7 +462,7 @@ class BST extends binary_tree_1.BinaryTree {
473
462
  };
474
463
  const isToRightByRange = (cur) => {
475
464
  if (isRange) {
476
- const range = keyNodeEntryRawOrPredicate;
465
+ const range = keyNodeEntryOrPredicate;
477
466
  const rightS = this.isReverse ? range.low : range.high;
478
467
  const rightI = this.isReverse ? range.includeLow : range.includeLow;
479
468
  return (rightI && this._compare(cur.key, rightS) <= 0) || (!rightI && this._compare(cur.key, rightS) < 0);
@@ -496,8 +485,8 @@ class BST extends binary_tree_1.BinaryTree {
496
485
  if (this.isRealNode(cur.right) && isToRightByRange(cur))
497
486
  dfs(cur.right);
498
487
  }
499
- else if (!this._isPredicate(keyNodeEntryRawOrPredicate)) {
500
- const benchmarkKey = this._extractKey(keyNodeEntryRawOrPredicate);
488
+ else if (!this._isPredicate(keyNodeEntryOrPredicate)) {
489
+ const benchmarkKey = this._extractKey(keyNodeEntryOrPredicate);
501
490
  if (this.isRealNode(cur.left) &&
502
491
  benchmarkKey !== null &&
503
492
  benchmarkKey !== undefined &&
@@ -533,8 +522,8 @@ class BST extends binary_tree_1.BinaryTree {
533
522
  if (this.isRealNode(cur.right) && isToRightByRange(cur))
534
523
  stack.push(cur.right);
535
524
  }
536
- else if (!this._isPredicate(keyNodeEntryRawOrPredicate)) {
537
- const benchmarkKey = this._extractKey(keyNodeEntryRawOrPredicate);
525
+ else if (!this._isPredicate(keyNodeEntryOrPredicate)) {
526
+ const benchmarkKey = this._extractKey(keyNodeEntryOrPredicate);
538
527
  if (this.isRealNode(cur.right) &&
539
528
  benchmarkKey !== null &&
540
529
  benchmarkKey !== undefined &&
@@ -558,16 +547,16 @@ class BST extends binary_tree_1.BinaryTree {
558
547
  }
559
548
  /**
560
549
  * Time Complexity: O(log n)
561
- * Space Complexity: O(n)
550
+ * Space Complexity: O(k + log n)
562
551
  *
563
552
  * The `rangeSearch` function searches for nodes within a specified range in a binary search tree.
564
553
  * @param {Range<K> | [K, K]} range - The `range` parameter in the `rangeSearch` function can be
565
554
  * either a `Range` object or an array of two elements representing the range boundaries.
566
555
  * @param {C} callback - The `callback` parameter in the `rangeSearch` function is a callback
567
556
  * function that is used to process each node that is found within the specified range during the
568
- * search operation. It is of type `NodeCallback<NODE>`, where `NODE` is the type of nodes in the
557
+ * search operation. It is of type `NodeCallback<BSTNode<K, V>>`, where `BSTNode<K, V>` is the type of nodes in the
569
558
  * data structure.
570
- * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `rangeSearch`
559
+ * @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter in the `rangeSearch`
571
560
  * function represents the node from which the search for nodes within the specified range will
572
561
  * begin. It is the starting point for the range search operation.
573
562
  * @param {IterationType} iterationType - The `iterationType` parameter in the `rangeSearch` function
@@ -583,12 +572,12 @@ class BST extends binary_tree_1.BinaryTree {
583
572
  }
584
573
  /**
585
574
  * Time Complexity: O(log n)
586
- * Space Complexity: O(1)
575
+ * Space Complexity: O(log n)
587
576
  *
588
- * This function retrieves a node based on a given keyNodeEntryRawOrPredicate within a binary search tree structure.
589
- * @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The `keyNodeEntryRawOrPredicate`
590
- * parameter can be of type `BTNRep<K, V, NODE>`, `R`, or `NodePredicate<NODE>`.
591
- * @param {R | BSTNOptKeyOrNode<K, NODE>} startNode - The `startNode` parameter in the `getNode` method
577
+ * This function retrieves a node based on a given keyNodeEntryOrPredicate within a binary search tree structure.
578
+ * @param {BTNRep<K, V, BSTNode<K, V>> | NodePredicate<BSTNode<K, V>>} keyNodeEntryOrPredicate - The `keyNodeEntryOrPredicate`
579
+ * parameter can be of type `BTNRep<K, V, BSTNode<K, V>>`, `R`, or `NodePredicate<BSTNode<K, V>>`.
580
+ * @param {BSTNOptKeyOrNode<K, BSTNode<K, V>>} startNode - The `startNode` parameter in the `getNode` method
592
581
  * is used to specify the starting point for searching nodes in the binary search tree. If no
593
582
  * specific starting point is provided, the default value is set to `this._root`, which is the root
594
583
  * node of the binary search tree.
@@ -596,14 +585,14 @@ class BST extends binary_tree_1.BinaryTree {
596
585
  * parameter that specifies the type of iteration to be used. It has a default value of
597
586
  * `this.iterationType`, which means it will use the iteration type defined in the class instance if
598
587
  * no value is provided when calling the method.
599
- * @returns The `getNode` method is returning an optional binary search tree node (`OptNode<NODE>`).
600
- * It is using the `getNodes` method to find the node based on the provided keyNodeEntryRawOrPredicate, beginning at
588
+ * @returns The `getNode` method is returning an optional binary search tree node (`OptNode<BSTNode<K, V>>`).
589
+ * It is using the `getNodes` method to find the node based on the provided keyNodeEntryOrPredicate, beginning at
601
590
  * the specified root node (`startNode`) and using the specified iteration type. The method then
602
591
  * returns the first node found or `undefined` if no node is found.
603
592
  */
604
- getNode(keyNodeEntryRawOrPredicate, startNode = this._root, iterationType = this.iterationType) {
593
+ getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
605
594
  var _a;
606
- return (_a = this.getNodes(keyNodeEntryRawOrPredicate, true, startNode, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
595
+ return (_a = this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
607
596
  }
608
597
  /**
609
598
  * Time complexity: O(n)
@@ -617,7 +606,7 @@ class BST extends binary_tree_1.BinaryTree {
617
606
  * @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
618
607
  * order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
619
608
  * take one of the following values:
620
- * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
609
+ * @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting
621
610
  * point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
622
611
  * node entry. If not specified, the default value is the root of the tree.
623
612
  * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
@@ -637,7 +626,7 @@ class BST extends binary_tree_1.BinaryTree {
637
626
  * @param {C} callback - The `callback` parameter is a function that will be called for each node
638
627
  * visited during the breadth-first search. It should take a single argument, which is the current
639
628
  * node being visited, and it can return a value of any type.
640
- * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
629
+ * @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting
641
630
  * point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
642
631
  * object. If no value is provided, the default value is the root of the tree.
643
632
  * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
@@ -655,9 +644,9 @@ class BST extends binary_tree_1.BinaryTree {
655
644
  * The function overrides the listLevels method from the superclass and returns an array of arrays
656
645
  * containing the results of the callback function applied to each level of the tree.
657
646
  * @param {C} callback - The `callback` parameter is a generic type `C` that extends
658
- * `NodeCallback<NODE>`. It represents a callback function that will be called for each node in the
647
+ * `NodeCallback<BSTNode<K, V>>`. It represents a callback function that will be called for each node in the
659
648
  * tree during the iteration process.
660
- * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
649
+ * @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting
661
650
  * point for listing the levels of the binary tree. It can be either a root node of the tree, a
662
651
  * key-value pair representing a node in the tree, or a key representing a node in the tree. If no
663
652
  * value is provided, the root of
@@ -681,7 +670,7 @@ class BST extends binary_tree_1.BinaryTree {
681
670
  * @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
682
671
  * traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
683
672
  * 0, or 1, where:
684
- * @param {BTNRep<K, V, NODE> | R} targetNode - The `targetNode` parameter is the node in
673
+ * @param {BTNRep<K, V, BSTNode<K, V>>} targetNode - The `targetNode` parameter is the node in
685
674
  * the binary tree that you want to start traversing from. It can be specified either by providing
686
675
  * the key of the node, the node itself, or an entry containing the key and value of the node. If no
687
676
  * `targetNode` is provided,
@@ -817,7 +806,8 @@ class BST extends binary_tree_1.BinaryTree {
817
806
  while (stack.length > 0 || node) {
818
807
  if (node) {
819
808
  stack.push(node);
820
- node = node.left;
809
+ if (node.left !== null)
810
+ node = node.left;
821
811
  }
822
812
  else {
823
813
  node = stack[stack.length - 1];
@@ -841,24 +831,70 @@ class BST extends binary_tree_1.BinaryTree {
841
831
  return balanced;
842
832
  }
843
833
  /**
844
- * The function returns the value of the _comparator property.
845
- * @returns The `_comparator` property is being returned.
834
+ * Time complexity: O(n)
835
+ * Space complexity: O(n)
836
+ *
837
+ * The `map` function in TypeScript overrides the default map behavior for a binary search tree by
838
+ * applying a callback function to each entry and creating a new tree with the results.
839
+ * @param callback - A function that will be called for each entry in the BST. It takes four
840
+ * arguments: the key, the value (which can be undefined), the index of the entry, and a reference to
841
+ * the BST itself.
842
+ * @param [options] - The `options` parameter in the `override map` method is of type `BSTOptions<MK,
843
+ * MV, MR>`. It is an optional parameter that allows you to specify additional options for the Binary
844
+ * Search Tree (BST) being created in the `map` method. These options could include configuration
845
+ * @param {any} [thisArg] - The `thisArg` parameter in the `override map` method is used to specify
846
+ * the value of `this` that should be used when executing the `callback` function. It allows you to
847
+ * set the context or scope in which the callback function will be called. This can be useful when
848
+ * you want
849
+ * @returns The `map` method is returning a new Binary Search Tree (`BST`) instance with the entries
850
+ * transformed by the provided callback function.
846
851
  */
847
- get comparator() {
848
- return this._comparator;
852
+ map(callback, options, thisArg) {
853
+ const newTree = new BST([], options);
854
+ let index = 0;
855
+ for (const [key, value] of this) {
856
+ newTree.add(callback.call(thisArg, key, value, index++, this));
857
+ }
858
+ return newTree;
849
859
  }
850
860
  /**
851
- * This function returns the value of the `_extractComparable` property.
852
- * @returns The method `extractComparable()` is being returned, which is a getter method for the
853
- * `_extractComparable` property.
861
+ * Time complexity: O(n)
862
+ * Space complexity: O(n)
863
+ *
864
+ * The function `clone` overrides the default cloning behavior to create a deep copy of a tree
865
+ * structure.
866
+ * @returns The `cloned` object is being returned.
867
+ */
868
+ clone() {
869
+ const cloned = this.createTree();
870
+ this._clone(cloned);
871
+ return cloned;
872
+ }
873
+ /**
874
+ * Time Complexity: O(1)
875
+ * Space Complexity: O(1)
876
+ *
877
+ * The function overrides a method and converts a key, value pair or entry or raw element to a node.
878
+ * @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - A variable that can be of
879
+ * type R or BTNRep<K, V, BSTNode<K, V>>. It represents either a key, a node, an entry, or a raw
880
+ * element.
881
+ * @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
882
+ * value associated with a key in a key-value pair.
883
+ * @returns either a BSTNode<K, V> object or undefined.
854
884
  */
855
- get extractComparable() {
856
- return this._extractComparable;
885
+ _keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value) {
886
+ const [node, entryValue] = super._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
887
+ if (node === null)
888
+ return [undefined, undefined];
889
+ return [node, value !== null && value !== void 0 ? value : entryValue];
857
890
  }
858
891
  /**
892
+ * Time Complexity: O(1)
893
+ * Space Complexity: O(1)
894
+ *
859
895
  * The function sets the root of a tree-like structure and updates the parent property of the new
860
896
  * root.
861
- * @param {OptNode<NODE>} v - v is a parameter of type NODE or undefined.
897
+ * @param {OptNode<BSTNode<K, V>>} v - v is a parameter of type BSTNode<K, V> or undefined.
862
898
  */
863
899
  _setRoot(v) {
864
900
  if (v) {
@@ -866,6 +902,20 @@ class BST extends binary_tree_1.BinaryTree {
866
902
  }
867
903
  this._root = v;
868
904
  }
905
+ /**
906
+ * Time Complexity: O(1)
907
+ * Space Complexity: O(1)
908
+ *
909
+ * The _compare function compares two values using a specified comparator function and optionally
910
+ * reverses the result.
911
+ * @param {K} a - The parameter `a` is of type `K`, which is used as an input for comparison in the
912
+ * `_compare` method.
913
+ * @param {K} b - The parameter `b` in the `_compare` function is of type `K`.
914
+ * @returns The `_compare` method is returning the result of the ternary expression. If `_isReverse`
915
+ * is true, it returns the negation of the result of calling the `_comparator` function with
916
+ * arguments `a` and `b`. If `_isReverse` is false, it returns the result of calling the
917
+ * `_comparator` function with arguments `a` and `b`.
918
+ */
869
919
  _compare(a, b) {
870
920
  return this._isReverse ? -this._comparator(a, b) : this._comparator(a, b);
871
921
  }