data-structure-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.
Files changed (47) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +13 -18
  3. package/benchmark/report.html +13 -13
  4. package/benchmark/report.json +167 -143
  5. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -0
  6. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +3 -0
  7. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
  8. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  9. package/dist/cjs/data-structures/binary-tree/binary-tree.js +32 -28
  10. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  11. package/dist/cjs/data-structures/binary-tree/bst.js +17 -17
  12. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  13. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +158 -141
  14. package/dist/cjs/data-structures/binary-tree/rb-tree.js +416 -396
  15. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  16. package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +1 -0
  17. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +84 -76
  18. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  19. package/dist/cjs/types/common.d.ts +6 -0
  20. package/dist/cjs/types/common.js +8 -1
  21. package/dist/cjs/types/common.js.map +1 -1
  22. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -0
  23. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +3 -0
  24. package/dist/mjs/data-structures/binary-tree/binary-tree.js +32 -28
  25. package/dist/mjs/data-structures/binary-tree/bst.js +17 -17
  26. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +158 -141
  27. package/dist/mjs/data-structures/binary-tree/rb-tree.js +413 -396
  28. package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +1 -0
  29. package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +84 -76
  30. package/dist/mjs/types/common.d.ts +6 -0
  31. package/dist/mjs/types/common.js +7 -0
  32. package/dist/umd/data-structure-typed.js +519 -467
  33. package/dist/umd/data-structure-typed.min.js +2 -2
  34. package/dist/umd/data-structure-typed.min.js.map +1 -1
  35. package/package.json +1 -1
  36. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +5 -1
  37. package/src/data-structures/binary-tree/avl-tree.ts +1 -1
  38. package/src/data-structures/binary-tree/binary-tree.ts +31 -29
  39. package/src/data-structures/binary-tree/bst.ts +18 -18
  40. package/src/data-structures/binary-tree/rb-tree.ts +436 -405
  41. package/src/data-structures/binary-tree/tree-multi-map.ts +85 -82
  42. package/src/types/common.ts +7 -0
  43. package/test/performance/data-structures/binary-tree/rb-tree.test.ts +26 -16
  44. package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +4 -1
  45. package/test/unit/data-structures/binary-tree/overall.test.ts +23 -21
  46. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +387 -324
  47. package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +335 -204
@@ -115,6 +115,7 @@ var dataStructureTyped = (() => {
115
115
  BinaryTree: () => BinaryTree,
116
116
  BinaryTreeNode: () => BinaryTreeNode,
117
117
  CP: () => CP,
118
+ CRUD: () => CRUD,
118
119
  Character: () => Character,
119
120
  Deque: () => Deque,
120
121
  DirectedEdge: () => DirectedEdge,
@@ -7689,6 +7690,13 @@ var dataStructureTyped = (() => {
7689
7690
  FamilyPosition2["MAL_NODE"] = "MAL_NODE";
7690
7691
  return FamilyPosition2;
7691
7692
  })(FamilyPosition || {});
7693
+ var CRUD = /* @__PURE__ */ ((CRUD2) => {
7694
+ CRUD2["CREATED"] = "CREATED";
7695
+ CRUD2["READ"] = "READ";
7696
+ CRUD2["UPDATED"] = "UPDATED";
7697
+ CRUD2["DELETED"] = "DELETED";
7698
+ return CRUD2;
7699
+ })(CRUD || {});
7692
7700
 
7693
7701
  // src/data-structures/binary-tree/binary-tree.ts
7694
7702
  var BinaryTreeNode = class {
@@ -8386,7 +8394,7 @@ var dataStructureTyped = (() => {
8386
8394
  return true;
8387
8395
  if (iterationType === "RECURSIVE" /* RECURSIVE */) {
8388
8396
  const dfs = (cur, min, max) => {
8389
- if (!cur)
8397
+ if (!this.isRealNode(cur))
8390
8398
  return true;
8391
8399
  const numKey = this.extractor(cur.key);
8392
8400
  if (numKey <= min || numKey >= max)
@@ -8401,14 +8409,14 @@ var dataStructureTyped = (() => {
8401
8409
  const stack = [];
8402
8410
  let prev = checkMax ? Number.MAX_SAFE_INTEGER : Number.MIN_SAFE_INTEGER;
8403
8411
  let curr = beginRoot;
8404
- while (curr || stack.length > 0) {
8405
- while (curr) {
8412
+ while (this.isRealNode(curr) || stack.length > 0) {
8413
+ while (this.isRealNode(curr)) {
8406
8414
  stack.push(curr);
8407
8415
  curr = curr.left;
8408
8416
  }
8409
8417
  curr = stack.pop();
8410
8418
  const numKey = this.extractor(curr.key);
8411
- if (!curr || !checkMax && prev >= numKey || checkMax && prev <= numKey)
8419
+ if (!this.isRealNode(curr) || !checkMax && prev >= numKey || checkMax && prev <= numKey)
8412
8420
  return false;
8413
8421
  prev = numKey;
8414
8422
  curr = curr.right;
@@ -8469,11 +8477,11 @@ var dataStructureTyped = (() => {
8469
8477
  */
8470
8478
  getHeight(beginRoot = this.root, iterationType = this.iterationType) {
8471
8479
  beginRoot = this.ensureNode(beginRoot);
8472
- if (!beginRoot)
8480
+ if (!this.isRealNode(beginRoot))
8473
8481
  return -1;
8474
8482
  if (iterationType === "RECURSIVE" /* RECURSIVE */) {
8475
8483
  const _getMaxHeight = (cur) => {
8476
- if (!cur)
8484
+ if (!this.isRealNode(cur))
8477
8485
  return -1;
8478
8486
  const leftHeight = _getMaxHeight(cur.left);
8479
8487
  const rightHeight = _getMaxHeight(cur.right);
@@ -8485,9 +8493,9 @@ var dataStructureTyped = (() => {
8485
8493
  let maxHeight = 0;
8486
8494
  while (stack.length > 0) {
8487
8495
  const { node, depth } = stack.pop();
8488
- if (node.left)
8496
+ if (this.isRealNode(node.left))
8489
8497
  stack.push({ node: node.left, depth: depth + 1 });
8490
- if (node.right)
8498
+ if (this.isRealNode(node.right))
8491
8499
  stack.push({ node: node.right, depth: depth + 1 });
8492
8500
  maxHeight = Math.max(maxHeight, depth);
8493
8501
  }
@@ -8518,9 +8526,9 @@ var dataStructureTyped = (() => {
8518
8526
  return -1;
8519
8527
  if (iterationType === "RECURSIVE" /* RECURSIVE */) {
8520
8528
  const _getMinHeight = (cur) => {
8521
- if (!cur)
8529
+ if (!this.isRealNode(cur))
8522
8530
  return 0;
8523
- if (!cur.left && !cur.right)
8531
+ if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
8524
8532
  return 0;
8525
8533
  const leftMinHeight = _getMinHeight(cur.left);
8526
8534
  const rightMinHeight = _getMinHeight(cur.right);
@@ -8532,16 +8540,16 @@ var dataStructureTyped = (() => {
8532
8540
  let node = beginRoot, last = null;
8533
8541
  const depths = /* @__PURE__ */ new Map();
8534
8542
  while (stack.length > 0 || node) {
8535
- if (node) {
8543
+ if (this.isRealNode(node)) {
8536
8544
  stack.push(node);
8537
8545
  node = node.left;
8538
8546
  } else {
8539
8547
  node = stack[stack.length - 1];
8540
- if (!node.right || last === node.right) {
8548
+ if (!this.isRealNode(node.right) || last === node.right) {
8541
8549
  node = stack.pop();
8542
- if (node) {
8543
- const leftMinHeight = node.left ? (_a = depths.get(node.left)) != null ? _a : -1 : -1;
8544
- const rightMinHeight = node.right ? (_b = depths.get(node.right)) != null ? _b : -1 : -1;
8550
+ if (this.isRealNode(node)) {
8551
+ const leftMinHeight = this.isRealNode(node.left) ? (_a = depths.get(node.left)) != null ? _a : -1 : -1;
8552
+ const rightMinHeight = this.isRealNode(node.right) ? (_b = depths.get(node.right)) != null ? _b : -1 : -1;
8545
8553
  depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
8546
8554
  last = node;
8547
8555
  node = null;
@@ -8603,8 +8611,10 @@ var dataStructureTyped = (() => {
8603
8611
  * is no leftmost node, it returns `null` or `undefined` depending on the input.
8604
8612
  */
8605
8613
  getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
8614
+ if (this.isNIL(beginRoot))
8615
+ return beginRoot;
8606
8616
  beginRoot = this.ensureNode(beginRoot);
8607
- if (!beginRoot)
8617
+ if (!this.isRealNode(beginRoot))
8608
8618
  return beginRoot;
8609
8619
  if (iterationType === "RECURSIVE" /* RECURSIVE */) {
8610
8620
  const _traverse = (cur) => {
@@ -8642,6 +8652,8 @@ var dataStructureTyped = (() => {
8642
8652
  * is no rightmost node, it returns `null` or `undefined`, depending on the input.
8643
8653
  */
8644
8654
  getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
8655
+ if (this.isNIL(beginRoot))
8656
+ return beginRoot;
8645
8657
  beginRoot = this.ensureNode(beginRoot);
8646
8658
  if (!beginRoot)
8647
8659
  return beginRoot;
@@ -8752,45 +8764,45 @@ var dataStructureTyped = (() => {
8752
8764
  switch (pattern) {
8753
8765
  case "in":
8754
8766
  if (includeNull) {
8755
- if (node && this.isNodeOrNull(node.left))
8767
+ if (this.isRealNode(node) && this.isNodeOrNull(node.left))
8756
8768
  _traverse(node.left);
8757
8769
  this.isNodeOrNull(node) && ans.push(callback(node));
8758
- if (node && this.isNodeOrNull(node.right))
8770
+ if (this.isRealNode(node) && this.isNodeOrNull(node.right))
8759
8771
  _traverse(node.right);
8760
8772
  } else {
8761
- if (node && node.left)
8773
+ if (this.isRealNode(node) && this.isRealNode(node.left))
8762
8774
  _traverse(node.left);
8763
8775
  this.isRealNode(node) && ans.push(callback(node));
8764
- if (node && node.right)
8776
+ if (this.isRealNode(node) && this.isRealNode(node.right))
8765
8777
  _traverse(node.right);
8766
8778
  }
8767
8779
  break;
8768
8780
  case "pre":
8769
8781
  if (includeNull) {
8770
8782
  this.isNodeOrNull(node) && ans.push(callback(node));
8771
- if (node && this.isNodeOrNull(node.left))
8783
+ if (this.isRealNode(node) && this.isNodeOrNull(node.left))
8772
8784
  _traverse(node.left);
8773
- if (node && this.isNodeOrNull(node.right))
8785
+ if (this.isRealNode(node) && this.isNodeOrNull(node.right))
8774
8786
  _traverse(node.right);
8775
8787
  } else {
8776
8788
  this.isRealNode(node) && ans.push(callback(node));
8777
- if (node && node.left)
8789
+ if (this.isRealNode(node) && this.isRealNode(node.left))
8778
8790
  _traverse(node.left);
8779
- if (node && node.right)
8791
+ if (this.isRealNode(node) && this.isRealNode(node.right))
8780
8792
  _traverse(node.right);
8781
8793
  }
8782
8794
  break;
8783
8795
  case "post":
8784
8796
  if (includeNull) {
8785
- if (node && this.isNodeOrNull(node.left))
8797
+ if (this.isRealNode(node) && this.isNodeOrNull(node.left))
8786
8798
  _traverse(node.left);
8787
- if (node && this.isNodeOrNull(node.right))
8799
+ if (this.isRealNode(node) && this.isNodeOrNull(node.right))
8788
8800
  _traverse(node.right);
8789
8801
  this.isNodeOrNull(node) && ans.push(callback(node));
8790
8802
  } else {
8791
- if (node && node.left)
8803
+ if (this.isRealNode(node) && this.isRealNode(node.left))
8792
8804
  _traverse(node.left);
8793
- if (node && node.right)
8805
+ if (this.isRealNode(node) && this.isRealNode(node.right))
8794
8806
  _traverse(node.right);
8795
8807
  this.isRealNode(node) && ans.push(callback(node));
8796
8808
  }
@@ -9707,17 +9719,17 @@ var dataStructureTyped = (() => {
9707
9719
  * found in the binary tree. If no node is found, it returns `undefined`.
9708
9720
  */
9709
9721
  getNodeByKey(key, iterationType = "ITERATIVE" /* ITERATIVE */) {
9710
- if (!this.root)
9722
+ if (!this.isRealNode(this.root))
9711
9723
  return void 0;
9712
9724
  if (iterationType === "RECURSIVE" /* RECURSIVE */) {
9713
9725
  const _dfs = (cur) => {
9714
9726
  if (cur.key === key)
9715
9727
  return cur;
9716
- if (!cur.left && !cur.right)
9728
+ if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
9717
9729
  return;
9718
- if (this._compare(cur.key, key) === "gt" /* gt */ && cur.left)
9730
+ if (this._compare(cur.key, key) === "gt" /* gt */ && this.isRealNode(cur.left))
9719
9731
  return _dfs(cur.left);
9720
- if (this._compare(cur.key, key) === "lt" /* lt */ && cur.right)
9732
+ if (this._compare(cur.key, key) === "lt" /* lt */ && this.isRealNode(cur.right))
9721
9733
  return _dfs(cur.right);
9722
9734
  };
9723
9735
  return _dfs(this.root);
@@ -9725,13 +9737,13 @@ var dataStructureTyped = (() => {
9725
9737
  const queue = new Queue([this.root]);
9726
9738
  while (queue.size > 0) {
9727
9739
  const cur = queue.shift();
9728
- if (cur) {
9740
+ if (this.isRealNode(cur)) {
9729
9741
  if (this._compare(cur.key, key) === "eq" /* eq */)
9730
9742
  return cur;
9731
9743
  if (this._compare(cur.key, key) === "gt" /* gt */)
9732
- cur.left && queue.push(cur.left);
9744
+ this.isRealNode(cur.left) && queue.push(cur.left);
9733
9745
  if (this._compare(cur.key, key) === "lt" /* lt */)
9734
- cur.right && queue.push(cur.right);
9746
+ this.isRealNode(cur.right) && queue.push(cur.right);
9735
9747
  }
9736
9748
  }
9737
9749
  }
@@ -9777,16 +9789,16 @@ var dataStructureTyped = (() => {
9777
9789
  if (onlyOne)
9778
9790
  return;
9779
9791
  }
9780
- if (!cur.left && !cur.right)
9792
+ if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
9781
9793
  return;
9782
9794
  if (callback === this._defaultOneParamCallback) {
9783
9795
  if (this._compare(cur.key, identifier) === "gt" /* gt */)
9784
- cur.left && _traverse(cur.left);
9796
+ this.isRealNode(cur.left) && _traverse(cur.left);
9785
9797
  if (this._compare(cur.key, identifier) === "lt" /* lt */)
9786
- cur.right && _traverse(cur.right);
9798
+ this.isRealNode(cur.right) && _traverse(cur.right);
9787
9799
  } else {
9788
- cur.left && _traverse(cur.left);
9789
- cur.right && _traverse(cur.right);
9800
+ this.isRealNode(cur.left) && _traverse(cur.left);
9801
+ this.isRealNode(cur.right) && _traverse(cur.right);
9790
9802
  }
9791
9803
  };
9792
9804
  _traverse(beginRoot);
@@ -9794,7 +9806,7 @@ var dataStructureTyped = (() => {
9794
9806
  const queue = new Queue([beginRoot]);
9795
9807
  while (queue.size > 0) {
9796
9808
  const cur = queue.shift();
9797
- if (cur) {
9809
+ if (this.isRealNode(cur)) {
9798
9810
  const callbackResult = callback(cur);
9799
9811
  if (callbackResult === identifier) {
9800
9812
  ans.push(cur);
@@ -9803,12 +9815,12 @@ var dataStructureTyped = (() => {
9803
9815
  }
9804
9816
  if (callback === this._defaultOneParamCallback) {
9805
9817
  if (this._compare(cur.key, identifier) === "gt" /* gt */)
9806
- cur.left && queue.push(cur.left);
9818
+ this.isRealNode(cur.left) && queue.push(cur.left);
9807
9819
  if (this._compare(cur.key, identifier) === "lt" /* lt */)
9808
- cur.right && queue.push(cur.right);
9820
+ this.isRealNode(cur.right) && queue.push(cur.right);
9809
9821
  } else {
9810
- cur.left && queue.push(cur.left);
9811
- cur.right && queue.push(cur.right);
9822
+ this.isRealNode(cur.left) && queue.push(cur.left);
9823
+ this.isRealNode(cur.right) && queue.push(cur.right);
9812
9824
  }
9813
9825
  }
9814
9826
  }
@@ -11161,7 +11173,7 @@ var dataStructureTyped = (() => {
11161
11173
  }
11162
11174
  /**
11163
11175
  * The function returns the color value of a variable.
11164
- * @returns The color value stored in the protected variable `_color`.
11176
+ * @returns The color value stored in the private variable `_color`.
11165
11177
  */
11166
11178
  get color() {
11167
11179
  return this._color;
@@ -11176,55 +11188,47 @@ var dataStructureTyped = (() => {
11176
11188
  };
11177
11189
  var RedBlackTree = class _RedBlackTree extends BST {
11178
11190
  /**
11179
- * This is the constructor function for a Red-Black Tree data structure in TypeScript, which
11180
- * initializes the tree with optional nodes and options.
11181
- * @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, NODE>`
11182
- * objects. It represents the initial nodes that will be added to the RBTree during its
11183
- * construction. If this parameter is provided, the `addMany` method is called to add all the
11184
- * nodes to the
11185
- * @param [options] - The `options` parameter is an optional object that allows you to customize the
11186
- * behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide
11187
- * only a subset of the properties defined in the `RBTreeOptions` interface.
11191
+ * This is the constructor function for a Red-Black Tree data structure in TypeScript.
11192
+ * @param keysOrNodesOrEntries - The `keysOrNodesOrEntries` parameter is an iterable object that can
11193
+ * contain keys, nodes, or entries. It is used to initialize the RBTree with the provided keys,
11194
+ * nodes, or entries.
11195
+ * @param [options] - The `options` parameter is an optional object that can be passed to the
11196
+ * constructor. It allows you to customize the behavior of the RBTree. It can include properties such
11197
+ * as `compareKeys`, `compareValues`, `allowDuplicates`, etc. These properties define how the RBTree
11198
+ * should compare keys and
11188
11199
  */
11189
11200
  constructor(keysOrNodesOrEntries = [], options) {
11190
11201
  super([], options);
11191
- __publicField(this, "_Sentinel", new RedBlackTreeNode(NaN));
11202
+ __publicField(this, "_SENTINEL", new RedBlackTreeNode(NaN));
11192
11203
  __publicField(this, "_root");
11193
- __publicField(this, "_size", 0);
11194
- this._root = this._Sentinel;
11195
- if (keysOrNodesOrEntries)
11196
- super.addMany(keysOrNodesOrEntries);
11204
+ this._root = this.SENTINEL;
11205
+ if (keysOrNodesOrEntries) {
11206
+ this.addMany(keysOrNodesOrEntries);
11207
+ }
11197
11208
  }
11198
11209
  /**
11199
- * The function returns the value of the `_Sentinel` property.
11200
- * @returns The method is returning the value of the `_Sentinel` property.
11210
+ * The function returns the value of the _SENTINEL property.
11211
+ * @returns The method is returning the value of the `_SENTINEL` property.
11201
11212
  */
11202
- get Sentinel() {
11203
- return this._Sentinel;
11213
+ get SENTINEL() {
11214
+ return this._SENTINEL;
11204
11215
  }
11205
11216
  /**
11206
- * The function returns the root node.
11207
- * @returns The root node of the data structure.
11217
+ * The function returns the root node of a tree or undefined if there is no root.
11218
+ * @returns The root node of the tree structure, or undefined if there is no root node.
11208
11219
  */
11209
11220
  get root() {
11210
11221
  return this._root;
11211
11222
  }
11212
- /**
11213
- * The function returns the size of an object.
11214
- * @returns The size of the object, which is a number.
11215
- */
11216
- get size() {
11217
- return this._size;
11218
- }
11219
11223
  /**
11220
11224
  * The function creates a new Red-Black Tree node with the specified key, value, and color.
11221
- * @param {K} key - The key parameter is the key value associated with the node. It is used to
11222
- * identify and compare nodes in the Red-Black Tree.
11225
+ * @param {K} key - The key parameter represents the key of the node being created. It is of type K,
11226
+ * which is a generic type representing the key's data type.
11223
11227
  * @param {V} [value] - The `value` parameter is an optional parameter that represents the value
11224
- * associated with the node. It is of type `V`, which is a generic type that can be replaced with any
11225
- * specific type when using the `createNode` method.
11228
+ * associated with the key in the node. It is not required and can be omitted if not needed.
11226
11229
  * @param {RBTNColor} color - The "color" parameter is used to specify the color of the node in a
11227
- * Red-Black Tree. It can be either "RED" or "BLACK". By default, the color is set to "BLACK".
11230
+ * Red-Black Tree. It is an optional parameter with a default value of "RBTNColor.BLACK". The color
11231
+ * can be either "RBTNColor.RED" or "RBTNColor.BLACK".
11228
11232
  * @returns The method is returning a new instance of a RedBlackTreeNode with the specified key,
11229
11233
  * value, and color.
11230
11234
  */
@@ -11232,10 +11236,10 @@ var dataStructureTyped = (() => {
11232
11236
  return new RedBlackTreeNode(key, value, color);
11233
11237
  }
11234
11238
  /**
11235
- * The function creates a Red-Black Tree with the specified options and returns it.
11236
- * @param {RBTreeOptions} [options] - The `options` parameter is an optional object that can be
11237
- * passed to the `createTree` function. It is used to customize the behavior of the `RedBlackTree`
11238
- * class.
11239
+ * The function creates a Red-Black Tree with the given options and returns it.
11240
+ * @param [options] - The `options` parameter is an optional object that contains configuration
11241
+ * options for creating the Red-Black Tree. It is of type `RBTreeOptions<K>`, where `K` represents
11242
+ * the type of keys in the tree.
11239
11243
  * @returns a new instance of a RedBlackTree object.
11240
11244
  */
11241
11245
  createTree(options) {
@@ -11244,12 +11248,18 @@ var dataStructureTyped = (() => {
11244
11248
  }, options));
11245
11249
  }
11246
11250
  /**
11247
- * The function `keyValueOrEntryToNode` takes an keyOrNodeOrEntry and converts it into a node object if possible.
11248
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`, where:
11249
- * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
11250
- * `keyValueOrEntryToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If a value
11251
- * is provided, it will be used when creating the new node. If no value is provided, the new node
11252
- * @returns a node of type NODE or undefined.
11251
+ * Time Complexity: O(1)
11252
+ * Space Complexity: O(1)
11253
+ */
11254
+ /**
11255
+ * Time Complexity: O(1)
11256
+ * Space Complexity: O(1)
11257
+ *
11258
+ * The function `keyValueOrEntryToNode` takes a key, value, or entry and returns a node if it is
11259
+ * valid, otherwise it returns undefined.
11260
+ * @param {KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntry - The key, value, or entry to convert.
11261
+ * @param {V} [value] - The value associated with the key (if `keyOrNodeOrEntry` is a key).
11262
+ * @returns {NODE | undefined} - The corresponding Red-Black Tree node, or `undefined` if conversion fails.
11253
11263
  */
11254
11264
  keyValueOrEntryToNode(keyOrNodeOrEntry, value) {
11255
11265
  let node;
@@ -11272,158 +11282,84 @@ var dataStructureTyped = (() => {
11272
11282
  return node;
11273
11283
  }
11274
11284
  /**
11275
- * The function checks if an keyOrNodeOrEntry is an instance of the RedBlackTreeNode class.
11276
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
11277
- * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the RedBlackTreeNode
11278
- * class.
11279
- */
11285
+ * Time Complexity: O(1)
11286
+ * Space Complexity: O(1)
11287
+ * /
11288
+
11289
+ /**
11290
+ * Time Complexity: O(1)
11291
+ * Space Complexity: O(1)
11292
+ *
11293
+ * The function checks if the input is an instance of the RedBlackTreeNode class.
11294
+ * @param {KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntry - The object to check.
11295
+ * @returns {boolean} - `true` if the object is a Red-Black Tree node, `false` otherwise.
11296
+ */
11280
11297
  isNode(keyOrNodeOrEntry) {
11281
11298
  return keyOrNodeOrEntry instanceof RedBlackTreeNode;
11282
11299
  }
11283
11300
  /**
11301
+ * Time Complexity: O(1)
11302
+ * Space Complexity: O(1)
11303
+ */
11304
+ /**
11305
+ * Time Complexity: O(1)
11306
+ * Space Complexity: O(1)
11307
+ *
11284
11308
  * The function checks if a given node is a real node in a Red-Black Tree.
11285
11309
  * @param {NODE | undefined} node - The `node` parameter is of type `NODE | undefined`, which means
11286
11310
  * it can either be of type `NODE` or `undefined`.
11287
11311
  * @returns a boolean value.
11288
11312
  */
11289
11313
  isRealNode(node) {
11290
- if (node === this._Sentinel || node === void 0)
11314
+ if (node === this.SENTINEL || node === void 0)
11291
11315
  return false;
11292
11316
  return node instanceof RedBlackTreeNode;
11293
11317
  }
11294
11318
  /**
11295
11319
  * Time Complexity: O(log n)
11296
11320
  * Space Complexity: O(1)
11297
- * On average (where n is the number of nodes in the tree)
11298
11321
  */
11299
11322
  /**
11300
11323
  * Time Complexity: O(log n)
11301
11324
  * Space Complexity: O(1)
11302
11325
  *
11303
- * The `add` function adds a new node to a binary search tree and performs necessary rotations and
11304
- * color changes to maintain the red-black tree properties.
11305
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
11306
- * entry.
11307
- * @param {V} [value] - The `value` parameter represents the value associated with the key that is
11308
- * being added to the binary search tree.
11309
- * @returns The method `add` returns either the newly added node (`NODE`) or `undefined`.
11326
+ * The `getNode` function retrieves a node from a Red-Black Tree based on the provided identifier and
11327
+ * callback function.
11328
+ * @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value or key
11329
+ * that you want to search for in the binary search tree. It can be of any type that is compatible
11330
+ * with the type of nodes in the tree.
11331
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
11332
+ * the tree. It is used to determine whether a node matches the given identifier. The `callback`
11333
+ * function should take a node as its parameter and return a value that can be compared to the
11334
+ * `identifier` parameter.
11335
+ * @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
11336
+ * search tree. It can be either a key or a node. If it is a key, it will be converted to a node
11337
+ * using the `ensureNode` method. If it is not provided, the `root`
11338
+ * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
11339
+ * be performed when searching for nodes in the binary search tree. It is an optional parameter and
11340
+ * its default value is taken from the `iterationType` property of the class.
11341
+ * @returns The method is returning a value of type `NODE | null | undefined`.
11310
11342
  */
11311
- add(keyOrNodeOrEntry, value) {
11312
- const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
11313
- if (newNode === void 0)
11314
- return false;
11315
- newNode.left = this._Sentinel;
11316
- newNode.right = this._Sentinel;
11317
- let y = void 0;
11318
- let x = this.root;
11319
- while (x !== this._Sentinel) {
11320
- y = x;
11321
- if (x) {
11322
- if (newNode.key < x.key) {
11323
- x = x.left;
11324
- } else if (newNode.key > x.key) {
11325
- x = x == null ? void 0 : x.right;
11326
- } else {
11327
- if (newNode !== x) {
11328
- this._replaceNode(x, newNode);
11329
- }
11330
- return false;
11331
- }
11332
- }
11333
- }
11334
- newNode.parent = y;
11335
- if (y === void 0) {
11336
- this._setRoot(newNode);
11337
- } else if (newNode.key < y.key) {
11338
- y.left = newNode;
11339
- } else {
11340
- y.right = newNode;
11341
- }
11342
- if (newNode.parent === void 0) {
11343
- newNode.color = 0 /* BLACK */;
11344
- this._size++;
11345
- return false;
11346
- }
11347
- if (newNode.parent.parent === void 0) {
11348
- this._size++;
11349
- return false;
11350
- }
11351
- this._fixInsert(newNode);
11352
- this._size++;
11353
- return true;
11343
+ getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
11344
+ var _a;
11345
+ if (identifier instanceof RedBlackTreeNode)
11346
+ callback = (node) => node;
11347
+ return (_a = super.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) != null ? _a : void 0;
11354
11348
  }
11355
11349
  /**
11356
- * Time Complexity: O(log n)
11350
+ * Time Complexity: O(1)
11357
11351
  * Space Complexity: O(1)
11358
11352
  */
11359
11353
  /**
11360
- * Time Complexity: O(log n)
11354
+ * Time Complexity: O(1)
11361
11355
  * Space Complexity: O(1)
11362
11356
  *
11363
- * The `delete` function removes a node from a binary tree based on a given identifier and updates
11364
- * the tree accordingly.
11365
- * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
11366
- * that you want to use to identify the node that you want to delete from the binary tree. It can be
11367
- * of any type that is returned by the callback function `C`. It can also be `null` or `undefined` if
11368
- * you don't want to
11369
- * @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` and
11370
- * returns a value of type `ReturnType<C>`. It is used to determine if a node should be deleted based
11371
- * on its identifier. The `callback` function is optional and defaults to `this._defaultOneParam
11372
- * @returns an array of `BinaryTreeDeleteResult<NODE>`.
11357
+ * The "clear" function sets the root node of a data structure to a sentinel value and resets the
11358
+ * size counter to zero.
11373
11359
  */
11374
- delete(identifier, callback = this._defaultOneParamCallback) {
11375
- const ans = [];
11376
- if (identifier === null)
11377
- return ans;
11378
- const helper = (node) => {
11379
- let z = this._Sentinel;
11380
- let x, y;
11381
- while (node !== this._Sentinel) {
11382
- if (node && callback(node) === identifier) {
11383
- z = node;
11384
- }
11385
- if (node && identifier && callback(node) <= identifier) {
11386
- node = node.right;
11387
- } else {
11388
- node = node == null ? void 0 : node.left;
11389
- }
11390
- }
11391
- if (z === this._Sentinel) {
11392
- this._size--;
11393
- return;
11394
- }
11395
- y = z;
11396
- let yOriginalColor = y.color;
11397
- if (z.left === this._Sentinel) {
11398
- x = z.right;
11399
- this._rbTransplant(z, z.right);
11400
- } else if (z.right === this._Sentinel) {
11401
- x = z.left;
11402
- this._rbTransplant(z, z.left);
11403
- } else {
11404
- y = this.getLeftMost(z.right);
11405
- yOriginalColor = y.color;
11406
- x = y.right;
11407
- if (y.parent === z) {
11408
- x.parent = y;
11409
- } else {
11410
- this._rbTransplant(y, y.right);
11411
- y.right = z.right;
11412
- y.right.parent = y;
11413
- }
11414
- this._rbTransplant(z, y);
11415
- y.left = z.left;
11416
- y.left.parent = y;
11417
- y.color = z.color;
11418
- }
11419
- if (yOriginalColor === 0 /* BLACK */) {
11420
- this._fixDelete(x);
11421
- }
11422
- this._size--;
11423
- ans.push({ deleted: z, needBalanced: void 0 });
11424
- };
11425
- helper(this.root);
11426
- return ans;
11360
+ clear() {
11361
+ super.clear();
11362
+ this._root = this.SENTINEL;
11427
11363
  }
11428
11364
  /**
11429
11365
  * Time Complexity: O(log n)
@@ -11433,43 +11369,30 @@ var dataStructureTyped = (() => {
11433
11369
  * Time Complexity: O(log n)
11434
11370
  * Space Complexity: O(1)
11435
11371
  *
11436
- * The function `getNode` retrieves a single node from a binary tree based on a given identifier and
11437
- * callback function.
11438
- * @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value used to
11439
- * identify the node you want to retrieve. It can be of any type that is the return type of the `C`
11440
- * callback function. If the `identifier` is `undefined`, it means you want to retrieve the first
11441
- * node that matches the other criteria
11442
- * @param {C} callback - The `callback` parameter is a function that will be called for each node in
11443
- * the binary tree. It is used to determine if a node matches the given identifier. The `callback`
11444
- * function should take a single parameter of type `NODE` (the type of the nodes in the binary tree) and
11445
- * @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter is the starting point for
11446
- * searching for a node in a binary tree. It can be either a key value or a node object. If it is not
11447
- * provided, the search will start from the root of the binary tree.
11448
- * @param iterationType - The `iterationType` parameter is a variable that determines the type of
11449
- * iteration to be performed when searching for nodes in the binary tree. It is used in the
11450
- * `getNodes` method, which is called within the `getNode` method.
11451
- * @returns a value of type `NODE`, `null`, or `undefined`.
11452
- */
11453
- getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
11454
- var _a;
11455
- if (identifier instanceof RedBlackTreeNode)
11456
- callback = (node) => node;
11457
- beginRoot = this.ensureNode(beginRoot);
11458
- return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) != null ? _a : void 0;
11459
- }
11460
- /**
11461
- * Time Complexity: O(1)
11462
- * Space Complexity: O(1)
11463
- */
11464
- /**
11465
- * Time Complexity: O(1)
11466
- * Space Complexity: O(1)
11467
- *
11468
- * The "clear" function sets the root node to the sentinel node and resets the size to 0.
11372
+ * The function adds a new node to a Red-Black Tree data structure and returns a boolean indicating
11373
+ * whether the operation was successful.
11374
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
11375
+ * entry.
11376
+ * @param {V} [value] - The `value` parameter is the value associated with the key that is being
11377
+ * added to the tree.
11378
+ * @returns The method is returning a boolean value. It returns true if the node was successfully
11379
+ * added or updated, and false otherwise.
11469
11380
  */
11470
- clear() {
11471
- this._root = this._Sentinel;
11472
- this._size = 0;
11381
+ add(keyOrNodeOrEntry, value) {
11382
+ const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
11383
+ if (!this.isRealNode(newNode))
11384
+ return false;
11385
+ const insertStatus = this._insert(newNode);
11386
+ if (insertStatus === "CREATED" /* CREATED */) {
11387
+ if (this.isRealNode(this._root)) {
11388
+ this._root.color = 0 /* BLACK */;
11389
+ } else {
11390
+ return false;
11391
+ }
11392
+ this._size++;
11393
+ return true;
11394
+ } else
11395
+ return insertStatus === "UPDATED" /* UPDATED */;
11473
11396
  }
11474
11397
  /**
11475
11398
  * Time Complexity: O(log n)
@@ -11479,27 +11402,69 @@ var dataStructureTyped = (() => {
11479
11402
  * Time Complexity: O(log n)
11480
11403
  * Space Complexity: O(1)
11481
11404
  *
11482
- * The function returns the predecessor of a given node in a red-black tree.
11483
- * @param {RedBlackTreeNode} x - The parameter `x` is of type `RedBlackTreeNode`, which represents a node in a
11484
- * Red-Black Tree.
11485
- * @returns the predecessor of the given RedBlackTreeNode 'x'.
11405
+ * The function `delete` in a binary tree class deletes a node from the tree and fixes the tree if
11406
+ * necessary.
11407
+ * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the
11408
+ * identifier of the node that needs to be deleted from the binary tree. It can be of any type that
11409
+ * is returned by the callback function `C`. It can also be `null` or `undefined` if the node to be
11410
+ * deleted is not found.
11411
+ * @param {C} callback - The `callback` parameter is a function that is used to retrieve a node from
11412
+ * the binary tree based on its identifier. It is an optional parameter and if not provided, the
11413
+ * `_defaultOneParamCallback` function is used as the default callback. The callback function should
11414
+ * return the identifier of the node to
11415
+ * @returns an array of BinaryTreeDeleteResult<NODE> objects.
11486
11416
  */
11487
- getPredecessor(x) {
11488
- if (this.isRealNode(x.left)) {
11489
- return this.getRightMost(x.left);
11417
+ delete(identifier, callback = this._defaultOneParamCallback) {
11418
+ if (identifier === null)
11419
+ return [];
11420
+ const results = [];
11421
+ const nodeToDelete = this.isRealNode(identifier) ? identifier : this.getNode(identifier, callback);
11422
+ if (!nodeToDelete) {
11423
+ return results;
11424
+ }
11425
+ let originalColor = nodeToDelete.color;
11426
+ let replacementNode;
11427
+ if (!this.isRealNode(nodeToDelete.left)) {
11428
+ replacementNode = nodeToDelete.right;
11429
+ this._transplant(nodeToDelete, nodeToDelete.right);
11430
+ } else if (!this.isRealNode(nodeToDelete.right)) {
11431
+ replacementNode = nodeToDelete.left;
11432
+ this._transplant(nodeToDelete, nodeToDelete.left);
11433
+ } else {
11434
+ const successor = this.getLeftMost(nodeToDelete.right);
11435
+ if (successor) {
11436
+ originalColor = successor.color;
11437
+ replacementNode = successor.right;
11438
+ if (successor.parent === nodeToDelete) {
11439
+ if (this.isRealNode(replacementNode)) {
11440
+ replacementNode.parent = successor;
11441
+ }
11442
+ } else {
11443
+ this._transplant(successor, successor.right);
11444
+ successor.right = nodeToDelete.right;
11445
+ if (this.isRealNode(successor.right)) {
11446
+ successor.right.parent = successor;
11447
+ }
11448
+ }
11449
+ this._transplant(nodeToDelete, successor);
11450
+ successor.left = nodeToDelete.left;
11451
+ if (this.isRealNode(successor.left)) {
11452
+ successor.left.parent = successor;
11453
+ }
11454
+ successor.color = nodeToDelete.color;
11455
+ }
11490
11456
  }
11491
- let y = x.parent;
11492
- while (this.isRealNode(y) && x === y.left) {
11493
- x = y;
11494
- y = y.parent;
11457
+ this._size--;
11458
+ if (originalColor === 0 /* BLACK */) {
11459
+ this._deleteFixup(replacementNode);
11495
11460
  }
11496
- return y;
11461
+ results.push({ deleted: nodeToDelete, needBalanced: void 0 });
11462
+ return results;
11497
11463
  }
11498
11464
  /**
11499
- * The function sets the root node of a tree structure and updates the parent property of the new
11500
- * root node.
11501
- * @param {NODE} v - The parameter "v" is of type "NODE", which represents a node in a data
11502
- * structure.
11465
+ * The function sets the root of a tree-like structure and updates the parent property of the new
11466
+ * root.
11467
+ * @param {NODE | undefined} v - v is a parameter of type NODE or undefined.
11503
11468
  */
11504
11469
  _setRoot(v) {
11505
11470
  if (v) {
@@ -11515,28 +11480,61 @@ var dataStructureTyped = (() => {
11515
11480
  * Time Complexity: O(1)
11516
11481
  * Space Complexity: O(1)
11517
11482
  *
11518
- * The function performs a left rotation on a binary tree node.
11519
- * @param {RedBlackTreeNode} x - The parameter `x` is of type `NODE`, which likely represents a node in a binary tree.
11483
+ * The function replaces an old node with a new node while preserving the color of the old node.
11484
+ * @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
11485
+ * the data structure.
11486
+ * @param {NODE} newNode - The `newNode` parameter is the new node that will replace the old node in
11487
+ * the data structure.
11488
+ * @returns The method is returning the result of calling the `_replaceNode` method from the
11489
+ * superclass, with the `oldNode` and `newNode` parameters.
11520
11490
  */
11521
- _leftRotate(x) {
11522
- if (x.right) {
11523
- const y = x.right;
11524
- x.right = y.left;
11525
- if (y.left !== this._Sentinel) {
11526
- if (y.left)
11527
- y.left.parent = x;
11528
- }
11529
- y.parent = x.parent;
11530
- if (x.parent === void 0) {
11531
- this._setRoot(y);
11532
- } else if (x === x.parent.left) {
11533
- x.parent.left = y;
11491
+ _replaceNode(oldNode, newNode) {
11492
+ newNode.color = oldNode.color;
11493
+ return super._replaceNode(oldNode, newNode);
11494
+ }
11495
+ /**
11496
+ * Time Complexity: O(log n)
11497
+ * Space Complexity: O(1)
11498
+ */
11499
+ /**
11500
+ * Time Complexity: O(log n)
11501
+ * Space Complexity: O(1)
11502
+ *
11503
+ * The `_insert` function inserts or updates a node in a binary search tree and performs necessary
11504
+ * fix-ups to maintain the red-black tree properties.
11505
+ * @param {NODE} node - The `node` parameter represents the node that needs to be inserted into a
11506
+ * binary search tree. It contains a `key` property that is used to determine the position of the
11507
+ * node in the tree.
11508
+ * @returns {'inserted' | 'updated'} - The result of the insertion.
11509
+ */
11510
+ _insert(node) {
11511
+ var _a, _b;
11512
+ let current = this.root;
11513
+ let parent = void 0;
11514
+ while (this.isRealNode(current)) {
11515
+ parent = current;
11516
+ if (node.key < current.key) {
11517
+ current = (_a = current.left) != null ? _a : this.SENTINEL;
11518
+ } else if (node.key > current.key) {
11519
+ current = (_b = current.right) != null ? _b : this.SENTINEL;
11534
11520
  } else {
11535
- x.parent.right = y;
11521
+ this._replaceNode(current, node);
11522
+ return "UPDATED" /* UPDATED */;
11536
11523
  }
11537
- y.left = x;
11538
- x.parent = y;
11539
11524
  }
11525
+ node.parent = parent;
11526
+ if (!parent) {
11527
+ this._setRoot(node);
11528
+ } else if (node.key < parent.key) {
11529
+ parent.left = node;
11530
+ } else {
11531
+ parent.right = node;
11532
+ }
11533
+ node.left = this.SENTINEL;
11534
+ node.right = this.SENTINEL;
11535
+ node.color = 1 /* RED */;
11536
+ this._insertFixup(node);
11537
+ return "CREATED" /* CREATED */;
11540
11538
  }
11541
11539
  /**
11542
11540
  * Time Complexity: O(1)
@@ -11546,28 +11544,21 @@ var dataStructureTyped = (() => {
11546
11544
  * Time Complexity: O(1)
11547
11545
  * Space Complexity: O(1)
11548
11546
  *
11549
- * The function performs a right rotation on a red-black tree node.
11550
- * @param {RedBlackTreeNode} x - x is a RedBlackTreeNode, which represents the node that needs to be right
11551
- * rotated.
11547
+ * The function `_transplant` is used to replace a node `u` with another node `v` in a binary tree.
11548
+ * @param {NODE} u - The parameter "u" represents a node in a binary tree.
11549
+ * @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`, which means it can
11550
+ * either be a `NODE` object or `undefined`.
11552
11551
  */
11553
- _rightRotate(x) {
11554
- if (x.left) {
11555
- const y = x.left;
11556
- x.left = y.right;
11557
- if (y.right !== this._Sentinel) {
11558
- if (y.right)
11559
- y.right.parent = x;
11560
- }
11561
- y.parent = x.parent;
11562
- if (x.parent === void 0) {
11563
- this._setRoot(y);
11564
- } else if (x === x.parent.right) {
11565
- x.parent.right = y;
11566
- } else {
11567
- x.parent.left = y;
11568
- }
11569
- y.right = x;
11570
- x.parent = y;
11552
+ _transplant(u, v) {
11553
+ if (!u.parent) {
11554
+ this._setRoot(v);
11555
+ } else if (u === u.parent.left) {
11556
+ u.parent.left = v;
11557
+ } else {
11558
+ u.parent.right = v;
11559
+ }
11560
+ if (v) {
11561
+ v.parent = u.parent;
11571
11562
  }
11572
11563
  }
11573
11564
  /**
@@ -11578,55 +11569,53 @@ var dataStructureTyped = (() => {
11578
11569
  * Time Complexity: O(log n)
11579
11570
  * Space Complexity: O(1)
11580
11571
  *
11581
- * The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
11582
- * @param {RedBlackTreeNode} k - The parameter `k` is a RedBlackTreeNode object, which represents a node in a
11583
- * red-black tree.
11584
- */
11585
- _fixInsert(k) {
11586
- let u;
11587
- while (k.parent && k.parent.color === 1 /* RED */) {
11588
- if (k.parent.parent && k.parent === k.parent.parent.right) {
11589
- u = k.parent.parent.left;
11590
- if (u && u.color === 1 /* RED */) {
11591
- k.parent.color = 0 /* BLACK */;
11592
- u.color = 0 /* BLACK */;
11593
- k.parent.parent.color = 1 /* RED */;
11594
- k = k.parent.parent;
11572
+ * The `_insertFixup` function is used to fix the Red-Black Tree after inserting a new node.
11573
+ * @param {NODE | undefined} z - The parameter `z` represents a node in the Red-Black Tree. It can
11574
+ * either be a valid node object or `undefined`.
11575
+ */
11576
+ _insertFixup(z) {
11577
+ var _a, _b, _c, _d;
11578
+ while (((_a = z == null ? void 0 : z.parent) == null ? void 0 : _a.color) === 1 /* RED */) {
11579
+ if (z.parent === ((_b = z.parent.parent) == null ? void 0 : _b.left)) {
11580
+ const y = z.parent.parent.right;
11581
+ if ((y == null ? void 0 : y.color) === 1 /* RED */) {
11582
+ z.parent.color = 0 /* BLACK */;
11583
+ y.color = 0 /* BLACK */;
11584
+ z.parent.parent.color = 1 /* RED */;
11585
+ z = z.parent.parent;
11595
11586
  } else {
11596
- if (k === k.parent.left) {
11597
- k = k.parent;
11598
- this._rightRotate(k);
11587
+ if (z === z.parent.right) {
11588
+ z = z.parent;
11589
+ this._leftRotate(z);
11599
11590
  }
11600
- if (k.parent.color === 1 /* RED */) {
11601
- k.parent.color = 0 /* BLACK */;
11602
- k.parent.parent.color = 1 /* RED */;
11591
+ if (z && this.isRealNode(z.parent) && this.isRealNode(z.parent.parent)) {
11592
+ z.parent.color = 0 /* BLACK */;
11593
+ z.parent.parent.color = 1 /* RED */;
11594
+ this._rightRotate(z.parent.parent);
11603
11595
  }
11604
- this._leftRotate(k.parent.parent);
11605
11596
  }
11606
11597
  } else {
11607
- u = k.parent.parent.right;
11608
- if (u && u.color === 1 /* RED */) {
11609
- k.parent.color = 0 /* BLACK */;
11610
- u.color = 0 /* BLACK */;
11611
- k.parent.parent.color = 1 /* RED */;
11612
- k = k.parent.parent;
11598
+ const y = (_d = (_c = z == null ? void 0 : z.parent) == null ? void 0 : _c.parent) == null ? void 0 : _d.left;
11599
+ if ((y == null ? void 0 : y.color) === 1 /* RED */) {
11600
+ z.parent.color = 0 /* BLACK */;
11601
+ y.color = 0 /* BLACK */;
11602
+ z.parent.parent.color = 1 /* RED */;
11603
+ z = z.parent.parent;
11613
11604
  } else {
11614
- if (k === k.parent.right) {
11615
- k = k.parent;
11616
- this._leftRotate(k);
11605
+ if (z === z.parent.left) {
11606
+ z = z.parent;
11607
+ this._rightRotate(z);
11617
11608
  }
11618
- if (k.parent.color === 1 /* RED */) {
11619
- k.parent.color = 0 /* BLACK */;
11620
- k.parent.parent.color = 1 /* RED */;
11609
+ if (z && this.isRealNode(z.parent) && this.isRealNode(z.parent.parent)) {
11610
+ z.parent.color = 0 /* BLACK */;
11611
+ z.parent.parent.color = 1 /* RED */;
11612
+ this._leftRotate(z.parent.parent);
11621
11613
  }
11622
- this._rightRotate(k.parent.parent);
11623
11614
  }
11624
11615
  }
11625
- if (k === this.root) {
11626
- break;
11627
- }
11628
11616
  }
11629
- this.root.color = 0 /* BLACK */;
11617
+ if (this.isRealNode(this._root))
11618
+ this._root.color = 0 /* BLACK */;
11630
11619
  }
11631
11620
  /**
11632
11621
  * Time Complexity: O(log n)
@@ -11636,69 +11625,75 @@ var dataStructureTyped = (() => {
11636
11625
  * Time Complexity: O(log n)
11637
11626
  * Space Complexity: O(1)
11638
11627
  *
11639
- * The function `_fixDelete` is used to fix the red-black tree after a node deletion.
11640
- * @param {RedBlackTreeNode} x - The parameter `x` represents a node in a Red-Black Tree (RBT).
11628
+ * The `_deleteFixup` function is used to fix the red-black tree after a node deletion by adjusting
11629
+ * the colors and performing rotations.
11630
+ * @param {NODE | undefined} node - The `node` parameter represents a node in a Red-Black Tree data
11631
+ * structure. It can be either a valid node object or `undefined`.
11632
+ * @returns The function does not return any value. It has a return type of `void`.
11641
11633
  */
11642
- _fixDelete(x) {
11643
- let s;
11644
- while (x !== this.root && x.color === 0 /* BLACK */) {
11645
- if (x.parent && x === x.parent.left) {
11646
- s = x.parent.right;
11647
- if (s.color === 1) {
11648
- s.color = 0 /* BLACK */;
11649
- x.parent.color = 1 /* RED */;
11650
- this._leftRotate(x.parent);
11651
- s = x.parent.right;
11634
+ _deleteFixup(node) {
11635
+ var _a, _b, _c, _d;
11636
+ if (!node || node === this.root || node.color === 0 /* BLACK */) {
11637
+ if (node) {
11638
+ node.color = 0 /* BLACK */;
11639
+ }
11640
+ return;
11641
+ }
11642
+ while (node && node !== this.root && node.color === 0 /* BLACK */) {
11643
+ const parent = node.parent;
11644
+ if (!parent) {
11645
+ break;
11646
+ }
11647
+ if (node === parent.left) {
11648
+ let sibling = parent.right;
11649
+ if ((sibling == null ? void 0 : sibling.color) === 1 /* RED */) {
11650
+ sibling.color = 0 /* BLACK */;
11651
+ parent.color = 1 /* RED */;
11652
+ this._leftRotate(parent);
11653
+ sibling = parent.right;
11652
11654
  }
11653
- if (s.left !== void 0 && s.left.color === 0 /* BLACK */ && s.right && s.right.color === 0 /* BLACK */) {
11654
- s.color = 1 /* RED */;
11655
- x = x.parent;
11655
+ if (((_b = (_a = sibling == null ? void 0 : sibling.left) == null ? void 0 : _a.color) != null ? _b : 0 /* BLACK */) === 0 /* BLACK */) {
11656
+ if (sibling)
11657
+ sibling.color = 1 /* RED */;
11658
+ node = parent;
11656
11659
  } else {
11657
- if (s.right && s.right.color === 0 /* BLACK */) {
11658
- if (s.left)
11659
- s.left.color = 0 /* BLACK */;
11660
- s.color = 1 /* RED */;
11661
- this._rightRotate(s);
11662
- s = x.parent.right;
11663
- }
11664
- if (s)
11665
- s.color = x.parent.color;
11666
- x.parent.color = 0 /* BLACK */;
11667
- if (s && s.right)
11668
- s.right.color = 0 /* BLACK */;
11669
- this._leftRotate(x.parent);
11670
- x = this.root;
11660
+ if (sibling == null ? void 0 : sibling.left)
11661
+ sibling.left.color = 0 /* BLACK */;
11662
+ if (sibling)
11663
+ sibling.color = parent.color;
11664
+ parent.color = 0 /* BLACK */;
11665
+ this._rightRotate(parent);
11666
+ node = this.root;
11671
11667
  }
11672
11668
  } else {
11673
- s = x.parent.left;
11674
- if (s.color === 1) {
11675
- s.color = 0 /* BLACK */;
11676
- x.parent.color = 1 /* RED */;
11677
- this._rightRotate(x.parent);
11678
- s = x.parent.left;
11669
+ let sibling = parent.left;
11670
+ if ((sibling == null ? void 0 : sibling.color) === 1 /* RED */) {
11671
+ sibling.color = 0 /* BLACK */;
11672
+ if (parent)
11673
+ parent.color = 1 /* RED */;
11674
+ this._rightRotate(parent);
11675
+ if (parent)
11676
+ sibling = parent.left;
11679
11677
  }
11680
- if (s && s.right && s.right.color === 0 /* BLACK */ && s.right.color === 0 /* BLACK */) {
11681
- s.color = 1 /* RED */;
11682
- x = x.parent;
11678
+ if (((_d = (_c = sibling == null ? void 0 : sibling.right) == null ? void 0 : _c.color) != null ? _d : 0 /* BLACK */) === 0 /* BLACK */) {
11679
+ if (sibling)
11680
+ sibling.color = 1 /* RED */;
11681
+ node = parent;
11683
11682
  } else {
11684
- if (s && s.left && s.left.color === 0 /* BLACK */) {
11685
- if (s.right)
11686
- s.right.color = 0 /* BLACK */;
11687
- s.color = 1 /* RED */;
11688
- this._leftRotate(s);
11689
- s = x.parent.left;
11690
- }
11691
- if (s)
11692
- s.color = x.parent.color;
11693
- x.parent.color = 0 /* BLACK */;
11694
- if (s && s.left)
11695
- s.left.color = 0 /* BLACK */;
11696
- this._rightRotate(x.parent);
11697
- x = this.root;
11683
+ if (sibling == null ? void 0 : sibling.right)
11684
+ sibling.right.color = 0 /* BLACK */;
11685
+ if (sibling)
11686
+ sibling.color = parent.color;
11687
+ if (parent)
11688
+ parent.color = 0 /* BLACK */;
11689
+ this._leftRotate(parent);
11690
+ node = this.root;
11698
11691
  }
11699
11692
  }
11700
11693
  }
11701
- x.color = 0 /* BLACK */;
11694
+ if (node) {
11695
+ node.color = 0 /* BLACK */;
11696
+ }
11702
11697
  }
11703
11698
  /**
11704
11699
  * Time Complexity: O(1)
@@ -11708,32 +11703,63 @@ var dataStructureTyped = (() => {
11708
11703
  * Time Complexity: O(1)
11709
11704
  * Space Complexity: O(1)
11710
11705
  *
11711
- * The function `_rbTransplant` replaces one node in a red-black tree with another node.
11712
- * @param {RedBlackTreeNode} u - The parameter "u" represents a RedBlackTreeNode object.
11713
- * @param {RedBlackTreeNode} v - The parameter "v" is a RedBlackTreeNode object.
11706
+ * The `_leftRotate` function performs a left rotation on a given node in a binary tree.
11707
+ * @param {NODE | undefined} x - The parameter `x` is of type `NODE | undefined`. It represents a
11708
+ * node in a binary tree or `undefined` if there is no node.
11709
+ * @returns void, which means it does not return any value.
11714
11710
  */
11715
- _rbTransplant(u, v) {
11716
- if (u.parent === void 0) {
11717
- this._setRoot(v);
11718
- } else if (u === u.parent.left) {
11719
- u.parent.left = v;
11711
+ _leftRotate(x) {
11712
+ if (!x || !x.right) {
11713
+ return;
11714
+ }
11715
+ const y = x.right;
11716
+ x.right = y.left;
11717
+ if (this.isRealNode(y.left)) {
11718
+ y.left.parent = x;
11719
+ }
11720
+ y.parent = x.parent;
11721
+ if (!x.parent) {
11722
+ this._setRoot(y);
11723
+ } else if (x === x.parent.left) {
11724
+ x.parent.left = y;
11720
11725
  } else {
11721
- u.parent.right = v;
11726
+ x.parent.right = y;
11722
11727
  }
11723
- v.parent = u.parent;
11728
+ y.left = x;
11729
+ x.parent = y;
11724
11730
  }
11725
11731
  /**
11726
- * The function replaces an old node with a new node while preserving the color of the old node.
11727
- * @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in a
11728
- * data structure. It is of type `NODE`, which is the type of the nodes in the data structure.
11729
- * @param {NODE} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
11730
- * data structure.
11731
- * @returns The method is returning the result of calling the `_replaceNode` method from the
11732
- * superclass, passing in the `oldNode` and `newNode` as arguments.
11732
+ * Time Complexity: O(1)
11733
+ * Space Complexity: O(1)
11733
11734
  */
11734
- _replaceNode(oldNode, newNode) {
11735
- newNode.color = oldNode.color;
11736
- return super._replaceNode(oldNode, newNode);
11735
+ /**
11736
+ * Time Complexity: O(1)
11737
+ * Space Complexity: O(1)
11738
+ *
11739
+ * The `_rightRotate` function performs a right rotation on a given node in a binary tree.
11740
+ * @param {NODE | undefined} y - The parameter `y` is of type `NODE | undefined`. It represents a
11741
+ * node in a binary tree or `undefined` if there is no node.
11742
+ * @returns void, which means it does not return any value.
11743
+ */
11744
+ _rightRotate(y) {
11745
+ if (!y || !y.left) {
11746
+ return;
11747
+ }
11748
+ const x = y.left;
11749
+ y.left = x.right;
11750
+ if (this.isRealNode(x.right)) {
11751
+ x.right.parent = y;
11752
+ }
11753
+ x.parent = y.parent;
11754
+ if (!y.parent) {
11755
+ this._setRoot(x);
11756
+ } else if (y === y.parent.left) {
11757
+ y.parent.left = x;
11758
+ } else {
11759
+ y.parent.right = x;
11760
+ }
11761
+ x.right = y;
11762
+ y.parent = x;
11737
11763
  }
11738
11764
  };
11739
11765
 
@@ -11784,6 +11810,9 @@ var dataStructureTyped = (() => {
11784
11810
  * @returns the sum of the count property of all nodes in the tree.
11785
11811
  */
11786
11812
  get count() {
11813
+ return this._count;
11814
+ }
11815
+ getMutableCount() {
11787
11816
  let sum = 0;
11788
11817
  this.dfs((node) => sum += node.count);
11789
11818
  return sum;
@@ -12140,6 +12169,9 @@ var dataStructureTyped = (() => {
12140
12169
  * @returns the sum of the count property of all nodes in the tree.
12141
12170
  */
12142
12171
  get count() {
12172
+ return this._count;
12173
+ }
12174
+ getMutableCount() {
12143
12175
  let sum = 0;
12144
12176
  this.dfs((node) => sum += node.count);
12145
12177
  return sum;
@@ -12232,14 +12264,14 @@ var dataStructureTyped = (() => {
12232
12264
  */
12233
12265
  add(keyOrNodeOrEntry, value, count = 1) {
12234
12266
  const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value, count);
12235
- if (newNode === void 0)
12267
+ const orgCount = (newNode == null ? void 0 : newNode.count) || 0;
12268
+ const isSuccessAdded = super.add(newNode);
12269
+ if (isSuccessAdded) {
12270
+ this._count += orgCount;
12271
+ return true;
12272
+ } else {
12236
12273
  return false;
12237
- const orgNodeCount = (newNode == null ? void 0 : newNode.count) || 0;
12238
- const inserted = super.add(newNode);
12239
- if (inserted) {
12240
- this._count += orgNodeCount;
12241
12274
  }
12242
- return true;
12243
12275
  }
12244
12276
  /**
12245
12277
  * Time Complexity: O(log n)
@@ -12266,63 +12298,83 @@ var dataStructureTyped = (() => {
12266
12298
  * @returns an array of BinaryTreeDeleteResult<NODE> objects.
12267
12299
  */
12268
12300
  delete(identifier, callback = this._defaultOneParamCallback, ignoreCount = false) {
12269
- const deleteResults = [];
12270
12301
  if (identifier === null)
12271
- return deleteResults;
12272
- const deleteHelper = (node) => {
12273
- let targetNode = this._Sentinel;
12274
- let currentNode;
12275
- while (node !== this._Sentinel) {
12276
- if (node && callback(node) === identifier) {
12277
- targetNode = node;
12278
- }
12279
- if (node && identifier && callback(node) <= identifier) {
12280
- node = node.right;
12281
- } else {
12282
- node = node == null ? void 0 : node.left;
12283
- }
12284
- }
12285
- if (targetNode === this._Sentinel) {
12286
- return;
12302
+ return [];
12303
+ const results = [];
12304
+ const nodeToDelete = this.isRealNode(identifier) ? identifier : this.getNode(identifier, callback);
12305
+ if (!nodeToDelete) {
12306
+ return results;
12307
+ }
12308
+ let originalColor = nodeToDelete.color;
12309
+ let replacementNode;
12310
+ if (!this.isRealNode(nodeToDelete.left)) {
12311
+ replacementNode = nodeToDelete.right;
12312
+ if (ignoreCount || nodeToDelete.count <= 1) {
12313
+ this._transplant(nodeToDelete, nodeToDelete.right);
12314
+ this._count -= nodeToDelete.count;
12315
+ } else {
12316
+ nodeToDelete.count--;
12317
+ this._count--;
12318
+ results.push({ deleted: nodeToDelete, needBalanced: void 0 });
12319
+ return results;
12320
+ }
12321
+ } else if (!this.isRealNode(nodeToDelete.right)) {
12322
+ replacementNode = nodeToDelete.left;
12323
+ if (ignoreCount || nodeToDelete.count <= 1) {
12324
+ this._transplant(nodeToDelete, nodeToDelete.left);
12325
+ this._count -= nodeToDelete.count;
12326
+ } else {
12327
+ nodeToDelete.count--;
12328
+ this._count--;
12329
+ results.push({ deleted: nodeToDelete, needBalanced: void 0 });
12330
+ return results;
12287
12331
  }
12288
- if (ignoreCount || targetNode.count <= 1) {
12289
- let parentNode = targetNode;
12290
- let parentNodeOriginalColor = parentNode.color;
12291
- if (targetNode.left === this._Sentinel) {
12292
- currentNode = targetNode.right;
12293
- this._rbTransplant(targetNode, targetNode.right);
12294
- } else if (targetNode.right === this._Sentinel) {
12295
- currentNode = targetNode.left;
12296
- this._rbTransplant(targetNode, targetNode.left);
12332
+ } else {
12333
+ const successor = this.getLeftMost(nodeToDelete.right);
12334
+ if (successor) {
12335
+ originalColor = successor.color;
12336
+ replacementNode = successor.right;
12337
+ if (successor.parent === nodeToDelete) {
12338
+ if (this.isRealNode(replacementNode)) {
12339
+ replacementNode.parent = successor;
12340
+ }
12297
12341
  } else {
12298
- parentNode = this.getLeftMost(targetNode.right);
12299
- parentNodeOriginalColor = parentNode.color;
12300
- currentNode = parentNode.right;
12301
- if (parentNode.parent === targetNode) {
12302
- currentNode.parent = parentNode;
12342
+ if (ignoreCount || nodeToDelete.count <= 1) {
12343
+ this._transplant(successor, successor.right);
12344
+ this._count -= nodeToDelete.count;
12303
12345
  } else {
12304
- this._rbTransplant(parentNode, parentNode.right);
12305
- parentNode.right = targetNode.right;
12306
- parentNode.right.parent = parentNode;
12346
+ nodeToDelete.count--;
12347
+ this._count--;
12348
+ results.push({ deleted: nodeToDelete, needBalanced: void 0 });
12349
+ return results;
12350
+ }
12351
+ successor.right = nodeToDelete.right;
12352
+ if (this.isRealNode(successor.right)) {
12353
+ successor.right.parent = successor;
12307
12354
  }
12308
- this._rbTransplant(targetNode, parentNode);
12309
- parentNode.left = targetNode.left;
12310
- parentNode.left.parent = parentNode;
12311
- parentNode.color = targetNode.color;
12312
12355
  }
12313
- if (parentNodeOriginalColor === 0 /* BLACK */) {
12314
- this._fixDelete(currentNode);
12356
+ if (ignoreCount || nodeToDelete.count <= 1) {
12357
+ this._transplant(nodeToDelete, successor);
12358
+ this._count -= nodeToDelete.count;
12359
+ } else {
12360
+ nodeToDelete.count--;
12361
+ this._count--;
12362
+ results.push({ deleted: nodeToDelete, needBalanced: void 0 });
12363
+ return results;
12315
12364
  }
12316
- this._size--;
12317
- this._count -= targetNode.count;
12318
- deleteResults.push({ deleted: targetNode, needBalanced: void 0 });
12319
- } else {
12320
- targetNode.count--;
12321
- this._count--;
12365
+ successor.left = nodeToDelete.left;
12366
+ if (this.isRealNode(successor.left)) {
12367
+ successor.left.parent = successor;
12368
+ }
12369
+ successor.color = nodeToDelete.color;
12322
12370
  }
12323
- };
12324
- deleteHelper(this.root);
12325
- return deleteResults;
12371
+ }
12372
+ this._size--;
12373
+ if (originalColor === 0 /* BLACK */) {
12374
+ this._deleteFixup(replacementNode);
12375
+ }
12376
+ results.push({ deleted: nodeToDelete, needBalanced: void 0 });
12377
+ return results;
12326
12378
  }
12327
12379
  /**
12328
12380
  * Time Complexity: O(1)