min-heap-typed 1.39.1 → 1.39.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/dist/data-structures/binary-tree/avl-tree.d.ts +10 -10
- package/dist/data-structures/binary-tree/avl-tree.js +4 -4
- package/dist/data-structures/binary-tree/binary-tree.d.ts +58 -107
- package/dist/data-structures/binary-tree/binary-tree.js +65 -27
- package/dist/data-structures/binary-tree/bst.d.ts +22 -22
- package/dist/data-structures/binary-tree/bst.js +12 -12
- package/dist/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +14 -14
- package/dist/data-structures/binary-tree/tree-multiset.js +7 -7
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +46 -28
- package/dist/data-structures/linked-list/doubly-linked-list.js +59 -49
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +75 -7
- package/dist/data-structures/linked-list/singly-linked-list.js +110 -9
- package/dist/data-structures/queue/deque.d.ts +20 -20
- package/dist/data-structures/queue/deque.js +22 -22
- package/dist/data-structures/queue/queue.d.ts +3 -3
- package/dist/data-structures/queue/queue.js +3 -3
- package/dist/interfaces/binary-tree.d.ts +4 -4
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/types/helpers.d.ts +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +10 -10
- package/src/data-structures/binary-tree/binary-tree.ts +165 -53
- package/src/data-structures/binary-tree/bst.ts +29 -31
- package/src/data-structures/binary-tree/rb-tree.ts +5 -5
- package/src/data-structures/binary-tree/tree-multiset.ts +14 -14
- package/src/data-structures/linked-list/doubly-linked-list.ts +62 -56
- package/src/data-structures/linked-list/singly-linked-list.ts +118 -13
- package/src/data-structures/queue/deque.ts +22 -22
- package/src/data-structures/queue/queue.ts +3 -3
- package/src/interfaces/binary-tree.ts +4 -4
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +2 -2
- package/src/types/helpers.ts +1 -1
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import type {
|
|
9
|
+
import type {BinaryTreeNodeNested, BinaryTreeOptions, BTNCallback, BTNKey} from '../../types';
|
|
10
10
|
import {BinaryTreeDeletedResult, DFSOrderPattern, FamilyPosition, IterationType} from '../../types';
|
|
11
11
|
import {IBinaryTree} from '../../interfaces';
|
|
12
12
|
import {trampoline} from '../../utils';
|
|
@@ -21,7 +21,7 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
|
|
|
21
21
|
/**
|
|
22
22
|
* The key associated with the node.
|
|
23
23
|
*/
|
|
24
|
-
key:
|
|
24
|
+
key: BTNKey;
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
27
|
* The value stored in the node.
|
|
@@ -35,10 +35,10 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
|
|
|
35
35
|
|
|
36
36
|
/**
|
|
37
37
|
* Creates a new instance of BinaryTreeNode.
|
|
38
|
-
* @param {
|
|
38
|
+
* @param {BTNKey} key - The key associated with the node.
|
|
39
39
|
* @param {V} val - The value stored in the node.
|
|
40
40
|
*/
|
|
41
|
-
constructor(key:
|
|
41
|
+
constructor(key: BTNKey, val?: V) {
|
|
42
42
|
this.key = key;
|
|
43
43
|
this.val = val;
|
|
44
44
|
}
|
|
@@ -158,11 +158,11 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
158
158
|
|
|
159
159
|
/**
|
|
160
160
|
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
161
|
-
* @param {
|
|
161
|
+
* @param {BTNKey} key - The key for the new node.
|
|
162
162
|
* @param {V} val - The value for the new node.
|
|
163
163
|
* @returns {N} - The newly created BinaryTreeNode.
|
|
164
164
|
*/
|
|
165
|
-
createNode(key:
|
|
165
|
+
createNode(key: BTNKey, val?: V): N {
|
|
166
166
|
return new BinaryTreeNode<V, N>(key, val) as N;
|
|
167
167
|
}
|
|
168
168
|
|
|
@@ -184,11 +184,11 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
184
184
|
|
|
185
185
|
/**
|
|
186
186
|
* Add a node with the given key and value to the binary tree.
|
|
187
|
-
* @param {
|
|
187
|
+
* @param {BTNKey | N | null} keyOrNode - The key or node to add to the binary tree.
|
|
188
188
|
* @param {V} val - The value for the new node (optional).
|
|
189
189
|
* @returns {N | null | undefined} - The inserted node, or null if nothing was inserted, or undefined if the operation failed.
|
|
190
190
|
*/
|
|
191
|
-
add(keyOrNode:
|
|
191
|
+
add(keyOrNode: BTNKey | N | null, val?: V): N | null | undefined {
|
|
192
192
|
const _bfs = (root: N, newNode: N | null): N | undefined | null => {
|
|
193
193
|
const queue = new Queue<N | null>([root]);
|
|
194
194
|
while (queue.size > 0) {
|
|
@@ -241,14 +241,14 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
241
241
|
/**
|
|
242
242
|
* The `addMany` function takes an array of binary tree node IDs or nodes, and optionally an array of corresponding data
|
|
243
243
|
* values, and adds them to the binary tree.
|
|
244
|
-
* @param {(
|
|
244
|
+
* @param {(BTNKey | null)[] | (N | null)[]} keysOrNodes - An array of BTNKey or BinaryTreeNode
|
|
245
245
|
* objects, or null values.
|
|
246
246
|
* @param {V[]} [values] - The `values` parameter is an optional array of values (`V[]`) that corresponds to
|
|
247
247
|
* the nodes or node IDs being added. It is used to set the value of each node being added. If `values` is not provided,
|
|
248
248
|
* the value of the nodes will be `undefined`.
|
|
249
249
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
250
250
|
*/
|
|
251
|
-
addMany(keysOrNodes: (
|
|
251
|
+
addMany(keysOrNodes: (BTNKey | null)[] | (N | null)[], values?: V[]): (N | null | undefined)[] {
|
|
252
252
|
// TODO not sure addMany not be run multi times
|
|
253
253
|
return keysOrNodes.map((keyOrNode, i) => {
|
|
254
254
|
if (keyOrNode instanceof BinaryTreeNode) {
|
|
@@ -266,33 +266,39 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
266
266
|
|
|
267
267
|
/**
|
|
268
268
|
* The `refill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
|
|
269
|
-
* @param {(
|
|
270
|
-
* `
|
|
269
|
+
* @param {(BTNKey | N)[]} keysOrNodes - The `keysOrNodes` parameter is an array that can contain either
|
|
270
|
+
* `BTNKey` or `N` values.
|
|
271
271
|
* @param {N[] | Array<V>} [data] - The `data` parameter is an optional array of values that will be assigned to
|
|
272
272
|
* the nodes being added. If provided, the length of the `data` array should be equal to the length of the `keysOrNodes`
|
|
273
273
|
* array. Each value in the `data` array will be assigned to the
|
|
274
274
|
* @returns The method is returning a boolean value.
|
|
275
275
|
*/
|
|
276
|
-
refill(keysOrNodes: (
|
|
276
|
+
refill(keysOrNodes: (BTNKey | null)[] | (N | null)[], data?: Array<V>): boolean {
|
|
277
277
|
this.clear();
|
|
278
278
|
return keysOrNodes.length === this.addMany(keysOrNodes, data).length;
|
|
279
279
|
}
|
|
280
280
|
|
|
281
|
+
delete<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C): BinaryTreeDeletedResult<N>[];
|
|
282
|
+
|
|
283
|
+
delete<C extends BTNCallback<N, N>>(identifier: N | null, callback?: C): BinaryTreeDeletedResult<N>[];
|
|
284
|
+
|
|
285
|
+
delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C): BinaryTreeDeletedResult<N>[];
|
|
286
|
+
|
|
281
287
|
/**
|
|
282
288
|
* The `delete` function removes a node from a binary search tree and returns the deleted node along
|
|
283
289
|
* with the parent node that needs to be balanced.
|
|
284
|
-
* a key (`
|
|
290
|
+
* a key (`BTNKey`). If it is a key, the function will find the corresponding node in the
|
|
285
291
|
* binary tree.
|
|
286
292
|
* @returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
287
293
|
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a
|
|
288
|
-
* `
|
|
294
|
+
* `BTNKey` or a generic type `N`. It represents the property of the node that we are
|
|
289
295
|
* searching for. It can be a specific key value or any other property of the node.
|
|
290
296
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
291
297
|
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
292
298
|
* included in the result. The `callback` parameter has a default value of
|
|
293
299
|
* `this._defaultCallbackByKey`, which
|
|
294
300
|
*/
|
|
295
|
-
delete<C extends
|
|
301
|
+
delete<C extends BTNCallback<N>>(
|
|
296
302
|
identifier: ReturnType<C> | null,
|
|
297
303
|
callback: C = this._defaultCallbackByKey as C
|
|
298
304
|
): BinaryTreeDeletedResult<N>[] {
|
|
@@ -342,16 +348,16 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
342
348
|
/**
|
|
343
349
|
* The function `getDepth` calculates the depth of a given node in a binary tree relative to a
|
|
344
350
|
* specified root node.
|
|
345
|
-
* @param {
|
|
351
|
+
* @param {BTNKey | N | null} distNode - The `distNode` parameter represents the node
|
|
346
352
|
* whose depth we want to find in the binary tree. It can be either a node object (`N`), a key value
|
|
347
|
-
* of the node (`
|
|
348
|
-
* @param {
|
|
353
|
+
* of the node (`BTNKey`), or `null`.
|
|
354
|
+
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter represents the
|
|
349
355
|
* starting node from which we want to calculate the depth. It can be either a node object or the key
|
|
350
356
|
* of a node in the binary tree. If no value is provided for `beginRoot`, it defaults to the root
|
|
351
357
|
* node of the binary tree.
|
|
352
358
|
* @returns the depth of the `distNode` relative to the `beginRoot`.
|
|
353
359
|
*/
|
|
354
|
-
getDepth(distNode:
|
|
360
|
+
getDepth(distNode: BTNKey | N | null, beginRoot: BTNKey | N | null = this.root): number {
|
|
355
361
|
if (typeof distNode === 'number') distNode = this.get(distNode);
|
|
356
362
|
if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot);
|
|
357
363
|
let depth = 0;
|
|
@@ -368,16 +374,16 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
368
374
|
/**
|
|
369
375
|
* The `getHeight` function calculates the maximum height of a binary tree using either recursive or
|
|
370
376
|
* iterative approach.
|
|
371
|
-
* @param {
|
|
377
|
+
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter represents the
|
|
372
378
|
* starting node from which the height of the binary tree is calculated. It can be either a node
|
|
373
|
-
* object (`N`), a key value of a node in the tree (`
|
|
379
|
+
* object (`N`), a key value of a node in the tree (`BTNKey`), or `null` if no starting
|
|
374
380
|
* node is specified. If `
|
|
375
381
|
* @param iterationType - The `iterationType` parameter is used to determine whether to calculate the
|
|
376
382
|
* height of the binary tree using a recursive approach or an iterative approach. It can have two
|
|
377
383
|
* possible values:
|
|
378
384
|
* @returns the height of the binary tree.
|
|
379
385
|
*/
|
|
380
|
-
getHeight(beginRoot:
|
|
386
|
+
getHeight(beginRoot: BTNKey | N | null = this.root, iterationType = this.iterationType): number {
|
|
381
387
|
if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot);
|
|
382
388
|
if (!beginRoot) return -1;
|
|
383
389
|
|
|
@@ -479,11 +485,35 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
479
485
|
return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
|
|
480
486
|
}
|
|
481
487
|
|
|
488
|
+
getNodes<C extends BTNCallback<N, BTNKey>>(
|
|
489
|
+
identifier: BTNKey,
|
|
490
|
+
callback: C,
|
|
491
|
+
onlyOne?: boolean,
|
|
492
|
+
beginRoot?: N | null,
|
|
493
|
+
iterationType?: IterationType
|
|
494
|
+
): N[];
|
|
495
|
+
|
|
496
|
+
getNodes<C extends BTNCallback<N, N>>(
|
|
497
|
+
identifier: N | null,
|
|
498
|
+
callback: C,
|
|
499
|
+
onlyOne?: boolean,
|
|
500
|
+
beginRoot?: N | null,
|
|
501
|
+
iterationType?: IterationType
|
|
502
|
+
): N[];
|
|
503
|
+
|
|
504
|
+
getNodes<C extends BTNCallback<N>>(
|
|
505
|
+
identifier: ReturnType<C>,
|
|
506
|
+
callback: C,
|
|
507
|
+
onlyOne?: boolean,
|
|
508
|
+
beginRoot?: N | null,
|
|
509
|
+
iterationType?: IterationType
|
|
510
|
+
): N[];
|
|
511
|
+
|
|
482
512
|
/**
|
|
483
513
|
* The function `getNodes` returns an array of nodes that match a given node property, using either
|
|
484
514
|
* recursive or iterative traversal.
|
|
485
515
|
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a
|
|
486
|
-
* `
|
|
516
|
+
* `BTNKey` or a generic type `N`. It represents the property of the node that we are
|
|
487
517
|
* searching for. It can be a specific key value or any other property of the node.
|
|
488
518
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
489
519
|
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
@@ -500,7 +530,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
500
530
|
* traverse the binary tree. It can have two possible values:
|
|
501
531
|
* @returns The function `getNodes` returns an array of nodes (`N[]`).
|
|
502
532
|
*/
|
|
503
|
-
getNodes<C extends
|
|
533
|
+
getNodes<C extends BTNCallback<N>>(
|
|
504
534
|
identifier: ReturnType<C> | null,
|
|
505
535
|
callback: C = this._defaultCallbackByKey as C,
|
|
506
536
|
onlyOne = false,
|
|
@@ -541,10 +571,31 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
541
571
|
return ans;
|
|
542
572
|
}
|
|
543
573
|
|
|
574
|
+
has<C extends BTNCallback<N, BTNKey>>(
|
|
575
|
+
identifier: BTNKey,
|
|
576
|
+
callback?: C,
|
|
577
|
+
beginRoot?: N,
|
|
578
|
+
iterationType?: IterationType
|
|
579
|
+
): boolean;
|
|
580
|
+
|
|
581
|
+
has<C extends BTNCallback<N, N>>(
|
|
582
|
+
identifier: N | null,
|
|
583
|
+
callback?: C,
|
|
584
|
+
beginRoot?: N,
|
|
585
|
+
iterationType?: IterationType
|
|
586
|
+
): boolean;
|
|
587
|
+
|
|
588
|
+
has<C extends BTNCallback<N>>(
|
|
589
|
+
identifier: ReturnType<C> | null,
|
|
590
|
+
callback: C,
|
|
591
|
+
beginRoot?: N,
|
|
592
|
+
iterationType?: IterationType
|
|
593
|
+
): boolean;
|
|
594
|
+
|
|
544
595
|
/**
|
|
545
596
|
* The function checks if a binary tree has a node with a given property or key.
|
|
546
|
-
* @param {
|
|
547
|
-
* the node that you want to find in the binary tree. It can be either a `
|
|
597
|
+
* @param {BTNKey | N} identifier - The `identifier` parameter is the key or value of
|
|
598
|
+
* the node that you want to find in the binary tree. It can be either a `BTNKey` or a
|
|
548
599
|
* generic type `N`.
|
|
549
600
|
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
550
601
|
* matches the desired criteria. It takes a node as input and returns a boolean value indicating
|
|
@@ -558,7 +609,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
558
609
|
* performed when searching for nodes in the binary tree. It can have one of the following values:
|
|
559
610
|
* @returns a boolean value.
|
|
560
611
|
*/
|
|
561
|
-
has<C extends
|
|
612
|
+
has<C extends BTNCallback<N>>(
|
|
562
613
|
identifier: ReturnType<C> | null,
|
|
563
614
|
callback: C = this._defaultCallbackByKey as C,
|
|
564
615
|
beginRoot = this.root,
|
|
@@ -569,10 +620,31 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
569
620
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
570
621
|
}
|
|
571
622
|
|
|
623
|
+
get<C extends BTNCallback<N, BTNKey>>(
|
|
624
|
+
identifier: BTNKey,
|
|
625
|
+
callback?: C,
|
|
626
|
+
beginRoot?: N,
|
|
627
|
+
iterationType?: IterationType
|
|
628
|
+
): N | null;
|
|
629
|
+
|
|
630
|
+
get<C extends BTNCallback<N, N>>(
|
|
631
|
+
identifier: N | null,
|
|
632
|
+
callback?: C,
|
|
633
|
+
beginRoot?: N,
|
|
634
|
+
iterationType?: IterationType
|
|
635
|
+
): N | null;
|
|
636
|
+
|
|
637
|
+
get<C extends BTNCallback<N>>(
|
|
638
|
+
identifier: ReturnType<C>,
|
|
639
|
+
callback: C,
|
|
640
|
+
beginRoot?: N,
|
|
641
|
+
iterationType?: IterationType
|
|
642
|
+
): N | null;
|
|
643
|
+
|
|
572
644
|
/**
|
|
573
645
|
* The function `get` returns the first node in a binary tree that matches the given property or key.
|
|
574
|
-
* @param {
|
|
575
|
-
* the node that you want to find in the binary tree. It can be either a `
|
|
646
|
+
* @param {BTNKey | N} identifier - The `identifier` parameter is the key or value of
|
|
647
|
+
* the node that you want to find in the binary tree. It can be either a `BTNKey` or `N`
|
|
576
648
|
* type.
|
|
577
649
|
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
578
650
|
* matches the desired criteria. It takes a node as input and returns a boolean value indicating
|
|
@@ -584,7 +656,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
584
656
|
* performed when searching for a node in the binary tree. It can have one of the following values:
|
|
585
657
|
* @returns either the found node (of type N) or null if no node is found.
|
|
586
658
|
*/
|
|
587
|
-
get<C extends
|
|
659
|
+
get<C extends BTNCallback<N>>(
|
|
588
660
|
identifier: ReturnType<C> | null,
|
|
589
661
|
callback: C = this._defaultCallbackByKey as C,
|
|
590
662
|
beginRoot = this.root,
|
|
@@ -621,15 +693,15 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
621
693
|
/**
|
|
622
694
|
* The function `getLeftMost` returns the leftmost node in a binary tree, either using recursive or
|
|
623
695
|
* iterative traversal.
|
|
624
|
-
* @param {
|
|
696
|
+
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter is the starting point
|
|
625
697
|
* for finding the leftmost node in a binary tree. It can be either a node object (`N`), a key value
|
|
626
|
-
* of a node (`
|
|
698
|
+
* of a node (`BTNKey`), or `null` if the tree is empty.
|
|
627
699
|
* @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
|
|
628
700
|
* be performed when finding the leftmost node in a binary tree. It can have two possible values:
|
|
629
701
|
* @returns The function `getLeftMost` returns the leftmost node (`N`) in a binary tree. If there is
|
|
630
702
|
* no leftmost node, it returns `null`.
|
|
631
703
|
*/
|
|
632
|
-
getLeftMost(beginRoot:
|
|
704
|
+
getLeftMost(beginRoot: BTNKey | N | null = this.root, iterationType = this.iterationType): N | null {
|
|
633
705
|
if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot);
|
|
634
706
|
|
|
635
707
|
if (!beginRoot) return beginRoot;
|
|
@@ -699,7 +771,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
699
771
|
if (!beginRoot) return true;
|
|
700
772
|
|
|
701
773
|
if (iterationType === IterationType.RECURSIVE) {
|
|
702
|
-
const dfs = (cur: N | null | undefined, min:
|
|
774
|
+
const dfs = (cur: N | null | undefined, min: BTNKey, max: BTNKey): boolean => {
|
|
703
775
|
if (!cur) return true;
|
|
704
776
|
if (cur.key <= min || cur.key >= max) return false;
|
|
705
777
|
return dfs(cur.left, min, cur.key) && dfs(cur.right, cur.key, max);
|
|
@@ -744,21 +816,21 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
744
816
|
* subtree traversal. It takes a single argument, which is the current node being traversed, and
|
|
745
817
|
* returns a value. The return values from each callback invocation will be collected and returned as
|
|
746
818
|
* an array.
|
|
747
|
-
* @param {
|
|
819
|
+
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter is the starting point
|
|
748
820
|
* for traversing the subtree. It can be either a node object, a key value of a node, or `null` to
|
|
749
821
|
* start from the root of the tree.
|
|
750
822
|
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
751
823
|
* performed on the binary tree. It can have two possible values:
|
|
752
|
-
* @returns The function `subTreeTraverse` returns an array of `ReturnType<
|
|
824
|
+
* @returns The function `subTreeTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
|
|
753
825
|
*/
|
|
754
|
-
subTreeTraverse<C extends
|
|
826
|
+
subTreeTraverse<C extends BTNCallback<N>>(
|
|
755
827
|
callback: C = this._defaultCallbackByKey as C,
|
|
756
|
-
beginRoot:
|
|
828
|
+
beginRoot: BTNKey | N | null = this.root,
|
|
757
829
|
iterationType = this.iterationType
|
|
758
830
|
): ReturnType<C>[] {
|
|
759
831
|
if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot);
|
|
760
832
|
|
|
761
|
-
const ans: ReturnType<
|
|
833
|
+
const ans: ReturnType<BTNCallback<N>>[] = [];
|
|
762
834
|
if (!beginRoot) return ans;
|
|
763
835
|
|
|
764
836
|
if (iterationType === IterationType.RECURSIVE) {
|
|
@@ -796,16 +868,16 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
796
868
|
* is `null`, an empty array will be returned.
|
|
797
869
|
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
798
870
|
* iteration used in the depth-first search algorithm. It can have two possible values:
|
|
799
|
-
* @returns The function `dfs` returns an array of `ReturnType<
|
|
871
|
+
* @returns The function `dfs` returns an array of `ReturnType<BTNCallback<N>>` values.
|
|
800
872
|
*/
|
|
801
|
-
dfs<C extends
|
|
873
|
+
dfs<C extends BTNCallback<N>>(
|
|
802
874
|
callback: C = this._defaultCallbackByKey as C,
|
|
803
875
|
pattern: DFSOrderPattern = 'in',
|
|
804
876
|
beginRoot: N | null = this.root,
|
|
805
877
|
iterationType: IterationType = IterationType.ITERATIVE
|
|
806
878
|
): ReturnType<C>[] {
|
|
807
879
|
if (!beginRoot) return [];
|
|
808
|
-
const ans: ReturnType<
|
|
880
|
+
const ans: ReturnType<BTNCallback<N>>[] = [];
|
|
809
881
|
if (iterationType === IterationType.RECURSIVE) {
|
|
810
882
|
const _traverse = (node: N) => {
|
|
811
883
|
switch (pattern) {
|
|
@@ -874,22 +946,22 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
874
946
|
* function on each node.
|
|
875
947
|
* @param callback - The `callback` parameter is a function that will be called for each node in the
|
|
876
948
|
* breadth-first search. It takes a node of type `N` as its argument and returns a value of type
|
|
877
|
-
* `ReturnType<
|
|
949
|
+
* `ReturnType<BTNCallback<N>>`. The default value for this parameter is `this._defaultCallbackByKey
|
|
878
950
|
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
|
|
879
951
|
* search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
|
|
880
952
|
* will not be performed and an empty array will be returned.
|
|
881
953
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be used
|
|
882
954
|
* in the breadth-first search (BFS) algorithm. It can have two possible values:
|
|
883
|
-
* @returns The function `bfs` returns an array of `ReturnType<
|
|
955
|
+
* @returns The function `bfs` returns an array of `ReturnType<BTNCallback<N>>[]`.
|
|
884
956
|
*/
|
|
885
|
-
bfs<C extends
|
|
957
|
+
bfs<C extends BTNCallback<N>>(
|
|
886
958
|
callback: C = this._defaultCallbackByKey as C,
|
|
887
959
|
beginRoot: N | null = this.root,
|
|
888
960
|
iterationType = this.iterationType
|
|
889
961
|
): ReturnType<C>[] {
|
|
890
962
|
if (!beginRoot) return [];
|
|
891
963
|
|
|
892
|
-
const ans: ReturnType<
|
|
964
|
+
const ans: ReturnType<BTNCallback<N>>[] = [];
|
|
893
965
|
|
|
894
966
|
if (iterationType === IterationType.RECURSIVE) {
|
|
895
967
|
const queue = new Queue<N>([beginRoot]);
|
|
@@ -939,7 +1011,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
939
1011
|
* level in a binary tree. Each inner array contains the return type of the provided callback
|
|
940
1012
|
* function `C` applied to the nodes at that level.
|
|
941
1013
|
*/
|
|
942
|
-
listLevels<C extends
|
|
1014
|
+
listLevels<C extends BTNCallback<N>>(
|
|
943
1015
|
callback: C = this._defaultCallbackByKey as C,
|
|
944
1016
|
beginRoot: N | null = this.root,
|
|
945
1017
|
iterationType = this.iterationType
|
|
@@ -998,7 +1070,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
998
1070
|
* The `morris` function performs a depth-first traversal of a binary tree using the Morris traversal
|
|
999
1071
|
* algorithm and returns an array of values obtained by applying a callback function to each node.
|
|
1000
1072
|
* @param callback - The `callback` parameter is a function that will be called on each node in the
|
|
1001
|
-
* tree. It takes a node of type `N` as input and returns a value of type `ReturnType<
|
|
1073
|
+
* tree. It takes a node of type `N` as input and returns a value of type `ReturnType<BTNCallback<N>>`. The
|
|
1002
1074
|
* default value for this parameter is `this._defaultCallbackByKey`.
|
|
1003
1075
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
|
|
1004
1076
|
* determines the order in which the nodes of a binary tree are traversed. It can have one of the
|
|
@@ -1006,15 +1078,15 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1006
1078
|
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the Morris
|
|
1007
1079
|
* traversal. It specifies the root node of the tree from which the traversal should begin. If
|
|
1008
1080
|
* `beginRoot` is `null`, an empty array will be returned.
|
|
1009
|
-
* @returns The `morris` function returns an array of `ReturnType<
|
|
1081
|
+
* @returns The `morris` function returns an array of `ReturnType<BTNCallback<N>>` values.
|
|
1010
1082
|
*/
|
|
1011
|
-
morris<C extends
|
|
1083
|
+
morris<C extends BTNCallback<N>>(
|
|
1012
1084
|
callback: C = this._defaultCallbackByKey as C,
|
|
1013
1085
|
pattern: DFSOrderPattern = 'in',
|
|
1014
1086
|
beginRoot: N | null = this.root
|
|
1015
1087
|
): ReturnType<C>[] {
|
|
1016
1088
|
if (beginRoot === null) return [];
|
|
1017
|
-
const ans: ReturnType<
|
|
1089
|
+
const ans: ReturnType<BTNCallback<N>>[] = [];
|
|
1018
1090
|
|
|
1019
1091
|
let cur: N | null | undefined = beginRoot;
|
|
1020
1092
|
const _reverseEdge = (node: N | null | undefined) => {
|
|
@@ -1093,6 +1165,46 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1093
1165
|
return ans;
|
|
1094
1166
|
}
|
|
1095
1167
|
|
|
1168
|
+
/**
|
|
1169
|
+
* The above function is an iterator for a binary tree that can be used to traverse the tree in
|
|
1170
|
+
* either an iterative or recursive manner.
|
|
1171
|
+
* @param node - The `node` parameter represents the current node in the binary tree from which the
|
|
1172
|
+
* iteration starts. It is an optional parameter with a default value of `this.root`, which means
|
|
1173
|
+
* that if no node is provided, the iteration will start from the root of the binary tree.
|
|
1174
|
+
* @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
|
|
1175
|
+
* binary tree nodes in a specific order.
|
|
1176
|
+
*/
|
|
1177
|
+
*[Symbol.iterator](node = this.root): Generator<BTNKey, void, undefined> {
|
|
1178
|
+
if (!node) {
|
|
1179
|
+
return;
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
if (this.iterationType === IterationType.ITERATIVE) {
|
|
1183
|
+
const stack: (N | null | undefined)[] = [];
|
|
1184
|
+
let current: N | null | undefined = node;
|
|
1185
|
+
|
|
1186
|
+
while (current || stack.length > 0) {
|
|
1187
|
+
while (current) {
|
|
1188
|
+
stack.push(current);
|
|
1189
|
+
current = current.left;
|
|
1190
|
+
}
|
|
1191
|
+
|
|
1192
|
+
current = stack.pop();
|
|
1193
|
+
|
|
1194
|
+
if (current) yield current.key;
|
|
1195
|
+
if (current) current = current.right;
|
|
1196
|
+
}
|
|
1197
|
+
} else {
|
|
1198
|
+
if (node.left) {
|
|
1199
|
+
yield* this[Symbol.iterator](node.left);
|
|
1200
|
+
}
|
|
1201
|
+
yield node.key;
|
|
1202
|
+
if (node.right) {
|
|
1203
|
+
yield* this[Symbol.iterator](node.right);
|
|
1204
|
+
}
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1096
1208
|
/**
|
|
1097
1209
|
* Swap the data of two nodes in the binary tree.
|
|
1098
1210
|
* @param {N} srcNode - The source node to swap.
|
|
@@ -1121,7 +1233,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1121
1233
|
* the tree's structure should be restored to its original state to maintain the tree's integrity.
|
|
1122
1234
|
* This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
|
|
1123
1235
|
*/
|
|
1124
|
-
protected _defaultCallbackByKey:
|
|
1236
|
+
protected _defaultCallbackByKey: BTNCallback<N, BTNKey> = node => node.key;
|
|
1125
1237
|
|
|
1126
1238
|
/**
|
|
1127
1239
|
* The function `_addTo` adds a new node to a binary tree if there is an available position.
|