stack-typed 1.49.0 → 1.49.2
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/base/iterable-base.d.ts +11 -0
- package/dist/data-structures/base/iterable-base.js +21 -0
- package/dist/data-structures/binary-tree/avl-tree.d.ts +10 -12
- package/dist/data-structures/binary-tree/avl-tree.js +3 -4
- package/dist/data-structures/binary-tree/binary-tree.d.ts +58 -58
- package/dist/data-structures/binary-tree/binary-tree.js +6 -6
- package/dist/data-structures/binary-tree/bst.d.ts +15 -15
- package/dist/data-structures/binary-tree/bst.js +3 -3
- package/dist/data-structures/binary-tree/rb-tree.d.ts +11 -11
- package/dist/data-structures/binary-tree/rb-tree.js +5 -6
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +14 -14
- package/dist/data-structures/binary-tree/tree-multimap.js +3 -3
- package/dist/data-structures/graph/abstract-graph.d.ts +9 -3
- package/dist/data-structures/graph/abstract-graph.js +27 -31
- package/dist/data-structures/graph/directed-graph.d.ts +8 -1
- package/dist/data-structures/graph/directed-graph.js +1 -8
- package/dist/data-structures/graph/map-graph.d.ts +1 -1
- package/dist/data-structures/graph/undirected-graph.d.ts +8 -1
- package/dist/data-structures/graph/undirected-graph.js +1 -8
- package/dist/data-structures/hash/hash-map.d.ts +22 -10
- package/dist/data-structures/hash/hash-map.js +28 -16
- package/dist/data-structures/hash/hash-table.d.ts +2 -2
- package/dist/data-structures/heap/heap.d.ts +20 -38
- package/dist/data-structures/heap/heap.js +22 -42
- package/dist/data-structures/heap/max-heap.d.ts +11 -1
- package/dist/data-structures/heap/max-heap.js +10 -7
- package/dist/data-structures/heap/min-heap.d.ts +11 -1
- package/dist/data-structures/heap/min-heap.js +10 -7
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +95 -95
- package/dist/data-structures/linked-list/doubly-linked-list.js +132 -136
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +18 -23
- package/dist/data-structures/linked-list/singly-linked-list.js +42 -49
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -2
- package/dist/data-structures/linked-list/skip-linked-list.js +2 -2
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +1 -1
- package/dist/data-structures/priority-queue/max-priority-queue.js +0 -7
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +1 -1
- package/dist/data-structures/priority-queue/min-priority-queue.js +0 -7
- package/dist/data-structures/priority-queue/priority-queue.d.ts +9 -1
- package/dist/data-structures/priority-queue/priority-queue.js +8 -7
- package/dist/data-structures/queue/deque.d.ts +76 -80
- package/dist/data-structures/queue/deque.js +106 -122
- package/dist/data-structures/queue/queue.d.ts +30 -16
- package/dist/data-structures/queue/queue.js +31 -24
- package/dist/data-structures/stack/stack.d.ts +17 -8
- package/dist/data-structures/stack/stack.js +9 -9
- package/dist/data-structures/trie/trie.d.ts +14 -5
- package/dist/data-structures/trie/trie.js +13 -13
- package/dist/interfaces/binary-tree.d.ts +4 -4
- package/dist/types/common.d.ts +32 -8
- package/dist/types/common.js +22 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -24
- package/dist/types/data-structures/binary-tree/binary-tree.js +0 -22
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
- package/package.json +2 -2
- package/src/data-structures/base/iterable-base.ts +24 -0
- package/src/data-structures/binary-tree/avl-tree.ts +14 -14
- package/src/data-structures/binary-tree/binary-tree.ts +74 -77
- package/src/data-structures/binary-tree/bst.ts +18 -17
- package/src/data-structures/binary-tree/rb-tree.ts +17 -18
- package/src/data-structures/binary-tree/tree-multimap.ts +22 -20
- package/src/data-structures/graph/abstract-graph.ts +35 -25
- package/src/data-structures/graph/directed-graph.ts +2 -2
- package/src/data-structures/graph/map-graph.ts +1 -1
- package/src/data-structures/graph/undirected-graph.ts +2 -2
- package/src/data-structures/hash/hash-map.ts +40 -24
- package/src/data-structures/hash/hash-table.ts +3 -3
- package/src/data-structures/heap/heap.ts +33 -60
- package/src/data-structures/heap/max-heap.ts +11 -2
- package/src/data-structures/heap/min-heap.ts +11 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +147 -145
- package/src/data-structures/linked-list/singly-linked-list.ts +52 -52
- package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +9 -2
- package/src/data-structures/queue/deque.ts +129 -144
- package/src/data-structures/queue/queue.ts +37 -26
- package/src/data-structures/stack/stack.ts +20 -14
- package/src/data-structures/trie/trie.ts +18 -13
- package/src/interfaces/binary-tree.ts +5 -5
- package/src/types/common.ts +37 -12
- package/src/types/data-structures/binary-tree/avl-tree.ts +0 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -26
- package/src/types/data-structures/binary-tree/bst.ts +0 -1
- package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/tree-multimap.ts +1 -1
|
@@ -7,14 +7,15 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import type {
|
|
9
9
|
BSTNested,
|
|
10
|
-
|
|
10
|
+
BSTNKeyOrNode,
|
|
11
11
|
BSTNodeNested,
|
|
12
12
|
BSTOptions,
|
|
13
13
|
BTNCallback,
|
|
14
|
-
|
|
14
|
+
BTNExemplar,
|
|
15
|
+
BTNKeyOrNode,
|
|
15
16
|
BTNodePureExemplar
|
|
16
17
|
} from '../../types';
|
|
17
|
-
import { BSTVariant,
|
|
18
|
+
import { BSTVariant, CP, IterationType } from '../../types';
|
|
18
19
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
19
20
|
import { IBinaryTree } from '../../interfaces';
|
|
20
21
|
import { Queue } from '../queue';
|
|
@@ -87,12 +88,12 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
87
88
|
/**
|
|
88
89
|
* This is the constructor function for a binary search tree class in TypeScript, which initializes
|
|
89
90
|
* the tree with optional elements and options.
|
|
90
|
-
* @param [elements] - An optional iterable of
|
|
91
|
+
* @param [elements] - An optional iterable of BTNExemplar objects that will be added to the
|
|
91
92
|
* binary search tree.
|
|
92
93
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
93
94
|
* configuration options for the binary search tree. It can have the following properties:
|
|
94
95
|
*/
|
|
95
|
-
constructor(elements?: Iterable<
|
|
96
|
+
constructor(elements?: Iterable<BTNExemplar<K, V, N>>, options?: Partial<BSTOptions<K>>) {
|
|
96
97
|
super([], options);
|
|
97
98
|
|
|
98
99
|
if (options) {
|
|
@@ -147,10 +148,10 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
147
148
|
|
|
148
149
|
/**
|
|
149
150
|
* The function checks if an exemplar is an instance of BSTNode.
|
|
150
|
-
* @param exemplar - The `exemplar` parameter is a variable of type `
|
|
151
|
+
* @param exemplar - The `exemplar` parameter is a variable of type `BTNExemplar<K, V, N>`.
|
|
151
152
|
* @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
|
|
152
153
|
*/
|
|
153
|
-
override isNode(exemplar:
|
|
154
|
+
override isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N {
|
|
154
155
|
return exemplar instanceof BSTNode;
|
|
155
156
|
}
|
|
156
157
|
|
|
@@ -158,12 +159,12 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
158
159
|
/**
|
|
159
160
|
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
|
|
160
161
|
* otherwise it returns undefined.
|
|
161
|
-
* @param exemplar - The `exemplar` parameter is of type `
|
|
162
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, where:
|
|
162
163
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
163
164
|
* `exemplarToNode` function. It represents the value associated with the exemplar node.
|
|
164
165
|
* @returns a node of type N or undefined.
|
|
165
166
|
*/
|
|
166
|
-
override exemplarToNode(exemplar:
|
|
167
|
+
override exemplarToNode(exemplar: BTNExemplar<K, V, N>, value?: V): N | undefined {
|
|
167
168
|
let node: N | undefined;
|
|
168
169
|
if (exemplar === null || exemplar === undefined) {
|
|
169
170
|
return;
|
|
@@ -201,7 +202,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
201
202
|
* @returns The method `add` returns either the newly added node (`newNode`) or `undefined` if the
|
|
202
203
|
* node was not added.
|
|
203
204
|
*/
|
|
204
|
-
override add(keyOrNodeOrEntry:
|
|
205
|
+
override add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V): N | undefined {
|
|
205
206
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
206
207
|
if (newNode === undefined) return;
|
|
207
208
|
|
|
@@ -273,7 +274,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
273
274
|
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
274
275
|
*/
|
|
275
276
|
override addMany(
|
|
276
|
-
keysOrNodesOrEntries: Iterable<
|
|
277
|
+
keysOrNodesOrEntries: Iterable<BTNExemplar<K, V, N>>,
|
|
277
278
|
values?: Iterable<V | undefined>,
|
|
278
279
|
isBalanceAdd = true,
|
|
279
280
|
iterationType = this.iterationType
|
|
@@ -297,7 +298,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
297
298
|
|
|
298
299
|
const realBTNExemplars: BTNodePureExemplar<K, V, N>[] = [];
|
|
299
300
|
|
|
300
|
-
const isRealBTNExemplar = (kve:
|
|
301
|
+
const isRealBTNExemplar = (kve: BTNExemplar<K, V, N>): kve is BTNodePureExemplar<K, V, N> => {
|
|
301
302
|
if (kve === undefined || kve === null) return false;
|
|
302
303
|
return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null));
|
|
303
304
|
}
|
|
@@ -379,7 +380,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
379
380
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
380
381
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
381
382
|
*/
|
|
382
|
-
lastKey(beginRoot:
|
|
383
|
+
lastKey(beginRoot: BSTNKeyOrNode<K, N> = this.root): K | undefined {
|
|
383
384
|
let current = this.ensureNode(beginRoot);
|
|
384
385
|
if (!current) return undefined;
|
|
385
386
|
|
|
@@ -448,7 +449,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
448
449
|
* data type.
|
|
449
450
|
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
450
451
|
*/
|
|
451
|
-
override isNotNodeInstance(potentialKey:
|
|
452
|
+
override isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K {
|
|
452
453
|
return !(potentialKey instanceof BSTNode)
|
|
453
454
|
}
|
|
454
455
|
|
|
@@ -466,7 +467,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
466
467
|
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
467
468
|
* @returns either a node object (N) or undefined.
|
|
468
469
|
*/
|
|
469
|
-
override ensureNode(key:
|
|
470
|
+
override ensureNode(key: BSTNKeyOrNode<K, N>, iterationType = IterationType.ITERATIVE): N | undefined {
|
|
470
471
|
return this.isNotNodeInstance(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
471
472
|
}
|
|
472
473
|
|
|
@@ -497,7 +498,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
497
498
|
identifier: ReturnType<C> | undefined,
|
|
498
499
|
callback: C = this._defaultOneParamCallback as C,
|
|
499
500
|
onlyOne = false,
|
|
500
|
-
beginRoot:
|
|
501
|
+
beginRoot: BSTNKeyOrNode<K, N> = this.root,
|
|
501
502
|
iterationType = this.iterationType
|
|
502
503
|
): N[] {
|
|
503
504
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -578,7 +579,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
578
579
|
lesserOrGreaterTraverse<C extends BTNCallback<N>>(
|
|
579
580
|
callback: C = this._defaultOneParamCallback as C,
|
|
580
581
|
lesserOrGreater: CP = CP.lt,
|
|
581
|
-
targetNode:
|
|
582
|
+
targetNode: BSTNKeyOrNode<K, N> = this.root,
|
|
582
583
|
iterationType = this.iterationType
|
|
583
584
|
): ReturnType<C>[] {
|
|
584
585
|
targetNode = this.ensureNode(targetNode);
|
|
@@ -7,11 +7,11 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import {
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
BinaryTreeDeleteResult,
|
|
11
|
+
BSTNKeyOrNode,
|
|
12
12
|
BTNCallback,
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
BTNExemplar,
|
|
14
|
+
BTNKeyOrNode,
|
|
15
15
|
IterationType,
|
|
16
16
|
RBTNColor,
|
|
17
17
|
RBTreeOptions,
|
|
@@ -20,7 +20,6 @@ import {
|
|
|
20
20
|
} from '../../types';
|
|
21
21
|
import { BST, BSTNode } from './bst';
|
|
22
22
|
import { IBinaryTree } from '../../interfaces';
|
|
23
|
-
import { BinaryTreeNode } from './binary-tree';
|
|
24
23
|
|
|
25
24
|
export class RedBlackTreeNode<K = any, V = any, N extends RedBlackTreeNode<K, V, N> = RedBlackTreeNodeNested<K, V>> extends BSTNode<
|
|
26
25
|
K, V,
|
|
@@ -49,7 +48,7 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
|
|
|
49
48
|
/**
|
|
50
49
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
|
51
50
|
* initializes the tree with optional elements and options.
|
|
52
|
-
* @param [elements] - The `elements` parameter is an optional iterable of `
|
|
51
|
+
* @param [elements] - The `elements` parameter is an optional iterable of `BTNExemplar<K, V, N>`
|
|
53
52
|
* objects. It represents the initial elements that will be added to the RBTree during its
|
|
54
53
|
* construction. If this parameter is provided, the `addMany` method is called to add all the
|
|
55
54
|
* elements to the
|
|
@@ -57,7 +56,7 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
|
|
|
57
56
|
* behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide
|
|
58
57
|
* only a subset of the properties defined in the `RBTreeOptions` interface.
|
|
59
58
|
*/
|
|
60
|
-
constructor(elements?: Iterable<
|
|
59
|
+
constructor(elements?: Iterable<BTNExemplar<K, V, N>>, options?: Partial<RBTreeOptions<K>>) {
|
|
61
60
|
super([], options);
|
|
62
61
|
|
|
63
62
|
this._root = this.Sentinel;
|
|
@@ -108,11 +107,11 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
|
|
|
108
107
|
|
|
109
108
|
/**
|
|
110
109
|
* The function checks if an exemplar is an instance of the RedBlackTreeNode class.
|
|
111
|
-
* @param exemplar - The `exemplar` parameter is of type `
|
|
110
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`.
|
|
112
111
|
* @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode
|
|
113
112
|
* class.
|
|
114
113
|
*/
|
|
115
|
-
override isNode(exemplar:
|
|
114
|
+
override isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N {
|
|
116
115
|
return exemplar instanceof RedBlackTreeNode;
|
|
117
116
|
}
|
|
118
117
|
|
|
@@ -122,19 +121,19 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
|
|
|
122
121
|
* data type.
|
|
123
122
|
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
124
123
|
*/
|
|
125
|
-
override isNotNodeInstance(potentialKey:
|
|
124
|
+
override isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K {
|
|
126
125
|
return !(potentialKey instanceof RedBlackTreeNode)
|
|
127
126
|
}
|
|
128
127
|
|
|
129
128
|
/**
|
|
130
129
|
* The function `exemplarToNode` takes an exemplar and converts it into a node object if possible.
|
|
131
|
-
* @param exemplar - The `exemplar` parameter is of type `
|
|
130
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, where:
|
|
132
131
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
133
132
|
* `exemplarToNode` function. It represents the value associated with the exemplar node. If a value
|
|
134
133
|
* is provided, it will be used when creating the new node. If no value is provided, the new node
|
|
135
134
|
* @returns a node of type N or undefined.
|
|
136
135
|
*/
|
|
137
|
-
override exemplarToNode(exemplar:
|
|
136
|
+
override exemplarToNode(exemplar: BTNExemplar<K, V, N>, value?: V): N | undefined {
|
|
138
137
|
let node: N | undefined;
|
|
139
138
|
|
|
140
139
|
if (exemplar === null || exemplar === undefined) {
|
|
@@ -173,7 +172,7 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
|
|
|
173
172
|
* being added to the binary search tree.
|
|
174
173
|
* @returns The method `add` returns either the newly added node (`N`) or `undefined`.
|
|
175
174
|
*/
|
|
176
|
-
override add(keyOrNodeOrEntry:
|
|
175
|
+
override add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V): N | undefined {
|
|
177
176
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
178
177
|
if (newNode === undefined) return;
|
|
179
178
|
|
|
@@ -242,13 +241,13 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
|
|
|
242
241
|
* @param {C} callback - The `callback` parameter is a function that takes a node of type `N` and
|
|
243
242
|
* returns a value of type `ReturnType<C>`. It is used to determine if a node should be deleted based
|
|
244
243
|
* on its identifier. The `callback` function is optional and defaults to `this._defaultOneParam
|
|
245
|
-
* @returns an array of `
|
|
244
|
+
* @returns an array of `BinaryTreeDeleteResult<N>`.
|
|
246
245
|
*/
|
|
247
246
|
delete<C extends BTNCallback<N>>(
|
|
248
247
|
identifier: ReturnType<C> | null | undefined,
|
|
249
248
|
callback: C = this._defaultOneParamCallback as C
|
|
250
|
-
):
|
|
251
|
-
const ans:
|
|
249
|
+
): BinaryTreeDeleteResult<N>[] {
|
|
250
|
+
const ans: BinaryTreeDeleteResult<N>[] = [];
|
|
252
251
|
if (identifier === null) return ans;
|
|
253
252
|
const helper = (node: N | undefined): void => {
|
|
254
253
|
let z: N = this.Sentinel;
|
|
@@ -365,10 +364,10 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
|
|
|
365
364
|
getNode<C extends BTNCallback<N>>(
|
|
366
365
|
identifier: ReturnType<C> | undefined,
|
|
367
366
|
callback: C = this._defaultOneParamCallback as C,
|
|
368
|
-
beginRoot:
|
|
367
|
+
beginRoot: BSTNKeyOrNode<K, N> = this.root,
|
|
369
368
|
iterationType = this.iterationType
|
|
370
369
|
): N | null | undefined {
|
|
371
|
-
if ((identifier as any) instanceof
|
|
370
|
+
if ((identifier as any) instanceof RedBlackTreeNode) callback = (node => node) as C;
|
|
372
371
|
beginRoot = this.ensureNode(beginRoot);
|
|
373
372
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
|
|
374
373
|
}
|
|
@@ -5,15 +5,17 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type {
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
import type {
|
|
9
|
+
BinaryTreeDeleteResult,
|
|
10
|
+
BSTNKeyOrNode,
|
|
11
11
|
BTNCallback,
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
BTNExemplar,
|
|
13
|
+
BTNKeyOrNode,
|
|
14
|
+
TreeMultimapNested,
|
|
15
|
+
TreeMultimapNodeNested,
|
|
16
|
+
TreeMultimapOptions
|
|
16
17
|
} from '../../types';
|
|
18
|
+
import { FamilyPosition, IterationType } from '../../types';
|
|
17
19
|
import { IBinaryTree } from '../../interfaces';
|
|
18
20
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
19
21
|
|
|
@@ -48,7 +50,7 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|
|
48
50
|
extends AVLTree<K, V, N, TREE>
|
|
49
51
|
implements IBinaryTree<K, V, N, TREE> {
|
|
50
52
|
|
|
51
|
-
constructor(elements?: Iterable<
|
|
53
|
+
constructor(elements?: Iterable<BTNExemplar<K, V, N>>, options?: Partial<TreeMultimapOptions<K>>) {
|
|
52
54
|
super([], options);
|
|
53
55
|
if (elements) this.addMany(elements);
|
|
54
56
|
}
|
|
@@ -84,11 +86,11 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|
|
84
86
|
|
|
85
87
|
/**
|
|
86
88
|
* The function checks if an exemplar is an instance of the TreeMultimapNode class.
|
|
87
|
-
* @param exemplar - The `exemplar` parameter is of type `
|
|
89
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`.
|
|
88
90
|
* @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
|
|
89
91
|
* class.
|
|
90
92
|
*/
|
|
91
|
-
override isNode(exemplar:
|
|
93
|
+
override isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N {
|
|
92
94
|
return exemplar instanceof TreeMultimapNode;
|
|
93
95
|
}
|
|
94
96
|
|
|
@@ -98,13 +100,13 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|
|
98
100
|
* data type.
|
|
99
101
|
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
100
102
|
*/
|
|
101
|
-
override isNotNodeInstance(potentialKey:
|
|
103
|
+
override isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K {
|
|
102
104
|
return !(potentialKey instanceof TreeMultimapNode)
|
|
103
105
|
}
|
|
104
106
|
|
|
105
107
|
/**
|
|
106
108
|
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
107
|
-
* @param exemplar - The `exemplar` parameter is of type `
|
|
109
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, which means it
|
|
108
110
|
* can be one of the following:
|
|
109
111
|
* @param {V} [value] - The `value` parameter is an optional argument that represents the value
|
|
110
112
|
* associated with the node. It is of type `V`, which can be any data type. If no value is provided,
|
|
@@ -113,7 +115,7 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|
|
113
115
|
* times the value should be added to the node. If not provided, it defaults to 1.
|
|
114
116
|
* @returns a node of type `N` or `undefined`.
|
|
115
117
|
*/
|
|
116
|
-
override exemplarToNode(exemplar:
|
|
118
|
+
override exemplarToNode(exemplar: BTNExemplar<K, V, N>, value?: V, count = 1): N | undefined {
|
|
117
119
|
let node: N | undefined;
|
|
118
120
|
if (exemplar === undefined || exemplar === null) {
|
|
119
121
|
return;
|
|
@@ -155,7 +157,7 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|
|
155
157
|
* @returns The method is returning either the newly inserted node or `undefined` if the insertion
|
|
156
158
|
* was not successful.
|
|
157
159
|
*/
|
|
158
|
-
override add(keyOrNodeOrEntry:
|
|
160
|
+
override add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V, count = 1): N | undefined {
|
|
159
161
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value, count);
|
|
160
162
|
if (newNode === undefined) return;
|
|
161
163
|
|
|
@@ -182,7 +184,7 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|
|
182
184
|
* either keys, nodes, or entries.
|
|
183
185
|
* @returns The method is returning an array of type `N | undefined`.
|
|
184
186
|
*/
|
|
185
|
-
override addMany(keysOrNodesOrEntries: Iterable<
|
|
187
|
+
override addMany(keysOrNodesOrEntries: Iterable<BTNExemplar<K, V, N>>): (N | undefined)[] {
|
|
186
188
|
return super.addMany(keysOrNodesOrEntries);
|
|
187
189
|
}
|
|
188
190
|
|
|
@@ -262,14 +264,14 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|
|
262
264
|
* being deleted. If set to true, the count of the node will not be considered and the node will be
|
|
263
265
|
* deleted regardless of its count. If set to false (default), the count of the node will be
|
|
264
266
|
* decremented by 1 and
|
|
265
|
-
* @returns an array of `
|
|
267
|
+
* @returns an array of `BinaryTreeDeleteResult<N>`.
|
|
266
268
|
*/
|
|
267
269
|
override delete<C extends BTNCallback<N>>(
|
|
268
270
|
identifier: ReturnType<C>,
|
|
269
271
|
callback: C = this._defaultOneParamCallback as C,
|
|
270
272
|
ignoreCount = false
|
|
271
|
-
):
|
|
272
|
-
const deletedResult:
|
|
273
|
+
): BinaryTreeDeleteResult<N>[] {
|
|
274
|
+
const deletedResult: BinaryTreeDeleteResult<N>[] = [];
|
|
273
275
|
if (!this.root) return deletedResult;
|
|
274
276
|
|
|
275
277
|
const curr: N | undefined = this.getNode(identifier, callback) ?? undefined;
|
|
@@ -370,7 +372,7 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|
|
370
372
|
* @returns The method `_addTo` returns either the `parent.left` or `parent.right` node that was
|
|
371
373
|
* added, or `undefined` if no node was added.
|
|
372
374
|
*/
|
|
373
|
-
protected override _addTo(newNode: N | undefined, parent:
|
|
375
|
+
protected override _addTo(newNode: N | undefined, parent: BSTNKeyOrNode<K, N>): N | undefined {
|
|
374
376
|
parent = this.ensureNode(parent);
|
|
375
377
|
if (parent) {
|
|
376
378
|
if (parent.left === undefined) {
|
|
@@ -405,7 +407,7 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|
|
405
407
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
406
408
|
* if either `srcNode` or `destNode` is undefined.
|
|
407
409
|
*/
|
|
408
|
-
protected override _swapProperties(srcNode:
|
|
410
|
+
protected override _swapProperties(srcNode: BSTNKeyOrNode<K, N>, destNode: BSTNKeyOrNode<K, N>): N | undefined {
|
|
409
411
|
srcNode = this.ensureNode(srcNode);
|
|
410
412
|
destNode = this.ensureNode(destNode);
|
|
411
413
|
if (srcNode && destNode) {
|
|
@@ -5,13 +5,12 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
+
import type { DijkstraResult, EntryCallback, VertexKey } from '../../types';
|
|
8
9
|
import { uuidV4 } from '../../utils';
|
|
9
|
-
import {
|
|
10
|
-
import type { DijkstraResult, VertexKey } from '../../types';
|
|
11
|
-
import { EntryCallback } from "../../types";
|
|
10
|
+
import { IterableEntryBase } from "../base";
|
|
12
11
|
import { IGraph } from '../../interfaces';
|
|
12
|
+
import { Heap } from '../heap';
|
|
13
13
|
import { Queue } from '../queue';
|
|
14
|
-
import { IterableEntryBase } from "../base";
|
|
15
14
|
|
|
16
15
|
export abstract class AbstractVertex<V = any> {
|
|
17
16
|
key: VertexKey;
|
|
@@ -527,14 +526,10 @@ export abstract class AbstractGraph<
|
|
|
527
526
|
*/
|
|
528
527
|
dijkstraWithoutHeap(
|
|
529
528
|
src: VO | VertexKey,
|
|
530
|
-
dest
|
|
531
|
-
getMinDist
|
|
532
|
-
genPaths
|
|
529
|
+
dest: VO | VertexKey | undefined = undefined,
|
|
530
|
+
getMinDist: boolean = false,
|
|
531
|
+
genPaths: boolean = false
|
|
533
532
|
): DijkstraResult<VO> {
|
|
534
|
-
if (getMinDist === undefined) getMinDist = false;
|
|
535
|
-
if (genPaths === undefined) genPaths = false;
|
|
536
|
-
|
|
537
|
-
if (dest === undefined) dest = undefined;
|
|
538
533
|
let minDist = Infinity;
|
|
539
534
|
let minDest: VO | undefined = undefined;
|
|
540
535
|
let minPath: VO[] = [];
|
|
@@ -675,14 +670,11 @@ export abstract class AbstractGraph<
|
|
|
675
670
|
*/
|
|
676
671
|
dijkstra(
|
|
677
672
|
src: VO | VertexKey,
|
|
678
|
-
dest
|
|
679
|
-
getMinDist
|
|
680
|
-
genPaths
|
|
673
|
+
dest: VO | VertexKey | undefined = undefined,
|
|
674
|
+
getMinDist: boolean = false,
|
|
675
|
+
genPaths: boolean = false
|
|
681
676
|
): DijkstraResult<VO> {
|
|
682
|
-
if (getMinDist === undefined) getMinDist = false;
|
|
683
|
-
if (genPaths === undefined) genPaths = false;
|
|
684
677
|
|
|
685
|
-
if (dest === undefined) dest = undefined;
|
|
686
678
|
let minDist = Infinity;
|
|
687
679
|
let minDest: VO | undefined = undefined;
|
|
688
680
|
let minPath: VO[] = [];
|
|
@@ -702,7 +694,7 @@ export abstract class AbstractGraph<
|
|
|
702
694
|
if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
|
|
703
695
|
}
|
|
704
696
|
|
|
705
|
-
const heap = new
|
|
697
|
+
const heap = new Heap<{ key: number; value: VO }>([], { comparator: (a, b) => a.key - b.key });
|
|
706
698
|
heap.add({ key: 0, value: srcVertex });
|
|
707
699
|
|
|
708
700
|
distMap.set(srcVertex, 0);
|
|
@@ -1094,15 +1086,33 @@ export abstract class AbstractGraph<
|
|
|
1094
1086
|
}
|
|
1095
1087
|
|
|
1096
1088
|
const cycles: Map<number, VO[]> = new Map();
|
|
1089
|
+
|
|
1097
1090
|
if (needCycles) {
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1091
|
+
const visitedMap: Map<VO, boolean> = new Map();
|
|
1092
|
+
const stack: VO[] = [];
|
|
1093
|
+
const findCyclesDFS = (cur: VO, parent: VO | undefined) => {
|
|
1094
|
+
visitedMap.set(cur, true);
|
|
1095
|
+
stack.push(cur);
|
|
1096
|
+
|
|
1097
|
+
const neighbors = this.getNeighbors(cur);
|
|
1098
|
+
|
|
1099
|
+
for (const neighbor of neighbors) {
|
|
1100
|
+
if (!visitedMap.get(neighbor)) {
|
|
1101
|
+
findCyclesDFS(neighbor, cur);
|
|
1102
|
+
} else if (stack.includes(neighbor) && neighbor !== parent) {
|
|
1103
|
+
const cycleStartIndex = stack.indexOf(neighbor);
|
|
1104
|
+
const cycle = stack.slice(cycleStartIndex);
|
|
1105
|
+
const cycleLow = Math.min(...cycle.map(v => dfnMap.get(v) || Infinity));
|
|
1106
|
+
cycles.set(cycleLow, cycle);
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
|
|
1110
|
+
stack.pop();
|
|
1111
|
+
};
|
|
1102
1112
|
|
|
1103
|
-
|
|
1104
|
-
if (
|
|
1105
|
-
|
|
1113
|
+
vertexMap.forEach(v => {
|
|
1114
|
+
if (!visitedMap.get(v)) {
|
|
1115
|
+
findCyclesDFS(v, undefined);
|
|
1106
1116
|
}
|
|
1107
1117
|
});
|
|
1108
1118
|
}
|
|
@@ -5,10 +5,10 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { arrayRemove } from '../../utils';
|
|
9
|
-
import { AbstractEdge, AbstractGraph, AbstractVertex } from './abstract-graph';
|
|
10
8
|
import type { TopologicalStatus, VertexKey } from '../../types';
|
|
9
|
+
import { AbstractEdge, AbstractGraph, AbstractVertex } from './abstract-graph';
|
|
11
10
|
import { IGraph } from '../../interfaces';
|
|
11
|
+
import { arrayRemove } from '../../utils';
|
|
12
12
|
|
|
13
13
|
export class DirectedVertex<V = any> extends AbstractVertex<V> {
|
|
14
14
|
/**
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { MapGraphCoordinate, VertexKey } from '../../types';
|
|
1
|
+
import type { MapGraphCoordinate, VertexKey } from '../../types';
|
|
2
2
|
import { DirectedEdge, DirectedGraph, DirectedVertex } from './directed-graph';
|
|
3
3
|
|
|
4
4
|
export class MapVertex<V = any> extends DirectedVertex<V> {
|
|
@@ -5,10 +5,10 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { arrayRemove } from '../../utils';
|
|
9
|
-
import { AbstractEdge, AbstractGraph, AbstractVertex } from './abstract-graph';
|
|
10
8
|
import type { VertexKey } from '../../types';
|
|
11
9
|
import { IGraph } from '../../interfaces';
|
|
10
|
+
import { AbstractEdge, AbstractGraph, AbstractVertex } from './abstract-graph';
|
|
11
|
+
import { arrayRemove } from '../../utils';
|
|
12
12
|
|
|
13
13
|
export class UndirectedVertex<V = any> extends AbstractVertex<V> {
|
|
14
14
|
/**
|