min-heap-typed 1.52.6 → 1.52.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/dist/constants/index.d.ts +4 -0
  2. package/dist/constants/index.js +8 -0
  3. package/dist/data-structures/base/iterable-element-base.d.ts +8 -1
  4. package/dist/data-structures/base/iterable-element-base.js +10 -1
  5. package/dist/data-structures/base/iterable-entry-base.d.ts +8 -1
  6. package/dist/data-structures/base/iterable-entry-base.js +10 -10
  7. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +31 -32
  8. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +43 -44
  9. package/dist/data-structures/binary-tree/avl-tree.d.ts +23 -24
  10. package/dist/data-structures/binary-tree/avl-tree.js +71 -64
  11. package/dist/data-structures/binary-tree/binary-indexed-tree.js +2 -2
  12. package/dist/data-structures/binary-tree/binary-tree.d.ts +591 -402
  13. package/dist/data-structures/binary-tree/binary-tree.js +690 -604
  14. package/dist/data-structures/binary-tree/bst.d.ts +72 -65
  15. package/dist/data-structures/binary-tree/bst.js +122 -125
  16. package/dist/data-structures/binary-tree/rb-tree.d.ts +21 -24
  17. package/dist/data-structures/binary-tree/rb-tree.js +42 -39
  18. package/dist/data-structures/binary-tree/segment-tree.d.ts +2 -2
  19. package/dist/data-structures/binary-tree/segment-tree.js +2 -2
  20. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +27 -31
  21. package/dist/data-structures/binary-tree/tree-multi-map.js +45 -43
  22. package/dist/data-structures/graph/abstract-graph.d.ts +2 -2
  23. package/dist/data-structures/graph/abstract-graph.js +7 -4
  24. package/dist/data-structures/graph/directed-graph.d.ts +2 -2
  25. package/dist/data-structures/graph/directed-graph.js +4 -2
  26. package/dist/data-structures/graph/undirected-graph.d.ts +2 -2
  27. package/dist/data-structures/hash/hash-map.d.ts +2 -2
  28. package/dist/data-structures/hash/hash-map.js +1 -1
  29. package/dist/data-structures/heap/heap.js +3 -3
  30. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
  31. package/dist/data-structures/linked-list/doubly-linked-list.js +7 -7
  32. package/dist/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  33. package/dist/data-structures/linked-list/singly-linked-list.js +6 -6
  34. package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -2
  35. package/dist/data-structures/matrix/matrix.d.ts +2 -2
  36. package/dist/data-structures/matrix/navigator.d.ts +2 -2
  37. package/dist/data-structures/matrix/navigator.js +4 -2
  38. package/dist/data-structures/queue/deque.d.ts +3 -3
  39. package/dist/data-structures/queue/deque.js +29 -29
  40. package/dist/data-structures/queue/queue.d.ts +1 -1
  41. package/dist/data-structures/stack/stack.d.ts +2 -2
  42. package/dist/data-structures/trie/trie.d.ts +2 -2
  43. package/dist/data-structures/trie/trie.js +1 -1
  44. package/dist/index.d.ts +1 -0
  45. package/dist/index.js +1 -0
  46. package/dist/interfaces/binary-tree.d.ts +2 -2
  47. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -4
  48. package/dist/types/data-structures/binary-tree/binary-tree.js +0 -6
  49. package/package.json +2 -2
  50. package/src/constants/index.ts +4 -0
  51. package/src/data-structures/base/iterable-element-base.ts +11 -1
  52. package/src/data-structures/base/iterable-entry-base.ts +11 -19
  53. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +46 -50
  54. package/src/data-structures/binary-tree/avl-tree.ts +68 -71
  55. package/src/data-structures/binary-tree/binary-indexed-tree.ts +2 -2
  56. package/src/data-structures/binary-tree/binary-tree.ts +716 -748
  57. package/src/data-structures/binary-tree/bst.ts +137 -146
  58. package/src/data-structures/binary-tree/rb-tree.ts +46 -46
  59. package/src/data-structures/binary-tree/segment-tree.ts +2 -2
  60. package/src/data-structures/binary-tree/tree-multi-map.ts +49 -49
  61. package/src/data-structures/graph/abstract-graph.ts +6 -6
  62. package/src/data-structures/graph/directed-graph.ts +4 -4
  63. package/src/data-structures/graph/undirected-graph.ts +2 -2
  64. package/src/data-structures/hash/hash-map.ts +3 -3
  65. package/src/data-structures/heap/heap.ts +3 -3
  66. package/src/data-structures/linked-list/doubly-linked-list.ts +9 -9
  67. package/src/data-structures/linked-list/singly-linked-list.ts +8 -8
  68. package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
  69. package/src/data-structures/matrix/matrix.ts +2 -2
  70. package/src/data-structures/matrix/navigator.ts +4 -4
  71. package/src/data-structures/queue/deque.ts +31 -31
  72. package/src/data-structures/queue/queue.ts +1 -1
  73. package/src/data-structures/stack/stack.ts +2 -2
  74. package/src/data-structures/trie/trie.ts +3 -3
  75. package/src/index.ts +2 -1
  76. package/src/interfaces/binary-tree.ts +3 -3
  77. package/src/types/data-structures/binary-tree/binary-tree.ts +3 -5
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * data-structure-typed
3
3
  *
4
- * @author Tyler Zeng
5
- * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
4
+ * @author Pablo Zeng
5
+ * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
8
 
@@ -15,18 +15,20 @@ import type {
15
15
  BTNCallback,
16
16
  BTNEntry,
17
17
  BTNKeyOrNodeOrEntry,
18
+ BTNPredicate,
18
19
  DFSOrderPattern,
20
+ DFSStackItem,
19
21
  EntryCallback,
20
22
  FamilyPosition,
21
23
  IterationType,
22
24
  NodeDisplayLayout,
23
25
  OptBTNOrNull
24
26
  } from '../../types';
25
- import { DFSOperation, DFSStackItem } from '../../types';
26
27
  import { IBinaryTree } from '../../interfaces';
27
28
  import { isComparable, trampoline } from '../../utils';
28
29
  import { Queue } from '../queue';
29
30
  import { IterableEntryBase } from '../base';
31
+ import { DFSOperation } from '../../constants';
30
32
 
31
33
  /**
32
34
  * Represents a node in a binary tree.
@@ -44,13 +46,6 @@ export class BinaryTreeNode<
44
46
 
45
47
  parent?: NODE;
46
48
 
47
- /**
48
- * The constructor function initializes an object with a key and an optional value.
49
- * @param {K} key - The "key" parameter is of type K, which represents the type of the key for the
50
- * constructor. It is used to set the key property of the object being created.
51
- * @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
52
- * value associated with the key in the constructor.
53
- */
54
49
  constructor(key: K, value?: V) {
55
50
  this.key = key;
56
51
  this.value = value;
@@ -58,21 +53,10 @@ export class BinaryTreeNode<
58
53
 
59
54
  protected _left?: OptBTNOrNull<NODE>;
60
55
 
61
- /**
62
- * The function returns the value of the `_left` property, which can be of type `NODE`, `null`, or
63
- * `undefined`.
64
- * @returns The left node of the current node is being returned. It can be either a NODE object,
65
- * null, or undefined.
66
- */
67
56
  get left(): OptBTNOrNull<NODE> {
68
57
  return this._left;
69
58
  }
70
59
 
71
- /**
72
- * The function sets the left child of a node and updates its parent reference.
73
- * @param {OptBTNOrNull<NODE>} v - The parameter `v` can be of type `NODE`, `null`, or
74
- * `undefined`.
75
- */
76
60
  set left(v: OptBTNOrNull<NODE>) {
77
61
  if (v) {
78
62
  v.parent = this as unknown as NODE;
@@ -82,20 +66,10 @@ export class BinaryTreeNode<
82
66
 
83
67
  protected _right?: OptBTNOrNull<NODE>;
84
68
 
85
- /**
86
- * The function returns the right node of a binary tree or null if it doesn't exist.
87
- * @returns The method is returning the value of the `_right` property, which can be a `NODE` object,
88
- * `null`, or `undefined`.
89
- */
90
69
  get right(): OptBTNOrNull<NODE> {
91
70
  return this._right;
92
71
  }
93
72
 
94
- /**
95
- * The function sets the right child of a node and updates its parent.
96
- * @param {OptBTNOrNull<NODE>} v - The parameter `v` can be of type `NODE`, `null`, or
97
- * `undefined`.
98
- */
99
73
  set right(v: OptBTNOrNull<NODE>) {
100
74
  if (v) {
101
75
  v.parent = this as unknown as NODE;
@@ -103,10 +77,6 @@ export class BinaryTreeNode<
103
77
  this._right = v;
104
78
  }
105
79
 
106
- /**
107
- * Get the position of the node within its family.
108
- * @returns {FamilyPosition} - The family position of the node.
109
- */
110
80
  get familyPosition(): FamilyPosition {
111
81
  const that = this as unknown as NODE;
112
82
  if (!this.parent) {
@@ -130,7 +100,6 @@ export class BinaryTreeNode<
130
100
  * 4. Subtrees: Each child of a node forms the root of a subtree.
131
101
  * 5. Leaf Nodes: Nodes without children are leaves.
132
102
  */
133
-
134
103
  export class BinaryTree<
135
104
  K = any,
136
105
  V = any,
@@ -144,16 +113,16 @@ export class BinaryTree<
144
113
  iterationType: IterationType = 'ITERATIVE';
145
114
 
146
115
  /**
147
- * The constructor function initializes a binary tree object with optional keysOrNodesOrEntriesOrRawElements and options.
148
- * @param [keysOrNodesOrEntriesOrRawElements] - Optional iterable of BTNKeyOrNodeOrEntry objects. These objects represent the
149
- * nodes to be added to the binary tree.
150
- * @param [options] - The `options` parameter is an optional object that can contain additional
151
- * configuration options for the binary tree. In this case, it is of type
152
- * `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
153
- * required.
116
+ * The constructor initializes a binary tree with optional options and adds keys, nodes, entries, or
117
+ * raw data if provided.
118
+ * @param keysOrNodesOrEntriesOrRaws - The `keysOrNodesOrEntriesOrRaws` parameter in the constructor
119
+ * is an iterable that can contain elements of type `BTNKeyOrNodeOrEntry<K, V, NODE>` or `R`. It is
120
+ * initialized with an empty array `[]` by default.
121
+ * @param [options] - The `options` parameter in the constructor is an object that can contain the
122
+ * following properties:
154
123
  */
155
124
  constructor(
156
- keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
125
+ keysOrNodesOrEntriesOrRaws: Iterable<BTNKeyOrNodeOrEntry<K, V, NODE> | R> = [],
157
126
  options?: BinaryTreeOptions<K, V, R>
158
127
  ) {
159
128
  super();
@@ -164,105 +133,100 @@ export class BinaryTree<
164
133
  else if (toEntryFn) throw TypeError('toEntryFn must be a function type');
165
134
  }
166
135
 
167
- if (keysOrNodesOrEntriesOrRawElements) this.addMany(keysOrNodesOrEntriesOrRawElements);
136
+ if (keysOrNodesOrEntriesOrRaws) this.addMany(keysOrNodesOrEntriesOrRaws);
168
137
  }
169
138
 
170
139
  protected _root?: OptBTNOrNull<NODE>;
171
140
 
172
- /**
173
- * The function returns the root node, which can be of type NODE, null, or undefined.
174
- * @returns The method is returning the value of the `_root` property, which can be of type `NODE`,
175
- * `null`, or `undefined`.
176
- */
177
141
  get root(): OptBTNOrNull<NODE> {
178
142
  return this._root;
179
143
  }
180
144
 
181
145
  protected _size: number = 0;
182
146
 
183
- /**
184
- * The function returns the size of an object.
185
- * @returns The size of the object, which is a number.
186
- */
187
147
  get size(): number {
188
148
  return this._size;
189
149
  }
190
150
 
191
151
  protected _NIL: NODE = new BinaryTreeNode<K, V>(NaN as K) as unknown as NODE;
192
152
 
193
- /**
194
- * The function returns the value of the _NIL property.
195
- * @returns The method is returning the value of the `_NIL` property.
196
- */
197
153
  get NIL(): NODE {
198
154
  return this._NIL;
199
155
  }
200
156
 
201
157
  protected _toEntryFn?: (rawElement: R) => BTNEntry<K, V>;
202
158
 
203
- /**
204
- * The function returns the value of the _toEntryFn property.
205
- * @returns The function being returned is `this._toEntryFn`.
206
- */
207
159
  get toEntryFn() {
208
160
  return this._toEntryFn;
209
161
  }
210
162
 
211
163
  /**
212
- * Creates a new instance of BinaryTreeNode with the given key and value.
213
- * @param {K} key - The key for the new node.
214
- * @param {V} value - The value for the new node.
215
- * @returns {NODE} - The newly created BinaryTreeNode.
164
+ * The function creates a new binary tree node with a specified key and optional value.
165
+ * @param {K} key - The `key` parameter is the key of the node being created in the binary tree.
166
+ * @param {V} [value] - The `value` parameter in the `createNode` function is optional, meaning it is
167
+ * not required to be provided when calling the function. If a `value` is provided, it should be of
168
+ * type `V`, which is the type of the value associated with the node.
169
+ * @returns A new BinaryTreeNode instance with the provided key and value is being returned, casted
170
+ * as NODE.
216
171
  */
217
172
  createNode(key: K, value?: V): NODE {
218
173
  return new BinaryTreeNode<K, V, NODE>(key, value) as NODE;
219
174
  }
220
175
 
221
176
  /**
222
- * The function creates a binary tree with the given options.
223
- * @param [options] - The `options` parameter is an optional object that allows you to customize the
224
- * behavior of the `BinaryTree` class. It is of type `Partial<BinaryTreeOptions>`, which means that
225
- * you can provide only a subset of the properties defined in the `BinaryTreeOptions` interface.
226
- * @returns a new instance of a binary tree.
177
+ * The function creates a binary tree with the specified options.
178
+ * @param [options] - The `options` parameter in the `createTree` function is an optional parameter
179
+ * that allows you to provide partial configuration options for creating a binary tree. It is of type
180
+ * `Partial<BinaryTreeOptions<K, V, R>>`, which means you can pass in an object containing a subset
181
+ * of properties
182
+ * @returns A new instance of a binary tree with the specified options is being returned.
227
183
  */
228
- createTree(options?: Partial<BinaryTreeOptions<K, V, R>>): TREE {
229
- return new BinaryTree<K, V, R, NODE, TREE>([], { iterationType: this.iterationType, ...options }) as TREE;
184
+ createTree(options?: BinaryTreeOptions<K, V, R>): TREE {
185
+ return new BinaryTree<K, V, R, NODE, TREE>([], {
186
+ iterationType: this.iterationType,
187
+ toEntryFn: this._toEntryFn,
188
+ ...options
189
+ }) as TREE;
230
190
  }
231
191
 
232
192
  /**
233
- * The function `keyValueOrEntryOrRawElementToNode` converts a key-value pair, entry, or raw element
234
- * into a node object.
235
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
236
- * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
237
- * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
238
- * `keyValueOrEntryOrRawElementToNode` function. It represents the value associated with a key in a
239
- * key-value pair. If provided, it will be used to create a node with the specified key and value.
240
- * @returns The function `keyValueOrEntryOrRawElementToNode` returns either a `NODE` object, `null`,
241
- * or `undefined`.
193
+ * The function `keyValueOrEntryOrRawElementToNode` converts various input types into a node object
194
+ * or returns null.
195
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The
196
+ * `keyValueOrEntryOrRawElementToNode` function takes in a parameter `keyOrNodeOrEntryOrRaw`, which
197
+ * can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>` or `R`. This parameter represents either a key, a
198
+ * node, an entry
199
+ * @param {V} [value] - The `value` parameter in the `keyValueOrEntryOrRawElementToNode` function is
200
+ * an optional parameter of type `V`. It represents the value associated with the key in the node
201
+ * being created. If a `value` is provided, it will be used when creating the node. If
202
+ * @returns The `keyValueOrEntryOrRawElementToNode` function returns an optional node
203
+ * (`OptBTNOrNull<NODE>`) based on the input parameters provided. The function checks the type of the
204
+ * input parameter (`keyOrNodeOrEntryOrRaw`) and processes it accordingly to return a node or null
205
+ * value.
242
206
  */
243
207
  keyValueOrEntryOrRawElementToNode(
244
- keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
208
+ keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
245
209
  value?: V
246
210
  ): OptBTNOrNull<NODE> {
247
- if (keyOrNodeOrEntryOrRawElement === undefined) return;
248
- if (keyOrNodeOrEntryOrRawElement === null) return null;
211
+ if (keyOrNodeOrEntryOrRaw === undefined) return;
212
+ if (keyOrNodeOrEntryOrRaw === null) return null;
249
213
 
250
- if (this.isNode(keyOrNodeOrEntryOrRawElement)) return keyOrNodeOrEntryOrRawElement;
214
+ if (this.isNode(keyOrNodeOrEntryOrRaw)) return keyOrNodeOrEntryOrRaw;
251
215
 
252
- if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
253
- const [key, entryValue] = keyOrNodeOrEntryOrRawElement;
216
+ if (this.isEntry(keyOrNodeOrEntryOrRaw)) {
217
+ const [key, entryValue] = keyOrNodeOrEntryOrRaw;
254
218
  if (key === undefined) return;
255
219
  else if (key === null) return null;
256
220
  if (this.isKey(key)) return this.createNode(key, value ?? entryValue);
257
221
  }
258
222
 
259
- if (this.toEntryFn) {
260
- const [key, entryValue] = this.toEntryFn(keyOrNodeOrEntryOrRawElement as R);
223
+ if (this._toEntryFn) {
224
+ const [key, entryValue] = this._toEntryFn(keyOrNodeOrEntryOrRaw as R);
261
225
  if (this.isKey(key)) return this.createNode(key, value ?? entryValue);
262
226
  else return;
263
227
  }
264
228
 
265
- if (this.isKey(keyOrNodeOrEntryOrRawElement)) return this.createNode(keyOrNodeOrEntryOrRawElement, value);
229
+ if (this.isKey(keyOrNodeOrEntryOrRaw)) return this.createNode(keyOrNodeOrEntryOrRaw, value);
266
230
 
267
231
  return;
268
232
  }
@@ -271,110 +235,126 @@ export class BinaryTree<
271
235
  * Time Complexity: O(n)
272
236
  * Space Complexity: O(log n)
273
237
  *
274
- * The `ensureNode` function checks if the input is a valid node and returns it, or converts it to a
275
- * node if it is a key or entry.
276
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
277
- * `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, `BTNKeyOrNodeOrEntry<K, V, NODE>`, or
278
- * a raw element.
279
- * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
280
- * parameter that specifies the type of iteration to be used when searching for a node. It has a
281
- * default value of `'ITERATIVE'`.
282
- * @returns The function `ensureNode` returns either a `NODE` object, `null`, or `undefined`.
238
+ * The function `ensureNode` in TypeScript checks if a given input is a node, entry, key, or raw
239
+ * value and returns the corresponding node or null.
240
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The `keyOrNodeOrEntryOrRaw`
241
+ * parameter in the `ensureNode` function can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>` or `R`. It
242
+ * is used to determine whether the input is a key, node, entry, or raw data. The
243
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `ensureNode` function
244
+ * is used to specify the type of iteration to be performed. It has a default value of
245
+ * `this.iterationType` if not explicitly provided.
246
+ * @returns The `ensureNode` function returns either a node, `null`, or `undefined` based on the
247
+ * conditions specified in the code snippet.
283
248
  */
284
249
  ensureNode(
285
- keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
250
+ keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
286
251
  iterationType: IterationType = this.iterationType
287
252
  ): OptBTNOrNull<NODE> {
288
- if (keyOrNodeOrEntryOrRawElement === null) return null;
289
- if (keyOrNodeOrEntryOrRawElement === undefined) return;
290
- if (keyOrNodeOrEntryOrRawElement === this.NIL) return;
291
- if (this.isNode(keyOrNodeOrEntryOrRawElement)) return keyOrNodeOrEntryOrRawElement;
253
+ if (keyOrNodeOrEntryOrRaw === null) return null;
254
+ if (keyOrNodeOrEntryOrRaw === undefined) return;
255
+ if (keyOrNodeOrEntryOrRaw === this._NIL) return;
256
+ if (this.isNode(keyOrNodeOrEntryOrRaw)) return keyOrNodeOrEntryOrRaw;
292
257
 
293
- if (this.toEntryFn) {
294
- const [key] = this.toEntryFn(keyOrNodeOrEntryOrRawElement as R);
295
- if (this.isKey(key)) return this.getNodeByKey(key);
296
- }
297
-
298
- if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
299
- const key = keyOrNodeOrEntryOrRawElement[0];
258
+ if (this.isEntry(keyOrNodeOrEntryOrRaw)) {
259
+ const key = keyOrNodeOrEntryOrRaw[0];
300
260
  if (key === null) return null;
301
261
  if (key === undefined) return;
302
262
  return this.getNodeByKey(key, iterationType);
303
263
  }
304
264
 
305
- if (this.isKey(keyOrNodeOrEntryOrRawElement)) return this.getNodeByKey(keyOrNodeOrEntryOrRawElement, iterationType);
265
+ if (this._toEntryFn) {
266
+ const [key] = this._toEntryFn(keyOrNodeOrEntryOrRaw as R);
267
+ if (this.isKey(key)) return this.getNodeByKey(key);
268
+ }
269
+
270
+ if (this.isKey(keyOrNodeOrEntryOrRaw)) return this.getNodeByKey(keyOrNodeOrEntryOrRaw, iterationType);
306
271
  return;
307
272
  }
308
273
 
309
274
  /**
310
- * The function checks if the input is an instance of the BinaryTreeNode class.
311
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
312
- * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
313
- * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
314
- * an instance of the `BinaryTreeNode` class.
275
+ * The function isNode checks if the input is an instance of BinaryTreeNode.
276
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
277
+ * `keyOrNodeOrEntryOrRaw` can be either a key, a node, an entry, or raw data. The function is
278
+ * checking if the input is an instance of a `BinaryTreeNode` and returning a boolean value
279
+ * accordingly.
280
+ * @returns The function `isNode` is checking if the input `keyOrNodeOrEntryOrRaw` is an instance of
281
+ * `BinaryTreeNode`. If it is, the function returns `true`, indicating that the input is a node. If
282
+ * it is not an instance of `BinaryTreeNode`, the function returns `false`, indicating that the input
283
+ * is not a node.
315
284
  */
316
- isNode(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntryOrRawElement is NODE {
317
- return keyOrNodeOrEntryOrRawElement instanceof BinaryTreeNode;
285
+ isNode(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): keyOrNodeOrEntryOrRaw is NODE {
286
+ return keyOrNodeOrEntryOrRaw instanceof BinaryTreeNode;
318
287
  }
319
288
 
320
289
  /**
321
- * The function checks if a given node is a valid node in a binary search tree.
322
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
323
- * `BTNKeyOrNodeOrEntry<K, V, NODE>`.
324
- * @returns a boolean value.
290
+ * The function `isRealNode` checks if a given input is a valid node in a binary tree.
291
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The `keyOrNodeOrEntryOrRaw`
292
+ * parameter in the `isRealNode` function can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>` or `R`.
293
+ * The function checks if the input parameter is a `NODE` type by verifying if it is not equal
294
+ * @returns The function `isRealNode` is checking if the input `keyOrNodeOrEntryOrRaw` is a valid
295
+ * node by comparing it to `this._NIL`, `null`, and `undefined`. If the input is not one of these
296
+ * values, it then calls the `isNode` method to further determine if the input is a node. The
297
+ * function will return a boolean value indicating whether the
325
298
  */
326
- isRealNode(node: R | BTNKeyOrNodeOrEntry<K, V, NODE>): node is NODE {
327
- if (node === this.NIL || node === null || node === undefined) return false;
328
- return this.isNode(node);
299
+ isRealNode(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): keyOrNodeOrEntryOrRaw is NODE {
300
+ if (keyOrNodeOrEntryOrRaw === this._NIL || keyOrNodeOrEntryOrRaw === null || keyOrNodeOrEntryOrRaw === undefined)
301
+ return false;
302
+ return this.isNode(keyOrNodeOrEntryOrRaw);
329
303
  }
330
304
 
331
305
  /**
332
- * The function checks if a given node is a real node or null.
333
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
334
- * `BTNKeyOrNodeOrEntry<K, V, NODE>`.
335
- * @returns a boolean value.
306
+ * The function checks if a given input is a valid node or null.
307
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
308
+ * `keyOrNodeOrEntryOrRaw` in the `isRealNodeOrNull` function can be of type `BTNKeyOrNodeOrEntry<K,
309
+ * V, NODE>` or `R`. It is a union type that can either be a key, a node, an entry, or
310
+ * @returns The function `isRealNodeOrNull` is returning a boolean value. It checks if the input
311
+ * `keyOrNodeOrEntryOrRaw` is either `null` or a real node, and returns `true` if it is a node or
312
+ * `null`, and `false` otherwise.
336
313
  */
337
- isRealNodeOrNull(node: R | BTNKeyOrNodeOrEntry<K, V, NODE>): node is NODE | null {
338
- return node === null || this.isRealNode(node);
314
+ isRealNodeOrNull(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): keyOrNodeOrEntryOrRaw is NODE | null {
315
+ return keyOrNodeOrEntryOrRaw === null || this.isRealNode(keyOrNodeOrEntryOrRaw);
339
316
  }
340
317
 
341
318
  /**
342
- * The function checks if a given node is equal to the NIL value.
343
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
344
- * `BTNKeyOrNodeOrEntry<K, V, NODE>`.
345
- * @returns a boolean value.
319
+ * The function isNIL checks if a given key, node, entry, or raw value is equal to the _NIL value.
320
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - BTNKeyOrNodeOrEntry<K, V,
321
+ * NODE> | R
322
+ * @returns The function is checking if the `keyOrNodeOrEntryOrRaw` parameter is equal to the `_NIL`
323
+ * property of the current object and returning a boolean value based on that comparison.
346
324
  */
347
- isNIL(node: R | BTNKeyOrNodeOrEntry<K, V, NODE>): boolean {
348
- return node === this.NIL;
325
+ isNIL(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): boolean {
326
+ return keyOrNodeOrEntryOrRaw === this._NIL;
349
327
  }
350
328
 
351
329
  /**
352
- * The function `isLeaf` determines whether a given node is a leaf node in a binary tree structure.
353
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} node - The `node` parameter in the `isLeaf` function
354
- * can be either a regular node (`R`) or a `BTNKeyOrNodeOrEntry<K, V, NODE>`.
355
- * @returns The `isLeaf` function is checking if the provided node is a leaf node in a binary tree.
356
- * If the node is `undefined`, it returns `false`. If the node is `null`, it returns `true`.
357
- * Otherwise, it checks if both the left and right children of the node are not real nodes, and
358
- * returns `true` if they are not, indicating that the node is a
330
+ * The function determines whether a given key, node, entry, or raw data is a leaf node in a binary
331
+ * tree.
332
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
333
+ * `keyOrNodeOrEntryOrRaw` can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>` or `R`. It represents a
334
+ * key, node, entry, or raw data in a binary tree structure. The function `isLeaf` checks whether the
335
+ * provided
336
+ * @returns The function `isLeaf` returns a boolean value indicating whether the input
337
+ * `keyOrNodeOrEntryOrRaw` is a leaf node in a binary tree.
359
338
  */
360
- isLeaf(node: R | BTNKeyOrNodeOrEntry<K, V, NODE>): boolean {
361
- node = this.ensureNode(node);
362
- if (node === undefined) return false;
363
- if (node === null) return true;
364
- return !this.isRealNode(node.left) && !this.isRealNode(node.right);
339
+ isLeaf(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): boolean {
340
+ keyOrNodeOrEntryOrRaw = this.ensureNode(keyOrNodeOrEntryOrRaw);
341
+ if (keyOrNodeOrEntryOrRaw === undefined) return false;
342
+ if (keyOrNodeOrEntryOrRaw === null) return true;
343
+ return !this.isRealNode(keyOrNodeOrEntryOrRaw.left) && !this.isRealNode(keyOrNodeOrEntryOrRaw.right);
365
344
  }
366
345
 
367
346
  /**
368
- * The function checks if the input is an array with two elements, indicating it is a binary tree
369
- * node entry.
370
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
371
- * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
372
- * @returns a boolean value.
347
+ * The function `isEntry` checks if the input is a BTNEntry object by verifying if it is an array
348
+ * with a length of 2.
349
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The `keyOrNodeOrEntryOrRaw`
350
+ * parameter in the `isEntry` function can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>` or type `R`.
351
+ * The function checks if the provided `keyOrNodeOrEntryOrRaw` is of type `BTN
352
+ * @returns The `isEntry` function is checking if the `keyOrNodeOrEntryOrRaw` parameter is an array
353
+ * with a length of 2. If it is, then it returns `true`, indicating that the parameter is of type
354
+ * `BTNEntry<K, V>`. If the condition is not met, it returns `false`.
373
355
  */
374
- isEntry(
375
- keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>
376
- ): keyOrNodeOrEntryOrRawElement is BTNEntry<K, V> {
377
- return Array.isArray(keyOrNodeOrEntryOrRawElement) && keyOrNodeOrEntryOrRawElement.length === 2;
356
+ isEntry(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): keyOrNodeOrEntryOrRaw is BTNEntry<K, V> {
357
+ return Array.isArray(keyOrNodeOrEntryOrRaw) && keyOrNodeOrEntryOrRaw.length === 2;
378
358
  }
379
359
 
380
360
  /**
@@ -397,30 +377,31 @@ export class BinaryTree<
397
377
  * Time Complexity O(n)
398
378
  * Space Complexity O(1)
399
379
  *
400
- * The `add` function is used to insert a new node into a binary tree, checking for duplicate keys
401
- * and finding the appropriate insertion position.
402
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
403
- * `keyOrNodeOrEntryOrRawElement` parameter can accept a value of type `R`, which represents the key,
404
- * node, entry, or raw element to be added to the tree. It can also accept a value of type
405
- * `BTNKeyOrNodeOrEntry<K, V, NODE>
406
- * @param {V} [value] - The `value` parameter is an optional value that can be associated with the
407
- * key being added to the tree. It represents the value that will be stored in the tree for the given
408
- * key.
409
- * @returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
410
- * insertion position cannot be found or if there are duplicate keys.
380
+ * The `add` function in TypeScript adds a new node to a binary tree while handling duplicate keys
381
+ * and finding the correct insertion position.
382
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The `add` method you provided
383
+ * seems to be for adding a new node to a binary tree structure. The `keyOrNodeOrEntryOrRaw`
384
+ * parameter in the method can accept different types of values:
385
+ * @param {V} [value] - The `value` parameter in the `add` method represents the value associated
386
+ * with the key that you want to add to the binary tree. When adding a key-value pair to the binary
387
+ * tree, you provide the key and its corresponding value. The `add` method then creates a new node
388
+ * with this
389
+ * @returns The `add` method returns a boolean value. It returns `true` if the insertion of the new
390
+ * node was successful, and `false` if the insertion position could not be found or if a duplicate
391
+ * key was found and the node was replaced instead of inserted.
411
392
  */
412
- add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
413
- const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
393
+ add(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R, value?: V): boolean {
394
+ const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw, value);
414
395
  if (newNode === undefined) return false;
415
396
 
416
397
  // If the tree is empty, directly set the new node as the root node
417
- if (!this.root) {
418
- this._root = newNode;
398
+ if (!this._root) {
399
+ this._setRoot(newNode);
419
400
  this._size = 1;
420
401
  return true;
421
402
  }
422
403
 
423
- const queue = new Queue<NODE>([this.root]);
404
+ const queue = new Queue<NODE>([this._root]);
424
405
  let potentialParent: NODE | undefined; // Record the parent node of the potential insertion location
425
406
 
426
407
  while (queue.size > 0) {
@@ -441,10 +422,10 @@ export class BinaryTree<
441
422
 
442
423
  // Continue traversing the left and right subtrees
443
424
  if (cur.left !== null) {
444
- cur.left && queue.push(cur.left);
425
+ if (cur.left) queue.push(cur.left);
445
426
  }
446
427
  if (cur.right !== null) {
447
- cur.right && queue.push(cur.right);
428
+ if (cur.right) queue.push(cur.right);
448
429
  }
449
430
  }
450
431
 
@@ -466,18 +447,22 @@ export class BinaryTree<
466
447
  * Time Complexity: O(k * n)
467
448
  * Space Complexity: O(1)
468
449
  *
469
- * The `addMany` function takes in an iterable of keys or nodes or entries or raw elements, and an
470
- * optional iterable of values, and adds each key or node or entry with its corresponding value to a
471
- * data structure, returning an array of booleans indicating whether each insertion was successful.
472
- * @param keysOrNodesOrEntriesOrRawElements - An iterable containing keys, nodes, entries, or raw
473
- * elements. These elements will be added to the data structure.
474
- * @param [values] - An optional iterable of values that correspond to the keys or nodes or entries
475
- * in the `keysOrNodesOrEntriesOrRawElements` parameter.
476
- * @returns The function `addMany` returns an array of booleans indicating whether each element was
477
- * successfully added to the data structure.
450
+ * The `addMany` function takes in multiple keys or nodes or entries or raw values along with
451
+ * optional values, and adds them to a data structure while returning an array indicating whether
452
+ * each insertion was successful.
453
+ * @param keysOrNodesOrEntriesOrRaws - `keysOrNodesOrEntriesOrRaws` is an iterable that can contain a
454
+ * mix of keys, nodes, entries, or raw values. Each element in this iterable can be of type
455
+ * `BTNKeyOrNodeOrEntry<K, V, NODE>` or `R`.
456
+ * @param [values] - The `values` parameter in the `addMany` function is an optional parameter that
457
+ * accepts an iterable of values. These values correspond to the keys or nodes being added in the
458
+ * `keysOrNodesOrEntriesOrRaws` parameter. If provided, the function will iterate over the values and
459
+ * assign them
460
+ * @returns The `addMany` method returns an array of boolean values indicating whether each key,
461
+ * node, entry, or raw value was successfully added to the data structure. Each boolean value
462
+ * corresponds to the success of adding the corresponding key or value in the input iterable.
478
463
  */
479
464
  addMany(
480
- keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>>,
465
+ keysOrNodesOrEntriesOrRaws: Iterable<BTNKeyOrNodeOrEntry<K, V, NODE> | R>,
481
466
  values?: Iterable<V | undefined>
482
467
  ): boolean[] {
483
468
  // TODO not sure addMany not be run multi times
@@ -488,7 +473,7 @@ export class BinaryTree<
488
473
  valuesIterator = values[Symbol.iterator]();
489
474
  }
490
475
 
491
- for (const keyOrNodeOrEntryOrRawElement of keysOrNodesOrEntriesOrRawElements) {
476
+ for (const keyOrNodeOrEntryOrRaw of keysOrNodesOrEntriesOrRaws) {
492
477
  let value: V | undefined | null = undefined;
493
478
 
494
479
  if (valuesIterator) {
@@ -498,7 +483,7 @@ export class BinaryTree<
498
483
  }
499
484
  }
500
485
 
501
- inserted.push(this.add(keyOrNodeOrEntryOrRawElement, value));
486
+ inserted.push(this.add(keyOrNodeOrEntryOrRaw, value));
502
487
  }
503
488
 
504
489
  return inserted;
@@ -508,54 +493,42 @@ export class BinaryTree<
508
493
  * Time Complexity: O(k * n)
509
494
  * Space Complexity: O(1)
510
495
  *
511
- * The `refill` function clears the current data and adds new data to the collection.
512
- * @param keysOrNodesOrEntriesOrRawElements - An iterable collection of keys, nodes, entries, or raw
513
- * elements. These can be of any type (R) or a specific type (BTNKeyOrNodeOrEntry<K, V, NODE>).
514
- * @param [values] - The `values` parameter is an optional iterable of values that will be associated
515
- * with the keys or nodes being added. If provided, the values will be assigned to the corresponding
516
- * keys or nodes. If not provided, the values will be set to `undefined`.
496
+ * The `refill` function clears the existing data structure and then adds new key-value pairs based
497
+ * on the provided input.
498
+ * @param keysOrNodesOrEntriesOrRaws - The `keysOrNodesOrEntriesOrRaws` parameter in the `refill`
499
+ * method can accept an iterable containing a mix of `BTNKeyOrNodeOrEntry<K, V, NODE>` objects or `R`
500
+ * objects.
501
+ * @param [values] - The `values` parameter in the `refill` method is an optional parameter that
502
+ * accepts an iterable of values of type `V` or `undefined`.
517
503
  */
518
504
  refill(
519
- keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>>,
505
+ keysOrNodesOrEntriesOrRaws: Iterable<BTNKeyOrNodeOrEntry<K, V, NODE> | R>,
520
506
  values?: Iterable<V | undefined>
521
507
  ): void {
522
508
  this.clear();
523
- this.addMany(keysOrNodesOrEntriesOrRawElements, values);
509
+ this.addMany(keysOrNodesOrEntriesOrRaws, values);
524
510
  }
525
511
 
526
- delete<C extends BTNCallback<NODE, K>>(identifier: K, callback?: C): BinaryTreeDeleteResult<NODE>[];
527
-
528
- delete<C extends BTNCallback<NODE, NODE>>(
529
- identifier: OptBTNOrNull<NODE>,
530
- callback?: C
531
- ): BinaryTreeDeleteResult<NODE>[];
532
-
533
- delete<C extends BTNCallback<NODE>>(identifier: ReturnType<C>, callback: C): BinaryTreeDeleteResult<NODE>[];
534
-
535
512
  /**
536
513
  * Time Complexity: O(n)
537
514
  * Space Complexity: O(1)
538
515
  *
539
- * The above function is a TypeScript implementation of deleting a node from a binary tree, returning
540
- * the deleted node and the node that needs to be balanced.
541
- * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
542
- * used to identify the node that needs to be deleted from the binary tree. It can be of any type
543
- * that is returned by the callback function.
544
- * @param {C} callback - The `callback` parameter is a function that is used to determine the
545
- * identifier of the node to be deleted. It is of type `C`, which extends the `BTNCallback<NODE>`
546
- * interface. The `BTNCallback<NODE>` interface represents a callback function that takes a node of
547
- * type `NODE
548
- * @returns an array of `BinaryTreeDeleteResult<NODE>`.
516
+ * The function `delete` in TypeScript implements the deletion of a node in a binary tree and returns
517
+ * the deleted node along with information for tree balancing.
518
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw
519
+ * - The `delete` method you provided is used to delete a node from a binary tree based on the key,
520
+ * node, entry or raw data. The method returns an array of
521
+ * `BinaryTreeDeleteResult` objects containing information about the deleted node and whether
522
+ * balancing is needed.
523
+ * @returns The `delete` method returns an array of `BinaryTreeDeleteResult` objects. Each object in
524
+ * the array contains information about the node that was deleted (`deleted`) and the node that may
525
+ * need to be balanced (`needBalanced`).
549
526
  */
550
- delete<C extends BTNCallback<NODE>>(
551
- identifier: ReturnType<C> | null | undefined,
552
- callback: C = this._DEFAULT_CALLBACK as C
553
- ): BinaryTreeDeleteResult<NODE>[] {
527
+ delete(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): BinaryTreeDeleteResult<NODE>[] {
554
528
  const deletedResult: BinaryTreeDeleteResult<NODE>[] = [];
555
- if (!this.root) return deletedResult;
556
- callback = this._ensureCallback(identifier, callback);
529
+ if (!this._root) return deletedResult;
557
530
 
558
- const curr = this.getNode(identifier, callback);
531
+ const curr = this.getNode(keyOrNodeOrEntryOrRaw);
559
532
  if (!curr) return deletedResult;
560
533
 
561
534
  const parent: NODE | undefined = curr?.parent;
@@ -589,84 +562,56 @@ export class BinaryTree<
589
562
  curr.right = undefined;
590
563
  }
591
564
 
592
- this._size = this.size - 1;
565
+ this._size = this._size - 1;
593
566
 
594
567
  deletedResult.push({ deleted: orgCurrent, needBalanced });
595
568
  return deletedResult;
596
569
  }
597
570
 
598
- getNodes<C extends BTNCallback<NODE, K>>(
599
- identifier: K,
600
- callback?: C,
601
- onlyOne?: boolean,
602
- beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
603
- iterationType?: IterationType
604
- ): NODE[];
605
-
606
- getNodes<C extends BTNCallback<NODE, NODE>>(
607
- identifier: OptBTNOrNull<NODE>,
608
- callback?: C,
609
- onlyOne?: boolean,
610
- beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
611
- iterationType?: IterationType
612
- ): NODE[];
613
-
614
- getNodes<C extends BTNCallback<NODE>>(
615
- identifier: ReturnType<C>,
616
- callback: C,
617
- onlyOne?: boolean,
618
- beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
619
- iterationType?: IterationType
620
- ): NODE[];
621
-
622
571
  /**
623
572
  * Time Complexity: O(n)
624
573
  * Space Complexity: O(k + log n)
625
574
  *
626
- * The function `getNodes` returns an array of nodes that match a given identifier, using either a
627
- * recursive or iterative approach.
628
- * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
629
- * that is used to identify the nodes. It can be of any type and is used to match against the result
630
- * of the callback function for each node.
631
- * @param {C} callback - The `callback` parameter is a function that takes a node as input and
632
- * returns a value. This value is used to identify the nodes that match the given identifier. The
633
- * `callback` function is optional and defaults to a default callback function
634
- * (`this._DEFAULT_CALLBACK`) if not provided.
635
- * @param [onlyOne=false] - A boolean value indicating whether to return only one node that matches
636
- * the identifier or all nodes that match the identifier. If set to true, only the first matching
637
- * node will be returned. If set to false, all matching nodes will be returned. The default value is
638
- * false.
639
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
640
- * point for the search. It can be either a node object, a key-value pair, or a key. If it is not
641
- * provided, the `root` of the data structure is used as the starting point.
642
- * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
643
- * iteration to be performed on the nodes of a binary tree. It can have two possible values:
644
- * @returns an array of NODE objects.
575
+ * The function `getNodes` retrieves nodes from a binary tree based on a key, node, entry, raw data,
576
+ * or predicate, with options for recursive or iterative traversal.
577
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrNodeOrEntryOrRawOrPredicate
578
+ * - The `getNodes` function you provided takes several parameters:
579
+ * @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` function is a boolean flag that
580
+ * determines whether to return only the first node that matches the criteria specified by the
581
+ * `keyOrNodeOrEntryOrRawOrPredicate` parameter. If `onlyOne` is set to `true`, the function will
582
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
583
+ * `getNodes` function is used to specify the starting point for traversing the binary tree. It
584
+ * represents the root node of the binary tree or the node from which the traversal should begin. If
585
+ * not provided, the default value is set to `this._root
586
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` function
587
+ * determines the type of iteration to be performed when traversing the nodes of a binary tree. It
588
+ * can have two possible values:
589
+ * @returns The `getNodes` function returns an array of nodes that satisfy the provided condition
590
+ * based on the input parameters and the iteration type specified.
645
591
  */
646
- getNodes<C extends BTNCallback<NODE>>(
647
- identifier: ReturnType<C> | null | undefined,
648
- callback: C = this._DEFAULT_CALLBACK as C,
592
+ getNodes(
593
+ keyOrNodeOrEntryOrRawOrPredicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>,
649
594
  onlyOne = false,
650
- beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
595
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
651
596
  iterationType: IterationType = this.iterationType
652
597
  ): NODE[] {
653
- if (identifier === undefined) return [];
654
- if (identifier === null) return [];
598
+ if (keyOrNodeOrEntryOrRawOrPredicate === undefined) return [];
599
+ if (keyOrNodeOrEntryOrRawOrPredicate === null) return [];
655
600
  beginRoot = this.ensureNode(beginRoot);
656
601
  if (!beginRoot) return [];
657
- callback = this._ensureCallback(identifier, callback);
602
+ const callback = this._ensurePredicate(keyOrNodeOrEntryOrRawOrPredicate);
658
603
 
659
604
  const ans: NODE[] = [];
660
605
 
661
606
  if (iterationType === 'RECURSIVE') {
662
607
  const dfs = (cur: NODE) => {
663
- if (callback(cur) === identifier) {
608
+ if (callback(cur)) {
664
609
  ans.push(cur);
665
610
  if (onlyOne) return;
666
611
  }
667
612
  if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
668
- this.isRealNode(cur.left) && dfs(cur.left);
669
- this.isRealNode(cur.right) && dfs(cur.right);
613
+ if (this.isRealNode(cur.left)) dfs(cur.left);
614
+ if (this.isRealNode(cur.right)) dfs(cur.right);
670
615
  };
671
616
 
672
617
  dfs(beginRoot);
@@ -675,12 +620,12 @@ export class BinaryTree<
675
620
  while (stack.length > 0) {
676
621
  const cur = stack.pop();
677
622
  if (this.isRealNode(cur)) {
678
- if (callback(cur) === identifier) {
623
+ if (callback(cur)) {
679
624
  ans.push(cur);
680
625
  if (onlyOne) return ans;
681
626
  }
682
- this.isRealNode(cur.left) && stack.push(cur.left);
683
- this.isRealNode(cur.right) && stack.push(cur.right);
627
+ if (this.isRealNode(cur.left)) stack.push(cur.left);
628
+ if (this.isRealNode(cur.right)) stack.push(cur.right);
684
629
  }
685
630
  }
686
631
  }
@@ -688,178 +633,115 @@ export class BinaryTree<
688
633
  return ans;
689
634
  }
690
635
 
691
- getNode<C extends BTNCallback<NODE, K>>(
692
- identifier: K,
693
- callback?: C,
694
- beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
695
- iterationType?: IterationType
696
- ): OptBTNOrNull<NODE>;
697
-
698
- getNode<C extends BTNCallback<NODE, NODE>>(
699
- identifier: OptBTNOrNull<NODE>,
700
- callback?: C,
701
- beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
702
- iterationType?: IterationType
703
- ): OptBTNOrNull<NODE>;
704
-
705
- getNode<C extends BTNCallback<NODE>>(
706
- identifier: ReturnType<C>,
707
- callback: C,
708
- beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
709
- iterationType?: IterationType
710
- ): OptBTNOrNull<NODE>;
711
-
712
636
  /**
713
637
  * Time Complexity: O(n)
714
638
  * Space Complexity: O(log n).
715
639
  *
716
- * The function `getNode` returns the first node that matches the given identifier and callback,
717
- * starting from the specified root node and using the specified iteration type.
718
- * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
719
- * used to identify the node you want to retrieve. It can be of any type that is the return type of
720
- * the `C` callback function, or it can be `null` or `undefined`.
721
- * @param {C} callback - The `callback` parameter is a function that will be used to determine if a
722
- * node matches the desired criteria. It should return a value that can be used to identify the node.
723
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
724
- * point for searching nodes in a tree structure. It can be either a root node, a key-value pair, or
725
- * a node entry. If not provided, the search will start from the root of the tree.
726
- * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
727
- * of iteration to be performed when searching for nodes. It can have one of the following values:
728
- * @returns The method is returning a NODE object, or null, or undefined.
640
+ * The `getNode` function retrieves a node based on the provided key, node, entry, raw data, or
641
+ * predicate.
642
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrNodeOrEntryOrRawOrPredicate
643
+ * - The `keyOrNodeOrEntryOrRawOrPredicate` parameter in the `getNode` function can accept a key,
644
+ * node, entry, raw data, or a predicate function.
645
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
646
+ * `getNode` function is used to specify the starting point for searching for a node in a binary
647
+ * tree. If no specific starting point is provided, the default value is set to `this._root`, which
648
+ * is typically the root node of the binary tree.
649
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `getNode` method is
650
+ * used to specify the type of iteration to be performed when searching for a node. It has a default
651
+ * value of `this.iterationType`, which means it will use the iteration type defined in the current
652
+ * context if no specific value is provided
653
+ * @returns The `getNode` function is returning the first node that matches the specified criteria,
654
+ * or `null` if no matching node is found.
729
655
  */
730
- getNode<C extends BTNCallback<NODE>>(
731
- identifier: ReturnType<C> | null | undefined,
732
- callback: C = this._DEFAULT_CALLBACK as C,
733
- beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
656
+ getNode(
657
+ keyOrNodeOrEntryOrRawOrPredicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>,
658
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
734
659
  iterationType: IterationType = this.iterationType
735
660
  ): OptBTNOrNull<NODE> {
736
- return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
661
+ return this.getNodes(keyOrNodeOrEntryOrRawOrPredicate, true, beginRoot, iterationType)[0] ?? null;
737
662
  }
738
663
 
739
664
  /**
740
665
  * Time Complexity: O(n)
741
666
  * Space Complexity: O(log n)
742
667
  *
743
- * The function `getNodeByKey` returns a node with a specific key value from a tree structure.
744
- * @param {K} key - The key parameter is the value that you want to search for in the tree. It is
745
- * used to find the node with the matching key value.
746
- * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
747
- * parameter that specifies the type of iteration to be used when searching for a node in the tree.
748
- * It has a default value of `'ITERATIVE'`.
749
- * @returns a value of type NODE, null, or undefined.
668
+ * The function `getNodeByKey` retrieves a node by its key from a binary tree structure.
669
+ * @param {K} key - The `key` parameter is the value used to search for a specific node in a data
670
+ * structure.
671
+ * @param {IterationType} iterationType - The `iterationType` parameter is a type of iteration that
672
+ * specifies how the tree nodes should be traversed when searching for a node with the given key. It
673
+ * is an optional parameter with a default value of `this.iterationType`.
674
+ * @returns The `getNodeByKey` function is returning an optional binary tree node
675
+ * (`OptBTNOrNull<NODE>`).
750
676
  */
751
677
  getNodeByKey(key: K, iterationType: IterationType = this.iterationType): OptBTNOrNull<NODE> {
752
- return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
678
+ return this.getNode(key, this._root, iterationType);
753
679
  }
754
680
 
755
- override get<C extends BTNCallback<NODE, K>>(
756
- identifier: K,
757
- callback?: C,
758
- beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
759
- iterationType?: IterationType
760
- ): V | undefined;
761
-
762
- override get<C extends BTNCallback<NODE, NODE>>(
763
- identifier: OptBTNOrNull<NODE>,
764
- callback?: C,
765
- beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
766
- iterationType?: IterationType
767
- ): V | undefined;
768
-
769
- override get<C extends BTNCallback<NODE>>(
770
- identifier: ReturnType<C>,
771
- callback: C,
772
- beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
773
- iterationType?: IterationType
774
- ): V | undefined;
775
-
776
681
  /**
777
682
  * Time Complexity: O(n)
778
683
  * Space Complexity: O(log n)
779
684
  *
780
- * The function `get` in TypeScript overrides the base class method and returns the value associated
781
- * with the given identifier.
782
- * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
783
- * used to identify the node in the binary tree. It can be of any type that is returned by the
784
- * callback function `C`. It can also be `null` or `undefined` if no identifier is provided.
785
- * @param {C} callback - The `callback` parameter is a function that will be used to determine if a
786
- * node matches the given identifier. It is optional and defaults to `this._DEFAULT_CALLBACK`.
787
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
788
- * point for the search in the binary tree. It can be either a root node of the tree or a key, node,
789
- * or entry object that exists in the tree. If no specific starting point is provided, the search
790
- * will begin from the root of the
791
- * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
792
- * of iteration to be performed when searching for a node in the tree. It can have one of the
793
- * following values:
794
- * @returns The method is returning the value associated with the specified identifier in the binary
795
- * tree.
685
+ * This function overrides the `get` method to retrieve the value associated with a specified key,
686
+ * node, entry, raw data, or predicate in a data structure.
687
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrNodeOrEntryOrRawOrPredicate
688
+ * - The `keyOrNodeOrEntryOrRawOrPredicate` parameter in the `get` method can accept one of the
689
+ * following types:
690
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `get`
691
+ * method is used to specify the starting point for searching for a key or node in the binary tree.
692
+ * If no specific starting point is provided, the default starting point is the root of the binary
693
+ * tree (`this._root`).
694
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `get` method is used
695
+ * to specify the type of iteration to be performed when searching for a key in the binary tree. It
696
+ * is an optional parameter with a default value of `this.iterationType`, which means it will use the
697
+ * iteration type defined in the
698
+ * @returns The `get` method is returning the value associated with the specified key, node, entry,
699
+ * raw data, or predicate in the binary tree map. If the specified key or node is found in the tree,
700
+ * the method returns the corresponding value. If the key or node is not found, it returns
701
+ * `undefined`.
796
702
  */
797
- override get<C extends BTNCallback<NODE>>(
798
- identifier: ReturnType<C> | null | undefined,
799
- callback: C = this._DEFAULT_CALLBACK as C,
800
- beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
703
+ override get(
704
+ keyOrNodeOrEntryOrRawOrPredicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>,
705
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
801
706
  iterationType: IterationType = this.iterationType
802
707
  ): V | undefined {
803
- return this.getNode(identifier, callback, beginRoot, iterationType)?.value;
708
+ return this.getNode(keyOrNodeOrEntryOrRawOrPredicate, beginRoot, iterationType)?.value;
804
709
  }
805
710
 
806
- override has<C extends BTNCallback<NODE, K>>(
807
- identifier: K,
808
- callback?: C,
809
- beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
810
- iterationType?: IterationType
811
- ): boolean;
812
-
813
- override has<C extends BTNCallback<NODE, NODE>>(
814
- identifier: OptBTNOrNull<NODE>,
815
- callback?: C,
816
- beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
817
- iterationType?: IterationType
818
- ): boolean;
819
-
820
- override has<C extends BTNCallback<NODE>>(
821
- identifier: ReturnType<C> | null | undefined,
822
- callback: C,
823
- beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
824
- iterationType?: IterationType
825
- ): boolean;
826
-
827
711
  /**
828
712
  * Time Complexity: O(n)
829
713
  * Space Complexity: O(log n)
830
714
  *
831
- * The `has` function checks if a given identifier exists in the data structure and returns a boolean
832
- * value.
833
- * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
834
- * used to identify a specific node or entry in the data structure. It can be of any type that is
835
- * returned by the callback function `C`. It can also be `null` or `undefined` if no specific
836
- * identifier is provided.
837
- * @param {C} callback - The `callback` parameter is a function that will be used to determine
838
- * whether a node should be included in the result or not. It is of type `C`, which extends the
839
- * `BTNCallback<NODE>` type.
840
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
841
- * point for the iteration in the data structure. It can be either a root node, a key-value pair, or
842
- * a node entry. If not specified, it defaults to the root of the data structure.
843
- * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
844
- * of iteration to be performed. It is an optional parameter with a default value of `IterationType`.
845
- * @returns The method is returning a boolean value.
715
+ * The `has` function in TypeScript checks if a specified key, node, entry, raw data, or predicate
716
+ * exists in the data structure.
717
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrNodeOrEntryOrRawOrPredicate
718
+ * - The `keyOrNodeOrEntryOrRawOrPredicate` parameter in the `override has` method can accept one of
719
+ * the following types:
720
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
721
+ * `override` method is used to specify the starting point for the search operation within the data
722
+ * structure. It defaults to `this._root` if not provided explicitly.
723
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `override has` method
724
+ * is used to specify the type of iteration to be performed. It has a default value of
725
+ * `this.iterationType`, which means it will use the iteration type defined in the current context if
726
+ * no value is provided when calling the method.
727
+ * @returns The `override has` method is returning a boolean value. It checks if there are any nodes
728
+ * that match the provided key, node, entry, raw data, or predicate in the tree structure. If there
729
+ * are matching nodes, it returns `true`, indicating that the tree contains the specified element.
730
+ * Otherwise, it returns `false`.
846
731
  */
847
- override has<C extends BTNCallback<NODE>>(
848
- identifier: ReturnType<C> | null | undefined,
849
- callback: C = this._DEFAULT_CALLBACK as C,
850
- beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
732
+ override has(
733
+ keyOrNodeOrEntryOrRawOrPredicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>,
734
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
851
735
  iterationType: IterationType = this.iterationType
852
736
  ): boolean {
853
- callback = this._ensureCallback(identifier, callback);
854
-
855
- return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
737
+ return this.getNodes(keyOrNodeOrEntryOrRawOrPredicate, true, beginRoot, iterationType).length > 0;
856
738
  }
857
739
 
858
740
  /**
859
741
  * Time Complexity: O(1)
860
742
  * Space Complexity: O(1)
861
743
  *
862
- * Clear the binary tree, removing all nodes.
744
+ * The `clear` function resets the root node and size of a data structure to empty.
863
745
  */
864
746
  clear() {
865
747
  this._setRoot(undefined);
@@ -870,26 +752,31 @@ export class BinaryTree<
870
752
  * Time Complexity: O(1)
871
753
  * Space Complexity: O(1)
872
754
  *
873
- * Check if the binary tree is empty.
874
- * @returns {boolean} - True if the binary tree is empty, false otherwise.
755
+ * The `isEmpty` function in TypeScript checks if a data structure has no elements and returns a
756
+ * boolean value.
757
+ * @returns The `isEmpty()` method is returning a boolean value, specifically `true` if the `_size`
758
+ * property is equal to 0, indicating that the data structure is empty, and `false` otherwise.
875
759
  */
876
760
  isEmpty(): boolean {
877
- return this.size === 0;
761
+ return this._size === 0;
878
762
  }
879
763
 
880
764
  /**
881
765
  * Time Complexity: O(n)
882
766
  * Space Complexity: O(log n)
883
767
  *
884
- * The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
885
- * height of the tree.
886
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The parameter `beginRoot` is optional and
887
- * has a default value of `this.root`. It represents the starting point for checking if the tree is
888
- * perfectly balanced. It can be either a root node (`R`), a key or node or entry
889
- * (`BTNKeyOrNodeOrEntry<K, V, NODE
890
- * @returns a boolean value.
768
+ * The function checks if a binary tree is perfectly balanced by comparing its minimum height with
769
+ * its height.
770
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter is the starting
771
+ * point for checking if the binary tree is perfectly balanced. It represents the root node of the
772
+ * binary tree or a specific node from which the balance check should begin.
773
+ * @returns The method `isPerfectlyBalanced` is returning a boolean value, which indicates whether
774
+ * the tree starting from the `beginRoot` node is perfectly balanced or not. The return value is
775
+ * determined by comparing the minimum height of the tree with the height of the tree. If the minimum
776
+ * height plus 1 is greater than or equal to the height of the tree, then it is considered perfectly
777
+ * balanced and
891
778
  */
892
- isPerfectlyBalanced(beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root): boolean {
779
+ isPerfectlyBalanced(beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root): boolean {
893
780
  return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
894
781
  }
895
782
 
@@ -897,18 +784,22 @@ export class BinaryTree<
897
784
  * Time Complexity: O(n)
898
785
  * Space Complexity: O(1)
899
786
  *
900
- * The function `isBST` checks if a binary search tree is valid, either recursively or iteratively.
901
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
902
- * starting point for checking if a binary search tree (BST) is valid. It can be either a root node
903
- * of the BST, a key value of a node in the BST, or an entry object containing both the key and value
904
- * of a node in the BST
905
- * @param {IterationType} iterationType - The `iterationType` parameter is used to determine the type
906
- * of iteration to be performed while checking if the binary search tree (BST) is valid. It can have
907
- * two possible values:
908
- * @returns a boolean value.
787
+ * The function `isBST` in TypeScript checks if a binary search tree is valid using either recursive
788
+ * or iterative methods.
789
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `isBST`
790
+ * function represents the starting point for checking whether a binary search tree (BST) is valid.
791
+ * It can be a node in the BST or a reference to the root of the BST. If no specific node is
792
+ * provided, the function will default to
793
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `isBST` function
794
+ * determines whether the function should use a recursive approach or an iterative approach to check
795
+ * if the binary search tree (BST) is valid.
796
+ * @returns The `isBST` method is returning a boolean value, which indicates whether the binary
797
+ * search tree (BST) represented by the given root node is a valid BST or not. The method checks if
798
+ * the tree satisfies the BST property, where for every node, all nodes in its left subtree have keys
799
+ * less than the node's key, and all nodes in its right subtree have keys greater than the node's
909
800
  */
910
801
  isBST(
911
- beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
802
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
912
803
  iterationType: IterationType = this.iterationType
913
804
  ): boolean {
914
805
  // TODO there is a bug
@@ -955,19 +846,21 @@ export class BinaryTree<
955
846
  * Time Complexity: O(n)
956
847
  * Space Complexity: O(1)
957
848
  *
958
- * The function calculates the depth of a given node or key in a tree-like data structure.
959
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} dist - The `dist` parameter can be either a `R`
960
- * (representing a root node), or a `BTNKeyOrNodeOrEntry<K, V, NODE>` (representing a key, node, or
961
- * entry).
962
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is optional and
963
- * represents the starting point from which to calculate the depth. It can be either a reference to a
964
- * node in the tree or a key-value pair or an entry object. If not provided, the default value is
965
- * `this.root`, which refers to the root node
966
- * @returns the depth of a node in a tree structure.
849
+ * The `getDepth` function calculates the depth between two nodes in a binary tree.
850
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} dist - The `dist` parameter in the `getDepth`
851
+ * function represents the node or entry in a binary tree map, or a reference to a node in the tree.
852
+ * It is the target node for which you want to calculate the depth from the `beginRoot` node.
853
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
854
+ * `getDepth` function represents the starting point from which you want to calculate the depth of a
855
+ * given node or entry in a binary tree. If no specific starting point is provided, the default value
856
+ * for `beginRoot` is set to the root of the binary
857
+ * @returns The `getDepth` method returns the depth of a given node `dist` relative to the
858
+ * `beginRoot` node in a binary tree. If the `dist` node is not found in the path to the `beginRoot`
859
+ * node, it returns the depth of the `dist` node from the root of the tree.
967
860
  */
968
861
  getDepth(
969
- dist: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
970
- beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root
862
+ dist: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
863
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root
971
864
  ): number {
972
865
  let distEnsured = this.ensureNode(dist);
973
866
  const beginRootEnsured = this.ensureNode(beginRoot);
@@ -987,16 +880,20 @@ export class BinaryTree<
987
880
  * Space Complexity: O(1)
988
881
  *
989
882
  * The `getHeight` function calculates the maximum height of a binary tree using either a recursive
990
- * or iterative approach.
991
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
992
- * starting point for calculating the height of a tree. It can be either a root node (`R`), a key or
993
- * node or entry (`BTNKeyOrNodeOrEntry<K, V, NODE>`), or it defaults to the root of the current tree.
994
- * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
995
- * iteration used to calculate the height of the tree. It can have two possible values:
996
- * @returns the maximum height of the binary tree.
883
+ * or iterative approach in TypeScript.
884
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter is the starting
885
+ * point from which the height of the binary tree will be calculated. It can be a node in the binary
886
+ * tree or a reference to the root of the tree. If not provided, it defaults to the root of the
887
+ * binary tree data structure.
888
+ * @param {IterationType} iterationType - The `iterationType` parameter is used to determine the type
889
+ * of iteration to be performed while calculating the height of the binary tree. It can have two
890
+ * possible values:
891
+ * @returns The `getHeight` method returns the height of the binary tree starting from the specified
892
+ * root node. The height is calculated based on the maximum depth of the tree, considering either a
893
+ * recursive approach or an iterative approach depending on the `iterationType` parameter.
997
894
  */
998
895
  getHeight(
999
- beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
896
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
1000
897
  iterationType: IterationType = this.iterationType
1001
898
  ): number {
1002
899
  beginRoot = this.ensureNode(beginRoot);
@@ -1033,19 +930,21 @@ export class BinaryTree<
1033
930
  * Space Complexity: O(log n)
1034
931
  *
1035
932
  * The `getMinHeight` function calculates the minimum height of a binary tree using either a
1036
- * recursive or iterative approach.
1037
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
1038
- * starting point for calculating the minimum height of a tree. It can be either a root node (`R`), a
1039
- * key or node or entry (`BTNKeyOrNodeOrEntry<K, V, NODE>`), or it defaults to the root of the current
1040
- * tree.
1041
- * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
1042
- * iteration to be used when calculating the minimum height of the tree. It can have two possible
1043
- * values:
1044
- * @returns The function `getMinHeight` returns a number, which represents the minimum height of the
1045
- * binary tree.
933
+ * recursive or iterative approach in TypeScript.
934
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
935
+ * `getMinHeight` function represents the starting node from which the minimum height of the binary
936
+ * tree will be calculated. It is either a node in the binary tree or a reference to the root of the
937
+ * tree. If not provided, the default value is the root
938
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `getMinHeight` method
939
+ * specifies the type of iteration to use when calculating the minimum height of a binary tree. It
940
+ * can have two possible values:
941
+ * @returns The `getMinHeight` method returns the minimum height of the binary tree starting from the
942
+ * specified root node. The height is calculated based on the shortest path from the root node to a
943
+ * leaf node in the tree. The method uses either a recursive approach or an iterative approach (using
944
+ * a stack) based on the `iterationType` parameter.
1046
945
  */
1047
946
  getMinHeight(
1048
- beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
947
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
1049
948
  iterationType: IterationType = this.iterationType
1050
949
  ): number {
1051
950
  beginRoot = this.ensureNode(beginRoot);
@@ -1076,8 +975,8 @@ export class BinaryTree<
1076
975
  if (!this.isRealNode(node.right) || last === node.right) {
1077
976
  node = stack.pop();
1078
977
  if (this.isRealNode(node)) {
1079
- const leftMinHeight = this.isRealNode(node.left) ? (depths.get(node.left) ?? -1) : -1;
1080
- const rightMinHeight = this.isRealNode(node.right) ? (depths.get(node.right) ?? -1) : -1;
978
+ const leftMinHeight = this.isRealNode(node.left) ? depths.get(node.left)! : -1;
979
+ const rightMinHeight = this.isRealNode(node.right) ? depths.get(node.right)! : -1;
1081
980
  depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
1082
981
  last = node;
1083
982
  node = null;
@@ -1086,7 +985,7 @@ export class BinaryTree<
1086
985
  }
1087
986
  }
1088
987
 
1089
- return depths.get(beginRoot) ?? -1;
988
+ return depths.get(beginRoot)!;
1090
989
  }
1091
990
  }
1092
991
 
@@ -1094,27 +993,38 @@ export class BinaryTree<
1094
993
  * Time Complexity: O(log n)
1095
994
  * Space Complexity: O(log n)
1096
995
  *
1097
- * The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
1098
- * up to the root node, with an option to reverse the order of the nodes.
1099
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginNode - The `beginNode` parameter can be either of
1100
- * type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
1101
- * @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
1102
- * resulting path should be reversed or not. If `isReverse` is set to `true`, the path will be
1103
- * reversed before returning it. If `isReverse` is set to `false` or not provided, the path will
1104
- * @returns The function `getPathToRoot` returns an array of `NODE` objects.
996
+ * The function `getPathToRoot` in TypeScript retrieves the path from a given node to the root of a
997
+ * tree structure, applying a specified callback function along the way.
998
+ * @param {C} callback - The `callback` parameter is a function that is used to process each node in
999
+ * the path to the root. It is expected to be a function that takes a node as an argument and returns
1000
+ * a value based on that node. The return type of the callback function is determined by the generic
1001
+ * type `C
1002
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginNode - The `beginNode` parameter in the
1003
+ * `getPathToRoot` function can be either a key, a node, an entry, or any other value of type `R`.
1004
+ * @param [isReverse=true] - The `isReverse` parameter in the `getPathToRoot` function determines
1005
+ * whether the resulting path from the given `beginNode` to the root should be in reverse order or
1006
+ * not. If `isReverse` is set to `true`, the path will be reversed before being returned. If `is
1007
+ * @returns The function `getPathToRoot` returns an array of the return values of the callback
1008
+ * function `callback` applied to each node in the path from the `beginNode` to the root node. The
1009
+ * array is either in reverse order or in the original order based on the value of the `isReverse`
1010
+ * parameter.
1105
1011
  */
1106
- getPathToRoot(beginNode: R | BTNKeyOrNodeOrEntry<K, V, NODE>, isReverse = true): NODE[] {
1107
- const result: NODE[] = [];
1012
+ getPathToRoot<C extends BTNCallback<OptBTNOrNull<NODE>>>(
1013
+ callback: C = this._DEFAULT_BTN_CALLBACK as C,
1014
+ beginNode: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
1015
+ isReverse = true
1016
+ ): ReturnType<C>[] {
1017
+ const result: ReturnType<C>[] = [];
1108
1018
  let beginNodeEnsured = this.ensureNode(beginNode);
1109
1019
 
1110
1020
  if (!beginNodeEnsured) return result;
1111
1021
 
1112
1022
  while (beginNodeEnsured.parent) {
1113
1023
  // Array.push + Array.reverse is more efficient than Array.unshift
1114
- result.push(beginNodeEnsured);
1024
+ result.push(callback(beginNodeEnsured));
1115
1025
  beginNodeEnsured = beginNodeEnsured.parent;
1116
1026
  }
1117
- result.push(beginNodeEnsured);
1027
+ result.push(callback(beginNodeEnsured));
1118
1028
  return isReverse ? result.reverse() : result;
1119
1029
  }
1120
1030
 
@@ -1125,23 +1035,23 @@ export class BinaryTree<
1125
1035
  * The function `getLeftMost` retrieves the leftmost node in a binary tree using either recursive or
1126
1036
  * tail-recursive iteration.
1127
1037
  * @param {C} callback - The `callback` parameter is a function that will be called with the leftmost
1128
- * node of a binary tree or null if the tree is empty. It has a default value of `_DEFAULT_CALLBACK`
1129
- * if not provided explicitly.
1130
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter in the
1038
+ * node of a binary tree or with `undefined` if the tree is empty. It is provided with a default
1039
+ * value of `_DEFAULT_BTN_CALLBACK` if not specified.
1040
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
1131
1041
  * `getLeftMost` function represents the starting point for finding the leftmost node in a binary
1132
- * tree. It can be either a reference to the root node of the tree (`R`), or a key, node, or entry in
1133
- * the binary tree structure (`
1042
+ * tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
1043
+ * starting point is provided, the function will default
1134
1044
  * @param {IterationType} iterationType - The `iterationType` parameter in the `getLeftMost` function
1135
1045
  * specifies the type of iteration to be used when traversing the binary tree nodes. It can have two
1136
1046
  * possible values:
1137
1047
  * @returns The `getLeftMost` function returns the result of the callback function `C` applied to the
1138
- * leftmost node in the binary tree starting from the `beginRoot` node. If the `beginRoot` is `NIL`,
1139
- * it returns the result of the callback function applied to `undefined`. If the `beginRoot` is not a
1140
- * real node, it returns the result of the callback function applied
1048
+ * leftmost node in the binary tree starting from the `beginRoot` node. If the `beginRoot` node is
1049
+ * `NIL`, it returns the result of the callback function applied to `undefined`. If the `beginRoot`
1050
+ * node is not a real node, it returns the result of the callback
1141
1051
  */
1142
1052
  getLeftMost<C extends BTNCallback<OptBTNOrNull<NODE>>>(
1143
- callback: C = this._DEFAULT_CALLBACK as C,
1144
- beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
1053
+ callback: C = this._DEFAULT_BTN_CALLBACK as C,
1054
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
1145
1055
  iterationType: IterationType = this.iterationType
1146
1056
  ): ReturnType<C> {
1147
1057
  if (this.isNIL(beginRoot)) return callback(undefined);
@@ -1174,24 +1084,24 @@ export class BinaryTree<
1174
1084
  * The function `getRightMost` retrieves the rightmost node in a binary tree using either recursive
1175
1085
  * or iterative traversal methods.
1176
1086
  * @param {C} callback - The `callback` parameter is a function that will be called with the result
1177
- * of the operation. It has a generic type `C` which extends `BTNCallback<OptBTNOrNull<NODE>>`. The
1178
- * default value for `callback` is `this._DEFAULT_CALLBACK` if it is not provided.
1179
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter in the
1087
+ * of finding the rightmost node in a binary tree. It is of type `BTNCallback<OptBTNOrNull<NODE>>`,
1088
+ * which means it is a callback function that can accept either an optional binary tree node or null
1089
+ * as
1090
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
1180
1091
  * `getRightMost` function represents the starting point for finding the rightmost node in a binary
1181
- * tree. It can be either a reference to the root node of the tree (`this.root`) or a specific key,
1182
- * node, or entry in the tree. If
1092
+ * tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
1093
+ * starting point is provided, the function will default
1183
1094
  * @param {IterationType} iterationType - The `iterationType` parameter in the `getRightMost`
1184
- * function specifies the type of iteration to be used when finding the rightmost node in a binary
1185
- * tree. It can have two possible values:
1186
- * @returns The `getRightMost` function returns the result of the callback function `C` applied to
1187
- * the rightmost node in the binary tree. The rightmost node is found either through a recursive
1188
- * depth-first search (if `iterationType` is 'RECURSIVE') or through an indirect implementation of
1189
- * iteration using tail recursion optimization. The result of the callback function applied to the
1190
- * rightmost node is returned
1095
+ * function specifies the type of iteration to be used when traversing the binary tree nodes. It can
1096
+ * have two possible values:
1097
+ * @returns The `getRightMost` function returns the result of the callback function `C`, which is
1098
+ * passed as a parameter to the function. The callback function is called with the rightmost node in
1099
+ * the binary tree structure, determined based on the specified iteration type ('RECURSIVE' or
1100
+ * other).
1191
1101
  */
1192
1102
  getRightMost<C extends BTNCallback<OptBTNOrNull<NODE>>>(
1193
- callback: C = this._DEFAULT_CALLBACK as C,
1194
- beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
1103
+ callback: C = this._DEFAULT_BTN_CALLBACK as C,
1104
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
1195
1105
  iterationType: IterationType = this.iterationType
1196
1106
  ): ReturnType<C> {
1197
1107
  if (this.isNIL(beginRoot)) return callback(undefined);
@@ -1221,10 +1131,14 @@ export class BinaryTree<
1221
1131
  * Time Complexity: O(log n)
1222
1132
  * Space Complexity: O(1)
1223
1133
  *
1224
- * The function returns the predecessor node of a given node in a binary tree.
1225
- * @param {NODE} node - The parameter "node" is of type "NODE", which represents a node in a binary
1226
- * tree.
1227
- * @returns the predecessor node of the given node.
1134
+ * The function `getPredecessor` in TypeScript returns the predecessor node of a given node in a
1135
+ * binary tree.
1136
+ * @param {NODE} node - The `getPredecessor` function you provided seems to be attempting to find the
1137
+ * predecessor of a given node in a binary tree. However, there seems to be a logical issue in the
1138
+ * while loop condition that might cause an infinite loop.
1139
+ * @returns The `getPredecessor` function returns the predecessor node of the input `NODE` parameter.
1140
+ * If the left child of the input node exists, it traverses to the rightmost node of the left subtree
1141
+ * to find the predecessor. If the left child does not exist, it returns the input node itself.
1228
1142
  */
1229
1143
  getPredecessor(node: NODE): NODE {
1230
1144
  if (this.isRealNode(node.left)) {
@@ -1244,10 +1158,14 @@ export class BinaryTree<
1244
1158
  * Time Complexity: O(log n)
1245
1159
  * Space Complexity: O(1)
1246
1160
  *
1247
- * The function `getSuccessor` returns the next node in a binary tree given a current node.
1248
- * @param {K | NODE | null} [x] - The parameter `x` can be of type `K`, `NODE`, or `null`.
1249
- * @returns The function `getSuccessor` returns a `NODE` object if a successor exists, `null` if
1250
- * there is no successor, and `undefined` if the input `x` is not a valid node.
1161
+ * The function `getSuccessor` in TypeScript returns the next node in an in-order traversal of a
1162
+ * binary tree.
1163
+ * @param {K | NODE | null} [x] - The `getSuccessor` function takes a parameter `x`, which can be of
1164
+ * type `K`, `NODE`, or `null`.
1165
+ * @returns The `getSuccessor` function returns the successor node of the input node `x`. If `x` has
1166
+ * a right child, the function returns the leftmost node in the right subtree of `x`. If `x` does not
1167
+ * have a right child, the function traverses up the parent nodes until it finds a node that is not
1168
+ * the right child of its parent, and returns that node
1251
1169
  */
1252
1170
  getSuccessor(x?: K | NODE | null): OptBTNOrNull<NODE> {
1253
1171
  x = this.ensureNode(x);
@@ -1268,14 +1186,14 @@ export class BinaryTree<
1268
1186
  dfs<C extends BTNCallback<NODE>>(
1269
1187
  callback?: C,
1270
1188
  pattern?: DFSOrderPattern,
1271
- beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
1189
+ beginRoot?: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
1272
1190
  iterationType?: IterationType
1273
1191
  ): ReturnType<C>[];
1274
1192
 
1275
1193
  dfs<C extends BTNCallback<NODE | null>>(
1276
1194
  callback?: C,
1277
1195
  pattern?: DFSOrderPattern,
1278
- beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
1196
+ beginRoot?: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
1279
1197
  iterationType?: IterationType,
1280
1198
  includeNull?: boolean
1281
1199
  ): ReturnType<C>[];
@@ -1284,29 +1202,32 @@ export class BinaryTree<
1284
1202
  * Time complexity: O(n)
1285
1203
  * Space complexity: O(n)
1286
1204
  *
1287
- * The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback
1288
- * function on each node according to a specified pattern and iteration type.
1289
- * @param {C} callback - The `callback` parameter is a function that will be called for each node
1290
- * visited during the depth-first search. It takes a node as an argument and returns a value. The
1291
- * return type of the callback function is determined by the generic type `C`.
1292
- * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter determines the order in which the
1293
- * nodes are visited during the depth-first search. It can have one of the following values:
1294
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
1295
- * point of the depth-first search. It can be either a node object, a key-value pair, or a key. If it
1296
- * is a key or key-value pair, the method will find the corresponding node in the tree and start the
1297
- * search from there.
1298
- * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter determines the
1299
- * type of iteration to use during the depth-first search. It can have two possible values:
1300
- * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
1301
- * whether or not to include null values in the depth-first search traversal. If `includeNull` is set
1302
- * to `true`, null values will be included in the traversal. If `includeNull` is set to `false`, null
1303
- * values will
1304
- * @returns an array of the return types of the callback function.
1205
+ * The function `dfs` performs a depth-first search traversal on a binary tree structure based on the
1206
+ * specified parameters.
1207
+ * @param {C} callback - The `callback` parameter is a generic type `C` that extends the
1208
+ * `BTNCallback` interface with a type parameter of `OptBTNOrNull<NODE>`. It has a default value of
1209
+ * `this._DEFAULT_BTN_CALLBACK as C`.
1210
+ * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `dfs` method specifies the
1211
+ * order in which the Depth-First Search (DFS) algorithm should traverse the nodes in the tree. The
1212
+ * possible values for the `pattern` parameter are:
1213
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `dfs`
1214
+ * method is used to specify the starting point for the Depth-First Search traversal. It can be
1215
+ * either a `BTNKeyOrNodeOrEntry` object representing a key, node, or entry in the binary tree map,
1216
+ * or it can be a
1217
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `dfs` method specifies
1218
+ * the type of iteration to be performed during the depth-first search traversal. It is used to
1219
+ * determine the order in which nodes are visited during the traversal.
1220
+ * @param [includeNull=false] - The `includeNull` parameter in the `dfs` method is a boolean flag
1221
+ * that determines whether null values should be included in the traversal or not. If `includeNull`
1222
+ * is set to `true`, then null values will be included in the traversal process. If it is set to
1223
+ * `false`,
1224
+ * @returns The `dfs` method is returning an array of the return type specified by the generic type
1225
+ * parameter `C`. The return type is determined by the callback function provided to the method.
1305
1226
  */
1306
1227
  dfs<C extends BTNCallback<OptBTNOrNull<NODE>>>(
1307
- callback: C = this._DEFAULT_CALLBACK as C,
1228
+ callback: C = this._DEFAULT_BTN_CALLBACK as C,
1308
1229
  pattern: DFSOrderPattern = 'IN',
1309
- beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
1230
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
1310
1231
  iterationType: IterationType = this.iterationType,
1311
1232
  includeNull = false
1312
1233
  ): ReturnType<C>[] {
@@ -1317,14 +1238,14 @@ export class BinaryTree<
1317
1238
 
1318
1239
  bfs<C extends BTNCallback<NODE>>(
1319
1240
  callback?: C,
1320
- beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
1241
+ beginRoot?: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
1321
1242
  iterationType?: IterationType,
1322
1243
  includeNull?: false
1323
1244
  ): ReturnType<C>[];
1324
1245
 
1325
1246
  bfs<C extends BTNCallback<NODE | null>>(
1326
1247
  callback?: C,
1327
- beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
1248
+ beginRoot?: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
1328
1249
  iterationType?: IterationType,
1329
1250
  includeNull?: true
1330
1251
  ): ReturnType<C>[];
@@ -1333,27 +1254,28 @@ export class BinaryTree<
1333
1254
  * Time complexity: O(n)
1334
1255
  * Space complexity: O(n)
1335
1256
  *
1336
- * The `bfs` function performs a breadth-first search on a binary tree, calling a callback function
1337
- * on each node and returning an array of the results.
1338
- * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1339
- * the breadth-first search traversal. It takes a single argument, which is the current node being
1340
- * visited, and returns a value of any type.
1341
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
1342
- * starting point of the breadth-first search. It can be either a root node of a tree or a key, node,
1343
- * or entry object. If no value is provided, the `root` property of the class is used as the default
1344
- * starting point.
1345
- * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
1346
- * iteration to be performed. It can have two possible values:
1347
- * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
1348
- * whether or not to include null values in the breadth-first search (BFS) traversal. If
1349
- * `includeNull` is set to `true`, null values will be included in the traversal. If `includeNull` is
1350
- * set to `false
1351
- * @returns The function `bfs` returns an array of values that are the result of invoking the
1352
- * `callback` function on each node in the breadth-first order traversal of the binary tree.
1257
+ * The `bfs` function performs a breadth-first search traversal on a binary tree or binary search
1258
+ * tree, executing a specified callback function on each node visited.
1259
+ * @param {C} callback - The `callback` parameter in the `bfs` function is a function that will be
1260
+ * called on each node visited during the breadth-first search traversal. It is a generic type `C`
1261
+ * that extends the `BTNCallback` type, which takes a parameter of type `NODE` or `null`.
1262
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `bfs`
1263
+ * function represents the starting point for the breadth-first search traversal in a binary tree. It
1264
+ * can be specified as a key, node, or entry in the binary tree structure. If not provided, the
1265
+ * default value is the root node of the binary
1266
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `bfs` function
1267
+ * determines the type of iteration to be performed on the binary tree nodes. It can have two
1268
+ * possible values:
1269
+ * @param [includeNull=false] - The `includeNull` parameter in the `bfs` function determines whether
1270
+ * to include `null` values in the breadth-first search traversal of a binary tree. If `includeNull`
1271
+ * is set to `true`, the traversal will include `null` values for nodes that do not have children
1272
+ * (left
1273
+ * @returns The `bfs` function returns an array of values that are the result of applying the
1274
+ * provided callback function to each node in the binary tree in a breadth-first search manner.
1353
1275
  */
1354
1276
  bfs<C extends BTNCallback<NODE | null>>(
1355
- callback: C = this._DEFAULT_CALLBACK as C,
1356
- beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
1277
+ callback: C = this._DEFAULT_BTN_CALLBACK as C,
1278
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
1357
1279
  iterationType: IterationType = this.iterationType,
1358
1280
  includeNull = false
1359
1281
  ): ReturnType<C>[] {
@@ -1409,39 +1331,37 @@ export class BinaryTree<
1409
1331
  * Time complexity: O(n)
1410
1332
  * Space complexity: O(n)
1411
1333
  *
1412
- * The `leaves` function in TypeScript iterates through a binary tree to find and return the leaf
1413
- * nodes based on a specified callback and iteration type.
1334
+ * The `leaves` function in TypeScript returns an array of values from leaf nodes in a binary tree
1335
+ * structure based on a specified callback and iteration type.
1414
1336
  * @param {C} callback - The `callback` parameter is a function that will be called on each leaf node
1415
- * in the binary tree. It is a generic type `C` that extends `BTNCallback<NODE | null>`, where `NODE`
1416
- * represents a node in the binary tree. The default value for `callback` is
1417
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter in the `leaves`
1337
+ * in the binary tree. It is optional and defaults to a default callback function if not provided.
1338
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `leaves`
1418
1339
  * method is used to specify the starting point for finding and processing the leaves of a binary
1419
- * tree. It represents the root node of the binary tree or a specific key, node, or entry within the
1420
- * tree from which the search for leaves should begin
1340
+ * tree. It can be provided as either a key, a node, or an entry in the binary tree structure. If not
1341
+ * explicitly provided, the default value
1421
1342
  * @param {IterationType} iterationType - The `iterationType` parameter in the `leaves` method
1422
1343
  * specifies the type of iteration to be performed when collecting the leaves of a binary tree. It
1423
1344
  * can have two possible values:
1424
1345
  * @returns The `leaves` method returns an array of values that are the result of applying the
1425
- * provided callback function to the leaf nodes in the binary tree structure.
1346
+ * provided callback function to each leaf node in the binary tree.
1426
1347
  */
1427
1348
  leaves<C extends BTNCallback<NODE | null>>(
1428
- callback: C = this._DEFAULT_CALLBACK as C,
1429
- beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
1349
+ callback: C = this._DEFAULT_BTN_CALLBACK as C,
1350
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
1430
1351
  iterationType: IterationType = this.iterationType
1431
1352
  ): ReturnType<C>[] {
1432
1353
  beginRoot = this.ensureNode(beginRoot);
1433
1354
  const leaves: ReturnType<BTNCallback<NODE>>[] = [];
1434
- if (!this.isRealNode(beginRoot)) {
1435
- return [];
1436
- }
1355
+ if (!this.isRealNode(beginRoot)) return [];
1356
+
1437
1357
  if (iterationType === 'RECURSIVE') {
1438
1358
  const dfs = (cur: NODE) => {
1439
1359
  if (this.isLeaf(cur)) {
1440
1360
  leaves.push(callback(cur));
1441
1361
  }
1442
1362
  if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
1443
- this.isRealNode(cur.left) && dfs(cur.left);
1444
- this.isRealNode(cur.right) && dfs(cur.right);
1363
+ if (this.isRealNode(cur.left)) dfs(cur.left);
1364
+ if (this.isRealNode(cur.right)) dfs(cur.right);
1445
1365
  };
1446
1366
 
1447
1367
  dfs(beginRoot);
@@ -1453,8 +1373,8 @@ export class BinaryTree<
1453
1373
  if (this.isLeaf(cur)) {
1454
1374
  leaves.push(callback(cur));
1455
1375
  }
1456
- this.isRealNode(cur.left) && queue.push(cur.left);
1457
- this.isRealNode(cur.right) && queue.push(cur.right);
1376
+ if (this.isRealNode(cur.left)) queue.push(cur.left);
1377
+ if (this.isRealNode(cur.right)) queue.push(cur.right);
1458
1378
  }
1459
1379
  }
1460
1380
  }
@@ -1464,14 +1384,14 @@ export class BinaryTree<
1464
1384
 
1465
1385
  listLevels<C extends BTNCallback<NODE>>(
1466
1386
  callback?: C,
1467
- beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
1387
+ beginRoot?: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
1468
1388
  iterationType?: IterationType,
1469
1389
  includeNull?: false
1470
1390
  ): ReturnType<C>[][];
1471
1391
 
1472
1392
  listLevels<C extends BTNCallback<NODE | null>>(
1473
1393
  callback?: C,
1474
- beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
1394
+ beginRoot?: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
1475
1395
  iterationType?: IterationType,
1476
1396
  includeNull?: true
1477
1397
  ): ReturnType<C>[][];
@@ -1480,26 +1400,29 @@ export class BinaryTree<
1480
1400
  * Time complexity: O(n)
1481
1401
  * Space complexity: O(n)
1482
1402
  *
1483
- * The `listLevels` function returns an array of arrays, where each inner array represents a level in
1484
- * a binary tree and contains the results of applying a callback function to the nodes at that level.
1485
- * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1486
- * the tree. It takes a node as an argument and returns a value. The return type of the callback
1487
- * function is determined by the generic type `C` which extends `BTNCallback<NODE | null>`.
1488
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
1489
- * starting point for traversing the tree. It can be either a root node, a key-value pair, or a node
1490
- * entry. If no value is provided, the `root` property of the class is used as the default starting
1491
- * point.
1492
- * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
1493
- * iteration to be performed on the binary tree. It can have two possible values:
1494
- * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
1495
- * whether or not to include null values in the resulting levels. If `includeNull` is set to `true`,
1496
- * null values will be included in the levels. If `includeNull` is set to `false`, null values will
1497
- * be excluded
1498
- * @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
1403
+ * The `listLevels` function in TypeScript generates a list of nodes at each level of a binary tree,
1404
+ * using either recursive or iterative traversal based on the specified iteration type.
1405
+ * @param {C} callback - The `callback` parameter is a function that will be applied to each node in
1406
+ * the binary tree during the traversal. It is used to process each node and determine what
1407
+ * information to include in the output for each level of the tree.
1408
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
1409
+ * `listLevels` function represents the starting point for traversing the binary tree. It can be
1410
+ * either a key, a node, or an entry in the binary tree. If not provided, the default value is the
1411
+ * root of the binary tree.
1412
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `listLevels` function
1413
+ * determines the type of iteration to be performed on the binary tree nodes. It can have two
1414
+ * possible values:
1415
+ * @param [includeNull=false] - The `includeNull` parameter in the `listLevels` method determines
1416
+ * whether or not to include null nodes in the traversal of the binary tree. If `includeNull` is set
1417
+ * to `true`, the traversal will include null nodes in the levels of the tree. If set to `false`,
1418
+ * null
1419
+ * @returns The `listLevels` method returns an array of arrays, where each inner array represents a
1420
+ * level in a binary tree. Each inner array contains the return value of the provided callback
1421
+ * function applied to the nodes at that level.
1499
1422
  */
1500
1423
  listLevels<C extends BTNCallback<NODE | null>>(
1501
- callback: C = this._DEFAULT_CALLBACK as C,
1502
- beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
1424
+ callback: C = this._DEFAULT_BTN_CALLBACK as C,
1425
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
1503
1426
  iterationType: IterationType = this.iterationType,
1504
1427
  includeNull = false
1505
1428
  ): ReturnType<C>[][] {
@@ -1548,28 +1471,29 @@ export class BinaryTree<
1548
1471
  * Time complexity: O(n)
1549
1472
  * Space complexity: O(n)
1550
1473
  *
1551
- * The `morris` function performs a depth-first traversal on a binary tree using the Morris traversal
1552
- * algorithm.
1553
- * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1554
- * the tree. It takes a single argument, which is the current node, and can return any value. The
1555
- * return type of the `callback` function is determined by the `ReturnType<C>` type, which represents
1556
- * the return
1557
- * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function is used
1558
- * to specify the order in which the nodes of a binary tree are traversed. It can take one of the
1559
- * following values:
1560
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
1561
- * point for the traversal. It can be either a node object, a key, or an entry object. If no value is
1562
- * provided, the `root` of the tree is used as the starting point.
1563
- * @returns The function `morris` returns an array of values that are the return values of the
1564
- * callback function `callback`.
1474
+ * The `morris` function in TypeScript performs a Depth-First Search traversal on a binary tree using
1475
+ * Morris Traversal algorithm with different order patterns.
1476
+ * @param {C} callback - The `callback` parameter in the `morris` function is a function that will be
1477
+ * called on each node in the binary tree during the traversal. It is of type `C`, which extends the
1478
+ * `BTNCallback<NODE>` type. The default value for `callback` is `this._DEFAULT
1479
+ * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function specifies
1480
+ * the type of Depth-First Search (DFS) order pattern to traverse the binary tree. The possible
1481
+ * values for the `pattern` parameter are:
1482
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `morris`
1483
+ * function is the starting point for the Morris traversal algorithm. It represents the root node of
1484
+ * the binary tree or the node from which the traversal should begin. It can be provided as either a
1485
+ * key, a node, an entry, or a reference
1486
+ * @returns The `morris` function is returning an array of values that are the result of applying the
1487
+ * provided callback function to each node in the binary tree in the specified order pattern (IN,
1488
+ * PRE, or POST).
1565
1489
  */
1566
1490
  morris<C extends BTNCallback<NODE>>(
1567
- callback: C = this._DEFAULT_CALLBACK as C,
1491
+ callback: C = this._DEFAULT_BTN_CALLBACK as C,
1568
1492
  pattern: DFSOrderPattern = 'IN',
1569
- beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root
1493
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root
1570
1494
  ): ReturnType<C>[] {
1571
1495
  beginRoot = this.ensureNode(beginRoot);
1572
- if (beginRoot === null) return [];
1496
+ if (!beginRoot) return [];
1573
1497
  const ans: ReturnType<BTNCallback<NODE>>[] = [];
1574
1498
 
1575
1499
  let cur: OptBTNOrNull<NODE> = beginRoot;
@@ -1653,8 +1577,12 @@ export class BinaryTree<
1653
1577
  * Time complexity: O(n)
1654
1578
  * Space complexity: O(n)
1655
1579
  *
1656
- * The `clone` function creates a deep copy of a tree object.
1657
- * @returns The `clone()` method is returning a cloned instance of the `TREE` object.
1580
+ * The `clone` function creates a deep copy of a tree structure by traversing it using breadth-first
1581
+ * search.
1582
+ * @returns The `clone()` method is returning a cloned copy of the tree with the same structure and
1583
+ * values as the original tree. The method creates a new tree, iterates over the nodes of the
1584
+ * original tree using breadth-first search (bfs), and adds the nodes to the new tree. If a node in
1585
+ * the original tree is null, a null node is added to the cloned tree. If a node
1658
1586
  */
1659
1587
  clone(): TREE {
1660
1588
  const cloned = this.createTree();
@@ -1663,7 +1591,7 @@ export class BinaryTree<
1663
1591
  if (node === null) cloned.add(null);
1664
1592
  else cloned.add([node.key, node.value]);
1665
1593
  },
1666
- this.root,
1594
+ this._root,
1667
1595
  this.iterationType,
1668
1596
  true
1669
1597
  );
@@ -1674,16 +1602,17 @@ export class BinaryTree<
1674
1602
  * Time Complexity: O(n)
1675
1603
  * Space Complexity: O(n)
1676
1604
  *
1677
- * The `filter` function creates a new tree with entries that pass a given predicate function.
1678
- * @param predicate - The `predicate` parameter is a callback function that is used to test each
1679
- * element in the tree. It takes three arguments: `value`, `key`, and `index`. The `value` argument
1680
- * represents the value of the current element being processed, the `key` argument represents the key
1681
- * of the
1682
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
1683
- * specify the value of `this` within the `predicate` function. When the `predicate` function is
1684
- * called, `thisArg` will be used as the value of `this` within the function. If `thisArg`
1685
- * @returns The `filter` method is returning a new tree object that contains the entries that pass
1686
- * the given predicate function.
1605
+ * The `filter` function iterates over key-value pairs in a tree data structure and creates a new
1606
+ * tree with elements that satisfy a given predicate.
1607
+ * @param predicate - The `predicate` parameter in the `filter` method is a function that will be
1608
+ * called with four arguments: the `value` of the current entry, the `key` of the current entry, the
1609
+ * `index` of the current entry in the iteration, and the reference to the tree itself (`
1610
+ * @param {any} [thisArg] - The `thisArg` parameter in the `filter` method allows you to specify the
1611
+ * value of `this` that should be used when executing the `predicate` function. This is useful when
1612
+ * the `predicate` function relies on the context of a specific object or value. By providing a
1613
+ * `thisArg
1614
+ * @returns The `filter` method is returning a new tree that contains entries that pass the provided
1615
+ * predicate function.
1687
1616
  */
1688
1617
  filter(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: any) {
1689
1618
  const newTree = this.createTree();
@@ -1700,16 +1629,16 @@ export class BinaryTree<
1700
1629
  * Time Complexity: O(n)
1701
1630
  * Space Complexity: O(n)
1702
1631
  *
1703
- * The `map` function creates a new tree by applying a callback function to each entry in the current
1704
- * tree.
1705
- * @param callback - The callback parameter is a function that will be called for each entry in the
1706
- * tree. It takes three arguments: value, key, and index. The value argument represents the value of
1707
- * the current entry, the key argument represents the key of the current entry, and the index
1708
- * argument represents the index of the
1709
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
1710
- * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
1711
- * passed as the `this` value to the `callback` function. If `thisArg` is
1712
- * @returns The `map` method is returning a new tree object.
1632
+ * The `map` function iterates over key-value pairs in a tree data structure, applies a callback
1633
+ * function to each value, and returns a new tree with the updated values.
1634
+ * @param callback - The `callback` parameter in the `map` method is a function that will be called
1635
+ * on each entry in the tree. It takes four arguments:
1636
+ * @param {any} [thisArg] - The `thisArg` parameter in the `map` function is an optional parameter
1637
+ * that specifies the value to be passed as `this` when executing the callback function. If provided,
1638
+ * the `thisArg` value will be used as the `this` value within the callback function. If `thisArg
1639
+ * @returns The `map` method is returning a new tree with the entries modified by the provided
1640
+ * callback function. Each entry in the original tree is passed to the callback function, and the
1641
+ * result of the callback function is added to the new tree.
1713
1642
  */
1714
1643
  map(callback: EntryCallback<K, V | undefined, V>, thisArg?: any) {
1715
1644
  const newTree = this.createTree();
@@ -1734,31 +1663,32 @@ export class BinaryTree<
1734
1663
  * Time Complexity: O(n)
1735
1664
  * Space Complexity: O(n)
1736
1665
  *
1737
- * The `print` function in TypeScript prints the binary tree structure with customizable options.
1738
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
1739
- * point for printing the binary tree. It can be either a node of the binary tree or a key or entry
1740
- * that exists in the binary tree. If no value is provided, the root of the binary tree will be used
1741
- * as the starting point.
1742
- * @param {BinaryTreePrintOptions} [options] - The `options` parameter is an optional object that
1743
- * allows you to customize the printing behavior. It has the following properties:
1744
- * @returns Nothing is being returned. The function has a return type of `void`, which means it does
1745
- * not return any value.
1666
+ * The function `toVisual` in TypeScript overrides the visual representation of a binary tree with
1667
+ * customizable options for displaying undefined, null, and sentinel nodes.
1668
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
1669
+ * `toVisual` method is used to specify the starting point for visualizing the binary tree structure.
1670
+ * It can be a node, key, entry, or the root of the tree. If no specific starting point is provided,
1671
+ * the default is set to the root
1672
+ * @param {BinaryTreePrintOptions} [options] - The `options` parameter in the `toVisual` method is an
1673
+ * object that contains the following properties:
1674
+ * @returns The `override toVisual` method returns a string that represents the visual display of the
1675
+ * binary tree based on the provided options for showing undefined, null, and Red-Black NIL nodes.
1676
+ * The method constructs the visual representation by calling the `_displayAux` method and appending
1677
+ * the lines to the output string. The final output string contains the visual representation of the
1678
+ * binary tree with the specified options.
1746
1679
  */
1747
- override print(beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root, options?: BinaryTreePrintOptions): string {
1748
- const opts = { isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false, ...options };
1680
+ override toVisual(
1681
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
1682
+ options?: BinaryTreePrintOptions
1683
+ ): string {
1684
+ const opts = { isShowUndefined: false, isShowNull: true, isShowRedBlackNIL: false, ...options };
1749
1685
  beginRoot = this.ensureNode(beginRoot);
1750
1686
  let output = '';
1751
1687
  if (!beginRoot) return output;
1752
1688
 
1753
- if (opts.isShowUndefined)
1754
- output += `U for undefined
1755
- `;
1756
- if (opts.isShowNull)
1757
- output += `N for null
1758
- `;
1759
- if (opts.isShowRedBlackNIL)
1760
- output += `S for Sentinel Node(NIL)
1761
- `;
1689
+ if (opts.isShowUndefined) output += `U for undefined\n`;
1690
+ if (opts.isShowNull) output += `N for null\n`;
1691
+ if (opts.isShowRedBlackNIL) output += `S for Sentinel Node(NIL)\n`;
1762
1692
 
1763
1693
  const display = (root: OptBTNOrNull<NODE>): void => {
1764
1694
  const [lines, , ,] = this._displayAux(root, opts);
@@ -1773,68 +1703,71 @@ export class BinaryTree<
1773
1703
  return output;
1774
1704
  }
1775
1705
 
1776
- protected _dfs<C extends BTNCallback<NODE>>(
1777
- callback?: C,
1778
- pattern?: DFSOrderPattern,
1779
- beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
1780
- iterationType?: IterationType
1781
- ): ReturnType<C>[];
1782
-
1783
- protected _dfs<C extends BTNCallback<NODE | null>>(
1784
- callback?: C,
1785
- pattern?: DFSOrderPattern,
1786
- beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
1787
- iterationType?: IterationType,
1788
- includeNull?: boolean
1789
- ): ReturnType<C>[];
1706
+ /**
1707
+ * Time Complexity: O(n)
1708
+ * Space Complexity: O(n)
1709
+ *
1710
+ * The function `print` in TypeScript overrides the default print behavior to log a visual
1711
+ * representation of the binary tree to the console.
1712
+ * @param {BinaryTreePrintOptions} [options] - The `options` parameter is used to specify the
1713
+ * printing options for the binary tree. It is an optional parameter that allows you to customize how
1714
+ * the binary tree is printed, such as choosing between different traversal orders or formatting
1715
+ * options.
1716
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
1717
+ * `override print` method is used to specify the starting point for printing the binary tree. It can
1718
+ * be either a key, a node, an entry, or the root of the tree. If no specific starting point is
1719
+ * provided, the default value is set to
1720
+ */
1721
+ override print(options?: BinaryTreePrintOptions, beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root) {
1722
+ console.log(this.toVisual(beginRoot, options));
1723
+ }
1790
1724
 
1791
1725
  /**
1792
1726
  * Time complexity: O(n)
1793
1727
  * Space complexity: O(n)
1794
1728
  *
1795
- * The function `_dfs` performs a depth-first search traversal on a binary tree structure based on
1729
+ * The `_dfs` function performs a depth-first search traversal on a binary tree structure based on
1796
1730
  * the specified order pattern and callback function.
1797
- * @param {C} callback - The `callback` parameter is a function that will be called on each node
1798
- * visited during the depth-first search. It is of type `C`, which extends
1799
- * `BTNCallback<OptBTNOrNull<NODE>>`. The default value is set to `this._DEFAULT_CALLBACK` if not
1800
- * provided.
1731
+ * @param {C} callback - The `callback` parameter in the `_dfs` method is a function that will be
1732
+ * called on each node visited during the depth-first search traversal. It is of type `C`, which
1733
+ * extends `BTNCallback<OptBTNOrNull<NODE>>`. The default value for this parameter is `this._DEFAULT
1801
1734
  * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `_dfs` method specifies the
1802
- * order in which the Depth-First Search (DFS) algorithm should traverse the nodes in a binary tree.
1803
- * It can have one of the following values:
1804
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter in the `_dfs`
1735
+ * order in which the nodes are visited during the Depth-First Search traversal. It can have one of
1736
+ * the following values:
1737
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `_dfs`
1805
1738
  * method is used to specify the starting point for the depth-first search traversal in a binary
1806
- * tree. It can be provided as either the root node of the tree or a key, node, or entry that exists
1807
- * in the tree. If no specific `
1739
+ * tree. It can be provided as either a `BTNKeyOrNodeOrEntry` object or a reference to the root node
1740
+ * of the tree. If no specific
1808
1741
  * @param {IterationType} iterationType - The `iterationType` parameter in the `_dfs` method
1809
- * specifies the type of iteration to be performed during the Depth-First Search (DFS) traversal. It
1810
- * can have two possible values:
1742
+ * specifies the type of iteration to be performed during the Depth-First Search (DFS) traversal of a
1743
+ * binary tree. It can have two possible values:
1811
1744
  * @param [includeNull=false] - The `includeNull` parameter in the `_dfs` method is a boolean flag
1812
1745
  * that determines whether null nodes should be included in the depth-first search traversal. If
1813
- * `includeNull` is set to `true`, the traversal will consider null nodes as valid nodes to visit and
1814
- * process. If set to `
1746
+ * `includeNull` is set to `true`, null nodes will be considered during the traversal process. If it
1747
+ * is set to `false`,
1815
1748
  * @param shouldVisitLeft - The `shouldVisitLeft` parameter is a function that takes a node as input
1816
1749
  * and returns a boolean value. It is used to determine whether the left child of a node should be
1817
1750
  * visited during the depth-first search traversal. By default, it checks if the node is truthy (not
1818
1751
  * null or undefined
1819
- * @param shouldVisitRight - The `shouldVisitRight` parameter is a function that takes a node as
1820
- * input and returns a boolean value. It is used to determine whether the right child of a node
1752
+ * @param shouldVisitRight - The `shouldVisitRight` parameter is a function that takes a node as an
1753
+ * argument and returns a boolean value. It is used to determine whether the right child of a node
1821
1754
  * should be visited during the depth-first search traversal. The default implementation checks if
1822
- * the node is truthy before visiting the right child.
1755
+ * the node is truthy before visiting the right child
1823
1756
  * @param shouldVisitRoot - The `shouldVisitRoot` parameter is a function that takes a node as an
1824
- * argument and returns a boolean value. It is used to determine whether a given node should be
1757
+ * argument and returns a boolean value. It is used to determine whether the root node should be
1825
1758
  * visited during the depth-first search traversal based on certain conditions. The default
1826
1759
  * implementation checks if the node is a real node or null based
1827
- * @param shouldProcessRoot - The `shouldProcessRoot` parameter is a function that takes a node as
1828
- * input and returns a boolean value indicating whether the node should be processed during the
1829
- * depth-first search traversal. The default implementation of this function simply returns `true`,
1830
- * meaning that by default all nodes will be processed. However, you can
1831
- * @returns The `_dfs` method returns an array of the return type of the callback function provided
1760
+ * @param shouldProcessRoot - The `shouldProcessRoot` parameter is a function that takes a node as an
1761
+ * argument and returns a boolean value indicating whether the node should be processed during the
1762
+ * depth-first search traversal. The default implementation checks if the node is a real node or null
1763
+ * based on the `includeNull` flag. If `
1764
+ * @returns The function `_dfs` returns an array of the return type of the callback function provided
1832
1765
  * as input.
1833
1766
  */
1834
1767
  protected _dfs<C extends BTNCallback<OptBTNOrNull<NODE>>>(
1835
- callback: C = this._DEFAULT_CALLBACK as C,
1768
+ callback: C = this._DEFAULT_BTN_CALLBACK as C,
1836
1769
  pattern: DFSOrderPattern = 'IN',
1837
- beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
1770
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
1838
1771
  iterationType: IterationType = this.iterationType,
1839
1772
  includeNull = false,
1840
1773
  shouldVisitLeft: (node: OptBTNOrNull<NODE>) => boolean = node => !!node,
@@ -1928,13 +1861,18 @@ export class BinaryTree<
1928
1861
  * Time Complexity: O(1)
1929
1862
  * Space Complexity: O(1)
1930
1863
  *
1931
- * The function `_getIterator` is a generator function that returns an iterator for the key-value
1932
- * pairs in a binary search tree.
1933
- * @param node - The `node` parameter represents the current node in the binary search tree. It is
1934
- * initially set to the root node of the tree.
1935
- * @returns an IterableIterator<[K, V | undefined]>.
1864
+ * The function `_getIterator` returns an iterable iterator for a binary tree data structure, either
1865
+ * using an iterative approach or a recursive approach based on the specified iteration type.
1866
+ * @param node - The `node` parameter in the `_getIterator` method represents the current node being
1867
+ * processed during iteration. It is initially set to the root node of the data structure (or the
1868
+ * node passed as an argument), and then it is traversed through the data structure based on the
1869
+ * iteration type specified (`ITER
1870
+ * @returns The `_getIterator` method returns an IterableIterator containing key-value pairs of nodes
1871
+ * in a binary tree structure. The method uses an iterative approach to traverse the tree based on
1872
+ * the `iterationType` property. If the `iterationType` is set to 'ITERATIVE', the method uses a
1873
+ * stack to perform an in-order traversal of the tree. If the `iterationType` is not 'ITERATIVE
1936
1874
  */
1937
- protected *_getIterator(node = this.root): IterableIterator<[K, V | undefined]> {
1875
+ protected *_getIterator(node = this._root): IterableIterator<[K, V | undefined]> {
1938
1876
  if (!node) return;
1939
1877
 
1940
1878
  if (this.iterationType === 'ITERATIVE') {
@@ -1969,18 +1907,16 @@ export class BinaryTree<
1969
1907
  * Time Complexity: O(n)
1970
1908
  * Space Complexity: O(n)
1971
1909
  *
1972
- * The `_displayAux` function is responsible for generating the display layout of a binary tree node,
1973
- * taking into account various options such as whether to show null, undefined, or NaN nodes.
1974
- * @param {OptBTNOrNull<NODE>} node - The `node` parameter represents a node in a binary tree.
1975
- * It can be of type `NODE`, `null`, or `undefined`.
1976
- * @param {BinaryTreePrintOptions} options - The `options` parameter is an object that contains the
1977
- * following properties:
1978
- * @returns The function `_displayAux` returns a `NodeDisplayLayout` which is an array containing the
1979
- * following elements:
1980
- * 1. `mergedLines`: An array of strings representing the lines of the node display.
1981
- * 2. `totalWidth`: The total width of the node display.
1982
- * 3. `totalHeight`: The total height of the node display.
1983
- * 4. `middleIndex`: The index of the middle character
1910
+ * The function `_displayAux` in TypeScript is responsible for generating the display layout of nodes
1911
+ * in a binary tree based on specified options.
1912
+ * @param node - The `node` parameter in the `_displayAux` function represents a node in a binary
1913
+ * tree. It can be either a valid node containing a key or a special type of node like null,
1914
+ * undefined, or a Red-Black tree NIL node. The function checks the type of the node and its
1915
+ * @param {BinaryTreePrintOptions} options - The `options` parameter in the `_displayAux` function
1916
+ * contains the following properties:
1917
+ * @returns The `_displayAux` function returns a `NodeDisplayLayout`, which is an array containing
1918
+ * information about how to display a node in a binary tree. The `NodeDisplayLayout` consists of four
1919
+ * elements:
1984
1920
  */
1985
1921
  protected _displayAux(node: OptBTNOrNull<NODE>, options: BinaryTreePrintOptions): NodeDisplayLayout {
1986
1922
  const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
@@ -2050,24 +1986,26 @@ export class BinaryTree<
2050
1986
  }
2051
1987
  }
2052
1988
 
2053
- protected _DEFAULT_CALLBACK = (node: OptBTNOrNull<NODE>) => (node ? node.key : undefined);
1989
+ protected _DEFAULT_BTN_CALLBACK = (node: OptBTNOrNull<NODE>) => (node ? node.key : undefined);
2054
1990
 
2055
1991
  /**
2056
1992
  * Time Complexity: O(1)
2057
1993
  * Space Complexity: O(1)
2058
1994
  *
2059
- * The function `_swapProperties` swaps the key-value properties between two nodes.
2060
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} srcNode - The source node that will be swapped with the
2061
- * destination node. It can be either an instance of the class `R`, or an object of type
2062
- * `BTNKeyOrNodeOrEntry<K, V, NODE>`.
2063
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} destNode - The `destNode` parameter is the node where
2064
- * the properties will be swapped with the `srcNode`.
2065
- * @returns either the `destNode` object with its properties swapped with the `srcNode` object's
2066
- * properties, or `undefined` if either `srcNode` or `destNode` is falsy.
1995
+ * The _swapProperties function swaps key and value properties between two nodes in a binary tree.
1996
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} srcNode - The `srcNode` parameter in the
1997
+ * `_swapProperties` method can be either a BTNKeyOrNodeOrEntry object containing key and value
1998
+ * properties, or it can be of type R.
1999
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} destNode - The `destNode` parameter in the
2000
+ * `_swapProperties` method represents the node or entry where the properties will be swapped with
2001
+ * the `srcNode`. It can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>` or `R`. The method ensures that
2002
+ * both `srcNode
2003
+ * @returns The `_swapProperties` method returns either the `destNode` with its key and value swapped
2004
+ * with the `srcNode`, or `undefined` if either `srcNode` or `destNode` is falsy.
2067
2005
  */
2068
2006
  protected _swapProperties(
2069
- srcNode: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
2070
- destNode: R | BTNKeyOrNodeOrEntry<K, V, NODE>
2007
+ srcNode: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
2008
+ destNode: BTNKeyOrNodeOrEntry<K, V, NODE> | R
2071
2009
  ): NODE | undefined {
2072
2010
  srcNode = this.ensureNode(srcNode);
2073
2011
  destNode = this.ensureNode(destNode);
@@ -2093,13 +2031,15 @@ export class BinaryTree<
2093
2031
  * Time Complexity: O(1)
2094
2032
  * Space Complexity: O(1)
2095
2033
  *
2096
- * The function replaces a node in a binary tree with a new node, updating the parent, left child,
2097
- * right child, and root if necessary.
2098
- * @param {NODE} oldNode - The oldNode parameter represents the node that needs to be replaced in the
2099
- * tree.
2100
- * @param {NODE} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
2101
- * tree.
2102
- * @returns the newNode.
2034
+ * The _replaceNode function replaces an old node with a new node in a binary tree structure.
2035
+ * @param {NODE} oldNode - The `oldNode` parameter represents the node that you want to replace in a
2036
+ * tree data structure.
2037
+ * @param {NODE} newNode - The `newNode` parameter in the `_replaceNode` function represents the node
2038
+ * that will replace the `oldNode` in a tree data structure. This function is responsible for
2039
+ * updating the parent, left child, right child, and root (if necessary) references when replacing a
2040
+ * node in the tree.
2041
+ * @returns The method `_replaceNode` is returning the `newNode` that was passed as a parameter after
2042
+ * replacing the `oldNode` with it in the binary tree structure.
2103
2043
  */
2104
2044
  protected _replaceNode(oldNode: NODE, newNode: NODE): NODE {
2105
2045
  if (oldNode.parent) {
@@ -2112,8 +2052,8 @@ export class BinaryTree<
2112
2052
  newNode.left = oldNode.left;
2113
2053
  newNode.right = oldNode.right;
2114
2054
  newNode.parent = oldNode.parent;
2115
- if (this.root === oldNode) {
2116
- this._root = newNode;
2055
+ if (this._root === oldNode) {
2056
+ this._setRoot(newNode);
2117
2057
  }
2118
2058
 
2119
2059
  return newNode;
@@ -2123,10 +2063,10 @@ export class BinaryTree<
2123
2063
  * Time Complexity: O(1)
2124
2064
  * Space Complexity: O(1)
2125
2065
  *
2126
- * The function sets the root property of an object to the provided value, and also updates the
2127
- * parent property of the new root.
2128
- * @param {OptBTNOrNull<NODE>} v - The parameter `v` is of type `OptBTNOrNull<NODE>`. This
2129
- * means that it can accept a value of type `NODE`, `null`, or `undefined`.
2066
+ * The function _setRoot sets the root node of a data structure while updating the parent reference
2067
+ * of the previous root node.
2068
+ * @param v - The parameter `v` in the `_setRoot` method is of type `OptBTNOrNull<NODE>`, which means
2069
+ * it can either be an optional `NODE` type or `null`.
2130
2070
  */
2131
2071
  protected _setRoot(v: OptBTNOrNull<NODE>) {
2132
2072
  if (v) {
@@ -2139,23 +2079,51 @@ export class BinaryTree<
2139
2079
  * Time Complexity: O(1)
2140
2080
  * Space Complexity: O(1)
2141
2081
  *
2142
- * The function `_ensureCallback` ensures that a callback function is provided and returns it.
2143
- * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is of type
2144
- * `ReturnType<C> | null | undefined`. This means it can accept a value that is the return type of
2145
- * the generic type `C`, or it can be `null` or `undefined`.
2146
- * @param {C} callback - The `callback` parameter is a function that takes a `node` as an argument
2147
- * and returns a value. It is of type `C`, which is a generic type that extends the
2148
- * `BTNCallback<NODE>` type.
2149
- * @returns the callback parameter.
2082
+ * The function `_ensurePredicate` in TypeScript ensures that the input is converted into a valid
2083
+ * predicate function for a binary tree node.
2084
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrEntryOrRawOrPredicate - The
2085
+ * `_ensurePredicate` method in the provided code snippet is responsible for ensuring that the input
2086
+ * parameter `keyOrEntryOrRawOrPredicate` is transformed into a valid predicate function that can be
2087
+ * used for filtering nodes in a binary tree.
2088
+ * @returns A BTNPredicate<NODE> function is being returned.
2150
2089
  */
2151
- protected _ensureCallback<C extends BTNCallback<NODE>>(
2152
- identifier: ReturnType<C> | null | undefined,
2153
- callback: C = this._DEFAULT_CALLBACK as C
2154
- ): C {
2155
- if ((!callback || callback === this._DEFAULT_CALLBACK) && this.isNode(identifier)) {
2156
- callback = (node => node) as C;
2090
+ protected _ensurePredicate(
2091
+ keyOrEntryOrRawOrPredicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>
2092
+ ): BTNPredicate<NODE> {
2093
+ if (keyOrEntryOrRawOrPredicate === null || keyOrEntryOrRawOrPredicate === undefined)
2094
+ return (node: NODE) => (node ? false : false);
2095
+
2096
+ if (this._isPredicated(keyOrEntryOrRawOrPredicate)) return keyOrEntryOrRawOrPredicate;
2097
+
2098
+ if (this.isRealNode(keyOrEntryOrRawOrPredicate)) return (node: NODE) => node === keyOrEntryOrRawOrPredicate;
2099
+
2100
+ if (this.isEntry(keyOrEntryOrRawOrPredicate)) {
2101
+ const [key] = keyOrEntryOrRawOrPredicate;
2102
+ return (node: NODE) => node.key === key;
2157
2103
  }
2158
2104
 
2159
- return callback;
2105
+ if (this.isKey(keyOrEntryOrRawOrPredicate)) return (node: NODE) => node.key === keyOrEntryOrRawOrPredicate;
2106
+
2107
+ if (this._toEntryFn) {
2108
+ const [key] = this._toEntryFn(keyOrEntryOrRawOrPredicate);
2109
+ return (node: NODE) => node.key === key;
2110
+ }
2111
+ return (node: NODE) => node.key === keyOrEntryOrRawOrPredicate;
2112
+ }
2113
+
2114
+ /**
2115
+ * Time Complexity: O(1)
2116
+ * Space Complexity: O(1)
2117
+ *
2118
+ * The function `_isPredicated` checks if a given parameter is a function.
2119
+ * @param {any} p - The parameter `p` is a variable of type `any`, which means it can hold any type
2120
+ * of value. In this context, the function `_isPredicated` is checking if `p` is a function that
2121
+ * satisfies the type `BTNPredicate<NODE>`.
2122
+ * @returns The function is checking if the input `p` is a function and returning a boolean value
2123
+ * based on that check. If `p` is a function, it will return `true`, indicating that `p` is a
2124
+ * predicate function for a binary tree node. If `p` is not a function, it will return `false`.
2125
+ */
2126
+ protected _isPredicated(p: any): p is BTNPredicate<NODE> {
2127
+ return typeof p === 'function';
2160
2128
  }
2161
2129
  }