min-heap-typed 1.50.3 → 1.50.5
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 +10 -8
- package/dist/data-structures/base/iterable-base.js +8 -12
- package/dist/data-structures/binary-tree/{tree-multimap.d.ts → avl-tree-multi-map.d.ts} +11 -11
- package/dist/data-structures/binary-tree/{tree-multimap.js → avl-tree-multi-map.js} +14 -14
- package/dist/data-structures/binary-tree/bst.js +5 -7
- 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.js +17 -9
- 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 +1 -0
- package/dist/data-structures/graph/abstract-graph.js +3 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +14 -76
- package/dist/data-structures/linked-list/doubly-linked-list.js +16 -86
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +27 -69
- package/dist/data-structures/linked-list/singly-linked-list.js +35 -79
- package/dist/data-structures/queue/deque.d.ts +0 -53
- package/dist/data-structures/queue/deque.js +0 -61
- package/dist/data-structures/queue/queue.d.ts +0 -70
- package/dist/data-structures/queue/queue.js +0 -87
- 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 +14 -10
- package/src/data-structures/binary-tree/{tree-multimap.ts → avl-tree-multi-map.ts} +20 -20
- package/src/data-structures/binary-tree/bst.ts +5 -6
- package/src/data-structures/binary-tree/index.ts +2 -1
- package/src/data-structures/binary-tree/rb-tree.ts +20 -10
- package/src/data-structures/binary-tree/tree-multi-map.ts +463 -0
- package/src/data-structures/graph/abstract-graph.ts +4 -0
- package/src/data-structures/heap/heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +16 -94
- package/src/data-structures/linked-list/singly-linked-list.ts +35 -87
- package/src/data-structures/queue/deque.ts +0 -67
- package/src/data-structures/queue/queue.ts +0 -98
- 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;
|
|
@@ -41,6 +41,7 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
41
41
|
protected _vertexMap: Map<VertexKey, VO>;
|
|
42
42
|
get vertexMap(): Map<VertexKey, VO>;
|
|
43
43
|
set vertexMap(v: Map<VertexKey, VO>);
|
|
44
|
+
get size(): number;
|
|
44
45
|
/**
|
|
45
46
|
* In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
|
|
46
47
|
* This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
|
|
@@ -50,6 +50,9 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
50
50
|
set vertexMap(v) {
|
|
51
51
|
this._vertexMap = v;
|
|
52
52
|
}
|
|
53
|
+
get size() {
|
|
54
|
+
return this._vertexMap.size;
|
|
55
|
+
}
|
|
53
56
|
/**
|
|
54
57
|
* Time Complexity: O(1) - Constant time for Map lookup.
|
|
55
58
|
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
@@ -131,24 +131,19 @@ export declare class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
131
131
|
* Space Complexity: O(1)
|
|
132
132
|
*/
|
|
133
133
|
/**
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
* The push
|
|
138
|
-
* @param {E} value - The value to be added to the linked list.
|
|
134
|
+
* The push function adds a new element to the end of a doubly linked list.
|
|
135
|
+
* @param {E} element - The "element" parameter represents the value that you want to add to the
|
|
136
|
+
* doubly linked list.
|
|
137
|
+
* @returns The `push` method is returning a boolean value, `true`.
|
|
139
138
|
*/
|
|
140
|
-
push(
|
|
139
|
+
push(element: E): boolean;
|
|
141
140
|
/**
|
|
142
141
|
* Time Complexity: O(1)
|
|
143
142
|
* Space Complexity: O(1)
|
|
144
143
|
*/
|
|
145
144
|
/**
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
*
|
|
149
|
-
* The `pop()` function removes and returns the value of the last node in a doubly linked list.
|
|
150
|
-
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
|
151
|
-
* list is empty, it returns undefined.
|
|
145
|
+
* The `pop()` function removes and returns the value of the last element in a linked list.
|
|
146
|
+
* @returns The method is returning the value of the removed node.
|
|
152
147
|
*/
|
|
153
148
|
pop(): E | undefined;
|
|
154
149
|
/**
|
|
@@ -156,12 +151,8 @@ export declare class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
156
151
|
* Space Complexity: O(1)
|
|
157
152
|
*/
|
|
158
153
|
/**
|
|
159
|
-
*
|
|
160
|
-
*
|
|
161
|
-
*
|
|
162
|
-
* The `shift()` function removes and returns the value of the first node in a doubly linked list.
|
|
163
|
-
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
|
|
164
|
-
* list.
|
|
154
|
+
* The `shift()` function removes and returns the value of the first element in a doubly linked list.
|
|
155
|
+
* @returns The value of the removed node.
|
|
165
156
|
*/
|
|
166
157
|
shift(): E | undefined;
|
|
167
158
|
/**
|
|
@@ -169,14 +160,12 @@ export declare class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
169
160
|
* Space Complexity: O(1)
|
|
170
161
|
*/
|
|
171
162
|
/**
|
|
172
|
-
*
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
* The unshift
|
|
176
|
-
* @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
|
|
177
|
-
* doubly linked list.
|
|
163
|
+
* The unshift function adds a new element to the beginning of a doubly linked list.
|
|
164
|
+
* @param {E} element - The "element" parameter represents the value of the element that you want to
|
|
165
|
+
* add to the beginning of the doubly linked list.
|
|
166
|
+
* @returns The `unshift` method is returning a boolean value, `true`.
|
|
178
167
|
*/
|
|
179
|
-
unshift(
|
|
168
|
+
unshift(element: E): boolean;
|
|
180
169
|
/**
|
|
181
170
|
* Time Complexity: O(n)
|
|
182
171
|
* Space Complexity: O(1)
|
|
@@ -449,57 +438,6 @@ export declare class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
449
438
|
* object.
|
|
450
439
|
*/
|
|
451
440
|
map<T>(callback: ElementCallback<E, T>, thisArg?: any): DoublyLinkedList<T>;
|
|
452
|
-
/**
|
|
453
|
-
* Time Complexity: O(1)
|
|
454
|
-
* Space Complexity: O(1)
|
|
455
|
-
*/
|
|
456
|
-
/**
|
|
457
|
-
* Time Complexity: O(1)
|
|
458
|
-
* Space Complexity: O(1)
|
|
459
|
-
*
|
|
460
|
-
* The addLast function adds a new node with the given value to the end of the doubly linked list.
|
|
461
|
-
* @param {E} value - The value to be added to the linked list.
|
|
462
|
-
*/
|
|
463
|
-
addLast(value: E): boolean;
|
|
464
|
-
/**
|
|
465
|
-
* Time Complexity: O(1)
|
|
466
|
-
* Space Complexity: O(1)
|
|
467
|
-
*/
|
|
468
|
-
/**
|
|
469
|
-
* Time Complexity: O(1)
|
|
470
|
-
* Space Complexity: O(1)
|
|
471
|
-
*
|
|
472
|
-
* The `pollLast()` function removes and returns the value of the last node in a doubly linked list.
|
|
473
|
-
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
|
474
|
-
* list is empty, it returns undefined.
|
|
475
|
-
*/
|
|
476
|
-
pollLast(): E | undefined;
|
|
477
|
-
/**
|
|
478
|
-
* Time Complexity: O(1)
|
|
479
|
-
* Space Complexity: O(1)
|
|
480
|
-
*/
|
|
481
|
-
/**
|
|
482
|
-
* Time Complexity: O(1)
|
|
483
|
-
* Space Complexity: O(1)
|
|
484
|
-
*
|
|
485
|
-
* The `pollFirst()` function removes and returns the value of the first node in a doubly linked list.
|
|
486
|
-
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
|
|
487
|
-
* list.
|
|
488
|
-
*/
|
|
489
|
-
pollFirst(): E | undefined;
|
|
490
|
-
/**
|
|
491
|
-
* Time Complexity: O(1)
|
|
492
|
-
* Space Complexity: O(1)
|
|
493
|
-
*/
|
|
494
|
-
/**
|
|
495
|
-
* Time Complexity: O(1)
|
|
496
|
-
* Space Complexity: O(1)
|
|
497
|
-
*
|
|
498
|
-
* The addFirst function adds a new node with the given value to the beginning of a doubly linked list.
|
|
499
|
-
* @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
|
|
500
|
-
* doubly linked list.
|
|
501
|
-
*/
|
|
502
|
-
addFirst(value: E): void;
|
|
503
441
|
/**
|
|
504
442
|
* The function returns an iterator that iterates over the values of a linked list.
|
|
505
443
|
*/
|
|
@@ -161,14 +161,13 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
161
161
|
* Space Complexity: O(1)
|
|
162
162
|
*/
|
|
163
163
|
/**
|
|
164
|
-
*
|
|
165
|
-
*
|
|
166
|
-
*
|
|
167
|
-
* The push
|
|
168
|
-
* @param {E} value - The value to be added to the linked list.
|
|
164
|
+
* The push function adds a new element to the end of a doubly linked list.
|
|
165
|
+
* @param {E} element - The "element" parameter represents the value that you want to add to the
|
|
166
|
+
* doubly linked list.
|
|
167
|
+
* @returns The `push` method is returning a boolean value, `true`.
|
|
169
168
|
*/
|
|
170
|
-
push(
|
|
171
|
-
const newNode = new DoublyLinkedListNode(
|
|
169
|
+
push(element) {
|
|
170
|
+
const newNode = new DoublyLinkedListNode(element);
|
|
172
171
|
if (!this.head) {
|
|
173
172
|
this._head = newNode;
|
|
174
173
|
this._tail = newNode;
|
|
@@ -186,12 +185,8 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
186
185
|
* Space Complexity: O(1)
|
|
187
186
|
*/
|
|
188
187
|
/**
|
|
189
|
-
*
|
|
190
|
-
*
|
|
191
|
-
*
|
|
192
|
-
* The `pop()` function removes and returns the value of the last node in a doubly linked list.
|
|
193
|
-
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
|
194
|
-
* list is empty, it returns undefined.
|
|
188
|
+
* The `pop()` function removes and returns the value of the last element in a linked list.
|
|
189
|
+
* @returns The method is returning the value of the removed node.
|
|
195
190
|
*/
|
|
196
191
|
pop() {
|
|
197
192
|
if (!this.tail)
|
|
@@ -213,12 +208,8 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
213
208
|
* Space Complexity: O(1)
|
|
214
209
|
*/
|
|
215
210
|
/**
|
|
216
|
-
*
|
|
217
|
-
*
|
|
218
|
-
*
|
|
219
|
-
* The `shift()` function removes and returns the value of the first node in a doubly linked list.
|
|
220
|
-
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
|
|
221
|
-
* list.
|
|
211
|
+
* The `shift()` function removes and returns the value of the first element in a doubly linked list.
|
|
212
|
+
* @returns The value of the removed node.
|
|
222
213
|
*/
|
|
223
214
|
shift() {
|
|
224
215
|
if (!this.head)
|
|
@@ -240,15 +231,13 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
240
231
|
* Space Complexity: O(1)
|
|
241
232
|
*/
|
|
242
233
|
/**
|
|
243
|
-
*
|
|
244
|
-
*
|
|
245
|
-
*
|
|
246
|
-
* The unshift
|
|
247
|
-
* @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
|
|
248
|
-
* doubly linked list.
|
|
234
|
+
* The unshift function adds a new element to the beginning of a doubly linked list.
|
|
235
|
+
* @param {E} element - The "element" parameter represents the value of the element that you want to
|
|
236
|
+
* add to the beginning of the doubly linked list.
|
|
237
|
+
* @returns The `unshift` method is returning a boolean value, `true`.
|
|
249
238
|
*/
|
|
250
|
-
unshift(
|
|
251
|
-
const newNode = new DoublyLinkedListNode(
|
|
239
|
+
unshift(element) {
|
|
240
|
+
const newNode = new DoublyLinkedListNode(element);
|
|
252
241
|
if (!this.head) {
|
|
253
242
|
this._head = newNode;
|
|
254
243
|
this._tail = newNode;
|
|
@@ -738,65 +727,6 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
738
727
|
}
|
|
739
728
|
return mappedList;
|
|
740
729
|
}
|
|
741
|
-
/**
|
|
742
|
-
* Time Complexity: O(1)
|
|
743
|
-
* Space Complexity: O(1)
|
|
744
|
-
*/
|
|
745
|
-
/**
|
|
746
|
-
* Time Complexity: O(1)
|
|
747
|
-
* Space Complexity: O(1)
|
|
748
|
-
*
|
|
749
|
-
* The addLast function adds a new node with the given value to the end of the doubly linked list.
|
|
750
|
-
* @param {E} value - The value to be added to the linked list.
|
|
751
|
-
*/
|
|
752
|
-
addLast(value) {
|
|
753
|
-
return this.push(value);
|
|
754
|
-
}
|
|
755
|
-
/**
|
|
756
|
-
* Time Complexity: O(1)
|
|
757
|
-
* Space Complexity: O(1)
|
|
758
|
-
*/
|
|
759
|
-
/**
|
|
760
|
-
* Time Complexity: O(1)
|
|
761
|
-
* Space Complexity: O(1)
|
|
762
|
-
*
|
|
763
|
-
* The `pollLast()` function removes and returns the value of the last node in a doubly linked list.
|
|
764
|
-
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
|
765
|
-
* list is empty, it returns undefined.
|
|
766
|
-
*/
|
|
767
|
-
pollLast() {
|
|
768
|
-
return this.pop();
|
|
769
|
-
}
|
|
770
|
-
/**
|
|
771
|
-
* Time Complexity: O(1)
|
|
772
|
-
* Space Complexity: O(1)
|
|
773
|
-
*/
|
|
774
|
-
/**
|
|
775
|
-
* Time Complexity: O(1)
|
|
776
|
-
* Space Complexity: O(1)
|
|
777
|
-
*
|
|
778
|
-
* The `pollFirst()` function removes and returns the value of the first node in a doubly linked list.
|
|
779
|
-
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
|
|
780
|
-
* list.
|
|
781
|
-
*/
|
|
782
|
-
pollFirst() {
|
|
783
|
-
return this.shift();
|
|
784
|
-
}
|
|
785
|
-
/**
|
|
786
|
-
* Time Complexity: O(1)
|
|
787
|
-
* Space Complexity: O(1)
|
|
788
|
-
*/
|
|
789
|
-
/**
|
|
790
|
-
* Time Complexity: O(1)
|
|
791
|
-
* Space Complexity: O(1)
|
|
792
|
-
*
|
|
793
|
-
* The addFirst function adds a new node with the given value to the beginning of a doubly linked list.
|
|
794
|
-
* @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
|
|
795
|
-
* doubly linked list.
|
|
796
|
-
*/
|
|
797
|
-
addFirst(value) {
|
|
798
|
-
this.unshift(value);
|
|
799
|
-
}
|
|
800
730
|
/**
|
|
801
731
|
* The function returns an iterator that iterates over the values of a linked list.
|
|
802
732
|
*/
|