red-black-tree-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 +329 -177
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +4019 -0
- package/dist/cjs-legacy/index.cjs.map +1 -0
- package/dist/esm/index.mjs +329 -177
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +4010 -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/red-black-tree-typed.js +176 -22
- package/dist/umd/red-black-tree-typed.js.map +1 -1
- package/dist/umd/red-black-tree-typed.min.js +3 -3
- package/dist/umd/red-black-tree-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/cjs/index.cjs
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var __defProp = Object.defineProperty;
|
|
4
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
5
4
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
|
-
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
7
5
|
|
|
8
6
|
// src/utils/utils.ts
|
|
9
7
|
function isPrimitiveComparable(value) {
|
|
@@ -62,7 +60,10 @@ function makeTrampoline(fn) {
|
|
|
62
60
|
__name(makeTrampoline, "makeTrampoline");
|
|
63
61
|
|
|
64
62
|
// src/data-structures/base/iterable-element-base.ts
|
|
65
|
-
var
|
|
63
|
+
var IterableElementBase = class {
|
|
64
|
+
static {
|
|
65
|
+
__name(this, "IterableElementBase");
|
|
66
|
+
}
|
|
66
67
|
/**
|
|
67
68
|
* Create a new iterable base.
|
|
68
69
|
*
|
|
@@ -73,19 +74,19 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
73
74
|
* Time O(1), Space O(1).
|
|
74
75
|
*/
|
|
75
76
|
constructor(options) {
|
|
76
|
-
/**
|
|
77
|
-
* The converter used to transform a raw element (`R`) into a public element (`E`).
|
|
78
|
-
*
|
|
79
|
-
* @remarks
|
|
80
|
-
* Time O(1), Space O(1).
|
|
81
|
-
*/
|
|
82
|
-
__publicField(this, "_toElementFn");
|
|
83
77
|
if (options) {
|
|
84
78
|
const { toElementFn } = options;
|
|
85
79
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
86
80
|
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
87
81
|
}
|
|
88
82
|
}
|
|
83
|
+
/**
|
|
84
|
+
* The converter used to transform a raw element (`R`) into a public element (`E`).
|
|
85
|
+
*
|
|
86
|
+
* @remarks
|
|
87
|
+
* Time O(1), Space O(1).
|
|
88
|
+
*/
|
|
89
|
+
_toElementFn;
|
|
89
90
|
/**
|
|
90
91
|
* Exposes the current `toElementFn`, if configured.
|
|
91
92
|
*
|
|
@@ -280,11 +281,12 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
280
281
|
console.log(this.toVisual());
|
|
281
282
|
}
|
|
282
283
|
};
|
|
283
|
-
__name(_IterableElementBase, "IterableElementBase");
|
|
284
|
-
var IterableElementBase = _IterableElementBase;
|
|
285
284
|
|
|
286
285
|
// src/data-structures/base/linear-base.ts
|
|
287
|
-
var
|
|
286
|
+
var LinearBase = class _LinearBase extends IterableElementBase {
|
|
287
|
+
static {
|
|
288
|
+
__name(this, "LinearBase");
|
|
289
|
+
}
|
|
288
290
|
/**
|
|
289
291
|
* Construct a linear container with runtime options.
|
|
290
292
|
* @param options - `{ maxLen?, ... }` bounds/behavior options.
|
|
@@ -292,12 +294,12 @@ var _LinearBase = class _LinearBase extends IterableElementBase {
|
|
|
292
294
|
*/
|
|
293
295
|
constructor(options) {
|
|
294
296
|
super(options);
|
|
295
|
-
__publicField(this, "_maxLen", -1);
|
|
296
297
|
if (options) {
|
|
297
298
|
const { maxLen } = options;
|
|
298
299
|
if (typeof maxLen === "number" && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen;
|
|
299
300
|
}
|
|
300
301
|
}
|
|
302
|
+
_maxLen = -1;
|
|
301
303
|
/**
|
|
302
304
|
* Upper bound for length (if positive), or `-1` when unbounded.
|
|
303
305
|
* @returns Maximum allowed length.
|
|
@@ -430,7 +432,7 @@ var _LinearBase = class _LinearBase extends IterableElementBase {
|
|
|
430
432
|
return array;
|
|
431
433
|
}
|
|
432
434
|
reduceRight(callbackfn, initialValue) {
|
|
433
|
-
let accumulator = initialValue
|
|
435
|
+
let accumulator = initialValue ?? 0;
|
|
434
436
|
for (let i = this.length - 1; i >= 0; i--) {
|
|
435
437
|
accumulator = callbackfn(accumulator, this.at(i), i, this);
|
|
436
438
|
}
|
|
@@ -472,11 +474,12 @@ var _LinearBase = class _LinearBase extends IterableElementBase {
|
|
|
472
474
|
return this;
|
|
473
475
|
}
|
|
474
476
|
};
|
|
475
|
-
__name(_LinearBase, "LinearBase");
|
|
476
|
-
var LinearBase = _LinearBase;
|
|
477
477
|
|
|
478
478
|
// src/data-structures/queue/queue.ts
|
|
479
|
-
var
|
|
479
|
+
var Queue = class _Queue extends LinearBase {
|
|
480
|
+
static {
|
|
481
|
+
__name(this, "Queue");
|
|
482
|
+
}
|
|
480
483
|
/**
|
|
481
484
|
* Create a Queue and optionally bulk-insert elements.
|
|
482
485
|
* @remarks Time O(N), Space O(N)
|
|
@@ -486,15 +489,13 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
486
489
|
*/
|
|
487
490
|
constructor(elements = [], options) {
|
|
488
491
|
super(options);
|
|
489
|
-
__publicField(this, "_elements", []);
|
|
490
|
-
__publicField(this, "_offset", 0);
|
|
491
|
-
__publicField(this, "_autoCompactRatio", 0.5);
|
|
492
492
|
if (options) {
|
|
493
493
|
const { autoCompactRatio = 0.5 } = options;
|
|
494
494
|
this._autoCompactRatio = autoCompactRatio;
|
|
495
495
|
}
|
|
496
496
|
this.pushMany(elements);
|
|
497
497
|
}
|
|
498
|
+
_elements = [];
|
|
498
499
|
/**
|
|
499
500
|
* Get the underlying array buffer.
|
|
500
501
|
* @remarks Time O(1), Space O(1)
|
|
@@ -503,6 +504,7 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
503
504
|
get elements() {
|
|
504
505
|
return this._elements;
|
|
505
506
|
}
|
|
507
|
+
_offset = 0;
|
|
506
508
|
/**
|
|
507
509
|
* Get the current start offset into the array.
|
|
508
510
|
* @remarks Time O(1), Space O(1)
|
|
@@ -511,6 +513,7 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
511
513
|
get offset() {
|
|
512
514
|
return this._offset;
|
|
513
515
|
}
|
|
516
|
+
_autoCompactRatio = 0.5;
|
|
514
517
|
/**
|
|
515
518
|
* Get the compaction threshold (offset/size).
|
|
516
519
|
* @remarks Time O(1), Space O(1)
|
|
@@ -755,11 +758,10 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
755
758
|
* @returns A new Queue with mapped elements.
|
|
756
759
|
*/
|
|
757
760
|
map(callback, options, thisArg) {
|
|
758
|
-
var _a, _b;
|
|
759
761
|
const out = new this.constructor([], {
|
|
760
|
-
toElementFn: options
|
|
761
|
-
maxLen:
|
|
762
|
-
autoCompactRatio:
|
|
762
|
+
toElementFn: options?.toElementFn,
|
|
763
|
+
maxLen: options?.maxLen ?? this._maxLen,
|
|
764
|
+
autoCompactRatio: options?.autoCompactRatio ?? this._autoCompactRatio
|
|
763
765
|
});
|
|
764
766
|
let index = 0;
|
|
765
767
|
for (const v of this)
|
|
@@ -774,14 +776,13 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
774
776
|
* @returns A new queue with mapped elements (same element type).
|
|
775
777
|
*/
|
|
776
778
|
mapSame(callback, thisArg) {
|
|
777
|
-
var _a;
|
|
778
779
|
const Ctor = this.constructor;
|
|
779
780
|
const out = new Ctor([], {
|
|
780
781
|
toElementFn: this.toElementFn,
|
|
781
782
|
maxLen: this._maxLen,
|
|
782
783
|
autoCompactRatio: this._autoCompactRatio
|
|
783
784
|
});
|
|
784
|
-
|
|
785
|
+
out._setAutoCompactRatio?.(this._autoCompactRatio);
|
|
785
786
|
let index = 0;
|
|
786
787
|
for (const v of this) {
|
|
787
788
|
const mv = thisArg === void 0 ? callback(v, index++, this) : callback.call(thisArg, v, index++, this);
|
|
@@ -841,11 +842,12 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
841
842
|
return new Ctor(elements, options);
|
|
842
843
|
}
|
|
843
844
|
};
|
|
844
|
-
__name(_Queue, "Queue");
|
|
845
|
-
var Queue = _Queue;
|
|
846
845
|
|
|
847
846
|
// src/data-structures/base/iterable-entry-base.ts
|
|
848
|
-
var
|
|
847
|
+
var IterableEntryBase = class {
|
|
848
|
+
static {
|
|
849
|
+
__name(this, "IterableEntryBase");
|
|
850
|
+
}
|
|
849
851
|
/**
|
|
850
852
|
* Default iterator yielding `[key, value]` entries.
|
|
851
853
|
* @returns Iterator of `[K, V]`.
|
|
@@ -894,7 +896,7 @@ var _IterableEntryBase = class _IterableEntryBase {
|
|
|
894
896
|
every(predicate, thisArg) {
|
|
895
897
|
let index = 0;
|
|
896
898
|
for (const item of this) {
|
|
897
|
-
if (!predicate.call(thisArg, item[
|
|
899
|
+
if (!predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
898
900
|
return false;
|
|
899
901
|
}
|
|
900
902
|
}
|
|
@@ -910,7 +912,7 @@ var _IterableEntryBase = class _IterableEntryBase {
|
|
|
910
912
|
some(predicate, thisArg) {
|
|
911
913
|
let index = 0;
|
|
912
914
|
for (const item of this) {
|
|
913
|
-
if (predicate.call(thisArg, item[
|
|
915
|
+
if (predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
914
916
|
return true;
|
|
915
917
|
}
|
|
916
918
|
}
|
|
@@ -926,7 +928,7 @@ var _IterableEntryBase = class _IterableEntryBase {
|
|
|
926
928
|
let index = 0;
|
|
927
929
|
for (const item of this) {
|
|
928
930
|
const [key, value] = item;
|
|
929
|
-
callbackfn.call(thisArg,
|
|
931
|
+
callbackfn.call(thisArg, value, key, index++, this);
|
|
930
932
|
}
|
|
931
933
|
}
|
|
932
934
|
/**
|
|
@@ -940,7 +942,7 @@ var _IterableEntryBase = class _IterableEntryBase {
|
|
|
940
942
|
let index = 0;
|
|
941
943
|
for (const item of this) {
|
|
942
944
|
const [key, value] = item;
|
|
943
|
-
if (callbackfn.call(thisArg,
|
|
945
|
+
if (callbackfn.call(thisArg, value, key, index++, this)) return item;
|
|
944
946
|
}
|
|
945
947
|
return;
|
|
946
948
|
}
|
|
@@ -1014,8 +1016,6 @@ var _IterableEntryBase = class _IterableEntryBase {
|
|
|
1014
1016
|
console.log(this.toVisual());
|
|
1015
1017
|
}
|
|
1016
1018
|
};
|
|
1017
|
-
__name(_IterableEntryBase, "IterableEntryBase");
|
|
1018
|
-
var IterableEntryBase = _IterableEntryBase;
|
|
1019
1019
|
|
|
1020
1020
|
// src/common/index.ts
|
|
1021
1021
|
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
@@ -1023,7 +1023,7 @@ var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
|
1023
1023
|
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
1024
1024
|
return DFSOperation2;
|
|
1025
1025
|
})(DFSOperation || {});
|
|
1026
|
-
var
|
|
1026
|
+
var Range = class {
|
|
1027
1027
|
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
1028
1028
|
this.low = low;
|
|
1029
1029
|
this.high = high;
|
|
@@ -1032,6 +1032,9 @@ var _Range = class _Range {
|
|
|
1032
1032
|
if (!(isComparable(low) && isComparable(high))) throw new RangeError("low or high is not comparable");
|
|
1033
1033
|
if (low > high) throw new RangeError("low must be less than or equal to high");
|
|
1034
1034
|
}
|
|
1035
|
+
static {
|
|
1036
|
+
__name(this, "Range");
|
|
1037
|
+
}
|
|
1035
1038
|
// Determine whether a key is within the range
|
|
1036
1039
|
isInRange(key, comparator) {
|
|
1037
1040
|
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
@@ -1039,11 +1042,15 @@ var _Range = class _Range {
|
|
|
1039
1042
|
return lowCheck && highCheck;
|
|
1040
1043
|
}
|
|
1041
1044
|
};
|
|
1042
|
-
__name(_Range, "Range");
|
|
1043
|
-
var Range = _Range;
|
|
1044
1045
|
|
|
1045
1046
|
// src/data-structures/binary-tree/binary-tree.ts
|
|
1046
|
-
var
|
|
1047
|
+
var BinaryTreeNode = class {
|
|
1048
|
+
static {
|
|
1049
|
+
__name(this, "BinaryTreeNode");
|
|
1050
|
+
}
|
|
1051
|
+
key;
|
|
1052
|
+
value;
|
|
1053
|
+
parent = void 0;
|
|
1047
1054
|
/**
|
|
1048
1055
|
* Creates an instance of BinaryTreeNode.
|
|
1049
1056
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1052,17 +1059,10 @@ var _BinaryTreeNode = class _BinaryTreeNode {
|
|
|
1052
1059
|
* @param [value] - The value associated with the key.
|
|
1053
1060
|
*/
|
|
1054
1061
|
constructor(key, value) {
|
|
1055
|
-
__publicField(this, "key");
|
|
1056
|
-
__publicField(this, "value");
|
|
1057
|
-
__publicField(this, "parent");
|
|
1058
|
-
__publicField(this, "_left");
|
|
1059
|
-
__publicField(this, "_right");
|
|
1060
|
-
__publicField(this, "_height", 0);
|
|
1061
|
-
__publicField(this, "_color", "BLACK");
|
|
1062
|
-
__publicField(this, "_count", 1);
|
|
1063
1062
|
this.key = key;
|
|
1064
1063
|
this.value = value;
|
|
1065
1064
|
}
|
|
1065
|
+
_left = void 0;
|
|
1066
1066
|
/**
|
|
1067
1067
|
* Gets the left child of the node.
|
|
1068
1068
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1084,6 +1084,7 @@ var _BinaryTreeNode = class _BinaryTreeNode {
|
|
|
1084
1084
|
}
|
|
1085
1085
|
this._left = v;
|
|
1086
1086
|
}
|
|
1087
|
+
_right = void 0;
|
|
1087
1088
|
/**
|
|
1088
1089
|
* Gets the right child of the node.
|
|
1089
1090
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1105,6 +1106,7 @@ var _BinaryTreeNode = class _BinaryTreeNode {
|
|
|
1105
1106
|
}
|
|
1106
1107
|
this._right = v;
|
|
1107
1108
|
}
|
|
1109
|
+
_height = 0;
|
|
1108
1110
|
/**
|
|
1109
1111
|
* Gets the height of the node (used in self-balancing trees).
|
|
1110
1112
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1123,6 +1125,7 @@ var _BinaryTreeNode = class _BinaryTreeNode {
|
|
|
1123
1125
|
set height(value) {
|
|
1124
1126
|
this._height = value;
|
|
1125
1127
|
}
|
|
1128
|
+
_color = "BLACK";
|
|
1126
1129
|
/**
|
|
1127
1130
|
* Gets the color of the node (used in Red-Black trees).
|
|
1128
1131
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1141,6 +1144,7 @@ var _BinaryTreeNode = class _BinaryTreeNode {
|
|
|
1141
1144
|
set color(value) {
|
|
1142
1145
|
this._color = value;
|
|
1143
1146
|
}
|
|
1147
|
+
_count = 1;
|
|
1144
1148
|
/**
|
|
1145
1149
|
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
1146
1150
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1177,9 +1181,11 @@ var _BinaryTreeNode = class _BinaryTreeNode {
|
|
|
1177
1181
|
return "MAL_NODE";
|
|
1178
1182
|
}
|
|
1179
1183
|
};
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1184
|
+
var BinaryTree = class extends IterableEntryBase {
|
|
1185
|
+
static {
|
|
1186
|
+
__name(this, "BinaryTree");
|
|
1187
|
+
}
|
|
1188
|
+
iterationType = "ITERATIVE";
|
|
1183
1189
|
/**
|
|
1184
1190
|
* Creates an instance of BinaryTree.
|
|
1185
1191
|
* @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.
|
|
@@ -1189,22 +1195,6 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1189
1195
|
*/
|
|
1190
1196
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
1191
1197
|
super();
|
|
1192
|
-
__publicField(this, "iterationType", "ITERATIVE");
|
|
1193
|
-
__publicField(this, "_isMapMode", true);
|
|
1194
|
-
__publicField(this, "_isDuplicate", false);
|
|
1195
|
-
__publicField(this, "_store", /* @__PURE__ */ new Map());
|
|
1196
|
-
__publicField(this, "_root");
|
|
1197
|
-
__publicField(this, "_size", 0);
|
|
1198
|
-
__publicField(this, "_NIL", new BinaryTreeNode(NaN));
|
|
1199
|
-
__publicField(this, "_toEntryFn");
|
|
1200
|
-
/**
|
|
1201
|
-
* (Protected) Default callback function, returns the node's key.
|
|
1202
|
-
* @remarks Time O(1)
|
|
1203
|
-
*
|
|
1204
|
-
* @param node - The node.
|
|
1205
|
-
* @returns The node's key or undefined.
|
|
1206
|
-
*/
|
|
1207
|
-
__publicField(this, "_DEFAULT_NODE_CALLBACK", /* @__PURE__ */ __name((node) => node ? node.key : void 0, "_DEFAULT_NODE_CALLBACK"));
|
|
1208
1198
|
if (options) {
|
|
1209
1199
|
const { iterationType, toEntryFn, isMapMode, isDuplicate } = options;
|
|
1210
1200
|
if (iterationType) this.iterationType = iterationType;
|
|
@@ -1215,6 +1205,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1215
1205
|
}
|
|
1216
1206
|
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
1217
1207
|
}
|
|
1208
|
+
_isMapMode = true;
|
|
1218
1209
|
/**
|
|
1219
1210
|
* Gets whether the tree is in Map mode.
|
|
1220
1211
|
* @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)
|
|
@@ -1224,6 +1215,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1224
1215
|
get isMapMode() {
|
|
1225
1216
|
return this._isMapMode;
|
|
1226
1217
|
}
|
|
1218
|
+
_isDuplicate = false;
|
|
1227
1219
|
/**
|
|
1228
1220
|
* Gets whether the tree allows duplicate keys.
|
|
1229
1221
|
* @remarks Time O(1)
|
|
@@ -1233,6 +1225,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1233
1225
|
get isDuplicate() {
|
|
1234
1226
|
return this._isDuplicate;
|
|
1235
1227
|
}
|
|
1228
|
+
_store = /* @__PURE__ */ new Map();
|
|
1236
1229
|
/**
|
|
1237
1230
|
* Gets the external value store (used in Map mode).
|
|
1238
1231
|
* @remarks Time O(1)
|
|
@@ -1242,6 +1235,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1242
1235
|
get store() {
|
|
1243
1236
|
return this._store;
|
|
1244
1237
|
}
|
|
1238
|
+
_root;
|
|
1245
1239
|
/**
|
|
1246
1240
|
* Gets the root node of the tree.
|
|
1247
1241
|
* @remarks Time O(1)
|
|
@@ -1251,6 +1245,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1251
1245
|
get root() {
|
|
1252
1246
|
return this._root;
|
|
1253
1247
|
}
|
|
1248
|
+
_size = 0;
|
|
1254
1249
|
/**
|
|
1255
1250
|
* Gets the number of nodes in the tree.
|
|
1256
1251
|
* @remarks Time O(1)
|
|
@@ -1260,6 +1255,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1260
1255
|
get size() {
|
|
1261
1256
|
return this._size;
|
|
1262
1257
|
}
|
|
1258
|
+
_NIL = new BinaryTreeNode(NaN);
|
|
1263
1259
|
/**
|
|
1264
1260
|
* Gets the sentinel NIL node (used in self-balancing trees like Red-Black Tree).
|
|
1265
1261
|
* @remarks Time O(1)
|
|
@@ -1269,6 +1265,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1269
1265
|
get NIL() {
|
|
1270
1266
|
return this._NIL;
|
|
1271
1267
|
}
|
|
1268
|
+
_toEntryFn;
|
|
1272
1269
|
/**
|
|
1273
1270
|
* Gets the function used to convert raw data objects (R) into [key, value] entries.
|
|
1274
1271
|
* @remarks Time O(1)
|
|
@@ -1428,7 +1425,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1428
1425
|
if (newNode === void 0) return false;
|
|
1429
1426
|
if (!this._root) {
|
|
1430
1427
|
this._setRoot(newNode);
|
|
1431
|
-
if (this._isMapMode) this._setValue(newNode
|
|
1428
|
+
if (this._isMapMode) this._setValue(newNode?.key, newValue);
|
|
1432
1429
|
this._size = 1;
|
|
1433
1430
|
return true;
|
|
1434
1431
|
}
|
|
@@ -1460,7 +1457,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1460
1457
|
} else if (potentialParent.right === void 0) {
|
|
1461
1458
|
potentialParent.right = newNode;
|
|
1462
1459
|
}
|
|
1463
|
-
if (this._isMapMode) this._setValue(newNode
|
|
1460
|
+
if (this._isMapMode) this._setValue(newNode?.key, newValue);
|
|
1464
1461
|
this._size++;
|
|
1465
1462
|
return true;
|
|
1466
1463
|
}
|
|
@@ -1525,7 +1522,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1525
1522
|
if (!this._root) return deletedResult;
|
|
1526
1523
|
const curr = this.getNode(keyNodeOrEntry);
|
|
1527
1524
|
if (!curr) return deletedResult;
|
|
1528
|
-
const parent = curr
|
|
1525
|
+
const parent = curr?.parent;
|
|
1529
1526
|
let needBalanced;
|
|
1530
1527
|
let orgCurrent = curr;
|
|
1531
1528
|
if (!curr.left && !curr.right && !parent) {
|
|
@@ -1630,13 +1627,12 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1630
1627
|
* @returns The associated value, or undefined.
|
|
1631
1628
|
*/
|
|
1632
1629
|
get(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
1633
|
-
var _a;
|
|
1634
1630
|
if (this._isMapMode) {
|
|
1635
1631
|
const key = this._extractKey(keyNodeEntryOrPredicate);
|
|
1636
1632
|
if (key === null || key === void 0) return;
|
|
1637
1633
|
return this._store.get(key);
|
|
1638
1634
|
}
|
|
1639
|
-
return
|
|
1635
|
+
return this.getNode(keyNodeEntryOrPredicate, startNode, iterationType)?.value;
|
|
1640
1636
|
}
|
|
1641
1637
|
has(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
1642
1638
|
return this.search(keyNodeEntryOrPredicate, true, (node) => node, startNode, iterationType).length > 0;
|
|
@@ -1724,7 +1720,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1724
1720
|
let distEnsured = this.ensureNode(dist);
|
|
1725
1721
|
const beginRootEnsured = this.ensureNode(startNode);
|
|
1726
1722
|
let depth = 0;
|
|
1727
|
-
while (distEnsured
|
|
1723
|
+
while (distEnsured?.parent) {
|
|
1728
1724
|
if (distEnsured === beginRootEnsured) {
|
|
1729
1725
|
return depth;
|
|
1730
1726
|
}
|
|
@@ -2197,7 +2193,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2197
2193
|
filter(predicate, thisArg) {
|
|
2198
2194
|
const out = this._createInstance();
|
|
2199
2195
|
let i = 0;
|
|
2200
|
-
for (const [k, v] of this) if (predicate.call(thisArg,
|
|
2196
|
+
for (const [k, v] of this) if (predicate.call(thisArg, v, k, i++, this)) out.add([k, v]);
|
|
2201
2197
|
return out;
|
|
2202
2198
|
}
|
|
2203
2199
|
/**
|
|
@@ -2215,7 +2211,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2215
2211
|
map(cb, options, thisArg) {
|
|
2216
2212
|
const out = this._createLike([], options);
|
|
2217
2213
|
let i = 0;
|
|
2218
|
-
for (const [k, v] of this) out.add(cb.call(thisArg,
|
|
2214
|
+
for (const [k, v] of this) out.add(cb.call(thisArg, v, k, i++, this));
|
|
2219
2215
|
return out;
|
|
2220
2216
|
}
|
|
2221
2217
|
/**
|
|
@@ -2286,10 +2282,10 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2286
2282
|
const dfs = /* @__PURE__ */ __name((node) => {
|
|
2287
2283
|
if (!shouldVisitRoot(node)) return;
|
|
2288
2284
|
const visitLeft = /* @__PURE__ */ __name(() => {
|
|
2289
|
-
if (shouldVisitLeft(node) &&
|
|
2285
|
+
if (shouldVisitLeft(node) && node?.left !== void 0) dfs(node?.left);
|
|
2290
2286
|
}, "visitLeft");
|
|
2291
2287
|
const visitRight = /* @__PURE__ */ __name(() => {
|
|
2292
|
-
if (shouldVisitRight(node) &&
|
|
2288
|
+
if (shouldVisitRight(node) && node?.right !== void 0) dfs(node?.right);
|
|
2293
2289
|
}, "visitRight");
|
|
2294
2290
|
switch (pattern) {
|
|
2295
2291
|
case "IN":
|
|
@@ -2322,12 +2318,10 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2322
2318
|
} else {
|
|
2323
2319
|
const stack = [{ opt: 0 /* VISIT */, node: startNode }];
|
|
2324
2320
|
const pushLeft = /* @__PURE__ */ __name((cur) => {
|
|
2325
|
-
|
|
2326
|
-
if (shouldVisitLeft(cur.node)) stack.push({ opt: 0 /* VISIT */, node: (_a = cur.node) == null ? void 0 : _a.left });
|
|
2321
|
+
if (shouldVisitLeft(cur.node)) stack.push({ opt: 0 /* VISIT */, node: cur.node?.left });
|
|
2327
2322
|
}, "pushLeft");
|
|
2328
2323
|
const pushRight = /* @__PURE__ */ __name((cur) => {
|
|
2329
|
-
|
|
2330
|
-
if (shouldVisitRight(cur.node)) stack.push({ opt: 0 /* VISIT */, node: (_a = cur.node) == null ? void 0 : _a.right });
|
|
2324
|
+
if (shouldVisitRight(cur.node)) stack.push({ opt: 0 /* VISIT */, node: cur.node?.right });
|
|
2331
2325
|
}, "pushRight");
|
|
2332
2326
|
const pushRoot = /* @__PURE__ */ __name((cur) => {
|
|
2333
2327
|
if (shouldVisitRoot(cur.node)) stack.push({ opt: 1 /* PROCESS */, node: cur.node });
|
|
@@ -2399,6 +2393,14 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2399
2393
|
}
|
|
2400
2394
|
}
|
|
2401
2395
|
}
|
|
2396
|
+
/**
|
|
2397
|
+
* (Protected) Default callback function, returns the node's key.
|
|
2398
|
+
* @remarks Time O(1)
|
|
2399
|
+
*
|
|
2400
|
+
* @param node - The node.
|
|
2401
|
+
* @returns The node's key or undefined.
|
|
2402
|
+
*/
|
|
2403
|
+
_DEFAULT_NODE_CALLBACK = /* @__PURE__ */ __name((node) => node ? node.key : void 0, "_DEFAULT_NODE_CALLBACK");
|
|
2402
2404
|
/**
|
|
2403
2405
|
* (Protected) Snapshots the current tree's configuration options.
|
|
2404
2406
|
* @remarks Time O(1)
|
|
@@ -2424,7 +2426,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2424
2426
|
*/
|
|
2425
2427
|
_createInstance(options) {
|
|
2426
2428
|
const Ctor = this.constructor;
|
|
2427
|
-
return new Ctor([], { ...this._snapshotOptions(), ...options
|
|
2429
|
+
return new Ctor([], { ...this._snapshotOptions(), ...options ?? {} });
|
|
2428
2430
|
}
|
|
2429
2431
|
/**
|
|
2430
2432
|
* (Protected) Creates a new instance of the same tree constructor, potentially with different generic types.
|
|
@@ -2437,7 +2439,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2437
2439
|
*/
|
|
2438
2440
|
_createLike(iter = [], options) {
|
|
2439
2441
|
const Ctor = this.constructor;
|
|
2440
|
-
return new Ctor(iter, { ...this._snapshotOptions(), ...options
|
|
2442
|
+
return new Ctor(iter, { ...this._snapshotOptions(), ...options ?? {} });
|
|
2441
2443
|
}
|
|
2442
2444
|
/**
|
|
2443
2445
|
* (Protected) Converts a key, node, or entry into a standardized [node, value] tuple.
|
|
@@ -2455,7 +2457,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2455
2457
|
const [key, entryValue] = keyNodeOrEntry;
|
|
2456
2458
|
if (key === void 0) return [void 0, void 0];
|
|
2457
2459
|
else if (key === null) return [null, void 0];
|
|
2458
|
-
const finalValue = value
|
|
2460
|
+
const finalValue = value ?? entryValue;
|
|
2459
2461
|
return [this.createNode(key, finalValue), finalValue];
|
|
2460
2462
|
}
|
|
2461
2463
|
return [this.createNode(keyNodeOrEntry, value), value];
|
|
@@ -2662,11 +2664,15 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2662
2664
|
this._store.clear();
|
|
2663
2665
|
}
|
|
2664
2666
|
};
|
|
2665
|
-
__name(_BinaryTree, "BinaryTree");
|
|
2666
|
-
var BinaryTree = _BinaryTree;
|
|
2667
2667
|
|
|
2668
2668
|
// src/data-structures/binary-tree/bst.ts
|
|
2669
|
-
var
|
|
2669
|
+
var BSTNode = class {
|
|
2670
|
+
static {
|
|
2671
|
+
__name(this, "BSTNode");
|
|
2672
|
+
}
|
|
2673
|
+
key;
|
|
2674
|
+
value;
|
|
2675
|
+
parent = void 0;
|
|
2670
2676
|
/**
|
|
2671
2677
|
* Creates an instance of BSTNode.
|
|
2672
2678
|
* @remarks Time O(1), Space O(1)
|
|
@@ -2675,11 +2681,10 @@ var _BSTNode = class _BSTNode extends BinaryTreeNode {
|
|
|
2675
2681
|
* @param [value] - The value associated with the key.
|
|
2676
2682
|
*/
|
|
2677
2683
|
constructor(key, value) {
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
__publicField(this, "_left");
|
|
2681
|
-
__publicField(this, "_right");
|
|
2684
|
+
this.key = key;
|
|
2685
|
+
this.value = value;
|
|
2682
2686
|
}
|
|
2687
|
+
_left = void 0;
|
|
2683
2688
|
/**
|
|
2684
2689
|
* Gets the left child of the node.
|
|
2685
2690
|
* @remarks Time O(1), Space O(1)
|
|
@@ -2699,6 +2704,7 @@ var _BSTNode = class _BSTNode extends BinaryTreeNode {
|
|
|
2699
2704
|
if (v) v.parent = this;
|
|
2700
2705
|
this._left = v;
|
|
2701
2706
|
}
|
|
2707
|
+
_right = void 0;
|
|
2702
2708
|
/**
|
|
2703
2709
|
* Gets the right child of the node.
|
|
2704
2710
|
* @remarks Time O(1), Space O(1)
|
|
@@ -2718,10 +2724,85 @@ var _BSTNode = class _BSTNode extends BinaryTreeNode {
|
|
|
2718
2724
|
if (v) v.parent = this;
|
|
2719
2725
|
this._right = v;
|
|
2720
2726
|
}
|
|
2727
|
+
_height = 0;
|
|
2728
|
+
/**
|
|
2729
|
+
* Gets the height of the node (used in self-balancing trees).
|
|
2730
|
+
* @remarks Time O(1), Space O(1)
|
|
2731
|
+
*
|
|
2732
|
+
* @returns The height.
|
|
2733
|
+
*/
|
|
2734
|
+
get height() {
|
|
2735
|
+
return this._height;
|
|
2736
|
+
}
|
|
2737
|
+
/**
|
|
2738
|
+
* Sets the height of the node.
|
|
2739
|
+
* @remarks Time O(1), Space O(1)
|
|
2740
|
+
*
|
|
2741
|
+
* @param value - The new height.
|
|
2742
|
+
*/
|
|
2743
|
+
set height(value) {
|
|
2744
|
+
this._height = value;
|
|
2745
|
+
}
|
|
2746
|
+
_color = "BLACK";
|
|
2747
|
+
/**
|
|
2748
|
+
* Gets the color of the node (used in Red-Black trees).
|
|
2749
|
+
* @remarks Time O(1), Space O(1)
|
|
2750
|
+
*
|
|
2751
|
+
* @returns The node's color.
|
|
2752
|
+
*/
|
|
2753
|
+
get color() {
|
|
2754
|
+
return this._color;
|
|
2755
|
+
}
|
|
2756
|
+
/**
|
|
2757
|
+
* Sets the color of the node.
|
|
2758
|
+
* @remarks Time O(1), Space O(1)
|
|
2759
|
+
*
|
|
2760
|
+
* @param value - The new color.
|
|
2761
|
+
*/
|
|
2762
|
+
set color(value) {
|
|
2763
|
+
this._color = value;
|
|
2764
|
+
}
|
|
2765
|
+
_count = 1;
|
|
2766
|
+
/**
|
|
2767
|
+
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
2768
|
+
* @remarks Time O(1), Space O(1)
|
|
2769
|
+
*
|
|
2770
|
+
* @returns The subtree node count.
|
|
2771
|
+
*/
|
|
2772
|
+
get count() {
|
|
2773
|
+
return this._count;
|
|
2774
|
+
}
|
|
2775
|
+
/**
|
|
2776
|
+
* Sets the count of nodes in the subtree.
|
|
2777
|
+
* @remarks Time O(1), Space O(1)
|
|
2778
|
+
*
|
|
2779
|
+
* @param value - The new count.
|
|
2780
|
+
*/
|
|
2781
|
+
set count(value) {
|
|
2782
|
+
this._count = value;
|
|
2783
|
+
}
|
|
2784
|
+
/**
|
|
2785
|
+
* Gets the position of the node relative to its parent.
|
|
2786
|
+
* @remarks Time O(1), Space O(1)
|
|
2787
|
+
*
|
|
2788
|
+
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
2789
|
+
*/
|
|
2790
|
+
get familyPosition() {
|
|
2791
|
+
if (!this.parent) {
|
|
2792
|
+
return this.left || this.right ? "ROOT" : "ISOLATED";
|
|
2793
|
+
}
|
|
2794
|
+
if (this.parent.left === this) {
|
|
2795
|
+
return this.left || this.right ? "ROOT_LEFT" : "LEFT";
|
|
2796
|
+
} else if (this.parent.right === this) {
|
|
2797
|
+
return this.left || this.right ? "ROOT_RIGHT" : "RIGHT";
|
|
2798
|
+
}
|
|
2799
|
+
return "MAL_NODE";
|
|
2800
|
+
}
|
|
2721
2801
|
};
|
|
2722
|
-
|
|
2723
|
-
|
|
2724
|
-
|
|
2802
|
+
var BST = class extends BinaryTree {
|
|
2803
|
+
static {
|
|
2804
|
+
__name(this, "BST");
|
|
2805
|
+
}
|
|
2725
2806
|
/**
|
|
2726
2807
|
* Creates an instance of BST.
|
|
2727
2808
|
* @remarks Time O(N log N) or O(N^2) depending on `isBalanceAdd` in `addMany` and input order. Space O(N).
|
|
@@ -2731,33 +2812,6 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2731
2812
|
*/
|
|
2732
2813
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
2733
2814
|
super([], options);
|
|
2734
|
-
__publicField(this, "_root");
|
|
2735
|
-
__publicField(this, "_isReverse", false);
|
|
2736
|
-
/**
|
|
2737
|
-
* The default comparator function.
|
|
2738
|
-
* @remarks Time O(1) (or O(C) if `specifyComparable` is used, C is complexity of that function).
|
|
2739
|
-
*/
|
|
2740
|
-
__publicField(this, "_comparator", /* @__PURE__ */ __name((a, b) => {
|
|
2741
|
-
if (isComparable(a) && isComparable(b)) {
|
|
2742
|
-
if (a > b) return 1;
|
|
2743
|
-
if (a < b) return -1;
|
|
2744
|
-
return 0;
|
|
2745
|
-
}
|
|
2746
|
-
if (this._specifyComparable) {
|
|
2747
|
-
const va = this._specifyComparable(a);
|
|
2748
|
-
const vb = this._specifyComparable(b);
|
|
2749
|
-
if (va > vb) return 1;
|
|
2750
|
-
if (va < vb) return -1;
|
|
2751
|
-
return 0;
|
|
2752
|
-
}
|
|
2753
|
-
if (typeof a === "object" || typeof b === "object") {
|
|
2754
|
-
throw TypeError(
|
|
2755
|
-
`When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
|
|
2756
|
-
);
|
|
2757
|
-
}
|
|
2758
|
-
return 0;
|
|
2759
|
-
}, "_comparator"));
|
|
2760
|
-
__publicField(this, "_specifyComparable");
|
|
2761
2815
|
if (options) {
|
|
2762
2816
|
const { specifyComparable, isReverse } = options;
|
|
2763
2817
|
if (typeof specifyComparable === "function") this._specifyComparable = specifyComparable;
|
|
@@ -2765,6 +2819,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2765
2819
|
}
|
|
2766
2820
|
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
2767
2821
|
}
|
|
2822
|
+
_root = void 0;
|
|
2768
2823
|
/**
|
|
2769
2824
|
* Gets the root node of the tree.
|
|
2770
2825
|
* @remarks Time O(1)
|
|
@@ -2774,6 +2829,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2774
2829
|
get root() {
|
|
2775
2830
|
return this._root;
|
|
2776
2831
|
}
|
|
2832
|
+
_isReverse = false;
|
|
2777
2833
|
/**
|
|
2778
2834
|
* Gets whether the tree's comparison logic is reversed.
|
|
2779
2835
|
* @remarks Time O(1)
|
|
@@ -2783,6 +2839,30 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2783
2839
|
get isReverse() {
|
|
2784
2840
|
return this._isReverse;
|
|
2785
2841
|
}
|
|
2842
|
+
/**
|
|
2843
|
+
* The default comparator function.
|
|
2844
|
+
* @remarks Time O(1) (or O(C) if `specifyComparable` is used, C is complexity of that function).
|
|
2845
|
+
*/
|
|
2846
|
+
_comparator = /* @__PURE__ */ __name((a, b) => {
|
|
2847
|
+
if (isComparable(a) && isComparable(b)) {
|
|
2848
|
+
if (a > b) return 1;
|
|
2849
|
+
if (a < b) return -1;
|
|
2850
|
+
return 0;
|
|
2851
|
+
}
|
|
2852
|
+
if (this._specifyComparable) {
|
|
2853
|
+
const va = this._specifyComparable(a);
|
|
2854
|
+
const vb = this._specifyComparable(b);
|
|
2855
|
+
if (va > vb) return 1;
|
|
2856
|
+
if (va < vb) return -1;
|
|
2857
|
+
return 0;
|
|
2858
|
+
}
|
|
2859
|
+
if (typeof a === "object" || typeof b === "object") {
|
|
2860
|
+
throw TypeError(
|
|
2861
|
+
`When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
|
|
2862
|
+
);
|
|
2863
|
+
}
|
|
2864
|
+
return 0;
|
|
2865
|
+
}, "_comparator");
|
|
2786
2866
|
/**
|
|
2787
2867
|
* Gets the comparator function used by the tree.
|
|
2788
2868
|
* @remarks Time O(1)
|
|
@@ -2792,6 +2872,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2792
2872
|
get comparator() {
|
|
2793
2873
|
return this._comparator;
|
|
2794
2874
|
}
|
|
2875
|
+
_specifyComparable;
|
|
2795
2876
|
/**
|
|
2796
2877
|
* Gets the function used to extract a comparable value from a complex key.
|
|
2797
2878
|
* @remarks Time O(1)
|
|
@@ -2821,8 +2902,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2821
2902
|
* @returns The resolved node, or undefined if not found.
|
|
2822
2903
|
*/
|
|
2823
2904
|
ensureNode(keyNodeOrEntry, iterationType = this.iterationType) {
|
|
2824
|
-
|
|
2825
|
-
return (_a = super.ensureNode(keyNodeOrEntry, iterationType)) != null ? _a : void 0;
|
|
2905
|
+
return super.ensureNode(keyNodeOrEntry, iterationType) ?? void 0;
|
|
2826
2906
|
}
|
|
2827
2907
|
/**
|
|
2828
2908
|
* Checks if the given item is a `BSTNode` instance.
|
|
@@ -2895,8 +2975,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2895
2975
|
* @returns The first matching node, or undefined if not found.
|
|
2896
2976
|
*/
|
|
2897
2977
|
getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
2898
|
-
|
|
2899
|
-
return (_a = this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0]) != null ? _a : void 0;
|
|
2978
|
+
return this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0] ?? void 0;
|
|
2900
2979
|
}
|
|
2901
2980
|
/**
|
|
2902
2981
|
* Searches the tree for nodes matching a predicate, key, or range.
|
|
@@ -3001,7 +3080,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3001
3080
|
if (newNode === void 0) return false;
|
|
3002
3081
|
if (this._root === void 0) {
|
|
3003
3082
|
this._setRoot(newNode);
|
|
3004
|
-
if (this._isMapMode) this._setValue(newNode
|
|
3083
|
+
if (this._isMapMode) this._setValue(newNode?.key, newValue);
|
|
3005
3084
|
this._size++;
|
|
3006
3085
|
return true;
|
|
3007
3086
|
}
|
|
@@ -3014,7 +3093,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3014
3093
|
} else if (this._compare(current.key, newNode.key) > 0) {
|
|
3015
3094
|
if (current.left === void 0) {
|
|
3016
3095
|
current.left = newNode;
|
|
3017
|
-
if (this._isMapMode) this._setValue(newNode
|
|
3096
|
+
if (this._isMapMode) this._setValue(newNode?.key, newValue);
|
|
3018
3097
|
this._size++;
|
|
3019
3098
|
return true;
|
|
3020
3099
|
}
|
|
@@ -3022,7 +3101,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3022
3101
|
} else {
|
|
3023
3102
|
if (current.right === void 0) {
|
|
3024
3103
|
current.right = newNode;
|
|
3025
|
-
if (this._isMapMode) this._setValue(newNode
|
|
3104
|
+
if (this._isMapMode) this._setValue(newNode?.key, newValue);
|
|
3026
3105
|
this._size++;
|
|
3027
3106
|
return true;
|
|
3028
3107
|
}
|
|
@@ -3045,10 +3124,10 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3045
3124
|
*/
|
|
3046
3125
|
addMany(keysNodesEntriesOrRaws, values, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
3047
3126
|
const inserted = [];
|
|
3048
|
-
const valuesIterator = values
|
|
3127
|
+
const valuesIterator = values?.[Symbol.iterator]();
|
|
3049
3128
|
if (!isBalanceAdd) {
|
|
3050
3129
|
for (let kve of keysNodesEntriesOrRaws) {
|
|
3051
|
-
const val = valuesIterator
|
|
3130
|
+
const val = valuesIterator?.next().value;
|
|
3052
3131
|
if (this.isRaw(kve)) kve = this._toEntryFn(kve);
|
|
3053
3132
|
inserted.push(this.add(kve, val));
|
|
3054
3133
|
}
|
|
@@ -3057,7 +3136,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3057
3136
|
const realBTNExemplars = [];
|
|
3058
3137
|
let i = 0;
|
|
3059
3138
|
for (const kve of keysNodesEntriesOrRaws) {
|
|
3060
|
-
realBTNExemplars.push({ key: kve, value: valuesIterator
|
|
3139
|
+
realBTNExemplars.push({ key: kve, value: valuesIterator?.next().value, orgIndex: i++ });
|
|
3061
3140
|
}
|
|
3062
3141
|
const sorted = realBTNExemplars.sort(({ key: a }, { key: b }) => {
|
|
3063
3142
|
let keyA, keyB;
|
|
@@ -3238,7 +3317,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3238
3317
|
const out = this._createLike([], options);
|
|
3239
3318
|
let index = 0;
|
|
3240
3319
|
for (const [key, value] of this) {
|
|
3241
|
-
out.add(callback.call(thisArg,
|
|
3320
|
+
out.add(callback.call(thisArg, value, key, index++, this));
|
|
3242
3321
|
}
|
|
3243
3322
|
return out;
|
|
3244
3323
|
}
|
|
@@ -3279,7 +3358,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3279
3358
|
*/
|
|
3280
3359
|
_createInstance(options) {
|
|
3281
3360
|
const Ctor = this.constructor;
|
|
3282
|
-
return new Ctor([], { ...this._snapshotOptions(), ...options
|
|
3361
|
+
return new Ctor([], { ...this._snapshotOptions(), ...options ?? {} });
|
|
3283
3362
|
}
|
|
3284
3363
|
/**
|
|
3285
3364
|
* (Protected) Creates a new instance of the same BST constructor, potentially with different generic types.
|
|
@@ -3292,7 +3371,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3292
3371
|
*/
|
|
3293
3372
|
_createLike(iter = [], options) {
|
|
3294
3373
|
const Ctor = this.constructor;
|
|
3295
|
-
return new Ctor(iter, { ...this._snapshotOptions(), ...options
|
|
3374
|
+
return new Ctor(iter, { ...this._snapshotOptions(), ...options ?? {} });
|
|
3296
3375
|
}
|
|
3297
3376
|
/**
|
|
3298
3377
|
* (Protected) Snapshots the current BST's configuration options.
|
|
@@ -3319,7 +3398,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3319
3398
|
_keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value) {
|
|
3320
3399
|
const [node, entryValue] = super._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
3321
3400
|
if (node === null) return [void 0, void 0];
|
|
3322
|
-
return [node, value
|
|
3401
|
+
return [node, value ?? entryValue];
|
|
3323
3402
|
}
|
|
3324
3403
|
/**
|
|
3325
3404
|
* (Protected) Sets the root node and clears its parent reference.
|
|
@@ -3350,7 +3429,6 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3350
3429
|
* @returns True if the node was found and deleted, false otherwise.
|
|
3351
3430
|
*/
|
|
3352
3431
|
_deleteByKey(key) {
|
|
3353
|
-
var _a;
|
|
3354
3432
|
let node = this._root;
|
|
3355
3433
|
while (node) {
|
|
3356
3434
|
const cmp = this._compare(node.key, key);
|
|
@@ -3359,7 +3437,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3359
3437
|
}
|
|
3360
3438
|
if (!node) return false;
|
|
3361
3439
|
const transplant = /* @__PURE__ */ __name((u, v) => {
|
|
3362
|
-
const p = u
|
|
3440
|
+
const p = u?.parent;
|
|
3363
3441
|
if (!p) {
|
|
3364
3442
|
this._setRoot(v);
|
|
3365
3443
|
} else if (p.left === u) {
|
|
@@ -3389,15 +3467,19 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3389
3467
|
succ.left = node.left;
|
|
3390
3468
|
if (succ.left) succ.left.parent = succ;
|
|
3391
3469
|
}
|
|
3392
|
-
this._size = Math.max(0, (
|
|
3470
|
+
this._size = Math.max(0, (this._size ?? 0) - 1);
|
|
3393
3471
|
return true;
|
|
3394
3472
|
}
|
|
3395
3473
|
};
|
|
3396
|
-
__name(_BST, "BST");
|
|
3397
|
-
var BST = _BST;
|
|
3398
3474
|
|
|
3399
3475
|
// src/data-structures/binary-tree/red-black-tree.ts
|
|
3400
|
-
var
|
|
3476
|
+
var RedBlackTreeNode = class {
|
|
3477
|
+
static {
|
|
3478
|
+
__name(this, "RedBlackTreeNode");
|
|
3479
|
+
}
|
|
3480
|
+
key;
|
|
3481
|
+
value;
|
|
3482
|
+
parent = void 0;
|
|
3401
3483
|
/**
|
|
3402
3484
|
* Create a Red-Black Tree and optionally bulk-insert items.
|
|
3403
3485
|
* @remarks Time O(n log n), Space O(n)
|
|
@@ -3407,12 +3489,11 @@ var _RedBlackTreeNode = class _RedBlackTreeNode extends BSTNode {
|
|
|
3407
3489
|
* @returns New RedBlackTree instance.
|
|
3408
3490
|
*/
|
|
3409
3491
|
constructor(key, value, color = "BLACK") {
|
|
3410
|
-
|
|
3411
|
-
|
|
3412
|
-
|
|
3413
|
-
__publicField(this, "_right");
|
|
3414
|
-
this._color = color;
|
|
3492
|
+
this.key = key;
|
|
3493
|
+
this.value = value;
|
|
3494
|
+
this.color = color;
|
|
3415
3495
|
}
|
|
3496
|
+
_left = void 0;
|
|
3416
3497
|
/**
|
|
3417
3498
|
* Get the left child pointer.
|
|
3418
3499
|
* @remarks Time O(1), Space O(1)
|
|
@@ -3433,6 +3514,7 @@ var _RedBlackTreeNode = class _RedBlackTreeNode extends BSTNode {
|
|
|
3433
3514
|
}
|
|
3434
3515
|
this._left = v;
|
|
3435
3516
|
}
|
|
3517
|
+
_right = void 0;
|
|
3436
3518
|
/**
|
|
3437
3519
|
* Get the right child pointer.
|
|
3438
3520
|
* @remarks Time O(1), Space O(1)
|
|
@@ -3453,18 +3535,93 @@ var _RedBlackTreeNode = class _RedBlackTreeNode extends BSTNode {
|
|
|
3453
3535
|
}
|
|
3454
3536
|
this._right = v;
|
|
3455
3537
|
}
|
|
3538
|
+
_height = 0;
|
|
3539
|
+
/**
|
|
3540
|
+
* Gets the height of the node (used in self-balancing trees).
|
|
3541
|
+
* @remarks Time O(1), Space O(1)
|
|
3542
|
+
*
|
|
3543
|
+
* @returns The height.
|
|
3544
|
+
*/
|
|
3545
|
+
get height() {
|
|
3546
|
+
return this._height;
|
|
3547
|
+
}
|
|
3548
|
+
/**
|
|
3549
|
+
* Sets the height of the node.
|
|
3550
|
+
* @remarks Time O(1), Space O(1)
|
|
3551
|
+
*
|
|
3552
|
+
* @param value - The new height.
|
|
3553
|
+
*/
|
|
3554
|
+
set height(value) {
|
|
3555
|
+
this._height = value;
|
|
3556
|
+
}
|
|
3557
|
+
_color = "BLACK";
|
|
3558
|
+
/**
|
|
3559
|
+
* Gets the color of the node (used in Red-Black trees).
|
|
3560
|
+
* @remarks Time O(1), Space O(1)
|
|
3561
|
+
*
|
|
3562
|
+
* @returns The node's color.
|
|
3563
|
+
*/
|
|
3564
|
+
get color() {
|
|
3565
|
+
return this._color;
|
|
3566
|
+
}
|
|
3567
|
+
/**
|
|
3568
|
+
* Sets the color of the node.
|
|
3569
|
+
* @remarks Time O(1), Space O(1)
|
|
3570
|
+
*
|
|
3571
|
+
* @param value - The new color.
|
|
3572
|
+
*/
|
|
3573
|
+
set color(value) {
|
|
3574
|
+
this._color = value;
|
|
3575
|
+
}
|
|
3576
|
+
_count = 1;
|
|
3577
|
+
/**
|
|
3578
|
+
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
3579
|
+
* @remarks Time O(1), Space O(1)
|
|
3580
|
+
*
|
|
3581
|
+
* @returns The subtree node count.
|
|
3582
|
+
*/
|
|
3583
|
+
get count() {
|
|
3584
|
+
return this._count;
|
|
3585
|
+
}
|
|
3586
|
+
/**
|
|
3587
|
+
* Sets the count of nodes in the subtree.
|
|
3588
|
+
* @remarks Time O(1), Space O(1)
|
|
3589
|
+
*
|
|
3590
|
+
* @param value - The new count.
|
|
3591
|
+
*/
|
|
3592
|
+
set count(value) {
|
|
3593
|
+
this._count = value;
|
|
3594
|
+
}
|
|
3595
|
+
/**
|
|
3596
|
+
* Gets the position of the node relative to its parent.
|
|
3597
|
+
* @remarks Time O(1), Space O(1)
|
|
3598
|
+
*
|
|
3599
|
+
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
3600
|
+
*/
|
|
3601
|
+
get familyPosition() {
|
|
3602
|
+
if (!this.parent) {
|
|
3603
|
+
return this.left || this.right ? "ROOT" : "ISOLATED";
|
|
3604
|
+
}
|
|
3605
|
+
if (this.parent.left === this) {
|
|
3606
|
+
return this.left || this.right ? "ROOT_LEFT" : "LEFT";
|
|
3607
|
+
} else if (this.parent.right === this) {
|
|
3608
|
+
return this.left || this.right ? "ROOT_RIGHT" : "RIGHT";
|
|
3609
|
+
}
|
|
3610
|
+
return "MAL_NODE";
|
|
3611
|
+
}
|
|
3456
3612
|
};
|
|
3457
|
-
|
|
3458
|
-
|
|
3459
|
-
|
|
3613
|
+
var RedBlackTree = class extends BST {
|
|
3614
|
+
static {
|
|
3615
|
+
__name(this, "RedBlackTree");
|
|
3616
|
+
}
|
|
3460
3617
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
3461
3618
|
super([], options);
|
|
3462
|
-
__publicField(this, "_root");
|
|
3463
3619
|
this._root = this.NIL;
|
|
3464
3620
|
if (keysNodesEntriesOrRaws) {
|
|
3465
3621
|
this.addMany(keysNodesEntriesOrRaws);
|
|
3466
3622
|
}
|
|
3467
3623
|
}
|
|
3624
|
+
_root;
|
|
3468
3625
|
/**
|
|
3469
3626
|
* Get the current root node.
|
|
3470
3627
|
* @remarks Time O(1), Space O(1)
|
|
@@ -3603,17 +3760,17 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
3603
3760
|
const out = this._createLike([], options);
|
|
3604
3761
|
let index = 0;
|
|
3605
3762
|
for (const [key, value] of this) {
|
|
3606
|
-
out.add(callback.call(thisArg,
|
|
3763
|
+
out.add(callback.call(thisArg, value, key, index++, this));
|
|
3607
3764
|
}
|
|
3608
3765
|
return out;
|
|
3609
3766
|
}
|
|
3610
3767
|
_createInstance(options) {
|
|
3611
3768
|
const Ctor = this.constructor;
|
|
3612
|
-
return new Ctor([], { ...this._snapshotOptions(), ...options
|
|
3769
|
+
return new Ctor([], { ...this._snapshotOptions(), ...options ?? {} });
|
|
3613
3770
|
}
|
|
3614
3771
|
_createLike(iter = [], options) {
|
|
3615
3772
|
const Ctor = this.constructor;
|
|
3616
|
-
return new Ctor(iter, { ...this._snapshotOptions(), ...options
|
|
3773
|
+
return new Ctor(iter, { ...this._snapshotOptions(), ...options ?? {} });
|
|
3617
3774
|
}
|
|
3618
3775
|
_setRoot(v) {
|
|
3619
3776
|
if (v) {
|
|
@@ -3632,16 +3789,15 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
3632
3789
|
* @returns Status string: 'CREATED' or 'UPDATED'.
|
|
3633
3790
|
*/
|
|
3634
3791
|
_insert(node) {
|
|
3635
|
-
|
|
3636
|
-
let current = this.root;
|
|
3792
|
+
let current = this.root ?? this.NIL;
|
|
3637
3793
|
let parent = void 0;
|
|
3638
|
-
while (this.
|
|
3794
|
+
while (current !== this.NIL) {
|
|
3639
3795
|
parent = current;
|
|
3640
3796
|
const compared = this._compare(node.key, current.key);
|
|
3641
3797
|
if (compared < 0) {
|
|
3642
|
-
current =
|
|
3798
|
+
current = current.left ?? this.NIL;
|
|
3643
3799
|
} else if (compared > 0) {
|
|
3644
|
-
current =
|
|
3800
|
+
current = current.right ?? this.NIL;
|
|
3645
3801
|
} else {
|
|
3646
3802
|
this._replaceNode(current, node);
|
|
3647
3803
|
return "UPDATED";
|
|
@@ -3687,11 +3843,10 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
3687
3843
|
* @returns void
|
|
3688
3844
|
*/
|
|
3689
3845
|
_insertFixup(z) {
|
|
3690
|
-
|
|
3691
|
-
|
|
3692
|
-
if (z.parent === ((_b = z.parent.parent) == null ? void 0 : _b.left)) {
|
|
3846
|
+
while (z?.parent?.color === "RED") {
|
|
3847
|
+
if (z.parent === z.parent.parent?.left) {
|
|
3693
3848
|
const y = z.parent.parent.right;
|
|
3694
|
-
if (
|
|
3849
|
+
if (y?.color === "RED") {
|
|
3695
3850
|
z.parent.color = "BLACK";
|
|
3696
3851
|
y.color = "BLACK";
|
|
3697
3852
|
z.parent.parent.color = "RED";
|
|
@@ -3701,15 +3856,15 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
3701
3856
|
z = z.parent;
|
|
3702
3857
|
this._leftRotate(z);
|
|
3703
3858
|
}
|
|
3704
|
-
if (z &&
|
|
3859
|
+
if (z && z.parent && z.parent.parent) {
|
|
3705
3860
|
z.parent.color = "BLACK";
|
|
3706
3861
|
z.parent.parent.color = "RED";
|
|
3707
3862
|
this._rightRotate(z.parent.parent);
|
|
3708
3863
|
}
|
|
3709
3864
|
}
|
|
3710
3865
|
} else {
|
|
3711
|
-
const y =
|
|
3712
|
-
if (
|
|
3866
|
+
const y = z?.parent?.parent?.left ?? void 0;
|
|
3867
|
+
if (y?.color === "RED") {
|
|
3713
3868
|
z.parent.color = "BLACK";
|
|
3714
3869
|
y.color = "BLACK";
|
|
3715
3870
|
z.parent.parent.color = "RED";
|
|
@@ -3719,7 +3874,7 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
3719
3874
|
z = z.parent;
|
|
3720
3875
|
this._rightRotate(z);
|
|
3721
3876
|
}
|
|
3722
|
-
if (z &&
|
|
3877
|
+
if (z && z.parent && z.parent.parent) {
|
|
3723
3878
|
z.parent.color = "BLACK";
|
|
3724
3879
|
z.parent.parent.color = "RED";
|
|
3725
3880
|
this._leftRotate(z.parent.parent);
|
|
@@ -3736,7 +3891,6 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
3736
3891
|
* @returns void
|
|
3737
3892
|
*/
|
|
3738
3893
|
_deleteFixup(node) {
|
|
3739
|
-
var _a, _b, _c, _d;
|
|
3740
3894
|
if (!node || node === this.root || node.color === "BLACK") {
|
|
3741
3895
|
if (node) {
|
|
3742
3896
|
node.color = "BLACK";
|
|
@@ -3750,17 +3904,17 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
3750
3904
|
}
|
|
3751
3905
|
if (node === parent.left) {
|
|
3752
3906
|
let sibling = parent.right;
|
|
3753
|
-
if (
|
|
3907
|
+
if (sibling?.color === "RED") {
|
|
3754
3908
|
sibling.color = "BLACK";
|
|
3755
3909
|
parent.color = "RED";
|
|
3756
3910
|
this._leftRotate(parent);
|
|
3757
3911
|
sibling = parent.right;
|
|
3758
3912
|
}
|
|
3759
|
-
if ((
|
|
3913
|
+
if ((sibling?.left?.color ?? "BLACK") === "BLACK") {
|
|
3760
3914
|
if (sibling) sibling.color = "RED";
|
|
3761
3915
|
node = parent;
|
|
3762
3916
|
} else {
|
|
3763
|
-
if (sibling
|
|
3917
|
+
if (sibling?.left) sibling.left.color = "BLACK";
|
|
3764
3918
|
if (sibling) sibling.color = parent.color;
|
|
3765
3919
|
parent.color = "BLACK";
|
|
3766
3920
|
this._rightRotate(parent);
|
|
@@ -3768,17 +3922,17 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
3768
3922
|
}
|
|
3769
3923
|
} else {
|
|
3770
3924
|
let sibling = parent.left;
|
|
3771
|
-
if (
|
|
3925
|
+
if (sibling?.color === "RED") {
|
|
3772
3926
|
sibling.color = "BLACK";
|
|
3773
3927
|
if (parent) parent.color = "RED";
|
|
3774
3928
|
this._rightRotate(parent);
|
|
3775
3929
|
if (parent) sibling = parent.left;
|
|
3776
3930
|
}
|
|
3777
|
-
if ((
|
|
3931
|
+
if ((sibling?.right?.color ?? "BLACK") === "BLACK") {
|
|
3778
3932
|
if (sibling) sibling.color = "RED";
|
|
3779
3933
|
node = parent;
|
|
3780
3934
|
} else {
|
|
3781
|
-
if (sibling
|
|
3935
|
+
if (sibling?.right) sibling.right.color = "BLACK";
|
|
3782
3936
|
if (sibling) sibling.color = parent.color;
|
|
3783
3937
|
if (parent) parent.color = "BLACK";
|
|
3784
3938
|
this._leftRotate(parent);
|
|
@@ -3802,7 +3956,7 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
3802
3956
|
}
|
|
3803
3957
|
const y = x.right;
|
|
3804
3958
|
x.right = y.left;
|
|
3805
|
-
if (
|
|
3959
|
+
if (y.left && y.left !== this.NIL) {
|
|
3806
3960
|
y.left.parent = x;
|
|
3807
3961
|
}
|
|
3808
3962
|
y.parent = x.parent;
|
|
@@ -3828,7 +3982,7 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
3828
3982
|
}
|
|
3829
3983
|
const x = y.left;
|
|
3830
3984
|
y.left = x.right;
|
|
3831
|
-
if (
|
|
3985
|
+
if (x.right && x.right !== this.NIL) {
|
|
3832
3986
|
x.right.parent = y;
|
|
3833
3987
|
}
|
|
3834
3988
|
x.parent = y.parent;
|
|
@@ -3843,8 +3997,6 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
3843
3997
|
y.parent = x;
|
|
3844
3998
|
}
|
|
3845
3999
|
};
|
|
3846
|
-
__name(_RedBlackTree, "RedBlackTree");
|
|
3847
|
-
var RedBlackTree = _RedBlackTree;
|
|
3848
4000
|
/**
|
|
3849
4001
|
* data-structure-typed
|
|
3850
4002
|
*
|