min-heap-typed 1.50.2 → 1.50.3
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 +6 -0
- package/dist/data-structures/binary-tree/avl-tree.d.ts +29 -1
- package/dist/data-structures/binary-tree/avl-tree.js +33 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +22 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/data-structures/binary-tree/binary-tree.js +1 -1
- package/dist/data-structures/binary-tree/bst.d.ts +46 -13
- package/dist/data-structures/binary-tree/bst.js +46 -13
- package/dist/data-structures/binary-tree/rb-tree.d.ts +54 -2
- package/dist/data-structures/binary-tree/rb-tree.js +73 -15
- package/dist/data-structures/binary-tree/segment-tree.d.ts +99 -6
- package/dist/data-structures/binary-tree/segment-tree.js +127 -10
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +35 -2
- package/dist/data-structures/binary-tree/tree-multimap.js +38 -0
- package/dist/data-structures/graph/abstract-graph.d.ts +0 -78
- package/dist/data-structures/graph/abstract-graph.js +0 -189
- package/dist/data-structures/graph/directed-graph.d.ts +59 -0
- package/dist/data-structures/graph/directed-graph.js +105 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +60 -7
- package/dist/data-structures/graph/undirected-graph.js +126 -18
- package/dist/data-structures/hash/hash-map.d.ts +143 -23
- package/dist/data-structures/hash/hash-map.js +196 -62
- package/dist/data-structures/heap/heap.d.ts +29 -19
- package/dist/data-structures/heap/heap.js +29 -20
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +71 -25
- package/dist/data-structures/linked-list/doubly-linked-list.js +83 -25
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +26 -3
- package/dist/data-structures/linked-list/singly-linked-list.js +34 -3
- 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/matrix/matrix.d.ts +1 -1
- package/dist/data-structures/matrix/matrix.js +1 -1
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +10 -0
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +11 -0
- package/dist/data-structures/priority-queue/priority-queue.d.ts +8 -0
- package/dist/data-structures/priority-queue/priority-queue.js +8 -0
- package/dist/data-structures/queue/deque.d.ts +95 -21
- package/dist/data-structures/queue/deque.js +100 -16
- package/dist/data-structures/queue/queue.d.ts +65 -45
- package/dist/data-structures/queue/queue.js +65 -45
- package/dist/data-structures/stack/stack.d.ts +36 -22
- package/dist/data-structures/stack/stack.js +36 -22
- package/dist/data-structures/tree/tree.d.ts +57 -3
- package/dist/data-structures/tree/tree.js +77 -11
- package/dist/data-structures/trie/trie.d.ts +100 -36
- package/dist/data-structures/trie/trie.js +115 -36
- package/package.json +2 -2
- package/src/data-structures/base/iterable-base.ts +12 -0
- package/src/data-structures/binary-tree/avl-tree.ts +37 -3
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
- package/src/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/data-structures/binary-tree/bst.ts +46 -13
- package/src/data-structures/binary-tree/rb-tree.ts +79 -18
- package/src/data-structures/binary-tree/segment-tree.ts +145 -11
- package/src/data-structures/binary-tree/tree-multimap.ts +42 -3
- package/src/data-structures/graph/abstract-graph.ts +0 -211
- package/src/data-structures/graph/directed-graph.ts +122 -0
- package/src/data-structures/graph/undirected-graph.ts +143 -19
- package/src/data-structures/hash/hash-map.ts +228 -76
- package/src/data-structures/heap/heap.ts +31 -20
- package/src/data-structures/linked-list/doubly-linked-list.ts +96 -29
- package/src/data-structures/linked-list/singly-linked-list.ts +42 -6
- package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
- package/src/data-structures/matrix/matrix.ts +1 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +10 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -0
- package/src/data-structures/priority-queue/priority-queue.ts +8 -0
- package/src/data-structures/queue/deque.ts +118 -22
- package/src/data-structures/queue/queue.ts +68 -45
- package/src/data-structures/stack/stack.ts +39 -23
- package/src/data-structures/tree/tree.ts +89 -15
- package/src/data-structures/trie/trie.ts +131 -40
|
@@ -24,11 +24,38 @@ export class RedBlackTreeNode<
|
|
|
24
24
|
V = any,
|
|
25
25
|
NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNodeNested<K, V>
|
|
26
26
|
> extends BSTNode<K, V, NODE> {
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
/**
|
|
28
|
+
* The constructor function initializes a Red-Black Tree Node with a key, an optional value, and a
|
|
29
|
+
* color.
|
|
30
|
+
* @param {K} key - The key parameter is of type K and represents the key of the node in the
|
|
31
|
+
* Red-Black Tree.
|
|
32
|
+
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
33
|
+
* associated with the key in the Red-Black Tree Node. It is not required and can be omitted when
|
|
34
|
+
* creating a new instance of the Red-Black Tree Node.
|
|
35
|
+
* @param {RBTNColor} color - The `color` parameter is used to specify the color of the Red-Black
|
|
36
|
+
* Tree Node. It is an optional parameter with a default value of `RBTNColor.BLACK`.
|
|
37
|
+
*/
|
|
29
38
|
constructor(key: K, value?: V, color: RBTNColor = RBTNColor.BLACK) {
|
|
30
39
|
super(key, value);
|
|
31
|
-
this.
|
|
40
|
+
this._color = color;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
protected _color: RBTNColor;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The function returns the color value of a variable.
|
|
47
|
+
* @returns The color value stored in the protected variable `_color`.
|
|
48
|
+
*/
|
|
49
|
+
get color(): RBTNColor {
|
|
50
|
+
return this._color;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The function sets the color property to the specified value.
|
|
55
|
+
* @param {RBTNColor} value - The value parameter is of type RBTNColor.
|
|
56
|
+
*/
|
|
57
|
+
set color(value: RBTNColor) {
|
|
58
|
+
this._color = value;
|
|
32
59
|
}
|
|
33
60
|
}
|
|
34
61
|
|
|
@@ -47,8 +74,6 @@ export class RedBlackTree<
|
|
|
47
74
|
>
|
|
48
75
|
extends BST<K, V, NODE, TREE>
|
|
49
76
|
implements IBinaryTree<K, V, NODE, TREE> {
|
|
50
|
-
Sentinel: NODE = new RedBlackTreeNode<K, V>(NaN as K) as unknown as NODE;
|
|
51
|
-
|
|
52
77
|
/**
|
|
53
78
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
|
54
79
|
* initializes the tree with optional nodes and options.
|
|
@@ -63,18 +88,36 @@ export class RedBlackTree<
|
|
|
63
88
|
constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: RBTreeOptions<K>) {
|
|
64
89
|
super([], options);
|
|
65
90
|
|
|
66
|
-
this._root = this.
|
|
91
|
+
this._root = this._Sentinel;
|
|
67
92
|
if (keysOrNodesOrEntries) super.addMany(keysOrNodesOrEntries);
|
|
68
93
|
}
|
|
69
94
|
|
|
95
|
+
protected _Sentinel: NODE = new RedBlackTreeNode<K, V>(NaN as K) as unknown as NODE;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* The function returns the value of the `_Sentinel` property.
|
|
99
|
+
* @returns The method is returning the value of the `_Sentinel` property.
|
|
100
|
+
*/
|
|
101
|
+
get Sentinel(): NODE {
|
|
102
|
+
return this._Sentinel;
|
|
103
|
+
}
|
|
104
|
+
|
|
70
105
|
protected _root: NODE;
|
|
71
106
|
|
|
107
|
+
/**
|
|
108
|
+
* The function returns the root node.
|
|
109
|
+
* @returns The root node of the data structure.
|
|
110
|
+
*/
|
|
72
111
|
get root(): NODE {
|
|
73
112
|
return this._root;
|
|
74
113
|
}
|
|
75
114
|
|
|
76
115
|
protected _size: number = 0;
|
|
77
116
|
|
|
117
|
+
/**
|
|
118
|
+
* The function returns the size of an object.
|
|
119
|
+
* @returns The size of the object, which is a number.
|
|
120
|
+
*/
|
|
78
121
|
get size(): number {
|
|
79
122
|
return this._size;
|
|
80
123
|
}
|
|
@@ -149,8 +192,14 @@ export class RedBlackTree<
|
|
|
149
192
|
return keyOrNodeOrEntry instanceof RedBlackTreeNode;
|
|
150
193
|
}
|
|
151
194
|
|
|
195
|
+
/**
|
|
196
|
+
* The function checks if a given node is a real node in a Red-Black Tree.
|
|
197
|
+
* @param {NODE | undefined} node - The `node` parameter is of type `NODE | undefined`, which means
|
|
198
|
+
* it can either be of type `NODE` or `undefined`.
|
|
199
|
+
* @returns a boolean value.
|
|
200
|
+
*/
|
|
152
201
|
override isRealNode(node: NODE | undefined): node is NODE {
|
|
153
|
-
if (node === this.
|
|
202
|
+
if (node === this._Sentinel || node === undefined) return false;
|
|
154
203
|
return node instanceof RedBlackTreeNode;
|
|
155
204
|
}
|
|
156
205
|
|
|
@@ -176,13 +225,13 @@ export class RedBlackTree<
|
|
|
176
225
|
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
|
|
177
226
|
if (newNode === undefined) return false;
|
|
178
227
|
|
|
179
|
-
newNode.left = this.
|
|
180
|
-
newNode.right = this.
|
|
228
|
+
newNode.left = this._Sentinel;
|
|
229
|
+
newNode.right = this._Sentinel;
|
|
181
230
|
|
|
182
231
|
let y: NODE | undefined = undefined;
|
|
183
232
|
let x: NODE | undefined = this.root;
|
|
184
233
|
|
|
185
|
-
while (x !== this.
|
|
234
|
+
while (x !== this._Sentinel) {
|
|
186
235
|
y = x;
|
|
187
236
|
if (x) {
|
|
188
237
|
if (newNode.key < x.key) {
|
|
@@ -250,9 +299,9 @@ export class RedBlackTree<
|
|
|
250
299
|
const ans: BinaryTreeDeleteResult<NODE>[] = [];
|
|
251
300
|
if (identifier === null) return ans;
|
|
252
301
|
const helper = (node: NODE | undefined): void => {
|
|
253
|
-
let z: NODE = this.
|
|
302
|
+
let z: NODE = this._Sentinel;
|
|
254
303
|
let x: NODE | undefined, y: NODE;
|
|
255
|
-
while (node !== this.
|
|
304
|
+
while (node !== this._Sentinel) {
|
|
256
305
|
if (node && callback(node) === identifier) {
|
|
257
306
|
z = node;
|
|
258
307
|
}
|
|
@@ -264,17 +313,17 @@ export class RedBlackTree<
|
|
|
264
313
|
}
|
|
265
314
|
}
|
|
266
315
|
|
|
267
|
-
if (z === this.
|
|
316
|
+
if (z === this._Sentinel) {
|
|
268
317
|
this._size--;
|
|
269
318
|
return;
|
|
270
319
|
}
|
|
271
320
|
|
|
272
321
|
y = z;
|
|
273
322
|
let yOriginalColor: number = y.color;
|
|
274
|
-
if (z.left === this.
|
|
323
|
+
if (z.left === this._Sentinel) {
|
|
275
324
|
x = z.right;
|
|
276
325
|
this._rbTransplant(z, z.right!);
|
|
277
|
-
} else if (z.right === this.
|
|
326
|
+
} else if (z.right === this._Sentinel) {
|
|
278
327
|
x = z.left;
|
|
279
328
|
this._rbTransplant(z, z.left!);
|
|
280
329
|
} else {
|
|
@@ -346,8 +395,14 @@ export class RedBlackTree<
|
|
|
346
395
|
* Space Complexity: O(1)
|
|
347
396
|
*/
|
|
348
397
|
|
|
398
|
+
/**
|
|
399
|
+
* Time Complexity: O(1)
|
|
400
|
+
* Space Complexity: O(1)
|
|
401
|
+
*
|
|
402
|
+
* The "clear" function sets the root node to the sentinel node and resets the size to 0.
|
|
403
|
+
*/
|
|
349
404
|
override clear() {
|
|
350
|
-
this._root = this.
|
|
405
|
+
this._root = this._Sentinel;
|
|
351
406
|
this._size = 0;
|
|
352
407
|
}
|
|
353
408
|
|
|
@@ -379,6 +434,12 @@ export class RedBlackTree<
|
|
|
379
434
|
return y!;
|
|
380
435
|
}
|
|
381
436
|
|
|
437
|
+
/**
|
|
438
|
+
* The function sets the root node of a tree structure and updates the parent property of the new
|
|
439
|
+
* root node.
|
|
440
|
+
* @param {NODE} v - The parameter "v" is of type "NODE", which represents a node in a data
|
|
441
|
+
* structure.
|
|
442
|
+
*/
|
|
382
443
|
protected override _setRoot(v: NODE) {
|
|
383
444
|
if (v) {
|
|
384
445
|
v.parent = undefined;
|
|
@@ -402,7 +463,7 @@ export class RedBlackTree<
|
|
|
402
463
|
if (x.right) {
|
|
403
464
|
const y: NODE = x.right;
|
|
404
465
|
x.right = y.left;
|
|
405
|
-
if (y.left !== this.
|
|
466
|
+
if (y.left !== this._Sentinel) {
|
|
406
467
|
if (y.left) y.left.parent = x;
|
|
407
468
|
}
|
|
408
469
|
y.parent = x.parent;
|
|
@@ -435,7 +496,7 @@ export class RedBlackTree<
|
|
|
435
496
|
if (x.left) {
|
|
436
497
|
const y: NODE = x.left;
|
|
437
498
|
x.left = y.right;
|
|
438
|
-
if (y.right !== this.
|
|
499
|
+
if (y.right !== this._Sentinel) {
|
|
439
500
|
if (y.right) y.right.parent = x;
|
|
440
501
|
}
|
|
441
502
|
y.parent = x.parent;
|
|
@@ -9,18 +9,136 @@
|
|
|
9
9
|
import type { SegmentTreeNodeVal } from '../../types';
|
|
10
10
|
|
|
11
11
|
export class SegmentTreeNode {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
12
|
+
/**
|
|
13
|
+
* The constructor initializes the properties of a SegmentTreeNode object.
|
|
14
|
+
* @param {number} start - The `start` parameter represents the starting index of the segment covered
|
|
15
|
+
* by this node in a segment tree.
|
|
16
|
+
* @param {number} end - The `end` parameter represents the end index of the segment covered by this
|
|
17
|
+
* node in a segment tree.
|
|
18
|
+
* @param {number} sum - The `sum` parameter represents the sum of the values in the range covered by
|
|
19
|
+
* the segment tree node.
|
|
20
|
+
* @param {SegmentTreeNodeVal | undefined} [value] - The `value` parameter is an optional parameter
|
|
21
|
+
* of type `SegmentTreeNodeVal`. It represents the value associated with the segment tree node.
|
|
22
|
+
*/
|
|
19
23
|
constructor(start: number, end: number, sum: number, value?: SegmentTreeNodeVal | undefined) {
|
|
20
|
-
this.
|
|
21
|
-
this.
|
|
22
|
-
this.
|
|
23
|
-
this.
|
|
24
|
+
this._start = start;
|
|
25
|
+
this._end = end;
|
|
26
|
+
this._sum = sum;
|
|
27
|
+
this._value = value || undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
protected _start = 0;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The function returns the value of the protected variable _start.
|
|
34
|
+
* @returns The start value, which is of type number.
|
|
35
|
+
*/
|
|
36
|
+
get start(): number {
|
|
37
|
+
return this._start;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The above function sets the value of the "start" property.
|
|
42
|
+
* @param {number} value - The value parameter is of type number.
|
|
43
|
+
*/
|
|
44
|
+
set start(value: number) {
|
|
45
|
+
this._start = value;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
protected _end = 0;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The function returns the value of the protected variable `_end`.
|
|
52
|
+
* @returns The value of the protected property `_end`.
|
|
53
|
+
*/
|
|
54
|
+
get end(): number {
|
|
55
|
+
return this._end;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* The above function sets the value of the "end" property.
|
|
60
|
+
* @param {number} value - The value parameter is a number that represents the new value for the end
|
|
61
|
+
* property.
|
|
62
|
+
*/
|
|
63
|
+
set end(value: number) {
|
|
64
|
+
this._end = value;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
protected _value: SegmentTreeNodeVal | undefined = undefined;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The function returns the value of a segment tree node.
|
|
71
|
+
* @returns The value being returned is either a `SegmentTreeNodeVal` object or `undefined`.
|
|
72
|
+
*/
|
|
73
|
+
get value(): SegmentTreeNodeVal | undefined {
|
|
74
|
+
return this._value;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The function sets the value of a segment tree node.
|
|
79
|
+
* @param {SegmentTreeNodeVal | undefined} value - The `value` parameter is of type
|
|
80
|
+
* `SegmentTreeNodeVal` or `undefined`.
|
|
81
|
+
*/
|
|
82
|
+
set value(value: SegmentTreeNodeVal | undefined) {
|
|
83
|
+
this._value = value;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
protected _sum = 0;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The function returns the value of the sum property.
|
|
90
|
+
* @returns The method is returning the value of the variable `_sum`.
|
|
91
|
+
*/
|
|
92
|
+
get sum(): number {
|
|
93
|
+
return this._sum;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* The above function sets the value of the sum property.
|
|
98
|
+
* @param {number} value - The parameter "value" is of type "number".
|
|
99
|
+
*/
|
|
100
|
+
set sum(value: number) {
|
|
101
|
+
this._sum = value;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
protected _left: SegmentTreeNode | undefined = undefined;
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* The function returns the left child of a segment tree node.
|
|
108
|
+
* @returns The `left` property of the `SegmentTreeNode` object is being returned. It is of type
|
|
109
|
+
* `SegmentTreeNode` or `undefined`.
|
|
110
|
+
*/
|
|
111
|
+
get left(): SegmentTreeNode | undefined {
|
|
112
|
+
return this._left;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* The function sets the value of the left property of a SegmentTreeNode object.
|
|
117
|
+
* @param {SegmentTreeNode | undefined} value - The value parameter is of type SegmentTreeNode or
|
|
118
|
+
* undefined.
|
|
119
|
+
*/
|
|
120
|
+
set left(value: SegmentTreeNode | undefined) {
|
|
121
|
+
this._left = value;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
protected _right: SegmentTreeNode | undefined = undefined;
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* The function returns the right child of a segment tree node.
|
|
128
|
+
* @returns The `getRight()` method is returning a value of type `SegmentTreeNode` or `undefined`.
|
|
129
|
+
*/
|
|
130
|
+
get right(): SegmentTreeNode | undefined {
|
|
131
|
+
return this._right;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* The function sets the right child of a segment tree node.
|
|
136
|
+
* @param {SegmentTreeNode | undefined} value - The `value` parameter is of type `SegmentTreeNode |
|
|
137
|
+
* undefined`. This means that it can accept either a `SegmentTreeNode` object or `undefined` as its
|
|
138
|
+
* value.
|
|
139
|
+
*/
|
|
140
|
+
set right(value: SegmentTreeNode | undefined) {
|
|
141
|
+
this._right = value;
|
|
24
142
|
}
|
|
25
143
|
}
|
|
26
144
|
|
|
@@ -51,24 +169,40 @@ export class SegmentTree {
|
|
|
51
169
|
|
|
52
170
|
protected _values: number[] = [];
|
|
53
171
|
|
|
172
|
+
/**
|
|
173
|
+
* The function returns an array of numbers.
|
|
174
|
+
* @returns An array of numbers is being returned.
|
|
175
|
+
*/
|
|
54
176
|
get values(): number[] {
|
|
55
177
|
return this._values;
|
|
56
178
|
}
|
|
57
179
|
|
|
58
180
|
protected _start = 0;
|
|
59
181
|
|
|
182
|
+
/**
|
|
183
|
+
* The function returns the value of the protected variable _start.
|
|
184
|
+
* @returns The start value, which is of type number.
|
|
185
|
+
*/
|
|
60
186
|
get start(): number {
|
|
61
187
|
return this._start;
|
|
62
188
|
}
|
|
63
189
|
|
|
64
190
|
protected _end: number;
|
|
65
191
|
|
|
192
|
+
/**
|
|
193
|
+
* The function returns the value of the protected variable `_end`.
|
|
194
|
+
* @returns The value of the protected property `_end`.
|
|
195
|
+
*/
|
|
66
196
|
get end(): number {
|
|
67
197
|
return this._end;
|
|
68
198
|
}
|
|
69
199
|
|
|
70
200
|
protected _root: SegmentTreeNode | undefined;
|
|
71
201
|
|
|
202
|
+
/**
|
|
203
|
+
* The function returns the root node of a segment tree.
|
|
204
|
+
* @returns The `root` property of the class `SegmentTreeNode` or `undefined` if it is not defined.
|
|
205
|
+
*/
|
|
72
206
|
get root(): SegmentTreeNode | undefined {
|
|
73
207
|
return this._root;
|
|
74
208
|
}
|
|
@@ -23,8 +23,6 @@ export class TreeMultimapNode<
|
|
|
23
23
|
V = any,
|
|
24
24
|
NODE extends TreeMultimapNode<K, V, NODE> = TreeMultimapNodeNested<K, V>
|
|
25
25
|
> extends AVLTreeNode<K, V, NODE> {
|
|
26
|
-
count: number;
|
|
27
|
-
|
|
28
26
|
/**
|
|
29
27
|
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
30
28
|
* @param {K} key - The `key` parameter is of type `K` and represents the unique identifier
|
|
@@ -39,6 +37,25 @@ export class TreeMultimapNode<
|
|
|
39
37
|
super(key, value);
|
|
40
38
|
this.count = count;
|
|
41
39
|
}
|
|
40
|
+
|
|
41
|
+
protected _count: number = 1;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The function returns the value of the protected variable _count.
|
|
45
|
+
* @returns The count property of the object, which is of type number.
|
|
46
|
+
*/
|
|
47
|
+
get count(): number {
|
|
48
|
+
return this._count;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The above function sets the value of the count property.
|
|
53
|
+
* @param {number} value - The value parameter is of type number, which means it can accept any
|
|
54
|
+
* numeric value.
|
|
55
|
+
*/
|
|
56
|
+
set count(value: number) {
|
|
57
|
+
this._count = value;
|
|
58
|
+
}
|
|
42
59
|
}
|
|
43
60
|
|
|
44
61
|
/**
|
|
@@ -57,9 +74,14 @@ export class TreeMultimap<
|
|
|
57
74
|
if (keysOrNodesOrEntries) this.addMany(keysOrNodesOrEntries);
|
|
58
75
|
}
|
|
59
76
|
|
|
60
|
-
|
|
77
|
+
protected _count = 0;
|
|
61
78
|
|
|
62
79
|
// TODO the _count is not accurate after nodes count modified
|
|
80
|
+
/**
|
|
81
|
+
* The function calculates the sum of the count property of all nodes in a tree using depth-first
|
|
82
|
+
* search.
|
|
83
|
+
* @returns the sum of the count property of all nodes in the tree.
|
|
84
|
+
*/
|
|
63
85
|
get count(): number {
|
|
64
86
|
let sum = 0;
|
|
65
87
|
this.dfs(node => (sum += node.count));
|
|
@@ -79,6 +101,15 @@ export class TreeMultimap<
|
|
|
79
101
|
return new TreeMultimapNode(key, value, count) as NODE;
|
|
80
102
|
}
|
|
81
103
|
|
|
104
|
+
/**
|
|
105
|
+
* The function creates a new TreeMultimap object with the specified options and returns it.
|
|
106
|
+
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
107
|
+
* configuration options for creating the `TreeMultimap` object. It can include properties such as
|
|
108
|
+
* `iterationType` and `variant`, which are used to specify the type of iteration and the variant of
|
|
109
|
+
* the tree, respectively. These properties can be
|
|
110
|
+
* @returns a new instance of the `TreeMultimap` class, with the provided options merged with the
|
|
111
|
+
* default options. The returned value is casted as `TREE`.
|
|
112
|
+
*/
|
|
82
113
|
override createTree(options?: TreeMultimapOptions<K>): TREE {
|
|
83
114
|
return new TreeMultimap<K, V, NODE, TREE>([], {
|
|
84
115
|
iterationType: this.iterationType,
|
|
@@ -375,6 +406,14 @@ export class TreeMultimap<
|
|
|
375
406
|
return undefined;
|
|
376
407
|
}
|
|
377
408
|
|
|
409
|
+
/**
|
|
410
|
+
* The function replaces an old node with a new node and updates the count property of the new node.
|
|
411
|
+
* @param {NODE} oldNode - The `oldNode` parameter is of type `NODE` and represents the node that
|
|
412
|
+
* needs to be replaced in a data structure.
|
|
413
|
+
* @param {NODE} newNode - The `newNode` parameter is an object of type `NODE`.
|
|
414
|
+
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
415
|
+
* superclass, after updating the `count` property of the `newNode` object.
|
|
416
|
+
*/
|
|
378
417
|
protected _replaceNode(oldNode: NODE, newNode: NODE): NODE {
|
|
379
418
|
newNode.count = oldNode.count + newNode.count;
|
|
380
419
|
return super._replaceNode(oldNode, newNode);
|
|
@@ -954,217 +954,6 @@ export abstract class AbstractGraph<
|
|
|
954
954
|
return { costs, predecessor };
|
|
955
955
|
}
|
|
956
956
|
|
|
957
|
-
/**
|
|
958
|
-
* Time Complexity: O(V + E) - Linear time (Tarjan's algorithm).
|
|
959
|
-
* Space Complexity: O(V) - Linear space (Tarjan's algorithm).
|
|
960
|
-
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
961
|
-
* Tarjan can find cycles in directed or undirected graph
|
|
962
|
-
* Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time,
|
|
963
|
-
* Tarjan solve the bi-connected components of undirected graphs;
|
|
964
|
-
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
965
|
-
* /
|
|
966
|
-
|
|
967
|
-
/**
|
|
968
|
-
* Time Complexity: O(V + E) - Linear time (Tarjan's algorithm).
|
|
969
|
-
* Space Complexity: O(V) - Linear space (Tarjan's algorithm).
|
|
970
|
-
*
|
|
971
|
-
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
972
|
-
* Tarjan can find cycles in directed or undirected graph
|
|
973
|
-
* Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time,
|
|
974
|
-
* Tarjan solve the bi-connected components of undirected graphs;
|
|
975
|
-
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
976
|
-
* The `tarjan` function is used to perform various graph analysis tasks such as finding articulation points, bridges,
|
|
977
|
-
* strongly connected components (SCCs), and cycles in a graph.
|
|
978
|
-
* @param {boolean} [needCutVertexes] - A boolean value indicating whether or not to calculate and return the
|
|
979
|
-
* articulation points in the graph. Articulation points are the vertexMap in a graph whose removal would increase the
|
|
980
|
-
* number of connected components in the graph.
|
|
981
|
-
* @param {boolean} [needBridges] - A boolean flag indicating whether the algorithm should find and return the bridges
|
|
982
|
-
* (edgeMap whose removal would increase the number of connected components in the graph).
|
|
983
|
-
* @param {boolean} [needSCCs] - A boolean value indicating whether the Strongly Connected Components (SCCs) of the
|
|
984
|
-
* graph are needed. If set to true, the function will calculate and return the SCCs of the graph. If set to false, the
|
|
985
|
-
* SCCs will not be calculated or returned.
|
|
986
|
-
* @param {boolean} [needCycles] - A boolean flag indicating whether the algorithm should find cycles in the graph. If
|
|
987
|
-
* set to true, the algorithm will return a map of cycles, where the keys are the low values of the SCCs and the values
|
|
988
|
-
* are arrays of vertexMap that form cycles within the SCCs.
|
|
989
|
-
* @returns The function `tarjan` returns an object with the following properties:
|
|
990
|
-
*/
|
|
991
|
-
tarjan(
|
|
992
|
-
needCutVertexes: boolean = false,
|
|
993
|
-
needBridges: boolean = false,
|
|
994
|
-
needSCCs: boolean = true,
|
|
995
|
-
needCycles: boolean = false
|
|
996
|
-
) {
|
|
997
|
-
// !! in undirected graph we will not let child visit parent when dfs
|
|
998
|
-
// !! articulation point(in dfs search tree not in graph): (cur !== root && cur.has(child)) && (low(child) >= dfn(cur)) || (cur === root && cur.children() >= 2)
|
|
999
|
-
// !! bridge: low(child) > dfn(cur)
|
|
1000
|
-
|
|
1001
|
-
const defaultConfig = false;
|
|
1002
|
-
if (needCutVertexes === undefined) needCutVertexes = defaultConfig;
|
|
1003
|
-
if (needBridges === undefined) needBridges = defaultConfig;
|
|
1004
|
-
if (needSCCs === undefined) needSCCs = defaultConfig;
|
|
1005
|
-
if (needCycles === undefined) needCycles = defaultConfig;
|
|
1006
|
-
|
|
1007
|
-
const dfnMap: Map<VO, number> = new Map();
|
|
1008
|
-
const lowMap: Map<VO, number> = new Map();
|
|
1009
|
-
const vertexMap = this._vertexMap;
|
|
1010
|
-
vertexMap.forEach(v => {
|
|
1011
|
-
dfnMap.set(v, -1);
|
|
1012
|
-
lowMap.set(v, Infinity);
|
|
1013
|
-
});
|
|
1014
|
-
|
|
1015
|
-
const [root] = vertexMap.values();
|
|
1016
|
-
|
|
1017
|
-
const cutVertexes: VO[] = [];
|
|
1018
|
-
const bridges: EO[] = [];
|
|
1019
|
-
let dfn = 0;
|
|
1020
|
-
const dfs = (cur: VO, parent: VO | undefined) => {
|
|
1021
|
-
dfn++;
|
|
1022
|
-
dfnMap.set(cur, dfn);
|
|
1023
|
-
lowMap.set(cur, dfn);
|
|
1024
|
-
|
|
1025
|
-
const neighbors = this.getNeighbors(cur);
|
|
1026
|
-
let childCount = 0; // child in dfs tree not child in graph
|
|
1027
|
-
for (const neighbor of neighbors) {
|
|
1028
|
-
if (neighbor !== parent) {
|
|
1029
|
-
if (dfnMap.get(neighbor) === -1) {
|
|
1030
|
-
childCount++;
|
|
1031
|
-
dfs(neighbor, cur);
|
|
1032
|
-
}
|
|
1033
|
-
const childLow = lowMap.get(neighbor);
|
|
1034
|
-
const curLow = lowMap.get(cur);
|
|
1035
|
-
// TODO after no-non-undefined-assertion not ensure the logic
|
|
1036
|
-
if (curLow !== undefined && childLow !== undefined) {
|
|
1037
|
-
lowMap.set(cur, Math.min(curLow, childLow));
|
|
1038
|
-
}
|
|
1039
|
-
const curFromMap = dfnMap.get(cur);
|
|
1040
|
-
if (childLow !== undefined && curFromMap !== undefined) {
|
|
1041
|
-
if (needCutVertexes) {
|
|
1042
|
-
if ((cur === root && childCount >= 2) || (cur !== root && childLow >= curFromMap)) {
|
|
1043
|
-
// todo not ensure the logic if (cur === root && childCount >= 2 || ((cur !== root) && (childLow >= curFromMap))) {
|
|
1044
|
-
cutVertexes.push(cur);
|
|
1045
|
-
}
|
|
1046
|
-
}
|
|
1047
|
-
|
|
1048
|
-
if (needBridges) {
|
|
1049
|
-
if (childLow > curFromMap) {
|
|
1050
|
-
const edgeCurToNeighbor = this.getEdge(cur, neighbor);
|
|
1051
|
-
if (edgeCurToNeighbor) {
|
|
1052
|
-
bridges.push(edgeCurToNeighbor);
|
|
1053
|
-
}
|
|
1054
|
-
}
|
|
1055
|
-
}
|
|
1056
|
-
}
|
|
1057
|
-
}
|
|
1058
|
-
}
|
|
1059
|
-
};
|
|
1060
|
-
|
|
1061
|
-
dfs(root, undefined);
|
|
1062
|
-
|
|
1063
|
-
let SCCs: Map<number, VO[]> = new Map();
|
|
1064
|
-
|
|
1065
|
-
const getSCCs = () => {
|
|
1066
|
-
const SCCs: Map<number, VO[]> = new Map();
|
|
1067
|
-
lowMap.forEach((low, vertex) => {
|
|
1068
|
-
if (!SCCs.has(low)) {
|
|
1069
|
-
SCCs.set(low, [vertex]);
|
|
1070
|
-
} else {
|
|
1071
|
-
SCCs.get(low)?.push(vertex);
|
|
1072
|
-
}
|
|
1073
|
-
});
|
|
1074
|
-
return SCCs;
|
|
1075
|
-
};
|
|
1076
|
-
|
|
1077
|
-
if (needSCCs) {
|
|
1078
|
-
SCCs = getSCCs();
|
|
1079
|
-
}
|
|
1080
|
-
|
|
1081
|
-
const cycles: Map<number, VO[]> = new Map();
|
|
1082
|
-
|
|
1083
|
-
if (needCycles) {
|
|
1084
|
-
const visitedMap: Map<VO, boolean> = new Map();
|
|
1085
|
-
const stack: VO[] = [];
|
|
1086
|
-
const findCyclesDFS = (cur: VO, parent: VO | undefined) => {
|
|
1087
|
-
visitedMap.set(cur, true);
|
|
1088
|
-
stack.push(cur);
|
|
1089
|
-
|
|
1090
|
-
const neighbors = this.getNeighbors(cur);
|
|
1091
|
-
|
|
1092
|
-
for (const neighbor of neighbors) {
|
|
1093
|
-
if (!visitedMap.get(neighbor)) {
|
|
1094
|
-
findCyclesDFS(neighbor, cur);
|
|
1095
|
-
} else if (stack.includes(neighbor) && neighbor !== parent) {
|
|
1096
|
-
const cycleStartIndex = stack.indexOf(neighbor);
|
|
1097
|
-
const cycle = stack.slice(cycleStartIndex);
|
|
1098
|
-
const cycleLow = Math.min(...cycle.map(v => dfnMap.get(v) || Infinity));
|
|
1099
|
-
cycles.set(cycleLow, cycle);
|
|
1100
|
-
}
|
|
1101
|
-
}
|
|
1102
|
-
|
|
1103
|
-
stack.pop();
|
|
1104
|
-
};
|
|
1105
|
-
|
|
1106
|
-
vertexMap.forEach(v => {
|
|
1107
|
-
if (!visitedMap.get(v)) {
|
|
1108
|
-
findCyclesDFS(v, undefined);
|
|
1109
|
-
}
|
|
1110
|
-
});
|
|
1111
|
-
}
|
|
1112
|
-
|
|
1113
|
-
return { dfnMap, lowMap, bridges, cutVertexes, SCCs, cycles };
|
|
1114
|
-
}
|
|
1115
|
-
|
|
1116
|
-
/**
|
|
1117
|
-
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
|
|
1118
|
-
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
|
|
1119
|
-
*/
|
|
1120
|
-
|
|
1121
|
-
/**
|
|
1122
|
-
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
|
|
1123
|
-
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
|
|
1124
|
-
*
|
|
1125
|
-
* The function returns a map that associates each vertex object with its corresponding depth-first
|
|
1126
|
-
* number.
|
|
1127
|
-
* @returns A Map object with keys of type VO and values of type number.
|
|
1128
|
-
*/
|
|
1129
|
-
getDFNMap(): Map<VO, number> {
|
|
1130
|
-
return this.tarjan(false, false, false, false).dfnMap;
|
|
1131
|
-
}
|
|
1132
|
-
|
|
1133
|
-
/**
|
|
1134
|
-
* The function returns a Map object that contains the low values of each vertex in a Tarjan
|
|
1135
|
-
* algorithm.
|
|
1136
|
-
* @returns The method `getLowMap()` is returning a `Map` object with keys of type `VO` and values of
|
|
1137
|
-
* type `number`.
|
|
1138
|
-
*/
|
|
1139
|
-
getLowMap(): Map<VO, number> {
|
|
1140
|
-
return this.tarjan(false, false, false, false).lowMap;
|
|
1141
|
-
}
|
|
1142
|
-
|
|
1143
|
-
/**
|
|
1144
|
-
* The function "getCutVertexes" returns an array of cut vertexes using the Tarjan algorithm.
|
|
1145
|
-
* @returns an array of VO objects, specifically the cut vertexes.
|
|
1146
|
-
*/
|
|
1147
|
-
getCutVertexes(): VO[] {
|
|
1148
|
-
return this.tarjan(true, false, false, false).cutVertexes;
|
|
1149
|
-
}
|
|
1150
|
-
|
|
1151
|
-
/**
|
|
1152
|
-
* The function "getSCCs" returns a map of strongly connected components (SCCs) using the Tarjan
|
|
1153
|
-
* algorithm.
|
|
1154
|
-
* @returns a map where the keys are numbers and the values are arrays of VO objects.
|
|
1155
|
-
*/
|
|
1156
|
-
getSCCs(): Map<number, VO[]> {
|
|
1157
|
-
return this.tarjan(false, false, true, false).SCCs;
|
|
1158
|
-
}
|
|
1159
|
-
|
|
1160
|
-
/**
|
|
1161
|
-
* The function "getBridges" returns an array of bridges using the Tarjan algorithm.
|
|
1162
|
-
* @returns the bridges found using the Tarjan algorithm.
|
|
1163
|
-
*/
|
|
1164
|
-
getBridges() {
|
|
1165
|
-
return this.tarjan(false, true, false, false).bridges;
|
|
1166
|
-
}
|
|
1167
|
-
|
|
1168
957
|
/**
|
|
1169
958
|
* O(V+E+C)
|
|
1170
959
|
* O(V+C)
|