min-heap-typed 1.50.5 → 1.50.6

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.
@@ -51,6 +51,7 @@ export declare class TreeMultiMap<K = any, V = any, NODE extends TreeMultiMapNod
51
51
  * @returns the sum of the count property of all nodes in the tree.
52
52
  */
53
53
  get count(): number;
54
+ getMutableCount(): number;
54
55
  /**
55
56
  * The function creates a new TreeMultiMapNode object with the specified key, value, and count.
56
57
  * @param {K} key - The key parameter represents the key of the node being created. It is of type K,
@@ -58,10 +58,12 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
58
58
  * @returns the sum of the count property of all nodes in the tree.
59
59
  */
60
60
  get count() {
61
+ return this._count;
62
+ }
63
+ getMutableCount() {
61
64
  let sum = 0;
62
65
  this.dfs(node => (sum += node.count));
63
66
  return sum;
64
- // return this._count;
65
67
  }
66
68
  /**
67
69
  * The function creates a new TreeMultiMapNode object with the specified key, value, and count.
@@ -154,14 +156,15 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
154
156
  */
155
157
  add(keyOrNodeOrEntry, value, count = 1) {
156
158
  const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value, count);
157
- if (newNode === undefined)
159
+ const orgCount = (newNode === null || newNode === void 0 ? void 0 : newNode.count) || 0;
160
+ const isSuccessAdded = super.add(newNode);
161
+ if (isSuccessAdded) {
162
+ this._count += orgCount;
163
+ return true;
164
+ }
165
+ else {
158
166
  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
167
  }
164
- return true;
165
168
  }
166
169
  /**
167
170
  * Time Complexity: O(log n)
@@ -188,86 +191,91 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
188
191
  * @returns an array of BinaryTreeDeleteResult<NODE> objects.
189
192
  */
190
193
  delete(identifier, callback = this._defaultOneParamCallback, ignoreCount = false) {
191
- const deleteResults = [];
192
194
  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
- }
195
+ return [];
196
+ const results = [];
197
+ const nodeToDelete = this.isRealNode(identifier) ? identifier : this.getNode(identifier, callback);
198
+ if (!nodeToDelete) {
199
+ return results;
200
+ }
201
+ let originalColor = nodeToDelete.color;
202
+ let replacementNode;
203
+ if (!this.isRealNode(nodeToDelete.left)) {
204
+ replacementNode = nodeToDelete.right;
205
+ if (ignoreCount || nodeToDelete.count <= 1) {
206
+ this._transplant(nodeToDelete, nodeToDelete.right);
207
+ this._count -= nodeToDelete.count;
212
208
  }
213
- // If the target node is not found, decrement size and return
214
- if (targetNode === this._Sentinel) {
215
- return;
209
+ else {
210
+ nodeToDelete.count--;
211
+ this._count--;
212
+ results.push({ deleted: nodeToDelete, needBalanced: undefined });
213
+ return results;
216
214
  }
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);
215
+ }
216
+ else if (!this.isRealNode(nodeToDelete.right)) {
217
+ replacementNode = nodeToDelete.left;
218
+ if (ignoreCount || nodeToDelete.count <= 1) {
219
+ this._transplant(nodeToDelete, nodeToDelete.left);
220
+ this._count -= nodeToDelete.count;
221
+ }
222
+ else {
223
+ nodeToDelete.count--;
224
+ this._count--;
225
+ results.push({ deleted: nodeToDelete, needBalanced: undefined });
226
+ return results;
227
+ }
228
+ }
229
+ else {
230
+ const successor = this.getLeftMost(nodeToDelete.right);
231
+ if (successor) {
232
+ originalColor = successor.color;
233
+ replacementNode = successor.right;
234
+ if (successor.parent === nodeToDelete) {
235
+ if (this.isRealNode(replacementNode)) {
236
+ replacementNode.parent = successor;
237
+ }
231
238
  }
232
239
  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
+ if (ignoreCount || nodeToDelete.count <= 1) {
241
+ this._transplant(successor, successor.right);
242
+ this._count -= nodeToDelete.count;
240
243
  }
241
244
  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;
245
+ nodeToDelete.count--;
246
+ this._count--;
247
+ results.push({ deleted: nodeToDelete, needBalanced: undefined });
248
+ return results;
249
+ }
250
+ successor.right = nodeToDelete.right;
251
+ if (this.isRealNode(successor.right)) {
252
+ successor.right.parent = successor;
246
253
  }
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
254
  }
253
- // Fix the Red-Black Tree properties after deletion
254
- if (parentNodeOriginalColor === types_1.RBTNColor.BLACK) {
255
- this._fixDelete(currentNode);
255
+ if (ignoreCount || nodeToDelete.count <= 1) {
256
+ this._transplant(nodeToDelete, successor);
257
+ this._count -= nodeToDelete.count;
256
258
  }
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--;
259
+ else {
260
+ nodeToDelete.count--;
261
+ this._count--;
262
+ results.push({ deleted: nodeToDelete, needBalanced: undefined });
263
+ return results;
264
+ }
265
+ successor.left = nodeToDelete.left;
266
+ if (this.isRealNode(successor.left)) {
267
+ successor.left.parent = successor;
268
+ }
269
+ successor.color = nodeToDelete.color;
265
270
  }
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
+ this._size--;
273
+ // If the original color was black, fix the tree
274
+ if (originalColor === types_1.RBTNColor.BLACK) {
275
+ this._deleteFixup(replacementNode);
276
+ }
277
+ results.push({ deleted: nodeToDelete, needBalanced: undefined });
278
+ return results;
271
279
  }
272
280
  /**
273
281
  * Time Complexity: O(1)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "min-heap-typed",
3
- "version": "1.50.5",
3
+ "version": "1.50.6",
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.4"
135
+ "data-structure-typed": "^1.50.6"
136
136
  }
137
137
  }
@@ -934,7 +934,7 @@ export class BinaryTree<
934
934
 
935
935
  if (iterationType === IterationType.RECURSIVE) {
936
936
  const dfs = (cur: NODE | null | undefined, min: number, max: number): boolean => {
937
- if (!cur) return true;
937
+ if (!this.isRealNode(cur)) return true;
938
938
  const numKey = this.extractor(cur.key);
939
939
  if (numKey <= min || numKey >= max) return false;
940
940
  return dfs(cur.left, min, numKey) && dfs(cur.right, numKey, max);
@@ -949,14 +949,14 @@ export class BinaryTree<
949
949
  let prev = checkMax ? Number.MAX_SAFE_INTEGER : Number.MIN_SAFE_INTEGER;
950
950
  // @ts-ignore
951
951
  let curr: NODE | null | undefined = beginRoot;
952
- while (curr || stack.length > 0) {
953
- while (curr) {
952
+ while (this.isRealNode(curr) || stack.length > 0) {
953
+ while (this.isRealNode(curr)) {
954
954
  stack.push(curr);
955
955
  curr = curr.left;
956
956
  }
957
957
  curr = stack.pop()!;
958
958
  const numKey = this.extractor(curr.key);
959
- if (!curr || (!checkMax && prev >= numKey) || (checkMax && prev <= numKey)) return false;
959
+ if (!this.isRealNode(curr) || (!checkMax && prev >= numKey) || (checkMax && prev <= numKey)) return false;
960
960
  prev = numKey;
961
961
  curr = curr.right;
962
962
  }
@@ -1025,7 +1025,7 @@ export class BinaryTree<
1025
1025
 
1026
1026
  if (iterationType === IterationType.RECURSIVE) {
1027
1027
  const _getMaxHeight = (cur: NODE | null | undefined): number => {
1028
- if (!cur) return -1;
1028
+ if (!this.isRealNode(cur)) return -1;
1029
1029
  const leftHeight = _getMaxHeight(cur.left);
1030
1030
  const rightHeight = _getMaxHeight(cur.right);
1031
1031
  return Math.max(leftHeight, rightHeight) + 1;
@@ -1039,8 +1039,8 @@ export class BinaryTree<
1039
1039
  while (stack.length > 0) {
1040
1040
  const { node, depth } = stack.pop()!;
1041
1041
 
1042
- if (node.left) stack.push({ node: node.left, depth: depth + 1 });
1043
- if (node.right) stack.push({ node: node.right, depth: depth + 1 });
1042
+ if (this.isRealNode(node.left)) stack.push({ node: node.left, depth: depth + 1 });
1043
+ if (this.isRealNode(node.right)) stack.push({ node: node.right, depth: depth + 1 });
1044
1044
 
1045
1045
  maxHeight = Math.max(maxHeight, depth);
1046
1046
  }
@@ -1354,34 +1354,34 @@ export class BinaryTree<
1354
1354
  switch (pattern) {
1355
1355
  case 'in':
1356
1356
  if (includeNull) {
1357
- if (node && this.isNodeOrNull(node.left)) _traverse(node.left);
1357
+ if (this.isRealNode(node) && this.isNodeOrNull(node.left)) _traverse(node.left);
1358
1358
  this.isNodeOrNull(node) && ans.push(callback(node));
1359
- if (node && this.isNodeOrNull(node.right)) _traverse(node.right);
1359
+ if (this.isRealNode(node) && this.isNodeOrNull(node.right)) _traverse(node.right);
1360
1360
  } else {
1361
- if (node && node.left) _traverse(node.left);
1361
+ if (this.isRealNode(node) && this.isRealNode(node.left)) _traverse(node.left);
1362
1362
  this.isRealNode(node) && ans.push(callback(node));
1363
- if (node && node.right) _traverse(node.right);
1363
+ if (this.isRealNode(node) && this.isRealNode(node.right)) _traverse(node.right);
1364
1364
  }
1365
1365
  break;
1366
1366
  case 'pre':
1367
1367
  if (includeNull) {
1368
1368
  this.isNodeOrNull(node) && ans.push(callback(node));
1369
- if (node && this.isNodeOrNull(node.left)) _traverse(node.left);
1370
- if (node && this.isNodeOrNull(node.right)) _traverse(node.right);
1369
+ if (this.isRealNode(node) && this.isNodeOrNull(node.left)) _traverse(node.left);
1370
+ if (this.isRealNode(node) && this.isNodeOrNull(node.right)) _traverse(node.right);
1371
1371
  } else {
1372
1372
  this.isRealNode(node) && ans.push(callback(node));
1373
- if (node && node.left) _traverse(node.left);
1374
- if (node && node.right) _traverse(node.right);
1373
+ if (this.isRealNode(node) && this.isRealNode(node.left)) _traverse(node.left);
1374
+ if (this.isRealNode(node) && this.isRealNode(node.right)) _traverse(node.right);
1375
1375
  }
1376
1376
  break;
1377
1377
  case 'post':
1378
1378
  if (includeNull) {
1379
- if (node && this.isNodeOrNull(node.left)) _traverse(node.left);
1380
- if (node && this.isNodeOrNull(node.right)) _traverse(node.right);
1379
+ if (this.isRealNode(node) && this.isNodeOrNull(node.left)) _traverse(node.left);
1380
+ if (this.isRealNode(node) && this.isNodeOrNull(node.right)) _traverse(node.right);
1381
1381
  this.isNodeOrNull(node) && ans.push(callback(node));
1382
1382
  } else {
1383
- if (node && node.left) _traverse(node.left);
1384
- if (node && node.right) _traverse(node.right);
1383
+ if (this.isRealNode(node) && this.isRealNode(node.left)) _traverse(node.left);
1384
+ if (this.isRealNode(node) && this.isRealNode(node.right)) _traverse(node.right);
1385
1385
  this.isRealNode(node) && ans.push(callback(node));
1386
1386
  }
1387
1387