min-heap-typed 1.50.3 → 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/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/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/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/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;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { AVLTreeMultiMap, AVLTreeMultiMapNode } from '../../../data-structures';
|
|
2
|
+
import type { AVLTreeOptions } from './avl-tree';
|
|
3
|
+
export type AVLTreeMultiMapNodeNested<K, V> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
4
|
+
export type AVLTreeMultiMapNested<K, V, N extends AVLTreeMultiMapNode<K, V, N>> = AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
5
|
+
export type AVLTreeMultiMapOptions<K> = AVLTreeOptions<K> & {};
|
|
@@ -18,5 +18,6 @@ __exportStar(require("./binary-tree"), exports);
|
|
|
18
18
|
__exportStar(require("./bst"), exports);
|
|
19
19
|
__exportStar(require("./avl-tree"), exports);
|
|
20
20
|
__exportStar(require("./segment-tree"), exports);
|
|
21
|
-
__exportStar(require("./tree-
|
|
21
|
+
__exportStar(require("./avl-tree-multi-map"), exports);
|
|
22
22
|
__exportStar(require("./rb-tree"), exports);
|
|
23
|
+
__exportStar(require("./tree-multi-map"), exports);
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { TreeMultiMap, TreeMultiMapNode } from '../../../data-structures';
|
|
2
|
+
import type { RBTreeOptions } from './rb-tree';
|
|
3
|
+
export type TreeMultiMapNodeNested<K, V> = TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
4
|
+
export type TreeMultiMapNested<K, V, N extends TreeMultiMapNode<K, V, N>> = TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
5
|
+
export type TreeMultiMapOptions<K> = RBTreeOptions<K> & {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "min-heap-typed",
|
|
3
|
-
"version": "1.50.
|
|
3
|
+
"version": "1.50.4",
|
|
4
4
|
"description": "Min Heap. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -132,6 +132,6 @@
|
|
|
132
132
|
"typescript": "^4.9.5"
|
|
133
133
|
},
|
|
134
134
|
"dependencies": {
|
|
135
|
-
"data-structure-typed": "^1.50.
|
|
135
|
+
"data-structure-typed": "^1.50.4"
|
|
136
136
|
}
|
|
137
137
|
}
|
|
@@ -6,22 +6,22 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type {
|
|
9
|
+
AVLTreeMultiMapNested,
|
|
10
|
+
AVLTreeMultiMapNodeNested,
|
|
11
|
+
AVLTreeMultiMapOptions,
|
|
9
12
|
BinaryTreeDeleteResult,
|
|
10
13
|
BSTNKeyOrNode,
|
|
11
14
|
BTNCallback,
|
|
12
|
-
KeyOrNodeOrEntry
|
|
13
|
-
TreeMultimapNested,
|
|
14
|
-
TreeMultimapNodeNested,
|
|
15
|
-
TreeMultimapOptions
|
|
15
|
+
KeyOrNodeOrEntry
|
|
16
16
|
} from '../../types';
|
|
17
17
|
import { FamilyPosition, IterationType } from '../../types';
|
|
18
18
|
import { IBinaryTree } from '../../interfaces';
|
|
19
19
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
20
20
|
|
|
21
|
-
export class
|
|
21
|
+
export class AVLTreeMultiMapNode<
|
|
22
22
|
K = any,
|
|
23
23
|
V = any,
|
|
24
|
-
NODE extends
|
|
24
|
+
NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNodeNested<K, V>
|
|
25
25
|
> extends AVLTreeNode<K, V, NODE> {
|
|
26
26
|
/**
|
|
27
27
|
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
@@ -59,17 +59,17 @@ export class TreeMultimapNode<
|
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
/**
|
|
62
|
-
* The only distinction between a
|
|
62
|
+
* The only distinction between a AVLTreeMultiMap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
63
63
|
*/
|
|
64
|
-
export class
|
|
64
|
+
export class AVLTreeMultiMap<
|
|
65
65
|
K = any,
|
|
66
66
|
V = any,
|
|
67
|
-
NODE extends
|
|
68
|
-
TREE extends
|
|
67
|
+
NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNodeNested<K, V>>,
|
|
68
|
+
TREE extends AVLTreeMultiMap<K, V, NODE, TREE> = AVLTreeMultiMap<K, V, NODE, AVLTreeMultiMapNested<K, V, NODE>>
|
|
69
69
|
>
|
|
70
70
|
extends AVLTree<K, V, NODE, TREE>
|
|
71
71
|
implements IBinaryTree<K, V, NODE, TREE> {
|
|
72
|
-
constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?:
|
|
72
|
+
constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: AVLTreeMultiMapOptions<K>) {
|
|
73
73
|
super([], options);
|
|
74
74
|
if (keysOrNodesOrEntries) this.addMany(keysOrNodesOrEntries);
|
|
75
75
|
}
|
|
@@ -98,20 +98,20 @@ export class TreeMultimap<
|
|
|
98
98
|
* @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
|
|
99
99
|
*/
|
|
100
100
|
override createNode(key: K, value?: V, count?: number): NODE {
|
|
101
|
-
return new
|
|
101
|
+
return new AVLTreeMultiMapNode(key, value, count) as NODE;
|
|
102
102
|
}
|
|
103
103
|
|
|
104
104
|
/**
|
|
105
|
-
* The function creates a new
|
|
105
|
+
* The function creates a new AVLTreeMultiMap object with the specified options and returns it.
|
|
106
106
|
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
107
|
-
* configuration options for creating the `
|
|
107
|
+
* configuration options for creating the `AVLTreeMultiMap` object. It can include properties such as
|
|
108
108
|
* `iterationType` and `variant`, which are used to specify the type of iteration and the variant of
|
|
109
109
|
* the tree, respectively. These properties can be
|
|
110
|
-
* @returns a new instance of the `
|
|
110
|
+
* @returns a new instance of the `AVLTreeMultiMap` class, with the provided options merged with the
|
|
111
111
|
* default options. The returned value is casted as `TREE`.
|
|
112
112
|
*/
|
|
113
|
-
override createTree(options?:
|
|
114
|
-
return new
|
|
113
|
+
override createTree(options?: AVLTreeMultiMapOptions<K>): TREE {
|
|
114
|
+
return new AVLTreeMultiMap<K, V, NODE, TREE>([], {
|
|
115
115
|
iterationType: this.iterationType,
|
|
116
116
|
variant: this.variant,
|
|
117
117
|
...options
|
|
@@ -155,13 +155,13 @@ export class TreeMultimap<
|
|
|
155
155
|
}
|
|
156
156
|
|
|
157
157
|
/**
|
|
158
|
-
* The function checks if an keyOrNodeOrEntry is an instance of the
|
|
158
|
+
* The function checks if an keyOrNodeOrEntry is an instance of the AVLTreeMultiMapNode class.
|
|
159
159
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
160
|
-
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the
|
|
160
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeMultiMapNode
|
|
161
161
|
* class.
|
|
162
162
|
*/
|
|
163
163
|
override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE {
|
|
164
|
-
return keyOrNodeOrEntry instanceof
|
|
164
|
+
return keyOrNodeOrEntry instanceof AVLTreeMultiMapNode;
|
|
165
165
|
}
|
|
166
166
|
|
|
167
167
|
/**
|
|
@@ -708,9 +708,8 @@ export class BST<
|
|
|
708
708
|
const compared = this._compare(cur.key, targetKey);
|
|
709
709
|
if (compared === lesserOrGreater) ans.push(callback(cur));
|
|
710
710
|
|
|
711
|
-
if (
|
|
712
|
-
if (
|
|
713
|
-
if (cur.right && this._compare(cur.right.key, targetKey) === lesserOrGreater) _traverse(cur.right);
|
|
711
|
+
if (this.isRealNode(cur.left)) _traverse(cur.left);
|
|
712
|
+
if (this.isRealNode(cur.right)) _traverse(cur.right);
|
|
714
713
|
};
|
|
715
714
|
|
|
716
715
|
_traverse(this.root);
|
|
@@ -719,12 +718,12 @@ export class BST<
|
|
|
719
718
|
const queue = new Queue<NODE>([this.root]);
|
|
720
719
|
while (queue.size > 0) {
|
|
721
720
|
const cur = queue.shift();
|
|
722
|
-
if (cur) {
|
|
721
|
+
if (this.isRealNode(cur)) {
|
|
723
722
|
const compared = this._compare(cur.key, targetKey);
|
|
724
723
|
if (compared === lesserOrGreater) ans.push(callback(cur));
|
|
725
724
|
|
|
726
|
-
if (
|
|
727
|
-
if (
|
|
725
|
+
if (this.isRealNode(cur.left)) queue.push(cur.left);
|
|
726
|
+
if (this.isRealNode(cur.right)) queue.push(cur.right);
|
|
728
727
|
}
|
|
729
728
|
}
|
|
730
729
|
return ans;
|
|
@@ -527,12 +527,14 @@ export class RedBlackTree<
|
|
|
527
527
|
*/
|
|
528
528
|
protected _fixInsert(k: NODE): void {
|
|
529
529
|
let u: NODE | undefined;
|
|
530
|
-
while (k.parent && k.parent.color ===
|
|
530
|
+
while (k.parent && k.parent.color === RBTNColor.RED) {
|
|
531
531
|
if (k.parent.parent && k.parent === k.parent.parent.right) {
|
|
532
532
|
u = k.parent.parent.left;
|
|
533
|
-
|
|
534
|
-
|
|
533
|
+
|
|
534
|
+
if (u && u.color === RBTNColor.RED) {
|
|
535
|
+
// Delay color flip
|
|
535
536
|
k.parent.color = RBTNColor.BLACK;
|
|
537
|
+
u.color = RBTNColor.BLACK;
|
|
536
538
|
k.parent.parent.color = RBTNColor.RED;
|
|
537
539
|
k = k.parent.parent;
|
|
538
540
|
} else {
|
|
@@ -541,16 +543,20 @@ export class RedBlackTree<
|
|
|
541
543
|
this._rightRotate(k);
|
|
542
544
|
}
|
|
543
545
|
|
|
544
|
-
|
|
545
|
-
k.parent!.
|
|
546
|
+
// Check color before rotation
|
|
547
|
+
if (k.parent!.color === RBTNColor.RED) {
|
|
548
|
+
k.parent!.color = RBTNColor.BLACK;
|
|
549
|
+
k.parent!.parent!.color = RBTNColor.RED;
|
|
550
|
+
}
|
|
546
551
|
this._leftRotate(k.parent!.parent!);
|
|
547
552
|
}
|
|
548
553
|
} else {
|
|
549
|
-
u = k.parent
|
|
554
|
+
u = k.parent!.parent!.right;
|
|
550
555
|
|
|
551
|
-
if (u && u.color ===
|
|
552
|
-
|
|
556
|
+
if (u && u.color === RBTNColor.RED) {
|
|
557
|
+
// Delay color flip
|
|
553
558
|
k.parent.color = RBTNColor.BLACK;
|
|
559
|
+
u.color = RBTNColor.BLACK;
|
|
554
560
|
k.parent.parent!.color = RBTNColor.RED;
|
|
555
561
|
k = k.parent.parent!;
|
|
556
562
|
} else {
|
|
@@ -559,11 +565,15 @@ export class RedBlackTree<
|
|
|
559
565
|
this._leftRotate(k);
|
|
560
566
|
}
|
|
561
567
|
|
|
562
|
-
|
|
563
|
-
k.parent!.
|
|
568
|
+
// Check color before rotation
|
|
569
|
+
if (k.parent!.color === RBTNColor.RED) {
|
|
570
|
+
k.parent!.color = RBTNColor.BLACK;
|
|
571
|
+
k.parent!.parent!.color = RBTNColor.RED;
|
|
572
|
+
}
|
|
564
573
|
this._rightRotate(k.parent!.parent!);
|
|
565
574
|
}
|
|
566
575
|
}
|
|
576
|
+
|
|
567
577
|
if (k === this.root) {
|
|
568
578
|
break;
|
|
569
579
|
}
|