data-structure-typed 1.54.2 → 1.54.3
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/CHANGELOG.md +1 -1
- package/LICENSE +2 -2
- package/README.md +14 -1
- package/README_zh-CN.md +1 -1
- package/benchmark/report.html +4 -1
- package/benchmark/report.json +76 -17
- package/dist/cjs/data-structures/binary-tree/avl-tree-counter.d.ts +21 -20
- package/dist/cjs/data-structures/binary-tree/avl-tree-counter.js +8 -7
- package/dist/cjs/data-structures/binary-tree/avl-tree-counter.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +12 -12
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +2 -2
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +25 -21
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +12 -8
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +111 -225
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +177 -144
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +59 -53
- package/dist/cjs/data-structures/binary-tree/bst.js +75 -119
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/red-black-tree.d.ts +18 -18
- package/dist/cjs/data-structures/binary-tree/red-black-tree.js +6 -6
- package/dist/cjs/data-structures/binary-tree/red-black-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-counter.d.ts +19 -19
- package/dist/cjs/data-structures/binary-tree/tree-counter.js +12 -12
- package/dist/cjs/data-structures/binary-tree/tree-counter.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +12 -12
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +2 -2
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -0
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/cjs/utils/utils.d.ts +2 -2
- package/dist/esm/data-structures/binary-tree/avl-tree-counter.d.ts +21 -20
- package/dist/esm/data-structures/binary-tree/avl-tree-counter.js +9 -8
- package/dist/esm/data-structures/binary-tree/avl-tree-counter.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.d.ts +12 -12
- package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js +3 -3
- package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/avl-tree.d.ts +25 -21
- package/dist/esm/data-structures/binary-tree/avl-tree.js +13 -9
- package/dist/esm/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/binary-tree.d.ts +111 -225
- package/dist/esm/data-structures/binary-tree/binary-tree.js +181 -148
- package/dist/esm/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/bst.d.ts +59 -53
- package/dist/esm/data-structures/binary-tree/bst.js +76 -120
- package/dist/esm/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/red-black-tree.d.ts +18 -18
- package/dist/esm/data-structures/binary-tree/red-black-tree.js +7 -7
- package/dist/esm/data-structures/binary-tree/red-black-tree.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/tree-counter.d.ts +19 -19
- package/dist/esm/data-structures/binary-tree/tree-counter.js +13 -13
- package/dist/esm/data-structures/binary-tree/tree-counter.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/tree-multi-map.d.ts +12 -12
- package/dist/esm/data-structures/binary-tree/tree-multi-map.js +3 -3
- package/dist/esm/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/esm/types/data-structures/binary-tree/binary-tree.d.ts +1 -0
- package/dist/esm/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/esm/utils/utils.d.ts +2 -2
- package/dist/umd/data-structure-typed.js +296 -279
- package/dist/umd/data-structure-typed.min.js +5 -12
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +7 -7
- package/src/data-structures/binary-tree/avl-tree-counter.ts +30 -23
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +25 -15
- package/src/data-structures/binary-tree/avl-tree.ts +35 -29
- package/src/data-structures/binary-tree/binary-tree.ts +469 -252
- package/src/data-structures/binary-tree/bst.ts +141 -143
- package/src/data-structures/binary-tree/red-black-tree.ts +27 -35
- package/src/data-structures/binary-tree/tree-counter.ts +33 -27
- package/src/data-structures/binary-tree/tree-multi-map.ts +25 -17
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -0
- package/src/types/data-structures/binary-tree/bst.ts +1 -1
- package/src/utils/utils.ts +2 -2
- package/test/integration/compile.mjs +21 -21
- package/test/performance/data-structures/binary-tree/avl-tree.test.mjs +71 -0
- package/test/performance/data-structures/binary-tree/red-black-tree.test.mjs +81 -0
- package/test/performance/{reportor.js → reportor.mjs} +264 -8
- package/test/performance/reportor.ts +1 -1
- package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +7 -7
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +4 -5
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +72 -5
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +132 -82
- package/test/unit/data-structures/binary-tree/bst.test.ts +12 -12
- package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +4 -12
- package/test/unit/data-structures/binary-tree/tree-counter.test.ts +4 -4
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +5 -5
- package/test/utils/json2html.ts +0 -154
- package/test/performance/data-structures/binary-tree/avl-tree.test.js +0 -45
- /package/test/performance/data-structures/binary-tree/{rb-tree.test.ts → red-black-tree.test.ts} +0 -0
|
@@ -15,6 +15,9 @@ import { DFSOperation, Range } from '../../common';
|
|
|
15
15
|
* @template BinaryTreeNode<K, V> - The type of the family relationship in the binary tree.
|
|
16
16
|
*/
|
|
17
17
|
export class BinaryTreeNode {
|
|
18
|
+
key;
|
|
19
|
+
value;
|
|
20
|
+
parent = undefined;
|
|
18
21
|
/**
|
|
19
22
|
* The constructor function initializes an object with a key and an optional value in TypeScript.
|
|
20
23
|
* @param {K} key - The `key` parameter in the constructor function is used to store the key value
|
|
@@ -27,9 +30,6 @@ export class BinaryTreeNode {
|
|
|
27
30
|
this.key = key;
|
|
28
31
|
this.value = value;
|
|
29
32
|
}
|
|
30
|
-
key;
|
|
31
|
-
value;
|
|
32
|
-
parent = undefined;
|
|
33
33
|
_left = undefined;
|
|
34
34
|
get left() {
|
|
35
35
|
return this._left;
|
|
@@ -92,11 +92,12 @@ export class BinaryTreeNode {
|
|
|
92
92
|
* 5. Leaf Nodes: Nodes without children are leaves.
|
|
93
93
|
*/
|
|
94
94
|
export class BinaryTree extends IterableEntryBase {
|
|
95
|
+
iterationType = 'ITERATIVE';
|
|
95
96
|
/**
|
|
96
97
|
* This TypeScript constructor function initializes a binary tree with optional options and adds
|
|
97
98
|
* elements based on the provided input.
|
|
98
99
|
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
|
|
99
|
-
* iterable that can contain either objects of type `
|
|
100
|
+
* iterable that can contain either objects of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. It
|
|
100
101
|
* is used to initialize the binary tree with keys, nodes, entries, or raw data.
|
|
101
102
|
* @param [options] - The `options` parameter in the constructor is an optional object that can
|
|
102
103
|
* contain the following properties:
|
|
@@ -104,11 +105,13 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
104
105
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
105
106
|
super();
|
|
106
107
|
if (options) {
|
|
107
|
-
const { iterationType, toEntryFn, isMapMode } = options;
|
|
108
|
+
const { iterationType, toEntryFn, isMapMode, isDuplicate } = options;
|
|
108
109
|
if (iterationType)
|
|
109
110
|
this.iterationType = iterationType;
|
|
110
111
|
if (isMapMode !== undefined)
|
|
111
112
|
this._isMapMode = isMapMode;
|
|
113
|
+
if (isDuplicate !== undefined)
|
|
114
|
+
this._isDuplicate = isDuplicate;
|
|
112
115
|
if (typeof toEntryFn === 'function')
|
|
113
116
|
this._toEntryFn = toEntryFn;
|
|
114
117
|
else if (toEntryFn)
|
|
@@ -117,11 +120,14 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
117
120
|
if (keysNodesEntriesOrRaws)
|
|
118
121
|
this.addMany(keysNodesEntriesOrRaws);
|
|
119
122
|
}
|
|
120
|
-
iterationType = 'ITERATIVE';
|
|
121
123
|
_isMapMode = true;
|
|
122
124
|
get isMapMode() {
|
|
123
125
|
return this._isMapMode;
|
|
124
126
|
}
|
|
127
|
+
_isDuplicate = false;
|
|
128
|
+
get isDuplicate() {
|
|
129
|
+
return this._isDuplicate;
|
|
130
|
+
}
|
|
125
131
|
_store = new Map();
|
|
126
132
|
get store() {
|
|
127
133
|
return this._store;
|
|
@@ -182,8 +188,8 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
182
188
|
*
|
|
183
189
|
* The function `ensureNode` in TypeScript checks if a given input is a node, entry, key, or raw
|
|
184
190
|
* value and returns the corresponding node or null.
|
|
185
|
-
* @param {
|
|
186
|
-
* parameter in the `ensureNode` function can be of type `
|
|
191
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `keyNodeOrEntry`
|
|
192
|
+
* parameter in the `ensureNode` function can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. It
|
|
187
193
|
* is used to determine whether the input is a key, node, entry, or raw data. The
|
|
188
194
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `ensureNode` function
|
|
189
195
|
* is used to specify the type of iteration to be performed. It has a default value of
|
|
@@ -215,7 +221,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
215
221
|
* Space Complexity: O(1)
|
|
216
222
|
*
|
|
217
223
|
* The function isNode checks if the input is an instance of BinaryTreeNode.
|
|
218
|
-
* @param {
|
|
224
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter
|
|
219
225
|
* `keyNodeOrEntry` can be either a key, a node, an entry, or raw data. The function is
|
|
220
226
|
* checking if the input is an instance of a `BinaryTreeNode` and returning a boolean value
|
|
221
227
|
* accordingly.
|
|
@@ -232,7 +238,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
232
238
|
* Space Complexity: O(1)
|
|
233
239
|
*
|
|
234
240
|
* The function `isRaw` checks if the input parameter is of type `R` by verifying if it is an object.
|
|
235
|
-
* @param {
|
|
241
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R} keyNodeEntryOrRaw - K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
|
|
236
242
|
* @returns The function `isRaw` is checking if the `keyNodeEntryOrRaw` parameter is of type `R` by
|
|
237
243
|
* checking if it is an object. If the parameter is an object, the function will return `true`,
|
|
238
244
|
* indicating that it is of type `R`.
|
|
@@ -245,8 +251,8 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
245
251
|
* Space Complexity: O(1)
|
|
246
252
|
*
|
|
247
253
|
* The function `isRealNode` checks if a given input is a valid node in a binary tree.
|
|
248
|
-
* @param {
|
|
249
|
-
* parameter in the `isRealNode` function can be of type `
|
|
254
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `keyNodeOrEntry`
|
|
255
|
+
* parameter in the `isRealNode` function can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`.
|
|
250
256
|
* The function checks if the input parameter is a `BinaryTreeNode<K, V>` type by verifying if it is not equal
|
|
251
257
|
* @returns The function `isRealNode` is checking if the input `keyNodeOrEntry` is a valid
|
|
252
258
|
* node by comparing it to `this._NIL`, `null`, and `undefined`. If the input is not one of these
|
|
@@ -263,7 +269,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
263
269
|
* Space Complexity: O(1)
|
|
264
270
|
*
|
|
265
271
|
* The function checks if a given input is a valid node or null.
|
|
266
|
-
* @param {
|
|
272
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter
|
|
267
273
|
* `keyNodeOrEntry` in the `isRealNodeOrNull` function can be of type `BTNRep<K,
|
|
268
274
|
* V, BinaryTreeNode<K, V>>` or `R`. It is a union type that can either be a key, a node, an entry, or
|
|
269
275
|
* @returns The function `isRealNodeOrNull` is returning a boolean value. It checks if the input
|
|
@@ -278,7 +284,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
278
284
|
* Space Complexity: O(1)
|
|
279
285
|
*
|
|
280
286
|
* The function isNIL checks if a given key, node, entry, or raw value is equal to the _NIL value.
|
|
281
|
-
* @param {
|
|
287
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - BTNRep<K, V,
|
|
282
288
|
* BinaryTreeNode<K, V>>
|
|
283
289
|
* @returns The function is checking if the `keyNodeOrEntry` parameter is equal to the `_NIL`
|
|
284
290
|
* property of the current object and returning a boolean value based on that comparison.
|
|
@@ -291,9 +297,9 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
291
297
|
* Space Complexity: O(1)
|
|
292
298
|
*
|
|
293
299
|
* The function `isRange` checks if the input parameter is an instance of the `Range` class.
|
|
294
|
-
* @param {
|
|
295
|
-
*
|
|
296
|
-
* of type `
|
|
300
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>> | Range<K>} keyNodeEntryOrPredicate
|
|
301
|
+
* - The `keyNodeEntryOrPredicate` parameter in the `isRange` function can be
|
|
302
|
+
* of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `, `NodePredicate<BinaryTreeNode<K, V>>`, or
|
|
297
303
|
* `Range<K>`. The function checks if the `keyNodeEntry
|
|
298
304
|
* @returns The `isRange` function is checking if the `keyNodeEntryOrPredicate` parameter is an
|
|
299
305
|
* instance of the `Range` class. If it is an instance of `Range`, the function will return `true`,
|
|
@@ -309,8 +315,8 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
309
315
|
*
|
|
310
316
|
* The function determines whether a given key, node, entry, or raw data is a leaf node in a binary
|
|
311
317
|
* tree.
|
|
312
|
-
* @param {
|
|
313
|
-
* `keyNodeOrEntry` can be of type `
|
|
318
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter
|
|
319
|
+
* `keyNodeOrEntry` can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. It represents a
|
|
314
320
|
* key, node, entry, or raw data in a binary tree structure. The function `isLeaf` checks whether the
|
|
315
321
|
* provided
|
|
316
322
|
* @returns The function `isLeaf` returns a boolean value indicating whether the input
|
|
@@ -330,8 +336,8 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
330
336
|
*
|
|
331
337
|
* The function `isEntry` checks if the input is a BTNEntry object by verifying if it is an array
|
|
332
338
|
* with a length of 2.
|
|
333
|
-
* @param {
|
|
334
|
-
* parameter in the `isEntry` function can be of type `
|
|
339
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `keyNodeOrEntry`
|
|
340
|
+
* parameter in the `isEntry` function can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or type `R`.
|
|
335
341
|
* The function checks if the provided `keyNodeOrEntry` is of type `BTN
|
|
336
342
|
* @returns The `isEntry` function is checking if the `keyNodeOrEntry` parameter is an array
|
|
337
343
|
* with a length of 2. If it is, then it returns `true`, indicating that the parameter is of type
|
|
@@ -362,7 +368,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
362
368
|
*
|
|
363
369
|
* The `add` function in TypeScript adds a new node to a binary tree while handling duplicate keys
|
|
364
370
|
* and finding the correct insertion position.
|
|
365
|
-
* @param {
|
|
371
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `add` method you provided
|
|
366
372
|
* seems to be for adding a new node to a binary tree structure. The `keyNodeOrEntry`
|
|
367
373
|
* parameter in the method can accept different types of values:
|
|
368
374
|
* @param {V} [value] - The `value` parameter in the `add` method represents the value associated
|
|
@@ -391,12 +397,14 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
391
397
|
const cur = queue.shift();
|
|
392
398
|
if (!cur)
|
|
393
399
|
continue;
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
this.
|
|
399
|
-
|
|
400
|
+
if (!this._isDuplicate) {
|
|
401
|
+
// Check for duplicate keys when newNode is not null
|
|
402
|
+
if (newNode !== null && cur.key === newNode.key) {
|
|
403
|
+
this._replaceNode(cur, newNode);
|
|
404
|
+
if (this._isMapMode)
|
|
405
|
+
this._setValue(cur.key, newValue);
|
|
406
|
+
return true; // If duplicate keys are found, no insertion is performed
|
|
407
|
+
}
|
|
400
408
|
}
|
|
401
409
|
// Record the first possible insertion location found
|
|
402
410
|
if (potentialParent === undefined && (cur.left === undefined || cur.right === undefined)) {
|
|
@@ -436,7 +444,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
436
444
|
* each insertion was successful.
|
|
437
445
|
* @param keysNodesEntriesOrRaws - `keysNodesEntriesOrRaws` is an iterable that can contain a
|
|
438
446
|
* mix of keys, nodes, entries, or raw values. Each element in this iterable can be of type
|
|
439
|
-
* `
|
|
447
|
+
* `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`.
|
|
440
448
|
* @param [values] - The `values` parameter in the `addMany` function is an optional parameter that
|
|
441
449
|
* accepts an iterable of values. These values correspond to the keys or nodes being added in the
|
|
442
450
|
* `keysNodesEntriesOrRaws` parameter. If provided, the function will iterate over the values and
|
|
@@ -484,7 +492,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
484
492
|
* The `refill` function clears the existing data structure and then adds new key-value pairs based
|
|
485
493
|
* on the provided input.
|
|
486
494
|
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the `refill`
|
|
487
|
-
* method can accept an iterable containing a mix of `
|
|
495
|
+
* method can accept an iterable containing a mix of `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` objects or `R`
|
|
488
496
|
* objects.
|
|
489
497
|
* @param [values] - The `values` parameter in the `refill` method is an optional parameter that
|
|
490
498
|
* accepts an iterable of values of type `V` or `undefined`.
|
|
@@ -499,7 +507,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
499
507
|
*
|
|
500
508
|
* The function `delete` in TypeScript implements the deletion of a node in a binary tree and returns
|
|
501
509
|
* the deleted node along with information for tree balancing.
|
|
502
|
-
* @param {
|
|
510
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry
|
|
503
511
|
* - The `delete` method you provided is used to delete a node from a binary tree based on the key,
|
|
504
512
|
* node, entry or raw data. The method returns an array of
|
|
505
513
|
* `BinaryTreeDeleteResult` objects containing information about the deleted node and whether
|
|
@@ -561,15 +569,15 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
561
569
|
*
|
|
562
570
|
* The `search` function in TypeScript performs a depth-first or breadth-first search on a tree
|
|
563
571
|
* structure based on a given predicate or key, with options to return multiple results or just one.
|
|
564
|
-
* @param {
|
|
572
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate - The
|
|
565
573
|
* `keyNodeEntryOrPredicate` parameter in the `search` function can accept three types of values:
|
|
566
574
|
* @param [onlyOne=false] - The `onlyOne` parameter in the `search` function is a boolean flag that
|
|
567
575
|
* determines whether the search should stop after finding the first matching node. If `onlyOne` is
|
|
568
576
|
* set to `true`, the search will return as soon as a matching node is found. If `onlyOne` is
|
|
569
577
|
* @param {C} callback - The `callback` parameter in the `search` function is a callback function
|
|
570
578
|
* that will be called on each node that matches the search criteria. It is of type `C`, which
|
|
571
|
-
* extends `NodeCallback<BinaryTreeNode<K, V
|
|
572
|
-
* @param {
|
|
579
|
+
* extends `NodeCallback<BinaryTreeNode<K, V> | null>`. The default value for `callback` is `this._DEFAULT_NODE_CALLBACK` if
|
|
580
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `search` function is
|
|
573
581
|
* used to specify the node from which the search operation should begin. It represents the starting
|
|
574
582
|
* point in the binary tree where the search will be performed. If no specific `startNode` is
|
|
575
583
|
* provided, the search operation will start from the root
|
|
@@ -630,12 +638,12 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
630
638
|
*
|
|
631
639
|
* The function `getNodes` retrieves nodes from a binary tree based on a key, node, entry, raw data,
|
|
632
640
|
* or predicate, with options for recursive or iterative traversal.
|
|
633
|
-
* @param {
|
|
641
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
|
|
634
642
|
* - The `getNodes` function you provided takes several parameters:
|
|
635
643
|
* @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` function is a boolean flag that
|
|
636
644
|
* determines whether to return only the first node that matches the criteria specified by the
|
|
637
645
|
* `keyNodeEntryOrPredicate` parameter. If `onlyOne` is set to `true`, the function will
|
|
638
|
-
* @param {
|
|
646
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
|
|
639
647
|
* `getNodes` function is used to specify the starting point for traversing the binary tree. It
|
|
640
648
|
* represents the root node of the binary tree or the node from which the traversal should begin. If
|
|
641
649
|
* not provided, the default value is set to `this._root
|
|
@@ -654,10 +662,10 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
654
662
|
*
|
|
655
663
|
* The `getNode` function retrieves a node based on the provided key, node, entry, raw data, or
|
|
656
664
|
* predicate.
|
|
657
|
-
* @param {
|
|
665
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
|
|
658
666
|
* - The `keyNodeEntryOrPredicate` parameter in the `getNode` function can accept a key,
|
|
659
667
|
* node, entry, raw data, or a predicate function.
|
|
660
|
-
* @param {
|
|
668
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
|
|
661
669
|
* `getNode` function is used to specify the starting point for searching for a node in a binary
|
|
662
670
|
* tree. If no specific starting point is provided, the default value is set to `this._root`, which
|
|
663
671
|
* is typically the root node of the binary tree.
|
|
@@ -677,10 +685,10 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
677
685
|
*
|
|
678
686
|
* This function overrides the `get` method to retrieve the value associated with a specified key,
|
|
679
687
|
* node, entry, raw data, or predicate in a data structure.
|
|
680
|
-
* @param {
|
|
688
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
|
|
681
689
|
* - The `keyNodeEntryOrPredicate` parameter in the `get` method can accept one of the
|
|
682
690
|
* following types:
|
|
683
|
-
* @param {
|
|
691
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `get`
|
|
684
692
|
* method is used to specify the starting point for searching for a key or node in the binary tree.
|
|
685
693
|
* If no specific starting point is provided, the default starting point is the root of the binary
|
|
686
694
|
* tree (`this._root`).
|
|
@@ -708,10 +716,10 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
708
716
|
*
|
|
709
717
|
* The `has` function in TypeScript checks if a specified key, node, entry, raw data, or predicate
|
|
710
718
|
* exists in the data structure.
|
|
711
|
-
* @param {
|
|
719
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
|
|
712
720
|
* - The `keyNodeEntryOrPredicate` parameter in the `override has` method can accept one of
|
|
713
721
|
* the following types:
|
|
714
|
-
* @param {
|
|
722
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
|
|
715
723
|
* `override` method is used to specify the starting point for the search operation within the data
|
|
716
724
|
* structure. It defaults to `this._root` if not provided explicitly.
|
|
717
725
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `override has` method
|
|
@@ -755,7 +763,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
755
763
|
*
|
|
756
764
|
* The function checks if a binary tree is perfectly balanced by comparing its minimum height with
|
|
757
765
|
* its height.
|
|
758
|
-
* @param {
|
|
766
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter is the starting
|
|
759
767
|
* point for checking if the binary tree is perfectly balanced. It represents the root node of the
|
|
760
768
|
* binary tree or a specific node from which the balance check should begin.
|
|
761
769
|
* @returns The method `isPerfectlyBalanced` is returning a boolean value, which indicates whether
|
|
@@ -773,7 +781,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
773
781
|
*
|
|
774
782
|
* The function `isBST` in TypeScript checks if a binary search tree is valid using either recursive
|
|
775
783
|
* or iterative methods.
|
|
776
|
-
* @param {
|
|
784
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `isBST`
|
|
777
785
|
* function represents the starting point for checking whether a binary search tree (BST) is valid.
|
|
778
786
|
* It can be a node in the BST or a reference to the root of the BST. If no specific node is
|
|
779
787
|
* provided, the function will default to
|
|
@@ -832,10 +840,10 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
832
840
|
* Space Complexity: O(log n)
|
|
833
841
|
*
|
|
834
842
|
* The `getDepth` function calculates the depth between two nodes in a binary tree.
|
|
835
|
-
* @param {
|
|
843
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } dist - The `dist` parameter in the `getDepth`
|
|
836
844
|
* function represents the node or entry in a binary tree map, or a reference to a node in the tree.
|
|
837
845
|
* It is the target node for which you want to calculate the depth from the `startNode` node.
|
|
838
|
-
* @param {
|
|
846
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
|
|
839
847
|
* `getDepth` function represents the starting point from which you want to calculate the depth of a
|
|
840
848
|
* given node or entry in a binary tree. If no specific starting point is provided, the default value
|
|
841
849
|
* for `startNode` is set to the root of the binary
|
|
@@ -862,7 +870,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
862
870
|
*
|
|
863
871
|
* The `getHeight` function calculates the maximum height of a binary tree using either a recursive
|
|
864
872
|
* or iterative approach in TypeScript.
|
|
865
|
-
* @param {
|
|
873
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter is the starting
|
|
866
874
|
* point from which the height of the binary tree will be calculated. It can be a node in the binary
|
|
867
875
|
* tree or a reference to the root of the tree. If not provided, it defaults to the root of the
|
|
868
876
|
* binary tree data structure.
|
|
@@ -907,7 +915,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
907
915
|
*
|
|
908
916
|
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
909
917
|
* recursive or iterative approach in TypeScript.
|
|
910
|
-
* @param {
|
|
918
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
|
|
911
919
|
* `getMinHeight` function represents the starting node from which the minimum height of the binary
|
|
912
920
|
* tree will be calculated. It is either a node in the binary tree or a reference to the root of the
|
|
913
921
|
* tree. If not provided, the default value is the root
|
|
@@ -973,7 +981,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
973
981
|
* the path to the root. It is expected to be a function that takes a node as an argument and returns
|
|
974
982
|
* a value based on that node. The return type of the callback function is determined by the generic
|
|
975
983
|
* type `C
|
|
976
|
-
* @param {
|
|
984
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } beginNode - The `beginNode` parameter in the
|
|
977
985
|
* `getPathToRoot` function can be either a key, a node, an entry, or any other value of type `R`.
|
|
978
986
|
* @param [isReverse=true] - The `isReverse` parameter in the `getPathToRoot` function determines
|
|
979
987
|
* whether the resulting path from the given `beginNode` to the root should be in reverse order or
|
|
@@ -1005,7 +1013,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1005
1013
|
* @param {C} callback - The `callback` parameter is a function that will be called with the leftmost
|
|
1006
1014
|
* node of a binary tree or with `undefined` if the tree is empty. It is provided with a default
|
|
1007
1015
|
* value of `_DEFAULT_NODE_CALLBACK` if not specified.
|
|
1008
|
-
* @param {
|
|
1016
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
|
|
1009
1017
|
* `getLeftMost` function represents the starting point for finding the leftmost node in a binary
|
|
1010
1018
|
* tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
|
|
1011
1019
|
* starting point is provided, the function will default
|
|
@@ -1051,7 +1059,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1051
1059
|
* of finding the rightmost node in a binary tree. It is of type `NodeCallback<OptNodeOrNull<BinaryTreeNode<K, V>>>`,
|
|
1052
1060
|
* which means it is a callback function that can accept either an optional binary tree node or null
|
|
1053
1061
|
* as
|
|
1054
|
-
* @param {
|
|
1062
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
|
|
1055
1063
|
* `getRightMost` function represents the starting point for finding the rightmost node in a binary
|
|
1056
1064
|
* tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
|
|
1057
1065
|
* starting point is provided, the function will default
|
|
@@ -1145,33 +1153,35 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1145
1153
|
* Time complexity: O(n)
|
|
1146
1154
|
* Space complexity: O(n)
|
|
1147
1155
|
*
|
|
1148
|
-
* The function
|
|
1149
|
-
*
|
|
1150
|
-
* @param {C} callback - The `callback` parameter is a
|
|
1151
|
-
*
|
|
1152
|
-
*
|
|
1153
|
-
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `dfs`
|
|
1154
|
-
* order in which the
|
|
1155
|
-
*
|
|
1156
|
-
* @param {
|
|
1157
|
-
*
|
|
1158
|
-
*
|
|
1159
|
-
*
|
|
1160
|
-
* @param {
|
|
1161
|
-
*
|
|
1162
|
-
*
|
|
1163
|
-
*
|
|
1164
|
-
*
|
|
1165
|
-
*
|
|
1166
|
-
* `
|
|
1167
|
-
*
|
|
1168
|
-
*
|
|
1156
|
+
* The function performs a depth-first search on a binary tree structure based on the specified
|
|
1157
|
+
* parameters.
|
|
1158
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
1159
|
+
* visited during the depth-first search. It should accept a `BinaryTreeNode` as an argument and
|
|
1160
|
+
* return an optional node or null. The default value for this parameter is `_DEFAULT_NODE_CALLBACK`.
|
|
1161
|
+
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `dfs` function specifies
|
|
1162
|
+
* the order in which the nodes are visited during a depth-first search traversal. The possible
|
|
1163
|
+
* values for the `pattern` parameter are:
|
|
1164
|
+
* @param {boolean} [onlyOne=false] - The `onlyOne` parameter in the `dfs` function is a boolean flag
|
|
1165
|
+
* that determines whether the depth-first search should stop after finding the first matching node
|
|
1166
|
+
* or continue searching for all matching nodes. If `onlyOne` is set to `true`, the search will stop
|
|
1167
|
+
* after finding the first matching node
|
|
1168
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined}
|
|
1169
|
+
* startNode - The `startNode` parameter in the `dfs` function can be one of the following types:
|
|
1170
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `dfs` function
|
|
1171
|
+
* specifies the type of iteration to be performed during the Depth-First Search traversal. It is
|
|
1172
|
+
* used to determine the order in which nodes are visited during the traversal. The possible values
|
|
1173
|
+
* for `iterationType` are typically defined as an enum or a
|
|
1174
|
+
* @param [includeNull=false] - The `includeNull` parameter in the `dfs` function determines whether
|
|
1175
|
+
* null nodes should be included in the depth-first search traversal. If `includeNull` is set to
|
|
1176
|
+
* `true`, null nodes will be included in the traversal process. If it is set to `false`, null nodes
|
|
1177
|
+
* will be skipped
|
|
1178
|
+
* @returns The `dfs` method is returning an array of the return type of the callback function `C`.
|
|
1169
1179
|
*/
|
|
1170
|
-
dfs(callback = this._DEFAULT_NODE_CALLBACK, pattern = 'IN', startNode = this._root, iterationType = this.iterationType, includeNull = false) {
|
|
1180
|
+
dfs(callback = this._DEFAULT_NODE_CALLBACK, pattern = 'IN', onlyOne = false, startNode = this._root, iterationType = this.iterationType, includeNull = false) {
|
|
1171
1181
|
startNode = this.ensureNode(startNode);
|
|
1172
1182
|
if (!startNode)
|
|
1173
1183
|
return [];
|
|
1174
|
-
return this._dfs(callback, pattern, startNode, iterationType, includeNull);
|
|
1184
|
+
return this._dfs(callback, pattern, onlyOne, startNode, iterationType, includeNull);
|
|
1175
1185
|
}
|
|
1176
1186
|
/**
|
|
1177
1187
|
* Time complexity: O(n)
|
|
@@ -1182,7 +1192,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1182
1192
|
* @param {C} callback - The `callback` parameter in the `bfs` function is a function that will be
|
|
1183
1193
|
* called on each node visited during the breadth-first search traversal. It is a generic type `C`
|
|
1184
1194
|
* that extends the `NodeCallback` type, which takes a parameter of type `BinaryTreeNode<K, V>` or `null`.
|
|
1185
|
-
* @param {
|
|
1195
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `bfs`
|
|
1186
1196
|
* function represents the starting point for the breadth-first search traversal in a binary tree. It
|
|
1187
1197
|
* can be specified as a key, node, or entry in the binary tree structure. If not provided, the
|
|
1188
1198
|
* default value is the root node of the binary
|
|
@@ -1258,7 +1268,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1258
1268
|
* structure based on a specified callback and iteration type.
|
|
1259
1269
|
* @param {C} callback - The `callback` parameter is a function that will be called on each leaf node
|
|
1260
1270
|
* in the binary tree. It is optional and defaults to a default callback function if not provided.
|
|
1261
|
-
* @param {
|
|
1271
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `leaves`
|
|
1262
1272
|
* method is used to specify the starting point for finding and processing the leaves of a binary
|
|
1263
1273
|
* tree. It can be provided as either a key, a node, or an entry in the binary tree structure. If not
|
|
1264
1274
|
* explicitly provided, the default value
|
|
@@ -1313,7 +1323,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1313
1323
|
* @param {C} callback - The `callback` parameter is a function that will be applied to each node in
|
|
1314
1324
|
* the binary tree during the traversal. It is used to process each node and determine what
|
|
1315
1325
|
* information to include in the output for each level of the tree.
|
|
1316
|
-
* @param {
|
|
1326
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
|
|
1317
1327
|
* `listLevels` function represents the starting point for traversing the binary tree. It can be
|
|
1318
1328
|
* either a key, a node, or an entry in the binary tree. If not provided, the default value is the
|
|
1319
1329
|
* root of the binary tree.
|
|
@@ -1385,11 +1395,11 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1385
1395
|
* Morris Traversal algorithm with different order patterns.
|
|
1386
1396
|
* @param {C} callback - The `callback` parameter in the `morris` function is a function that will be
|
|
1387
1397
|
* called on each node in the binary tree during the traversal. It is of type `C`, which extends the
|
|
1388
|
-
* `NodeCallback<BinaryTreeNode<K, V
|
|
1398
|
+
* `NodeCallback<BinaryTreeNode<K, V> | null>` type. The default value for `callback` is `this._DEFAULT
|
|
1389
1399
|
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function specifies
|
|
1390
1400
|
* the type of Depth-First Search (DFS) order pattern to traverse the binary tree. The possible
|
|
1391
1401
|
* values for the `pattern` parameter are:
|
|
1392
|
-
* @param {
|
|
1402
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `morris`
|
|
1393
1403
|
* function is the starting point for the Morris traversal algorithm. It represents the root node of
|
|
1394
1404
|
* the binary tree or the node from which the traversal should begin. It can be provided as either a
|
|
1395
1405
|
* key, a node, an entry, or a reference
|
|
@@ -1498,20 +1508,6 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1498
1508
|
this._clone(cloned);
|
|
1499
1509
|
return cloned;
|
|
1500
1510
|
}
|
|
1501
|
-
_clone(cloned) {
|
|
1502
|
-
this.bfs(node => {
|
|
1503
|
-
if (node === null)
|
|
1504
|
-
cloned.add(null);
|
|
1505
|
-
else {
|
|
1506
|
-
if (this._isMapMode)
|
|
1507
|
-
cloned.add([node.key, this._store.get(node.key)]);
|
|
1508
|
-
else
|
|
1509
|
-
cloned.add([node.key, node.value]);
|
|
1510
|
-
}
|
|
1511
|
-
}, this._root, this.iterationType, true);
|
|
1512
|
-
if (this._isMapMode)
|
|
1513
|
-
cloned._store = this._store;
|
|
1514
|
-
}
|
|
1515
1511
|
/**
|
|
1516
1512
|
* Time Complexity: O(n)
|
|
1517
1513
|
* Space Complexity: O(n)
|
|
@@ -1571,7 +1567,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1571
1567
|
*
|
|
1572
1568
|
* The function `toVisual` in TypeScript overrides the visual representation of a binary tree with
|
|
1573
1569
|
* customizable options for displaying undefined, null, and sentinel nodes.
|
|
1574
|
-
* @param {
|
|
1570
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
|
|
1575
1571
|
* `toVisual` method is used to specify the starting point for visualizing the binary tree structure.
|
|
1576
1572
|
* It can be a node, key, entry, or the root of the tree. If no specific starting point is provided,
|
|
1577
1573
|
* the default is set to the root
|
|
@@ -1616,7 +1612,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1616
1612
|
* printing options for the binary tree. It is an optional parameter that allows you to customize how
|
|
1617
1613
|
* the binary tree is printed, such as choosing between different traversal orders or formatting
|
|
1618
1614
|
* options.
|
|
1619
|
-
* @param {
|
|
1615
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
|
|
1620
1616
|
* `override print` method is used to specify the starting point for printing the binary tree. It can
|
|
1621
1617
|
* be either a key, a node, an entry, or the root of the tree. If no specific starting point is
|
|
1622
1618
|
* provided, the default value is set to
|
|
@@ -1624,21 +1620,35 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1624
1620
|
print(options, startNode = this._root) {
|
|
1625
1621
|
console.log(this.toVisual(startNode, options));
|
|
1626
1622
|
}
|
|
1623
|
+
_clone(cloned) {
|
|
1624
|
+
this.bfs(node => {
|
|
1625
|
+
if (node === null)
|
|
1626
|
+
cloned.add(null);
|
|
1627
|
+
else {
|
|
1628
|
+
if (this._isMapMode)
|
|
1629
|
+
cloned.add([node.key, this._store.get(node.key)]);
|
|
1630
|
+
else
|
|
1631
|
+
cloned.add([node.key, node.value]);
|
|
1632
|
+
}
|
|
1633
|
+
}, this._root, this.iterationType, true);
|
|
1634
|
+
if (this._isMapMode)
|
|
1635
|
+
cloned._store = this._store;
|
|
1636
|
+
}
|
|
1627
1637
|
/**
|
|
1628
1638
|
* Time Complexity: O(1)
|
|
1629
1639
|
* Space Complexity: O(1)
|
|
1630
1640
|
*
|
|
1631
1641
|
* The function `keyValueNodeEntryRawToNodeAndValue` converts various input types into a node object
|
|
1632
1642
|
* or returns null.
|
|
1633
|
-
* @param {
|
|
1643
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The
|
|
1634
1644
|
* `keyValueNodeEntryRawToNodeAndValue` function takes in a parameter `keyNodeOrEntry`, which
|
|
1635
|
-
* can be of type `
|
|
1645
|
+
* can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. This parameter represents either a key, a
|
|
1636
1646
|
* node, an entry
|
|
1637
1647
|
* @param {V} [value] - The `value` parameter in the `keyValueNodeEntryRawToNodeAndValue` function is
|
|
1638
1648
|
* an optional parameter of type `V`. It represents the value associated with the key in the node
|
|
1639
1649
|
* being created. If a `value` is provided, it will be used when creating the node. If
|
|
1640
1650
|
* @returns The `keyValueNodeEntryRawToNodeAndValue` function returns an optional node
|
|
1641
|
-
* (`
|
|
1651
|
+
* (`BinaryTreeNode<K, V> | null | undefined`) based on the input parameters provided. The function checks the type of the
|
|
1642
1652
|
* input parameter (`keyNodeOrEntry`) and processes it accordingly to return a node or null
|
|
1643
1653
|
* value.
|
|
1644
1654
|
*/
|
|
@@ -1664,45 +1674,48 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1664
1674
|
* Time complexity: O(n)
|
|
1665
1675
|
* Space complexity: O(n)
|
|
1666
1676
|
*
|
|
1667
|
-
* The `_dfs` function performs a depth-first search traversal on a binary tree
|
|
1668
|
-
*
|
|
1677
|
+
* The `_dfs` function performs a depth-first search traversal on a binary tree, with customizable
|
|
1678
|
+
* options for traversal order and node processing.
|
|
1669
1679
|
* @param {C} callback - The `callback` parameter in the `_dfs` method is a function that will be
|
|
1670
|
-
* called on each node visited during the depth-first search traversal. It is
|
|
1671
|
-
* extends `NodeCallback<
|
|
1680
|
+
* called on each node visited during the depth-first search traversal. It is a generic type `C` that
|
|
1681
|
+
* extends `NodeCallback<BinaryTreeNode<K, V> | null>`. The default value for `callback`
|
|
1672
1682
|
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `_dfs` method specifies the
|
|
1673
|
-
* order in which the nodes are visited during
|
|
1674
|
-
*
|
|
1675
|
-
* @param {
|
|
1676
|
-
*
|
|
1677
|
-
*
|
|
1678
|
-
*
|
|
1683
|
+
* order in which the nodes are visited during a depth-first search traversal. It can have one of the
|
|
1684
|
+
* following values:
|
|
1685
|
+
* @param {boolean} [onlyOne=false] - The `onlyOne` parameter in the `_dfs` method is a boolean flag
|
|
1686
|
+
* that determines whether the traversal should stop after processing a single node. If `onlyOne` is
|
|
1687
|
+
* set to `true`, the traversal will return as soon as a single node is processed. If it is set to
|
|
1688
|
+
* `false
|
|
1689
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined}
|
|
1690
|
+
* startNode - The `startNode` parameter in the `_dfs` method is used to specify the starting node
|
|
1691
|
+
* for the depth-first search traversal. It can be provided in different forms:
|
|
1679
1692
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `_dfs` method
|
|
1680
|
-
* specifies the
|
|
1681
|
-
*
|
|
1682
|
-
* @param [includeNull=false] - The `includeNull` parameter in the `_dfs` method
|
|
1683
|
-
*
|
|
1684
|
-
*
|
|
1685
|
-
*
|
|
1686
|
-
* @param shouldVisitLeft - The `shouldVisitLeft` parameter
|
|
1687
|
-
*
|
|
1688
|
-
*
|
|
1689
|
-
*
|
|
1690
|
-
* @param shouldVisitRight - The `shouldVisitRight` parameter
|
|
1691
|
-
*
|
|
1692
|
-
*
|
|
1693
|
-
*
|
|
1694
|
-
* @param shouldVisitRoot - The `shouldVisitRoot` parameter
|
|
1695
|
-
*
|
|
1696
|
-
*
|
|
1697
|
-
*
|
|
1698
|
-
* @param shouldProcessRoot - The `shouldProcessRoot` parameter
|
|
1699
|
-
*
|
|
1700
|
-
*
|
|
1701
|
-
*
|
|
1702
|
-
* @returns The
|
|
1703
|
-
*
|
|
1693
|
+
* specifies whether the traversal should be done recursively or iteratively. It can have two
|
|
1694
|
+
* possible values:
|
|
1695
|
+
* @param [includeNull=false] - The `includeNull` parameter in the `_dfs` method determines whether
|
|
1696
|
+
* null nodes should be included in the traversal process. If `includeNull` is set to `true`, the
|
|
1697
|
+
* method will consider null nodes as valid nodes to visit or process. If `includeNull` is set to
|
|
1698
|
+
* `false`,
|
|
1699
|
+
* @param shouldVisitLeft - The `shouldVisitLeft` parameter in the `_dfs` method is a function that
|
|
1700
|
+
* determines whether the left child of a node should be visited during the Depth-First Search
|
|
1701
|
+
* traversal. By default, it checks if the node is not null or undefined before visiting the left
|
|
1702
|
+
* child. You can customize this behavior
|
|
1703
|
+
* @param shouldVisitRight - The `shouldVisitRight` parameter in the `_dfs` method is a function that
|
|
1704
|
+
* determines whether to visit the right child node of the current node during a depth-first search
|
|
1705
|
+
* traversal. The default implementation of this function checks if the node is not null or undefined
|
|
1706
|
+
* before deciding to visit it.
|
|
1707
|
+
* @param shouldVisitRoot - The `shouldVisitRoot` parameter in the `_dfs` method is a function that
|
|
1708
|
+
* determines whether a given node should be visited during the depth-first search traversal. The
|
|
1709
|
+
* function takes a node as an argument and returns a boolean value indicating whether the node
|
|
1710
|
+
* should be visited.
|
|
1711
|
+
* @param shouldProcessRoot - The `shouldProcessRoot` parameter in the `_dfs` method is a function
|
|
1712
|
+
* that determines whether the root node should be processed during the Depth-First Search traversal.
|
|
1713
|
+
* It takes a node (BinaryTreeNode<K, V> | null | undefined) as input and returns a boolean value. If
|
|
1714
|
+
* the function
|
|
1715
|
+
* @returns The `_dfs` method returns an array of the return type of the provided callback function
|
|
1716
|
+
* `C`.
|
|
1704
1717
|
*/
|
|
1705
|
-
_dfs(callback = this._DEFAULT_NODE_CALLBACK, pattern = 'IN', startNode = this._root, iterationType = this.iterationType, includeNull = false, shouldVisitLeft = node => !!node, shouldVisitRight = node => !!node, shouldVisitRoot = node => {
|
|
1718
|
+
_dfs(callback = this._DEFAULT_NODE_CALLBACK, pattern = 'IN', onlyOne = false, startNode = this._root, iterationType = this.iterationType, includeNull = false, shouldVisitLeft = node => !!node, shouldVisitRight = node => !!node, shouldVisitRoot = node => {
|
|
1706
1719
|
if (includeNull)
|
|
1707
1720
|
return this.isRealNodeOrNull(node);
|
|
1708
1721
|
return this.isRealNode(node);
|
|
@@ -1716,31 +1729,40 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1716
1729
|
if (!shouldVisitRoot(node))
|
|
1717
1730
|
return;
|
|
1718
1731
|
const visitLeft = () => {
|
|
1719
|
-
if (shouldVisitLeft(node))
|
|
1732
|
+
if (shouldVisitLeft(node) && node?.left !== undefined)
|
|
1720
1733
|
dfs(node?.left);
|
|
1721
1734
|
};
|
|
1722
1735
|
const visitRight = () => {
|
|
1723
|
-
if (shouldVisitRight(node))
|
|
1736
|
+
if (shouldVisitRight(node) && node?.right !== undefined)
|
|
1724
1737
|
dfs(node?.right);
|
|
1725
1738
|
};
|
|
1726
1739
|
switch (pattern) {
|
|
1727
1740
|
case 'IN':
|
|
1728
1741
|
visitLeft();
|
|
1729
|
-
if (shouldProcessRoot(node))
|
|
1742
|
+
if (shouldProcessRoot(node)) {
|
|
1730
1743
|
ans.push(callback(node));
|
|
1744
|
+
if (onlyOne)
|
|
1745
|
+
return;
|
|
1746
|
+
}
|
|
1731
1747
|
visitRight();
|
|
1732
1748
|
break;
|
|
1733
1749
|
case 'PRE':
|
|
1734
|
-
if (shouldProcessRoot(node))
|
|
1750
|
+
if (shouldProcessRoot(node)) {
|
|
1735
1751
|
ans.push(callback(node));
|
|
1752
|
+
if (onlyOne)
|
|
1753
|
+
return;
|
|
1754
|
+
}
|
|
1736
1755
|
visitLeft();
|
|
1737
1756
|
visitRight();
|
|
1738
1757
|
break;
|
|
1739
1758
|
case 'POST':
|
|
1740
1759
|
visitLeft();
|
|
1741
1760
|
visitRight();
|
|
1742
|
-
if (shouldProcessRoot(node))
|
|
1761
|
+
if (shouldProcessRoot(node)) {
|
|
1743
1762
|
ans.push(callback(node));
|
|
1763
|
+
if (onlyOne)
|
|
1764
|
+
return;
|
|
1765
|
+
}
|
|
1744
1766
|
break;
|
|
1745
1767
|
}
|
|
1746
1768
|
};
|
|
@@ -1767,8 +1789,11 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1767
1789
|
if (!shouldVisitRoot(cur.node))
|
|
1768
1790
|
continue;
|
|
1769
1791
|
if (cur.opt === DFSOperation.PROCESS) {
|
|
1770
|
-
if (shouldProcessRoot(cur.node))
|
|
1792
|
+
if (shouldProcessRoot(cur.node) && cur.node !== undefined) {
|
|
1771
1793
|
ans.push(callback(cur.node));
|
|
1794
|
+
if (onlyOne)
|
|
1795
|
+
return ans;
|
|
1796
|
+
}
|
|
1772
1797
|
}
|
|
1773
1798
|
else {
|
|
1774
1799
|
switch (pattern) {
|
|
@@ -1915,12 +1940,12 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1915
1940
|
* Space Complexity: O(1)
|
|
1916
1941
|
*
|
|
1917
1942
|
* The _swapProperties function swaps key and value properties between two nodes in a binary tree.
|
|
1918
|
-
* @param {
|
|
1943
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } srcNode - The `srcNode` parameter in the
|
|
1919
1944
|
* `_swapProperties` method can be either a BTNRep object containing key and value
|
|
1920
1945
|
* properties, or it can be of type R.
|
|
1921
|
-
* @param {
|
|
1946
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } destNode - The `destNode` parameter in the
|
|
1922
1947
|
* `_swapProperties` method represents the node or entry where the properties will be swapped with
|
|
1923
|
-
* the `srcNode`. It can be of type `
|
|
1948
|
+
* the `srcNode`. It can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. The method ensures that
|
|
1924
1949
|
* both `srcNode
|
|
1925
1950
|
* @returns The `_swapProperties` method returns either the `destNode` with its key and value swapped
|
|
1926
1951
|
* with the `srcNode`, or `undefined` if either `srcNode` or `destNode` is falsy.
|
|
@@ -1980,7 +2005,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1980
2005
|
*
|
|
1981
2006
|
* The function _setRoot sets the root node of a data structure while updating the parent reference
|
|
1982
2007
|
* of the previous root node.
|
|
1983
|
-
* @param v - The parameter `v` in the `_setRoot` method is of type `
|
|
2008
|
+
* @param v - The parameter `v` in the `_setRoot` method is of type `BinaryTreeNode<K, V> | null | undefined`, which means
|
|
1984
2009
|
* it can either be an optional `BinaryTreeNode<K, V>` type or `null`.
|
|
1985
2010
|
*/
|
|
1986
2011
|
_setRoot(v) {
|
|
@@ -1995,7 +2020,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1995
2020
|
*
|
|
1996
2021
|
* The function `_ensurePredicate` in TypeScript ensures that the input is converted into a valid
|
|
1997
2022
|
* predicate function for a binary tree node.
|
|
1998
|
-
* @param {
|
|
2023
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate - The
|
|
1999
2024
|
* `_ensurePredicate` method in the provided code snippet is responsible for ensuring that the input
|
|
2000
2025
|
* parameter `keyNodeEntryOrPredicate` is transformed into a valid predicate function that can be
|
|
2001
2026
|
* used for filtering nodes in a binary tree.
|
|
@@ -2010,9 +2035,17 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
2010
2035
|
return (node) => node === keyNodeEntryOrPredicate;
|
|
2011
2036
|
if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
2012
2037
|
const [key] = keyNodeEntryOrPredicate;
|
|
2013
|
-
return (node) =>
|
|
2038
|
+
return (node) => {
|
|
2039
|
+
if (!node)
|
|
2040
|
+
return false;
|
|
2041
|
+
return node.key === key;
|
|
2042
|
+
};
|
|
2014
2043
|
}
|
|
2015
|
-
return (node) =>
|
|
2044
|
+
return (node) => {
|
|
2045
|
+
if (!node)
|
|
2046
|
+
return false;
|
|
2047
|
+
return node.key === keyNodeEntryOrPredicate;
|
|
2048
|
+
};
|
|
2016
2049
|
}
|
|
2017
2050
|
/**
|
|
2018
2051
|
* Time Complexity: O(1)
|
|
@@ -2035,8 +2068,8 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
2035
2068
|
*
|
|
2036
2069
|
* The function `_extractKey` in TypeScript returns the key from a given input, which can be a node,
|
|
2037
2070
|
* entry, raw data, or null/undefined.
|
|
2038
|
-
* @param {
|
|
2039
|
-
* TypeScript method that takes in a parameter `keyNodeOrEntry` of type `
|
|
2071
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `_extractKey` method you provided is a
|
|
2072
|
+
* TypeScript method that takes in a parameter `keyNodeOrEntry` of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `,
|
|
2040
2073
|
* where `BTNRep` is a generic type with keys `K`, `V`, and `BinaryTreeNode<K, V>`, and `
|
|
2041
2074
|
* @returns The `_extractKey` method returns the key value extracted from the `keyNodeOrEntry`
|
|
2042
2075
|
* parameter. The return value can be a key value of type `K`, `null`, or `undefined`, depending on
|