data-structure-typed 1.51.8 → 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 (106) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +13 -13
  3. package/benchmark/report.html +1 -37
  4. package/benchmark/report.json +13 -385
  5. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +104 -66
  6. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +119 -87
  7. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
  8. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +80 -60
  9. package/dist/cjs/data-structures/binary-tree/avl-tree.js +78 -59
  10. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  11. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +316 -224
  12. package/dist/cjs/data-structures/binary-tree/binary-tree.js +471 -361
  13. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  14. package/dist/cjs/data-structures/binary-tree/bst.d.ts +198 -200
  15. package/dist/cjs/data-structures/binary-tree/bst.js +215 -249
  16. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  17. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +71 -72
  18. package/dist/cjs/data-structures/binary-tree/rb-tree.js +107 -98
  19. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  20. package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +90 -73
  21. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +105 -93
  22. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  23. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +10 -15
  24. package/dist/cjs/data-structures/graph/abstract-graph.js +10 -15
  25. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  26. package/dist/cjs/data-structures/hash/hash-map.d.ts +31 -38
  27. package/dist/cjs/data-structures/hash/hash-map.js +40 -55
  28. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  29. package/dist/cjs/data-structures/queue/deque.d.ts +2 -3
  30. package/dist/cjs/data-structures/queue/deque.js +2 -3
  31. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  32. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  33. package/dist/cjs/data-structures/trie/trie.d.ts +1 -1
  34. package/dist/cjs/data-structures/trie/trie.js +1 -1
  35. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  36. package/dist/cjs/interfaces/binary-tree.d.ts +6 -6
  37. package/dist/cjs/types/common.d.ts +1 -2
  38. package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
  39. package/dist/cjs/types/data-structures/binary-tree/avl-tree.d.ts +2 -2
  40. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +5 -4
  41. package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +4 -4
  42. package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +2 -2
  43. package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -3
  44. package/dist/cjs/utils/utils.js +3 -5
  45. package/dist/cjs/utils/utils.js.map +1 -1
  46. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +104 -66
  47. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +119 -87
  48. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +80 -60
  49. package/dist/mjs/data-structures/binary-tree/avl-tree.js +78 -59
  50. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +316 -224
  51. package/dist/mjs/data-structures/binary-tree/binary-tree.js +472 -362
  52. package/dist/mjs/data-structures/binary-tree/bst.d.ts +198 -200
  53. package/dist/mjs/data-structures/binary-tree/bst.js +219 -255
  54. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +71 -72
  55. package/dist/mjs/data-structures/binary-tree/rb-tree.js +107 -98
  56. package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +90 -73
  57. package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +105 -93
  58. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +10 -15
  59. package/dist/mjs/data-structures/graph/abstract-graph.js +10 -15
  60. package/dist/mjs/data-structures/hash/hash-map.d.ts +31 -38
  61. package/dist/mjs/data-structures/hash/hash-map.js +41 -55
  62. package/dist/mjs/data-structures/queue/deque.d.ts +2 -3
  63. package/dist/mjs/data-structures/queue/deque.js +2 -3
  64. package/dist/mjs/data-structures/trie/trie.d.ts +1 -1
  65. package/dist/mjs/data-structures/trie/trie.js +1 -1
  66. package/dist/mjs/interfaces/binary-tree.d.ts +6 -6
  67. package/dist/mjs/types/common.d.ts +1 -2
  68. package/dist/mjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
  69. package/dist/mjs/types/data-structures/binary-tree/avl-tree.d.ts +2 -2
  70. package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +5 -4
  71. package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +4 -4
  72. package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +2 -2
  73. package/dist/mjs/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -3
  74. package/dist/mjs/utils/utils.js +3 -5
  75. package/dist/umd/data-structure-typed.js +1340 -1181
  76. package/dist/umd/data-structure-typed.min.js +2 -2
  77. package/dist/umd/data-structure-typed.min.js.map +1 -1
  78. package/package.json +6 -6
  79. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +142 -92
  80. package/src/data-structures/binary-tree/avl-tree.ts +94 -66
  81. package/src/data-structures/binary-tree/binary-tree.ts +530 -398
  82. package/src/data-structures/binary-tree/bst.ts +251 -270
  83. package/src/data-structures/binary-tree/rb-tree.ts +121 -100
  84. package/src/data-structures/binary-tree/tree-multi-map.ts +125 -99
  85. package/src/data-structures/graph/abstract-graph.ts +10 -10
  86. package/src/data-structures/hash/hash-map.ts +42 -49
  87. package/src/data-structures/queue/deque.ts +2 -2
  88. package/src/data-structures/queue/queue.ts +1 -1
  89. package/src/data-structures/trie/trie.ts +2 -2
  90. package/src/interfaces/binary-tree.ts +8 -7
  91. package/src/types/common.ts +1 -2
  92. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +2 -2
  93. package/src/types/data-structures/binary-tree/avl-tree.ts +2 -2
  94. package/src/types/data-structures/binary-tree/binary-tree.ts +5 -4
  95. package/src/types/data-structures/binary-tree/bst.ts +4 -4
  96. package/src/types/data-structures/binary-tree/rb-tree.ts +2 -2
  97. package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -3
  98. package/src/utils/utils.ts +3 -3
  99. package/test/integration/avl-tree.test.ts +2 -2
  100. package/test/integration/bst.test.ts +3 -3
  101. package/test/integration/index.html +25 -11
  102. package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +68 -0
  103. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +56 -0
  104. package/test/unit/data-structures/graph/directed-graph.test.ts +2 -2
  105. package/test/utils/big-o.ts +12 -6
  106. package/tsconfig-base.json +1 -0
@@ -97,24 +97,25 @@ export class BinaryTreeNode {
97
97
  export class BinaryTree extends IterableEntryBase {
98
98
  iterationType = 'ITERATIVE';
99
99
  /**
100
- * The constructor function initializes a binary tree object with optional keysOrNodesOrEntries and options.
101
- * @param [keysOrNodesOrEntries] - An optional iterable of KeyOrNodeOrEntry objects. These objects represent the
100
+ * The constructor function initializes a binary tree object with optional keysOrNodesOrEntriesOrRawElements and options.
101
+ * @param [keysOrNodesOrEntriesOrRawElements] - An optional iterable of KeyOrNodeOrEntry objects. These objects represent the
102
102
  * nodes to be added to the binary tree.
103
103
  * @param [options] - The `options` parameter is an optional object that can contain additional
104
104
  * configuration options for the binary tree. In this case, it is of type
105
105
  * `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
106
106
  * required.
107
107
  */
108
- constructor(keysOrNodesOrEntries = [], options) {
108
+ constructor(keysOrNodesOrEntriesOrRawElements = [], options) {
109
109
  super();
110
110
  if (options) {
111
- const { iterationType } = options;
111
+ const { iterationType, toEntryFn } = options;
112
112
  if (iterationType)
113
113
  this.iterationType = iterationType;
114
+ if (typeof toEntryFn === 'function')
115
+ this._toEntryFn = toEntryFn;
114
116
  }
115
- this._size = 0;
116
- if (keysOrNodesOrEntries)
117
- this.addMany(keysOrNodesOrEntries);
117
+ if (keysOrNodesOrEntriesOrRawElements)
118
+ this.addMany(keysOrNodesOrEntriesOrRawElements);
118
119
  }
119
120
  _root;
120
121
  /**
@@ -125,7 +126,7 @@ export class BinaryTree extends IterableEntryBase {
125
126
  get root() {
126
127
  return this._root;
127
128
  }
128
- _size;
129
+ _size = 0;
129
130
  /**
130
131
  * The function returns the size of an object.
131
132
  * @returns The size of the object, which is a number.
@@ -141,6 +142,14 @@ export class BinaryTree extends IterableEntryBase {
141
142
  get NIL() {
142
143
  return this._NIL;
143
144
  }
145
+ _toEntryFn;
146
+ /**
147
+ * The function returns the value of the _toEntryFn property.
148
+ * @returns The function being returned is `this._toEntryFn`.
149
+ */
150
+ get toEntryFn() {
151
+ return this._toEntryFn;
152
+ }
144
153
  /**
145
154
  * Creates a new instance of BinaryTreeNode with the given key and value.
146
155
  * @param {K} key - The key for the new node.
@@ -161,42 +170,42 @@ export class BinaryTree extends IterableEntryBase {
161
170
  return new BinaryTree([], { iterationType: this.iterationType, ...options });
162
171
  }
163
172
  /**
164
- * The function `keyValueOrEntryToNode` converts an keyOrNodeOrEntry object into a node object.
165
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
173
+ * The function `keyValueOrEntryOrRawElementToNode` converts a key-value pair, entry, or raw element
174
+ * into a node object.
175
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
176
+ * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
166
177
  * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
167
- * `keyValueOrEntryToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If no value
168
- * is provided, it will be `undefined`.
169
- * @returns a value of type NODE (node), or null, or undefined.
178
+ * `keyValueOrEntryOrRawElementToNode` function. It represents the value associated with a key in a
179
+ * key-value pair. If provided, it will be used to create a node with the specified key and value.
180
+ * @returns The function `keyValueOrEntryOrRawElementToNode` returns either a `NODE` object, `null`,
181
+ * or `undefined`.
170
182
  */
171
- keyValueOrEntryToNode(keyOrNodeOrEntry, value) {
172
- if (keyOrNodeOrEntry === undefined)
183
+ keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value) {
184
+ if (keyOrNodeOrEntryOrRawElement === undefined)
173
185
  return;
174
- let node;
175
- if (keyOrNodeOrEntry === null) {
176
- node = null;
177
- }
178
- else if (this.isEntry(keyOrNodeOrEntry)) {
179
- const [key, value] = keyOrNodeOrEntry;
180
- if (key === undefined) {
186
+ if (keyOrNodeOrEntryOrRawElement === null)
187
+ return null;
188
+ if (this.isNode(keyOrNodeOrEntryOrRawElement))
189
+ return keyOrNodeOrEntryOrRawElement;
190
+ if (this.toEntryFn) {
191
+ const [key, entryValue] = this.toEntryFn(keyOrNodeOrEntryOrRawElement);
192
+ if (key)
193
+ return this.createNode(key, entryValue ?? value);
194
+ else
181
195
  return;
182
- }
183
- else if (key === null) {
184
- node = null;
185
- }
186
- else {
187
- node = this.createNode(key, value);
188
- }
189
- }
190
- else if (this.isNode(keyOrNodeOrEntry)) {
191
- node = keyOrNodeOrEntry;
192
- }
193
- else if (!this.isNode(keyOrNodeOrEntry)) {
194
- node = this.createNode(keyOrNodeOrEntry, value);
195
196
  }
196
- else {
197
- return;
197
+ if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
198
+ const [key, value] = keyOrNodeOrEntryOrRawElement;
199
+ if (key === undefined)
200
+ return;
201
+ else if (key === null)
202
+ return null;
203
+ else
204
+ return this.createNode(key, value);
198
205
  }
199
- return node;
206
+ if (this.isKey(keyOrNodeOrEntryOrRawElement))
207
+ return this.createNode(keyOrNodeOrEntryOrRawElement, value);
208
+ return;
200
209
  }
201
210
  /**
202
211
  * Time Complexity: O(n)
@@ -206,56 +215,56 @@ export class BinaryTree extends IterableEntryBase {
206
215
  * Time Complexity: O(n)
207
216
  * Space Complexity: O(log n)
208
217
  *
209
- * The function `ensureNode` returns the node corresponding to the given key if it is a valid node
210
- * key, otherwise it returns the key itself.
211
- * @param {K | NODE | null | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `NODE`,
212
- * `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
213
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
214
- * type of iteration to be used when searching for a node by key. It has a default value of
215
- * `'ITERATIVE'`.
216
- * @returns either the node corresponding to the given key if it is a valid node key, or the key
217
- * itself if it is not a valid node key.
218
- */
219
- ensureNode(keyOrNodeOrEntry, iterationType = 'ITERATIVE') {
220
- if (keyOrNodeOrEntry === this.NIL)
218
+ * The `ensureNode` function checks if the input is a valid node and returns it, or converts it to a
219
+ * node if it is a key or entry.
220
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
221
+ * `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, `KeyOrNodeOrEntry<K, V, NODE>`, or
222
+ * a raw element.
223
+ * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
224
+ * parameter that specifies the type of iteration to be used when searching for a node. It has a
225
+ * default value of `'ITERATIVE'`.
226
+ * @returns The function `ensureNode` returns either a `NODE` object, `null`, or `undefined`.
227
+ */
228
+ ensureNode(keyOrNodeOrEntryOrRawElement, iterationType = 'ITERATIVE') {
229
+ if (keyOrNodeOrEntryOrRawElement === null)
230
+ return null;
231
+ if (keyOrNodeOrEntryOrRawElement === undefined)
232
+ return;
233
+ if (keyOrNodeOrEntryOrRawElement === this.NIL)
221
234
  return;
222
- if (this.isRealNode(keyOrNodeOrEntry)) {
223
- return keyOrNodeOrEntry;
235
+ if (this.isNode(keyOrNodeOrEntryOrRawElement))
236
+ return keyOrNodeOrEntryOrRawElement;
237
+ if (this.toEntryFn) {
238
+ const [key] = this.toEntryFn(keyOrNodeOrEntryOrRawElement);
239
+ if (key)
240
+ return this.getNodeByKey(key);
224
241
  }
225
- if (this.isEntry(keyOrNodeOrEntry)) {
226
- const key = keyOrNodeOrEntry[0];
242
+ if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
243
+ const key = keyOrNodeOrEntryOrRawElement[0];
227
244
  if (key === null)
228
245
  return null;
229
246
  if (key === undefined)
230
247
  return;
231
248
  return this.getNodeByKey(key, iterationType);
232
249
  }
233
- if (keyOrNodeOrEntry === null)
234
- return null;
235
- if (keyOrNodeOrEntry === undefined)
236
- return;
237
- return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
250
+ if (this.isKey(keyOrNodeOrEntryOrRawElement))
251
+ return this.getNodeByKey(keyOrNodeOrEntryOrRawElement, iterationType);
252
+ return;
238
253
  }
239
254
  /**
240
- * The function checks if a given node is a real node or null.
241
- * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
242
- * @returns a boolean value.
255
+ * The function checks if the input is an instance of the BinaryTreeNode class.
256
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
257
+ * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
258
+ * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
259
+ * an instance of the `BinaryTreeNode` class.
243
260
  */
244
- isNodeOrNull(node) {
245
- return this.isRealNode(node) || node === null;
261
+ isNode(keyOrNodeOrEntryOrRawElement) {
262
+ return keyOrNodeOrEntryOrRawElement instanceof BinaryTreeNode;
246
263
  }
247
264
  /**
248
- * The function "isNode" checks if an keyOrNodeOrEntry is an instance of the BinaryTreeNode class.
249
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V,NODE>`.
250
- * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the class NODE.
251
- */
252
- isNode(keyOrNodeOrEntry) {
253
- return keyOrNodeOrEntry instanceof BinaryTreeNode;
254
- }
255
- /**
256
- * The function checks if a given node is a real node by verifying if it is an instance of
257
- * BinaryTreeNode and its key is not NaN.
258
- * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
265
+ * The function checks if a given node is a valid node in a binary search tree.
266
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
267
+ * `KeyOrNodeOrEntry<K, V, NODE>`.
259
268
  * @returns a boolean value.
260
269
  */
261
270
  isRealNode(node) {
@@ -264,21 +273,64 @@ export class BinaryTree extends IterableEntryBase {
264
273
  return this.isNode(node);
265
274
  }
266
275
  /**
267
- * The function checks if a given node is a BinaryTreeNode instance and has a key value of NaN.
268
- * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
276
+ * The function checks if a given node is a real node or null.
277
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
278
+ * `KeyOrNodeOrEntry<K, V, NODE>`.
279
+ * @returns a boolean value.
280
+ */
281
+ isNodeOrNull(node) {
282
+ return this.isRealNode(node) || node === null;
283
+ }
284
+ /**
285
+ * The function checks if a given node is equal to the NIL value.
286
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
287
+ * `KeyOrNodeOrEntry<K, V, NODE>`.
269
288
  * @returns a boolean value.
270
289
  */
271
290
  isNIL(node) {
272
291
  return node === this.NIL;
273
292
  }
274
293
  /**
275
- * The function checks if a given value is an entry in a binary tree node.
276
- * @param keyOrNodeOrEntry - KeyOrNodeOrEntry<K, V,NODE> - A generic type representing a node in a binary tree. It has
277
- * two type parameters V and NODE, representing the value and node type respectively.
294
+ * The function checks if the input is an array with two elements, indicating it is a binary tree
295
+ * node entry.
296
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
297
+ * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
278
298
  * @returns a boolean value.
279
299
  */
280
- isEntry(keyOrNodeOrEntry) {
281
- return Array.isArray(keyOrNodeOrEntry) && keyOrNodeOrEntry.length === 2;
300
+ isEntry(keyOrNodeOrEntryOrRawElement) {
301
+ return Array.isArray(keyOrNodeOrEntryOrRawElement) && keyOrNodeOrEntryOrRawElement.length === 2;
302
+ }
303
+ /**
304
+ * The function checks if a given value is a valid key by evaluating its type and value.
305
+ * @param {any} key - The `key` parameter can be of any type. It is the value that we want to check
306
+ * if it is a valid key.
307
+ * @param [isCheckValueOf=true] - The `isCheckValueOf` parameter is a boolean flag that determines
308
+ * whether the function should check the valueOf() method of an object when the key is of type
309
+ * 'object'. If `isCheckValueOf` is true, the function will recursively call itself with the value
310
+ * returned by key.valueOf().
311
+ * @returns a boolean value.
312
+ */
313
+ isKey(key, isCheckValueOf = true) {
314
+ if (key === null)
315
+ return true;
316
+ const keyType = typeof key;
317
+ if (keyType === 'string' || keyType === 'bigint' || keyType === 'boolean')
318
+ return true;
319
+ if (keyType === 'number')
320
+ return !isNaN(key);
321
+ if (keyType === 'symbol' || keyType === 'undefined')
322
+ return false;
323
+ if (keyType === 'function')
324
+ return this.isKey(key());
325
+ if (keyType === 'object') {
326
+ if (typeof key.toString === 'function')
327
+ return true;
328
+ if (isCheckValueOf && typeof key.valueOf === 'function') {
329
+ this.isKey(key.valueOf(), false);
330
+ }
331
+ return false;
332
+ }
333
+ return false;
282
334
  }
283
335
  /**
284
336
  * Time Complexity O(n)
@@ -288,14 +340,20 @@ export class BinaryTree extends IterableEntryBase {
288
340
  * Time Complexity O(n)
289
341
  * Space Complexity O(1)
290
342
  *
291
- * The `add` function adds a new node to a binary tree, either by creating a new node or replacing an
292
- * existing node with the same key.
293
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
294
- * @param {V} [value] - The value to be inserted into the binary tree.
295
- * @returns The function `add` returns either a node (`NODE`), `null`, or `undefined`.
296
- */
297
- add(keyOrNodeOrEntry, value) {
298
- const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
343
+ * The `add` function is used to insert a new node into a binary tree, checking for duplicate keys
344
+ * and finding the appropriate insertion position.
345
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
346
+ * `keyOrNodeOrEntryOrRawElement` parameter can accept a value of type `R`, which represents the key,
347
+ * node, entry, or raw element to be added to the tree. It can also accept a value of type
348
+ * `KeyOrNodeOrEntry<K, V, NODE>
349
+ * @param {V} [value] - The `value` parameter is an optional value that can be associated with the
350
+ * key being added to the tree. It represents the value that will be stored in the tree for the given
351
+ * key.
352
+ * @returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
353
+ * insertion position cannot be found or if there are duplicate keys.
354
+ */
355
+ add(keyOrNodeOrEntryOrRawElement, value) {
356
+ const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
299
357
  if (newNode === undefined)
300
358
  return false;
301
359
  // If the tree is empty, directly set the new node as the root node
@@ -349,20 +407,24 @@ export class BinaryTree extends IterableEntryBase {
349
407
  * Time Complexity: O(k * n)
350
408
  * Space Complexity: O(1)
351
409
  *
352
- * The `addMany` function takes in a collection of keysOrNodesOrEntries and an optional collection of values, and
353
- * adds each node with its corresponding value to the data structure.
354
- * @param keysOrNodesOrEntries - An iterable collection of KeyOrNodeOrEntry objects.
355
- * @param [values] - An optional iterable of values that will be assigned to each node being added.
356
- * @returns The function `addMany` returns an array of `NODE`, `null`, or `undefined` values.
357
- */
358
- addMany(keysOrNodesOrEntries, values) {
410
+ * The `addMany` function takes in an iterable of keys or nodes or entries or raw elements, and an
411
+ * optional iterable of values, and adds each key or node or entry with its corresponding value to a
412
+ * data structure, returning an array of booleans indicating whether each insertion was successful.
413
+ * @param keysOrNodesOrEntriesOrRawElements - An iterable containing keys, nodes, entries, or raw
414
+ * elements. These elements will be added to the data structure.
415
+ * @param [values] - An optional iterable of values that correspond to the keys or nodes or entries
416
+ * in the `keysOrNodesOrEntriesOrRawElements` parameter.
417
+ * @returns The function `addMany` returns an array of booleans indicating whether each element was
418
+ * successfully added to the data structure.
419
+ */
420
+ addMany(keysOrNodesOrEntriesOrRawElements, values) {
359
421
  // TODO not sure addMany not be run multi times
360
422
  const inserted = [];
361
423
  let valuesIterator;
362
424
  if (values) {
363
425
  valuesIterator = values[Symbol.iterator]();
364
426
  }
365
- for (const keyOrNodeOrEntry of keysOrNodesOrEntries) {
427
+ for (const keyOrNodeOrEntryOrRawElement of keysOrNodesOrEntriesOrRawElements) {
366
428
  let value = undefined;
367
429
  if (valuesIterator) {
368
430
  const valueResult = valuesIterator.next();
@@ -370,7 +432,7 @@ export class BinaryTree extends IterableEntryBase {
370
432
  value = valueResult.value;
371
433
  }
372
434
  }
373
- inserted.push(this.add(keyOrNodeOrEntry, value));
435
+ inserted.push(this.add(keyOrNodeOrEntryOrRawElement, value));
374
436
  }
375
437
  return inserted;
376
438
  }
@@ -383,36 +445,34 @@ export class BinaryTree extends IterableEntryBase {
383
445
  * Time Complexity: O(k * n)
384
446
  * Space Complexity: O(1)
385
447
  *
386
- * The `refill` function clears the current data and adds new key-value pairs to the data structure.
387
- * @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries. These can be of type
388
- * KeyOrNodeOrEntry<K, V, NODE>.
389
- * @param [values] - The `values` parameter is an optional iterable that contains the values to be
390
- * associated with the keys or nodes or entries in the `keysOrNodesOrEntries` parameter. If provided,
391
- * the values will be associated with the corresponding keys or nodes or entries in the
392
- * `keysOrNodesOrEntries` iterable
393
- */
394
- refill(keysOrNodesOrEntries, values) {
448
+ * The `refill` function clears the current data and adds new data to the collection.
449
+ * @param keysOrNodesOrEntriesOrRawElements - An iterable collection of keys, nodes, entries, or raw
450
+ * elements. These can be of any type (R) or a specific type (KeyOrNodeOrEntry<K, V, NODE>).
451
+ * @param [values] - The `values` parameter is an optional iterable of values that will be associated
452
+ * with the keys or nodes being added. If provided, the values will be assigned to the corresponding
453
+ * keys or nodes. If not provided, the values will be set to `undefined`.
454
+ */
455
+ refill(keysOrNodesOrEntriesOrRawElements, values) {
395
456
  this.clear();
396
- this.addMany(keysOrNodesOrEntries, values);
457
+ this.addMany(keysOrNodesOrEntriesOrRawElements, values);
397
458
  }
398
459
  /**
399
460
  * Time Complexity: O(n)
400
461
  * Space Complexity: O(1)
401
- * /
402
-
403
- /**
462
+ */
463
+ /**
404
464
  * Time Complexity: O(n)
405
465
  * Space Complexity: O(1)
406
466
  *
407
- * The function deletes a node from a binary tree and returns an array of the deleted nodes along
408
- * with the nodes that need to be balanced.
409
- * @param {ReturnType<C> | null | undefined} identifier - The identifier parameter is the value or
410
- * object that you want to delete from the binary tree. It can be of any type that is compatible with
411
- * the callback function's return type. It can also be null or undefined if you want to delete a
412
- * specific node based on its value or object.
467
+ * The above function is a TypeScript implementation of deleting a node from a binary tree, returning
468
+ * the deleted node and the node that needs to be balanced.
469
+ * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
470
+ * used to identify the node that needs to be deleted from the binary tree. It can be of any type
471
+ * that is returned by the callback function.
413
472
  * @param {C} callback - The `callback` parameter is a function that is used to determine the
414
- * identifier of the node to be deleted. It is optional and has a default value of
415
- * `this._DEFAULT_CALLBACK`. The `callback` function should return the identifier of the node.
473
+ * identifier of the node to be deleted. It is of type `C`, which extends the `BTNCallback<NODE>`
474
+ * interface. The `BTNCallback<NODE>` interface represents a callback function that takes a node of
475
+ * type `NODE
416
476
  * @returns an array of `BinaryTreeDeleteResult<NODE>`.
417
477
  */
418
478
  delete(identifier, callback = this._DEFAULT_CALLBACK) {
@@ -467,28 +527,27 @@ export class BinaryTree extends IterableEntryBase {
467
527
  */
468
528
  /**
469
529
  * Time Complexity: O(n)
470
- * Space Complexity: O(k + log n).
530
+ * Space Complexity: O(k + log n)
471
531
  *
472
- * The function `getNodes` retrieves nodes from a binary tree based on a given identifier and
473
- * callback function.
532
+ * The function `getNodes` returns an array of nodes that match a given identifier, using either a
533
+ * recursive or iterative approach.
474
534
  * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
475
- * that you want to search for in the binary tree. It can be of any type that is returned by the
476
- * callback function `C`. It can also be `null` or `undefined` if you don't want to search for a
477
- * specific value.
478
- * @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as
479
- * input and returns a value of type `C`. It is used to determine if a node matches the given
480
- * identifier. If no callback is provided, the `_DEFAULT_CALLBACK` function is used as the
481
- * default
482
- * @param [onlyOne=false] - A boolean value indicating whether to only return the first node that
483
- * matches the identifier. If set to true, the function will stop iterating once it finds a matching
484
- * node and return that node. If set to false (default), the function will continue iterating and
485
- * return all nodes that match the identifier.
486
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the
487
- * starting node for the traversal. It can be either a key, a node object, or `null`/`undefined`. If
488
- * it is `null` or `undefined`, an empty array will be returned.
489
- * @param iterationType - The `iterationType` parameter determines the type of iteration used to
490
- * traverse the binary tree. It can have two possible values:
491
- * @returns an array of nodes of type `NODE`.
535
+ * that is used to identify the nodes. It can be of any type and is used to match against the result
536
+ * of the callback function for each node.
537
+ * @param {C} callback - The `callback` parameter is a function that takes a node as input and
538
+ * returns a value. This value is used to identify the nodes that match the given identifier. The
539
+ * `callback` function is optional and defaults to a default callback function
540
+ * (`this._DEFAULT_CALLBACK`) if not provided.
541
+ * @param [onlyOne=false] - A boolean value indicating whether to return only one node that matches
542
+ * the identifier or all nodes that match the identifier. If set to true, only the first matching
543
+ * node will be returned. If set to false, all matching nodes will be returned. The default value is
544
+ * false.
545
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
546
+ * point for the search. It can be either a node object, a key-value pair, or a key. If it is not
547
+ * provided, the `root` of the data structure is used as the starting point.
548
+ * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
549
+ * iteration to be performed on the nodes of a binary tree. It can have two possible values:
550
+ * @returns an array of NODE objects.
492
551
  */
493
552
  getNodes(identifier, callback = this._DEFAULT_CALLBACK, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
494
553
  beginRoot = this.ensureNode(beginRoot);
@@ -533,24 +592,21 @@ export class BinaryTree extends IterableEntryBase {
533
592
  */
534
593
  /**
535
594
  * Time Complexity: O(n)
536
- * Space Complexity: O(log n)
595
+ * Space Complexity: O(log n).
537
596
  *
538
- * The function `getNode` returns the first node that matches the given identifier and callback
539
- * function.
597
+ * The function `getNode` returns the first node that matches the given identifier and callback,
598
+ * starting from the specified root node and using the specified iteration type.
540
599
  * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
541
- * used to identify the node you want to retrieve. It can be of any type that is returned by the
542
- * callback function `C`. It can also be `null` or `undefined` if you don't have a specific
543
- * identifier.
544
- * @param {C} callback - The `callback` parameter is a function that will be called for each node in
545
- * the binary tree. It is used to determine if a node matches the given identifier. The `callback`
546
- * function should take a single parameter of type `NODE` (the type of the nodes in the binary tree) and
547
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
548
- * for searching the binary tree. It can be either a key value, a node object, or `null`/`undefined`.
549
- * If `null` or `undefined` is passed, the search will start from the root of the binary tree.
550
- * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
551
- * be performed when searching for nodes in the binary tree. It determines the order in which the
552
- * nodes are visited during the search.
553
- * @returns a value of type `NODE | null | undefined`.
600
+ * used to identify the node you want to retrieve. It can be of any type that is the return type of
601
+ * the `C` callback function, or it can be `null` or `undefined`.
602
+ * @param {C} callback - The `callback` parameter is a function that will be used to determine if a
603
+ * node matches the desired criteria. It should return a value that can be used to identify the node.
604
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
605
+ * point for searching nodes in a tree structure. It can be either a root node, a key-value pair, or
606
+ * a node entry. If not provided, the search will start from the root of the tree.
607
+ * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
608
+ * of iteration to be performed when searching for nodes. It can have one of the following values:
609
+ * @returns The method is returning a NODE object, or null, or undefined.
554
610
  */
555
611
  getNode(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
556
612
  return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
@@ -563,15 +619,13 @@ export class BinaryTree extends IterableEntryBase {
563
619
  * Time Complexity: O(n)
564
620
  * Space Complexity: O(log n)
565
621
  *
566
- * The function `getNodeByKey` searches for a node in a binary tree by its key, using either
567
- * recursive or iterative iteration.
568
- * @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
569
- * It is used to find the node with the matching key value.
570
- * @param iterationType - The `iterationType` parameter is used to determine whether the search for
571
- * the node with the given key should be performed iteratively or recursively. It has two possible
572
- * values:
573
- * @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
574
- * found in the binary tree. If no node is found, it returns `undefined`.
622
+ * The function `getNodeByKey` returns a node with a specific key value from a tree structure.
623
+ * @param {K} key - The key parameter is the value that you want to search for in the tree. It is
624
+ * used to find the node with the matching key value.
625
+ * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
626
+ * parameter that specifies the type of iteration to be used when searching for a node in the tree.
627
+ * It has a default value of `'ITERATIVE'`.
628
+ * @returns a value of type NODE, null, or undefined.
575
629
  */
576
630
  getNodeByKey(key, iterationType = 'ITERATIVE') {
577
631
  return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
@@ -584,51 +638,49 @@ export class BinaryTree extends IterableEntryBase {
584
638
  * Time Complexity: O(n)
585
639
  * Space Complexity: O(log n)
586
640
  *
587
- * The function `get` retrieves the value of a node in a binary tree based on the provided identifier
588
- * and callback function.
641
+ * The function `get` in TypeScript overrides the base class method and returns the value associated
642
+ * with the given identifier.
589
643
  * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
590
- * used to identify the node in the binary tree. It can be of any type that is the return type of the
644
+ * used to identify the node in the binary tree. It can be of any type that is returned by the
591
645
  * callback function `C`. It can also be `null` or `undefined` if no identifier is provided.
592
- * @param {C} callback - The `callback` parameter is a function that will be called with each node in
593
- * the binary tree. It is used to determine whether a node matches the given identifier. The callback
594
- * function should return a value that can be compared to the identifier to determine if it is a
595
- * match.
596
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
597
- * for the search in the binary tree. It can be specified as a `K` (a unique identifier for a
598
- * node), a node object of type `NODE`, or `null`/`undefined` to start the search from the root of
599
- * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
600
- * be performed when searching for a node in the binary tree. It is an optional parameter with a
601
- * default value specified by `this.iterationType`.
602
- * @returns The value of the node with the given identifier is being returned. If the node is not
603
- * found, `undefined` is returned.
646
+ * @param {C} callback - The `callback` parameter is a function that will be used to determine if a
647
+ * node matches the given identifier. It is optional and defaults to `this._DEFAULT_CALLBACK`.
648
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
649
+ * point for the search in the binary tree. It can be either a root node of the tree or a key, node,
650
+ * or entry object that exists in the tree. If no specific starting point is provided, the search
651
+ * will begin from the root of the
652
+ * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
653
+ * of iteration to be performed when searching for a node in the tree. It can have one of the
654
+ * following values:
655
+ * @returns The method is returning the value associated with the specified identifier in the binary
656
+ * tree.
604
657
  */
605
658
  get(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
606
659
  return this.getNode(identifier, callback, beginRoot, iterationType)?.value;
607
660
  }
608
661
  /**
609
662
  * Time Complexity: O(n)
610
- * Space Complexity: O(log n).
663
+ * Space Complexity: O(log n)
611
664
  */
612
665
  /**
613
666
  * Time Complexity: O(n)
614
- * Space Complexity: O(log n).
667
+ * Space Complexity: O(log n)
615
668
  *
616
- * 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.
617
671
  * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
618
- * that you want to search for in the binary tree. It can be of any type that is returned by the
619
- * callback function `C`. It can also be `null` or `undefined` if you don't want to specify a
620
- * specific identifier.
621
- * @param {C} callback - The `callback` parameter is a function that will be called for each node in
622
- * the binary tree. It is used to filter the nodes based on certain conditions. The `callback`
623
- * function should return a boolean value indicating whether the node should be included in the
624
- * result or not.
625
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
626
- * for the search in the binary tree. It can be specified as a `K` (a unique identifier for a
627
- * node in the binary tree), a node object (`NODE`), or `null`/`undefined` to start the search from
628
- * @param iterationType - The `iterationType` parameter is a variable that determines the type of
629
- * iteration to be performed on the binary tree. It is used to specify whether the iteration should
630
- * be performed in a pre-order, in-order, or post-order manner.
631
- * @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.
632
684
  */
633
685
  has(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
634
686
  callback = this._ensureCallback(identifier, callback);
@@ -672,9 +724,10 @@ export class BinaryTree extends IterableEntryBase {
672
724
  *
673
725
  * The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
674
726
  * height of the tree.
675
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
676
- * for calculating the height and minimum height of a binary tree. It can be either a `K` (a key
677
- * 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
678
731
  * @returns a boolean value.
679
732
  */
680
733
  isPerfectlyBalanced(beginRoot = this.root) {
@@ -688,12 +741,14 @@ export class BinaryTree extends IterableEntryBase {
688
741
  * Time Complexity: O(n)
689
742
  * Space Complexity: O(1)
690
743
  *
691
- * The function `isSubtreeBST` checks if a given binary tree is a valid binary search tree.
692
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the root
693
- * node of the binary search tree (BST) that you want to check if it is a subtree of another BST.
694
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
695
- * type of iteration to use when checking if a subtree is a binary search tree (BST). It can have two
696
- * 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:
697
752
  * @returns a boolean value.
698
753
  */
699
754
  isBST(beginRoot = this.root, iterationType = this.iterationType) {
@@ -746,14 +801,15 @@ export class BinaryTree extends IterableEntryBase {
746
801
  * Time Complexity: O(n)
747
802
  * Space Complexity: O(1)
748
803
  *
749
- * The function calculates the depth of a given node in a binary tree.
750
- * @param {K | NODE | null | undefined} dist - The `dist` parameter represents the node in
751
- * the binary tree whose depth we want to find. It can be of type `K`, `NODE`, `null`, or
752
- * `undefined`.
753
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
754
- * from which we want to calculate the depth. It can be either a `K` (binary tree node key) or
755
- * `NODE` (binary tree node) or `null` or `undefined`. If no value is provided for `beginRoot
756
- * @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.
757
813
  */
758
814
  getDepth(dist, beginRoot = this.root) {
759
815
  let distEnsured = this.ensureNode(dist);
@@ -774,17 +830,16 @@ export class BinaryTree extends IterableEntryBase {
774
830
  */
775
831
  /**
776
832
  * Time Complexity: O(n)
777
- * Space Complexity: O(log n)
833
+ * Space Complexity: O(1)
778
834
  *
779
- * The function `getHeight` calculates the maximum height of a binary tree using either recursive or
780
- * iterative traversal.
781
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the
782
- * starting node of the binary tree from which we want to calculate the height. It can be of type
783
- * `K`, `NODE`, `null`, or `undefined`. If not provided, it defaults to `this.root`.
784
- * @param iterationType - The `iterationType` parameter is used to determine whether to calculate the
785
- * height of the tree using a recursive approach or an iterative approach. It can have two possible
786
- * values:
787
- * @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.
788
843
  */
789
844
  getHeight(beginRoot = this.root, iterationType = this.iterationType) {
790
845
  beginRoot = this.ensureNode(beginRoot);
@@ -824,12 +879,15 @@ export class BinaryTree extends IterableEntryBase {
824
879
  *
825
880
  * The `getMinHeight` function calculates the minimum height of a binary tree using either a
826
881
  * recursive or iterative approach.
827
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the
828
- * starting node of the binary tree from which we want to calculate the minimum height. It can be of
829
- * type `K`, `NODE`, `null`, or `undefined`. If no value is provided, it defaults to `this.root`.
830
- * @param iterationType - The `iterationType` parameter is used to determine the method of iteration
831
- * to calculate the minimum height of a binary tree. It can have two possible values:
832
- * @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.
833
891
  */
834
892
  getMinHeight(beginRoot = this.root, iterationType = this.iterationType) {
835
893
  beginRoot = this.ensureNode(beginRoot);
@@ -878,31 +936,27 @@ export class BinaryTree extends IterableEntryBase {
878
936
  /**
879
937
  * Time Complexity: O(log n)
880
938
  * Space Complexity: O(log n)
881
- * /
882
-
883
- /**
939
+ */
940
+ /**
884
941
  * Time Complexity: O(log n)
885
942
  * Space Complexity: O(log n)
886
943
  *
887
- * The function `getPathToRoot` returns an array of nodes from a given node to the root of a tree
888
- * structure, with the option to reverse the order of the nodes.
889
- * @param {K | NODE | null | undefined} beginNode - The `beginRoot` parameter represents the
890
- * starting node from which you want to find the path to the root. It can be of type `K`, `NODE`,
891
- * `null`, or `undefined`.
944
+ * The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
945
+ * up to the root node, with an option to reverse the order of the nodes.
946
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginNode - The `beginNode` parameter can be either of
947
+ * type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
892
948
  * @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
893
949
  * resulting path should be reversed or not. If `isReverse` is set to `true`, the path will be
894
- * reversed before returning it. If `isReverse` is set to `false`, the path will be returned as is
895
- * @returns The function `getPathToRoot` returns an array of nodes (`NODE[]`).
950
+ * reversed before returning it. If `isReverse` is set to `false` or not provided, the path will
951
+ * @returns The function `getPathToRoot` returns an array of `NODE` objects.
896
952
  */
897
953
  getPathToRoot(beginNode, isReverse = true) {
898
- // TODO to support get path through passing key
899
954
  const result = [];
900
955
  let beginNodeEnsured = this.ensureNode(beginNode);
901
956
  if (!beginNodeEnsured)
902
957
  return result;
903
958
  while (beginNodeEnsured.parent) {
904
959
  // Array.push + Array.reverse is more efficient than Array.unshift
905
- // TODO may consider using Deque, so far this is not the performance bottleneck
906
960
  result.push(beginNodeEnsured);
907
961
  beginNodeEnsured = beginNodeEnsured.parent;
908
962
  }
@@ -917,15 +971,14 @@ export class BinaryTree extends IterableEntryBase {
917
971
  * Time Complexity: O(log n)
918
972
  * Space Complexity: O(1)
919
973
  *
920
- * The function `getLeftMost` returns the leftmost node in a binary tree, either recursively or
921
- * iteratively.
922
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
923
- * for finding the leftmost node in a binary tree. It can be either a `K` (a key value), `NODE` (a
924
- * node), `null`, or `undefined`. If not provided, it defaults to `this.root`,
925
- * @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
926
- * be performed when finding the leftmost node in a binary tree. It can have two possible values:
927
- * @returns The function `getLeftMost` returns the leftmost node (`NODE`) in the binary tree. If there
928
- * is no leftmost node, it returns `null` or `undefined` depending on the input.
974
+ * The `getLeftMost` function returns the leftmost node in a binary tree, either using recursive or
975
+ * iterative traversal.
976
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
977
+ * starting point for finding the leftmost node in a binary tree. It can be either a root node (`R`),
978
+ * a key or node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or `null` or `undefined`.
979
+ * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
980
+ * of iteration to be performed. It can have two possible values:
981
+ * @returns The function `getLeftMost` returns the leftmost node in a binary tree.
929
982
  */
930
983
  getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
931
984
  if (this.isNIL(beginRoot))
@@ -959,16 +1012,15 @@ export class BinaryTree extends IterableEntryBase {
959
1012
  * Time Complexity: O(log n)
960
1013
  * Space Complexity: O(1)
961
1014
  *
962
- * The function `getRightMost` returns the rightmost node in a binary tree, either recursively or
1015
+ * The `getRightMost` function returns the rightmost node in a binary tree, either recursively or
963
1016
  * iteratively.
964
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the
965
- * starting node from which we want to find the rightmost node. It can be of type `K`, `NODE`,
966
- * `null`, or `undefined`. If not provided, it defaults to `this.root`, which is a property of the
967
- * current object.
968
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
969
- * type of iteration to use when finding the rightmost node. It can have one of two values:
970
- * @returns The function `getRightMost` returns the rightmost node (`NODE`) in a binary tree. If there
971
- * is no rightmost node, it returns `null` or `undefined`, depending on the input.
1017
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
1018
+ * starting point for finding the rightmost node in a binary tree. It can be either a root node
1019
+ * (`R`), a key or node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or `null` or `undefined`.
1020
+ * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
1021
+ * of iteration to be performed when finding the rightmost node in a binary tree. It can have two
1022
+ * possible values:
1023
+ * @returns The function `getRightMost` returns a NODE object, `null`, or `undefined`.
972
1024
  */
973
1025
  getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
974
1026
  if (this.isNIL(beginRoot))
@@ -1003,10 +1055,10 @@ export class BinaryTree extends IterableEntryBase {
1003
1055
  * Time Complexity: O(log n)
1004
1056
  * Space Complexity: O(1)
1005
1057
  *
1006
- * The function returns the predecessor of a given node in a tree.
1007
- * @param {NODE} node - The parameter `node` is of type `RedBlackTreeNode`, which represents a node in a
1058
+ * The function returns the predecessor node of a given node in a binary tree.
1059
+ * @param {NODE} node - The parameter "node" is of type "NODE", which represents a node in a binary
1008
1060
  * tree.
1009
- * @returns the predecessor of the given 'node'.
1061
+ * @returns the predecessor node of the given node.
1010
1062
  */
1011
1063
  getPredecessor(node) {
1012
1064
  if (this.isRealNode(node.left)) {
@@ -1032,8 +1084,8 @@ export class BinaryTree extends IterableEntryBase {
1032
1084
  *
1033
1085
  * The function `getSuccessor` returns the next node in a binary tree given a current node.
1034
1086
  * @param {K | NODE | null} [x] - The parameter `x` can be of type `K`, `NODE`, or `null`.
1035
- * @returns the successor of the given node or key. The successor is the node that comes immediately
1036
- * after the given node in the inorder traversal of the binary tree.
1087
+ * @returns The function `getSuccessor` returns a `NODE` object if a successor exists, `null` if
1088
+ * there is no successor, and `undefined` if the input `x` is not a valid node.
1037
1089
  */
1038
1090
  getSuccessor(x) {
1039
1091
  x = this.ensureNode(x);
@@ -1052,30 +1104,29 @@ export class BinaryTree extends IterableEntryBase {
1052
1104
  /**
1053
1105
  * Time complexity: O(n)
1054
1106
  * Space complexity: O(n)
1055
- * /
1056
-
1057
- /**
1107
+ */
1108
+ /**
1058
1109
  * Time complexity: O(n)
1059
1110
  * Space complexity: O(n)
1060
1111
  *
1061
- * The `dfs` function performs a depth-first search traversal on a binary tree or graph, based on the
1062
- * specified pattern and iteration type, and returns an array of values obtained from applying a
1063
- * callback function to each visited node.
1064
- * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1065
- * the tree during the depth-first search. It takes a single parameter, which can be of type `NODE`,
1066
- * `null`, or `undefined`, and returns a value of any type. The default value for this parameter is
1067
- * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the
1068
- * nodes are traversed during the depth-first search. It can have one of the following values:
1069
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
1070
- * for the depth-first search traversal. It can be specified as a key, a node object, or
1071
- * `null`/`undefined`. If not provided, the `beginRoot` will default to the root node of the tree.
1072
- * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
1073
- * iteration to use when traversing the tree. It can have one of the following values:
1112
+ * The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback
1113
+ * function on each node according to a specified pattern and iteration type.
1114
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node
1115
+ * visited during the depth-first search. It takes a node as an argument and returns a value. The
1116
+ * return type of the callback function is determined by the generic type `C`.
1117
+ * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter determines the order in which the
1118
+ * nodes are visited during the depth-first search. It can have one of the following values:
1119
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
1120
+ * point of the depth-first search. It can be either a node object, a key-value pair, or a key. If it
1121
+ * is a key or key-value pair, the method will find the corresponding node in the tree and start the
1122
+ * search from there.
1123
+ * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter determines the
1124
+ * type of iteration to use during the depth-first search. It can have two possible values:
1074
1125
  * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
1075
- * whether null or undefined nodes should be included in the traversal. If `includeNull` is set to
1076
- * `true`, null or undefined nodes will be included in the traversal. If `includeNull` is set to
1077
- * `false`, null or undefined
1078
- * @returns an array of values that are the return values of the callback function.
1126
+ * whether or not to include null values in the depth-first search traversal. If `includeNull` is set
1127
+ * to `true`, null values will be included in the traversal. If `includeNull` is set to `false`, null
1128
+ * values will
1129
+ * @returns an array of the return types of the callback function.
1079
1130
  */
1080
1131
  dfs(callback = this._DEFAULT_CALLBACK, pattern = 'IN', beginRoot = this.root, iterationType = 'ITERATIVE', includeNull = false) {
1081
1132
  beginRoot = this.ensureNode(beginRoot);
@@ -1191,22 +1242,23 @@ export class BinaryTree extends IterableEntryBase {
1191
1242
  * Time complexity: O(n)
1192
1243
  * Space complexity: O(n)
1193
1244
  *
1194
- * The `bfs` function performs a breadth-first search traversal on a binary tree, executing a
1195
- * callback function on each node.
1245
+ * The `bfs` function performs a breadth-first search on a binary tree, calling a callback function
1246
+ * on each node and returning an array of the results.
1196
1247
  * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1197
- * the breadth-first search traversal. It takes a single parameter, which is the current node being
1248
+ * the breadth-first search traversal. It takes a single argument, which is the current node being
1198
1249
  * visited, and returns a value of any type.
1199
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the
1200
- * starting node for the breadth-first search traversal. It can be specified as a key, a node object,
1201
- * or `null`/`undefined` to indicate the root of the tree. If not provided, the `root` property of
1202
- * the class is used as
1203
- * @param iterationType - The `iterationType` parameter determines the type of iteration to be
1204
- * performed during the breadth-first search (BFS). It can have two possible values:
1205
- * @param [includeNull=false] - The `includeNull` parameter is a boolean flag that determines whether
1206
- * to include null values in the breadth-first search traversal. If `includeNull` is set to
1207
- * `true`, null values will be included in the traversal, otherwise they will be skipped.
1208
- * @returns an array of values that are the result of invoking the callback function on each node in
1209
- * the breadth-first traversal of a binary tree.
1250
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
1251
+ * starting point of the breadth-first search. It can be either a root node of a tree or a key, node,
1252
+ * or entry object. If no value is provided, the `root` property of the class is used as the default
1253
+ * starting point.
1254
+ * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
1255
+ * iteration to be performed. It can have two possible values:
1256
+ * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
1257
+ * whether or not to include null values in the breadth-first search (BFS) traversal. If
1258
+ * `includeNull` is set to `true`, null values will be included in the traversal. If `includeNull` is
1259
+ * set to `false
1260
+ * @returns The function `bfs` returns an array of values that are the result of invoking the
1261
+ * `callback` function on each node in the breadth-first order traversal of the binary tree.
1210
1262
  */
1211
1263
  bfs(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
1212
1264
  beginRoot = this.ensureNode(beginRoot);
@@ -1269,18 +1321,18 @@ export class BinaryTree extends IterableEntryBase {
1269
1321
  * Space complexity: O(n)
1270
1322
  *
1271
1323
  * The `listLevels` function returns an array of arrays, where each inner array represents a level in
1272
- * a binary tree and contains the values returned by a callback function applied to the nodes at that
1273
- * level.
1324
+ * a binary tree and contains the results of applying a callback function to the nodes at that level.
1274
1325
  * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1275
- * the tree. It takes a single parameter, which can be of type `NODE`, `null`, or `undefined`, and
1276
- * returns a value of any type.
1277
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the
1278
- * starting node for traversing the tree. It can be either a node object (`NODE`), a key value
1279
- * (`K`), `null`, or `undefined`. If not provided, it defaults to the root node of the tree.
1280
- * @param iterationType - The `iterationType` parameter determines the type of iteration to be
1281
- * performed on the tree. It can have two possible values:
1326
+ * the tree. It takes a node as an argument and returns a value. The return type of the callback
1327
+ * function is determined by the generic type `C` which extends `BTNCallback<NODE | null>`.
1328
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
1329
+ * starting point for traversing the tree. It can be either a root node, a key-value pair, or a node
1330
+ * entry. If no value is provided, the `root` property of the class is used as the default starting
1331
+ * point.
1332
+ * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
1333
+ * iteration to be performed on the binary tree. It can have two possible values:
1282
1334
  * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
1283
- * whether to include null values in the resulting levels. If `includeNull` is set to `true`,
1335
+ * whether or not to include null values in the resulting levels. If `includeNull` is set to `true`,
1284
1336
  * null values will be included in the levels. If `includeNull` is set to `false`, null values will
1285
1337
  * be excluded
1286
1338
  * @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
@@ -1345,17 +1397,17 @@ export class BinaryTree extends IterableEntryBase {
1345
1397
  * The `morris` function performs a depth-first traversal on a binary tree using the Morris traversal
1346
1398
  * algorithm.
1347
1399
  * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1348
- * the tree. It takes a single parameter of type `NODE` (the type of the nodes in the tree) and returns
1349
- * a value of any type.
1350
- * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
1351
- * determines the order in which the nodes of a binary tree are traversed. It can have one of the
1400
+ * the tree. It takes a single argument, which is the current node, and can return any value. The
1401
+ * return type of the `callback` function is determined by the `ReturnType<C>` type, which represents
1402
+ * the return
1403
+ * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function is used
1404
+ * to specify the order in which the nodes of a binary tree are traversed. It can take one of the
1352
1405
  * following values:
1353
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
1354
- * for the traversal. It can be specified as a key, a node object, or `null`/`undefined` to indicate
1355
- * the root of the tree. If no value is provided, the default value is the root of the tree.
1356
- * @returns The function `morris` returns an array of values that are the result of invoking the
1357
- * `callback` function on each node in the binary tree. The type of the array nodes is determined
1358
- * by the return type of the `callback` function.
1406
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
1407
+ * point for the traversal. It can be either a node object, a key, or an entry object. If no value is
1408
+ * provided, the `root` of the tree is used as the starting point.
1409
+ * @returns The function `morris` returns an array of values that are the return values of the
1410
+ * callback function `callback`.
1359
1411
  */
1360
1412
  morris(callback = this._DEFAULT_CALLBACK, pattern = 'IN', beginRoot = this.root) {
1361
1413
  beginRoot = this.ensureNode(beginRoot);
@@ -1450,8 +1502,7 @@ export class BinaryTree extends IterableEntryBase {
1450
1502
  * Time complexity: O(n)
1451
1503
  * Space complexity: O(n)
1452
1504
  *
1453
- * The `clone` function creates a new tree object and copies all the nodes from the original tree to
1454
- * the new tree.
1505
+ * The `clone` function creates a deep copy of a tree object.
1455
1506
  * @returns The `clone()` method is returning a cloned instance of the `TREE` object.
1456
1507
  */
1457
1508
  clone() {
@@ -1472,16 +1523,16 @@ export class BinaryTree extends IterableEntryBase {
1472
1523
  * Time Complexity: O(n)
1473
1524
  * Space Complexity: O(n)
1474
1525
  *
1475
- * The `filter` function creates a new tree by iterating over the nodes of the current tree and
1476
- * adding only the nodes that satisfy the given predicate function.
1477
- * @param predicate - The `predicate` parameter is a function that takes three arguments: `value`,
1478
- * `key`, and `index`. It should return a boolean value indicating whether the pair should be
1479
- * included in the filtered tree or not.
1480
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
1481
- * to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
1482
- * it will be passed as the first argument to the `predicate` function. If `thisArg` is
1483
- * @returns The `filter` method is returning a new tree object that contains the key-value pairs that
1484
- * pass the given predicate function.
1526
+ * The `filter` function creates a new tree with entries that pass a given predicate function.
1527
+ * @param predicate - The `predicate` parameter is a callback function that is used to test each
1528
+ * element in the tree. It takes three arguments: `value`, `key`, and `index`. The `value` argument
1529
+ * represents the value of the current element being processed, the `key` argument represents the key
1530
+ * of the
1531
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
1532
+ * specify the value of `this` within the `predicate` function. When the `predicate` function is
1533
+ * called, `thisArg` will be used as the value of `this` within the function. If `thisArg`
1534
+ * @returns The `filter` method is returning a new tree object that contains the entries that pass
1535
+ * the given predicate function.
1485
1536
  */
1486
1537
  filter(predicate, thisArg) {
1487
1538
  const newTree = this.createTree();
@@ -1501,15 +1552,15 @@ export class BinaryTree extends IterableEntryBase {
1501
1552
  * Time Complexity: O(n)
1502
1553
  * Space Complexity: O(n)
1503
1554
  *
1504
- * The `map` function creates a new tree by applying a callback function to each key-value pair in
1505
- * the original tree.
1506
- * @param callback - The callback parameter is a function that will be called for each key-value pair
1507
- * in the tree. It takes four arguments: the value of the current pair, the key of the current pair,
1508
- * the index of the current pair, and a reference to the tree itself. The callback function should
1509
- * return a new
1510
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
1511
- * specify the value of `this` within the callback function. If you pass a value for `thisArg`, it
1512
- * will be used as the `this` value when the callback function is called. If you don't pass a value
1555
+ * The `map` function creates a new tree by applying a callback function to each entry in the current
1556
+ * tree.
1557
+ * @param callback - The callback parameter is a function that will be called for each entry in the
1558
+ * tree. It takes three arguments: value, key, and index. The value argument represents the value of
1559
+ * the current entry, the key argument represents the key of the current entry, and the index
1560
+ * argument represents the index of the
1561
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
1562
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
1563
+ * passed as the `this` value to the `callback` function. If `thisArg` is
1513
1564
  * @returns The `map` method is returning a new tree object.
1514
1565
  */
1515
1566
  map(callback, thisArg) {
@@ -1537,11 +1588,15 @@ export class BinaryTree extends IterableEntryBase {
1537
1588
  * Time Complexity: O(n)
1538
1589
  * Space Complexity: O(n)
1539
1590
  *
1540
- * The `print` function is used to display a binary tree structure in a visually appealing way.
1541
- * @param {K | NODE | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `K | NODE | null |
1542
- * undefined`. It represents the root node of a binary tree. The root node can have one of the
1543
- * following types:
1544
- * @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.
1591
+ * The `print` function in TypeScript prints the binary tree structure with customizable options.
1592
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
1593
+ * point for printing the binary tree. It can be either a node of the binary tree or a key or entry
1594
+ * that exists in the binary tree. If no value is provided, the root of the binary tree will be used
1595
+ * as the starting point.
1596
+ * @param {BinaryTreePrintOptions} [options] - The `options` parameter is an optional object that
1597
+ * allows you to customize the printing behavior. It has the following properties:
1598
+ * @returns Nothing is being returned. The function has a return type of `void`, which means it does
1599
+ * not return any value.
1545
1600
  */
1546
1601
  print(beginRoot = this.root, options) {
1547
1602
  const opts = { isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false, ...options };
@@ -1566,13 +1621,18 @@ export class BinaryTree extends IterableEntryBase {
1566
1621
  display(beginRoot);
1567
1622
  }
1568
1623
  /**
1569
- * The function `_getIterator` is a protected generator function that returns an iterator for the
1570
- * key-value pairs in a binary search tree.
1571
- * @param node - The `node` parameter represents the current node in the binary search tree. It is an
1572
- * optional parameter with a default value of `this.root`, which means if no node is provided, the
1573
- * root node of the tree will be used as the starting point for iteration.
1574
- * @returns The function `_getIterator` returns an `IterableIterator` of key-value pairs `[K, V |
1575
- * undefined]`.
1624
+ * Time Complexity: O(1)
1625
+ * Space Complexity: O(1)
1626
+ */
1627
+ /**
1628
+ * Time Complexity: O(1)
1629
+ * Space Complexity: O(1)
1630
+ *
1631
+ * The function `_getIterator` is a generator function that returns an iterator for the key-value
1632
+ * pairs in a binary search tree.
1633
+ * @param node - The `node` parameter represents the current node in the binary search tree. It is
1634
+ * initially set to the root node of the tree.
1635
+ * @returns an IterableIterator<[K, V | undefined]>.
1576
1636
  */
1577
1637
  *_getIterator(node = this.root) {
1578
1638
  if (!node)
@@ -1603,6 +1663,13 @@ export class BinaryTree extends IterableEntryBase {
1603
1663
  }
1604
1664
  }
1605
1665
  /**
1666
+ * Time Complexity: O(n)
1667
+ * Space Complexity: O(n)
1668
+ */
1669
+ /**
1670
+ * Time Complexity: O(n)
1671
+ * Space Complexity: O(n)
1672
+ *
1606
1673
  * The `_displayAux` function is responsible for generating the display layout of a binary tree node,
1607
1674
  * taking into account various options such as whether to show null, undefined, or NaN nodes.
1608
1675
  * @param {NODE | null | undefined} node - The `node` parameter represents a node in a binary tree.
@@ -1670,10 +1737,21 @@ export class BinaryTree extends IterableEntryBase {
1670
1737
  }
1671
1738
  _DEFAULT_CALLBACK = (node) => (node ? node.key : undefined);
1672
1739
  /**
1673
- * Swap the data of two nodes in the binary tree.
1674
- * @param {NODE} srcNode - The source node to swap.
1675
- * @param {NODE} destNode - The destination node to swap.
1676
- * @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.
1677
1755
  */
1678
1756
  _swapProperties(srcNode, destNode) {
1679
1757
  srcNode = this.ensureNode(srcNode);
@@ -1692,12 +1770,20 @@ export class BinaryTree extends IterableEntryBase {
1692
1770
  return undefined;
1693
1771
  }
1694
1772
  /**
1695
- * 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.
1696
1782
  * @param {NODE} oldNode - The oldNode parameter represents the node that needs to be replaced in the
1697
1783
  * tree.
1698
1784
  * @param {NODE} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
1699
1785
  * tree.
1700
- * @returns The method is returning the newNode.
1786
+ * @returns the newNode.
1701
1787
  */
1702
1788
  _replaceNode(oldNode, newNode) {
1703
1789
  if (oldNode.parent) {
@@ -1717,10 +1803,17 @@ export class BinaryTree extends IterableEntryBase {
1717
1803
  return newNode;
1718
1804
  }
1719
1805
  /**
1720
- * The function sets the root property of an object to a given value, and if the value is not null,
1721
- * it also sets the parent property of the value to undefined.
1722
- * @param {NODE | null | undefined} v - The parameter `v` is of type `NODE | null | undefined`, which means it can either be of
1723
- * 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`.
1724
1817
  */
1725
1818
  _setRoot(v) {
1726
1819
  if (v) {
@@ -1728,6 +1821,23 @@ export class BinaryTree extends IterableEntryBase {
1728
1821
  }
1729
1822
  this._root = v;
1730
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
+ */
1731
1841
  _ensureCallback(identifier, callback = this._DEFAULT_CALLBACK) {
1732
1842
  if ((!callback || callback === this._DEFAULT_CALLBACK) && this.isNode(identifier)) {
1733
1843
  callback = (node => node);