graph-typed 1.43.1 → 1.44.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/binary-tree.d.ts +8 -8
- package/dist/data-structures/binary-tree/binary-tree.js +62 -62
- package/dist/data-structures/binary-tree/rb-tree.d.ts +4 -4
- package/dist/data-structures/binary-tree/rb-tree.js +1 -1
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +24 -24
- package/dist/data-structures/binary-tree/tree-multimap.js +50 -50
- package/dist/data-structures/graph/abstract-graph.d.ts +37 -37
- package/dist/data-structures/graph/abstract-graph.js +37 -37
- package/dist/interfaces/binary-tree.d.ts +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +2 -3
- package/src/data-structures/binary-tree/binary-tree.ts +85 -92
- package/src/data-structures/binary-tree/bst.ts +14 -22
- package/src/data-structures/binary-tree/rb-tree.ts +11 -20
- package/src/data-structures/binary-tree/tree-multimap.ts +56 -58
- package/src/data-structures/graph/abstract-graph.ts +6 -22
- package/src/data-structures/graph/directed-graph.ts +3 -9
- package/src/data-structures/graph/map-graph.ts +6 -6
- package/src/data-structures/graph/undirected-graph.ts +1 -2
- package/src/data-structures/heap/heap.ts +1 -6
- package/src/data-structures/trie/trie.ts +1 -1
- package/src/interfaces/binary-tree.ts +1 -1
- package/src/types/utils/validate-type.ts +2 -16
|
@@ -10,10 +10,7 @@ import {BiTreeDeleteResult, BTNCallback, CP, FamilyPosition, IterationType} from
|
|
|
10
10
|
import {IBinaryTree} from '../../interfaces';
|
|
11
11
|
import {AVLTree, AVLTreeNode} from './avl-tree';
|
|
12
12
|
|
|
13
|
-
export class TreeMultimapNode<
|
|
14
|
-
V = any,
|
|
15
|
-
N extends TreeMultimapNode<V, N> = TreeMultimapNodeNested<V>
|
|
16
|
-
> extends AVLTreeNode<V, N> {
|
|
13
|
+
export class TreeMultimapNode<V = any, N extends TreeMultimapNode<V, N> = TreeMultimapNodeNested<V>> extends AVLTreeNode<V, N> {
|
|
17
14
|
count: number;
|
|
18
15
|
|
|
19
16
|
/**
|
|
@@ -160,52 +157,6 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
160
157
|
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
161
158
|
*/
|
|
162
159
|
|
|
163
|
-
/**
|
|
164
|
-
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
165
|
-
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
166
|
-
*
|
|
167
|
-
* The function adds a new node to a binary tree, either as the left child or the right child of a
|
|
168
|
-
* given parent node.
|
|
169
|
-
* @param {N | undefined} newNode - The `newNode` parameter represents the node that needs to be
|
|
170
|
-
* added to the binary tree. It can be of type `N` (which represents a node in the binary tree) or
|
|
171
|
-
* `undefined` if there is no node to add.
|
|
172
|
-
* @param {BTNKey | N | undefined} parent - The `parent` parameter represents the parent node to
|
|
173
|
-
* which the new node will be added as a child. It can be either a node object (`N`) or a key value
|
|
174
|
-
* (`BTNKey`).
|
|
175
|
-
* @returns The method `_addTo` returns either the `parent.left` or `parent.right` node that was
|
|
176
|
-
* added, or `undefined` if no node was added.
|
|
177
|
-
*/
|
|
178
|
-
protected override _addTo(newNode: N | undefined, parent: BTNKey | N | undefined): N | undefined {
|
|
179
|
-
parent = this.ensureNotKey(parent);
|
|
180
|
-
if (parent) {
|
|
181
|
-
if (parent.left === undefined) {
|
|
182
|
-
parent.left = newNode;
|
|
183
|
-
if (newNode !== undefined) {
|
|
184
|
-
this._size = this.size + 1;
|
|
185
|
-
this._count += newNode.count;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
return parent.left;
|
|
189
|
-
} else if (parent.right === undefined) {
|
|
190
|
-
parent.right = newNode;
|
|
191
|
-
if (newNode !== undefined) {
|
|
192
|
-
this._size = this.size + 1;
|
|
193
|
-
this._count += newNode.count;
|
|
194
|
-
}
|
|
195
|
-
return parent.right;
|
|
196
|
-
} else {
|
|
197
|
-
return;
|
|
198
|
-
}
|
|
199
|
-
} else {
|
|
200
|
-
return;
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* Time Complexity: O(k log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each.
|
|
206
|
-
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
207
|
-
*/
|
|
208
|
-
|
|
209
160
|
/**
|
|
210
161
|
* Time Complexity: O(k log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each.
|
|
211
162
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -241,8 +192,8 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
241
192
|
}
|
|
242
193
|
|
|
243
194
|
/**
|
|
244
|
-
* Time Complexity: O(
|
|
245
|
-
* Space Complexity: O(
|
|
195
|
+
* Time Complexity: O(k log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each.
|
|
196
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
246
197
|
*/
|
|
247
198
|
|
|
248
199
|
/**
|
|
@@ -295,8 +246,8 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
295
246
|
}
|
|
296
247
|
|
|
297
248
|
/**
|
|
298
|
-
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree.
|
|
299
|
-
* Space Complexity: O(
|
|
249
|
+
* Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
|
|
250
|
+
* Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
|
|
300
251
|
*/
|
|
301
252
|
|
|
302
253
|
/**
|
|
@@ -330,7 +281,8 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
330
281
|
if (!curr) return deletedResult;
|
|
331
282
|
|
|
332
283
|
const parent: N | undefined = curr?.parent ? curr.parent : undefined;
|
|
333
|
-
let needBalanced: N | undefined = undefined,
|
|
284
|
+
let needBalanced: N | undefined = undefined,
|
|
285
|
+
orgCurrent: N | undefined = curr;
|
|
334
286
|
|
|
335
287
|
if (curr.count > 1 && !ignoreCount) {
|
|
336
288
|
curr.count--;
|
|
@@ -377,6 +329,11 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
377
329
|
return deletedResult;
|
|
378
330
|
}
|
|
379
331
|
|
|
332
|
+
/**
|
|
333
|
+
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (AVLTree) has logarithmic time complexity.
|
|
334
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
335
|
+
*/
|
|
336
|
+
|
|
380
337
|
/**
|
|
381
338
|
* The clear() function clears the contents of a data structure and sets the count to zero.
|
|
382
339
|
*/
|
|
@@ -385,6 +342,47 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
385
342
|
this._count = 0;
|
|
386
343
|
}
|
|
387
344
|
|
|
345
|
+
/**
|
|
346
|
+
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
347
|
+
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
348
|
+
*
|
|
349
|
+
* The function adds a new node to a binary tree, either as the left child or the right child of a
|
|
350
|
+
* given parent node.
|
|
351
|
+
* @param {N | undefined} newNode - The `newNode` parameter represents the node that needs to be
|
|
352
|
+
* added to the binary tree. It can be of type `N` (which represents a node in the binary tree) or
|
|
353
|
+
* `undefined` if there is no node to add.
|
|
354
|
+
* @param {BTNKey | N | undefined} parent - The `parent` parameter represents the parent node to
|
|
355
|
+
* which the new node will be added as a child. It can be either a node object (`N`) or a key value
|
|
356
|
+
* (`BTNKey`).
|
|
357
|
+
* @returns The method `_addTo` returns either the `parent.left` or `parent.right` node that was
|
|
358
|
+
* added, or `undefined` if no node was added.
|
|
359
|
+
*/
|
|
360
|
+
protected override _addTo(newNode: N | undefined, parent: BTNKey | N | undefined): N | undefined {
|
|
361
|
+
parent = this.ensureNotKey(parent);
|
|
362
|
+
if (parent) {
|
|
363
|
+
if (parent.left === undefined) {
|
|
364
|
+
parent.left = newNode;
|
|
365
|
+
if (newNode !== undefined) {
|
|
366
|
+
this._size = this.size + 1;
|
|
367
|
+
this._count += newNode.count;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
return parent.left;
|
|
371
|
+
} else if (parent.right === undefined) {
|
|
372
|
+
parent.right = newNode;
|
|
373
|
+
if (newNode !== undefined) {
|
|
374
|
+
this._size = this.size + 1;
|
|
375
|
+
this._count += newNode.count;
|
|
376
|
+
}
|
|
377
|
+
return parent.right;
|
|
378
|
+
} else {
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
} else {
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
388
386
|
/**
|
|
389
387
|
* The `_swap` function swaps the key, value, count, and height properties between two nodes.
|
|
390
388
|
* @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node from
|
|
@@ -394,7 +392,7 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
394
392
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
395
393
|
* if either `srcNode` or `destNode` is undefined.
|
|
396
394
|
*/
|
|
397
|
-
protected _swap(srcNode: BTNKey | N | undefined, destNode:BTNKey | N | undefined): N | undefined{
|
|
395
|
+
protected _swap(srcNode: BTNKey | N | undefined, destNode: BTNKey | N | undefined): N | undefined {
|
|
398
396
|
srcNode = this.ensureNotKey(srcNode);
|
|
399
397
|
destNode = this.ensureNotKey(destNode);
|
|
400
398
|
if (srcNode && destNode) {
|
|
@@ -416,6 +414,6 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
416
414
|
|
|
417
415
|
return destNode;
|
|
418
416
|
}
|
|
419
|
-
return
|
|
417
|
+
return undefined;
|
|
420
418
|
}
|
|
421
|
-
}
|
|
419
|
+
}
|
|
@@ -64,8 +64,7 @@ export abstract class AbstractGraph<
|
|
|
64
64
|
E = any,
|
|
65
65
|
VO extends AbstractVertex<V> = AbstractVertex<V>,
|
|
66
66
|
EO extends AbstractEdge<E> = AbstractEdge<E>
|
|
67
|
-
> implements IGraph<V, E, VO, EO>
|
|
68
|
-
{
|
|
67
|
+
> implements IGraph<V, E, VO, EO> {
|
|
69
68
|
protected _vertices: Map<VertexKey, VO> = new Map<VertexKey, VO>();
|
|
70
69
|
|
|
71
70
|
get vertices(): Map<VertexKey, VO> {
|
|
@@ -496,7 +495,7 @@ export abstract class AbstractGraph<
|
|
|
496
495
|
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
497
496
|
*/
|
|
498
497
|
|
|
499
|
-
|
|
498
|
+
/**
|
|
500
499
|
* Time Complexity: O(V^2 + E) - Quadratic time in the worst case (no heap optimization).
|
|
501
500
|
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
502
501
|
*
|
|
@@ -515,12 +514,7 @@ export abstract class AbstractGraph<
|
|
|
515
514
|
* shortest paths from the source vertex to all other vertices in the graph. If `genPaths
|
|
516
515
|
* @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<VO>`.
|
|
517
516
|
*/
|
|
518
|
-
dijkstraWithoutHeap(
|
|
519
|
-
src: VO | VertexKey,
|
|
520
|
-
dest?: VO | VertexKey | null,
|
|
521
|
-
getMinDist?: boolean,
|
|
522
|
-
genPaths?: boolean
|
|
523
|
-
): DijkstraResult<VO> {
|
|
517
|
+
dijkstraWithoutHeap(src: VO | VertexKey, dest?: VO | VertexKey | null, getMinDist?: boolean, genPaths?: boolean): DijkstraResult<VO> {
|
|
524
518
|
if (getMinDist === undefined) getMinDist = false;
|
|
525
519
|
if (genPaths === undefined) genPaths = false;
|
|
526
520
|
|
|
@@ -643,7 +637,7 @@ export abstract class AbstractGraph<
|
|
|
643
637
|
* Space Complexity: O(V + E) - Depends on the implementation (using a binary heap).
|
|
644
638
|
*/
|
|
645
639
|
|
|
646
|
-
|
|
640
|
+
/**
|
|
647
641
|
* Time Complexity: O((V + E) * log(V)) - Depends on the implementation (using a binary heap).
|
|
648
642
|
* Space Complexity: O(V + E) - Depends on the implementation (using a binary heap).
|
|
649
643
|
*
|
|
@@ -663,12 +657,7 @@ export abstract class AbstractGraph<
|
|
|
663
657
|
* shortest paths from the source vertex to all other vertices in the graph. If `genPaths
|
|
664
658
|
* @returns The function `dijkstra` returns an object of type `DijkstraResult<VO>`.
|
|
665
659
|
*/
|
|
666
|
-
dijkstra(
|
|
667
|
-
src: VO | VertexKey,
|
|
668
|
-
dest?: VO | VertexKey | null,
|
|
669
|
-
getMinDist?: boolean,
|
|
670
|
-
genPaths?: boolean
|
|
671
|
-
): DijkstraResult<VO> {
|
|
660
|
+
dijkstra(src: VO | VertexKey, dest?: VO | VertexKey | null, getMinDist?: boolean, genPaths?: boolean): DijkstraResult<VO> {
|
|
672
661
|
if (getMinDist === undefined) getMinDist = false;
|
|
673
662
|
if (genPaths === undefined) genPaths = false;
|
|
674
663
|
|
|
@@ -993,12 +982,7 @@ export abstract class AbstractGraph<
|
|
|
993
982
|
* are arrays of vertices that form cycles within the SCCs.
|
|
994
983
|
* @returns The function `tarjan` returns an object with the following properties:
|
|
995
984
|
*/
|
|
996
|
-
tarjan(
|
|
997
|
-
needCutVertexes: boolean = false,
|
|
998
|
-
needBridges: boolean = false,
|
|
999
|
-
needSCCs: boolean = true,
|
|
1000
|
-
needCycles: boolean = false
|
|
1001
|
-
) {
|
|
985
|
+
tarjan(needCutVertexes: boolean = false, needBridges: boolean = false, needSCCs: boolean = true, needCycles: boolean = false) {
|
|
1002
986
|
// !! in undirected graph we will not let child visit parent when dfs
|
|
1003
987
|
// !! articulation point(in dfs search tree not in graph): (cur !== root && cur.has(child)) && (low(child) >= dfn(cur)) || (cur === root && cur.children() >= 2)
|
|
1004
988
|
// !! bridge: low(child) > dfn(cur)
|
|
@@ -45,15 +45,9 @@ export class DirectedEdge<E = any> extends AbstractEdge<E> {
|
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
export class DirectedGraph<
|
|
49
|
-
V = any,
|
|
50
|
-
E = any,
|
|
51
|
-
VO extends DirectedVertex<V> = DirectedVertex<V>,
|
|
52
|
-
EO extends DirectedEdge<E> = DirectedEdge<E>
|
|
53
|
-
>
|
|
48
|
+
export class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V> = DirectedVertex<V>, EO extends DirectedEdge<E> = DirectedEdge<E>>
|
|
54
49
|
extends AbstractGraph<V, E, VO, EO>
|
|
55
|
-
implements IGraph<V, E, VO, EO>
|
|
56
|
-
{
|
|
50
|
+
implements IGraph<V, E, VO, EO> {
|
|
57
51
|
/**
|
|
58
52
|
* The constructor function initializes an instance of a class.
|
|
59
53
|
*/
|
|
@@ -591,4 +585,4 @@ export class DirectedGraph<
|
|
|
591
585
|
return false;
|
|
592
586
|
}
|
|
593
587
|
}
|
|
594
|
-
}
|
|
588
|
+
}
|
|
@@ -40,12 +40,12 @@ export class MapEdge<E = any> extends DirectedEdge<E> {
|
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
export class MapGraph<
|
|
44
|
-
V
|
|
45
|
-
E
|
|
46
|
-
VO
|
|
47
|
-
EO
|
|
48
|
-
>
|
|
43
|
+
export class MapGraph<V = any, E = any, VO extends MapVertex<V> = MapVertex<V>, EO extends MapEdge<E> = MapEdge<E>> extends DirectedGraph<
|
|
44
|
+
V,
|
|
45
|
+
E,
|
|
46
|
+
VO,
|
|
47
|
+
EO
|
|
48
|
+
> {
|
|
49
49
|
/**
|
|
50
50
|
* The constructor function initializes the origin and bottomRight properties of a MapGraphCoordinate object.
|
|
51
51
|
* @param {MapGraphCoordinate} origin - The `origin` parameter is a `MapGraphCoordinate` object that represents the
|
|
@@ -49,8 +49,7 @@ export class UndirectedGraph<
|
|
|
49
49
|
EO extends UndirectedEdge<E> = UndirectedEdge<E>
|
|
50
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 edges.
|
|
56
55
|
*/
|
|
@@ -445,7 +445,6 @@ export class FibonacciHeap<E> {
|
|
|
445
445
|
return this.push(element);
|
|
446
446
|
}
|
|
447
447
|
|
|
448
|
-
|
|
449
448
|
/**
|
|
450
449
|
* Time Complexity: O(1)
|
|
451
450
|
* Space Complexity: O(1)
|
|
@@ -473,7 +472,6 @@ export class FibonacciHeap<E> {
|
|
|
473
472
|
return this;
|
|
474
473
|
}
|
|
475
474
|
|
|
476
|
-
|
|
477
475
|
/**
|
|
478
476
|
* Time Complexity: O(1)
|
|
479
477
|
* Space Complexity: O(1)
|
|
@@ -743,10 +741,7 @@ export class FibonacciHeap<E> {
|
|
|
743
741
|
protected consolidate(): void {
|
|
744
742
|
const A: (FibonacciHeapNode<E> | undefined)[] = new Array(this.size);
|
|
745
743
|
const nodes = this.consumeLinkedList(this.root);
|
|
746
|
-
let x: FibonacciHeapNode<E> | undefined,
|
|
747
|
-
y: FibonacciHeapNode<E> | undefined,
|
|
748
|
-
d: number,
|
|
749
|
-
t: FibonacciHeapNode<E> | undefined;
|
|
744
|
+
let x: FibonacciHeapNode<E> | undefined, y: FibonacciHeapNode<E> | undefined, d: number, t: FibonacciHeapNode<E> | undefined;
|
|
750
745
|
|
|
751
746
|
for (const node of nodes) {
|
|
752
747
|
x = node;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {BinaryTreeNode} from '../data-structures';
|
|
2
|
-
import {
|
|
2
|
+
import {BinaryTreeNodeNested, BiTreeDeleteResult, BTNCallback, BTNKey} from '../types';
|
|
3
3
|
|
|
4
4
|
export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>> {
|
|
5
5
|
createNode(key: BTNKey, value?: N['value']): N;
|
|
@@ -16,20 +16,6 @@ export type ObjectWithNumberKey = {
|
|
|
16
16
|
key: number;
|
|
17
17
|
};
|
|
18
18
|
|
|
19
|
-
export type RestrictValByKey =
|
|
20
|
-
| NonNumberNonObjectButDefined
|
|
21
|
-
| ObjectWithoutKey
|
|
22
|
-
| ObjectWithNonNumberKey
|
|
23
|
-
| ObjectWithNumberKey;
|
|
19
|
+
export type RestrictValByKey = NonNumberNonObjectButDefined | ObjectWithoutKey | ObjectWithNonNumberKey | ObjectWithNumberKey;
|
|
24
20
|
|
|
25
|
-
export type DummyAny =
|
|
26
|
-
| string
|
|
27
|
-
| number
|
|
28
|
-
| boolean
|
|
29
|
-
| null
|
|
30
|
-
| undefined
|
|
31
|
-
| object
|
|
32
|
-
| symbol
|
|
33
|
-
| void
|
|
34
|
-
| ((...args: []) => any)
|
|
35
|
-
| never;
|
|
21
|
+
export type DummyAny = string | number | boolean | null | undefined | object | symbol | void | ((...args: []) => any) | never;
|