tree-multimap-typed 2.1.2 → 2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.cjs +433 -193
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +4300 -0
- package/dist/cjs-legacy/index.cjs.map +1 -0
- package/dist/esm/index.mjs +433 -193
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +4289 -0
- package/dist/esm-legacy/index.mjs.map +1 -0
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +57 -3
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +65 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +61 -5
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -0
- package/dist/types/data-structures/binary-tree/bst.d.ts +58 -3
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +59 -4
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +57 -3
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +66 -3
- package/dist/types/types/data-structures/base/base.d.ts +1 -1
- package/dist/umd/tree-multimap-typed.js +268 -26
- package/dist/umd/tree-multimap-typed.js.map +1 -1
- package/dist/umd/tree-multimap-typed.min.js +3 -3
- package/dist/umd/tree-multimap-typed.min.js.map +1 -1
- package/package.json +20 -2
- package/src/data-structures/base/iterable-entry-base.ts +4 -4
- package/src/data-structures/binary-tree/avl-tree-counter.ts +103 -12
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +116 -12
- package/src/data-structures/binary-tree/avl-tree.ts +109 -16
- package/src/data-structures/binary-tree/binary-tree.ts +3 -2
- package/src/data-structures/binary-tree/bst.ts +104 -12
- package/src/data-structures/binary-tree/red-black-tree.ts +110 -19
- package/src/data-structures/binary-tree/tree-counter.ts +102 -11
- package/src/data-structures/binary-tree/tree-multi-map.ts +124 -12
- package/src/data-structures/graph/abstract-graph.ts +8 -8
- package/src/data-structures/graph/directed-graph.ts +5 -5
- package/src/data-structures/graph/undirected-graph.ts +5 -5
- package/src/data-structures/hash/hash-map.ts +4 -4
- package/src/types/data-structures/base/base.ts +1 -1
- package/tsup.node.config.js +40 -6
package/dist/esm/index.mjs
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
2
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
4
|
-
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
3
|
|
|
6
4
|
// src/utils/utils.ts
|
|
7
5
|
function isPrimitiveComparable(value) {
|
|
@@ -60,7 +58,10 @@ function makeTrampoline(fn) {
|
|
|
60
58
|
__name(makeTrampoline, "makeTrampoline");
|
|
61
59
|
|
|
62
60
|
// src/data-structures/base/iterable-element-base.ts
|
|
63
|
-
var
|
|
61
|
+
var IterableElementBase = class {
|
|
62
|
+
static {
|
|
63
|
+
__name(this, "IterableElementBase");
|
|
64
|
+
}
|
|
64
65
|
/**
|
|
65
66
|
* Create a new iterable base.
|
|
66
67
|
*
|
|
@@ -71,19 +72,19 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
71
72
|
* Time O(1), Space O(1).
|
|
72
73
|
*/
|
|
73
74
|
constructor(options) {
|
|
74
|
-
/**
|
|
75
|
-
* The converter used to transform a raw element (`R`) into a public element (`E`).
|
|
76
|
-
*
|
|
77
|
-
* @remarks
|
|
78
|
-
* Time O(1), Space O(1).
|
|
79
|
-
*/
|
|
80
|
-
__publicField(this, "_toElementFn");
|
|
81
75
|
if (options) {
|
|
82
76
|
const { toElementFn } = options;
|
|
83
77
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
84
78
|
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
85
79
|
}
|
|
86
80
|
}
|
|
81
|
+
/**
|
|
82
|
+
* The converter used to transform a raw element (`R`) into a public element (`E`).
|
|
83
|
+
*
|
|
84
|
+
* @remarks
|
|
85
|
+
* Time O(1), Space O(1).
|
|
86
|
+
*/
|
|
87
|
+
_toElementFn;
|
|
87
88
|
/**
|
|
88
89
|
* Exposes the current `toElementFn`, if configured.
|
|
89
90
|
*
|
|
@@ -278,11 +279,12 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
278
279
|
console.log(this.toVisual());
|
|
279
280
|
}
|
|
280
281
|
};
|
|
281
|
-
__name(_IterableElementBase, "IterableElementBase");
|
|
282
|
-
var IterableElementBase = _IterableElementBase;
|
|
283
282
|
|
|
284
283
|
// src/data-structures/base/linear-base.ts
|
|
285
|
-
var
|
|
284
|
+
var LinearBase = class _LinearBase extends IterableElementBase {
|
|
285
|
+
static {
|
|
286
|
+
__name(this, "LinearBase");
|
|
287
|
+
}
|
|
286
288
|
/**
|
|
287
289
|
* Construct a linear container with runtime options.
|
|
288
290
|
* @param options - `{ maxLen?, ... }` bounds/behavior options.
|
|
@@ -290,12 +292,12 @@ var _LinearBase = class _LinearBase extends IterableElementBase {
|
|
|
290
292
|
*/
|
|
291
293
|
constructor(options) {
|
|
292
294
|
super(options);
|
|
293
|
-
__publicField(this, "_maxLen", -1);
|
|
294
295
|
if (options) {
|
|
295
296
|
const { maxLen } = options;
|
|
296
297
|
if (typeof maxLen === "number" && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen;
|
|
297
298
|
}
|
|
298
299
|
}
|
|
300
|
+
_maxLen = -1;
|
|
299
301
|
/**
|
|
300
302
|
* Upper bound for length (if positive), or `-1` when unbounded.
|
|
301
303
|
* @returns Maximum allowed length.
|
|
@@ -428,7 +430,7 @@ var _LinearBase = class _LinearBase extends IterableElementBase {
|
|
|
428
430
|
return array;
|
|
429
431
|
}
|
|
430
432
|
reduceRight(callbackfn, initialValue) {
|
|
431
|
-
let accumulator = initialValue
|
|
433
|
+
let accumulator = initialValue ?? 0;
|
|
432
434
|
for (let i = this.length - 1; i >= 0; i--) {
|
|
433
435
|
accumulator = callbackfn(accumulator, this.at(i), i, this);
|
|
434
436
|
}
|
|
@@ -470,11 +472,12 @@ var _LinearBase = class _LinearBase extends IterableElementBase {
|
|
|
470
472
|
return this;
|
|
471
473
|
}
|
|
472
474
|
};
|
|
473
|
-
__name(_LinearBase, "LinearBase");
|
|
474
|
-
var LinearBase = _LinearBase;
|
|
475
475
|
|
|
476
476
|
// src/data-structures/queue/queue.ts
|
|
477
|
-
var
|
|
477
|
+
var Queue = class _Queue extends LinearBase {
|
|
478
|
+
static {
|
|
479
|
+
__name(this, "Queue");
|
|
480
|
+
}
|
|
478
481
|
/**
|
|
479
482
|
* Create a Queue and optionally bulk-insert elements.
|
|
480
483
|
* @remarks Time O(N), Space O(N)
|
|
@@ -484,15 +487,13 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
484
487
|
*/
|
|
485
488
|
constructor(elements = [], options) {
|
|
486
489
|
super(options);
|
|
487
|
-
__publicField(this, "_elements", []);
|
|
488
|
-
__publicField(this, "_offset", 0);
|
|
489
|
-
__publicField(this, "_autoCompactRatio", 0.5);
|
|
490
490
|
if (options) {
|
|
491
491
|
const { autoCompactRatio = 0.5 } = options;
|
|
492
492
|
this._autoCompactRatio = autoCompactRatio;
|
|
493
493
|
}
|
|
494
494
|
this.pushMany(elements);
|
|
495
495
|
}
|
|
496
|
+
_elements = [];
|
|
496
497
|
/**
|
|
497
498
|
* Get the underlying array buffer.
|
|
498
499
|
* @remarks Time O(1), Space O(1)
|
|
@@ -501,6 +502,7 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
501
502
|
get elements() {
|
|
502
503
|
return this._elements;
|
|
503
504
|
}
|
|
505
|
+
_offset = 0;
|
|
504
506
|
/**
|
|
505
507
|
* Get the current start offset into the array.
|
|
506
508
|
* @remarks Time O(1), Space O(1)
|
|
@@ -509,6 +511,7 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
509
511
|
get offset() {
|
|
510
512
|
return this._offset;
|
|
511
513
|
}
|
|
514
|
+
_autoCompactRatio = 0.5;
|
|
512
515
|
/**
|
|
513
516
|
* Get the compaction threshold (offset/size).
|
|
514
517
|
* @remarks Time O(1), Space O(1)
|
|
@@ -753,11 +756,10 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
753
756
|
* @returns A new Queue with mapped elements.
|
|
754
757
|
*/
|
|
755
758
|
map(callback, options, thisArg) {
|
|
756
|
-
var _a, _b;
|
|
757
759
|
const out = new this.constructor([], {
|
|
758
|
-
toElementFn: options
|
|
759
|
-
maxLen:
|
|
760
|
-
autoCompactRatio:
|
|
760
|
+
toElementFn: options?.toElementFn,
|
|
761
|
+
maxLen: options?.maxLen ?? this._maxLen,
|
|
762
|
+
autoCompactRatio: options?.autoCompactRatio ?? this._autoCompactRatio
|
|
761
763
|
});
|
|
762
764
|
let index = 0;
|
|
763
765
|
for (const v of this)
|
|
@@ -772,14 +774,13 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
772
774
|
* @returns A new queue with mapped elements (same element type).
|
|
773
775
|
*/
|
|
774
776
|
mapSame(callback, thisArg) {
|
|
775
|
-
var _a;
|
|
776
777
|
const Ctor = this.constructor;
|
|
777
778
|
const out = new Ctor([], {
|
|
778
779
|
toElementFn: this.toElementFn,
|
|
779
780
|
maxLen: this._maxLen,
|
|
780
781
|
autoCompactRatio: this._autoCompactRatio
|
|
781
782
|
});
|
|
782
|
-
|
|
783
|
+
out._setAutoCompactRatio?.(this._autoCompactRatio);
|
|
783
784
|
let index = 0;
|
|
784
785
|
for (const v of this) {
|
|
785
786
|
const mv = thisArg === void 0 ? callback(v, index++, this) : callback.call(thisArg, v, index++, this);
|
|
@@ -839,11 +840,12 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
839
840
|
return new Ctor(elements, options);
|
|
840
841
|
}
|
|
841
842
|
};
|
|
842
|
-
__name(_Queue, "Queue");
|
|
843
|
-
var Queue = _Queue;
|
|
844
843
|
|
|
845
844
|
// src/data-structures/base/iterable-entry-base.ts
|
|
846
|
-
var
|
|
845
|
+
var IterableEntryBase = class {
|
|
846
|
+
static {
|
|
847
|
+
__name(this, "IterableEntryBase");
|
|
848
|
+
}
|
|
847
849
|
/**
|
|
848
850
|
* Default iterator yielding `[key, value]` entries.
|
|
849
851
|
* @returns Iterator of `[K, V]`.
|
|
@@ -892,7 +894,7 @@ var _IterableEntryBase = class _IterableEntryBase {
|
|
|
892
894
|
every(predicate, thisArg) {
|
|
893
895
|
let index = 0;
|
|
894
896
|
for (const item of this) {
|
|
895
|
-
if (!predicate.call(thisArg, item[
|
|
897
|
+
if (!predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
896
898
|
return false;
|
|
897
899
|
}
|
|
898
900
|
}
|
|
@@ -908,7 +910,7 @@ var _IterableEntryBase = class _IterableEntryBase {
|
|
|
908
910
|
some(predicate, thisArg) {
|
|
909
911
|
let index = 0;
|
|
910
912
|
for (const item of this) {
|
|
911
|
-
if (predicate.call(thisArg, item[
|
|
913
|
+
if (predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
912
914
|
return true;
|
|
913
915
|
}
|
|
914
916
|
}
|
|
@@ -924,7 +926,7 @@ var _IterableEntryBase = class _IterableEntryBase {
|
|
|
924
926
|
let index = 0;
|
|
925
927
|
for (const item of this) {
|
|
926
928
|
const [key, value] = item;
|
|
927
|
-
callbackfn.call(thisArg,
|
|
929
|
+
callbackfn.call(thisArg, value, key, index++, this);
|
|
928
930
|
}
|
|
929
931
|
}
|
|
930
932
|
/**
|
|
@@ -938,7 +940,7 @@ var _IterableEntryBase = class _IterableEntryBase {
|
|
|
938
940
|
let index = 0;
|
|
939
941
|
for (const item of this) {
|
|
940
942
|
const [key, value] = item;
|
|
941
|
-
if (callbackfn.call(thisArg,
|
|
943
|
+
if (callbackfn.call(thisArg, value, key, index++, this)) return item;
|
|
942
944
|
}
|
|
943
945
|
return;
|
|
944
946
|
}
|
|
@@ -1012,8 +1014,6 @@ var _IterableEntryBase = class _IterableEntryBase {
|
|
|
1012
1014
|
console.log(this.toVisual());
|
|
1013
1015
|
}
|
|
1014
1016
|
};
|
|
1015
|
-
__name(_IterableEntryBase, "IterableEntryBase");
|
|
1016
|
-
var IterableEntryBase = _IterableEntryBase;
|
|
1017
1017
|
|
|
1018
1018
|
// src/common/index.ts
|
|
1019
1019
|
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
@@ -1021,7 +1021,7 @@ var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
|
1021
1021
|
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
1022
1022
|
return DFSOperation2;
|
|
1023
1023
|
})(DFSOperation || {});
|
|
1024
|
-
var
|
|
1024
|
+
var Range = class {
|
|
1025
1025
|
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
1026
1026
|
this.low = low;
|
|
1027
1027
|
this.high = high;
|
|
@@ -1030,6 +1030,9 @@ var _Range = class _Range {
|
|
|
1030
1030
|
if (!(isComparable(low) && isComparable(high))) throw new RangeError("low or high is not comparable");
|
|
1031
1031
|
if (low > high) throw new RangeError("low must be less than or equal to high");
|
|
1032
1032
|
}
|
|
1033
|
+
static {
|
|
1034
|
+
__name(this, "Range");
|
|
1035
|
+
}
|
|
1033
1036
|
// Determine whether a key is within the range
|
|
1034
1037
|
isInRange(key, comparator) {
|
|
1035
1038
|
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
@@ -1037,11 +1040,15 @@ var _Range = class _Range {
|
|
|
1037
1040
|
return lowCheck && highCheck;
|
|
1038
1041
|
}
|
|
1039
1042
|
};
|
|
1040
|
-
__name(_Range, "Range");
|
|
1041
|
-
var Range = _Range;
|
|
1042
1043
|
|
|
1043
1044
|
// src/data-structures/binary-tree/binary-tree.ts
|
|
1044
|
-
var
|
|
1045
|
+
var BinaryTreeNode = class {
|
|
1046
|
+
static {
|
|
1047
|
+
__name(this, "BinaryTreeNode");
|
|
1048
|
+
}
|
|
1049
|
+
key;
|
|
1050
|
+
value;
|
|
1051
|
+
parent = void 0;
|
|
1045
1052
|
/**
|
|
1046
1053
|
* Creates an instance of BinaryTreeNode.
|
|
1047
1054
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1050,17 +1057,10 @@ var _BinaryTreeNode = class _BinaryTreeNode {
|
|
|
1050
1057
|
* @param [value] - The value associated with the key.
|
|
1051
1058
|
*/
|
|
1052
1059
|
constructor(key, value) {
|
|
1053
|
-
__publicField(this, "key");
|
|
1054
|
-
__publicField(this, "value");
|
|
1055
|
-
__publicField(this, "parent");
|
|
1056
|
-
__publicField(this, "_left");
|
|
1057
|
-
__publicField(this, "_right");
|
|
1058
|
-
__publicField(this, "_height", 0);
|
|
1059
|
-
__publicField(this, "_color", "BLACK");
|
|
1060
|
-
__publicField(this, "_count", 1);
|
|
1061
1060
|
this.key = key;
|
|
1062
1061
|
this.value = value;
|
|
1063
1062
|
}
|
|
1063
|
+
_left = void 0;
|
|
1064
1064
|
/**
|
|
1065
1065
|
* Gets the left child of the node.
|
|
1066
1066
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1082,6 +1082,7 @@ var _BinaryTreeNode = class _BinaryTreeNode {
|
|
|
1082
1082
|
}
|
|
1083
1083
|
this._left = v;
|
|
1084
1084
|
}
|
|
1085
|
+
_right = void 0;
|
|
1085
1086
|
/**
|
|
1086
1087
|
* Gets the right child of the node.
|
|
1087
1088
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1103,6 +1104,7 @@ var _BinaryTreeNode = class _BinaryTreeNode {
|
|
|
1103
1104
|
}
|
|
1104
1105
|
this._right = v;
|
|
1105
1106
|
}
|
|
1107
|
+
_height = 0;
|
|
1106
1108
|
/**
|
|
1107
1109
|
* Gets the height of the node (used in self-balancing trees).
|
|
1108
1110
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1121,6 +1123,7 @@ var _BinaryTreeNode = class _BinaryTreeNode {
|
|
|
1121
1123
|
set height(value) {
|
|
1122
1124
|
this._height = value;
|
|
1123
1125
|
}
|
|
1126
|
+
_color = "BLACK";
|
|
1124
1127
|
/**
|
|
1125
1128
|
* Gets the color of the node (used in Red-Black trees).
|
|
1126
1129
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1139,6 +1142,7 @@ var _BinaryTreeNode = class _BinaryTreeNode {
|
|
|
1139
1142
|
set color(value) {
|
|
1140
1143
|
this._color = value;
|
|
1141
1144
|
}
|
|
1145
|
+
_count = 1;
|
|
1142
1146
|
/**
|
|
1143
1147
|
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
1144
1148
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1175,9 +1179,11 @@ var _BinaryTreeNode = class _BinaryTreeNode {
|
|
|
1175
1179
|
return "MAL_NODE";
|
|
1176
1180
|
}
|
|
1177
1181
|
};
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1182
|
+
var BinaryTree = class extends IterableEntryBase {
|
|
1183
|
+
static {
|
|
1184
|
+
__name(this, "BinaryTree");
|
|
1185
|
+
}
|
|
1186
|
+
iterationType = "ITERATIVE";
|
|
1181
1187
|
/**
|
|
1182
1188
|
* Creates an instance of BinaryTree.
|
|
1183
1189
|
* @remarks Time O(N * M), where N is the number of items in `keysNodesEntriesOrRaws` and M is the tree size at insertion time (due to O(M) `add` operation). Space O(N) for storing the nodes.
|
|
@@ -1187,22 +1193,6 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1187
1193
|
*/
|
|
1188
1194
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
1189
1195
|
super();
|
|
1190
|
-
__publicField(this, "iterationType", "ITERATIVE");
|
|
1191
|
-
__publicField(this, "_isMapMode", true);
|
|
1192
|
-
__publicField(this, "_isDuplicate", false);
|
|
1193
|
-
__publicField(this, "_store", /* @__PURE__ */ new Map());
|
|
1194
|
-
__publicField(this, "_root");
|
|
1195
|
-
__publicField(this, "_size", 0);
|
|
1196
|
-
__publicField(this, "_NIL", new BinaryTreeNode(NaN));
|
|
1197
|
-
__publicField(this, "_toEntryFn");
|
|
1198
|
-
/**
|
|
1199
|
-
* (Protected) Default callback function, returns the node's key.
|
|
1200
|
-
* @remarks Time O(1)
|
|
1201
|
-
*
|
|
1202
|
-
* @param node - The node.
|
|
1203
|
-
* @returns The node's key or undefined.
|
|
1204
|
-
*/
|
|
1205
|
-
__publicField(this, "_DEFAULT_NODE_CALLBACK", /* @__PURE__ */ __name((node) => node ? node.key : void 0, "_DEFAULT_NODE_CALLBACK"));
|
|
1206
1196
|
if (options) {
|
|
1207
1197
|
const { iterationType, toEntryFn, isMapMode, isDuplicate } = options;
|
|
1208
1198
|
if (iterationType) this.iterationType = iterationType;
|
|
@@ -1213,6 +1203,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1213
1203
|
}
|
|
1214
1204
|
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
1215
1205
|
}
|
|
1206
|
+
_isMapMode = true;
|
|
1216
1207
|
/**
|
|
1217
1208
|
* Gets whether the tree is in Map mode.
|
|
1218
1209
|
* @remarks In Map mode (default), values are stored in an external Map, and nodes only hold keys. If false, values are stored directly on the nodes. Time O(1)
|
|
@@ -1222,6 +1213,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1222
1213
|
get isMapMode() {
|
|
1223
1214
|
return this._isMapMode;
|
|
1224
1215
|
}
|
|
1216
|
+
_isDuplicate = false;
|
|
1225
1217
|
/**
|
|
1226
1218
|
* Gets whether the tree allows duplicate keys.
|
|
1227
1219
|
* @remarks Time O(1)
|
|
@@ -1231,6 +1223,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1231
1223
|
get isDuplicate() {
|
|
1232
1224
|
return this._isDuplicate;
|
|
1233
1225
|
}
|
|
1226
|
+
_store = /* @__PURE__ */ new Map();
|
|
1234
1227
|
/**
|
|
1235
1228
|
* Gets the external value store (used in Map mode).
|
|
1236
1229
|
* @remarks Time O(1)
|
|
@@ -1240,6 +1233,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1240
1233
|
get store() {
|
|
1241
1234
|
return this._store;
|
|
1242
1235
|
}
|
|
1236
|
+
_root;
|
|
1243
1237
|
/**
|
|
1244
1238
|
* Gets the root node of the tree.
|
|
1245
1239
|
* @remarks Time O(1)
|
|
@@ -1249,6 +1243,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1249
1243
|
get root() {
|
|
1250
1244
|
return this._root;
|
|
1251
1245
|
}
|
|
1246
|
+
_size = 0;
|
|
1252
1247
|
/**
|
|
1253
1248
|
* Gets the number of nodes in the tree.
|
|
1254
1249
|
* @remarks Time O(1)
|
|
@@ -1258,6 +1253,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1258
1253
|
get size() {
|
|
1259
1254
|
return this._size;
|
|
1260
1255
|
}
|
|
1256
|
+
_NIL = new BinaryTreeNode(NaN);
|
|
1261
1257
|
/**
|
|
1262
1258
|
* Gets the sentinel NIL node (used in self-balancing trees like Red-Black Tree).
|
|
1263
1259
|
* @remarks Time O(1)
|
|
@@ -1267,6 +1263,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1267
1263
|
get NIL() {
|
|
1268
1264
|
return this._NIL;
|
|
1269
1265
|
}
|
|
1266
|
+
_toEntryFn;
|
|
1270
1267
|
/**
|
|
1271
1268
|
* Gets the function used to convert raw data objects (R) into [key, value] entries.
|
|
1272
1269
|
* @remarks Time O(1)
|
|
@@ -1426,7 +1423,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1426
1423
|
if (newNode === void 0) return false;
|
|
1427
1424
|
if (!this._root) {
|
|
1428
1425
|
this._setRoot(newNode);
|
|
1429
|
-
if (this._isMapMode) this._setValue(newNode
|
|
1426
|
+
if (this._isMapMode) this._setValue(newNode?.key, newValue);
|
|
1430
1427
|
this._size = 1;
|
|
1431
1428
|
return true;
|
|
1432
1429
|
}
|
|
@@ -1458,7 +1455,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1458
1455
|
} else if (potentialParent.right === void 0) {
|
|
1459
1456
|
potentialParent.right = newNode;
|
|
1460
1457
|
}
|
|
1461
|
-
if (this._isMapMode) this._setValue(newNode
|
|
1458
|
+
if (this._isMapMode) this._setValue(newNode?.key, newValue);
|
|
1462
1459
|
this._size++;
|
|
1463
1460
|
return true;
|
|
1464
1461
|
}
|
|
@@ -1523,7 +1520,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1523
1520
|
if (!this._root) return deletedResult;
|
|
1524
1521
|
const curr = this.getNode(keyNodeOrEntry);
|
|
1525
1522
|
if (!curr) return deletedResult;
|
|
1526
|
-
const parent = curr
|
|
1523
|
+
const parent = curr?.parent;
|
|
1527
1524
|
let needBalanced;
|
|
1528
1525
|
let orgCurrent = curr;
|
|
1529
1526
|
if (!curr.left && !curr.right && !parent) {
|
|
@@ -1628,13 +1625,12 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1628
1625
|
* @returns The associated value, or undefined.
|
|
1629
1626
|
*/
|
|
1630
1627
|
get(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
1631
|
-
var _a;
|
|
1632
1628
|
if (this._isMapMode) {
|
|
1633
1629
|
const key = this._extractKey(keyNodeEntryOrPredicate);
|
|
1634
1630
|
if (key === null || key === void 0) return;
|
|
1635
1631
|
return this._store.get(key);
|
|
1636
1632
|
}
|
|
1637
|
-
return
|
|
1633
|
+
return this.getNode(keyNodeEntryOrPredicate, startNode, iterationType)?.value;
|
|
1638
1634
|
}
|
|
1639
1635
|
has(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
1640
1636
|
return this.search(keyNodeEntryOrPredicate, true, (node) => node, startNode, iterationType).length > 0;
|
|
@@ -1722,7 +1718,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1722
1718
|
let distEnsured = this.ensureNode(dist);
|
|
1723
1719
|
const beginRootEnsured = this.ensureNode(startNode);
|
|
1724
1720
|
let depth = 0;
|
|
1725
|
-
while (distEnsured
|
|
1721
|
+
while (distEnsured?.parent) {
|
|
1726
1722
|
if (distEnsured === beginRootEnsured) {
|
|
1727
1723
|
return depth;
|
|
1728
1724
|
}
|
|
@@ -2195,7 +2191,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2195
2191
|
filter(predicate, thisArg) {
|
|
2196
2192
|
const out = this._createInstance();
|
|
2197
2193
|
let i = 0;
|
|
2198
|
-
for (const [k, v] of this) if (predicate.call(thisArg,
|
|
2194
|
+
for (const [k, v] of this) if (predicate.call(thisArg, v, k, i++, this)) out.add([k, v]);
|
|
2199
2195
|
return out;
|
|
2200
2196
|
}
|
|
2201
2197
|
/**
|
|
@@ -2213,7 +2209,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2213
2209
|
map(cb, options, thisArg) {
|
|
2214
2210
|
const out = this._createLike([], options);
|
|
2215
2211
|
let i = 0;
|
|
2216
|
-
for (const [k, v] of this) out.add(cb.call(thisArg,
|
|
2212
|
+
for (const [k, v] of this) out.add(cb.call(thisArg, v, k, i++, this));
|
|
2217
2213
|
return out;
|
|
2218
2214
|
}
|
|
2219
2215
|
/**
|
|
@@ -2284,10 +2280,10 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2284
2280
|
const dfs = /* @__PURE__ */ __name((node) => {
|
|
2285
2281
|
if (!shouldVisitRoot(node)) return;
|
|
2286
2282
|
const visitLeft = /* @__PURE__ */ __name(() => {
|
|
2287
|
-
if (shouldVisitLeft(node) &&
|
|
2283
|
+
if (shouldVisitLeft(node) && node?.left !== void 0) dfs(node?.left);
|
|
2288
2284
|
}, "visitLeft");
|
|
2289
2285
|
const visitRight = /* @__PURE__ */ __name(() => {
|
|
2290
|
-
if (shouldVisitRight(node) &&
|
|
2286
|
+
if (shouldVisitRight(node) && node?.right !== void 0) dfs(node?.right);
|
|
2291
2287
|
}, "visitRight");
|
|
2292
2288
|
switch (pattern) {
|
|
2293
2289
|
case "IN":
|
|
@@ -2320,12 +2316,10 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2320
2316
|
} else {
|
|
2321
2317
|
const stack = [{ opt: 0 /* VISIT */, node: startNode }];
|
|
2322
2318
|
const pushLeft = /* @__PURE__ */ __name((cur) => {
|
|
2323
|
-
|
|
2324
|
-
if (shouldVisitLeft(cur.node)) stack.push({ opt: 0 /* VISIT */, node: (_a = cur.node) == null ? void 0 : _a.left });
|
|
2319
|
+
if (shouldVisitLeft(cur.node)) stack.push({ opt: 0 /* VISIT */, node: cur.node?.left });
|
|
2325
2320
|
}, "pushLeft");
|
|
2326
2321
|
const pushRight = /* @__PURE__ */ __name((cur) => {
|
|
2327
|
-
|
|
2328
|
-
if (shouldVisitRight(cur.node)) stack.push({ opt: 0 /* VISIT */, node: (_a = cur.node) == null ? void 0 : _a.right });
|
|
2322
|
+
if (shouldVisitRight(cur.node)) stack.push({ opt: 0 /* VISIT */, node: cur.node?.right });
|
|
2329
2323
|
}, "pushRight");
|
|
2330
2324
|
const pushRoot = /* @__PURE__ */ __name((cur) => {
|
|
2331
2325
|
if (shouldVisitRoot(cur.node)) stack.push({ opt: 1 /* PROCESS */, node: cur.node });
|
|
@@ -2397,6 +2391,14 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2397
2391
|
}
|
|
2398
2392
|
}
|
|
2399
2393
|
}
|
|
2394
|
+
/**
|
|
2395
|
+
* (Protected) Default callback function, returns the node's key.
|
|
2396
|
+
* @remarks Time O(1)
|
|
2397
|
+
*
|
|
2398
|
+
* @param node - The node.
|
|
2399
|
+
* @returns The node's key or undefined.
|
|
2400
|
+
*/
|
|
2401
|
+
_DEFAULT_NODE_CALLBACK = /* @__PURE__ */ __name((node) => node ? node.key : void 0, "_DEFAULT_NODE_CALLBACK");
|
|
2400
2402
|
/**
|
|
2401
2403
|
* (Protected) Snapshots the current tree's configuration options.
|
|
2402
2404
|
* @remarks Time O(1)
|
|
@@ -2422,7 +2424,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2422
2424
|
*/
|
|
2423
2425
|
_createInstance(options) {
|
|
2424
2426
|
const Ctor = this.constructor;
|
|
2425
|
-
return new Ctor([], { ...this._snapshotOptions(), ...options
|
|
2427
|
+
return new Ctor([], { ...this._snapshotOptions(), ...options ?? {} });
|
|
2426
2428
|
}
|
|
2427
2429
|
/**
|
|
2428
2430
|
* (Protected) Creates a new instance of the same tree constructor, potentially with different generic types.
|
|
@@ -2435,7 +2437,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2435
2437
|
*/
|
|
2436
2438
|
_createLike(iter = [], options) {
|
|
2437
2439
|
const Ctor = this.constructor;
|
|
2438
|
-
return new Ctor(iter, { ...this._snapshotOptions(), ...options
|
|
2440
|
+
return new Ctor(iter, { ...this._snapshotOptions(), ...options ?? {} });
|
|
2439
2441
|
}
|
|
2440
2442
|
/**
|
|
2441
2443
|
* (Protected) Converts a key, node, or entry into a standardized [node, value] tuple.
|
|
@@ -2453,7 +2455,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2453
2455
|
const [key, entryValue] = keyNodeOrEntry;
|
|
2454
2456
|
if (key === void 0) return [void 0, void 0];
|
|
2455
2457
|
else if (key === null) return [null, void 0];
|
|
2456
|
-
const finalValue = value
|
|
2458
|
+
const finalValue = value ?? entryValue;
|
|
2457
2459
|
return [this.createNode(key, finalValue), finalValue];
|
|
2458
2460
|
}
|
|
2459
2461
|
return [this.createNode(keyNodeOrEntry, value), value];
|
|
@@ -2660,11 +2662,15 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2660
2662
|
this._store.clear();
|
|
2661
2663
|
}
|
|
2662
2664
|
};
|
|
2663
|
-
__name(_BinaryTree, "BinaryTree");
|
|
2664
|
-
var BinaryTree = _BinaryTree;
|
|
2665
2665
|
|
|
2666
2666
|
// src/data-structures/binary-tree/bst.ts
|
|
2667
|
-
var
|
|
2667
|
+
var BSTNode = class {
|
|
2668
|
+
static {
|
|
2669
|
+
__name(this, "BSTNode");
|
|
2670
|
+
}
|
|
2671
|
+
key;
|
|
2672
|
+
value;
|
|
2673
|
+
parent = void 0;
|
|
2668
2674
|
/**
|
|
2669
2675
|
* Creates an instance of BSTNode.
|
|
2670
2676
|
* @remarks Time O(1), Space O(1)
|
|
@@ -2673,11 +2679,10 @@ var _BSTNode = class _BSTNode extends BinaryTreeNode {
|
|
|
2673
2679
|
* @param [value] - The value associated with the key.
|
|
2674
2680
|
*/
|
|
2675
2681
|
constructor(key, value) {
|
|
2676
|
-
|
|
2677
|
-
|
|
2678
|
-
__publicField(this, "_left");
|
|
2679
|
-
__publicField(this, "_right");
|
|
2682
|
+
this.key = key;
|
|
2683
|
+
this.value = value;
|
|
2680
2684
|
}
|
|
2685
|
+
_left = void 0;
|
|
2681
2686
|
/**
|
|
2682
2687
|
* Gets the left child of the node.
|
|
2683
2688
|
* @remarks Time O(1), Space O(1)
|
|
@@ -2697,6 +2702,7 @@ var _BSTNode = class _BSTNode extends BinaryTreeNode {
|
|
|
2697
2702
|
if (v) v.parent = this;
|
|
2698
2703
|
this._left = v;
|
|
2699
2704
|
}
|
|
2705
|
+
_right = void 0;
|
|
2700
2706
|
/**
|
|
2701
2707
|
* Gets the right child of the node.
|
|
2702
2708
|
* @remarks Time O(1), Space O(1)
|
|
@@ -2716,10 +2722,85 @@ var _BSTNode = class _BSTNode extends BinaryTreeNode {
|
|
|
2716
2722
|
if (v) v.parent = this;
|
|
2717
2723
|
this._right = v;
|
|
2718
2724
|
}
|
|
2725
|
+
_height = 0;
|
|
2726
|
+
/**
|
|
2727
|
+
* Gets the height of the node (used in self-balancing trees).
|
|
2728
|
+
* @remarks Time O(1), Space O(1)
|
|
2729
|
+
*
|
|
2730
|
+
* @returns The height.
|
|
2731
|
+
*/
|
|
2732
|
+
get height() {
|
|
2733
|
+
return this._height;
|
|
2734
|
+
}
|
|
2735
|
+
/**
|
|
2736
|
+
* Sets the height of the node.
|
|
2737
|
+
* @remarks Time O(1), Space O(1)
|
|
2738
|
+
*
|
|
2739
|
+
* @param value - The new height.
|
|
2740
|
+
*/
|
|
2741
|
+
set height(value) {
|
|
2742
|
+
this._height = value;
|
|
2743
|
+
}
|
|
2744
|
+
_color = "BLACK";
|
|
2745
|
+
/**
|
|
2746
|
+
* Gets the color of the node (used in Red-Black trees).
|
|
2747
|
+
* @remarks Time O(1), Space O(1)
|
|
2748
|
+
*
|
|
2749
|
+
* @returns The node's color.
|
|
2750
|
+
*/
|
|
2751
|
+
get color() {
|
|
2752
|
+
return this._color;
|
|
2753
|
+
}
|
|
2754
|
+
/**
|
|
2755
|
+
* Sets the color of the node.
|
|
2756
|
+
* @remarks Time O(1), Space O(1)
|
|
2757
|
+
*
|
|
2758
|
+
* @param value - The new color.
|
|
2759
|
+
*/
|
|
2760
|
+
set color(value) {
|
|
2761
|
+
this._color = value;
|
|
2762
|
+
}
|
|
2763
|
+
_count = 1;
|
|
2764
|
+
/**
|
|
2765
|
+
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
2766
|
+
* @remarks Time O(1), Space O(1)
|
|
2767
|
+
*
|
|
2768
|
+
* @returns The subtree node count.
|
|
2769
|
+
*/
|
|
2770
|
+
get count() {
|
|
2771
|
+
return this._count;
|
|
2772
|
+
}
|
|
2773
|
+
/**
|
|
2774
|
+
* Sets the count of nodes in the subtree.
|
|
2775
|
+
* @remarks Time O(1), Space O(1)
|
|
2776
|
+
*
|
|
2777
|
+
* @param value - The new count.
|
|
2778
|
+
*/
|
|
2779
|
+
set count(value) {
|
|
2780
|
+
this._count = value;
|
|
2781
|
+
}
|
|
2782
|
+
/**
|
|
2783
|
+
* Gets the position of the node relative to its parent.
|
|
2784
|
+
* @remarks Time O(1), Space O(1)
|
|
2785
|
+
*
|
|
2786
|
+
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
2787
|
+
*/
|
|
2788
|
+
get familyPosition() {
|
|
2789
|
+
if (!this.parent) {
|
|
2790
|
+
return this.left || this.right ? "ROOT" : "ISOLATED";
|
|
2791
|
+
}
|
|
2792
|
+
if (this.parent.left === this) {
|
|
2793
|
+
return this.left || this.right ? "ROOT_LEFT" : "LEFT";
|
|
2794
|
+
} else if (this.parent.right === this) {
|
|
2795
|
+
return this.left || this.right ? "ROOT_RIGHT" : "RIGHT";
|
|
2796
|
+
}
|
|
2797
|
+
return "MAL_NODE";
|
|
2798
|
+
}
|
|
2719
2799
|
};
|
|
2720
|
-
|
|
2721
|
-
|
|
2722
|
-
|
|
2800
|
+
var BST = class extends BinaryTree {
|
|
2801
|
+
static {
|
|
2802
|
+
__name(this, "BST");
|
|
2803
|
+
}
|
|
2723
2804
|
/**
|
|
2724
2805
|
* Creates an instance of BST.
|
|
2725
2806
|
* @remarks Time O(N log N) or O(N^2) depending on `isBalanceAdd` in `addMany` and input order. Space O(N).
|
|
@@ -2729,33 +2810,6 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2729
2810
|
*/
|
|
2730
2811
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
2731
2812
|
super([], options);
|
|
2732
|
-
__publicField(this, "_root");
|
|
2733
|
-
__publicField(this, "_isReverse", false);
|
|
2734
|
-
/**
|
|
2735
|
-
* The default comparator function.
|
|
2736
|
-
* @remarks Time O(1) (or O(C) if `specifyComparable` is used, C is complexity of that function).
|
|
2737
|
-
*/
|
|
2738
|
-
__publicField(this, "_comparator", /* @__PURE__ */ __name((a, b) => {
|
|
2739
|
-
if (isComparable(a) && isComparable(b)) {
|
|
2740
|
-
if (a > b) return 1;
|
|
2741
|
-
if (a < b) return -1;
|
|
2742
|
-
return 0;
|
|
2743
|
-
}
|
|
2744
|
-
if (this._specifyComparable) {
|
|
2745
|
-
const va = this._specifyComparable(a);
|
|
2746
|
-
const vb = this._specifyComparable(b);
|
|
2747
|
-
if (va > vb) return 1;
|
|
2748
|
-
if (va < vb) return -1;
|
|
2749
|
-
return 0;
|
|
2750
|
-
}
|
|
2751
|
-
if (typeof a === "object" || typeof b === "object") {
|
|
2752
|
-
throw TypeError(
|
|
2753
|
-
`When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
|
|
2754
|
-
);
|
|
2755
|
-
}
|
|
2756
|
-
return 0;
|
|
2757
|
-
}, "_comparator"));
|
|
2758
|
-
__publicField(this, "_specifyComparable");
|
|
2759
2813
|
if (options) {
|
|
2760
2814
|
const { specifyComparable, isReverse } = options;
|
|
2761
2815
|
if (typeof specifyComparable === "function") this._specifyComparable = specifyComparable;
|
|
@@ -2763,6 +2817,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2763
2817
|
}
|
|
2764
2818
|
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
2765
2819
|
}
|
|
2820
|
+
_root = void 0;
|
|
2766
2821
|
/**
|
|
2767
2822
|
* Gets the root node of the tree.
|
|
2768
2823
|
* @remarks Time O(1)
|
|
@@ -2772,6 +2827,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2772
2827
|
get root() {
|
|
2773
2828
|
return this._root;
|
|
2774
2829
|
}
|
|
2830
|
+
_isReverse = false;
|
|
2775
2831
|
/**
|
|
2776
2832
|
* Gets whether the tree's comparison logic is reversed.
|
|
2777
2833
|
* @remarks Time O(1)
|
|
@@ -2781,6 +2837,30 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2781
2837
|
get isReverse() {
|
|
2782
2838
|
return this._isReverse;
|
|
2783
2839
|
}
|
|
2840
|
+
/**
|
|
2841
|
+
* The default comparator function.
|
|
2842
|
+
* @remarks Time O(1) (or O(C) if `specifyComparable` is used, C is complexity of that function).
|
|
2843
|
+
*/
|
|
2844
|
+
_comparator = /* @__PURE__ */ __name((a, b) => {
|
|
2845
|
+
if (isComparable(a) && isComparable(b)) {
|
|
2846
|
+
if (a > b) return 1;
|
|
2847
|
+
if (a < b) return -1;
|
|
2848
|
+
return 0;
|
|
2849
|
+
}
|
|
2850
|
+
if (this._specifyComparable) {
|
|
2851
|
+
const va = this._specifyComparable(a);
|
|
2852
|
+
const vb = this._specifyComparable(b);
|
|
2853
|
+
if (va > vb) return 1;
|
|
2854
|
+
if (va < vb) return -1;
|
|
2855
|
+
return 0;
|
|
2856
|
+
}
|
|
2857
|
+
if (typeof a === "object" || typeof b === "object") {
|
|
2858
|
+
throw TypeError(
|
|
2859
|
+
`When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
|
|
2860
|
+
);
|
|
2861
|
+
}
|
|
2862
|
+
return 0;
|
|
2863
|
+
}, "_comparator");
|
|
2784
2864
|
/**
|
|
2785
2865
|
* Gets the comparator function used by the tree.
|
|
2786
2866
|
* @remarks Time O(1)
|
|
@@ -2790,6 +2870,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2790
2870
|
get comparator() {
|
|
2791
2871
|
return this._comparator;
|
|
2792
2872
|
}
|
|
2873
|
+
_specifyComparable;
|
|
2793
2874
|
/**
|
|
2794
2875
|
* Gets the function used to extract a comparable value from a complex key.
|
|
2795
2876
|
* @remarks Time O(1)
|
|
@@ -2819,8 +2900,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2819
2900
|
* @returns The resolved node, or undefined if not found.
|
|
2820
2901
|
*/
|
|
2821
2902
|
ensureNode(keyNodeOrEntry, iterationType = this.iterationType) {
|
|
2822
|
-
|
|
2823
|
-
return (_a = super.ensureNode(keyNodeOrEntry, iterationType)) != null ? _a : void 0;
|
|
2903
|
+
return super.ensureNode(keyNodeOrEntry, iterationType) ?? void 0;
|
|
2824
2904
|
}
|
|
2825
2905
|
/**
|
|
2826
2906
|
* Checks if the given item is a `BSTNode` instance.
|
|
@@ -2893,8 +2973,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2893
2973
|
* @returns The first matching node, or undefined if not found.
|
|
2894
2974
|
*/
|
|
2895
2975
|
getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
2896
|
-
|
|
2897
|
-
return (_a = this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0]) != null ? _a : void 0;
|
|
2976
|
+
return this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0] ?? void 0;
|
|
2898
2977
|
}
|
|
2899
2978
|
/**
|
|
2900
2979
|
* Searches the tree for nodes matching a predicate, key, or range.
|
|
@@ -2999,7 +3078,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2999
3078
|
if (newNode === void 0) return false;
|
|
3000
3079
|
if (this._root === void 0) {
|
|
3001
3080
|
this._setRoot(newNode);
|
|
3002
|
-
if (this._isMapMode) this._setValue(newNode
|
|
3081
|
+
if (this._isMapMode) this._setValue(newNode?.key, newValue);
|
|
3003
3082
|
this._size++;
|
|
3004
3083
|
return true;
|
|
3005
3084
|
}
|
|
@@ -3012,7 +3091,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3012
3091
|
} else if (this._compare(current.key, newNode.key) > 0) {
|
|
3013
3092
|
if (current.left === void 0) {
|
|
3014
3093
|
current.left = newNode;
|
|
3015
|
-
if (this._isMapMode) this._setValue(newNode
|
|
3094
|
+
if (this._isMapMode) this._setValue(newNode?.key, newValue);
|
|
3016
3095
|
this._size++;
|
|
3017
3096
|
return true;
|
|
3018
3097
|
}
|
|
@@ -3020,7 +3099,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3020
3099
|
} else {
|
|
3021
3100
|
if (current.right === void 0) {
|
|
3022
3101
|
current.right = newNode;
|
|
3023
|
-
if (this._isMapMode) this._setValue(newNode
|
|
3102
|
+
if (this._isMapMode) this._setValue(newNode?.key, newValue);
|
|
3024
3103
|
this._size++;
|
|
3025
3104
|
return true;
|
|
3026
3105
|
}
|
|
@@ -3043,10 +3122,10 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3043
3122
|
*/
|
|
3044
3123
|
addMany(keysNodesEntriesOrRaws, values, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
3045
3124
|
const inserted = [];
|
|
3046
|
-
const valuesIterator = values
|
|
3125
|
+
const valuesIterator = values?.[Symbol.iterator]();
|
|
3047
3126
|
if (!isBalanceAdd) {
|
|
3048
3127
|
for (let kve of keysNodesEntriesOrRaws) {
|
|
3049
|
-
const val = valuesIterator
|
|
3128
|
+
const val = valuesIterator?.next().value;
|
|
3050
3129
|
if (this.isRaw(kve)) kve = this._toEntryFn(kve);
|
|
3051
3130
|
inserted.push(this.add(kve, val));
|
|
3052
3131
|
}
|
|
@@ -3055,7 +3134,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3055
3134
|
const realBTNExemplars = [];
|
|
3056
3135
|
let i = 0;
|
|
3057
3136
|
for (const kve of keysNodesEntriesOrRaws) {
|
|
3058
|
-
realBTNExemplars.push({ key: kve, value: valuesIterator
|
|
3137
|
+
realBTNExemplars.push({ key: kve, value: valuesIterator?.next().value, orgIndex: i++ });
|
|
3059
3138
|
}
|
|
3060
3139
|
const sorted = realBTNExemplars.sort(({ key: a }, { key: b }) => {
|
|
3061
3140
|
let keyA, keyB;
|
|
@@ -3236,7 +3315,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3236
3315
|
const out = this._createLike([], options);
|
|
3237
3316
|
let index = 0;
|
|
3238
3317
|
for (const [key, value] of this) {
|
|
3239
|
-
out.add(callback.call(thisArg,
|
|
3318
|
+
out.add(callback.call(thisArg, value, key, index++, this));
|
|
3240
3319
|
}
|
|
3241
3320
|
return out;
|
|
3242
3321
|
}
|
|
@@ -3277,7 +3356,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3277
3356
|
*/
|
|
3278
3357
|
_createInstance(options) {
|
|
3279
3358
|
const Ctor = this.constructor;
|
|
3280
|
-
return new Ctor([], { ...this._snapshotOptions(), ...options
|
|
3359
|
+
return new Ctor([], { ...this._snapshotOptions(), ...options ?? {} });
|
|
3281
3360
|
}
|
|
3282
3361
|
/**
|
|
3283
3362
|
* (Protected) Creates a new instance of the same BST constructor, potentially with different generic types.
|
|
@@ -3290,7 +3369,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3290
3369
|
*/
|
|
3291
3370
|
_createLike(iter = [], options) {
|
|
3292
3371
|
const Ctor = this.constructor;
|
|
3293
|
-
return new Ctor(iter, { ...this._snapshotOptions(), ...options
|
|
3372
|
+
return new Ctor(iter, { ...this._snapshotOptions(), ...options ?? {} });
|
|
3294
3373
|
}
|
|
3295
3374
|
/**
|
|
3296
3375
|
* (Protected) Snapshots the current BST's configuration options.
|
|
@@ -3317,7 +3396,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3317
3396
|
_keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value) {
|
|
3318
3397
|
const [node, entryValue] = super._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
3319
3398
|
if (node === null) return [void 0, void 0];
|
|
3320
|
-
return [node, value
|
|
3399
|
+
return [node, value ?? entryValue];
|
|
3321
3400
|
}
|
|
3322
3401
|
/**
|
|
3323
3402
|
* (Protected) Sets the root node and clears its parent reference.
|
|
@@ -3348,7 +3427,6 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3348
3427
|
* @returns True if the node was found and deleted, false otherwise.
|
|
3349
3428
|
*/
|
|
3350
3429
|
_deleteByKey(key) {
|
|
3351
|
-
var _a;
|
|
3352
3430
|
let node = this._root;
|
|
3353
3431
|
while (node) {
|
|
3354
3432
|
const cmp = this._compare(node.key, key);
|
|
@@ -3357,7 +3435,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3357
3435
|
}
|
|
3358
3436
|
if (!node) return false;
|
|
3359
3437
|
const transplant = /* @__PURE__ */ __name((u, v) => {
|
|
3360
|
-
const p = u
|
|
3438
|
+
const p = u?.parent;
|
|
3361
3439
|
if (!p) {
|
|
3362
3440
|
this._setRoot(v);
|
|
3363
3441
|
} else if (p.left === u) {
|
|
@@ -3387,15 +3465,19 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3387
3465
|
succ.left = node.left;
|
|
3388
3466
|
if (succ.left) succ.left.parent = succ;
|
|
3389
3467
|
}
|
|
3390
|
-
this._size = Math.max(0, (
|
|
3468
|
+
this._size = Math.max(0, (this._size ?? 0) - 1);
|
|
3391
3469
|
return true;
|
|
3392
3470
|
}
|
|
3393
3471
|
};
|
|
3394
|
-
__name(_BST, "BST");
|
|
3395
|
-
var BST = _BST;
|
|
3396
3472
|
|
|
3397
3473
|
// src/data-structures/binary-tree/red-black-tree.ts
|
|
3398
|
-
var
|
|
3474
|
+
var RedBlackTreeNode = class {
|
|
3475
|
+
static {
|
|
3476
|
+
__name(this, "RedBlackTreeNode");
|
|
3477
|
+
}
|
|
3478
|
+
key;
|
|
3479
|
+
value;
|
|
3480
|
+
parent = void 0;
|
|
3399
3481
|
/**
|
|
3400
3482
|
* Create a Red-Black Tree and optionally bulk-insert items.
|
|
3401
3483
|
* @remarks Time O(n log n), Space O(n)
|
|
@@ -3405,12 +3487,11 @@ var _RedBlackTreeNode = class _RedBlackTreeNode extends BSTNode {
|
|
|
3405
3487
|
* @returns New RedBlackTree instance.
|
|
3406
3488
|
*/
|
|
3407
3489
|
constructor(key, value, color = "BLACK") {
|
|
3408
|
-
|
|
3409
|
-
|
|
3410
|
-
|
|
3411
|
-
__publicField(this, "_right");
|
|
3412
|
-
this._color = color;
|
|
3490
|
+
this.key = key;
|
|
3491
|
+
this.value = value;
|
|
3492
|
+
this.color = color;
|
|
3413
3493
|
}
|
|
3494
|
+
_left = void 0;
|
|
3414
3495
|
/**
|
|
3415
3496
|
* Get the left child pointer.
|
|
3416
3497
|
* @remarks Time O(1), Space O(1)
|
|
@@ -3431,6 +3512,7 @@ var _RedBlackTreeNode = class _RedBlackTreeNode extends BSTNode {
|
|
|
3431
3512
|
}
|
|
3432
3513
|
this._left = v;
|
|
3433
3514
|
}
|
|
3515
|
+
_right = void 0;
|
|
3434
3516
|
/**
|
|
3435
3517
|
* Get the right child pointer.
|
|
3436
3518
|
* @remarks Time O(1), Space O(1)
|
|
@@ -3451,18 +3533,93 @@ var _RedBlackTreeNode = class _RedBlackTreeNode extends BSTNode {
|
|
|
3451
3533
|
}
|
|
3452
3534
|
this._right = v;
|
|
3453
3535
|
}
|
|
3536
|
+
_height = 0;
|
|
3537
|
+
/**
|
|
3538
|
+
* Gets the height of the node (used in self-balancing trees).
|
|
3539
|
+
* @remarks Time O(1), Space O(1)
|
|
3540
|
+
*
|
|
3541
|
+
* @returns The height.
|
|
3542
|
+
*/
|
|
3543
|
+
get height() {
|
|
3544
|
+
return this._height;
|
|
3545
|
+
}
|
|
3546
|
+
/**
|
|
3547
|
+
* Sets the height of the node.
|
|
3548
|
+
* @remarks Time O(1), Space O(1)
|
|
3549
|
+
*
|
|
3550
|
+
* @param value - The new height.
|
|
3551
|
+
*/
|
|
3552
|
+
set height(value) {
|
|
3553
|
+
this._height = value;
|
|
3554
|
+
}
|
|
3555
|
+
_color = "BLACK";
|
|
3556
|
+
/**
|
|
3557
|
+
* Gets the color of the node (used in Red-Black trees).
|
|
3558
|
+
* @remarks Time O(1), Space O(1)
|
|
3559
|
+
*
|
|
3560
|
+
* @returns The node's color.
|
|
3561
|
+
*/
|
|
3562
|
+
get color() {
|
|
3563
|
+
return this._color;
|
|
3564
|
+
}
|
|
3565
|
+
/**
|
|
3566
|
+
* Sets the color of the node.
|
|
3567
|
+
* @remarks Time O(1), Space O(1)
|
|
3568
|
+
*
|
|
3569
|
+
* @param value - The new color.
|
|
3570
|
+
*/
|
|
3571
|
+
set color(value) {
|
|
3572
|
+
this._color = value;
|
|
3573
|
+
}
|
|
3574
|
+
_count = 1;
|
|
3575
|
+
/**
|
|
3576
|
+
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
3577
|
+
* @remarks Time O(1), Space O(1)
|
|
3578
|
+
*
|
|
3579
|
+
* @returns The subtree node count.
|
|
3580
|
+
*/
|
|
3581
|
+
get count() {
|
|
3582
|
+
return this._count;
|
|
3583
|
+
}
|
|
3584
|
+
/**
|
|
3585
|
+
* Sets the count of nodes in the subtree.
|
|
3586
|
+
* @remarks Time O(1), Space O(1)
|
|
3587
|
+
*
|
|
3588
|
+
* @param value - The new count.
|
|
3589
|
+
*/
|
|
3590
|
+
set count(value) {
|
|
3591
|
+
this._count = value;
|
|
3592
|
+
}
|
|
3593
|
+
/**
|
|
3594
|
+
* Gets the position of the node relative to its parent.
|
|
3595
|
+
* @remarks Time O(1), Space O(1)
|
|
3596
|
+
*
|
|
3597
|
+
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
3598
|
+
*/
|
|
3599
|
+
get familyPosition() {
|
|
3600
|
+
if (!this.parent) {
|
|
3601
|
+
return this.left || this.right ? "ROOT" : "ISOLATED";
|
|
3602
|
+
}
|
|
3603
|
+
if (this.parent.left === this) {
|
|
3604
|
+
return this.left || this.right ? "ROOT_LEFT" : "LEFT";
|
|
3605
|
+
} else if (this.parent.right === this) {
|
|
3606
|
+
return this.left || this.right ? "ROOT_RIGHT" : "RIGHT";
|
|
3607
|
+
}
|
|
3608
|
+
return "MAL_NODE";
|
|
3609
|
+
}
|
|
3454
3610
|
};
|
|
3455
|
-
|
|
3456
|
-
|
|
3457
|
-
|
|
3611
|
+
var RedBlackTree = class extends BST {
|
|
3612
|
+
static {
|
|
3613
|
+
__name(this, "RedBlackTree");
|
|
3614
|
+
}
|
|
3458
3615
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
3459
3616
|
super([], options);
|
|
3460
|
-
__publicField(this, "_root");
|
|
3461
3617
|
this._root = this.NIL;
|
|
3462
3618
|
if (keysNodesEntriesOrRaws) {
|
|
3463
3619
|
this.addMany(keysNodesEntriesOrRaws);
|
|
3464
3620
|
}
|
|
3465
3621
|
}
|
|
3622
|
+
_root;
|
|
3466
3623
|
/**
|
|
3467
3624
|
* Get the current root node.
|
|
3468
3625
|
* @remarks Time O(1), Space O(1)
|
|
@@ -3601,17 +3758,17 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
3601
3758
|
const out = this._createLike([], options);
|
|
3602
3759
|
let index = 0;
|
|
3603
3760
|
for (const [key, value] of this) {
|
|
3604
|
-
out.add(callback.call(thisArg,
|
|
3761
|
+
out.add(callback.call(thisArg, value, key, index++, this));
|
|
3605
3762
|
}
|
|
3606
3763
|
return out;
|
|
3607
3764
|
}
|
|
3608
3765
|
_createInstance(options) {
|
|
3609
3766
|
const Ctor = this.constructor;
|
|
3610
|
-
return new Ctor([], { ...this._snapshotOptions(), ...options
|
|
3767
|
+
return new Ctor([], { ...this._snapshotOptions(), ...options ?? {} });
|
|
3611
3768
|
}
|
|
3612
3769
|
_createLike(iter = [], options) {
|
|
3613
3770
|
const Ctor = this.constructor;
|
|
3614
|
-
return new Ctor(iter, { ...this._snapshotOptions(), ...options
|
|
3771
|
+
return new Ctor(iter, { ...this._snapshotOptions(), ...options ?? {} });
|
|
3615
3772
|
}
|
|
3616
3773
|
_setRoot(v) {
|
|
3617
3774
|
if (v) {
|
|
@@ -3630,16 +3787,15 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
3630
3787
|
* @returns Status string: 'CREATED' or 'UPDATED'.
|
|
3631
3788
|
*/
|
|
3632
3789
|
_insert(node) {
|
|
3633
|
-
|
|
3634
|
-
let current = this.root;
|
|
3790
|
+
let current = this.root ?? this.NIL;
|
|
3635
3791
|
let parent = void 0;
|
|
3636
|
-
while (this.
|
|
3792
|
+
while (current !== this.NIL) {
|
|
3637
3793
|
parent = current;
|
|
3638
3794
|
const compared = this._compare(node.key, current.key);
|
|
3639
3795
|
if (compared < 0) {
|
|
3640
|
-
current =
|
|
3796
|
+
current = current.left ?? this.NIL;
|
|
3641
3797
|
} else if (compared > 0) {
|
|
3642
|
-
current =
|
|
3798
|
+
current = current.right ?? this.NIL;
|
|
3643
3799
|
} else {
|
|
3644
3800
|
this._replaceNode(current, node);
|
|
3645
3801
|
return "UPDATED";
|
|
@@ -3685,11 +3841,10 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
3685
3841
|
* @returns void
|
|
3686
3842
|
*/
|
|
3687
3843
|
_insertFixup(z) {
|
|
3688
|
-
|
|
3689
|
-
|
|
3690
|
-
if (z.parent === ((_b = z.parent.parent) == null ? void 0 : _b.left)) {
|
|
3844
|
+
while (z?.parent?.color === "RED") {
|
|
3845
|
+
if (z.parent === z.parent.parent?.left) {
|
|
3691
3846
|
const y = z.parent.parent.right;
|
|
3692
|
-
if (
|
|
3847
|
+
if (y?.color === "RED") {
|
|
3693
3848
|
z.parent.color = "BLACK";
|
|
3694
3849
|
y.color = "BLACK";
|
|
3695
3850
|
z.parent.parent.color = "RED";
|
|
@@ -3699,15 +3854,15 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
3699
3854
|
z = z.parent;
|
|
3700
3855
|
this._leftRotate(z);
|
|
3701
3856
|
}
|
|
3702
|
-
if (z &&
|
|
3857
|
+
if (z && z.parent && z.parent.parent) {
|
|
3703
3858
|
z.parent.color = "BLACK";
|
|
3704
3859
|
z.parent.parent.color = "RED";
|
|
3705
3860
|
this._rightRotate(z.parent.parent);
|
|
3706
3861
|
}
|
|
3707
3862
|
}
|
|
3708
3863
|
} else {
|
|
3709
|
-
const y =
|
|
3710
|
-
if (
|
|
3864
|
+
const y = z?.parent?.parent?.left ?? void 0;
|
|
3865
|
+
if (y?.color === "RED") {
|
|
3711
3866
|
z.parent.color = "BLACK";
|
|
3712
3867
|
y.color = "BLACK";
|
|
3713
3868
|
z.parent.parent.color = "RED";
|
|
@@ -3717,7 +3872,7 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
3717
3872
|
z = z.parent;
|
|
3718
3873
|
this._rightRotate(z);
|
|
3719
3874
|
}
|
|
3720
|
-
if (z &&
|
|
3875
|
+
if (z && z.parent && z.parent.parent) {
|
|
3721
3876
|
z.parent.color = "BLACK";
|
|
3722
3877
|
z.parent.parent.color = "RED";
|
|
3723
3878
|
this._leftRotate(z.parent.parent);
|
|
@@ -3734,7 +3889,6 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
3734
3889
|
* @returns void
|
|
3735
3890
|
*/
|
|
3736
3891
|
_deleteFixup(node) {
|
|
3737
|
-
var _a, _b, _c, _d;
|
|
3738
3892
|
if (!node || node === this.root || node.color === "BLACK") {
|
|
3739
3893
|
if (node) {
|
|
3740
3894
|
node.color = "BLACK";
|
|
@@ -3748,17 +3902,17 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
3748
3902
|
}
|
|
3749
3903
|
if (node === parent.left) {
|
|
3750
3904
|
let sibling = parent.right;
|
|
3751
|
-
if (
|
|
3905
|
+
if (sibling?.color === "RED") {
|
|
3752
3906
|
sibling.color = "BLACK";
|
|
3753
3907
|
parent.color = "RED";
|
|
3754
3908
|
this._leftRotate(parent);
|
|
3755
3909
|
sibling = parent.right;
|
|
3756
3910
|
}
|
|
3757
|
-
if ((
|
|
3911
|
+
if ((sibling?.left?.color ?? "BLACK") === "BLACK") {
|
|
3758
3912
|
if (sibling) sibling.color = "RED";
|
|
3759
3913
|
node = parent;
|
|
3760
3914
|
} else {
|
|
3761
|
-
if (sibling
|
|
3915
|
+
if (sibling?.left) sibling.left.color = "BLACK";
|
|
3762
3916
|
if (sibling) sibling.color = parent.color;
|
|
3763
3917
|
parent.color = "BLACK";
|
|
3764
3918
|
this._rightRotate(parent);
|
|
@@ -3766,17 +3920,17 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
3766
3920
|
}
|
|
3767
3921
|
} else {
|
|
3768
3922
|
let sibling = parent.left;
|
|
3769
|
-
if (
|
|
3923
|
+
if (sibling?.color === "RED") {
|
|
3770
3924
|
sibling.color = "BLACK";
|
|
3771
3925
|
if (parent) parent.color = "RED";
|
|
3772
3926
|
this._rightRotate(parent);
|
|
3773
3927
|
if (parent) sibling = parent.left;
|
|
3774
3928
|
}
|
|
3775
|
-
if ((
|
|
3929
|
+
if ((sibling?.right?.color ?? "BLACK") === "BLACK") {
|
|
3776
3930
|
if (sibling) sibling.color = "RED";
|
|
3777
3931
|
node = parent;
|
|
3778
3932
|
} else {
|
|
3779
|
-
if (sibling
|
|
3933
|
+
if (sibling?.right) sibling.right.color = "BLACK";
|
|
3780
3934
|
if (sibling) sibling.color = parent.color;
|
|
3781
3935
|
if (parent) parent.color = "BLACK";
|
|
3782
3936
|
this._leftRotate(parent);
|
|
@@ -3800,7 +3954,7 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
3800
3954
|
}
|
|
3801
3955
|
const y = x.right;
|
|
3802
3956
|
x.right = y.left;
|
|
3803
|
-
if (
|
|
3957
|
+
if (y.left && y.left !== this.NIL) {
|
|
3804
3958
|
y.left.parent = x;
|
|
3805
3959
|
}
|
|
3806
3960
|
y.parent = x.parent;
|
|
@@ -3826,7 +3980,7 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
3826
3980
|
}
|
|
3827
3981
|
const x = y.left;
|
|
3828
3982
|
y.left = x.right;
|
|
3829
|
-
if (
|
|
3983
|
+
if (x.right && x.right !== this.NIL) {
|
|
3830
3984
|
x.right.parent = y;
|
|
3831
3985
|
}
|
|
3832
3986
|
x.parent = y.parent;
|
|
@@ -3841,11 +3995,15 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
3841
3995
|
y.parent = x;
|
|
3842
3996
|
}
|
|
3843
3997
|
};
|
|
3844
|
-
__name(_RedBlackTree, "RedBlackTree");
|
|
3845
|
-
var RedBlackTree = _RedBlackTree;
|
|
3846
3998
|
|
|
3847
3999
|
// src/data-structures/binary-tree/tree-multi-map.ts
|
|
3848
|
-
var
|
|
4000
|
+
var TreeMultiMapNode = class {
|
|
4001
|
+
static {
|
|
4002
|
+
__name(this, "TreeMultiMapNode");
|
|
4003
|
+
}
|
|
4004
|
+
key;
|
|
4005
|
+
value;
|
|
4006
|
+
parent = void 0;
|
|
3849
4007
|
/**
|
|
3850
4008
|
* Create a TreeMultiMap node with an optional value bucket.
|
|
3851
4009
|
* @remarks Time O(1), Space O(1)
|
|
@@ -3853,12 +4011,12 @@ var _TreeMultiMapNode = class _TreeMultiMapNode extends RedBlackTreeNode {
|
|
|
3853
4011
|
* @param [value] - Initial array of values.
|
|
3854
4012
|
* @returns New TreeMultiMapNode instance.
|
|
3855
4013
|
*/
|
|
3856
|
-
constructor(key, value) {
|
|
3857
|
-
|
|
3858
|
-
|
|
3859
|
-
|
|
3860
|
-
__publicField(this, "_right");
|
|
4014
|
+
constructor(key, value = [], color = "BLACK") {
|
|
4015
|
+
this.key = key;
|
|
4016
|
+
this.value = value;
|
|
4017
|
+
this.color = color;
|
|
3861
4018
|
}
|
|
4019
|
+
_left = void 0;
|
|
3862
4020
|
/**
|
|
3863
4021
|
* Get the left child pointer.
|
|
3864
4022
|
* @remarks Time O(1), Space O(1)
|
|
@@ -3879,6 +4037,7 @@ var _TreeMultiMapNode = class _TreeMultiMapNode extends RedBlackTreeNode {
|
|
|
3879
4037
|
}
|
|
3880
4038
|
this._left = v;
|
|
3881
4039
|
}
|
|
4040
|
+
_right = void 0;
|
|
3882
4041
|
/**
|
|
3883
4042
|
* Get the right child pointer.
|
|
3884
4043
|
* @remarks Time O(1), Space O(1)
|
|
@@ -3899,10 +4058,85 @@ var _TreeMultiMapNode = class _TreeMultiMapNode extends RedBlackTreeNode {
|
|
|
3899
4058
|
}
|
|
3900
4059
|
this._right = v;
|
|
3901
4060
|
}
|
|
4061
|
+
_height = 0;
|
|
4062
|
+
/**
|
|
4063
|
+
* Gets the height of the node (used in self-balancing trees).
|
|
4064
|
+
* @remarks Time O(1), Space O(1)
|
|
4065
|
+
*
|
|
4066
|
+
* @returns The height.
|
|
4067
|
+
*/
|
|
4068
|
+
get height() {
|
|
4069
|
+
return this._height;
|
|
4070
|
+
}
|
|
4071
|
+
/**
|
|
4072
|
+
* Sets the height of the node.
|
|
4073
|
+
* @remarks Time O(1), Space O(1)
|
|
4074
|
+
*
|
|
4075
|
+
* @param value - The new height.
|
|
4076
|
+
*/
|
|
4077
|
+
set height(value) {
|
|
4078
|
+
this._height = value;
|
|
4079
|
+
}
|
|
4080
|
+
_color = "BLACK";
|
|
4081
|
+
/**
|
|
4082
|
+
* Gets the color of the node (used in Red-Black trees).
|
|
4083
|
+
* @remarks Time O(1), Space O(1)
|
|
4084
|
+
*
|
|
4085
|
+
* @returns The node's color.
|
|
4086
|
+
*/
|
|
4087
|
+
get color() {
|
|
4088
|
+
return this._color;
|
|
4089
|
+
}
|
|
4090
|
+
/**
|
|
4091
|
+
* Sets the color of the node.
|
|
4092
|
+
* @remarks Time O(1), Space O(1)
|
|
4093
|
+
*
|
|
4094
|
+
* @param value - The new color.
|
|
4095
|
+
*/
|
|
4096
|
+
set color(value) {
|
|
4097
|
+
this._color = value;
|
|
4098
|
+
}
|
|
4099
|
+
_count = 1;
|
|
4100
|
+
/**
|
|
4101
|
+
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
4102
|
+
* @remarks Time O(1), Space O(1)
|
|
4103
|
+
*
|
|
4104
|
+
* @returns The subtree node count.
|
|
4105
|
+
*/
|
|
4106
|
+
get count() {
|
|
4107
|
+
return this._count;
|
|
4108
|
+
}
|
|
4109
|
+
/**
|
|
4110
|
+
* Sets the count of nodes in the subtree.
|
|
4111
|
+
* @remarks Time O(1), Space O(1)
|
|
4112
|
+
*
|
|
4113
|
+
* @param value - The new count.
|
|
4114
|
+
*/
|
|
4115
|
+
set count(value) {
|
|
4116
|
+
this._count = value;
|
|
4117
|
+
}
|
|
4118
|
+
/**
|
|
4119
|
+
* Gets the position of the node relative to its parent.
|
|
4120
|
+
* @remarks Time O(1), Space O(1)
|
|
4121
|
+
*
|
|
4122
|
+
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
4123
|
+
*/
|
|
4124
|
+
get familyPosition() {
|
|
4125
|
+
if (!this.parent) {
|
|
4126
|
+
return this.left || this.right ? "ROOT" : "ISOLATED";
|
|
4127
|
+
}
|
|
4128
|
+
if (this.parent.left === this) {
|
|
4129
|
+
return this.left || this.right ? "ROOT_LEFT" : "LEFT";
|
|
4130
|
+
} else if (this.parent.right === this) {
|
|
4131
|
+
return this.left || this.right ? "ROOT_RIGHT" : "RIGHT";
|
|
4132
|
+
}
|
|
4133
|
+
return "MAL_NODE";
|
|
4134
|
+
}
|
|
3902
4135
|
};
|
|
3903
|
-
|
|
3904
|
-
|
|
3905
|
-
|
|
4136
|
+
var TreeMultiMap = class extends RedBlackTree {
|
|
4137
|
+
static {
|
|
4138
|
+
__name(this, "TreeMultiMap");
|
|
4139
|
+
}
|
|
3906
4140
|
/**
|
|
3907
4141
|
* Create a TreeMultiMap and optionally bulk-insert items.
|
|
3908
4142
|
* @remarks Time O(N log N), Space O(N)
|
|
@@ -3919,6 +4153,16 @@ var _TreeMultiMap = class _TreeMultiMap extends RedBlackTree {
|
|
|
3919
4153
|
createNode(key, value = []) {
|
|
3920
4154
|
return new TreeMultiMapNode(key, this._isMapMode ? [] : value);
|
|
3921
4155
|
}
|
|
4156
|
+
/**
|
|
4157
|
+
* Checks if the given item is a `TreeMultiMapNode` instance.
|
|
4158
|
+
* @remarks Time O(1), Space O(1)
|
|
4159
|
+
*
|
|
4160
|
+
* @param keyNodeOrEntry - The item to check.
|
|
4161
|
+
* @returns True if it's a TreeMultiMapNode, false otherwise.
|
|
4162
|
+
*/
|
|
4163
|
+
isNode(keyNodeOrEntry) {
|
|
4164
|
+
return keyNodeOrEntry instanceof TreeMultiMapNode;
|
|
4165
|
+
}
|
|
3922
4166
|
/**
|
|
3923
4167
|
* Insert a value or a list of values into the multimap. If the key exists, values are appended.
|
|
3924
4168
|
* @remarks Time O(log N + M), Space O(1)
|
|
@@ -3999,7 +4243,7 @@ var _TreeMultiMap = class _TreeMultiMap extends RedBlackTree {
|
|
|
3999
4243
|
map(callback, options, thisArg) {
|
|
4000
4244
|
const out = this._createLike([], options);
|
|
4001
4245
|
let i = 0;
|
|
4002
|
-
for (const [k, v] of this) out.add(callback.call(thisArg,
|
|
4246
|
+
for (const [k, v] of this) out.add(callback.call(thisArg, v, k, i++, this));
|
|
4003
4247
|
return out;
|
|
4004
4248
|
}
|
|
4005
4249
|
/**
|
|
@@ -4012,9 +4256,8 @@ var _TreeMultiMap = class _TreeMultiMap extends RedBlackTree {
|
|
|
4012
4256
|
* @returns An empty like-kind instance.
|
|
4013
4257
|
*/
|
|
4014
4258
|
_createInstance(options) {
|
|
4015
|
-
var _a, _b;
|
|
4016
4259
|
const Ctor = this.constructor;
|
|
4017
|
-
return new Ctor([], { ...
|
|
4260
|
+
return new Ctor([], { ...this._snapshotOptions?.() ?? {}, ...options ?? {} });
|
|
4018
4261
|
}
|
|
4019
4262
|
/**
|
|
4020
4263
|
* (Protected) Create a like-kind instance and seed it from an iterable.
|
|
@@ -4027,13 +4270,10 @@ var _TreeMultiMap = class _TreeMultiMap extends RedBlackTree {
|
|
|
4027
4270
|
* @returns A like-kind RedBlackTree built from the iterable.
|
|
4028
4271
|
*/
|
|
4029
4272
|
_createLike(iter = [], options) {
|
|
4030
|
-
var _a, _b;
|
|
4031
4273
|
const Ctor = this.constructor;
|
|
4032
|
-
return new Ctor(iter, { ...
|
|
4274
|
+
return new Ctor(iter, { ...this._snapshotOptions?.() ?? {}, ...options ?? {} });
|
|
4033
4275
|
}
|
|
4034
4276
|
};
|
|
4035
|
-
__name(_TreeMultiMap, "TreeMultiMap");
|
|
4036
|
-
var TreeMultiMap = _TreeMultiMap;
|
|
4037
4277
|
/**
|
|
4038
4278
|
* data-structure-typed
|
|
4039
4279
|
*
|