min-heap-typed 1.50.5 → 1.50.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -0
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +3 -0
- package/dist/data-structures/binary-tree/binary-tree.js +32 -28
- package/dist/data-structures/binary-tree/bst.js +17 -17
- package/dist/data-structures/binary-tree/rb-tree.d.ts +158 -141
- package/dist/data-structures/binary-tree/rb-tree.js +416 -396
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +1 -0
- package/dist/data-structures/binary-tree/tree-multi-map.js +84 -76
- package/dist/types/common.d.ts +6 -0
- package/dist/types/common.js +8 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +5 -1
- package/src/data-structures/binary-tree/avl-tree.ts +1 -1
- package/src/data-structures/binary-tree/binary-tree.ts +31 -29
- package/src/data-structures/binary-tree/bst.ts +18 -18
- package/src/data-structures/binary-tree/rb-tree.ts +436 -405
- package/src/data-structures/binary-tree/tree-multi-map.ts +85 -82
- package/src/types/common.ts +7 -0
|
@@ -88,10 +88,13 @@ export class TreeMultiMap<
|
|
|
88
88
|
* @returns the sum of the count property of all nodes in the tree.
|
|
89
89
|
*/
|
|
90
90
|
get count(): number {
|
|
91
|
+
return this._count;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
getMutableCount(): number {
|
|
91
95
|
let sum = 0;
|
|
92
96
|
this.dfs(node => (sum += node.count));
|
|
93
97
|
return sum;
|
|
94
|
-
// return this._count;
|
|
95
98
|
}
|
|
96
99
|
|
|
97
100
|
/**
|
|
@@ -192,14 +195,15 @@ export class TreeMultiMap<
|
|
|
192
195
|
*/
|
|
193
196
|
override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V, count = 1): boolean {
|
|
194
197
|
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value, count);
|
|
195
|
-
|
|
198
|
+
const orgCount = newNode?.count || 0;
|
|
199
|
+
const isSuccessAdded = super.add(newNode);
|
|
196
200
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
+
if (isSuccessAdded) {
|
|
202
|
+
this._count += orgCount;
|
|
203
|
+
return true;
|
|
204
|
+
} else {
|
|
205
|
+
return false;
|
|
201
206
|
}
|
|
202
|
-
return true;
|
|
203
207
|
}
|
|
204
208
|
|
|
205
209
|
/**
|
|
@@ -232,92 +236,91 @@ export class TreeMultiMap<
|
|
|
232
236
|
callback: C = this._defaultOneParamCallback as C,
|
|
233
237
|
ignoreCount = false
|
|
234
238
|
): BinaryTreeDeleteResult<NODE>[] {
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
// Helper function to perform deletion
|
|
239
|
-
const deleteHelper = (node: NODE | undefined): void => {
|
|
240
|
-
// Initialize targetNode to the sentinel node
|
|
241
|
-
let targetNode: NODE = this._Sentinel;
|
|
242
|
-
let currentNode: NODE | undefined;
|
|
243
|
-
|
|
244
|
-
// Find the node to be deleted based on the identifier
|
|
245
|
-
while (node !== this._Sentinel) {
|
|
246
|
-
// Update targetNode if the current node matches the identifier
|
|
247
|
-
if (node && callback(node) === identifier) {
|
|
248
|
-
targetNode = node;
|
|
249
|
-
}
|
|
239
|
+
if (identifier === null) return [];
|
|
240
|
+
const results: BinaryTreeDeleteResult<NODE>[] = [];
|
|
250
241
|
|
|
251
|
-
|
|
252
|
-
if (node && identifier && callback(node) <= identifier) {
|
|
253
|
-
node = node.right;
|
|
254
|
-
} else {
|
|
255
|
-
node = node?.left;
|
|
256
|
-
}
|
|
257
|
-
}
|
|
242
|
+
const nodeToDelete = this.isRealNode(identifier) ? identifier : this.getNode(identifier, callback);
|
|
258
243
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
}
|
|
244
|
+
if (!nodeToDelete) {
|
|
245
|
+
return results;
|
|
246
|
+
}
|
|
263
247
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
248
|
+
let originalColor = nodeToDelete.color;
|
|
249
|
+
let replacementNode: NODE | undefined;
|
|
250
|
+
|
|
251
|
+
if (!this.isRealNode(nodeToDelete.left)) {
|
|
252
|
+
replacementNode = nodeToDelete.right;
|
|
253
|
+
if (ignoreCount || nodeToDelete.count <= 1) {
|
|
254
|
+
this._transplant(nodeToDelete, nodeToDelete.right);
|
|
255
|
+
this._count -= nodeToDelete.count;
|
|
256
|
+
} else {
|
|
257
|
+
nodeToDelete.count--;
|
|
258
|
+
this._count--;
|
|
259
|
+
results.push({ deleted: nodeToDelete, needBalanced: undefined });
|
|
260
|
+
return results;
|
|
261
|
+
}
|
|
262
|
+
} else if (!this.isRealNode(nodeToDelete.right)) {
|
|
263
|
+
replacementNode = nodeToDelete.left;
|
|
264
|
+
if (ignoreCount || nodeToDelete.count <= 1) {
|
|
265
|
+
this._transplant(nodeToDelete, nodeToDelete.left);
|
|
266
|
+
this._count -= nodeToDelete.count;
|
|
267
|
+
} else {
|
|
268
|
+
nodeToDelete.count--;
|
|
269
|
+
this._count--;
|
|
270
|
+
results.push({ deleted: nodeToDelete, needBalanced: undefined });
|
|
271
|
+
return results;
|
|
272
|
+
}
|
|
273
|
+
} else {
|
|
274
|
+
const successor = this.getLeftMost(nodeToDelete.right);
|
|
275
|
+
if (successor) {
|
|
276
|
+
originalColor = successor.color;
|
|
277
|
+
replacementNode = successor.right;
|
|
278
|
+
|
|
279
|
+
if (successor.parent === nodeToDelete) {
|
|
280
|
+
if (this.isRealNode(replacementNode)) {
|
|
281
|
+
replacementNode.parent = successor;
|
|
282
|
+
}
|
|
278
283
|
} else {
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
currentNode = parentNode.right;
|
|
283
|
-
|
|
284
|
-
if (parentNode.parent === targetNode) {
|
|
285
|
-
// Target node's right child becomes its parent's left child
|
|
286
|
-
currentNode!.parent = parentNode;
|
|
284
|
+
if (ignoreCount || nodeToDelete.count <= 1) {
|
|
285
|
+
this._transplant(successor, successor.right);
|
|
286
|
+
this._count -= nodeToDelete.count;
|
|
287
287
|
} else {
|
|
288
|
-
|
|
289
|
-
this.
|
|
290
|
-
|
|
291
|
-
|
|
288
|
+
nodeToDelete.count--;
|
|
289
|
+
this._count--;
|
|
290
|
+
results.push({ deleted: nodeToDelete, needBalanced: undefined });
|
|
291
|
+
return results;
|
|
292
|
+
}
|
|
293
|
+
successor.right = nodeToDelete.right;
|
|
294
|
+
if (this.isRealNode(successor.right)) {
|
|
295
|
+
successor.right.parent = successor;
|
|
292
296
|
}
|
|
293
|
-
|
|
294
|
-
// Replace the target node with its in-order successor
|
|
295
|
-
this._rbTransplant(targetNode, parentNode);
|
|
296
|
-
parentNode.left = targetNode.left;
|
|
297
|
-
parentNode.left!.parent = parentNode;
|
|
298
|
-
parentNode.color = targetNode.color;
|
|
299
297
|
}
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
298
|
+
if (ignoreCount || nodeToDelete.count <= 1) {
|
|
299
|
+
this._transplant(nodeToDelete, successor);
|
|
300
|
+
this._count -= nodeToDelete.count;
|
|
301
|
+
} else {
|
|
302
|
+
nodeToDelete.count--;
|
|
303
|
+
this._count--;
|
|
304
|
+
results.push({ deleted: nodeToDelete, needBalanced: undefined });
|
|
305
|
+
return results;
|
|
304
306
|
}
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
} else {
|
|
311
|
-
targetNode.count--;
|
|
312
|
-
this._count--;
|
|
307
|
+
successor.left = nodeToDelete.left;
|
|
308
|
+
if (this.isRealNode(successor.left)) {
|
|
309
|
+
successor.left.parent = successor;
|
|
310
|
+
}
|
|
311
|
+
successor.color = nodeToDelete.color;
|
|
313
312
|
}
|
|
314
|
-
}
|
|
313
|
+
}
|
|
314
|
+
this._size--;
|
|
315
|
+
|
|
316
|
+
// If the original color was black, fix the tree
|
|
317
|
+
if (originalColor === RBTNColor.BLACK) {
|
|
318
|
+
this._deleteFixup(replacementNode);
|
|
319
|
+
}
|
|
315
320
|
|
|
316
|
-
|
|
317
|
-
deleteHelper(this.root);
|
|
321
|
+
results.push({ deleted: nodeToDelete, needBalanced: undefined });
|
|
318
322
|
|
|
319
|
-
|
|
320
|
-
return deleteResults;
|
|
323
|
+
return results;
|
|
321
324
|
}
|
|
322
325
|
|
|
323
326
|
/**
|
package/src/types/common.ts
CHANGED
|
@@ -63,3 +63,10 @@ export type BTNodePureExemplar<K, V, N> = [K, V | undefined] | BTNodePureKeyOrNo
|
|
|
63
63
|
export type BSTNKeyOrNode<K, N> = K | undefined | N;
|
|
64
64
|
|
|
65
65
|
export type BinaryTreeDeleteResult<N> = { deleted: N | null | undefined; needBalanced: N | null | undefined };
|
|
66
|
+
|
|
67
|
+
export enum CRUD {
|
|
68
|
+
CREATED = 'CREATED',
|
|
69
|
+
READ = 'READ',
|
|
70
|
+
UPDATED = 'UPDATED',
|
|
71
|
+
DELETED = 'DELETED'
|
|
72
|
+
}
|