avl-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 +309 -154
- 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 +309 -154
- 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 +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/avl-tree-typed.js +166 -12
- 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/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,11 +1016,9 @@ 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
|
-
var
|
|
1021
|
+
var Range = class {
|
|
1022
1022
|
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
1023
1023
|
this.low = low;
|
|
1024
1024
|
this.high = high;
|
|
@@ -1027,6 +1027,9 @@ var _Range = class _Range {
|
|
|
1027
1027
|
if (!(isComparable(low) && isComparable(high))) throw new RangeError("low or high is not comparable");
|
|
1028
1028
|
if (low > high) throw new RangeError("low must be less than or equal to high");
|
|
1029
1029
|
}
|
|
1030
|
+
static {
|
|
1031
|
+
__name(this, "Range");
|
|
1032
|
+
}
|
|
1030
1033
|
// Determine whether a key is within the range
|
|
1031
1034
|
isInRange(key, comparator) {
|
|
1032
1035
|
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
@@ -1034,11 +1037,15 @@ var _Range = class _Range {
|
|
|
1034
1037
|
return lowCheck && highCheck;
|
|
1035
1038
|
}
|
|
1036
1039
|
};
|
|
1037
|
-
__name(_Range, "Range");
|
|
1038
|
-
var Range = _Range;
|
|
1039
1040
|
|
|
1040
1041
|
// src/data-structures/binary-tree/binary-tree.ts
|
|
1041
|
-
var
|
|
1042
|
+
var BinaryTreeNode = class {
|
|
1043
|
+
static {
|
|
1044
|
+
__name(this, "BinaryTreeNode");
|
|
1045
|
+
}
|
|
1046
|
+
key;
|
|
1047
|
+
value;
|
|
1048
|
+
parent = void 0;
|
|
1042
1049
|
/**
|
|
1043
1050
|
* Creates an instance of BinaryTreeNode.
|
|
1044
1051
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1047,17 +1054,10 @@ var _BinaryTreeNode = class _BinaryTreeNode {
|
|
|
1047
1054
|
* @param [value] - The value associated with the key.
|
|
1048
1055
|
*/
|
|
1049
1056
|
constructor(key, value) {
|
|
1050
|
-
__publicField(this, "key");
|
|
1051
|
-
__publicField(this, "value");
|
|
1052
|
-
__publicField(this, "parent");
|
|
1053
|
-
__publicField(this, "_left");
|
|
1054
|
-
__publicField(this, "_right");
|
|
1055
|
-
__publicField(this, "_height", 0);
|
|
1056
|
-
__publicField(this, "_color", "BLACK");
|
|
1057
|
-
__publicField(this, "_count", 1);
|
|
1058
1057
|
this.key = key;
|
|
1059
1058
|
this.value = value;
|
|
1060
1059
|
}
|
|
1060
|
+
_left = void 0;
|
|
1061
1061
|
/**
|
|
1062
1062
|
* Gets the left child of the node.
|
|
1063
1063
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1079,6 +1079,7 @@ var _BinaryTreeNode = class _BinaryTreeNode {
|
|
|
1079
1079
|
}
|
|
1080
1080
|
this._left = v;
|
|
1081
1081
|
}
|
|
1082
|
+
_right = void 0;
|
|
1082
1083
|
/**
|
|
1083
1084
|
* Gets the right child of the node.
|
|
1084
1085
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1100,6 +1101,7 @@ var _BinaryTreeNode = class _BinaryTreeNode {
|
|
|
1100
1101
|
}
|
|
1101
1102
|
this._right = v;
|
|
1102
1103
|
}
|
|
1104
|
+
_height = 0;
|
|
1103
1105
|
/**
|
|
1104
1106
|
* Gets the height of the node (used in self-balancing trees).
|
|
1105
1107
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1118,6 +1120,7 @@ var _BinaryTreeNode = class _BinaryTreeNode {
|
|
|
1118
1120
|
set height(value) {
|
|
1119
1121
|
this._height = value;
|
|
1120
1122
|
}
|
|
1123
|
+
_color = "BLACK";
|
|
1121
1124
|
/**
|
|
1122
1125
|
* Gets the color of the node (used in Red-Black trees).
|
|
1123
1126
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1136,6 +1139,7 @@ var _BinaryTreeNode = class _BinaryTreeNode {
|
|
|
1136
1139
|
set color(value) {
|
|
1137
1140
|
this._color = value;
|
|
1138
1141
|
}
|
|
1142
|
+
_count = 1;
|
|
1139
1143
|
/**
|
|
1140
1144
|
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
1141
1145
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1172,9 +1176,11 @@ var _BinaryTreeNode = class _BinaryTreeNode {
|
|
|
1172
1176
|
return "MAL_NODE";
|
|
1173
1177
|
}
|
|
1174
1178
|
};
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1179
|
+
var BinaryTree = class extends IterableEntryBase {
|
|
1180
|
+
static {
|
|
1181
|
+
__name(this, "BinaryTree");
|
|
1182
|
+
}
|
|
1183
|
+
iterationType = "ITERATIVE";
|
|
1178
1184
|
/**
|
|
1179
1185
|
* Creates an instance of BinaryTree.
|
|
1180
1186
|
* @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.
|
|
@@ -1184,22 +1190,6 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1184
1190
|
*/
|
|
1185
1191
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
1186
1192
|
super();
|
|
1187
|
-
__publicField(this, "iterationType", "ITERATIVE");
|
|
1188
|
-
__publicField(this, "_isMapMode", true);
|
|
1189
|
-
__publicField(this, "_isDuplicate", false);
|
|
1190
|
-
__publicField(this, "_store", /* @__PURE__ */ new Map());
|
|
1191
|
-
__publicField(this, "_root");
|
|
1192
|
-
__publicField(this, "_size", 0);
|
|
1193
|
-
__publicField(this, "_NIL", new BinaryTreeNode(NaN));
|
|
1194
|
-
__publicField(this, "_toEntryFn");
|
|
1195
|
-
/**
|
|
1196
|
-
* (Protected) Default callback function, returns the node's key.
|
|
1197
|
-
* @remarks Time O(1)
|
|
1198
|
-
*
|
|
1199
|
-
* @param node - The node.
|
|
1200
|
-
* @returns The node's key or undefined.
|
|
1201
|
-
*/
|
|
1202
|
-
__publicField(this, "_DEFAULT_NODE_CALLBACK", /* @__PURE__ */ __name((node) => node ? node.key : void 0, "_DEFAULT_NODE_CALLBACK"));
|
|
1203
1193
|
if (options) {
|
|
1204
1194
|
const { iterationType, toEntryFn, isMapMode, isDuplicate } = options;
|
|
1205
1195
|
if (iterationType) this.iterationType = iterationType;
|
|
@@ -1210,6 +1200,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1210
1200
|
}
|
|
1211
1201
|
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
1212
1202
|
}
|
|
1203
|
+
_isMapMode = true;
|
|
1213
1204
|
/**
|
|
1214
1205
|
* Gets whether the tree is in Map mode.
|
|
1215
1206
|
* @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)
|
|
@@ -1219,6 +1210,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1219
1210
|
get isMapMode() {
|
|
1220
1211
|
return this._isMapMode;
|
|
1221
1212
|
}
|
|
1213
|
+
_isDuplicate = false;
|
|
1222
1214
|
/**
|
|
1223
1215
|
* Gets whether the tree allows duplicate keys.
|
|
1224
1216
|
* @remarks Time O(1)
|
|
@@ -1228,6 +1220,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1228
1220
|
get isDuplicate() {
|
|
1229
1221
|
return this._isDuplicate;
|
|
1230
1222
|
}
|
|
1223
|
+
_store = /* @__PURE__ */ new Map();
|
|
1231
1224
|
/**
|
|
1232
1225
|
* Gets the external value store (used in Map mode).
|
|
1233
1226
|
* @remarks Time O(1)
|
|
@@ -1237,6 +1230,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1237
1230
|
get store() {
|
|
1238
1231
|
return this._store;
|
|
1239
1232
|
}
|
|
1233
|
+
_root;
|
|
1240
1234
|
/**
|
|
1241
1235
|
* Gets the root node of the tree.
|
|
1242
1236
|
* @remarks Time O(1)
|
|
@@ -1246,6 +1240,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1246
1240
|
get root() {
|
|
1247
1241
|
return this._root;
|
|
1248
1242
|
}
|
|
1243
|
+
_size = 0;
|
|
1249
1244
|
/**
|
|
1250
1245
|
* Gets the number of nodes in the tree.
|
|
1251
1246
|
* @remarks Time O(1)
|
|
@@ -1255,6 +1250,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1255
1250
|
get size() {
|
|
1256
1251
|
return this._size;
|
|
1257
1252
|
}
|
|
1253
|
+
_NIL = new BinaryTreeNode(NaN);
|
|
1258
1254
|
/**
|
|
1259
1255
|
* Gets the sentinel NIL node (used in self-balancing trees like Red-Black Tree).
|
|
1260
1256
|
* @remarks Time O(1)
|
|
@@ -1264,6 +1260,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1264
1260
|
get NIL() {
|
|
1265
1261
|
return this._NIL;
|
|
1266
1262
|
}
|
|
1263
|
+
_toEntryFn;
|
|
1267
1264
|
/**
|
|
1268
1265
|
* Gets the function used to convert raw data objects (R) into [key, value] entries.
|
|
1269
1266
|
* @remarks Time O(1)
|
|
@@ -1423,7 +1420,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1423
1420
|
if (newNode === void 0) return false;
|
|
1424
1421
|
if (!this._root) {
|
|
1425
1422
|
this._setRoot(newNode);
|
|
1426
|
-
if (this._isMapMode) this._setValue(newNode
|
|
1423
|
+
if (this._isMapMode) this._setValue(newNode?.key, newValue);
|
|
1427
1424
|
this._size = 1;
|
|
1428
1425
|
return true;
|
|
1429
1426
|
}
|
|
@@ -1455,7 +1452,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1455
1452
|
} else if (potentialParent.right === void 0) {
|
|
1456
1453
|
potentialParent.right = newNode;
|
|
1457
1454
|
}
|
|
1458
|
-
if (this._isMapMode) this._setValue(newNode
|
|
1455
|
+
if (this._isMapMode) this._setValue(newNode?.key, newValue);
|
|
1459
1456
|
this._size++;
|
|
1460
1457
|
return true;
|
|
1461
1458
|
}
|
|
@@ -1520,7 +1517,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1520
1517
|
if (!this._root) return deletedResult;
|
|
1521
1518
|
const curr = this.getNode(keyNodeOrEntry);
|
|
1522
1519
|
if (!curr) return deletedResult;
|
|
1523
|
-
const parent = curr
|
|
1520
|
+
const parent = curr?.parent;
|
|
1524
1521
|
let needBalanced;
|
|
1525
1522
|
let orgCurrent = curr;
|
|
1526
1523
|
if (!curr.left && !curr.right && !parent) {
|
|
@@ -1625,13 +1622,12 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1625
1622
|
* @returns The associated value, or undefined.
|
|
1626
1623
|
*/
|
|
1627
1624
|
get(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
1628
|
-
var _a;
|
|
1629
1625
|
if (this._isMapMode) {
|
|
1630
1626
|
const key = this._extractKey(keyNodeEntryOrPredicate);
|
|
1631
1627
|
if (key === null || key === void 0) return;
|
|
1632
1628
|
return this._store.get(key);
|
|
1633
1629
|
}
|
|
1634
|
-
return
|
|
1630
|
+
return this.getNode(keyNodeEntryOrPredicate, startNode, iterationType)?.value;
|
|
1635
1631
|
}
|
|
1636
1632
|
has(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
1637
1633
|
return this.search(keyNodeEntryOrPredicate, true, (node) => node, startNode, iterationType).length > 0;
|
|
@@ -1719,7 +1715,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1719
1715
|
let distEnsured = this.ensureNode(dist);
|
|
1720
1716
|
const beginRootEnsured = this.ensureNode(startNode);
|
|
1721
1717
|
let depth = 0;
|
|
1722
|
-
while (distEnsured
|
|
1718
|
+
while (distEnsured?.parent) {
|
|
1723
1719
|
if (distEnsured === beginRootEnsured) {
|
|
1724
1720
|
return depth;
|
|
1725
1721
|
}
|
|
@@ -2192,7 +2188,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2192
2188
|
filter(predicate, thisArg) {
|
|
2193
2189
|
const out = this._createInstance();
|
|
2194
2190
|
let i = 0;
|
|
2195
|
-
for (const [k, v] of this) if (predicate.call(thisArg,
|
|
2191
|
+
for (const [k, v] of this) if (predicate.call(thisArg, v, k, i++, this)) out.add([k, v]);
|
|
2196
2192
|
return out;
|
|
2197
2193
|
}
|
|
2198
2194
|
/**
|
|
@@ -2210,7 +2206,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2210
2206
|
map(cb, options, thisArg) {
|
|
2211
2207
|
const out = this._createLike([], options);
|
|
2212
2208
|
let i = 0;
|
|
2213
|
-
for (const [k, v] of this) out.add(cb.call(thisArg,
|
|
2209
|
+
for (const [k, v] of this) out.add(cb.call(thisArg, v, k, i++, this));
|
|
2214
2210
|
return out;
|
|
2215
2211
|
}
|
|
2216
2212
|
/**
|
|
@@ -2281,10 +2277,10 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2281
2277
|
const dfs = /* @__PURE__ */ __name((node) => {
|
|
2282
2278
|
if (!shouldVisitRoot(node)) return;
|
|
2283
2279
|
const visitLeft = /* @__PURE__ */ __name(() => {
|
|
2284
|
-
if (shouldVisitLeft(node) &&
|
|
2280
|
+
if (shouldVisitLeft(node) && node?.left !== void 0) dfs(node?.left);
|
|
2285
2281
|
}, "visitLeft");
|
|
2286
2282
|
const visitRight = /* @__PURE__ */ __name(() => {
|
|
2287
|
-
if (shouldVisitRight(node) &&
|
|
2283
|
+
if (shouldVisitRight(node) && node?.right !== void 0) dfs(node?.right);
|
|
2288
2284
|
}, "visitRight");
|
|
2289
2285
|
switch (pattern) {
|
|
2290
2286
|
case "IN":
|
|
@@ -2317,12 +2313,10 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2317
2313
|
} else {
|
|
2318
2314
|
const stack = [{ opt: 0 /* VISIT */, node: startNode }];
|
|
2319
2315
|
const pushLeft = /* @__PURE__ */ __name((cur) => {
|
|
2320
|
-
|
|
2321
|
-
if (shouldVisitLeft(cur.node)) stack.push({ opt: 0 /* VISIT */, node: (_a = cur.node) == null ? void 0 : _a.left });
|
|
2316
|
+
if (shouldVisitLeft(cur.node)) stack.push({ opt: 0 /* VISIT */, node: cur.node?.left });
|
|
2322
2317
|
}, "pushLeft");
|
|
2323
2318
|
const pushRight = /* @__PURE__ */ __name((cur) => {
|
|
2324
|
-
|
|
2325
|
-
if (shouldVisitRight(cur.node)) stack.push({ opt: 0 /* VISIT */, node: (_a = cur.node) == null ? void 0 : _a.right });
|
|
2319
|
+
if (shouldVisitRight(cur.node)) stack.push({ opt: 0 /* VISIT */, node: cur.node?.right });
|
|
2326
2320
|
}, "pushRight");
|
|
2327
2321
|
const pushRoot = /* @__PURE__ */ __name((cur) => {
|
|
2328
2322
|
if (shouldVisitRoot(cur.node)) stack.push({ opt: 1 /* PROCESS */, node: cur.node });
|
|
@@ -2394,6 +2388,14 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2394
2388
|
}
|
|
2395
2389
|
}
|
|
2396
2390
|
}
|
|
2391
|
+
/**
|
|
2392
|
+
* (Protected) Default callback function, returns the node's key.
|
|
2393
|
+
* @remarks Time O(1)
|
|
2394
|
+
*
|
|
2395
|
+
* @param node - The node.
|
|
2396
|
+
* @returns The node's key or undefined.
|
|
2397
|
+
*/
|
|
2398
|
+
_DEFAULT_NODE_CALLBACK = /* @__PURE__ */ __name((node) => node ? node.key : void 0, "_DEFAULT_NODE_CALLBACK");
|
|
2397
2399
|
/**
|
|
2398
2400
|
* (Protected) Snapshots the current tree's configuration options.
|
|
2399
2401
|
* @remarks Time O(1)
|
|
@@ -2419,7 +2421,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2419
2421
|
*/
|
|
2420
2422
|
_createInstance(options) {
|
|
2421
2423
|
const Ctor = this.constructor;
|
|
2422
|
-
return new Ctor([], { ...this._snapshotOptions(), ...options
|
|
2424
|
+
return new Ctor([], { ...this._snapshotOptions(), ...options ?? {} });
|
|
2423
2425
|
}
|
|
2424
2426
|
/**
|
|
2425
2427
|
* (Protected) Creates a new instance of the same tree constructor, potentially with different generic types.
|
|
@@ -2432,7 +2434,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2432
2434
|
*/
|
|
2433
2435
|
_createLike(iter = [], options) {
|
|
2434
2436
|
const Ctor = this.constructor;
|
|
2435
|
-
return new Ctor(iter, { ...this._snapshotOptions(), ...options
|
|
2437
|
+
return new Ctor(iter, { ...this._snapshotOptions(), ...options ?? {} });
|
|
2436
2438
|
}
|
|
2437
2439
|
/**
|
|
2438
2440
|
* (Protected) Converts a key, node, or entry into a standardized [node, value] tuple.
|
|
@@ -2450,7 +2452,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2450
2452
|
const [key, entryValue] = keyNodeOrEntry;
|
|
2451
2453
|
if (key === void 0) return [void 0, void 0];
|
|
2452
2454
|
else if (key === null) return [null, void 0];
|
|
2453
|
-
const finalValue = value
|
|
2455
|
+
const finalValue = value ?? entryValue;
|
|
2454
2456
|
return [this.createNode(key, finalValue), finalValue];
|
|
2455
2457
|
}
|
|
2456
2458
|
return [this.createNode(keyNodeOrEntry, value), value];
|
|
@@ -2657,11 +2659,15 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2657
2659
|
this._store.clear();
|
|
2658
2660
|
}
|
|
2659
2661
|
};
|
|
2660
|
-
__name(_BinaryTree, "BinaryTree");
|
|
2661
|
-
var BinaryTree = _BinaryTree;
|
|
2662
2662
|
|
|
2663
2663
|
// src/data-structures/binary-tree/bst.ts
|
|
2664
|
-
var
|
|
2664
|
+
var BSTNode = class {
|
|
2665
|
+
static {
|
|
2666
|
+
__name(this, "BSTNode");
|
|
2667
|
+
}
|
|
2668
|
+
key;
|
|
2669
|
+
value;
|
|
2670
|
+
parent = void 0;
|
|
2665
2671
|
/**
|
|
2666
2672
|
* Creates an instance of BSTNode.
|
|
2667
2673
|
* @remarks Time O(1), Space O(1)
|
|
@@ -2670,11 +2676,10 @@ var _BSTNode = class _BSTNode extends BinaryTreeNode {
|
|
|
2670
2676
|
* @param [value] - The value associated with the key.
|
|
2671
2677
|
*/
|
|
2672
2678
|
constructor(key, value) {
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
__publicField(this, "_left");
|
|
2676
|
-
__publicField(this, "_right");
|
|
2679
|
+
this.key = key;
|
|
2680
|
+
this.value = value;
|
|
2677
2681
|
}
|
|
2682
|
+
_left = void 0;
|
|
2678
2683
|
/**
|
|
2679
2684
|
* Gets the left child of the node.
|
|
2680
2685
|
* @remarks Time O(1), Space O(1)
|
|
@@ -2694,6 +2699,7 @@ var _BSTNode = class _BSTNode extends BinaryTreeNode {
|
|
|
2694
2699
|
if (v) v.parent = this;
|
|
2695
2700
|
this._left = v;
|
|
2696
2701
|
}
|
|
2702
|
+
_right = void 0;
|
|
2697
2703
|
/**
|
|
2698
2704
|
* Gets the right child of the node.
|
|
2699
2705
|
* @remarks Time O(1), Space O(1)
|
|
@@ -2713,10 +2719,85 @@ var _BSTNode = class _BSTNode extends BinaryTreeNode {
|
|
|
2713
2719
|
if (v) v.parent = this;
|
|
2714
2720
|
this._right = v;
|
|
2715
2721
|
}
|
|
2722
|
+
_height = 0;
|
|
2723
|
+
/**
|
|
2724
|
+
* Gets the height of the node (used in self-balancing trees).
|
|
2725
|
+
* @remarks Time O(1), Space O(1)
|
|
2726
|
+
*
|
|
2727
|
+
* @returns The height.
|
|
2728
|
+
*/
|
|
2729
|
+
get height() {
|
|
2730
|
+
return this._height;
|
|
2731
|
+
}
|
|
2732
|
+
/**
|
|
2733
|
+
* Sets the height of the node.
|
|
2734
|
+
* @remarks Time O(1), Space O(1)
|
|
2735
|
+
*
|
|
2736
|
+
* @param value - The new height.
|
|
2737
|
+
*/
|
|
2738
|
+
set height(value) {
|
|
2739
|
+
this._height = value;
|
|
2740
|
+
}
|
|
2741
|
+
_color = "BLACK";
|
|
2742
|
+
/**
|
|
2743
|
+
* Gets the color of the node (used in Red-Black trees).
|
|
2744
|
+
* @remarks Time O(1), Space O(1)
|
|
2745
|
+
*
|
|
2746
|
+
* @returns The node's color.
|
|
2747
|
+
*/
|
|
2748
|
+
get color() {
|
|
2749
|
+
return this._color;
|
|
2750
|
+
}
|
|
2751
|
+
/**
|
|
2752
|
+
* Sets the color of the node.
|
|
2753
|
+
* @remarks Time O(1), Space O(1)
|
|
2754
|
+
*
|
|
2755
|
+
* @param value - The new color.
|
|
2756
|
+
*/
|
|
2757
|
+
set color(value) {
|
|
2758
|
+
this._color = value;
|
|
2759
|
+
}
|
|
2760
|
+
_count = 1;
|
|
2761
|
+
/**
|
|
2762
|
+
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
2763
|
+
* @remarks Time O(1), Space O(1)
|
|
2764
|
+
*
|
|
2765
|
+
* @returns The subtree node count.
|
|
2766
|
+
*/
|
|
2767
|
+
get count() {
|
|
2768
|
+
return this._count;
|
|
2769
|
+
}
|
|
2770
|
+
/**
|
|
2771
|
+
* Sets the count of nodes in the subtree.
|
|
2772
|
+
* @remarks Time O(1), Space O(1)
|
|
2773
|
+
*
|
|
2774
|
+
* @param value - The new count.
|
|
2775
|
+
*/
|
|
2776
|
+
set count(value) {
|
|
2777
|
+
this._count = value;
|
|
2778
|
+
}
|
|
2779
|
+
/**
|
|
2780
|
+
* Gets the position of the node relative to its parent.
|
|
2781
|
+
* @remarks Time O(1), Space O(1)
|
|
2782
|
+
*
|
|
2783
|
+
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
2784
|
+
*/
|
|
2785
|
+
get familyPosition() {
|
|
2786
|
+
if (!this.parent) {
|
|
2787
|
+
return this.left || this.right ? "ROOT" : "ISOLATED";
|
|
2788
|
+
}
|
|
2789
|
+
if (this.parent.left === this) {
|
|
2790
|
+
return this.left || this.right ? "ROOT_LEFT" : "LEFT";
|
|
2791
|
+
} else if (this.parent.right === this) {
|
|
2792
|
+
return this.left || this.right ? "ROOT_RIGHT" : "RIGHT";
|
|
2793
|
+
}
|
|
2794
|
+
return "MAL_NODE";
|
|
2795
|
+
}
|
|
2716
2796
|
};
|
|
2717
|
-
|
|
2718
|
-
|
|
2719
|
-
|
|
2797
|
+
var BST = class extends BinaryTree {
|
|
2798
|
+
static {
|
|
2799
|
+
__name(this, "BST");
|
|
2800
|
+
}
|
|
2720
2801
|
/**
|
|
2721
2802
|
* Creates an instance of BST.
|
|
2722
2803
|
* @remarks Time O(N log N) or O(N^2) depending on `isBalanceAdd` in `addMany` and input order. Space O(N).
|
|
@@ -2726,33 +2807,6 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2726
2807
|
*/
|
|
2727
2808
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
2728
2809
|
super([], options);
|
|
2729
|
-
__publicField(this, "_root");
|
|
2730
|
-
__publicField(this, "_isReverse", false);
|
|
2731
|
-
/**
|
|
2732
|
-
* The default comparator function.
|
|
2733
|
-
* @remarks Time O(1) (or O(C) if `specifyComparable` is used, C is complexity of that function).
|
|
2734
|
-
*/
|
|
2735
|
-
__publicField(this, "_comparator", /* @__PURE__ */ __name((a, b) => {
|
|
2736
|
-
if (isComparable(a) && isComparable(b)) {
|
|
2737
|
-
if (a > b) return 1;
|
|
2738
|
-
if (a < b) return -1;
|
|
2739
|
-
return 0;
|
|
2740
|
-
}
|
|
2741
|
-
if (this._specifyComparable) {
|
|
2742
|
-
const va = this._specifyComparable(a);
|
|
2743
|
-
const vb = this._specifyComparable(b);
|
|
2744
|
-
if (va > vb) return 1;
|
|
2745
|
-
if (va < vb) return -1;
|
|
2746
|
-
return 0;
|
|
2747
|
-
}
|
|
2748
|
-
if (typeof a === "object" || typeof b === "object") {
|
|
2749
|
-
throw TypeError(
|
|
2750
|
-
`When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
|
|
2751
|
-
);
|
|
2752
|
-
}
|
|
2753
|
-
return 0;
|
|
2754
|
-
}, "_comparator"));
|
|
2755
|
-
__publicField(this, "_specifyComparable");
|
|
2756
2810
|
if (options) {
|
|
2757
2811
|
const { specifyComparable, isReverse } = options;
|
|
2758
2812
|
if (typeof specifyComparable === "function") this._specifyComparable = specifyComparable;
|
|
@@ -2760,6 +2814,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2760
2814
|
}
|
|
2761
2815
|
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
2762
2816
|
}
|
|
2817
|
+
_root = void 0;
|
|
2763
2818
|
/**
|
|
2764
2819
|
* Gets the root node of the tree.
|
|
2765
2820
|
* @remarks Time O(1)
|
|
@@ -2769,6 +2824,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2769
2824
|
get root() {
|
|
2770
2825
|
return this._root;
|
|
2771
2826
|
}
|
|
2827
|
+
_isReverse = false;
|
|
2772
2828
|
/**
|
|
2773
2829
|
* Gets whether the tree's comparison logic is reversed.
|
|
2774
2830
|
* @remarks Time O(1)
|
|
@@ -2778,6 +2834,30 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2778
2834
|
get isReverse() {
|
|
2779
2835
|
return this._isReverse;
|
|
2780
2836
|
}
|
|
2837
|
+
/**
|
|
2838
|
+
* The default comparator function.
|
|
2839
|
+
* @remarks Time O(1) (or O(C) if `specifyComparable` is used, C is complexity of that function).
|
|
2840
|
+
*/
|
|
2841
|
+
_comparator = /* @__PURE__ */ __name((a, b) => {
|
|
2842
|
+
if (isComparable(a) && isComparable(b)) {
|
|
2843
|
+
if (a > b) return 1;
|
|
2844
|
+
if (a < b) return -1;
|
|
2845
|
+
return 0;
|
|
2846
|
+
}
|
|
2847
|
+
if (this._specifyComparable) {
|
|
2848
|
+
const va = this._specifyComparable(a);
|
|
2849
|
+
const vb = this._specifyComparable(b);
|
|
2850
|
+
if (va > vb) return 1;
|
|
2851
|
+
if (va < vb) return -1;
|
|
2852
|
+
return 0;
|
|
2853
|
+
}
|
|
2854
|
+
if (typeof a === "object" || typeof b === "object") {
|
|
2855
|
+
throw TypeError(
|
|
2856
|
+
`When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
|
|
2857
|
+
);
|
|
2858
|
+
}
|
|
2859
|
+
return 0;
|
|
2860
|
+
}, "_comparator");
|
|
2781
2861
|
/**
|
|
2782
2862
|
* Gets the comparator function used by the tree.
|
|
2783
2863
|
* @remarks Time O(1)
|
|
@@ -2787,6 +2867,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2787
2867
|
get comparator() {
|
|
2788
2868
|
return this._comparator;
|
|
2789
2869
|
}
|
|
2870
|
+
_specifyComparable;
|
|
2790
2871
|
/**
|
|
2791
2872
|
* Gets the function used to extract a comparable value from a complex key.
|
|
2792
2873
|
* @remarks Time O(1)
|
|
@@ -2816,8 +2897,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2816
2897
|
* @returns The resolved node, or undefined if not found.
|
|
2817
2898
|
*/
|
|
2818
2899
|
ensureNode(keyNodeOrEntry, iterationType = this.iterationType) {
|
|
2819
|
-
|
|
2820
|
-
return (_a = super.ensureNode(keyNodeOrEntry, iterationType)) != null ? _a : void 0;
|
|
2900
|
+
return super.ensureNode(keyNodeOrEntry, iterationType) ?? void 0;
|
|
2821
2901
|
}
|
|
2822
2902
|
/**
|
|
2823
2903
|
* Checks if the given item is a `BSTNode` instance.
|
|
@@ -2890,8 +2970,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2890
2970
|
* @returns The first matching node, or undefined if not found.
|
|
2891
2971
|
*/
|
|
2892
2972
|
getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
2893
|
-
|
|
2894
|
-
return (_a = this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0]) != null ? _a : void 0;
|
|
2973
|
+
return this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0] ?? void 0;
|
|
2895
2974
|
}
|
|
2896
2975
|
/**
|
|
2897
2976
|
* Searches the tree for nodes matching a predicate, key, or range.
|
|
@@ -2996,7 +3075,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2996
3075
|
if (newNode === void 0) return false;
|
|
2997
3076
|
if (this._root === void 0) {
|
|
2998
3077
|
this._setRoot(newNode);
|
|
2999
|
-
if (this._isMapMode) this._setValue(newNode
|
|
3078
|
+
if (this._isMapMode) this._setValue(newNode?.key, newValue);
|
|
3000
3079
|
this._size++;
|
|
3001
3080
|
return true;
|
|
3002
3081
|
}
|
|
@@ -3009,7 +3088,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3009
3088
|
} else if (this._compare(current.key, newNode.key) > 0) {
|
|
3010
3089
|
if (current.left === void 0) {
|
|
3011
3090
|
current.left = newNode;
|
|
3012
|
-
if (this._isMapMode) this._setValue(newNode
|
|
3091
|
+
if (this._isMapMode) this._setValue(newNode?.key, newValue);
|
|
3013
3092
|
this._size++;
|
|
3014
3093
|
return true;
|
|
3015
3094
|
}
|
|
@@ -3017,7 +3096,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3017
3096
|
} else {
|
|
3018
3097
|
if (current.right === void 0) {
|
|
3019
3098
|
current.right = newNode;
|
|
3020
|
-
if (this._isMapMode) this._setValue(newNode
|
|
3099
|
+
if (this._isMapMode) this._setValue(newNode?.key, newValue);
|
|
3021
3100
|
this._size++;
|
|
3022
3101
|
return true;
|
|
3023
3102
|
}
|
|
@@ -3040,10 +3119,10 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3040
3119
|
*/
|
|
3041
3120
|
addMany(keysNodesEntriesOrRaws, values, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
3042
3121
|
const inserted = [];
|
|
3043
|
-
const valuesIterator = values
|
|
3122
|
+
const valuesIterator = values?.[Symbol.iterator]();
|
|
3044
3123
|
if (!isBalanceAdd) {
|
|
3045
3124
|
for (let kve of keysNodesEntriesOrRaws) {
|
|
3046
|
-
const val = valuesIterator
|
|
3125
|
+
const val = valuesIterator?.next().value;
|
|
3047
3126
|
if (this.isRaw(kve)) kve = this._toEntryFn(kve);
|
|
3048
3127
|
inserted.push(this.add(kve, val));
|
|
3049
3128
|
}
|
|
@@ -3052,7 +3131,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3052
3131
|
const realBTNExemplars = [];
|
|
3053
3132
|
let i = 0;
|
|
3054
3133
|
for (const kve of keysNodesEntriesOrRaws) {
|
|
3055
|
-
realBTNExemplars.push({ key: kve, value: valuesIterator
|
|
3134
|
+
realBTNExemplars.push({ key: kve, value: valuesIterator?.next().value, orgIndex: i++ });
|
|
3056
3135
|
}
|
|
3057
3136
|
const sorted = realBTNExemplars.sort(({ key: a }, { key: b }) => {
|
|
3058
3137
|
let keyA, keyB;
|
|
@@ -3233,7 +3312,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3233
3312
|
const out = this._createLike([], options);
|
|
3234
3313
|
let index = 0;
|
|
3235
3314
|
for (const [key, value] of this) {
|
|
3236
|
-
out.add(callback.call(thisArg,
|
|
3315
|
+
out.add(callback.call(thisArg, value, key, index++, this));
|
|
3237
3316
|
}
|
|
3238
3317
|
return out;
|
|
3239
3318
|
}
|
|
@@ -3274,7 +3353,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3274
3353
|
*/
|
|
3275
3354
|
_createInstance(options) {
|
|
3276
3355
|
const Ctor = this.constructor;
|
|
3277
|
-
return new Ctor([], { ...this._snapshotOptions(), ...options
|
|
3356
|
+
return new Ctor([], { ...this._snapshotOptions(), ...options ?? {} });
|
|
3278
3357
|
}
|
|
3279
3358
|
/**
|
|
3280
3359
|
* (Protected) Creates a new instance of the same BST constructor, potentially with different generic types.
|
|
@@ -3287,7 +3366,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3287
3366
|
*/
|
|
3288
3367
|
_createLike(iter = [], options) {
|
|
3289
3368
|
const Ctor = this.constructor;
|
|
3290
|
-
return new Ctor(iter, { ...this._snapshotOptions(), ...options
|
|
3369
|
+
return new Ctor(iter, { ...this._snapshotOptions(), ...options ?? {} });
|
|
3291
3370
|
}
|
|
3292
3371
|
/**
|
|
3293
3372
|
* (Protected) Snapshots the current BST's configuration options.
|
|
@@ -3314,7 +3393,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3314
3393
|
_keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value) {
|
|
3315
3394
|
const [node, entryValue] = super._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
3316
3395
|
if (node === null) return [void 0, void 0];
|
|
3317
|
-
return [node, value
|
|
3396
|
+
return [node, value ?? entryValue];
|
|
3318
3397
|
}
|
|
3319
3398
|
/**
|
|
3320
3399
|
* (Protected) Sets the root node and clears its parent reference.
|
|
@@ -3345,7 +3424,6 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3345
3424
|
* @returns True if the node was found and deleted, false otherwise.
|
|
3346
3425
|
*/
|
|
3347
3426
|
_deleteByKey(key) {
|
|
3348
|
-
var _a;
|
|
3349
3427
|
let node = this._root;
|
|
3350
3428
|
while (node) {
|
|
3351
3429
|
const cmp = this._compare(node.key, key);
|
|
@@ -3354,7 +3432,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3354
3432
|
}
|
|
3355
3433
|
if (!node) return false;
|
|
3356
3434
|
const transplant = /* @__PURE__ */ __name((u, v) => {
|
|
3357
|
-
const p = u
|
|
3435
|
+
const p = u?.parent;
|
|
3358
3436
|
if (!p) {
|
|
3359
3437
|
this._setRoot(v);
|
|
3360
3438
|
} else if (p.left === u) {
|
|
@@ -3384,15 +3462,19 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3384
3462
|
succ.left = node.left;
|
|
3385
3463
|
if (succ.left) succ.left.parent = succ;
|
|
3386
3464
|
}
|
|
3387
|
-
this._size = Math.max(0, (
|
|
3465
|
+
this._size = Math.max(0, (this._size ?? 0) - 1);
|
|
3388
3466
|
return true;
|
|
3389
3467
|
}
|
|
3390
3468
|
};
|
|
3391
|
-
__name(_BST, "BST");
|
|
3392
|
-
var BST = _BST;
|
|
3393
3469
|
|
|
3394
3470
|
// src/data-structures/binary-tree/avl-tree.ts
|
|
3395
|
-
var
|
|
3471
|
+
var AVLTreeNode = class {
|
|
3472
|
+
static {
|
|
3473
|
+
__name(this, "AVLTreeNode");
|
|
3474
|
+
}
|
|
3475
|
+
key;
|
|
3476
|
+
value;
|
|
3477
|
+
parent = void 0;
|
|
3396
3478
|
/**
|
|
3397
3479
|
* Creates an instance of AVLTreeNode.
|
|
3398
3480
|
* @remarks Time O(1), Space O(1)
|
|
@@ -3401,11 +3483,10 @@ var _AVLTreeNode = class _AVLTreeNode extends BSTNode {
|
|
|
3401
3483
|
* @param [value] - The value associated with the key.
|
|
3402
3484
|
*/
|
|
3403
3485
|
constructor(key, value) {
|
|
3404
|
-
|
|
3405
|
-
|
|
3406
|
-
__publicField(this, "_left");
|
|
3407
|
-
__publicField(this, "_right");
|
|
3486
|
+
this.key = key;
|
|
3487
|
+
this.value = value;
|
|
3408
3488
|
}
|
|
3489
|
+
_left = void 0;
|
|
3409
3490
|
/**
|
|
3410
3491
|
* Gets the left child of the node.
|
|
3411
3492
|
* @remarks Time O(1), Space O(1)
|
|
@@ -3427,6 +3508,7 @@ var _AVLTreeNode = class _AVLTreeNode extends BSTNode {
|
|
|
3427
3508
|
}
|
|
3428
3509
|
this._left = v;
|
|
3429
3510
|
}
|
|
3511
|
+
_right = void 0;
|
|
3430
3512
|
/**
|
|
3431
3513
|
* Gets the right child of the node.
|
|
3432
3514
|
* @remarks Time O(1), Space O(1)
|
|
@@ -3448,10 +3530,85 @@ var _AVLTreeNode = class _AVLTreeNode extends BSTNode {
|
|
|
3448
3530
|
}
|
|
3449
3531
|
this._right = v;
|
|
3450
3532
|
}
|
|
3533
|
+
_height = 0;
|
|
3534
|
+
/**
|
|
3535
|
+
* Gets the height of the node (used in self-balancing trees).
|
|
3536
|
+
* @remarks Time O(1), Space O(1)
|
|
3537
|
+
*
|
|
3538
|
+
* @returns The height.
|
|
3539
|
+
*/
|
|
3540
|
+
get height() {
|
|
3541
|
+
return this._height;
|
|
3542
|
+
}
|
|
3543
|
+
/**
|
|
3544
|
+
* Sets the height of the node.
|
|
3545
|
+
* @remarks Time O(1), Space O(1)
|
|
3546
|
+
*
|
|
3547
|
+
* @param value - The new height.
|
|
3548
|
+
*/
|
|
3549
|
+
set height(value) {
|
|
3550
|
+
this._height = value;
|
|
3551
|
+
}
|
|
3552
|
+
_color = "BLACK";
|
|
3553
|
+
/**
|
|
3554
|
+
* Gets the color of the node (used in Red-Black trees).
|
|
3555
|
+
* @remarks Time O(1), Space O(1)
|
|
3556
|
+
*
|
|
3557
|
+
* @returns The node's color.
|
|
3558
|
+
*/
|
|
3559
|
+
get color() {
|
|
3560
|
+
return this._color;
|
|
3561
|
+
}
|
|
3562
|
+
/**
|
|
3563
|
+
* Sets the color of the node.
|
|
3564
|
+
* @remarks Time O(1), Space O(1)
|
|
3565
|
+
*
|
|
3566
|
+
* @param value - The new color.
|
|
3567
|
+
*/
|
|
3568
|
+
set color(value) {
|
|
3569
|
+
this._color = value;
|
|
3570
|
+
}
|
|
3571
|
+
_count = 1;
|
|
3572
|
+
/**
|
|
3573
|
+
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
3574
|
+
* @remarks Time O(1), Space O(1)
|
|
3575
|
+
*
|
|
3576
|
+
* @returns The subtree node count.
|
|
3577
|
+
*/
|
|
3578
|
+
get count() {
|
|
3579
|
+
return this._count;
|
|
3580
|
+
}
|
|
3581
|
+
/**
|
|
3582
|
+
* Sets the count of nodes in the subtree.
|
|
3583
|
+
* @remarks Time O(1), Space O(1)
|
|
3584
|
+
*
|
|
3585
|
+
* @param value - The new count.
|
|
3586
|
+
*/
|
|
3587
|
+
set count(value) {
|
|
3588
|
+
this._count = value;
|
|
3589
|
+
}
|
|
3590
|
+
/**
|
|
3591
|
+
* Gets the position of the node relative to its parent.
|
|
3592
|
+
* @remarks Time O(1), Space O(1)
|
|
3593
|
+
*
|
|
3594
|
+
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
3595
|
+
*/
|
|
3596
|
+
get familyPosition() {
|
|
3597
|
+
if (!this.parent) {
|
|
3598
|
+
return this.left || this.right ? "ROOT" : "ISOLATED";
|
|
3599
|
+
}
|
|
3600
|
+
if (this.parent.left === this) {
|
|
3601
|
+
return this.left || this.right ? "ROOT_LEFT" : "LEFT";
|
|
3602
|
+
} else if (this.parent.right === this) {
|
|
3603
|
+
return this.left || this.right ? "ROOT_RIGHT" : "RIGHT";
|
|
3604
|
+
}
|
|
3605
|
+
return "MAL_NODE";
|
|
3606
|
+
}
|
|
3451
3607
|
};
|
|
3452
|
-
|
|
3453
|
-
|
|
3454
|
-
|
|
3608
|
+
var AVLTree = class extends BST {
|
|
3609
|
+
static {
|
|
3610
|
+
__name(this, "AVLTree");
|
|
3611
|
+
}
|
|
3455
3612
|
/**
|
|
3456
3613
|
* Creates an instance of AVLTree.
|
|
3457
3614
|
* @remarks Time O(N log N) (from `addMany` with balanced add). Space O(N).
|
|
@@ -3560,7 +3717,7 @@ var _AVLTree = class _AVLTree extends BST {
|
|
|
3560
3717
|
const out = this._createLike([], options);
|
|
3561
3718
|
let index = 0;
|
|
3562
3719
|
for (const [key, value] of this) {
|
|
3563
|
-
out.add(callback.call(thisArg,
|
|
3720
|
+
out.add(callback.call(thisArg, value, key, index++, this));
|
|
3564
3721
|
}
|
|
3565
3722
|
return out;
|
|
3566
3723
|
}
|
|
@@ -3574,7 +3731,7 @@ var _AVLTree = class _AVLTree extends BST {
|
|
|
3574
3731
|
*/
|
|
3575
3732
|
_createInstance(options) {
|
|
3576
3733
|
const Ctor = this.constructor;
|
|
3577
|
-
return new Ctor([], { ...this._snapshotOptions(), ...options
|
|
3734
|
+
return new Ctor([], { ...this._snapshotOptions(), ...options ?? {} });
|
|
3578
3735
|
}
|
|
3579
3736
|
/**
|
|
3580
3737
|
* (Protected) Creates a new instance of the same AVLTree constructor, potentially with different generic types.
|
|
@@ -3587,7 +3744,7 @@ var _AVLTree = class _AVLTree extends BST {
|
|
|
3587
3744
|
*/
|
|
3588
3745
|
_createLike(iter = [], options) {
|
|
3589
3746
|
const Ctor = this.constructor;
|
|
3590
|
-
return new Ctor(iter, { ...this._snapshotOptions(), ...options
|
|
3747
|
+
return new Ctor(iter, { ...this._snapshotOptions(), ...options ?? {} });
|
|
3591
3748
|
}
|
|
3592
3749
|
/**
|
|
3593
3750
|
* (Protected) Swaps properties of two nodes, including height.
|
|
@@ -3656,7 +3813,7 @@ var _AVLTree = class _AVLTree extends BST {
|
|
|
3656
3813
|
if (A === this.root) {
|
|
3657
3814
|
if (B) this._setRoot(B);
|
|
3658
3815
|
} else {
|
|
3659
|
-
if (
|
|
3816
|
+
if (parentOfA?.left === A) {
|
|
3660
3817
|
parentOfA.left = B;
|
|
3661
3818
|
} else {
|
|
3662
3819
|
if (parentOfA) parentOfA.right = B;
|
|
@@ -3839,8 +3996,6 @@ var _AVLTree = class _AVLTree extends BST {
|
|
|
3839
3996
|
return super._replaceNode(oldNode, newNode);
|
|
3840
3997
|
}
|
|
3841
3998
|
};
|
|
3842
|
-
__name(_AVLTree, "AVLTree");
|
|
3843
|
-
var AVLTree = _AVLTree;
|
|
3844
3999
|
/**
|
|
3845
4000
|
* data-structure-typed
|
|
3846
4001
|
*
|