min-heap-typed 1.41.0 → 1.41.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/binary-tree.d.ts +6 -3
- package/dist/data-structures/binary-tree/binary-tree.js +30 -8
- package/dist/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/data-structures/binary-tree/bst.js +2 -2
- package/dist/data-structures/binary-tree/rb-tree.d.ts +5 -4
- package/dist/data-structures/binary-tree/rb-tree.js +42 -44
- package/dist/data-structures/binary-tree/tree-multiset.js +2 -2
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +3 -2
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +1 -1
- package/src/data-structures/binary-tree/binary-tree.ts +65 -17
- package/src/data-structures/binary-tree/bst.ts +7 -4
- package/src/data-structures/binary-tree/rb-tree.ts +47 -68
- package/src/data-structures/binary-tree/tree-multiset.ts +4 -3
- package/src/data-structures/graph/abstract-graph.ts +11 -12
- package/src/data-structures/graph/directed-graph.ts +7 -6
- package/src/data-structures/graph/undirected-graph.ts +7 -6
- 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 +2 -2
- package/src/data-structures/heap/max-heap.ts +1 -1
- package/src/data-structures/heap/min-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/matrix/matrix.ts +1 -1
- package/src/data-structures/matrix/vector2d.ts +1 -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 +1 -1
- package/src/data-structures/queue/deque.ts +3 -4
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/types/data-structures/matrix/navigator.ts +1 -1
- package/src/types/utils/utils.ts +1 -1
- package/src/types/utils/validate-type.ts +2 -2
|
@@ -19,7 +19,8 @@ export class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extend
|
|
|
19
19
|
|
|
20
20
|
export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>>
|
|
21
21
|
extends BinaryTree<V, N>
|
|
22
|
-
implements IBinaryTree<V, N>
|
|
22
|
+
implements IBinaryTree<V, N>
|
|
23
|
+
{
|
|
23
24
|
/**
|
|
24
25
|
* The constructor function initializes a binary search tree object with an optional comparator
|
|
25
26
|
* function.
|
|
@@ -153,7 +154,9 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
153
154
|
return super.addMany(keysOrNodes, data);
|
|
154
155
|
}
|
|
155
156
|
const inserted: (N | null | undefined)[] = [];
|
|
156
|
-
const combinedArr: [BTNKey | N, V][] = keysOrNodes.map(
|
|
157
|
+
const combinedArr: [BTNKey | N, V][] = keysOrNodes.map(
|
|
158
|
+
(value: BTNKey | N, index) => [value, data?.[index]] as [BTNKey | N, V]
|
|
159
|
+
);
|
|
157
160
|
let sorted = [];
|
|
158
161
|
|
|
159
162
|
function isNodeOrNullTuple(arr: [BTNKey | N, V][]): arr is [N, V][] {
|
|
@@ -231,7 +234,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
231
234
|
* @returns either the first node that matches the given nodeProperty and callback, or null if no
|
|
232
235
|
* matching node is found.
|
|
233
236
|
*/
|
|
234
|
-
override
|
|
237
|
+
override getNode<C extends BTNCallback<N>>(
|
|
235
238
|
identifier: ReturnType<C> | null,
|
|
236
239
|
callback: C = ((node: N) => node.key) as C,
|
|
237
240
|
beginRoot = this.root,
|
|
@@ -362,7 +365,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
362
365
|
targetNode: BTNKey | N | null = this.root,
|
|
363
366
|
iterationType = this.iterationType
|
|
364
367
|
): ReturnType<C>[] {
|
|
365
|
-
if (typeof targetNode === 'number') targetNode = this.
|
|
368
|
+
if (typeof targetNode === 'number') targetNode = this.getNode(targetNode);
|
|
366
369
|
const ans: ReturnType<BTNCallback<N>>[] = [];
|
|
367
370
|
if (!targetNode) return ans;
|
|
368
371
|
const targetKey = targetNode.key;
|
|
@@ -1,27 +1,26 @@
|
|
|
1
|
-
import {RBTNColor} from
|
|
1
|
+
import {RBTNColor} from '../../types';
|
|
2
2
|
|
|
3
3
|
export class RBTreeNode {
|
|
4
|
-
key: number
|
|
4
|
+
key: number;
|
|
5
5
|
parent: RBTreeNode;
|
|
6
6
|
left: RBTreeNode;
|
|
7
7
|
right: RBTreeNode;
|
|
8
8
|
color: number = RBTNColor.BLACK;
|
|
9
9
|
|
|
10
|
-
constructor() {
|
|
10
|
+
constructor(key: number, color: RBTNColor = RBTNColor.BLACK) {
|
|
11
|
+
this.key = key;
|
|
12
|
+
this.color = color;
|
|
11
13
|
this.parent = null as unknown as RBTreeNode;
|
|
12
14
|
this.left = null as unknown as RBTreeNode;
|
|
13
15
|
this.right = null as unknown as RBTreeNode;
|
|
14
16
|
}
|
|
15
17
|
}
|
|
16
18
|
|
|
19
|
+
export const SN = new RBTreeNode(0);
|
|
20
|
+
|
|
17
21
|
export class RedBlackTree {
|
|
18
|
-
|
|
19
22
|
constructor() {
|
|
20
|
-
this.
|
|
21
|
-
this.NIL.color = RBTNColor.BLACK;
|
|
22
|
-
this.NIL.left = null as unknown as RBTreeNode;
|
|
23
|
-
this.NIL.right = null as unknown as RBTreeNode;
|
|
24
|
-
this._root = this.NIL;
|
|
23
|
+
this._root = SN;
|
|
25
24
|
}
|
|
26
25
|
|
|
27
26
|
protected _root: RBTreeNode;
|
|
@@ -30,12 +29,6 @@ export class RedBlackTree {
|
|
|
30
29
|
return this._root;
|
|
31
30
|
}
|
|
32
31
|
|
|
33
|
-
protected _NIL: RBTreeNode;
|
|
34
|
-
|
|
35
|
-
get NIL(): RBTreeNode {
|
|
36
|
-
return this._NIL;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
32
|
/**
|
|
40
33
|
* The `insert` function inserts a new node with a given key into a red-black tree and fixes any
|
|
41
34
|
* violations of the red-black tree properties.
|
|
@@ -44,18 +37,14 @@ export class RedBlackTree {
|
|
|
44
37
|
* @returns The function does not explicitly return anything.
|
|
45
38
|
*/
|
|
46
39
|
insert(key: number): void {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
node.
|
|
50
|
-
node.key = key;
|
|
51
|
-
node.left = this.NIL;
|
|
52
|
-
node.right = this.NIL;
|
|
53
|
-
node.color = RBTNColor.RED;
|
|
40
|
+
const node: RBTreeNode = new RBTreeNode(key, RBTNColor.RED);
|
|
41
|
+
node.left = SN;
|
|
42
|
+
node.right = SN;
|
|
54
43
|
|
|
55
44
|
let y: RBTreeNode = null as unknown as RBTreeNode;
|
|
56
45
|
let x: RBTreeNode = this.root;
|
|
57
46
|
|
|
58
|
-
while (x !==
|
|
47
|
+
while (x !== SN) {
|
|
59
48
|
y = x;
|
|
60
49
|
if (node.key < x.key) {
|
|
61
50
|
x = x.left;
|
|
@@ -94,10 +83,9 @@ export class RedBlackTree {
|
|
|
94
83
|
*/
|
|
95
84
|
delete(key: number): void {
|
|
96
85
|
const helper = (node: RBTreeNode): void => {
|
|
97
|
-
|
|
98
|
-
let z: RBTreeNode = this.NIL;
|
|
86
|
+
let z: RBTreeNode = SN;
|
|
99
87
|
let x: RBTreeNode, y: RBTreeNode;
|
|
100
|
-
while (node !==
|
|
88
|
+
while (node !== SN) {
|
|
101
89
|
if (node.key === key) {
|
|
102
90
|
z = node;
|
|
103
91
|
}
|
|
@@ -109,17 +97,16 @@ export class RedBlackTree {
|
|
|
109
97
|
}
|
|
110
98
|
}
|
|
111
99
|
|
|
112
|
-
if (z ===
|
|
113
|
-
console.log("Couldn't find key in the tree");
|
|
100
|
+
if (z === SN) {
|
|
114
101
|
return;
|
|
115
102
|
}
|
|
116
103
|
|
|
117
104
|
y = z;
|
|
118
105
|
let yOriginalColor: number = y.color;
|
|
119
|
-
if (z.left ===
|
|
106
|
+
if (z.left === SN) {
|
|
120
107
|
x = z.right;
|
|
121
108
|
this._rbTransplant(z, z.right);
|
|
122
|
-
} else if (z.right ===
|
|
109
|
+
} else if (z.right === SN) {
|
|
123
110
|
x = z.left;
|
|
124
111
|
this._rbTransplant(z, z.left);
|
|
125
112
|
} else {
|
|
@@ -139,13 +126,17 @@ export class RedBlackTree {
|
|
|
139
126
|
y.left.parent = y;
|
|
140
127
|
y.color = z.color;
|
|
141
128
|
}
|
|
142
|
-
if (yOriginalColor ===
|
|
129
|
+
if (yOriginalColor === RBTNColor.BLACK) {
|
|
143
130
|
this._fixDelete(x);
|
|
144
131
|
}
|
|
145
|
-
}
|
|
132
|
+
};
|
|
146
133
|
helper(this.root);
|
|
147
134
|
}
|
|
148
135
|
|
|
136
|
+
isRealNode(node: RBTreeNode): node is RBTreeNode {
|
|
137
|
+
return node !== SN && node !== null;
|
|
138
|
+
}
|
|
139
|
+
|
|
149
140
|
/**
|
|
150
141
|
* The function `getNode` is a recursive depth-first search algorithm that searches for a node with a
|
|
151
142
|
* given key in a red-black tree.
|
|
@@ -158,40 +149,40 @@ export class RedBlackTree {
|
|
|
158
149
|
*/
|
|
159
150
|
getNode(key: number, beginRoot = this.root): RBTreeNode {
|
|
160
151
|
const dfs = (node: RBTreeNode): RBTreeNode => {
|
|
161
|
-
if (
|
|
162
|
-
|
|
163
|
-
|
|
152
|
+
if (this.isRealNode(node)) {
|
|
153
|
+
if (key === node.key) {
|
|
154
|
+
return node;
|
|
155
|
+
}
|
|
164
156
|
|
|
165
|
-
|
|
166
|
-
return dfs(node.
|
|
157
|
+
if (key < node.key) return dfs(node.left);
|
|
158
|
+
return dfs(node.right);
|
|
159
|
+
} else {
|
|
160
|
+
return null as unknown as RBTreeNode;
|
|
167
161
|
}
|
|
168
|
-
|
|
169
|
-
}
|
|
162
|
+
};
|
|
170
163
|
return dfs(beginRoot);
|
|
171
164
|
}
|
|
172
165
|
|
|
173
|
-
|
|
174
166
|
/**
|
|
175
167
|
* The function returns the leftmost node in a red-black tree.
|
|
176
168
|
* @param {RBTreeNode} node - The parameter "node" is of type RBTreeNode, which represents a node in
|
|
177
169
|
* a Red-Black Tree.
|
|
178
170
|
* @returns The leftmost node in the given RBTreeNode.
|
|
179
171
|
*/
|
|
180
|
-
getLeftMost(node: RBTreeNode): RBTreeNode {
|
|
181
|
-
while (node.left !== null && node.left !==
|
|
172
|
+
getLeftMost(node: RBTreeNode = this.root): RBTreeNode {
|
|
173
|
+
while (node.left !== null && node.left !== SN) {
|
|
182
174
|
node = node.left;
|
|
183
175
|
}
|
|
184
176
|
return node;
|
|
185
177
|
}
|
|
186
178
|
|
|
187
|
-
|
|
188
179
|
/**
|
|
189
180
|
* The function returns the rightmost node in a red-black tree.
|
|
190
181
|
* @param {RBTreeNode} node - The parameter "node" is of type RBTreeNode.
|
|
191
182
|
* @returns the rightmost node in a red-black tree.
|
|
192
183
|
*/
|
|
193
184
|
getRightMost(node: RBTreeNode): RBTreeNode {
|
|
194
|
-
while (node.right !== null && node.right !==
|
|
185
|
+
while (node.right !== null && node.right !== SN) {
|
|
195
186
|
node = node.right;
|
|
196
187
|
}
|
|
197
188
|
return node;
|
|
@@ -203,13 +194,12 @@ export class RedBlackTree {
|
|
|
203
194
|
* @returns the successor of the given RBTreeNode.
|
|
204
195
|
*/
|
|
205
196
|
getSuccessor(x: RBTreeNode): RBTreeNode {
|
|
206
|
-
|
|
207
|
-
if (x.right !== this.NIL) {
|
|
197
|
+
if (x.right !== SN) {
|
|
208
198
|
return this.getLeftMost(x.right);
|
|
209
199
|
}
|
|
210
200
|
|
|
211
201
|
let y: RBTreeNode = x.parent;
|
|
212
|
-
while (y !==
|
|
202
|
+
while (y !== SN && y !== null && x === y.right) {
|
|
213
203
|
x = y;
|
|
214
204
|
y = y.parent;
|
|
215
205
|
}
|
|
@@ -223,13 +213,12 @@ export class RedBlackTree {
|
|
|
223
213
|
* @returns the predecessor of the given RBTreeNode 'x'.
|
|
224
214
|
*/
|
|
225
215
|
getPredecessor(x: RBTreeNode): RBTreeNode {
|
|
226
|
-
|
|
227
|
-
if (x.left !== this.NIL) {
|
|
216
|
+
if (x.left !== SN) {
|
|
228
217
|
return this.getRightMost(x.left);
|
|
229
218
|
}
|
|
230
219
|
|
|
231
220
|
let y: RBTreeNode = x.parent;
|
|
232
|
-
while (y !==
|
|
221
|
+
while (y !== SN && x === y.left) {
|
|
233
222
|
x = y;
|
|
234
223
|
y = y.parent;
|
|
235
224
|
}
|
|
@@ -244,7 +233,7 @@ export class RedBlackTree {
|
|
|
244
233
|
protected _leftRotate(x: RBTreeNode): void {
|
|
245
234
|
const y: RBTreeNode = x.right;
|
|
246
235
|
x.right = y.left;
|
|
247
|
-
if (y.left !==
|
|
236
|
+
if (y.left !== SN) {
|
|
248
237
|
y.left.parent = x;
|
|
249
238
|
}
|
|
250
239
|
y.parent = x.parent;
|
|
@@ -267,7 +256,7 @@ export class RedBlackTree {
|
|
|
267
256
|
protected _rightRotate(x: RBTreeNode): void {
|
|
268
257
|
const y: RBTreeNode = x.left;
|
|
269
258
|
x.left = y.right;
|
|
270
|
-
if (y.right !==
|
|
259
|
+
if (y.right !== SN) {
|
|
271
260
|
y.right.parent = x;
|
|
272
261
|
}
|
|
273
262
|
y.parent = x.parent;
|
|
@@ -289,24 +278,21 @@ export class RedBlackTree {
|
|
|
289
278
|
*/
|
|
290
279
|
protected _fixDelete(x: RBTreeNode): void {
|
|
291
280
|
let s: RBTreeNode;
|
|
292
|
-
while (x !== this.root && x.color ===
|
|
281
|
+
while (x !== this.root && x.color === RBTNColor.BLACK) {
|
|
293
282
|
if (x === x.parent.left) {
|
|
294
283
|
s = x.parent.right;
|
|
295
284
|
if (s.color === 1) {
|
|
296
|
-
|
|
297
285
|
s.color = RBTNColor.BLACK;
|
|
298
286
|
x.parent.color = RBTNColor.RED;
|
|
299
287
|
this._leftRotate(x.parent);
|
|
300
288
|
s = x.parent.right;
|
|
301
289
|
}
|
|
302
290
|
|
|
303
|
-
if (s.left.color ===
|
|
304
|
-
|
|
291
|
+
if (s.left !== null && s.left.color === RBTNColor.BLACK && s.right.color === RBTNColor.BLACK) {
|
|
305
292
|
s.color = RBTNColor.RED;
|
|
306
293
|
x = x.parent;
|
|
307
294
|
} else {
|
|
308
|
-
if (s.right.color ===
|
|
309
|
-
|
|
295
|
+
if (s.right.color === RBTNColor.BLACK) {
|
|
310
296
|
s.left.color = RBTNColor.BLACK;
|
|
311
297
|
s.color = RBTNColor.RED;
|
|
312
298
|
this._rightRotate(s);
|
|
@@ -322,20 +308,17 @@ export class RedBlackTree {
|
|
|
322
308
|
} else {
|
|
323
309
|
s = x.parent.left;
|
|
324
310
|
if (s.color === 1) {
|
|
325
|
-
|
|
326
311
|
s.color = RBTNColor.BLACK;
|
|
327
312
|
x.parent.color = RBTNColor.RED;
|
|
328
313
|
this._rightRotate(x.parent);
|
|
329
314
|
s = x.parent.left;
|
|
330
315
|
}
|
|
331
316
|
|
|
332
|
-
if (s.right.color ===
|
|
333
|
-
|
|
317
|
+
if (s.right.color === RBTNColor.BLACK && s.right.color === RBTNColor.BLACK) {
|
|
334
318
|
s.color = RBTNColor.RED;
|
|
335
319
|
x = x.parent;
|
|
336
320
|
} else {
|
|
337
|
-
if (s.left.color ===
|
|
338
|
-
|
|
321
|
+
if (s.left.color === RBTNColor.BLACK) {
|
|
339
322
|
s.right.color = RBTNColor.BLACK;
|
|
340
323
|
s.color = RBTNColor.RED;
|
|
341
324
|
this._leftRotate(s);
|
|
@@ -380,14 +363,12 @@ export class RedBlackTree {
|
|
|
380
363
|
if (k.parent === k.parent.parent.right) {
|
|
381
364
|
u = k.parent.parent.left;
|
|
382
365
|
if (u.color === 1) {
|
|
383
|
-
|
|
384
366
|
u.color = RBTNColor.BLACK;
|
|
385
367
|
k.parent.color = RBTNColor.BLACK;
|
|
386
368
|
k.parent.parent.color = RBTNColor.RED;
|
|
387
369
|
k = k.parent.parent;
|
|
388
370
|
} else {
|
|
389
371
|
if (k === k.parent.left) {
|
|
390
|
-
|
|
391
372
|
k = k.parent;
|
|
392
373
|
this._rightRotate(k);
|
|
393
374
|
}
|
|
@@ -400,14 +381,12 @@ export class RedBlackTree {
|
|
|
400
381
|
u = k.parent.parent.right;
|
|
401
382
|
|
|
402
383
|
if (u.color === 1) {
|
|
403
|
-
|
|
404
384
|
u.color = RBTNColor.BLACK;
|
|
405
385
|
k.parent.color = RBTNColor.BLACK;
|
|
406
386
|
k.parent.parent.color = RBTNColor.RED;
|
|
407
387
|
k = k.parent.parent;
|
|
408
388
|
} else {
|
|
409
389
|
if (k === k.parent.right) {
|
|
410
|
-
|
|
411
390
|
k = k.parent;
|
|
412
391
|
this._leftRotate(k);
|
|
413
392
|
}
|
|
@@ -423,4 +402,4 @@ export class RedBlackTree {
|
|
|
423
402
|
}
|
|
424
403
|
this.root.color = RBTNColor.BLACK;
|
|
425
404
|
}
|
|
426
|
-
}
|
|
405
|
+
}
|
|
@@ -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.
|
|
@@ -169,7 +170,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
169
170
|
} else if (parent.right === undefined) {
|
|
170
171
|
parent.right = newNode;
|
|
171
172
|
if (newNode !== null) {
|
|
172
|
-
this._size =
|
|
173
|
+
this._size = this.size + 1;
|
|
173
174
|
this._setCount(this.count + newNode.count);
|
|
174
175
|
}
|
|
175
176
|
return parent.right;
|
|
@@ -282,7 +283,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
282
283
|
const bstDeletedResult: BinaryTreeDeletedResult<N>[] = [];
|
|
283
284
|
if (!this.root) return bstDeletedResult;
|
|
284
285
|
|
|
285
|
-
const curr: N | null = this.
|
|
286
|
+
const curr: N | null = this.getNode(identifier, callback);
|
|
286
287
|
if (!curr) return bstDeletedResult;
|
|
287
288
|
|
|
288
289
|
const parent: N | null = curr?.parent ? curr.parent : null;
|
|
@@ -26,7 +26,6 @@ export abstract class AbstractVertex<V = any> {
|
|
|
26
26
|
this.key = key;
|
|
27
27
|
this.value = value;
|
|
28
28
|
}
|
|
29
|
-
|
|
30
29
|
}
|
|
31
30
|
|
|
32
31
|
export abstract class AbstractEdge<E = any> {
|
|
@@ -65,7 +64,8 @@ export abstract class AbstractGraph<
|
|
|
65
64
|
E = any,
|
|
66
65
|
VO extends AbstractVertex<V> = AbstractVertex<V>,
|
|
67
66
|
EO extends AbstractEdge<E> = AbstractEdge<E>
|
|
68
|
-
> implements IGraph<V, E, VO, EO>
|
|
67
|
+
> implements IGraph<V, E, VO, EO>
|
|
68
|
+
{
|
|
69
69
|
protected _vertices: Map<VertexKey, VO> = new Map<VertexKey, VO>();
|
|
70
70
|
|
|
71
71
|
get vertices(): Map<VertexKey, VO> {
|
|
@@ -513,14 +513,14 @@ export abstract class AbstractGraph<
|
|
|
513
513
|
}
|
|
514
514
|
|
|
515
515
|
getMinDist &&
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
516
|
+
distMap.forEach((d, v) => {
|
|
517
|
+
if (v !== srcVertex) {
|
|
518
|
+
if (d < minDist) {
|
|
519
|
+
minDist = d;
|
|
520
|
+
if (genPaths) minDest = v;
|
|
521
|
+
}
|
|
521
522
|
}
|
|
522
|
-
}
|
|
523
|
-
});
|
|
523
|
+
});
|
|
524
524
|
|
|
525
525
|
genPaths && getPaths(minDest);
|
|
526
526
|
|
|
@@ -582,7 +582,7 @@ export abstract class AbstractGraph<
|
|
|
582
582
|
if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
|
|
583
583
|
}
|
|
584
584
|
|
|
585
|
-
const heap = new PriorityQueue<{
|
|
585
|
+
const heap = new PriorityQueue<{key: number; value: VO}>({comparator: (a, b) => a.key - b.key});
|
|
586
586
|
heap.add({key: 0, value: srcVertex});
|
|
587
587
|
|
|
588
588
|
distMap.set(srcVertex, 0);
|
|
@@ -811,7 +811,7 @@ export abstract class AbstractGraph<
|
|
|
811
811
|
* `predecessor` property is a 2D array of vertices (or `null`) representing the predecessor vertices in the shortest
|
|
812
812
|
* path between vertices in the
|
|
813
813
|
*/
|
|
814
|
-
floyd(): {
|
|
814
|
+
floyd(): {costs: number[][]; predecessor: (VO | null)[][]} {
|
|
815
815
|
const idAndVertices = [...this._vertices];
|
|
816
816
|
const n = idAndVertices.length;
|
|
817
817
|
|
|
@@ -996,5 +996,4 @@ export abstract class AbstractGraph<
|
|
|
996
996
|
protected _getVertexKey(vertexOrKey: VO | VertexKey): VertexKey {
|
|
997
997
|
return vertexOrKey instanceof AbstractVertex ? vertexOrKey.key : vertexOrKey;
|
|
998
998
|
}
|
|
999
|
-
|
|
1000
999
|
}
|
|
@@ -46,13 +46,14 @@ 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>
|
|
55
|
+
implements IGraph<V, E, VO, EO>
|
|
56
|
+
{
|
|
56
57
|
/**
|
|
57
58
|
* The constructor function initializes an instance of a class.
|
|
58
59
|
*/
|
|
@@ -43,13 +43,14 @@ 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>
|
|
52
|
+
implements IGraph<V, E, VO, EO>
|
|
53
|
+
{
|
|
53
54
|
/**
|
|
54
55
|
* The constructor initializes a new Map object to store edges.
|
|
55
56
|
*/
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
export class TreeMap {
|
|
2
|
-
}
|
|
1
|
+
export class TreeMap {}
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
export class TreeSet {
|
|
2
|
-
}
|
|
1
|
+
export class TreeSet {}
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
import type {Comparator, DFSOrderPattern} from '../../types';
|
|
9
9
|
|
|
10
10
|
export class Heap<E = any> {
|
|
11
|
-
constructor(options: {
|
|
11
|
+
constructor(options: {comparator: Comparator<E>; nodes?: E[]}) {
|
|
12
12
|
this._comparator = options.comparator;
|
|
13
13
|
if (options.nodes && options.nodes.length > 0) {
|
|
14
14
|
this._nodes = options.nodes;
|
|
@@ -48,7 +48,7 @@ export class Heap<E = any> {
|
|
|
48
48
|
* @returns A new Heap instance.
|
|
49
49
|
* @param options
|
|
50
50
|
*/
|
|
51
|
-
static heapify<E>(options: {
|
|
51
|
+
static heapify<E>(options: {nodes: E[]; comparator: Comparator<E>}): Heap<E> {
|
|
52
52
|
return new Heap<E>(options);
|
|
53
53
|
}
|
|
54
54
|
|
|
@@ -11,7 +11,7 @@ import type {Comparator} from '../../types';
|
|
|
11
11
|
|
|
12
12
|
export class MaxHeap<E = any> extends Heap<E> {
|
|
13
13
|
constructor(
|
|
14
|
-
options: {
|
|
14
|
+
options: {comparator: Comparator<E>; nodes?: E[]} = {
|
|
15
15
|
comparator: (a: E, b: E) => {
|
|
16
16
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
17
17
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -11,7 +11,7 @@ import type {Comparator} from '../../types';
|
|
|
11
11
|
|
|
12
12
|
export class MinHeap<E = any> extends Heap<E> {
|
|
13
13
|
constructor(
|
|
14
|
-
options: {
|
|
14
|
+
options: {comparator: Comparator<E>; nodes?: E[]} = {
|
|
15
15
|
comparator: (a: E, b: E) => {
|
|
16
16
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
17
17
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -594,7 +594,7 @@ export class DoublyLinkedList<E = any> {
|
|
|
594
594
|
/**
|
|
595
595
|
* The function returns an iterator that iterates over the values of a linked list.
|
|
596
596
|
*/
|
|
597
|
-
*
|
|
597
|
+
*[Symbol.iterator]() {
|
|
598
598
|
let current = this.head;
|
|
599
599
|
|
|
600
600
|
while (current) {
|
|
@@ -565,7 +565,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
565
565
|
/**
|
|
566
566
|
* The function returns an iterator that iterates over the values of a linked list.
|
|
567
567
|
*/
|
|
568
|
-
*
|
|
568
|
+
*[Symbol.iterator]() {
|
|
569
569
|
let current = this.head;
|
|
570
570
|
|
|
571
571
|
while (current) {
|
|
@@ -14,7 +14,7 @@ export class MatrixNTI2D<V = any> {
|
|
|
14
14
|
* given initial value or 0 if not provided.
|
|
15
15
|
* @param options - An object containing the following properties:
|
|
16
16
|
*/
|
|
17
|
-
constructor(options: {
|
|
17
|
+
constructor(options: {row: number; col: number; initialVal?: V}) {
|
|
18
18
|
const {row, col, initialVal} = options;
|
|
19
19
|
this._matrix = new Array(row).fill(undefined).map(() => new Array(col).fill(initialVal || 0));
|
|
20
20
|
}
|
|
@@ -10,7 +10,7 @@ import type {Comparator} from '../../types';
|
|
|
10
10
|
|
|
11
11
|
export class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
12
12
|
constructor(
|
|
13
|
-
options: {
|
|
13
|
+
options: {comparator: Comparator<E>; nodes?: E[]} = {
|
|
14
14
|
comparator: (a: E, b: E) => {
|
|
15
15
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
16
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -10,7 +10,7 @@ import type {Comparator} from '../../types';
|
|
|
10
10
|
|
|
11
11
|
export class MinPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
12
12
|
constructor(
|
|
13
|
-
options: {
|
|
13
|
+
options: {comparator: Comparator<E>; nodes?: E[]} = {
|
|
14
14
|
comparator: (a: E, b: E) => {
|
|
15
15
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
16
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -10,7 +10,7 @@ import {Heap} from '../heap';
|
|
|
10
10
|
import {Comparator} from '../../types';
|
|
11
11
|
|
|
12
12
|
export class PriorityQueue<E = any> extends Heap<E> {
|
|
13
|
-
constructor(options: {
|
|
13
|
+
constructor(options: {comparator: Comparator<E>; nodes?: E[]}) {
|
|
14
14
|
super(options);
|
|
15
15
|
}
|
|
16
16
|
}
|
|
@@ -9,8 +9,7 @@ import {DoublyLinkedList} from '../linked-list';
|
|
|
9
9
|
|
|
10
10
|
// O(n) time complexity of obtaining the value
|
|
11
11
|
// O(1) time complexity of adding at the beginning and the end
|
|
12
|
-
export class Deque<E = any> extends DoublyLinkedList<E> {
|
|
13
|
-
}
|
|
12
|
+
export class Deque<E = any> extends DoublyLinkedList<E> {}
|
|
14
13
|
|
|
15
14
|
// O(1) time complexity of obtaining the value
|
|
16
15
|
// O(n) time complexity of adding at the beginning and the end
|
|
@@ -20,9 +19,9 @@ export class ObjectDeque<E = number> {
|
|
|
20
19
|
if (capacity !== undefined) this._capacity = capacity;
|
|
21
20
|
}
|
|
22
21
|
|
|
23
|
-
protected _nodes: {
|
|
22
|
+
protected _nodes: {[key: number]: E} = {};
|
|
24
23
|
|
|
25
|
-
get nodes(): {
|
|
24
|
+
get nodes(): {[p: number]: E} {
|
|
26
25
|
return this._nodes;
|
|
27
26
|
}
|
|
28
27
|
|
package/src/types/utils/utils.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export type ToThunkFn = () => ReturnType<TrlFn>;
|
|
2
|
-
export type Thunk = () => ReturnType<ToThunkFn> & {
|
|
2
|
+
export type Thunk = () => ReturnType<ToThunkFn> & {__THUNK__: symbol};
|
|
3
3
|
export type TrlFn = (...args: any[]) => any;
|
|
4
4
|
export type TrlAsyncFn = (...args: any[]) => any;
|
|
5
5
|
|