linked-list-typed 1.38.9 → 1.39.1
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 +2 -2
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +8 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +17 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +86 -32
- package/dist/data-structures/binary-tree/binary-tree.js +8 -8
- package/dist/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/data-structures/binary-tree/bst.js +2 -2
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +2 -2
- package/dist/data-structures/graph/abstract-graph.d.ts +4 -4
- package/dist/data-structures/graph/abstract-graph.js +5 -5
- package/dist/data-structures/graph/directed-graph.d.ts +4 -4
- package/dist/data-structures/graph/directed-graph.js +6 -6
- package/dist/data-structures/graph/map-graph.d.ts +1 -1
- package/dist/data-structures/graph/map-graph.js +1 -1
- package/dist/data-structures/graph/undirected-graph.d.ts +3 -3
- package/dist/data-structures/graph/undirected-graph.js +4 -4
- package/dist/data-structures/heap/heap.d.ts +10 -5
- package/dist/data-structures/heap/heap.js +10 -10
- package/dist/data-structures/heap/max-heap.d.ts +4 -1
- package/dist/data-structures/heap/max-heap.js +9 -7
- package/dist/data-structures/heap/min-heap.d.ts +4 -1
- package/dist/data-structures/heap/min-heap.js +9 -7
- package/dist/data-structures/matrix/matrix2d.d.ts +1 -2
- package/dist/data-structures/matrix/matrix2d.js +3 -7
- package/dist/data-structures/matrix/vector2d.d.ts +0 -1
- package/dist/data-structures/matrix/vector2d.js +0 -1
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +4 -1
- package/dist/data-structures/priority-queue/max-priority-queue.js +9 -7
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +4 -1
- package/dist/data-structures/priority-queue/min-priority-queue.js +9 -7
- package/dist/data-structures/priority-queue/priority-queue.d.ts +5 -2
- package/dist/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/interfaces/binary-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -2
- package/dist/types/helpers.d.ts +1 -4
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +5 -4
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +21 -1
- package/src/data-structures/binary-tree/binary-tree.ts +37 -93
- package/src/data-structures/binary-tree/bst.ts +11 -17
- package/src/data-structures/binary-tree/rb-tree.ts +2 -1
- package/src/data-structures/binary-tree/tree-multiset.ts +4 -3
- package/src/data-structures/graph/abstract-graph.ts +16 -15
- package/src/data-structures/graph/directed-graph.ts +8 -7
- package/src/data-structures/graph/map-graph.ts +2 -2
- package/src/data-structures/graph/undirected-graph.ts +9 -8
- package/src/data-structures/hash/hash-map.ts +1 -1
- package/src/data-structures/hash/tree-map.ts +1 -2
- package/src/data-structures/hash/tree-set.ts +1 -2
- package/src/data-structures/heap/heap.ts +12 -12
- package/src/data-structures/heap/max-heap.ts +8 -6
- package/src/data-structures/heap/min-heap.ts +8 -6
- package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
- package/src/data-structures/matrix/matrix.ts +1 -1
- package/src/data-structures/matrix/matrix2d.ts +1 -3
- package/src/data-structures/matrix/vector2d.ts +1 -4
- package/src/data-structures/priority-queue/max-priority-queue.ts +8 -6
- package/src/data-structures/priority-queue/min-priority-queue.ts +8 -6
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/src/data-structures/queue/deque.ts +4 -5
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/interfaces/binary-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/binary-tree.ts +0 -4
- package/src/types/data-structures/matrix/navigator.ts +1 -1
- package/src/types/helpers.ts +1 -7
- package/src/types/utils/utils.ts +1 -1
- package/src/types/utils/validate-type.ts +2 -2
|
@@ -6,16 +6,8 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import type {
|
|
10
|
-
|
|
11
|
-
BFSCallbackReturn,
|
|
12
|
-
BinaryTreeNodeKey,
|
|
13
|
-
BinaryTreeNodeNested,
|
|
14
|
-
BinaryTreeOptions,
|
|
15
|
-
MapCallback,
|
|
16
|
-
MapCallbackReturn
|
|
17
|
-
} from '../../types';
|
|
18
|
-
import {BinaryTreeDeletedResult, DefaultMapCallback, DFSOrderPattern, FamilyPosition, IterationType} from '../../types';
|
|
9
|
+
import type {OneParamCallback, BinaryTreeNodeKey, BinaryTreeNodeNested, BinaryTreeOptions} from '../../types';
|
|
10
|
+
import {BinaryTreeDeletedResult, DFSOrderPattern, FamilyPosition, IterationType} from '../../types';
|
|
19
11
|
import {IBinaryTree} from '../../interfaces';
|
|
20
12
|
import {trampoline} from '../../utils';
|
|
21
13
|
import {Queue} from '../queue';
|
|
@@ -116,7 +108,8 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
|
|
|
116
108
|
* @template N - The type of the binary tree's nodes.
|
|
117
109
|
*/
|
|
118
110
|
export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>>
|
|
119
|
-
implements IBinaryTree<V, N>
|
|
111
|
+
implements IBinaryTree<V, N>
|
|
112
|
+
{
|
|
120
113
|
/**
|
|
121
114
|
* Creates a new instance of BinaryTree.
|
|
122
115
|
* @param {BinaryTreeOptions} [options] - The options for the binary tree.
|
|
@@ -285,10 +278,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
285
278
|
return keysOrNodes.length === this.addMany(keysOrNodes, data).length;
|
|
286
279
|
}
|
|
287
280
|
|
|
288
|
-
delete<C extends MapCallback<N>>(identifier: ReturnType<C> | N): BinaryTreeDeletedResult<N>[];
|
|
289
|
-
|
|
290
|
-
delete<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): BinaryTreeDeletedResult<N>[];
|
|
291
|
-
|
|
292
281
|
/**
|
|
293
282
|
* The `delete` function removes a node from a binary search tree and returns the deleted node along
|
|
294
283
|
* with the parent node that needs to be balanced.
|
|
@@ -303,13 +292,13 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
303
292
|
* included in the result. The `callback` parameter has a default value of
|
|
304
293
|
* `this._defaultCallbackByKey`, which
|
|
305
294
|
*/
|
|
306
|
-
delete<C extends
|
|
307
|
-
identifier: ReturnType<C> |
|
|
295
|
+
delete<C extends OneParamCallback<N>>(
|
|
296
|
+
identifier: ReturnType<C> | null,
|
|
308
297
|
callback: C = this._defaultCallbackByKey as C
|
|
309
298
|
): BinaryTreeDeletedResult<N>[] {
|
|
310
299
|
const bstDeletedResult: BinaryTreeDeletedResult<N>[] = [];
|
|
311
300
|
if (!this.root) return bstDeletedResult;
|
|
312
|
-
if (identifier instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
301
|
+
if ((identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
313
302
|
|
|
314
303
|
const curr = this.get(identifier, callback);
|
|
315
304
|
if (!curr) return bstDeletedResult;
|
|
@@ -320,7 +309,8 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
320
309
|
|
|
321
310
|
if (!curr.left) {
|
|
322
311
|
if (!parent) {
|
|
323
|
-
|
|
312
|
+
// Handle the case when there's only one root node
|
|
313
|
+
this._setRoot(null);
|
|
324
314
|
} else {
|
|
325
315
|
const {familyPosition: fp} = curr;
|
|
326
316
|
if (fp === FamilyPosition.LEFT || fp === FamilyPosition.ROOT_LEFT) {
|
|
@@ -405,7 +395,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
405
395
|
return -1;
|
|
406
396
|
}
|
|
407
397
|
|
|
408
|
-
const stack: {
|
|
398
|
+
const stack: {node: N; depth: number}[] = [{node: beginRoot, depth: 0}];
|
|
409
399
|
let maxHeight = 0;
|
|
410
400
|
|
|
411
401
|
while (stack.length > 0) {
|
|
@@ -489,29 +479,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
489
479
|
return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
|
|
490
480
|
}
|
|
491
481
|
|
|
492
|
-
getNodes<C extends MapCallback<N>>(identifier: ReturnType<C> | N): N[];
|
|
493
|
-
|
|
494
|
-
getNodes<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): N[];
|
|
495
|
-
|
|
496
|
-
getNodes<C extends MapCallback<N>>(identifier: ReturnType<C> | N, onlyOne: boolean): N[];
|
|
497
|
-
|
|
498
|
-
getNodes<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C, onlyOne: boolean): N[];
|
|
499
|
-
|
|
500
|
-
getNodes<C extends MapCallback<N>>(
|
|
501
|
-
identifier: ReturnType<C> | N,
|
|
502
|
-
callback: C,
|
|
503
|
-
onlyOne: boolean,
|
|
504
|
-
beginRoot: N | null
|
|
505
|
-
): N[];
|
|
506
|
-
|
|
507
|
-
getNodes<C extends MapCallback<N>>(
|
|
508
|
-
identifier: ReturnType<C> | N,
|
|
509
|
-
callback: C,
|
|
510
|
-
onlyOne: boolean,
|
|
511
|
-
beginRoot: N | null,
|
|
512
|
-
iterationType: IterationType
|
|
513
|
-
): N[];
|
|
514
|
-
|
|
515
482
|
/**
|
|
516
483
|
* The function `getNodes` returns an array of nodes that match a given node property, using either
|
|
517
484
|
* recursive or iterative traversal.
|
|
@@ -533,15 +500,15 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
533
500
|
* traverse the binary tree. It can have two possible values:
|
|
534
501
|
* @returns The function `getNodes` returns an array of nodes (`N[]`).
|
|
535
502
|
*/
|
|
536
|
-
getNodes<C extends
|
|
537
|
-
identifier: ReturnType<C> |
|
|
503
|
+
getNodes<C extends OneParamCallback<N>>(
|
|
504
|
+
identifier: ReturnType<C> | null,
|
|
538
505
|
callback: C = this._defaultCallbackByKey as C,
|
|
539
506
|
onlyOne = false,
|
|
540
507
|
beginRoot: N | null = this.root,
|
|
541
508
|
iterationType = this.iterationType
|
|
542
509
|
): N[] {
|
|
543
510
|
if (!beginRoot) return [];
|
|
544
|
-
if (identifier instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
511
|
+
if ((identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
545
512
|
const ans: N[] = [];
|
|
546
513
|
|
|
547
514
|
if (iterationType === IterationType.RECURSIVE) {
|
|
@@ -574,14 +541,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
574
541
|
return ans;
|
|
575
542
|
}
|
|
576
543
|
|
|
577
|
-
has<C extends MapCallback<N>>(identifier: ReturnType<C> | N): boolean;
|
|
578
|
-
|
|
579
|
-
has<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): boolean;
|
|
580
|
-
|
|
581
|
-
has<C extends MapCallback<N>>(identifier: ReturnType<C> | N, beginRoot: N | null): boolean;
|
|
582
|
-
|
|
583
|
-
has<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C, beginRoot: N | null): boolean;
|
|
584
|
-
|
|
585
544
|
/**
|
|
586
545
|
* The function checks if a binary tree has a node with a given property or key.
|
|
587
546
|
* @param {BinaryTreeNodeKey | N} identifier - The `identifier` parameter is the key or value of
|
|
@@ -599,32 +558,17 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
599
558
|
* performed when searching for nodes in the binary tree. It can have one of the following values:
|
|
600
559
|
* @returns a boolean value.
|
|
601
560
|
*/
|
|
602
|
-
has<C extends
|
|
603
|
-
identifier: ReturnType<C> |
|
|
561
|
+
has<C extends OneParamCallback<N>>(
|
|
562
|
+
identifier: ReturnType<C> | null,
|
|
604
563
|
callback: C = this._defaultCallbackByKey as C,
|
|
605
564
|
beginRoot = this.root,
|
|
606
565
|
iterationType = this.iterationType
|
|
607
566
|
): boolean {
|
|
608
|
-
if (identifier instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
567
|
+
if ((identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
609
568
|
// TODO may support finding node by value equal
|
|
610
569
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
611
570
|
}
|
|
612
571
|
|
|
613
|
-
get<C extends MapCallback<N>>(identifier: ReturnType<C> | N): N | null;
|
|
614
|
-
|
|
615
|
-
get<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): N | null;
|
|
616
|
-
|
|
617
|
-
get<C extends MapCallback<N>>(identifier: ReturnType<C> | N, beginRoot: N | null): N | null;
|
|
618
|
-
|
|
619
|
-
get<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C, beginRoot: N | null): N | null;
|
|
620
|
-
|
|
621
|
-
get<C extends MapCallback<N>>(
|
|
622
|
-
identifier: ReturnType<C> | N,
|
|
623
|
-
callback: C,
|
|
624
|
-
beginRoot: N | null,
|
|
625
|
-
iterationType: IterationType
|
|
626
|
-
): N | null;
|
|
627
|
-
|
|
628
572
|
/**
|
|
629
573
|
* The function `get` returns the first node in a binary tree that matches the given property or key.
|
|
630
574
|
* @param {BinaryTreeNodeKey | N} identifier - The `identifier` parameter is the key or value of
|
|
@@ -640,13 +584,13 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
640
584
|
* performed when searching for a node in the binary tree. It can have one of the following values:
|
|
641
585
|
* @returns either the found node (of type N) or null if no node is found.
|
|
642
586
|
*/
|
|
643
|
-
get<C extends
|
|
644
|
-
identifier: ReturnType<C> |
|
|
587
|
+
get<C extends OneParamCallback<N>>(
|
|
588
|
+
identifier: ReturnType<C> | null,
|
|
645
589
|
callback: C = this._defaultCallbackByKey as C,
|
|
646
590
|
beginRoot = this.root,
|
|
647
591
|
iterationType = this.iterationType
|
|
648
592
|
): N | null {
|
|
649
|
-
if (identifier instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
593
|
+
if ((identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
650
594
|
// TODO may support finding node by value equal
|
|
651
595
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
|
|
652
596
|
}
|
|
@@ -750,7 +694,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
750
694
|
* possible values:
|
|
751
695
|
* @returns The function `isSubtreeBST` returns a boolean value.
|
|
752
696
|
*/
|
|
753
|
-
isSubtreeBST(beginRoot: N, iterationType = this.iterationType): boolean {
|
|
697
|
+
isSubtreeBST(beginRoot: N | null, iterationType = this.iterationType): boolean {
|
|
754
698
|
// TODO there is a bug
|
|
755
699
|
if (!beginRoot) return true;
|
|
756
700
|
|
|
@@ -805,16 +749,16 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
805
749
|
* start from the root of the tree.
|
|
806
750
|
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
807
751
|
* performed on the binary tree. It can have two possible values:
|
|
808
|
-
* @returns The function `subTreeTraverse` returns an array of `
|
|
752
|
+
* @returns The function `subTreeTraverse` returns an array of `ReturnType<OneParamCallback<N>>`.
|
|
809
753
|
*/
|
|
810
|
-
subTreeTraverse<C extends
|
|
754
|
+
subTreeTraverse<C extends OneParamCallback<N>>(
|
|
811
755
|
callback: C = this._defaultCallbackByKey as C,
|
|
812
756
|
beginRoot: BinaryTreeNodeKey | N | null = this.root,
|
|
813
757
|
iterationType = this.iterationType
|
|
814
758
|
): ReturnType<C>[] {
|
|
815
759
|
if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot);
|
|
816
760
|
|
|
817
|
-
const ans:
|
|
761
|
+
const ans: ReturnType<OneParamCallback<N>>[] = [];
|
|
818
762
|
if (!beginRoot) return ans;
|
|
819
763
|
|
|
820
764
|
if (iterationType === IterationType.RECURSIVE) {
|
|
@@ -852,16 +796,16 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
852
796
|
* is `null`, an empty array will be returned.
|
|
853
797
|
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
854
798
|
* iteration used in the depth-first search algorithm. It can have two possible values:
|
|
855
|
-
* @returns The function `dfs` returns an array of `
|
|
799
|
+
* @returns The function `dfs` returns an array of `ReturnType<OneParamCallback<N>>` values.
|
|
856
800
|
*/
|
|
857
|
-
dfs<C extends
|
|
801
|
+
dfs<C extends OneParamCallback<N>>(
|
|
858
802
|
callback: C = this._defaultCallbackByKey as C,
|
|
859
803
|
pattern: DFSOrderPattern = 'in',
|
|
860
804
|
beginRoot: N | null = this.root,
|
|
861
805
|
iterationType: IterationType = IterationType.ITERATIVE
|
|
862
806
|
): ReturnType<C>[] {
|
|
863
807
|
if (!beginRoot) return [];
|
|
864
|
-
const ans:
|
|
808
|
+
const ans: ReturnType<OneParamCallback<N>>[] = [];
|
|
865
809
|
if (iterationType === IterationType.RECURSIVE) {
|
|
866
810
|
const _traverse = (node: N) => {
|
|
867
811
|
switch (pattern) {
|
|
@@ -888,7 +832,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
888
832
|
_traverse(beginRoot);
|
|
889
833
|
} else {
|
|
890
834
|
// 0: visit, 1: print
|
|
891
|
-
const stack: {
|
|
835
|
+
const stack: {opt: 0 | 1; node: N | null | undefined}[] = [{opt: 0, node: beginRoot}];
|
|
892
836
|
|
|
893
837
|
while (stack.length > 0) {
|
|
894
838
|
const cur = stack.pop();
|
|
@@ -930,22 +874,22 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
930
874
|
* function on each node.
|
|
931
875
|
* @param callback - The `callback` parameter is a function that will be called for each node in the
|
|
932
876
|
* breadth-first search. It takes a node of type `N` as its argument and returns a value of type
|
|
933
|
-
* `
|
|
877
|
+
* `ReturnType<OneParamCallback<N>>`. The default value for this parameter is `this._defaultCallbackByKey
|
|
934
878
|
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
|
|
935
879
|
* search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
|
|
936
880
|
* will not be performed and an empty array will be returned.
|
|
937
881
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be used
|
|
938
882
|
* in the breadth-first search (BFS) algorithm. It can have two possible values:
|
|
939
|
-
* @returns The function `bfs` returns an array of `
|
|
883
|
+
* @returns The function `bfs` returns an array of `ReturnType<OneParamCallback<N>>[]`.
|
|
940
884
|
*/
|
|
941
|
-
bfs<C extends
|
|
885
|
+
bfs<C extends OneParamCallback<N>>(
|
|
942
886
|
callback: C = this._defaultCallbackByKey as C,
|
|
943
887
|
beginRoot: N | null = this.root,
|
|
944
888
|
iterationType = this.iterationType
|
|
945
889
|
): ReturnType<C>[] {
|
|
946
890
|
if (!beginRoot) return [];
|
|
947
891
|
|
|
948
|
-
const ans:
|
|
892
|
+
const ans: ReturnType<OneParamCallback<N>>[] = [];
|
|
949
893
|
|
|
950
894
|
if (iterationType === IterationType.RECURSIVE) {
|
|
951
895
|
const queue = new Queue<N>([beginRoot]);
|
|
@@ -995,7 +939,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
995
939
|
* level in a binary tree. Each inner array contains the return type of the provided callback
|
|
996
940
|
* function `C` applied to the nodes at that level.
|
|
997
941
|
*/
|
|
998
|
-
listLevels<C extends
|
|
942
|
+
listLevels<C extends OneParamCallback<N>>(
|
|
999
943
|
callback: C = this._defaultCallbackByKey as C,
|
|
1000
944
|
beginRoot: N | null = this.root,
|
|
1001
945
|
iterationType = this.iterationType
|
|
@@ -1054,7 +998,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1054
998
|
* The `morris` function performs a depth-first traversal of a binary tree using the Morris traversal
|
|
1055
999
|
* algorithm and returns an array of values obtained by applying a callback function to each node.
|
|
1056
1000
|
* @param callback - The `callback` parameter is a function that will be called on each node in the
|
|
1057
|
-
* tree. It takes a node of type `N` as input and returns a value of type `
|
|
1001
|
+
* tree. It takes a node of type `N` as input and returns a value of type `ReturnType<OneParamCallback<N>>`. The
|
|
1058
1002
|
* default value for this parameter is `this._defaultCallbackByKey`.
|
|
1059
1003
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
|
|
1060
1004
|
* determines the order in which the nodes of a binary tree are traversed. It can have one of the
|
|
@@ -1062,15 +1006,15 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1062
1006
|
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the Morris
|
|
1063
1007
|
* traversal. It specifies the root node of the tree from which the traversal should begin. If
|
|
1064
1008
|
* `beginRoot` is `null`, an empty array will be returned.
|
|
1065
|
-
* @returns The `morris` function returns an array of `
|
|
1009
|
+
* @returns The `morris` function returns an array of `ReturnType<OneParamCallback<N>>` values.
|
|
1066
1010
|
*/
|
|
1067
|
-
morris<C extends
|
|
1011
|
+
morris<C extends OneParamCallback<N>>(
|
|
1068
1012
|
callback: C = this._defaultCallbackByKey as C,
|
|
1069
1013
|
pattern: DFSOrderPattern = 'in',
|
|
1070
1014
|
beginRoot: N | null = this.root
|
|
1071
1015
|
): ReturnType<C>[] {
|
|
1072
1016
|
if (beginRoot === null) return [];
|
|
1073
|
-
const ans:
|
|
1017
|
+
const ans: ReturnType<OneParamCallback<N>>[] = [];
|
|
1074
1018
|
|
|
1075
1019
|
let cur: N | null | undefined = beginRoot;
|
|
1076
1020
|
const _reverseEdge = (node: N | null | undefined) => {
|
|
@@ -1177,7 +1121,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1177
1121
|
* the tree's structure should be restored to its original state to maintain the tree's integrity.
|
|
1178
1122
|
* This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
|
|
1179
1123
|
*/
|
|
1180
|
-
protected _defaultCallbackByKey:
|
|
1124
|
+
protected _defaultCallbackByKey: OneParamCallback<N, BinaryTreeNodeKey> = node => node.key;
|
|
1181
1125
|
|
|
1182
1126
|
/**
|
|
1183
1127
|
* The function `_addTo` adds a new node to a binary tree if there is an available position.
|
|
@@ -5,14 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type {
|
|
9
|
-
BinaryTreeNodeKey,
|
|
10
|
-
BSTComparator,
|
|
11
|
-
BSTNodeNested,
|
|
12
|
-
BSTOptions,
|
|
13
|
-
MapCallback,
|
|
14
|
-
MapCallbackReturn
|
|
15
|
-
} from '../../types';
|
|
8
|
+
import type {BinaryTreeNodeKey, BSTComparator, BSTNodeNested, BSTOptions, OneParamCallback} from '../../types';
|
|
16
9
|
import {CP, IterationType} from '../../types';
|
|
17
10
|
import {BinaryTree, BinaryTreeNode} from './binary-tree';
|
|
18
11
|
import {IBinaryTree} from '../../interfaces';
|
|
@@ -26,7 +19,8 @@ export class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extend
|
|
|
26
19
|
|
|
27
20
|
export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>>
|
|
28
21
|
extends BinaryTree<V, N>
|
|
29
|
-
implements IBinaryTree<V, N>
|
|
22
|
+
implements IBinaryTree<V, N>
|
|
23
|
+
{
|
|
30
24
|
/**
|
|
31
25
|
* The constructor function initializes a binary search tree object with an optional comparator
|
|
32
26
|
* function.
|
|
@@ -227,7 +221,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
227
221
|
* callback.
|
|
228
222
|
* @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter is used to specify the
|
|
229
223
|
* property of the binary tree node that you want to search for. It can be either a specific key
|
|
230
|
-
* value (`BinaryTreeNodeKey`) or a custom callback function (`
|
|
224
|
+
* value (`BinaryTreeNodeKey`) or a custom callback function (`OneParamCallback<N>`) that determines
|
|
231
225
|
* whether a node matches the desired property.
|
|
232
226
|
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
233
227
|
* matches the desired property. It takes a node as input and returns a boolean value indicating
|
|
@@ -240,8 +234,8 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
240
234
|
* @returns either the first node that matches the given nodeProperty and callback, or null if no
|
|
241
235
|
* matching node is found.
|
|
242
236
|
*/
|
|
243
|
-
override get<C extends
|
|
244
|
-
identifier: ReturnType<C> |
|
|
237
|
+
override get<C extends OneParamCallback<N>>(
|
|
238
|
+
identifier: ReturnType<C> | null,
|
|
245
239
|
callback: C = this._defaultCallbackByKey as C,
|
|
246
240
|
beginRoot = this.root,
|
|
247
241
|
iterationType = this.iterationType
|
|
@@ -291,8 +285,8 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
291
285
|
* traverse the binary tree. It can have one of the following values:
|
|
292
286
|
* @returns an array of nodes (N[]).
|
|
293
287
|
*/
|
|
294
|
-
override getNodes<C extends
|
|
295
|
-
identifier: ReturnType<C> |
|
|
288
|
+
override getNodes<C extends OneParamCallback<N>>(
|
|
289
|
+
identifier: ReturnType<C> | null,
|
|
296
290
|
callback: C = this._defaultCallbackByKey as C,
|
|
297
291
|
onlyOne = false,
|
|
298
292
|
beginRoot: N | null = this.root,
|
|
@@ -363,16 +357,16 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
363
357
|
* (`BinaryTreeNodeKey`), or `null` to
|
|
364
358
|
* @param iterationType - The `iterationType` parameter determines whether the traversal should be
|
|
365
359
|
* done recursively or iteratively. It can have two possible values:
|
|
366
|
-
* @returns The function `lesserOrGreaterTraverse` returns an array of `
|
|
360
|
+
* @returns The function `lesserOrGreaterTraverse` returns an array of `ReturnType<OneParamCallback<N>>`.
|
|
367
361
|
*/
|
|
368
|
-
lesserOrGreaterTraverse<C extends
|
|
362
|
+
lesserOrGreaterTraverse<C extends OneParamCallback<N>>(
|
|
369
363
|
callback: C = this._defaultCallbackByKey as C,
|
|
370
364
|
lesserOrGreater: CP = CP.lt,
|
|
371
365
|
targetNode: BinaryTreeNodeKey | N | null = this.root,
|
|
372
366
|
iterationType = this.iterationType
|
|
373
367
|
): ReturnType<C>[] {
|
|
374
368
|
if (typeof targetNode === 'number') targetNode = this.get(targetNode);
|
|
375
|
-
const ans:
|
|
369
|
+
const ans: ReturnType<OneParamCallback<N>>[] = [];
|
|
376
370
|
if (!targetNode) return ans;
|
|
377
371
|
const targetKey = targetNode.key;
|
|
378
372
|
if (!this.root) return ans;
|
|
@@ -21,7 +21,8 @@ export class RBTreeNode<V = any, N extends RBTreeNode<V, N> = RBTreeNodeNested<V
|
|
|
21
21
|
|
|
22
22
|
export class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNested<V>>>
|
|
23
23
|
extends BST<V, N>
|
|
24
|
-
implements IBinaryTree<V, N>
|
|
24
|
+
implements IBinaryTree<V, N>
|
|
25
|
+
{
|
|
25
26
|
constructor(options?: RBTreeOptions) {
|
|
26
27
|
super(options);
|
|
27
28
|
}
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type {BinaryTreeNodeKey, TreeMultisetNodeNested, TreeMultisetOptions} from '../../types';
|
|
9
|
-
import {BinaryTreeDeletedResult, CP, FamilyPosition, IterationType,
|
|
9
|
+
import {BinaryTreeDeletedResult, CP, FamilyPosition, IterationType, OneParamCallback} from '../../types';
|
|
10
10
|
import {IBinaryTree} from '../../interfaces';
|
|
11
11
|
import {AVLTree, AVLTreeNode} from './avl-tree';
|
|
12
12
|
|
|
@@ -37,7 +37,8 @@ export class TreeMultisetNode<
|
|
|
37
37
|
*/
|
|
38
38
|
export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultisetNode<V, TreeMultisetNodeNested<V>>>
|
|
39
39
|
extends AVLTree<V, N>
|
|
40
|
-
implements IBinaryTree<V, N>
|
|
40
|
+
implements IBinaryTree<V, N>
|
|
41
|
+
{
|
|
41
42
|
/**
|
|
42
43
|
* The constructor function for a TreeMultiset class in TypeScript, which extends another class and sets an option to
|
|
43
44
|
* merge duplicated values.
|
|
@@ -274,7 +275,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
274
275
|
* decremented by 1 and
|
|
275
276
|
* @returns The method `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
276
277
|
*/
|
|
277
|
-
override delete<C extends
|
|
278
|
+
override delete<C extends OneParamCallback<N>>(
|
|
278
279
|
identifier: ReturnType<C>,
|
|
279
280
|
callback: C = this._defaultCallbackByKey as C,
|
|
280
281
|
ignoreCount = false
|
|
@@ -105,7 +105,8 @@ export abstract class AbstractEdge<V = any> {
|
|
|
105
105
|
export abstract class AbstractGraph<
|
|
106
106
|
V extends AbstractVertex<any> = AbstractVertex<any>,
|
|
107
107
|
E extends AbstractEdge<any> = AbstractEdge<any>
|
|
108
|
-
> implements IGraph<V, E>
|
|
108
|
+
> implements IGraph<V, E>
|
|
109
|
+
{
|
|
109
110
|
private _vertices: Map<VertexKey, V> = new Map<VertexKey, V>();
|
|
110
111
|
|
|
111
112
|
get vertices(): Map<VertexKey, V> {
|
|
@@ -130,7 +131,7 @@ export abstract class AbstractGraph<
|
|
|
130
131
|
*/
|
|
131
132
|
abstract createEdge(srcOrV1: VertexKey | string, destOrV2: VertexKey | string, weight?: number, val?: E): E;
|
|
132
133
|
|
|
133
|
-
abstract
|
|
134
|
+
abstract deleteEdge(edge: E): E | null;
|
|
134
135
|
|
|
135
136
|
abstract getEdge(srcOrKey: V | VertexKey, destOrKey: V | VertexKey): E | null;
|
|
136
137
|
|
|
@@ -179,12 +180,12 @@ export abstract class AbstractGraph<
|
|
|
179
180
|
}
|
|
180
181
|
|
|
181
182
|
/**
|
|
182
|
-
* The `
|
|
183
|
+
* The `deleteVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
|
|
183
184
|
* @param {V | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`V`) or a vertex ID
|
|
184
185
|
* (`VertexKey`).
|
|
185
186
|
* @returns The method is returning a boolean value.
|
|
186
187
|
*/
|
|
187
|
-
|
|
188
|
+
deleteVertex(vertexOrKey: V | VertexKey): boolean {
|
|
188
189
|
const vertexKey = this._getVertexKey(vertexOrKey);
|
|
189
190
|
return this._vertices.delete(vertexKey);
|
|
190
191
|
}
|
|
@@ -196,10 +197,10 @@ export abstract class AbstractGraph<
|
|
|
196
197
|
* @returns a boolean value. It returns true if at least one vertex was successfully removed, and false if no vertices
|
|
197
198
|
* were removed.
|
|
198
199
|
*/
|
|
199
|
-
|
|
200
|
+
removeManyVertices(vertices: V[] | VertexKey[]): boolean {
|
|
200
201
|
const removed: boolean[] = [];
|
|
201
202
|
for (const v of vertices) {
|
|
202
|
-
removed.push(this.
|
|
203
|
+
removed.push(this.deleteVertex(v));
|
|
203
204
|
}
|
|
204
205
|
return removed.length > 0;
|
|
205
206
|
}
|
|
@@ -553,14 +554,14 @@ export abstract class AbstractGraph<
|
|
|
553
554
|
}
|
|
554
555
|
|
|
555
556
|
getMinDist &&
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
557
|
+
distMap.forEach((d, v) => {
|
|
558
|
+
if (v !== srcVertex) {
|
|
559
|
+
if (d < minDist) {
|
|
560
|
+
minDist = d;
|
|
561
|
+
if (genPaths) minDest = v;
|
|
562
|
+
}
|
|
561
563
|
}
|
|
562
|
-
}
|
|
563
|
-
});
|
|
564
|
+
});
|
|
564
565
|
|
|
565
566
|
genPaths && getPaths(minDest);
|
|
566
567
|
|
|
@@ -622,7 +623,7 @@ export abstract class AbstractGraph<
|
|
|
622
623
|
if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
|
|
623
624
|
}
|
|
624
625
|
|
|
625
|
-
const heap = new PriorityQueue<{
|
|
626
|
+
const heap = new PriorityQueue<{key: number; val: V}>({comparator: (a, b) => a.key - b.key});
|
|
626
627
|
heap.add({key: 0, val: srcVertex});
|
|
627
628
|
|
|
628
629
|
distMap.set(srcVertex, 0);
|
|
@@ -851,7 +852,7 @@ export abstract class AbstractGraph<
|
|
|
851
852
|
* `predecessor` property is a 2D array of vertices (or `null`) representing the predecessor vertices in the shortest
|
|
852
853
|
* path between vertices in the
|
|
853
854
|
*/
|
|
854
|
-
floyd(): {
|
|
855
|
+
floyd(): {costs: number[][]; predecessor: (V | null)[][]} {
|
|
855
856
|
const idAndVertices = [...this._vertices];
|
|
856
857
|
const n = idAndVertices.length;
|
|
857
858
|
|
|
@@ -64,7 +64,8 @@ export class DirectedEdge<V = any> extends AbstractEdge<V> {
|
|
|
64
64
|
|
|
65
65
|
export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E extends DirectedEdge<any> = DirectedEdge>
|
|
66
66
|
extends AbstractGraph<V, E>
|
|
67
|
-
implements IGraph<V, E>
|
|
67
|
+
implements IGraph<V, E>
|
|
68
|
+
{
|
|
68
69
|
/**
|
|
69
70
|
* The constructor function initializes an instance of a class.
|
|
70
71
|
*/
|
|
@@ -153,7 +154,7 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
|
|
|
153
154
|
* @param {V | VertexKey} destOrKey - The `destOrKey` parameter represents the destination vertex or its ID.
|
|
154
155
|
* @returns the removed edge (E) if it exists, or null if either the source or destination vertex does not exist.
|
|
155
156
|
*/
|
|
156
|
-
|
|
157
|
+
deleteEdgeSrcToDest(srcOrKey: V | VertexKey, destOrKey: V | VertexKey): E | null {
|
|
157
158
|
const src: V | null = this._getVertex(srcOrKey);
|
|
158
159
|
const dest: V | null = this._getVertex(destOrKey);
|
|
159
160
|
let removed: E | null = null;
|
|
@@ -177,9 +178,9 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
|
|
|
177
178
|
* The function removes an edge from a graph and returns the removed edge, or null if the edge was not found.
|
|
178
179
|
* @param {E} edge - The `edge` parameter is an object that represents an edge in a graph. It has two properties: `src`
|
|
179
180
|
* and `dest`, which represent the source and destination vertices of the edge, respectively.
|
|
180
|
-
* @returns The method `
|
|
181
|
+
* @returns The method `deleteEdge` returns the removed edge (`E`) if it exists, or `null` if the edge does not exist.
|
|
181
182
|
*/
|
|
182
|
-
|
|
183
|
+
deleteEdge(edge: E): E | null {
|
|
183
184
|
let removed: E | null = null;
|
|
184
185
|
const src = this._getVertex(edge.src);
|
|
185
186
|
const dest = this._getVertex(edge.dest);
|
|
@@ -206,12 +207,12 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
|
|
|
206
207
|
* the second vertex in the edge that needs to be removed.
|
|
207
208
|
* @returns an array of removed edges (E[]).
|
|
208
209
|
*/
|
|
209
|
-
|
|
210
|
+
deleteEdgesBetween(v1: VertexKey | V, v2: VertexKey | V): E[] {
|
|
210
211
|
const removed: E[] = [];
|
|
211
212
|
|
|
212
213
|
if (v1 && v2) {
|
|
213
|
-
const v1ToV2 = this.
|
|
214
|
-
const v2ToV1 = this.
|
|
214
|
+
const v1ToV2 = this.deleteEdgeSrcToDest(v1, v2);
|
|
215
|
+
const v2ToV1 = this.deleteEdgeSrcToDest(v2, v1);
|
|
215
216
|
|
|
216
217
|
v1ToV2 && removed.push(v1ToV2);
|
|
217
218
|
v2ToV1 && removed.push(v2ToV1);
|
|
@@ -109,9 +109,9 @@ export class MapGraph<V extends MapVertex<V['val']> = MapVertex, E extends MapEd
|
|
|
109
109
|
*/
|
|
110
110
|
override createVertex(
|
|
111
111
|
key: VertexKey,
|
|
112
|
-
val?: V['val'],
|
|
113
112
|
lat: number = this.origin[0],
|
|
114
|
-
long: number = this.origin[1]
|
|
113
|
+
long: number = this.origin[1],
|
|
114
|
+
val?: V['val']
|
|
115
115
|
): V {
|
|
116
116
|
return new MapVertex(key, lat, long, val) as V;
|
|
117
117
|
}
|
|
@@ -51,11 +51,12 @@ export class UndirectedEdge<V = number> extends AbstractEdge<V> {
|
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
export class UndirectedGraph<
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
>
|
|
54
|
+
V extends UndirectedVertex<any> = UndirectedVertex,
|
|
55
|
+
E extends UndirectedEdge<any> = UndirectedEdge
|
|
56
|
+
>
|
|
57
57
|
extends AbstractGraph<V, E>
|
|
58
|
-
implements IGraph<V, E>
|
|
58
|
+
implements IGraph<V, E>
|
|
59
|
+
{
|
|
59
60
|
/**
|
|
60
61
|
* The constructor initializes a new Map object to store edges.
|
|
61
62
|
*/
|
|
@@ -127,7 +128,7 @@ export class UndirectedGraph<
|
|
|
127
128
|
* (VertexKey). It represents the second vertex of the edge that needs to be removed.
|
|
128
129
|
* @returns the removed edge (E) if it exists, or null if either of the vertices (V) does not exist.
|
|
129
130
|
*/
|
|
130
|
-
|
|
131
|
+
deleteEdgeBetween(v1: V | VertexKey, v2: V | VertexKey): E | null {
|
|
131
132
|
const vertex1: V | null = this._getVertex(v1);
|
|
132
133
|
const vertex2: V | null = this._getVertex(v2);
|
|
133
134
|
|
|
@@ -148,12 +149,12 @@ export class UndirectedGraph<
|
|
|
148
149
|
}
|
|
149
150
|
|
|
150
151
|
/**
|
|
151
|
-
* The
|
|
152
|
+
* The deleteEdge function removes an edge between two vertices in a graph.
|
|
152
153
|
* @param {E} edge - The parameter "edge" is of type E, which represents an edge in a graph.
|
|
153
154
|
* @returns The method is returning either the removed edge (of type E) or null if the edge was not found.
|
|
154
155
|
*/
|
|
155
|
-
|
|
156
|
-
return this.
|
|
156
|
+
deleteEdge(edge: E): E | null {
|
|
157
|
+
return this.deleteEdgeBetween(edge.vertices[0], edge.vertices[1]);
|
|
157
158
|
}
|
|
158
159
|
|
|
159
160
|
/**
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
export class TreeMap {
|
|
2
|
-
}
|
|
1
|
+
export class TreeMap {}
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
export class TreeSet {
|
|
2
|
-
}
|
|
1
|
+
export class TreeSet {}
|