min-heap-typed 1.50.2 → 1.50.4
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/{tree-multimap.d.ts → avl-tree-multi-map.d.ts} +43 -10
- package/dist/data-structures/binary-tree/{tree-multimap.js → avl-tree-multi-map.js} +49 -11
- 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 +51 -20
- package/dist/data-structures/binary-tree/index.d.ts +2 -1
- package/dist/data-structures/binary-tree/index.js +2 -1
- package/dist/data-structures/binary-tree/rb-tree.d.ts +54 -2
- package/dist/data-structures/binary-tree/rb-tree.js +90 -24
- 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-multi-map.d.ts +200 -0
- package/dist/data-structures/binary-tree/tree-multi-map.js +399 -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/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -0
- package/dist/types/data-structures/binary-tree/index.d.ts +2 -1
- package/dist/types/data-structures/binary-tree/index.js +2 -1
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +5 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.js +2 -0
- package/package.json +2 -2
- package/src/data-structures/base/iterable-base.ts +12 -0
- package/src/data-structures/binary-tree/{tree-multimap.ts → avl-tree-multi-map.ts} +59 -20
- 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 +51 -19
- package/src/data-structures/binary-tree/index.ts +2 -1
- package/src/data-structures/binary-tree/rb-tree.ts +99 -28
- package/src/data-structures/binary-tree/segment-tree.ts +145 -11
- package/src/data-structures/binary-tree/tree-multi-map.ts +463 -0
- 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
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +8 -0
- package/src/types/data-structures/binary-tree/index.ts +2 -1
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +8 -0
- package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +0 -5
- package/src/types/data-structures/binary-tree/tree-multimap.ts +0 -8
- /package/dist/types/data-structures/binary-tree/{tree-multimap.js → avl-tree-multi-map.js} +0 -0
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TreeMultiMap = exports.TreeMultiMapNode = void 0;
|
|
4
|
+
const types_1 = require("../../types");
|
|
5
|
+
const rb_tree_1 = require("./rb-tree");
|
|
6
|
+
class TreeMultiMapNode extends rb_tree_1.RedBlackTreeNode {
|
|
7
|
+
/**
|
|
8
|
+
* The constructor function initializes an instance of a class with a key, value, and count.
|
|
9
|
+
* @param {K} key - The key parameter is of type K, which represents the type of the key for the
|
|
10
|
+
* constructor. It is required and must be provided when creating an instance of the class.
|
|
11
|
+
* @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the
|
|
12
|
+
* value associated with the key in the constructor. If no value is provided, it will be `undefined`.
|
|
13
|
+
* @param [count=1] - The "count" parameter is an optional parameter that specifies the number of
|
|
14
|
+
* times the key-value pair should be repeated. If no value is provided for "count", it defaults to
|
|
15
|
+
* 1.
|
|
16
|
+
*/
|
|
17
|
+
constructor(key, value, count = 1) {
|
|
18
|
+
super(key, value);
|
|
19
|
+
this._count = 1;
|
|
20
|
+
this.count = count;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The function returns the value of the private variable _count.
|
|
24
|
+
* @returns The count property of the object, which is of type number.
|
|
25
|
+
*/
|
|
26
|
+
get count() {
|
|
27
|
+
return this._count;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The above function sets the value of the count property.
|
|
31
|
+
* @param {number} value - The value parameter is of type number, which means it can accept any
|
|
32
|
+
* numeric value.
|
|
33
|
+
*/
|
|
34
|
+
set count(value) {
|
|
35
|
+
this._count = value;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
exports.TreeMultiMapNode = TreeMultiMapNode;
|
|
39
|
+
class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
40
|
+
/**
|
|
41
|
+
* The constructor function initializes a new instance of the TreeMultiMap class with optional
|
|
42
|
+
* initial keys, nodes, or entries.
|
|
43
|
+
* @param keysOrNodesOrEntries - The `keysOrNodesOrEntries` parameter is an iterable object that can
|
|
44
|
+
* contain keys, nodes, or entries. It is used to initialize the TreeMultiMap with the provided keys,
|
|
45
|
+
* nodes, or entries.
|
|
46
|
+
* @param [options] - The `options` parameter is an optional object that can be passed to the
|
|
47
|
+
* constructor. It allows you to customize the behavior of the `TreeMultiMap` instance.
|
|
48
|
+
*/
|
|
49
|
+
constructor(keysOrNodesOrEntries = [], options) {
|
|
50
|
+
super([], options);
|
|
51
|
+
this._count = 0;
|
|
52
|
+
if (keysOrNodesOrEntries)
|
|
53
|
+
this.addMany(keysOrNodesOrEntries);
|
|
54
|
+
}
|
|
55
|
+
// TODO the _count is not accurate after nodes count modified
|
|
56
|
+
/**
|
|
57
|
+
* The function calculates the sum of the count property of all nodes in a tree structure.
|
|
58
|
+
* @returns the sum of the count property of all nodes in the tree.
|
|
59
|
+
*/
|
|
60
|
+
get count() {
|
|
61
|
+
let sum = 0;
|
|
62
|
+
this.dfs(node => (sum += node.count));
|
|
63
|
+
return sum;
|
|
64
|
+
// return this._count;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* The function creates a new TreeMultiMapNode object with the specified key, value, and count.
|
|
68
|
+
* @param {K} key - The key parameter represents the key of the node being created. It is of type K,
|
|
69
|
+
* which is a generic type that can be replaced with any specific type when using the function.
|
|
70
|
+
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
71
|
+
* associated with the key in the node. It is of type `V`, which can be any data type.
|
|
72
|
+
* @param {number} [count] - The `count` parameter represents the number of occurrences of a
|
|
73
|
+
* key-value pair in the TreeMultiMap. It is an optional parameter, so if it is not provided, it will
|
|
74
|
+
* default to 1.
|
|
75
|
+
* @returns a new instance of the TreeMultiMapNode class, casted as NODE.
|
|
76
|
+
*/
|
|
77
|
+
createNode(key, value, count) {
|
|
78
|
+
return new TreeMultiMapNode(key, value, count);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* The function creates a new instance of a TreeMultiMap with the specified options and returns it.
|
|
82
|
+
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
83
|
+
* configuration options for creating the `TreeMultiMap`. It can include properties such as
|
|
84
|
+
* `keyComparator`, `valueComparator`, `allowDuplicates`, etc.
|
|
85
|
+
* @returns a new instance of the `TreeMultiMap` class, with the provided options merged with the
|
|
86
|
+
* existing `iterationType` option. The returned value is casted as `TREE`.
|
|
87
|
+
*/
|
|
88
|
+
createTree(options) {
|
|
89
|
+
return new TreeMultiMap([], Object.assign({ iterationType: this.iterationType }, options));
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* The function `keyValueOrEntryToNode` takes a key, value, and count and returns a node if the input
|
|
93
|
+
* is valid.
|
|
94
|
+
* @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be of type `KeyOrNodeOrEntry<K, V,
|
|
95
|
+
* NODE>`. It can accept three types of values:
|
|
96
|
+
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
97
|
+
* value associated with a key in a key-value pair.
|
|
98
|
+
* @param [count=1] - The count parameter is an optional parameter that specifies the number of times
|
|
99
|
+
* the key-value pair should be added to the node. If not provided, it defaults to 1.
|
|
100
|
+
* @returns a NODE object or undefined.
|
|
101
|
+
*/
|
|
102
|
+
keyValueOrEntryToNode(keyOrNodeOrEntry, value, count = 1) {
|
|
103
|
+
let node;
|
|
104
|
+
if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
else if (this.isNode(keyOrNodeOrEntry)) {
|
|
108
|
+
node = keyOrNodeOrEntry;
|
|
109
|
+
}
|
|
110
|
+
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
111
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
112
|
+
if (key === undefined || key === null) {
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
node = this.createNode(key, value, count);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
120
|
+
node = this.createNode(keyOrNodeOrEntry, value, count);
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
return node;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* The function "isNode" checks if a given key, node, or entry is an instance of the TreeMultiMapNode
|
|
129
|
+
* class.
|
|
130
|
+
* @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be of type `KeyOrNodeOrEntry<K, V,
|
|
131
|
+
* NODE>`.
|
|
132
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntry` is an instance
|
|
133
|
+
* of the `TreeMultiMapNode` class.
|
|
134
|
+
*/
|
|
135
|
+
isNode(keyOrNodeOrEntry) {
|
|
136
|
+
return keyOrNodeOrEntry instanceof TreeMultiMapNode;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Time Complexity: O(log n)
|
|
140
|
+
* Space Complexity: O(1)
|
|
141
|
+
*/
|
|
142
|
+
/**
|
|
143
|
+
* Time Complexity: O(log n)
|
|
144
|
+
* Space Complexity: O(1)
|
|
145
|
+
*
|
|
146
|
+
* The function overrides the add method in TypeScript and adds a new node to the data structure.
|
|
147
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can accept three types of values:
|
|
148
|
+
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
149
|
+
* data structure.
|
|
150
|
+
* @param [count=1] - The `count` parameter represents the number of times the key-value pair should
|
|
151
|
+
* be added to the data structure. By default, it is set to 1, meaning that the key-value pair will
|
|
152
|
+
* be added once. However, you can specify a different value for `count` if you want to add
|
|
153
|
+
* @returns a boolean value.
|
|
154
|
+
*/
|
|
155
|
+
add(keyOrNodeOrEntry, value, count = 1) {
|
|
156
|
+
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value, count);
|
|
157
|
+
if (newNode === undefined)
|
|
158
|
+
return false;
|
|
159
|
+
const orgNodeCount = (newNode === null || newNode === void 0 ? void 0 : newNode.count) || 0;
|
|
160
|
+
const inserted = super.add(newNode);
|
|
161
|
+
if (inserted) {
|
|
162
|
+
this._count += orgNodeCount;
|
|
163
|
+
}
|
|
164
|
+
return true;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Time Complexity: O(log n)
|
|
168
|
+
* Space Complexity: O(1)
|
|
169
|
+
*/
|
|
170
|
+
/**
|
|
171
|
+
* Time Complexity: O(log n)
|
|
172
|
+
* Space Complexity: O(1)
|
|
173
|
+
*
|
|
174
|
+
* The `delete` function in a TypeScript class is used to delete nodes from a binary tree based on a
|
|
175
|
+
* given identifier, and it returns an array of results containing information about the deleted
|
|
176
|
+
* nodes.
|
|
177
|
+
* @param {ReturnType<C> | null | undefined} identifier - The identifier parameter is the value used
|
|
178
|
+
* to identify the node to be deleted. It can be of any type that is returned by the callback
|
|
179
|
+
* function. It can also be null or undefined if no node needs to be deleted.
|
|
180
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as
|
|
181
|
+
* input and returns a value of type `ReturnType<C>`. It is used to determine if a node matches the
|
|
182
|
+
* identifier for deletion. If no callback is provided, the `_defaultOneParamCallback` function is
|
|
183
|
+
* used
|
|
184
|
+
* @param [ignoreCount=false] - A boolean value indicating whether to ignore the count of the target
|
|
185
|
+
* node when performing deletion. If set to true, the count of the target node will not be considered
|
|
186
|
+
* and the node will be deleted regardless of its count. If set to false (default), the count of the
|
|
187
|
+
* target node will be decremented
|
|
188
|
+
* @returns an array of BinaryTreeDeleteResult<NODE> objects.
|
|
189
|
+
*/
|
|
190
|
+
delete(identifier, callback = this._defaultOneParamCallback, ignoreCount = false) {
|
|
191
|
+
const deleteResults = [];
|
|
192
|
+
if (identifier === null)
|
|
193
|
+
return deleteResults;
|
|
194
|
+
// Helper function to perform deletion
|
|
195
|
+
const deleteHelper = (node) => {
|
|
196
|
+
// Initialize targetNode to the sentinel node
|
|
197
|
+
let targetNode = this._Sentinel;
|
|
198
|
+
let currentNode;
|
|
199
|
+
// Find the node to be deleted based on the identifier
|
|
200
|
+
while (node !== this._Sentinel) {
|
|
201
|
+
// Update targetNode if the current node matches the identifier
|
|
202
|
+
if (node && callback(node) === identifier) {
|
|
203
|
+
targetNode = node;
|
|
204
|
+
}
|
|
205
|
+
// Move to the right or left based on the comparison with the identifier
|
|
206
|
+
if (node && identifier && callback(node) <= identifier) {
|
|
207
|
+
node = node.right;
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
node = node === null || node === void 0 ? void 0 : node.left;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
// If the target node is not found, decrement size and return
|
|
214
|
+
if (targetNode === this._Sentinel) {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
if (ignoreCount || targetNode.count <= 1) {
|
|
218
|
+
// Store the parent of the target node and its original color
|
|
219
|
+
let parentNode = targetNode;
|
|
220
|
+
let parentNodeOriginalColor = parentNode.color;
|
|
221
|
+
// Handle deletion based on the number of children of the target node
|
|
222
|
+
if (targetNode.left === this._Sentinel) {
|
|
223
|
+
// Target node has no left child - deletion case 1
|
|
224
|
+
currentNode = targetNode.right;
|
|
225
|
+
this._rbTransplant(targetNode, targetNode.right);
|
|
226
|
+
}
|
|
227
|
+
else if (targetNode.right === this._Sentinel) {
|
|
228
|
+
// Target node has no right child - deletion case 2
|
|
229
|
+
currentNode = targetNode.left;
|
|
230
|
+
this._rbTransplant(targetNode, targetNode.left);
|
|
231
|
+
}
|
|
232
|
+
else {
|
|
233
|
+
// Target node has both left and right children - deletion case 3
|
|
234
|
+
parentNode = this.getLeftMost(targetNode.right);
|
|
235
|
+
parentNodeOriginalColor = parentNode.color;
|
|
236
|
+
currentNode = parentNode.right;
|
|
237
|
+
if (parentNode.parent === targetNode) {
|
|
238
|
+
// Target node's right child becomes its parent's left child
|
|
239
|
+
currentNode.parent = parentNode;
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
// Replace parentNode with its right child and update connections
|
|
243
|
+
this._rbTransplant(parentNode, parentNode.right);
|
|
244
|
+
parentNode.right = targetNode.right;
|
|
245
|
+
parentNode.right.parent = parentNode;
|
|
246
|
+
}
|
|
247
|
+
// Replace the target node with its in-order successor
|
|
248
|
+
this._rbTransplant(targetNode, parentNode);
|
|
249
|
+
parentNode.left = targetNode.left;
|
|
250
|
+
parentNode.left.parent = parentNode;
|
|
251
|
+
parentNode.color = targetNode.color;
|
|
252
|
+
}
|
|
253
|
+
// Fix the Red-Black Tree properties after deletion
|
|
254
|
+
if (parentNodeOriginalColor === types_1.RBTNColor.BLACK) {
|
|
255
|
+
this._fixDelete(currentNode);
|
|
256
|
+
}
|
|
257
|
+
// Decrement the size and store information about the deleted node
|
|
258
|
+
this._size--;
|
|
259
|
+
this._count -= targetNode.count;
|
|
260
|
+
deleteResults.push({ deleted: targetNode, needBalanced: undefined });
|
|
261
|
+
}
|
|
262
|
+
else {
|
|
263
|
+
targetNode.count--;
|
|
264
|
+
this._count--;
|
|
265
|
+
}
|
|
266
|
+
};
|
|
267
|
+
// Call the helper function with the root of the tree
|
|
268
|
+
deleteHelper(this.root);
|
|
269
|
+
// Return the result array
|
|
270
|
+
return deleteResults;
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Time Complexity: O(1)
|
|
274
|
+
* Space Complexity: O(1)
|
|
275
|
+
*/
|
|
276
|
+
/**
|
|
277
|
+
* Time Complexity: O(1)
|
|
278
|
+
* Space Complexity: O(1)
|
|
279
|
+
*
|
|
280
|
+
* The "clear" function overrides the parent class's "clear" function and also resets the count to
|
|
281
|
+
* zero.
|
|
282
|
+
*/
|
|
283
|
+
clear() {
|
|
284
|
+
super.clear();
|
|
285
|
+
this._count = 0;
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Time Complexity: O(n log n)
|
|
289
|
+
* Space Complexity: O(log n)
|
|
290
|
+
*/
|
|
291
|
+
/**
|
|
292
|
+
* Time Complexity: O(n log n)
|
|
293
|
+
* Space Complexity: O(log n)
|
|
294
|
+
*
|
|
295
|
+
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
|
|
296
|
+
* tree using either a recursive or iterative approach.
|
|
297
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
298
|
+
* type of iteration to use when building the balanced binary search tree. It can have two possible
|
|
299
|
+
* values:
|
|
300
|
+
* @returns a boolean value.
|
|
301
|
+
*/
|
|
302
|
+
perfectlyBalance(iterationType = this.iterationType) {
|
|
303
|
+
const sorted = this.dfs(node => node, 'in'), n = sorted.length;
|
|
304
|
+
if (sorted.length < 1)
|
|
305
|
+
return false;
|
|
306
|
+
this.clear();
|
|
307
|
+
if (iterationType === types_1.IterationType.RECURSIVE) {
|
|
308
|
+
const buildBalanceBST = (l, r) => {
|
|
309
|
+
if (l > r)
|
|
310
|
+
return;
|
|
311
|
+
const m = l + Math.floor((r - l) / 2);
|
|
312
|
+
const midNode = sorted[m];
|
|
313
|
+
this.add(midNode.key, midNode.value, midNode.count);
|
|
314
|
+
buildBalanceBST(l, m - 1);
|
|
315
|
+
buildBalanceBST(m + 1, r);
|
|
316
|
+
};
|
|
317
|
+
buildBalanceBST(0, n - 1);
|
|
318
|
+
return true;
|
|
319
|
+
}
|
|
320
|
+
else {
|
|
321
|
+
const stack = [[0, n - 1]];
|
|
322
|
+
while (stack.length > 0) {
|
|
323
|
+
const popped = stack.pop();
|
|
324
|
+
if (popped) {
|
|
325
|
+
const [l, r] = popped;
|
|
326
|
+
if (l <= r) {
|
|
327
|
+
const m = l + Math.floor((r - l) / 2);
|
|
328
|
+
const midNode = sorted[m];
|
|
329
|
+
this.add(midNode.key, midNode.value, midNode.count);
|
|
330
|
+
stack.push([m + 1, r]);
|
|
331
|
+
stack.push([l, m - 1]);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
return true;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Time complexity: O(n)
|
|
340
|
+
* Space complexity: O(n)
|
|
341
|
+
*/
|
|
342
|
+
/**
|
|
343
|
+
* Time complexity: O(n)
|
|
344
|
+
* Space complexity: O(n)
|
|
345
|
+
*
|
|
346
|
+
* The function overrides the clone method to create a deep copy of a tree object.
|
|
347
|
+
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
348
|
+
*/
|
|
349
|
+
clone() {
|
|
350
|
+
const cloned = this.createTree();
|
|
351
|
+
this.bfs(node => cloned.add(node.key, node.value, node.count));
|
|
352
|
+
return cloned;
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* The function swaps the properties of two nodes in a binary search tree.
|
|
356
|
+
* @param srcNode - The source node that needs to be swapped with the destination node. It can be
|
|
357
|
+
* either a key or a node object.
|
|
358
|
+
* @param destNode - The `destNode` parameter is the node in the binary search tree where the
|
|
359
|
+
* properties will be swapped with the `srcNode`.
|
|
360
|
+
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
361
|
+
* If both `srcNode` and `destNode` are valid nodes, the method swaps their `key`, `value`, `count`,
|
|
362
|
+
* and `color` properties. If the swapping is successful, the method returns the modified `destNode`.
|
|
363
|
+
* If either `srcNode` or `destNode` is
|
|
364
|
+
*/
|
|
365
|
+
_swapProperties(srcNode, destNode) {
|
|
366
|
+
srcNode = this.ensureNode(srcNode);
|
|
367
|
+
destNode = this.ensureNode(destNode);
|
|
368
|
+
if (srcNode && destNode) {
|
|
369
|
+
const { key, value, count, color } = destNode;
|
|
370
|
+
const tempNode = this.createNode(key, value, count);
|
|
371
|
+
if (tempNode) {
|
|
372
|
+
tempNode.color = color;
|
|
373
|
+
destNode.key = srcNode.key;
|
|
374
|
+
destNode.value = srcNode.value;
|
|
375
|
+
destNode.count = srcNode.count;
|
|
376
|
+
destNode.color = srcNode.color;
|
|
377
|
+
srcNode.key = tempNode.key;
|
|
378
|
+
srcNode.value = tempNode.value;
|
|
379
|
+
srcNode.count = tempNode.count;
|
|
380
|
+
srcNode.color = tempNode.color;
|
|
381
|
+
}
|
|
382
|
+
return destNode;
|
|
383
|
+
}
|
|
384
|
+
return undefined;
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* The function replaces an old node with a new node and updates the count property of the new node.
|
|
388
|
+
* @param {NODE} oldNode - The `oldNode` parameter is of type `NODE` and represents the node that
|
|
389
|
+
* needs to be replaced in the data structure.
|
|
390
|
+
* @param {NODE} newNode - The `newNode` parameter is an object of type `NODE`.
|
|
391
|
+
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
392
|
+
* superclass, after updating the `count` property of the `newNode` object.
|
|
393
|
+
*/
|
|
394
|
+
_replaceNode(oldNode, newNode) {
|
|
395
|
+
newNode.count = oldNode.count + newNode.count;
|
|
396
|
+
return super._replaceNode(oldNode, newNode);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
exports.TreeMultiMap = TreeMultiMap;
|
|
@@ -362,84 +362,6 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
362
362
|
costs: number[][];
|
|
363
363
|
predecessor: (VO | undefined)[][];
|
|
364
364
|
};
|
|
365
|
-
/**
|
|
366
|
-
* Time Complexity: O(V + E) - Linear time (Tarjan's algorithm).
|
|
367
|
-
* Space Complexity: O(V) - Linear space (Tarjan's algorithm).
|
|
368
|
-
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
369
|
-
* Tarjan can find cycles in directed or undirected graph
|
|
370
|
-
* Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time,
|
|
371
|
-
* Tarjan solve the bi-connected components of undirected graphs;
|
|
372
|
-
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
373
|
-
* /
|
|
374
|
-
|
|
375
|
-
/**
|
|
376
|
-
* Time Complexity: O(V + E) - Linear time (Tarjan's algorithm).
|
|
377
|
-
* Space Complexity: O(V) - Linear space (Tarjan's algorithm).
|
|
378
|
-
*
|
|
379
|
-
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
380
|
-
* Tarjan can find cycles in directed or undirected graph
|
|
381
|
-
* Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time,
|
|
382
|
-
* Tarjan solve the bi-connected components of undirected graphs;
|
|
383
|
-
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
384
|
-
* The `tarjan` function is used to perform various graph analysis tasks such as finding articulation points, bridges,
|
|
385
|
-
* strongly connected components (SCCs), and cycles in a graph.
|
|
386
|
-
* @param {boolean} [needCutVertexes] - A boolean value indicating whether or not to calculate and return the
|
|
387
|
-
* articulation points in the graph. Articulation points are the vertexMap in a graph whose removal would increase the
|
|
388
|
-
* number of connected components in the graph.
|
|
389
|
-
* @param {boolean} [needBridges] - A boolean flag indicating whether the algorithm should find and return the bridges
|
|
390
|
-
* (edgeMap whose removal would increase the number of connected components in the graph).
|
|
391
|
-
* @param {boolean} [needSCCs] - A boolean value indicating whether the Strongly Connected Components (SCCs) of the
|
|
392
|
-
* graph are needed. If set to true, the function will calculate and return the SCCs of the graph. If set to false, the
|
|
393
|
-
* SCCs will not be calculated or returned.
|
|
394
|
-
* @param {boolean} [needCycles] - A boolean flag indicating whether the algorithm should find cycles in the graph. If
|
|
395
|
-
* set to true, the algorithm will return a map of cycles, where the keys are the low values of the SCCs and the values
|
|
396
|
-
* are arrays of vertexMap that form cycles within the SCCs.
|
|
397
|
-
* @returns The function `tarjan` returns an object with the following properties:
|
|
398
|
-
*/
|
|
399
|
-
tarjan(needCutVertexes?: boolean, needBridges?: boolean, needSCCs?: boolean, needCycles?: boolean): {
|
|
400
|
-
dfnMap: Map<VO, number>;
|
|
401
|
-
lowMap: Map<VO, number>;
|
|
402
|
-
bridges: EO[];
|
|
403
|
-
cutVertexes: VO[];
|
|
404
|
-
SCCs: Map<number, VO[]>;
|
|
405
|
-
cycles: Map<number, VO[]>;
|
|
406
|
-
};
|
|
407
|
-
/**
|
|
408
|
-
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
|
|
409
|
-
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
|
|
410
|
-
*/
|
|
411
|
-
/**
|
|
412
|
-
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
|
|
413
|
-
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
|
|
414
|
-
*
|
|
415
|
-
* The function returns a map that associates each vertex object with its corresponding depth-first
|
|
416
|
-
* number.
|
|
417
|
-
* @returns A Map object with keys of type VO and values of type number.
|
|
418
|
-
*/
|
|
419
|
-
getDFNMap(): Map<VO, number>;
|
|
420
|
-
/**
|
|
421
|
-
* The function returns a Map object that contains the low values of each vertex in a Tarjan
|
|
422
|
-
* algorithm.
|
|
423
|
-
* @returns The method `getLowMap()` is returning a `Map` object with keys of type `VO` and values of
|
|
424
|
-
* type `number`.
|
|
425
|
-
*/
|
|
426
|
-
getLowMap(): Map<VO, number>;
|
|
427
|
-
/**
|
|
428
|
-
* The function "getCutVertexes" returns an array of cut vertexes using the Tarjan algorithm.
|
|
429
|
-
* @returns an array of VO objects, specifically the cut vertexes.
|
|
430
|
-
*/
|
|
431
|
-
getCutVertexes(): VO[];
|
|
432
|
-
/**
|
|
433
|
-
* The function "getSCCs" returns a map of strongly connected components (SCCs) using the Tarjan
|
|
434
|
-
* algorithm.
|
|
435
|
-
* @returns a map where the keys are numbers and the values are arrays of VO objects.
|
|
436
|
-
*/
|
|
437
|
-
getSCCs(): Map<number, VO[]>;
|
|
438
|
-
/**
|
|
439
|
-
* The function "getBridges" returns an array of bridges using the Tarjan algorithm.
|
|
440
|
-
* @returns the bridges found using the Tarjan algorithm.
|
|
441
|
-
*/
|
|
442
|
-
getBridges(): EO[];
|
|
443
365
|
/**
|
|
444
366
|
* O(V+E+C)
|
|
445
367
|
* O(V+C)
|
|
@@ -820,195 +820,6 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
820
820
|
}
|
|
821
821
|
return { costs, predecessor };
|
|
822
822
|
}
|
|
823
|
-
/**
|
|
824
|
-
* Time Complexity: O(V + E) - Linear time (Tarjan's algorithm).
|
|
825
|
-
* Space Complexity: O(V) - Linear space (Tarjan's algorithm).
|
|
826
|
-
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
827
|
-
* Tarjan can find cycles in directed or undirected graph
|
|
828
|
-
* Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time,
|
|
829
|
-
* Tarjan solve the bi-connected components of undirected graphs;
|
|
830
|
-
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
831
|
-
* /
|
|
832
|
-
|
|
833
|
-
/**
|
|
834
|
-
* Time Complexity: O(V + E) - Linear time (Tarjan's algorithm).
|
|
835
|
-
* Space Complexity: O(V) - Linear space (Tarjan's algorithm).
|
|
836
|
-
*
|
|
837
|
-
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
838
|
-
* Tarjan can find cycles in directed or undirected graph
|
|
839
|
-
* Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time,
|
|
840
|
-
* Tarjan solve the bi-connected components of undirected graphs;
|
|
841
|
-
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
842
|
-
* The `tarjan` function is used to perform various graph analysis tasks such as finding articulation points, bridges,
|
|
843
|
-
* strongly connected components (SCCs), and cycles in a graph.
|
|
844
|
-
* @param {boolean} [needCutVertexes] - A boolean value indicating whether or not to calculate and return the
|
|
845
|
-
* articulation points in the graph. Articulation points are the vertexMap in a graph whose removal would increase the
|
|
846
|
-
* number of connected components in the graph.
|
|
847
|
-
* @param {boolean} [needBridges] - A boolean flag indicating whether the algorithm should find and return the bridges
|
|
848
|
-
* (edgeMap whose removal would increase the number of connected components in the graph).
|
|
849
|
-
* @param {boolean} [needSCCs] - A boolean value indicating whether the Strongly Connected Components (SCCs) of the
|
|
850
|
-
* graph are needed. If set to true, the function will calculate and return the SCCs of the graph. If set to false, the
|
|
851
|
-
* SCCs will not be calculated or returned.
|
|
852
|
-
* @param {boolean} [needCycles] - A boolean flag indicating whether the algorithm should find cycles in the graph. If
|
|
853
|
-
* set to true, the algorithm will return a map of cycles, where the keys are the low values of the SCCs and the values
|
|
854
|
-
* are arrays of vertexMap that form cycles within the SCCs.
|
|
855
|
-
* @returns The function `tarjan` returns an object with the following properties:
|
|
856
|
-
*/
|
|
857
|
-
tarjan(needCutVertexes = false, needBridges = false, needSCCs = true, needCycles = false) {
|
|
858
|
-
// !! in undirected graph we will not let child visit parent when dfs
|
|
859
|
-
// !! articulation point(in dfs search tree not in graph): (cur !== root && cur.has(child)) && (low(child) >= dfn(cur)) || (cur === root && cur.children() >= 2)
|
|
860
|
-
// !! bridge: low(child) > dfn(cur)
|
|
861
|
-
const defaultConfig = false;
|
|
862
|
-
if (needCutVertexes === undefined)
|
|
863
|
-
needCutVertexes = defaultConfig;
|
|
864
|
-
if (needBridges === undefined)
|
|
865
|
-
needBridges = defaultConfig;
|
|
866
|
-
if (needSCCs === undefined)
|
|
867
|
-
needSCCs = defaultConfig;
|
|
868
|
-
if (needCycles === undefined)
|
|
869
|
-
needCycles = defaultConfig;
|
|
870
|
-
const dfnMap = new Map();
|
|
871
|
-
const lowMap = new Map();
|
|
872
|
-
const vertexMap = this._vertexMap;
|
|
873
|
-
vertexMap.forEach(v => {
|
|
874
|
-
dfnMap.set(v, -1);
|
|
875
|
-
lowMap.set(v, Infinity);
|
|
876
|
-
});
|
|
877
|
-
const [root] = vertexMap.values();
|
|
878
|
-
const cutVertexes = [];
|
|
879
|
-
const bridges = [];
|
|
880
|
-
let dfn = 0;
|
|
881
|
-
const dfs = (cur, parent) => {
|
|
882
|
-
dfn++;
|
|
883
|
-
dfnMap.set(cur, dfn);
|
|
884
|
-
lowMap.set(cur, dfn);
|
|
885
|
-
const neighbors = this.getNeighbors(cur);
|
|
886
|
-
let childCount = 0; // child in dfs tree not child in graph
|
|
887
|
-
for (const neighbor of neighbors) {
|
|
888
|
-
if (neighbor !== parent) {
|
|
889
|
-
if (dfnMap.get(neighbor) === -1) {
|
|
890
|
-
childCount++;
|
|
891
|
-
dfs(neighbor, cur);
|
|
892
|
-
}
|
|
893
|
-
const childLow = lowMap.get(neighbor);
|
|
894
|
-
const curLow = lowMap.get(cur);
|
|
895
|
-
// TODO after no-non-undefined-assertion not ensure the logic
|
|
896
|
-
if (curLow !== undefined && childLow !== undefined) {
|
|
897
|
-
lowMap.set(cur, Math.min(curLow, childLow));
|
|
898
|
-
}
|
|
899
|
-
const curFromMap = dfnMap.get(cur);
|
|
900
|
-
if (childLow !== undefined && curFromMap !== undefined) {
|
|
901
|
-
if (needCutVertexes) {
|
|
902
|
-
if ((cur === root && childCount >= 2) || (cur !== root && childLow >= curFromMap)) {
|
|
903
|
-
// todo not ensure the logic if (cur === root && childCount >= 2 || ((cur !== root) && (childLow >= curFromMap))) {
|
|
904
|
-
cutVertexes.push(cur);
|
|
905
|
-
}
|
|
906
|
-
}
|
|
907
|
-
if (needBridges) {
|
|
908
|
-
if (childLow > curFromMap) {
|
|
909
|
-
const edgeCurToNeighbor = this.getEdge(cur, neighbor);
|
|
910
|
-
if (edgeCurToNeighbor) {
|
|
911
|
-
bridges.push(edgeCurToNeighbor);
|
|
912
|
-
}
|
|
913
|
-
}
|
|
914
|
-
}
|
|
915
|
-
}
|
|
916
|
-
}
|
|
917
|
-
}
|
|
918
|
-
};
|
|
919
|
-
dfs(root, undefined);
|
|
920
|
-
let SCCs = new Map();
|
|
921
|
-
const getSCCs = () => {
|
|
922
|
-
const SCCs = new Map();
|
|
923
|
-
lowMap.forEach((low, vertex) => {
|
|
924
|
-
var _a;
|
|
925
|
-
if (!SCCs.has(low)) {
|
|
926
|
-
SCCs.set(low, [vertex]);
|
|
927
|
-
}
|
|
928
|
-
else {
|
|
929
|
-
(_a = SCCs.get(low)) === null || _a === void 0 ? void 0 : _a.push(vertex);
|
|
930
|
-
}
|
|
931
|
-
});
|
|
932
|
-
return SCCs;
|
|
933
|
-
};
|
|
934
|
-
if (needSCCs) {
|
|
935
|
-
SCCs = getSCCs();
|
|
936
|
-
}
|
|
937
|
-
const cycles = new Map();
|
|
938
|
-
if (needCycles) {
|
|
939
|
-
const visitedMap = new Map();
|
|
940
|
-
const stack = [];
|
|
941
|
-
const findCyclesDFS = (cur, parent) => {
|
|
942
|
-
visitedMap.set(cur, true);
|
|
943
|
-
stack.push(cur);
|
|
944
|
-
const neighbors = this.getNeighbors(cur);
|
|
945
|
-
for (const neighbor of neighbors) {
|
|
946
|
-
if (!visitedMap.get(neighbor)) {
|
|
947
|
-
findCyclesDFS(neighbor, cur);
|
|
948
|
-
}
|
|
949
|
-
else if (stack.includes(neighbor) && neighbor !== parent) {
|
|
950
|
-
const cycleStartIndex = stack.indexOf(neighbor);
|
|
951
|
-
const cycle = stack.slice(cycleStartIndex);
|
|
952
|
-
const cycleLow = Math.min(...cycle.map(v => dfnMap.get(v) || Infinity));
|
|
953
|
-
cycles.set(cycleLow, cycle);
|
|
954
|
-
}
|
|
955
|
-
}
|
|
956
|
-
stack.pop();
|
|
957
|
-
};
|
|
958
|
-
vertexMap.forEach(v => {
|
|
959
|
-
if (!visitedMap.get(v)) {
|
|
960
|
-
findCyclesDFS(v, undefined);
|
|
961
|
-
}
|
|
962
|
-
});
|
|
963
|
-
}
|
|
964
|
-
return { dfnMap, lowMap, bridges, cutVertexes, SCCs, cycles };
|
|
965
|
-
}
|
|
966
|
-
/**
|
|
967
|
-
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
|
|
968
|
-
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
|
|
969
|
-
*/
|
|
970
|
-
/**
|
|
971
|
-
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
|
|
972
|
-
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
|
|
973
|
-
*
|
|
974
|
-
* The function returns a map that associates each vertex object with its corresponding depth-first
|
|
975
|
-
* number.
|
|
976
|
-
* @returns A Map object with keys of type VO and values of type number.
|
|
977
|
-
*/
|
|
978
|
-
getDFNMap() {
|
|
979
|
-
return this.tarjan(false, false, false, false).dfnMap;
|
|
980
|
-
}
|
|
981
|
-
/**
|
|
982
|
-
* The function returns a Map object that contains the low values of each vertex in a Tarjan
|
|
983
|
-
* algorithm.
|
|
984
|
-
* @returns The method `getLowMap()` is returning a `Map` object with keys of type `VO` and values of
|
|
985
|
-
* type `number`.
|
|
986
|
-
*/
|
|
987
|
-
getLowMap() {
|
|
988
|
-
return this.tarjan(false, false, false, false).lowMap;
|
|
989
|
-
}
|
|
990
|
-
/**
|
|
991
|
-
* The function "getCutVertexes" returns an array of cut vertexes using the Tarjan algorithm.
|
|
992
|
-
* @returns an array of VO objects, specifically the cut vertexes.
|
|
993
|
-
*/
|
|
994
|
-
getCutVertexes() {
|
|
995
|
-
return this.tarjan(true, false, false, false).cutVertexes;
|
|
996
|
-
}
|
|
997
|
-
/**
|
|
998
|
-
* The function "getSCCs" returns a map of strongly connected components (SCCs) using the Tarjan
|
|
999
|
-
* algorithm.
|
|
1000
|
-
* @returns a map where the keys are numbers and the values are arrays of VO objects.
|
|
1001
|
-
*/
|
|
1002
|
-
getSCCs() {
|
|
1003
|
-
return this.tarjan(false, false, true, false).SCCs;
|
|
1004
|
-
}
|
|
1005
|
-
/**
|
|
1006
|
-
* The function "getBridges" returns an array of bridges using the Tarjan algorithm.
|
|
1007
|
-
* @returns the bridges found using the Tarjan algorithm.
|
|
1008
|
-
*/
|
|
1009
|
-
getBridges() {
|
|
1010
|
-
return this.tarjan(false, true, false, false).bridges;
|
|
1011
|
-
}
|
|
1012
823
|
/**
|
|
1013
824
|
* O(V+E+C)
|
|
1014
825
|
* O(V+C)
|