heap-typed 1.51.7 → 1.51.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.
- package/README.md +72 -80
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +103 -74
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +116 -93
- package/dist/data-structures/binary-tree/avl-tree.d.ts +82 -62
- package/dist/data-structures/binary-tree/avl-tree.js +90 -71
- package/dist/data-structures/binary-tree/binary-tree.d.ts +318 -233
- package/dist/data-structures/binary-tree/binary-tree.js +492 -392
- package/dist/data-structures/binary-tree/bst.d.ts +204 -251
- package/dist/data-structures/binary-tree/bst.js +256 -358
- package/dist/data-structures/binary-tree/rb-tree.d.ts +74 -85
- package/dist/data-structures/binary-tree/rb-tree.js +111 -119
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +92 -76
- package/dist/data-structures/binary-tree/tree-multi-map.js +105 -93
- package/dist/data-structures/graph/abstract-graph.d.ts +10 -15
- package/dist/data-structures/graph/abstract-graph.js +10 -15
- package/dist/data-structures/hash/hash-map.d.ts +31 -38
- package/dist/data-structures/hash/hash-map.js +40 -55
- package/dist/data-structures/heap/heap.d.ts +1 -3
- package/dist/data-structures/queue/deque.d.ts +2 -3
- package/dist/data-structures/queue/deque.js +2 -3
- package/dist/data-structures/trie/trie.d.ts +1 -1
- package/dist/data-structures/trie/trie.js +1 -1
- package/dist/interfaces/binary-tree.d.ts +7 -7
- package/dist/types/common.d.ts +2 -3
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +4 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +4 -3
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +6 -5
- package/dist/types/data-structures/binary-tree/bst.d.ts +6 -5
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +4 -3
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +4 -3
- package/dist/types/utils/utils.d.ts +10 -1
- package/dist/utils/utils.d.ts +2 -1
- package/dist/utils/utils.js +27 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +142 -100
- package/src/data-structures/binary-tree/avl-tree.ts +109 -80
- package/src/data-structures/binary-tree/binary-tree.ts +556 -433
- package/src/data-structures/binary-tree/bst.ts +286 -375
- package/src/data-structures/binary-tree/rb-tree.ts +132 -125
- package/src/data-structures/binary-tree/tree-multi-map.ts +129 -102
- package/src/data-structures/graph/abstract-graph.ts +10 -10
- package/src/data-structures/hash/hash-map.ts +42 -49
- package/src/data-structures/heap/heap.ts +1 -1
- package/src/data-structures/queue/deque.ts +2 -2
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/data-structures/trie/trie.ts +2 -2
- package/src/interfaces/binary-tree.ts +11 -9
- package/src/types/common.ts +2 -3
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +4 -3
- package/src/types/data-structures/binary-tree/avl-tree.ts +4 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +7 -6
- package/src/types/data-structures/binary-tree/bst.ts +6 -5
- package/src/types/data-structures/binary-tree/rb-tree.ts +4 -3
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +4 -3
- package/src/types/utils/utils.ts +14 -1
- package/src/utils/utils.ts +20 -1
|
@@ -95,37 +95,29 @@ exports.BinaryTreeNode = BinaryTreeNode;
|
|
|
95
95
|
*/
|
|
96
96
|
class BinaryTree extends base_1.IterableEntryBase {
|
|
97
97
|
/**
|
|
98
|
-
* The constructor function initializes a binary tree object with optional
|
|
99
|
-
* @param [
|
|
98
|
+
* The constructor function initializes a binary tree object with optional keysOrNodesOrEntriesOrRawElements and options.
|
|
99
|
+
* @param [keysOrNodesOrEntriesOrRawElements] - An optional iterable of KeyOrNodeOrEntry objects. These objects represent the
|
|
100
100
|
* nodes to be added to the binary tree.
|
|
101
101
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
102
102
|
* configuration options for the binary tree. In this case, it is of type
|
|
103
103
|
* `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
|
|
104
104
|
* required.
|
|
105
105
|
*/
|
|
106
|
-
constructor(
|
|
106
|
+
constructor(keysOrNodesOrEntriesOrRawElements = [], options) {
|
|
107
107
|
super();
|
|
108
108
|
this.iterationType = 'ITERATIVE';
|
|
109
|
-
this.
|
|
109
|
+
this._size = 0;
|
|
110
110
|
this._NIL = new BinaryTreeNode(NaN);
|
|
111
111
|
this._DEFAULT_CALLBACK = (node) => (node ? node.key : undefined);
|
|
112
112
|
if (options) {
|
|
113
|
-
const { iterationType,
|
|
113
|
+
const { iterationType, toEntryFn } = options;
|
|
114
114
|
if (iterationType)
|
|
115
115
|
this.iterationType = iterationType;
|
|
116
|
-
if (
|
|
117
|
-
this.
|
|
116
|
+
if (typeof toEntryFn === 'function')
|
|
117
|
+
this._toEntryFn = toEntryFn;
|
|
118
118
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
this.addMany(keysOrNodesOrEntries);
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* The function returns the value of the `_extractor` property.
|
|
125
|
-
* @returns The `_extractor` property is being returned.
|
|
126
|
-
*/
|
|
127
|
-
get extractor() {
|
|
128
|
-
return this._extractor;
|
|
119
|
+
if (keysOrNodesOrEntriesOrRawElements)
|
|
120
|
+
this.addMany(keysOrNodesOrEntriesOrRawElements);
|
|
129
121
|
}
|
|
130
122
|
/**
|
|
131
123
|
* The function returns the root node, which can be of type NODE, null, or undefined.
|
|
@@ -149,6 +141,13 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
149
141
|
get NIL() {
|
|
150
142
|
return this._NIL;
|
|
151
143
|
}
|
|
144
|
+
/**
|
|
145
|
+
* The function returns the value of the _toEntryFn property.
|
|
146
|
+
* @returns The function being returned is `this._toEntryFn`.
|
|
147
|
+
*/
|
|
148
|
+
get toEntryFn() {
|
|
149
|
+
return this._toEntryFn;
|
|
150
|
+
}
|
|
152
151
|
/**
|
|
153
152
|
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
154
153
|
* @param {K} key - The key for the new node.
|
|
@@ -169,42 +168,42 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
169
168
|
return new BinaryTree([], Object.assign({ iterationType: this.iterationType }, options));
|
|
170
169
|
}
|
|
171
170
|
/**
|
|
172
|
-
* The function `
|
|
173
|
-
*
|
|
171
|
+
* The function `keyValueOrEntryOrRawElementToNode` converts a key-value pair, entry, or raw element
|
|
172
|
+
* into a node object.
|
|
173
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
174
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
174
175
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
175
|
-
* `
|
|
176
|
-
*
|
|
177
|
-
* @returns
|
|
176
|
+
* `keyValueOrEntryOrRawElementToNode` function. It represents the value associated with a key in a
|
|
177
|
+
* key-value pair. If provided, it will be used to create a node with the specified key and value.
|
|
178
|
+
* @returns The function `keyValueOrEntryOrRawElementToNode` returns either a `NODE` object, `null`,
|
|
179
|
+
* or `undefined`.
|
|
178
180
|
*/
|
|
179
|
-
|
|
180
|
-
if (
|
|
181
|
+
keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value) {
|
|
182
|
+
if (keyOrNodeOrEntryOrRawElement === undefined)
|
|
181
183
|
return;
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
const [key,
|
|
188
|
-
if (key
|
|
184
|
+
if (keyOrNodeOrEntryOrRawElement === null)
|
|
185
|
+
return null;
|
|
186
|
+
if (this.isNode(keyOrNodeOrEntryOrRawElement))
|
|
187
|
+
return keyOrNodeOrEntryOrRawElement;
|
|
188
|
+
if (this.toEntryFn) {
|
|
189
|
+
const [key, entryValue] = this.toEntryFn(keyOrNodeOrEntryOrRawElement);
|
|
190
|
+
if (key)
|
|
191
|
+
return this.createNode(key, entryValue !== null && entryValue !== void 0 ? entryValue : value);
|
|
192
|
+
else
|
|
189
193
|
return;
|
|
190
|
-
}
|
|
191
|
-
else if (key === null) {
|
|
192
|
-
node = null;
|
|
193
|
-
}
|
|
194
|
-
else {
|
|
195
|
-
node = this.createNode(key, value);
|
|
196
|
-
}
|
|
197
194
|
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
195
|
+
if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
|
|
196
|
+
const [key, value] = keyOrNodeOrEntryOrRawElement;
|
|
197
|
+
if (key === undefined)
|
|
198
|
+
return;
|
|
199
|
+
else if (key === null)
|
|
200
|
+
return null;
|
|
201
|
+
else
|
|
202
|
+
return this.createNode(key, value);
|
|
206
203
|
}
|
|
207
|
-
|
|
204
|
+
if (this.isKey(keyOrNodeOrEntryOrRawElement))
|
|
205
|
+
return this.createNode(keyOrNodeOrEntryOrRawElement, value);
|
|
206
|
+
return;
|
|
208
207
|
}
|
|
209
208
|
/**
|
|
210
209
|
* Time Complexity: O(n)
|
|
@@ -214,56 +213,56 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
214
213
|
* Time Complexity: O(n)
|
|
215
214
|
* Space Complexity: O(log n)
|
|
216
215
|
*
|
|
217
|
-
* The
|
|
218
|
-
*
|
|
219
|
-
* @param {
|
|
220
|
-
* `
|
|
221
|
-
*
|
|
222
|
-
*
|
|
223
|
-
*
|
|
224
|
-
*
|
|
225
|
-
*
|
|
226
|
-
*/
|
|
227
|
-
ensureNode(
|
|
228
|
-
if (
|
|
216
|
+
* The `ensureNode` function checks if the input is a valid node and returns it, or converts it to a
|
|
217
|
+
* node if it is a key or entry.
|
|
218
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
219
|
+
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, `KeyOrNodeOrEntry<K, V, NODE>`, or
|
|
220
|
+
* a raw element.
|
|
221
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
222
|
+
* parameter that specifies the type of iteration to be used when searching for a node. It has a
|
|
223
|
+
* default value of `'ITERATIVE'`.
|
|
224
|
+
* @returns The function `ensureNode` returns either a `NODE` object, `null`, or `undefined`.
|
|
225
|
+
*/
|
|
226
|
+
ensureNode(keyOrNodeOrEntryOrRawElement, iterationType = 'ITERATIVE') {
|
|
227
|
+
if (keyOrNodeOrEntryOrRawElement === null)
|
|
228
|
+
return null;
|
|
229
|
+
if (keyOrNodeOrEntryOrRawElement === undefined)
|
|
229
230
|
return;
|
|
230
|
-
if (this.
|
|
231
|
-
return
|
|
231
|
+
if (keyOrNodeOrEntryOrRawElement === this.NIL)
|
|
232
|
+
return;
|
|
233
|
+
if (this.isNode(keyOrNodeOrEntryOrRawElement))
|
|
234
|
+
return keyOrNodeOrEntryOrRawElement;
|
|
235
|
+
if (this.toEntryFn) {
|
|
236
|
+
const [key] = this.toEntryFn(keyOrNodeOrEntryOrRawElement);
|
|
237
|
+
if (key)
|
|
238
|
+
return this.getNodeByKey(key);
|
|
232
239
|
}
|
|
233
|
-
if (this.isEntry(
|
|
234
|
-
const key =
|
|
240
|
+
if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
|
|
241
|
+
const key = keyOrNodeOrEntryOrRawElement[0];
|
|
235
242
|
if (key === null)
|
|
236
243
|
return null;
|
|
237
244
|
if (key === undefined)
|
|
238
245
|
return;
|
|
239
246
|
return this.getNodeByKey(key, iterationType);
|
|
240
247
|
}
|
|
241
|
-
if (
|
|
242
|
-
return
|
|
243
|
-
|
|
244
|
-
return;
|
|
245
|
-
return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
246
|
-
}
|
|
247
|
-
/**
|
|
248
|
-
* The function checks if a given node is a real node or null.
|
|
249
|
-
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
250
|
-
* @returns a boolean value.
|
|
251
|
-
*/
|
|
252
|
-
isNodeOrNull(node) {
|
|
253
|
-
return this.isRealNode(node) || node === null;
|
|
248
|
+
if (this.isKey(keyOrNodeOrEntryOrRawElement))
|
|
249
|
+
return this.getNodeByKey(keyOrNodeOrEntryOrRawElement, iterationType);
|
|
250
|
+
return;
|
|
254
251
|
}
|
|
255
252
|
/**
|
|
256
|
-
* The function
|
|
257
|
-
* @param
|
|
258
|
-
*
|
|
253
|
+
* The function checks if the input is an instance of the BinaryTreeNode class.
|
|
254
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
255
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
256
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
257
|
+
* an instance of the `BinaryTreeNode` class.
|
|
259
258
|
*/
|
|
260
|
-
isNode(
|
|
261
|
-
return
|
|
259
|
+
isNode(keyOrNodeOrEntryOrRawElement) {
|
|
260
|
+
return keyOrNodeOrEntryOrRawElement instanceof BinaryTreeNode;
|
|
262
261
|
}
|
|
263
262
|
/**
|
|
264
|
-
* The function checks if a given node is a
|
|
265
|
-
*
|
|
266
|
-
*
|
|
263
|
+
* The function checks if a given node is a valid node in a binary search tree.
|
|
264
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
|
|
265
|
+
* `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
267
266
|
* @returns a boolean value.
|
|
268
267
|
*/
|
|
269
268
|
isRealNode(node) {
|
|
@@ -272,21 +271,64 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
272
271
|
return this.isNode(node);
|
|
273
272
|
}
|
|
274
273
|
/**
|
|
275
|
-
* The function checks if a given node is a
|
|
276
|
-
* @param {
|
|
274
|
+
* The function checks if a given node is a real node or null.
|
|
275
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
|
|
276
|
+
* `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
277
|
+
* @returns a boolean value.
|
|
278
|
+
*/
|
|
279
|
+
isNodeOrNull(node) {
|
|
280
|
+
return this.isRealNode(node) || node === null;
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* The function checks if a given node is equal to the NIL value.
|
|
284
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
|
|
285
|
+
* `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
277
286
|
* @returns a boolean value.
|
|
278
287
|
*/
|
|
279
288
|
isNIL(node) {
|
|
280
289
|
return node === this.NIL;
|
|
281
290
|
}
|
|
282
291
|
/**
|
|
283
|
-
* The function checks if
|
|
284
|
-
*
|
|
285
|
-
*
|
|
292
|
+
* The function checks if the input is an array with two elements, indicating it is a binary tree
|
|
293
|
+
* node entry.
|
|
294
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
295
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
286
296
|
* @returns a boolean value.
|
|
287
297
|
*/
|
|
288
|
-
isEntry(
|
|
289
|
-
return Array.isArray(
|
|
298
|
+
isEntry(keyOrNodeOrEntryOrRawElement) {
|
|
299
|
+
return Array.isArray(keyOrNodeOrEntryOrRawElement) && keyOrNodeOrEntryOrRawElement.length === 2;
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* The function checks if a given value is a valid key by evaluating its type and value.
|
|
303
|
+
* @param {any} key - The `key` parameter can be of any type. It is the value that we want to check
|
|
304
|
+
* if it is a valid key.
|
|
305
|
+
* @param [isCheckValueOf=true] - The `isCheckValueOf` parameter is a boolean flag that determines
|
|
306
|
+
* whether the function should check the valueOf() method of an object when the key is of type
|
|
307
|
+
* 'object'. If `isCheckValueOf` is true, the function will recursively call itself with the value
|
|
308
|
+
* returned by key.valueOf().
|
|
309
|
+
* @returns a boolean value.
|
|
310
|
+
*/
|
|
311
|
+
isKey(key, isCheckValueOf = true) {
|
|
312
|
+
if (key === null)
|
|
313
|
+
return true;
|
|
314
|
+
const keyType = typeof key;
|
|
315
|
+
if (keyType === 'string' || keyType === 'bigint' || keyType === 'boolean')
|
|
316
|
+
return true;
|
|
317
|
+
if (keyType === 'number')
|
|
318
|
+
return !isNaN(key);
|
|
319
|
+
if (keyType === 'symbol' || keyType === 'undefined')
|
|
320
|
+
return false;
|
|
321
|
+
if (keyType === 'function')
|
|
322
|
+
return this.isKey(key());
|
|
323
|
+
if (keyType === 'object') {
|
|
324
|
+
if (typeof key.toString === 'function')
|
|
325
|
+
return true;
|
|
326
|
+
if (isCheckValueOf && typeof key.valueOf === 'function') {
|
|
327
|
+
this.isKey(key.valueOf(), false);
|
|
328
|
+
}
|
|
329
|
+
return false;
|
|
330
|
+
}
|
|
331
|
+
return false;
|
|
290
332
|
}
|
|
291
333
|
/**
|
|
292
334
|
* Time Complexity O(n)
|
|
@@ -296,14 +338,20 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
296
338
|
* Time Complexity O(n)
|
|
297
339
|
* Space Complexity O(1)
|
|
298
340
|
*
|
|
299
|
-
* The `add` function
|
|
300
|
-
*
|
|
301
|
-
* @param
|
|
302
|
-
*
|
|
303
|
-
*
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
341
|
+
* The `add` function is used to insert a new node into a binary tree, checking for duplicate keys
|
|
342
|
+
* and finding the appropriate insertion position.
|
|
343
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
|
|
344
|
+
* `keyOrNodeOrEntryOrRawElement` parameter can accept a value of type `R`, which represents the key,
|
|
345
|
+
* node, entry, or raw element to be added to the tree. It can also accept a value of type
|
|
346
|
+
* `KeyOrNodeOrEntry<K, V, NODE>
|
|
347
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
348
|
+
* key being added to the tree. It represents the value that will be stored in the tree for the given
|
|
349
|
+
* key.
|
|
350
|
+
* @returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
|
|
351
|
+
* insertion position cannot be found or if there are duplicate keys.
|
|
352
|
+
*/
|
|
353
|
+
add(keyOrNodeOrEntryOrRawElement, value) {
|
|
354
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
|
|
307
355
|
if (newNode === undefined)
|
|
308
356
|
return false;
|
|
309
357
|
// If the tree is empty, directly set the new node as the root node
|
|
@@ -357,20 +405,24 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
357
405
|
* Time Complexity: O(k * n)
|
|
358
406
|
* Space Complexity: O(1)
|
|
359
407
|
*
|
|
360
|
-
* The `addMany` function takes in
|
|
361
|
-
* adds each node with its corresponding value to
|
|
362
|
-
*
|
|
363
|
-
* @param
|
|
364
|
-
*
|
|
365
|
-
|
|
366
|
-
|
|
408
|
+
* The `addMany` function takes in an iterable of keys or nodes or entries or raw elements, and an
|
|
409
|
+
* optional iterable of values, and adds each key or node or entry with its corresponding value to a
|
|
410
|
+
* data structure, returning an array of booleans indicating whether each insertion was successful.
|
|
411
|
+
* @param keysOrNodesOrEntriesOrRawElements - An iterable containing keys, nodes, entries, or raw
|
|
412
|
+
* elements. These elements will be added to the data structure.
|
|
413
|
+
* @param [values] - An optional iterable of values that correspond to the keys or nodes or entries
|
|
414
|
+
* in the `keysOrNodesOrEntriesOrRawElements` parameter.
|
|
415
|
+
* @returns The function `addMany` returns an array of booleans indicating whether each element was
|
|
416
|
+
* successfully added to the data structure.
|
|
417
|
+
*/
|
|
418
|
+
addMany(keysOrNodesOrEntriesOrRawElements, values) {
|
|
367
419
|
// TODO not sure addMany not be run multi times
|
|
368
420
|
const inserted = [];
|
|
369
421
|
let valuesIterator;
|
|
370
422
|
if (values) {
|
|
371
423
|
valuesIterator = values[Symbol.iterator]();
|
|
372
424
|
}
|
|
373
|
-
for (const
|
|
425
|
+
for (const keyOrNodeOrEntryOrRawElement of keysOrNodesOrEntriesOrRawElements) {
|
|
374
426
|
let value = undefined;
|
|
375
427
|
if (valuesIterator) {
|
|
376
428
|
const valueResult = valuesIterator.next();
|
|
@@ -378,7 +430,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
378
430
|
value = valueResult.value;
|
|
379
431
|
}
|
|
380
432
|
}
|
|
381
|
-
inserted.push(this.add(
|
|
433
|
+
inserted.push(this.add(keyOrNodeOrEntryOrRawElement, value));
|
|
382
434
|
}
|
|
383
435
|
return inserted;
|
|
384
436
|
}
|
|
@@ -391,36 +443,34 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
391
443
|
* Time Complexity: O(k * n)
|
|
392
444
|
* Space Complexity: O(1)
|
|
393
445
|
*
|
|
394
|
-
* The `refill` function clears the current data and adds new
|
|
395
|
-
* @param
|
|
396
|
-
* KeyOrNodeOrEntry<K, V, NODE
|
|
397
|
-
* @param [values] - The `values` parameter is an optional iterable
|
|
398
|
-
*
|
|
399
|
-
*
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
refill(keysOrNodesOrEntries, values) {
|
|
446
|
+
* The `refill` function clears the current data and adds new data to the collection.
|
|
447
|
+
* @param keysOrNodesOrEntriesOrRawElements - An iterable collection of keys, nodes, entries, or raw
|
|
448
|
+
* elements. These can be of any type (R) or a specific type (KeyOrNodeOrEntry<K, V, NODE>).
|
|
449
|
+
* @param [values] - The `values` parameter is an optional iterable of values that will be associated
|
|
450
|
+
* with the keys or nodes being added. If provided, the values will be assigned to the corresponding
|
|
451
|
+
* keys or nodes. If not provided, the values will be set to `undefined`.
|
|
452
|
+
*/
|
|
453
|
+
refill(keysOrNodesOrEntriesOrRawElements, values) {
|
|
403
454
|
this.clear();
|
|
404
|
-
this.addMany(
|
|
455
|
+
this.addMany(keysOrNodesOrEntriesOrRawElements, values);
|
|
405
456
|
}
|
|
406
457
|
/**
|
|
407
458
|
* Time Complexity: O(n)
|
|
408
459
|
* Space Complexity: O(1)
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
/**
|
|
460
|
+
*/
|
|
461
|
+
/**
|
|
412
462
|
* Time Complexity: O(n)
|
|
413
463
|
* Space Complexity: O(1)
|
|
414
464
|
*
|
|
415
|
-
* The function
|
|
416
|
-
*
|
|
417
|
-
* @param {ReturnType<C> | null | undefined} identifier - The identifier parameter is the value
|
|
418
|
-
*
|
|
419
|
-
* the callback function
|
|
420
|
-
* specific node based on its value or object.
|
|
465
|
+
* The above function is a TypeScript implementation of deleting a node from a binary tree, returning
|
|
466
|
+
* the deleted node and the node that needs to be balanced.
|
|
467
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
468
|
+
* used to identify the node that needs to be deleted from the binary tree. It can be of any type
|
|
469
|
+
* that is returned by the callback function.
|
|
421
470
|
* @param {C} callback - The `callback` parameter is a function that is used to determine the
|
|
422
|
-
* identifier of the node to be deleted. It is
|
|
423
|
-
*
|
|
471
|
+
* identifier of the node to be deleted. It is of type `C`, which extends the `BTNCallback<NODE>`
|
|
472
|
+
* interface. The `BTNCallback<NODE>` interface represents a callback function that takes a node of
|
|
473
|
+
* type `NODE
|
|
424
474
|
* @returns an array of `BinaryTreeDeleteResult<NODE>`.
|
|
425
475
|
*/
|
|
426
476
|
delete(identifier, callback = this._DEFAULT_CALLBACK) {
|
|
@@ -475,28 +525,27 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
475
525
|
*/
|
|
476
526
|
/**
|
|
477
527
|
* Time Complexity: O(n)
|
|
478
|
-
* Space Complexity: O(k + log n)
|
|
528
|
+
* Space Complexity: O(k + log n)
|
|
479
529
|
*
|
|
480
|
-
* The function `getNodes`
|
|
481
|
-
*
|
|
530
|
+
* The function `getNodes` returns an array of nodes that match a given identifier, using either a
|
|
531
|
+
* recursive or iterative approach.
|
|
482
532
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
483
|
-
* that
|
|
484
|
-
* callback function
|
|
485
|
-
*
|
|
486
|
-
*
|
|
487
|
-
*
|
|
488
|
-
*
|
|
489
|
-
*
|
|
490
|
-
*
|
|
491
|
-
*
|
|
492
|
-
*
|
|
493
|
-
*
|
|
494
|
-
*
|
|
495
|
-
*
|
|
496
|
-
*
|
|
497
|
-
*
|
|
498
|
-
*
|
|
499
|
-
* @returns an array of nodes of type `NODE`.
|
|
533
|
+
* that is used to identify the nodes. It can be of any type and is used to match against the result
|
|
534
|
+
* of the callback function for each node.
|
|
535
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node as input and
|
|
536
|
+
* returns a value. This value is used to identify the nodes that match the given identifier. The
|
|
537
|
+
* `callback` function is optional and defaults to a default callback function
|
|
538
|
+
* (`this._DEFAULT_CALLBACK`) if not provided.
|
|
539
|
+
* @param [onlyOne=false] - A boolean value indicating whether to return only one node that matches
|
|
540
|
+
* the identifier or all nodes that match the identifier. If set to true, only the first matching
|
|
541
|
+
* node will be returned. If set to false, all matching nodes will be returned. The default value is
|
|
542
|
+
* false.
|
|
543
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
544
|
+
* point for the search. It can be either a node object, a key-value pair, or a key. If it is not
|
|
545
|
+
* provided, the `root` of the data structure is used as the starting point.
|
|
546
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
547
|
+
* iteration to be performed on the nodes of a binary tree. It can have two possible values:
|
|
548
|
+
* @returns an array of NODE objects.
|
|
500
549
|
*/
|
|
501
550
|
getNodes(identifier, callback = this._DEFAULT_CALLBACK, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
502
551
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -541,24 +590,21 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
541
590
|
*/
|
|
542
591
|
/**
|
|
543
592
|
* Time Complexity: O(n)
|
|
544
|
-
* Space Complexity: O(log n)
|
|
593
|
+
* Space Complexity: O(log n).
|
|
545
594
|
*
|
|
546
|
-
* The function `getNode` returns the first node that matches the given identifier and callback
|
|
547
|
-
*
|
|
595
|
+
* The function `getNode` returns the first node that matches the given identifier and callback,
|
|
596
|
+
* starting from the specified root node and using the specified iteration type.
|
|
548
597
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
549
|
-
* used to identify the node you want to retrieve. It can be of any type that is
|
|
550
|
-
* callback function
|
|
551
|
-
*
|
|
552
|
-
*
|
|
553
|
-
*
|
|
554
|
-
*
|
|
555
|
-
*
|
|
556
|
-
*
|
|
557
|
-
*
|
|
558
|
-
* @
|
|
559
|
-
* be performed when searching for nodes in the binary tree. It determines the order in which the
|
|
560
|
-
* nodes are visited during the search.
|
|
561
|
-
* @returns a value of type `NODE | null | undefined`.
|
|
598
|
+
* used to identify the node you want to retrieve. It can be of any type that is the return type of
|
|
599
|
+
* the `C` callback function, or it can be `null` or `undefined`.
|
|
600
|
+
* @param {C} callback - The `callback` parameter is a function that will be used to determine if a
|
|
601
|
+
* node matches the desired criteria. It should return a value that can be used to identify the node.
|
|
602
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
603
|
+
* point for searching nodes in a tree structure. It can be either a root node, a key-value pair, or
|
|
604
|
+
* a node entry. If not provided, the search will start from the root of the tree.
|
|
605
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
606
|
+
* of iteration to be performed when searching for nodes. It can have one of the following values:
|
|
607
|
+
* @returns The method is returning a NODE object, or null, or undefined.
|
|
562
608
|
*/
|
|
563
609
|
getNode(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
564
610
|
var _a;
|
|
@@ -572,15 +618,13 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
572
618
|
* Time Complexity: O(n)
|
|
573
619
|
* Space Complexity: O(log n)
|
|
574
620
|
*
|
|
575
|
-
* The function `getNodeByKey`
|
|
576
|
-
*
|
|
577
|
-
*
|
|
578
|
-
*
|
|
579
|
-
*
|
|
580
|
-
*
|
|
581
|
-
*
|
|
582
|
-
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
583
|
-
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
621
|
+
* The function `getNodeByKey` returns a node with a specific key value from a tree structure.
|
|
622
|
+
* @param {K} key - The key parameter is the value that you want to search for in the tree. It is
|
|
623
|
+
* used to find the node with the matching key value.
|
|
624
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
625
|
+
* parameter that specifies the type of iteration to be used when searching for a node in the tree.
|
|
626
|
+
* It has a default value of `'ITERATIVE'`.
|
|
627
|
+
* @returns a value of type NODE, null, or undefined.
|
|
584
628
|
*/
|
|
585
629
|
getNodeByKey(key, iterationType = 'ITERATIVE') {
|
|
586
630
|
return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
|
|
@@ -593,23 +637,22 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
593
637
|
* Time Complexity: O(n)
|
|
594
638
|
* Space Complexity: O(log n)
|
|
595
639
|
*
|
|
596
|
-
* The function `get`
|
|
597
|
-
*
|
|
640
|
+
* The function `get` in TypeScript overrides the base class method and returns the value associated
|
|
641
|
+
* with the given identifier.
|
|
598
642
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
599
|
-
* used to identify the node in the binary tree. It can be of any type that is
|
|
643
|
+
* used to identify the node in the binary tree. It can be of any type that is returned by the
|
|
600
644
|
* callback function `C`. It can also be `null` or `undefined` if no identifier is provided.
|
|
601
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
602
|
-
* the
|
|
603
|
-
*
|
|
604
|
-
*
|
|
605
|
-
*
|
|
606
|
-
*
|
|
607
|
-
*
|
|
608
|
-
*
|
|
609
|
-
*
|
|
610
|
-
*
|
|
611
|
-
*
|
|
612
|
-
* found, `undefined` is returned.
|
|
645
|
+
* @param {C} callback - The `callback` parameter is a function that will be used to determine if a
|
|
646
|
+
* node matches the given identifier. It is optional and defaults to `this._DEFAULT_CALLBACK`.
|
|
647
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
648
|
+
* point for the search in the binary tree. It can be either a root node of the tree or a key, node,
|
|
649
|
+
* or entry object that exists in the tree. If no specific starting point is provided, the search
|
|
650
|
+
* will begin from the root of the
|
|
651
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
652
|
+
* of iteration to be performed when searching for a node in the tree. It can have one of the
|
|
653
|
+
* following values:
|
|
654
|
+
* @returns The method is returning the value associated with the specified identifier in the binary
|
|
655
|
+
* tree.
|
|
613
656
|
*/
|
|
614
657
|
get(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
615
658
|
var _a;
|
|
@@ -617,28 +660,27 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
617
660
|
}
|
|
618
661
|
/**
|
|
619
662
|
* Time Complexity: O(n)
|
|
620
|
-
* Space Complexity: O(log n)
|
|
663
|
+
* Space Complexity: O(log n)
|
|
621
664
|
*/
|
|
622
665
|
/**
|
|
623
666
|
* Time Complexity: O(n)
|
|
624
|
-
* Space Complexity: O(log n)
|
|
667
|
+
* Space Complexity: O(log n)
|
|
625
668
|
*
|
|
626
|
-
* The function checks if a
|
|
669
|
+
* The `has` function checks if a given identifier exists in the data structure and returns a boolean
|
|
670
|
+
* value.
|
|
627
671
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
628
|
-
*
|
|
629
|
-
* callback function `C`. It can also be `null` or `undefined` if
|
|
630
|
-
*
|
|
631
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
632
|
-
*
|
|
633
|
-
*
|
|
634
|
-
*
|
|
635
|
-
*
|
|
636
|
-
*
|
|
637
|
-
*
|
|
638
|
-
*
|
|
639
|
-
*
|
|
640
|
-
* be performed in a pre-order, in-order, or post-order manner.
|
|
641
|
-
* @returns a boolean value.
|
|
672
|
+
* used to identify a specific node or entry in the data structure. It can be of any type that is
|
|
673
|
+
* returned by the callback function `C`. It can also be `null` or `undefined` if no specific
|
|
674
|
+
* identifier is provided.
|
|
675
|
+
* @param {C} callback - The `callback` parameter is a function that will be used to determine
|
|
676
|
+
* whether a node should be included in the result or not. It is of type `C`, which extends the
|
|
677
|
+
* `BTNCallback<NODE>` type.
|
|
678
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
679
|
+
* point for the iteration in the data structure. It can be either a root node, a key-value pair, or
|
|
680
|
+
* a node entry. If not specified, it defaults to the root of the data structure.
|
|
681
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
682
|
+
* of iteration to be performed. It is an optional parameter with a default value of `IterationType`.
|
|
683
|
+
* @returns The method is returning a boolean value.
|
|
642
684
|
*/
|
|
643
685
|
has(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
644
686
|
callback = this._ensureCallback(identifier, callback);
|
|
@@ -682,9 +724,10 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
682
724
|
*
|
|
683
725
|
* The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
|
|
684
726
|
* height of the tree.
|
|
685
|
-
* @param {
|
|
686
|
-
*
|
|
687
|
-
*
|
|
727
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The parameter `beginRoot` is optional and
|
|
728
|
+
* has a default value of `this.root`. It represents the starting point for checking if the tree is
|
|
729
|
+
* perfectly balanced. It can be either a root node (`R`), a key or node or entry
|
|
730
|
+
* (`KeyOrNodeOrEntry<K, V, NODE
|
|
688
731
|
* @returns a boolean value.
|
|
689
732
|
*/
|
|
690
733
|
isPerfectlyBalanced(beginRoot = this.root) {
|
|
@@ -698,12 +741,14 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
698
741
|
* Time Complexity: O(n)
|
|
699
742
|
* Space Complexity: O(1)
|
|
700
743
|
*
|
|
701
|
-
* The function `
|
|
702
|
-
* @param {
|
|
703
|
-
*
|
|
704
|
-
*
|
|
705
|
-
*
|
|
706
|
-
*
|
|
744
|
+
* The function `isBST` checks if a binary search tree is valid, either recursively or iteratively.
|
|
745
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
746
|
+
* starting point for checking if a binary search tree (BST) is valid. It can be either a root node
|
|
747
|
+
* of the BST, a key value of a node in the BST, or an entry object containing both the key and value
|
|
748
|
+
* of a node in the BST
|
|
749
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to determine the type
|
|
750
|
+
* of iteration to be performed while checking if the binary search tree (BST) is valid. It can have
|
|
751
|
+
* two possible values:
|
|
707
752
|
* @returns a boolean value.
|
|
708
753
|
*/
|
|
709
754
|
isBST(beginRoot = this.root, iterationType = this.iterationType) {
|
|
@@ -715,7 +760,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
715
760
|
const dfs = (cur, min, max) => {
|
|
716
761
|
if (!this.isRealNode(cur))
|
|
717
762
|
return true;
|
|
718
|
-
const numKey =
|
|
763
|
+
const numKey = Number(cur.key);
|
|
719
764
|
if (numKey <= min || numKey >= max)
|
|
720
765
|
return false;
|
|
721
766
|
return dfs(cur.left, min, numKey) && dfs(cur.right, numKey, max);
|
|
@@ -736,7 +781,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
736
781
|
curr = curr.left;
|
|
737
782
|
}
|
|
738
783
|
curr = stack.pop();
|
|
739
|
-
const numKey =
|
|
784
|
+
const numKey = Number(curr.key);
|
|
740
785
|
if (!this.isRealNode(curr) || (!checkMax && prev >= numKey) || (checkMax && prev <= numKey))
|
|
741
786
|
return false;
|
|
742
787
|
prev = numKey;
|
|
@@ -756,25 +801,26 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
756
801
|
* Time Complexity: O(n)
|
|
757
802
|
* Space Complexity: O(1)
|
|
758
803
|
*
|
|
759
|
-
* The function calculates the depth of a given node in a
|
|
760
|
-
* @param {
|
|
761
|
-
*
|
|
762
|
-
*
|
|
763
|
-
* @param {
|
|
764
|
-
* from which
|
|
765
|
-
*
|
|
766
|
-
*
|
|
804
|
+
* The function calculates the depth of a given node or key in a tree-like data structure.
|
|
805
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} dist - The `dist` parameter can be either a `R`
|
|
806
|
+
* (representing a root node), or a `KeyOrNodeOrEntry<K, V, NODE>` (representing a key, node, or
|
|
807
|
+
* entry).
|
|
808
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is optional and
|
|
809
|
+
* represents the starting point from which to calculate the depth. It can be either a reference to a
|
|
810
|
+
* node in the tree or a key-value pair or an entry object. If not provided, the default value is
|
|
811
|
+
* `this.root`, which refers to the root node
|
|
812
|
+
* @returns the depth of a node in a tree structure.
|
|
767
813
|
*/
|
|
768
814
|
getDepth(dist, beginRoot = this.root) {
|
|
769
|
-
|
|
770
|
-
|
|
815
|
+
let distEnsured = this.ensureNode(dist);
|
|
816
|
+
const beginRootEnsured = this.ensureNode(beginRoot);
|
|
771
817
|
let depth = 0;
|
|
772
|
-
while (
|
|
773
|
-
if (
|
|
818
|
+
while (distEnsured === null || distEnsured === void 0 ? void 0 : distEnsured.parent) {
|
|
819
|
+
if (distEnsured === beginRootEnsured) {
|
|
774
820
|
return depth;
|
|
775
821
|
}
|
|
776
822
|
depth++;
|
|
777
|
-
|
|
823
|
+
distEnsured = distEnsured.parent;
|
|
778
824
|
}
|
|
779
825
|
return depth;
|
|
780
826
|
}
|
|
@@ -784,17 +830,16 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
784
830
|
*/
|
|
785
831
|
/**
|
|
786
832
|
* Time Complexity: O(n)
|
|
787
|
-
* Space Complexity: O(
|
|
833
|
+
* Space Complexity: O(1)
|
|
788
834
|
*
|
|
789
|
-
* The
|
|
790
|
-
* iterative
|
|
791
|
-
* @param {
|
|
792
|
-
* starting
|
|
793
|
-
*
|
|
794
|
-
* @param iterationType - The `iterationType` parameter
|
|
795
|
-
*
|
|
796
|
-
*
|
|
797
|
-
* @returns the height of the binary tree.
|
|
835
|
+
* The `getHeight` function calculates the maximum height of a binary tree using either a recursive
|
|
836
|
+
* or iterative approach.
|
|
837
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
838
|
+
* starting point for calculating the height of a tree. It can be either a root node (`R`), a key or
|
|
839
|
+
* node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or it defaults to the root of the current tree.
|
|
840
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
841
|
+
* iteration used to calculate the height of the tree. It can have two possible values:
|
|
842
|
+
* @returns the maximum height of the binary tree.
|
|
798
843
|
*/
|
|
799
844
|
getHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
800
845
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -834,12 +879,15 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
834
879
|
*
|
|
835
880
|
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
836
881
|
* recursive or iterative approach.
|
|
837
|
-
* @param {
|
|
838
|
-
* starting
|
|
839
|
-
*
|
|
840
|
-
*
|
|
841
|
-
*
|
|
842
|
-
*
|
|
882
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
883
|
+
* starting point for calculating the minimum height of a tree. It can be either a root node (`R`), a
|
|
884
|
+
* key or node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or it defaults to the root of the current
|
|
885
|
+
* tree.
|
|
886
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
887
|
+
* iteration to be used when calculating the minimum height of the tree. It can have two possible
|
|
888
|
+
* values:
|
|
889
|
+
* @returns The function `getMinHeight` returns a number, which represents the minimum height of the
|
|
890
|
+
* binary tree.
|
|
843
891
|
*/
|
|
844
892
|
getMinHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
845
893
|
var _a, _b, _c;
|
|
@@ -889,35 +937,31 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
889
937
|
/**
|
|
890
938
|
* Time Complexity: O(log n)
|
|
891
939
|
* Space Complexity: O(log n)
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
/**
|
|
940
|
+
*/
|
|
941
|
+
/**
|
|
895
942
|
* Time Complexity: O(log n)
|
|
896
943
|
* Space Complexity: O(log n)
|
|
897
944
|
*
|
|
898
|
-
* The function `getPathToRoot` returns an array of nodes from a given node
|
|
899
|
-
*
|
|
900
|
-
* @param {
|
|
901
|
-
*
|
|
902
|
-
* `null`, or `undefined`.
|
|
945
|
+
* The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
|
|
946
|
+
* up to the root node, with an option to reverse the order of the nodes.
|
|
947
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginNode - The `beginNode` parameter can be either of
|
|
948
|
+
* type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
903
949
|
* @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
|
|
904
950
|
* resulting path should be reversed or not. If `isReverse` is set to `true`, the path will be
|
|
905
|
-
* reversed before returning it. If `isReverse` is set to `false
|
|
906
|
-
* @returns The function `getPathToRoot` returns an array of
|
|
951
|
+
* reversed before returning it. If `isReverse` is set to `false` or not provided, the path will
|
|
952
|
+
* @returns The function `getPathToRoot` returns an array of `NODE` objects.
|
|
907
953
|
*/
|
|
908
954
|
getPathToRoot(beginNode, isReverse = true) {
|
|
909
|
-
// TODO to support get path through passing key
|
|
910
955
|
const result = [];
|
|
911
|
-
|
|
912
|
-
if (!
|
|
956
|
+
let beginNodeEnsured = this.ensureNode(beginNode);
|
|
957
|
+
if (!beginNodeEnsured)
|
|
913
958
|
return result;
|
|
914
|
-
while (
|
|
959
|
+
while (beginNodeEnsured.parent) {
|
|
915
960
|
// Array.push + Array.reverse is more efficient than Array.unshift
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
beginNode = beginNode.parent;
|
|
961
|
+
result.push(beginNodeEnsured);
|
|
962
|
+
beginNodeEnsured = beginNodeEnsured.parent;
|
|
919
963
|
}
|
|
920
|
-
result.push(
|
|
964
|
+
result.push(beginNodeEnsured);
|
|
921
965
|
return isReverse ? result.reverse() : result;
|
|
922
966
|
}
|
|
923
967
|
/**
|
|
@@ -928,15 +972,14 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
928
972
|
* Time Complexity: O(log n)
|
|
929
973
|
* Space Complexity: O(1)
|
|
930
974
|
*
|
|
931
|
-
* The
|
|
932
|
-
*
|
|
933
|
-
* @param {
|
|
934
|
-
* for finding the leftmost node in a binary tree. It can be either a `
|
|
935
|
-
*
|
|
936
|
-
* @param iterationType - The `iterationType` parameter is used to
|
|
937
|
-
*
|
|
938
|
-
* @returns The function `getLeftMost` returns the leftmost node
|
|
939
|
-
* is no leftmost node, it returns `null` or `undefined` depending on the input.
|
|
975
|
+
* The `getLeftMost` function returns the leftmost node in a binary tree, either using recursive or
|
|
976
|
+
* iterative traversal.
|
|
977
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
978
|
+
* starting point for finding the leftmost node in a binary tree. It can be either a root node (`R`),
|
|
979
|
+
* a key or node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or `null` or `undefined`.
|
|
980
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
981
|
+
* of iteration to be performed. It can have two possible values:
|
|
982
|
+
* @returns The function `getLeftMost` returns the leftmost node in a binary tree.
|
|
940
983
|
*/
|
|
941
984
|
getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
942
985
|
if (this.isNIL(beginRoot))
|
|
@@ -970,16 +1013,15 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
970
1013
|
* Time Complexity: O(log n)
|
|
971
1014
|
* Space Complexity: O(1)
|
|
972
1015
|
*
|
|
973
|
-
* The
|
|
1016
|
+
* The `getRightMost` function returns the rightmost node in a binary tree, either recursively or
|
|
974
1017
|
* iteratively.
|
|
975
|
-
* @param {
|
|
976
|
-
* starting
|
|
977
|
-
* `
|
|
978
|
-
*
|
|
979
|
-
*
|
|
980
|
-
*
|
|
981
|
-
* @returns The function `getRightMost` returns
|
|
982
|
-
* is no rightmost node, it returns `null` or `undefined`, depending on the input.
|
|
1018
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
1019
|
+
* starting point for finding the rightmost node in a binary tree. It can be either a root node
|
|
1020
|
+
* (`R`), a key or node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or `null` or `undefined`.
|
|
1021
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
1022
|
+
* of iteration to be performed when finding the rightmost node in a binary tree. It can have two
|
|
1023
|
+
* possible values:
|
|
1024
|
+
* @returns The function `getRightMost` returns a NODE object, `null`, or `undefined`.
|
|
983
1025
|
*/
|
|
984
1026
|
getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
985
1027
|
if (this.isNIL(beginRoot))
|
|
@@ -1014,10 +1056,10 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1014
1056
|
* Time Complexity: O(log n)
|
|
1015
1057
|
* Space Complexity: O(1)
|
|
1016
1058
|
*
|
|
1017
|
-
* The function returns the predecessor of a given node in a tree.
|
|
1018
|
-
* @param {NODE} node - The parameter
|
|
1059
|
+
* The function returns the predecessor node of a given node in a binary tree.
|
|
1060
|
+
* @param {NODE} node - The parameter "node" is of type "NODE", which represents a node in a binary
|
|
1019
1061
|
* tree.
|
|
1020
|
-
* @returns the predecessor of the given
|
|
1062
|
+
* @returns the predecessor node of the given node.
|
|
1021
1063
|
*/
|
|
1022
1064
|
getPredecessor(node) {
|
|
1023
1065
|
if (this.isRealNode(node.left)) {
|
|
@@ -1043,8 +1085,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1043
1085
|
*
|
|
1044
1086
|
* The function `getSuccessor` returns the next node in a binary tree given a current node.
|
|
1045
1087
|
* @param {K | NODE | null} [x] - The parameter `x` can be of type `K`, `NODE`, or `null`.
|
|
1046
|
-
* @returns
|
|
1047
|
-
*
|
|
1088
|
+
* @returns The function `getSuccessor` returns a `NODE` object if a successor exists, `null` if
|
|
1089
|
+
* there is no successor, and `undefined` if the input `x` is not a valid node.
|
|
1048
1090
|
*/
|
|
1049
1091
|
getSuccessor(x) {
|
|
1050
1092
|
x = this.ensureNode(x);
|
|
@@ -1063,30 +1105,29 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1063
1105
|
/**
|
|
1064
1106
|
* Time complexity: O(n)
|
|
1065
1107
|
* Space complexity: O(n)
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
/**
|
|
1108
|
+
*/
|
|
1109
|
+
/**
|
|
1069
1110
|
* Time complexity: O(n)
|
|
1070
1111
|
* Space complexity: O(n)
|
|
1071
1112
|
*
|
|
1072
|
-
* The `dfs` function performs a depth-first search traversal on a binary tree
|
|
1073
|
-
*
|
|
1074
|
-
* callback function
|
|
1075
|
-
*
|
|
1076
|
-
*
|
|
1077
|
-
*
|
|
1078
|
-
*
|
|
1079
|
-
*
|
|
1080
|
-
*
|
|
1081
|
-
*
|
|
1082
|
-
*
|
|
1083
|
-
* @param {IterationType} iterationType - The `iterationType` parameter determines the
|
|
1084
|
-
* iteration to use
|
|
1113
|
+
* The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback
|
|
1114
|
+
* function on each node according to a specified pattern and iteration type.
|
|
1115
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
1116
|
+
* visited during the depth-first search. It takes a node as an argument and returns a value. The
|
|
1117
|
+
* return type of the callback function is determined by the generic type `C`.
|
|
1118
|
+
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter determines the order in which the
|
|
1119
|
+
* nodes are visited during the depth-first search. It can have one of the following values:
|
|
1120
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
1121
|
+
* point of the depth-first search. It can be either a node object, a key-value pair, or a key. If it
|
|
1122
|
+
* is a key or key-value pair, the method will find the corresponding node in the tree and start the
|
|
1123
|
+
* search from there.
|
|
1124
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter determines the
|
|
1125
|
+
* type of iteration to use during the depth-first search. It can have two possible values:
|
|
1085
1126
|
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
1086
|
-
* whether
|
|
1087
|
-
* `true`, null
|
|
1088
|
-
*
|
|
1089
|
-
* @returns an array of
|
|
1127
|
+
* whether or not to include null values in the depth-first search traversal. If `includeNull` is set
|
|
1128
|
+
* to `true`, null values will be included in the traversal. If `includeNull` is set to `false`, null
|
|
1129
|
+
* values will
|
|
1130
|
+
* @returns an array of the return types of the callback function.
|
|
1090
1131
|
*/
|
|
1091
1132
|
dfs(callback = this._DEFAULT_CALLBACK, pattern = 'IN', beginRoot = this.root, iterationType = 'ITERATIVE', includeNull = false) {
|
|
1092
1133
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -1202,22 +1243,23 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1202
1243
|
* Time complexity: O(n)
|
|
1203
1244
|
* Space complexity: O(n)
|
|
1204
1245
|
*
|
|
1205
|
-
* The `bfs` function performs a breadth-first search
|
|
1206
|
-
*
|
|
1246
|
+
* The `bfs` function performs a breadth-first search on a binary tree, calling a callback function
|
|
1247
|
+
* on each node and returning an array of the results.
|
|
1207
1248
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
1208
|
-
* the breadth-first search traversal. It takes a single
|
|
1249
|
+
* the breadth-first search traversal. It takes a single argument, which is the current node being
|
|
1209
1250
|
* visited, and returns a value of any type.
|
|
1210
|
-
* @param {
|
|
1211
|
-
* starting
|
|
1212
|
-
* or
|
|
1213
|
-
*
|
|
1214
|
-
* @param iterationType - The `iterationType` parameter determines the type of
|
|
1215
|
-
*
|
|
1216
|
-
* @param [includeNull=false] - The `includeNull` parameter is a boolean
|
|
1217
|
-
* to include null values in the breadth-first search traversal. If
|
|
1218
|
-
* `true`, null values will be included in the traversal
|
|
1219
|
-
*
|
|
1220
|
-
*
|
|
1251
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
1252
|
+
* starting point of the breadth-first search. It can be either a root node of a tree or a key, node,
|
|
1253
|
+
* or entry object. If no value is provided, the `root` property of the class is used as the default
|
|
1254
|
+
* starting point.
|
|
1255
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
1256
|
+
* iteration to be performed. It can have two possible values:
|
|
1257
|
+
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
1258
|
+
* whether or not to include null values in the breadth-first search (BFS) traversal. If
|
|
1259
|
+
* `includeNull` is set to `true`, null values will be included in the traversal. If `includeNull` is
|
|
1260
|
+
* set to `false
|
|
1261
|
+
* @returns The function `bfs` returns an array of values that are the result of invoking the
|
|
1262
|
+
* `callback` function on each node in the breadth-first order traversal of the binary tree.
|
|
1221
1263
|
*/
|
|
1222
1264
|
bfs(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
1223
1265
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -1280,18 +1322,18 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1280
1322
|
* Space complexity: O(n)
|
|
1281
1323
|
*
|
|
1282
1324
|
* The `listLevels` function returns an array of arrays, where each inner array represents a level in
|
|
1283
|
-
* a binary tree and contains the
|
|
1284
|
-
* level.
|
|
1325
|
+
* a binary tree and contains the results of applying a callback function to the nodes at that level.
|
|
1285
1326
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
1286
|
-
* the tree. It takes a
|
|
1287
|
-
*
|
|
1288
|
-
* @param {
|
|
1289
|
-
* starting
|
|
1290
|
-
*
|
|
1291
|
-
*
|
|
1292
|
-
*
|
|
1327
|
+
* the tree. It takes a node as an argument and returns a value. The return type of the callback
|
|
1328
|
+
* function is determined by the generic type `C` which extends `BTNCallback<NODE | null>`.
|
|
1329
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
1330
|
+
* starting point for traversing the tree. It can be either a root node, a key-value pair, or a node
|
|
1331
|
+
* entry. If no value is provided, the `root` property of the class is used as the default starting
|
|
1332
|
+
* point.
|
|
1333
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
1334
|
+
* iteration to be performed on the binary tree. It can have two possible values:
|
|
1293
1335
|
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
1294
|
-
* whether to include null values in the resulting levels. If `includeNull` is set to `true`,
|
|
1336
|
+
* whether or not to include null values in the resulting levels. If `includeNull` is set to `true`,
|
|
1295
1337
|
* null values will be included in the levels. If `includeNull` is set to `false`, null values will
|
|
1296
1338
|
* be excluded
|
|
1297
1339
|
* @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
|
|
@@ -1356,17 +1398,17 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1356
1398
|
* The `morris` function performs a depth-first traversal on a binary tree using the Morris traversal
|
|
1357
1399
|
* algorithm.
|
|
1358
1400
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
1359
|
-
* the tree. It takes a single
|
|
1360
|
-
*
|
|
1361
|
-
*
|
|
1362
|
-
*
|
|
1401
|
+
* the tree. It takes a single argument, which is the current node, and can return any value. The
|
|
1402
|
+
* return type of the `callback` function is determined by the `ReturnType<C>` type, which represents
|
|
1403
|
+
* the return
|
|
1404
|
+
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function is used
|
|
1405
|
+
* to specify the order in which the nodes of a binary tree are traversed. It can take one of the
|
|
1363
1406
|
* following values:
|
|
1364
|
-
* @param {
|
|
1365
|
-
* for the traversal. It can be
|
|
1366
|
-
* the root of the tree
|
|
1367
|
-
* @returns The function `morris` returns an array of values that are the
|
|
1368
|
-
*
|
|
1369
|
-
* by the return type of the `callback` function.
|
|
1407
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
1408
|
+
* point for the traversal. It can be either a node object, a key, or an entry object. If no value is
|
|
1409
|
+
* provided, the `root` of the tree is used as the starting point.
|
|
1410
|
+
* @returns The function `morris` returns an array of values that are the return values of the
|
|
1411
|
+
* callback function `callback`.
|
|
1370
1412
|
*/
|
|
1371
1413
|
morris(callback = this._DEFAULT_CALLBACK, pattern = 'IN', beginRoot = this.root) {
|
|
1372
1414
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -1461,8 +1503,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1461
1503
|
* Time complexity: O(n)
|
|
1462
1504
|
* Space complexity: O(n)
|
|
1463
1505
|
*
|
|
1464
|
-
* The `clone` function creates a
|
|
1465
|
-
* the new tree.
|
|
1506
|
+
* The `clone` function creates a deep copy of a tree object.
|
|
1466
1507
|
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
1467
1508
|
*/
|
|
1468
1509
|
clone() {
|
|
@@ -1483,16 +1524,16 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1483
1524
|
* Time Complexity: O(n)
|
|
1484
1525
|
* Space Complexity: O(n)
|
|
1485
1526
|
*
|
|
1486
|
-
* The `filter` function creates a new tree
|
|
1487
|
-
*
|
|
1488
|
-
*
|
|
1489
|
-
*
|
|
1490
|
-
*
|
|
1491
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that
|
|
1492
|
-
*
|
|
1493
|
-
*
|
|
1494
|
-
* @returns The `filter` method is returning a new tree object that contains the
|
|
1495
|
-
*
|
|
1527
|
+
* The `filter` function creates a new tree with entries that pass a given predicate function.
|
|
1528
|
+
* @param predicate - The `predicate` parameter is a callback function that is used to test each
|
|
1529
|
+
* element in the tree. It takes three arguments: `value`, `key`, and `index`. The `value` argument
|
|
1530
|
+
* represents the value of the current element being processed, the `key` argument represents the key
|
|
1531
|
+
* of the
|
|
1532
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
1533
|
+
* specify the value of `this` within the `predicate` function. When the `predicate` function is
|
|
1534
|
+
* called, `thisArg` will be used as the value of `this` within the function. If `thisArg`
|
|
1535
|
+
* @returns The `filter` method is returning a new tree object that contains the entries that pass
|
|
1536
|
+
* the given predicate function.
|
|
1496
1537
|
*/
|
|
1497
1538
|
filter(predicate, thisArg) {
|
|
1498
1539
|
const newTree = this.createTree();
|
|
@@ -1512,15 +1553,15 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1512
1553
|
* Time Complexity: O(n)
|
|
1513
1554
|
* Space Complexity: O(n)
|
|
1514
1555
|
*
|
|
1515
|
-
* The `map` function creates a new tree by applying a callback function to each
|
|
1516
|
-
*
|
|
1517
|
-
* @param callback - The callback parameter is a function that will be called for each
|
|
1518
|
-
*
|
|
1519
|
-
* the
|
|
1520
|
-
*
|
|
1521
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that
|
|
1522
|
-
*
|
|
1523
|
-
*
|
|
1556
|
+
* The `map` function creates a new tree by applying a callback function to each entry in the current
|
|
1557
|
+
* tree.
|
|
1558
|
+
* @param callback - The callback parameter is a function that will be called for each entry in the
|
|
1559
|
+
* tree. It takes three arguments: value, key, and index. The value argument represents the value of
|
|
1560
|
+
* the current entry, the key argument represents the key of the current entry, and the index
|
|
1561
|
+
* argument represents the index of the
|
|
1562
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
1563
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
1564
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
1524
1565
|
* @returns The `map` method is returning a new tree object.
|
|
1525
1566
|
*/
|
|
1526
1567
|
map(callback, thisArg) {
|
|
@@ -1548,11 +1589,15 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1548
1589
|
* Time Complexity: O(n)
|
|
1549
1590
|
* Space Complexity: O(n)
|
|
1550
1591
|
*
|
|
1551
|
-
* The `print` function
|
|
1552
|
-
* @param {
|
|
1553
|
-
*
|
|
1554
|
-
*
|
|
1555
|
-
*
|
|
1592
|
+
* The `print` function in TypeScript prints the binary tree structure with customizable options.
|
|
1593
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
1594
|
+
* point for printing the binary tree. It can be either a node of the binary tree or a key or entry
|
|
1595
|
+
* that exists in the binary tree. If no value is provided, the root of the binary tree will be used
|
|
1596
|
+
* as the starting point.
|
|
1597
|
+
* @param {BinaryTreePrintOptions} [options] - The `options` parameter is an optional object that
|
|
1598
|
+
* allows you to customize the printing behavior. It has the following properties:
|
|
1599
|
+
* @returns Nothing is being returned. The function has a return type of `void`, which means it does
|
|
1600
|
+
* not return any value.
|
|
1556
1601
|
*/
|
|
1557
1602
|
print(beginRoot = this.root, options) {
|
|
1558
1603
|
const opts = Object.assign({ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false }, options);
|
|
@@ -1563,10 +1608,10 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1563
1608
|
console.log(`U for undefined
|
|
1564
1609
|
`);
|
|
1565
1610
|
if (opts.isShowNull)
|
|
1566
|
-
console.log(`
|
|
1611
|
+
console.log(`N for null
|
|
1567
1612
|
`);
|
|
1568
1613
|
if (opts.isShowRedBlackNIL)
|
|
1569
|
-
console.log(`S for Sentinel Node
|
|
1614
|
+
console.log(`S for Sentinel Node(NIL)
|
|
1570
1615
|
`);
|
|
1571
1616
|
const display = (root) => {
|
|
1572
1617
|
const [lines, , ,] = this._displayAux(root, opts);
|
|
@@ -1577,13 +1622,18 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1577
1622
|
display(beginRoot);
|
|
1578
1623
|
}
|
|
1579
1624
|
/**
|
|
1580
|
-
*
|
|
1581
|
-
*
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
*
|
|
1585
|
-
*
|
|
1586
|
-
*
|
|
1625
|
+
* Time Complexity: O(1)
|
|
1626
|
+
* Space Complexity: O(1)
|
|
1627
|
+
*/
|
|
1628
|
+
/**
|
|
1629
|
+
* Time Complexity: O(1)
|
|
1630
|
+
* Space Complexity: O(1)
|
|
1631
|
+
*
|
|
1632
|
+
* The function `_getIterator` is a generator function that returns an iterator for the key-value
|
|
1633
|
+
* pairs in a binary search tree.
|
|
1634
|
+
* @param node - The `node` parameter represents the current node in the binary search tree. It is
|
|
1635
|
+
* initially set to the root node of the tree.
|
|
1636
|
+
* @returns an IterableIterator<[K, V | undefined]>.
|
|
1587
1637
|
*/
|
|
1588
1638
|
*_getIterator(node = this.root) {
|
|
1589
1639
|
if (!node)
|
|
@@ -1592,28 +1642,35 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1592
1642
|
const stack = [];
|
|
1593
1643
|
let current = node;
|
|
1594
1644
|
while (current || stack.length > 0) {
|
|
1595
|
-
while (
|
|
1645
|
+
while (this.isRealNode(current)) {
|
|
1596
1646
|
stack.push(current);
|
|
1597
1647
|
current = current.left;
|
|
1598
1648
|
}
|
|
1599
1649
|
current = stack.pop();
|
|
1600
|
-
if (
|
|
1650
|
+
if (this.isRealNode(current)) {
|
|
1601
1651
|
yield [current.key, current.value];
|
|
1602
1652
|
current = current.right;
|
|
1603
1653
|
}
|
|
1604
1654
|
}
|
|
1605
1655
|
}
|
|
1606
1656
|
else {
|
|
1607
|
-
if (node.left &&
|
|
1657
|
+
if (node.left && this.isRealNode(node)) {
|
|
1608
1658
|
yield* this[Symbol.iterator](node.left);
|
|
1609
1659
|
}
|
|
1610
1660
|
yield [node.key, node.value];
|
|
1611
|
-
if (node.right &&
|
|
1661
|
+
if (node.right && this.isRealNode(node)) {
|
|
1612
1662
|
yield* this[Symbol.iterator](node.right);
|
|
1613
1663
|
}
|
|
1614
1664
|
}
|
|
1615
1665
|
}
|
|
1616
1666
|
/**
|
|
1667
|
+
* Time Complexity: O(n)
|
|
1668
|
+
* Space Complexity: O(n)
|
|
1669
|
+
*/
|
|
1670
|
+
/**
|
|
1671
|
+
* Time Complexity: O(n)
|
|
1672
|
+
* Space Complexity: O(n)
|
|
1673
|
+
*
|
|
1617
1674
|
* The `_displayAux` function is responsible for generating the display layout of a binary tree node,
|
|
1618
1675
|
* taking into account various options such as whether to show null, undefined, or NaN nodes.
|
|
1619
1676
|
* @param {NODE | null | undefined} node - The `node` parameter represents a node in a binary tree.
|
|
@@ -1637,12 +1694,12 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1637
1694
|
else if (node === undefined && !isShowUndefined) {
|
|
1638
1695
|
return emptyDisplayLayout;
|
|
1639
1696
|
}
|
|
1640
|
-
else if (
|
|
1697
|
+
else if (this.isNIL(node) && !isShowRedBlackNIL) {
|
|
1641
1698
|
return emptyDisplayLayout;
|
|
1642
1699
|
}
|
|
1643
1700
|
else if (node !== null && node !== undefined) {
|
|
1644
1701
|
// Display logic of normal nodes
|
|
1645
|
-
const key = node.key, line =
|
|
1702
|
+
const key = node.key, line = this.isNIL(node) ? 'S' : key.toString(), width = line.length;
|
|
1646
1703
|
return _buildNodeDisplay(line, width, this._displayAux(node.left, options), this._displayAux(node.right, options));
|
|
1647
1704
|
}
|
|
1648
1705
|
else {
|
|
@@ -1680,10 +1737,21 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1680
1737
|
}
|
|
1681
1738
|
}
|
|
1682
1739
|
/**
|
|
1683
|
-
*
|
|
1684
|
-
*
|
|
1685
|
-
|
|
1686
|
-
|
|
1740
|
+
* Time Complexity: O(1)
|
|
1741
|
+
* Space Complexity: O(1)
|
|
1742
|
+
*/
|
|
1743
|
+
/**
|
|
1744
|
+
* Time Complexity: O(1)
|
|
1745
|
+
* Space Complexity: O(1)
|
|
1746
|
+
*
|
|
1747
|
+
* The function `_swapProperties` swaps the key-value properties between two nodes.
|
|
1748
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} srcNode - The source node that will be swapped with the
|
|
1749
|
+
* destination node. It can be either an instance of the class `R`, or an object of type
|
|
1750
|
+
* `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
1751
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} destNode - The `destNode` parameter is the node where
|
|
1752
|
+
* the properties will be swapped with the `srcNode`.
|
|
1753
|
+
* @returns either the `destNode` object with its properties swapped with the `srcNode` object's
|
|
1754
|
+
* properties, or `undefined` if either `srcNode` or `destNode` is falsy.
|
|
1687
1755
|
*/
|
|
1688
1756
|
_swapProperties(srcNode, destNode) {
|
|
1689
1757
|
srcNode = this.ensureNode(srcNode);
|
|
@@ -1702,12 +1770,20 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1702
1770
|
return undefined;
|
|
1703
1771
|
}
|
|
1704
1772
|
/**
|
|
1705
|
-
*
|
|
1773
|
+
* Time Complexity: O(1)
|
|
1774
|
+
* Space Complexity: O(1)
|
|
1775
|
+
*/
|
|
1776
|
+
/**
|
|
1777
|
+
* Time Complexity: O(1)
|
|
1778
|
+
* Space Complexity: O(1)
|
|
1779
|
+
*
|
|
1780
|
+
* The function replaces a node in a binary tree with a new node, updating the parent, left child,
|
|
1781
|
+
* right child, and root if necessary.
|
|
1706
1782
|
* @param {NODE} oldNode - The oldNode parameter represents the node that needs to be replaced in the
|
|
1707
1783
|
* tree.
|
|
1708
1784
|
* @param {NODE} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
|
|
1709
1785
|
* tree.
|
|
1710
|
-
* @returns
|
|
1786
|
+
* @returns the newNode.
|
|
1711
1787
|
*/
|
|
1712
1788
|
_replaceNode(oldNode, newNode) {
|
|
1713
1789
|
if (oldNode.parent) {
|
|
@@ -1727,10 +1803,17 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1727
1803
|
return newNode;
|
|
1728
1804
|
}
|
|
1729
1805
|
/**
|
|
1730
|
-
*
|
|
1731
|
-
*
|
|
1732
|
-
|
|
1733
|
-
|
|
1806
|
+
* Time Complexity: O(1)
|
|
1807
|
+
* Space Complexity: O(1)
|
|
1808
|
+
*/
|
|
1809
|
+
/**
|
|
1810
|
+
* Time Complexity: O(1)
|
|
1811
|
+
* Space Complexity: O(1)
|
|
1812
|
+
*
|
|
1813
|
+
* The function sets the root property of an object to the provided value, and also updates the
|
|
1814
|
+
* parent property of the new root.
|
|
1815
|
+
* @param {NODE | null | undefined} v - The parameter `v` is of type `NODE | null | undefined`. This
|
|
1816
|
+
* means that it can accept a value of type `NODE`, `null`, or `undefined`.
|
|
1734
1817
|
*/
|
|
1735
1818
|
_setRoot(v) {
|
|
1736
1819
|
if (v) {
|
|
@@ -1738,6 +1821,23 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1738
1821
|
}
|
|
1739
1822
|
this._root = v;
|
|
1740
1823
|
}
|
|
1824
|
+
/**
|
|
1825
|
+
* Time Complexity: O(1)
|
|
1826
|
+
* Space Complexity: O(1)
|
|
1827
|
+
*/
|
|
1828
|
+
/**
|
|
1829
|
+
* Time Complexity: O(1)
|
|
1830
|
+
* Space Complexity: O(1)
|
|
1831
|
+
*
|
|
1832
|
+
* The function `_ensureCallback` ensures that a callback function is provided and returns it.
|
|
1833
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is of type
|
|
1834
|
+
* `ReturnType<C> | null | undefined`. This means it can accept a value that is the return type of
|
|
1835
|
+
* the generic type `C`, or it can be `null` or `undefined`.
|
|
1836
|
+
* @param {C} callback - The `callback` parameter is a function that takes a `node` as an argument
|
|
1837
|
+
* and returns a value. It is of type `C`, which is a generic type that extends the
|
|
1838
|
+
* `BTNCallback<NODE>` type.
|
|
1839
|
+
* @returns the callback parameter.
|
|
1840
|
+
*/
|
|
1741
1841
|
_ensureCallback(identifier, callback = this._DEFAULT_CALLBACK) {
|
|
1742
1842
|
if ((!callback || callback === this._DEFAULT_CALLBACK) && this.isNode(identifier)) {
|
|
1743
1843
|
callback = (node => node);
|