min-heap-typed 1.49.9 → 1.50.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.
- package/dist/data-structures/binary-tree/avl-tree.d.ts +0 -7
- package/dist/data-structures/binary-tree/avl-tree.js +0 -9
- package/dist/data-structures/binary-tree/binary-tree.d.ts +5 -22
- package/dist/data-structures/binary-tree/binary-tree.js +9 -80
- package/dist/data-structures/binary-tree/bst.d.ts +89 -27
- package/dist/data-structures/binary-tree/bst.js +131 -46
- package/dist/data-structures/binary-tree/rb-tree.d.ts +0 -7
- package/dist/data-structures/binary-tree/rb-tree.js +1 -10
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +0 -7
- package/dist/data-structures/binary-tree/tree-multimap.js +2 -11
- package/dist/data-structures/hash/hash-map.d.ts +3 -3
- package/dist/data-structures/hash/hash-map.js +6 -6
- package/package.json +2 -2
- package/src/data-structures/base/iterable-base.ts +6 -6
- package/src/data-structures/binary-tree/avl-tree.ts +7 -18
- package/src/data-structures/binary-tree/binary-tree.ts +111 -149
- package/src/data-structures/binary-tree/bst.ts +159 -56
- package/src/data-structures/binary-tree/rb-tree.ts +7 -18
- package/src/data-structures/binary-tree/tree-multimap.ts +8 -19
- package/src/data-structures/graph/abstract-graph.ts +14 -15
- package/src/data-structures/graph/directed-graph.ts +6 -7
- package/src/data-structures/graph/undirected-graph.ts +6 -7
- package/src/data-structures/hash/hash-map.ts +23 -22
- package/src/data-structures/heap/heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
- package/src/data-structures/queue/deque.ts +3 -3
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/data-structures/stack/stack.ts +1 -1
- package/src/data-structures/trie/trie.ts +1 -1
- package/src/types/data-structures/graph/abstract-graph.ts +8 -8
|
@@ -13,7 +13,7 @@ import type {
|
|
|
13
13
|
BTNodePureExemplar,
|
|
14
14
|
KeyOrNodeOrEntry
|
|
15
15
|
} from '../../types';
|
|
16
|
-
import { BSTVariant, CP, IterationType } from '../../types';
|
|
16
|
+
import { BSTVariant, CP, DFSOrderPattern, IterationType } from '../../types';
|
|
17
17
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
18
18
|
import { IBinaryTree } from '../../interfaces';
|
|
19
19
|
import { Queue } from '../queue';
|
|
@@ -83,14 +83,13 @@ export class BSTNode<K = any, V = any, N extends BSTNode<K, V, N> = BSTNodeNeste
|
|
|
83
83
|
* 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
|
|
84
84
|
*/
|
|
85
85
|
export class BST<
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
86
|
+
K = any,
|
|
87
|
+
V = any,
|
|
88
|
+
N extends BSTNode<K, V, N> = BSTNode<K, V, BSTNodeNested<K, V>>,
|
|
89
|
+
TREE extends BST<K, V, N, TREE> = BST<K, V, N, BSTNested<K, V, N>>
|
|
90
|
+
>
|
|
91
91
|
extends BinaryTree<K, V, N, TREE>
|
|
92
|
-
implements IBinaryTree<K, V, N, TREE>
|
|
93
|
-
{
|
|
92
|
+
implements IBinaryTree<K, V, N, TREE> {
|
|
94
93
|
/**
|
|
95
94
|
* This is the constructor function for a binary search tree class in TypeScript, which initializes
|
|
96
95
|
* the tree with optional keysOrNodesOrEntries and options.
|
|
@@ -172,7 +171,7 @@ export class BST<
|
|
|
172
171
|
} else {
|
|
173
172
|
node = this.createNode(key, value);
|
|
174
173
|
}
|
|
175
|
-
} else if (this.
|
|
174
|
+
} else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
176
175
|
node = this.createNode(keyOrNodeOrEntry, value);
|
|
177
176
|
} else {
|
|
178
177
|
return;
|
|
@@ -213,16 +212,6 @@ export class BST<
|
|
|
213
212
|
return res;
|
|
214
213
|
}
|
|
215
214
|
|
|
216
|
-
/**
|
|
217
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
218
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
219
|
-
* data type.
|
|
220
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
221
|
-
*/
|
|
222
|
-
override isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K {
|
|
223
|
-
return !(potentialKey instanceof BSTNode);
|
|
224
|
-
}
|
|
225
|
-
|
|
226
215
|
/**
|
|
227
216
|
* The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
|
|
228
217
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
@@ -408,43 +397,6 @@ export class BST<
|
|
|
408
397
|
return inserted;
|
|
409
398
|
}
|
|
410
399
|
|
|
411
|
-
/**
|
|
412
|
-
* Time Complexity: O(n log n)
|
|
413
|
-
* Space Complexity: O(n)
|
|
414
|
-
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
415
|
-
*/
|
|
416
|
-
|
|
417
|
-
/**
|
|
418
|
-
* Time Complexity: O(n log n)
|
|
419
|
-
* Space Complexity: O(n)
|
|
420
|
-
*
|
|
421
|
-
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
422
|
-
* leftmost node if the comparison result is greater than.
|
|
423
|
-
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
424
|
-
* type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
|
|
425
|
-
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
426
|
-
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
427
|
-
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
428
|
-
* rightmost node otherwise. If no node is found, it returns 0.
|
|
429
|
-
*/
|
|
430
|
-
lastKey(beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root): K | undefined {
|
|
431
|
-
let current = this.ensureNode(beginRoot);
|
|
432
|
-
if (!current) return undefined;
|
|
433
|
-
|
|
434
|
-
if (this._variant === BSTVariant.STANDARD) {
|
|
435
|
-
// For BSTVariant.MIN, find the rightmost node
|
|
436
|
-
while (current.right !== undefined) {
|
|
437
|
-
current = current.right;
|
|
438
|
-
}
|
|
439
|
-
} else {
|
|
440
|
-
// For BSTVariant.MAX, find the leftmost node
|
|
441
|
-
while (current.left !== undefined) {
|
|
442
|
-
current = current.left;
|
|
443
|
-
}
|
|
444
|
-
}
|
|
445
|
-
return current.key;
|
|
446
|
-
}
|
|
447
|
-
|
|
448
400
|
/**
|
|
449
401
|
* Time Complexity: O(log n)
|
|
450
402
|
* Space Complexity: O(1)
|
|
@@ -574,6 +526,157 @@ export class BST<
|
|
|
574
526
|
return ans;
|
|
575
527
|
}
|
|
576
528
|
|
|
529
|
+
// /**
|
|
530
|
+
// * The function overrides the subTreeTraverse method and returns the result of calling the super
|
|
531
|
+
// * method with the provided arguments.
|
|
532
|
+
// * @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
533
|
+
// * the subtree traversal. It should accept a single parameter of type `N`, which represents a node in
|
|
534
|
+
// * the tree. The return type of the callback function can be any type.
|
|
535
|
+
// * @param beginRoot - The `beginRoot` parameter is the starting point for traversing the subtree. It
|
|
536
|
+
// * can be either a key, a node, or an entry.
|
|
537
|
+
// * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
538
|
+
// * be performed during the traversal of the subtree. It can have one of the following values:
|
|
539
|
+
// * @returns The method is returning an array of the return type of the callback function.
|
|
540
|
+
// */
|
|
541
|
+
// override subTreeTraverse<C extends BTNCallback<N>>(
|
|
542
|
+
// callback: C = this._defaultOneParamCallback as C,
|
|
543
|
+
// beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
|
|
544
|
+
// iterationType = this.iterationType
|
|
545
|
+
// ): ReturnType<C>[] {
|
|
546
|
+
// return super.subTreeTraverse(callback, beginRoot, iterationType, false);
|
|
547
|
+
// }
|
|
548
|
+
|
|
549
|
+
/**
|
|
550
|
+
* Time complexity: O(n)
|
|
551
|
+
* Space complexity: O(n)
|
|
552
|
+
*/
|
|
553
|
+
|
|
554
|
+
/**
|
|
555
|
+
* Time complexity: O(n)
|
|
556
|
+
* Space complexity: O(n)
|
|
557
|
+
*
|
|
558
|
+
* The function overrides the depth-first search method and returns an array of the return types of
|
|
559
|
+
* the callback function.
|
|
560
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
561
|
+
* during the depth-first search traversal. It is an optional parameter and if not provided, a
|
|
562
|
+
* default callback function will be used.
|
|
563
|
+
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter specifies the order in which the
|
|
564
|
+
* nodes are visited during the depth-first search. It can have one of the following values:
|
|
565
|
+
* @param beginRoot - The `beginRoot` parameter is used to specify the starting point for the
|
|
566
|
+
* Depth-First Search (DFS) traversal. It can be either a key, a node, or an entry in the tree. If no
|
|
567
|
+
* value is provided, the DFS traversal will start from the root of the tree.
|
|
568
|
+
* @param {IterationType} iterationType - The `iterationType` parameter specifies the type of
|
|
569
|
+
* iteration to be used during the Depth-First Search (DFS) traversal. It can have one of the
|
|
570
|
+
* following values:
|
|
571
|
+
* @returns The method is returning an array of the return type of the callback function.
|
|
572
|
+
*/
|
|
573
|
+
override dfs<C extends BTNCallback<N>>(
|
|
574
|
+
callback: C = this._defaultOneParamCallback as C,
|
|
575
|
+
pattern: DFSOrderPattern = 'in',
|
|
576
|
+
beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
|
|
577
|
+
iterationType: IterationType = IterationType.ITERATIVE
|
|
578
|
+
): ReturnType<C>[] {
|
|
579
|
+
return super.dfs(callback, pattern, beginRoot, iterationType, false);
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
/**
|
|
583
|
+
* Time complexity: O(n)
|
|
584
|
+
* Space complexity: O(n)
|
|
585
|
+
*/
|
|
586
|
+
|
|
587
|
+
/**
|
|
588
|
+
* Time complexity: O(n)
|
|
589
|
+
* Space complexity: O(n)
|
|
590
|
+
*
|
|
591
|
+
* The function overrides the breadth-first search method and returns an array of the return types of
|
|
592
|
+
* the callback function.
|
|
593
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
594
|
+
* visited during the breadth-first search traversal. It is an optional parameter and if not
|
|
595
|
+
* provided, a default callback function will be used.
|
|
596
|
+
* @param beginRoot - The `beginRoot` parameter is the starting point for the breadth-first search
|
|
597
|
+
* traversal. It can be either a key, a node, or an entry in the tree. If not specified, the root of
|
|
598
|
+
* the tree is used as the starting point.
|
|
599
|
+
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
600
|
+
* be performed during the breadth-first search (BFS) traversal. It determines the order in which the
|
|
601
|
+
* nodes are visited.
|
|
602
|
+
* @returns The method is returning an array of the return type of the callback function.
|
|
603
|
+
*/
|
|
604
|
+
override bfs<C extends BTNCallback<N>>(
|
|
605
|
+
callback: C = this._defaultOneParamCallback as C,
|
|
606
|
+
beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
|
|
607
|
+
iterationType = this.iterationType
|
|
608
|
+
): ReturnType<C>[] {
|
|
609
|
+
return super.bfs(callback, beginRoot, iterationType, false);
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* Time complexity: O(n)
|
|
614
|
+
* Space complexity: O(n)
|
|
615
|
+
*/
|
|
616
|
+
|
|
617
|
+
/**
|
|
618
|
+
* Time complexity: O(n)
|
|
619
|
+
* Space complexity: O(n)
|
|
620
|
+
*
|
|
621
|
+
* The function overrides the listLevels method and returns an array of arrays containing the return
|
|
622
|
+
* type of the callback function for each level of the tree.
|
|
623
|
+
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
624
|
+
* `BTNCallback<N>`. It represents a callback function that will be called for each node in the tree
|
|
625
|
+
* during the level listing process.
|
|
626
|
+
* @param beginRoot - The `beginRoot` parameter is used to specify the starting point for listing the
|
|
627
|
+
* levels of a binary tree. It can be either a key, a node, or an entry in the binary tree. If not
|
|
628
|
+
* provided, the root of the binary tree is used as the starting point.
|
|
629
|
+
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
630
|
+
* be performed on the tree. It determines the order in which the nodes are visited during the
|
|
631
|
+
* iteration.
|
|
632
|
+
* @returns The method is returning a two-dimensional array of the return type of the callback
|
|
633
|
+
* function.
|
|
634
|
+
*/
|
|
635
|
+
override listLevels<C extends BTNCallback<N>>(
|
|
636
|
+
callback: C = this._defaultOneParamCallback as C,
|
|
637
|
+
beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
|
|
638
|
+
iterationType = this.iterationType
|
|
639
|
+
): ReturnType<C>[][] {
|
|
640
|
+
return super.listLevels(callback, beginRoot, iterationType, false);
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
/**
|
|
644
|
+
* Time Complexity: O(n log n)
|
|
645
|
+
* Space Complexity: O(n)
|
|
646
|
+
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
647
|
+
*/
|
|
648
|
+
|
|
649
|
+
/**
|
|
650
|
+
* Time Complexity: O(n log n)
|
|
651
|
+
* Space Complexity: O(n)
|
|
652
|
+
*
|
|
653
|
+
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
654
|
+
* leftmost node if the comparison result is greater than.
|
|
655
|
+
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
656
|
+
* type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
|
|
657
|
+
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
658
|
+
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
659
|
+
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
660
|
+
* rightmost node otherwise. If no node is found, it returns 0.
|
|
661
|
+
*/
|
|
662
|
+
lastKey(beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root): K | undefined {
|
|
663
|
+
let current = this.ensureNode(beginRoot);
|
|
664
|
+
if (!current) return undefined;
|
|
665
|
+
|
|
666
|
+
if (this._variant === BSTVariant.STANDARD) {
|
|
667
|
+
// For BSTVariant.MIN, find the rightmost node
|
|
668
|
+
while (current.right !== undefined) {
|
|
669
|
+
current = current.right;
|
|
670
|
+
}
|
|
671
|
+
} else {
|
|
672
|
+
// For BSTVariant.MAX, find the leftmost node
|
|
673
|
+
while (current.left !== undefined) {
|
|
674
|
+
current = current.left;
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
return current.key;
|
|
678
|
+
}
|
|
679
|
+
|
|
577
680
|
/**
|
|
578
681
|
* Time Complexity: O(log n)
|
|
579
682
|
* Space Complexity: O(log n)
|
|
@@ -41,14 +41,13 @@ export class RedBlackTreeNode<
|
|
|
41
41
|
* 5. Black balance: Every path from any node to each of its leaf nodes contains the same number of black nodes.
|
|
42
42
|
*/
|
|
43
43
|
export class RedBlackTree<
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
44
|
+
K = any,
|
|
45
|
+
V = any,
|
|
46
|
+
N extends RedBlackTreeNode<K, V, N> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>,
|
|
47
|
+
TREE extends RedBlackTree<K, V, N, TREE> = RedBlackTree<K, V, N, RedBlackTreeNested<K, V, N>>
|
|
48
|
+
>
|
|
49
49
|
extends BST<K, V, N, TREE>
|
|
50
|
-
implements IBinaryTree<K, V, N, TREE>
|
|
51
|
-
{
|
|
50
|
+
implements IBinaryTree<K, V, N, TREE> {
|
|
52
51
|
Sentinel: N = new RedBlackTreeNode<K, V>(NaN as K) as unknown as N;
|
|
53
52
|
|
|
54
53
|
/**
|
|
@@ -133,7 +132,7 @@ export class RedBlackTree<
|
|
|
133
132
|
} else {
|
|
134
133
|
node = this.createNode(key, value, RBTNColor.RED);
|
|
135
134
|
}
|
|
136
|
-
} else if (this.
|
|
135
|
+
} else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
137
136
|
node = this.createNode(keyOrNodeOrEntry, value, RBTNColor.RED);
|
|
138
137
|
} else {
|
|
139
138
|
return;
|
|
@@ -161,16 +160,6 @@ export class RedBlackTree<
|
|
|
161
160
|
return node instanceof RedBlackTreeNode;
|
|
162
161
|
}
|
|
163
162
|
|
|
164
|
-
/**
|
|
165
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
166
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
167
|
-
* data type.
|
|
168
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
169
|
-
*/
|
|
170
|
-
override isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K {
|
|
171
|
-
return !(potentialKey instanceof RedBlackTreeNode);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
163
|
/**
|
|
175
164
|
* Time Complexity: O(log n)
|
|
176
165
|
* Space Complexity: O(1)
|
|
@@ -45,14 +45,13 @@ export class TreeMultimapNode<
|
|
|
45
45
|
* The only distinction between a TreeMultimap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
46
46
|
*/
|
|
47
47
|
export class TreeMultimap<
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
48
|
+
K = any,
|
|
49
|
+
V = any,
|
|
50
|
+
N extends TreeMultimapNode<K, V, N> = TreeMultimapNode<K, V, TreeMultimapNodeNested<K, V>>,
|
|
51
|
+
TREE extends TreeMultimap<K, V, N, TREE> = TreeMultimap<K, V, N, TreeMultimapNested<K, V, N>>
|
|
52
|
+
>
|
|
53
53
|
extends AVLTree<K, V, N, TREE>
|
|
54
|
-
implements IBinaryTree<K, V, N, TREE>
|
|
55
|
-
{
|
|
54
|
+
implements IBinaryTree<K, V, N, TREE> {
|
|
56
55
|
constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>> = [], options?: TreeMultimapOptions<K>) {
|
|
57
56
|
super([], options);
|
|
58
57
|
if (keysOrNodesOrEntries) this.addMany(keysOrNodesOrEntries);
|
|
@@ -63,7 +62,7 @@ export class TreeMultimap<
|
|
|
63
62
|
// TODO the _count is not accurate after nodes count modified
|
|
64
63
|
get count(): number {
|
|
65
64
|
let sum = 0;
|
|
66
|
-
this.
|
|
65
|
+
this.dfs(node => (sum += node.count));
|
|
67
66
|
return sum;
|
|
68
67
|
}
|
|
69
68
|
|
|
@@ -112,7 +111,7 @@ export class TreeMultimap<
|
|
|
112
111
|
} else {
|
|
113
112
|
node = this.createNode(key, value, count);
|
|
114
113
|
}
|
|
115
|
-
} else if (this.
|
|
114
|
+
} else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
116
115
|
node = this.createNode(keyOrNodeOrEntry, value, count);
|
|
117
116
|
} else {
|
|
118
117
|
return;
|
|
@@ -130,16 +129,6 @@ export class TreeMultimap<
|
|
|
130
129
|
return keyOrNodeOrEntry instanceof TreeMultimapNode;
|
|
131
130
|
}
|
|
132
131
|
|
|
133
|
-
/**
|
|
134
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
135
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
136
|
-
* data type.
|
|
137
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
138
|
-
*/
|
|
139
|
-
override isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K {
|
|
140
|
-
return !(potentialKey instanceof TreeMultimapNode);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
132
|
/**
|
|
144
133
|
* Time Complexity: O(log n)
|
|
145
134
|
* Space Complexity: O(1)
|
|
@@ -61,14 +61,13 @@ export abstract class AbstractEdge<E = any> {
|
|
|
61
61
|
}
|
|
62
62
|
|
|
63
63
|
export abstract class AbstractGraph<
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
64
|
+
V = any,
|
|
65
|
+
E = any,
|
|
66
|
+
VO extends AbstractVertex<V> = AbstractVertex<V>,
|
|
67
|
+
EO extends AbstractEdge<E> = AbstractEdge<E>
|
|
68
|
+
>
|
|
69
69
|
extends IterableEntryBase<VertexKey, V | undefined>
|
|
70
|
-
implements IGraph<V, E, VO, EO>
|
|
71
|
-
{
|
|
70
|
+
implements IGraph<V, E, VO, EO> {
|
|
72
71
|
constructor() {
|
|
73
72
|
super();
|
|
74
73
|
}
|
|
@@ -611,14 +610,14 @@ export abstract class AbstractGraph<
|
|
|
611
610
|
}
|
|
612
611
|
|
|
613
612
|
getMinDist &&
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
}
|
|
613
|
+
distMap.forEach((d, v) => {
|
|
614
|
+
if (v !== srcVertex) {
|
|
615
|
+
if (d < minDist) {
|
|
616
|
+
minDist = d;
|
|
617
|
+
if (genPaths) minDest = v;
|
|
620
618
|
}
|
|
621
|
-
}
|
|
619
|
+
}
|
|
620
|
+
});
|
|
622
621
|
|
|
623
622
|
genPaths && getPaths(minDest);
|
|
624
623
|
|
|
@@ -1273,7 +1272,7 @@ export abstract class AbstractGraph<
|
|
|
1273
1272
|
return mapped;
|
|
1274
1273
|
}
|
|
1275
1274
|
|
|
1276
|
-
protected
|
|
1275
|
+
protected* _getIterator(): IterableIterator<[VertexKey, V | undefined]> {
|
|
1277
1276
|
for (const vertex of this._vertexMap.values()) {
|
|
1278
1277
|
yield [vertex.key, vertex.value];
|
|
1279
1278
|
}
|
|
@@ -46,14 +46,13 @@ export class DirectedEdge<E = any> extends AbstractEdge<E> {
|
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
export class DirectedGraph<
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
49
|
+
V = any,
|
|
50
|
+
E = any,
|
|
51
|
+
VO extends DirectedVertex<V> = DirectedVertex<V>,
|
|
52
|
+
EO extends DirectedEdge<E> = DirectedEdge<E>
|
|
53
|
+
>
|
|
54
54
|
extends AbstractGraph<V, E, VO, EO>
|
|
55
|
-
implements IGraph<V, E, VO, EO>
|
|
56
|
-
{
|
|
55
|
+
implements IGraph<V, E, VO, EO> {
|
|
57
56
|
/**
|
|
58
57
|
* The constructor function initializes an instance of a class.
|
|
59
58
|
*/
|
|
@@ -43,14 +43,13 @@ export class UndirectedEdge<E = number> extends AbstractEdge<E> {
|
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
export class UndirectedGraph<
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
46
|
+
V = any,
|
|
47
|
+
E = any,
|
|
48
|
+
VO extends UndirectedVertex<V> = UndirectedVertex<V>,
|
|
49
|
+
EO extends UndirectedEdge<E> = UndirectedEdge<E>
|
|
50
|
+
>
|
|
51
51
|
extends AbstractGraph<V, E, VO, EO>
|
|
52
|
-
implements IGraph<V, E, VO, EO>
|
|
53
|
-
{
|
|
52
|
+
implements IGraph<V, E, VO, EO> {
|
|
54
53
|
/**
|
|
55
54
|
* The constructor initializes a new Map object to store edgeMap.
|
|
56
55
|
*/
|
|
@@ -24,24 +24,6 @@ import { isWeakKey, rangeCheck } from '../../utils';
|
|
|
24
24
|
export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K, V> {
|
|
25
25
|
protected _store: { [key: string]: HashMapStoreItem<K, V> } = {};
|
|
26
26
|
protected _objMap: Map<object, V> = new Map();
|
|
27
|
-
protected _toEntryFn: (rawElement: R) => [K, V] = (rawElement: R) => {
|
|
28
|
-
if (this.isEntry(rawElement)) {
|
|
29
|
-
// TODO, For performance optimization, it may be necessary to only inspect the first element traversed.
|
|
30
|
-
return rawElement;
|
|
31
|
-
} else {
|
|
32
|
-
throw new Error(
|
|
33
|
-
"If the provided rawCollection does not adhere to the [key, value] type format, the toEntryFn in the constructor's options parameter needs to specified."
|
|
34
|
-
);
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
get toEntryFn() {
|
|
39
|
-
return this._toEntryFn;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
isEntry(rawElement: any): rawElement is [K, V] {
|
|
43
|
-
return Array.isArray(rawElement) && rawElement.length === 2;
|
|
44
|
-
}
|
|
45
27
|
|
|
46
28
|
/**
|
|
47
29
|
* The constructor function initializes a HashMap object with an optional initial collection and
|
|
@@ -66,12 +48,31 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
66
48
|
}
|
|
67
49
|
}
|
|
68
50
|
|
|
51
|
+
protected _toEntryFn: (rawElement: R) => [K, V] = (rawElement: R) => {
|
|
52
|
+
if (this.isEntry(rawElement)) {
|
|
53
|
+
// TODO, For performance optimization, it may be necessary to only inspect the first element traversed.
|
|
54
|
+
return rawElement;
|
|
55
|
+
} else {
|
|
56
|
+
throw new Error(
|
|
57
|
+
"If the provided rawCollection does not adhere to the [key, value] type format, the toEntryFn in the constructor's options parameter needs to specified."
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
get toEntryFn() {
|
|
63
|
+
return this._toEntryFn;
|
|
64
|
+
}
|
|
65
|
+
|
|
69
66
|
protected _size = 0;
|
|
70
67
|
|
|
71
68
|
get size(): number {
|
|
72
69
|
return this._size;
|
|
73
70
|
}
|
|
74
71
|
|
|
72
|
+
isEntry(rawElement: any): rawElement is [K, V] {
|
|
73
|
+
return Array.isArray(rawElement) && rawElement.length === 2;
|
|
74
|
+
}
|
|
75
|
+
|
|
75
76
|
isEmpty(): boolean {
|
|
76
77
|
return this.size === 0;
|
|
77
78
|
}
|
|
@@ -248,7 +249,7 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
248
249
|
* The function returns an iterator that yields key-value pairs from both an object store and an
|
|
249
250
|
* object map.
|
|
250
251
|
*/
|
|
251
|
-
protected
|
|
252
|
+
protected* _getIterator(): IterableIterator<[K, V]> {
|
|
252
253
|
for (const node of Object.values(this._store)) {
|
|
253
254
|
yield [node.key, node.value] as [K, V];
|
|
254
255
|
}
|
|
@@ -347,7 +348,7 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
347
348
|
/**
|
|
348
349
|
* The `begin()` function in TypeScript iterates over a linked list and yields key-value pairs.
|
|
349
350
|
*/
|
|
350
|
-
*begin() {
|
|
351
|
+
* begin() {
|
|
351
352
|
let node = this._head;
|
|
352
353
|
while (node !== this._sentinel) {
|
|
353
354
|
yield [node.key, node.value];
|
|
@@ -359,7 +360,7 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
359
360
|
* The function `reverseBegin()` iterates over a linked list in reverse order, yielding each node's
|
|
360
361
|
* key and value.
|
|
361
362
|
*/
|
|
362
|
-
*reverseBegin() {
|
|
363
|
+
* reverseBegin() {
|
|
363
364
|
let node = this._tail;
|
|
364
365
|
while (node !== this._sentinel) {
|
|
365
366
|
yield [node.key, node.value];
|
|
@@ -660,7 +661,7 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
660
661
|
*
|
|
661
662
|
* The above function is an iterator that yields key-value pairs from a linked list.
|
|
662
663
|
*/
|
|
663
|
-
protected
|
|
664
|
+
protected* _getIterator() {
|
|
664
665
|
let node = this._head;
|
|
665
666
|
while (node !== this._sentinel) {
|
|
666
667
|
yield [node.key, node.value] as [K, V];
|
|
@@ -391,7 +391,7 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
391
391
|
return mappedHeap;
|
|
392
392
|
}
|
|
393
393
|
|
|
394
|
-
protected
|
|
394
|
+
protected* _getIterator(): IterableIterator<E> {
|
|
395
395
|
for (const element of this.elements) {
|
|
396
396
|
yield element;
|
|
397
397
|
}
|
|
@@ -808,7 +808,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
808
808
|
/**
|
|
809
809
|
* The function returns an iterator that iterates over the values of a linked list.
|
|
810
810
|
*/
|
|
811
|
-
protected
|
|
811
|
+
protected* _getIterator(): IterableIterator<E> {
|
|
812
812
|
let current = this.head;
|
|
813
813
|
|
|
814
814
|
while (current) {
|
|
@@ -741,7 +741,7 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
741
741
|
return mappedList;
|
|
742
742
|
}
|
|
743
743
|
|
|
744
|
-
protected
|
|
744
|
+
protected* _getIterator(): IterableIterator<E> {
|
|
745
745
|
let current = this.head;
|
|
746
746
|
|
|
747
747
|
while (current) {
|
|
@@ -232,7 +232,7 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
232
232
|
/**
|
|
233
233
|
* The below function is a generator that yields elements from a collection one by one.
|
|
234
234
|
*/
|
|
235
|
-
*begin(): Generator<E> {
|
|
235
|
+
* begin(): Generator<E> {
|
|
236
236
|
let index = 0;
|
|
237
237
|
while (index < this.size) {
|
|
238
238
|
yield this.getAt(index);
|
|
@@ -244,7 +244,7 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
244
244
|
* The function `reverseBegin()` is a generator that yields elements in reverse order starting from
|
|
245
245
|
* the last element.
|
|
246
246
|
*/
|
|
247
|
-
*reverseBegin(): Generator<E> {
|
|
247
|
+
* reverseBegin(): Generator<E> {
|
|
248
248
|
let index = this.size - 1;
|
|
249
249
|
while (index >= 0) {
|
|
250
250
|
yield this.getAt(index);
|
|
@@ -735,7 +735,7 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
735
735
|
* The above function is an implementation of the iterator protocol in TypeScript, allowing the
|
|
736
736
|
* object to be iterated over using a for...of loop.
|
|
737
737
|
*/
|
|
738
|
-
protected
|
|
738
|
+
protected* _getIterator(): IterableIterator<E> {
|
|
739
739
|
for (let i = 0; i < this.size; ++i) {
|
|
740
740
|
yield this.getAt(i);
|
|
741
741
|
}
|
|
@@ -345,7 +345,7 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
345
345
|
* Space Complexity: O(n)
|
|
346
346
|
*/
|
|
347
347
|
|
|
348
|
-
protected
|
|
348
|
+
protected* _getIterator(): IterableIterator<E> {
|
|
349
349
|
for (const item of this.elements) {
|
|
350
350
|
yield item;
|
|
351
351
|
}
|
|
@@ -229,7 +229,7 @@ export class Stack<E = any> extends IterableElementBase<E> {
|
|
|
229
229
|
* Custom iterator for the Stack class.
|
|
230
230
|
* @returns An iterator object.
|
|
231
231
|
*/
|
|
232
|
-
protected
|
|
232
|
+
protected* _getIterator(): IterableIterator<E> {
|
|
233
233
|
for (let i = 0; i < this.elements.length; i++) {
|
|
234
234
|
yield this.elements[i];
|
|
235
235
|
}
|
|
@@ -410,7 +410,7 @@ export class Trie extends IterableElementBase<string> {
|
|
|
410
410
|
return newTrie;
|
|
411
411
|
}
|
|
412
412
|
|
|
413
|
-
protected
|
|
413
|
+
protected* _getIterator(): IterableIterator<string> {
|
|
414
414
|
function* _dfs(node: TrieNode, path: string): IterableIterator<string> {
|
|
415
415
|
if (node.isEnd) {
|
|
416
416
|
yield path;
|
|
@@ -2,12 +2,12 @@ export type VertexKey = string | number;
|
|
|
2
2
|
|
|
3
3
|
export type DijkstraResult<V> =
|
|
4
4
|
| {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
5
|
+
distMap: Map<V, number>;
|
|
6
|
+
distPaths?: Map<V, V[]>;
|
|
7
|
+
preMap: Map<V, V | undefined>;
|
|
8
|
+
seen: Set<V>;
|
|
9
|
+
paths: V[][];
|
|
10
|
+
minDist: number;
|
|
11
|
+
minPath: V[];
|
|
12
|
+
}
|
|
13
13
|
| undefined;
|