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
|
@@ -98,7 +98,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
98
98
|
* This TypeScript constructor function initializes a binary tree with optional options and adds
|
|
99
99
|
* elements based on the provided input.
|
|
100
100
|
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
|
|
101
|
-
* iterable that can contain either objects of type `
|
|
101
|
+
* iterable that can contain either objects of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. It
|
|
102
102
|
* is used to initialize the binary tree with keys, nodes, entries, or raw data.
|
|
103
103
|
* @param [options] - The `options` parameter in the constructor is an optional object that can
|
|
104
104
|
* contain the following properties:
|
|
@@ -107,16 +107,19 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
107
107
|
super();
|
|
108
108
|
this.iterationType = 'ITERATIVE';
|
|
109
109
|
this._isMapMode = true;
|
|
110
|
+
this._isDuplicate = false;
|
|
110
111
|
this._store = new Map();
|
|
111
112
|
this._size = 0;
|
|
112
113
|
this._NIL = new BinaryTreeNode(NaN);
|
|
113
114
|
this._DEFAULT_NODE_CALLBACK = (node) => (node ? node.key : undefined);
|
|
114
115
|
if (options) {
|
|
115
|
-
const { iterationType, toEntryFn, isMapMode } = options;
|
|
116
|
+
const { iterationType, toEntryFn, isMapMode, isDuplicate } = options;
|
|
116
117
|
if (iterationType)
|
|
117
118
|
this.iterationType = iterationType;
|
|
118
119
|
if (isMapMode !== undefined)
|
|
119
120
|
this._isMapMode = isMapMode;
|
|
121
|
+
if (isDuplicate !== undefined)
|
|
122
|
+
this._isDuplicate = isDuplicate;
|
|
120
123
|
if (typeof toEntryFn === 'function')
|
|
121
124
|
this._toEntryFn = toEntryFn;
|
|
122
125
|
else if (toEntryFn)
|
|
@@ -128,6 +131,9 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
128
131
|
get isMapMode() {
|
|
129
132
|
return this._isMapMode;
|
|
130
133
|
}
|
|
134
|
+
get isDuplicate() {
|
|
135
|
+
return this._isDuplicate;
|
|
136
|
+
}
|
|
131
137
|
get store() {
|
|
132
138
|
return this._store;
|
|
133
139
|
}
|
|
@@ -178,8 +184,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
178
184
|
*
|
|
179
185
|
* The function `ensureNode` in TypeScript checks if a given input is a node, entry, key, or raw
|
|
180
186
|
* value and returns the corresponding node or null.
|
|
181
|
-
* @param {
|
|
182
|
-
* parameter in the `ensureNode` function can be of type `
|
|
187
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `keyNodeOrEntry`
|
|
188
|
+
* parameter in the `ensureNode` function can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. It
|
|
183
189
|
* is used to determine whether the input is a key, node, entry, or raw data. The
|
|
184
190
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `ensureNode` function
|
|
185
191
|
* is used to specify the type of iteration to be performed. It has a default value of
|
|
@@ -211,7 +217,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
211
217
|
* Space Complexity: O(1)
|
|
212
218
|
*
|
|
213
219
|
* The function isNode checks if the input is an instance of BinaryTreeNode.
|
|
214
|
-
* @param {
|
|
220
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter
|
|
215
221
|
* `keyNodeOrEntry` can be either a key, a node, an entry, or raw data. The function is
|
|
216
222
|
* checking if the input is an instance of a `BinaryTreeNode` and returning a boolean value
|
|
217
223
|
* accordingly.
|
|
@@ -228,7 +234,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
228
234
|
* Space Complexity: O(1)
|
|
229
235
|
*
|
|
230
236
|
* The function `isRaw` checks if the input parameter is of type `R` by verifying if it is an object.
|
|
231
|
-
* @param {
|
|
237
|
+
* @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
|
|
232
238
|
* @returns The function `isRaw` is checking if the `keyNodeEntryOrRaw` parameter is of type `R` by
|
|
233
239
|
* checking if it is an object. If the parameter is an object, the function will return `true`,
|
|
234
240
|
* indicating that it is of type `R`.
|
|
@@ -241,8 +247,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
241
247
|
* Space Complexity: O(1)
|
|
242
248
|
*
|
|
243
249
|
* The function `isRealNode` checks if a given input is a valid node in a binary tree.
|
|
244
|
-
* @param {
|
|
245
|
-
* parameter in the `isRealNode` function can be of type `
|
|
250
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `keyNodeOrEntry`
|
|
251
|
+
* parameter in the `isRealNode` function can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`.
|
|
246
252
|
* The function checks if the input parameter is a `BinaryTreeNode<K, V>` type by verifying if it is not equal
|
|
247
253
|
* @returns The function `isRealNode` is checking if the input `keyNodeOrEntry` is a valid
|
|
248
254
|
* node by comparing it to `this._NIL`, `null`, and `undefined`. If the input is not one of these
|
|
@@ -259,7 +265,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
259
265
|
* Space Complexity: O(1)
|
|
260
266
|
*
|
|
261
267
|
* The function checks if a given input is a valid node or null.
|
|
262
|
-
* @param {
|
|
268
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter
|
|
263
269
|
* `keyNodeOrEntry` in the `isRealNodeOrNull` function can be of type `BTNRep<K,
|
|
264
270
|
* V, BinaryTreeNode<K, V>>` or `R`. It is a union type that can either be a key, a node, an entry, or
|
|
265
271
|
* @returns The function `isRealNodeOrNull` is returning a boolean value. It checks if the input
|
|
@@ -274,7 +280,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
274
280
|
* Space Complexity: O(1)
|
|
275
281
|
*
|
|
276
282
|
* The function isNIL checks if a given key, node, entry, or raw value is equal to the _NIL value.
|
|
277
|
-
* @param {
|
|
283
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - BTNRep<K, V,
|
|
278
284
|
* BinaryTreeNode<K, V>>
|
|
279
285
|
* @returns The function is checking if the `keyNodeOrEntry` parameter is equal to the `_NIL`
|
|
280
286
|
* property of the current object and returning a boolean value based on that comparison.
|
|
@@ -287,9 +293,9 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
287
293
|
* Space Complexity: O(1)
|
|
288
294
|
*
|
|
289
295
|
* The function `isRange` checks if the input parameter is an instance of the `Range` class.
|
|
290
|
-
* @param {
|
|
291
|
-
*
|
|
292
|
-
* of type `
|
|
296
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>> | Range<K>} keyNodeEntryOrPredicate
|
|
297
|
+
* - The `keyNodeEntryOrPredicate` parameter in the `isRange` function can be
|
|
298
|
+
* of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `, `NodePredicate<BinaryTreeNode<K, V>>`, or
|
|
293
299
|
* `Range<K>`. The function checks if the `keyNodeEntry
|
|
294
300
|
* @returns The `isRange` function is checking if the `keyNodeEntryOrPredicate` parameter is an
|
|
295
301
|
* instance of the `Range` class. If it is an instance of `Range`, the function will return `true`,
|
|
@@ -305,8 +311,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
305
311
|
*
|
|
306
312
|
* The function determines whether a given key, node, entry, or raw data is a leaf node in a binary
|
|
307
313
|
* tree.
|
|
308
|
-
* @param {
|
|
309
|
-
* `keyNodeOrEntry` can be of type `
|
|
314
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter
|
|
315
|
+
* `keyNodeOrEntry` can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. It represents a
|
|
310
316
|
* key, node, entry, or raw data in a binary tree structure. The function `isLeaf` checks whether the
|
|
311
317
|
* provided
|
|
312
318
|
* @returns The function `isLeaf` returns a boolean value indicating whether the input
|
|
@@ -326,8 +332,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
326
332
|
*
|
|
327
333
|
* The function `isEntry` checks if the input is a BTNEntry object by verifying if it is an array
|
|
328
334
|
* with a length of 2.
|
|
329
|
-
* @param {
|
|
330
|
-
* parameter in the `isEntry` function can be of type `
|
|
335
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `keyNodeOrEntry`
|
|
336
|
+
* parameter in the `isEntry` function can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or type `R`.
|
|
331
337
|
* The function checks if the provided `keyNodeOrEntry` is of type `BTN
|
|
332
338
|
* @returns The `isEntry` function is checking if the `keyNodeOrEntry` parameter is an array
|
|
333
339
|
* with a length of 2. If it is, then it returns `true`, indicating that the parameter is of type
|
|
@@ -358,7 +364,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
358
364
|
*
|
|
359
365
|
* The `add` function in TypeScript adds a new node to a binary tree while handling duplicate keys
|
|
360
366
|
* and finding the correct insertion position.
|
|
361
|
-
* @param {
|
|
367
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `add` method you provided
|
|
362
368
|
* seems to be for adding a new node to a binary tree structure. The `keyNodeOrEntry`
|
|
363
369
|
* parameter in the method can accept different types of values:
|
|
364
370
|
* @param {V} [value] - The `value` parameter in the `add` method represents the value associated
|
|
@@ -387,12 +393,14 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
387
393
|
const cur = queue.shift();
|
|
388
394
|
if (!cur)
|
|
389
395
|
continue;
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
this.
|
|
395
|
-
|
|
396
|
+
if (!this._isDuplicate) {
|
|
397
|
+
// Check for duplicate keys when newNode is not null
|
|
398
|
+
if (newNode !== null && cur.key === newNode.key) {
|
|
399
|
+
this._replaceNode(cur, newNode);
|
|
400
|
+
if (this._isMapMode)
|
|
401
|
+
this._setValue(cur.key, newValue);
|
|
402
|
+
return true; // If duplicate keys are found, no insertion is performed
|
|
403
|
+
}
|
|
396
404
|
}
|
|
397
405
|
// Record the first possible insertion location found
|
|
398
406
|
if (potentialParent === undefined && (cur.left === undefined || cur.right === undefined)) {
|
|
@@ -432,7 +440,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
432
440
|
* each insertion was successful.
|
|
433
441
|
* @param keysNodesEntriesOrRaws - `keysNodesEntriesOrRaws` is an iterable that can contain a
|
|
434
442
|
* mix of keys, nodes, entries, or raw values. Each element in this iterable can be of type
|
|
435
|
-
* `
|
|
443
|
+
* `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`.
|
|
436
444
|
* @param [values] - The `values` parameter in the `addMany` function is an optional parameter that
|
|
437
445
|
* accepts an iterable of values. These values correspond to the keys or nodes being added in the
|
|
438
446
|
* `keysNodesEntriesOrRaws` parameter. If provided, the function will iterate over the values and
|
|
@@ -480,7 +488,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
480
488
|
* The `refill` function clears the existing data structure and then adds new key-value pairs based
|
|
481
489
|
* on the provided input.
|
|
482
490
|
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the `refill`
|
|
483
|
-
* method can accept an iterable containing a mix of `
|
|
491
|
+
* method can accept an iterable containing a mix of `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` objects or `R`
|
|
484
492
|
* objects.
|
|
485
493
|
* @param [values] - The `values` parameter in the `refill` method is an optional parameter that
|
|
486
494
|
* accepts an iterable of values of type `V` or `undefined`.
|
|
@@ -495,7 +503,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
495
503
|
*
|
|
496
504
|
* The function `delete` in TypeScript implements the deletion of a node in a binary tree and returns
|
|
497
505
|
* the deleted node along with information for tree balancing.
|
|
498
|
-
* @param {
|
|
506
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry
|
|
499
507
|
* - The `delete` method you provided is used to delete a node from a binary tree based on the key,
|
|
500
508
|
* node, entry or raw data. The method returns an array of
|
|
501
509
|
* `BinaryTreeDeleteResult` objects containing information about the deleted node and whether
|
|
@@ -557,15 +565,15 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
557
565
|
*
|
|
558
566
|
* The `search` function in TypeScript performs a depth-first or breadth-first search on a tree
|
|
559
567
|
* structure based on a given predicate or key, with options to return multiple results or just one.
|
|
560
|
-
* @param {
|
|
568
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate - The
|
|
561
569
|
* `keyNodeEntryOrPredicate` parameter in the `search` function can accept three types of values:
|
|
562
570
|
* @param [onlyOne=false] - The `onlyOne` parameter in the `search` function is a boolean flag that
|
|
563
571
|
* determines whether the search should stop after finding the first matching node. If `onlyOne` is
|
|
564
572
|
* set to `true`, the search will return as soon as a matching node is found. If `onlyOne` is
|
|
565
573
|
* @param {C} callback - The `callback` parameter in the `search` function is a callback function
|
|
566
574
|
* that will be called on each node that matches the search criteria. It is of type `C`, which
|
|
567
|
-
* extends `NodeCallback<BinaryTreeNode<K, V
|
|
568
|
-
* @param {
|
|
575
|
+
* extends `NodeCallback<BinaryTreeNode<K, V> | null>`. The default value for `callback` is `this._DEFAULT_NODE_CALLBACK` if
|
|
576
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `search` function is
|
|
569
577
|
* used to specify the node from which the search operation should begin. It represents the starting
|
|
570
578
|
* point in the binary tree where the search will be performed. If no specific `startNode` is
|
|
571
579
|
* provided, the search operation will start from the root
|
|
@@ -626,12 +634,12 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
626
634
|
*
|
|
627
635
|
* The function `getNodes` retrieves nodes from a binary tree based on a key, node, entry, raw data,
|
|
628
636
|
* or predicate, with options for recursive or iterative traversal.
|
|
629
|
-
* @param {
|
|
637
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
|
|
630
638
|
* - The `getNodes` function you provided takes several parameters:
|
|
631
639
|
* @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` function is a boolean flag that
|
|
632
640
|
* determines whether to return only the first node that matches the criteria specified by the
|
|
633
641
|
* `keyNodeEntryOrPredicate` parameter. If `onlyOne` is set to `true`, the function will
|
|
634
|
-
* @param {
|
|
642
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
|
|
635
643
|
* `getNodes` function is used to specify the starting point for traversing the binary tree. It
|
|
636
644
|
* represents the root node of the binary tree or the node from which the traversal should begin. If
|
|
637
645
|
* not provided, the default value is set to `this._root
|
|
@@ -650,10 +658,10 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
650
658
|
*
|
|
651
659
|
* The `getNode` function retrieves a node based on the provided key, node, entry, raw data, or
|
|
652
660
|
* predicate.
|
|
653
|
-
* @param {
|
|
661
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
|
|
654
662
|
* - The `keyNodeEntryOrPredicate` parameter in the `getNode` function can accept a key,
|
|
655
663
|
* node, entry, raw data, or a predicate function.
|
|
656
|
-
* @param {
|
|
664
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
|
|
657
665
|
* `getNode` function is used to specify the starting point for searching for a node in a binary
|
|
658
666
|
* tree. If no specific starting point is provided, the default value is set to `this._root`, which
|
|
659
667
|
* is typically the root node of the binary tree.
|
|
@@ -673,10 +681,10 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
673
681
|
*
|
|
674
682
|
* This function overrides the `get` method to retrieve the value associated with a specified key,
|
|
675
683
|
* node, entry, raw data, or predicate in a data structure.
|
|
676
|
-
* @param {
|
|
684
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
|
|
677
685
|
* - The `keyNodeEntryOrPredicate` parameter in the `get` method can accept one of the
|
|
678
686
|
* following types:
|
|
679
|
-
* @param {
|
|
687
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `get`
|
|
680
688
|
* method is used to specify the starting point for searching for a key or node in the binary tree.
|
|
681
689
|
* If no specific starting point is provided, the default starting point is the root of the binary
|
|
682
690
|
* tree (`this._root`).
|
|
@@ -705,10 +713,10 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
705
713
|
*
|
|
706
714
|
* The `has` function in TypeScript checks if a specified key, node, entry, raw data, or predicate
|
|
707
715
|
* exists in the data structure.
|
|
708
|
-
* @param {
|
|
716
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
|
|
709
717
|
* - The `keyNodeEntryOrPredicate` parameter in the `override has` method can accept one of
|
|
710
718
|
* the following types:
|
|
711
|
-
* @param {
|
|
719
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
|
|
712
720
|
* `override` method is used to specify the starting point for the search operation within the data
|
|
713
721
|
* structure. It defaults to `this._root` if not provided explicitly.
|
|
714
722
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `override has` method
|
|
@@ -752,7 +760,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
752
760
|
*
|
|
753
761
|
* The function checks if a binary tree is perfectly balanced by comparing its minimum height with
|
|
754
762
|
* its height.
|
|
755
|
-
* @param {
|
|
763
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter is the starting
|
|
756
764
|
* point for checking if the binary tree is perfectly balanced. It represents the root node of the
|
|
757
765
|
* binary tree or a specific node from which the balance check should begin.
|
|
758
766
|
* @returns The method `isPerfectlyBalanced` is returning a boolean value, which indicates whether
|
|
@@ -770,7 +778,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
770
778
|
*
|
|
771
779
|
* The function `isBST` in TypeScript checks if a binary search tree is valid using either recursive
|
|
772
780
|
* or iterative methods.
|
|
773
|
-
* @param {
|
|
781
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `isBST`
|
|
774
782
|
* function represents the starting point for checking whether a binary search tree (BST) is valid.
|
|
775
783
|
* It can be a node in the BST or a reference to the root of the BST. If no specific node is
|
|
776
784
|
* provided, the function will default to
|
|
@@ -829,10 +837,10 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
829
837
|
* Space Complexity: O(log n)
|
|
830
838
|
*
|
|
831
839
|
* The `getDepth` function calculates the depth between two nodes in a binary tree.
|
|
832
|
-
* @param {
|
|
840
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } dist - The `dist` parameter in the `getDepth`
|
|
833
841
|
* function represents the node or entry in a binary tree map, or a reference to a node in the tree.
|
|
834
842
|
* It is the target node for which you want to calculate the depth from the `startNode` node.
|
|
835
|
-
* @param {
|
|
843
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
|
|
836
844
|
* `getDepth` function represents the starting point from which you want to calculate the depth of a
|
|
837
845
|
* given node or entry in a binary tree. If no specific starting point is provided, the default value
|
|
838
846
|
* for `startNode` is set to the root of the binary
|
|
@@ -859,7 +867,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
859
867
|
*
|
|
860
868
|
* The `getHeight` function calculates the maximum height of a binary tree using either a recursive
|
|
861
869
|
* or iterative approach in TypeScript.
|
|
862
|
-
* @param {
|
|
870
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter is the starting
|
|
863
871
|
* point from which the height of the binary tree will be calculated. It can be a node in the binary
|
|
864
872
|
* tree or a reference to the root of the tree. If not provided, it defaults to the root of the
|
|
865
873
|
* binary tree data structure.
|
|
@@ -904,7 +912,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
904
912
|
*
|
|
905
913
|
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
906
914
|
* recursive or iterative approach in TypeScript.
|
|
907
|
-
* @param {
|
|
915
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
|
|
908
916
|
* `getMinHeight` function represents the starting node from which the minimum height of the binary
|
|
909
917
|
* tree will be calculated. It is either a node in the binary tree or a reference to the root of the
|
|
910
918
|
* tree. If not provided, the default value is the root
|
|
@@ -970,7 +978,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
970
978
|
* the path to the root. It is expected to be a function that takes a node as an argument and returns
|
|
971
979
|
* a value based on that node. The return type of the callback function is determined by the generic
|
|
972
980
|
* type `C
|
|
973
|
-
* @param {
|
|
981
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } beginNode - The `beginNode` parameter in the
|
|
974
982
|
* `getPathToRoot` function can be either a key, a node, an entry, or any other value of type `R`.
|
|
975
983
|
* @param [isReverse=true] - The `isReverse` parameter in the `getPathToRoot` function determines
|
|
976
984
|
* whether the resulting path from the given `beginNode` to the root should be in reverse order or
|
|
@@ -1002,7 +1010,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1002
1010
|
* @param {C} callback - The `callback` parameter is a function that will be called with the leftmost
|
|
1003
1011
|
* node of a binary tree or with `undefined` if the tree is empty. It is provided with a default
|
|
1004
1012
|
* value of `_DEFAULT_NODE_CALLBACK` if not specified.
|
|
1005
|
-
* @param {
|
|
1013
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
|
|
1006
1014
|
* `getLeftMost` function represents the starting point for finding the leftmost node in a binary
|
|
1007
1015
|
* tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
|
|
1008
1016
|
* starting point is provided, the function will default
|
|
@@ -1048,7 +1056,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1048
1056
|
* of finding the rightmost node in a binary tree. It is of type `NodeCallback<OptNodeOrNull<BinaryTreeNode<K, V>>>`,
|
|
1049
1057
|
* which means it is a callback function that can accept either an optional binary tree node or null
|
|
1050
1058
|
* as
|
|
1051
|
-
* @param {
|
|
1059
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
|
|
1052
1060
|
* `getRightMost` function represents the starting point for finding the rightmost node in a binary
|
|
1053
1061
|
* tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
|
|
1054
1062
|
* starting point is provided, the function will default
|
|
@@ -1142,33 +1150,35 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1142
1150
|
* Time complexity: O(n)
|
|
1143
1151
|
* Space complexity: O(n)
|
|
1144
1152
|
*
|
|
1145
|
-
* The function
|
|
1146
|
-
*
|
|
1147
|
-
* @param {C} callback - The `callback` parameter is a
|
|
1148
|
-
*
|
|
1149
|
-
*
|
|
1150
|
-
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `dfs`
|
|
1151
|
-
* order in which the
|
|
1152
|
-
*
|
|
1153
|
-
* @param {
|
|
1154
|
-
*
|
|
1155
|
-
*
|
|
1156
|
-
*
|
|
1157
|
-
* @param {
|
|
1158
|
-
*
|
|
1159
|
-
*
|
|
1160
|
-
*
|
|
1161
|
-
*
|
|
1162
|
-
*
|
|
1163
|
-
* `
|
|
1164
|
-
*
|
|
1165
|
-
*
|
|
1153
|
+
* The function performs a depth-first search on a binary tree structure based on the specified
|
|
1154
|
+
* parameters.
|
|
1155
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
1156
|
+
* visited during the depth-first search. It should accept a `BinaryTreeNode` as an argument and
|
|
1157
|
+
* return an optional node or null. The default value for this parameter is `_DEFAULT_NODE_CALLBACK`.
|
|
1158
|
+
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `dfs` function specifies
|
|
1159
|
+
* the order in which the nodes are visited during a depth-first search traversal. The possible
|
|
1160
|
+
* values for the `pattern` parameter are:
|
|
1161
|
+
* @param {boolean} [onlyOne=false] - The `onlyOne` parameter in the `dfs` function is a boolean flag
|
|
1162
|
+
* that determines whether the depth-first search should stop after finding the first matching node
|
|
1163
|
+
* or continue searching for all matching nodes. If `onlyOne` is set to `true`, the search will stop
|
|
1164
|
+
* after finding the first matching node
|
|
1165
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined}
|
|
1166
|
+
* startNode - The `startNode` parameter in the `dfs` function can be one of the following types:
|
|
1167
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `dfs` function
|
|
1168
|
+
* specifies the type of iteration to be performed during the Depth-First Search traversal. It is
|
|
1169
|
+
* used to determine the order in which nodes are visited during the traversal. The possible values
|
|
1170
|
+
* for `iterationType` are typically defined as an enum or a
|
|
1171
|
+
* @param [includeNull=false] - The `includeNull` parameter in the `dfs` function determines whether
|
|
1172
|
+
* null nodes should be included in the depth-first search traversal. If `includeNull` is set to
|
|
1173
|
+
* `true`, null nodes will be included in the traversal process. If it is set to `false`, null nodes
|
|
1174
|
+
* will be skipped
|
|
1175
|
+
* @returns The `dfs` method is returning an array of the return type of the callback function `C`.
|
|
1166
1176
|
*/
|
|
1167
|
-
dfs(callback = this._DEFAULT_NODE_CALLBACK, pattern = 'IN', startNode = this._root, iterationType = this.iterationType, includeNull = false) {
|
|
1177
|
+
dfs(callback = this._DEFAULT_NODE_CALLBACK, pattern = 'IN', onlyOne = false, startNode = this._root, iterationType = this.iterationType, includeNull = false) {
|
|
1168
1178
|
startNode = this.ensureNode(startNode);
|
|
1169
1179
|
if (!startNode)
|
|
1170
1180
|
return [];
|
|
1171
|
-
return this._dfs(callback, pattern, startNode, iterationType, includeNull);
|
|
1181
|
+
return this._dfs(callback, pattern, onlyOne, startNode, iterationType, includeNull);
|
|
1172
1182
|
}
|
|
1173
1183
|
/**
|
|
1174
1184
|
* Time complexity: O(n)
|
|
@@ -1179,7 +1189,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1179
1189
|
* @param {C} callback - The `callback` parameter in the `bfs` function is a function that will be
|
|
1180
1190
|
* called on each node visited during the breadth-first search traversal. It is a generic type `C`
|
|
1181
1191
|
* that extends the `NodeCallback` type, which takes a parameter of type `BinaryTreeNode<K, V>` or `null`.
|
|
1182
|
-
* @param {
|
|
1192
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `bfs`
|
|
1183
1193
|
* function represents the starting point for the breadth-first search traversal in a binary tree. It
|
|
1184
1194
|
* can be specified as a key, node, or entry in the binary tree structure. If not provided, the
|
|
1185
1195
|
* default value is the root node of the binary
|
|
@@ -1255,7 +1265,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1255
1265
|
* structure based on a specified callback and iteration type.
|
|
1256
1266
|
* @param {C} callback - The `callback` parameter is a function that will be called on each leaf node
|
|
1257
1267
|
* in the binary tree. It is optional and defaults to a default callback function if not provided.
|
|
1258
|
-
* @param {
|
|
1268
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `leaves`
|
|
1259
1269
|
* method is used to specify the starting point for finding and processing the leaves of a binary
|
|
1260
1270
|
* tree. It can be provided as either a key, a node, or an entry in the binary tree structure. If not
|
|
1261
1271
|
* explicitly provided, the default value
|
|
@@ -1310,7 +1320,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1310
1320
|
* @param {C} callback - The `callback` parameter is a function that will be applied to each node in
|
|
1311
1321
|
* the binary tree during the traversal. It is used to process each node and determine what
|
|
1312
1322
|
* information to include in the output for each level of the tree.
|
|
1313
|
-
* @param {
|
|
1323
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
|
|
1314
1324
|
* `listLevels` function represents the starting point for traversing the binary tree. It can be
|
|
1315
1325
|
* either a key, a node, or an entry in the binary tree. If not provided, the default value is the
|
|
1316
1326
|
* root of the binary tree.
|
|
@@ -1382,11 +1392,11 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1382
1392
|
* Morris Traversal algorithm with different order patterns.
|
|
1383
1393
|
* @param {C} callback - The `callback` parameter in the `morris` function is a function that will be
|
|
1384
1394
|
* called on each node in the binary tree during the traversal. It is of type `C`, which extends the
|
|
1385
|
-
* `NodeCallback<BinaryTreeNode<K, V
|
|
1395
|
+
* `NodeCallback<BinaryTreeNode<K, V> | null>` type. The default value for `callback` is `this._DEFAULT
|
|
1386
1396
|
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function specifies
|
|
1387
1397
|
* the type of Depth-First Search (DFS) order pattern to traverse the binary tree. The possible
|
|
1388
1398
|
* values for the `pattern` parameter are:
|
|
1389
|
-
* @param {
|
|
1399
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `morris`
|
|
1390
1400
|
* function is the starting point for the Morris traversal algorithm. It represents the root node of
|
|
1391
1401
|
* the binary tree or the node from which the traversal should begin. It can be provided as either a
|
|
1392
1402
|
* key, a node, an entry, or a reference
|
|
@@ -1495,20 +1505,6 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1495
1505
|
this._clone(cloned);
|
|
1496
1506
|
return cloned;
|
|
1497
1507
|
}
|
|
1498
|
-
_clone(cloned) {
|
|
1499
|
-
this.bfs(node => {
|
|
1500
|
-
if (node === null)
|
|
1501
|
-
cloned.add(null);
|
|
1502
|
-
else {
|
|
1503
|
-
if (this._isMapMode)
|
|
1504
|
-
cloned.add([node.key, this._store.get(node.key)]);
|
|
1505
|
-
else
|
|
1506
|
-
cloned.add([node.key, node.value]);
|
|
1507
|
-
}
|
|
1508
|
-
}, this._root, this.iterationType, true);
|
|
1509
|
-
if (this._isMapMode)
|
|
1510
|
-
cloned._store = this._store;
|
|
1511
|
-
}
|
|
1512
1508
|
/**
|
|
1513
1509
|
* Time Complexity: O(n)
|
|
1514
1510
|
* Space Complexity: O(n)
|
|
@@ -1568,7 +1564,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1568
1564
|
*
|
|
1569
1565
|
* The function `toVisual` in TypeScript overrides the visual representation of a binary tree with
|
|
1570
1566
|
* customizable options for displaying undefined, null, and sentinel nodes.
|
|
1571
|
-
* @param {
|
|
1567
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
|
|
1572
1568
|
* `toVisual` method is used to specify the starting point for visualizing the binary tree structure.
|
|
1573
1569
|
* It can be a node, key, entry, or the root of the tree. If no specific starting point is provided,
|
|
1574
1570
|
* the default is set to the root
|
|
@@ -1613,7 +1609,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1613
1609
|
* printing options for the binary tree. It is an optional parameter that allows you to customize how
|
|
1614
1610
|
* the binary tree is printed, such as choosing between different traversal orders or formatting
|
|
1615
1611
|
* options.
|
|
1616
|
-
* @param {
|
|
1612
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
|
|
1617
1613
|
* `override print` method is used to specify the starting point for printing the binary tree. It can
|
|
1618
1614
|
* be either a key, a node, an entry, or the root of the tree. If no specific starting point is
|
|
1619
1615
|
* provided, the default value is set to
|
|
@@ -1621,21 +1617,35 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1621
1617
|
print(options, startNode = this._root) {
|
|
1622
1618
|
console.log(this.toVisual(startNode, options));
|
|
1623
1619
|
}
|
|
1620
|
+
_clone(cloned) {
|
|
1621
|
+
this.bfs(node => {
|
|
1622
|
+
if (node === null)
|
|
1623
|
+
cloned.add(null);
|
|
1624
|
+
else {
|
|
1625
|
+
if (this._isMapMode)
|
|
1626
|
+
cloned.add([node.key, this._store.get(node.key)]);
|
|
1627
|
+
else
|
|
1628
|
+
cloned.add([node.key, node.value]);
|
|
1629
|
+
}
|
|
1630
|
+
}, this._root, this.iterationType, true);
|
|
1631
|
+
if (this._isMapMode)
|
|
1632
|
+
cloned._store = this._store;
|
|
1633
|
+
}
|
|
1624
1634
|
/**
|
|
1625
1635
|
* Time Complexity: O(1)
|
|
1626
1636
|
* Space Complexity: O(1)
|
|
1627
1637
|
*
|
|
1628
1638
|
* The function `keyValueNodeEntryRawToNodeAndValue` converts various input types into a node object
|
|
1629
1639
|
* or returns null.
|
|
1630
|
-
* @param {
|
|
1640
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The
|
|
1631
1641
|
* `keyValueNodeEntryRawToNodeAndValue` function takes in a parameter `keyNodeOrEntry`, which
|
|
1632
|
-
* can be of type `
|
|
1642
|
+
* can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. This parameter represents either a key, a
|
|
1633
1643
|
* node, an entry
|
|
1634
1644
|
* @param {V} [value] - The `value` parameter in the `keyValueNodeEntryRawToNodeAndValue` function is
|
|
1635
1645
|
* an optional parameter of type `V`. It represents the value associated with the key in the node
|
|
1636
1646
|
* being created. If a `value` is provided, it will be used when creating the node. If
|
|
1637
1647
|
* @returns The `keyValueNodeEntryRawToNodeAndValue` function returns an optional node
|
|
1638
|
-
* (`
|
|
1648
|
+
* (`BinaryTreeNode<K, V> | null | undefined`) based on the input parameters provided. The function checks the type of the
|
|
1639
1649
|
* input parameter (`keyNodeOrEntry`) and processes it accordingly to return a node or null
|
|
1640
1650
|
* value.
|
|
1641
1651
|
*/
|
|
@@ -1661,45 +1671,48 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1661
1671
|
* Time complexity: O(n)
|
|
1662
1672
|
* Space complexity: O(n)
|
|
1663
1673
|
*
|
|
1664
|
-
* The `_dfs` function performs a depth-first search traversal on a binary tree
|
|
1665
|
-
*
|
|
1674
|
+
* The `_dfs` function performs a depth-first search traversal on a binary tree, with customizable
|
|
1675
|
+
* options for traversal order and node processing.
|
|
1666
1676
|
* @param {C} callback - The `callback` parameter in the `_dfs` method is a function that will be
|
|
1667
|
-
* called on each node visited during the depth-first search traversal. It is
|
|
1668
|
-
* extends `NodeCallback<
|
|
1677
|
+
* called on each node visited during the depth-first search traversal. It is a generic type `C` that
|
|
1678
|
+
* extends `NodeCallback<BinaryTreeNode<K, V> | null>`. The default value for `callback`
|
|
1669
1679
|
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `_dfs` method specifies the
|
|
1670
|
-
* order in which the nodes are visited during
|
|
1671
|
-
*
|
|
1672
|
-
* @param {
|
|
1673
|
-
*
|
|
1674
|
-
*
|
|
1675
|
-
*
|
|
1680
|
+
* order in which the nodes are visited during a depth-first search traversal. It can have one of the
|
|
1681
|
+
* following values:
|
|
1682
|
+
* @param {boolean} [onlyOne=false] - The `onlyOne` parameter in the `_dfs` method is a boolean flag
|
|
1683
|
+
* that determines whether the traversal should stop after processing a single node. If `onlyOne` is
|
|
1684
|
+
* set to `true`, the traversal will return as soon as a single node is processed. If it is set to
|
|
1685
|
+
* `false
|
|
1686
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined}
|
|
1687
|
+
* startNode - The `startNode` parameter in the `_dfs` method is used to specify the starting node
|
|
1688
|
+
* for the depth-first search traversal. It can be provided in different forms:
|
|
1676
1689
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `_dfs` method
|
|
1677
|
-
* specifies the
|
|
1678
|
-
*
|
|
1679
|
-
* @param [includeNull=false] - The `includeNull` parameter in the `_dfs` method
|
|
1680
|
-
*
|
|
1681
|
-
*
|
|
1682
|
-
*
|
|
1683
|
-
* @param shouldVisitLeft - The `shouldVisitLeft` parameter
|
|
1684
|
-
*
|
|
1685
|
-
*
|
|
1686
|
-
*
|
|
1687
|
-
* @param shouldVisitRight - The `shouldVisitRight` parameter
|
|
1688
|
-
*
|
|
1689
|
-
*
|
|
1690
|
-
*
|
|
1691
|
-
* @param shouldVisitRoot - The `shouldVisitRoot` parameter
|
|
1692
|
-
*
|
|
1693
|
-
*
|
|
1694
|
-
*
|
|
1695
|
-
* @param shouldProcessRoot - The `shouldProcessRoot` parameter
|
|
1696
|
-
*
|
|
1697
|
-
*
|
|
1698
|
-
*
|
|
1699
|
-
* @returns The
|
|
1700
|
-
*
|
|
1690
|
+
* specifies whether the traversal should be done recursively or iteratively. It can have two
|
|
1691
|
+
* possible values:
|
|
1692
|
+
* @param [includeNull=false] - The `includeNull` parameter in the `_dfs` method determines whether
|
|
1693
|
+
* null nodes should be included in the traversal process. If `includeNull` is set to `true`, the
|
|
1694
|
+
* method will consider null nodes as valid nodes to visit or process. If `includeNull` is set to
|
|
1695
|
+
* `false`,
|
|
1696
|
+
* @param shouldVisitLeft - The `shouldVisitLeft` parameter in the `_dfs` method is a function that
|
|
1697
|
+
* determines whether the left child of a node should be visited during the Depth-First Search
|
|
1698
|
+
* traversal. By default, it checks if the node is not null or undefined before visiting the left
|
|
1699
|
+
* child. You can customize this behavior
|
|
1700
|
+
* @param shouldVisitRight - The `shouldVisitRight` parameter in the `_dfs` method is a function that
|
|
1701
|
+
* determines whether to visit the right child node of the current node during a depth-first search
|
|
1702
|
+
* traversal. The default implementation of this function checks if the node is not null or undefined
|
|
1703
|
+
* before deciding to visit it.
|
|
1704
|
+
* @param shouldVisitRoot - The `shouldVisitRoot` parameter in the `_dfs` method is a function that
|
|
1705
|
+
* determines whether a given node should be visited during the depth-first search traversal. The
|
|
1706
|
+
* function takes a node as an argument and returns a boolean value indicating whether the node
|
|
1707
|
+
* should be visited.
|
|
1708
|
+
* @param shouldProcessRoot - The `shouldProcessRoot` parameter in the `_dfs` method is a function
|
|
1709
|
+
* that determines whether the root node should be processed during the Depth-First Search traversal.
|
|
1710
|
+
* It takes a node (BinaryTreeNode<K, V> | null | undefined) as input and returns a boolean value. If
|
|
1711
|
+
* the function
|
|
1712
|
+
* @returns The `_dfs` method returns an array of the return type of the provided callback function
|
|
1713
|
+
* `C`.
|
|
1701
1714
|
*/
|
|
1702
|
-
_dfs(callback = this._DEFAULT_NODE_CALLBACK, pattern = 'IN', startNode = this._root, iterationType = this.iterationType, includeNull = false, shouldVisitLeft = node => !!node, shouldVisitRight = node => !!node, shouldVisitRoot = node => {
|
|
1715
|
+
_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 => {
|
|
1703
1716
|
if (includeNull)
|
|
1704
1717
|
return this.isRealNodeOrNull(node);
|
|
1705
1718
|
return this.isRealNode(node);
|
|
@@ -1713,31 +1726,40 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1713
1726
|
if (!shouldVisitRoot(node))
|
|
1714
1727
|
return;
|
|
1715
1728
|
const visitLeft = () => {
|
|
1716
|
-
if (shouldVisitLeft(node))
|
|
1729
|
+
if (shouldVisitLeft(node) && (node === null || node === void 0 ? void 0 : node.left) !== undefined)
|
|
1717
1730
|
dfs(node === null || node === void 0 ? void 0 : node.left);
|
|
1718
1731
|
};
|
|
1719
1732
|
const visitRight = () => {
|
|
1720
|
-
if (shouldVisitRight(node))
|
|
1733
|
+
if (shouldVisitRight(node) && (node === null || node === void 0 ? void 0 : node.right) !== undefined)
|
|
1721
1734
|
dfs(node === null || node === void 0 ? void 0 : node.right);
|
|
1722
1735
|
};
|
|
1723
1736
|
switch (pattern) {
|
|
1724
1737
|
case 'IN':
|
|
1725
1738
|
visitLeft();
|
|
1726
|
-
if (shouldProcessRoot(node))
|
|
1739
|
+
if (shouldProcessRoot(node)) {
|
|
1727
1740
|
ans.push(callback(node));
|
|
1741
|
+
if (onlyOne)
|
|
1742
|
+
return;
|
|
1743
|
+
}
|
|
1728
1744
|
visitRight();
|
|
1729
1745
|
break;
|
|
1730
1746
|
case 'PRE':
|
|
1731
|
-
if (shouldProcessRoot(node))
|
|
1747
|
+
if (shouldProcessRoot(node)) {
|
|
1732
1748
|
ans.push(callback(node));
|
|
1749
|
+
if (onlyOne)
|
|
1750
|
+
return;
|
|
1751
|
+
}
|
|
1733
1752
|
visitLeft();
|
|
1734
1753
|
visitRight();
|
|
1735
1754
|
break;
|
|
1736
1755
|
case 'POST':
|
|
1737
1756
|
visitLeft();
|
|
1738
1757
|
visitRight();
|
|
1739
|
-
if (shouldProcessRoot(node))
|
|
1758
|
+
if (shouldProcessRoot(node)) {
|
|
1740
1759
|
ans.push(callback(node));
|
|
1760
|
+
if (onlyOne)
|
|
1761
|
+
return;
|
|
1762
|
+
}
|
|
1741
1763
|
break;
|
|
1742
1764
|
}
|
|
1743
1765
|
};
|
|
@@ -1766,8 +1788,11 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1766
1788
|
if (!shouldVisitRoot(cur.node))
|
|
1767
1789
|
continue;
|
|
1768
1790
|
if (cur.opt === common_1.DFSOperation.PROCESS) {
|
|
1769
|
-
if (shouldProcessRoot(cur.node))
|
|
1791
|
+
if (shouldProcessRoot(cur.node) && cur.node !== undefined) {
|
|
1770
1792
|
ans.push(callback(cur.node));
|
|
1793
|
+
if (onlyOne)
|
|
1794
|
+
return ans;
|
|
1795
|
+
}
|
|
1771
1796
|
}
|
|
1772
1797
|
else {
|
|
1773
1798
|
switch (pattern) {
|
|
@@ -1913,12 +1938,12 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1913
1938
|
* Space Complexity: O(1)
|
|
1914
1939
|
*
|
|
1915
1940
|
* The _swapProperties function swaps key and value properties between two nodes in a binary tree.
|
|
1916
|
-
* @param {
|
|
1941
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } srcNode - The `srcNode` parameter in the
|
|
1917
1942
|
* `_swapProperties` method can be either a BTNRep object containing key and value
|
|
1918
1943
|
* properties, or it can be of type R.
|
|
1919
|
-
* @param {
|
|
1944
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } destNode - The `destNode` parameter in the
|
|
1920
1945
|
* `_swapProperties` method represents the node or entry where the properties will be swapped with
|
|
1921
|
-
* the `srcNode`. It can be of type `
|
|
1946
|
+
* the `srcNode`. It can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. The method ensures that
|
|
1922
1947
|
* both `srcNode
|
|
1923
1948
|
* @returns The `_swapProperties` method returns either the `destNode` with its key and value swapped
|
|
1924
1949
|
* with the `srcNode`, or `undefined` if either `srcNode` or `destNode` is falsy.
|
|
@@ -1978,7 +2003,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1978
2003
|
*
|
|
1979
2004
|
* The function _setRoot sets the root node of a data structure while updating the parent reference
|
|
1980
2005
|
* of the previous root node.
|
|
1981
|
-
* @param v - The parameter `v` in the `_setRoot` method is of type `
|
|
2006
|
+
* @param v - The parameter `v` in the `_setRoot` method is of type `BinaryTreeNode<K, V> | null | undefined`, which means
|
|
1982
2007
|
* it can either be an optional `BinaryTreeNode<K, V>` type or `null`.
|
|
1983
2008
|
*/
|
|
1984
2009
|
_setRoot(v) {
|
|
@@ -1993,7 +2018,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1993
2018
|
*
|
|
1994
2019
|
* The function `_ensurePredicate` in TypeScript ensures that the input is converted into a valid
|
|
1995
2020
|
* predicate function for a binary tree node.
|
|
1996
|
-
* @param {
|
|
2021
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate - The
|
|
1997
2022
|
* `_ensurePredicate` method in the provided code snippet is responsible for ensuring that the input
|
|
1998
2023
|
* parameter `keyNodeEntryOrPredicate` is transformed into a valid predicate function that can be
|
|
1999
2024
|
* used for filtering nodes in a binary tree.
|
|
@@ -2008,9 +2033,17 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
2008
2033
|
return (node) => node === keyNodeEntryOrPredicate;
|
|
2009
2034
|
if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
2010
2035
|
const [key] = keyNodeEntryOrPredicate;
|
|
2011
|
-
return (node) =>
|
|
2036
|
+
return (node) => {
|
|
2037
|
+
if (!node)
|
|
2038
|
+
return false;
|
|
2039
|
+
return node.key === key;
|
|
2040
|
+
};
|
|
2012
2041
|
}
|
|
2013
|
-
return (node) =>
|
|
2042
|
+
return (node) => {
|
|
2043
|
+
if (!node)
|
|
2044
|
+
return false;
|
|
2045
|
+
return node.key === keyNodeEntryOrPredicate;
|
|
2046
|
+
};
|
|
2014
2047
|
}
|
|
2015
2048
|
/**
|
|
2016
2049
|
* Time Complexity: O(1)
|
|
@@ -2033,8 +2066,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
2033
2066
|
*
|
|
2034
2067
|
* The function `_extractKey` in TypeScript returns the key from a given input, which can be a node,
|
|
2035
2068
|
* entry, raw data, or null/undefined.
|
|
2036
|
-
* @param {
|
|
2037
|
-
* TypeScript method that takes in a parameter `keyNodeOrEntry` of type `
|
|
2069
|
+
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `_extractKey` method you provided is a
|
|
2070
|
+
* TypeScript method that takes in a parameter `keyNodeOrEntry` of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `,
|
|
2038
2071
|
* where `BTNRep` is a generic type with keys `K`, `V`, and `BinaryTreeNode<K, V>`, and `
|
|
2039
2072
|
* @returns The `_extractKey` method returns the key value extracted from the `keyNodeOrEntry`
|
|
2040
2073
|
* parameter. The return value can be a key value of type `K`, `null`, or `undefined`, depending on
|