min-heap-typed 1.52.6 → 1.52.8

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 +534 -402
  13. package/dist/data-structures/binary-tree/binary-tree.js +668 -597
  14. package/dist/data-structures/binary-tree/bst.d.ts +72 -65
  15. package/dist/data-structures/binary-tree/bst.js +115 -113
  16. package/dist/data-structures/binary-tree/rb-tree.d.ts +21 -24
  17. package/dist/data-structures/binary-tree/rb-tree.js +40 -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 +28 -31
  21. package/dist/data-structures/binary-tree/tree-multi-map.js +44 -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 +47 -50
  54. package/src/data-structures/binary-tree/avl-tree.ts +69 -71
  55. package/src/data-structures/binary-tree/binary-indexed-tree.ts +2 -2
  56. package/src/data-structures/binary-tree/binary-tree.ts +697 -725
  57. package/src/data-structures/binary-tree/bst.ts +123 -129
  58. package/src/data-structures/binary-tree/rb-tree.ts +44 -46
  59. package/src/data-structures/binary-tree/segment-tree.ts +2 -2
  60. package/src/data-structures/binary-tree/tree-multi-map.ts +48 -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,44 @@ 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 | BTNPredicate<NODE>} keyOrNodeOrEntryOrRawOrPredicate
519
+ * - The `delete` method you provided is used to delete a node from a binary tree based on the key,
520
+ * node, entry, raw data, or a custom predicate. 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
527
+ delete(
528
+ keyOrNodeOrEntryOrRawOrPredicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>
553
529
  ): BinaryTreeDeleteResult<NODE>[] {
554
530
  const deletedResult: BinaryTreeDeleteResult<NODE>[] = [];
555
- if (!this.root) return deletedResult;
556
- callback = this._ensureCallback(identifier, callback);
531
+ if (!this._root) return deletedResult;
557
532
 
558
- const curr = this.getNode(identifier, callback);
533
+ const curr = this.getNode(keyOrNodeOrEntryOrRawOrPredicate);
559
534
  if (!curr) return deletedResult;
560
535
 
561
536
  const parent: NODE | undefined = curr?.parent;
@@ -589,84 +564,56 @@ export class BinaryTree<
589
564
  curr.right = undefined;
590
565
  }
591
566
 
592
- this._size = this.size - 1;
567
+ this._size = this._size - 1;
593
568
 
594
569
  deletedResult.push({ deleted: orgCurrent, needBalanced });
595
570
  return deletedResult;
596
571
  }
597
572
 
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
573
  /**
623
574
  * Time Complexity: O(n)
624
575
  * Space Complexity: O(k + log n)
625
576
  *
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.
577
+ * The function `getNodes` retrieves nodes from a binary tree based on a key, node, entry, raw data,
578
+ * or predicate, with options for recursive or iterative traversal.
579
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrNodeOrEntryOrRawOrPredicate
580
+ * - The `getNodes` function you provided takes several parameters:
581
+ * @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` function is a boolean flag that
582
+ * determines whether to return only the first node that matches the criteria specified by the
583
+ * `keyOrNodeOrEntryOrRawOrPredicate` parameter. If `onlyOne` is set to `true`, the function will
584
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
585
+ * `getNodes` function is used to specify the starting point for traversing the binary tree. It
586
+ * represents the root node of the binary tree or the node from which the traversal should begin. If
587
+ * not provided, the default value is set to `this._root
588
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` function
589
+ * determines the type of iteration to be performed when traversing the nodes of a binary tree. It
590
+ * can have two possible values:
591
+ * @returns The `getNodes` function returns an array of nodes that satisfy the provided condition
592
+ * based on the input parameters and the iteration type specified.
645
593
  */
646
- getNodes<C extends BTNCallback<NODE>>(
647
- identifier: ReturnType<C> | null | undefined,
648
- callback: C = this._DEFAULT_CALLBACK as C,
594
+ getNodes(
595
+ keyOrNodeOrEntryOrRawOrPredicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>,
649
596
  onlyOne = false,
650
- beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
597
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
651
598
  iterationType: IterationType = this.iterationType
652
599
  ): NODE[] {
653
- if (identifier === undefined) return [];
654
- if (identifier === null) return [];
600
+ if (keyOrNodeOrEntryOrRawOrPredicate === undefined) return [];
601
+ if (keyOrNodeOrEntryOrRawOrPredicate === null) return [];
655
602
  beginRoot = this.ensureNode(beginRoot);
656
603
  if (!beginRoot) return [];
657
- callback = this._ensureCallback(identifier, callback);
604
+ const callback = this._ensurePredicate(keyOrNodeOrEntryOrRawOrPredicate);
658
605
 
659
606
  const ans: NODE[] = [];
660
607
 
661
608
  if (iterationType === 'RECURSIVE') {
662
609
  const dfs = (cur: NODE) => {
663
- if (callback(cur) === identifier) {
610
+ if (callback(cur)) {
664
611
  ans.push(cur);
665
612
  if (onlyOne) return;
666
613
  }
667
614
  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);
615
+ if (this.isRealNode(cur.left)) dfs(cur.left);
616
+ if (this.isRealNode(cur.right)) dfs(cur.right);
670
617
  };
671
618
 
672
619
  dfs(beginRoot);
@@ -675,12 +622,12 @@ export class BinaryTree<
675
622
  while (stack.length > 0) {
676
623
  const cur = stack.pop();
677
624
  if (this.isRealNode(cur)) {
678
- if (callback(cur) === identifier) {
625
+ if (callback(cur)) {
679
626
  ans.push(cur);
680
627
  if (onlyOne) return ans;
681
628
  }
682
- this.isRealNode(cur.left) && stack.push(cur.left);
683
- this.isRealNode(cur.right) && stack.push(cur.right);
629
+ if (this.isRealNode(cur.left)) stack.push(cur.left);
630
+ if (this.isRealNode(cur.right)) stack.push(cur.right);
684
631
  }
685
632
  }
686
633
  }
@@ -688,178 +635,115 @@ export class BinaryTree<
688
635
  return ans;
689
636
  }
690
637
 
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
638
  /**
713
639
  * Time Complexity: O(n)
714
640
  * Space Complexity: O(log n).
715
641
  *
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.
642
+ * The `getNode` function retrieves a node based on the provided key, node, entry, raw data, or
643
+ * predicate.
644
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrNodeOrEntryOrRawOrPredicate
645
+ * - The `keyOrNodeOrEntryOrRawOrPredicate` parameter in the `getNode` function can accept a key,
646
+ * node, entry, raw data, or a predicate function.
647
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
648
+ * `getNode` function is used to specify the starting point for searching for a node in a binary
649
+ * tree. If no specific starting point is provided, the default value is set to `this._root`, which
650
+ * is typically the root node of the binary tree.
651
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `getNode` method is
652
+ * used to specify the type of iteration to be performed when searching for a node. It has a default
653
+ * value of `this.iterationType`, which means it will use the iteration type defined in the current
654
+ * context if no specific value is provided
655
+ * @returns The `getNode` function is returning the first node that matches the specified criteria,
656
+ * or `null` if no matching node is found.
729
657
  */
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,
658
+ getNode(
659
+ keyOrNodeOrEntryOrRawOrPredicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>,
660
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
734
661
  iterationType: IterationType = this.iterationType
735
662
  ): OptBTNOrNull<NODE> {
736
- return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
663
+ return this.getNodes(keyOrNodeOrEntryOrRawOrPredicate, true, beginRoot, iterationType)[0] ?? null;
737
664
  }
738
665
 
739
666
  /**
740
667
  * Time Complexity: O(n)
741
668
  * Space Complexity: O(log n)
742
669
  *
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.
670
+ * The function `getNodeByKey` retrieves a node by its key from a binary tree structure.
671
+ * @param {K} key - The `key` parameter is the value used to search for a specific node in a data
672
+ * structure.
673
+ * @param {IterationType} iterationType - The `iterationType` parameter is a type of iteration that
674
+ * specifies how the tree nodes should be traversed when searching for a node with the given key. It
675
+ * is an optional parameter with a default value of `this.iterationType`.
676
+ * @returns The `getNodeByKey` function is returning an optional binary tree node
677
+ * (`OptBTNOrNull<NODE>`).
750
678
  */
751
679
  getNodeByKey(key: K, iterationType: IterationType = this.iterationType): OptBTNOrNull<NODE> {
752
- return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
680
+ return this.getNode(key, this._root, iterationType);
753
681
  }
754
682
 
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
683
  /**
777
684
  * Time Complexity: O(n)
778
685
  * Space Complexity: O(log n)
779
686
  *
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.
687
+ * This function overrides the `get` method to retrieve the value associated with a specified key,
688
+ * node, entry, raw data, or predicate in a data structure.
689
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrNodeOrEntryOrRawOrPredicate
690
+ * - The `keyOrNodeOrEntryOrRawOrPredicate` parameter in the `get` method can accept one of the
691
+ * following types:
692
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `get`
693
+ * method is used to specify the starting point for searching for a key or node in the binary tree.
694
+ * If no specific starting point is provided, the default starting point is the root of the binary
695
+ * tree (`this._root`).
696
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `get` method is used
697
+ * to specify the type of iteration to be performed when searching for a key in the binary tree. It
698
+ * is an optional parameter with a default value of `this.iterationType`, which means it will use the
699
+ * iteration type defined in the
700
+ * @returns The `get` method is returning the value associated with the specified key, node, entry,
701
+ * raw data, or predicate in the binary tree map. If the specified key or node is found in the tree,
702
+ * the method returns the corresponding value. If the key or node is not found, it returns
703
+ * `undefined`.
796
704
  */
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,
705
+ override get(
706
+ keyOrNodeOrEntryOrRawOrPredicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>,
707
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
801
708
  iterationType: IterationType = this.iterationType
802
709
  ): V | undefined {
803
- return this.getNode(identifier, callback, beginRoot, iterationType)?.value;
710
+ return this.getNode(keyOrNodeOrEntryOrRawOrPredicate, beginRoot, iterationType)?.value;
804
711
  }
805
712
 
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
713
  /**
828
714
  * Time Complexity: O(n)
829
715
  * Space Complexity: O(log n)
830
716
  *
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.
717
+ * The `has` function in TypeScript checks if a specified key, node, entry, raw data, or predicate
718
+ * exists in the data structure.
719
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrNodeOrEntryOrRawOrPredicate
720
+ * - The `keyOrNodeOrEntryOrRawOrPredicate` parameter in the `override has` method can accept one of
721
+ * the following types:
722
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
723
+ * `override` method is used to specify the starting point for the search operation within the data
724
+ * structure. It defaults to `this._root` if not provided explicitly.
725
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `override has` method
726
+ * is used to specify the type of iteration to be performed. It has a default value of
727
+ * `this.iterationType`, which means it will use the iteration type defined in the current context if
728
+ * no value is provided when calling the method.
729
+ * @returns The `override has` method is returning a boolean value. It checks if there are any nodes
730
+ * that match the provided key, node, entry, raw data, or predicate in the tree structure. If there
731
+ * are matching nodes, it returns `true`, indicating that the tree contains the specified element.
732
+ * Otherwise, it returns `false`.
846
733
  */
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,
734
+ override has(
735
+ keyOrNodeOrEntryOrRawOrPredicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>,
736
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
851
737
  iterationType: IterationType = this.iterationType
852
738
  ): boolean {
853
- callback = this._ensureCallback(identifier, callback);
854
-
855
- return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
739
+ return this.getNodes(keyOrNodeOrEntryOrRawOrPredicate, true, beginRoot, iterationType).length > 0;
856
740
  }
857
741
 
858
742
  /**
859
743
  * Time Complexity: O(1)
860
744
  * Space Complexity: O(1)
861
745
  *
862
- * Clear the binary tree, removing all nodes.
746
+ * The `clear` function resets the root node and size of a data structure to empty.
863
747
  */
864
748
  clear() {
865
749
  this._setRoot(undefined);
@@ -870,26 +754,31 @@ export class BinaryTree<
870
754
  * Time Complexity: O(1)
871
755
  * Space Complexity: O(1)
872
756
  *
873
- * Check if the binary tree is empty.
874
- * @returns {boolean} - True if the binary tree is empty, false otherwise.
757
+ * The `isEmpty` function in TypeScript checks if a data structure has no elements and returns a
758
+ * boolean value.
759
+ * @returns The `isEmpty()` method is returning a boolean value, specifically `true` if the `_size`
760
+ * property is equal to 0, indicating that the data structure is empty, and `false` otherwise.
875
761
  */
876
762
  isEmpty(): boolean {
877
- return this.size === 0;
763
+ return this._size === 0;
878
764
  }
879
765
 
880
766
  /**
881
767
  * Time Complexity: O(n)
882
768
  * Space Complexity: O(log n)
883
769
  *
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.
770
+ * The function checks if a binary tree is perfectly balanced by comparing its minimum height with
771
+ * its height.
772
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter is the starting
773
+ * point for checking if the binary tree is perfectly balanced. It represents the root node of the
774
+ * binary tree or a specific node from which the balance check should begin.
775
+ * @returns The method `isPerfectlyBalanced` is returning a boolean value, which indicates whether
776
+ * the tree starting from the `beginRoot` node is perfectly balanced or not. The return value is
777
+ * determined by comparing the minimum height of the tree with the height of the tree. If the minimum
778
+ * height plus 1 is greater than or equal to the height of the tree, then it is considered perfectly
779
+ * balanced and
891
780
  */
892
- isPerfectlyBalanced(beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root): boolean {
781
+ isPerfectlyBalanced(beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root): boolean {
893
782
  return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
894
783
  }
895
784
 
@@ -897,18 +786,22 @@ export class BinaryTree<
897
786
  * Time Complexity: O(n)
898
787
  * Space Complexity: O(1)
899
788
  *
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.
789
+ * The function `isBST` in TypeScript checks if a binary search tree is valid using either recursive
790
+ * or iterative methods.
791
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `isBST`
792
+ * function represents the starting point for checking whether a binary search tree (BST) is valid.
793
+ * It can be a node in the BST or a reference to the root of the BST. If no specific node is
794
+ * provided, the function will default to
795
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `isBST` function
796
+ * determines whether the function should use a recursive approach or an iterative approach to check
797
+ * if the binary search tree (BST) is valid.
798
+ * @returns The `isBST` method is returning a boolean value, which indicates whether the binary
799
+ * search tree (BST) represented by the given root node is a valid BST or not. The method checks if
800
+ * the tree satisfies the BST property, where for every node, all nodes in its left subtree have keys
801
+ * less than the node's key, and all nodes in its right subtree have keys greater than the node's
909
802
  */
910
803
  isBST(
911
- beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
804
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
912
805
  iterationType: IterationType = this.iterationType
913
806
  ): boolean {
914
807
  // TODO there is a bug
@@ -955,19 +848,21 @@ export class BinaryTree<
955
848
  * Time Complexity: O(n)
956
849
  * Space Complexity: O(1)
957
850
  *
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.
851
+ * The `getDepth` function calculates the depth between two nodes in a binary tree.
852
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} dist - The `dist` parameter in the `getDepth`
853
+ * function represents the node or entry in a binary tree map, or a reference to a node in the tree.
854
+ * It is the target node for which you want to calculate the depth from the `beginRoot` node.
855
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
856
+ * `getDepth` function represents the starting point from which you want to calculate the depth of a
857
+ * given node or entry in a binary tree. If no specific starting point is provided, the default value
858
+ * for `beginRoot` is set to the root of the binary
859
+ * @returns The `getDepth` method returns the depth of a given node `dist` relative to the
860
+ * `beginRoot` node in a binary tree. If the `dist` node is not found in the path to the `beginRoot`
861
+ * node, it returns the depth of the `dist` node from the root of the tree.
967
862
  */
968
863
  getDepth(
969
- dist: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
970
- beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root
864
+ dist: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
865
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root
971
866
  ): number {
972
867
  let distEnsured = this.ensureNode(dist);
973
868
  const beginRootEnsured = this.ensureNode(beginRoot);
@@ -987,16 +882,20 @@ export class BinaryTree<
987
882
  * Space Complexity: O(1)
988
883
  *
989
884
  * 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.
885
+ * or iterative approach in TypeScript.
886
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter is the starting
887
+ * point from which the height of the binary tree will be calculated. It can be a node in the binary
888
+ * tree or a reference to the root of the tree. If not provided, it defaults to the root of the
889
+ * binary tree data structure.
890
+ * @param {IterationType} iterationType - The `iterationType` parameter is used to determine the type
891
+ * of iteration to be performed while calculating the height of the binary tree. It can have two
892
+ * possible values:
893
+ * @returns The `getHeight` method returns the height of the binary tree starting from the specified
894
+ * root node. The height is calculated based on the maximum depth of the tree, considering either a
895
+ * recursive approach or an iterative approach depending on the `iterationType` parameter.
997
896
  */
998
897
  getHeight(
999
- beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
898
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
1000
899
  iterationType: IterationType = this.iterationType
1001
900
  ): number {
1002
901
  beginRoot = this.ensureNode(beginRoot);
@@ -1033,19 +932,21 @@ export class BinaryTree<
1033
932
  * Space Complexity: O(log n)
1034
933
  *
1035
934
  * 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.
935
+ * recursive or iterative approach in TypeScript.
936
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
937
+ * `getMinHeight` function represents the starting node from which the minimum height of the binary
938
+ * tree will be calculated. It is either a node in the binary tree or a reference to the root of the
939
+ * tree. If not provided, the default value is the root
940
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `getMinHeight` method
941
+ * specifies the type of iteration to use when calculating the minimum height of a binary tree. It
942
+ * can have two possible values:
943
+ * @returns The `getMinHeight` method returns the minimum height of the binary tree starting from the
944
+ * specified root node. The height is calculated based on the shortest path from the root node to a
945
+ * leaf node in the tree. The method uses either a recursive approach or an iterative approach (using
946
+ * a stack) based on the `iterationType` parameter.
1046
947
  */
1047
948
  getMinHeight(
1048
- beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
949
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
1049
950
  iterationType: IterationType = this.iterationType
1050
951
  ): number {
1051
952
  beginRoot = this.ensureNode(beginRoot);
@@ -1076,8 +977,8 @@ export class BinaryTree<
1076
977
  if (!this.isRealNode(node.right) || last === node.right) {
1077
978
  node = stack.pop();
1078
979
  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;
980
+ const leftMinHeight = this.isRealNode(node.left) ? depths.get(node.left)! : -1;
981
+ const rightMinHeight = this.isRealNode(node.right) ? depths.get(node.right)! : -1;
1081
982
  depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
1082
983
  last = node;
1083
984
  node = null;
@@ -1086,7 +987,7 @@ export class BinaryTree<
1086
987
  }
1087
988
  }
1088
989
 
1089
- return depths.get(beginRoot) ?? -1;
990
+ return depths.get(beginRoot)!;
1090
991
  }
1091
992
  }
1092
993
 
@@ -1094,27 +995,38 @@ export class BinaryTree<
1094
995
  * Time Complexity: O(log n)
1095
996
  * Space Complexity: O(log n)
1096
997
  *
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.
998
+ * The function `getPathToRoot` in TypeScript retrieves the path from a given node to the root of a
999
+ * tree structure, applying a specified callback function along the way.
1000
+ * @param {C} callback - The `callback` parameter is a function that is used to process each node in
1001
+ * the path to the root. It is expected to be a function that takes a node as an argument and returns
1002
+ * a value based on that node. The return type of the callback function is determined by the generic
1003
+ * type `C
1004
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginNode - The `beginNode` parameter in the
1005
+ * `getPathToRoot` function can be either a key, a node, an entry, or any other value of type `R`.
1006
+ * @param [isReverse=true] - The `isReverse` parameter in the `getPathToRoot` function determines
1007
+ * whether the resulting path from the given `beginNode` to the root should be in reverse order or
1008
+ * not. If `isReverse` is set to `true`, the path will be reversed before being returned. If `is
1009
+ * @returns The function `getPathToRoot` returns an array of the return values of the callback
1010
+ * function `callback` applied to each node in the path from the `beginNode` to the root node. The
1011
+ * array is either in reverse order or in the original order based on the value of the `isReverse`
1012
+ * parameter.
1105
1013
  */
1106
- getPathToRoot(beginNode: R | BTNKeyOrNodeOrEntry<K, V, NODE>, isReverse = true): NODE[] {
1107
- const result: NODE[] = [];
1014
+ getPathToRoot<C extends BTNCallback<OptBTNOrNull<NODE>>>(
1015
+ callback: C = this._DEFAULT_BTN_CALLBACK as C,
1016
+ beginNode: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
1017
+ isReverse = true
1018
+ ): ReturnType<C>[] {
1019
+ const result: ReturnType<C>[] = [];
1108
1020
  let beginNodeEnsured = this.ensureNode(beginNode);
1109
1021
 
1110
1022
  if (!beginNodeEnsured) return result;
1111
1023
 
1112
1024
  while (beginNodeEnsured.parent) {
1113
1025
  // Array.push + Array.reverse is more efficient than Array.unshift
1114
- result.push(beginNodeEnsured);
1026
+ result.push(callback(beginNodeEnsured));
1115
1027
  beginNodeEnsured = beginNodeEnsured.parent;
1116
1028
  }
1117
- result.push(beginNodeEnsured);
1029
+ result.push(callback(beginNodeEnsured));
1118
1030
  return isReverse ? result.reverse() : result;
1119
1031
  }
1120
1032
 
@@ -1125,23 +1037,23 @@ export class BinaryTree<
1125
1037
  * The function `getLeftMost` retrieves the leftmost node in a binary tree using either recursive or
1126
1038
  * tail-recursive iteration.
1127
1039
  * @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
1040
+ * node of a binary tree or with `undefined` if the tree is empty. It is provided with a default
1041
+ * value of `_DEFAULT_BTN_CALLBACK` if not specified.
1042
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
1131
1043
  * `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 (`
1044
+ * tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
1045
+ * starting point is provided, the function will default
1134
1046
  * @param {IterationType} iterationType - The `iterationType` parameter in the `getLeftMost` function
1135
1047
  * specifies the type of iteration to be used when traversing the binary tree nodes. It can have two
1136
1048
  * possible values:
1137
1049
  * @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
1050
+ * leftmost node in the binary tree starting from the `beginRoot` node. If the `beginRoot` node is
1051
+ * `NIL`, it returns the result of the callback function applied to `undefined`. If the `beginRoot`
1052
+ * node is not a real node, it returns the result of the callback
1141
1053
  */
1142
1054
  getLeftMost<C extends BTNCallback<OptBTNOrNull<NODE>>>(
1143
- callback: C = this._DEFAULT_CALLBACK as C,
1144
- beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
1055
+ callback: C = this._DEFAULT_BTN_CALLBACK as C,
1056
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
1145
1057
  iterationType: IterationType = this.iterationType
1146
1058
  ): ReturnType<C> {
1147
1059
  if (this.isNIL(beginRoot)) return callback(undefined);
@@ -1174,24 +1086,24 @@ export class BinaryTree<
1174
1086
  * The function `getRightMost` retrieves the rightmost node in a binary tree using either recursive
1175
1087
  * or iterative traversal methods.
1176
1088
  * @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
1089
+ * of finding the rightmost node in a binary tree. It is of type `BTNCallback<OptBTNOrNull<NODE>>`,
1090
+ * which means it is a callback function that can accept either an optional binary tree node or null
1091
+ * as
1092
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
1180
1093
  * `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
1094
+ * tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
1095
+ * starting point is provided, the function will default
1183
1096
  * @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
1097
+ * function specifies the type of iteration to be used when traversing the binary tree nodes. It can
1098
+ * have two possible values:
1099
+ * @returns The `getRightMost` function returns the result of the callback function `C`, which is
1100
+ * passed as a parameter to the function. The callback function is called with the rightmost node in
1101
+ * the binary tree structure, determined based on the specified iteration type ('RECURSIVE' or
1102
+ * other).
1191
1103
  */
1192
1104
  getRightMost<C extends BTNCallback<OptBTNOrNull<NODE>>>(
1193
- callback: C = this._DEFAULT_CALLBACK as C,
1194
- beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
1105
+ callback: C = this._DEFAULT_BTN_CALLBACK as C,
1106
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
1195
1107
  iterationType: IterationType = this.iterationType
1196
1108
  ): ReturnType<C> {
1197
1109
  if (this.isNIL(beginRoot)) return callback(undefined);
@@ -1221,10 +1133,14 @@ export class BinaryTree<
1221
1133
  * Time Complexity: O(log n)
1222
1134
  * Space Complexity: O(1)
1223
1135
  *
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.
1136
+ * The function `getPredecessor` in TypeScript returns the predecessor node of a given node in a
1137
+ * binary tree.
1138
+ * @param {NODE} node - The `getPredecessor` function you provided seems to be attempting to find the
1139
+ * predecessor of a given node in a binary tree. However, there seems to be a logical issue in the
1140
+ * while loop condition that might cause an infinite loop.
1141
+ * @returns The `getPredecessor` function returns the predecessor node of the input `NODE` parameter.
1142
+ * If the left child of the input node exists, it traverses to the rightmost node of the left subtree
1143
+ * to find the predecessor. If the left child does not exist, it returns the input node itself.
1228
1144
  */
1229
1145
  getPredecessor(node: NODE): NODE {
1230
1146
  if (this.isRealNode(node.left)) {
@@ -1244,10 +1160,14 @@ export class BinaryTree<
1244
1160
  * Time Complexity: O(log n)
1245
1161
  * Space Complexity: O(1)
1246
1162
  *
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.
1163
+ * The function `getSuccessor` in TypeScript returns the next node in an in-order traversal of a
1164
+ * binary tree.
1165
+ * @param {K | NODE | null} [x] - The `getSuccessor` function takes a parameter `x`, which can be of
1166
+ * type `K`, `NODE`, or `null`.
1167
+ * @returns The `getSuccessor` function returns the successor node of the input node `x`. If `x` has
1168
+ * a right child, the function returns the leftmost node in the right subtree of `x`. If `x` does not
1169
+ * have a right child, the function traverses up the parent nodes until it finds a node that is not
1170
+ * the right child of its parent, and returns that node
1251
1171
  */
1252
1172
  getSuccessor(x?: K | NODE | null): OptBTNOrNull<NODE> {
1253
1173
  x = this.ensureNode(x);
@@ -1268,14 +1188,14 @@ export class BinaryTree<
1268
1188
  dfs<C extends BTNCallback<NODE>>(
1269
1189
  callback?: C,
1270
1190
  pattern?: DFSOrderPattern,
1271
- beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
1191
+ beginRoot?: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
1272
1192
  iterationType?: IterationType
1273
1193
  ): ReturnType<C>[];
1274
1194
 
1275
1195
  dfs<C extends BTNCallback<NODE | null>>(
1276
1196
  callback?: C,
1277
1197
  pattern?: DFSOrderPattern,
1278
- beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
1198
+ beginRoot?: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
1279
1199
  iterationType?: IterationType,
1280
1200
  includeNull?: boolean
1281
1201
  ): ReturnType<C>[];
@@ -1284,29 +1204,32 @@ export class BinaryTree<
1284
1204
  * Time complexity: O(n)
1285
1205
  * Space complexity: O(n)
1286
1206
  *
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.
1207
+ * The function `dfs` performs a depth-first search traversal on a binary tree structure based on the
1208
+ * specified parameters.
1209
+ * @param {C} callback - The `callback` parameter is a generic type `C` that extends the
1210
+ * `BTNCallback` interface with a type parameter of `OptBTNOrNull<NODE>`. It has a default value of
1211
+ * `this._DEFAULT_BTN_CALLBACK as C`.
1212
+ * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `dfs` method specifies the
1213
+ * order in which the Depth-First Search (DFS) algorithm should traverse the nodes in the tree. The
1214
+ * possible values for the `pattern` parameter are:
1215
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `dfs`
1216
+ * method is used to specify the starting point for the Depth-First Search traversal. It can be
1217
+ * either a `BTNKeyOrNodeOrEntry` object representing a key, node, or entry in the binary tree map,
1218
+ * or it can be a
1219
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `dfs` method specifies
1220
+ * the type of iteration to be performed during the depth-first search traversal. It is used to
1221
+ * determine the order in which nodes are visited during the traversal.
1222
+ * @param [includeNull=false] - The `includeNull` parameter in the `dfs` method is a boolean flag
1223
+ * that determines whether null values should be included in the traversal or not. If `includeNull`
1224
+ * is set to `true`, then null values will be included in the traversal process. If it is set to
1225
+ * `false`,
1226
+ * @returns The `dfs` method is returning an array of the return type specified by the generic type
1227
+ * parameter `C`. The return type is determined by the callback function provided to the method.
1305
1228
  */
1306
1229
  dfs<C extends BTNCallback<OptBTNOrNull<NODE>>>(
1307
- callback: C = this._DEFAULT_CALLBACK as C,
1230
+ callback: C = this._DEFAULT_BTN_CALLBACK as C,
1308
1231
  pattern: DFSOrderPattern = 'IN',
1309
- beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
1232
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
1310
1233
  iterationType: IterationType = this.iterationType,
1311
1234
  includeNull = false
1312
1235
  ): ReturnType<C>[] {
@@ -1317,14 +1240,14 @@ export class BinaryTree<
1317
1240
 
1318
1241
  bfs<C extends BTNCallback<NODE>>(
1319
1242
  callback?: C,
1320
- beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
1243
+ beginRoot?: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
1321
1244
  iterationType?: IterationType,
1322
1245
  includeNull?: false
1323
1246
  ): ReturnType<C>[];
1324
1247
 
1325
1248
  bfs<C extends BTNCallback<NODE | null>>(
1326
1249
  callback?: C,
1327
- beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
1250
+ beginRoot?: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
1328
1251
  iterationType?: IterationType,
1329
1252
  includeNull?: true
1330
1253
  ): ReturnType<C>[];
@@ -1333,27 +1256,28 @@ export class BinaryTree<
1333
1256
  * Time complexity: O(n)
1334
1257
  * Space complexity: O(n)
1335
1258
  *
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.
1259
+ * The `bfs` function performs a breadth-first search traversal on a binary tree or binary search
1260
+ * tree, executing a specified callback function on each node visited.
1261
+ * @param {C} callback - The `callback` parameter in the `bfs` function is a function that will be
1262
+ * called on each node visited during the breadth-first search traversal. It is a generic type `C`
1263
+ * that extends the `BTNCallback` type, which takes a parameter of type `NODE` or `null`.
1264
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `bfs`
1265
+ * function represents the starting point for the breadth-first search traversal in a binary tree. It
1266
+ * can be specified as a key, node, or entry in the binary tree structure. If not provided, the
1267
+ * default value is the root node of the binary
1268
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `bfs` function
1269
+ * determines the type of iteration to be performed on the binary tree nodes. It can have two
1270
+ * possible values:
1271
+ * @param [includeNull=false] - The `includeNull` parameter in the `bfs` function determines whether
1272
+ * to include `null` values in the breadth-first search traversal of a binary tree. If `includeNull`
1273
+ * is set to `true`, the traversal will include `null` values for nodes that do not have children
1274
+ * (left
1275
+ * @returns The `bfs` function returns an array of values that are the result of applying the
1276
+ * provided callback function to each node in the binary tree in a breadth-first search manner.
1353
1277
  */
1354
1278
  bfs<C extends BTNCallback<NODE | null>>(
1355
- callback: C = this._DEFAULT_CALLBACK as C,
1356
- beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
1279
+ callback: C = this._DEFAULT_BTN_CALLBACK as C,
1280
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
1357
1281
  iterationType: IterationType = this.iterationType,
1358
1282
  includeNull = false
1359
1283
  ): ReturnType<C>[] {
@@ -1409,39 +1333,37 @@ export class BinaryTree<
1409
1333
  * Time complexity: O(n)
1410
1334
  * Space complexity: O(n)
1411
1335
  *
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.
1336
+ * The `leaves` function in TypeScript returns an array of values from leaf nodes in a binary tree
1337
+ * structure based on a specified callback and iteration type.
1414
1338
  * @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`
1339
+ * in the binary tree. It is optional and defaults to a default callback function if not provided.
1340
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `leaves`
1418
1341
  * 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
1342
+ * tree. It can be provided as either a key, a node, or an entry in the binary tree structure. If not
1343
+ * explicitly provided, the default value
1421
1344
  * @param {IterationType} iterationType - The `iterationType` parameter in the `leaves` method
1422
1345
  * specifies the type of iteration to be performed when collecting the leaves of a binary tree. It
1423
1346
  * can have two possible values:
1424
1347
  * @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.
1348
+ * provided callback function to each leaf node in the binary tree.
1426
1349
  */
1427
1350
  leaves<C extends BTNCallback<NODE | null>>(
1428
- callback: C = this._DEFAULT_CALLBACK as C,
1429
- beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
1351
+ callback: C = this._DEFAULT_BTN_CALLBACK as C,
1352
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
1430
1353
  iterationType: IterationType = this.iterationType
1431
1354
  ): ReturnType<C>[] {
1432
1355
  beginRoot = this.ensureNode(beginRoot);
1433
1356
  const leaves: ReturnType<BTNCallback<NODE>>[] = [];
1434
- if (!this.isRealNode(beginRoot)) {
1435
- return [];
1436
- }
1357
+ if (!this.isRealNode(beginRoot)) return [];
1358
+
1437
1359
  if (iterationType === 'RECURSIVE') {
1438
1360
  const dfs = (cur: NODE) => {
1439
1361
  if (this.isLeaf(cur)) {
1440
1362
  leaves.push(callback(cur));
1441
1363
  }
1442
1364
  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);
1365
+ if (this.isRealNode(cur.left)) dfs(cur.left);
1366
+ if (this.isRealNode(cur.right)) dfs(cur.right);
1445
1367
  };
1446
1368
 
1447
1369
  dfs(beginRoot);
@@ -1453,8 +1375,8 @@ export class BinaryTree<
1453
1375
  if (this.isLeaf(cur)) {
1454
1376
  leaves.push(callback(cur));
1455
1377
  }
1456
- this.isRealNode(cur.left) && queue.push(cur.left);
1457
- this.isRealNode(cur.right) && queue.push(cur.right);
1378
+ if (this.isRealNode(cur.left)) queue.push(cur.left);
1379
+ if (this.isRealNode(cur.right)) queue.push(cur.right);
1458
1380
  }
1459
1381
  }
1460
1382
  }
@@ -1464,14 +1386,14 @@ export class BinaryTree<
1464
1386
 
1465
1387
  listLevels<C extends BTNCallback<NODE>>(
1466
1388
  callback?: C,
1467
- beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
1389
+ beginRoot?: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
1468
1390
  iterationType?: IterationType,
1469
1391
  includeNull?: false
1470
1392
  ): ReturnType<C>[][];
1471
1393
 
1472
1394
  listLevels<C extends BTNCallback<NODE | null>>(
1473
1395
  callback?: C,
1474
- beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
1396
+ beginRoot?: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
1475
1397
  iterationType?: IterationType,
1476
1398
  includeNull?: true
1477
1399
  ): ReturnType<C>[][];
@@ -1480,26 +1402,29 @@ export class BinaryTree<
1480
1402
  * Time complexity: O(n)
1481
1403
  * Space complexity: O(n)
1482
1404
  *
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>[][]`.
1405
+ * The `listLevels` function in TypeScript generates a list of nodes at each level of a binary tree,
1406
+ * using either recursive or iterative traversal based on the specified iteration type.
1407
+ * @param {C} callback - The `callback` parameter is a function that will be applied to each node in
1408
+ * the binary tree during the traversal. It is used to process each node and determine what
1409
+ * information to include in the output for each level of the tree.
1410
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
1411
+ * `listLevels` function represents the starting point for traversing the binary tree. It can be
1412
+ * either a key, a node, or an entry in the binary tree. If not provided, the default value is the
1413
+ * root of the binary tree.
1414
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `listLevels` function
1415
+ * determines the type of iteration to be performed on the binary tree nodes. It can have two
1416
+ * possible values:
1417
+ * @param [includeNull=false] - The `includeNull` parameter in the `listLevels` method determines
1418
+ * whether or not to include null nodes in the traversal of the binary tree. If `includeNull` is set
1419
+ * to `true`, the traversal will include null nodes in the levels of the tree. If set to `false`,
1420
+ * null
1421
+ * @returns The `listLevels` method returns an array of arrays, where each inner array represents a
1422
+ * level in a binary tree. Each inner array contains the return value of the provided callback
1423
+ * function applied to the nodes at that level.
1499
1424
  */
1500
1425
  listLevels<C extends BTNCallback<NODE | null>>(
1501
- callback: C = this._DEFAULT_CALLBACK as C,
1502
- beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
1426
+ callback: C = this._DEFAULT_BTN_CALLBACK as C,
1427
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
1503
1428
  iterationType: IterationType = this.iterationType,
1504
1429
  includeNull = false
1505
1430
  ): ReturnType<C>[][] {
@@ -1548,28 +1473,29 @@ export class BinaryTree<
1548
1473
  * Time complexity: O(n)
1549
1474
  * Space complexity: O(n)
1550
1475
  *
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`.
1476
+ * The `morris` function in TypeScript performs a Depth-First Search traversal on a binary tree using
1477
+ * Morris Traversal algorithm with different order patterns.
1478
+ * @param {C} callback - The `callback` parameter in the `morris` function is a function that will be
1479
+ * called on each node in the binary tree during the traversal. It is of type `C`, which extends the
1480
+ * `BTNCallback<NODE>` type. The default value for `callback` is `this._DEFAULT
1481
+ * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function specifies
1482
+ * the type of Depth-First Search (DFS) order pattern to traverse the binary tree. The possible
1483
+ * values for the `pattern` parameter are:
1484
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `morris`
1485
+ * function is the starting point for the Morris traversal algorithm. It represents the root node of
1486
+ * the binary tree or the node from which the traversal should begin. It can be provided as either a
1487
+ * key, a node, an entry, or a reference
1488
+ * @returns The `morris` function is returning an array of values that are the result of applying the
1489
+ * provided callback function to each node in the binary tree in the specified order pattern (IN,
1490
+ * PRE, or POST).
1565
1491
  */
1566
1492
  morris<C extends BTNCallback<NODE>>(
1567
- callback: C = this._DEFAULT_CALLBACK as C,
1493
+ callback: C = this._DEFAULT_BTN_CALLBACK as C,
1568
1494
  pattern: DFSOrderPattern = 'IN',
1569
- beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root
1495
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root
1570
1496
  ): ReturnType<C>[] {
1571
1497
  beginRoot = this.ensureNode(beginRoot);
1572
- if (beginRoot === null) return [];
1498
+ if (!beginRoot) return [];
1573
1499
  const ans: ReturnType<BTNCallback<NODE>>[] = [];
1574
1500
 
1575
1501
  let cur: OptBTNOrNull<NODE> = beginRoot;
@@ -1653,8 +1579,12 @@ export class BinaryTree<
1653
1579
  * Time complexity: O(n)
1654
1580
  * Space complexity: O(n)
1655
1581
  *
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.
1582
+ * The `clone` function creates a deep copy of a tree structure by traversing it using breadth-first
1583
+ * search.
1584
+ * @returns The `clone()` method is returning a cloned copy of the tree with the same structure and
1585
+ * values as the original tree. The method creates a new tree, iterates over the nodes of the
1586
+ * original tree using breadth-first search (bfs), and adds the nodes to the new tree. If a node in
1587
+ * the original tree is null, a null node is added to the cloned tree. If a node
1658
1588
  */
1659
1589
  clone(): TREE {
1660
1590
  const cloned = this.createTree();
@@ -1663,7 +1593,7 @@ export class BinaryTree<
1663
1593
  if (node === null) cloned.add(null);
1664
1594
  else cloned.add([node.key, node.value]);
1665
1595
  },
1666
- this.root,
1596
+ this._root,
1667
1597
  this.iterationType,
1668
1598
  true
1669
1599
  );
@@ -1674,16 +1604,17 @@ export class BinaryTree<
1674
1604
  * Time Complexity: O(n)
1675
1605
  * Space Complexity: O(n)
1676
1606
  *
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.
1607
+ * The `filter` function iterates over key-value pairs in a tree data structure and creates a new
1608
+ * tree with elements that satisfy a given predicate.
1609
+ * @param predicate - The `predicate` parameter in the `filter` method is a function that will be
1610
+ * called with four arguments: the `value` of the current entry, the `key` of the current entry, the
1611
+ * `index` of the current entry in the iteration, and the reference to the tree itself (`
1612
+ * @param {any} [thisArg] - The `thisArg` parameter in the `filter` method allows you to specify the
1613
+ * value of `this` that should be used when executing the `predicate` function. This is useful when
1614
+ * the `predicate` function relies on the context of a specific object or value. By providing a
1615
+ * `thisArg
1616
+ * @returns The `filter` method is returning a new tree that contains entries that pass the provided
1617
+ * predicate function.
1687
1618
  */
1688
1619
  filter(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: any) {
1689
1620
  const newTree = this.createTree();
@@ -1700,16 +1631,16 @@ export class BinaryTree<
1700
1631
  * Time Complexity: O(n)
1701
1632
  * Space Complexity: O(n)
1702
1633
  *
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.
1634
+ * The `map` function iterates over key-value pairs in a tree data structure, applies a callback
1635
+ * function to each value, and returns a new tree with the updated values.
1636
+ * @param callback - The `callback` parameter in the `map` method is a function that will be called
1637
+ * on each entry in the tree. It takes four arguments:
1638
+ * @param {any} [thisArg] - The `thisArg` parameter in the `map` function is an optional parameter
1639
+ * that specifies the value to be passed as `this` when executing the callback function. If provided,
1640
+ * the `thisArg` value will be used as the `this` value within the callback function. If `thisArg
1641
+ * @returns The `map` method is returning a new tree with the entries modified by the provided
1642
+ * callback function. Each entry in the original tree is passed to the callback function, and the
1643
+ * result of the callback function is added to the new tree.
1713
1644
  */
1714
1645
  map(callback: EntryCallback<K, V | undefined, V>, thisArg?: any) {
1715
1646
  const newTree = this.createTree();
@@ -1734,17 +1665,24 @@ export class BinaryTree<
1734
1665
  * Time Complexity: O(n)
1735
1666
  * Space Complexity: O(n)
1736
1667
  *
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.
1668
+ * The function `toVisual` in TypeScript overrides the visual representation of a binary tree with
1669
+ * customizable options for displaying undefined, null, and sentinel nodes.
1670
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
1671
+ * `toVisual` method is used to specify the starting point for visualizing the binary tree structure.
1672
+ * It can be a node, key, entry, or the root of the tree. If no specific starting point is provided,
1673
+ * the default is set to the root
1674
+ * @param {BinaryTreePrintOptions} [options] - The `options` parameter in the `toVisual` method is an
1675
+ * object that contains the following properties:
1676
+ * @returns The `override toVisual` method returns a string that represents the visual display of the
1677
+ * binary tree based on the provided options for showing undefined, null, and Red-Black NIL nodes.
1678
+ * The method constructs the visual representation by calling the `_displayAux` method and appending
1679
+ * the lines to the output string. The final output string contains the visual representation of the
1680
+ * binary tree with the specified options.
1746
1681
  */
1747
- override print(beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root, options?: BinaryTreePrintOptions): string {
1682
+ override toVisual(
1683
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
1684
+ options?: BinaryTreePrintOptions
1685
+ ): string {
1748
1686
  const opts = { isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false, ...options };
1749
1687
  beginRoot = this.ensureNode(beginRoot);
1750
1688
  let output = '';
@@ -1776,14 +1714,14 @@ export class BinaryTree<
1776
1714
  protected _dfs<C extends BTNCallback<NODE>>(
1777
1715
  callback?: C,
1778
1716
  pattern?: DFSOrderPattern,
1779
- beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
1717
+ beginRoot?: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
1780
1718
  iterationType?: IterationType
1781
1719
  ): ReturnType<C>[];
1782
1720
 
1783
1721
  protected _dfs<C extends BTNCallback<NODE | null>>(
1784
1722
  callback?: C,
1785
1723
  pattern?: DFSOrderPattern,
1786
- beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
1724
+ beginRoot?: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
1787
1725
  iterationType?: IterationType,
1788
1726
  includeNull?: boolean
1789
1727
  ): ReturnType<C>[];
@@ -1792,49 +1730,48 @@ export class BinaryTree<
1792
1730
  * Time complexity: O(n)
1793
1731
  * Space complexity: O(n)
1794
1732
  *
1795
- * The function `_dfs` performs a depth-first search traversal on a binary tree structure based on
1733
+ * The `_dfs` function performs a depth-first search traversal on a binary tree structure based on
1796
1734
  * 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.
1735
+ * @param {C} callback - The `callback` parameter in the `_dfs` method is a function that will be
1736
+ * called on each node visited during the depth-first search traversal. It is of type `C`, which
1737
+ * extends `BTNCallback<OptBTNOrNull<NODE>>`. The default value for this parameter is `this._DEFAULT
1801
1738
  * @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`
1739
+ * order in which the nodes are visited during the Depth-First Search traversal. It can have one of
1740
+ * the following values:
1741
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `_dfs`
1805
1742
  * 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 `
1743
+ * tree. It can be provided as either a `BTNKeyOrNodeOrEntry` object or a reference to the root node
1744
+ * of the tree. If no specific
1808
1745
  * @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:
1746
+ * specifies the type of iteration to be performed during the Depth-First Search (DFS) traversal of a
1747
+ * binary tree. It can have two possible values:
1811
1748
  * @param [includeNull=false] - The `includeNull` parameter in the `_dfs` method is a boolean flag
1812
1749
  * 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 `
1750
+ * `includeNull` is set to `true`, null nodes will be considered during the traversal process. If it
1751
+ * is set to `false`,
1815
1752
  * @param shouldVisitLeft - The `shouldVisitLeft` parameter is a function that takes a node as input
1816
1753
  * and returns a boolean value. It is used to determine whether the left child of a node should be
1817
1754
  * visited during the depth-first search traversal. By default, it checks if the node is truthy (not
1818
1755
  * 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
1756
+ * @param shouldVisitRight - The `shouldVisitRight` parameter is a function that takes a node as an
1757
+ * argument and returns a boolean value. It is used to determine whether the right child of a node
1821
1758
  * should be visited during the depth-first search traversal. The default implementation checks if
1822
- * the node is truthy before visiting the right child.
1759
+ * the node is truthy before visiting the right child
1823
1760
  * @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
1761
+ * argument and returns a boolean value. It is used to determine whether the root node should be
1825
1762
  * visited during the depth-first search traversal based on certain conditions. The default
1826
1763
  * 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
1764
+ * @param shouldProcessRoot - The `shouldProcessRoot` parameter is a function that takes a node as an
1765
+ * argument and returns a boolean value indicating whether the node should be processed during the
1766
+ * depth-first search traversal. The default implementation checks if the node is a real node or null
1767
+ * based on the `includeNull` flag. If `
1768
+ * @returns The function `_dfs` returns an array of the return type of the callback function provided
1832
1769
  * as input.
1833
1770
  */
1834
1771
  protected _dfs<C extends BTNCallback<OptBTNOrNull<NODE>>>(
1835
- callback: C = this._DEFAULT_CALLBACK as C,
1772
+ callback: C = this._DEFAULT_BTN_CALLBACK as C,
1836
1773
  pattern: DFSOrderPattern = 'IN',
1837
- beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
1774
+ beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
1838
1775
  iterationType: IterationType = this.iterationType,
1839
1776
  includeNull = false,
1840
1777
  shouldVisitLeft: (node: OptBTNOrNull<NODE>) => boolean = node => !!node,
@@ -1928,13 +1865,18 @@ export class BinaryTree<
1928
1865
  * Time Complexity: O(1)
1929
1866
  * Space Complexity: O(1)
1930
1867
  *
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]>.
1868
+ * The function `_getIterator` returns an iterable iterator for a binary tree data structure, either
1869
+ * using an iterative approach or a recursive approach based on the specified iteration type.
1870
+ * @param node - The `node` parameter in the `_getIterator` method represents the current node being
1871
+ * processed during iteration. It is initially set to the root node of the data structure (or the
1872
+ * node passed as an argument), and then it is traversed through the data structure based on the
1873
+ * iteration type specified (`ITER
1874
+ * @returns The `_getIterator` method returns an IterableIterator containing key-value pairs of nodes
1875
+ * in a binary tree structure. The method uses an iterative approach to traverse the tree based on
1876
+ * the `iterationType` property. If the `iterationType` is set to 'ITERATIVE', the method uses a
1877
+ * stack to perform an in-order traversal of the tree. If the `iterationType` is not 'ITERATIVE
1936
1878
  */
1937
- protected *_getIterator(node = this.root): IterableIterator<[K, V | undefined]> {
1879
+ protected *_getIterator(node = this._root): IterableIterator<[K, V | undefined]> {
1938
1880
  if (!node) return;
1939
1881
 
1940
1882
  if (this.iterationType === 'ITERATIVE') {
@@ -1969,18 +1911,16 @@ export class BinaryTree<
1969
1911
  * Time Complexity: O(n)
1970
1912
  * Space Complexity: O(n)
1971
1913
  *
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
1914
+ * The function `_displayAux` in TypeScript is responsible for generating the display layout of nodes
1915
+ * in a binary tree based on specified options.
1916
+ * @param node - The `node` parameter in the `_displayAux` function represents a node in a binary
1917
+ * tree. It can be either a valid node containing a key or a special type of node like null,
1918
+ * undefined, or a Red-Black tree NIL node. The function checks the type of the node and its
1919
+ * @param {BinaryTreePrintOptions} options - The `options` parameter in the `_displayAux` function
1920
+ * contains the following properties:
1921
+ * @returns The `_displayAux` function returns a `NodeDisplayLayout`, which is an array containing
1922
+ * information about how to display a node in a binary tree. The `NodeDisplayLayout` consists of four
1923
+ * elements:
1984
1924
  */
1985
1925
  protected _displayAux(node: OptBTNOrNull<NODE>, options: BinaryTreePrintOptions): NodeDisplayLayout {
1986
1926
  const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
@@ -2050,24 +1990,26 @@ export class BinaryTree<
2050
1990
  }
2051
1991
  }
2052
1992
 
2053
- protected _DEFAULT_CALLBACK = (node: OptBTNOrNull<NODE>) => (node ? node.key : undefined);
1993
+ protected _DEFAULT_BTN_CALLBACK = (node: OptBTNOrNull<NODE>) => (node ? node.key : undefined);
2054
1994
 
2055
1995
  /**
2056
1996
  * Time Complexity: O(1)
2057
1997
  * Space Complexity: O(1)
2058
1998
  *
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.
1999
+ * The _swapProperties function swaps key and value properties between two nodes in a binary tree.
2000
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} srcNode - The `srcNode` parameter in the
2001
+ * `_swapProperties` method can be either a BTNKeyOrNodeOrEntry object containing key and value
2002
+ * properties, or it can be of type R.
2003
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} destNode - The `destNode` parameter in the
2004
+ * `_swapProperties` method represents the node or entry where the properties will be swapped with
2005
+ * the `srcNode`. It can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>` or `R`. The method ensures that
2006
+ * both `srcNode
2007
+ * @returns The `_swapProperties` method returns either the `destNode` with its key and value swapped
2008
+ * with the `srcNode`, or `undefined` if either `srcNode` or `destNode` is falsy.
2067
2009
  */
2068
2010
  protected _swapProperties(
2069
- srcNode: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
2070
- destNode: R | BTNKeyOrNodeOrEntry<K, V, NODE>
2011
+ srcNode: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
2012
+ destNode: BTNKeyOrNodeOrEntry<K, V, NODE> | R
2071
2013
  ): NODE | undefined {
2072
2014
  srcNode = this.ensureNode(srcNode);
2073
2015
  destNode = this.ensureNode(destNode);
@@ -2093,13 +2035,15 @@ export class BinaryTree<
2093
2035
  * Time Complexity: O(1)
2094
2036
  * Space Complexity: O(1)
2095
2037
  *
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.
2038
+ * The _replaceNode function replaces an old node with a new node in a binary tree structure.
2039
+ * @param {NODE} oldNode - The `oldNode` parameter represents the node that you want to replace in a
2040
+ * tree data structure.
2041
+ * @param {NODE} newNode - The `newNode` parameter in the `_replaceNode` function represents the node
2042
+ * that will replace the `oldNode` in a tree data structure. This function is responsible for
2043
+ * updating the parent, left child, right child, and root (if necessary) references when replacing a
2044
+ * node in the tree.
2045
+ * @returns The method `_replaceNode` is returning the `newNode` that was passed as a parameter after
2046
+ * replacing the `oldNode` with it in the binary tree structure.
2103
2047
  */
2104
2048
  protected _replaceNode(oldNode: NODE, newNode: NODE): NODE {
2105
2049
  if (oldNode.parent) {
@@ -2112,8 +2056,8 @@ export class BinaryTree<
2112
2056
  newNode.left = oldNode.left;
2113
2057
  newNode.right = oldNode.right;
2114
2058
  newNode.parent = oldNode.parent;
2115
- if (this.root === oldNode) {
2116
- this._root = newNode;
2059
+ if (this._root === oldNode) {
2060
+ this._setRoot(newNode);
2117
2061
  }
2118
2062
 
2119
2063
  return newNode;
@@ -2123,10 +2067,10 @@ export class BinaryTree<
2123
2067
  * Time Complexity: O(1)
2124
2068
  * Space Complexity: O(1)
2125
2069
  *
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`.
2070
+ * The function _setRoot sets the root node of a data structure while updating the parent reference
2071
+ * of the previous root node.
2072
+ * @param v - The parameter `v` in the `_setRoot` method is of type `OptBTNOrNull<NODE>`, which means
2073
+ * it can either be an optional `NODE` type or `null`.
2130
2074
  */
2131
2075
  protected _setRoot(v: OptBTNOrNull<NODE>) {
2132
2076
  if (v) {
@@ -2139,23 +2083,51 @@ export class BinaryTree<
2139
2083
  * Time Complexity: O(1)
2140
2084
  * Space Complexity: O(1)
2141
2085
  *
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.
2086
+ * The function `_ensurePredicate` in TypeScript ensures that the input is converted into a valid
2087
+ * predicate function for a binary tree node.
2088
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrEntryOrRawOrPredicate - The
2089
+ * `_ensurePredicate` method in the provided code snippet is responsible for ensuring that the input
2090
+ * parameter `keyOrEntryOrRawOrPredicate` is transformed into a valid predicate function that can be
2091
+ * used for filtering nodes in a binary tree.
2092
+ * @returns A BTNPredicate<NODE> function is being returned.
2150
2093
  */
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;
2094
+ protected _ensurePredicate(
2095
+ keyOrEntryOrRawOrPredicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>
2096
+ ): BTNPredicate<NODE> {
2097
+ if (keyOrEntryOrRawOrPredicate === null || keyOrEntryOrRawOrPredicate === undefined)
2098
+ return (node: NODE) => (node ? false : false);
2099
+
2100
+ if (this._isPredicated(keyOrEntryOrRawOrPredicate)) return keyOrEntryOrRawOrPredicate;
2101
+
2102
+ if (this.isRealNode(keyOrEntryOrRawOrPredicate)) return (node: NODE) => node === keyOrEntryOrRawOrPredicate;
2103
+
2104
+ if (this.isEntry(keyOrEntryOrRawOrPredicate)) {
2105
+ const [key] = keyOrEntryOrRawOrPredicate;
2106
+ return (node: NODE) => node.key === key;
2157
2107
  }
2158
2108
 
2159
- return callback;
2109
+ if (this.isKey(keyOrEntryOrRawOrPredicate)) return (node: NODE) => node.key === keyOrEntryOrRawOrPredicate;
2110
+
2111
+ if (this._toEntryFn) {
2112
+ const [key] = this._toEntryFn(keyOrEntryOrRawOrPredicate);
2113
+ return (node: NODE) => node.key === key;
2114
+ }
2115
+ return (node: NODE) => node.key === keyOrEntryOrRawOrPredicate;
2116
+ }
2117
+
2118
+ /**
2119
+ * Time Complexity: O(1)
2120
+ * Space Complexity: O(1)
2121
+ *
2122
+ * The function `_isPredicated` checks if a given parameter is a function.
2123
+ * @param {any} p - The parameter `p` is a variable of type `any`, which means it can hold any type
2124
+ * of value. In this context, the function `_isPredicated` is checking if `p` is a function that
2125
+ * satisfies the type `BTNPredicate<NODE>`.
2126
+ * @returns The function is checking if the input `p` is a function and returning a boolean value
2127
+ * based on that check. If `p` is a function, it will return `true`, indicating that `p` is a
2128
+ * predicate function for a binary tree node. If `p` is not a function, it will return `false`.
2129
+ */
2130
+ protected _isPredicated(p: any): p is BTNPredicate<NODE> {
2131
+ return typeof p === 'function';
2160
2132
  }
2161
2133
  }