avl-tree-typed 2.1.2 → 2.2.0
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 +301 -146
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +4013 -0
- package/dist/cjs-legacy/index.cjs.map +1 -0
- package/dist/esm/index.mjs +301 -146
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +4006 -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 +58 -4
- package/dist/types/data-structures/binary-tree/bst.d.ts +57 -3
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +58 -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 +65 -3
- package/dist/umd/avl-tree-typed.js +158 -4
- package/dist/umd/avl-tree-typed.js.map +1 -1
- package/dist/umd/avl-tree-typed.min.js +3 -3
- package/dist/umd/avl-tree-typed.min.js.map +1 -1
- package/package.json +20 -2
- package/src/data-structures/binary-tree/avl-tree-counter.ts +102 -11
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +115 -11
- package/src/data-structures/binary-tree/avl-tree.ts +105 -14
- package/src/data-structures/binary-tree/bst.ts +102 -11
- package/src/data-structures/binary-tree/red-black-tree.ts +108 -18
- package/src/data-structures/binary-tree/tree-counter.ts +101 -10
- package/src/data-structures/binary-tree/tree-multi-map.ts +122 -11
- package/src/data-structures/graph/abstract-graph.ts +5 -5
- package/src/data-structures/graph/directed-graph.ts +5 -5
- package/src/data-structures/graph/undirected-graph.ts +5 -5
- 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]`.
|
|
@@ -1012,11 +1014,9 @@ 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
|
-
var
|
|
1019
|
+
var Range = class {
|
|
1020
1020
|
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
1021
1021
|
this.low = low;
|
|
1022
1022
|
this.high = high;
|
|
@@ -1025,6 +1025,9 @@ var _Range = class _Range {
|
|
|
1025
1025
|
if (!(isComparable(low) && isComparable(high))) throw new RangeError("low or high is not comparable");
|
|
1026
1026
|
if (low > high) throw new RangeError("low must be less than or equal to high");
|
|
1027
1027
|
}
|
|
1028
|
+
static {
|
|
1029
|
+
__name(this, "Range");
|
|
1030
|
+
}
|
|
1028
1031
|
// Determine whether a key is within the range
|
|
1029
1032
|
isInRange(key, comparator) {
|
|
1030
1033
|
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
@@ -1032,11 +1035,15 @@ var _Range = class _Range {
|
|
|
1032
1035
|
return lowCheck && highCheck;
|
|
1033
1036
|
}
|
|
1034
1037
|
};
|
|
1035
|
-
__name(_Range, "Range");
|
|
1036
|
-
var Range = _Range;
|
|
1037
1038
|
|
|
1038
1039
|
// src/data-structures/binary-tree/binary-tree.ts
|
|
1039
|
-
var
|
|
1040
|
+
var BinaryTreeNode = class {
|
|
1041
|
+
static {
|
|
1042
|
+
__name(this, "BinaryTreeNode");
|
|
1043
|
+
}
|
|
1044
|
+
key;
|
|
1045
|
+
value;
|
|
1046
|
+
parent = void 0;
|
|
1040
1047
|
/**
|
|
1041
1048
|
* Creates an instance of BinaryTreeNode.
|
|
1042
1049
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1045,17 +1052,10 @@ var _BinaryTreeNode = class _BinaryTreeNode {
|
|
|
1045
1052
|
* @param [value] - The value associated with the key.
|
|
1046
1053
|
*/
|
|
1047
1054
|
constructor(key, value) {
|
|
1048
|
-
__publicField(this, "key");
|
|
1049
|
-
__publicField(this, "value");
|
|
1050
|
-
__publicField(this, "parent");
|
|
1051
|
-
__publicField(this, "_left");
|
|
1052
|
-
__publicField(this, "_right");
|
|
1053
|
-
__publicField(this, "_height", 0);
|
|
1054
|
-
__publicField(this, "_color", "BLACK");
|
|
1055
|
-
__publicField(this, "_count", 1);
|
|
1056
1055
|
this.key = key;
|
|
1057
1056
|
this.value = value;
|
|
1058
1057
|
}
|
|
1058
|
+
_left = void 0;
|
|
1059
1059
|
/**
|
|
1060
1060
|
* Gets the left child of the node.
|
|
1061
1061
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1077,6 +1077,7 @@ var _BinaryTreeNode = class _BinaryTreeNode {
|
|
|
1077
1077
|
}
|
|
1078
1078
|
this._left = v;
|
|
1079
1079
|
}
|
|
1080
|
+
_right = void 0;
|
|
1080
1081
|
/**
|
|
1081
1082
|
* Gets the right child of the node.
|
|
1082
1083
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1098,6 +1099,7 @@ var _BinaryTreeNode = class _BinaryTreeNode {
|
|
|
1098
1099
|
}
|
|
1099
1100
|
this._right = v;
|
|
1100
1101
|
}
|
|
1102
|
+
_height = 0;
|
|
1101
1103
|
/**
|
|
1102
1104
|
* Gets the height of the node (used in self-balancing trees).
|
|
1103
1105
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1116,6 +1118,7 @@ var _BinaryTreeNode = class _BinaryTreeNode {
|
|
|
1116
1118
|
set height(value) {
|
|
1117
1119
|
this._height = value;
|
|
1118
1120
|
}
|
|
1121
|
+
_color = "BLACK";
|
|
1119
1122
|
/**
|
|
1120
1123
|
* Gets the color of the node (used in Red-Black trees).
|
|
1121
1124
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1134,6 +1137,7 @@ var _BinaryTreeNode = class _BinaryTreeNode {
|
|
|
1134
1137
|
set color(value) {
|
|
1135
1138
|
this._color = value;
|
|
1136
1139
|
}
|
|
1140
|
+
_count = 1;
|
|
1137
1141
|
/**
|
|
1138
1142
|
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
1139
1143
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1170,9 +1174,11 @@ var _BinaryTreeNode = class _BinaryTreeNode {
|
|
|
1170
1174
|
return "MAL_NODE";
|
|
1171
1175
|
}
|
|
1172
1176
|
};
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1177
|
+
var BinaryTree = class extends IterableEntryBase {
|
|
1178
|
+
static {
|
|
1179
|
+
__name(this, "BinaryTree");
|
|
1180
|
+
}
|
|
1181
|
+
iterationType = "ITERATIVE";
|
|
1176
1182
|
/**
|
|
1177
1183
|
* Creates an instance of BinaryTree.
|
|
1178
1184
|
* @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.
|
|
@@ -1182,22 +1188,6 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1182
1188
|
*/
|
|
1183
1189
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
1184
1190
|
super();
|
|
1185
|
-
__publicField(this, "iterationType", "ITERATIVE");
|
|
1186
|
-
__publicField(this, "_isMapMode", true);
|
|
1187
|
-
__publicField(this, "_isDuplicate", false);
|
|
1188
|
-
__publicField(this, "_store", /* @__PURE__ */ new Map());
|
|
1189
|
-
__publicField(this, "_root");
|
|
1190
|
-
__publicField(this, "_size", 0);
|
|
1191
|
-
__publicField(this, "_NIL", new BinaryTreeNode(NaN));
|
|
1192
|
-
__publicField(this, "_toEntryFn");
|
|
1193
|
-
/**
|
|
1194
|
-
* (Protected) Default callback function, returns the node's key.
|
|
1195
|
-
* @remarks Time O(1)
|
|
1196
|
-
*
|
|
1197
|
-
* @param node - The node.
|
|
1198
|
-
* @returns The node's key or undefined.
|
|
1199
|
-
*/
|
|
1200
|
-
__publicField(this, "_DEFAULT_NODE_CALLBACK", /* @__PURE__ */ __name((node) => node ? node.key : void 0, "_DEFAULT_NODE_CALLBACK"));
|
|
1201
1191
|
if (options) {
|
|
1202
1192
|
const { iterationType, toEntryFn, isMapMode, isDuplicate } = options;
|
|
1203
1193
|
if (iterationType) this.iterationType = iterationType;
|
|
@@ -1208,6 +1198,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1208
1198
|
}
|
|
1209
1199
|
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
1210
1200
|
}
|
|
1201
|
+
_isMapMode = true;
|
|
1211
1202
|
/**
|
|
1212
1203
|
* Gets whether the tree is in Map mode.
|
|
1213
1204
|
* @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)
|
|
@@ -1217,6 +1208,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1217
1208
|
get isMapMode() {
|
|
1218
1209
|
return this._isMapMode;
|
|
1219
1210
|
}
|
|
1211
|
+
_isDuplicate = false;
|
|
1220
1212
|
/**
|
|
1221
1213
|
* Gets whether the tree allows duplicate keys.
|
|
1222
1214
|
* @remarks Time O(1)
|
|
@@ -1226,6 +1218,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1226
1218
|
get isDuplicate() {
|
|
1227
1219
|
return this._isDuplicate;
|
|
1228
1220
|
}
|
|
1221
|
+
_store = /* @__PURE__ */ new Map();
|
|
1229
1222
|
/**
|
|
1230
1223
|
* Gets the external value store (used in Map mode).
|
|
1231
1224
|
* @remarks Time O(1)
|
|
@@ -1235,6 +1228,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1235
1228
|
get store() {
|
|
1236
1229
|
return this._store;
|
|
1237
1230
|
}
|
|
1231
|
+
_root;
|
|
1238
1232
|
/**
|
|
1239
1233
|
* Gets the root node of the tree.
|
|
1240
1234
|
* @remarks Time O(1)
|
|
@@ -1244,6 +1238,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1244
1238
|
get root() {
|
|
1245
1239
|
return this._root;
|
|
1246
1240
|
}
|
|
1241
|
+
_size = 0;
|
|
1247
1242
|
/**
|
|
1248
1243
|
* Gets the number of nodes in the tree.
|
|
1249
1244
|
* @remarks Time O(1)
|
|
@@ -1253,6 +1248,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1253
1248
|
get size() {
|
|
1254
1249
|
return this._size;
|
|
1255
1250
|
}
|
|
1251
|
+
_NIL = new BinaryTreeNode(NaN);
|
|
1256
1252
|
/**
|
|
1257
1253
|
* Gets the sentinel NIL node (used in self-balancing trees like Red-Black Tree).
|
|
1258
1254
|
* @remarks Time O(1)
|
|
@@ -1262,6 +1258,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1262
1258
|
get NIL() {
|
|
1263
1259
|
return this._NIL;
|
|
1264
1260
|
}
|
|
1261
|
+
_toEntryFn;
|
|
1265
1262
|
/**
|
|
1266
1263
|
* Gets the function used to convert raw data objects (R) into [key, value] entries.
|
|
1267
1264
|
* @remarks Time O(1)
|
|
@@ -1421,7 +1418,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1421
1418
|
if (newNode === void 0) return false;
|
|
1422
1419
|
if (!this._root) {
|
|
1423
1420
|
this._setRoot(newNode);
|
|
1424
|
-
if (this._isMapMode) this._setValue(newNode
|
|
1421
|
+
if (this._isMapMode) this._setValue(newNode?.key, newValue);
|
|
1425
1422
|
this._size = 1;
|
|
1426
1423
|
return true;
|
|
1427
1424
|
}
|
|
@@ -1453,7 +1450,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1453
1450
|
} else if (potentialParent.right === void 0) {
|
|
1454
1451
|
potentialParent.right = newNode;
|
|
1455
1452
|
}
|
|
1456
|
-
if (this._isMapMode) this._setValue(newNode
|
|
1453
|
+
if (this._isMapMode) this._setValue(newNode?.key, newValue);
|
|
1457
1454
|
this._size++;
|
|
1458
1455
|
return true;
|
|
1459
1456
|
}
|
|
@@ -1518,7 +1515,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1518
1515
|
if (!this._root) return deletedResult;
|
|
1519
1516
|
const curr = this.getNode(keyNodeOrEntry);
|
|
1520
1517
|
if (!curr) return deletedResult;
|
|
1521
|
-
const parent = curr
|
|
1518
|
+
const parent = curr?.parent;
|
|
1522
1519
|
let needBalanced;
|
|
1523
1520
|
let orgCurrent = curr;
|
|
1524
1521
|
if (!curr.left && !curr.right && !parent) {
|
|
@@ -1623,13 +1620,12 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1623
1620
|
* @returns The associated value, or undefined.
|
|
1624
1621
|
*/
|
|
1625
1622
|
get(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
1626
|
-
var _a;
|
|
1627
1623
|
if (this._isMapMode) {
|
|
1628
1624
|
const key = this._extractKey(keyNodeEntryOrPredicate);
|
|
1629
1625
|
if (key === null || key === void 0) return;
|
|
1630
1626
|
return this._store.get(key);
|
|
1631
1627
|
}
|
|
1632
|
-
return
|
|
1628
|
+
return this.getNode(keyNodeEntryOrPredicate, startNode, iterationType)?.value;
|
|
1633
1629
|
}
|
|
1634
1630
|
has(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
1635
1631
|
return this.search(keyNodeEntryOrPredicate, true, (node) => node, startNode, iterationType).length > 0;
|
|
@@ -1717,7 +1713,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1717
1713
|
let distEnsured = this.ensureNode(dist);
|
|
1718
1714
|
const beginRootEnsured = this.ensureNode(startNode);
|
|
1719
1715
|
let depth = 0;
|
|
1720
|
-
while (distEnsured
|
|
1716
|
+
while (distEnsured?.parent) {
|
|
1721
1717
|
if (distEnsured === beginRootEnsured) {
|
|
1722
1718
|
return depth;
|
|
1723
1719
|
}
|
|
@@ -2279,10 +2275,10 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2279
2275
|
const dfs = /* @__PURE__ */ __name((node) => {
|
|
2280
2276
|
if (!shouldVisitRoot(node)) return;
|
|
2281
2277
|
const visitLeft = /* @__PURE__ */ __name(() => {
|
|
2282
|
-
if (shouldVisitLeft(node) &&
|
|
2278
|
+
if (shouldVisitLeft(node) && node?.left !== void 0) dfs(node?.left);
|
|
2283
2279
|
}, "visitLeft");
|
|
2284
2280
|
const visitRight = /* @__PURE__ */ __name(() => {
|
|
2285
|
-
if (shouldVisitRight(node) &&
|
|
2281
|
+
if (shouldVisitRight(node) && node?.right !== void 0) dfs(node?.right);
|
|
2286
2282
|
}, "visitRight");
|
|
2287
2283
|
switch (pattern) {
|
|
2288
2284
|
case "IN":
|
|
@@ -2315,12 +2311,10 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2315
2311
|
} else {
|
|
2316
2312
|
const stack = [{ opt: 0 /* VISIT */, node: startNode }];
|
|
2317
2313
|
const pushLeft = /* @__PURE__ */ __name((cur) => {
|
|
2318
|
-
|
|
2319
|
-
if (shouldVisitLeft(cur.node)) stack.push({ opt: 0 /* VISIT */, node: (_a = cur.node) == null ? void 0 : _a.left });
|
|
2314
|
+
if (shouldVisitLeft(cur.node)) stack.push({ opt: 0 /* VISIT */, node: cur.node?.left });
|
|
2320
2315
|
}, "pushLeft");
|
|
2321
2316
|
const pushRight = /* @__PURE__ */ __name((cur) => {
|
|
2322
|
-
|
|
2323
|
-
if (shouldVisitRight(cur.node)) stack.push({ opt: 0 /* VISIT */, node: (_a = cur.node) == null ? void 0 : _a.right });
|
|
2317
|
+
if (shouldVisitRight(cur.node)) stack.push({ opt: 0 /* VISIT */, node: cur.node?.right });
|
|
2324
2318
|
}, "pushRight");
|
|
2325
2319
|
const pushRoot = /* @__PURE__ */ __name((cur) => {
|
|
2326
2320
|
if (shouldVisitRoot(cur.node)) stack.push({ opt: 1 /* PROCESS */, node: cur.node });
|
|
@@ -2392,6 +2386,14 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2392
2386
|
}
|
|
2393
2387
|
}
|
|
2394
2388
|
}
|
|
2389
|
+
/**
|
|
2390
|
+
* (Protected) Default callback function, returns the node's key.
|
|
2391
|
+
* @remarks Time O(1)
|
|
2392
|
+
*
|
|
2393
|
+
* @param node - The node.
|
|
2394
|
+
* @returns The node's key or undefined.
|
|
2395
|
+
*/
|
|
2396
|
+
_DEFAULT_NODE_CALLBACK = /* @__PURE__ */ __name((node) => node ? node.key : void 0, "_DEFAULT_NODE_CALLBACK");
|
|
2395
2397
|
/**
|
|
2396
2398
|
* (Protected) Snapshots the current tree's configuration options.
|
|
2397
2399
|
* @remarks Time O(1)
|
|
@@ -2417,7 +2419,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2417
2419
|
*/
|
|
2418
2420
|
_createInstance(options) {
|
|
2419
2421
|
const Ctor = this.constructor;
|
|
2420
|
-
return new Ctor([], { ...this._snapshotOptions(), ...options
|
|
2422
|
+
return new Ctor([], { ...this._snapshotOptions(), ...options ?? {} });
|
|
2421
2423
|
}
|
|
2422
2424
|
/**
|
|
2423
2425
|
* (Protected) Creates a new instance of the same tree constructor, potentially with different generic types.
|
|
@@ -2430,7 +2432,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2430
2432
|
*/
|
|
2431
2433
|
_createLike(iter = [], options) {
|
|
2432
2434
|
const Ctor = this.constructor;
|
|
2433
|
-
return new Ctor(iter, { ...this._snapshotOptions(), ...options
|
|
2435
|
+
return new Ctor(iter, { ...this._snapshotOptions(), ...options ?? {} });
|
|
2434
2436
|
}
|
|
2435
2437
|
/**
|
|
2436
2438
|
* (Protected) Converts a key, node, or entry into a standardized [node, value] tuple.
|
|
@@ -2448,7 +2450,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2448
2450
|
const [key, entryValue] = keyNodeOrEntry;
|
|
2449
2451
|
if (key === void 0) return [void 0, void 0];
|
|
2450
2452
|
else if (key === null) return [null, void 0];
|
|
2451
|
-
const finalValue = value
|
|
2453
|
+
const finalValue = value ?? entryValue;
|
|
2452
2454
|
return [this.createNode(key, finalValue), finalValue];
|
|
2453
2455
|
}
|
|
2454
2456
|
return [this.createNode(keyNodeOrEntry, value), value];
|
|
@@ -2655,11 +2657,15 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2655
2657
|
this._store.clear();
|
|
2656
2658
|
}
|
|
2657
2659
|
};
|
|
2658
|
-
__name(_BinaryTree, "BinaryTree");
|
|
2659
|
-
var BinaryTree = _BinaryTree;
|
|
2660
2660
|
|
|
2661
2661
|
// src/data-structures/binary-tree/bst.ts
|
|
2662
|
-
var
|
|
2662
|
+
var BSTNode = class {
|
|
2663
|
+
static {
|
|
2664
|
+
__name(this, "BSTNode");
|
|
2665
|
+
}
|
|
2666
|
+
key;
|
|
2667
|
+
value;
|
|
2668
|
+
parent = void 0;
|
|
2663
2669
|
/**
|
|
2664
2670
|
* Creates an instance of BSTNode.
|
|
2665
2671
|
* @remarks Time O(1), Space O(1)
|
|
@@ -2668,11 +2674,10 @@ var _BSTNode = class _BSTNode extends BinaryTreeNode {
|
|
|
2668
2674
|
* @param [value] - The value associated with the key.
|
|
2669
2675
|
*/
|
|
2670
2676
|
constructor(key, value) {
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
__publicField(this, "_left");
|
|
2674
|
-
__publicField(this, "_right");
|
|
2677
|
+
this.key = key;
|
|
2678
|
+
this.value = value;
|
|
2675
2679
|
}
|
|
2680
|
+
_left = void 0;
|
|
2676
2681
|
/**
|
|
2677
2682
|
* Gets the left child of the node.
|
|
2678
2683
|
* @remarks Time O(1), Space O(1)
|
|
@@ -2692,6 +2697,7 @@ var _BSTNode = class _BSTNode extends BinaryTreeNode {
|
|
|
2692
2697
|
if (v) v.parent = this;
|
|
2693
2698
|
this._left = v;
|
|
2694
2699
|
}
|
|
2700
|
+
_right = void 0;
|
|
2695
2701
|
/**
|
|
2696
2702
|
* Gets the right child of the node.
|
|
2697
2703
|
* @remarks Time O(1), Space O(1)
|
|
@@ -2711,10 +2717,85 @@ var _BSTNode = class _BSTNode extends BinaryTreeNode {
|
|
|
2711
2717
|
if (v) v.parent = this;
|
|
2712
2718
|
this._right = v;
|
|
2713
2719
|
}
|
|
2720
|
+
_height = 0;
|
|
2721
|
+
/**
|
|
2722
|
+
* Gets the height of the node (used in self-balancing trees).
|
|
2723
|
+
* @remarks Time O(1), Space O(1)
|
|
2724
|
+
*
|
|
2725
|
+
* @returns The height.
|
|
2726
|
+
*/
|
|
2727
|
+
get height() {
|
|
2728
|
+
return this._height;
|
|
2729
|
+
}
|
|
2730
|
+
/**
|
|
2731
|
+
* Sets the height of the node.
|
|
2732
|
+
* @remarks Time O(1), Space O(1)
|
|
2733
|
+
*
|
|
2734
|
+
* @param value - The new height.
|
|
2735
|
+
*/
|
|
2736
|
+
set height(value) {
|
|
2737
|
+
this._height = value;
|
|
2738
|
+
}
|
|
2739
|
+
_color = "BLACK";
|
|
2740
|
+
/**
|
|
2741
|
+
* Gets the color of the node (used in Red-Black trees).
|
|
2742
|
+
* @remarks Time O(1), Space O(1)
|
|
2743
|
+
*
|
|
2744
|
+
* @returns The node's color.
|
|
2745
|
+
*/
|
|
2746
|
+
get color() {
|
|
2747
|
+
return this._color;
|
|
2748
|
+
}
|
|
2749
|
+
/**
|
|
2750
|
+
* Sets the color of the node.
|
|
2751
|
+
* @remarks Time O(1), Space O(1)
|
|
2752
|
+
*
|
|
2753
|
+
* @param value - The new color.
|
|
2754
|
+
*/
|
|
2755
|
+
set color(value) {
|
|
2756
|
+
this._color = value;
|
|
2757
|
+
}
|
|
2758
|
+
_count = 1;
|
|
2759
|
+
/**
|
|
2760
|
+
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
2761
|
+
* @remarks Time O(1), Space O(1)
|
|
2762
|
+
*
|
|
2763
|
+
* @returns The subtree node count.
|
|
2764
|
+
*/
|
|
2765
|
+
get count() {
|
|
2766
|
+
return this._count;
|
|
2767
|
+
}
|
|
2768
|
+
/**
|
|
2769
|
+
* Sets the count of nodes in the subtree.
|
|
2770
|
+
* @remarks Time O(1), Space O(1)
|
|
2771
|
+
*
|
|
2772
|
+
* @param value - The new count.
|
|
2773
|
+
*/
|
|
2774
|
+
set count(value) {
|
|
2775
|
+
this._count = value;
|
|
2776
|
+
}
|
|
2777
|
+
/**
|
|
2778
|
+
* Gets the position of the node relative to its parent.
|
|
2779
|
+
* @remarks Time O(1), Space O(1)
|
|
2780
|
+
*
|
|
2781
|
+
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
2782
|
+
*/
|
|
2783
|
+
get familyPosition() {
|
|
2784
|
+
if (!this.parent) {
|
|
2785
|
+
return this.left || this.right ? "ROOT" : "ISOLATED";
|
|
2786
|
+
}
|
|
2787
|
+
if (this.parent.left === this) {
|
|
2788
|
+
return this.left || this.right ? "ROOT_LEFT" : "LEFT";
|
|
2789
|
+
} else if (this.parent.right === this) {
|
|
2790
|
+
return this.left || this.right ? "ROOT_RIGHT" : "RIGHT";
|
|
2791
|
+
}
|
|
2792
|
+
return "MAL_NODE";
|
|
2793
|
+
}
|
|
2714
2794
|
};
|
|
2715
|
-
|
|
2716
|
-
|
|
2717
|
-
|
|
2795
|
+
var BST = class extends BinaryTree {
|
|
2796
|
+
static {
|
|
2797
|
+
__name(this, "BST");
|
|
2798
|
+
}
|
|
2718
2799
|
/**
|
|
2719
2800
|
* Creates an instance of BST.
|
|
2720
2801
|
* @remarks Time O(N log N) or O(N^2) depending on `isBalanceAdd` in `addMany` and input order. Space O(N).
|
|
@@ -2724,33 +2805,6 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2724
2805
|
*/
|
|
2725
2806
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
2726
2807
|
super([], options);
|
|
2727
|
-
__publicField(this, "_root");
|
|
2728
|
-
__publicField(this, "_isReverse", false);
|
|
2729
|
-
/**
|
|
2730
|
-
* The default comparator function.
|
|
2731
|
-
* @remarks Time O(1) (or O(C) if `specifyComparable` is used, C is complexity of that function).
|
|
2732
|
-
*/
|
|
2733
|
-
__publicField(this, "_comparator", /* @__PURE__ */ __name((a, b) => {
|
|
2734
|
-
if (isComparable(a) && isComparable(b)) {
|
|
2735
|
-
if (a > b) return 1;
|
|
2736
|
-
if (a < b) return -1;
|
|
2737
|
-
return 0;
|
|
2738
|
-
}
|
|
2739
|
-
if (this._specifyComparable) {
|
|
2740
|
-
const va = this._specifyComparable(a);
|
|
2741
|
-
const vb = this._specifyComparable(b);
|
|
2742
|
-
if (va > vb) return 1;
|
|
2743
|
-
if (va < vb) return -1;
|
|
2744
|
-
return 0;
|
|
2745
|
-
}
|
|
2746
|
-
if (typeof a === "object" || typeof b === "object") {
|
|
2747
|
-
throw TypeError(
|
|
2748
|
-
`When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
|
|
2749
|
-
);
|
|
2750
|
-
}
|
|
2751
|
-
return 0;
|
|
2752
|
-
}, "_comparator"));
|
|
2753
|
-
__publicField(this, "_specifyComparable");
|
|
2754
2808
|
if (options) {
|
|
2755
2809
|
const { specifyComparable, isReverse } = options;
|
|
2756
2810
|
if (typeof specifyComparable === "function") this._specifyComparable = specifyComparable;
|
|
@@ -2758,6 +2812,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2758
2812
|
}
|
|
2759
2813
|
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
2760
2814
|
}
|
|
2815
|
+
_root = void 0;
|
|
2761
2816
|
/**
|
|
2762
2817
|
* Gets the root node of the tree.
|
|
2763
2818
|
* @remarks Time O(1)
|
|
@@ -2767,6 +2822,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2767
2822
|
get root() {
|
|
2768
2823
|
return this._root;
|
|
2769
2824
|
}
|
|
2825
|
+
_isReverse = false;
|
|
2770
2826
|
/**
|
|
2771
2827
|
* Gets whether the tree's comparison logic is reversed.
|
|
2772
2828
|
* @remarks Time O(1)
|
|
@@ -2776,6 +2832,30 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2776
2832
|
get isReverse() {
|
|
2777
2833
|
return this._isReverse;
|
|
2778
2834
|
}
|
|
2835
|
+
/**
|
|
2836
|
+
* The default comparator function.
|
|
2837
|
+
* @remarks Time O(1) (or O(C) if `specifyComparable` is used, C is complexity of that function).
|
|
2838
|
+
*/
|
|
2839
|
+
_comparator = /* @__PURE__ */ __name((a, b) => {
|
|
2840
|
+
if (isComparable(a) && isComparable(b)) {
|
|
2841
|
+
if (a > b) return 1;
|
|
2842
|
+
if (a < b) return -1;
|
|
2843
|
+
return 0;
|
|
2844
|
+
}
|
|
2845
|
+
if (this._specifyComparable) {
|
|
2846
|
+
const va = this._specifyComparable(a);
|
|
2847
|
+
const vb = this._specifyComparable(b);
|
|
2848
|
+
if (va > vb) return 1;
|
|
2849
|
+
if (va < vb) return -1;
|
|
2850
|
+
return 0;
|
|
2851
|
+
}
|
|
2852
|
+
if (typeof a === "object" || typeof b === "object") {
|
|
2853
|
+
throw TypeError(
|
|
2854
|
+
`When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
|
|
2855
|
+
);
|
|
2856
|
+
}
|
|
2857
|
+
return 0;
|
|
2858
|
+
}, "_comparator");
|
|
2779
2859
|
/**
|
|
2780
2860
|
* Gets the comparator function used by the tree.
|
|
2781
2861
|
* @remarks Time O(1)
|
|
@@ -2785,6 +2865,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2785
2865
|
get comparator() {
|
|
2786
2866
|
return this._comparator;
|
|
2787
2867
|
}
|
|
2868
|
+
_specifyComparable;
|
|
2788
2869
|
/**
|
|
2789
2870
|
* Gets the function used to extract a comparable value from a complex key.
|
|
2790
2871
|
* @remarks Time O(1)
|
|
@@ -2814,8 +2895,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2814
2895
|
* @returns The resolved node, or undefined if not found.
|
|
2815
2896
|
*/
|
|
2816
2897
|
ensureNode(keyNodeOrEntry, iterationType = this.iterationType) {
|
|
2817
|
-
|
|
2818
|
-
return (_a = super.ensureNode(keyNodeOrEntry, iterationType)) != null ? _a : void 0;
|
|
2898
|
+
return super.ensureNode(keyNodeOrEntry, iterationType) ?? void 0;
|
|
2819
2899
|
}
|
|
2820
2900
|
/**
|
|
2821
2901
|
* Checks if the given item is a `BSTNode` instance.
|
|
@@ -2888,8 +2968,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2888
2968
|
* @returns The first matching node, or undefined if not found.
|
|
2889
2969
|
*/
|
|
2890
2970
|
getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
2891
|
-
|
|
2892
|
-
return (_a = this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0]) != null ? _a : void 0;
|
|
2971
|
+
return this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0] ?? void 0;
|
|
2893
2972
|
}
|
|
2894
2973
|
/**
|
|
2895
2974
|
* Searches the tree for nodes matching a predicate, key, or range.
|
|
@@ -2994,7 +3073,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2994
3073
|
if (newNode === void 0) return false;
|
|
2995
3074
|
if (this._root === void 0) {
|
|
2996
3075
|
this._setRoot(newNode);
|
|
2997
|
-
if (this._isMapMode) this._setValue(newNode
|
|
3076
|
+
if (this._isMapMode) this._setValue(newNode?.key, newValue);
|
|
2998
3077
|
this._size++;
|
|
2999
3078
|
return true;
|
|
3000
3079
|
}
|
|
@@ -3007,7 +3086,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3007
3086
|
} else if (this._compare(current.key, newNode.key) > 0) {
|
|
3008
3087
|
if (current.left === void 0) {
|
|
3009
3088
|
current.left = newNode;
|
|
3010
|
-
if (this._isMapMode) this._setValue(newNode
|
|
3089
|
+
if (this._isMapMode) this._setValue(newNode?.key, newValue);
|
|
3011
3090
|
this._size++;
|
|
3012
3091
|
return true;
|
|
3013
3092
|
}
|
|
@@ -3015,7 +3094,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3015
3094
|
} else {
|
|
3016
3095
|
if (current.right === void 0) {
|
|
3017
3096
|
current.right = newNode;
|
|
3018
|
-
if (this._isMapMode) this._setValue(newNode
|
|
3097
|
+
if (this._isMapMode) this._setValue(newNode?.key, newValue);
|
|
3019
3098
|
this._size++;
|
|
3020
3099
|
return true;
|
|
3021
3100
|
}
|
|
@@ -3038,10 +3117,10 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3038
3117
|
*/
|
|
3039
3118
|
addMany(keysNodesEntriesOrRaws, values, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
3040
3119
|
const inserted = [];
|
|
3041
|
-
const valuesIterator = values
|
|
3120
|
+
const valuesIterator = values?.[Symbol.iterator]();
|
|
3042
3121
|
if (!isBalanceAdd) {
|
|
3043
3122
|
for (let kve of keysNodesEntriesOrRaws) {
|
|
3044
|
-
const val = valuesIterator
|
|
3123
|
+
const val = valuesIterator?.next().value;
|
|
3045
3124
|
if (this.isRaw(kve)) kve = this._toEntryFn(kve);
|
|
3046
3125
|
inserted.push(this.add(kve, val));
|
|
3047
3126
|
}
|
|
@@ -3050,7 +3129,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3050
3129
|
const realBTNExemplars = [];
|
|
3051
3130
|
let i = 0;
|
|
3052
3131
|
for (const kve of keysNodesEntriesOrRaws) {
|
|
3053
|
-
realBTNExemplars.push({ key: kve, value: valuesIterator
|
|
3132
|
+
realBTNExemplars.push({ key: kve, value: valuesIterator?.next().value, orgIndex: i++ });
|
|
3054
3133
|
}
|
|
3055
3134
|
const sorted = realBTNExemplars.sort(({ key: a }, { key: b }) => {
|
|
3056
3135
|
let keyA, keyB;
|
|
@@ -3272,7 +3351,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3272
3351
|
*/
|
|
3273
3352
|
_createInstance(options) {
|
|
3274
3353
|
const Ctor = this.constructor;
|
|
3275
|
-
return new Ctor([], { ...this._snapshotOptions(), ...options
|
|
3354
|
+
return new Ctor([], { ...this._snapshotOptions(), ...options ?? {} });
|
|
3276
3355
|
}
|
|
3277
3356
|
/**
|
|
3278
3357
|
* (Protected) Creates a new instance of the same BST constructor, potentially with different generic types.
|
|
@@ -3285,7 +3364,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3285
3364
|
*/
|
|
3286
3365
|
_createLike(iter = [], options) {
|
|
3287
3366
|
const Ctor = this.constructor;
|
|
3288
|
-
return new Ctor(iter, { ...this._snapshotOptions(), ...options
|
|
3367
|
+
return new Ctor(iter, { ...this._snapshotOptions(), ...options ?? {} });
|
|
3289
3368
|
}
|
|
3290
3369
|
/**
|
|
3291
3370
|
* (Protected) Snapshots the current BST's configuration options.
|
|
@@ -3312,7 +3391,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3312
3391
|
_keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value) {
|
|
3313
3392
|
const [node, entryValue] = super._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
3314
3393
|
if (node === null) return [void 0, void 0];
|
|
3315
|
-
return [node, value
|
|
3394
|
+
return [node, value ?? entryValue];
|
|
3316
3395
|
}
|
|
3317
3396
|
/**
|
|
3318
3397
|
* (Protected) Sets the root node and clears its parent reference.
|
|
@@ -3343,7 +3422,6 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3343
3422
|
* @returns True if the node was found and deleted, false otherwise.
|
|
3344
3423
|
*/
|
|
3345
3424
|
_deleteByKey(key) {
|
|
3346
|
-
var _a;
|
|
3347
3425
|
let node = this._root;
|
|
3348
3426
|
while (node) {
|
|
3349
3427
|
const cmp = this._compare(node.key, key);
|
|
@@ -3352,7 +3430,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3352
3430
|
}
|
|
3353
3431
|
if (!node) return false;
|
|
3354
3432
|
const transplant = /* @__PURE__ */ __name((u, v) => {
|
|
3355
|
-
const p = u
|
|
3433
|
+
const p = u?.parent;
|
|
3356
3434
|
if (!p) {
|
|
3357
3435
|
this._setRoot(v);
|
|
3358
3436
|
} else if (p.left === u) {
|
|
@@ -3382,15 +3460,19 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3382
3460
|
succ.left = node.left;
|
|
3383
3461
|
if (succ.left) succ.left.parent = succ;
|
|
3384
3462
|
}
|
|
3385
|
-
this._size = Math.max(0, (
|
|
3463
|
+
this._size = Math.max(0, (this._size ?? 0) - 1);
|
|
3386
3464
|
return true;
|
|
3387
3465
|
}
|
|
3388
3466
|
};
|
|
3389
|
-
__name(_BST, "BST");
|
|
3390
|
-
var BST = _BST;
|
|
3391
3467
|
|
|
3392
3468
|
// src/data-structures/binary-tree/avl-tree.ts
|
|
3393
|
-
var
|
|
3469
|
+
var AVLTreeNode = class {
|
|
3470
|
+
static {
|
|
3471
|
+
__name(this, "AVLTreeNode");
|
|
3472
|
+
}
|
|
3473
|
+
key;
|
|
3474
|
+
value;
|
|
3475
|
+
parent = void 0;
|
|
3394
3476
|
/**
|
|
3395
3477
|
* Creates an instance of AVLTreeNode.
|
|
3396
3478
|
* @remarks Time O(1), Space O(1)
|
|
@@ -3399,11 +3481,10 @@ var _AVLTreeNode = class _AVLTreeNode extends BSTNode {
|
|
|
3399
3481
|
* @param [value] - The value associated with the key.
|
|
3400
3482
|
*/
|
|
3401
3483
|
constructor(key, value) {
|
|
3402
|
-
|
|
3403
|
-
|
|
3404
|
-
__publicField(this, "_left");
|
|
3405
|
-
__publicField(this, "_right");
|
|
3484
|
+
this.key = key;
|
|
3485
|
+
this.value = value;
|
|
3406
3486
|
}
|
|
3487
|
+
_left = void 0;
|
|
3407
3488
|
/**
|
|
3408
3489
|
* Gets the left child of the node.
|
|
3409
3490
|
* @remarks Time O(1), Space O(1)
|
|
@@ -3425,6 +3506,7 @@ var _AVLTreeNode = class _AVLTreeNode extends BSTNode {
|
|
|
3425
3506
|
}
|
|
3426
3507
|
this._left = v;
|
|
3427
3508
|
}
|
|
3509
|
+
_right = void 0;
|
|
3428
3510
|
/**
|
|
3429
3511
|
* Gets the right child of the node.
|
|
3430
3512
|
* @remarks Time O(1), Space O(1)
|
|
@@ -3446,10 +3528,85 @@ var _AVLTreeNode = class _AVLTreeNode extends BSTNode {
|
|
|
3446
3528
|
}
|
|
3447
3529
|
this._right = v;
|
|
3448
3530
|
}
|
|
3531
|
+
_height = 0;
|
|
3532
|
+
/**
|
|
3533
|
+
* Gets the height of the node (used in self-balancing trees).
|
|
3534
|
+
* @remarks Time O(1), Space O(1)
|
|
3535
|
+
*
|
|
3536
|
+
* @returns The height.
|
|
3537
|
+
*/
|
|
3538
|
+
get height() {
|
|
3539
|
+
return this._height;
|
|
3540
|
+
}
|
|
3541
|
+
/**
|
|
3542
|
+
* Sets the height of the node.
|
|
3543
|
+
* @remarks Time O(1), Space O(1)
|
|
3544
|
+
*
|
|
3545
|
+
* @param value - The new height.
|
|
3546
|
+
*/
|
|
3547
|
+
set height(value) {
|
|
3548
|
+
this._height = value;
|
|
3549
|
+
}
|
|
3550
|
+
_color = "BLACK";
|
|
3551
|
+
/**
|
|
3552
|
+
* Gets the color of the node (used in Red-Black trees).
|
|
3553
|
+
* @remarks Time O(1), Space O(1)
|
|
3554
|
+
*
|
|
3555
|
+
* @returns The node's color.
|
|
3556
|
+
*/
|
|
3557
|
+
get color() {
|
|
3558
|
+
return this._color;
|
|
3559
|
+
}
|
|
3560
|
+
/**
|
|
3561
|
+
* Sets the color of the node.
|
|
3562
|
+
* @remarks Time O(1), Space O(1)
|
|
3563
|
+
*
|
|
3564
|
+
* @param value - The new color.
|
|
3565
|
+
*/
|
|
3566
|
+
set color(value) {
|
|
3567
|
+
this._color = value;
|
|
3568
|
+
}
|
|
3569
|
+
_count = 1;
|
|
3570
|
+
/**
|
|
3571
|
+
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
3572
|
+
* @remarks Time O(1), Space O(1)
|
|
3573
|
+
*
|
|
3574
|
+
* @returns The subtree node count.
|
|
3575
|
+
*/
|
|
3576
|
+
get count() {
|
|
3577
|
+
return this._count;
|
|
3578
|
+
}
|
|
3579
|
+
/**
|
|
3580
|
+
* Sets the count of nodes in the subtree.
|
|
3581
|
+
* @remarks Time O(1), Space O(1)
|
|
3582
|
+
*
|
|
3583
|
+
* @param value - The new count.
|
|
3584
|
+
*/
|
|
3585
|
+
set count(value) {
|
|
3586
|
+
this._count = value;
|
|
3587
|
+
}
|
|
3588
|
+
/**
|
|
3589
|
+
* Gets the position of the node relative to its parent.
|
|
3590
|
+
* @remarks Time O(1), Space O(1)
|
|
3591
|
+
*
|
|
3592
|
+
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
3593
|
+
*/
|
|
3594
|
+
get familyPosition() {
|
|
3595
|
+
if (!this.parent) {
|
|
3596
|
+
return this.left || this.right ? "ROOT" : "ISOLATED";
|
|
3597
|
+
}
|
|
3598
|
+
if (this.parent.left === this) {
|
|
3599
|
+
return this.left || this.right ? "ROOT_LEFT" : "LEFT";
|
|
3600
|
+
} else if (this.parent.right === this) {
|
|
3601
|
+
return this.left || this.right ? "ROOT_RIGHT" : "RIGHT";
|
|
3602
|
+
}
|
|
3603
|
+
return "MAL_NODE";
|
|
3604
|
+
}
|
|
3449
3605
|
};
|
|
3450
|
-
|
|
3451
|
-
|
|
3452
|
-
|
|
3606
|
+
var AVLTree = class extends BST {
|
|
3607
|
+
static {
|
|
3608
|
+
__name(this, "AVLTree");
|
|
3609
|
+
}
|
|
3453
3610
|
/**
|
|
3454
3611
|
* Creates an instance of AVLTree.
|
|
3455
3612
|
* @remarks Time O(N log N) (from `addMany` with balanced add). Space O(N).
|
|
@@ -3572,7 +3729,7 @@ var _AVLTree = class _AVLTree extends BST {
|
|
|
3572
3729
|
*/
|
|
3573
3730
|
_createInstance(options) {
|
|
3574
3731
|
const Ctor = this.constructor;
|
|
3575
|
-
return new Ctor([], { ...this._snapshotOptions(), ...options
|
|
3732
|
+
return new Ctor([], { ...this._snapshotOptions(), ...options ?? {} });
|
|
3576
3733
|
}
|
|
3577
3734
|
/**
|
|
3578
3735
|
* (Protected) Creates a new instance of the same AVLTree constructor, potentially with different generic types.
|
|
@@ -3585,7 +3742,7 @@ var _AVLTree = class _AVLTree extends BST {
|
|
|
3585
3742
|
*/
|
|
3586
3743
|
_createLike(iter = [], options) {
|
|
3587
3744
|
const Ctor = this.constructor;
|
|
3588
|
-
return new Ctor(iter, { ...this._snapshotOptions(), ...options
|
|
3745
|
+
return new Ctor(iter, { ...this._snapshotOptions(), ...options ?? {} });
|
|
3589
3746
|
}
|
|
3590
3747
|
/**
|
|
3591
3748
|
* (Protected) Swaps properties of two nodes, including height.
|
|
@@ -3654,7 +3811,7 @@ var _AVLTree = class _AVLTree extends BST {
|
|
|
3654
3811
|
if (A === this.root) {
|
|
3655
3812
|
if (B) this._setRoot(B);
|
|
3656
3813
|
} else {
|
|
3657
|
-
if (
|
|
3814
|
+
if (parentOfA?.left === A) {
|
|
3658
3815
|
parentOfA.left = B;
|
|
3659
3816
|
} else {
|
|
3660
3817
|
if (parentOfA) parentOfA.right = B;
|
|
@@ -3837,8 +3994,6 @@ var _AVLTree = class _AVLTree extends BST {
|
|
|
3837
3994
|
return super._replaceNode(oldNode, newNode);
|
|
3838
3995
|
}
|
|
3839
3996
|
};
|
|
3840
|
-
__name(_AVLTree, "AVLTree");
|
|
3841
|
-
var AVLTree = _AVLTree;
|
|
3842
3997
|
/**
|
|
3843
3998
|
* data-structure-typed
|
|
3844
3999
|
*
|