min-heap-typed 1.42.9 → 1.43.0
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.
|
@@ -139,23 +139,18 @@ class BinaryTree {
|
|
|
139
139
|
const queue = new queue_1.Queue([root]);
|
|
140
140
|
while (queue.size > 0) {
|
|
141
141
|
const cur = queue.shift();
|
|
142
|
-
if (cur) {
|
|
143
|
-
|
|
144
|
-
cur.value = newNode.value;
|
|
145
|
-
return;
|
|
146
|
-
}
|
|
147
|
-
const inserted = this._addTo(newNode, cur);
|
|
148
|
-
if (inserted !== undefined)
|
|
149
|
-
return inserted;
|
|
150
|
-
if (cur.left)
|
|
151
|
-
queue.push(cur.left);
|
|
152
|
-
if (cur.right)
|
|
153
|
-
queue.push(cur.right);
|
|
154
|
-
}
|
|
155
|
-
else
|
|
142
|
+
if (newNode && cur.key === newNode.key) {
|
|
143
|
+
cur.value = newNode.value;
|
|
156
144
|
return;
|
|
145
|
+
}
|
|
146
|
+
const inserted = this._addTo(newNode, cur);
|
|
147
|
+
if (inserted !== undefined)
|
|
148
|
+
return inserted;
|
|
149
|
+
if (cur.left)
|
|
150
|
+
queue.push(cur.left);
|
|
151
|
+
if (cur.right)
|
|
152
|
+
queue.push(cur.right);
|
|
157
153
|
}
|
|
158
|
-
return;
|
|
159
154
|
};
|
|
160
155
|
let inserted, needInsert;
|
|
161
156
|
if (keyOrNode === null) {
|
|
@@ -259,7 +254,7 @@ class BinaryTree {
|
|
|
259
254
|
const deletedResult = [];
|
|
260
255
|
if (!this.root)
|
|
261
256
|
return deletedResult;
|
|
262
|
-
if (identifier instanceof BinaryTreeNode)
|
|
257
|
+
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
263
258
|
callback = (node => node);
|
|
264
259
|
const curr = this.getNode(identifier, callback);
|
|
265
260
|
if (!curr)
|
|
@@ -284,16 +279,18 @@ class BinaryTree {
|
|
|
284
279
|
}
|
|
285
280
|
}
|
|
286
281
|
else {
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
if (parentOfLeftSubTreeMax
|
|
293
|
-
parentOfLeftSubTreeMax.right
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
282
|
+
if (curr.left) {
|
|
283
|
+
const leftSubTreeRightMost = this.getRightMost(curr.left);
|
|
284
|
+
if (leftSubTreeRightMost) {
|
|
285
|
+
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
286
|
+
orgCurrent = this._swap(curr, leftSubTreeRightMost);
|
|
287
|
+
if (parentOfLeftSubTreeMax) {
|
|
288
|
+
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
289
|
+
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
290
|
+
else
|
|
291
|
+
parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
|
|
292
|
+
needBalanced = parentOfLeftSubTreeMax;
|
|
293
|
+
}
|
|
297
294
|
}
|
|
298
295
|
}
|
|
299
296
|
}
|
|
@@ -365,9 +362,6 @@ class BinaryTree {
|
|
|
365
362
|
return _getMaxHeight(beginRoot);
|
|
366
363
|
}
|
|
367
364
|
else {
|
|
368
|
-
if (!beginRoot) {
|
|
369
|
-
return -1;
|
|
370
|
-
}
|
|
371
365
|
const stack = [{ node: beginRoot, depth: 0 }];
|
|
372
366
|
let maxHeight = 0;
|
|
373
367
|
while (stack.length > 0) {
|
|
@@ -492,9 +486,7 @@ class BinaryTree {
|
|
|
492
486
|
* @returns an array of nodes of type `N`.
|
|
493
487
|
*/
|
|
494
488
|
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
495
|
-
if (!
|
|
496
|
-
return [];
|
|
497
|
-
if (identifier instanceof BinaryTreeNode)
|
|
489
|
+
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
498
490
|
callback = (node => node);
|
|
499
491
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
500
492
|
if (!beginRoot)
|
|
@@ -556,7 +548,7 @@ class BinaryTree {
|
|
|
556
548
|
* @returns a boolean value.
|
|
557
549
|
*/
|
|
558
550
|
has(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
559
|
-
if (identifier instanceof BinaryTreeNode)
|
|
551
|
+
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
560
552
|
callback = (node => node);
|
|
561
553
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
562
554
|
}
|
|
@@ -587,7 +579,7 @@ class BinaryTree {
|
|
|
587
579
|
*/
|
|
588
580
|
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
589
581
|
var _a;
|
|
590
|
-
if (identifier instanceof BinaryTreeNode)
|
|
582
|
+
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
591
583
|
callback = (node => node);
|
|
592
584
|
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
|
|
593
585
|
}
|
|
@@ -680,7 +672,7 @@ class BinaryTree {
|
|
|
680
672
|
*/
|
|
681
673
|
get(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
682
674
|
var _a, _b;
|
|
683
|
-
if (identifier instanceof BinaryTreeNode)
|
|
675
|
+
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
684
676
|
callback = (node => node);
|
|
685
677
|
return (_b = (_a = this.getNode(identifier, callback, beginRoot, iterationType)) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : undefined;
|
|
686
678
|
}
|
|
@@ -882,9 +874,9 @@ class BinaryTree {
|
|
|
882
874
|
return this.isSubtreeBST(this.root, iterationType);
|
|
883
875
|
}
|
|
884
876
|
/**
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
877
|
+
* Time complexity: O(n)
|
|
878
|
+
* Space complexity: O(log n)
|
|
879
|
+
*/
|
|
888
880
|
/**
|
|
889
881
|
* Time complexity: O(n)
|
|
890
882
|
* Space complexity: O(log n)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "min-heap-typed",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.43.0",
|
|
4
4
|
"description": "Min Heap. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -132,6 +132,6 @@
|
|
|
132
132
|
"typescript": "^4.9.5"
|
|
133
133
|
},
|
|
134
134
|
"dependencies": {
|
|
135
|
-
"data-structure-typed": "^1.
|
|
135
|
+
"data-structure-typed": "^1.43.0"
|
|
136
136
|
}
|
|
137
137
|
}
|
|
@@ -108,8 +108,7 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
|
|
|
108
108
|
* @template N - The type of the binary tree's nodes.
|
|
109
109
|
*/
|
|
110
110
|
export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>>
|
|
111
|
-
implements IBinaryTree<V, N>
|
|
112
|
-
{
|
|
111
|
+
implements IBinaryTree<V, N> {
|
|
113
112
|
iterationType: IterationType = IterationType.ITERATIVE;
|
|
114
113
|
|
|
115
114
|
/**
|
|
@@ -173,21 +172,18 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
173
172
|
*/
|
|
174
173
|
add(keyOrNode: BTNKey | N | null | undefined, value?: V): N | null | undefined {
|
|
175
174
|
const _bfs = (root: N, newNode: N | null): N | undefined | null => {
|
|
176
|
-
const queue = new Queue<N
|
|
175
|
+
const queue = new Queue<N>([root]);
|
|
177
176
|
while (queue.size > 0) {
|
|
178
|
-
const cur = queue.shift()
|
|
179
|
-
if (cur) {
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
if (cur.right) queue.push(cur.right);
|
|
188
|
-
} else return;
|
|
177
|
+
const cur = queue.shift()!;
|
|
178
|
+
if (newNode && cur.key === newNode.key) {
|
|
179
|
+
cur.value = newNode.value;
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
const inserted = this._addTo(newNode, cur);
|
|
183
|
+
if (inserted !== undefined) return inserted;
|
|
184
|
+
if (cur.left) queue.push(cur.left);
|
|
185
|
+
if (cur.right) queue.push(cur.right);
|
|
189
186
|
}
|
|
190
|
-
return;
|
|
191
187
|
};
|
|
192
188
|
|
|
193
189
|
let inserted: N | null | undefined, needInsert: N | null | undefined;
|
|
@@ -224,7 +220,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
224
220
|
/**
|
|
225
221
|
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
226
222
|
* Space Complexity: O(1)
|
|
227
|
-
*
|
|
223
|
+
*
|
|
228
224
|
* The `addMany` function takes an array of keys or nodes and an optional array of values, and adds
|
|
229
225
|
* each key-value pair to a data structure.
|
|
230
226
|
* @param {(BTNKey | N |null | undefined)[]} keysOrNodes - An array of keys or nodes to be added to
|
|
@@ -235,7 +231,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
235
231
|
* keys or nodes during the add operation.
|
|
236
232
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
237
233
|
*/
|
|
238
|
-
addMany(keysOrNodes: (BTNKey | N |null | undefined)[], values?: (V | undefined)[]): (N | null | undefined)[] {
|
|
234
|
+
addMany(keysOrNodes: (BTNKey | N | null | undefined)[], values?: (V | undefined)[]): (N | null | undefined)[] {
|
|
239
235
|
// TODO not sure addMany not be run multi times
|
|
240
236
|
return keysOrNodes.map((keyOrNode, i) => {
|
|
241
237
|
if (keyOrNode instanceof BinaryTreeNode) {
|
|
@@ -259,7 +255,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
259
255
|
/**
|
|
260
256
|
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
261
257
|
* Space Complexity: O(1)
|
|
262
|
-
*
|
|
258
|
+
*
|
|
263
259
|
* The `refill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
|
|
264
260
|
* @param {(BTNKey | N)[]} keysOrNodes - The `keysOrNodes` parameter is an array that can contain either
|
|
265
261
|
* `BTNKey` or `N` values.
|
|
@@ -287,7 +283,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
287
283
|
/**
|
|
288
284
|
* Time Complexity: O(n)
|
|
289
285
|
* Space Complexity: O(1)
|
|
290
|
-
*
|
|
286
|
+
*
|
|
291
287
|
* The function deletes a node from a binary tree and returns an array of the deleted nodes along
|
|
292
288
|
* with the nodes that need to be balanced.
|
|
293
289
|
* @param {ReturnType<C> | null | undefined} identifier - The identifier parameter is the value or
|
|
@@ -305,7 +301,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
305
301
|
): BiTreeDeleteResult<N>[] {
|
|
306
302
|
const deletedResult: BiTreeDeleteResult<N>[] = [];
|
|
307
303
|
if (!this.root) return deletedResult;
|
|
308
|
-
if ((identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
304
|
+
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
309
305
|
|
|
310
306
|
const curr = this.getNode(identifier, callback);
|
|
311
307
|
if (!curr) return deletedResult;
|
|
@@ -328,17 +324,20 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
328
324
|
needBalanced = parent;
|
|
329
325
|
}
|
|
330
326
|
} else {
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
if (parentOfLeftSubTreeMax
|
|
337
|
-
parentOfLeftSubTreeMax.right
|
|
338
|
-
|
|
339
|
-
|
|
327
|
+
if (curr.left) {
|
|
328
|
+
const leftSubTreeRightMost = this.getRightMost(curr.left);
|
|
329
|
+
if (leftSubTreeRightMost) {
|
|
330
|
+
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
331
|
+
orgCurrent = this._swap(curr, leftSubTreeRightMost);
|
|
332
|
+
if (parentOfLeftSubTreeMax) {
|
|
333
|
+
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
334
|
+
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
335
|
+
else parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
|
|
336
|
+
needBalanced = parentOfLeftSubTreeMax;
|
|
337
|
+
}
|
|
340
338
|
}
|
|
341
339
|
}
|
|
340
|
+
|
|
342
341
|
}
|
|
343
342
|
this._size = this.size - 1;
|
|
344
343
|
|
|
@@ -354,7 +353,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
354
353
|
/**
|
|
355
354
|
* Time Complexity: O(n)
|
|
356
355
|
* Space Complexity: O(1)
|
|
357
|
-
*
|
|
356
|
+
*
|
|
358
357
|
* The function calculates the depth of a given node in a binary tree.
|
|
359
358
|
* @param {BTNKey | N | null | undefined} distNode - The `distNode` parameter represents the node in
|
|
360
359
|
* the binary tree whose depth we want to find. It can be of type `BTNKey`, `N`, `null`, or
|
|
@@ -388,7 +387,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
388
387
|
/**
|
|
389
388
|
* Time Complexity: O(n)
|
|
390
389
|
* Space Complexity: O(log n)
|
|
391
|
-
*
|
|
390
|
+
*
|
|
392
391
|
* The function `getHeight` calculates the maximum height of a binary tree using either recursive or
|
|
393
392
|
* iterative traversal.
|
|
394
393
|
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
@@ -413,11 +412,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
413
412
|
|
|
414
413
|
return _getMaxHeight(beginRoot);
|
|
415
414
|
} else {
|
|
416
|
-
|
|
417
|
-
return -1;
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
const stack: {node: N; depth: number}[] = [{node: beginRoot, depth: 0}];
|
|
415
|
+
const stack: { node: N; depth: number }[] = [{node: beginRoot, depth: 0}];
|
|
421
416
|
let maxHeight = 0;
|
|
422
417
|
|
|
423
418
|
while (stack.length > 0) {
|
|
@@ -425,7 +420,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
425
420
|
|
|
426
421
|
if (node.left) stack.push({node: node.left, depth: depth + 1});
|
|
427
422
|
if (node.right) stack.push({node: node.right, depth: depth + 1});
|
|
428
|
-
|
|
423
|
+
|
|
429
424
|
maxHeight = Math.max(maxHeight, depth);
|
|
430
425
|
}
|
|
431
426
|
|
|
@@ -442,7 +437,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
442
437
|
/**
|
|
443
438
|
* Time Complexity: O(n)
|
|
444
439
|
* Space Complexity: O(log n)
|
|
445
|
-
*
|
|
440
|
+
*
|
|
446
441
|
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
447
442
|
* recursive or iterative approach.
|
|
448
443
|
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
@@ -455,7 +450,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
455
450
|
getMinHeight(beginRoot: BTNKey | N | null | undefined = this.root, iterationType = this.iterationType): number {
|
|
456
451
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
457
452
|
if (!beginRoot) return -1;
|
|
458
|
-
|
|
453
|
+
|
|
459
454
|
if (iterationType === IterationType.RECURSIVE) {
|
|
460
455
|
const _getMinHeight = (cur: N | null | undefined): number => {
|
|
461
456
|
if (!cur) return 0;
|
|
@@ -503,7 +498,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
503
498
|
/**
|
|
504
499
|
* Time Complexity: O(n)
|
|
505
500
|
* Space Complexity: O(log n)
|
|
506
|
-
*
|
|
501
|
+
*
|
|
507
502
|
* The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
|
|
508
503
|
* height of the tree.
|
|
509
504
|
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
|
|
@@ -548,7 +543,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
548
543
|
/**
|
|
549
544
|
* Time Complexity: O(n)
|
|
550
545
|
* Space Complexity: O(log n).
|
|
551
|
-
*
|
|
546
|
+
*
|
|
552
547
|
* The function `getNodes` retrieves nodes from a binary tree based on a given identifier and
|
|
553
548
|
* callback function.
|
|
554
549
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
@@ -577,11 +572,10 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
577
572
|
beginRoot: BTNKey | N | null | undefined = this.root,
|
|
578
573
|
iterationType = this.iterationType
|
|
579
574
|
): N[] {
|
|
580
|
-
if (!
|
|
581
|
-
if ((identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
575
|
+
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
582
576
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
583
577
|
if (!beginRoot) return [];
|
|
584
|
-
|
|
578
|
+
|
|
585
579
|
const ans: N[] = [];
|
|
586
580
|
|
|
587
581
|
if (iterationType === IterationType.RECURSIVE) {
|
|
@@ -642,7 +636,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
642
636
|
|
|
643
637
|
/**
|
|
644
638
|
* Time Complexity: O(n)
|
|
645
|
-
*
|
|
639
|
+
*
|
|
646
640
|
* The function checks if a Binary Tree Node with a specific identifier exists in the tree.
|
|
647
641
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
648
642
|
* that you want to search for in the binary tree. It can be of any type that is returned by the
|
|
@@ -666,7 +660,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
666
660
|
beginRoot: BTNKey | N | null | undefined = this.root,
|
|
667
661
|
iterationType = this.iterationType
|
|
668
662
|
): boolean {
|
|
669
|
-
if ((identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
663
|
+
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
670
664
|
|
|
671
665
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
672
666
|
}
|
|
@@ -700,7 +694,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
700
694
|
/**
|
|
701
695
|
* Time Complexity: O(n)
|
|
702
696
|
* Space Complexity: O(log n)
|
|
703
|
-
*
|
|
697
|
+
*
|
|
704
698
|
* The function `getNode` returns the first node that matches the given identifier and callback
|
|
705
699
|
* function.
|
|
706
700
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
@@ -724,7 +718,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
724
718
|
beginRoot: BTNKey | N | null | undefined = this.root,
|
|
725
719
|
iterationType = this.iterationType
|
|
726
720
|
): N | null | undefined {
|
|
727
|
-
if ((identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
721
|
+
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
728
722
|
|
|
729
723
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
|
|
730
724
|
}
|
|
@@ -737,7 +731,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
737
731
|
/**
|
|
738
732
|
* Time Complexity: O(n)
|
|
739
733
|
* Space Complexity: O(log n)
|
|
740
|
-
*
|
|
734
|
+
*
|
|
741
735
|
* The function `getNodeByKey` searches for a node in a binary tree by its key, using either
|
|
742
736
|
* recursive or iterative iteration.
|
|
743
737
|
* @param {BTNKey} key - The `key` parameter is the key value that we are searching for in the tree.
|
|
@@ -772,7 +766,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
772
766
|
}
|
|
773
767
|
}
|
|
774
768
|
}
|
|
775
|
-
|
|
769
|
+
|
|
776
770
|
/**
|
|
777
771
|
* The function `ensureNotKey` returns the node corresponding to the given key if it is a valid node
|
|
778
772
|
* key, otherwise it returns the key itself.
|
|
@@ -787,7 +781,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
787
781
|
ensureNotKey(key: BTNKey | N | null | undefined, iterationType = IterationType.ITERATIVE): N | null | undefined {
|
|
788
782
|
return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
789
783
|
}
|
|
790
|
-
|
|
784
|
+
|
|
791
785
|
get<C extends BTNCallback<N, BTNKey>>(
|
|
792
786
|
identifier: BTNKey,
|
|
793
787
|
callback?: C,
|
|
@@ -813,11 +807,11 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
813
807
|
* Time Complexity: O(n)
|
|
814
808
|
* Space Complexity: O(log n)
|
|
815
809
|
*/
|
|
816
|
-
|
|
810
|
+
|
|
817
811
|
/**
|
|
818
812
|
* Time Complexity: O(n)
|
|
819
813
|
* Space Complexity: O(log n)
|
|
820
|
-
*
|
|
814
|
+
*
|
|
821
815
|
* The function `get` retrieves the value of a node in a binary tree based on the provided identifier
|
|
822
816
|
* and callback function.
|
|
823
817
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
@@ -839,14 +833,14 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
839
833
|
get<C extends BTNCallback<N>>(
|
|
840
834
|
identifier: ReturnType<C> | null | undefined,
|
|
841
835
|
callback: C = this._defaultOneParamCallback as C,
|
|
842
|
-
beginRoot:BTNKey | N | null | undefined = this.root,
|
|
836
|
+
beginRoot: BTNKey | N | null | undefined = this.root,
|
|
843
837
|
iterationType = this.iterationType
|
|
844
838
|
): V | undefined {
|
|
845
|
-
if ((identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
839
|
+
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
846
840
|
|
|
847
841
|
return this.getNode(identifier, callback, beginRoot, iterationType)?.value ?? undefined;
|
|
848
842
|
}
|
|
849
|
-
|
|
843
|
+
|
|
850
844
|
/**
|
|
851
845
|
* Clear the binary tree, removing all nodes.
|
|
852
846
|
*/
|
|
@@ -871,7 +865,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
871
865
|
/**
|
|
872
866
|
* Time Complexity: O(log n)
|
|
873
867
|
* Space Complexity: O(log n)
|
|
874
|
-
*
|
|
868
|
+
*
|
|
875
869
|
* The function `getPathToRoot` returns an array of nodes from a given node to the root of a tree
|
|
876
870
|
* structure, with the option to reverse the order of the nodes.
|
|
877
871
|
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
@@ -886,9 +880,9 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
886
880
|
// TODO to support get path through passing key
|
|
887
881
|
const result: N[] = [];
|
|
888
882
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
889
|
-
|
|
883
|
+
|
|
890
884
|
if (!beginRoot) return result;
|
|
891
|
-
|
|
885
|
+
|
|
892
886
|
while (beginRoot.parent) {
|
|
893
887
|
// Array.push + Array.reverse is more efficient than Array.unshift
|
|
894
888
|
// TODO may consider using Deque, so far this is not the performance bottleneck
|
|
@@ -907,7 +901,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
907
901
|
/**
|
|
908
902
|
* Time Complexity: O(log n)
|
|
909
903
|
* Space Complexity: O(1)
|
|
910
|
-
*
|
|
904
|
+
*
|
|
911
905
|
* The function `getLeftMost` returns the leftmost node in a binary tree, either recursively or
|
|
912
906
|
* iteratively.
|
|
913
907
|
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
|
|
@@ -949,7 +943,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
949
943
|
/**
|
|
950
944
|
* Time Complexity: O(log n)
|
|
951
945
|
* Space Complexity: O(1)
|
|
952
|
-
*
|
|
946
|
+
*
|
|
953
947
|
* The function `getRightMost` returns the rightmost node in a binary tree, either recursively or
|
|
954
948
|
* iteratively.
|
|
955
949
|
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
@@ -992,7 +986,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
992
986
|
/**
|
|
993
987
|
* Time Complexity: O(n)
|
|
994
988
|
* Space Complexity: O(1)
|
|
995
|
-
*
|
|
989
|
+
*
|
|
996
990
|
* The function `isSubtreeBST` checks if a given binary tree is a valid binary search tree.
|
|
997
991
|
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the root
|
|
998
992
|
* node of the binary search tree (BST) that you want to check if it is a subtree of another BST.
|
|
@@ -1040,7 +1034,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1040
1034
|
/**
|
|
1041
1035
|
* Time Complexity: O(n)
|
|
1042
1036
|
* Space Complexity: O(1)
|
|
1043
|
-
*
|
|
1037
|
+
*
|
|
1044
1038
|
* The function checks if a binary tree is a binary search tree.
|
|
1045
1039
|
* @param iterationType - The parameter "iterationType" is used to specify the type of iteration to
|
|
1046
1040
|
* be used when checking if the binary tree is a binary search tree (BST). It is an optional
|
|
@@ -1074,15 +1068,15 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1074
1068
|
includeNull?: true
|
|
1075
1069
|
): ReturnType<C>[];
|
|
1076
1070
|
|
|
1077
|
-
|
|
1071
|
+
/**
|
|
1078
1072
|
* Time complexity: O(n)
|
|
1079
1073
|
* Space complexity: O(log n)
|
|
1080
1074
|
*/
|
|
1081
|
-
|
|
1075
|
+
|
|
1082
1076
|
/**
|
|
1083
1077
|
* Time complexity: O(n)
|
|
1084
1078
|
* Space complexity: O(log n)
|
|
1085
|
-
*
|
|
1079
|
+
*
|
|
1086
1080
|
* The function `subTreeTraverse` traverses a binary tree and applies a callback function to each
|
|
1087
1081
|
* node, either recursively or iteratively.
|
|
1088
1082
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
@@ -1146,7 +1140,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1146
1140
|
}
|
|
1147
1141
|
return ans;
|
|
1148
1142
|
}
|
|
1149
|
-
|
|
1143
|
+
|
|
1150
1144
|
/**
|
|
1151
1145
|
* The function checks if a given node is a real node by verifying if it is an instance of
|
|
1152
1146
|
* BinaryTreeNode and its key is not NaN.
|
|
@@ -1171,7 +1165,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1171
1165
|
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
1172
1166
|
* @returns a boolean value.
|
|
1173
1167
|
*/
|
|
1174
|
-
isNodeOrNull(node: any): node is (N | null){
|
|
1168
|
+
isNodeOrNull(node: any): node is (N | null) {
|
|
1175
1169
|
return this.isRealNode(node) || node === null;
|
|
1176
1170
|
}
|
|
1177
1171
|
|
|
@@ -1181,7 +1175,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1181
1175
|
* data type.
|
|
1182
1176
|
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
1183
1177
|
*/
|
|
1184
|
-
isNodeKey(potentialKey: any)
|
|
1178
|
+
isNodeKey(potentialKey: any): potentialKey is number {
|
|
1185
1179
|
return typeof potentialKey === 'number';
|
|
1186
1180
|
}
|
|
1187
1181
|
|
|
@@ -1213,11 +1207,11 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1213
1207
|
* Time complexity: O(n)
|
|
1214
1208
|
* Space complexity: O(n)
|
|
1215
1209
|
*/
|
|
1216
|
-
|
|
1210
|
+
|
|
1217
1211
|
/**
|
|
1218
1212
|
* Time complexity: O(n)
|
|
1219
1213
|
* Space complexity: O(n)
|
|
1220
|
-
*
|
|
1214
|
+
*
|
|
1221
1215
|
* The `dfs` function performs a depth-first search traversal on a binary tree or graph, based on the
|
|
1222
1216
|
* specified pattern and iteration type, and returns an array of values obtained from applying a
|
|
1223
1217
|
* callback function to each visited node.
|
|
@@ -1291,7 +1285,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1291
1285
|
_traverse(beginRoot);
|
|
1292
1286
|
} else {
|
|
1293
1287
|
// 0: visit, 1: print
|
|
1294
|
-
const stack: {opt: 0 | 1; node: N | null | undefined}[] = [{opt: 0, node: beginRoot}];
|
|
1288
|
+
const stack: { opt: 0 | 1; node: N | null | undefined }[] = [{opt: 0, node: beginRoot}];
|
|
1295
1289
|
|
|
1296
1290
|
while (stack.length > 0) {
|
|
1297
1291
|
const cur = stack.pop();
|
|
@@ -1362,7 +1356,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1362
1356
|
/**
|
|
1363
1357
|
* Time complexity: O(n)
|
|
1364
1358
|
* Space complexity: O(n)
|
|
1365
|
-
*
|
|
1359
|
+
*
|
|
1366
1360
|
* The `bfs` function performs a breadth-first search traversal on a binary tree, executing a
|
|
1367
1361
|
* callback function on each node.
|
|
1368
1362
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
@@ -1460,11 +1454,11 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1460
1454
|
* Space complexity: O(n)
|
|
1461
1455
|
*/
|
|
1462
1456
|
|
|
1463
|
-
|
|
1457
|
+
|
|
1464
1458
|
/**
|
|
1465
1459
|
* Time complexity: O(n)
|
|
1466
1460
|
* Space complexity: O(n)
|
|
1467
|
-
*
|
|
1461
|
+
*
|
|
1468
1462
|
* The `listLevels` function returns an array of arrays, where each inner array represents a level in
|
|
1469
1463
|
* a binary tree and contains the values returned by a callback function applied to the nodes at that
|
|
1470
1464
|
* level.
|
|
@@ -1529,7 +1523,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1529
1523
|
return levelsNodes;
|
|
1530
1524
|
}
|
|
1531
1525
|
|
|
1532
|
-
getPredecessor(node: N
|
|
1526
|
+
getPredecessor(node: N): N
|
|
1533
1527
|
|
|
1534
1528
|
|
|
1535
1529
|
/**
|
|
@@ -1538,7 +1532,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1538
1532
|
* `null`, or `undefined`.
|
|
1539
1533
|
* @returns The function `getPredecessor` returns a value of type `N | undefined`.
|
|
1540
1534
|
*/
|
|
1541
|
-
getPredecessor(node: BTNKey | N | null | undefined): N | undefined{
|
|
1535
|
+
getPredecessor(node: BTNKey | N | null | undefined): N | undefined {
|
|
1542
1536
|
node = this.ensureNotKey(node);
|
|
1543
1537
|
if (!this.isRealNode(node)) return undefined;
|
|
1544
1538
|
|
|
@@ -1697,7 +1691,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1697
1691
|
* @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
|
|
1698
1692
|
* binary tree nodes in a specific order.
|
|
1699
1693
|
*/
|
|
1700
|
-
*[Symbol.iterator](node = this.root): Generator<BTNKey, void, undefined> {
|
|
1694
|
+
* [Symbol.iterator](node = this.root): Generator<BTNKey, void, undefined> {
|
|
1701
1695
|
if (!node) {
|
|
1702
1696
|
return;
|
|
1703
1697
|
}
|
|
@@ -1736,7 +1730,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1736
1730
|
* @param {N} destNode - The destination node to swap.
|
|
1737
1731
|
* @returns {N} - The destination node after the swap.
|
|
1738
1732
|
*/
|
|
1739
|
-
protected _swap(srcNode: BTNKey | N | null | undefined, destNode:BTNKey | N | null | undefined): N | undefined{
|
|
1733
|
+
protected _swap(srcNode: BTNKey | N | null | undefined, destNode: BTNKey | N | null | undefined): N | undefined {
|
|
1740
1734
|
srcNode = this.ensureNotKey(srcNode);
|
|
1741
1735
|
destNode = this.ensureNotKey(destNode);
|
|
1742
1736
|
|
|
@@ -1826,7 +1820,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1826
1820
|
}
|
|
1827
1821
|
};
|
|
1828
1822
|
|
|
1829
|
-
const _displayAux = (node: N | null
|
|
1823
|
+
const _displayAux = (node: N | null | undefined): [string[], number, number, number] => {
|
|
1830
1824
|
if (!this.isRealNode(node)) {
|
|
1831
1825
|
return [[], 0, 0, 0];
|
|
1832
1826
|
}
|