linked-list-typed 1.52.6 → 1.52.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/dist/constants/index.d.ts +4 -0
  2. package/dist/constants/index.js +8 -0
  3. package/dist/data-structures/base/iterable-element-base.d.ts +8 -1
  4. package/dist/data-structures/base/iterable-element-base.js +10 -1
  5. package/dist/data-structures/base/iterable-entry-base.d.ts +8 -1
  6. package/dist/data-structures/base/iterable-entry-base.js +10 -10
  7. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +31 -32
  8. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +43 -44
  9. package/dist/data-structures/binary-tree/avl-tree.d.ts +23 -24
  10. package/dist/data-structures/binary-tree/avl-tree.js +71 -64
  11. package/dist/data-structures/binary-tree/binary-indexed-tree.js +2 -2
  12. package/dist/data-structures/binary-tree/binary-tree.d.ts +591 -402
  13. package/dist/data-structures/binary-tree/binary-tree.js +690 -604
  14. package/dist/data-structures/binary-tree/bst.d.ts +72 -65
  15. package/dist/data-structures/binary-tree/bst.js +122 -125
  16. package/dist/data-structures/binary-tree/rb-tree.d.ts +21 -24
  17. package/dist/data-structures/binary-tree/rb-tree.js +42 -39
  18. package/dist/data-structures/binary-tree/segment-tree.d.ts +2 -2
  19. package/dist/data-structures/binary-tree/segment-tree.js +2 -2
  20. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +27 -31
  21. package/dist/data-structures/binary-tree/tree-multi-map.js +45 -43
  22. package/dist/data-structures/graph/abstract-graph.d.ts +2 -2
  23. package/dist/data-structures/graph/abstract-graph.js +7 -4
  24. package/dist/data-structures/graph/directed-graph.d.ts +2 -2
  25. package/dist/data-structures/graph/directed-graph.js +4 -2
  26. package/dist/data-structures/graph/undirected-graph.d.ts +2 -2
  27. package/dist/data-structures/hash/hash-map.d.ts +2 -2
  28. package/dist/data-structures/hash/hash-map.js +1 -1
  29. package/dist/data-structures/heap/heap.js +3 -3
  30. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
  31. package/dist/data-structures/linked-list/doubly-linked-list.js +7 -7
  32. package/dist/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  33. package/dist/data-structures/linked-list/singly-linked-list.js +6 -6
  34. package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -2
  35. package/dist/data-structures/matrix/matrix.d.ts +2 -2
  36. package/dist/data-structures/matrix/navigator.d.ts +2 -2
  37. package/dist/data-structures/matrix/navigator.js +4 -2
  38. package/dist/data-structures/queue/deque.d.ts +3 -3
  39. package/dist/data-structures/queue/deque.js +29 -29
  40. package/dist/data-structures/queue/queue.d.ts +1 -1
  41. package/dist/data-structures/stack/stack.d.ts +2 -2
  42. package/dist/data-structures/trie/trie.d.ts +2 -2
  43. package/dist/data-structures/trie/trie.js +1 -1
  44. package/dist/index.d.ts +1 -0
  45. package/dist/index.js +1 -0
  46. package/dist/interfaces/binary-tree.d.ts +2 -2
  47. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -4
  48. package/dist/types/data-structures/binary-tree/binary-tree.js +0 -6
  49. package/package.json +2 -2
  50. package/src/constants/index.ts +4 -0
  51. package/src/data-structures/base/iterable-element-base.ts +11 -1
  52. package/src/data-structures/base/iterable-entry-base.ts +11 -19
  53. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +46 -50
  54. package/src/data-structures/binary-tree/avl-tree.ts +68 -71
  55. package/src/data-structures/binary-tree/binary-indexed-tree.ts +2 -2
  56. package/src/data-structures/binary-tree/binary-tree.ts +716 -748
  57. package/src/data-structures/binary-tree/bst.ts +137 -146
  58. package/src/data-structures/binary-tree/rb-tree.ts +46 -46
  59. package/src/data-structures/binary-tree/segment-tree.ts +2 -2
  60. package/src/data-structures/binary-tree/tree-multi-map.ts +49 -49
  61. package/src/data-structures/graph/abstract-graph.ts +6 -6
  62. package/src/data-structures/graph/directed-graph.ts +4 -4
  63. package/src/data-structures/graph/undirected-graph.ts +2 -2
  64. package/src/data-structures/hash/hash-map.ts +3 -3
  65. package/src/data-structures/heap/heap.ts +3 -3
  66. package/src/data-structures/linked-list/doubly-linked-list.ts +9 -9
  67. package/src/data-structures/linked-list/singly-linked-list.ts +8 -8
  68. package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
  69. package/src/data-structures/matrix/matrix.ts +2 -2
  70. package/src/data-structures/matrix/navigator.ts +4 -4
  71. package/src/data-structures/queue/deque.ts +31 -31
  72. package/src/data-structures/queue/queue.ts +1 -1
  73. package/src/data-structures/stack/stack.ts +2 -2
  74. package/src/data-structures/trie/trie.ts +3 -3
  75. package/src/index.ts +2 -1
  76. package/src/interfaces/binary-tree.ts +3 -3
  77. package/src/types/data-structures/binary-tree/binary-tree.ts +3 -5
@@ -2,76 +2,44 @@
2
2
  /**
3
3
  * data-structure-typed
4
4
  *
5
- * @author Tyler Zeng
6
- * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
5
+ * @author Pablo Zeng
6
+ * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
7
7
  * @license MIT License
8
8
  */
9
9
  Object.defineProperty(exports, "__esModule", { value: true });
10
10
  exports.BinaryTree = exports.BinaryTreeNode = void 0;
11
- const types_1 = require("../../types");
12
11
  const utils_1 = require("../../utils");
13
12
  const queue_1 = require("../queue");
14
13
  const base_1 = require("../base");
14
+ const constants_1 = require("../../constants");
15
15
  /**
16
16
  * Represents a node in a binary tree.
17
17
  * @template V - The type of data stored in the node.
18
18
  * @template NODE - The type of the family relationship in the binary tree.
19
19
  */
20
20
  class BinaryTreeNode {
21
- /**
22
- * The constructor function initializes an object with a key and an optional value.
23
- * @param {K} key - The "key" parameter is of type K, which represents the type of the key for the
24
- * constructor. It is used to set the key property of the object being created.
25
- * @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
26
- * value associated with the key in the constructor.
27
- */
28
21
  constructor(key, value) {
29
22
  this.key = key;
30
23
  this.value = value;
31
24
  }
32
- /**
33
- * The function returns the value of the `_left` property, which can be of type `NODE`, `null`, or
34
- * `undefined`.
35
- * @returns The left node of the current node is being returned. It can be either a NODE object,
36
- * null, or undefined.
37
- */
38
25
  get left() {
39
26
  return this._left;
40
27
  }
41
- /**
42
- * The function sets the left child of a node and updates its parent reference.
43
- * @param {OptBTNOrNull<NODE>} v - The parameter `v` can be of type `NODE`, `null`, or
44
- * `undefined`.
45
- */
46
28
  set left(v) {
47
29
  if (v) {
48
30
  v.parent = this;
49
31
  }
50
32
  this._left = v;
51
33
  }
52
- /**
53
- * The function returns the right node of a binary tree or null if it doesn't exist.
54
- * @returns The method is returning the value of the `_right` property, which can be a `NODE` object,
55
- * `null`, or `undefined`.
56
- */
57
34
  get right() {
58
35
  return this._right;
59
36
  }
60
- /**
61
- * The function sets the right child of a node and updates its parent.
62
- * @param {OptBTNOrNull<NODE>} v - The parameter `v` can be of type `NODE`, `null`, or
63
- * `undefined`.
64
- */
65
37
  set right(v) {
66
38
  if (v) {
67
39
  v.parent = this;
68
40
  }
69
41
  this._right = v;
70
42
  }
71
- /**
72
- * Get the position of the node within its family.
73
- * @returns {FamilyPosition} - The family position of the node.
74
- */
75
43
  get familyPosition() {
76
44
  const that = this;
77
45
  if (!this.parent) {
@@ -96,20 +64,20 @@ exports.BinaryTreeNode = BinaryTreeNode;
96
64
  */
97
65
  class BinaryTree extends base_1.IterableEntryBase {
98
66
  /**
99
- * The constructor function initializes a binary tree object with optional keysOrNodesOrEntriesOrRawElements and options.
100
- * @param [keysOrNodesOrEntriesOrRawElements] - Optional iterable of BTNKeyOrNodeOrEntry objects. These objects represent the
101
- * nodes to be added to the binary tree.
102
- * @param [options] - The `options` parameter is an optional object that can contain additional
103
- * configuration options for the binary tree. In this case, it is of type
104
- * `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
105
- * required.
67
+ * The constructor initializes a binary tree with optional options and adds keys, nodes, entries, or
68
+ * raw data if provided.
69
+ * @param keysOrNodesOrEntriesOrRaws - The `keysOrNodesOrEntriesOrRaws` parameter in the constructor
70
+ * is an iterable that can contain elements of type `BTNKeyOrNodeOrEntry<K, V, NODE>` or `R`. It is
71
+ * initialized with an empty array `[]` by default.
72
+ * @param [options] - The `options` parameter in the constructor is an object that can contain the
73
+ * following properties:
106
74
  */
107
- constructor(keysOrNodesOrEntriesOrRawElements = [], options) {
75
+ constructor(keysOrNodesOrEntriesOrRaws = [], options) {
108
76
  super();
109
77
  this.iterationType = 'ITERATIVE';
110
78
  this._size = 0;
111
79
  this._NIL = new BinaryTreeNode(NaN);
112
- this._DEFAULT_CALLBACK = (node) => (node ? node.key : undefined);
80
+ this._DEFAULT_BTN_CALLBACK = (node) => (node ? node.key : undefined);
113
81
  if (options) {
114
82
  const { iterationType, toEntryFn } = options;
115
83
  if (iterationType)
@@ -119,77 +87,68 @@ class BinaryTree extends base_1.IterableEntryBase {
119
87
  else if (toEntryFn)
120
88
  throw TypeError('toEntryFn must be a function type');
121
89
  }
122
- if (keysOrNodesOrEntriesOrRawElements)
123
- this.addMany(keysOrNodesOrEntriesOrRawElements);
90
+ if (keysOrNodesOrEntriesOrRaws)
91
+ this.addMany(keysOrNodesOrEntriesOrRaws);
124
92
  }
125
- /**
126
- * The function returns the root node, which can be of type NODE, null, or undefined.
127
- * @returns The method is returning the value of the `_root` property, which can be of type `NODE`,
128
- * `null`, or `undefined`.
129
- */
130
93
  get root() {
131
94
  return this._root;
132
95
  }
133
- /**
134
- * The function returns the size of an object.
135
- * @returns The size of the object, which is a number.
136
- */
137
96
  get size() {
138
97
  return this._size;
139
98
  }
140
- /**
141
- * The function returns the value of the _NIL property.
142
- * @returns The method is returning the value of the `_NIL` property.
143
- */
144
99
  get NIL() {
145
100
  return this._NIL;
146
101
  }
147
- /**
148
- * The function returns the value of the _toEntryFn property.
149
- * @returns The function being returned is `this._toEntryFn`.
150
- */
151
102
  get toEntryFn() {
152
103
  return this._toEntryFn;
153
104
  }
154
105
  /**
155
- * Creates a new instance of BinaryTreeNode with the given key and value.
156
- * @param {K} key - The key for the new node.
157
- * @param {V} value - The value for the new node.
158
- * @returns {NODE} - The newly created BinaryTreeNode.
106
+ * The function creates a new binary tree node with a specified key and optional value.
107
+ * @param {K} key - The `key` parameter is the key of the node being created in the binary tree.
108
+ * @param {V} [value] - The `value` parameter in the `createNode` function is optional, meaning it is
109
+ * not required to be provided when calling the function. If a `value` is provided, it should be of
110
+ * type `V`, which is the type of the value associated with the node.
111
+ * @returns A new BinaryTreeNode instance with the provided key and value is being returned, casted
112
+ * as NODE.
159
113
  */
160
114
  createNode(key, value) {
161
115
  return new BinaryTreeNode(key, value);
162
116
  }
163
117
  /**
164
- * The function creates a binary tree with the given options.
165
- * @param [options] - The `options` parameter is an optional object that allows you to customize the
166
- * behavior of the `BinaryTree` class. It is of type `Partial<BinaryTreeOptions>`, which means that
167
- * you can provide only a subset of the properties defined in the `BinaryTreeOptions` interface.
168
- * @returns a new instance of a binary tree.
118
+ * The function creates a binary tree with the specified options.
119
+ * @param [options] - The `options` parameter in the `createTree` function is an optional parameter
120
+ * that allows you to provide partial configuration options for creating a binary tree. It is of type
121
+ * `Partial<BinaryTreeOptions<K, V, R>>`, which means you can pass in an object containing a subset
122
+ * of properties
123
+ * @returns A new instance of a binary tree with the specified options is being returned.
169
124
  */
170
125
  createTree(options) {
171
- return new BinaryTree([], Object.assign({ iterationType: this.iterationType }, options));
126
+ return new BinaryTree([], Object.assign({ iterationType: this.iterationType, toEntryFn: this._toEntryFn }, options));
172
127
  }
173
128
  /**
174
- * The function `keyValueOrEntryOrRawElementToNode` converts a key-value pair, entry, or raw element
175
- * into a node object.
176
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
177
- * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
178
- * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
179
- * `keyValueOrEntryOrRawElementToNode` function. It represents the value associated with a key in a
180
- * key-value pair. If provided, it will be used to create a node with the specified key and value.
181
- * @returns The function `keyValueOrEntryOrRawElementToNode` returns either a `NODE` object, `null`,
182
- * or `undefined`.
129
+ * The function `keyValueOrEntryOrRawElementToNode` converts various input types into a node object
130
+ * or returns null.
131
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The
132
+ * `keyValueOrEntryOrRawElementToNode` function takes in a parameter `keyOrNodeOrEntryOrRaw`, which
133
+ * can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>` or `R`. This parameter represents either a key, a
134
+ * node, an entry
135
+ * @param {V} [value] - The `value` parameter in the `keyValueOrEntryOrRawElementToNode` function is
136
+ * an optional parameter of type `V`. It represents the value associated with the key in the node
137
+ * being created. If a `value` is provided, it will be used when creating the node. If
138
+ * @returns The `keyValueOrEntryOrRawElementToNode` function returns an optional node
139
+ * (`OptBTNOrNull<NODE>`) based on the input parameters provided. The function checks the type of the
140
+ * input parameter (`keyOrNodeOrEntryOrRaw`) and processes it accordingly to return a node or null
141
+ * value.
183
142
  */
184
- keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value) {
185
- if (keyOrNodeOrEntryOrRawElement === undefined)
143
+ keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw, value) {
144
+ if (keyOrNodeOrEntryOrRaw === undefined)
186
145
  return;
187
- if (keyOrNodeOrEntryOrRawElement === null)
146
+ if (keyOrNodeOrEntryOrRaw === null)
188
147
  return null;
189
- if (this.isNode(keyOrNodeOrEntryOrRawElement))
190
- return keyOrNodeOrEntryOrRawElement;
191
- if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
192
- const [key, entryValue] = keyOrNodeOrEntryOrRawElement;
148
+ if (this.isNode(keyOrNodeOrEntryOrRaw))
149
+ return keyOrNodeOrEntryOrRaw;
150
+ if (this.isEntry(keyOrNodeOrEntryOrRaw)) {
151
+ const [key, entryValue] = keyOrNodeOrEntryOrRaw;
193
152
  if (key === undefined)
194
153
  return;
195
154
  else if (key === null)
@@ -197,122 +156,139 @@ class BinaryTree extends base_1.IterableEntryBase {
197
156
  if (this.isKey(key))
198
157
  return this.createNode(key, value !== null && value !== void 0 ? value : entryValue);
199
158
  }
200
- if (this.toEntryFn) {
201
- const [key, entryValue] = this.toEntryFn(keyOrNodeOrEntryOrRawElement);
159
+ if (this._toEntryFn) {
160
+ const [key, entryValue] = this._toEntryFn(keyOrNodeOrEntryOrRaw);
202
161
  if (this.isKey(key))
203
162
  return this.createNode(key, value !== null && value !== void 0 ? value : entryValue);
204
163
  else
205
164
  return;
206
165
  }
207
- if (this.isKey(keyOrNodeOrEntryOrRawElement))
208
- return this.createNode(keyOrNodeOrEntryOrRawElement, value);
166
+ if (this.isKey(keyOrNodeOrEntryOrRaw))
167
+ return this.createNode(keyOrNodeOrEntryOrRaw, value);
209
168
  return;
210
169
  }
211
170
  /**
212
171
  * Time Complexity: O(n)
213
172
  * Space Complexity: O(log n)
214
173
  *
215
- * The `ensureNode` function checks if the input is a valid node and returns it, or converts it to a
216
- * node if it is a key or entry.
217
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
218
- * `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, `BTNKeyOrNodeOrEntry<K, V, NODE>`, or
219
- * a raw element.
220
- * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
221
- * parameter that specifies the type of iteration to be used when searching for a node. It has a
222
- * default value of `'ITERATIVE'`.
223
- * @returns The function `ensureNode` returns either a `NODE` object, `null`, or `undefined`.
174
+ * The function `ensureNode` in TypeScript checks if a given input is a node, entry, key, or raw
175
+ * value and returns the corresponding node or null.
176
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The `keyOrNodeOrEntryOrRaw`
177
+ * parameter in the `ensureNode` function can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>` or `R`. It
178
+ * is used to determine whether the input is a key, node, entry, or raw data. The
179
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `ensureNode` function
180
+ * is used to specify the type of iteration to be performed. It has a default value of
181
+ * `this.iterationType` if not explicitly provided.
182
+ * @returns The `ensureNode` function returns either a node, `null`, or `undefined` based on the
183
+ * conditions specified in the code snippet.
224
184
  */
225
- ensureNode(keyOrNodeOrEntryOrRawElement, iterationType = this.iterationType) {
226
- if (keyOrNodeOrEntryOrRawElement === null)
185
+ ensureNode(keyOrNodeOrEntryOrRaw, iterationType = this.iterationType) {
186
+ if (keyOrNodeOrEntryOrRaw === null)
227
187
  return null;
228
- if (keyOrNodeOrEntryOrRawElement === undefined)
188
+ if (keyOrNodeOrEntryOrRaw === undefined)
229
189
  return;
230
- if (keyOrNodeOrEntryOrRawElement === this.NIL)
190
+ if (keyOrNodeOrEntryOrRaw === this._NIL)
231
191
  return;
232
- if (this.isNode(keyOrNodeOrEntryOrRawElement))
233
- return keyOrNodeOrEntryOrRawElement;
234
- if (this.toEntryFn) {
235
- const [key] = this.toEntryFn(keyOrNodeOrEntryOrRawElement);
236
- if (this.isKey(key))
237
- return this.getNodeByKey(key);
238
- }
239
- if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
240
- const key = keyOrNodeOrEntryOrRawElement[0];
192
+ if (this.isNode(keyOrNodeOrEntryOrRaw))
193
+ return keyOrNodeOrEntryOrRaw;
194
+ if (this.isEntry(keyOrNodeOrEntryOrRaw)) {
195
+ const key = keyOrNodeOrEntryOrRaw[0];
241
196
  if (key === null)
242
197
  return null;
243
198
  if (key === undefined)
244
199
  return;
245
200
  return this.getNodeByKey(key, iterationType);
246
201
  }
247
- if (this.isKey(keyOrNodeOrEntryOrRawElement))
248
- return this.getNodeByKey(keyOrNodeOrEntryOrRawElement, iterationType);
202
+ if (this._toEntryFn) {
203
+ const [key] = this._toEntryFn(keyOrNodeOrEntryOrRaw);
204
+ if (this.isKey(key))
205
+ return this.getNodeByKey(key);
206
+ }
207
+ if (this.isKey(keyOrNodeOrEntryOrRaw))
208
+ return this.getNodeByKey(keyOrNodeOrEntryOrRaw, iterationType);
249
209
  return;
250
210
  }
251
211
  /**
252
- * The function checks if the input is an instance of the BinaryTreeNode class.
253
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
254
- * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
255
- * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
256
- * an instance of the `BinaryTreeNode` class.
212
+ * The function isNode checks if the input is an instance of BinaryTreeNode.
213
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
214
+ * `keyOrNodeOrEntryOrRaw` can be either a key, a node, an entry, or raw data. The function is
215
+ * checking if the input is an instance of a `BinaryTreeNode` and returning a boolean value
216
+ * accordingly.
217
+ * @returns The function `isNode` is checking if the input `keyOrNodeOrEntryOrRaw` is an instance of
218
+ * `BinaryTreeNode`. If it is, the function returns `true`, indicating that the input is a node. If
219
+ * it is not an instance of `BinaryTreeNode`, the function returns `false`, indicating that the input
220
+ * is not a node.
257
221
  */
258
- isNode(keyOrNodeOrEntryOrRawElement) {
259
- return keyOrNodeOrEntryOrRawElement instanceof BinaryTreeNode;
222
+ isNode(keyOrNodeOrEntryOrRaw) {
223
+ return keyOrNodeOrEntryOrRaw instanceof BinaryTreeNode;
260
224
  }
261
225
  /**
262
- * The function checks if a given node is a valid node in a binary search tree.
263
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
264
- * `BTNKeyOrNodeOrEntry<K, V, NODE>`.
265
- * @returns a boolean value.
226
+ * The function `isRealNode` checks if a given input is a valid node in a binary tree.
227
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The `keyOrNodeOrEntryOrRaw`
228
+ * parameter in the `isRealNode` function can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>` or `R`.
229
+ * The function checks if the input parameter is a `NODE` type by verifying if it is not equal
230
+ * @returns The function `isRealNode` is checking if the input `keyOrNodeOrEntryOrRaw` is a valid
231
+ * node by comparing it to `this._NIL`, `null`, and `undefined`. If the input is not one of these
232
+ * values, it then calls the `isNode` method to further determine if the input is a node. The
233
+ * function will return a boolean value indicating whether the
266
234
  */
267
- isRealNode(node) {
268
- if (node === this.NIL || node === null || node === undefined)
235
+ isRealNode(keyOrNodeOrEntryOrRaw) {
236
+ if (keyOrNodeOrEntryOrRaw === this._NIL || keyOrNodeOrEntryOrRaw === null || keyOrNodeOrEntryOrRaw === undefined)
269
237
  return false;
270
- return this.isNode(node);
238
+ return this.isNode(keyOrNodeOrEntryOrRaw);
271
239
  }
272
240
  /**
273
- * The function checks if a given node is a real node or null.
274
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
275
- * `BTNKeyOrNodeOrEntry<K, V, NODE>`.
276
- * @returns a boolean value.
241
+ * The function checks if a given input is a valid node or null.
242
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
243
+ * `keyOrNodeOrEntryOrRaw` in the `isRealNodeOrNull` function can be of type `BTNKeyOrNodeOrEntry<K,
244
+ * V, NODE>` or `R`. It is a union type that can either be a key, a node, an entry, or
245
+ * @returns The function `isRealNodeOrNull` is returning a boolean value. It checks if the input
246
+ * `keyOrNodeOrEntryOrRaw` is either `null` or a real node, and returns `true` if it is a node or
247
+ * `null`, and `false` otherwise.
277
248
  */
278
- isRealNodeOrNull(node) {
279
- return node === null || this.isRealNode(node);
249
+ isRealNodeOrNull(keyOrNodeOrEntryOrRaw) {
250
+ return keyOrNodeOrEntryOrRaw === null || this.isRealNode(keyOrNodeOrEntryOrRaw);
280
251
  }
281
252
  /**
282
- * The function checks if a given node is equal to the NIL value.
283
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
284
- * `BTNKeyOrNodeOrEntry<K, V, NODE>`.
285
- * @returns a boolean value.
253
+ * The function isNIL checks if a given key, node, entry, or raw value is equal to the _NIL value.
254
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - BTNKeyOrNodeOrEntry<K, V,
255
+ * NODE> | R
256
+ * @returns The function is checking if the `keyOrNodeOrEntryOrRaw` parameter is equal to the `_NIL`
257
+ * property of the current object and returning a boolean value based on that comparison.
286
258
  */
287
- isNIL(node) {
288
- return node === this.NIL;
259
+ isNIL(keyOrNodeOrEntryOrRaw) {
260
+ return keyOrNodeOrEntryOrRaw === this._NIL;
289
261
  }
290
262
  /**
291
- * The function `isLeaf` determines whether a given node is a leaf node in a binary tree structure.
292
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} node - The `node` parameter in the `isLeaf` function
293
- * can be either a regular node (`R`) or a `BTNKeyOrNodeOrEntry<K, V, NODE>`.
294
- * @returns The `isLeaf` function is checking if the provided node is a leaf node in a binary tree.
295
- * If the node is `undefined`, it returns `false`. If the node is `null`, it returns `true`.
296
- * Otherwise, it checks if both the left and right children of the node are not real nodes, and
297
- * returns `true` if they are not, indicating that the node is a
263
+ * The function determines whether a given key, node, entry, or raw data is a leaf node in a binary
264
+ * tree.
265
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
266
+ * `keyOrNodeOrEntryOrRaw` can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>` or `R`. It represents a
267
+ * key, node, entry, or raw data in a binary tree structure. The function `isLeaf` checks whether the
268
+ * provided
269
+ * @returns The function `isLeaf` returns a boolean value indicating whether the input
270
+ * `keyOrNodeOrEntryOrRaw` is a leaf node in a binary tree.
298
271
  */
299
- isLeaf(node) {
300
- node = this.ensureNode(node);
301
- if (node === undefined)
272
+ isLeaf(keyOrNodeOrEntryOrRaw) {
273
+ keyOrNodeOrEntryOrRaw = this.ensureNode(keyOrNodeOrEntryOrRaw);
274
+ if (keyOrNodeOrEntryOrRaw === undefined)
302
275
  return false;
303
- if (node === null)
276
+ if (keyOrNodeOrEntryOrRaw === null)
304
277
  return true;
305
- return !this.isRealNode(node.left) && !this.isRealNode(node.right);
278
+ return !this.isRealNode(keyOrNodeOrEntryOrRaw.left) && !this.isRealNode(keyOrNodeOrEntryOrRaw.right);
306
279
  }
307
280
  /**
308
- * The function checks if the input is an array with two elements, indicating it is a binary tree
309
- * node entry.
310
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
311
- * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
312
- * @returns a boolean value.
281
+ * The function `isEntry` checks if the input is a BTNEntry object by verifying if it is an array
282
+ * with a length of 2.
283
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The `keyOrNodeOrEntryOrRaw`
284
+ * parameter in the `isEntry` function can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>` or type `R`.
285
+ * The function checks if the provided `keyOrNodeOrEntryOrRaw` is of type `BTN
286
+ * @returns The `isEntry` function is checking if the `keyOrNodeOrEntryOrRaw` parameter is an array
287
+ * with a length of 2. If it is, then it returns `true`, indicating that the parameter is of type
288
+ * `BTNEntry<K, V>`. If the condition is not met, it returns `false`.
313
289
  */
314
- isEntry(keyOrNodeOrEntryOrRawElement) {
315
- return Array.isArray(keyOrNodeOrEntryOrRawElement) && keyOrNodeOrEntryOrRawElement.length === 2;
290
+ isEntry(keyOrNodeOrEntryOrRaw) {
291
+ return Array.isArray(keyOrNodeOrEntryOrRaw) && keyOrNodeOrEntryOrRaw.length === 2;
316
292
  }
317
293
  /**
318
294
  * Time Complexity O(1)
@@ -334,29 +310,30 @@ class BinaryTree extends base_1.IterableEntryBase {
334
310
  * Time Complexity O(n)
335
311
  * Space Complexity O(1)
336
312
  *
337
- * The `add` function is used to insert a new node into a binary tree, checking for duplicate keys
338
- * and finding the appropriate insertion position.
339
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
340
- * `keyOrNodeOrEntryOrRawElement` parameter can accept a value of type `R`, which represents the key,
341
- * node, entry, or raw element to be added to the tree. It can also accept a value of type
342
- * `BTNKeyOrNodeOrEntry<K, V, NODE>
343
- * @param {V} [value] - The `value` parameter is an optional value that can be associated with the
344
- * key being added to the tree. It represents the value that will be stored in the tree for the given
345
- * key.
346
- * @returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
347
- * insertion position cannot be found or if there are duplicate keys.
313
+ * The `add` function in TypeScript adds a new node to a binary tree while handling duplicate keys
314
+ * and finding the correct insertion position.
315
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The `add` method you provided
316
+ * seems to be for adding a new node to a binary tree structure. The `keyOrNodeOrEntryOrRaw`
317
+ * parameter in the method can accept different types of values:
318
+ * @param {V} [value] - The `value` parameter in the `add` method represents the value associated
319
+ * with the key that you want to add to the binary tree. When adding a key-value pair to the binary
320
+ * tree, you provide the key and its corresponding value. The `add` method then creates a new node
321
+ * with this
322
+ * @returns The `add` method returns a boolean value. It returns `true` if the insertion of the new
323
+ * node was successful, and `false` if the insertion position could not be found or if a duplicate
324
+ * key was found and the node was replaced instead of inserted.
348
325
  */
349
- add(keyOrNodeOrEntryOrRawElement, value) {
350
- const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
326
+ add(keyOrNodeOrEntryOrRaw, value) {
327
+ const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw, value);
351
328
  if (newNode === undefined)
352
329
  return false;
353
330
  // If the tree is empty, directly set the new node as the root node
354
- if (!this.root) {
355
- this._root = newNode;
331
+ if (!this._root) {
332
+ this._setRoot(newNode);
356
333
  this._size = 1;
357
334
  return true;
358
335
  }
359
- const queue = new queue_1.Queue([this.root]);
336
+ const queue = new queue_1.Queue([this._root]);
360
337
  let potentialParent; // Record the parent node of the potential insertion location
361
338
  while (queue.size > 0) {
362
339
  const cur = queue.shift();
@@ -373,10 +350,12 @@ class BinaryTree extends base_1.IterableEntryBase {
373
350
  }
374
351
  // Continue traversing the left and right subtrees
375
352
  if (cur.left !== null) {
376
- cur.left && queue.push(cur.left);
353
+ if (cur.left)
354
+ queue.push(cur.left);
377
355
  }
378
356
  if (cur.right !== null) {
379
- cur.right && queue.push(cur.right);
357
+ if (cur.right)
358
+ queue.push(cur.right);
380
359
  }
381
360
  }
382
361
  // At the end of the traversal, if the insertion position is found, insert
@@ -396,24 +375,28 @@ class BinaryTree extends base_1.IterableEntryBase {
396
375
  * Time Complexity: O(k * n)
397
376
  * Space Complexity: O(1)
398
377
  *
399
- * The `addMany` function takes in an iterable of keys or nodes or entries or raw elements, and an
400
- * optional iterable of values, and adds each key or node or entry with its corresponding value to a
401
- * data structure, returning an array of booleans indicating whether each insertion was successful.
402
- * @param keysOrNodesOrEntriesOrRawElements - An iterable containing keys, nodes, entries, or raw
403
- * elements. These elements will be added to the data structure.
404
- * @param [values] - An optional iterable of values that correspond to the keys or nodes or entries
405
- * in the `keysOrNodesOrEntriesOrRawElements` parameter.
406
- * @returns The function `addMany` returns an array of booleans indicating whether each element was
407
- * successfully added to the data structure.
378
+ * The `addMany` function takes in multiple keys or nodes or entries or raw values along with
379
+ * optional values, and adds them to a data structure while returning an array indicating whether
380
+ * each insertion was successful.
381
+ * @param keysOrNodesOrEntriesOrRaws - `keysOrNodesOrEntriesOrRaws` is an iterable that can contain a
382
+ * mix of keys, nodes, entries, or raw values. Each element in this iterable can be of type
383
+ * `BTNKeyOrNodeOrEntry<K, V, NODE>` or `R`.
384
+ * @param [values] - The `values` parameter in the `addMany` function is an optional parameter that
385
+ * accepts an iterable of values. These values correspond to the keys or nodes being added in the
386
+ * `keysOrNodesOrEntriesOrRaws` parameter. If provided, the function will iterate over the values and
387
+ * assign them
388
+ * @returns The `addMany` method returns an array of boolean values indicating whether each key,
389
+ * node, entry, or raw value was successfully added to the data structure. Each boolean value
390
+ * corresponds to the success of adding the corresponding key or value in the input iterable.
408
391
  */
409
- addMany(keysOrNodesOrEntriesOrRawElements, values) {
392
+ addMany(keysOrNodesOrEntriesOrRaws, values) {
410
393
  // TODO not sure addMany not be run multi times
411
394
  const inserted = [];
412
395
  let valuesIterator;
413
396
  if (values) {
414
397
  valuesIterator = values[Symbol.iterator]();
415
398
  }
416
- for (const keyOrNodeOrEntryOrRawElement of keysOrNodesOrEntriesOrRawElements) {
399
+ for (const keyOrNodeOrEntryOrRaw of keysOrNodesOrEntriesOrRaws) {
417
400
  let value = undefined;
418
401
  if (valuesIterator) {
419
402
  const valueResult = valuesIterator.next();
@@ -421,7 +404,7 @@ class BinaryTree extends base_1.IterableEntryBase {
421
404
  value = valueResult.value;
422
405
  }
423
406
  }
424
- inserted.push(this.add(keyOrNodeOrEntryOrRawElement, value));
407
+ inserted.push(this.add(keyOrNodeOrEntryOrRaw, value));
425
408
  }
426
409
  return inserted;
427
410
  }
@@ -429,38 +412,38 @@ class BinaryTree extends base_1.IterableEntryBase {
429
412
  * Time Complexity: O(k * n)
430
413
  * Space Complexity: O(1)
431
414
  *
432
- * The `refill` function clears the current data and adds new data to the collection.
433
- * @param keysOrNodesOrEntriesOrRawElements - An iterable collection of keys, nodes, entries, or raw
434
- * elements. These can be of any type (R) or a specific type (BTNKeyOrNodeOrEntry<K, V, NODE>).
435
- * @param [values] - The `values` parameter is an optional iterable of values that will be associated
436
- * with the keys or nodes being added. If provided, the values will be assigned to the corresponding
437
- * keys or nodes. If not provided, the values will be set to `undefined`.
415
+ * The `refill` function clears the existing data structure and then adds new key-value pairs based
416
+ * on the provided input.
417
+ * @param keysOrNodesOrEntriesOrRaws - The `keysOrNodesOrEntriesOrRaws` parameter in the `refill`
418
+ * method can accept an iterable containing a mix of `BTNKeyOrNodeOrEntry<K, V, NODE>` objects or `R`
419
+ * objects.
420
+ * @param [values] - The `values` parameter in the `refill` method is an optional parameter that
421
+ * accepts an iterable of values of type `V` or `undefined`.
438
422
  */
439
- refill(keysOrNodesOrEntriesOrRawElements, values) {
423
+ refill(keysOrNodesOrEntriesOrRaws, values) {
440
424
  this.clear();
441
- this.addMany(keysOrNodesOrEntriesOrRawElements, values);
425
+ this.addMany(keysOrNodesOrEntriesOrRaws, values);
442
426
  }
443
427
  /**
444
428
  * Time Complexity: O(n)
445
429
  * Space Complexity: O(1)
446
430
  *
447
- * The above function is a TypeScript implementation of deleting a node from a binary tree, returning
448
- * the deleted node and the node that needs to be balanced.
449
- * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
450
- * used to identify the node that needs to be deleted from the binary tree. It can be of any type
451
- * that is returned by the callback function.
452
- * @param {C} callback - The `callback` parameter is a function that is used to determine the
453
- * identifier of the node to be deleted. It is of type `C`, which extends the `BTNCallback<NODE>`
454
- * interface. The `BTNCallback<NODE>` interface represents a callback function that takes a node of
455
- * type `NODE
456
- * @returns an array of `BinaryTreeDeleteResult<NODE>`.
431
+ * The function `delete` in TypeScript implements the deletion of a node in a binary tree and returns
432
+ * the deleted node along with information for tree balancing.
433
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw
434
+ * - The `delete` method you provided is used to delete a node from a binary tree based on the key,
435
+ * node, entry or raw data. The method returns an array of
436
+ * `BinaryTreeDeleteResult` objects containing information about the deleted node and whether
437
+ * balancing is needed.
438
+ * @returns The `delete` method returns an array of `BinaryTreeDeleteResult` objects. Each object in
439
+ * the array contains information about the node that was deleted (`deleted`) and the node that may
440
+ * need to be balanced (`needBalanced`).
457
441
  */
458
- delete(identifier, callback = this._DEFAULT_CALLBACK) {
442
+ delete(keyOrNodeOrEntryOrRaw) {
459
443
  const deletedResult = [];
460
- if (!this.root)
444
+ if (!this._root)
461
445
  return deletedResult;
462
- callback = this._ensureCallback(identifier, callback);
463
- const curr = this.getNode(identifier, callback);
446
+ const curr = this.getNode(keyOrNodeOrEntryOrRaw);
464
447
  if (!curr)
465
448
  return deletedResult;
466
449
  const parent = curr === null || curr === void 0 ? void 0 : curr.parent;
@@ -497,7 +480,7 @@ class BinaryTree extends base_1.IterableEntryBase {
497
480
  this._setRoot(curr.right);
498
481
  curr.right = undefined;
499
482
  }
500
- this._size = this.size - 1;
483
+ this._size = this._size - 1;
501
484
  deletedResult.push({ deleted: orgCurrent, needBalanced });
502
485
  return deletedResult;
503
486
  }
@@ -505,47 +488,46 @@ class BinaryTree extends base_1.IterableEntryBase {
505
488
  * Time Complexity: O(n)
506
489
  * Space Complexity: O(k + log n)
507
490
  *
508
- * The function `getNodes` returns an array of nodes that match a given identifier, using either a
509
- * recursive or iterative approach.
510
- * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
511
- * that is used to identify the nodes. It can be of any type and is used to match against the result
512
- * of the callback function for each node.
513
- * @param {C} callback - The `callback` parameter is a function that takes a node as input and
514
- * returns a value. This value is used to identify the nodes that match the given identifier. The
515
- * `callback` function is optional and defaults to a default callback function
516
- * (`this._DEFAULT_CALLBACK`) if not provided.
517
- * @param [onlyOne=false] - A boolean value indicating whether to return only one node that matches
518
- * the identifier or all nodes that match the identifier. If set to true, only the first matching
519
- * node will be returned. If set to false, all matching nodes will be returned. The default value is
520
- * false.
521
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
522
- * point for the search. It can be either a node object, a key-value pair, or a key. If it is not
523
- * provided, the `root` of the data structure is used as the starting point.
524
- * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
525
- * iteration to be performed on the nodes of a binary tree. It can have two possible values:
526
- * @returns an array of NODE objects.
491
+ * The function `getNodes` retrieves nodes from a binary tree based on a key, node, entry, raw data,
492
+ * or predicate, with options for recursive or iterative traversal.
493
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrNodeOrEntryOrRawOrPredicate
494
+ * - The `getNodes` function you provided takes several parameters:
495
+ * @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` function is a boolean flag that
496
+ * determines whether to return only the first node that matches the criteria specified by the
497
+ * `keyOrNodeOrEntryOrRawOrPredicate` parameter. If `onlyOne` is set to `true`, the function will
498
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
499
+ * `getNodes` function is used to specify the starting point for traversing the binary tree. It
500
+ * represents the root node of the binary tree or the node from which the traversal should begin. If
501
+ * not provided, the default value is set to `this._root
502
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` function
503
+ * determines the type of iteration to be performed when traversing the nodes of a binary tree. It
504
+ * can have two possible values:
505
+ * @returns The `getNodes` function returns an array of nodes that satisfy the provided condition
506
+ * based on the input parameters and the iteration type specified.
527
507
  */
528
- getNodes(identifier, callback = this._DEFAULT_CALLBACK, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
529
- if (identifier === undefined)
508
+ getNodes(keyOrNodeOrEntryOrRawOrPredicate, onlyOne = false, beginRoot = this._root, iterationType = this.iterationType) {
509
+ if (keyOrNodeOrEntryOrRawOrPredicate === undefined)
530
510
  return [];
531
- if (identifier === null)
511
+ if (keyOrNodeOrEntryOrRawOrPredicate === null)
532
512
  return [];
533
513
  beginRoot = this.ensureNode(beginRoot);
534
514
  if (!beginRoot)
535
515
  return [];
536
- callback = this._ensureCallback(identifier, callback);
516
+ const callback = this._ensurePredicate(keyOrNodeOrEntryOrRawOrPredicate);
537
517
  const ans = [];
538
518
  if (iterationType === 'RECURSIVE') {
539
519
  const dfs = (cur) => {
540
- if (callback(cur) === identifier) {
520
+ if (callback(cur)) {
541
521
  ans.push(cur);
542
522
  if (onlyOne)
543
523
  return;
544
524
  }
545
525
  if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
546
526
  return;
547
- this.isRealNode(cur.left) && dfs(cur.left);
548
- this.isRealNode(cur.right) && dfs(cur.right);
527
+ if (this.isRealNode(cur.left))
528
+ dfs(cur.left);
529
+ if (this.isRealNode(cur.right))
530
+ dfs(cur.right);
549
531
  };
550
532
  dfs(beginRoot);
551
533
  }
@@ -554,13 +536,15 @@ class BinaryTree extends base_1.IterableEntryBase {
554
536
  while (stack.length > 0) {
555
537
  const cur = stack.pop();
556
538
  if (this.isRealNode(cur)) {
557
- if (callback(cur) === identifier) {
539
+ if (callback(cur)) {
558
540
  ans.push(cur);
559
541
  if (onlyOne)
560
542
  return ans;
561
543
  }
562
- this.isRealNode(cur.left) && stack.push(cur.left);
563
- this.isRealNode(cur.right) && stack.push(cur.right);
544
+ if (this.isRealNode(cur.left))
545
+ stack.push(cur.left);
546
+ if (this.isRealNode(cur.right))
547
+ stack.push(cur.right);
564
548
  }
565
549
  }
566
550
  }
@@ -570,93 +554,97 @@ class BinaryTree extends base_1.IterableEntryBase {
570
554
  * Time Complexity: O(n)
571
555
  * Space Complexity: O(log n).
572
556
  *
573
- * The function `getNode` returns the first node that matches the given identifier and callback,
574
- * starting from the specified root node and using the specified iteration type.
575
- * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
576
- * used to identify the node you want to retrieve. It can be of any type that is the return type of
577
- * the `C` callback function, or it can be `null` or `undefined`.
578
- * @param {C} callback - The `callback` parameter is a function that will be used to determine if a
579
- * node matches the desired criteria. It should return a value that can be used to identify the node.
580
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
581
- * point for searching nodes in a tree structure. It can be either a root node, a key-value pair, or
582
- * a node entry. If not provided, the search will start from the root of the tree.
583
- * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
584
- * of iteration to be performed when searching for nodes. It can have one of the following values:
585
- * @returns The method is returning a NODE object, or null, or undefined.
557
+ * The `getNode` function retrieves a node based on the provided key, node, entry, raw data, or
558
+ * predicate.
559
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrNodeOrEntryOrRawOrPredicate
560
+ * - The `keyOrNodeOrEntryOrRawOrPredicate` parameter in the `getNode` function can accept a key,
561
+ * node, entry, raw data, or a predicate function.
562
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
563
+ * `getNode` function is used to specify the starting point for searching for a node in a binary
564
+ * tree. If no specific starting point is provided, the default value is set to `this._root`, which
565
+ * is typically the root node of the binary tree.
566
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `getNode` method is
567
+ * used to specify the type of iteration to be performed when searching for a node. It has a default
568
+ * value of `this.iterationType`, which means it will use the iteration type defined in the current
569
+ * context if no specific value is provided
570
+ * @returns The `getNode` function is returning the first node that matches the specified criteria,
571
+ * or `null` if no matching node is found.
586
572
  */
587
- getNode(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
573
+ getNode(keyOrNodeOrEntryOrRawOrPredicate, beginRoot = this._root, iterationType = this.iterationType) {
588
574
  var _a;
589
- return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
575
+ return (_a = this.getNodes(keyOrNodeOrEntryOrRawOrPredicate, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
590
576
  }
591
577
  /**
592
578
  * Time Complexity: O(n)
593
579
  * Space Complexity: O(log n)
594
580
  *
595
- * The function `getNodeByKey` returns a node with a specific key value from a tree structure.
596
- * @param {K} key - The key parameter is the value that you want to search for in the tree. It is
597
- * used to find the node with the matching key value.
598
- * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
599
- * parameter that specifies the type of iteration to be used when searching for a node in the tree.
600
- * It has a default value of `'ITERATIVE'`.
601
- * @returns a value of type NODE, null, or undefined.
581
+ * The function `getNodeByKey` retrieves a node by its key from a binary tree structure.
582
+ * @param {K} key - The `key` parameter is the value used to search for a specific node in a data
583
+ * structure.
584
+ * @param {IterationType} iterationType - The `iterationType` parameter is a type of iteration that
585
+ * specifies how the tree nodes should be traversed when searching for a node with the given key. It
586
+ * is an optional parameter with a default value of `this.iterationType`.
587
+ * @returns The `getNodeByKey` function is returning an optional binary tree node
588
+ * (`OptBTNOrNull<NODE>`).
602
589
  */
603
590
  getNodeByKey(key, iterationType = this.iterationType) {
604
- return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
591
+ return this.getNode(key, this._root, iterationType);
605
592
  }
606
593
  /**
607
594
  * Time Complexity: O(n)
608
595
  * Space Complexity: O(log n)
609
596
  *
610
- * The function `get` in TypeScript overrides the base class method and returns the value associated
611
- * with the given identifier.
612
- * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
613
- * used to identify the node in the binary tree. It can be of any type that is returned by the
614
- * callback function `C`. It can also be `null` or `undefined` if no identifier is provided.
615
- * @param {C} callback - The `callback` parameter is a function that will be used to determine if a
616
- * node matches the given identifier. It is optional and defaults to `this._DEFAULT_CALLBACK`.
617
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
618
- * point for the search in the binary tree. It can be either a root node of the tree or a key, node,
619
- * or entry object that exists in the tree. If no specific starting point is provided, the search
620
- * will begin from the root of the
621
- * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
622
- * of iteration to be performed when searching for a node in the tree. It can have one of the
623
- * following values:
624
- * @returns The method is returning the value associated with the specified identifier in the binary
625
- * tree.
597
+ * This function overrides the `get` method to retrieve the value associated with a specified key,
598
+ * node, entry, raw data, or predicate in a data structure.
599
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrNodeOrEntryOrRawOrPredicate
600
+ * - The `keyOrNodeOrEntryOrRawOrPredicate` parameter in the `get` method can accept one of the
601
+ * following types:
602
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `get`
603
+ * method is used to specify the starting point for searching for a key or node in the binary tree.
604
+ * If no specific starting point is provided, the default starting point is the root of the binary
605
+ * tree (`this._root`).
606
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `get` method is used
607
+ * to specify the type of iteration to be performed when searching for a key in the binary tree. It
608
+ * is an optional parameter with a default value of `this.iterationType`, which means it will use the
609
+ * iteration type defined in the
610
+ * @returns The `get` method is returning the value associated with the specified key, node, entry,
611
+ * raw data, or predicate in the binary tree map. If the specified key or node is found in the tree,
612
+ * the method returns the corresponding value. If the key or node is not found, it returns
613
+ * `undefined`.
626
614
  */
627
- get(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
615
+ get(keyOrNodeOrEntryOrRawOrPredicate, beginRoot = this._root, iterationType = this.iterationType) {
628
616
  var _a;
629
- return (_a = this.getNode(identifier, callback, beginRoot, iterationType)) === null || _a === void 0 ? void 0 : _a.value;
617
+ return (_a = this.getNode(keyOrNodeOrEntryOrRawOrPredicate, beginRoot, iterationType)) === null || _a === void 0 ? void 0 : _a.value;
630
618
  }
631
619
  /**
632
620
  * Time Complexity: O(n)
633
621
  * Space Complexity: O(log n)
634
622
  *
635
- * The `has` function checks if a given identifier exists in the data structure and returns a boolean
636
- * value.
637
- * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
638
- * used to identify a specific node or entry in the data structure. It can be of any type that is
639
- * returned by the callback function `C`. It can also be `null` or `undefined` if no specific
640
- * identifier is provided.
641
- * @param {C} callback - The `callback` parameter is a function that will be used to determine
642
- * whether a node should be included in the result or not. It is of type `C`, which extends the
643
- * `BTNCallback<NODE>` type.
644
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
645
- * point for the iteration in the data structure. It can be either a root node, a key-value pair, or
646
- * a node entry. If not specified, it defaults to the root of the data structure.
647
- * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
648
- * of iteration to be performed. It is an optional parameter with a default value of `IterationType`.
649
- * @returns The method is returning a boolean value.
623
+ * The `has` function in TypeScript checks if a specified key, node, entry, raw data, or predicate
624
+ * exists in the data structure.
625
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrNodeOrEntryOrRawOrPredicate
626
+ * - The `keyOrNodeOrEntryOrRawOrPredicate` parameter in the `override has` method can accept one of
627
+ * the following types:
628
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
629
+ * `override` method is used to specify the starting point for the search operation within the data
630
+ * structure. It defaults to `this._root` if not provided explicitly.
631
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `override has` method
632
+ * is used to specify the type of iteration to be performed. It has a default value of
633
+ * `this.iterationType`, which means it will use the iteration type defined in the current context if
634
+ * no value is provided when calling the method.
635
+ * @returns The `override has` method is returning a boolean value. It checks if there are any nodes
636
+ * that match the provided key, node, entry, raw data, or predicate in the tree structure. If there
637
+ * are matching nodes, it returns `true`, indicating that the tree contains the specified element.
638
+ * Otherwise, it returns `false`.
650
639
  */
651
- has(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
652
- callback = this._ensureCallback(identifier, callback);
653
- return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
640
+ has(keyOrNodeOrEntryOrRawOrPredicate, beginRoot = this._root, iterationType = this.iterationType) {
641
+ return this.getNodes(keyOrNodeOrEntryOrRawOrPredicate, true, beginRoot, iterationType).length > 0;
654
642
  }
655
643
  /**
656
644
  * Time Complexity: O(1)
657
645
  * Space Complexity: O(1)
658
646
  *
659
- * Clear the binary tree, removing all nodes.
647
+ * The `clear` function resets the root node and size of a data structure to empty.
660
648
  */
661
649
  clear() {
662
650
  this._setRoot(undefined);
@@ -666,42 +654,51 @@ class BinaryTree extends base_1.IterableEntryBase {
666
654
  * Time Complexity: O(1)
667
655
  * Space Complexity: O(1)
668
656
  *
669
- * Check if the binary tree is empty.
670
- * @returns {boolean} - True if the binary tree is empty, false otherwise.
657
+ * The `isEmpty` function in TypeScript checks if a data structure has no elements and returns a
658
+ * boolean value.
659
+ * @returns The `isEmpty()` method is returning a boolean value, specifically `true` if the `_size`
660
+ * property is equal to 0, indicating that the data structure is empty, and `false` otherwise.
671
661
  */
672
662
  isEmpty() {
673
- return this.size === 0;
663
+ return this._size === 0;
674
664
  }
675
665
  /**
676
666
  * Time Complexity: O(n)
677
667
  * Space Complexity: O(log n)
678
668
  *
679
- * The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
680
- * height of the tree.
681
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The parameter `beginRoot` is optional and
682
- * has a default value of `this.root`. It represents the starting point for checking if the tree is
683
- * perfectly balanced. It can be either a root node (`R`), a key or node or entry
684
- * (`BTNKeyOrNodeOrEntry<K, V, NODE
685
- * @returns a boolean value.
669
+ * The function checks if a binary tree is perfectly balanced by comparing its minimum height with
670
+ * its height.
671
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter is the starting
672
+ * point for checking if the binary tree is perfectly balanced. It represents the root node of the
673
+ * binary tree or a specific node from which the balance check should begin.
674
+ * @returns The method `isPerfectlyBalanced` is returning a boolean value, which indicates whether
675
+ * the tree starting from the `beginRoot` node is perfectly balanced or not. The return value is
676
+ * determined by comparing the minimum height of the tree with the height of the tree. If the minimum
677
+ * height plus 1 is greater than or equal to the height of the tree, then it is considered perfectly
678
+ * balanced and
686
679
  */
687
- isPerfectlyBalanced(beginRoot = this.root) {
680
+ isPerfectlyBalanced(beginRoot = this._root) {
688
681
  return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
689
682
  }
690
683
  /**
691
684
  * Time Complexity: O(n)
692
685
  * Space Complexity: O(1)
693
686
  *
694
- * The function `isBST` checks if a binary search tree is valid, either recursively or iteratively.
695
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
696
- * starting point for checking if a binary search tree (BST) is valid. It can be either a root node
697
- * of the BST, a key value of a node in the BST, or an entry object containing both the key and value
698
- * of a node in the BST
699
- * @param {IterationType} iterationType - The `iterationType` parameter is used to determine the type
700
- * of iteration to be performed while checking if the binary search tree (BST) is valid. It can have
701
- * two possible values:
702
- * @returns a boolean value.
687
+ * The function `isBST` in TypeScript checks if a binary search tree is valid using either recursive
688
+ * or iterative methods.
689
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `isBST`
690
+ * function represents the starting point for checking whether a binary search tree (BST) is valid.
691
+ * It can be a node in the BST or a reference to the root of the BST. If no specific node is
692
+ * provided, the function will default to
693
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `isBST` function
694
+ * determines whether the function should use a recursive approach or an iterative approach to check
695
+ * if the binary search tree (BST) is valid.
696
+ * @returns The `isBST` method is returning a boolean value, which indicates whether the binary
697
+ * search tree (BST) represented by the given root node is a valid BST or not. The method checks if
698
+ * the tree satisfies the BST property, where for every node, all nodes in its left subtree have keys
699
+ * less than the node's key, and all nodes in its right subtree have keys greater than the node's
703
700
  */
704
- isBST(beginRoot = this.root, iterationType = this.iterationType) {
701
+ isBST(beginRoot = this._root, iterationType = this.iterationType) {
705
702
  // TODO there is a bug
706
703
  beginRoot = this.ensureNode(beginRoot);
707
704
  if (!beginRoot)
@@ -747,17 +744,19 @@ class BinaryTree extends base_1.IterableEntryBase {
747
744
  * Time Complexity: O(n)
748
745
  * Space Complexity: O(1)
749
746
  *
750
- * The function calculates the depth of a given node or key in a tree-like data structure.
751
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} dist - The `dist` parameter can be either a `R`
752
- * (representing a root node), or a `BTNKeyOrNodeOrEntry<K, V, NODE>` (representing a key, node, or
753
- * entry).
754
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is optional and
755
- * represents the starting point from which to calculate the depth. It can be either a reference to a
756
- * node in the tree or a key-value pair or an entry object. If not provided, the default value is
757
- * `this.root`, which refers to the root node
758
- * @returns the depth of a node in a tree structure.
747
+ * The `getDepth` function calculates the depth between two nodes in a binary tree.
748
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} dist - The `dist` parameter in the `getDepth`
749
+ * function represents the node or entry in a binary tree map, or a reference to a node in the tree.
750
+ * It is the target node for which you want to calculate the depth from the `beginRoot` node.
751
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
752
+ * `getDepth` function represents the starting point from which you want to calculate the depth of a
753
+ * given node or entry in a binary tree. If no specific starting point is provided, the default value
754
+ * for `beginRoot` is set to the root of the binary
755
+ * @returns The `getDepth` method returns the depth of a given node `dist` relative to the
756
+ * `beginRoot` node in a binary tree. If the `dist` node is not found in the path to the `beginRoot`
757
+ * node, it returns the depth of the `dist` node from the root of the tree.
759
758
  */
760
- getDepth(dist, beginRoot = this.root) {
759
+ getDepth(dist, beginRoot = this._root) {
761
760
  let distEnsured = this.ensureNode(dist);
762
761
  const beginRootEnsured = this.ensureNode(beginRoot);
763
762
  let depth = 0;
@@ -775,15 +774,19 @@ class BinaryTree extends base_1.IterableEntryBase {
775
774
  * Space Complexity: O(1)
776
775
  *
777
776
  * The `getHeight` function calculates the maximum height of a binary tree using either a recursive
778
- * or iterative approach.
779
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
780
- * starting point for calculating the height of a tree. It can be either a root node (`R`), a key or
781
- * node or entry (`BTNKeyOrNodeOrEntry<K, V, NODE>`), or it defaults to the root of the current tree.
782
- * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
783
- * iteration used to calculate the height of the tree. It can have two possible values:
784
- * @returns the maximum height of the binary tree.
777
+ * or iterative approach in TypeScript.
778
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter is the starting
779
+ * point from which the height of the binary tree will be calculated. It can be a node in the binary
780
+ * tree or a reference to the root of the tree. If not provided, it defaults to the root of the
781
+ * binary tree data structure.
782
+ * @param {IterationType} iterationType - The `iterationType` parameter is used to determine the type
783
+ * of iteration to be performed while calculating the height of the binary tree. It can have two
784
+ * possible values:
785
+ * @returns The `getHeight` method returns the height of the binary tree starting from the specified
786
+ * root node. The height is calculated based on the maximum depth of the tree, considering either a
787
+ * recursive approach or an iterative approach depending on the `iterationType` parameter.
785
788
  */
786
- getHeight(beginRoot = this.root, iterationType = this.iterationType) {
789
+ getHeight(beginRoot = this._root, iterationType = this.iterationType) {
787
790
  beginRoot = this.ensureNode(beginRoot);
788
791
  if (!this.isRealNode(beginRoot))
789
792
  return -1;
@@ -816,19 +819,20 @@ class BinaryTree extends base_1.IterableEntryBase {
816
819
  * Space Complexity: O(log n)
817
820
  *
818
821
  * The `getMinHeight` function calculates the minimum height of a binary tree using either a
819
- * recursive or iterative approach.
820
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
821
- * starting point for calculating the minimum height of a tree. It can be either a root node (`R`), a
822
- * key or node or entry (`BTNKeyOrNodeOrEntry<K, V, NODE>`), or it defaults to the root of the current
823
- * tree.
824
- * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
825
- * iteration to be used when calculating the minimum height of the tree. It can have two possible
826
- * values:
827
- * @returns The function `getMinHeight` returns a number, which represents the minimum height of the
828
- * binary tree.
822
+ * recursive or iterative approach in TypeScript.
823
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
824
+ * `getMinHeight` function represents the starting node from which the minimum height of the binary
825
+ * tree will be calculated. It is either a node in the binary tree or a reference to the root of the
826
+ * tree. If not provided, the default value is the root
827
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `getMinHeight` method
828
+ * specifies the type of iteration to use when calculating the minimum height of a binary tree. It
829
+ * can have two possible values:
830
+ * @returns The `getMinHeight` method returns the minimum height of the binary tree starting from the
831
+ * specified root node. The height is calculated based on the shortest path from the root node to a
832
+ * leaf node in the tree. The method uses either a recursive approach or an iterative approach (using
833
+ * a stack) based on the `iterationType` parameter.
829
834
  */
830
- getMinHeight(beginRoot = this.root, iterationType = this.iterationType) {
831
- var _a, _b, _c;
835
+ getMinHeight(beginRoot = this._root, iterationType = this.iterationType) {
832
836
  beginRoot = this.ensureNode(beginRoot);
833
837
  if (!beginRoot)
834
838
  return -1;
@@ -858,8 +862,8 @@ class BinaryTree extends base_1.IterableEntryBase {
858
862
  if (!this.isRealNode(node.right) || last === node.right) {
859
863
  node = stack.pop();
860
864
  if (this.isRealNode(node)) {
861
- const leftMinHeight = this.isRealNode(node.left) ? ((_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1) : -1;
862
- const rightMinHeight = this.isRealNode(node.right) ? ((_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1) : -1;
865
+ const leftMinHeight = this.isRealNode(node.left) ? depths.get(node.left) : -1;
866
+ const rightMinHeight = this.isRealNode(node.right) ? depths.get(node.right) : -1;
863
867
  depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
864
868
  last = node;
865
869
  node = null;
@@ -869,33 +873,40 @@ class BinaryTree extends base_1.IterableEntryBase {
869
873
  node = node.right;
870
874
  }
871
875
  }
872
- return (_c = depths.get(beginRoot)) !== null && _c !== void 0 ? _c : -1;
876
+ return depths.get(beginRoot);
873
877
  }
874
878
  }
875
879
  /**
876
880
  * Time Complexity: O(log n)
877
881
  * Space Complexity: O(log n)
878
882
  *
879
- * The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
880
- * up to the root node, with an option to reverse the order of the nodes.
881
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginNode - The `beginNode` parameter can be either of
882
- * type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
883
- * @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
884
- * resulting path should be reversed or not. If `isReverse` is set to `true`, the path will be
885
- * reversed before returning it. If `isReverse` is set to `false` or not provided, the path will
886
- * @returns The function `getPathToRoot` returns an array of `NODE` objects.
883
+ * The function `getPathToRoot` in TypeScript retrieves the path from a given node to the root of a
884
+ * tree structure, applying a specified callback function along the way.
885
+ * @param {C} callback - The `callback` parameter is a function that is used to process each node in
886
+ * the path to the root. It is expected to be a function that takes a node as an argument and returns
887
+ * a value based on that node. The return type of the callback function is determined by the generic
888
+ * type `C
889
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginNode - The `beginNode` parameter in the
890
+ * `getPathToRoot` function can be either a key, a node, an entry, or any other value of type `R`.
891
+ * @param [isReverse=true] - The `isReverse` parameter in the `getPathToRoot` function determines
892
+ * whether the resulting path from the given `beginNode` to the root should be in reverse order or
893
+ * not. If `isReverse` is set to `true`, the path will be reversed before being returned. If `is
894
+ * @returns The function `getPathToRoot` returns an array of the return values of the callback
895
+ * function `callback` applied to each node in the path from the `beginNode` to the root node. The
896
+ * array is either in reverse order or in the original order based on the value of the `isReverse`
897
+ * parameter.
887
898
  */
888
- getPathToRoot(beginNode, isReverse = true) {
899
+ getPathToRoot(callback = this._DEFAULT_BTN_CALLBACK, beginNode, isReverse = true) {
889
900
  const result = [];
890
901
  let beginNodeEnsured = this.ensureNode(beginNode);
891
902
  if (!beginNodeEnsured)
892
903
  return result;
893
904
  while (beginNodeEnsured.parent) {
894
905
  // Array.push + Array.reverse is more efficient than Array.unshift
895
- result.push(beginNodeEnsured);
906
+ result.push(callback(beginNodeEnsured));
896
907
  beginNodeEnsured = beginNodeEnsured.parent;
897
908
  }
898
- result.push(beginNodeEnsured);
909
+ result.push(callback(beginNodeEnsured));
899
910
  return isReverse ? result.reverse() : result;
900
911
  }
901
912
  /**
@@ -905,21 +916,21 @@ class BinaryTree extends base_1.IterableEntryBase {
905
916
  * The function `getLeftMost` retrieves the leftmost node in a binary tree using either recursive or
906
917
  * tail-recursive iteration.
907
918
  * @param {C} callback - The `callback` parameter is a function that will be called with the leftmost
908
- * node of a binary tree or null if the tree is empty. It has a default value of `_DEFAULT_CALLBACK`
909
- * if not provided explicitly.
910
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter in the
919
+ * node of a binary tree or with `undefined` if the tree is empty. It is provided with a default
920
+ * value of `_DEFAULT_BTN_CALLBACK` if not specified.
921
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
911
922
  * `getLeftMost` function represents the starting point for finding the leftmost node in a binary
912
- * tree. It can be either a reference to the root node of the tree (`R`), or a key, node, or entry in
913
- * the binary tree structure (`
923
+ * tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
924
+ * starting point is provided, the function will default
914
925
  * @param {IterationType} iterationType - The `iterationType` parameter in the `getLeftMost` function
915
926
  * specifies the type of iteration to be used when traversing the binary tree nodes. It can have two
916
927
  * possible values:
917
928
  * @returns The `getLeftMost` function returns the result of the callback function `C` applied to the
918
- * leftmost node in the binary tree starting from the `beginRoot` node. If the `beginRoot` is `NIL`,
919
- * it returns the result of the callback function applied to `undefined`. If the `beginRoot` is not a
920
- * real node, it returns the result of the callback function applied
929
+ * leftmost node in the binary tree starting from the `beginRoot` node. If the `beginRoot` node is
930
+ * `NIL`, it returns the result of the callback function applied to `undefined`. If the `beginRoot`
931
+ * node is not a real node, it returns the result of the callback
921
932
  */
922
- getLeftMost(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
933
+ getLeftMost(callback = this._DEFAULT_BTN_CALLBACK, beginRoot = this._root, iterationType = this.iterationType) {
923
934
  if (this.isNIL(beginRoot))
924
935
  return callback(undefined);
925
936
  beginRoot = this.ensureNode(beginRoot);
@@ -950,22 +961,22 @@ class BinaryTree extends base_1.IterableEntryBase {
950
961
  * The function `getRightMost` retrieves the rightmost node in a binary tree using either recursive
951
962
  * or iterative traversal methods.
952
963
  * @param {C} callback - The `callback` parameter is a function that will be called with the result
953
- * of the operation. It has a generic type `C` which extends `BTNCallback<OptBTNOrNull<NODE>>`. The
954
- * default value for `callback` is `this._DEFAULT_CALLBACK` if it is not provided.
955
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter in the
964
+ * of finding the rightmost node in a binary tree. It is of type `BTNCallback<OptBTNOrNull<NODE>>`,
965
+ * which means it is a callback function that can accept either an optional binary tree node or null
966
+ * as
967
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
956
968
  * `getRightMost` function represents the starting point for finding the rightmost node in a binary
957
- * tree. It can be either a reference to the root node of the tree (`this.root`) or a specific key,
958
- * node, or entry in the tree. If
969
+ * tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
970
+ * starting point is provided, the function will default
959
971
  * @param {IterationType} iterationType - The `iterationType` parameter in the `getRightMost`
960
- * function specifies the type of iteration to be used when finding the rightmost node in a binary
961
- * tree. It can have two possible values:
962
- * @returns The `getRightMost` function returns the result of the callback function `C` applied to
963
- * the rightmost node in the binary tree. The rightmost node is found either through a recursive
964
- * depth-first search (if `iterationType` is 'RECURSIVE') or through an indirect implementation of
965
- * iteration using tail recursion optimization. The result of the callback function applied to the
966
- * rightmost node is returned
972
+ * function specifies the type of iteration to be used when traversing the binary tree nodes. It can
973
+ * have two possible values:
974
+ * @returns The `getRightMost` function returns the result of the callback function `C`, which is
975
+ * passed as a parameter to the function. The callback function is called with the rightmost node in
976
+ * the binary tree structure, determined based on the specified iteration type ('RECURSIVE' or
977
+ * other).
967
978
  */
968
- getRightMost(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
979
+ getRightMost(callback = this._DEFAULT_BTN_CALLBACK, beginRoot = this._root, iterationType = this.iterationType) {
969
980
  if (this.isNIL(beginRoot))
970
981
  return callback(undefined);
971
982
  // TODO support get right most by passing key in
@@ -994,10 +1005,14 @@ class BinaryTree extends base_1.IterableEntryBase {
994
1005
  * Time Complexity: O(log n)
995
1006
  * Space Complexity: O(1)
996
1007
  *
997
- * The function returns the predecessor node of a given node in a binary tree.
998
- * @param {NODE} node - The parameter "node" is of type "NODE", which represents a node in a binary
999
- * tree.
1000
- * @returns the predecessor node of the given node.
1008
+ * The function `getPredecessor` in TypeScript returns the predecessor node of a given node in a
1009
+ * binary tree.
1010
+ * @param {NODE} node - The `getPredecessor` function you provided seems to be attempting to find the
1011
+ * predecessor of a given node in a binary tree. However, there seems to be a logical issue in the
1012
+ * while loop condition that might cause an infinite loop.
1013
+ * @returns The `getPredecessor` function returns the predecessor node of the input `NODE` parameter.
1014
+ * If the left child of the input node exists, it traverses to the rightmost node of the left subtree
1015
+ * to find the predecessor. If the left child does not exist, it returns the input node itself.
1001
1016
  */
1002
1017
  getPredecessor(node) {
1003
1018
  if (this.isRealNode(node.left)) {
@@ -1017,10 +1032,14 @@ class BinaryTree extends base_1.IterableEntryBase {
1017
1032
  * Time Complexity: O(log n)
1018
1033
  * Space Complexity: O(1)
1019
1034
  *
1020
- * The function `getSuccessor` returns the next node in a binary tree given a current node.
1021
- * @param {K | NODE | null} [x] - The parameter `x` can be of type `K`, `NODE`, or `null`.
1022
- * @returns The function `getSuccessor` returns a `NODE` object if a successor exists, `null` if
1023
- * there is no successor, and `undefined` if the input `x` is not a valid node.
1035
+ * The function `getSuccessor` in TypeScript returns the next node in an in-order traversal of a
1036
+ * binary tree.
1037
+ * @param {K | NODE | null} [x] - The `getSuccessor` function takes a parameter `x`, which can be of
1038
+ * type `K`, `NODE`, or `null`.
1039
+ * @returns The `getSuccessor` function returns the successor node of the input node `x`. If `x` has
1040
+ * a right child, the function returns the leftmost node in the right subtree of `x`. If `x` does not
1041
+ * have a right child, the function traverses up the parent nodes until it finds a node that is not
1042
+ * the right child of its parent, and returns that node
1024
1043
  */
1025
1044
  getSuccessor(x) {
1026
1045
  x = this.ensureNode(x);
@@ -1040,26 +1059,29 @@ class BinaryTree extends base_1.IterableEntryBase {
1040
1059
  * Time complexity: O(n)
1041
1060
  * Space complexity: O(n)
1042
1061
  *
1043
- * The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback
1044
- * function on each node according to a specified pattern and iteration type.
1045
- * @param {C} callback - The `callback` parameter is a function that will be called for each node
1046
- * visited during the depth-first search. It takes a node as an argument and returns a value. The
1047
- * return type of the callback function is determined by the generic type `C`.
1048
- * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter determines the order in which the
1049
- * nodes are visited during the depth-first search. It can have one of the following values:
1050
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
1051
- * point of the depth-first search. It can be either a node object, a key-value pair, or a key. If it
1052
- * is a key or key-value pair, the method will find the corresponding node in the tree and start the
1053
- * search from there.
1054
- * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter determines the
1055
- * type of iteration to use during the depth-first search. It can have two possible values:
1056
- * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
1057
- * whether or not to include null values in the depth-first search traversal. If `includeNull` is set
1058
- * to `true`, null values will be included in the traversal. If `includeNull` is set to `false`, null
1059
- * values will
1060
- * @returns an array of the return types of the callback function.
1062
+ * The function `dfs` performs a depth-first search traversal on a binary tree structure based on the
1063
+ * specified parameters.
1064
+ * @param {C} callback - The `callback` parameter is a generic type `C` that extends the
1065
+ * `BTNCallback` interface with a type parameter of `OptBTNOrNull<NODE>`. It has a default value of
1066
+ * `this._DEFAULT_BTN_CALLBACK as C`.
1067
+ * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `dfs` method specifies the
1068
+ * order in which the Depth-First Search (DFS) algorithm should traverse the nodes in the tree. The
1069
+ * possible values for the `pattern` parameter are:
1070
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `dfs`
1071
+ * method is used to specify the starting point for the Depth-First Search traversal. It can be
1072
+ * either a `BTNKeyOrNodeOrEntry` object representing a key, node, or entry in the binary tree map,
1073
+ * or it can be a
1074
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `dfs` method specifies
1075
+ * the type of iteration to be performed during the depth-first search traversal. It is used to
1076
+ * determine the order in which nodes are visited during the traversal.
1077
+ * @param [includeNull=false] - The `includeNull` parameter in the `dfs` method is a boolean flag
1078
+ * that determines whether null values should be included in the traversal or not. If `includeNull`
1079
+ * is set to `true`, then null values will be included in the traversal process. If it is set to
1080
+ * `false`,
1081
+ * @returns The `dfs` method is returning an array of the return type specified by the generic type
1082
+ * parameter `C`. The return type is determined by the callback function provided to the method.
1061
1083
  */
1062
- dfs(callback = this._DEFAULT_CALLBACK, pattern = 'IN', beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
1084
+ dfs(callback = this._DEFAULT_BTN_CALLBACK, pattern = 'IN', beginRoot = this._root, iterationType = this.iterationType, includeNull = false) {
1063
1085
  beginRoot = this.ensureNode(beginRoot);
1064
1086
  if (!beginRoot)
1065
1087
  return [];
@@ -1069,25 +1091,26 @@ class BinaryTree extends base_1.IterableEntryBase {
1069
1091
  * Time complexity: O(n)
1070
1092
  * Space complexity: O(n)
1071
1093
  *
1072
- * The `bfs` function performs a breadth-first search on a binary tree, calling a callback function
1073
- * on each node and returning an array of the results.
1074
- * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1075
- * the breadth-first search traversal. It takes a single argument, which is the current node being
1076
- * visited, and returns a value of any type.
1077
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
1078
- * starting point of the breadth-first search. It can be either a root node of a tree or a key, node,
1079
- * or entry object. If no value is provided, the `root` property of the class is used as the default
1080
- * starting point.
1081
- * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
1082
- * iteration to be performed. It can have two possible values:
1083
- * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
1084
- * whether or not to include null values in the breadth-first search (BFS) traversal. If
1085
- * `includeNull` is set to `true`, null values will be included in the traversal. If `includeNull` is
1086
- * set to `false
1087
- * @returns The function `bfs` returns an array of values that are the result of invoking the
1088
- * `callback` function on each node in the breadth-first order traversal of the binary tree.
1094
+ * The `bfs` function performs a breadth-first search traversal on a binary tree or binary search
1095
+ * tree, executing a specified callback function on each node visited.
1096
+ * @param {C} callback - The `callback` parameter in the `bfs` function is a function that will be
1097
+ * called on each node visited during the breadth-first search traversal. It is a generic type `C`
1098
+ * that extends the `BTNCallback` type, which takes a parameter of type `NODE` or `null`.
1099
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `bfs`
1100
+ * function represents the starting point for the breadth-first search traversal in a binary tree. It
1101
+ * can be specified as a key, node, or entry in the binary tree structure. If not provided, the
1102
+ * default value is the root node of the binary
1103
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `bfs` function
1104
+ * determines the type of iteration to be performed on the binary tree nodes. It can have two
1105
+ * possible values:
1106
+ * @param [includeNull=false] - The `includeNull` parameter in the `bfs` function determines whether
1107
+ * to include `null` values in the breadth-first search traversal of a binary tree. If `includeNull`
1108
+ * is set to `true`, the traversal will include `null` values for nodes that do not have children
1109
+ * (left
1110
+ * @returns The `bfs` function returns an array of values that are the result of applying the
1111
+ * provided callback function to each node in the binary tree in a breadth-first search manner.
1089
1112
  */
1090
- bfs(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
1113
+ bfs(callback = this._DEFAULT_BTN_CALLBACK, beginRoot = this._root, iterationType = this.iterationType, includeNull = false) {
1091
1114
  beginRoot = this.ensureNode(beginRoot);
1092
1115
  if (!beginRoot)
1093
1116
  return [];
@@ -1143,27 +1166,25 @@ class BinaryTree extends base_1.IterableEntryBase {
1143
1166
  * Time complexity: O(n)
1144
1167
  * Space complexity: O(n)
1145
1168
  *
1146
- * The `leaves` function in TypeScript iterates through a binary tree to find and return the leaf
1147
- * nodes based on a specified callback and iteration type.
1169
+ * The `leaves` function in TypeScript returns an array of values from leaf nodes in a binary tree
1170
+ * structure based on a specified callback and iteration type.
1148
1171
  * @param {C} callback - The `callback` parameter is a function that will be called on each leaf node
1149
- * in the binary tree. It is a generic type `C` that extends `BTNCallback<NODE | null>`, where `NODE`
1150
- * represents a node in the binary tree. The default value for `callback` is
1151
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter in the `leaves`
1172
+ * in the binary tree. It is optional and defaults to a default callback function if not provided.
1173
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `leaves`
1152
1174
  * method is used to specify the starting point for finding and processing the leaves of a binary
1153
- * tree. It represents the root node of the binary tree or a specific key, node, or entry within the
1154
- * tree from which the search for leaves should begin
1175
+ * tree. It can be provided as either a key, a node, or an entry in the binary tree structure. If not
1176
+ * explicitly provided, the default value
1155
1177
  * @param {IterationType} iterationType - The `iterationType` parameter in the `leaves` method
1156
1178
  * specifies the type of iteration to be performed when collecting the leaves of a binary tree. It
1157
1179
  * can have two possible values:
1158
1180
  * @returns The `leaves` method returns an array of values that are the result of applying the
1159
- * provided callback function to the leaf nodes in the binary tree structure.
1181
+ * provided callback function to each leaf node in the binary tree.
1160
1182
  */
1161
- leaves(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
1183
+ leaves(callback = this._DEFAULT_BTN_CALLBACK, beginRoot = this._root, iterationType = this.iterationType) {
1162
1184
  beginRoot = this.ensureNode(beginRoot);
1163
1185
  const leaves = [];
1164
- if (!this.isRealNode(beginRoot)) {
1186
+ if (!this.isRealNode(beginRoot))
1165
1187
  return [];
1166
- }
1167
1188
  if (iterationType === 'RECURSIVE') {
1168
1189
  const dfs = (cur) => {
1169
1190
  if (this.isLeaf(cur)) {
@@ -1171,8 +1192,10 @@ class BinaryTree extends base_1.IterableEntryBase {
1171
1192
  }
1172
1193
  if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
1173
1194
  return;
1174
- this.isRealNode(cur.left) && dfs(cur.left);
1175
- this.isRealNode(cur.right) && dfs(cur.right);
1195
+ if (this.isRealNode(cur.left))
1196
+ dfs(cur.left);
1197
+ if (this.isRealNode(cur.right))
1198
+ dfs(cur.right);
1176
1199
  };
1177
1200
  dfs(beginRoot);
1178
1201
  }
@@ -1184,8 +1207,10 @@ class BinaryTree extends base_1.IterableEntryBase {
1184
1207
  if (this.isLeaf(cur)) {
1185
1208
  leaves.push(callback(cur));
1186
1209
  }
1187
- this.isRealNode(cur.left) && queue.push(cur.left);
1188
- this.isRealNode(cur.right) && queue.push(cur.right);
1210
+ if (this.isRealNode(cur.left))
1211
+ queue.push(cur.left);
1212
+ if (this.isRealNode(cur.right))
1213
+ queue.push(cur.right);
1189
1214
  }
1190
1215
  }
1191
1216
  }
@@ -1195,24 +1220,27 @@ class BinaryTree extends base_1.IterableEntryBase {
1195
1220
  * Time complexity: O(n)
1196
1221
  * Space complexity: O(n)
1197
1222
  *
1198
- * The `listLevels` function returns an array of arrays, where each inner array represents a level in
1199
- * a binary tree and contains the results of applying a callback function to the nodes at that level.
1200
- * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1201
- * the tree. It takes a node as an argument and returns a value. The return type of the callback
1202
- * function is determined by the generic type `C` which extends `BTNCallback<NODE | null>`.
1203
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
1204
- * starting point for traversing the tree. It can be either a root node, a key-value pair, or a node
1205
- * entry. If no value is provided, the `root` property of the class is used as the default starting
1206
- * point.
1207
- * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
1208
- * iteration to be performed on the binary tree. It can have two possible values:
1209
- * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
1210
- * whether or not to include null values in the resulting levels. If `includeNull` is set to `true`,
1211
- * null values will be included in the levels. If `includeNull` is set to `false`, null values will
1212
- * be excluded
1213
- * @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
1223
+ * The `listLevels` function in TypeScript generates a list of nodes at each level of a binary tree,
1224
+ * using either recursive or iterative traversal based on the specified iteration type.
1225
+ * @param {C} callback - The `callback` parameter is a function that will be applied to each node in
1226
+ * the binary tree during the traversal. It is used to process each node and determine what
1227
+ * information to include in the output for each level of the tree.
1228
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
1229
+ * `listLevels` function represents the starting point for traversing the binary tree. It can be
1230
+ * either a key, a node, or an entry in the binary tree. If not provided, the default value is the
1231
+ * root of the binary tree.
1232
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `listLevels` function
1233
+ * determines the type of iteration to be performed on the binary tree nodes. It can have two
1234
+ * possible values:
1235
+ * @param [includeNull=false] - The `includeNull` parameter in the `listLevels` method determines
1236
+ * whether or not to include null nodes in the traversal of the binary tree. If `includeNull` is set
1237
+ * to `true`, the traversal will include null nodes in the levels of the tree. If set to `false`,
1238
+ * null
1239
+ * @returns The `listLevels` method returns an array of arrays, where each inner array represents a
1240
+ * level in a binary tree. Each inner array contains the return value of the provided callback
1241
+ * function applied to the nodes at that level.
1214
1242
  */
1215
- listLevels(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
1243
+ listLevels(callback = this._DEFAULT_BTN_CALLBACK, beginRoot = this._root, iterationType = this.iterationType, includeNull = false) {
1216
1244
  beginRoot = this.ensureNode(beginRoot);
1217
1245
  const levelsNodes = [];
1218
1246
  if (!beginRoot)
@@ -1265,24 +1293,25 @@ class BinaryTree extends base_1.IterableEntryBase {
1265
1293
  * Time complexity: O(n)
1266
1294
  * Space complexity: O(n)
1267
1295
  *
1268
- * The `morris` function performs a depth-first traversal on a binary tree using the Morris traversal
1269
- * algorithm.
1270
- * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1271
- * the tree. It takes a single argument, which is the current node, and can return any value. The
1272
- * return type of the `callback` function is determined by the `ReturnType<C>` type, which represents
1273
- * the return
1274
- * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function is used
1275
- * to specify the order in which the nodes of a binary tree are traversed. It can take one of the
1276
- * following values:
1277
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
1278
- * point for the traversal. It can be either a node object, a key, or an entry object. If no value is
1279
- * provided, the `root` of the tree is used as the starting point.
1280
- * @returns The function `morris` returns an array of values that are the return values of the
1281
- * callback function `callback`.
1296
+ * The `morris` function in TypeScript performs a Depth-First Search traversal on a binary tree using
1297
+ * Morris Traversal algorithm with different order patterns.
1298
+ * @param {C} callback - The `callback` parameter in the `morris` function is a function that will be
1299
+ * called on each node in the binary tree during the traversal. It is of type `C`, which extends the
1300
+ * `BTNCallback<NODE>` type. The default value for `callback` is `this._DEFAULT
1301
+ * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function specifies
1302
+ * the type of Depth-First Search (DFS) order pattern to traverse the binary tree. The possible
1303
+ * values for the `pattern` parameter are:
1304
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `morris`
1305
+ * function is the starting point for the Morris traversal algorithm. It represents the root node of
1306
+ * the binary tree or the node from which the traversal should begin. It can be provided as either a
1307
+ * key, a node, an entry, or a reference
1308
+ * @returns The `morris` function is returning an array of values that are the result of applying the
1309
+ * provided callback function to each node in the binary tree in the specified order pattern (IN,
1310
+ * PRE, or POST).
1282
1311
  */
1283
- morris(callback = this._DEFAULT_CALLBACK, pattern = 'IN', beginRoot = this.root) {
1312
+ morris(callback = this._DEFAULT_BTN_CALLBACK, pattern = 'IN', beginRoot = this._root) {
1284
1313
  beginRoot = this.ensureNode(beginRoot);
1285
- if (beginRoot === null)
1314
+ if (!beginRoot)
1286
1315
  return [];
1287
1316
  const ans = [];
1288
1317
  let cur = beginRoot;
@@ -1369,8 +1398,12 @@ class BinaryTree extends base_1.IterableEntryBase {
1369
1398
  * Time complexity: O(n)
1370
1399
  * Space complexity: O(n)
1371
1400
  *
1372
- * The `clone` function creates a deep copy of a tree object.
1373
- * @returns The `clone()` method is returning a cloned instance of the `TREE` object.
1401
+ * The `clone` function creates a deep copy of a tree structure by traversing it using breadth-first
1402
+ * search.
1403
+ * @returns The `clone()` method is returning a cloned copy of the tree with the same structure and
1404
+ * values as the original tree. The method creates a new tree, iterates over the nodes of the
1405
+ * original tree using breadth-first search (bfs), and adds the nodes to the new tree. If a node in
1406
+ * the original tree is null, a null node is added to the cloned tree. If a node
1374
1407
  */
1375
1408
  clone() {
1376
1409
  const cloned = this.createTree();
@@ -1379,23 +1412,24 @@ class BinaryTree extends base_1.IterableEntryBase {
1379
1412
  cloned.add(null);
1380
1413
  else
1381
1414
  cloned.add([node.key, node.value]);
1382
- }, this.root, this.iterationType, true);
1415
+ }, this._root, this.iterationType, true);
1383
1416
  return cloned;
1384
1417
  }
1385
1418
  /**
1386
1419
  * Time Complexity: O(n)
1387
1420
  * Space Complexity: O(n)
1388
1421
  *
1389
- * The `filter` function creates a new tree with entries that pass a given predicate function.
1390
- * @param predicate - The `predicate` parameter is a callback function that is used to test each
1391
- * element in the tree. It takes three arguments: `value`, `key`, and `index`. The `value` argument
1392
- * represents the value of the current element being processed, the `key` argument represents the key
1393
- * of the
1394
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
1395
- * specify the value of `this` within the `predicate` function. When the `predicate` function is
1396
- * called, `thisArg` will be used as the value of `this` within the function. If `thisArg`
1397
- * @returns The `filter` method is returning a new tree object that contains the entries that pass
1398
- * the given predicate function.
1422
+ * The `filter` function iterates over key-value pairs in a tree data structure and creates a new
1423
+ * tree with elements that satisfy a given predicate.
1424
+ * @param predicate - The `predicate` parameter in the `filter` method is a function that will be
1425
+ * called with four arguments: the `value` of the current entry, the `key` of the current entry, the
1426
+ * `index` of the current entry in the iteration, and the reference to the tree itself (`
1427
+ * @param {any} [thisArg] - The `thisArg` parameter in the `filter` method allows you to specify the
1428
+ * value of `this` that should be used when executing the `predicate` function. This is useful when
1429
+ * the `predicate` function relies on the context of a specific object or value. By providing a
1430
+ * `thisArg
1431
+ * @returns The `filter` method is returning a new tree that contains entries that pass the provided
1432
+ * predicate function.
1399
1433
  */
1400
1434
  filter(predicate, thisArg) {
1401
1435
  const newTree = this.createTree();
@@ -1411,16 +1445,16 @@ class BinaryTree extends base_1.IterableEntryBase {
1411
1445
  * Time Complexity: O(n)
1412
1446
  * Space Complexity: O(n)
1413
1447
  *
1414
- * The `map` function creates a new tree by applying a callback function to each entry in the current
1415
- * tree.
1416
- * @param callback - The callback parameter is a function that will be called for each entry in the
1417
- * tree. It takes three arguments: value, key, and index. The value argument represents the value of
1418
- * the current entry, the key argument represents the key of the current entry, and the index
1419
- * argument represents the index of the
1420
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
1421
- * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
1422
- * passed as the `this` value to the `callback` function. If `thisArg` is
1423
- * @returns The `map` method is returning a new tree object.
1448
+ * The `map` function iterates over key-value pairs in a tree data structure, applies a callback
1449
+ * function to each value, and returns a new tree with the updated values.
1450
+ * @param callback - The `callback` parameter in the `map` method is a function that will be called
1451
+ * on each entry in the tree. It takes four arguments:
1452
+ * @param {any} [thisArg] - The `thisArg` parameter in the `map` function is an optional parameter
1453
+ * that specifies the value to be passed as `this` when executing the callback function. If provided,
1454
+ * the `thisArg` value will be used as the `this` value within the callback function. If `thisArg
1455
+ * @returns The `map` method is returning a new tree with the entries modified by the provided
1456
+ * callback function. Each entry in the original tree is passed to the callback function, and the
1457
+ * result of the callback function is added to the new tree.
1424
1458
  */
1425
1459
  map(callback, thisArg) {
1426
1460
  const newTree = this.createTree();
@@ -1443,31 +1477,32 @@ class BinaryTree extends base_1.IterableEntryBase {
1443
1477
  * Time Complexity: O(n)
1444
1478
  * Space Complexity: O(n)
1445
1479
  *
1446
- * The `print` function in TypeScript prints the binary tree structure with customizable options.
1447
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
1448
- * point for printing the binary tree. It can be either a node of the binary tree or a key or entry
1449
- * that exists in the binary tree. If no value is provided, the root of the binary tree will be used
1450
- * as the starting point.
1451
- * @param {BinaryTreePrintOptions} [options] - The `options` parameter is an optional object that
1452
- * allows you to customize the printing behavior. It has the following properties:
1453
- * @returns Nothing is being returned. The function has a return type of `void`, which means it does
1454
- * not return any value.
1480
+ * The function `toVisual` in TypeScript overrides the visual representation of a binary tree with
1481
+ * customizable options for displaying undefined, null, and sentinel nodes.
1482
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
1483
+ * `toVisual` method is used to specify the starting point for visualizing the binary tree structure.
1484
+ * It can be a node, key, entry, or the root of the tree. If no specific starting point is provided,
1485
+ * the default is set to the root
1486
+ * @param {BinaryTreePrintOptions} [options] - The `options` parameter in the `toVisual` method is an
1487
+ * object that contains the following properties:
1488
+ * @returns The `override toVisual` method returns a string that represents the visual display of the
1489
+ * binary tree based on the provided options for showing undefined, null, and Red-Black NIL nodes.
1490
+ * The method constructs the visual representation by calling the `_displayAux` method and appending
1491
+ * the lines to the output string. The final output string contains the visual representation of the
1492
+ * binary tree with the specified options.
1455
1493
  */
1456
- print(beginRoot = this.root, options) {
1457
- const opts = Object.assign({ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false }, options);
1494
+ toVisual(beginRoot = this._root, options) {
1495
+ const opts = Object.assign({ isShowUndefined: false, isShowNull: true, isShowRedBlackNIL: false }, options);
1458
1496
  beginRoot = this.ensureNode(beginRoot);
1459
1497
  let output = '';
1460
1498
  if (!beginRoot)
1461
1499
  return output;
1462
1500
  if (opts.isShowUndefined)
1463
- output += `U for undefined
1464
- `;
1501
+ output += `U for undefined\n`;
1465
1502
  if (opts.isShowNull)
1466
- output += `N for null
1467
- `;
1503
+ output += `N for null\n`;
1468
1504
  if (opts.isShowRedBlackNIL)
1469
- output += `S for Sentinel Node(NIL)
1470
- `;
1505
+ output += `S for Sentinel Node(NIL)\n`;
1471
1506
  const display = (root) => {
1472
1507
  const [lines, , ,] = this._displayAux(root, opts);
1473
1508
  let paragraph = '';
@@ -1479,50 +1514,67 @@ class BinaryTree extends base_1.IterableEntryBase {
1479
1514
  display(beginRoot);
1480
1515
  return output;
1481
1516
  }
1517
+ /**
1518
+ * Time Complexity: O(n)
1519
+ * Space Complexity: O(n)
1520
+ *
1521
+ * The function `print` in TypeScript overrides the default print behavior to log a visual
1522
+ * representation of the binary tree to the console.
1523
+ * @param {BinaryTreePrintOptions} [options] - The `options` parameter is used to specify the
1524
+ * printing options for the binary tree. It is an optional parameter that allows you to customize how
1525
+ * the binary tree is printed, such as choosing between different traversal orders or formatting
1526
+ * options.
1527
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
1528
+ * `override print` method is used to specify the starting point for printing the binary tree. It can
1529
+ * be either a key, a node, an entry, or the root of the tree. If no specific starting point is
1530
+ * provided, the default value is set to
1531
+ */
1532
+ print(options, beginRoot = this._root) {
1533
+ console.log(this.toVisual(beginRoot, options));
1534
+ }
1482
1535
  /**
1483
1536
  * Time complexity: O(n)
1484
1537
  * Space complexity: O(n)
1485
1538
  *
1486
- * The function `_dfs` performs a depth-first search traversal on a binary tree structure based on
1539
+ * The `_dfs` function performs a depth-first search traversal on a binary tree structure based on
1487
1540
  * the specified order pattern and callback function.
1488
- * @param {C} callback - The `callback` parameter is a function that will be called on each node
1489
- * visited during the depth-first search. It is of type `C`, which extends
1490
- * `BTNCallback<OptBTNOrNull<NODE>>`. The default value is set to `this._DEFAULT_CALLBACK` if not
1491
- * provided.
1541
+ * @param {C} callback - The `callback` parameter in the `_dfs` method is a function that will be
1542
+ * called on each node visited during the depth-first search traversal. It is of type `C`, which
1543
+ * extends `BTNCallback<OptBTNOrNull<NODE>>`. The default value for this parameter is `this._DEFAULT
1492
1544
  * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `_dfs` method specifies the
1493
- * order in which the Depth-First Search (DFS) algorithm should traverse the nodes in a binary tree.
1494
- * It can have one of the following values:
1495
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter in the `_dfs`
1545
+ * order in which the nodes are visited during the Depth-First Search traversal. It can have one of
1546
+ * the following values:
1547
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `_dfs`
1496
1548
  * method is used to specify the starting point for the depth-first search traversal in a binary
1497
- * tree. It can be provided as either the root node of the tree or a key, node, or entry that exists
1498
- * in the tree. If no specific `
1549
+ * tree. It can be provided as either a `BTNKeyOrNodeOrEntry` object or a reference to the root node
1550
+ * of the tree. If no specific
1499
1551
  * @param {IterationType} iterationType - The `iterationType` parameter in the `_dfs` method
1500
- * specifies the type of iteration to be performed during the Depth-First Search (DFS) traversal. It
1501
- * can have two possible values:
1552
+ * specifies the type of iteration to be performed during the Depth-First Search (DFS) traversal of a
1553
+ * binary tree. It can have two possible values:
1502
1554
  * @param [includeNull=false] - The `includeNull` parameter in the `_dfs` method is a boolean flag
1503
1555
  * that determines whether null nodes should be included in the depth-first search traversal. If
1504
- * `includeNull` is set to `true`, the traversal will consider null nodes as valid nodes to visit and
1505
- * process. If set to `
1556
+ * `includeNull` is set to `true`, null nodes will be considered during the traversal process. If it
1557
+ * is set to `false`,
1506
1558
  * @param shouldVisitLeft - The `shouldVisitLeft` parameter is a function that takes a node as input
1507
1559
  * and returns a boolean value. It is used to determine whether the left child of a node should be
1508
1560
  * visited during the depth-first search traversal. By default, it checks if the node is truthy (not
1509
1561
  * null or undefined
1510
- * @param shouldVisitRight - The `shouldVisitRight` parameter is a function that takes a node as
1511
- * input and returns a boolean value. It is used to determine whether the right child of a node
1562
+ * @param shouldVisitRight - The `shouldVisitRight` parameter is a function that takes a node as an
1563
+ * argument and returns a boolean value. It is used to determine whether the right child of a node
1512
1564
  * should be visited during the depth-first search traversal. The default implementation checks if
1513
- * the node is truthy before visiting the right child.
1565
+ * the node is truthy before visiting the right child
1514
1566
  * @param shouldVisitRoot - The `shouldVisitRoot` parameter is a function that takes a node as an
1515
- * argument and returns a boolean value. It is used to determine whether a given node should be
1567
+ * argument and returns a boolean value. It is used to determine whether the root node should be
1516
1568
  * visited during the depth-first search traversal based on certain conditions. The default
1517
1569
  * implementation checks if the node is a real node or null based
1518
- * @param shouldProcessRoot - The `shouldProcessRoot` parameter is a function that takes a node as
1519
- * input and returns a boolean value indicating whether the node should be processed during the
1520
- * depth-first search traversal. The default implementation of this function simply returns `true`,
1521
- * meaning that by default all nodes will be processed. However, you can
1522
- * @returns The `_dfs` method returns an array of the return type of the callback function provided
1570
+ * @param shouldProcessRoot - The `shouldProcessRoot` parameter is a function that takes a node as an
1571
+ * argument and returns a boolean value indicating whether the node should be processed during the
1572
+ * depth-first search traversal. The default implementation checks if the node is a real node or null
1573
+ * based on the `includeNull` flag. If `
1574
+ * @returns The function `_dfs` returns an array of the return type of the callback function provided
1523
1575
  * as input.
1524
1576
  */
1525
- _dfs(callback = this._DEFAULT_CALLBACK, pattern = 'IN', beginRoot = this.root, iterationType = this.iterationType, includeNull = false, shouldVisitLeft = node => !!node, shouldVisitRight = node => !!node, shouldVisitRoot = node => {
1577
+ _dfs(callback = this._DEFAULT_BTN_CALLBACK, pattern = 'IN', beginRoot = this._root, iterationType = this.iterationType, includeNull = false, shouldVisitLeft = node => !!node, shouldVisitRight = node => !!node, shouldVisitRoot = node => {
1526
1578
  if (includeNull)
1527
1579
  return this.isRealNodeOrNull(node);
1528
1580
  return this.isRealNode(node);
@@ -1567,20 +1619,20 @@ class BinaryTree extends base_1.IterableEntryBase {
1567
1619
  dfs(beginRoot);
1568
1620
  }
1569
1621
  else {
1570
- const stack = [{ opt: types_1.DFSOperation.VISIT, node: beginRoot }];
1622
+ const stack = [{ opt: constants_1.DFSOperation.VISIT, node: beginRoot }];
1571
1623
  const pushLeft = (cur) => {
1572
1624
  var _a;
1573
1625
  if (shouldVisitLeft(cur.node))
1574
- stack.push({ opt: types_1.DFSOperation.VISIT, node: (_a = cur.node) === null || _a === void 0 ? void 0 : _a.left });
1626
+ stack.push({ opt: constants_1.DFSOperation.VISIT, node: (_a = cur.node) === null || _a === void 0 ? void 0 : _a.left });
1575
1627
  };
1576
1628
  const pushRight = (cur) => {
1577
1629
  var _a;
1578
1630
  if (shouldVisitRight(cur.node))
1579
- stack.push({ opt: types_1.DFSOperation.VISIT, node: (_a = cur.node) === null || _a === void 0 ? void 0 : _a.right });
1631
+ stack.push({ opt: constants_1.DFSOperation.VISIT, node: (_a = cur.node) === null || _a === void 0 ? void 0 : _a.right });
1580
1632
  };
1581
1633
  const pushRoot = (cur) => {
1582
1634
  if (shouldVisitRoot(cur.node))
1583
- stack.push({ opt: types_1.DFSOperation.PROCESS, node: cur.node });
1635
+ stack.push({ opt: constants_1.DFSOperation.PROCESS, node: cur.node });
1584
1636
  };
1585
1637
  while (stack.length > 0) {
1586
1638
  const cur = stack.pop();
@@ -1588,7 +1640,7 @@ class BinaryTree extends base_1.IterableEntryBase {
1588
1640
  continue;
1589
1641
  if (!shouldVisitRoot(cur.node))
1590
1642
  continue;
1591
- if (cur.opt === types_1.DFSOperation.PROCESS) {
1643
+ if (cur.opt === constants_1.DFSOperation.PROCESS) {
1592
1644
  if (shouldProcessRoot(cur.node))
1593
1645
  ans.push(callback(cur.node));
1594
1646
  }
@@ -1619,13 +1671,18 @@ class BinaryTree extends base_1.IterableEntryBase {
1619
1671
  * Time Complexity: O(1)
1620
1672
  * Space Complexity: O(1)
1621
1673
  *
1622
- * The function `_getIterator` is a generator function that returns an iterator for the key-value
1623
- * pairs in a binary search tree.
1624
- * @param node - The `node` parameter represents the current node in the binary search tree. It is
1625
- * initially set to the root node of the tree.
1626
- * @returns an IterableIterator<[K, V | undefined]>.
1674
+ * The function `_getIterator` returns an iterable iterator for a binary tree data structure, either
1675
+ * using an iterative approach or a recursive approach based on the specified iteration type.
1676
+ * @param node - The `node` parameter in the `_getIterator` method represents the current node being
1677
+ * processed during iteration. It is initially set to the root node of the data structure (or the
1678
+ * node passed as an argument), and then it is traversed through the data structure based on the
1679
+ * iteration type specified (`ITER
1680
+ * @returns The `_getIterator` method returns an IterableIterator containing key-value pairs of nodes
1681
+ * in a binary tree structure. The method uses an iterative approach to traverse the tree based on
1682
+ * the `iterationType` property. If the `iterationType` is set to 'ITERATIVE', the method uses a
1683
+ * stack to perform an in-order traversal of the tree. If the `iterationType` is not 'ITERATIVE
1627
1684
  */
1628
- *_getIterator(node = this.root) {
1685
+ *_getIterator(node = this._root) {
1629
1686
  if (!node)
1630
1687
  return;
1631
1688
  if (this.iterationType === 'ITERATIVE') {
@@ -1657,18 +1714,16 @@ class BinaryTree extends base_1.IterableEntryBase {
1657
1714
  * Time Complexity: O(n)
1658
1715
  * Space Complexity: O(n)
1659
1716
  *
1660
- * The `_displayAux` function is responsible for generating the display layout of a binary tree node,
1661
- * taking into account various options such as whether to show null, undefined, or NaN nodes.
1662
- * @param {OptBTNOrNull<NODE>} node - The `node` parameter represents a node in a binary tree.
1663
- * It can be of type `NODE`, `null`, or `undefined`.
1664
- * @param {BinaryTreePrintOptions} options - The `options` parameter is an object that contains the
1665
- * following properties:
1666
- * @returns The function `_displayAux` returns a `NodeDisplayLayout` which is an array containing the
1667
- * following elements:
1668
- * 1. `mergedLines`: An array of strings representing the lines of the node display.
1669
- * 2. `totalWidth`: The total width of the node display.
1670
- * 3. `totalHeight`: The total height of the node display.
1671
- * 4. `middleIndex`: The index of the middle character
1717
+ * The function `_displayAux` in TypeScript is responsible for generating the display layout of nodes
1718
+ * in a binary tree based on specified options.
1719
+ * @param node - The `node` parameter in the `_displayAux` function represents a node in a binary
1720
+ * tree. It can be either a valid node containing a key or a special type of node like null,
1721
+ * undefined, or a Red-Black tree NIL node. The function checks the type of the node and its
1722
+ * @param {BinaryTreePrintOptions} options - The `options` parameter in the `_displayAux` function
1723
+ * contains the following properties:
1724
+ * @returns The `_displayAux` function returns a `NodeDisplayLayout`, which is an array containing
1725
+ * information about how to display a node in a binary tree. The `NodeDisplayLayout` consists of four
1726
+ * elements:
1672
1727
  */
1673
1728
  _displayAux(node, options) {
1674
1729
  const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
@@ -1726,14 +1781,16 @@ class BinaryTree extends base_1.IterableEntryBase {
1726
1781
  * Time Complexity: O(1)
1727
1782
  * Space Complexity: O(1)
1728
1783
  *
1729
- * The function `_swapProperties` swaps the key-value properties between two nodes.
1730
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} srcNode - The source node that will be swapped with the
1731
- * destination node. It can be either an instance of the class `R`, or an object of type
1732
- * `BTNKeyOrNodeOrEntry<K, V, NODE>`.
1733
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} destNode - The `destNode` parameter is the node where
1734
- * the properties will be swapped with the `srcNode`.
1735
- * @returns either the `destNode` object with its properties swapped with the `srcNode` object's
1736
- * properties, or `undefined` if either `srcNode` or `destNode` is falsy.
1784
+ * The _swapProperties function swaps key and value properties between two nodes in a binary tree.
1785
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} srcNode - The `srcNode` parameter in the
1786
+ * `_swapProperties` method can be either a BTNKeyOrNodeOrEntry object containing key and value
1787
+ * properties, or it can be of type R.
1788
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} destNode - The `destNode` parameter in the
1789
+ * `_swapProperties` method represents the node or entry where the properties will be swapped with
1790
+ * the `srcNode`. It can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>` or `R`. The method ensures that
1791
+ * both `srcNode
1792
+ * @returns The `_swapProperties` method returns either the `destNode` with its key and value swapped
1793
+ * with the `srcNode`, or `undefined` if either `srcNode` or `destNode` is falsy.
1737
1794
  */
1738
1795
  _swapProperties(srcNode, destNode) {
1739
1796
  srcNode = this.ensureNode(srcNode);
@@ -1755,13 +1812,15 @@ class BinaryTree extends base_1.IterableEntryBase {
1755
1812
  * Time Complexity: O(1)
1756
1813
  * Space Complexity: O(1)
1757
1814
  *
1758
- * The function replaces a node in a binary tree with a new node, updating the parent, left child,
1759
- * right child, and root if necessary.
1760
- * @param {NODE} oldNode - The oldNode parameter represents the node that needs to be replaced in the
1761
- * tree.
1762
- * @param {NODE} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
1763
- * tree.
1764
- * @returns the newNode.
1815
+ * The _replaceNode function replaces an old node with a new node in a binary tree structure.
1816
+ * @param {NODE} oldNode - The `oldNode` parameter represents the node that you want to replace in a
1817
+ * tree data structure.
1818
+ * @param {NODE} newNode - The `newNode` parameter in the `_replaceNode` function represents the node
1819
+ * that will replace the `oldNode` in a tree data structure. This function is responsible for
1820
+ * updating the parent, left child, right child, and root (if necessary) references when replacing a
1821
+ * node in the tree.
1822
+ * @returns The method `_replaceNode` is returning the `newNode` that was passed as a parameter after
1823
+ * replacing the `oldNode` with it in the binary tree structure.
1765
1824
  */
1766
1825
  _replaceNode(oldNode, newNode) {
1767
1826
  if (oldNode.parent) {
@@ -1775,8 +1834,8 @@ class BinaryTree extends base_1.IterableEntryBase {
1775
1834
  newNode.left = oldNode.left;
1776
1835
  newNode.right = oldNode.right;
1777
1836
  newNode.parent = oldNode.parent;
1778
- if (this.root === oldNode) {
1779
- this._root = newNode;
1837
+ if (this._root === oldNode) {
1838
+ this._setRoot(newNode);
1780
1839
  }
1781
1840
  return newNode;
1782
1841
  }
@@ -1784,10 +1843,10 @@ class BinaryTree extends base_1.IterableEntryBase {
1784
1843
  * Time Complexity: O(1)
1785
1844
  * Space Complexity: O(1)
1786
1845
  *
1787
- * The function sets the root property of an object to the provided value, and also updates the
1788
- * parent property of the new root.
1789
- * @param {OptBTNOrNull<NODE>} v - The parameter `v` is of type `OptBTNOrNull<NODE>`. This
1790
- * means that it can accept a value of type `NODE`, `null`, or `undefined`.
1846
+ * The function _setRoot sets the root node of a data structure while updating the parent reference
1847
+ * of the previous root node.
1848
+ * @param v - The parameter `v` in the `_setRoot` method is of type `OptBTNOrNull<NODE>`, which means
1849
+ * it can either be an optional `NODE` type or `null`.
1791
1850
  */
1792
1851
  _setRoot(v) {
1793
1852
  if (v) {
@@ -1799,20 +1858,47 @@ class BinaryTree extends base_1.IterableEntryBase {
1799
1858
  * Time Complexity: O(1)
1800
1859
  * Space Complexity: O(1)
1801
1860
  *
1802
- * The function `_ensureCallback` ensures that a callback function is provided and returns it.
1803
- * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is of type
1804
- * `ReturnType<C> | null | undefined`. This means it can accept a value that is the return type of
1805
- * the generic type `C`, or it can be `null` or `undefined`.
1806
- * @param {C} callback - The `callback` parameter is a function that takes a `node` as an argument
1807
- * and returns a value. It is of type `C`, which is a generic type that extends the
1808
- * `BTNCallback<NODE>` type.
1809
- * @returns the callback parameter.
1861
+ * The function `_ensurePredicate` in TypeScript ensures that the input is converted into a valid
1862
+ * predicate function for a binary tree node.
1863
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrEntryOrRawOrPredicate - The
1864
+ * `_ensurePredicate` method in the provided code snippet is responsible for ensuring that the input
1865
+ * parameter `keyOrEntryOrRawOrPredicate` is transformed into a valid predicate function that can be
1866
+ * used for filtering nodes in a binary tree.
1867
+ * @returns A BTNPredicate<NODE> function is being returned.
1810
1868
  */
1811
- _ensureCallback(identifier, callback = this._DEFAULT_CALLBACK) {
1812
- if ((!callback || callback === this._DEFAULT_CALLBACK) && this.isNode(identifier)) {
1813
- callback = (node => node);
1869
+ _ensurePredicate(keyOrEntryOrRawOrPredicate) {
1870
+ if (keyOrEntryOrRawOrPredicate === null || keyOrEntryOrRawOrPredicate === undefined)
1871
+ return (node) => (node ? false : false);
1872
+ if (this._isPredicated(keyOrEntryOrRawOrPredicate))
1873
+ return keyOrEntryOrRawOrPredicate;
1874
+ if (this.isRealNode(keyOrEntryOrRawOrPredicate))
1875
+ return (node) => node === keyOrEntryOrRawOrPredicate;
1876
+ if (this.isEntry(keyOrEntryOrRawOrPredicate)) {
1877
+ const [key] = keyOrEntryOrRawOrPredicate;
1878
+ return (node) => node.key === key;
1879
+ }
1880
+ if (this.isKey(keyOrEntryOrRawOrPredicate))
1881
+ return (node) => node.key === keyOrEntryOrRawOrPredicate;
1882
+ if (this._toEntryFn) {
1883
+ const [key] = this._toEntryFn(keyOrEntryOrRawOrPredicate);
1884
+ return (node) => node.key === key;
1814
1885
  }
1815
- return callback;
1886
+ return (node) => node.key === keyOrEntryOrRawOrPredicate;
1887
+ }
1888
+ /**
1889
+ * Time Complexity: O(1)
1890
+ * Space Complexity: O(1)
1891
+ *
1892
+ * The function `_isPredicated` checks if a given parameter is a function.
1893
+ * @param {any} p - The parameter `p` is a variable of type `any`, which means it can hold any type
1894
+ * of value. In this context, the function `_isPredicated` is checking if `p` is a function that
1895
+ * satisfies the type `BTNPredicate<NODE>`.
1896
+ * @returns The function is checking if the input `p` is a function and returning a boolean value
1897
+ * based on that check. If `p` is a function, it will return `true`, indicating that `p` is a
1898
+ * predicate function for a binary tree node. If `p` is not a function, it will return `false`.
1899
+ */
1900
+ _isPredicated(p) {
1901
+ return typeof p === 'function';
1816
1902
  }
1817
1903
  }
1818
1904
  exports.BinaryTree = BinaryTree;