undirected-graph-typed 1.51.8 → 1.52.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. package/dist/data-structures/base/index.d.ts +2 -1
  2. package/dist/data-structures/base/index.js +2 -1
  3. package/dist/data-structures/base/iterable-element-base.d.ts +171 -0
  4. package/dist/data-structures/base/iterable-element-base.js +225 -0
  5. package/dist/data-structures/base/{iterable-base.d.ts → iterable-entry-base.d.ts} +4 -147
  6. package/dist/data-structures/base/{iterable-base.js → iterable-entry-base.js} +12 -189
  7. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +106 -68
  8. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +119 -87
  9. package/dist/data-structures/binary-tree/avl-tree.d.ts +82 -62
  10. package/dist/data-structures/binary-tree/avl-tree.js +78 -59
  11. package/dist/data-structures/binary-tree/binary-tree.d.ts +333 -241
  12. package/dist/data-structures/binary-tree/binary-tree.js +478 -366
  13. package/dist/data-structures/binary-tree/bst.d.ts +202 -212
  14. package/dist/data-structures/binary-tree/bst.js +208 -250
  15. package/dist/data-structures/binary-tree/rb-tree.d.ts +73 -74
  16. package/dist/data-structures/binary-tree/rb-tree.js +107 -98
  17. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +92 -75
  18. package/dist/data-structures/binary-tree/tree-multi-map.js +105 -93
  19. package/dist/data-structures/graph/abstract-graph.d.ts +10 -15
  20. package/dist/data-structures/graph/abstract-graph.js +10 -15
  21. package/dist/data-structures/graph/directed-graph.js +2 -1
  22. package/dist/data-structures/hash/hash-map.d.ts +33 -40
  23. package/dist/data-structures/hash/hash-map.js +40 -55
  24. package/dist/data-structures/heap/heap.d.ts +43 -114
  25. package/dist/data-structures/heap/heap.js +59 -127
  26. package/dist/data-structures/heap/max-heap.d.ts +50 -4
  27. package/dist/data-structures/heap/max-heap.js +76 -10
  28. package/dist/data-structures/heap/min-heap.d.ts +51 -5
  29. package/dist/data-structures/heap/min-heap.js +68 -11
  30. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +22 -28
  31. package/dist/data-structures/linked-list/doubly-linked-list.js +26 -28
  32. package/dist/data-structures/linked-list/singly-linked-list.d.ts +22 -25
  33. package/dist/data-structures/linked-list/singly-linked-list.js +29 -26
  34. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +50 -4
  35. package/dist/data-structures/priority-queue/max-priority-queue.js +79 -10
  36. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +51 -5
  37. package/dist/data-structures/priority-queue/min-priority-queue.js +71 -11
  38. package/dist/data-structures/priority-queue/priority-queue.d.ts +50 -4
  39. package/dist/data-structures/priority-queue/priority-queue.js +70 -1
  40. package/dist/data-structures/queue/deque.d.ts +28 -20
  41. package/dist/data-structures/queue/deque.js +45 -24
  42. package/dist/data-structures/queue/queue.d.ts +8 -29
  43. package/dist/data-structures/queue/queue.js +15 -32
  44. package/dist/data-structures/stack/stack.d.ts +17 -22
  45. package/dist/data-structures/stack/stack.js +25 -24
  46. package/dist/data-structures/trie/trie.d.ts +19 -14
  47. package/dist/data-structures/trie/trie.js +27 -16
  48. package/dist/interfaces/binary-tree.d.ts +7 -7
  49. package/dist/types/common.d.ts +1 -23
  50. package/dist/types/data-structures/base/base.d.ts +5 -2
  51. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -4
  52. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -4
  53. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +22 -5
  54. package/dist/types/data-structures/binary-tree/bst.d.ts +7 -5
  55. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -4
  56. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -4
  57. package/dist/types/data-structures/heap/heap.d.ts +3 -2
  58. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
  59. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +2 -1
  60. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
  61. package/dist/types/data-structures/queue/deque.d.ts +4 -2
  62. package/dist/types/data-structures/queue/queue.d.ts +2 -1
  63. package/dist/types/data-structures/stack/stack.d.ts +2 -1
  64. package/dist/types/data-structures/trie/trie.d.ts +3 -2
  65. package/dist/utils/utils.js +3 -5
  66. package/package.json +2 -2
  67. package/src/data-structures/base/index.ts +2 -1
  68. package/src/data-structures/base/iterable-element-base.ts +250 -0
  69. package/src/data-structures/base/{iterable-base.ts → iterable-entry-base.ts} +22 -213
  70. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +146 -97
  71. package/src/data-structures/binary-tree/avl-tree.ts +97 -70
  72. package/src/data-structures/binary-tree/binary-tree.ts +591 -455
  73. package/src/data-structures/binary-tree/bst.ts +266 -293
  74. package/src/data-structures/binary-tree/rb-tree.ts +124 -104
  75. package/src/data-structures/binary-tree/tree-multi-map.ts +128 -103
  76. package/src/data-structures/graph/abstract-graph.ts +10 -10
  77. package/src/data-structures/graph/directed-graph.ts +2 -1
  78. package/src/data-structures/hash/hash-map.ts +46 -53
  79. package/src/data-structures/heap/heap.ts +71 -152
  80. package/src/data-structures/heap/max-heap.ts +88 -13
  81. package/src/data-structures/heap/min-heap.ts +78 -15
  82. package/src/data-structures/linked-list/doubly-linked-list.ts +32 -32
  83. package/src/data-structures/linked-list/singly-linked-list.ts +37 -29
  84. package/src/data-structures/priority-queue/max-priority-queue.ts +94 -13
  85. package/src/data-structures/priority-queue/min-priority-queue.ts +84 -15
  86. package/src/data-structures/priority-queue/priority-queue.ts +81 -4
  87. package/src/data-structures/queue/deque.ts +52 -27
  88. package/src/data-structures/queue/queue.ts +23 -37
  89. package/src/data-structures/stack/stack.ts +31 -26
  90. package/src/data-structures/trie/trie.ts +35 -20
  91. package/src/interfaces/binary-tree.ts +10 -10
  92. package/src/types/common.ts +2 -25
  93. package/src/types/data-structures/base/base.ts +14 -6
  94. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +3 -4
  95. package/src/types/data-structures/binary-tree/avl-tree.ts +3 -4
  96. package/src/types/data-structures/binary-tree/binary-tree.ts +26 -6
  97. package/src/types/data-structures/binary-tree/bst.ts +11 -5
  98. package/src/types/data-structures/binary-tree/rb-tree.ts +3 -4
  99. package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -4
  100. package/src/types/data-structures/heap/heap.ts +4 -1
  101. package/src/types/data-structures/linked-list/doubly-linked-list.ts +3 -1
  102. package/src/types/data-structures/linked-list/singly-linked-list.ts +3 -1
  103. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
  104. package/src/types/data-structures/queue/deque.ts +6 -1
  105. package/src/types/data-structures/queue/queue.ts +3 -1
  106. package/src/types/data-structures/stack/stack.ts +3 -1
  107. package/src/types/data-structures/trie/trie.ts +3 -1
  108. package/src/utils/utils.ts +3 -3
@@ -14,13 +14,13 @@ import type {
14
14
  BinaryTreePrintOptions,
15
15
  BTNCallback,
16
16
  BTNEntry,
17
- Comparable,
17
+ BTNKeyOrNodeOrEntry,
18
18
  DFSOrderPattern,
19
19
  EntryCallback,
20
20
  FamilyPosition,
21
21
  IterationType,
22
- KeyOrNodeOrEntry,
23
- NodeDisplayLayout
22
+ NodeDisplayLayout,
23
+ OptBTNOrNull
24
24
  } from '../../types';
25
25
  import { IBinaryTree } from '../../interfaces';
26
26
  import { trampoline } from '../../utils';
@@ -33,7 +33,7 @@ import { IterableEntryBase } from '../base';
33
33
  * @template NODE - The type of the family relationship in the binary tree.
34
34
  */
35
35
  export class BinaryTreeNode<
36
- K extends Comparable,
36
+ K = any,
37
37
  V = any,
38
38
  NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>
39
39
  > {
@@ -63,16 +63,16 @@ export class BinaryTreeNode<
63
63
  * @returns The left node of the current node is being returned. It can be either a NODE object,
64
64
  * null, or undefined.
65
65
  */
66
- get left(): NODE | null | undefined {
66
+ get left(): OptBTNOrNull<NODE> {
67
67
  return this._left;
68
68
  }
69
69
 
70
70
  /**
71
71
  * The function sets the left child of a node and updates its parent reference.
72
- * @param {NODE | null | undefined} v - The parameter `v` can be of type `NODE`, `null`, or
72
+ * @param {OptBTNOrNull<NODE>} v - The parameter `v` can be of type `NODE`, `null`, or
73
73
  * `undefined`.
74
74
  */
75
- set left(v: NODE | null | undefined) {
75
+ set left(v: OptBTNOrNull<NODE>) {
76
76
  if (v) {
77
77
  v.parent = this as unknown as NODE;
78
78
  }
@@ -86,16 +86,16 @@ export class BinaryTreeNode<
86
86
  * @returns The method is returning the value of the `_right` property, which can be a `NODE` object,
87
87
  * `null`, or `undefined`.
88
88
  */
89
- get right(): NODE | null | undefined {
89
+ get right(): OptBTNOrNull<NODE> {
90
90
  return this._right;
91
91
  }
92
92
 
93
93
  /**
94
94
  * The function sets the right child of a node and updates its parent.
95
- * @param {NODE | null | undefined} v - The parameter `v` can be of type `NODE`, `null`, or
95
+ * @param {OptBTNOrNull<NODE>} v - The parameter `v` can be of type `NODE`, `null`, or
96
96
  * `undefined`.
97
97
  */
98
- set right(v: NODE | null | undefined) {
98
+ set right(v: OptBTNOrNull<NODE>) {
99
99
  if (v) {
100
100
  v.parent = this as unknown as NODE;
101
101
  }
@@ -131,48 +131,52 @@ export class BinaryTreeNode<
131
131
  */
132
132
 
133
133
  export class BinaryTree<
134
- K extends Comparable,
134
+ K = any,
135
135
  V = any,
136
+ R = BTNEntry<K, V>,
136
137
  NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>,
137
- TREE extends BinaryTree<K, V, NODE, TREE> = BinaryTree<K, V, NODE, BinaryTreeNested<K, V, NODE>>
138
+ TREE extends BinaryTree<K, V, R, NODE, TREE> = BinaryTree<K, V, R, NODE, BinaryTreeNested<K, V, R, NODE>>
138
139
  >
139
140
  extends IterableEntryBase<K, V | undefined>
140
- implements IBinaryTree<K, V, NODE, TREE> {
141
+ implements IBinaryTree<K, V, R, NODE, TREE> {
141
142
  iterationType: IterationType = 'ITERATIVE';
142
143
 
143
144
  /**
144
- * The constructor function initializes a binary tree object with optional keysOrNodesOrEntries and options.
145
- * @param [keysOrNodesOrEntries] - An optional iterable of KeyOrNodeOrEntry objects. These objects represent the
145
+ * The constructor function initializes a binary tree object with optional keysOrNodesOrEntriesOrRawElements and options.
146
+ * @param [keysOrNodesOrEntriesOrRawElements] - Optional iterable of BTNKeyOrNodeOrEntry objects. These objects represent the
146
147
  * nodes to be added to the binary tree.
147
148
  * @param [options] - The `options` parameter is an optional object that can contain additional
148
149
  * configuration options for the binary tree. In this case, it is of type
149
150
  * `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
150
151
  * required.
151
152
  */
152
- constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: BinaryTreeOptions) {
153
+ constructor(
154
+ keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
155
+ options?: BinaryTreeOptions<K, V, R>
156
+ ) {
153
157
  super();
154
158
  if (options) {
155
- const { iterationType } = options;
159
+ const { iterationType, toEntryFn } = options;
156
160
  if (iterationType) this.iterationType = iterationType;
161
+ if (typeof toEntryFn === 'function') this._toEntryFn = toEntryFn;
162
+ else if (toEntryFn) throw TypeError('toEntryFn must be a function type');
157
163
  }
158
164
 
159
- this._size = 0;
160
-
161
- if (keysOrNodesOrEntries) this.addMany(keysOrNodesOrEntries);
165
+ if (keysOrNodesOrEntriesOrRawElements) this.addMany(keysOrNodesOrEntriesOrRawElements);
162
166
  }
163
167
 
164
- protected _root?: NODE | null;
168
+ protected _root?: OptBTNOrNull<NODE>;
165
169
 
166
170
  /**
167
171
  * The function returns the root node, which can be of type NODE, null, or undefined.
168
172
  * @returns The method is returning the value of the `_root` property, which can be of type `NODE`,
169
173
  * `null`, or `undefined`.
170
174
  */
171
- get root(): NODE | null | undefined {
175
+ get root(): OptBTNOrNull<NODE> {
172
176
  return this._root;
173
177
  }
174
178
 
175
- protected _size: number;
179
+ protected _size: number = 0;
176
180
 
177
181
  /**
178
182
  * The function returns the size of an object.
@@ -192,6 +196,16 @@ export class BinaryTree<
192
196
  return this._NIL;
193
197
  }
194
198
 
199
+ protected _toEntryFn?: (rawElement: R) => BTNEntry<K, V>;
200
+
201
+ /**
202
+ * The function returns the value of the _toEntryFn property.
203
+ * @returns The function being returned is `this._toEntryFn`.
204
+ */
205
+ get toEntryFn() {
206
+ return this._toEntryFn;
207
+ }
208
+
195
209
  /**
196
210
  * Creates a new instance of BinaryTreeNode with the given key and value.
197
211
  * @param {K} key - The key for the new node.
@@ -209,41 +223,46 @@ export class BinaryTree<
209
223
  * you can provide only a subset of the properties defined in the `BinaryTreeOptions` interface.
210
224
  * @returns a new instance of a binary tree.
211
225
  */
212
- createTree(options?: Partial<BinaryTreeOptions>): TREE {
213
- return new BinaryTree<K, V, NODE, TREE>([], { iterationType: this.iterationType, ...options }) as TREE;
226
+ createTree(options?: Partial<BinaryTreeOptions<K, V, R>>): TREE {
227
+ return new BinaryTree<K, V, R, NODE, TREE>([], { iterationType: this.iterationType, ...options }) as TREE;
214
228
  }
215
229
 
216
230
  /**
217
- * The function `keyValueOrEntryToNode` converts an keyOrNodeOrEntry object into a node object.
218
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
231
+ * The function `keyValueOrEntryOrRawElementToNode` converts a key-value pair, entry, or raw element
232
+ * into a node object.
233
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
234
+ * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
219
235
  * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
220
- * `keyValueOrEntryToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If no value
221
- * is provided, it will be `undefined`.
222
- * @returns a value of type NODE (node), or null, or undefined.
223
- */
224
- keyValueOrEntryToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): NODE | null | undefined {
225
- if (keyOrNodeOrEntry === undefined) return;
226
-
227
- let node: NODE | null | undefined;
228
- if (keyOrNodeOrEntry === null) {
229
- node = null;
230
- } else if (this.isEntry(keyOrNodeOrEntry)) {
231
- const [key, value] = keyOrNodeOrEntry;
232
- if (key === undefined) {
233
- return;
234
- } else if (key === null) {
235
- node = null;
236
- } else {
237
- node = this.createNode(key, value);
238
- }
239
- } else if (this.isNode(keyOrNodeOrEntry)) {
240
- node = keyOrNodeOrEntry;
241
- } else if (!this.isNode(keyOrNodeOrEntry)) {
242
- node = this.createNode(keyOrNodeOrEntry, value);
243
- } else {
244
- return;
236
+ * `keyValueOrEntryOrRawElementToNode` function. It represents the value associated with a key in a
237
+ * key-value pair. If provided, it will be used to create a node with the specified key and value.
238
+ * @returns The function `keyValueOrEntryOrRawElementToNode` returns either a `NODE` object, `null`,
239
+ * or `undefined`.
240
+ */
241
+ keyValueOrEntryOrRawElementToNode(
242
+ keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
243
+ value?: V
244
+ ): OptBTNOrNull<NODE> {
245
+ if (keyOrNodeOrEntryOrRawElement === undefined) return;
246
+ if (keyOrNodeOrEntryOrRawElement === null) return null;
247
+
248
+ if (this.isNode(keyOrNodeOrEntryOrRawElement)) return keyOrNodeOrEntryOrRawElement;
249
+
250
+ if (this.toEntryFn) {
251
+ const [key, entryValue] = this.toEntryFn(keyOrNodeOrEntryOrRawElement as R);
252
+ if (key) return this.createNode(key, entryValue ?? value);
253
+ else return;
245
254
  }
246
- return node;
255
+
256
+ if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
257
+ const [key, value] = keyOrNodeOrEntryOrRawElement;
258
+ if (key === undefined) return;
259
+ else if (key === null) return null;
260
+ else return this.createNode(key, value);
261
+ }
262
+
263
+ if (this.isKey(keyOrNodeOrEntryOrRawElement)) return this.createNode(keyOrNodeOrEntryOrRawElement, value);
264
+
265
+ return;
247
266
  }
248
267
 
249
268
  /**
@@ -255,81 +274,122 @@ export class BinaryTree<
255
274
  * Time Complexity: O(n)
256
275
  * Space Complexity: O(log n)
257
276
  *
258
- * The function `ensureNode` returns the node corresponding to the given key if it is a valid node
259
- * key, otherwise it returns the key itself.
260
- * @param {K | NODE | null | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `NODE`,
261
- * `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
262
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
263
- * type of iteration to be used when searching for a node by key. It has a default value of
264
- * `'ITERATIVE'`.
265
- * @returns either the node corresponding to the given key if it is a valid node key, or the key
266
- * itself if it is not a valid node key.
277
+ * The `ensureNode` function checks if the input is a valid node and returns it, or converts it to a
278
+ * node if it is a key or entry.
279
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
280
+ * `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, `BTNKeyOrNodeOrEntry<K, V, NODE>`, or
281
+ * a raw element.
282
+ * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
283
+ * parameter that specifies the type of iteration to be used when searching for a node. It has a
284
+ * default value of `'ITERATIVE'`.
285
+ * @returns The function `ensureNode` returns either a `NODE` object, `null`, or `undefined`.
267
286
  */
268
287
  ensureNode(
269
- keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>,
288
+ keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
270
289
  iterationType: IterationType = 'ITERATIVE'
271
- ): NODE | null | undefined {
272
- if (keyOrNodeOrEntry === this.NIL) return;
273
- if (this.isRealNode(keyOrNodeOrEntry)) {
274
- return keyOrNodeOrEntry;
290
+ ): OptBTNOrNull<NODE> {
291
+ if (keyOrNodeOrEntryOrRawElement === null) return null;
292
+ if (keyOrNodeOrEntryOrRawElement === undefined) return;
293
+ if (keyOrNodeOrEntryOrRawElement === this.NIL) return;
294
+ if (this.isNode(keyOrNodeOrEntryOrRawElement)) return keyOrNodeOrEntryOrRawElement;
295
+
296
+ if (this.toEntryFn) {
297
+ const [key] = this.toEntryFn(keyOrNodeOrEntryOrRawElement as R);
298
+ if (key) return this.getNodeByKey(key);
275
299
  }
276
- if (this.isEntry(keyOrNodeOrEntry)) {
277
- const key = keyOrNodeOrEntry[0];
300
+
301
+ if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
302
+ const key = keyOrNodeOrEntryOrRawElement[0];
278
303
  if (key === null) return null;
279
304
  if (key === undefined) return;
280
305
  return this.getNodeByKey(key, iterationType);
281
306
  }
282
- if (keyOrNodeOrEntry === null) return null;
283
- if (keyOrNodeOrEntry === undefined) return;
284
- return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
307
+
308
+ if (this.isKey(keyOrNodeOrEntryOrRawElement)) return this.getNodeByKey(keyOrNodeOrEntryOrRawElement, iterationType);
309
+ return;
285
310
  }
286
311
 
287
312
  /**
288
- * The function checks if a given node is a real node or null.
289
- * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
290
- * @returns a boolean value.
313
+ * The function checks if the input is an instance of the BinaryTreeNode class.
314
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
315
+ * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
316
+ * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
317
+ * an instance of the `BinaryTreeNode` class.
291
318
  */
292
- isNodeOrNull(node: KeyOrNodeOrEntry<K, V, NODE>): node is NODE | null {
293
- return this.isRealNode(node) || node === null;
319
+ isNode(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntryOrRawElement is NODE {
320
+ return keyOrNodeOrEntryOrRawElement instanceof BinaryTreeNode;
294
321
  }
295
322
 
296
323
  /**
297
- * The function "isNode" checks if an keyOrNodeOrEntry is an instance of the BinaryTreeNode class.
298
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V,NODE>`.
299
- * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the class NODE.
324
+ * The function checks if a given node is a valid node in a binary search tree.
325
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
326
+ * `BTNKeyOrNodeOrEntry<K, V, NODE>`.
327
+ * @returns a boolean value.
300
328
  */
301
- isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE {
302
- return keyOrNodeOrEntry instanceof BinaryTreeNode;
329
+ isRealNode(node: R | BTNKeyOrNodeOrEntry<K, V, NODE>): node is NODE {
330
+ if (node === this.NIL || node === null || node === undefined) return false;
331
+ return this.isNode(node);
303
332
  }
304
333
 
305
334
  /**
306
- * The function checks if a given node is a real node by verifying if it is an instance of
307
- * BinaryTreeNode and its key is not NaN.
308
- * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
335
+ * The function checks if a given node is a real node or null.
336
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
337
+ * `BTNKeyOrNodeOrEntry<K, V, NODE>`.
309
338
  * @returns a boolean value.
310
339
  */
311
- isRealNode(node: KeyOrNodeOrEntry<K, V, NODE>): node is NODE {
312
- if (node === this.NIL || node === null || node === undefined) return false;
313
- return this.isNode(node);
340
+ isNodeOrNull(node: R | BTNKeyOrNodeOrEntry<K, V, NODE>): node is NODE | null {
341
+ return this.isRealNode(node) || node === null;
314
342
  }
315
343
 
316
344
  /**
317
- * The function checks if a given node is a BinaryTreeNode instance and has a key value of NaN.
318
- * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
345
+ * The function checks if a given node is equal to the NIL value.
346
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
347
+ * `BTNKeyOrNodeOrEntry<K, V, NODE>`.
319
348
  * @returns a boolean value.
320
349
  */
321
- isNIL(node: KeyOrNodeOrEntry<K, V, NODE>) {
350
+ isNIL(node: R | BTNKeyOrNodeOrEntry<K, V, NODE>) {
322
351
  return node === this.NIL;
323
352
  }
324
353
 
325
354
  /**
326
- * The function checks if a given value is an entry in a binary tree node.
327
- * @param keyOrNodeOrEntry - KeyOrNodeOrEntry<K, V,NODE> - A generic type representing a node in a binary tree. It has
328
- * two type parameters V and NODE, representing the value and node type respectively.
355
+ * The function checks if the input is an array with two elements, indicating it is a binary tree
356
+ * node entry.
357
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
358
+ * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
329
359
  * @returns a boolean value.
330
360
  */
331
- isEntry(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is BTNEntry<K, V> {
332
- return Array.isArray(keyOrNodeOrEntry) && keyOrNodeOrEntry.length === 2;
361
+ isEntry(
362
+ keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>
363
+ ): keyOrNodeOrEntryOrRawElement is BTNEntry<K, V> {
364
+ return Array.isArray(keyOrNodeOrEntryOrRawElement) && keyOrNodeOrEntryOrRawElement.length === 2;
365
+ }
366
+
367
+ /**
368
+ * The function checks if a given value is a valid key by evaluating its type and value.
369
+ * @param {any} key - The `key` parameter can be of any type. It is the value that we want to check
370
+ * if it is a valid key.
371
+ * @param [isCheckValueOf=true] - The `isCheckValueOf` parameter is a boolean flag that determines
372
+ * whether the function should check the valueOf() method of an object when the key is of type
373
+ * 'object'. If `isCheckValueOf` is true, the function will recursively call itself with the value
374
+ * returned by key.valueOf().
375
+ * @returns a boolean value.
376
+ */
377
+ isKey(key: any, isCheckValueOf = true): key is K {
378
+ if (key === null) return true;
379
+ const keyType = typeof key;
380
+ if (keyType === 'string' || keyType === 'bigint' || keyType === 'boolean') return true;
381
+ if (keyType === 'number') return !isNaN(key);
382
+ if (keyType === 'symbol' || keyType === 'undefined') return false;
383
+ if (keyType === 'function') return this.isKey(key());
384
+ if (keyType === 'object') {
385
+ if (typeof key.toString === 'function') return true;
386
+ if (isCheckValueOf && typeof key.valueOf === 'function') {
387
+ this.isKey(key.valueOf(), false);
388
+ }
389
+ return false;
390
+ }
391
+
392
+ return false;
333
393
  }
334
394
 
335
395
  /**
@@ -341,14 +401,20 @@ export class BinaryTree<
341
401
  * Time Complexity O(n)
342
402
  * Space Complexity O(1)
343
403
  *
344
- * The `add` function adds a new node to a binary tree, either by creating a new node or replacing an
345
- * existing node with the same key.
346
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
347
- * @param {V} [value] - The value to be inserted into the binary tree.
348
- * @returns The function `add` returns either a node (`NODE`), `null`, or `undefined`.
349
- */
350
- add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
351
- const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
404
+ * The `add` function is used to insert a new node into a binary tree, checking for duplicate keys
405
+ * and finding the appropriate insertion position.
406
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
407
+ * `keyOrNodeOrEntryOrRawElement` parameter can accept a value of type `R`, which represents the key,
408
+ * node, entry, or raw element to be added to the tree. It can also accept a value of type
409
+ * `BTNKeyOrNodeOrEntry<K, V, NODE>
410
+ * @param {V} [value] - The `value` parameter is an optional value that can be associated with the
411
+ * key being added to the tree. It represents the value that will be stored in the tree for the given
412
+ * key.
413
+ * @returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
414
+ * insertion position cannot be found or if there are duplicate keys.
415
+ */
416
+ add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
417
+ const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
352
418
  if (newNode === undefined) return false;
353
419
 
354
420
  // If the tree is empty, directly set the new node as the root node
@@ -410,13 +476,20 @@ export class BinaryTree<
410
476
  * Time Complexity: O(k * n)
411
477
  * Space Complexity: O(1)
412
478
  *
413
- * The `addMany` function takes in a collection of keysOrNodesOrEntries and an optional collection of values, and
414
- * adds each node with its corresponding value to the data structure.
415
- * @param keysOrNodesOrEntries - An iterable collection of KeyOrNodeOrEntry objects.
416
- * @param [values] - An optional iterable of values that will be assigned to each node being added.
417
- * @returns The function `addMany` returns an array of `NODE`, `null`, or `undefined` values.
418
- */
419
- addMany(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>>, values?: Iterable<V | undefined>): boolean[] {
479
+ * The `addMany` function takes in an iterable of keys or nodes or entries or raw elements, and an
480
+ * optional iterable of values, and adds each key or node or entry with its corresponding value to a
481
+ * data structure, returning an array of booleans indicating whether each insertion was successful.
482
+ * @param keysOrNodesOrEntriesOrRawElements - An iterable containing keys, nodes, entries, or raw
483
+ * elements. These elements will be added to the data structure.
484
+ * @param [values] - An optional iterable of values that correspond to the keys or nodes or entries
485
+ * in the `keysOrNodesOrEntriesOrRawElements` parameter.
486
+ * @returns The function `addMany` returns an array of booleans indicating whether each element was
487
+ * successfully added to the data structure.
488
+ */
489
+ addMany(
490
+ keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>>,
491
+ values?: Iterable<V | undefined>
492
+ ): boolean[] {
420
493
  // TODO not sure addMany not be run multi times
421
494
  const inserted: boolean[] = [];
422
495
 
@@ -425,7 +498,7 @@ export class BinaryTree<
425
498
  valuesIterator = values[Symbol.iterator]();
426
499
  }
427
500
 
428
- for (const keyOrNodeOrEntry of keysOrNodesOrEntries) {
501
+ for (const keyOrNodeOrEntryOrRawElement of keysOrNodesOrEntriesOrRawElements) {
429
502
  let value: V | undefined | null = undefined;
430
503
 
431
504
  if (valuesIterator) {
@@ -435,7 +508,7 @@ export class BinaryTree<
435
508
  }
436
509
  }
437
510
 
438
- inserted.push(this.add(keyOrNodeOrEntry, value));
511
+ inserted.push(this.add(keyOrNodeOrEntryOrRawElement, value));
439
512
  }
440
513
 
441
514
  return inserted;
@@ -451,23 +524,25 @@ export class BinaryTree<
451
524
  * Time Complexity: O(k * n)
452
525
  * Space Complexity: O(1)
453
526
  *
454
- * The `refill` function clears the current data and adds new key-value pairs to the data structure.
455
- * @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries. These can be of type
456
- * KeyOrNodeOrEntry<K, V, NODE>.
457
- * @param [values] - The `values` parameter is an optional iterable that contains the values to be
458
- * associated with the keys or nodes or entries in the `keysOrNodesOrEntries` parameter. If provided,
459
- * the values will be associated with the corresponding keys or nodes or entries in the
460
- * `keysOrNodesOrEntries` iterable
461
- */
462
- refill(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>>, values?: Iterable<V | undefined>): void {
527
+ * The `refill` function clears the current data and adds new data to the collection.
528
+ * @param keysOrNodesOrEntriesOrRawElements - An iterable collection of keys, nodes, entries, or raw
529
+ * elements. These can be of any type (R) or a specific type (BTNKeyOrNodeOrEntry<K, V, NODE>).
530
+ * @param [values] - The `values` parameter is an optional iterable of values that will be associated
531
+ * with the keys or nodes being added. If provided, the values will be assigned to the corresponding
532
+ * keys or nodes. If not provided, the values will be set to `undefined`.
533
+ */
534
+ refill(
535
+ keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>>,
536
+ values?: Iterable<V | undefined>
537
+ ): void {
463
538
  this.clear();
464
- this.addMany(keysOrNodesOrEntries, values);
539
+ this.addMany(keysOrNodesOrEntriesOrRawElements, values);
465
540
  }
466
541
 
467
542
  delete<C extends BTNCallback<NODE, K>>(identifier: K, callback?: C): BinaryTreeDeleteResult<NODE>[];
468
543
 
469
544
  delete<C extends BTNCallback<NODE, NODE>>(
470
- identifier: NODE | null | undefined,
545
+ identifier: OptBTNOrNull<NODE>,
471
546
  callback?: C
472
547
  ): BinaryTreeDeleteResult<NODE>[];
473
548
 
@@ -476,21 +551,21 @@ export class BinaryTree<
476
551
  /**
477
552
  * Time Complexity: O(n)
478
553
  * Space Complexity: O(1)
479
- * /
554
+ */
480
555
 
481
- /**
556
+ /**
482
557
  * Time Complexity: O(n)
483
558
  * Space Complexity: O(1)
484
559
  *
485
- * The function deletes a node from a binary tree and returns an array of the deleted nodes along
486
- * with the nodes that need to be balanced.
487
- * @param {ReturnType<C> | null | undefined} identifier - The identifier parameter is the value or
488
- * object that you want to delete from the binary tree. It can be of any type that is compatible with
489
- * the callback function's return type. It can also be null or undefined if you want to delete a
490
- * specific node based on its value or object.
560
+ * The above function is a TypeScript implementation of deleting a node from a binary tree, returning
561
+ * the deleted node and the node that needs to be balanced.
562
+ * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
563
+ * used to identify the node that needs to be deleted from the binary tree. It can be of any type
564
+ * that is returned by the callback function.
491
565
  * @param {C} callback - The `callback` parameter is a function that is used to determine the
492
- * identifier of the node to be deleted. It is optional and has a default value of
493
- * `this._DEFAULT_CALLBACK`. The `callback` function should return the identifier of the node.
566
+ * identifier of the node to be deleted. It is of type `C`, which extends the `BTNCallback<NODE>`
567
+ * interface. The `BTNCallback<NODE>` interface represents a callback function that takes a node of
568
+ * type `NODE
494
569
  * @returns an array of `BinaryTreeDeleteResult<NODE>`.
495
570
  */
496
571
  delete<C extends BTNCallback<NODE>>(
@@ -545,15 +620,15 @@ export class BinaryTree<
545
620
  identifier: K,
546
621
  callback?: C,
547
622
  onlyOne?: boolean,
548
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
623
+ beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
549
624
  iterationType?: IterationType
550
625
  ): NODE[];
551
626
 
552
627
  getNodes<C extends BTNCallback<NODE, NODE>>(
553
- identifier: NODE | null | undefined,
628
+ identifier: OptBTNOrNull<NODE>,
554
629
  callback?: C,
555
630
  onlyOne?: boolean,
556
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
631
+ beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
557
632
  iterationType?: IterationType
558
633
  ): NODE[];
559
634
 
@@ -561,7 +636,7 @@ export class BinaryTree<
561
636
  identifier: ReturnType<C>,
562
637
  callback: C,
563
638
  onlyOne?: boolean,
564
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
639
+ beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
565
640
  iterationType?: IterationType
566
641
  ): NODE[];
567
642
 
@@ -572,34 +647,33 @@ export class BinaryTree<
572
647
 
573
648
  /**
574
649
  * Time Complexity: O(n)
575
- * Space Complexity: O(k + log n).
650
+ * Space Complexity: O(k + log n)
576
651
  *
577
- * The function `getNodes` retrieves nodes from a binary tree based on a given identifier and
578
- * callback function.
652
+ * The function `getNodes` returns an array of nodes that match a given identifier, using either a
653
+ * recursive or iterative approach.
579
654
  * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
580
- * that you want to search for in the binary tree. It can be of any type that is returned by the
581
- * callback function `C`. It can also be `null` or `undefined` if you don't want to search for a
582
- * specific value.
583
- * @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as
584
- * input and returns a value of type `C`. It is used to determine if a node matches the given
585
- * identifier. If no callback is provided, the `_DEFAULT_CALLBACK` function is used as the
586
- * default
587
- * @param [onlyOne=false] - A boolean value indicating whether to only return the first node that
588
- * matches the identifier. If set to true, the function will stop iterating once it finds a matching
589
- * node and return that node. If set to false (default), the function will continue iterating and
590
- * return all nodes that match the identifier.
591
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the
592
- * starting node for the traversal. It can be either a key, a node object, or `null`/`undefined`. If
593
- * it is `null` or `undefined`, an empty array will be returned.
594
- * @param iterationType - The `iterationType` parameter determines the type of iteration used to
595
- * traverse the binary tree. It can have two possible values:
596
- * @returns an array of nodes of type `NODE`.
655
+ * that is used to identify the nodes. It can be of any type and is used to match against the result
656
+ * of the callback function for each node.
657
+ * @param {C} callback - The `callback` parameter is a function that takes a node as input and
658
+ * returns a value. This value is used to identify the nodes that match the given identifier. The
659
+ * `callback` function is optional and defaults to a default callback function
660
+ * (`this._DEFAULT_CALLBACK`) if not provided.
661
+ * @param [onlyOne=false] - A boolean value indicating whether to return only one node that matches
662
+ * the identifier or all nodes that match the identifier. If set to true, only the first matching
663
+ * node will be returned. If set to false, all matching nodes will be returned. The default value is
664
+ * false.
665
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
666
+ * point for the search. It can be either a node object, a key-value pair, or a key. If it is not
667
+ * provided, the `root` of the data structure is used as the starting point.
668
+ * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
669
+ * iteration to be performed on the nodes of a binary tree. It can have two possible values:
670
+ * @returns an array of NODE objects.
597
671
  */
598
672
  getNodes<C extends BTNCallback<NODE>>(
599
673
  identifier: ReturnType<C> | null | undefined,
600
674
  callback: C = this._DEFAULT_CALLBACK as C,
601
675
  onlyOne = false,
602
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
676
+ beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
603
677
  iterationType: IterationType = this.iterationType
604
678
  ): NODE[] {
605
679
  beginRoot = this.ensureNode(beginRoot);
@@ -641,23 +715,23 @@ export class BinaryTree<
641
715
  getNode<C extends BTNCallback<NODE, K>>(
642
716
  identifier: K,
643
717
  callback?: C,
644
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
718
+ beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
645
719
  iterationType?: IterationType
646
- ): NODE | null | undefined;
720
+ ): OptBTNOrNull<NODE>;
647
721
 
648
722
  getNode<C extends BTNCallback<NODE, NODE>>(
649
- identifier: NODE | null | undefined,
723
+ identifier: OptBTNOrNull<NODE>,
650
724
  callback?: C,
651
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
725
+ beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
652
726
  iterationType?: IterationType
653
- ): NODE | null | undefined;
727
+ ): OptBTNOrNull<NODE>;
654
728
 
655
729
  getNode<C extends BTNCallback<NODE>>(
656
730
  identifier: ReturnType<C>,
657
731
  callback: C,
658
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
732
+ beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
659
733
  iterationType?: IterationType
660
- ): NODE | null | undefined;
734
+ ): OptBTNOrNull<NODE>;
661
735
 
662
736
  /**
663
737
  * Time Complexity: O(n)
@@ -666,31 +740,28 @@ export class BinaryTree<
666
740
 
667
741
  /**
668
742
  * Time Complexity: O(n)
669
- * Space Complexity: O(log n)
743
+ * Space Complexity: O(log n).
670
744
  *
671
- * The function `getNode` returns the first node that matches the given identifier and callback
672
- * function.
745
+ * The function `getNode` returns the first node that matches the given identifier and callback,
746
+ * starting from the specified root node and using the specified iteration type.
673
747
  * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
674
- * used to identify the node you want to retrieve. It can be of any type that is returned by the
675
- * callback function `C`. It can also be `null` or `undefined` if you don't have a specific
676
- * identifier.
677
- * @param {C} callback - The `callback` parameter is a function that will be called for each node in
678
- * the binary tree. It is used to determine if a node matches the given identifier. The `callback`
679
- * function should take a single parameter of type `NODE` (the type of the nodes in the binary tree) and
680
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
681
- * for searching the binary tree. It can be either a key value, a node object, or `null`/`undefined`.
682
- * If `null` or `undefined` is passed, the search will start from the root of the binary tree.
683
- * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
684
- * be performed when searching for nodes in the binary tree. It determines the order in which the
685
- * nodes are visited during the search.
686
- * @returns a value of type `NODE | null | undefined`.
748
+ * used to identify the node you want to retrieve. It can be of any type that is the return type of
749
+ * the `C` callback function, or it can be `null` or `undefined`.
750
+ * @param {C} callback - The `callback` parameter is a function that will be used to determine if a
751
+ * node matches the desired criteria. It should return a value that can be used to identify the node.
752
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
753
+ * point for searching nodes in a tree structure. It can be either a root node, a key-value pair, or
754
+ * a node entry. If not provided, the search will start from the root of the tree.
755
+ * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
756
+ * of iteration to be performed when searching for nodes. It can have one of the following values:
757
+ * @returns The method is returning a NODE object, or null, or undefined.
687
758
  */
688
759
  getNode<C extends BTNCallback<NODE>>(
689
760
  identifier: ReturnType<C> | null | undefined,
690
761
  callback: C = this._DEFAULT_CALLBACK as C,
691
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
762
+ beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
692
763
  iterationType: IterationType = this.iterationType
693
- ): NODE | null | undefined {
764
+ ): OptBTNOrNull<NODE> {
694
765
  return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
695
766
  }
696
767
 
@@ -703,38 +774,36 @@ export class BinaryTree<
703
774
  * Time Complexity: O(n)
704
775
  * Space Complexity: O(log n)
705
776
  *
706
- * The function `getNodeByKey` searches for a node in a binary tree by its key, using either
707
- * recursive or iterative iteration.
708
- * @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
709
- * It is used to find the node with the matching key value.
710
- * @param iterationType - The `iterationType` parameter is used to determine whether the search for
711
- * the node with the given key should be performed iteratively or recursively. It has two possible
712
- * values:
713
- * @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
714
- * found in the binary tree. If no node is found, it returns `undefined`.
715
- */
716
- getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): NODE | null | undefined {
777
+ * The function `getNodeByKey` returns a node with a specific key value from a tree structure.
778
+ * @param {K} key - The key parameter is the value that you want to search for in the tree. It is
779
+ * used to find the node with the matching key value.
780
+ * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
781
+ * parameter that specifies the type of iteration to be used when searching for a node in the tree.
782
+ * It has a default value of `'ITERATIVE'`.
783
+ * @returns a value of type NODE, null, or undefined.
784
+ */
785
+ getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): OptBTNOrNull<NODE> {
717
786
  return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
718
787
  }
719
788
 
720
789
  override get<C extends BTNCallback<NODE, K>>(
721
790
  identifier: K,
722
791
  callback?: C,
723
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
792
+ beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
724
793
  iterationType?: IterationType
725
794
  ): V | undefined;
726
795
 
727
796
  override get<C extends BTNCallback<NODE, NODE>>(
728
- identifier: NODE | null | undefined,
797
+ identifier: OptBTNOrNull<NODE>,
729
798
  callback?: C,
730
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
799
+ beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
731
800
  iterationType?: IterationType
732
801
  ): V | undefined;
733
802
 
734
803
  override get<C extends BTNCallback<NODE>>(
735
804
  identifier: ReturnType<C>,
736
805
  callback: C,
737
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
806
+ beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
738
807
  iterationType?: IterationType
739
808
  ): V | undefined;
740
809
 
@@ -747,28 +816,27 @@ export class BinaryTree<
747
816
  * Time Complexity: O(n)
748
817
  * Space Complexity: O(log n)
749
818
  *
750
- * The function `get` retrieves the value of a node in a binary tree based on the provided identifier
751
- * and callback function.
819
+ * The function `get` in TypeScript overrides the base class method and returns the value associated
820
+ * with the given identifier.
752
821
  * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
753
- * used to identify the node in the binary tree. It can be of any type that is the return type of the
822
+ * used to identify the node in the binary tree. It can be of any type that is returned by the
754
823
  * callback function `C`. It can also be `null` or `undefined` if no identifier is provided.
755
- * @param {C} callback - The `callback` parameter is a function that will be called with each node in
756
- * the binary tree. It is used to determine whether a node matches the given identifier. The callback
757
- * function should return a value that can be compared to the identifier to determine if it is a
758
- * match.
759
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
760
- * for the search in the binary tree. It can be specified as a `K` (a unique identifier for a
761
- * node), a node object of type `NODE`, or `null`/`undefined` to start the search from the root of
762
- * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
763
- * be performed when searching for a node in the binary tree. It is an optional parameter with a
764
- * default value specified by `this.iterationType`.
765
- * @returns The value of the node with the given identifier is being returned. If the node is not
766
- * found, `undefined` is returned.
824
+ * @param {C} callback - The `callback` parameter is a function that will be used to determine if a
825
+ * node matches the given identifier. It is optional and defaults to `this._DEFAULT_CALLBACK`.
826
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
827
+ * point for the search in the binary tree. It can be either a root node of the tree or a key, node,
828
+ * or entry object that exists in the tree. If no specific starting point is provided, the search
829
+ * will begin from the root of the
830
+ * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
831
+ * of iteration to be performed when searching for a node in the tree. It can have one of the
832
+ * following values:
833
+ * @returns The method is returning the value associated with the specified identifier in the binary
834
+ * tree.
767
835
  */
768
836
  override get<C extends BTNCallback<NODE>>(
769
837
  identifier: ReturnType<C> | null | undefined,
770
838
  callback: C = this._DEFAULT_CALLBACK as C,
771
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
839
+ beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
772
840
  iterationType: IterationType = this.iterationType
773
841
  ): V | undefined {
774
842
  return this.getNode(identifier, callback, beginRoot, iterationType)?.value;
@@ -777,54 +845,53 @@ export class BinaryTree<
777
845
  override has<C extends BTNCallback<NODE, K>>(
778
846
  identifier: K,
779
847
  callback?: C,
780
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
848
+ beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
781
849
  iterationType?: IterationType
782
850
  ): boolean;
783
851
 
784
852
  override has<C extends BTNCallback<NODE, NODE>>(
785
- identifier: NODE | null | undefined,
853
+ identifier: OptBTNOrNull<NODE>,
786
854
  callback?: C,
787
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
855
+ beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
788
856
  iterationType?: IterationType
789
857
  ): boolean;
790
858
 
791
859
  override has<C extends BTNCallback<NODE>>(
792
860
  identifier: ReturnType<C> | null | undefined,
793
861
  callback: C,
794
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
862
+ beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
795
863
  iterationType?: IterationType
796
864
  ): boolean;
797
865
 
798
866
  /**
799
867
  * Time Complexity: O(n)
800
- * Space Complexity: O(log n).
868
+ * Space Complexity: O(log n)
801
869
  */
802
870
 
803
871
  /**
804
872
  * Time Complexity: O(n)
805
- * Space Complexity: O(log n).
873
+ * Space Complexity: O(log n)
806
874
  *
807
- * The function checks if a Binary Tree Node with a specific identifier exists in the tree.
875
+ * The `has` function checks if a given identifier exists in the data structure and returns a boolean
876
+ * value.
808
877
  * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
809
- * that you want to search for in the binary tree. It can be of any type that is returned by the
810
- * callback function `C`. It can also be `null` or `undefined` if you don't want to specify a
811
- * specific identifier.
812
- * @param {C} callback - The `callback` parameter is a function that will be called for each node in
813
- * the binary tree. It is used to filter the nodes based on certain conditions. The `callback`
814
- * function should return a boolean value indicating whether the node should be included in the
815
- * result or not.
816
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
817
- * for the search in the binary tree. It can be specified as a `K` (a unique identifier for a
818
- * node in the binary tree), a node object (`NODE`), or `null`/`undefined` to start the search from
819
- * @param iterationType - The `iterationType` parameter is a variable that determines the type of
820
- * iteration to be performed on the binary tree. It is used to specify whether the iteration should
821
- * be performed in a pre-order, in-order, or post-order manner.
822
- * @returns a boolean value.
878
+ * used to identify a specific node or entry in the data structure. It can be of any type that is
879
+ * returned by the callback function `C`. It can also be `null` or `undefined` if no specific
880
+ * identifier is provided.
881
+ * @param {C} callback - The `callback` parameter is a function that will be used to determine
882
+ * whether a node should be included in the result or not. It is of type `C`, which extends the
883
+ * `BTNCallback<NODE>` type.
884
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
885
+ * point for the iteration in the data structure. It can be either a root node, a key-value pair, or
886
+ * a node entry. If not specified, it defaults to the root of the data structure.
887
+ * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
888
+ * of iteration to be performed. It is an optional parameter with a default value of `IterationType`.
889
+ * @returns The method is returning a boolean value.
823
890
  */
824
891
  override has<C extends BTNCallback<NODE>>(
825
892
  identifier: ReturnType<C> | null | undefined,
826
893
  callback: C = this._DEFAULT_CALLBACK as C,
827
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
894
+ beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
828
895
  iterationType: IterationType = this.iterationType
829
896
  ): boolean {
830
897
  callback = this._ensureCallback(identifier, callback);
@@ -875,12 +942,13 @@ export class BinaryTree<
875
942
  *
876
943
  * The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
877
944
  * height of the tree.
878
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
879
- * for calculating the height and minimum height of a binary tree. It can be either a `K` (a key
880
- * value of a binary tree node), `NODE` (a node of a binary tree), `null`, or `undefined`. If
945
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The parameter `beginRoot` is optional and
946
+ * has a default value of `this.root`. It represents the starting point for checking if the tree is
947
+ * perfectly balanced. It can be either a root node (`R`), a key or node or entry
948
+ * (`BTNKeyOrNodeOrEntry<K, V, NODE
881
949
  * @returns a boolean value.
882
950
  */
883
- isPerfectlyBalanced(beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root): boolean {
951
+ isPerfectlyBalanced(beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root): boolean {
884
952
  return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
885
953
  }
886
954
 
@@ -893,16 +961,18 @@ export class BinaryTree<
893
961
  * Time Complexity: O(n)
894
962
  * Space Complexity: O(1)
895
963
  *
896
- * The function `isSubtreeBST` checks if a given binary tree is a valid binary search tree.
897
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the root
898
- * node of the binary search tree (BST) that you want to check if it is a subtree of another BST.
899
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
900
- * type of iteration to use when checking if a subtree is a binary search tree (BST). It can have two
901
- * possible values:
964
+ * The function `isBST` checks if a binary search tree is valid, either recursively or iteratively.
965
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
966
+ * starting point for checking if a binary search tree (BST) is valid. It can be either a root node
967
+ * of the BST, a key value of a node in the BST, or an entry object containing both the key and value
968
+ * of a node in the BST
969
+ * @param {IterationType} iterationType - The `iterationType` parameter is used to determine the type
970
+ * of iteration to be performed while checking if the binary search tree (BST) is valid. It can have
971
+ * two possible values:
902
972
  * @returns a boolean value.
903
973
  */
904
974
  isBST(
905
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
975
+ beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
906
976
  iterationType: IterationType = this.iterationType
907
977
  ): boolean {
908
978
  // TODO there is a bug
@@ -910,7 +980,7 @@ export class BinaryTree<
910
980
  if (!beginRoot) return true;
911
981
 
912
982
  if (iterationType === 'RECURSIVE') {
913
- const dfs = (cur: NODE | null | undefined, min: number, max: number): boolean => {
983
+ const dfs = (cur: OptBTNOrNull<NODE>, min: number, max: number): boolean => {
914
984
  if (!this.isRealNode(cur)) return true;
915
985
  const numKey = Number(cur.key);
916
986
  if (numKey <= min || numKey >= max) return false;
@@ -925,7 +995,7 @@ export class BinaryTree<
925
995
  const stack = [];
926
996
  let prev = checkMax ? Number.MAX_SAFE_INTEGER : Number.MIN_SAFE_INTEGER;
927
997
  // @ts-ignore
928
- let curr: NODE | null | undefined = beginRoot;
998
+ let curr: OptBTNOrNull<NODE> = beginRoot;
929
999
  while (this.isRealNode(curr) || stack.length > 0) {
930
1000
  while (this.isRealNode(curr)) {
931
1001
  stack.push(curr);
@@ -954,16 +1024,20 @@ export class BinaryTree<
954
1024
  * Time Complexity: O(n)
955
1025
  * Space Complexity: O(1)
956
1026
  *
957
- * The function calculates the depth of a given node in a binary tree.
958
- * @param {K | NODE | null | undefined} dist - The `dist` parameter represents the node in
959
- * the binary tree whose depth we want to find. It can be of type `K`, `NODE`, `null`, or
960
- * `undefined`.
961
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
962
- * from which we want to calculate the depth. It can be either a `K` (binary tree node key) or
963
- * `NODE` (binary tree node) or `null` or `undefined`. If no value is provided for `beginRoot
964
- * @returns the depth of the `dist` relative to the `beginRoot`.
965
- */
966
- getDepth(dist: KeyOrNodeOrEntry<K, V, NODE>, beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root): number {
1027
+ * The function calculates the depth of a given node or key in a tree-like data structure.
1028
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} dist - The `dist` parameter can be either a `R`
1029
+ * (representing a root node), or a `BTNKeyOrNodeOrEntry<K, V, NODE>` (representing a key, node, or
1030
+ * entry).
1031
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is optional and
1032
+ * represents the starting point from which to calculate the depth. It can be either a reference to a
1033
+ * node in the tree or a key-value pair or an entry object. If not provided, the default value is
1034
+ * `this.root`, which refers to the root node
1035
+ * @returns the depth of a node in a tree structure.
1036
+ */
1037
+ getDepth(
1038
+ dist: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
1039
+ beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root
1040
+ ): number {
967
1041
  let distEnsured = this.ensureNode(dist);
968
1042
  const beginRootEnsured = this.ensureNode(beginRoot);
969
1043
  let depth = 0;
@@ -984,27 +1058,26 @@ export class BinaryTree<
984
1058
 
985
1059
  /**
986
1060
  * Time Complexity: O(n)
987
- * Space Complexity: O(log n)
1061
+ * Space Complexity: O(1)
988
1062
  *
989
- * The function `getHeight` calculates the maximum height of a binary tree using either recursive or
990
- * iterative traversal.
991
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the
992
- * starting node of the binary tree from which we want to calculate the height. It can be of type
993
- * `K`, `NODE`, `null`, or `undefined`. If not provided, it defaults to `this.root`.
994
- * @param iterationType - The `iterationType` parameter is used to determine whether to calculate the
995
- * height of the tree using a recursive approach or an iterative approach. It can have two possible
996
- * values:
997
- * @returns the height of the binary tree.
1063
+ * The `getHeight` function calculates the maximum height of a binary tree using either a recursive
1064
+ * or iterative approach.
1065
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
1066
+ * starting point for calculating the height of a tree. It can be either a root node (`R`), a key or
1067
+ * node or entry (`BTNKeyOrNodeOrEntry<K, V, NODE>`), or it defaults to the root of the current tree.
1068
+ * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
1069
+ * iteration used to calculate the height of the tree. It can have two possible values:
1070
+ * @returns the maximum height of the binary tree.
998
1071
  */
999
1072
  getHeight(
1000
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
1073
+ beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
1001
1074
  iterationType: IterationType = this.iterationType
1002
1075
  ): number {
1003
1076
  beginRoot = this.ensureNode(beginRoot);
1004
1077
  if (!this.isRealNode(beginRoot)) return -1;
1005
1078
 
1006
1079
  if (iterationType === 'RECURSIVE') {
1007
- const _getMaxHeight = (cur: NODE | null | undefined): number => {
1080
+ const _getMaxHeight = (cur: OptBTNOrNull<NODE>): number => {
1008
1081
  if (!this.isRealNode(cur)) return -1;
1009
1082
  const leftHeight = _getMaxHeight(cur.left);
1010
1083
  const rightHeight = _getMaxHeight(cur.right);
@@ -1040,22 +1113,25 @@ export class BinaryTree<
1040
1113
  *
1041
1114
  * The `getMinHeight` function calculates the minimum height of a binary tree using either a
1042
1115
  * recursive or iterative approach.
1043
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the
1044
- * starting node of the binary tree from which we want to calculate the minimum height. It can be of
1045
- * type `K`, `NODE`, `null`, or `undefined`. If no value is provided, it defaults to `this.root`.
1046
- * @param iterationType - The `iterationType` parameter is used to determine the method of iteration
1047
- * to calculate the minimum height of a binary tree. It can have two possible values:
1048
- * @returns The function `getMinHeight` returns the minimum height of a binary tree.
1116
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
1117
+ * starting point for calculating the minimum height of a tree. It can be either a root node (`R`), a
1118
+ * key or node or entry (`BTNKeyOrNodeOrEntry<K, V, NODE>`), or it defaults to the root of the current
1119
+ * tree.
1120
+ * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
1121
+ * iteration to be used when calculating the minimum height of the tree. It can have two possible
1122
+ * values:
1123
+ * @returns The function `getMinHeight` returns a number, which represents the minimum height of the
1124
+ * binary tree.
1049
1125
  */
1050
1126
  getMinHeight(
1051
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
1127
+ beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
1052
1128
  iterationType: IterationType = this.iterationType
1053
1129
  ): number {
1054
1130
  beginRoot = this.ensureNode(beginRoot);
1055
1131
  if (!beginRoot) return -1;
1056
1132
 
1057
1133
  if (iterationType === 'RECURSIVE') {
1058
- const _getMinHeight = (cur: NODE | null | undefined): number => {
1134
+ const _getMinHeight = (cur: OptBTNOrNull<NODE>): number => {
1059
1135
  if (!this.isRealNode(cur)) return 0;
1060
1136
  if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return 0;
1061
1137
  const leftMinHeight = _getMinHeight(cur.left);
@@ -1066,8 +1142,8 @@ export class BinaryTree<
1066
1142
  return _getMinHeight(beginRoot);
1067
1143
  } else {
1068
1144
  const stack: NODE[] = [];
1069
- let node: NODE | null | undefined = beginRoot,
1070
- last: NODE | null | undefined = null;
1145
+ let node: OptBTNOrNull<NODE> = beginRoot,
1146
+ last: OptBTNOrNull<NODE> = null;
1071
1147
  const depths: Map<NODE, number> = new Map();
1072
1148
 
1073
1149
  while (stack.length > 0 || node) {
@@ -1096,24 +1172,22 @@ export class BinaryTree<
1096
1172
  /**
1097
1173
  * Time Complexity: O(log n)
1098
1174
  * Space Complexity: O(log n)
1099
- * /
1175
+ */
1100
1176
 
1101
- /**
1177
+ /**
1102
1178
  * Time Complexity: O(log n)
1103
1179
  * Space Complexity: O(log n)
1104
1180
  *
1105
- * The function `getPathToRoot` returns an array of nodes from a given node to the root of a tree
1106
- * structure, with the option to reverse the order of the nodes.
1107
- * @param {K | NODE | null | undefined} beginNode - The `beginRoot` parameter represents the
1108
- * starting node from which you want to find the path to the root. It can be of type `K`, `NODE`,
1109
- * `null`, or `undefined`.
1181
+ * The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
1182
+ * up to the root node, with an option to reverse the order of the nodes.
1183
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginNode - The `beginNode` parameter can be either of
1184
+ * type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
1110
1185
  * @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
1111
1186
  * resulting path should be reversed or not. If `isReverse` is set to `true`, the path will be
1112
- * reversed before returning it. If `isReverse` is set to `false`, the path will be returned as is
1113
- * @returns The function `getPathToRoot` returns an array of nodes (`NODE[]`).
1187
+ * reversed before returning it. If `isReverse` is set to `false` or not provided, the path will
1188
+ * @returns The function `getPathToRoot` returns an array of `NODE` objects.
1114
1189
  */
1115
- getPathToRoot(beginNode: KeyOrNodeOrEntry<K, V, NODE>, isReverse = true): NODE[] {
1116
- // TODO to support get path through passing key
1190
+ getPathToRoot(beginNode: R | BTNKeyOrNodeOrEntry<K, V, NODE>, isReverse = true): NODE[] {
1117
1191
  const result: NODE[] = [];
1118
1192
  let beginNodeEnsured = this.ensureNode(beginNode);
1119
1193
 
@@ -1121,7 +1195,6 @@ export class BinaryTree<
1121
1195
 
1122
1196
  while (beginNodeEnsured.parent) {
1123
1197
  // Array.push + Array.reverse is more efficient than Array.unshift
1124
- // TODO may consider using Deque, so far this is not the performance bottleneck
1125
1198
  result.push(beginNodeEnsured);
1126
1199
  beginNodeEnsured = beginNodeEnsured.parent;
1127
1200
  }
@@ -1138,20 +1211,19 @@ export class BinaryTree<
1138
1211
  * Time Complexity: O(log n)
1139
1212
  * Space Complexity: O(1)
1140
1213
  *
1141
- * The function `getLeftMost` returns the leftmost node in a binary tree, either recursively or
1142
- * iteratively.
1143
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
1144
- * for finding the leftmost node in a binary tree. It can be either a `K` (a key value), `NODE` (a
1145
- * node), `null`, or `undefined`. If not provided, it defaults to `this.root`,
1146
- * @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
1147
- * be performed when finding the leftmost node in a binary tree. It can have two possible values:
1148
- * @returns The function `getLeftMost` returns the leftmost node (`NODE`) in the binary tree. If there
1149
- * is no leftmost node, it returns `null` or `undefined` depending on the input.
1214
+ * The `getLeftMost` function returns the leftmost node in a binary tree, either using recursive or
1215
+ * iterative traversal.
1216
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
1217
+ * starting point for finding the leftmost node in a binary tree. It can be either a root node (`R`),
1218
+ * a key or node or entry (`BTNKeyOrNodeOrEntry<K, V, NODE>`), or `null` or `undefined`.
1219
+ * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
1220
+ * of iteration to be performed. It can have two possible values:
1221
+ * @returns The function `getLeftMost` returns the leftmost node in a binary tree.
1150
1222
  */
1151
1223
  getLeftMost(
1152
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
1224
+ beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
1153
1225
  iterationType: IterationType = this.iterationType
1154
- ): NODE | null | undefined {
1226
+ ): OptBTNOrNull<NODE> {
1155
1227
  if (this.isNIL(beginRoot)) return beginRoot as NODE;
1156
1228
  beginRoot = this.ensureNode(beginRoot);
1157
1229
 
@@ -1184,21 +1256,20 @@ export class BinaryTree<
1184
1256
  * Time Complexity: O(log n)
1185
1257
  * Space Complexity: O(1)
1186
1258
  *
1187
- * The function `getRightMost` returns the rightmost node in a binary tree, either recursively or
1259
+ * The `getRightMost` function returns the rightmost node in a binary tree, either recursively or
1188
1260
  * iteratively.
1189
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the
1190
- * starting node from which we want to find the rightmost node. It can be of type `K`, `NODE`,
1191
- * `null`, or `undefined`. If not provided, it defaults to `this.root`, which is a property of the
1192
- * current object.
1193
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
1194
- * type of iteration to use when finding the rightmost node. It can have one of two values:
1195
- * @returns The function `getRightMost` returns the rightmost node (`NODE`) in a binary tree. If there
1196
- * is no rightmost node, it returns `null` or `undefined`, depending on the input.
1261
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
1262
+ * starting point for finding the rightmost node in a binary tree. It can be either a root node
1263
+ * (`R`), a key or node or entry (`BTNKeyOrNodeOrEntry<K, V, NODE>`), or `null` or `undefined`.
1264
+ * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
1265
+ * of iteration to be performed when finding the rightmost node in a binary tree. It can have two
1266
+ * possible values:
1267
+ * @returns The function `getRightMost` returns a NODE object, `null`, or `undefined`.
1197
1268
  */
1198
1269
  getRightMost(
1199
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
1270
+ beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
1200
1271
  iterationType: IterationType = this.iterationType
1201
- ): NODE | null | undefined {
1272
+ ): OptBTNOrNull<NODE> {
1202
1273
  if (this.isNIL(beginRoot)) return beginRoot as NODE;
1203
1274
  // TODO support get right most by passing key in
1204
1275
  beginRoot = this.ensureNode(beginRoot);
@@ -1231,14 +1302,14 @@ export class BinaryTree<
1231
1302
  * Time Complexity: O(log n)
1232
1303
  * Space Complexity: O(1)
1233
1304
  *
1234
- * The function returns the predecessor of a given node in a tree.
1235
- * @param {NODE} node - The parameter `node` is of type `RedBlackTreeNode`, which represents a node in a
1305
+ * The function returns the predecessor node of a given node in a binary tree.
1306
+ * @param {NODE} node - The parameter "node" is of type "NODE", which represents a node in a binary
1236
1307
  * tree.
1237
- * @returns the predecessor of the given 'node'.
1308
+ * @returns the predecessor node of the given node.
1238
1309
  */
1239
1310
  getPredecessor(node: NODE): NODE {
1240
1311
  if (this.isRealNode(node.left)) {
1241
- let predecessor: NODE | null | undefined = node.left;
1312
+ let predecessor: OptBTNOrNull<NODE> = node.left;
1242
1313
  while (!this.isRealNode(predecessor) || (this.isRealNode(predecessor.right) && predecessor.right !== node)) {
1243
1314
  if (this.isRealNode(predecessor)) {
1244
1315
  predecessor = predecessor.right;
@@ -1261,10 +1332,10 @@ export class BinaryTree<
1261
1332
  *
1262
1333
  * The function `getSuccessor` returns the next node in a binary tree given a current node.
1263
1334
  * @param {K | NODE | null} [x] - The parameter `x` can be of type `K`, `NODE`, or `null`.
1264
- * @returns the successor of the given node or key. The successor is the node that comes immediately
1265
- * after the given node in the inorder traversal of the binary tree.
1335
+ * @returns The function `getSuccessor` returns a `NODE` object if a successor exists, `null` if
1336
+ * there is no successor, and `undefined` if the input `x` is not a valid node.
1266
1337
  */
1267
- getSuccessor(x?: K | NODE | null): NODE | null | undefined {
1338
+ getSuccessor(x?: K | NODE | null): OptBTNOrNull<NODE> {
1268
1339
  x = this.ensureNode(x);
1269
1340
  if (!this.isRealNode(x)) return undefined;
1270
1341
 
@@ -1272,7 +1343,7 @@ export class BinaryTree<
1272
1343
  return this.getLeftMost(x.right);
1273
1344
  }
1274
1345
 
1275
- let y: NODE | null | undefined = x.parent;
1346
+ let y: OptBTNOrNull<NODE> = x.parent;
1276
1347
  while (this.isRealNode(y) && x === y.right) {
1277
1348
  x = y;
1278
1349
  y = y.parent;
@@ -1283,7 +1354,7 @@ export class BinaryTree<
1283
1354
  dfs<C extends BTNCallback<NODE>>(
1284
1355
  callback?: C,
1285
1356
  pattern?: DFSOrderPattern,
1286
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
1357
+ beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
1287
1358
  iterationType?: IterationType,
1288
1359
  includeNull?: false
1289
1360
  ): ReturnType<C>[];
@@ -1291,7 +1362,7 @@ export class BinaryTree<
1291
1362
  dfs<C extends BTNCallback<NODE | null>>(
1292
1363
  callback?: C,
1293
1364
  pattern?: DFSOrderPattern,
1294
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
1365
+ beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
1295
1366
  iterationType?: IterationType,
1296
1367
  includeNull?: true
1297
1368
  ): ReturnType<C>[];
@@ -1299,35 +1370,35 @@ export class BinaryTree<
1299
1370
  /**
1300
1371
  * Time complexity: O(n)
1301
1372
  * Space complexity: O(n)
1302
- * /
1373
+ */
1303
1374
 
1304
- /**
1375
+ /**
1305
1376
  * Time complexity: O(n)
1306
1377
  * Space complexity: O(n)
1307
1378
  *
1308
- * The `dfs` function performs a depth-first search traversal on a binary tree or graph, based on the
1309
- * specified pattern and iteration type, and returns an array of values obtained from applying a
1310
- * callback function to each visited node.
1311
- * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1312
- * the tree during the depth-first search. It takes a single parameter, which can be of type `NODE`,
1313
- * `null`, or `undefined`, and returns a value of any type. The default value for this parameter is
1314
- * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the
1315
- * nodes are traversed during the depth-first search. It can have one of the following values:
1316
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
1317
- * for the depth-first search traversal. It can be specified as a key, a node object, or
1318
- * `null`/`undefined`. If not provided, the `beginRoot` will default to the root node of the tree.
1319
- * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
1320
- * iteration to use when traversing the tree. It can have one of the following values:
1379
+ * The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback
1380
+ * function on each node according to a specified pattern and iteration type.
1381
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node
1382
+ * visited during the depth-first search. It takes a node as an argument and returns a value. The
1383
+ * return type of the callback function is determined by the generic type `C`.
1384
+ * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter determines the order in which the
1385
+ * nodes are visited during the depth-first search. It can have one of the following values:
1386
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
1387
+ * point of the depth-first search. It can be either a node object, a key-value pair, or a key. If it
1388
+ * is a key or key-value pair, the method will find the corresponding node in the tree and start the
1389
+ * search from there.
1390
+ * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter determines the
1391
+ * type of iteration to use during the depth-first search. It can have two possible values:
1321
1392
  * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
1322
- * whether null or undefined nodes should be included in the traversal. If `includeNull` is set to
1323
- * `true`, null or undefined nodes will be included in the traversal. If `includeNull` is set to
1324
- * `false`, null or undefined
1325
- * @returns an array of values that are the return values of the callback function.
1393
+ * whether or not to include null values in the depth-first search traversal. If `includeNull` is set
1394
+ * to `true`, null values will be included in the traversal. If `includeNull` is set to `false`, null
1395
+ * values will
1396
+ * @returns an array of the return types of the callback function.
1326
1397
  */
1327
- dfs<C extends BTNCallback<NODE | null | undefined>>(
1398
+ dfs<C extends BTNCallback<OptBTNOrNull<NODE>>>(
1328
1399
  callback: C = this._DEFAULT_CALLBACK as C,
1329
1400
  pattern: DFSOrderPattern = 'IN',
1330
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
1401
+ beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
1331
1402
  iterationType: IterationType = 'ITERATIVE',
1332
1403
  includeNull = false
1333
1404
  ): ReturnType<C>[] {
@@ -1335,7 +1406,7 @@ export class BinaryTree<
1335
1406
  if (!beginRoot) return [];
1336
1407
  const ans: ReturnType<C>[] = [];
1337
1408
  if (iterationType === 'RECURSIVE') {
1338
- const dfs = (node: NODE | null | undefined) => {
1409
+ const dfs = (node: OptBTNOrNull<NODE>) => {
1339
1410
  switch (pattern) {
1340
1411
  case 'IN':
1341
1412
  if (includeNull) {
@@ -1377,7 +1448,7 @@ export class BinaryTree<
1377
1448
  dfs(beginRoot);
1378
1449
  } else {
1379
1450
  // 0: visit, 1: print
1380
- const stack: { opt: 0 | 1; node: NODE | null | undefined }[] = [{ opt: 0, node: beginRoot }];
1451
+ const stack: { opt: 0 | 1; node: OptBTNOrNull<NODE> }[] = [{ opt: 0, node: beginRoot }];
1381
1452
 
1382
1453
  while (stack.length > 0) {
1383
1454
  const cur = stack.pop();
@@ -1421,14 +1492,14 @@ export class BinaryTree<
1421
1492
 
1422
1493
  bfs<C extends BTNCallback<NODE>>(
1423
1494
  callback?: C,
1424
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
1495
+ beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
1425
1496
  iterationType?: IterationType,
1426
1497
  includeNull?: false
1427
1498
  ): ReturnType<C>[];
1428
1499
 
1429
1500
  bfs<C extends BTNCallback<NODE | null>>(
1430
1501
  callback?: C,
1431
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
1502
+ beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
1432
1503
  iterationType?: IterationType,
1433
1504
  includeNull?: true
1434
1505
  ): ReturnType<C>[];
@@ -1442,26 +1513,27 @@ export class BinaryTree<
1442
1513
  * Time complexity: O(n)
1443
1514
  * Space complexity: O(n)
1444
1515
  *
1445
- * The `bfs` function performs a breadth-first search traversal on a binary tree, executing a
1446
- * callback function on each node.
1516
+ * The `bfs` function performs a breadth-first search on a binary tree, calling a callback function
1517
+ * on each node and returning an array of the results.
1447
1518
  * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1448
- * the breadth-first search traversal. It takes a single parameter, which is the current node being
1519
+ * the breadth-first search traversal. It takes a single argument, which is the current node being
1449
1520
  * visited, and returns a value of any type.
1450
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the
1451
- * starting node for the breadth-first search traversal. It can be specified as a key, a node object,
1452
- * or `null`/`undefined` to indicate the root of the tree. If not provided, the `root` property of
1453
- * the class is used as
1454
- * @param iterationType - The `iterationType` parameter determines the type of iteration to be
1455
- * performed during the breadth-first search (BFS). It can have two possible values:
1456
- * @param [includeNull=false] - The `includeNull` parameter is a boolean flag that determines whether
1457
- * to include null values in the breadth-first search traversal. If `includeNull` is set to
1458
- * `true`, null values will be included in the traversal, otherwise they will be skipped.
1459
- * @returns an array of values that are the result of invoking the callback function on each node in
1460
- * the breadth-first traversal of a binary tree.
1521
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
1522
+ * starting point of the breadth-first search. It can be either a root node of a tree or a key, node,
1523
+ * or entry object. If no value is provided, the `root` property of the class is used as the default
1524
+ * starting point.
1525
+ * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
1526
+ * iteration to be performed. It can have two possible values:
1527
+ * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
1528
+ * whether or not to include null values in the breadth-first search (BFS) traversal. If
1529
+ * `includeNull` is set to `true`, null values will be included in the traversal. If `includeNull` is
1530
+ * set to `false
1531
+ * @returns The function `bfs` returns an array of values that are the result of invoking the
1532
+ * `callback` function on each node in the breadth-first order traversal of the binary tree.
1461
1533
  */
1462
1534
  bfs<C extends BTNCallback<NODE | null>>(
1463
1535
  callback: C = this._DEFAULT_CALLBACK as C,
1464
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
1536
+ beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
1465
1537
  iterationType: IterationType = this.iterationType,
1466
1538
  includeNull = false
1467
1539
  ): ReturnType<C>[] {
@@ -1471,7 +1543,7 @@ export class BinaryTree<
1471
1543
  const ans: ReturnType<BTNCallback<NODE>>[] = [];
1472
1544
 
1473
1545
  if (iterationType === 'RECURSIVE') {
1474
- const queue: Queue<NODE | null | undefined> = new Queue<NODE | null | undefined>([beginRoot]);
1546
+ const queue: Queue<OptBTNOrNull<NODE>> = new Queue<OptBTNOrNull<NODE>>([beginRoot]);
1475
1547
 
1476
1548
  const dfs = (level: number) => {
1477
1549
  if (queue.size === 0) return;
@@ -1492,7 +1564,7 @@ export class BinaryTree<
1492
1564
 
1493
1565
  dfs(0);
1494
1566
  } else {
1495
- const queue = new Queue<NODE | null | undefined>([beginRoot]);
1567
+ const queue = new Queue<OptBTNOrNull<NODE>>([beginRoot]);
1496
1568
  while (queue.size > 0) {
1497
1569
  const levelSize = queue.size;
1498
1570
 
@@ -1515,14 +1587,14 @@ export class BinaryTree<
1515
1587
 
1516
1588
  listLevels<C extends BTNCallback<NODE>>(
1517
1589
  callback?: C,
1518
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
1590
+ beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
1519
1591
  iterationType?: IterationType,
1520
1592
  includeNull?: false
1521
1593
  ): ReturnType<C>[][];
1522
1594
 
1523
1595
  listLevels<C extends BTNCallback<NODE | null>>(
1524
1596
  callback?: C,
1525
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
1597
+ beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
1526
1598
  iterationType?: IterationType,
1527
1599
  includeNull?: true
1528
1600
  ): ReturnType<C>[][];
@@ -1537,25 +1609,25 @@ export class BinaryTree<
1537
1609
  * Space complexity: O(n)
1538
1610
  *
1539
1611
  * The `listLevels` function returns an array of arrays, where each inner array represents a level in
1540
- * a binary tree and contains the values returned by a callback function applied to the nodes at that
1541
- * level.
1612
+ * a binary tree and contains the results of applying a callback function to the nodes at that level.
1542
1613
  * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1543
- * the tree. It takes a single parameter, which can be of type `NODE`, `null`, or `undefined`, and
1544
- * returns a value of any type.
1545
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the
1546
- * starting node for traversing the tree. It can be either a node object (`NODE`), a key value
1547
- * (`K`), `null`, or `undefined`. If not provided, it defaults to the root node of the tree.
1548
- * @param iterationType - The `iterationType` parameter determines the type of iteration to be
1549
- * performed on the tree. It can have two possible values:
1614
+ * the tree. It takes a node as an argument and returns a value. The return type of the callback
1615
+ * function is determined by the generic type `C` which extends `BTNCallback<NODE | null>`.
1616
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
1617
+ * starting point for traversing the tree. It can be either a root node, a key-value pair, or a node
1618
+ * entry. If no value is provided, the `root` property of the class is used as the default starting
1619
+ * point.
1620
+ * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
1621
+ * iteration to be performed on the binary tree. It can have two possible values:
1550
1622
  * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
1551
- * whether to include null values in the resulting levels. If `includeNull` is set to `true`,
1623
+ * whether or not to include null values in the resulting levels. If `includeNull` is set to `true`,
1552
1624
  * null values will be included in the levels. If `includeNull` is set to `false`, null values will
1553
1625
  * be excluded
1554
1626
  * @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
1555
1627
  */
1556
1628
  listLevels<C extends BTNCallback<NODE | null>>(
1557
1629
  callback: C = this._DEFAULT_CALLBACK as C,
1558
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
1630
+ beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
1559
1631
  iterationType: IterationType = this.iterationType,
1560
1632
  includeNull = false
1561
1633
  ): ReturnType<C>[][] {
@@ -1612,31 +1684,31 @@ export class BinaryTree<
1612
1684
  * The `morris` function performs a depth-first traversal on a binary tree using the Morris traversal
1613
1685
  * algorithm.
1614
1686
  * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1615
- * the tree. It takes a single parameter of type `NODE` (the type of the nodes in the tree) and returns
1616
- * a value of any type.
1617
- * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
1618
- * determines the order in which the nodes of a binary tree are traversed. It can have one of the
1687
+ * the tree. It takes a single argument, which is the current node, and can return any value. The
1688
+ * return type of the `callback` function is determined by the `ReturnType<C>` type, which represents
1689
+ * the return
1690
+ * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function is used
1691
+ * to specify the order in which the nodes of a binary tree are traversed. It can take one of the
1619
1692
  * following values:
1620
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
1621
- * for the traversal. It can be specified as a key, a node object, or `null`/`undefined` to indicate
1622
- * the root of the tree. If no value is provided, the default value is the root of the tree.
1623
- * @returns The function `morris` returns an array of values that are the result of invoking the
1624
- * `callback` function on each node in the binary tree. The type of the array nodes is determined
1625
- * by the return type of the `callback` function.
1693
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
1694
+ * point for the traversal. It can be either a node object, a key, or an entry object. If no value is
1695
+ * provided, the `root` of the tree is used as the starting point.
1696
+ * @returns The function `morris` returns an array of values that are the return values of the
1697
+ * callback function `callback`.
1626
1698
  */
1627
1699
  morris<C extends BTNCallback<NODE>>(
1628
1700
  callback: C = this._DEFAULT_CALLBACK as C,
1629
1701
  pattern: DFSOrderPattern = 'IN',
1630
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root
1702
+ beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root
1631
1703
  ): ReturnType<C>[] {
1632
1704
  beginRoot = this.ensureNode(beginRoot);
1633
1705
  if (beginRoot === null) return [];
1634
1706
  const ans: ReturnType<BTNCallback<NODE>>[] = [];
1635
1707
 
1636
- let cur: NODE | null | undefined = beginRoot;
1637
- const _reverseEdge = (node: NODE | null | undefined) => {
1638
- let pre: NODE | null | undefined = null;
1639
- let next: NODE | null | undefined = null;
1708
+ let cur: OptBTNOrNull<NODE> = beginRoot;
1709
+ const _reverseEdge = (node: OptBTNOrNull<NODE>) => {
1710
+ let pre: OptBTNOrNull<NODE> = null;
1711
+ let next: OptBTNOrNull<NODE> = null;
1640
1712
  while (node) {
1641
1713
  next = node.right;
1642
1714
  node.right = pre;
@@ -1645,9 +1717,9 @@ export class BinaryTree<
1645
1717
  }
1646
1718
  return pre;
1647
1719
  };
1648
- const _printEdge = (node: NODE | null | undefined) => {
1649
- const tail: NODE | null | undefined = _reverseEdge(node);
1650
- let cur: NODE | null | undefined = tail;
1720
+ const _printEdge = (node: OptBTNOrNull<NODE>) => {
1721
+ const tail: OptBTNOrNull<NODE> = _reverseEdge(node);
1722
+ let cur: OptBTNOrNull<NODE> = tail;
1651
1723
  while (cur) {
1652
1724
  ans.push(callback(cur));
1653
1725
  cur = cur.right;
@@ -1719,8 +1791,7 @@ export class BinaryTree<
1719
1791
  * Time complexity: O(n)
1720
1792
  * Space complexity: O(n)
1721
1793
  *
1722
- * The `clone` function creates a new tree object and copies all the nodes from the original tree to
1723
- * the new tree.
1794
+ * The `clone` function creates a deep copy of a tree object.
1724
1795
  * @returns The `clone()` method is returning a cloned instance of the `TREE` object.
1725
1796
  */
1726
1797
  clone(): TREE {
@@ -1746,16 +1817,16 @@ export class BinaryTree<
1746
1817
  * Time Complexity: O(n)
1747
1818
  * Space Complexity: O(n)
1748
1819
  *
1749
- * The `filter` function creates a new tree by iterating over the nodes of the current tree and
1750
- * adding only the nodes that satisfy the given predicate function.
1751
- * @param predicate - The `predicate` parameter is a function that takes three arguments: `value`,
1752
- * `key`, and `index`. It should return a boolean value indicating whether the pair should be
1753
- * included in the filtered tree or not.
1754
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
1755
- * to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
1756
- * it will be passed as the first argument to the `predicate` function. If `thisArg` is
1757
- * @returns The `filter` method is returning a new tree object that contains the key-value pairs that
1758
- * pass the given predicate function.
1820
+ * The `filter` function creates a new tree with entries that pass a given predicate function.
1821
+ * @param predicate - The `predicate` parameter is a callback function that is used to test each
1822
+ * element in the tree. It takes three arguments: `value`, `key`, and `index`. The `value` argument
1823
+ * represents the value of the current element being processed, the `key` argument represents the key
1824
+ * of the
1825
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
1826
+ * specify the value of `this` within the `predicate` function. When the `predicate` function is
1827
+ * called, `thisArg` will be used as the value of `this` within the function. If `thisArg`
1828
+ * @returns The `filter` method is returning a new tree object that contains the entries that pass
1829
+ * the given predicate function.
1759
1830
  */
1760
1831
  filter(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: any) {
1761
1832
  const newTree = this.createTree();
@@ -1777,15 +1848,15 @@ export class BinaryTree<
1777
1848
  * Time Complexity: O(n)
1778
1849
  * Space Complexity: O(n)
1779
1850
  *
1780
- * The `map` function creates a new tree by applying a callback function to each key-value pair in
1781
- * the original tree.
1782
- * @param callback - The callback parameter is a function that will be called for each key-value pair
1783
- * in the tree. It takes four arguments: the value of the current pair, the key of the current pair,
1784
- * the index of the current pair, and a reference to the tree itself. The callback function should
1785
- * return a new
1786
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
1787
- * specify the value of `this` within the callback function. If you pass a value for `thisArg`, it
1788
- * will be used as the `this` value when the callback function is called. If you don't pass a value
1851
+ * The `map` function creates a new tree by applying a callback function to each entry in the current
1852
+ * tree.
1853
+ * @param callback - The callback parameter is a function that will be called for each entry in the
1854
+ * tree. It takes three arguments: value, key, and index. The value argument represents the value of
1855
+ * the current entry, the key argument represents the key of the current entry, and the index
1856
+ * argument represents the index of the
1857
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
1858
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
1859
+ * passed as the `this` value to the `callback` function. If `thisArg` is
1789
1860
  * @returns The `map` method is returning a new tree object.
1790
1861
  */
1791
1862
  map(callback: EntryCallback<K, V | undefined, V>, thisArg?: any) {
@@ -1816,13 +1887,17 @@ export class BinaryTree<
1816
1887
  * Time Complexity: O(n)
1817
1888
  * Space Complexity: O(n)
1818
1889
  *
1819
- * The `print` function is used to display a binary tree structure in a visually appealing way.
1820
- * @param {K | NODE | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `K | NODE | null |
1821
- * undefined`. It represents the root node of a binary tree. The root node can have one of the
1822
- * following types:
1823
- * @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.
1824
- */
1825
- override print(beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root, options?: BinaryTreePrintOptions): void {
1890
+ * The `print` function in TypeScript prints the binary tree structure with customizable options.
1891
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
1892
+ * point for printing the binary tree. It can be either a node of the binary tree or a key or entry
1893
+ * that exists in the binary tree. If no value is provided, the root of the binary tree will be used
1894
+ * as the starting point.
1895
+ * @param {BinaryTreePrintOptions} [options] - The `options` parameter is an optional object that
1896
+ * allows you to customize the printing behavior. It has the following properties:
1897
+ * @returns Nothing is being returned. The function has a return type of `void`, which means it does
1898
+ * not return any value.
1899
+ */
1900
+ override print(beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root, options?: BinaryTreePrintOptions): void {
1826
1901
  const opts = { isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false, ...options };
1827
1902
  beginRoot = this.ensureNode(beginRoot);
1828
1903
  if (!beginRoot) return;
@@ -1837,7 +1912,7 @@ export class BinaryTree<
1837
1912
  console.log(`S for Sentinel Node(NIL)
1838
1913
  `);
1839
1914
 
1840
- const display = (root: NODE | null | undefined): void => {
1915
+ const display = (root: OptBTNOrNull<NODE>): void => {
1841
1916
  const [lines, , ,] = this._displayAux(root, opts);
1842
1917
  for (const line of lines) {
1843
1918
  console.log(line);
@@ -1848,20 +1923,26 @@ export class BinaryTree<
1848
1923
  }
1849
1924
 
1850
1925
  /**
1851
- * The function `_getIterator` is a protected generator function that returns an iterator for the
1852
- * key-value pairs in a binary search tree.
1853
- * @param node - The `node` parameter represents the current node in the binary search tree. It is an
1854
- * optional parameter with a default value of `this.root`, which means if no node is provided, the
1855
- * root node of the tree will be used as the starting point for iteration.
1856
- * @returns The function `_getIterator` returns an `IterableIterator` of key-value pairs `[K, V |
1857
- * undefined]`.
1926
+ * Time Complexity: O(1)
1927
+ * Space Complexity: O(1)
1928
+ */
1929
+
1930
+ /**
1931
+ * Time Complexity: O(1)
1932
+ * Space Complexity: O(1)
1933
+ *
1934
+ * The function `_getIterator` is a generator function that returns an iterator for the key-value
1935
+ * pairs in a binary search tree.
1936
+ * @param node - The `node` parameter represents the current node in the binary search tree. It is
1937
+ * initially set to the root node of the tree.
1938
+ * @returns an IterableIterator<[K, V | undefined]>.
1858
1939
  */
1859
1940
  protected* _getIterator(node = this.root): IterableIterator<[K, V | undefined]> {
1860
1941
  if (!node) return;
1861
1942
 
1862
1943
  if (this.iterationType === 'ITERATIVE') {
1863
- const stack: (NODE | null | undefined)[] = [];
1864
- let current: NODE | null | undefined = node;
1944
+ const stack: OptBTNOrNull<NODE>[] = [];
1945
+ let current: OptBTNOrNull<NODE> = node;
1865
1946
 
1866
1947
  while (current || stack.length > 0) {
1867
1948
  while (this.isRealNode(current)) {
@@ -1888,9 +1969,17 @@ export class BinaryTree<
1888
1969
  }
1889
1970
 
1890
1971
  /**
1972
+ * Time Complexity: O(n)
1973
+ * Space Complexity: O(n)
1974
+ */
1975
+
1976
+ /**
1977
+ * Time Complexity: O(n)
1978
+ * Space Complexity: O(n)
1979
+ *
1891
1980
  * The `_displayAux` function is responsible for generating the display layout of a binary tree node,
1892
1981
  * taking into account various options such as whether to show null, undefined, or NaN nodes.
1893
- * @param {NODE | null | undefined} node - The `node` parameter represents a node in a binary tree.
1982
+ * @param {OptBTNOrNull<NODE>} node - The `node` parameter represents a node in a binary tree.
1894
1983
  * It can be of type `NODE`, `null`, or `undefined`.
1895
1984
  * @param {BinaryTreePrintOptions} options - The `options` parameter is an object that contains the
1896
1985
  * following properties:
@@ -1901,7 +1990,7 @@ export class BinaryTree<
1901
1990
  * 3. `totalHeight`: The total height of the node display.
1902
1991
  * 4. `middleIndex`: The index of the middle character
1903
1992
  */
1904
- protected _displayAux(node: NODE | null | undefined, options: BinaryTreePrintOptions): NodeDisplayLayout {
1993
+ protected _displayAux(node: OptBTNOrNull<NODE>, options: BinaryTreePrintOptions): NodeDisplayLayout {
1905
1994
  const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
1906
1995
  const emptyDisplayLayout = <NodeDisplayLayout>[['─'], 1, 0, 0];
1907
1996
 
@@ -1916,7 +2005,7 @@ export class BinaryTree<
1916
2005
  // Display logic of normal nodes
1917
2006
 
1918
2007
  const key = node.key,
1919
- line = this.isNIL(node) ? 'S' : key.toString(),
2008
+ line = this.isNIL(node) ? 'S' : String(key),
1920
2009
  width = line.length;
1921
2010
 
1922
2011
  return _buildNodeDisplay(
@@ -1927,7 +2016,7 @@ export class BinaryTree<
1927
2016
  );
1928
2017
  } else {
1929
2018
  // For cases where none of the conditions are met, null, undefined, and NaN nodes are not displayed
1930
- const line = node === undefined ? 'U' : 'NODE',
2019
+ const line = node === undefined ? 'U' : 'N',
1931
2020
  width = line.length;
1932
2021
 
1933
2022
  return _buildNodeDisplay(line, width, [[''], 1, 0, 0], [[''], 1, 0, 0]);
@@ -1969,17 +2058,29 @@ export class BinaryTree<
1969
2058
  }
1970
2059
  }
1971
2060
 
1972
- protected _DEFAULT_CALLBACK = (node: NODE | null | undefined) => (node ? node.key : undefined);
2061
+ protected _DEFAULT_CALLBACK = (node: OptBTNOrNull<NODE>) => (node ? node.key : undefined);
1973
2062
 
1974
2063
  /**
1975
- * Swap the data of two nodes in the binary tree.
1976
- * @param {NODE} srcNode - The source node to swap.
1977
- * @param {NODE} destNode - The destination node to swap.
1978
- * @returns {NODE} - The destination node after the swap.
2064
+ * Time Complexity: O(1)
2065
+ * Space Complexity: O(1)
2066
+ */
2067
+
2068
+ /**
2069
+ * Time Complexity: O(1)
2070
+ * Space Complexity: O(1)
2071
+ *
2072
+ * The function `_swapProperties` swaps the key-value properties between two nodes.
2073
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} srcNode - The source node that will be swapped with the
2074
+ * destination node. It can be either an instance of the class `R`, or an object of type
2075
+ * `BTNKeyOrNodeOrEntry<K, V, NODE>`.
2076
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} destNode - The `destNode` parameter is the node where
2077
+ * the properties will be swapped with the `srcNode`.
2078
+ * @returns either the `destNode` object with its properties swapped with the `srcNode` object's
2079
+ * properties, or `undefined` if either `srcNode` or `destNode` is falsy.
1979
2080
  */
1980
2081
  protected _swapProperties(
1981
- srcNode: KeyOrNodeOrEntry<K, V, NODE>,
1982
- destNode: KeyOrNodeOrEntry<K, V, NODE>
2082
+ srcNode: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
2083
+ destNode: R | BTNKeyOrNodeOrEntry<K, V, NODE>
1983
2084
  ): NODE | undefined {
1984
2085
  srcNode = this.ensureNode(srcNode);
1985
2086
  destNode = this.ensureNode(destNode);
@@ -2002,12 +2103,21 @@ export class BinaryTree<
2002
2103
  }
2003
2104
 
2004
2105
  /**
2005
- * The function replaces an old node with a new node in a binary tree.
2106
+ * Time Complexity: O(1)
2107
+ * Space Complexity: O(1)
2108
+ */
2109
+
2110
+ /**
2111
+ * Time Complexity: O(1)
2112
+ * Space Complexity: O(1)
2113
+ *
2114
+ * The function replaces a node in a binary tree with a new node, updating the parent, left child,
2115
+ * right child, and root if necessary.
2006
2116
  * @param {NODE} oldNode - The oldNode parameter represents the node that needs to be replaced in the
2007
2117
  * tree.
2008
2118
  * @param {NODE} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
2009
2119
  * tree.
2010
- * @returns The method is returning the newNode.
2120
+ * @returns the newNode.
2011
2121
  */
2012
2122
  protected _replaceNode(oldNode: NODE, newNode: NODE): NODE {
2013
2123
  if (oldNode.parent) {
@@ -2028,18 +2138,44 @@ export class BinaryTree<
2028
2138
  }
2029
2139
 
2030
2140
  /**
2031
- * The function sets the root property of an object to a given value, and if the value is not null,
2032
- * it also sets the parent property of the value to undefined.
2033
- * @param {NODE | null | undefined} v - The parameter `v` is of type `NODE | null | undefined`, which means it can either be of
2034
- * type `NODE` or `null`.
2141
+ * Time Complexity: O(1)
2142
+ * Space Complexity: O(1)
2035
2143
  */
2036
- protected _setRoot(v: NODE | null | undefined) {
2144
+
2145
+ /**
2146
+ * Time Complexity: O(1)
2147
+ * Space Complexity: O(1)
2148
+ *
2149
+ * The function sets the root property of an object to the provided value, and also updates the
2150
+ * parent property of the new root.
2151
+ * @param {OptBTNOrNull<NODE>} v - The parameter `v` is of type `OptBTNOrNull<NODE>`. This
2152
+ * means that it can accept a value of type `NODE`, `null`, or `undefined`.
2153
+ */
2154
+ protected _setRoot(v: OptBTNOrNull<NODE>) {
2037
2155
  if (v) {
2038
2156
  v.parent = undefined;
2039
2157
  }
2040
2158
  this._root = v;
2041
2159
  }
2042
2160
 
2161
+ /**
2162
+ * Time Complexity: O(1)
2163
+ * Space Complexity: O(1)
2164
+ */
2165
+
2166
+ /**
2167
+ * Time Complexity: O(1)
2168
+ * Space Complexity: O(1)
2169
+ *
2170
+ * The function `_ensureCallback` ensures that a callback function is provided and returns it.
2171
+ * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is of type
2172
+ * `ReturnType<C> | null | undefined`. This means it can accept a value that is the return type of
2173
+ * the generic type `C`, or it can be `null` or `undefined`.
2174
+ * @param {C} callback - The `callback` parameter is a function that takes a `node` as an argument
2175
+ * and returns a value. It is of type `C`, which is a generic type that extends the
2176
+ * `BTNCallback<NODE>` type.
2177
+ * @returns the callback parameter.
2178
+ */
2043
2179
  protected _ensureCallback<C extends BTNCallback<NODE>>(
2044
2180
  identifier: ReturnType<C> | null | undefined,
2045
2181
  callback: C = this._DEFAULT_CALLBACK as C