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.
- package/CHANGELOG.md +1 -1
- package/README.md +13 -18
- package/benchmark/report.html +13 -13
- package/benchmark/report.json +167 -143
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +3 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +32 -28
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.js +17 -17
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +158 -141
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +416 -396
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +1 -0
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +84 -76
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/types/common.d.ts +6 -0
- package/dist/cjs/types/common.js +8 -1
- package/dist/cjs/types/common.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -0
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +3 -0
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +32 -28
- package/dist/mjs/data-structures/binary-tree/bst.js +17 -17
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +158 -141
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +413 -396
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +1 -0
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +84 -76
- package/dist/mjs/types/common.d.ts +6 -0
- package/dist/mjs/types/common.js +7 -0
- package/dist/umd/data-structure-typed.js +519 -467
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- 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
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +26 -16
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +4 -1
- package/test/unit/data-structures/binary-tree/overall.test.ts +23 -21
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +387 -324
- 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
|
|
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
|
|
11180
|
-
*
|
|
11181
|
-
*
|
|
11182
|
-
*
|
|
11183
|
-
*
|
|
11184
|
-
*
|
|
11185
|
-
*
|
|
11186
|
-
*
|
|
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, "
|
|
11202
|
+
__publicField(this, "_SENTINEL", new RedBlackTreeNode(NaN));
|
|
11192
11203
|
__publicField(this, "_root");
|
|
11193
|
-
|
|
11194
|
-
|
|
11195
|
-
|
|
11196
|
-
|
|
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
|
|
11200
|
-
* @returns The method is returning the value of the `
|
|
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
|
|
11203
|
-
return this.
|
|
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
|
|
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
|
|
11222
|
-
*
|
|
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
|
|
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
|
|
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
|
|
11236
|
-
* @param
|
|
11237
|
-
*
|
|
11238
|
-
*
|
|
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
|
-
*
|
|
11248
|
-
*
|
|
11249
|
-
|
|
11250
|
-
|
|
11251
|
-
*
|
|
11252
|
-
*
|
|
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
|
-
|
|
11276
|
-
|
|
11277
|
-
|
|
11278
|
-
|
|
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.
|
|
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 `
|
|
11304
|
-
*
|
|
11305
|
-
* @param
|
|
11306
|
-
*
|
|
11307
|
-
*
|
|
11308
|
-
*
|
|
11309
|
-
*
|
|
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
|
-
|
|
11312
|
-
|
|
11313
|
-
if (
|
|
11314
|
-
|
|
11315
|
-
|
|
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(
|
|
11350
|
+
* Time Complexity: O(1)
|
|
11357
11351
|
* Space Complexity: O(1)
|
|
11358
11352
|
*/
|
|
11359
11353
|
/**
|
|
11360
|
-
* Time Complexity: O(
|
|
11354
|
+
* Time Complexity: O(1)
|
|
11361
11355
|
* Space Complexity: O(1)
|
|
11362
11356
|
*
|
|
11363
|
-
* The
|
|
11364
|
-
*
|
|
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
|
-
|
|
11375
|
-
|
|
11376
|
-
|
|
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
|
|
11437
|
-
*
|
|
11438
|
-
* @param
|
|
11439
|
-
*
|
|
11440
|
-
*
|
|
11441
|
-
*
|
|
11442
|
-
* @
|
|
11443
|
-
*
|
|
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
|
-
|
|
11471
|
-
|
|
11472
|
-
this.
|
|
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
|
|
11483
|
-
*
|
|
11484
|
-
*
|
|
11485
|
-
*
|
|
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
|
-
|
|
11488
|
-
if (
|
|
11489
|
-
return
|
|
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
|
-
|
|
11492
|
-
|
|
11493
|
-
|
|
11494
|
-
y = y.parent;
|
|
11457
|
+
this._size--;
|
|
11458
|
+
if (originalColor === 0 /* BLACK */) {
|
|
11459
|
+
this._deleteFixup(replacementNode);
|
|
11495
11460
|
}
|
|
11496
|
-
|
|
11461
|
+
results.push({ deleted: nodeToDelete, needBalanced: void 0 });
|
|
11462
|
+
return results;
|
|
11497
11463
|
}
|
|
11498
11464
|
/**
|
|
11499
|
-
* The function sets the root
|
|
11500
|
-
* root
|
|
11501
|
-
* @param {NODE} v -
|
|
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
|
|
11519
|
-
* @param {
|
|
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
|
-
|
|
11522
|
-
|
|
11523
|
-
|
|
11524
|
-
|
|
11525
|
-
|
|
11526
|
-
|
|
11527
|
-
|
|
11528
|
-
|
|
11529
|
-
|
|
11530
|
-
|
|
11531
|
-
|
|
11532
|
-
|
|
11533
|
-
|
|
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
|
-
|
|
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
|
|
11550
|
-
* @param {
|
|
11551
|
-
*
|
|
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
|
-
|
|
11554
|
-
if (
|
|
11555
|
-
|
|
11556
|
-
|
|
11557
|
-
|
|
11558
|
-
|
|
11559
|
-
|
|
11560
|
-
|
|
11561
|
-
|
|
11562
|
-
|
|
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 `
|
|
11582
|
-
* @param {
|
|
11583
|
-
*
|
|
11584
|
-
*/
|
|
11585
|
-
|
|
11586
|
-
|
|
11587
|
-
while (
|
|
11588
|
-
if (
|
|
11589
|
-
|
|
11590
|
-
if (
|
|
11591
|
-
|
|
11592
|
-
|
|
11593
|
-
|
|
11594
|
-
|
|
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 (
|
|
11597
|
-
|
|
11598
|
-
this.
|
|
11587
|
+
if (z === z.parent.right) {
|
|
11588
|
+
z = z.parent;
|
|
11589
|
+
this._leftRotate(z);
|
|
11599
11590
|
}
|
|
11600
|
-
if (
|
|
11601
|
-
|
|
11602
|
-
|
|
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
|
-
|
|
11608
|
-
if (
|
|
11609
|
-
|
|
11610
|
-
|
|
11611
|
-
|
|
11612
|
-
|
|
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 (
|
|
11615
|
-
|
|
11616
|
-
this.
|
|
11605
|
+
if (z === z.parent.left) {
|
|
11606
|
+
z = z.parent;
|
|
11607
|
+
this._rightRotate(z);
|
|
11617
11608
|
}
|
|
11618
|
-
if (
|
|
11619
|
-
|
|
11620
|
-
|
|
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.
|
|
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
|
|
11640
|
-
*
|
|
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
|
-
|
|
11643
|
-
|
|
11644
|
-
|
|
11645
|
-
if (
|
|
11646
|
-
|
|
11647
|
-
|
|
11648
|
-
|
|
11649
|
-
|
|
11650
|
-
|
|
11651
|
-
|
|
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 (
|
|
11654
|
-
|
|
11655
|
-
|
|
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 (
|
|
11658
|
-
|
|
11659
|
-
|
|
11660
|
-
|
|
11661
|
-
|
|
11662
|
-
|
|
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
|
-
|
|
11674
|
-
if (
|
|
11675
|
-
|
|
11676
|
-
|
|
11677
|
-
|
|
11678
|
-
|
|
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 (
|
|
11681
|
-
|
|
11682
|
-
|
|
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 (
|
|
11685
|
-
|
|
11686
|
-
|
|
11687
|
-
|
|
11688
|
-
|
|
11689
|
-
|
|
11690
|
-
|
|
11691
|
-
|
|
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
|
-
|
|
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
|
|
11712
|
-
* @param {
|
|
11713
|
-
*
|
|
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
|
-
|
|
11716
|
-
if (
|
|
11717
|
-
|
|
11718
|
-
}
|
|
11719
|
-
|
|
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
|
-
|
|
11726
|
+
x.parent.right = y;
|
|
11722
11727
|
}
|
|
11723
|
-
|
|
11728
|
+
y.left = x;
|
|
11729
|
+
x.parent = y;
|
|
11724
11730
|
}
|
|
11725
11731
|
/**
|
|
11726
|
-
*
|
|
11727
|
-
*
|
|
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
|
-
|
|
11735
|
-
|
|
11736
|
-
|
|
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
|
-
|
|
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
|
|
12272
|
-
const
|
|
12273
|
-
|
|
12274
|
-
|
|
12275
|
-
|
|
12276
|
-
|
|
12277
|
-
|
|
12278
|
-
|
|
12279
|
-
|
|
12280
|
-
|
|
12281
|
-
|
|
12282
|
-
|
|
12283
|
-
|
|
12284
|
-
}
|
|
12285
|
-
|
|
12286
|
-
|
|
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
|
-
|
|
12289
|
-
|
|
12290
|
-
|
|
12291
|
-
|
|
12292
|
-
|
|
12293
|
-
|
|
12294
|
-
|
|
12295
|
-
|
|
12296
|
-
|
|
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
|
-
|
|
12299
|
-
|
|
12300
|
-
|
|
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
|
-
|
|
12305
|
-
|
|
12306
|
-
|
|
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 (
|
|
12314
|
-
this.
|
|
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
|
-
|
|
12317
|
-
this.
|
|
12318
|
-
|
|
12319
|
-
|
|
12320
|
-
|
|
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
|
-
|
|
12325
|
-
|
|
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)
|