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.
@@ -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
- if (newNode === undefined) return false;
198
+ const orgCount = newNode?.count || 0;
199
+ const isSuccessAdded = super.add(newNode);
196
200
 
197
- const orgNodeCount = newNode?.count || 0;
198
- const inserted = super.add(newNode);
199
- if (inserted) {
200
- this._count += orgNodeCount;
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
- const deleteResults: BinaryTreeDeleteResult<NODE>[] = [];
236
- if (identifier === null) return deleteResults;
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
- // Move to the right or left based on the comparison with the identifier
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
- // If the target node is not found, decrement size and return
260
- if (targetNode === this._Sentinel) {
261
- return;
262
- }
244
+ if (!nodeToDelete) {
245
+ return results;
246
+ }
263
247
 
264
- if (ignoreCount || targetNode.count <= 1) {
265
- // Store the parent of the target node and its original color
266
- let parentNode = targetNode;
267
- let parentNodeOriginalColor: number = parentNode.color;
268
-
269
- // Handle deletion based on the number of children of the target node
270
- if (targetNode.left === this._Sentinel) {
271
- // Target node has no left child - deletion case 1
272
- currentNode = targetNode.right;
273
- this._rbTransplant(targetNode, targetNode.right!);
274
- } else if (targetNode.right === this._Sentinel) {
275
- // Target node has no right child - deletion case 2
276
- currentNode = targetNode.left;
277
- this._rbTransplant(targetNode, targetNode.left!);
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
- // Target node has both left and right children - deletion case 3
280
- parentNode = this.getLeftMost(targetNode.right)!;
281
- parentNodeOriginalColor = parentNode.color;
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
- // Replace parentNode with its right child and update connections
289
- this._rbTransplant(parentNode, parentNode.right!);
290
- parentNode.right = targetNode.right;
291
- parentNode.right!.parent = parentNode;
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
- // Fix the Red-Black Tree properties after deletion
302
- if (parentNodeOriginalColor === RBTNColor.BLACK) {
303
- this._fixDelete(currentNode!);
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
- // Decrement the size and store information about the deleted node
307
- this._size--;
308
- this._count -= targetNode.count;
309
- deleteResults.push({ deleted: targetNode, needBalanced: undefined });
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
- // Call the helper function with the root of the tree
317
- deleteHelper(this.root);
321
+ results.push({ deleted: nodeToDelete, needBalanced: undefined });
318
322
 
319
- // Return the result array
320
- return deleteResults;
323
+ return results;
321
324
  }
322
325
 
323
326
  /**
@@ -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
+ }