heap-typed 1.51.7 → 1.51.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/README.md +72 -80
  2. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +103 -74
  3. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +116 -93
  4. package/dist/data-structures/binary-tree/avl-tree.d.ts +82 -62
  5. package/dist/data-structures/binary-tree/avl-tree.js +90 -71
  6. package/dist/data-structures/binary-tree/binary-tree.d.ts +318 -233
  7. package/dist/data-structures/binary-tree/binary-tree.js +492 -392
  8. package/dist/data-structures/binary-tree/bst.d.ts +204 -251
  9. package/dist/data-structures/binary-tree/bst.js +256 -358
  10. package/dist/data-structures/binary-tree/rb-tree.d.ts +74 -85
  11. package/dist/data-structures/binary-tree/rb-tree.js +111 -119
  12. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +92 -76
  13. package/dist/data-structures/binary-tree/tree-multi-map.js +105 -93
  14. package/dist/data-structures/graph/abstract-graph.d.ts +10 -15
  15. package/dist/data-structures/graph/abstract-graph.js +10 -15
  16. package/dist/data-structures/hash/hash-map.d.ts +31 -38
  17. package/dist/data-structures/hash/hash-map.js +40 -55
  18. package/dist/data-structures/heap/heap.d.ts +1 -3
  19. package/dist/data-structures/queue/deque.d.ts +2 -3
  20. package/dist/data-structures/queue/deque.js +2 -3
  21. package/dist/data-structures/trie/trie.d.ts +1 -1
  22. package/dist/data-structures/trie/trie.js +1 -1
  23. package/dist/interfaces/binary-tree.d.ts +7 -7
  24. package/dist/types/common.d.ts +2 -3
  25. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +4 -3
  26. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +4 -3
  27. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +6 -5
  28. package/dist/types/data-structures/binary-tree/bst.d.ts +6 -5
  29. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +4 -3
  30. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +4 -3
  31. package/dist/types/utils/utils.d.ts +10 -1
  32. package/dist/utils/utils.d.ts +2 -1
  33. package/dist/utils/utils.js +27 -1
  34. package/package.json +2 -2
  35. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +142 -100
  36. package/src/data-structures/binary-tree/avl-tree.ts +109 -80
  37. package/src/data-structures/binary-tree/binary-tree.ts +556 -433
  38. package/src/data-structures/binary-tree/bst.ts +286 -375
  39. package/src/data-structures/binary-tree/rb-tree.ts +132 -125
  40. package/src/data-structures/binary-tree/tree-multi-map.ts +129 -102
  41. package/src/data-structures/graph/abstract-graph.ts +10 -10
  42. package/src/data-structures/hash/hash-map.ts +42 -49
  43. package/src/data-structures/heap/heap.ts +1 -1
  44. package/src/data-structures/queue/deque.ts +2 -2
  45. package/src/data-structures/queue/queue.ts +1 -1
  46. package/src/data-structures/trie/trie.ts +2 -2
  47. package/src/interfaces/binary-tree.ts +11 -9
  48. package/src/types/common.ts +2 -3
  49. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +4 -3
  50. package/src/types/data-structures/binary-tree/avl-tree.ts +4 -3
  51. package/src/types/data-structures/binary-tree/binary-tree.ts +7 -6
  52. package/src/types/data-structures/binary-tree/bst.ts +6 -5
  53. package/src/types/data-structures/binary-tree/rb-tree.ts +4 -3
  54. package/src/types/data-structures/binary-tree/tree-multi-map.ts +4 -3
  55. package/src/types/utils/utils.ts +14 -1
  56. package/src/utils/utils.ts +20 -1
@@ -95,37 +95,29 @@ exports.BinaryTreeNode = BinaryTreeNode;
95
95
  */
96
96
  class BinaryTree extends base_1.IterableEntryBase {
97
97
  /**
98
- * The constructor function initializes a binary tree object with optional keysOrNodesOrEntries and options.
99
- * @param [keysOrNodesOrEntries] - An optional iterable of KeyOrNodeOrEntry objects. These objects represent the
98
+ * The constructor function initializes a binary tree object with optional keysOrNodesOrEntriesOrRawElements and options.
99
+ * @param [keysOrNodesOrEntriesOrRawElements] - An optional iterable of KeyOrNodeOrEntry objects. These objects represent the
100
100
  * nodes to be added to the binary tree.
101
101
  * @param [options] - The `options` parameter is an optional object that can contain additional
102
102
  * configuration options for the binary tree. In this case, it is of type
103
103
  * `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
104
104
  * required.
105
105
  */
106
- constructor(keysOrNodesOrEntries = [], options) {
106
+ constructor(keysOrNodesOrEntriesOrRawElements = [], options) {
107
107
  super();
108
108
  this.iterationType = 'ITERATIVE';
109
- this._extractor = (key) => (typeof key === 'number' ? key : Number(key));
109
+ this._size = 0;
110
110
  this._NIL = new BinaryTreeNode(NaN);
111
111
  this._DEFAULT_CALLBACK = (node) => (node ? node.key : undefined);
112
112
  if (options) {
113
- const { iterationType, extractor } = options;
113
+ const { iterationType, toEntryFn } = options;
114
114
  if (iterationType)
115
115
  this.iterationType = iterationType;
116
- if (extractor)
117
- this._extractor = extractor;
116
+ if (typeof toEntryFn === 'function')
117
+ this._toEntryFn = toEntryFn;
118
118
  }
119
- this._size = 0;
120
- if (keysOrNodesOrEntries)
121
- this.addMany(keysOrNodesOrEntries);
122
- }
123
- /**
124
- * The function returns the value of the `_extractor` property.
125
- * @returns The `_extractor` property is being returned.
126
- */
127
- get extractor() {
128
- return this._extractor;
119
+ if (keysOrNodesOrEntriesOrRawElements)
120
+ this.addMany(keysOrNodesOrEntriesOrRawElements);
129
121
  }
130
122
  /**
131
123
  * The function returns the root node, which can be of type NODE, null, or undefined.
@@ -149,6 +141,13 @@ class BinaryTree extends base_1.IterableEntryBase {
149
141
  get NIL() {
150
142
  return this._NIL;
151
143
  }
144
+ /**
145
+ * The function returns the value of the _toEntryFn property.
146
+ * @returns The function being returned is `this._toEntryFn`.
147
+ */
148
+ get toEntryFn() {
149
+ return this._toEntryFn;
150
+ }
152
151
  /**
153
152
  * Creates a new instance of BinaryTreeNode with the given key and value.
154
153
  * @param {K} key - The key for the new node.
@@ -169,42 +168,42 @@ class BinaryTree extends base_1.IterableEntryBase {
169
168
  return new BinaryTree([], Object.assign({ iterationType: this.iterationType }, options));
170
169
  }
171
170
  /**
172
- * The function `keyValueOrEntryToNode` converts an keyOrNodeOrEntry object into a node object.
173
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
171
+ * The function `keyValueOrEntryOrRawElementToNode` converts a key-value pair, entry, or raw element
172
+ * into a node object.
173
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
174
+ * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
174
175
  * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
175
- * `keyValueOrEntryToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If no value
176
- * is provided, it will be `undefined`.
177
- * @returns a value of type NODE (node), or null, or undefined.
176
+ * `keyValueOrEntryOrRawElementToNode` function. It represents the value associated with a key in a
177
+ * key-value pair. If provided, it will be used to create a node with the specified key and value.
178
+ * @returns The function `keyValueOrEntryOrRawElementToNode` returns either a `NODE` object, `null`,
179
+ * or `undefined`.
178
180
  */
179
- keyValueOrEntryToNode(keyOrNodeOrEntry, value) {
180
- if (keyOrNodeOrEntry === undefined)
181
+ keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value) {
182
+ if (keyOrNodeOrEntryOrRawElement === undefined)
181
183
  return;
182
- let node;
183
- if (keyOrNodeOrEntry === null) {
184
- node = null;
185
- }
186
- else if (this.isEntry(keyOrNodeOrEntry)) {
187
- const [key, value] = keyOrNodeOrEntry;
188
- if (key === undefined) {
184
+ if (keyOrNodeOrEntryOrRawElement === null)
185
+ return null;
186
+ if (this.isNode(keyOrNodeOrEntryOrRawElement))
187
+ return keyOrNodeOrEntryOrRawElement;
188
+ if (this.toEntryFn) {
189
+ const [key, entryValue] = this.toEntryFn(keyOrNodeOrEntryOrRawElement);
190
+ if (key)
191
+ return this.createNode(key, entryValue !== null && entryValue !== void 0 ? entryValue : value);
192
+ else
189
193
  return;
190
- }
191
- else if (key === null) {
192
- node = null;
193
- }
194
- else {
195
- node = this.createNode(key, value);
196
- }
197
194
  }
198
- else if (this.isNode(keyOrNodeOrEntry)) {
199
- node = keyOrNodeOrEntry;
200
- }
201
- else if (!this.isNode(keyOrNodeOrEntry)) {
202
- node = this.createNode(keyOrNodeOrEntry, value);
203
- }
204
- else {
205
- return;
195
+ if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
196
+ const [key, value] = keyOrNodeOrEntryOrRawElement;
197
+ if (key === undefined)
198
+ return;
199
+ else if (key === null)
200
+ return null;
201
+ else
202
+ return this.createNode(key, value);
206
203
  }
207
- return node;
204
+ if (this.isKey(keyOrNodeOrEntryOrRawElement))
205
+ return this.createNode(keyOrNodeOrEntryOrRawElement, value);
206
+ return;
208
207
  }
209
208
  /**
210
209
  * Time Complexity: O(n)
@@ -214,56 +213,56 @@ class BinaryTree extends base_1.IterableEntryBase {
214
213
  * Time Complexity: O(n)
215
214
  * Space Complexity: O(log n)
216
215
  *
217
- * The function `ensureNode` returns the node corresponding to the given key if it is a valid node
218
- * key, otherwise it returns the key itself.
219
- * @param {K | NODE | null | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `NODE`,
220
- * `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
221
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
222
- * type of iteration to be used when searching for a node by key. It has a default value of
223
- * `'ITERATIVE'`.
224
- * @returns either the node corresponding to the given key if it is a valid node key, or the key
225
- * itself if it is not a valid node key.
226
- */
227
- ensureNode(keyOrNodeOrEntry, iterationType = 'ITERATIVE') {
228
- if (keyOrNodeOrEntry === this.NIL)
216
+ * The `ensureNode` function checks if the input is a valid node and returns it, or converts it to a
217
+ * node if it is a key or entry.
218
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
219
+ * `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, `KeyOrNodeOrEntry<K, V, NODE>`, or
220
+ * a raw element.
221
+ * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
222
+ * parameter that specifies the type of iteration to be used when searching for a node. It has a
223
+ * default value of `'ITERATIVE'`.
224
+ * @returns The function `ensureNode` returns either a `NODE` object, `null`, or `undefined`.
225
+ */
226
+ ensureNode(keyOrNodeOrEntryOrRawElement, iterationType = 'ITERATIVE') {
227
+ if (keyOrNodeOrEntryOrRawElement === null)
228
+ return null;
229
+ if (keyOrNodeOrEntryOrRawElement === undefined)
229
230
  return;
230
- if (this.isRealNode(keyOrNodeOrEntry)) {
231
- return keyOrNodeOrEntry;
231
+ if (keyOrNodeOrEntryOrRawElement === this.NIL)
232
+ return;
233
+ if (this.isNode(keyOrNodeOrEntryOrRawElement))
234
+ return keyOrNodeOrEntryOrRawElement;
235
+ if (this.toEntryFn) {
236
+ const [key] = this.toEntryFn(keyOrNodeOrEntryOrRawElement);
237
+ if (key)
238
+ return this.getNodeByKey(key);
232
239
  }
233
- if (this.isEntry(keyOrNodeOrEntry)) {
234
- const key = keyOrNodeOrEntry[0];
240
+ if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
241
+ const key = keyOrNodeOrEntryOrRawElement[0];
235
242
  if (key === null)
236
243
  return null;
237
244
  if (key === undefined)
238
245
  return;
239
246
  return this.getNodeByKey(key, iterationType);
240
247
  }
241
- if (keyOrNodeOrEntry === null)
242
- return null;
243
- if (keyOrNodeOrEntry === undefined)
244
- return;
245
- return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
246
- }
247
- /**
248
- * The function checks if a given node is a real node or null.
249
- * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
250
- * @returns a boolean value.
251
- */
252
- isNodeOrNull(node) {
253
- return this.isRealNode(node) || node === null;
248
+ if (this.isKey(keyOrNodeOrEntryOrRawElement))
249
+ return this.getNodeByKey(keyOrNodeOrEntryOrRawElement, iterationType);
250
+ return;
254
251
  }
255
252
  /**
256
- * The function "isNode" checks if an keyOrNodeOrEntry is an instance of the BinaryTreeNode class.
257
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V,NODE>`.
258
- * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the class NODE.
253
+ * The function checks if the input is an instance of the BinaryTreeNode class.
254
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
255
+ * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
256
+ * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
257
+ * an instance of the `BinaryTreeNode` class.
259
258
  */
260
- isNode(keyOrNodeOrEntry) {
261
- return keyOrNodeOrEntry instanceof BinaryTreeNode;
259
+ isNode(keyOrNodeOrEntryOrRawElement) {
260
+ return keyOrNodeOrEntryOrRawElement instanceof BinaryTreeNode;
262
261
  }
263
262
  /**
264
- * The function checks if a given node is a real node by verifying if it is an instance of
265
- * BinaryTreeNode and its key is not NaN.
266
- * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
263
+ * The function checks if a given node is a valid node in a binary search tree.
264
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
265
+ * `KeyOrNodeOrEntry<K, V, NODE>`.
267
266
  * @returns a boolean value.
268
267
  */
269
268
  isRealNode(node) {
@@ -272,21 +271,64 @@ class BinaryTree extends base_1.IterableEntryBase {
272
271
  return this.isNode(node);
273
272
  }
274
273
  /**
275
- * The function checks if a given node is a BinaryTreeNode instance and has a key value of NaN.
276
- * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
274
+ * The function checks if a given node is a real node or null.
275
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
276
+ * `KeyOrNodeOrEntry<K, V, NODE>`.
277
+ * @returns a boolean value.
278
+ */
279
+ isNodeOrNull(node) {
280
+ return this.isRealNode(node) || node === null;
281
+ }
282
+ /**
283
+ * The function checks if a given node is equal to the NIL value.
284
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
285
+ * `KeyOrNodeOrEntry<K, V, NODE>`.
277
286
  * @returns a boolean value.
278
287
  */
279
288
  isNIL(node) {
280
289
  return node === this.NIL;
281
290
  }
282
291
  /**
283
- * The function checks if a given value is an entry in a binary tree node.
284
- * @param keyOrNodeOrEntry - KeyOrNodeOrEntry<K, V,NODE> - A generic type representing a node in a binary tree. It has
285
- * two type parameters V and NODE, representing the value and node type respectively.
292
+ * The function checks if the input is an array with two elements, indicating it is a binary tree
293
+ * node entry.
294
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
295
+ * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
286
296
  * @returns a boolean value.
287
297
  */
288
- isEntry(keyOrNodeOrEntry) {
289
- return Array.isArray(keyOrNodeOrEntry) && keyOrNodeOrEntry.length === 2;
298
+ isEntry(keyOrNodeOrEntryOrRawElement) {
299
+ return Array.isArray(keyOrNodeOrEntryOrRawElement) && keyOrNodeOrEntryOrRawElement.length === 2;
300
+ }
301
+ /**
302
+ * The function checks if a given value is a valid key by evaluating its type and value.
303
+ * @param {any} key - The `key` parameter can be of any type. It is the value that we want to check
304
+ * if it is a valid key.
305
+ * @param [isCheckValueOf=true] - The `isCheckValueOf` parameter is a boolean flag that determines
306
+ * whether the function should check the valueOf() method of an object when the key is of type
307
+ * 'object'. If `isCheckValueOf` is true, the function will recursively call itself with the value
308
+ * returned by key.valueOf().
309
+ * @returns a boolean value.
310
+ */
311
+ isKey(key, isCheckValueOf = true) {
312
+ if (key === null)
313
+ return true;
314
+ const keyType = typeof key;
315
+ if (keyType === 'string' || keyType === 'bigint' || keyType === 'boolean')
316
+ return true;
317
+ if (keyType === 'number')
318
+ return !isNaN(key);
319
+ if (keyType === 'symbol' || keyType === 'undefined')
320
+ return false;
321
+ if (keyType === 'function')
322
+ return this.isKey(key());
323
+ if (keyType === 'object') {
324
+ if (typeof key.toString === 'function')
325
+ return true;
326
+ if (isCheckValueOf && typeof key.valueOf === 'function') {
327
+ this.isKey(key.valueOf(), false);
328
+ }
329
+ return false;
330
+ }
331
+ return false;
290
332
  }
291
333
  /**
292
334
  * Time Complexity O(n)
@@ -296,14 +338,20 @@ class BinaryTree extends base_1.IterableEntryBase {
296
338
  * Time Complexity O(n)
297
339
  * Space Complexity O(1)
298
340
  *
299
- * The `add` function adds a new node to a binary tree, either by creating a new node or replacing an
300
- * existing node with the same key.
301
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
302
- * @param {V} [value] - The value to be inserted into the binary tree.
303
- * @returns The function `add` returns either a node (`NODE`), `null`, or `undefined`.
304
- */
305
- add(keyOrNodeOrEntry, value) {
306
- const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
341
+ * The `add` function is used to insert a new node into a binary tree, checking for duplicate keys
342
+ * and finding the appropriate insertion position.
343
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
344
+ * `keyOrNodeOrEntryOrRawElement` parameter can accept a value of type `R`, which represents the key,
345
+ * node, entry, or raw element to be added to the tree. It can also accept a value of type
346
+ * `KeyOrNodeOrEntry<K, V, NODE>
347
+ * @param {V} [value] - The `value` parameter is an optional value that can be associated with the
348
+ * key being added to the tree. It represents the value that will be stored in the tree for the given
349
+ * key.
350
+ * @returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
351
+ * insertion position cannot be found or if there are duplicate keys.
352
+ */
353
+ add(keyOrNodeOrEntryOrRawElement, value) {
354
+ const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
307
355
  if (newNode === undefined)
308
356
  return false;
309
357
  // If the tree is empty, directly set the new node as the root node
@@ -357,20 +405,24 @@ class BinaryTree extends base_1.IterableEntryBase {
357
405
  * Time Complexity: O(k * n)
358
406
  * Space Complexity: O(1)
359
407
  *
360
- * The `addMany` function takes in a collection of keysOrNodesOrEntries and an optional collection of values, and
361
- * adds each node with its corresponding value to the data structure.
362
- * @param keysOrNodesOrEntries - An iterable collection of KeyOrNodeOrEntry objects.
363
- * @param [values] - An optional iterable of values that will be assigned to each node being added.
364
- * @returns The function `addMany` returns an array of `NODE`, `null`, or `undefined` values.
365
- */
366
- addMany(keysOrNodesOrEntries, values) {
408
+ * The `addMany` function takes in an iterable of keys or nodes or entries or raw elements, and an
409
+ * optional iterable of values, and adds each key or node or entry with its corresponding value to a
410
+ * data structure, returning an array of booleans indicating whether each insertion was successful.
411
+ * @param keysOrNodesOrEntriesOrRawElements - An iterable containing keys, nodes, entries, or raw
412
+ * elements. These elements will be added to the data structure.
413
+ * @param [values] - An optional iterable of values that correspond to the keys or nodes or entries
414
+ * in the `keysOrNodesOrEntriesOrRawElements` parameter.
415
+ * @returns The function `addMany` returns an array of booleans indicating whether each element was
416
+ * successfully added to the data structure.
417
+ */
418
+ addMany(keysOrNodesOrEntriesOrRawElements, values) {
367
419
  // TODO not sure addMany not be run multi times
368
420
  const inserted = [];
369
421
  let valuesIterator;
370
422
  if (values) {
371
423
  valuesIterator = values[Symbol.iterator]();
372
424
  }
373
- for (const keyOrNodeOrEntry of keysOrNodesOrEntries) {
425
+ for (const keyOrNodeOrEntryOrRawElement of keysOrNodesOrEntriesOrRawElements) {
374
426
  let value = undefined;
375
427
  if (valuesIterator) {
376
428
  const valueResult = valuesIterator.next();
@@ -378,7 +430,7 @@ class BinaryTree extends base_1.IterableEntryBase {
378
430
  value = valueResult.value;
379
431
  }
380
432
  }
381
- inserted.push(this.add(keyOrNodeOrEntry, value));
433
+ inserted.push(this.add(keyOrNodeOrEntryOrRawElement, value));
382
434
  }
383
435
  return inserted;
384
436
  }
@@ -391,36 +443,34 @@ class BinaryTree extends base_1.IterableEntryBase {
391
443
  * Time Complexity: O(k * n)
392
444
  * Space Complexity: O(1)
393
445
  *
394
- * The `refill` function clears the current data and adds new key-value pairs to the data structure.
395
- * @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries. These can be of type
396
- * KeyOrNodeOrEntry<K, V, NODE>.
397
- * @param [values] - The `values` parameter is an optional iterable that contains the values to be
398
- * associated with the keys or nodes or entries in the `keysOrNodesOrEntries` parameter. If provided,
399
- * the values will be associated with the corresponding keys or nodes or entries in the
400
- * `keysOrNodesOrEntries` iterable
401
- */
402
- refill(keysOrNodesOrEntries, values) {
446
+ * The `refill` function clears the current data and adds new data to the collection.
447
+ * @param keysOrNodesOrEntriesOrRawElements - An iterable collection of keys, nodes, entries, or raw
448
+ * elements. These can be of any type (R) or a specific type (KeyOrNodeOrEntry<K, V, NODE>).
449
+ * @param [values] - The `values` parameter is an optional iterable of values that will be associated
450
+ * with the keys or nodes being added. If provided, the values will be assigned to the corresponding
451
+ * keys or nodes. If not provided, the values will be set to `undefined`.
452
+ */
453
+ refill(keysOrNodesOrEntriesOrRawElements, values) {
403
454
  this.clear();
404
- this.addMany(keysOrNodesOrEntries, values);
455
+ this.addMany(keysOrNodesOrEntriesOrRawElements, values);
405
456
  }
406
457
  /**
407
458
  * Time Complexity: O(n)
408
459
  * Space Complexity: O(1)
409
- * /
410
-
411
- /**
460
+ */
461
+ /**
412
462
  * Time Complexity: O(n)
413
463
  * Space Complexity: O(1)
414
464
  *
415
- * The function deletes a node from a binary tree and returns an array of the deleted nodes along
416
- * with the nodes that need to be balanced.
417
- * @param {ReturnType<C> | null | undefined} identifier - The identifier parameter is the value or
418
- * object that you want to delete from the binary tree. It can be of any type that is compatible with
419
- * the callback function's return type. It can also be null or undefined if you want to delete a
420
- * specific node based on its value or object.
465
+ * The above function is a TypeScript implementation of deleting a node from a binary tree, returning
466
+ * the deleted node and the node that needs to be balanced.
467
+ * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
468
+ * used to identify the node that needs to be deleted from the binary tree. It can be of any type
469
+ * that is returned by the callback function.
421
470
  * @param {C} callback - The `callback` parameter is a function that is used to determine the
422
- * identifier of the node to be deleted. It is optional and has a default value of
423
- * `this._DEFAULT_CALLBACK`. The `callback` function should return the identifier of the node.
471
+ * identifier of the node to be deleted. It is of type `C`, which extends the `BTNCallback<NODE>`
472
+ * interface. The `BTNCallback<NODE>` interface represents a callback function that takes a node of
473
+ * type `NODE
424
474
  * @returns an array of `BinaryTreeDeleteResult<NODE>`.
425
475
  */
426
476
  delete(identifier, callback = this._DEFAULT_CALLBACK) {
@@ -475,28 +525,27 @@ class BinaryTree extends base_1.IterableEntryBase {
475
525
  */
476
526
  /**
477
527
  * Time Complexity: O(n)
478
- * Space Complexity: O(k + log n).
528
+ * Space Complexity: O(k + log n)
479
529
  *
480
- * The function `getNodes` retrieves nodes from a binary tree based on a given identifier and
481
- * callback function.
530
+ * The function `getNodes` returns an array of nodes that match a given identifier, using either a
531
+ * recursive or iterative approach.
482
532
  * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
483
- * that you want to search for in the binary tree. It can be of any type that is returned by the
484
- * callback function `C`. It can also be `null` or `undefined` if you don't want to search for a
485
- * specific value.
486
- * @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as
487
- * input and returns a value of type `C`. It is used to determine if a node matches the given
488
- * identifier. If no callback is provided, the `_DEFAULT_CALLBACK` function is used as the
489
- * default
490
- * @param [onlyOne=false] - A boolean value indicating whether to only return the first node that
491
- * matches the identifier. If set to true, the function will stop iterating once it finds a matching
492
- * node and return that node. If set to false (default), the function will continue iterating and
493
- * return all nodes that match the identifier.
494
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the
495
- * starting node for the traversal. It can be either a key, a node object, or `null`/`undefined`. If
496
- * it is `null` or `undefined`, an empty array will be returned.
497
- * @param iterationType - The `iterationType` parameter determines the type of iteration used to
498
- * traverse the binary tree. It can have two possible values:
499
- * @returns an array of nodes of type `NODE`.
533
+ * that is used to identify the nodes. It can be of any type and is used to match against the result
534
+ * of the callback function for each node.
535
+ * @param {C} callback - The `callback` parameter is a function that takes a node as input and
536
+ * returns a value. This value is used to identify the nodes that match the given identifier. The
537
+ * `callback` function is optional and defaults to a default callback function
538
+ * (`this._DEFAULT_CALLBACK`) if not provided.
539
+ * @param [onlyOne=false] - A boolean value indicating whether to return only one node that matches
540
+ * the identifier or all nodes that match the identifier. If set to true, only the first matching
541
+ * node will be returned. If set to false, all matching nodes will be returned. The default value is
542
+ * false.
543
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
544
+ * point for the search. It can be either a node object, a key-value pair, or a key. If it is not
545
+ * provided, the `root` of the data structure is used as the starting point.
546
+ * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
547
+ * iteration to be performed on the nodes of a binary tree. It can have two possible values:
548
+ * @returns an array of NODE objects.
500
549
  */
501
550
  getNodes(identifier, callback = this._DEFAULT_CALLBACK, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
502
551
  beginRoot = this.ensureNode(beginRoot);
@@ -541,24 +590,21 @@ class BinaryTree extends base_1.IterableEntryBase {
541
590
  */
542
591
  /**
543
592
  * Time Complexity: O(n)
544
- * Space Complexity: O(log n)
593
+ * Space Complexity: O(log n).
545
594
  *
546
- * The function `getNode` returns the first node that matches the given identifier and callback
547
- * function.
595
+ * The function `getNode` returns the first node that matches the given identifier and callback,
596
+ * starting from the specified root node and using the specified iteration type.
548
597
  * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
549
- * used to identify the node you want to retrieve. It can be of any type that is returned by the
550
- * callback function `C`. It can also be `null` or `undefined` if you don't have a specific
551
- * identifier.
552
- * @param {C} callback - The `callback` parameter is a function that will be called for each node in
553
- * the binary tree. It is used to determine if a node matches the given identifier. The `callback`
554
- * function should take a single parameter of type `NODE` (the type of the nodes in the binary tree) and
555
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
556
- * for searching the binary tree. It can be either a key value, a node object, or `null`/`undefined`.
557
- * If `null` or `undefined` is passed, the search will start from the root of the binary tree.
558
- * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
559
- * be performed when searching for nodes in the binary tree. It determines the order in which the
560
- * nodes are visited during the search.
561
- * @returns a value of type `NODE | null | undefined`.
598
+ * used to identify the node you want to retrieve. It can be of any type that is the return type of
599
+ * the `C` callback function, or it can be `null` or `undefined`.
600
+ * @param {C} callback - The `callback` parameter is a function that will be used to determine if a
601
+ * node matches the desired criteria. It should return a value that can be used to identify the node.
602
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
603
+ * point for searching nodes in a tree structure. It can be either a root node, a key-value pair, or
604
+ * a node entry. If not provided, the search will start from the root of the tree.
605
+ * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
606
+ * of iteration to be performed when searching for nodes. It can have one of the following values:
607
+ * @returns The method is returning a NODE object, or null, or undefined.
562
608
  */
563
609
  getNode(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
564
610
  var _a;
@@ -572,15 +618,13 @@ class BinaryTree extends base_1.IterableEntryBase {
572
618
  * Time Complexity: O(n)
573
619
  * Space Complexity: O(log n)
574
620
  *
575
- * The function `getNodeByKey` searches for a node in a binary tree by its key, using either
576
- * recursive or iterative iteration.
577
- * @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
578
- * It is used to find the node with the matching key value.
579
- * @param iterationType - The `iterationType` parameter is used to determine whether the search for
580
- * the node with the given key should be performed iteratively or recursively. It has two possible
581
- * values:
582
- * @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
583
- * found in the binary tree. If no node is found, it returns `undefined`.
621
+ * The function `getNodeByKey` returns a node with a specific key value from a tree structure.
622
+ * @param {K} key - The key parameter is the value that you want to search for in the tree. It is
623
+ * used to find the node with the matching key value.
624
+ * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
625
+ * parameter that specifies the type of iteration to be used when searching for a node in the tree.
626
+ * It has a default value of `'ITERATIVE'`.
627
+ * @returns a value of type NODE, null, or undefined.
584
628
  */
585
629
  getNodeByKey(key, iterationType = 'ITERATIVE') {
586
630
  return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
@@ -593,23 +637,22 @@ class BinaryTree extends base_1.IterableEntryBase {
593
637
  * Time Complexity: O(n)
594
638
  * Space Complexity: O(log n)
595
639
  *
596
- * The function `get` retrieves the value of a node in a binary tree based on the provided identifier
597
- * and callback function.
640
+ * The function `get` in TypeScript overrides the base class method and returns the value associated
641
+ * with the given identifier.
598
642
  * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
599
- * used to identify the node in the binary tree. It can be of any type that is the return type of the
643
+ * used to identify the node in the binary tree. It can be of any type that is returned by the
600
644
  * callback function `C`. It can also be `null` or `undefined` if no identifier is provided.
601
- * @param {C} callback - The `callback` parameter is a function that will be called with each node in
602
- * the binary tree. It is used to determine whether a node matches the given identifier. The callback
603
- * function should return a value that can be compared to the identifier to determine if it is a
604
- * match.
605
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
606
- * for the search in the binary tree. It can be specified as a `K` (a unique identifier for a
607
- * node), a node object of type `NODE`, or `null`/`undefined` to start the search from the root of
608
- * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
609
- * be performed when searching for a node in the binary tree. It is an optional parameter with a
610
- * default value specified by `this.iterationType`.
611
- * @returns The value of the node with the given identifier is being returned. If the node is not
612
- * found, `undefined` is returned.
645
+ * @param {C} callback - The `callback` parameter is a function that will be used to determine if a
646
+ * node matches the given identifier. It is optional and defaults to `this._DEFAULT_CALLBACK`.
647
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
648
+ * point for the search in the binary tree. It can be either a root node of the tree or a key, node,
649
+ * or entry object that exists in the tree. If no specific starting point is provided, the search
650
+ * will begin from the root of the
651
+ * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
652
+ * of iteration to be performed when searching for a node in the tree. It can have one of the
653
+ * following values:
654
+ * @returns The method is returning the value associated with the specified identifier in the binary
655
+ * tree.
613
656
  */
614
657
  get(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
615
658
  var _a;
@@ -617,28 +660,27 @@ class BinaryTree extends base_1.IterableEntryBase {
617
660
  }
618
661
  /**
619
662
  * Time Complexity: O(n)
620
- * Space Complexity: O(log n).
663
+ * Space Complexity: O(log n)
621
664
  */
622
665
  /**
623
666
  * Time Complexity: O(n)
624
- * Space Complexity: O(log n).
667
+ * Space Complexity: O(log n)
625
668
  *
626
- * The function checks if a Binary Tree Node with a specific identifier exists in the tree.
669
+ * The `has` function checks if a given identifier exists in the data structure and returns a boolean
670
+ * value.
627
671
  * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
628
- * that you want to search for in the binary tree. It can be of any type that is returned by the
629
- * callback function `C`. It can also be `null` or `undefined` if you don't want to specify a
630
- * specific identifier.
631
- * @param {C} callback - The `callback` parameter is a function that will be called for each node in
632
- * the binary tree. It is used to filter the nodes based on certain conditions. The `callback`
633
- * function should return a boolean value indicating whether the node should be included in the
634
- * result or not.
635
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
636
- * for the search in the binary tree. It can be specified as a `K` (a unique identifier for a
637
- * node in the binary tree), a node object (`NODE`), or `null`/`undefined` to start the search from
638
- * @param iterationType - The `iterationType` parameter is a variable that determines the type of
639
- * iteration to be performed on the binary tree. It is used to specify whether the iteration should
640
- * be performed in a pre-order, in-order, or post-order manner.
641
- * @returns a boolean value.
672
+ * used to identify a specific node or entry in the data structure. It can be of any type that is
673
+ * returned by the callback function `C`. It can also be `null` or `undefined` if no specific
674
+ * identifier is provided.
675
+ * @param {C} callback - The `callback` parameter is a function that will be used to determine
676
+ * whether a node should be included in the result or not. It is of type `C`, which extends the
677
+ * `BTNCallback<NODE>` type.
678
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
679
+ * point for the iteration in the data structure. It can be either a root node, a key-value pair, or
680
+ * a node entry. If not specified, it defaults to the root of the data structure.
681
+ * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
682
+ * of iteration to be performed. It is an optional parameter with a default value of `IterationType`.
683
+ * @returns The method is returning a boolean value.
642
684
  */
643
685
  has(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
644
686
  callback = this._ensureCallback(identifier, callback);
@@ -682,9 +724,10 @@ class BinaryTree extends base_1.IterableEntryBase {
682
724
  *
683
725
  * The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
684
726
  * height of the tree.
685
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
686
- * for calculating the height and minimum height of a binary tree. It can be either a `K` (a key
687
- * value of a binary tree node), `NODE` (a node of a binary tree), `null`, or `undefined`. If
727
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The parameter `beginRoot` is optional and
728
+ * has a default value of `this.root`. It represents the starting point for checking if the tree is
729
+ * perfectly balanced. It can be either a root node (`R`), a key or node or entry
730
+ * (`KeyOrNodeOrEntry<K, V, NODE
688
731
  * @returns a boolean value.
689
732
  */
690
733
  isPerfectlyBalanced(beginRoot = this.root) {
@@ -698,12 +741,14 @@ class BinaryTree extends base_1.IterableEntryBase {
698
741
  * Time Complexity: O(n)
699
742
  * Space Complexity: O(1)
700
743
  *
701
- * The function `isSubtreeBST` checks if a given binary tree is a valid binary search tree.
702
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the root
703
- * node of the binary search tree (BST) that you want to check if it is a subtree of another BST.
704
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
705
- * type of iteration to use when checking if a subtree is a binary search tree (BST). It can have two
706
- * possible values:
744
+ * The function `isBST` checks if a binary search tree is valid, either recursively or iteratively.
745
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
746
+ * starting point for checking if a binary search tree (BST) is valid. It can be either a root node
747
+ * of the BST, a key value of a node in the BST, or an entry object containing both the key and value
748
+ * of a node in the BST
749
+ * @param {IterationType} iterationType - The `iterationType` parameter is used to determine the type
750
+ * of iteration to be performed while checking if the binary search tree (BST) is valid. It can have
751
+ * two possible values:
707
752
  * @returns a boolean value.
708
753
  */
709
754
  isBST(beginRoot = this.root, iterationType = this.iterationType) {
@@ -715,7 +760,7 @@ class BinaryTree extends base_1.IterableEntryBase {
715
760
  const dfs = (cur, min, max) => {
716
761
  if (!this.isRealNode(cur))
717
762
  return true;
718
- const numKey = this.extractor(cur.key);
763
+ const numKey = Number(cur.key);
719
764
  if (numKey <= min || numKey >= max)
720
765
  return false;
721
766
  return dfs(cur.left, min, numKey) && dfs(cur.right, numKey, max);
@@ -736,7 +781,7 @@ class BinaryTree extends base_1.IterableEntryBase {
736
781
  curr = curr.left;
737
782
  }
738
783
  curr = stack.pop();
739
- const numKey = this.extractor(curr.key);
784
+ const numKey = Number(curr.key);
740
785
  if (!this.isRealNode(curr) || (!checkMax && prev >= numKey) || (checkMax && prev <= numKey))
741
786
  return false;
742
787
  prev = numKey;
@@ -756,25 +801,26 @@ class BinaryTree extends base_1.IterableEntryBase {
756
801
  * Time Complexity: O(n)
757
802
  * Space Complexity: O(1)
758
803
  *
759
- * The function calculates the depth of a given node in a binary tree.
760
- * @param {K | NODE | null | undefined} dist - The `dist` parameter represents the node in
761
- * the binary tree whose depth we want to find. It can be of type `K`, `NODE`, `null`, or
762
- * `undefined`.
763
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
764
- * from which we want to calculate the depth. It can be either a `K` (binary tree node key) or
765
- * `NODE` (binary tree node) or `null` or `undefined`. If no value is provided for `beginRoot
766
- * @returns the depth of the `dist` relative to the `beginRoot`.
804
+ * The function calculates the depth of a given node or key in a tree-like data structure.
805
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} dist - The `dist` parameter can be either a `R`
806
+ * (representing a root node), or a `KeyOrNodeOrEntry<K, V, NODE>` (representing a key, node, or
807
+ * entry).
808
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is optional and
809
+ * represents the starting point from which to calculate the depth. It can be either a reference to a
810
+ * node in the tree or a key-value pair or an entry object. If not provided, the default value is
811
+ * `this.root`, which refers to the root node
812
+ * @returns the depth of a node in a tree structure.
767
813
  */
768
814
  getDepth(dist, beginRoot = this.root) {
769
- dist = this.ensureNode(dist);
770
- beginRoot = this.ensureNode(beginRoot);
815
+ let distEnsured = this.ensureNode(dist);
816
+ const beginRootEnsured = this.ensureNode(beginRoot);
771
817
  let depth = 0;
772
- while (dist === null || dist === void 0 ? void 0 : dist.parent) {
773
- if (dist === beginRoot) {
818
+ while (distEnsured === null || distEnsured === void 0 ? void 0 : distEnsured.parent) {
819
+ if (distEnsured === beginRootEnsured) {
774
820
  return depth;
775
821
  }
776
822
  depth++;
777
- dist = dist.parent;
823
+ distEnsured = distEnsured.parent;
778
824
  }
779
825
  return depth;
780
826
  }
@@ -784,17 +830,16 @@ class BinaryTree extends base_1.IterableEntryBase {
784
830
  */
785
831
  /**
786
832
  * Time Complexity: O(n)
787
- * Space Complexity: O(log n)
833
+ * Space Complexity: O(1)
788
834
  *
789
- * The function `getHeight` calculates the maximum height of a binary tree using either recursive or
790
- * iterative traversal.
791
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the
792
- * starting node of the binary tree from which we want to calculate the height. It can be of type
793
- * `K`, `NODE`, `null`, or `undefined`. If not provided, it defaults to `this.root`.
794
- * @param iterationType - The `iterationType` parameter is used to determine whether to calculate the
795
- * height of the tree using a recursive approach or an iterative approach. It can have two possible
796
- * values:
797
- * @returns the height of the binary tree.
835
+ * The `getHeight` function calculates the maximum height of a binary tree using either a recursive
836
+ * or iterative approach.
837
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
838
+ * starting point for calculating the height of a tree. It can be either a root node (`R`), a key or
839
+ * node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or it defaults to the root of the current tree.
840
+ * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
841
+ * iteration used to calculate the height of the tree. It can have two possible values:
842
+ * @returns the maximum height of the binary tree.
798
843
  */
799
844
  getHeight(beginRoot = this.root, iterationType = this.iterationType) {
800
845
  beginRoot = this.ensureNode(beginRoot);
@@ -834,12 +879,15 @@ class BinaryTree extends base_1.IterableEntryBase {
834
879
  *
835
880
  * The `getMinHeight` function calculates the minimum height of a binary tree using either a
836
881
  * recursive or iterative approach.
837
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the
838
- * starting node of the binary tree from which we want to calculate the minimum height. It can be of
839
- * type `K`, `NODE`, `null`, or `undefined`. If no value is provided, it defaults to `this.root`.
840
- * @param iterationType - The `iterationType` parameter is used to determine the method of iteration
841
- * to calculate the minimum height of a binary tree. It can have two possible values:
842
- * @returns The function `getMinHeight` returns the minimum height of a binary tree.
882
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
883
+ * starting point for calculating the minimum height of a tree. It can be either a root node (`R`), a
884
+ * key or node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or it defaults to the root of the current
885
+ * tree.
886
+ * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
887
+ * iteration to be used when calculating the minimum height of the tree. It can have two possible
888
+ * values:
889
+ * @returns The function `getMinHeight` returns a number, which represents the minimum height of the
890
+ * binary tree.
843
891
  */
844
892
  getMinHeight(beginRoot = this.root, iterationType = this.iterationType) {
845
893
  var _a, _b, _c;
@@ -889,35 +937,31 @@ class BinaryTree extends base_1.IterableEntryBase {
889
937
  /**
890
938
  * Time Complexity: O(log n)
891
939
  * Space Complexity: O(log n)
892
- * /
893
-
894
- /**
940
+ */
941
+ /**
895
942
  * Time Complexity: O(log n)
896
943
  * Space Complexity: O(log n)
897
944
  *
898
- * The function `getPathToRoot` returns an array of nodes from a given node to the root of a tree
899
- * structure, with the option to reverse the order of the nodes.
900
- * @param {K | NODE | null | undefined} beginNode - The `beginRoot` parameter represents the
901
- * starting node from which you want to find the path to the root. It can be of type `K`, `NODE`,
902
- * `null`, or `undefined`.
945
+ * The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
946
+ * up to the root node, with an option to reverse the order of the nodes.
947
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginNode - The `beginNode` parameter can be either of
948
+ * type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
903
949
  * @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
904
950
  * resulting path should be reversed or not. If `isReverse` is set to `true`, the path will be
905
- * reversed before returning it. If `isReverse` is set to `false`, the path will be returned as is
906
- * @returns The function `getPathToRoot` returns an array of nodes (`NODE[]`).
951
+ * reversed before returning it. If `isReverse` is set to `false` or not provided, the path will
952
+ * @returns The function `getPathToRoot` returns an array of `NODE` objects.
907
953
  */
908
954
  getPathToRoot(beginNode, isReverse = true) {
909
- // TODO to support get path through passing key
910
955
  const result = [];
911
- beginNode = this.ensureNode(beginNode);
912
- if (!beginNode)
956
+ let beginNodeEnsured = this.ensureNode(beginNode);
957
+ if (!beginNodeEnsured)
913
958
  return result;
914
- while (beginNode.parent) {
959
+ while (beginNodeEnsured.parent) {
915
960
  // Array.push + Array.reverse is more efficient than Array.unshift
916
- // TODO may consider using Deque, so far this is not the performance bottleneck
917
- result.push(beginNode);
918
- beginNode = beginNode.parent;
961
+ result.push(beginNodeEnsured);
962
+ beginNodeEnsured = beginNodeEnsured.parent;
919
963
  }
920
- result.push(beginNode);
964
+ result.push(beginNodeEnsured);
921
965
  return isReverse ? result.reverse() : result;
922
966
  }
923
967
  /**
@@ -928,15 +972,14 @@ class BinaryTree extends base_1.IterableEntryBase {
928
972
  * Time Complexity: O(log n)
929
973
  * Space Complexity: O(1)
930
974
  *
931
- * The function `getLeftMost` returns the leftmost node in a binary tree, either recursively or
932
- * iteratively.
933
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
934
- * for finding the leftmost node in a binary tree. It can be either a `K` (a key value), `NODE` (a
935
- * node), `null`, or `undefined`. If not provided, it defaults to `this.root`,
936
- * @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
937
- * be performed when finding the leftmost node in a binary tree. It can have two possible values:
938
- * @returns The function `getLeftMost` returns the leftmost node (`NODE`) in the binary tree. If there
939
- * is no leftmost node, it returns `null` or `undefined` depending on the input.
975
+ * The `getLeftMost` function returns the leftmost node in a binary tree, either using recursive or
976
+ * iterative traversal.
977
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
978
+ * starting point for finding the leftmost node in a binary tree. It can be either a root node (`R`),
979
+ * a key or node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or `null` or `undefined`.
980
+ * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
981
+ * of iteration to be performed. It can have two possible values:
982
+ * @returns The function `getLeftMost` returns the leftmost node in a binary tree.
940
983
  */
941
984
  getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
942
985
  if (this.isNIL(beginRoot))
@@ -970,16 +1013,15 @@ class BinaryTree extends base_1.IterableEntryBase {
970
1013
  * Time Complexity: O(log n)
971
1014
  * Space Complexity: O(1)
972
1015
  *
973
- * The function `getRightMost` returns the rightmost node in a binary tree, either recursively or
1016
+ * The `getRightMost` function returns the rightmost node in a binary tree, either recursively or
974
1017
  * iteratively.
975
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the
976
- * starting node from which we want to find the rightmost node. It can be of type `K`, `NODE`,
977
- * `null`, or `undefined`. If not provided, it defaults to `this.root`, which is a property of the
978
- * current object.
979
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
980
- * type of iteration to use when finding the rightmost node. It can have one of two values:
981
- * @returns The function `getRightMost` returns the rightmost node (`NODE`) in a binary tree. If there
982
- * is no rightmost node, it returns `null` or `undefined`, depending on the input.
1018
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
1019
+ * starting point for finding the rightmost node in a binary tree. It can be either a root node
1020
+ * (`R`), a key or node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or `null` or `undefined`.
1021
+ * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
1022
+ * of iteration to be performed when finding the rightmost node in a binary tree. It can have two
1023
+ * possible values:
1024
+ * @returns The function `getRightMost` returns a NODE object, `null`, or `undefined`.
983
1025
  */
984
1026
  getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
985
1027
  if (this.isNIL(beginRoot))
@@ -1014,10 +1056,10 @@ class BinaryTree extends base_1.IterableEntryBase {
1014
1056
  * Time Complexity: O(log n)
1015
1057
  * Space Complexity: O(1)
1016
1058
  *
1017
- * The function returns the predecessor of a given node in a tree.
1018
- * @param {NODE} node - The parameter `node` is of type `RedBlackTreeNode`, which represents a node in a
1059
+ * The function returns the predecessor node of a given node in a binary tree.
1060
+ * @param {NODE} node - The parameter "node" is of type "NODE", which represents a node in a binary
1019
1061
  * tree.
1020
- * @returns the predecessor of the given 'node'.
1062
+ * @returns the predecessor node of the given node.
1021
1063
  */
1022
1064
  getPredecessor(node) {
1023
1065
  if (this.isRealNode(node.left)) {
@@ -1043,8 +1085,8 @@ class BinaryTree extends base_1.IterableEntryBase {
1043
1085
  *
1044
1086
  * The function `getSuccessor` returns the next node in a binary tree given a current node.
1045
1087
  * @param {K | NODE | null} [x] - The parameter `x` can be of type `K`, `NODE`, or `null`.
1046
- * @returns the successor of the given node or key. The successor is the node that comes immediately
1047
- * after the given node in the inorder traversal of the binary tree.
1088
+ * @returns The function `getSuccessor` returns a `NODE` object if a successor exists, `null` if
1089
+ * there is no successor, and `undefined` if the input `x` is not a valid node.
1048
1090
  */
1049
1091
  getSuccessor(x) {
1050
1092
  x = this.ensureNode(x);
@@ -1063,30 +1105,29 @@ class BinaryTree extends base_1.IterableEntryBase {
1063
1105
  /**
1064
1106
  * Time complexity: O(n)
1065
1107
  * Space complexity: O(n)
1066
- * /
1067
-
1068
- /**
1108
+ */
1109
+ /**
1069
1110
  * Time complexity: O(n)
1070
1111
  * Space complexity: O(n)
1071
1112
  *
1072
- * The `dfs` function performs a depth-first search traversal on a binary tree or graph, based on the
1073
- * specified pattern and iteration type, and returns an array of values obtained from applying a
1074
- * callback function to each visited node.
1075
- * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1076
- * the tree during the depth-first search. It takes a single parameter, which can be of type `NODE`,
1077
- * `null`, or `undefined`, and returns a value of any type. The default value for this parameter is
1078
- * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the
1079
- * nodes are traversed during the depth-first search. It can have one of the following values:
1080
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
1081
- * for the depth-first search traversal. It can be specified as a key, a node object, or
1082
- * `null`/`undefined`. If not provided, the `beginRoot` will default to the root node of the tree.
1083
- * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
1084
- * iteration to use when traversing the tree. It can have one of the following values:
1113
+ * The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback
1114
+ * function on each node according to a specified pattern and iteration type.
1115
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node
1116
+ * visited during the depth-first search. It takes a node as an argument and returns a value. The
1117
+ * return type of the callback function is determined by the generic type `C`.
1118
+ * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter determines the order in which the
1119
+ * nodes are visited during the depth-first search. It can have one of the following values:
1120
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
1121
+ * point of the depth-first search. It can be either a node object, a key-value pair, or a key. If it
1122
+ * is a key or key-value pair, the method will find the corresponding node in the tree and start the
1123
+ * search from there.
1124
+ * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter determines the
1125
+ * type of iteration to use during the depth-first search. It can have two possible values:
1085
1126
  * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
1086
- * whether null or undefined nodes should be included in the traversal. If `includeNull` is set to
1087
- * `true`, null or undefined nodes will be included in the traversal. If `includeNull` is set to
1088
- * `false`, null or undefined
1089
- * @returns an array of values that are the return values of the callback function.
1127
+ * whether or not to include null values in the depth-first search traversal. If `includeNull` is set
1128
+ * to `true`, null values will be included in the traversal. If `includeNull` is set to `false`, null
1129
+ * values will
1130
+ * @returns an array of the return types of the callback function.
1090
1131
  */
1091
1132
  dfs(callback = this._DEFAULT_CALLBACK, pattern = 'IN', beginRoot = this.root, iterationType = 'ITERATIVE', includeNull = false) {
1092
1133
  beginRoot = this.ensureNode(beginRoot);
@@ -1202,22 +1243,23 @@ class BinaryTree extends base_1.IterableEntryBase {
1202
1243
  * Time complexity: O(n)
1203
1244
  * Space complexity: O(n)
1204
1245
  *
1205
- * The `bfs` function performs a breadth-first search traversal on a binary tree, executing a
1206
- * callback function on each node.
1246
+ * The `bfs` function performs a breadth-first search on a binary tree, calling a callback function
1247
+ * on each node and returning an array of the results.
1207
1248
  * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1208
- * the breadth-first search traversal. It takes a single parameter, which is the current node being
1249
+ * the breadth-first search traversal. It takes a single argument, which is the current node being
1209
1250
  * visited, and returns a value of any type.
1210
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the
1211
- * starting node for the breadth-first search traversal. It can be specified as a key, a node object,
1212
- * or `null`/`undefined` to indicate the root of the tree. If not provided, the `root` property of
1213
- * the class is used as
1214
- * @param iterationType - The `iterationType` parameter determines the type of iteration to be
1215
- * performed during the breadth-first search (BFS). It can have two possible values:
1216
- * @param [includeNull=false] - The `includeNull` parameter is a boolean flag that determines whether
1217
- * to include null values in the breadth-first search traversal. If `includeNull` is set to
1218
- * `true`, null values will be included in the traversal, otherwise they will be skipped.
1219
- * @returns an array of values that are the result of invoking the callback function on each node in
1220
- * the breadth-first traversal of a binary tree.
1251
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
1252
+ * starting point of the breadth-first search. It can be either a root node of a tree or a key, node,
1253
+ * or entry object. If no value is provided, the `root` property of the class is used as the default
1254
+ * starting point.
1255
+ * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
1256
+ * iteration to be performed. It can have two possible values:
1257
+ * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
1258
+ * whether or not to include null values in the breadth-first search (BFS) traversal. If
1259
+ * `includeNull` is set to `true`, null values will be included in the traversal. If `includeNull` is
1260
+ * set to `false
1261
+ * @returns The function `bfs` returns an array of values that are the result of invoking the
1262
+ * `callback` function on each node in the breadth-first order traversal of the binary tree.
1221
1263
  */
1222
1264
  bfs(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
1223
1265
  beginRoot = this.ensureNode(beginRoot);
@@ -1280,18 +1322,18 @@ class BinaryTree extends base_1.IterableEntryBase {
1280
1322
  * Space complexity: O(n)
1281
1323
  *
1282
1324
  * The `listLevels` function returns an array of arrays, where each inner array represents a level in
1283
- * a binary tree and contains the values returned by a callback function applied to the nodes at that
1284
- * level.
1325
+ * a binary tree and contains the results of applying a callback function to the nodes at that level.
1285
1326
  * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1286
- * the tree. It takes a single parameter, which can be of type `NODE`, `null`, or `undefined`, and
1287
- * returns a value of any type.
1288
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the
1289
- * starting node for traversing the tree. It can be either a node object (`NODE`), a key value
1290
- * (`K`), `null`, or `undefined`. If not provided, it defaults to the root node of the tree.
1291
- * @param iterationType - The `iterationType` parameter determines the type of iteration to be
1292
- * performed on the tree. It can have two possible values:
1327
+ * the tree. It takes a node as an argument and returns a value. The return type of the callback
1328
+ * function is determined by the generic type `C` which extends `BTNCallback<NODE | null>`.
1329
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
1330
+ * starting point for traversing the tree. It can be either a root node, a key-value pair, or a node
1331
+ * entry. If no value is provided, the `root` property of the class is used as the default starting
1332
+ * point.
1333
+ * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
1334
+ * iteration to be performed on the binary tree. It can have two possible values:
1293
1335
  * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
1294
- * whether to include null values in the resulting levels. If `includeNull` is set to `true`,
1336
+ * whether or not to include null values in the resulting levels. If `includeNull` is set to `true`,
1295
1337
  * null values will be included in the levels. If `includeNull` is set to `false`, null values will
1296
1338
  * be excluded
1297
1339
  * @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
@@ -1356,17 +1398,17 @@ class BinaryTree extends base_1.IterableEntryBase {
1356
1398
  * The `morris` function performs a depth-first traversal on a binary tree using the Morris traversal
1357
1399
  * algorithm.
1358
1400
  * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1359
- * the tree. It takes a single parameter of type `NODE` (the type of the nodes in the tree) and returns
1360
- * a value of any type.
1361
- * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
1362
- * determines the order in which the nodes of a binary tree are traversed. It can have one of the
1401
+ * the tree. It takes a single argument, which is the current node, and can return any value. The
1402
+ * return type of the `callback` function is determined by the `ReturnType<C>` type, which represents
1403
+ * the return
1404
+ * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function is used
1405
+ * to specify the order in which the nodes of a binary tree are traversed. It can take one of the
1363
1406
  * following values:
1364
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
1365
- * for the traversal. It can be specified as a key, a node object, or `null`/`undefined` to indicate
1366
- * the root of the tree. If no value is provided, the default value is the root of the tree.
1367
- * @returns The function `morris` returns an array of values that are the result of invoking the
1368
- * `callback` function on each node in the binary tree. The type of the array nodes is determined
1369
- * by the return type of the `callback` function.
1407
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
1408
+ * point for the traversal. It can be either a node object, a key, or an entry object. If no value is
1409
+ * provided, the `root` of the tree is used as the starting point.
1410
+ * @returns The function `morris` returns an array of values that are the return values of the
1411
+ * callback function `callback`.
1370
1412
  */
1371
1413
  morris(callback = this._DEFAULT_CALLBACK, pattern = 'IN', beginRoot = this.root) {
1372
1414
  beginRoot = this.ensureNode(beginRoot);
@@ -1461,8 +1503,7 @@ class BinaryTree extends base_1.IterableEntryBase {
1461
1503
  * Time complexity: O(n)
1462
1504
  * Space complexity: O(n)
1463
1505
  *
1464
- * The `clone` function creates a new tree object and copies all the nodes from the original tree to
1465
- * the new tree.
1506
+ * The `clone` function creates a deep copy of a tree object.
1466
1507
  * @returns The `clone()` method is returning a cloned instance of the `TREE` object.
1467
1508
  */
1468
1509
  clone() {
@@ -1483,16 +1524,16 @@ class BinaryTree extends base_1.IterableEntryBase {
1483
1524
  * Time Complexity: O(n)
1484
1525
  * Space Complexity: O(n)
1485
1526
  *
1486
- * The `filter` function creates a new tree by iterating over the nodes of the current tree and
1487
- * adding only the nodes that satisfy the given predicate function.
1488
- * @param predicate - The `predicate` parameter is a function that takes three arguments: `value`,
1489
- * `key`, and `index`. It should return a boolean value indicating whether the pair should be
1490
- * included in the filtered tree or not.
1491
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
1492
- * to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
1493
- * it will be passed as the first argument to the `predicate` function. If `thisArg` is
1494
- * @returns The `filter` method is returning a new tree object that contains the key-value pairs that
1495
- * pass the given predicate function.
1527
+ * The `filter` function creates a new tree with entries that pass a given predicate function.
1528
+ * @param predicate - The `predicate` parameter is a callback function that is used to test each
1529
+ * element in the tree. It takes three arguments: `value`, `key`, and `index`. The `value` argument
1530
+ * represents the value of the current element being processed, the `key` argument represents the key
1531
+ * of the
1532
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
1533
+ * specify the value of `this` within the `predicate` function. When the `predicate` function is
1534
+ * called, `thisArg` will be used as the value of `this` within the function. If `thisArg`
1535
+ * @returns The `filter` method is returning a new tree object that contains the entries that pass
1536
+ * the given predicate function.
1496
1537
  */
1497
1538
  filter(predicate, thisArg) {
1498
1539
  const newTree = this.createTree();
@@ -1512,15 +1553,15 @@ class BinaryTree extends base_1.IterableEntryBase {
1512
1553
  * Time Complexity: O(n)
1513
1554
  * Space Complexity: O(n)
1514
1555
  *
1515
- * The `map` function creates a new tree by applying a callback function to each key-value pair in
1516
- * the original tree.
1517
- * @param callback - The callback parameter is a function that will be called for each key-value pair
1518
- * in the tree. It takes four arguments: the value of the current pair, the key of the current pair,
1519
- * the index of the current pair, and a reference to the tree itself. The callback function should
1520
- * return a new
1521
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
1522
- * specify the value of `this` within the callback function. If you pass a value for `thisArg`, it
1523
- * will be used as the `this` value when the callback function is called. If you don't pass a value
1556
+ * The `map` function creates a new tree by applying a callback function to each entry in the current
1557
+ * tree.
1558
+ * @param callback - The callback parameter is a function that will be called for each entry in the
1559
+ * tree. It takes three arguments: value, key, and index. The value argument represents the value of
1560
+ * the current entry, the key argument represents the key of the current entry, and the index
1561
+ * argument represents the index of the
1562
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
1563
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
1564
+ * passed as the `this` value to the `callback` function. If `thisArg` is
1524
1565
  * @returns The `map` method is returning a new tree object.
1525
1566
  */
1526
1567
  map(callback, thisArg) {
@@ -1548,11 +1589,15 @@ class BinaryTree extends base_1.IterableEntryBase {
1548
1589
  * Time Complexity: O(n)
1549
1590
  * Space Complexity: O(n)
1550
1591
  *
1551
- * The `print` function is used to display a binary tree structure in a visually appealing way.
1552
- * @param {K | NODE | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `K | NODE | null |
1553
- * undefined`. It represents the root node of a binary tree. The root node can have one of the
1554
- * following types:
1555
- * @param {BinaryTreePrintOptions} [options={ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false}] - Options object that controls printing behavior. You can specify whether to display undefined, null, or sentinel nodes.
1592
+ * The `print` function in TypeScript prints the binary tree structure with customizable options.
1593
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
1594
+ * point for printing the binary tree. It can be either a node of the binary tree or a key or entry
1595
+ * that exists in the binary tree. If no value is provided, the root of the binary tree will be used
1596
+ * as the starting point.
1597
+ * @param {BinaryTreePrintOptions} [options] - The `options` parameter is an optional object that
1598
+ * allows you to customize the printing behavior. It has the following properties:
1599
+ * @returns Nothing is being returned. The function has a return type of `void`, which means it does
1600
+ * not return any value.
1556
1601
  */
1557
1602
  print(beginRoot = this.root, options) {
1558
1603
  const opts = Object.assign({ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false }, options);
@@ -1563,10 +1608,10 @@ class BinaryTree extends base_1.IterableEntryBase {
1563
1608
  console.log(`U for undefined
1564
1609
  `);
1565
1610
  if (opts.isShowNull)
1566
- console.log(`NODE for null
1611
+ console.log(`N for null
1567
1612
  `);
1568
1613
  if (opts.isShowRedBlackNIL)
1569
- console.log(`S for Sentinel Node
1614
+ console.log(`S for Sentinel Node(NIL)
1570
1615
  `);
1571
1616
  const display = (root) => {
1572
1617
  const [lines, , ,] = this._displayAux(root, opts);
@@ -1577,13 +1622,18 @@ class BinaryTree extends base_1.IterableEntryBase {
1577
1622
  display(beginRoot);
1578
1623
  }
1579
1624
  /**
1580
- * The function `_getIterator` is a protected generator function that returns an iterator for the
1581
- * key-value pairs in a binary search tree.
1582
- * @param node - The `node` parameter represents the current node in the binary search tree. It is an
1583
- * optional parameter with a default value of `this.root`, which means if no node is provided, the
1584
- * root node of the tree will be used as the starting point for iteration.
1585
- * @returns The function `_getIterator` returns an `IterableIterator` of key-value pairs `[K, V |
1586
- * undefined]`.
1625
+ * Time Complexity: O(1)
1626
+ * Space Complexity: O(1)
1627
+ */
1628
+ /**
1629
+ * Time Complexity: O(1)
1630
+ * Space Complexity: O(1)
1631
+ *
1632
+ * The function `_getIterator` is a generator function that returns an iterator for the key-value
1633
+ * pairs in a binary search tree.
1634
+ * @param node - The `node` parameter represents the current node in the binary search tree. It is
1635
+ * initially set to the root node of the tree.
1636
+ * @returns an IterableIterator<[K, V | undefined]>.
1587
1637
  */
1588
1638
  *_getIterator(node = this.root) {
1589
1639
  if (!node)
@@ -1592,28 +1642,35 @@ class BinaryTree extends base_1.IterableEntryBase {
1592
1642
  const stack = [];
1593
1643
  let current = node;
1594
1644
  while (current || stack.length > 0) {
1595
- while (current && !isNaN(this.extractor(current.key))) {
1645
+ while (this.isRealNode(current)) {
1596
1646
  stack.push(current);
1597
1647
  current = current.left;
1598
1648
  }
1599
1649
  current = stack.pop();
1600
- if (current && !isNaN(this.extractor(current.key))) {
1650
+ if (this.isRealNode(current)) {
1601
1651
  yield [current.key, current.value];
1602
1652
  current = current.right;
1603
1653
  }
1604
1654
  }
1605
1655
  }
1606
1656
  else {
1607
- if (node.left && !isNaN(this.extractor(node.key))) {
1657
+ if (node.left && this.isRealNode(node)) {
1608
1658
  yield* this[Symbol.iterator](node.left);
1609
1659
  }
1610
1660
  yield [node.key, node.value];
1611
- if (node.right && !isNaN(this.extractor(node.key))) {
1661
+ if (node.right && this.isRealNode(node)) {
1612
1662
  yield* this[Symbol.iterator](node.right);
1613
1663
  }
1614
1664
  }
1615
1665
  }
1616
1666
  /**
1667
+ * Time Complexity: O(n)
1668
+ * Space Complexity: O(n)
1669
+ */
1670
+ /**
1671
+ * Time Complexity: O(n)
1672
+ * Space Complexity: O(n)
1673
+ *
1617
1674
  * The `_displayAux` function is responsible for generating the display layout of a binary tree node,
1618
1675
  * taking into account various options such as whether to show null, undefined, or NaN nodes.
1619
1676
  * @param {NODE | null | undefined} node - The `node` parameter represents a node in a binary tree.
@@ -1637,12 +1694,12 @@ class BinaryTree extends base_1.IterableEntryBase {
1637
1694
  else if (node === undefined && !isShowUndefined) {
1638
1695
  return emptyDisplayLayout;
1639
1696
  }
1640
- else if (node !== null && node !== undefined && isNaN(this.extractor(node.key)) && !isShowRedBlackNIL) {
1697
+ else if (this.isNIL(node) && !isShowRedBlackNIL) {
1641
1698
  return emptyDisplayLayout;
1642
1699
  }
1643
1700
  else if (node !== null && node !== undefined) {
1644
1701
  // Display logic of normal nodes
1645
- const key = node.key, line = isNaN(this.extractor(key)) ? 'S' : this.extractor(key).toString(), width = line.length;
1702
+ const key = node.key, line = this.isNIL(node) ? 'S' : key.toString(), width = line.length;
1646
1703
  return _buildNodeDisplay(line, width, this._displayAux(node.left, options), this._displayAux(node.right, options));
1647
1704
  }
1648
1705
  else {
@@ -1680,10 +1737,21 @@ class BinaryTree extends base_1.IterableEntryBase {
1680
1737
  }
1681
1738
  }
1682
1739
  /**
1683
- * Swap the data of two nodes in the binary tree.
1684
- * @param {NODE} srcNode - The source node to swap.
1685
- * @param {NODE} destNode - The destination node to swap.
1686
- * @returns {NODE} - The destination node after the swap.
1740
+ * Time Complexity: O(1)
1741
+ * Space Complexity: O(1)
1742
+ */
1743
+ /**
1744
+ * Time Complexity: O(1)
1745
+ * Space Complexity: O(1)
1746
+ *
1747
+ * The function `_swapProperties` swaps the key-value properties between two nodes.
1748
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} srcNode - The source node that will be swapped with the
1749
+ * destination node. It can be either an instance of the class `R`, or an object of type
1750
+ * `KeyOrNodeOrEntry<K, V, NODE>`.
1751
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} destNode - The `destNode` parameter is the node where
1752
+ * the properties will be swapped with the `srcNode`.
1753
+ * @returns either the `destNode` object with its properties swapped with the `srcNode` object's
1754
+ * properties, or `undefined` if either `srcNode` or `destNode` is falsy.
1687
1755
  */
1688
1756
  _swapProperties(srcNode, destNode) {
1689
1757
  srcNode = this.ensureNode(srcNode);
@@ -1702,12 +1770,20 @@ class BinaryTree extends base_1.IterableEntryBase {
1702
1770
  return undefined;
1703
1771
  }
1704
1772
  /**
1705
- * The function replaces an old node with a new node in a binary tree.
1773
+ * Time Complexity: O(1)
1774
+ * Space Complexity: O(1)
1775
+ */
1776
+ /**
1777
+ * Time Complexity: O(1)
1778
+ * Space Complexity: O(1)
1779
+ *
1780
+ * The function replaces a node in a binary tree with a new node, updating the parent, left child,
1781
+ * right child, and root if necessary.
1706
1782
  * @param {NODE} oldNode - The oldNode parameter represents the node that needs to be replaced in the
1707
1783
  * tree.
1708
1784
  * @param {NODE} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
1709
1785
  * tree.
1710
- * @returns The method is returning the newNode.
1786
+ * @returns the newNode.
1711
1787
  */
1712
1788
  _replaceNode(oldNode, newNode) {
1713
1789
  if (oldNode.parent) {
@@ -1727,10 +1803,17 @@ class BinaryTree extends base_1.IterableEntryBase {
1727
1803
  return newNode;
1728
1804
  }
1729
1805
  /**
1730
- * The function sets the root property of an object to a given value, and if the value is not null,
1731
- * it also sets the parent property of the value to undefined.
1732
- * @param {NODE | null | undefined} v - The parameter `v` is of type `NODE | null | undefined`, which means it can either be of
1733
- * type `NODE` or `null`.
1806
+ * Time Complexity: O(1)
1807
+ * Space Complexity: O(1)
1808
+ */
1809
+ /**
1810
+ * Time Complexity: O(1)
1811
+ * Space Complexity: O(1)
1812
+ *
1813
+ * The function sets the root property of an object to the provided value, and also updates the
1814
+ * parent property of the new root.
1815
+ * @param {NODE | null | undefined} v - The parameter `v` is of type `NODE | null | undefined`. This
1816
+ * means that it can accept a value of type `NODE`, `null`, or `undefined`.
1734
1817
  */
1735
1818
  _setRoot(v) {
1736
1819
  if (v) {
@@ -1738,6 +1821,23 @@ class BinaryTree extends base_1.IterableEntryBase {
1738
1821
  }
1739
1822
  this._root = v;
1740
1823
  }
1824
+ /**
1825
+ * Time Complexity: O(1)
1826
+ * Space Complexity: O(1)
1827
+ */
1828
+ /**
1829
+ * Time Complexity: O(1)
1830
+ * Space Complexity: O(1)
1831
+ *
1832
+ * The function `_ensureCallback` ensures that a callback function is provided and returns it.
1833
+ * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is of type
1834
+ * `ReturnType<C> | null | undefined`. This means it can accept a value that is the return type of
1835
+ * the generic type `C`, or it can be `null` or `undefined`.
1836
+ * @param {C} callback - The `callback` parameter is a function that takes a `node` as an argument
1837
+ * and returns a value. It is of type `C`, which is a generic type that extends the
1838
+ * `BTNCallback<NODE>` type.
1839
+ * @returns the callback parameter.
1840
+ */
1741
1841
  _ensureCallback(identifier, callback = this._DEFAULT_CALLBACK) {
1742
1842
  if ((!callback || callback === this._DEFAULT_CALLBACK) && this.isNode(identifier)) {
1743
1843
  callback = (node => node);