red-black-tree-typed 1.53.6 → 1.54.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (121) hide show
  1. package/README.md +52 -0
  2. package/dist/common/index.d.ts +12 -0
  3. package/dist/common/index.js +28 -0
  4. package/dist/data-structures/base/iterable-entry-base.js +4 -4
  5. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +213 -0
  6. package/dist/data-structures/binary-tree/avl-tree-counter.js +407 -0
  7. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +71 -170
  8. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +133 -331
  9. package/dist/data-structures/binary-tree/avl-tree.d.ts +103 -69
  10. package/dist/data-structures/binary-tree/avl-tree.js +131 -71
  11. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +3 -0
  12. package/dist/data-structures/binary-tree/binary-indexed-tree.js +3 -0
  13. package/dist/data-structures/binary-tree/binary-tree.d.ts +309 -208
  14. package/dist/data-structures/binary-tree/binary-tree.js +382 -300
  15. package/dist/data-structures/binary-tree/bst.d.ts +245 -127
  16. package/dist/data-structures/binary-tree/bst.js +366 -163
  17. package/dist/data-structures/binary-tree/index.d.ts +3 -1
  18. package/dist/data-structures/binary-tree/index.js +3 -1
  19. package/dist/data-structures/binary-tree/red-black-tree.d.ts +286 -0
  20. package/dist/data-structures/binary-tree/{rb-tree.js → red-black-tree.js} +181 -108
  21. package/dist/data-structures/binary-tree/tree-counter.d.ts +212 -0
  22. package/dist/data-structures/binary-tree/tree-counter.js +444 -0
  23. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +78 -170
  24. package/dist/data-structures/binary-tree/tree-multi-map.js +145 -367
  25. package/dist/data-structures/graph/abstract-graph.js +2 -2
  26. package/dist/data-structures/graph/directed-graph.d.ts +3 -0
  27. package/dist/data-structures/graph/directed-graph.js +3 -0
  28. package/dist/data-structures/graph/map-graph.d.ts +3 -0
  29. package/dist/data-structures/graph/map-graph.js +3 -0
  30. package/dist/data-structures/graph/undirected-graph.d.ts +3 -0
  31. package/dist/data-structures/graph/undirected-graph.js +3 -0
  32. package/dist/data-structures/hash/hash-map.d.ts +31 -1
  33. package/dist/data-structures/hash/hash-map.js +35 -5
  34. package/dist/data-structures/heap/heap.d.ts +26 -9
  35. package/dist/data-structures/heap/heap.js +37 -17
  36. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +64 -19
  37. package/dist/data-structures/linked-list/doubly-linked-list.js +92 -31
  38. package/dist/data-structures/linked-list/singly-linked-list.d.ts +48 -12
  39. package/dist/data-structures/linked-list/singly-linked-list.js +74 -27
  40. package/dist/data-structures/linked-list/skip-linked-list.d.ts +3 -0
  41. package/dist/data-structures/linked-list/skip-linked-list.js +3 -0
  42. package/dist/data-structures/matrix/matrix.d.ts +3 -0
  43. package/dist/data-structures/matrix/matrix.js +3 -0
  44. package/dist/data-structures/matrix/navigator.d.ts +3 -0
  45. package/dist/data-structures/matrix/navigator.js +3 -0
  46. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +3 -0
  47. package/dist/data-structures/priority-queue/max-priority-queue.js +3 -0
  48. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +3 -0
  49. package/dist/data-structures/priority-queue/min-priority-queue.js +3 -0
  50. package/dist/data-structures/queue/deque.d.ts +37 -8
  51. package/dist/data-structures/queue/deque.js +73 -29
  52. package/dist/data-structures/queue/queue.d.ts +41 -1
  53. package/dist/data-structures/queue/queue.js +51 -9
  54. package/dist/data-structures/stack/stack.d.ts +27 -10
  55. package/dist/data-structures/stack/stack.js +39 -20
  56. package/dist/data-structures/trie/trie.d.ts +111 -10
  57. package/dist/data-structures/trie/trie.js +123 -18
  58. package/dist/index.d.ts +2 -1
  59. package/dist/index.js +2 -1
  60. package/dist/interfaces/binary-tree.d.ts +8 -8
  61. package/dist/types/data-structures/base/base.d.ts +1 -1
  62. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
  63. package/dist/types/data-structures/binary-tree/avl-tree-counter.js +2 -0
  64. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -4
  65. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -3
  66. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -4
  67. package/dist/types/data-structures/binary-tree/bst.d.ts +6 -5
  68. package/dist/types/data-structures/binary-tree/index.d.ts +2 -0
  69. package/dist/types/data-structures/binary-tree/index.js +2 -0
  70. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -5
  71. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
  72. package/dist/types/data-structures/binary-tree/tree-counter.js +2 -0
  73. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -5
  74. package/dist/types/utils/utils.d.ts +10 -6
  75. package/dist/utils/utils.js +4 -2
  76. package/package.json +2 -2
  77. package/src/common/index.ts +25 -0
  78. package/src/data-structures/base/iterable-entry-base.ts +4 -4
  79. package/src/data-structures/binary-tree/avl-tree-counter.ts +463 -0
  80. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +152 -373
  81. package/src/data-structures/binary-tree/avl-tree.ts +164 -106
  82. package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -0
  83. package/src/data-structures/binary-tree/binary-tree.ts +563 -447
  84. package/src/data-structures/binary-tree/bst.ts +433 -237
  85. package/src/data-structures/binary-tree/index.ts +3 -1
  86. package/src/data-structures/binary-tree/{rb-tree.ts → red-black-tree.ts} +224 -146
  87. package/src/data-structures/binary-tree/tree-counter.ts +504 -0
  88. package/src/data-structures/binary-tree/tree-multi-map.ts +159 -401
  89. package/src/data-structures/graph/abstract-graph.ts +2 -2
  90. package/src/data-structures/graph/directed-graph.ts +3 -0
  91. package/src/data-structures/graph/map-graph.ts +3 -0
  92. package/src/data-structures/graph/undirected-graph.ts +3 -0
  93. package/src/data-structures/hash/hash-map.ts +37 -7
  94. package/src/data-structures/heap/heap.ts +72 -49
  95. package/src/data-structures/linked-list/doubly-linked-list.ts +186 -118
  96. package/src/data-structures/linked-list/singly-linked-list.ts +81 -28
  97. package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
  98. package/src/data-structures/matrix/matrix.ts +3 -0
  99. package/src/data-structures/matrix/navigator.ts +3 -0
  100. package/src/data-structures/priority-queue/max-priority-queue.ts +3 -0
  101. package/src/data-structures/priority-queue/min-priority-queue.ts +3 -0
  102. package/src/data-structures/queue/deque.ts +72 -28
  103. package/src/data-structures/queue/queue.ts +50 -7
  104. package/src/data-structures/stack/stack.ts +39 -20
  105. package/src/data-structures/trie/trie.ts +123 -17
  106. package/src/index.ts +4 -3
  107. package/src/interfaces/binary-tree.ts +10 -21
  108. package/src/types/data-structures/base/base.ts +1 -1
  109. package/src/types/data-structures/binary-tree/avl-tree-counter.ts +3 -0
  110. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -6
  111. package/src/types/data-structures/binary-tree/avl-tree.ts +0 -5
  112. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -6
  113. package/src/types/data-structures/binary-tree/bst.ts +8 -7
  114. package/src/types/data-structures/binary-tree/index.ts +3 -1
  115. package/src/types/data-structures/binary-tree/red-black-tree.ts +5 -0
  116. package/src/types/data-structures/binary-tree/tree-counter.ts +3 -0
  117. package/src/types/data-structures/binary-tree/tree-multi-map.ts +2 -7
  118. package/src/types/utils/utils.ts +16 -10
  119. package/src/utils/utils.ts +4 -2
  120. package/dist/data-structures/binary-tree/rb-tree.d.ts +0 -205
  121. package/src/types/data-structures/binary-tree/rb-tree.ts +0 -10
@@ -11,14 +11,28 @@ exports.BinaryTree = exports.BinaryTreeNode = void 0;
11
11
  const utils_1 = require("../../utils");
12
12
  const queue_1 = require("../queue");
13
13
  const base_1 = require("../base");
14
- const constants_1 = require("../../constants");
14
+ const common_1 = require("../../common");
15
15
  /**
16
16
  * Represents a node in a binary tree.
17
17
  * @template V - The type of data stored in the node.
18
- * @template NODE - The type of the family relationship in the binary tree.
18
+ * @template BinaryTreeNode<K, V> - 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 in TypeScript.
23
+ * @param {K} key - The `key` parameter in the constructor function is used to store the key value
24
+ * for the key-value pair.
25
+ * @param {V} [value] - The `value` parameter in the constructor is optional, meaning it does not
26
+ * have to be provided when creating an instance of the class. If a `value` is not provided, it will
27
+ * default to `undefined`.
28
+ */
21
29
  constructor(key, value) {
30
+ this.parent = undefined;
31
+ this._left = undefined;
32
+ this._right = undefined;
33
+ this._height = 0;
34
+ this._color = 'BLACK';
35
+ this._count = 1;
22
36
  this.key = key;
23
37
  this.value = value;
24
38
  }
@@ -40,15 +54,32 @@ class BinaryTreeNode {
40
54
  }
41
55
  this._right = v;
42
56
  }
57
+ get height() {
58
+ return this._height;
59
+ }
60
+ set height(value) {
61
+ this._height = value;
62
+ }
63
+ get color() {
64
+ return this._color;
65
+ }
66
+ set color(value) {
67
+ this._color = value;
68
+ }
69
+ get count() {
70
+ return this._count;
71
+ }
72
+ set count(value) {
73
+ this._count = value;
74
+ }
43
75
  get familyPosition() {
44
- const that = this;
45
76
  if (!this.parent) {
46
77
  return this.left || this.right ? 'ROOT' : 'ISOLATED';
47
78
  }
48
- if (this.parent.left === that) {
79
+ if (this.parent.left === this) {
49
80
  return this.left || this.right ? 'ROOT_LEFT' : 'LEFT';
50
81
  }
51
- else if (this.parent.right === that) {
82
+ else if (this.parent.right === this) {
52
83
  return this.left || this.right ? 'ROOT_RIGHT' : 'RIGHT';
53
84
  }
54
85
  return 'MAL_NODE';
@@ -64,13 +95,13 @@ exports.BinaryTreeNode = BinaryTreeNode;
64
95
  */
65
96
  class BinaryTree extends base_1.IterableEntryBase {
66
97
  /**
67
- * The constructor initializes a binary tree with optional options and adds keys, nodes, entries, or
68
- * raw data if provided.
69
- * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor
70
- * is an iterable that can contain elements of type `BTNRep<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:
98
+ * This TypeScript constructor function initializes a binary tree with optional options and adds
99
+ * elements based on the provided input.
100
+ * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
101
+ * iterable that can contain either objects of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. It
102
+ * is used to initialize the binary tree with keys, nodes, entries, or raw data.
103
+ * @param [options] - The `options` parameter in the constructor is an optional object that can
104
+ * contain the following properties:
74
105
  */
75
106
  constructor(keysNodesEntriesOrRaws = [], options) {
76
107
  super();
@@ -113,18 +144,24 @@ class BinaryTree extends base_1.IterableEntryBase {
113
144
  return this._toEntryFn;
114
145
  }
115
146
  /**
147
+ * Time Complexity: O(1)
148
+ * Space Complexity: O(1)
149
+ *
116
150
  * The function creates a new binary tree node with a specified key and optional value.
117
151
  * @param {K} key - The `key` parameter is the key of the node being created in the binary tree.
118
152
  * @param {V} [value] - The `value` parameter in the `createNode` function is optional, meaning it is
119
153
  * not required to be provided when calling the function. If a `value` is provided, it should be of
120
154
  * type `V`, which is the type of the value associated with the node.
121
155
  * @returns A new BinaryTreeNode instance with the provided key and value is being returned, casted
122
- * as NODE.
156
+ * as BinaryTreeNode<K, V>.
123
157
  */
124
158
  createNode(key, value) {
125
159
  return new BinaryTreeNode(key, this._isMapMode ? undefined : value);
126
160
  }
127
161
  /**
162
+ * Time Complexity: O(1)
163
+ * Space Complexity: O(1)
164
+ *
128
165
  * The function creates a binary tree with the specified options.
129
166
  * @param [options] - The `options` parameter in the `createTree` function is an optional parameter
130
167
  * that allows you to provide partial configuration options for creating a binary tree. It is of type
@@ -135,58 +172,14 @@ class BinaryTree extends base_1.IterableEntryBase {
135
172
  createTree(options) {
136
173
  return new BinaryTree([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, toEntryFn: this._toEntryFn }, options));
137
174
  }
138
- /**
139
- * The function `keyValueNodeEntryRawToNodeAndValue` converts various input types into a node object
140
- * or returns null.
141
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
142
- * `keyValueNodeEntryRawToNodeAndValue` function takes in a parameter `keyNodeEntryOrRaw`, which
143
- * can be of type `BTNRep<K, V, NODE>` or `R`. This parameter represents either a key, a
144
- * node, an entry
145
- * @param {V} [value] - The `value` parameter in the `keyValueNodeEntryRawToNodeAndValue` function is
146
- * an optional parameter of type `V`. It represents the value associated with the key in the node
147
- * being created. If a `value` is provided, it will be used when creating the node. If
148
- * @returns The `keyValueNodeEntryRawToNodeAndValue` function returns an optional node
149
- * (`OptNodeOrNull<NODE>`) based on the input parameters provided. The function checks the type of the
150
- * input parameter (`keyNodeEntryOrRaw`) and processes it accordingly to return a node or null
151
- * value.
152
- */
153
- keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value) {
154
- if (keyNodeEntryOrRaw === undefined)
155
- return [undefined, undefined];
156
- if (keyNodeEntryOrRaw === null)
157
- return [null, undefined];
158
- if (this.isNode(keyNodeEntryOrRaw))
159
- return [keyNodeEntryOrRaw, value];
160
- if (this.isEntry(keyNodeEntryOrRaw)) {
161
- const [key, entryValue] = keyNodeEntryOrRaw;
162
- if (key === undefined)
163
- return [undefined, undefined];
164
- else if (key === null)
165
- return [null, undefined];
166
- const finalValue = value !== null && value !== void 0 ? value : entryValue;
167
- return [this.createNode(key, finalValue), finalValue];
168
- }
169
- if (this.isKey(keyNodeEntryOrRaw))
170
- return [this.createNode(keyNodeEntryOrRaw, value), value];
171
- if (this.isRaw(keyNodeEntryOrRaw)) {
172
- if (this._toEntryFn) {
173
- const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
174
- const finalValue = value !== null && value !== void 0 ? value : entryValue;
175
- if (this.isKey(key))
176
- return [this.createNode(key, finalValue), finalValue];
177
- }
178
- return [undefined, undefined];
179
- }
180
- return [undefined, undefined];
181
- }
182
175
  /**
183
176
  * Time Complexity: O(n)
184
177
  * Space Complexity: O(log n)
185
178
  *
186
179
  * The function `ensureNode` in TypeScript checks if a given input is a node, entry, key, or raw
187
180
  * value and returns the corresponding node or null.
188
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `keyNodeEntryOrRaw`
189
- * parameter in the `ensureNode` function can be of type `BTNRep<K, V, NODE>` or `R`. It
181
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry`
182
+ * parameter in the `ensureNode` function can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. It
190
183
  * is used to determine whether the input is a key, node, entry, or raw data. The
191
184
  * @param {IterationType} iterationType - The `iterationType` parameter in the `ensureNode` function
192
185
  * is used to specify the type of iteration to be performed. It has a default value of
@@ -194,129 +187,167 @@ class BinaryTree extends base_1.IterableEntryBase {
194
187
  * @returns The `ensureNode` function returns either a node, `null`, or `undefined` based on the
195
188
  * conditions specified in the code snippet.
196
189
  */
197
- ensureNode(keyNodeEntryOrRaw, iterationType = this.iterationType) {
198
- if (keyNodeEntryOrRaw === null)
190
+ ensureNode(keyNodeOrEntry, iterationType = this.iterationType) {
191
+ if (keyNodeOrEntry === null)
199
192
  return null;
200
- if (keyNodeEntryOrRaw === undefined)
193
+ if (keyNodeOrEntry === undefined)
201
194
  return;
202
- if (keyNodeEntryOrRaw === this._NIL)
195
+ if (keyNodeOrEntry === this._NIL)
203
196
  return;
204
- if (this.isNode(keyNodeEntryOrRaw))
205
- return keyNodeEntryOrRaw;
206
- if (this.isEntry(keyNodeEntryOrRaw)) {
207
- const key = keyNodeEntryOrRaw[0];
197
+ if (this.isNode(keyNodeOrEntry))
198
+ return keyNodeOrEntry;
199
+ if (this.isEntry(keyNodeOrEntry)) {
200
+ const key = keyNodeOrEntry[0];
208
201
  if (key === null)
209
202
  return null;
210
203
  if (key === undefined)
211
204
  return;
212
- return this.getNodeByKey(key, iterationType);
213
- }
214
- if (this._toEntryFn) {
215
- const [key] = this._toEntryFn(keyNodeEntryOrRaw);
216
- if (this.isKey(key))
217
- return this.getNodeByKey(key);
205
+ return this.getNode(key, this._root, iterationType);
218
206
  }
219
- if (this.isKey(keyNodeEntryOrRaw))
220
- return this.getNodeByKey(keyNodeEntryOrRaw, iterationType);
221
- return;
207
+ return this.getNode(keyNodeOrEntry, this._root, iterationType);
222
208
  }
223
209
  /**
210
+ * Time Complexity: O(1)
211
+ * Space Complexity: O(1)
212
+ *
224
213
  * The function isNode checks if the input is an instance of BinaryTreeNode.
225
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
226
- * `keyNodeEntryOrRaw` can be either a key, a node, an entry, or raw data. The function is
214
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The parameter
215
+ * `keyNodeOrEntry` can be either a key, a node, an entry, or raw data. The function is
227
216
  * checking if the input is an instance of a `BinaryTreeNode` and returning a boolean value
228
217
  * accordingly.
229
- * @returns The function `isNode` is checking if the input `keyNodeEntryOrRaw` is an instance of
218
+ * @returns The function `isNode` is checking if the input `keyNodeOrEntry` is an instance of
230
219
  * `BinaryTreeNode`. If it is, the function returns `true`, indicating that the input is a node. If
231
220
  * it is not an instance of `BinaryTreeNode`, the function returns `false`, indicating that the input
232
221
  * is not a node.
233
222
  */
234
- isNode(keyNodeEntryOrRaw) {
235
- return keyNodeEntryOrRaw instanceof BinaryTreeNode;
223
+ isNode(keyNodeOrEntry) {
224
+ return keyNodeOrEntry instanceof BinaryTreeNode;
236
225
  }
226
+ /**
227
+ * Time Complexity: O(1)
228
+ * Space Complexity: O(1)
229
+ *
230
+ * The function `isRaw` checks if the input parameter is of type `R` by verifying if it is an object.
231
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>> | R} keyNodeEntryOrRaw - BTNRep<K, V, BinaryTreeNode<K, V>>
232
+ * @returns The function `isRaw` is checking if the `keyNodeEntryOrRaw` parameter is of type `R` by
233
+ * checking if it is an object. If the parameter is an object, the function will return `true`,
234
+ * indicating that it is of type `R`.
235
+ */
237
236
  isRaw(keyNodeEntryOrRaw) {
238
- return typeof keyNodeEntryOrRaw === 'object';
237
+ return this._toEntryFn !== undefined && typeof keyNodeEntryOrRaw === 'object';
239
238
  }
240
239
  /**
240
+ * Time Complexity: O(1)
241
+ * Space Complexity: O(1)
242
+ *
241
243
  * The function `isRealNode` checks if a given input is a valid node in a binary tree.
242
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `keyNodeEntryOrRaw`
243
- * parameter in the `isRealNode` function can be of type `BTNRep<K, V, NODE>` or `R`.
244
- * The function checks if the input parameter is a `NODE` type by verifying if it is not equal
245
- * @returns The function `isRealNode` is checking if the input `keyNodeEntryOrRaw` is a valid
244
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry`
245
+ * parameter in the `isRealNode` function can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`.
246
+ * The function checks if the input parameter is a `BinaryTreeNode<K, V>` type by verifying if it is not equal
247
+ * @returns The function `isRealNode` is checking if the input `keyNodeOrEntry` is a valid
246
248
  * node by comparing it to `this._NIL`, `null`, and `undefined`. If the input is not one of these
247
249
  * values, it then calls the `isNode` method to further determine if the input is a node. The
248
250
  * function will return a boolean value indicating whether the
249
251
  */
250
- isRealNode(keyNodeEntryOrRaw) {
251
- if (keyNodeEntryOrRaw === this._NIL || keyNodeEntryOrRaw === null || keyNodeEntryOrRaw === undefined)
252
+ isRealNode(keyNodeOrEntry) {
253
+ if (keyNodeOrEntry === this._NIL || keyNodeOrEntry === null || keyNodeOrEntry === undefined)
252
254
  return false;
253
- return this.isNode(keyNodeEntryOrRaw);
255
+ return this.isNode(keyNodeOrEntry);
254
256
  }
255
257
  /**
258
+ * Time Complexity: O(1)
259
+ * Space Complexity: O(1)
260
+ *
256
261
  * The function checks if a given input is a valid node or null.
257
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
258
- * `keyNodeEntryOrRaw` in the `isRealNodeOrNull` function can be of type `BTNRep<K,
259
- * V, NODE>` or `R`. It is a union type that can either be a key, a node, an entry, or
262
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The parameter
263
+ * `keyNodeOrEntry` in the `isRealNodeOrNull` function can be of type `BTNRep<K,
264
+ * V, BinaryTreeNode<K, V>>` or `R`. It is a union type that can either be a key, a node, an entry, or
260
265
  * @returns The function `isRealNodeOrNull` is returning a boolean value. It checks if the input
261
- * `keyNodeEntryOrRaw` is either `null` or a real node, and returns `true` if it is a node or
266
+ * `keyNodeOrEntry` is either `null` or a real node, and returns `true` if it is a node or
262
267
  * `null`, and `false` otherwise.
263
268
  */
264
- isRealNodeOrNull(keyNodeEntryOrRaw) {
265
- return keyNodeEntryOrRaw === null || this.isRealNode(keyNodeEntryOrRaw);
269
+ isRealNodeOrNull(keyNodeOrEntry) {
270
+ return keyNodeOrEntry === null || this.isRealNode(keyNodeOrEntry);
266
271
  }
267
272
  /**
273
+ * Time Complexity: O(1)
274
+ * Space Complexity: O(1)
275
+ *
268
276
  * The function isNIL checks if a given key, node, entry, or raw value is equal to the _NIL value.
269
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - BTNRep<K, V,
270
- * NODE> | R
271
- * @returns The function is checking if the `keyNodeEntryOrRaw` parameter is equal to the `_NIL`
277
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - BTNRep<K, V,
278
+ * BinaryTreeNode<K, V>>
279
+ * @returns The function is checking if the `keyNodeOrEntry` parameter is equal to the `_NIL`
272
280
  * property of the current object and returning a boolean value based on that comparison.
273
281
  */
274
- isNIL(keyNodeEntryOrRaw) {
275
- return keyNodeEntryOrRaw === this._NIL;
282
+ isNIL(keyNodeOrEntry) {
283
+ return keyNodeOrEntry === this._NIL;
284
+ }
285
+ /**
286
+ * Time Complexity: O(1)
287
+ * Space Complexity: O(1)
288
+ *
289
+ * The function `isRange` checks if the input parameter is an instance of the `Range` class.
290
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>> | Range<K>}
291
+ * keyNodeEntryOrPredicate - The `keyNodeEntryOrPredicate` parameter in the `isRange` function can be
292
+ * of type `BTNRep<K, V, BinaryTreeNode<K, V>>`, `NodePredicate<BinaryTreeNode<K, V>>`, or
293
+ * `Range<K>`. The function checks if the `keyNodeEntry
294
+ * @returns The `isRange` function is checking if the `keyNodeEntryOrPredicate` parameter is an
295
+ * instance of the `Range` class. If it is an instance of `Range`, the function will return `true`,
296
+ * indicating that the parameter is a `Range<K>`. If it is not an instance of `Range`, the function
297
+ * will return `false`.
298
+ */
299
+ isRange(keyNodeEntryOrPredicate) {
300
+ return keyNodeEntryOrPredicate instanceof common_1.Range;
276
301
  }
277
302
  /**
303
+ * Time Complexity: O(1)
304
+ * Space Complexity: O(1)
305
+ *
278
306
  * The function determines whether a given key, node, entry, or raw data is a leaf node in a binary
279
307
  * tree.
280
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
281
- * `keyNodeEntryOrRaw` can be of type `BTNRep<K, V, NODE>` or `R`. It represents a
308
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The parameter
309
+ * `keyNodeOrEntry` can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. It represents a
282
310
  * key, node, entry, or raw data in a binary tree structure. The function `isLeaf` checks whether the
283
311
  * provided
284
312
  * @returns The function `isLeaf` returns a boolean value indicating whether the input
285
- * `keyNodeEntryOrRaw` is a leaf node in a binary tree.
313
+ * `keyNodeOrEntry` is a leaf node in a binary tree.
286
314
  */
287
- isLeaf(keyNodeEntryOrRaw) {
288
- keyNodeEntryOrRaw = this.ensureNode(keyNodeEntryOrRaw);
289
- if (keyNodeEntryOrRaw === undefined)
315
+ isLeaf(keyNodeOrEntry) {
316
+ keyNodeOrEntry = this.ensureNode(keyNodeOrEntry);
317
+ if (keyNodeOrEntry === undefined)
290
318
  return false;
291
- if (keyNodeEntryOrRaw === null)
319
+ if (keyNodeOrEntry === null)
292
320
  return true;
293
- return !this.isRealNode(keyNodeEntryOrRaw.left) && !this.isRealNode(keyNodeEntryOrRaw.right);
321
+ return !this.isRealNode(keyNodeOrEntry.left) && !this.isRealNode(keyNodeOrEntry.right);
294
322
  }
295
323
  /**
324
+ * Time Complexity: O(1)
325
+ * Space Complexity: O(1)
326
+ *
296
327
  * The function `isEntry` checks if the input is a BTNEntry object by verifying if it is an array
297
328
  * with a length of 2.
298
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `keyNodeEntryOrRaw`
299
- * parameter in the `isEntry` function can be of type `BTNRep<K, V, NODE>` or type `R`.
300
- * The function checks if the provided `keyNodeEntryOrRaw` is of type `BTN
301
- * @returns The `isEntry` function is checking if the `keyNodeEntryOrRaw` parameter is an array
329
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry`
330
+ * parameter in the `isEntry` function can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or type `R`.
331
+ * The function checks if the provided `keyNodeOrEntry` is of type `BTN
332
+ * @returns The `isEntry` function is checking if the `keyNodeOrEntry` parameter is an array
302
333
  * with a length of 2. If it is, then it returns `true`, indicating that the parameter is of type
303
334
  * `BTNEntry<K, V>`. If the condition is not met, it returns `false`.
304
335
  */
305
- isEntry(keyNodeEntryOrRaw) {
306
- return Array.isArray(keyNodeEntryOrRaw) && keyNodeEntryOrRaw.length === 2;
336
+ isEntry(keyNodeOrEntry) {
337
+ return Array.isArray(keyNodeOrEntry) && keyNodeOrEntry.length === 2;
307
338
  }
308
339
  /**
309
340
  * Time Complexity O(1)
310
341
  * Space Complexity O(1)
311
342
  *
312
- * The function `isKey` checks if a given key is comparable.
343
+ * The function `isValidKey` checks if a given key is comparable.
313
344
  * @param {any} key - The `key` parameter is of type `any`, which means it can be any data type in
314
345
  * TypeScript.
315
- * @returns The function `isKey` is checking if the `key` parameter is `null` or if it is comparable.
346
+ * @returns The function `isValidKey` is checking if the `key` parameter is `null` or if it is comparable.
316
347
  * If the `key` is `null`, the function returns `true`. Otherwise, it returns the result of the
317
348
  * `isComparable` function, which is not provided in the code snippet.
318
349
  */
319
- isKey(key) {
350
+ isValidKey(key) {
320
351
  if (key === null)
321
352
  return true;
322
353
  return (0, utils_1.isComparable)(key);
@@ -327,8 +358,8 @@ class BinaryTree extends base_1.IterableEntryBase {
327
358
  *
328
359
  * The `add` function in TypeScript adds a new node to a binary tree while handling duplicate keys
329
360
  * and finding the correct insertion position.
330
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `add` method you provided
331
- * seems to be for adding a new node to a binary tree structure. The `keyNodeEntryOrRaw`
361
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The `add` method you provided
362
+ * seems to be for adding a new node to a binary tree structure. The `keyNodeOrEntry`
332
363
  * parameter in the method can accept different types of values:
333
364
  * @param {V} [value] - The `value` parameter in the `add` method represents the value associated
334
365
  * with the key that you want to add to the binary tree. When adding a key-value pair to the binary
@@ -338,8 +369,8 @@ class BinaryTree extends base_1.IterableEntryBase {
338
369
  * node was successful, and `false` if the insertion position could not be found or if a duplicate
339
370
  * key was found and the node was replaced instead of inserted.
340
371
  */
341
- add(keyNodeEntryOrRaw, value) {
342
- const [newNode, newValue] = this.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
372
+ add(keyNodeOrEntry, value) {
373
+ const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
343
374
  if (newNode === undefined)
344
375
  return false;
345
376
  // If the tree is empty, directly set the new node as the root node
@@ -394,14 +425,14 @@ class BinaryTree extends base_1.IterableEntryBase {
394
425
  }
395
426
  /**
396
427
  * Time Complexity: O(k * n)
397
- * Space Complexity: O(1)
428
+ * Space Complexity: O(k)
398
429
  *
399
430
  * The `addMany` function takes in multiple keys or nodes or entries or raw values along with
400
431
  * optional values, and adds them to a data structure while returning an array indicating whether
401
432
  * each insertion was successful.
402
433
  * @param keysNodesEntriesOrRaws - `keysNodesEntriesOrRaws` is an iterable that can contain a
403
434
  * mix of keys, nodes, entries, or raw values. Each element in this iterable can be of type
404
- * `BTNRep<K, V, NODE>` or `R`.
435
+ * `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`.
405
436
  * @param [values] - The `values` parameter in the `addMany` function is an optional parameter that
406
437
  * accepts an iterable of values. These values correspond to the keys or nodes being added in the
407
438
  * `keysNodesEntriesOrRaws` parameter. If provided, the function will iterate over the values and
@@ -417,7 +448,7 @@ class BinaryTree extends base_1.IterableEntryBase {
417
448
  if (values) {
418
449
  valuesIterator = values[Symbol.iterator]();
419
450
  }
420
- for (const keyNodeEntryOrRaw of keysNodesEntriesOrRaws) {
451
+ for (let keyNodeEntryOrRaw of keysNodesEntriesOrRaws) {
421
452
  let value = undefined;
422
453
  if (valuesIterator) {
423
454
  const valueResult = valuesIterator.next();
@@ -425,10 +456,23 @@ class BinaryTree extends base_1.IterableEntryBase {
425
456
  value = valueResult.value;
426
457
  }
427
458
  }
459
+ if (this.isRaw(keyNodeEntryOrRaw))
460
+ keyNodeEntryOrRaw = this._toEntryFn(keyNodeEntryOrRaw);
428
461
  inserted.push(this.add(keyNodeEntryOrRaw, value));
429
462
  }
430
463
  return inserted;
431
464
  }
465
+ /**
466
+ * Time Complexity: O(k * n)
467
+ * Space Complexity: O(1)
468
+ *
469
+ * The `merge` function in TypeScript merges another binary tree into the current tree by adding all
470
+ * elements from the other tree.
471
+ * @param anotherTree - BinaryTree<K, V, R, MK, MV, MR>
472
+ */
473
+ merge(anotherTree) {
474
+ this.addMany(anotherTree, []);
475
+ }
432
476
  /**
433
477
  * Time Complexity: O(k * n)
434
478
  * Space Complexity: O(1)
@@ -436,7 +480,7 @@ class BinaryTree extends base_1.IterableEntryBase {
436
480
  * The `refill` function clears the existing data structure and then adds new key-value pairs based
437
481
  * on the provided input.
438
482
  * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the `refill`
439
- * method can accept an iterable containing a mix of `BTNRep<K, V, NODE>` objects or `R`
483
+ * method can accept an iterable containing a mix of `BTNRep<K, V, BinaryTreeNode<K, V>>` objects or `R`
440
484
  * objects.
441
485
  * @param [values] - The `values` parameter in the `refill` method is an optional parameter that
442
486
  * accepts an iterable of values of type `V` or `undefined`.
@@ -451,7 +495,7 @@ class BinaryTree extends base_1.IterableEntryBase {
451
495
  *
452
496
  * The function `delete` in TypeScript implements the deletion of a node in a binary tree and returns
453
497
  * the deleted node along with information for tree balancing.
454
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw
498
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry
455
499
  * - The `delete` method you provided is used to delete a node from a binary tree based on the key,
456
500
  * node, entry or raw data. The method returns an array of
457
501
  * `BinaryTreeDeleteResult` objects containing information about the deleted node and whether
@@ -460,11 +504,11 @@ class BinaryTree extends base_1.IterableEntryBase {
460
504
  * the array contains information about the node that was deleted (`deleted`) and the node that may
461
505
  * need to be balanced (`needBalanced`).
462
506
  */
463
- delete(keyNodeEntryOrRaw) {
507
+ delete(keyNodeOrEntry) {
464
508
  const deletedResult = [];
465
509
  if (!this._root)
466
510
  return deletedResult;
467
- const curr = this.getNode(keyNodeEntryOrRaw);
511
+ const curr = this.getNode(keyNodeOrEntry);
468
512
  if (!curr)
469
513
  return deletedResult;
470
514
  const parent = curr === null || curr === void 0 ? void 0 : curr.parent;
@@ -511,37 +555,40 @@ class BinaryTree extends base_1.IterableEntryBase {
511
555
  * Time Complexity: O(n)
512
556
  * Space Complexity: O(k + log n)
513
557
  *
514
- * The function `getNodes` retrieves nodes from a binary tree based on a key, node, entry, raw data,
515
- * or predicate, with options for recursive or iterative traversal.
516
- * @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate
517
- * - The `getNodes` function you provided takes several parameters:
518
- * @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` function is a boolean flag that
519
- * determines whether to return only the first node that matches the criteria specified by the
520
- * `keyNodeEntryRawOrPredicate` parameter. If `onlyOne` is set to `true`, the function will
521
- * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
522
- * `getNodes` function is used to specify the starting point for traversing the binary tree. It
523
- * represents the root node of the binary tree or the node from which the traversal should begin. If
524
- * not provided, the default value is set to `this._root
525
- * @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` function
526
- * determines the type of iteration to be performed when traversing the nodes of a binary tree. It
527
- * can have two possible values:
528
- * @returns The `getNodes` function returns an array of nodes that satisfy the provided condition
529
- * based on the input parameters and the iteration type specified.
558
+ * The `search` function in TypeScript performs a depth-first or breadth-first search on a tree
559
+ * structure based on a given predicate or key, with options to return multiple results or just one.
560
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate - The
561
+ * `keyNodeEntryOrPredicate` parameter in the `search` function can accept three types of values:
562
+ * @param [onlyOne=false] - The `onlyOne` parameter in the `search` function is a boolean flag that
563
+ * determines whether the search should stop after finding the first matching node. If `onlyOne` is
564
+ * set to `true`, the search will return as soon as a matching node is found. If `onlyOne` is
565
+ * @param {C} callback - The `callback` parameter in the `search` function is a callback function
566
+ * that will be called on each node that matches the search criteria. It is of type `C`, which
567
+ * extends `NodeCallback<BinaryTreeNode<K, V>>`. The default value for `callback` is `this._DEFAULT_NODE_CALLBACK` if
568
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `search` function is
569
+ * used to specify the node from which the search operation should begin. It represents the starting
570
+ * point in the binary tree where the search will be performed. If no specific `startNode` is
571
+ * provided, the search operation will start from the root
572
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `search` function
573
+ * specifies the type of iteration to be used when searching for nodes in a binary tree. It can have
574
+ * two possible values:
575
+ * @returns The `search` function returns an array of values that match the provided criteria based
576
+ * on the search algorithm implemented within the function.
530
577
  */
531
- getNodes(keyNodeEntryRawOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
532
- if (keyNodeEntryRawOrPredicate === undefined)
578
+ search(keyNodeEntryOrPredicate, onlyOne = false, callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
579
+ if (keyNodeEntryOrPredicate === undefined)
533
580
  return [];
534
- if (keyNodeEntryRawOrPredicate === null)
581
+ if (keyNodeEntryOrPredicate === null)
535
582
  return [];
536
583
  startNode = this.ensureNode(startNode);
537
584
  if (!startNode)
538
585
  return [];
539
- const callback = this._ensurePredicate(keyNodeEntryRawOrPredicate);
586
+ const predicate = this._ensurePredicate(keyNodeEntryOrPredicate);
540
587
  const ans = [];
541
588
  if (iterationType === 'RECURSIVE') {
542
589
  const dfs = (cur) => {
543
- if (callback(cur)) {
544
- ans.push(cur);
590
+ if (predicate(cur)) {
591
+ ans.push(callback(cur));
545
592
  if (onlyOne)
546
593
  return;
547
594
  }
@@ -559,8 +606,8 @@ class BinaryTree extends base_1.IterableEntryBase {
559
606
  while (stack.length > 0) {
560
607
  const cur = stack.pop();
561
608
  if (this.isRealNode(cur)) {
562
- if (callback(cur)) {
563
- ans.push(cur);
609
+ if (predicate(cur)) {
610
+ ans.push(callback(cur));
564
611
  if (onlyOne)
565
612
  return ans;
566
613
  }
@@ -575,14 +622,38 @@ class BinaryTree extends base_1.IterableEntryBase {
575
622
  }
576
623
  /**
577
624
  * Time Complexity: O(n)
578
- * Space Complexity: O(log n).
625
+ * Space Complexity: O(k + log n)
626
+ *
627
+ * The function `getNodes` retrieves nodes from a binary tree based on a key, node, entry, raw data,
628
+ * or predicate, with options for recursive or iterative traversal.
629
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
630
+ * - The `getNodes` function you provided takes several parameters:
631
+ * @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` function is a boolean flag that
632
+ * determines whether to return only the first node that matches the criteria specified by the
633
+ * `keyNodeEntryOrPredicate` parameter. If `onlyOne` is set to `true`, the function will
634
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
635
+ * `getNodes` function is used to specify the starting point for traversing the binary tree. It
636
+ * represents the root node of the binary tree or the node from which the traversal should begin. If
637
+ * not provided, the default value is set to `this._root
638
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` function
639
+ * determines the type of iteration to be performed when traversing the nodes of a binary tree. It
640
+ * can have two possible values:
641
+ * @returns The `getNodes` function returns an array of nodes that satisfy the provided condition
642
+ * based on the input parameters and the iteration type specified.
643
+ */
644
+ getNodes(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
645
+ return this.search(keyNodeEntryOrPredicate, onlyOne, node => node, startNode, iterationType);
646
+ }
647
+ /**
648
+ * Time Complexity: O(n)
649
+ * Space Complexity: O(log n)
579
650
  *
580
651
  * The `getNode` function retrieves a node based on the provided key, node, entry, raw data, or
581
652
  * predicate.
582
- * @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate
583
- * - The `keyNodeEntryRawOrPredicate` parameter in the `getNode` function can accept a key,
653
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
654
+ * - The `keyNodeEntryOrPredicate` parameter in the `getNode` function can accept a key,
584
655
  * node, entry, raw data, or a predicate function.
585
- * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
656
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
586
657
  * `getNode` function is used to specify the starting point for searching for a node in a binary
587
658
  * tree. If no specific starting point is provided, the default value is set to `this._root`, which
588
659
  * is typically the root node of the binary tree.
@@ -593,25 +664,8 @@ class BinaryTree extends base_1.IterableEntryBase {
593
664
  * @returns The `getNode` function is returning the first node that matches the specified criteria,
594
665
  * or `null` if no matching node is found.
595
666
  */
596
- getNode(keyNodeEntryRawOrPredicate, startNode = this._root, iterationType = this.iterationType) {
597
- var _a;
598
- return (_a = this.getNodes(keyNodeEntryRawOrPredicate, true, startNode, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
599
- }
600
- /**
601
- * Time Complexity: O(n)
602
- * Space Complexity: O(log n)
603
- *
604
- * The function `getNodeByKey` retrieves a node by its key from a binary tree structure.
605
- * @param {K} key - The `key` parameter is the value used to search for a specific node in a data
606
- * structure.
607
- * @param {IterationType} iterationType - The `iterationType` parameter is a type of iteration that
608
- * specifies how the tree nodes should be traversed when searching for a node with the given key. It
609
- * is an optional parameter with a default value of `this.iterationType`.
610
- * @returns The `getNodeByKey` function is returning an optional binary tree node
611
- * (`OptNodeOrNull<NODE>`).
612
- */
613
- getNodeByKey(key, iterationType = this.iterationType) {
614
- return this.getNode(key, this._root, iterationType);
667
+ getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
668
+ return this.search(keyNodeEntryOrPredicate, true, node => node, startNode, iterationType)[0];
615
669
  }
616
670
  /**
617
671
  * Time Complexity: O(n)
@@ -619,10 +673,10 @@ class BinaryTree extends base_1.IterableEntryBase {
619
673
  *
620
674
  * This function overrides the `get` method to retrieve the value associated with a specified key,
621
675
  * node, entry, raw data, or predicate in a data structure.
622
- * @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate
623
- * - The `keyNodeEntryRawOrPredicate` parameter in the `get` method can accept one of the
676
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
677
+ * - The `keyNodeEntryOrPredicate` parameter in the `get` method can accept one of the
624
678
  * following types:
625
- * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `get`
679
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `get`
626
680
  * method is used to specify the starting point for searching for a key or node in the binary tree.
627
681
  * If no specific starting point is provided, the default starting point is the root of the binary
628
682
  * tree (`this._root`).
@@ -635,15 +689,15 @@ class BinaryTree extends base_1.IterableEntryBase {
635
689
  * the method returns the corresponding value. If the key or node is not found, it returns
636
690
  * `undefined`.
637
691
  */
638
- get(keyNodeEntryRawOrPredicate, startNode = this._root, iterationType = this.iterationType) {
692
+ get(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
639
693
  var _a;
640
694
  if (this._isMapMode) {
641
- const key = this._getKey(keyNodeEntryRawOrPredicate);
695
+ const key = this._extractKey(keyNodeEntryOrPredicate);
642
696
  if (key === null || key === undefined)
643
697
  return;
644
698
  return this._store.get(key);
645
699
  }
646
- return (_a = this.getNode(keyNodeEntryRawOrPredicate, startNode, iterationType)) === null || _a === void 0 ? void 0 : _a.value;
700
+ return (_a = this.getNode(keyNodeEntryOrPredicate, startNode, iterationType)) === null || _a === void 0 ? void 0 : _a.value;
647
701
  }
648
702
  /**
649
703
  * Time Complexity: O(n)
@@ -651,10 +705,10 @@ class BinaryTree extends base_1.IterableEntryBase {
651
705
  *
652
706
  * The `has` function in TypeScript checks if a specified key, node, entry, raw data, or predicate
653
707
  * exists in the data structure.
654
- * @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate
655
- * - The `keyNodeEntryRawOrPredicate` parameter in the `override has` method can accept one of
708
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
709
+ * - The `keyNodeEntryOrPredicate` parameter in the `override has` method can accept one of
656
710
  * the following types:
657
- * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
711
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
658
712
  * `override` method is used to specify the starting point for the search operation within the data
659
713
  * structure. It defaults to `this._root` if not provided explicitly.
660
714
  * @param {IterationType} iterationType - The `iterationType` parameter in the `override has` method
@@ -666,14 +720,14 @@ class BinaryTree extends base_1.IterableEntryBase {
666
720
  * are matching nodes, it returns `true`, indicating that the tree contains the specified element.
667
721
  * Otherwise, it returns `false`.
668
722
  */
669
- has(keyNodeEntryRawOrPredicate, startNode = this._root, iterationType = this.iterationType) {
670
- return this.getNodes(keyNodeEntryRawOrPredicate, true, startNode, iterationType).length > 0;
723
+ has(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
724
+ return this.search(keyNodeEntryOrPredicate, true, node => node, startNode, iterationType).length > 0;
671
725
  }
672
726
  /**
673
727
  * Time Complexity: O(1)
674
728
  * Space Complexity: O(1)
675
729
  *
676
- * The `clear` function resets the root node and size of a data structure to empty.
730
+ * The clear function removes nodes and values in map mode.
677
731
  */
678
732
  clear() {
679
733
  this._clearNodes();
@@ -698,7 +752,7 @@ class BinaryTree extends base_1.IterableEntryBase {
698
752
  *
699
753
  * The function checks if a binary tree is perfectly balanced by comparing its minimum height with
700
754
  * its height.
701
- * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
755
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter is the starting
702
756
  * point for checking if the binary tree is perfectly balanced. It represents the root node of the
703
757
  * binary tree or a specific node from which the balance check should begin.
704
758
  * @returns The method `isPerfectlyBalanced` is returning a boolean value, which indicates whether
@@ -712,11 +766,11 @@ class BinaryTree extends base_1.IterableEntryBase {
712
766
  }
713
767
  /**
714
768
  * Time Complexity: O(n)
715
- * Space Complexity: O(1)
769
+ * Space Complexity: O(log n)
716
770
  *
717
771
  * The function `isBST` in TypeScript checks if a binary search tree is valid using either recursive
718
772
  * or iterative methods.
719
- * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `isBST`
773
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `isBST`
720
774
  * function represents the starting point for checking whether a binary search tree (BST) is valid.
721
775
  * It can be a node in the BST or a reference to the root of the BST. If no specific node is
722
776
  * provided, the function will default to
@@ -772,13 +826,13 @@ class BinaryTree extends base_1.IterableEntryBase {
772
826
  }
773
827
  /**
774
828
  * Time Complexity: O(n)
775
- * Space Complexity: O(1)
829
+ * Space Complexity: O(log n)
776
830
  *
777
831
  * The `getDepth` function calculates the depth between two nodes in a binary tree.
778
- * @param {BTNRep<K, V, NODE> | R} dist - The `dist` parameter in the `getDepth`
832
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} dist - The `dist` parameter in the `getDepth`
779
833
  * function represents the node or entry in a binary tree map, or a reference to a node in the tree.
780
834
  * It is the target node for which you want to calculate the depth from the `startNode` node.
781
- * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
835
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
782
836
  * `getDepth` function represents the starting point from which you want to calculate the depth of a
783
837
  * given node or entry in a binary tree. If no specific starting point is provided, the default value
784
838
  * for `startNode` is set to the root of the binary
@@ -801,11 +855,11 @@ class BinaryTree extends base_1.IterableEntryBase {
801
855
  }
802
856
  /**
803
857
  * Time Complexity: O(n)
804
- * Space Complexity: O(1)
858
+ * Space Complexity: O(log n)
805
859
  *
806
860
  * The `getHeight` function calculates the maximum height of a binary tree using either a recursive
807
861
  * or iterative approach in TypeScript.
808
- * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
862
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter is the starting
809
863
  * point from which the height of the binary tree will be calculated. It can be a node in the binary
810
864
  * tree or a reference to the root of the tree. If not provided, it defaults to the root of the
811
865
  * binary tree data structure.
@@ -850,7 +904,7 @@ class BinaryTree extends base_1.IterableEntryBase {
850
904
  *
851
905
  * The `getMinHeight` function calculates the minimum height of a binary tree using either a
852
906
  * recursive or iterative approach in TypeScript.
853
- * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
907
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
854
908
  * `getMinHeight` function represents the starting node from which the minimum height of the binary
855
909
  * tree will be calculated. It is either a node in the binary tree or a reference to the root of the
856
910
  * tree. If not provided, the default value is the root
@@ -916,7 +970,7 @@ class BinaryTree extends base_1.IterableEntryBase {
916
970
  * the path to the root. It is expected to be a function that takes a node as an argument and returns
917
971
  * a value based on that node. The return type of the callback function is determined by the generic
918
972
  * type `C
919
- * @param {BTNRep<K, V, NODE> | R} beginNode - The `beginNode` parameter in the
973
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} beginNode - The `beginNode` parameter in the
920
974
  * `getPathToRoot` function can be either a key, a node, an entry, or any other value of type `R`.
921
975
  * @param [isReverse=true] - The `isReverse` parameter in the `getPathToRoot` function determines
922
976
  * whether the resulting path from the given `beginNode` to the root should be in reverse order or
@@ -926,7 +980,7 @@ class BinaryTree extends base_1.IterableEntryBase {
926
980
  * array is either in reverse order or in the original order based on the value of the `isReverse`
927
981
  * parameter.
928
982
  */
929
- getPathToRoot(callback = this._DEFAULT_NODE_CALLBACK, beginNode, isReverse = true) {
983
+ getPathToRoot(beginNode, callback = this._DEFAULT_NODE_CALLBACK, isReverse = false) {
930
984
  const result = [];
931
985
  let beginNodeEnsured = this.ensureNode(beginNode);
932
986
  if (!beginNodeEnsured)
@@ -941,14 +995,14 @@ class BinaryTree extends base_1.IterableEntryBase {
941
995
  }
942
996
  /**
943
997
  * Time Complexity: O(log n)
944
- * Space Complexity: O(1)
998
+ * Space Complexity: O(log n)
945
999
  *
946
1000
  * The function `getLeftMost` retrieves the leftmost node in a binary tree using either recursive or
947
1001
  * tail-recursive iteration.
948
1002
  * @param {C} callback - The `callback` parameter is a function that will be called with the leftmost
949
1003
  * node of a binary tree or with `undefined` if the tree is empty. It is provided with a default
950
1004
  * value of `_DEFAULT_NODE_CALLBACK` if not specified.
951
- * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
1005
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
952
1006
  * `getLeftMost` function represents the starting point for finding the leftmost node in a binary
953
1007
  * tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
954
1008
  * starting point is provided, the function will default
@@ -986,15 +1040,15 @@ class BinaryTree extends base_1.IterableEntryBase {
986
1040
  }
987
1041
  /**
988
1042
  * Time Complexity: O(log n)
989
- * Space Complexity: O(1)
1043
+ * Space Complexity: O(log n)
990
1044
  *
991
1045
  * The function `getRightMost` retrieves the rightmost node in a binary tree using either recursive
992
1046
  * or iterative traversal methods.
993
1047
  * @param {C} callback - The `callback` parameter is a function that will be called with the result
994
- * of finding the rightmost node in a binary tree. It is of type `NodeCallback<OptNodeOrNull<NODE>>`,
1048
+ * of finding the rightmost node in a binary tree. It is of type `NodeCallback<OptNodeOrNull<BinaryTreeNode<K, V>>>`,
995
1049
  * which means it is a callback function that can accept either an optional binary tree node or null
996
1050
  * as
997
- * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
1051
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
998
1052
  * `getRightMost` function represents the starting point for finding the rightmost node in a binary
999
1053
  * tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
1000
1054
  * starting point is provided, the function will default
@@ -1009,7 +1063,6 @@ class BinaryTree extends base_1.IterableEntryBase {
1009
1063
  getRightMost(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
1010
1064
  if (this.isNIL(startNode))
1011
1065
  return callback(undefined);
1012
- // TODO support get right most by passing key in
1013
1066
  startNode = this.ensureNode(startNode);
1014
1067
  if (!startNode)
1015
1068
  return callback(startNode);
@@ -1033,14 +1086,14 @@ class BinaryTree extends base_1.IterableEntryBase {
1033
1086
  }
1034
1087
  /**
1035
1088
  * Time Complexity: O(log n)
1036
- * Space Complexity: O(1)
1089
+ * Space Complexity: O(log n)
1037
1090
  *
1038
1091
  * The function `getPredecessor` in TypeScript returns the predecessor node of a given node in a
1039
1092
  * binary tree.
1040
- * @param {NODE} node - The `getPredecessor` function you provided seems to be attempting to find the
1093
+ * @param {BinaryTreeNode<K, V>} node - The `getPredecessor` function you provided seems to be attempting to find the
1041
1094
  * predecessor of a given node in a binary tree. However, there seems to be a logical issue in the
1042
1095
  * while loop condition that might cause an infinite loop.
1043
- * @returns The `getPredecessor` function returns the predecessor node of the input `NODE` parameter.
1096
+ * @returns The `getPredecessor` function returns the predecessor node of the input `BinaryTreeNode<K, V>` parameter.
1044
1097
  * If the left child of the input node exists, it traverses to the rightmost node of the left subtree
1045
1098
  * to find the predecessor. If the left child does not exist, it returns the input node itself.
1046
1099
  */
@@ -1060,12 +1113,12 @@ class BinaryTree extends base_1.IterableEntryBase {
1060
1113
  }
1061
1114
  /**
1062
1115
  * Time Complexity: O(log n)
1063
- * Space Complexity: O(1)
1116
+ * Space Complexity: O(log n)
1064
1117
  *
1065
1118
  * The function `getSuccessor` in TypeScript returns the next node in an in-order traversal of a
1066
1119
  * binary tree.
1067
- * @param {K | NODE | null} [x] - The `getSuccessor` function takes a parameter `x`, which can be of
1068
- * type `K`, `NODE`, or `null`.
1120
+ * @param {K | BinaryTreeNode<K, V> | null} [x] - The `getSuccessor` function takes a parameter `x`, which can be of
1121
+ * type `K`, `BinaryTreeNode<K, V>`, or `null`.
1069
1122
  * @returns The `getSuccessor` function returns the successor node of the input node `x`. If `x` has
1070
1123
  * a right child, the function returns the leftmost node in the right subtree of `x`. If `x` does not
1071
1124
  * have a right child, the function traverses up the parent nodes until it finds a node that is not
@@ -1092,12 +1145,12 @@ class BinaryTree extends base_1.IterableEntryBase {
1092
1145
  * The function `dfs` performs a depth-first search traversal on a binary tree structure based on the
1093
1146
  * specified parameters.
1094
1147
  * @param {C} callback - The `callback` parameter is a generic type `C` that extends the
1095
- * `NodeCallback` interface with a type parameter of `OptNodeOrNull<NODE>`. It has a default value of
1148
+ * `NodeCallback` interface with a type parameter of `OptNodeOrNull<BinaryTreeNode<K, V>>`. It has a default value of
1096
1149
  * `this._DEFAULT_NODE_CALLBACK as C`.
1097
1150
  * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `dfs` method specifies the
1098
1151
  * order in which the Depth-First Search (DFS) algorithm should traverse the nodes in the tree. The
1099
1152
  * possible values for the `pattern` parameter are:
1100
- * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `dfs`
1153
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `dfs`
1101
1154
  * method is used to specify the starting point for the Depth-First Search traversal. It can be
1102
1155
  * either a `BTNRep` object representing a key, node, or entry in the binary tree map,
1103
1156
  * or it can be a
@@ -1125,8 +1178,8 @@ class BinaryTree extends base_1.IterableEntryBase {
1125
1178
  * tree, executing a specified callback function on each node visited.
1126
1179
  * @param {C} callback - The `callback` parameter in the `bfs` function is a function that will be
1127
1180
  * called on each node visited during the breadth-first search traversal. It is a generic type `C`
1128
- * that extends the `NodeCallback` type, which takes a parameter of type `NODE` or `null`.
1129
- * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `bfs`
1181
+ * that extends the `NodeCallback` type, which takes a parameter of type `BinaryTreeNode<K, V>` or `null`.
1182
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `bfs`
1130
1183
  * function represents the starting point for the breadth-first search traversal in a binary tree. It
1131
1184
  * can be specified as a key, node, or entry in the binary tree structure. If not provided, the
1132
1185
  * default value is the root node of the binary
@@ -1146,7 +1199,9 @@ class BinaryTree extends base_1.IterableEntryBase {
1146
1199
  return [];
1147
1200
  const ans = [];
1148
1201
  if (iterationType === 'RECURSIVE') {
1149
- const queue = new queue_1.Queue([startNode]);
1202
+ const queue = new queue_1.Queue([
1203
+ startNode
1204
+ ]);
1150
1205
  const dfs = (level) => {
1151
1206
  if (queue.size === 0)
1152
1207
  return;
@@ -1200,7 +1255,7 @@ class BinaryTree extends base_1.IterableEntryBase {
1200
1255
  * structure based on a specified callback and iteration type.
1201
1256
  * @param {C} callback - The `callback` parameter is a function that will be called on each leaf node
1202
1257
  * in the binary tree. It is optional and defaults to a default callback function if not provided.
1203
- * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `leaves`
1258
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `leaves`
1204
1259
  * method is used to specify the starting point for finding and processing the leaves of a binary
1205
1260
  * tree. It can be provided as either a key, a node, or an entry in the binary tree structure. If not
1206
1261
  * explicitly provided, the default value
@@ -1255,7 +1310,7 @@ class BinaryTree extends base_1.IterableEntryBase {
1255
1310
  * @param {C} callback - The `callback` parameter is a function that will be applied to each node in
1256
1311
  * the binary tree during the traversal. It is used to process each node and determine what
1257
1312
  * information to include in the output for each level of the tree.
1258
- * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
1313
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
1259
1314
  * `listLevels` function represents the starting point for traversing the binary tree. It can be
1260
1315
  * either a key, a node, or an entry in the binary tree. If not provided, the default value is the
1261
1316
  * root of the binary tree.
@@ -1327,11 +1382,11 @@ class BinaryTree extends base_1.IterableEntryBase {
1327
1382
  * Morris Traversal algorithm with different order patterns.
1328
1383
  * @param {C} callback - The `callback` parameter in the `morris` function is a function that will be
1329
1384
  * called on each node in the binary tree during the traversal. It is of type `C`, which extends the
1330
- * `NodeCallback<NODE>` type. The default value for `callback` is `this._DEFAULT
1385
+ * `NodeCallback<BinaryTreeNode<K, V>>` type. The default value for `callback` is `this._DEFAULT
1331
1386
  * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function specifies
1332
1387
  * the type of Depth-First Search (DFS) order pattern to traverse the binary tree. The possible
1333
1388
  * values for the `pattern` parameter are:
1334
- * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `morris`
1389
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `morris`
1335
1390
  * function is the starting point for the Morris traversal algorithm. It represents the root node of
1336
1391
  * the binary tree or the node from which the traversal should begin. It can be provided as either a
1337
1392
  * key, a node, an entry, or a reference
@@ -1437,6 +1492,10 @@ class BinaryTree extends base_1.IterableEntryBase {
1437
1492
  */
1438
1493
  clone() {
1439
1494
  const cloned = this.createTree();
1495
+ this._clone(cloned);
1496
+ return cloned;
1497
+ }
1498
+ _clone(cloned) {
1440
1499
  this.bfs(node => {
1441
1500
  if (node === null)
1442
1501
  cloned.add(null);
@@ -1449,7 +1508,6 @@ class BinaryTree extends base_1.IterableEntryBase {
1449
1508
  }, this._root, this.iterationType, true);
1450
1509
  if (this._isMapMode)
1451
1510
  cloned._store = this._store;
1452
- return cloned;
1453
1511
  }
1454
1512
  /**
1455
1513
  * Time Complexity: O(n)
@@ -1471,7 +1529,7 @@ class BinaryTree extends base_1.IterableEntryBase {
1471
1529
  const newTree = this.createTree();
1472
1530
  let index = 0;
1473
1531
  for (const [key, value] of this) {
1474
- if (predicate.call(thisArg, value, key, index++, this)) {
1532
+ if (predicate.call(thisArg, key, value, index++, this)) {
1475
1533
  newTree.add([key, value]);
1476
1534
  }
1477
1535
  }
@@ -1481,41 +1539,36 @@ class BinaryTree extends base_1.IterableEntryBase {
1481
1539
  * Time Complexity: O(n)
1482
1540
  * Space Complexity: O(n)
1483
1541
  *
1484
- * The `map` function iterates over key-value pairs in a tree data structure, applies a callback
1485
- * function to each value, and returns a new tree with the updated values.
1486
- * @param callback - The `callback` parameter in the `map` method is a function that will be called
1487
- * on each entry in the tree. It takes four arguments:
1488
- * @param {any} [thisArg] - The `thisArg` parameter in the `map` function is an optional parameter
1489
- * that specifies the value to be passed as `this` when executing the callback function. If provided,
1490
- * the `thisArg` value will be used as the `this` value within the callback function. If `thisArg
1491
- * @returns The `map` method is returning a new tree with the entries modified by the provided
1492
- * callback function. Each entry in the original tree is passed to the callback function, and the
1493
- * result of the callback function is added to the new tree.
1542
+ * The `map` function in TypeScript creates a new BinaryTree by applying a callback function to each
1543
+ * entry in the original BinaryTree.
1544
+ * @param callback - A function that will be called for each entry in the current binary tree. It
1545
+ * takes the key, value (which can be undefined), and an array containing the mapped key and value as
1546
+ * arguments.
1547
+ * @param [options] - The `options` parameter in the `map` method is of type `BinaryTreeOptions<MK,
1548
+ * MV, MR>`. It is an optional parameter that allows you to specify additional options for the binary
1549
+ * tree being created during the mapping process. These options could include things like custom
1550
+ * comparators, initial
1551
+ * @param {any} [thisArg] - The `thisArg` parameter in the `map` method is used to specify the value
1552
+ * of `this` when executing the `callback` function. It allows you to set the context (value of
1553
+ * `this`) within the callback function. If `thisArg` is provided, it will be passed
1554
+ * @returns The `map` function is returning a new `BinaryTree` instance filled with entries that are
1555
+ * the result of applying the provided `callback` function to each entry in the original tree.
1494
1556
  */
1495
- map(callback, thisArg) {
1496
- const newTree = this.createTree();
1557
+ map(callback, options, thisArg) {
1558
+ const newTree = new BinaryTree([], options);
1497
1559
  let index = 0;
1498
1560
  for (const [key, value] of this) {
1499
- newTree.add([key, callback.call(thisArg, value, key, index++, this)]);
1561
+ newTree.add(callback.call(thisArg, key, value, index++, this));
1500
1562
  }
1501
1563
  return newTree;
1502
1564
  }
1503
- // // TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
1504
- // // map<NV>(callback: (entry: [K, V | undefined], tree: this) => NV) {
1505
- // // const newTree = this.createTree();
1506
- // // for (const [key, value] of this) {
1507
- // // newTree.add(key, callback([key, value], this));
1508
- // // }
1509
- // // return newTree;
1510
- // // }
1511
- //
1512
1565
  /**
1513
1566
  * Time Complexity: O(n)
1514
1567
  * Space Complexity: O(n)
1515
1568
  *
1516
1569
  * The function `toVisual` in TypeScript overrides the visual representation of a binary tree with
1517
1570
  * customizable options for displaying undefined, null, and sentinel nodes.
1518
- * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
1571
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
1519
1572
  * `toVisual` method is used to specify the starting point for visualizing the binary tree structure.
1520
1573
  * It can be a node, key, entry, or the root of the tree. If no specific starting point is provided,
1521
1574
  * the default is set to the root
@@ -1540,7 +1593,7 @@ class BinaryTree extends base_1.IterableEntryBase {
1540
1593
  if (opts.isShowRedBlackNIL)
1541
1594
  output += `S for Sentinel Node(NIL)\n`;
1542
1595
  const display = (root) => {
1543
- const [lines, , ,] = this._displayAux(root, opts);
1596
+ const [lines] = this._displayAux(root, opts);
1544
1597
  let paragraph = '';
1545
1598
  for (const line of lines) {
1546
1599
  paragraph += line + '\n';
@@ -1560,7 +1613,7 @@ class BinaryTree extends base_1.IterableEntryBase {
1560
1613
  * printing options for the binary tree. It is an optional parameter that allows you to customize how
1561
1614
  * the binary tree is printed, such as choosing between different traversal orders or formatting
1562
1615
  * options.
1563
- * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
1616
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
1564
1617
  * `override print` method is used to specify the starting point for printing the binary tree. It can
1565
1618
  * be either a key, a node, an entry, or the root of the tree. If no specific starting point is
1566
1619
  * provided, the default value is set to
@@ -1568,6 +1621,42 @@ class BinaryTree extends base_1.IterableEntryBase {
1568
1621
  print(options, startNode = this._root) {
1569
1622
  console.log(this.toVisual(startNode, options));
1570
1623
  }
1624
+ /**
1625
+ * Time Complexity: O(1)
1626
+ * Space Complexity: O(1)
1627
+ *
1628
+ * The function `keyValueNodeEntryRawToNodeAndValue` converts various input types into a node object
1629
+ * or returns null.
1630
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The
1631
+ * `keyValueNodeEntryRawToNodeAndValue` function takes in a parameter `keyNodeOrEntry`, which
1632
+ * can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. This parameter represents either a key, a
1633
+ * node, an entry
1634
+ * @param {V} [value] - The `value` parameter in the `keyValueNodeEntryRawToNodeAndValue` function is
1635
+ * an optional parameter of type `V`. It represents the value associated with the key in the node
1636
+ * being created. If a `value` is provided, it will be used when creating the node. If
1637
+ * @returns The `keyValueNodeEntryRawToNodeAndValue` function returns an optional node
1638
+ * (`OptNodeOrNull<BinaryTreeNode<K, V>>`) based on the input parameters provided. The function checks the type of the
1639
+ * input parameter (`keyNodeOrEntry`) and processes it accordingly to return a node or null
1640
+ * value.
1641
+ */
1642
+ _keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value) {
1643
+ if (keyNodeOrEntry === undefined)
1644
+ return [undefined, undefined];
1645
+ if (keyNodeOrEntry === null)
1646
+ return [null, undefined];
1647
+ if (this.isNode(keyNodeOrEntry))
1648
+ return [keyNodeOrEntry, value];
1649
+ if (this.isEntry(keyNodeOrEntry)) {
1650
+ const [key, entryValue] = keyNodeOrEntry;
1651
+ if (key === undefined)
1652
+ return [undefined, undefined];
1653
+ else if (key === null)
1654
+ return [null, undefined];
1655
+ const finalValue = value !== null && value !== void 0 ? value : entryValue;
1656
+ return [this.createNode(key, finalValue), finalValue];
1657
+ }
1658
+ return [this.createNode(keyNodeOrEntry, value), value];
1659
+ }
1571
1660
  /**
1572
1661
  * Time complexity: O(n)
1573
1662
  * Space complexity: O(n)
@@ -1576,11 +1665,11 @@ class BinaryTree extends base_1.IterableEntryBase {
1576
1665
  * the specified order pattern and callback function.
1577
1666
  * @param {C} callback - The `callback` parameter in the `_dfs` method is a function that will be
1578
1667
  * called on each node visited during the depth-first search traversal. It is of type `C`, which
1579
- * extends `NodeCallback<OptNodeOrNull<NODE>>`. The default value for this parameter is `this._DEFAULT
1668
+ * extends `NodeCallback<OptNodeOrNull<BinaryTreeNode<K, V>>>`. The default value for this parameter is `this._DEFAULT
1580
1669
  * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `_dfs` method specifies the
1581
1670
  * order in which the nodes are visited during the Depth-First Search traversal. It can have one of
1582
1671
  * the following values:
1583
- * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `_dfs`
1672
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `_dfs`
1584
1673
  * method is used to specify the starting point for the depth-first search traversal in a binary
1585
1674
  * tree. It can be provided as either a `BTNRep` object or a reference to the root node
1586
1675
  * of the tree. If no specific
@@ -1655,20 +1744,20 @@ class BinaryTree extends base_1.IterableEntryBase {
1655
1744
  dfs(startNode);
1656
1745
  }
1657
1746
  else {
1658
- const stack = [{ opt: constants_1.DFSOperation.VISIT, node: startNode }];
1747
+ const stack = [{ opt: common_1.DFSOperation.VISIT, node: startNode }];
1659
1748
  const pushLeft = (cur) => {
1660
1749
  var _a;
1661
1750
  if (shouldVisitLeft(cur.node))
1662
- stack.push({ opt: constants_1.DFSOperation.VISIT, node: (_a = cur.node) === null || _a === void 0 ? void 0 : _a.left });
1751
+ stack.push({ opt: common_1.DFSOperation.VISIT, node: (_a = cur.node) === null || _a === void 0 ? void 0 : _a.left });
1663
1752
  };
1664
1753
  const pushRight = (cur) => {
1665
1754
  var _a;
1666
1755
  if (shouldVisitRight(cur.node))
1667
- stack.push({ opt: constants_1.DFSOperation.VISIT, node: (_a = cur.node) === null || _a === void 0 ? void 0 : _a.right });
1756
+ stack.push({ opt: common_1.DFSOperation.VISIT, node: (_a = cur.node) === null || _a === void 0 ? void 0 : _a.right });
1668
1757
  };
1669
1758
  const pushRoot = (cur) => {
1670
1759
  if (shouldVisitRoot(cur.node))
1671
- stack.push({ opt: constants_1.DFSOperation.PROCESS, node: cur.node });
1760
+ stack.push({ opt: common_1.DFSOperation.PROCESS, node: cur.node });
1672
1761
  };
1673
1762
  while (stack.length > 0) {
1674
1763
  const cur = stack.pop();
@@ -1676,7 +1765,7 @@ class BinaryTree extends base_1.IterableEntryBase {
1676
1765
  continue;
1677
1766
  if (!shouldVisitRoot(cur.node))
1678
1767
  continue;
1679
- if (cur.opt === constants_1.DFSOperation.PROCESS) {
1768
+ if (cur.opt === common_1.DFSOperation.PROCESS) {
1680
1769
  if (shouldProcessRoot(cur.node))
1681
1770
  ans.push(callback(cur.node));
1682
1771
  }
@@ -1824,12 +1913,12 @@ class BinaryTree extends base_1.IterableEntryBase {
1824
1913
  * Space Complexity: O(1)
1825
1914
  *
1826
1915
  * The _swapProperties function swaps key and value properties between two nodes in a binary tree.
1827
- * @param {BTNRep<K, V, NODE> | R} srcNode - The `srcNode` parameter in the
1916
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} srcNode - The `srcNode` parameter in the
1828
1917
  * `_swapProperties` method can be either a BTNRep object containing key and value
1829
1918
  * properties, or it can be of type R.
1830
- * @param {BTNRep<K, V, NODE> | R} destNode - The `destNode` parameter in the
1919
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} destNode - The `destNode` parameter in the
1831
1920
  * `_swapProperties` method represents the node or entry where the properties will be swapped with
1832
- * the `srcNode`. It can be of type `BTNRep<K, V, NODE>` or `R`. The method ensures that
1921
+ * the `srcNode`. It can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. The method ensures that
1833
1922
  * both `srcNode
1834
1923
  * @returns The `_swapProperties` method returns either the `destNode` with its key and value swapped
1835
1924
  * with the `srcNode`, or `undefined` if either `srcNode` or `destNode` is falsy.
@@ -1857,9 +1946,9 @@ class BinaryTree extends base_1.IterableEntryBase {
1857
1946
  * Space Complexity: O(1)
1858
1947
  *
1859
1948
  * The _replaceNode function replaces an old node with a new node in a binary tree structure.
1860
- * @param {NODE} oldNode - The `oldNode` parameter represents the node that you want to replace in a
1949
+ * @param {BinaryTreeNode<K, V>} oldNode - The `oldNode` parameter represents the node that you want to replace in a
1861
1950
  * tree data structure.
1862
- * @param {NODE} newNode - The `newNode` parameter in the `_replaceNode` function represents the node
1951
+ * @param {BinaryTreeNode<K, V>} newNode - The `newNode` parameter in the `_replaceNode` function represents the node
1863
1952
  * that will replace the `oldNode` in a tree data structure. This function is responsible for
1864
1953
  * updating the parent, left child, right child, and root (if necessary) references when replacing a
1865
1954
  * node in the tree.
@@ -1889,8 +1978,8 @@ class BinaryTree extends base_1.IterableEntryBase {
1889
1978
  *
1890
1979
  * The function _setRoot sets the root node of a data structure while updating the parent reference
1891
1980
  * of the previous root node.
1892
- * @param v - The parameter `v` in the `_setRoot` method is of type `OptNodeOrNull<NODE>`, which means
1893
- * it can either be an optional `NODE` type or `null`.
1981
+ * @param v - The parameter `v` in the `_setRoot` method is of type `OptNodeOrNull<BinaryTreeNode<K, V>>`, which means
1982
+ * it can either be an optional `BinaryTreeNode<K, V>` type or `null`.
1894
1983
  */
1895
1984
  _setRoot(v) {
1896
1985
  if (v) {
@@ -1904,30 +1993,24 @@ class BinaryTree extends base_1.IterableEntryBase {
1904
1993
  *
1905
1994
  * The function `_ensurePredicate` in TypeScript ensures that the input is converted into a valid
1906
1995
  * predicate function for a binary tree node.
1907
- * @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The
1996
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate - The
1908
1997
  * `_ensurePredicate` method in the provided code snippet is responsible for ensuring that the input
1909
- * parameter `keyNodeEntryRawOrPredicate` is transformed into a valid predicate function that can be
1998
+ * parameter `keyNodeEntryOrPredicate` is transformed into a valid predicate function that can be
1910
1999
  * used for filtering nodes in a binary tree.
1911
- * @returns A NodePredicate<NODE> function is being returned.
2000
+ * @returns A NodePredicate<BinaryTreeNode<K, V>> function is being returned.
1912
2001
  */
1913
- _ensurePredicate(keyNodeEntryRawOrPredicate) {
1914
- if (keyNodeEntryRawOrPredicate === null || keyNodeEntryRawOrPredicate === undefined)
2002
+ _ensurePredicate(keyNodeEntryOrPredicate) {
2003
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === undefined)
1915
2004
  return (node) => (node ? false : false);
1916
- if (this._isPredicate(keyNodeEntryRawOrPredicate))
1917
- return keyNodeEntryRawOrPredicate;
1918
- if (this.isRealNode(keyNodeEntryRawOrPredicate))
1919
- return (node) => node === keyNodeEntryRawOrPredicate;
1920
- if (this.isEntry(keyNodeEntryRawOrPredicate)) {
1921
- const [key] = keyNodeEntryRawOrPredicate;
2005
+ if (this._isPredicate(keyNodeEntryOrPredicate))
2006
+ return keyNodeEntryOrPredicate;
2007
+ if (this.isRealNode(keyNodeEntryOrPredicate))
2008
+ return (node) => node === keyNodeEntryOrPredicate;
2009
+ if (this.isEntry(keyNodeEntryOrPredicate)) {
2010
+ const [key] = keyNodeEntryOrPredicate;
1922
2011
  return (node) => node.key === key;
1923
2012
  }
1924
- if (this.isKey(keyNodeEntryRawOrPredicate))
1925
- return (node) => node.key === keyNodeEntryRawOrPredicate;
1926
- if (this._toEntryFn) {
1927
- const [key] = this._toEntryFn(keyNodeEntryRawOrPredicate);
1928
- return (node) => node.key === key;
1929
- }
1930
- return (node) => node.key === keyNodeEntryRawOrPredicate;
2013
+ return (node) => node.key === keyNodeEntryOrPredicate;
1931
2014
  }
1932
2015
  /**
1933
2016
  * Time Complexity: O(1)
@@ -1936,7 +2019,7 @@ class BinaryTree extends base_1.IterableEntryBase {
1936
2019
  * The function `_isPredicate` checks if a given parameter is a function.
1937
2020
  * @param {any} p - The parameter `p` is a variable of type `any`, which means it can hold any type
1938
2021
  * of value. In this context, the function `_isPredicate` is checking if `p` is a function that
1939
- * satisfies the type `NodePredicate<NODE>`.
2022
+ * satisfies the type `NodePredicate<BinaryTreeNode<K, V>>`.
1940
2023
  * @returns The function is checking if the input `p` is a function and returning a boolean value
1941
2024
  * based on that check. If `p` is a function, it will return `true`, indicating that `p` is a
1942
2025
  * predicate function for a binary tree node. If `p` is not a function, it will return `false`.
@@ -1948,34 +2031,27 @@ class BinaryTree extends base_1.IterableEntryBase {
1948
2031
  * Time Complexity: O(1)
1949
2032
  * Space Complexity: O(1)
1950
2033
  *
1951
- * The function `_getKey` in TypeScript returns the key from a given input, which can be a node,
2034
+ * The function `_extractKey` in TypeScript returns the key from a given input, which can be a node,
1952
2035
  * entry, raw data, or null/undefined.
1953
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `_getKey` method you provided is a
1954
- * TypeScript method that takes in a parameter `keyNodeEntryOrRaw` of type `BTNRep<K, V, NODE> | R`,
1955
- * where `BTNRep` is a generic type with keys `K`, `V`, and `NODE`, and `
1956
- * @returns The `_getKey` method returns the key value extracted from the `keyNodeEntryOrRaw`
2036
+ * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The `_extractKey` method you provided is a
2037
+ * TypeScript method that takes in a parameter `keyNodeOrEntry` of type `BTNRep<K, V, BinaryTreeNode<K, V>>`,
2038
+ * where `BTNRep` is a generic type with keys `K`, `V`, and `BinaryTreeNode<K, V>`, and `
2039
+ * @returns The `_extractKey` method returns the key value extracted from the `keyNodeOrEntry`
1957
2040
  * parameter. The return value can be a key value of type `K`, `null`, or `undefined`, depending on
1958
2041
  * the conditions checked in the method.
1959
2042
  */
1960
- _getKey(keyNodeEntryOrRaw) {
1961
- if (keyNodeEntryOrRaw === null)
2043
+ _extractKey(keyNodeOrEntry) {
2044
+ if (keyNodeOrEntry === null)
1962
2045
  return null;
1963
- if (keyNodeEntryOrRaw === undefined)
1964
- return;
1965
- if (keyNodeEntryOrRaw === this._NIL)
2046
+ if (keyNodeOrEntry === undefined)
1966
2047
  return;
1967
- if (this.isNode(keyNodeEntryOrRaw))
1968
- return keyNodeEntryOrRaw.key;
1969
- if (this.isEntry(keyNodeEntryOrRaw))
1970
- return keyNodeEntryOrRaw[0];
1971
- if (this.isRaw(keyNodeEntryOrRaw)) {
1972
- if (this._toEntryFn) {
1973
- const [key] = this._toEntryFn(keyNodeEntryOrRaw);
1974
- return key;
1975
- }
2048
+ if (keyNodeOrEntry === this._NIL)
1976
2049
  return;
1977
- }
1978
- return keyNodeEntryOrRaw;
2050
+ if (this.isNode(keyNodeOrEntry))
2051
+ return keyNodeOrEntry.key;
2052
+ if (this.isEntry(keyNodeOrEntry))
2053
+ return keyNodeOrEntry[0];
2054
+ return keyNodeOrEntry;
1979
2055
  }
1980
2056
  /**
1981
2057
  * Time Complexity: O(1)
@@ -1999,6 +2075,9 @@ class BinaryTree extends base_1.IterableEntryBase {
1999
2075
  return this._store.set(key, value);
2000
2076
  }
2001
2077
  /**
2078
+ * Time Complexity: O(1)
2079
+ * Space Complexity: O(1)
2080
+ *
2002
2081
  * The _clearNodes function sets the root node to undefined and resets the size to 0.
2003
2082
  */
2004
2083
  _clearNodes() {
@@ -2006,6 +2085,9 @@ class BinaryTree extends base_1.IterableEntryBase {
2006
2085
  this._size = 0;
2007
2086
  }
2008
2087
  /**
2088
+ * Time Complexity: O(1)
2089
+ * Space Complexity: O(1)
2090
+ *
2009
2091
  * The _clearValues function clears all values stored in the _store object.
2010
2092
  */
2011
2093
  _clearValues() {