deque-typed 1.47.6 → 1.47.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/avl-tree.d.ts +40 -22
- package/dist/data-structures/binary-tree/avl-tree.js +45 -36
- package/dist/data-structures/binary-tree/binary-tree.d.ts +105 -113
- package/dist/data-structures/binary-tree/binary-tree.js +133 -119
- package/dist/data-structures/binary-tree/bst.d.ts +53 -44
- package/dist/data-structures/binary-tree/bst.js +137 -154
- package/dist/data-structures/binary-tree/rb-tree.d.ts +48 -15
- package/dist/data-structures/binary-tree/rb-tree.js +70 -33
- package/dist/data-structures/binary-tree/segment-tree.d.ts +6 -6
- package/dist/data-structures/binary-tree/segment-tree.js +7 -7
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +26 -37
- package/dist/data-structures/binary-tree/tree-multimap.js +58 -137
- package/dist/data-structures/graph/abstract-graph.d.ts +17 -17
- package/dist/data-structures/graph/abstract-graph.js +30 -30
- package/dist/data-structures/graph/directed-graph.d.ts +24 -24
- package/dist/data-structures/graph/directed-graph.js +28 -28
- package/dist/data-structures/graph/undirected-graph.d.ts +14 -14
- package/dist/data-structures/graph/undirected-graph.js +18 -18
- package/dist/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/data-structures/hash/hash-map.js +5 -8
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +28 -28
- package/dist/data-structures/linked-list/doubly-linked-list.js +33 -33
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +21 -21
- package/dist/data-structures/linked-list/singly-linked-list.js +27 -27
- package/dist/data-structures/linked-list/skip-linked-list.js +4 -4
- package/dist/data-structures/queue/queue.d.ts +13 -13
- package/dist/data-structures/queue/queue.js +13 -13
- package/dist/data-structures/stack/stack.d.ts +6 -6
- package/dist/data-structures/stack/stack.js +7 -7
- package/dist/data-structures/trie/trie.d.ts +3 -0
- package/dist/data-structures/trie/trie.js +19 -4
- package/dist/interfaces/binary-tree.d.ts +3 -3
- package/dist/types/common.d.ts +6 -1
- package/dist/types/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/types/data-structures/hash/hash-map.d.ts +1 -2
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +59 -39
- package/src/data-structures/binary-tree/binary-tree.ts +192 -180
- package/src/data-structures/binary-tree/bst.ts +157 -154
- package/src/data-structures/binary-tree/rb-tree.ts +78 -37
- package/src/data-structures/binary-tree/segment-tree.ts +10 -10
- package/src/data-structures/binary-tree/tree-multimap.ts +67 -145
- package/src/data-structures/graph/abstract-graph.ts +46 -46
- package/src/data-structures/graph/directed-graph.ts +40 -40
- package/src/data-structures/graph/undirected-graph.ts +26 -26
- package/src/data-structures/hash/hash-map.ts +8 -8
- package/src/data-structures/linked-list/doubly-linked-list.ts +45 -45
- package/src/data-structures/linked-list/singly-linked-list.ts +38 -38
- package/src/data-structures/linked-list/skip-linked-list.ts +4 -4
- package/src/data-structures/queue/queue.ts +13 -13
- package/src/data-structures/stack/stack.ts +9 -9
- package/src/data-structures/trie/trie.ts +23 -4
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +11 -1
- package/src/types/data-structures/graph/abstract-graph.ts +2 -2
- package/src/types/data-structures/hash/hash-map.ts +1 -2
|
@@ -11,16 +11,16 @@ import type { SegmentTreeNodeVal } from '../../types';
|
|
|
11
11
|
export class SegmentTreeNode {
|
|
12
12
|
start = 0;
|
|
13
13
|
end = 0;
|
|
14
|
-
value: SegmentTreeNodeVal |
|
|
14
|
+
value: SegmentTreeNodeVal | undefined = undefined;
|
|
15
15
|
sum = 0;
|
|
16
|
-
left: SegmentTreeNode |
|
|
17
|
-
right: SegmentTreeNode |
|
|
16
|
+
left: SegmentTreeNode | undefined = undefined;
|
|
17
|
+
right: SegmentTreeNode | undefined = undefined;
|
|
18
18
|
|
|
19
|
-
constructor(start: number, end: number, sum: number, value?: SegmentTreeNodeVal |
|
|
19
|
+
constructor(start: number, end: number, sum: number, value?: SegmentTreeNodeVal | undefined) {
|
|
20
20
|
this.start = start;
|
|
21
21
|
this.end = end;
|
|
22
22
|
this.sum = sum;
|
|
23
|
-
this.value = value ||
|
|
23
|
+
this.value = value || undefined;
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
|
|
@@ -44,7 +44,7 @@ export class SegmentTree {
|
|
|
44
44
|
if (values.length > 0) {
|
|
45
45
|
this._root = this.build(start, end);
|
|
46
46
|
} else {
|
|
47
|
-
this._root =
|
|
47
|
+
this._root = undefined;
|
|
48
48
|
this._values = [];
|
|
49
49
|
}
|
|
50
50
|
}
|
|
@@ -67,9 +67,9 @@ export class SegmentTree {
|
|
|
67
67
|
return this._end;
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
-
protected _root: SegmentTreeNode |
|
|
70
|
+
protected _root: SegmentTreeNode | undefined;
|
|
71
71
|
|
|
72
|
-
get root(): SegmentTreeNode |
|
|
72
|
+
get root(): SegmentTreeNode | undefined {
|
|
73
73
|
return this._root;
|
|
74
74
|
}
|
|
75
75
|
|
|
@@ -109,7 +109,7 @@ export class SegmentTree {
|
|
|
109
109
|
* @returns The function does not return anything.
|
|
110
110
|
*/
|
|
111
111
|
updateNode(index: number, sum: number, value?: SegmentTreeNodeVal) {
|
|
112
|
-
const root = this.root ||
|
|
112
|
+
const root = this.root || undefined;
|
|
113
113
|
if (!root) {
|
|
114
114
|
return;
|
|
115
115
|
}
|
|
@@ -145,7 +145,7 @@ export class SegmentTree {
|
|
|
145
145
|
* @returns The function `querySumByRange` returns a number.
|
|
146
146
|
*/
|
|
147
147
|
querySumByRange(indexA: number, indexB: number): number {
|
|
148
|
-
const root = this.root ||
|
|
148
|
+
const root = this.root || undefined;
|
|
149
149
|
if (!root) {
|
|
150
150
|
return 0;
|
|
151
151
|
}
|
|
@@ -5,16 +5,14 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
IterableEntriesOrKeys,
|
|
15
|
-
IterationType,
|
|
16
|
-
TreeMultimapNested
|
|
8
|
+
import type {
|
|
9
|
+
BSTNodeKeyOrNode,
|
|
10
|
+
BTNKey,
|
|
11
|
+
BTNodeExemplar,
|
|
12
|
+
TreeMultimapNodeNested,
|
|
13
|
+
TreeMultimapOptions
|
|
17
14
|
} from '../../types';
|
|
15
|
+
import { BiTreeDeleteResult, BTNCallback, FamilyPosition, IterationType, TreeMultimapNested } from '../../types';
|
|
18
16
|
import { IBinaryTree } from '../../interfaces';
|
|
19
17
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
20
18
|
|
|
@@ -48,21 +46,18 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
48
46
|
extends AVLTree<V, N, TREE>
|
|
49
47
|
implements IBinaryTree<V, N, TREE> {
|
|
50
48
|
|
|
51
|
-
|
|
52
|
-
* The constructor function for a TreeMultimap class in TypeScript, which extends another class and sets an option to
|
|
53
|
-
* merge duplicated values.
|
|
54
|
-
* @param {TreeMultimapOptions} [options] - An optional object that contains additional configuration options for the
|
|
55
|
-
* TreeMultimap.
|
|
56
|
-
*/
|
|
57
|
-
constructor(elements?: IterableEntriesOrKeys<V>, options?: Partial<TreeMultimapOptions>) {
|
|
49
|
+
constructor(elements?: Iterable<BTNodeExemplar<V, N>>, options?: Partial<TreeMultimapOptions>) {
|
|
58
50
|
super([], options);
|
|
59
|
-
if (elements) this.
|
|
51
|
+
if (elements) this.addMany(elements);
|
|
60
52
|
}
|
|
61
53
|
|
|
62
54
|
private _count = 0;
|
|
63
55
|
|
|
56
|
+
// TODO the _count is not accurate after nodes count modified
|
|
64
57
|
get count(): number {
|
|
65
|
-
|
|
58
|
+
let sum = 0;
|
|
59
|
+
this.subTreeTraverse(node => sum += node.count);
|
|
60
|
+
return sum;
|
|
66
61
|
}
|
|
67
62
|
|
|
68
63
|
/**
|
|
@@ -85,126 +80,66 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
85
80
|
}) as TREE;
|
|
86
81
|
}
|
|
87
82
|
|
|
83
|
+
/**
|
|
84
|
+
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
85
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
86
|
+
*/
|
|
87
|
+
|
|
88
88
|
/**
|
|
89
89
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
90
90
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
91
91
|
*
|
|
92
|
-
* The `add` function
|
|
93
|
-
*
|
|
94
|
-
* @param
|
|
95
|
-
* following types:
|
|
96
|
-
* @param {V} [value] - The `value` parameter represents the value associated with the key that is
|
|
97
|
-
* being added to the tree. It is an optional parameter, so it can be omitted if not needed.
|
|
92
|
+
* The `add` function overrides the base class `add` function to add a new node to the tree multimap
|
|
93
|
+
* and update the count.
|
|
94
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
|
|
98
95
|
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
99
|
-
* times the key
|
|
100
|
-
*
|
|
96
|
+
* times the key or node or entry should be added to the multimap. If not provided, the default value
|
|
97
|
+
* is 1.
|
|
98
|
+
* @returns either a node (`N`) or `undefined`.
|
|
101
99
|
*/
|
|
102
|
-
override add(
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
if (
|
|
107
|
-
newNode =
|
|
108
|
-
} else if (
|
|
109
|
-
newNode = undefined;
|
|
100
|
+
override add(keyOrNodeOrEntry: BTNodeExemplar<V, N>, count = 1): N | undefined {
|
|
101
|
+
let newNode: N | undefined;
|
|
102
|
+
if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) {
|
|
103
|
+
return;
|
|
104
|
+
} else if (keyOrNodeOrEntry instanceof TreeMultimapNode) {
|
|
105
|
+
newNode = keyOrNodeOrEntry;
|
|
106
|
+
} else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
107
|
+
newNode = this.createNode(keyOrNodeOrEntry, undefined, count);
|
|
108
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
109
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
110
|
+
if (key === undefined || key === null) {
|
|
111
|
+
return;
|
|
112
|
+
} else {
|
|
113
|
+
newNode = this.createNode(key, value, count);
|
|
114
|
+
}
|
|
110
115
|
} else {
|
|
111
|
-
|
|
116
|
+
return;
|
|
112
117
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
inserted = this.root;
|
|
118
|
-
} else {
|
|
119
|
-
let cur = this.root;
|
|
120
|
-
let traversing = true;
|
|
121
|
-
while (traversing) {
|
|
122
|
-
if (cur) {
|
|
123
|
-
if (newNode) {
|
|
124
|
-
if (this._compare(cur.key, newNode.key) === CP.eq) {
|
|
125
|
-
cur.value = newNode.value;
|
|
126
|
-
cur.count += newNode.count;
|
|
127
|
-
this._count += newNode.count;
|
|
128
|
-
traversing = false;
|
|
129
|
-
inserted = cur;
|
|
130
|
-
} else if (this._compare(cur.key, newNode.key) === CP.gt) {
|
|
131
|
-
// Traverse left of the node
|
|
132
|
-
if (cur.left === undefined) {
|
|
133
|
-
//Add to the left of the current node
|
|
134
|
-
cur.left = newNode;
|
|
135
|
-
this._size = this.size + 1;
|
|
136
|
-
this._count += newNode.count;
|
|
137
|
-
|
|
138
|
-
traversing = false;
|
|
139
|
-
inserted = cur.left;
|
|
140
|
-
} else {
|
|
141
|
-
//Traverse the left of the current node
|
|
142
|
-
if (cur.left) cur = cur.left;
|
|
143
|
-
}
|
|
144
|
-
} else if (this._compare(cur.key, newNode.key) === CP.lt) {
|
|
145
|
-
// Traverse right of the node
|
|
146
|
-
if (cur.right === undefined) {
|
|
147
|
-
//Add to the right of the current node
|
|
148
|
-
cur.right = newNode;
|
|
149
|
-
this._size = this.size + 1;
|
|
150
|
-
this._count += newNode.count;
|
|
151
|
-
|
|
152
|
-
traversing = false;
|
|
153
|
-
inserted = cur.right;
|
|
154
|
-
} else {
|
|
155
|
-
//Traverse the left of the current node
|
|
156
|
-
if (cur.right) cur = cur.right;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
} else {
|
|
160
|
-
// TODO may need to support undefined inserted
|
|
161
|
-
}
|
|
162
|
-
} else {
|
|
163
|
-
traversing = false;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
118
|
+
const orgNodeCount = newNode?.count || 0;
|
|
119
|
+
const inserted = super.add(newNode);
|
|
120
|
+
if (inserted) {
|
|
121
|
+
this._count += orgNodeCount;
|
|
166
122
|
}
|
|
167
|
-
if (inserted) this._balancePath(inserted);
|
|
168
123
|
return inserted;
|
|
169
124
|
}
|
|
170
125
|
|
|
171
126
|
/**
|
|
172
|
-
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
127
|
+
* Time Complexity: O(k log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
173
128
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
174
129
|
*/
|
|
175
130
|
|
|
176
131
|
/**
|
|
177
|
-
* Time Complexity: O(k log n) - logarithmic time
|
|
132
|
+
* Time Complexity: O(k log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
178
133
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
179
134
|
*
|
|
180
|
-
* The function
|
|
181
|
-
*
|
|
182
|
-
* @param
|
|
183
|
-
*
|
|
184
|
-
* @
|
|
185
|
-
* keys or nodes being added. It is used to associate data with each key or node being added to the
|
|
186
|
-
* TreeMultimap. If provided, the length of the `data` array should be the same as the length of the
|
|
187
|
-
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
135
|
+
* The function overrides the addMany method to add multiple keys, nodes, or entries to a data
|
|
136
|
+
* structure.
|
|
137
|
+
* @param keysOrNodesOrEntries - The parameter `keysOrNodesOrEntries` is an iterable that can contain
|
|
138
|
+
* either keys, nodes, or entries.
|
|
139
|
+
* @returns The method is returning an array of type `N | undefined`.
|
|
188
140
|
*/
|
|
189
|
-
override addMany(
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
for (let i = 0; i < keysOrNodes.length; i++) {
|
|
193
|
-
const keyOrNode = keysOrNodes[i];
|
|
194
|
-
|
|
195
|
-
if (keyOrNode instanceof TreeMultimapNode) {
|
|
196
|
-
inserted.push(this.add(keyOrNode.key, keyOrNode.value, keyOrNode.count));
|
|
197
|
-
continue;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
if (keyOrNode === undefined) {
|
|
201
|
-
inserted.push(this.add(NaN, undefined, 0));
|
|
202
|
-
continue;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
inserted.push(this.add(keyOrNode, data?.[i], 1));
|
|
206
|
-
}
|
|
207
|
-
return inserted;
|
|
141
|
+
override addMany(keysOrNodesOrEntries: Iterable<BTNodeExemplar<V, N>>): (N | undefined)[] {
|
|
142
|
+
return super.addMany(keysOrNodesOrEntries);
|
|
208
143
|
}
|
|
209
144
|
|
|
210
145
|
/**
|
|
@@ -235,7 +170,7 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
235
170
|
if (l > r) return;
|
|
236
171
|
const m = l + Math.floor((r - l) / 2);
|
|
237
172
|
const midNode = sorted[m];
|
|
238
|
-
this.add(midNode.key, midNode.value, midNode.count);
|
|
173
|
+
this.add([midNode.key, midNode.value], midNode.count);
|
|
239
174
|
buildBalanceBST(l, m - 1);
|
|
240
175
|
buildBalanceBST(m + 1, r);
|
|
241
176
|
};
|
|
@@ -251,7 +186,7 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
251
186
|
if (l <= r) {
|
|
252
187
|
const m = l + Math.floor((r - l) / 2);
|
|
253
188
|
const midNode = sorted[m];
|
|
254
|
-
this.add(midNode.key, midNode.value, midNode.count);
|
|
189
|
+
this.add([midNode.key, midNode.value], midNode.count);
|
|
255
190
|
stack.push([m + 1, r]);
|
|
256
191
|
stack.push([l, m - 1]);
|
|
257
192
|
}
|
|
@@ -320,7 +255,7 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
320
255
|
const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : undefined;
|
|
321
256
|
if (leftSubTreeRightMost) {
|
|
322
257
|
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
323
|
-
orgCurrent = this.
|
|
258
|
+
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
|
|
324
259
|
if (parentOfLeftSubTreeMax) {
|
|
325
260
|
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost) {
|
|
326
261
|
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
@@ -358,24 +293,6 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
358
293
|
this._count = 0;
|
|
359
294
|
}
|
|
360
295
|
|
|
361
|
-
/**
|
|
362
|
-
* 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.
|
|
363
|
-
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
364
|
-
*/
|
|
365
|
-
|
|
366
|
-
init(elements: IterableEntriesOrKeys<V>): void {
|
|
367
|
-
if (elements) {
|
|
368
|
-
for (const entryOrKey of elements) {
|
|
369
|
-
if (Array.isArray(entryOrKey)) {
|
|
370
|
-
const [key, value] = entryOrKey;
|
|
371
|
-
this.add(key, value);
|
|
372
|
-
} else {
|
|
373
|
-
this.add(entryOrKey);
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
|
|
379
296
|
/**
|
|
380
297
|
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
381
298
|
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
@@ -391,8 +308,8 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
391
308
|
* @returns The method `_addTo` returns either the `parent.left` or `parent.right` node that was
|
|
392
309
|
* added, or `undefined` if no node was added.
|
|
393
310
|
*/
|
|
394
|
-
protected override _addTo(newNode: N | undefined, parent:
|
|
395
|
-
parent = this.
|
|
311
|
+
protected override _addTo(newNode: N | undefined, parent: BSTNodeKeyOrNode<N>): N | undefined {
|
|
312
|
+
parent = this.ensureNode(parent);
|
|
396
313
|
if (parent) {
|
|
397
314
|
if (parent.left === undefined) {
|
|
398
315
|
parent.left = newNode;
|
|
@@ -418,7 +335,7 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
418
335
|
}
|
|
419
336
|
|
|
420
337
|
/**
|
|
421
|
-
* The `
|
|
338
|
+
* The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
|
|
422
339
|
* @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node from
|
|
423
340
|
* which the values will be swapped. It can be of type `BTNKey`, `N`, or `undefined`.
|
|
424
341
|
* @param {BTNKey | N | undefined} destNode - The `destNode` parameter represents the destination
|
|
@@ -426,9 +343,9 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
426
343
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
427
344
|
* if either `srcNode` or `destNode` is undefined.
|
|
428
345
|
*/
|
|
429
|
-
protected
|
|
430
|
-
srcNode = this.
|
|
431
|
-
destNode = this.
|
|
346
|
+
protected override _swapProperties(srcNode: BSTNodeKeyOrNode<N>, destNode: BSTNodeKeyOrNode<N>): N | undefined {
|
|
347
|
+
srcNode = this.ensureNode(srcNode);
|
|
348
|
+
destNode = this.ensureNode(destNode);
|
|
432
349
|
if (srcNode && destNode) {
|
|
433
350
|
const { key, value, count, height } = destNode;
|
|
434
351
|
const tempNode = this.createNode(key, value, count);
|
|
@@ -450,4 +367,9 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
450
367
|
}
|
|
451
368
|
return undefined;
|
|
452
369
|
}
|
|
370
|
+
|
|
371
|
+
protected _replaceNode(oldNode: N, newNode: N): N {
|
|
372
|
+
newNode.count = oldNode.count + newNode.count
|
|
373
|
+
return super._replaceNode(oldNode, newNode);
|
|
374
|
+
}
|
|
453
375
|
}
|
|
@@ -89,9 +89,9 @@ export abstract class AbstractGraph<
|
|
|
89
89
|
*/
|
|
90
90
|
abstract createEdge(srcOrV1: VertexKey, destOrV2: VertexKey, weight?: number, value?: E): EO;
|
|
91
91
|
|
|
92
|
-
abstract deleteEdge(edge: EO): EO |
|
|
92
|
+
abstract deleteEdge(edge: EO): EO | undefined;
|
|
93
93
|
|
|
94
|
-
abstract getEdge(srcOrKey: VO | VertexKey, destOrKey: VO | VertexKey): EO |
|
|
94
|
+
abstract getEdge(srcOrKey: VO | VertexKey, destOrKey: VO | VertexKey): EO | undefined;
|
|
95
95
|
|
|
96
96
|
abstract degreeOf(vertexOrKey: VO | VertexKey): number;
|
|
97
97
|
|
|
@@ -101,7 +101,7 @@ export abstract class AbstractGraph<
|
|
|
101
101
|
|
|
102
102
|
abstract getNeighbors(vertexOrKey: VO | VertexKey): VO[];
|
|
103
103
|
|
|
104
|
-
abstract getEndsOfEdge(edge: EO): [VO, VO] |
|
|
104
|
+
abstract getEndsOfEdge(edge: EO): [VO, VO] | undefined;
|
|
105
105
|
|
|
106
106
|
/**
|
|
107
107
|
* Time Complexity: O(1) - Constant time for Map lookup.
|
|
@@ -112,14 +112,14 @@ export abstract class AbstractGraph<
|
|
|
112
112
|
* Time Complexity: O(1) - Constant time for Map lookup.
|
|
113
113
|
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
114
114
|
*
|
|
115
|
-
* The function "getVertex" returns the vertex with the specified ID or
|
|
115
|
+
* The function "getVertex" returns the vertex with the specified ID or undefined if it doesn't exist.
|
|
116
116
|
* @param {VertexKey} vertexKey - The `vertexKey` parameter is the identifier of the vertex that you want to retrieve from
|
|
117
117
|
* the `_vertices` map.
|
|
118
118
|
* @returns The method `getVertex` returns the vertex with the specified `vertexKey` if it exists in the `_vertices`
|
|
119
|
-
* map. If the vertex does not exist, it returns `
|
|
119
|
+
* map. If the vertex does not exist, it returns `undefined`.
|
|
120
120
|
*/
|
|
121
|
-
getVertex(vertexKey: VertexKey): VO |
|
|
122
|
-
return this._vertices.get(vertexKey) ||
|
|
121
|
+
getVertex(vertexKey: VertexKey): VO | undefined {
|
|
122
|
+
return this._vertices.get(vertexKey) || undefined;
|
|
123
123
|
}
|
|
124
124
|
|
|
125
125
|
/**
|
|
@@ -365,7 +365,7 @@ export abstract class AbstractGraph<
|
|
|
365
365
|
* vertices. If `isWeight` is `false` or not provided, it uses a breadth-first search (BFS) algorithm to calculate the
|
|
366
366
|
* minimum number of
|
|
367
367
|
*/
|
|
368
|
-
getMinCostBetween(v1: VO | VertexKey, v2: VO | VertexKey, isWeight?: boolean): number |
|
|
368
|
+
getMinCostBetween(v1: VO | VertexKey, v2: VO | VertexKey, isWeight?: boolean): number | undefined {
|
|
369
369
|
if (isWeight === undefined) isWeight = false;
|
|
370
370
|
|
|
371
371
|
if (isWeight) {
|
|
@@ -380,7 +380,7 @@ export abstract class AbstractGraph<
|
|
|
380
380
|
const vertex2 = this._getVertex(v2);
|
|
381
381
|
const vertex1 = this._getVertex(v1);
|
|
382
382
|
if (!(vertex1 && vertex2)) {
|
|
383
|
-
return
|
|
383
|
+
return undefined;
|
|
384
384
|
}
|
|
385
385
|
|
|
386
386
|
const visited: Map<VO, boolean> = new Map();
|
|
@@ -406,7 +406,7 @@ export abstract class AbstractGraph<
|
|
|
406
406
|
}
|
|
407
407
|
cost++;
|
|
408
408
|
}
|
|
409
|
-
return
|
|
409
|
+
return undefined;
|
|
410
410
|
}
|
|
411
411
|
}
|
|
412
412
|
|
|
@@ -432,9 +432,9 @@ export abstract class AbstractGraph<
|
|
|
432
432
|
* followed by iterative computation of the shortest path. This approach may result in exponential time complexity,
|
|
433
433
|
* so the default method is to use the Dijkstra algorithm to obtain the shortest weighted path.
|
|
434
434
|
* @returns The function `getMinPathBetween` returns an array of vertices (`VO[]`) representing the minimum path between
|
|
435
|
-
* two vertices (`v1` and `v2`). If there is no path between the vertices, it returns `
|
|
435
|
+
* two vertices (`v1` and `v2`). If there is no path between the vertices, it returns `undefined`.
|
|
436
436
|
*/
|
|
437
|
-
getMinPathBetween(v1: VO | VertexKey, v2: VO | VertexKey, isWeight?: boolean, isDFS = false): VO[] |
|
|
437
|
+
getMinPathBetween(v1: VO | VertexKey, v2: VO | VertexKey, isWeight?: boolean, isDFS = false): VO[] | undefined {
|
|
438
438
|
if (isWeight === undefined) isWeight = false;
|
|
439
439
|
|
|
440
440
|
if (isWeight) {
|
|
@@ -451,7 +451,7 @@ export abstract class AbstractGraph<
|
|
|
451
451
|
}
|
|
452
452
|
index++;
|
|
453
453
|
}
|
|
454
|
-
return allPaths[minIndex] ||
|
|
454
|
+
return allPaths[minIndex] || undefined;
|
|
455
455
|
} else {
|
|
456
456
|
return this.dijkstra(v1, v2, true, true)?.minPath ?? [];
|
|
457
457
|
}
|
|
@@ -503,9 +503,9 @@ export abstract class AbstractGraph<
|
|
|
503
503
|
* a graph without using a heap data structure.
|
|
504
504
|
* @param {VO | VertexKey} src - The source vertex from which to start the Dijkstra's algorithm. It can be either a
|
|
505
505
|
* vertex object or a vertex ID.
|
|
506
|
-
* @param {VO | VertexKey |
|
|
506
|
+
* @param {VO | VertexKey | undefined} [dest] - The `dest` parameter in the `dijkstraWithoutHeap` function is an optional
|
|
507
507
|
* parameter that specifies the destination vertex for the Dijkstra algorithm. It can be either a vertex object or its
|
|
508
|
-
* identifier. If no destination is provided, the value is set to `
|
|
508
|
+
* identifier. If no destination is provided, the value is set to `undefined`.
|
|
509
509
|
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
510
510
|
* distance from the source vertex to the destination vertex should be calculated and returned in the result. If
|
|
511
511
|
* `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
|
|
@@ -516,29 +516,29 @@ export abstract class AbstractGraph<
|
|
|
516
516
|
*/
|
|
517
517
|
dijkstraWithoutHeap(
|
|
518
518
|
src: VO | VertexKey,
|
|
519
|
-
dest?: VO | VertexKey |
|
|
519
|
+
dest?: VO | VertexKey | undefined,
|
|
520
520
|
getMinDist?: boolean,
|
|
521
521
|
genPaths?: boolean
|
|
522
522
|
): DijkstraResult<VO> {
|
|
523
523
|
if (getMinDist === undefined) getMinDist = false;
|
|
524
524
|
if (genPaths === undefined) genPaths = false;
|
|
525
525
|
|
|
526
|
-
if (dest === undefined) dest =
|
|
526
|
+
if (dest === undefined) dest = undefined;
|
|
527
527
|
let minDist = Infinity;
|
|
528
|
-
let minDest: VO |
|
|
528
|
+
let minDest: VO | undefined = undefined;
|
|
529
529
|
let minPath: VO[] = [];
|
|
530
530
|
const paths: VO[][] = [];
|
|
531
531
|
|
|
532
532
|
const vertices = this._vertices;
|
|
533
533
|
const distMap: Map<VO, number> = new Map();
|
|
534
534
|
const seen: Set<VO> = new Set();
|
|
535
|
-
const preMap: Map<VO, VO |
|
|
535
|
+
const preMap: Map<VO, VO | undefined> = new Map(); // predecessor
|
|
536
536
|
const srcVertex = this._getVertex(src);
|
|
537
537
|
|
|
538
|
-
const destVertex = dest ? this._getVertex(dest) :
|
|
538
|
+
const destVertex = dest ? this._getVertex(dest) : undefined;
|
|
539
539
|
|
|
540
540
|
if (!srcVertex) {
|
|
541
|
-
return
|
|
541
|
+
return undefined;
|
|
542
542
|
}
|
|
543
543
|
|
|
544
544
|
for (const vertex of vertices) {
|
|
@@ -546,11 +546,11 @@ export abstract class AbstractGraph<
|
|
|
546
546
|
if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
|
|
547
547
|
}
|
|
548
548
|
distMap.set(srcVertex, 0);
|
|
549
|
-
preMap.set(srcVertex,
|
|
549
|
+
preMap.set(srcVertex, undefined);
|
|
550
550
|
|
|
551
551
|
const getMinOfNoSeen = () => {
|
|
552
552
|
let min = Infinity;
|
|
553
|
-
let minV: VO |
|
|
553
|
+
let minV: VO | undefined = undefined;
|
|
554
554
|
for (const [key, value] of distMap) {
|
|
555
555
|
if (!seen.has(key)) {
|
|
556
556
|
if (value < min) {
|
|
@@ -562,7 +562,7 @@ export abstract class AbstractGraph<
|
|
|
562
562
|
return minV;
|
|
563
563
|
};
|
|
564
564
|
|
|
565
|
-
const getPaths = (minV: VO |
|
|
565
|
+
const getPaths = (minV: VO | undefined) => {
|
|
566
566
|
for (const vertex of vertices) {
|
|
567
567
|
const vertexOrKey = vertex[1];
|
|
568
568
|
|
|
@@ -600,7 +600,7 @@ export abstract class AbstractGraph<
|
|
|
600
600
|
if (edge) {
|
|
601
601
|
const curFromMap = distMap.get(cur);
|
|
602
602
|
const neighborFromMap = distMap.get(neighbor);
|
|
603
|
-
// TODO after no-non-
|
|
603
|
+
// TODO after no-non-undefined-assertion not ensure the logic
|
|
604
604
|
if (curFromMap !== undefined && neighborFromMap !== undefined) {
|
|
605
605
|
if (edge.weight + curFromMap < neighborFromMap) {
|
|
606
606
|
distMap.set(neighbor, edge.weight + curFromMap);
|
|
@@ -651,7 +651,7 @@ export abstract class AbstractGraph<
|
|
|
651
651
|
* optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
|
|
652
652
|
* @param {VO | VertexKey} src - The `src` parameter represents the source vertex from which the Dijkstra algorithm will
|
|
653
653
|
* start. It can be either a vertex object or a vertex ID.
|
|
654
|
-
* @param {VO | VertexKey |
|
|
654
|
+
* @param {VO | VertexKey | undefined} [dest] - The `dest` parameter is the destination vertex or vertex ID. It specifies the
|
|
655
655
|
* vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm
|
|
656
656
|
* will calculate the shortest paths to all other vertices from the source vertex.
|
|
657
657
|
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
@@ -664,27 +664,27 @@ export abstract class AbstractGraph<
|
|
|
664
664
|
*/
|
|
665
665
|
dijkstra(
|
|
666
666
|
src: VO | VertexKey,
|
|
667
|
-
dest?: VO | VertexKey |
|
|
667
|
+
dest?: VO | VertexKey | undefined,
|
|
668
668
|
getMinDist?: boolean,
|
|
669
669
|
genPaths?: boolean
|
|
670
670
|
): DijkstraResult<VO> {
|
|
671
671
|
if (getMinDist === undefined) getMinDist = false;
|
|
672
672
|
if (genPaths === undefined) genPaths = false;
|
|
673
673
|
|
|
674
|
-
if (dest === undefined) dest =
|
|
674
|
+
if (dest === undefined) dest = undefined;
|
|
675
675
|
let minDist = Infinity;
|
|
676
|
-
let minDest: VO |
|
|
676
|
+
let minDest: VO | undefined = undefined;
|
|
677
677
|
let minPath: VO[] = [];
|
|
678
678
|
const paths: VO[][] = [];
|
|
679
679
|
const vertices = this._vertices;
|
|
680
680
|
const distMap: Map<VO, number> = new Map();
|
|
681
681
|
const seen: Set<VO> = new Set();
|
|
682
|
-
const preMap: Map<VO, VO |
|
|
682
|
+
const preMap: Map<VO, VO | undefined> = new Map(); // predecessor
|
|
683
683
|
|
|
684
684
|
const srcVertex = this._getVertex(src);
|
|
685
|
-
const destVertex = dest ? this._getVertex(dest) :
|
|
685
|
+
const destVertex = dest ? this._getVertex(dest) : undefined;
|
|
686
686
|
|
|
687
|
-
if (!srcVertex) return
|
|
687
|
+
if (!srcVertex) return undefined;
|
|
688
688
|
|
|
689
689
|
for (const vertex of vertices) {
|
|
690
690
|
const vertexOrKey = vertex[1];
|
|
@@ -695,14 +695,14 @@ export abstract class AbstractGraph<
|
|
|
695
695
|
heap.add({ key: 0, value: srcVertex });
|
|
696
696
|
|
|
697
697
|
distMap.set(srcVertex, 0);
|
|
698
|
-
preMap.set(srcVertex,
|
|
698
|
+
preMap.set(srcVertex, undefined);
|
|
699
699
|
|
|
700
700
|
/**
|
|
701
701
|
* The function `getPaths` retrieves all paths from vertices to a specified minimum vertex.
|
|
702
|
-
* @param {VO |
|
|
703
|
-
*
|
|
702
|
+
* @param {VO | undefined} minV - The parameter `minV` is of type `VO | undefined`. It represents the minimum vertex value or
|
|
703
|
+
* undefined.
|
|
704
704
|
*/
|
|
705
|
-
const getPaths = (minV: VO |
|
|
705
|
+
const getPaths = (minV: VO | undefined) => {
|
|
706
706
|
for (const vertex of vertices) {
|
|
707
707
|
const vertexOrKey = vertex[1];
|
|
708
708
|
if (vertexOrKey instanceof AbstractVertex) {
|
|
@@ -841,7 +841,7 @@ export abstract class AbstractGraph<
|
|
|
841
841
|
}
|
|
842
842
|
}
|
|
843
843
|
|
|
844
|
-
let minDest: VO |
|
|
844
|
+
let minDest: VO | undefined = undefined;
|
|
845
845
|
if (getMin) {
|
|
846
846
|
distMap.forEach((d, v) => {
|
|
847
847
|
if (v !== srcVertex) {
|
|
@@ -920,22 +920,22 @@ export abstract class AbstractGraph<
|
|
|
920
920
|
* graph.
|
|
921
921
|
* @returns The function `floydWarshall()` returns an object with two properties: `costs` and `predecessor`. The `costs`
|
|
922
922
|
* property is a 2D array of numbers representing the shortest path costs between vertices in a graph. The
|
|
923
|
-
* `predecessor` property is a 2D array of vertices (or `
|
|
923
|
+
* `predecessor` property is a 2D array of vertices (or `undefined`) representing the predecessor vertices in the shortest
|
|
924
924
|
* path between vertices in the
|
|
925
925
|
*/
|
|
926
|
-
floydWarshall(): { costs: number[][]; predecessor: (VO |
|
|
926
|
+
floydWarshall(): { costs: number[][]; predecessor: (VO | undefined)[][] } {
|
|
927
927
|
const idAndVertices = [...this._vertices];
|
|
928
928
|
const n = idAndVertices.length;
|
|
929
929
|
|
|
930
930
|
const costs: number[][] = [];
|
|
931
|
-
const predecessor: (VO |
|
|
931
|
+
const predecessor: (VO | undefined)[][] = [];
|
|
932
932
|
// successors
|
|
933
933
|
|
|
934
934
|
for (let i = 0; i < n; i++) {
|
|
935
935
|
costs[i] = [];
|
|
936
936
|
predecessor[i] = [];
|
|
937
937
|
for (let j = 0; j < n; j++) {
|
|
938
|
-
predecessor[i][j] =
|
|
938
|
+
predecessor[i][j] = undefined;
|
|
939
939
|
}
|
|
940
940
|
}
|
|
941
941
|
|
|
@@ -1021,7 +1021,7 @@ export abstract class AbstractGraph<
|
|
|
1021
1021
|
const cutVertexes: VO[] = [];
|
|
1022
1022
|
const bridges: EO[] = [];
|
|
1023
1023
|
let dfn = 0;
|
|
1024
|
-
const dfs = (cur: VO, parent: VO |
|
|
1024
|
+
const dfs = (cur: VO, parent: VO | undefined) => {
|
|
1025
1025
|
dfn++;
|
|
1026
1026
|
dfnMap.set(cur, dfn);
|
|
1027
1027
|
lowMap.set(cur, dfn);
|
|
@@ -1036,7 +1036,7 @@ export abstract class AbstractGraph<
|
|
|
1036
1036
|
}
|
|
1037
1037
|
const childLow = lowMap.get(neighbor);
|
|
1038
1038
|
const curLow = lowMap.get(cur);
|
|
1039
|
-
// TODO after no-non-
|
|
1039
|
+
// TODO after no-non-undefined-assertion not ensure the logic
|
|
1040
1040
|
if (curLow !== undefined && childLow !== undefined) {
|
|
1041
1041
|
lowMap.set(cur, Math.min(curLow, childLow));
|
|
1042
1042
|
}
|
|
@@ -1062,7 +1062,7 @@ export abstract class AbstractGraph<
|
|
|
1062
1062
|
}
|
|
1063
1063
|
};
|
|
1064
1064
|
|
|
1065
|
-
dfs(root,
|
|
1065
|
+
dfs(root, undefined);
|
|
1066
1066
|
|
|
1067
1067
|
let SCCs: Map<number, VO[]> = new Map();
|
|
1068
1068
|
|
|
@@ -1170,9 +1170,9 @@ export abstract class AbstractGraph<
|
|
|
1170
1170
|
return true;
|
|
1171
1171
|
}
|
|
1172
1172
|
|
|
1173
|
-
protected _getVertex(vertexOrKey: VertexKey | VO): VO |
|
|
1173
|
+
protected _getVertex(vertexOrKey: VertexKey | VO): VO | undefined {
|
|
1174
1174
|
const vertexKey = this._getVertexKey(vertexOrKey);
|
|
1175
|
-
return this._vertices.get(vertexKey) ||
|
|
1175
|
+
return this._vertices.get(vertexKey) || undefined;
|
|
1176
1176
|
}
|
|
1177
1177
|
|
|
1178
1178
|
protected _getVertexKey(vertexOrKey: VO | VertexKey): VertexKey {
|