bst-typed 2.5.1 → 2.5.3
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 +627 -120
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +627 -119
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +627 -121
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +627 -120
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/common/error.d.ts +9 -0
- package/dist/types/common/index.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +86 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +98 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +189 -13
- package/dist/types/data-structures/binary-tree/bst.d.ts +270 -3
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +136 -8
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +42 -0
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +1089 -161
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1243 -350
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +980 -255
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +1174 -284
- package/dist/types/data-structures/graph/directed-graph.d.ts +70 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +63 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +84 -6
- package/dist/types/data-structures/heap/heap.d.ts +140 -12
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +126 -0
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +106 -1
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +126 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +56 -0
- package/dist/types/data-structures/queue/deque.d.ts +127 -0
- package/dist/types/data-structures/queue/queue.d.ts +97 -0
- package/dist/types/data-structures/stack/stack.d.ts +72 -2
- package/dist/types/data-structures/trie/trie.d.ts +84 -0
- package/dist/types/interfaces/binary-tree.d.ts +2 -3
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +5 -0
- package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +4 -0
- package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +4 -0
- package/dist/umd/bst-typed.js +625 -118
- package/dist/umd/bst-typed.js.map +1 -1
- package/dist/umd/bst-typed.min.js +3 -3
- package/dist/umd/bst-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/common/error.ts +19 -1
- package/src/common/index.ts +1 -1
- package/src/data-structures/base/iterable-element-base.ts +3 -2
- package/src/data-structures/binary-tree/avl-tree.ts +99 -5
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +102 -4
- package/src/data-structures/binary-tree/binary-tree.ts +239 -78
- package/src/data-structures/binary-tree/bst.ts +542 -13
- package/src/data-structures/binary-tree/red-black-tree.ts +155 -15
- package/src/data-structures/binary-tree/segment-tree.ts +42 -0
- package/src/data-structures/binary-tree/tree-map.ts +1223 -261
- package/src/data-structures/binary-tree/tree-multi-map.ts +939 -30
- package/src/data-structures/binary-tree/tree-multi-set.ts +746 -10
- package/src/data-structures/binary-tree/tree-set.ts +1018 -99
- package/src/data-structures/graph/abstract-graph.ts +2 -2
- package/src/data-structures/graph/directed-graph.ts +71 -1
- package/src/data-structures/graph/undirected-graph.ts +64 -1
- package/src/data-structures/hash/hash-map.ts +102 -16
- package/src/data-structures/heap/heap.ts +153 -23
- package/src/data-structures/heap/max-heap.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +139 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +106 -1
- package/src/data-structures/linked-list/skip-linked-list.ts +131 -5
- package/src/data-structures/matrix/matrix.ts +65 -9
- package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
- package/src/data-structures/queue/deque.ts +130 -0
- package/src/data-structures/queue/queue.ts +109 -0
- package/src/data-structures/stack/stack.ts +75 -5
- package/src/data-structures/trie/trie.ts +86 -2
- package/src/interfaces/binary-tree.ts +1 -9
- package/src/types/data-structures/binary-tree/bst.ts +1 -0
- package/src/types/data-structures/binary-tree/tree-map.ts +6 -0
- package/src/types/data-structures/binary-tree/tree-multi-set.ts +5 -0
- package/src/types/data-structures/binary-tree/tree-set.ts +5 -0
package/dist/cjs/index.cjs
CHANGED
|
@@ -59,6 +59,61 @@ function makeTrampoline(fn) {
|
|
|
59
59
|
}
|
|
60
60
|
__name(makeTrampoline, "makeTrampoline");
|
|
61
61
|
|
|
62
|
+
// src/common/error.ts
|
|
63
|
+
function raise(ErrorClass, message) {
|
|
64
|
+
throw new ErrorClass(message);
|
|
65
|
+
}
|
|
66
|
+
__name(raise, "raise");
|
|
67
|
+
var ERR = {
|
|
68
|
+
// Range / index
|
|
69
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
70
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
71
|
+
// Type / argument
|
|
72
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
73
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
74
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
75
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
76
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
77
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
78
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
79
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
80
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
81
|
+
// State / operation
|
|
82
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
83
|
+
// Matrix
|
|
84
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
85
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
86
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
87
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
88
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch"),
|
|
89
|
+
// Order statistic
|
|
90
|
+
orderStatisticNotEnabled: /* @__PURE__ */ __name((method, ctx) => `${ctx ? ctx + ": " : ""}${method}() requires enableOrderStatistic: true.`, "orderStatisticNotEnabled")
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
// src/common/index.ts
|
|
94
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
95
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
96
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
97
|
+
return DFSOperation2;
|
|
98
|
+
})(DFSOperation || {});
|
|
99
|
+
var Range = class {
|
|
100
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
101
|
+
this.low = low;
|
|
102
|
+
this.high = high;
|
|
103
|
+
this.includeLow = includeLow;
|
|
104
|
+
this.includeHigh = includeHigh;
|
|
105
|
+
}
|
|
106
|
+
static {
|
|
107
|
+
__name(this, "Range");
|
|
108
|
+
}
|
|
109
|
+
// Determine whether a key is within the range
|
|
110
|
+
isInRange(key, comparator) {
|
|
111
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
112
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
113
|
+
return lowCheck && highCheck;
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
|
|
62
117
|
// src/data-structures/base/iterable-element-base.ts
|
|
63
118
|
var IterableElementBase = class {
|
|
64
119
|
static {
|
|
@@ -77,7 +132,7 @@ var IterableElementBase = class {
|
|
|
77
132
|
if (options) {
|
|
78
133
|
const { toElementFn } = options;
|
|
79
134
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
80
|
-
else if (toElementFn)
|
|
135
|
+
else if (toElementFn) raise(TypeError, "toElementFn must be a function type");
|
|
81
136
|
}
|
|
82
137
|
}
|
|
83
138
|
/**
|
|
@@ -240,7 +295,7 @@ var IterableElementBase = class {
|
|
|
240
295
|
acc = initialValue;
|
|
241
296
|
} else {
|
|
242
297
|
const first = iter.next();
|
|
243
|
-
if (first.done)
|
|
298
|
+
if (first.done) raise(TypeError, "Reduce of empty structure with no initial value");
|
|
244
299
|
acc = first.value;
|
|
245
300
|
index = 1;
|
|
246
301
|
}
|
|
@@ -475,55 +530,6 @@ var LinearBase = class _LinearBase extends IterableElementBase {
|
|
|
475
530
|
}
|
|
476
531
|
};
|
|
477
532
|
|
|
478
|
-
// src/common/error.ts
|
|
479
|
-
var ERR = {
|
|
480
|
-
// Range / index
|
|
481
|
-
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
482
|
-
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
483
|
-
// Type / argument
|
|
484
|
-
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
485
|
-
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
486
|
-
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
487
|
-
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
488
|
-
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
489
|
-
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
490
|
-
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
491
|
-
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
492
|
-
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
493
|
-
// State / operation
|
|
494
|
-
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
495
|
-
// Matrix
|
|
496
|
-
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
497
|
-
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
498
|
-
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
499
|
-
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
500
|
-
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
501
|
-
};
|
|
502
|
-
|
|
503
|
-
// src/common/index.ts
|
|
504
|
-
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
505
|
-
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
506
|
-
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
507
|
-
return DFSOperation2;
|
|
508
|
-
})(DFSOperation || {});
|
|
509
|
-
var Range = class {
|
|
510
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
511
|
-
this.low = low;
|
|
512
|
-
this.high = high;
|
|
513
|
-
this.includeLow = includeLow;
|
|
514
|
-
this.includeHigh = includeHigh;
|
|
515
|
-
}
|
|
516
|
-
static {
|
|
517
|
-
__name(this, "Range");
|
|
518
|
-
}
|
|
519
|
-
// Determine whether a key is within the range
|
|
520
|
-
isInRange(key, comparator) {
|
|
521
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
522
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
523
|
-
return lowCheck && highCheck;
|
|
524
|
-
}
|
|
525
|
-
};
|
|
526
|
-
|
|
527
533
|
// src/data-structures/base/iterable-entry-base.ts
|
|
528
534
|
var IterableEntryBase = class {
|
|
529
535
|
static {
|
|
@@ -790,6 +796,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
790
796
|
|
|
791
797
|
|
|
792
798
|
|
|
799
|
+
|
|
800
|
+
|
|
801
|
+
|
|
802
|
+
|
|
803
|
+
|
|
804
|
+
|
|
805
|
+
|
|
793
806
|
|
|
794
807
|
|
|
795
808
|
|
|
@@ -837,6 +850,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
837
850
|
|
|
838
851
|
|
|
839
852
|
|
|
853
|
+
|
|
854
|
+
|
|
855
|
+
|
|
856
|
+
|
|
857
|
+
|
|
858
|
+
|
|
859
|
+
|
|
840
860
|
|
|
841
861
|
|
|
842
862
|
|
|
@@ -854,6 +874,14 @@ var Queue = class _Queue extends LinearBase {
|
|
|
854
874
|
get first() {
|
|
855
875
|
return this.length > 0 ? this.elements[this._offset] : void 0;
|
|
856
876
|
}
|
|
877
|
+
/**
|
|
878
|
+
* Peek at the front element without removing it (alias for `first`).
|
|
879
|
+
* @remarks Time O(1), Space O(1)
|
|
880
|
+
* @returns Front element or undefined.
|
|
881
|
+
*/
|
|
882
|
+
peek() {
|
|
883
|
+
return this.first;
|
|
884
|
+
}
|
|
857
885
|
/**
|
|
858
886
|
* Get the last element (back) without removing it.
|
|
859
887
|
* @remarks Time O(1), Space O(1)
|
|
@@ -900,6 +928,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
900
928
|
|
|
901
929
|
|
|
902
930
|
|
|
931
|
+
|
|
932
|
+
|
|
933
|
+
|
|
934
|
+
|
|
935
|
+
|
|
936
|
+
|
|
937
|
+
|
|
903
938
|
|
|
904
939
|
|
|
905
940
|
|
|
@@ -959,6 +994,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
959
994
|
|
|
960
995
|
|
|
961
996
|
|
|
997
|
+
|
|
998
|
+
|
|
999
|
+
|
|
1000
|
+
|
|
1001
|
+
|
|
1002
|
+
|
|
1003
|
+
|
|
962
1004
|
|
|
963
1005
|
|
|
964
1006
|
|
|
@@ -1025,6 +1067,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1025
1067
|
|
|
1026
1068
|
|
|
1027
1069
|
|
|
1070
|
+
|
|
1071
|
+
|
|
1072
|
+
|
|
1073
|
+
|
|
1074
|
+
|
|
1075
|
+
|
|
1076
|
+
|
|
1028
1077
|
|
|
1029
1078
|
|
|
1030
1079
|
|
|
@@ -1081,6 +1130,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1081
1130
|
|
|
1082
1131
|
|
|
1083
1132
|
|
|
1133
|
+
|
|
1134
|
+
|
|
1135
|
+
|
|
1136
|
+
|
|
1137
|
+
|
|
1138
|
+
|
|
1139
|
+
|
|
1084
1140
|
|
|
1085
1141
|
|
|
1086
1142
|
|
|
@@ -1130,6 +1186,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1130
1186
|
|
|
1131
1187
|
|
|
1132
1188
|
|
|
1189
|
+
|
|
1190
|
+
|
|
1191
|
+
|
|
1192
|
+
|
|
1193
|
+
|
|
1194
|
+
|
|
1195
|
+
|
|
1133
1196
|
|
|
1134
1197
|
|
|
1135
1198
|
|
|
@@ -1184,6 +1247,21 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1184
1247
|
this._elements[this._offset + index] = newElement;
|
|
1185
1248
|
return true;
|
|
1186
1249
|
}
|
|
1250
|
+
/**
|
|
1251
|
+
* Delete the first element that satisfies a predicate.
|
|
1252
|
+
* @remarks Time O(N), Space O(N)
|
|
1253
|
+
* @param predicate - Function (value, index, queue) → boolean to decide deletion.
|
|
1254
|
+
* @returns True if a match was removed.
|
|
1255
|
+
*/
|
|
1256
|
+
deleteWhere(predicate) {
|
|
1257
|
+
for (let i = 0; i < this.length; i++) {
|
|
1258
|
+
if (predicate(this._elements[this._offset + i], i, this)) {
|
|
1259
|
+
this.deleteAt(i);
|
|
1260
|
+
return true;
|
|
1261
|
+
}
|
|
1262
|
+
}
|
|
1263
|
+
return false;
|
|
1264
|
+
}
|
|
1187
1265
|
/**
|
|
1188
1266
|
* Reverse the queue in-place by compacting then reversing.
|
|
1189
1267
|
* @remarks Time O(N), Space O(N)
|
|
@@ -1220,6 +1298,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1220
1298
|
|
|
1221
1299
|
|
|
1222
1300
|
|
|
1301
|
+
|
|
1302
|
+
|
|
1303
|
+
|
|
1304
|
+
|
|
1305
|
+
|
|
1306
|
+
|
|
1307
|
+
|
|
1223
1308
|
|
|
1224
1309
|
|
|
1225
1310
|
|
|
@@ -1263,6 +1348,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1263
1348
|
|
|
1264
1349
|
|
|
1265
1350
|
|
|
1351
|
+
|
|
1352
|
+
|
|
1353
|
+
|
|
1354
|
+
|
|
1355
|
+
|
|
1356
|
+
|
|
1357
|
+
|
|
1266
1358
|
|
|
1267
1359
|
|
|
1268
1360
|
|
|
@@ -1329,6 +1421,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1329
1421
|
|
|
1330
1422
|
|
|
1331
1423
|
|
|
1424
|
+
|
|
1425
|
+
|
|
1426
|
+
|
|
1427
|
+
|
|
1428
|
+
|
|
1429
|
+
|
|
1430
|
+
|
|
1332
1431
|
|
|
1333
1432
|
|
|
1334
1433
|
|
|
@@ -1379,6 +1478,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1379
1478
|
|
|
1380
1479
|
|
|
1381
1480
|
|
|
1481
|
+
|
|
1482
|
+
|
|
1483
|
+
|
|
1484
|
+
|
|
1485
|
+
|
|
1486
|
+
|
|
1487
|
+
|
|
1382
1488
|
|
|
1383
1489
|
|
|
1384
1490
|
|
|
@@ -1433,6 +1539,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1433
1539
|
|
|
1434
1540
|
|
|
1435
1541
|
|
|
1542
|
+
|
|
1543
|
+
|
|
1544
|
+
|
|
1545
|
+
|
|
1546
|
+
|
|
1547
|
+
|
|
1548
|
+
|
|
1436
1549
|
|
|
1437
1550
|
|
|
1438
1551
|
|
|
@@ -1691,7 +1804,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1691
1804
|
if (isMapMode !== void 0) this._isMapMode = isMapMode;
|
|
1692
1805
|
if (isDuplicate !== void 0) this._isDuplicate = isDuplicate;
|
|
1693
1806
|
if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
|
|
1694
|
-
else if (toEntryFn)
|
|
1807
|
+
else if (toEntryFn) raise(TypeError, ERR.notAFunction("toEntryFn", "BinaryTree"));
|
|
1695
1808
|
}
|
|
1696
1809
|
if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
|
|
1697
1810
|
}
|
|
@@ -1907,7 +2020,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1907
2020
|
}
|
|
1908
2021
|
/**
|
|
1909
2022
|
* Adds a new node to the tree.
|
|
1910
|
-
* @remarks Time O(
|
|
2023
|
+
* @remarks Time O(N) — level-order traversal to find an empty slot. Space O(N) for the BFS queue. BST/Red-Black Tree/AVL Tree subclasses override to O(log N).
|
|
1911
2024
|
*
|
|
1912
2025
|
* @param keyNodeOrEntry - The key, node, or entry to add.
|
|
1913
2026
|
* @returns True if the addition was successful, false otherwise.
|
|
@@ -1930,6 +2043,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1930
2043
|
|
|
1931
2044
|
|
|
1932
2045
|
|
|
2046
|
+
|
|
2047
|
+
|
|
2048
|
+
|
|
2049
|
+
|
|
2050
|
+
|
|
2051
|
+
|
|
2052
|
+
|
|
1933
2053
|
|
|
1934
2054
|
|
|
1935
2055
|
|
|
@@ -1952,7 +2072,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1952
2072
|
}
|
|
1953
2073
|
/**
|
|
1954
2074
|
* Adds or updates a new node to the tree.
|
|
1955
|
-
* @remarks Time O(
|
|
2075
|
+
* @remarks Time O(N) — level-order traversal to find an empty slot. Space O(N) for the BFS queue. BST/Red-Black Tree/AVL Tree subclasses override to O(log N).
|
|
1956
2076
|
*
|
|
1957
2077
|
* @param keyNodeOrEntry - The key, node, or entry to set or update.
|
|
1958
2078
|
* @param [value] - The value, if providing just a key.
|
|
@@ -1981,6 +2101,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1981
2101
|
|
|
1982
2102
|
|
|
1983
2103
|
|
|
2104
|
+
|
|
2105
|
+
|
|
2106
|
+
|
|
2107
|
+
|
|
2108
|
+
|
|
2109
|
+
|
|
2110
|
+
|
|
1984
2111
|
|
|
1985
2112
|
|
|
1986
2113
|
|
|
@@ -2084,6 +2211,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2084
2211
|
|
|
2085
2212
|
|
|
2086
2213
|
|
|
2214
|
+
|
|
2215
|
+
|
|
2216
|
+
|
|
2217
|
+
|
|
2218
|
+
|
|
2219
|
+
|
|
2220
|
+
|
|
2087
2221
|
|
|
2088
2222
|
|
|
2089
2223
|
|
|
@@ -2123,6 +2257,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2123
2257
|
|
|
2124
2258
|
|
|
2125
2259
|
|
|
2260
|
+
|
|
2261
|
+
|
|
2262
|
+
|
|
2263
|
+
|
|
2264
|
+
|
|
2265
|
+
|
|
2266
|
+
|
|
2126
2267
|
|
|
2127
2268
|
|
|
2128
2269
|
|
|
@@ -2183,6 +2324,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2183
2324
|
|
|
2184
2325
|
|
|
2185
2326
|
|
|
2327
|
+
|
|
2328
|
+
|
|
2329
|
+
|
|
2330
|
+
|
|
2331
|
+
|
|
2332
|
+
|
|
2333
|
+
|
|
2186
2334
|
|
|
2187
2335
|
|
|
2188
2336
|
|
|
@@ -2202,62 +2350,14 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2202
2350
|
this.setMany(anotherTree, []);
|
|
2203
2351
|
}
|
|
2204
2352
|
/**
|
|
2205
|
-
*
|
|
2206
|
-
* @remarks Time O(N)
|
|
2353
|
+
* Deletes a node from the tree (internal, returns balancing metadata).
|
|
2354
|
+
* @remarks Time O(N) — O(N) to find the node + O(H) for predecessor swap. Space O(1). BST/Red-Black Tree/AVL Tree subclasses override to O(log N).
|
|
2355
|
+
* @internal Used by AVL/BST subclasses that need balancing metadata after deletion.
|
|
2207
2356
|
*
|
|
2208
|
-
* @param
|
|
2209
|
-
* @
|
|
2357
|
+
* @param keyNodeEntryRawOrPredicate - The node to delete.
|
|
2358
|
+
* @returns An array containing deletion results with balancing metadata.
|
|
2210
2359
|
*/
|
|
2211
|
-
|
|
2212
|
-
this.clear();
|
|
2213
|
-
this.setMany(keysNodesEntriesOrRaws, values);
|
|
2214
|
-
}
|
|
2215
|
-
/**
|
|
2216
|
-
* Deletes a node from the tree.
|
|
2217
|
-
* @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation finds the node, and if it has two children, swaps it with the rightmost node of its left subtree (in-order predecessor) before deleting. Time O(N) in the worst case. O(N) to find the node (`getNode`) and O(H) (which is O(N) worst-case) to find the rightmost node. Space O(1) (if `getNode` is iterative, which it is).
|
|
2218
|
-
*
|
|
2219
|
-
* @param keyNodeEntryRawOrPredicate - The node to delete.
|
|
2220
|
-
* @returns An array containing deletion results (for compatibility with self-balancing trees).
|
|
2221
|
-
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
|
|
2243
|
-
|
|
2244
|
-
|
|
2245
|
-
|
|
2246
|
-
|
|
2247
|
-
|
|
2248
|
-
|
|
2249
|
-
|
|
2250
|
-
|
|
2251
|
-
|
|
2252
|
-
|
|
2253
|
-
* @example
|
|
2254
|
-
* // Remove a node
|
|
2255
|
-
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
2256
|
-
* tree.delete(3);
|
|
2257
|
-
* console.log(tree.has(3)); // false;
|
|
2258
|
-
* console.log(tree.size); // 4;
|
|
2259
|
-
*/
|
|
2260
|
-
delete(keyNodeEntryRawOrPredicate) {
|
|
2360
|
+
_deleteInternal(keyNodeEntryRawOrPredicate) {
|
|
2261
2361
|
const deletedResult = [];
|
|
2262
2362
|
if (!this._root) return deletedResult;
|
|
2263
2363
|
const curr = this.getNode(keyNodeEntryRawOrPredicate);
|
|
@@ -2300,9 +2400,64 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2300
2400
|
if (this._isMapMode && orgCurrent) this._store.delete(orgCurrent.key);
|
|
2301
2401
|
return deletedResult;
|
|
2302
2402
|
}
|
|
2403
|
+
/**
|
|
2404
|
+
* Deletes a node from the tree.
|
|
2405
|
+
* @remarks Time O(N) — O(N) to find the node + O(H) for predecessor swap. Space O(1). BST/Red-Black Tree/AVL Tree subclasses override to O(log N).
|
|
2406
|
+
*
|
|
2407
|
+
* @param keyNodeEntryRawOrPredicate - The node to delete.
|
|
2408
|
+
* @returns True if the node was found and deleted, false otherwise.
|
|
2409
|
+
|
|
2410
|
+
|
|
2411
|
+
|
|
2412
|
+
|
|
2413
|
+
|
|
2414
|
+
|
|
2415
|
+
|
|
2416
|
+
|
|
2417
|
+
|
|
2418
|
+
|
|
2419
|
+
|
|
2420
|
+
|
|
2421
|
+
|
|
2422
|
+
|
|
2423
|
+
|
|
2424
|
+
|
|
2425
|
+
|
|
2426
|
+
|
|
2427
|
+
|
|
2428
|
+
|
|
2429
|
+
|
|
2430
|
+
|
|
2431
|
+
|
|
2432
|
+
|
|
2433
|
+
|
|
2434
|
+
|
|
2435
|
+
|
|
2436
|
+
|
|
2437
|
+
|
|
2438
|
+
|
|
2439
|
+
|
|
2440
|
+
|
|
2441
|
+
|
|
2442
|
+
|
|
2443
|
+
|
|
2444
|
+
|
|
2445
|
+
|
|
2446
|
+
|
|
2447
|
+
|
|
2448
|
+
* @example
|
|
2449
|
+
* // Remove a node
|
|
2450
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
2451
|
+
* tree.delete(3);
|
|
2452
|
+
* console.log(tree.has(3)); // false;
|
|
2453
|
+
* console.log(tree.size); // 4;
|
|
2454
|
+
*/
|
|
2455
|
+
delete(keyNodeEntryRawOrPredicate) {
|
|
2456
|
+
return this._deleteInternal(keyNodeEntryRawOrPredicate).length > 0;
|
|
2457
|
+
}
|
|
2303
2458
|
/**
|
|
2304
2459
|
* Searches the tree for nodes matching a predicate.
|
|
2305
|
-
* @remarks Time O(
|
|
2460
|
+
* @remarks Time O(N) — full DFS scan; may visit every node. Space O(H) for call/explicit stack (O(N) worst-case). BST subclasses with key search override to O(log N).
|
|
2306
2461
|
*
|
|
2307
2462
|
* @template C - The type of the callback function.
|
|
2308
2463
|
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
@@ -2351,7 +2506,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2351
2506
|
}
|
|
2352
2507
|
/**
|
|
2353
2508
|
* Gets the first node matching a predicate.
|
|
2354
|
-
* @remarks Time O(
|
|
2509
|
+
* @remarks Time O(N) via `search`. Space O(H) or O(N). BST/Red-Black Tree/AVL Tree subclasses override to O(log N) for key lookups.
|
|
2355
2510
|
*
|
|
2356
2511
|
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
2357
2512
|
* @param [startNode=this._root] - The node to start the search from.
|
|
@@ -2379,6 +2534,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2379
2534
|
|
|
2380
2535
|
|
|
2381
2536
|
|
|
2537
|
+
|
|
2538
|
+
|
|
2539
|
+
|
|
2540
|
+
|
|
2541
|
+
|
|
2542
|
+
|
|
2543
|
+
|
|
2382
2544
|
|
|
2383
2545
|
|
|
2384
2546
|
|
|
@@ -2404,7 +2566,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2404
2566
|
}
|
|
2405
2567
|
/**
|
|
2406
2568
|
* Gets the value associated with a key.
|
|
2407
|
-
* @remarks Time O(
|
|
2569
|
+
* @remarks Time O(1) in Map mode, O(N) otherwise (via `getNode`). Space O(1) in Map mode, O(H) or O(N) otherwise. BST subclasses override non-Map-mode to O(log N).
|
|
2408
2570
|
*
|
|
2409
2571
|
* @param keyNodeEntryOrPredicate - The key, node, or entry to get the value for.
|
|
2410
2572
|
* @param [startNode=this._root] - The node to start searching from (if not in Map mode).
|
|
@@ -2434,6 +2596,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2434
2596
|
|
|
2435
2597
|
|
|
2436
2598
|
|
|
2599
|
+
|
|
2600
|
+
|
|
2601
|
+
|
|
2602
|
+
|
|
2603
|
+
|
|
2604
|
+
|
|
2605
|
+
|
|
2437
2606
|
|
|
2438
2607
|
|
|
2439
2608
|
|
|
@@ -2491,6 +2660,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2491
2660
|
|
|
2492
2661
|
|
|
2493
2662
|
|
|
2663
|
+
|
|
2664
|
+
|
|
2665
|
+
|
|
2666
|
+
|
|
2667
|
+
|
|
2668
|
+
|
|
2669
|
+
|
|
2494
2670
|
|
|
2495
2671
|
|
|
2496
2672
|
|
|
@@ -2536,6 +2712,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2536
2712
|
|
|
2537
2713
|
|
|
2538
2714
|
|
|
2715
|
+
|
|
2716
|
+
|
|
2717
|
+
|
|
2718
|
+
|
|
2719
|
+
|
|
2720
|
+
|
|
2721
|
+
|
|
2539
2722
|
|
|
2540
2723
|
|
|
2541
2724
|
|
|
@@ -2590,6 +2773,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2590
2773
|
|
|
2591
2774
|
|
|
2592
2775
|
|
|
2776
|
+
|
|
2777
|
+
|
|
2778
|
+
|
|
2779
|
+
|
|
2780
|
+
|
|
2781
|
+
|
|
2782
|
+
|
|
2593
2783
|
|
|
2594
2784
|
|
|
2595
2785
|
|
|
@@ -2671,6 +2861,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2671
2861
|
|
|
2672
2862
|
|
|
2673
2863
|
|
|
2864
|
+
|
|
2865
|
+
|
|
2866
|
+
|
|
2867
|
+
|
|
2868
|
+
|
|
2869
|
+
|
|
2870
|
+
|
|
2674
2871
|
|
|
2675
2872
|
|
|
2676
2873
|
|
|
@@ -2729,6 +2926,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2729
2926
|
|
|
2730
2927
|
|
|
2731
2928
|
|
|
2929
|
+
|
|
2930
|
+
|
|
2931
|
+
|
|
2932
|
+
|
|
2933
|
+
|
|
2934
|
+
|
|
2935
|
+
|
|
2732
2936
|
|
|
2733
2937
|
|
|
2734
2938
|
|
|
@@ -3203,6 +3407,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3203
3407
|
|
|
3204
3408
|
|
|
3205
3409
|
|
|
3410
|
+
|
|
3411
|
+
|
|
3412
|
+
|
|
3413
|
+
|
|
3414
|
+
|
|
3415
|
+
|
|
3416
|
+
|
|
3206
3417
|
|
|
3207
3418
|
|
|
3208
3419
|
|
|
@@ -3252,6 +3463,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3252
3463
|
|
|
3253
3464
|
|
|
3254
3465
|
|
|
3466
|
+
|
|
3467
|
+
|
|
3468
|
+
|
|
3469
|
+
|
|
3470
|
+
|
|
3471
|
+
|
|
3472
|
+
|
|
3255
3473
|
|
|
3256
3474
|
|
|
3257
3475
|
|
|
@@ -3305,6 +3523,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3305
3523
|
|
|
3306
3524
|
|
|
3307
3525
|
|
|
3526
|
+
|
|
3527
|
+
|
|
3528
|
+
|
|
3529
|
+
|
|
3530
|
+
|
|
3531
|
+
|
|
3532
|
+
|
|
3308
3533
|
|
|
3309
3534
|
|
|
3310
3535
|
|
|
@@ -3383,6 +3608,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3383
3608
|
|
|
3384
3609
|
|
|
3385
3610
|
|
|
3611
|
+
|
|
3612
|
+
|
|
3613
|
+
|
|
3614
|
+
|
|
3615
|
+
|
|
3616
|
+
|
|
3617
|
+
|
|
3386
3618
|
|
|
3387
3619
|
|
|
3388
3620
|
|
|
@@ -4022,12 +4254,16 @@ var BST = class extends BinaryTree {
|
|
|
4022
4254
|
} else {
|
|
4023
4255
|
this._comparator = this._createDefaultComparator();
|
|
4024
4256
|
}
|
|
4257
|
+
if (options.enableOrderStatistic) {
|
|
4258
|
+
this._enableOrderStatistic = true;
|
|
4259
|
+
}
|
|
4025
4260
|
} else {
|
|
4026
4261
|
this._comparator = this._createDefaultComparator();
|
|
4027
4262
|
}
|
|
4028
4263
|
if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
|
|
4029
4264
|
}
|
|
4030
4265
|
_root = void 0;
|
|
4266
|
+
_enableOrderStatistic = false;
|
|
4031
4267
|
/**
|
|
4032
4268
|
* Gets the root node of the tree.
|
|
4033
4269
|
* @remarks Time O(1)
|
|
@@ -4189,6 +4425,20 @@ var BST = class extends BinaryTree {
|
|
|
4189
4425
|
|
|
4190
4426
|
|
|
4191
4427
|
|
|
4428
|
+
|
|
4429
|
+
|
|
4430
|
+
|
|
4431
|
+
|
|
4432
|
+
|
|
4433
|
+
|
|
4434
|
+
|
|
4435
|
+
|
|
4436
|
+
|
|
4437
|
+
|
|
4438
|
+
|
|
4439
|
+
|
|
4440
|
+
|
|
4441
|
+
|
|
4192
4442
|
|
|
4193
4443
|
|
|
4194
4444
|
|
|
@@ -4358,6 +4608,84 @@ var BST = class extends BinaryTree {
|
|
|
4358
4608
|
const searchRange = range instanceof Range ? range : new Range(range[0], range[1]);
|
|
4359
4609
|
return this.search(searchRange, false, callback, startNode, iterationType);
|
|
4360
4610
|
}
|
|
4611
|
+
getByRank(k, callback = this._DEFAULT_NODE_CALLBACK, iterationType = this.iterationType) {
|
|
4612
|
+
if (!this._enableOrderStatistic) {
|
|
4613
|
+
raise(Error, ERR.orderStatisticNotEnabled("getByRank"));
|
|
4614
|
+
}
|
|
4615
|
+
if (k < 0 || k >= this._size) return void 0;
|
|
4616
|
+
let actualCallback = void 0;
|
|
4617
|
+
let actualIterationType = this.iterationType;
|
|
4618
|
+
if (typeof callback === "string") {
|
|
4619
|
+
actualIterationType = callback;
|
|
4620
|
+
} else if (callback) {
|
|
4621
|
+
actualCallback = callback;
|
|
4622
|
+
if (iterationType) {
|
|
4623
|
+
actualIterationType = iterationType;
|
|
4624
|
+
}
|
|
4625
|
+
}
|
|
4626
|
+
const node = actualIterationType === "RECURSIVE" ? this._getByRankRecursive(this._root, k) : this._getByRankIterative(this._root, k);
|
|
4627
|
+
if (!node) return void 0;
|
|
4628
|
+
return actualCallback ? actualCallback(node) : node.key;
|
|
4629
|
+
}
|
|
4630
|
+
getRank(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
4631
|
+
if (!this._enableOrderStatistic) {
|
|
4632
|
+
raise(Error, ERR.orderStatisticNotEnabled("getRank"));
|
|
4633
|
+
}
|
|
4634
|
+
if (!this._root || this._size === 0) return -1;
|
|
4635
|
+
let actualIterationType = this.iterationType;
|
|
4636
|
+
if (iterationType) actualIterationType = iterationType;
|
|
4637
|
+
let key;
|
|
4638
|
+
if (typeof keyNodeEntryOrPredicate === "function") {
|
|
4639
|
+
const results = this.search(keyNodeEntryOrPredicate, true);
|
|
4640
|
+
if (results.length === 0 || results[0] === void 0) return -1;
|
|
4641
|
+
key = results[0];
|
|
4642
|
+
} else if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
4643
|
+
return -1;
|
|
4644
|
+
} else if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
4645
|
+
key = keyNodeEntryOrPredicate.key;
|
|
4646
|
+
} else if (Array.isArray(keyNodeEntryOrPredicate)) {
|
|
4647
|
+
key = keyNodeEntryOrPredicate[0] ?? void 0;
|
|
4648
|
+
if (key === void 0 || key === null) return -1;
|
|
4649
|
+
} else {
|
|
4650
|
+
key = keyNodeEntryOrPredicate;
|
|
4651
|
+
}
|
|
4652
|
+
if (key === void 0) return -1;
|
|
4653
|
+
return actualIterationType === "RECURSIVE" ? this._getRankRecursive(this._root, key) : this._getRankIterative(this._root, key);
|
|
4654
|
+
}
|
|
4655
|
+
rangeByRank(start, end, callback = this._DEFAULT_NODE_CALLBACK, iterationType = this.iterationType) {
|
|
4656
|
+
if (!this._enableOrderStatistic) {
|
|
4657
|
+
raise(Error, ERR.orderStatisticNotEnabled("rangeByRank"));
|
|
4658
|
+
}
|
|
4659
|
+
if (this._size === 0) return [];
|
|
4660
|
+
const lo = Math.max(0, start);
|
|
4661
|
+
const hi = Math.min(this._size - 1, end);
|
|
4662
|
+
if (lo > hi) return [];
|
|
4663
|
+
let actualCallback = void 0;
|
|
4664
|
+
let actualIterationType = this.iterationType;
|
|
4665
|
+
if (typeof callback === "string") {
|
|
4666
|
+
actualIterationType = callback;
|
|
4667
|
+
} else if (callback) {
|
|
4668
|
+
actualCallback = callback;
|
|
4669
|
+
if (iterationType) {
|
|
4670
|
+
actualIterationType = iterationType;
|
|
4671
|
+
}
|
|
4672
|
+
}
|
|
4673
|
+
const results = [];
|
|
4674
|
+
const count = hi - lo + 1;
|
|
4675
|
+
const startNode = actualIterationType === "RECURSIVE" ? this._getByRankRecursive(this._root, lo) : this._getByRankIterative(this._root, lo);
|
|
4676
|
+
if (!startNode) return [];
|
|
4677
|
+
let collected = 0;
|
|
4678
|
+
const cb = actualCallback ?? this._DEFAULT_NODE_CALLBACK;
|
|
4679
|
+
let current = startNode;
|
|
4680
|
+
while (current && collected < count) {
|
|
4681
|
+
results.push(cb(current));
|
|
4682
|
+
collected++;
|
|
4683
|
+
if (collected < count) {
|
|
4684
|
+
current = this._next(current);
|
|
4685
|
+
}
|
|
4686
|
+
}
|
|
4687
|
+
return results;
|
|
4688
|
+
}
|
|
4361
4689
|
/**
|
|
4362
4690
|
* Adds a new node to the BST based on key comparison.
|
|
4363
4691
|
* @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
|
|
@@ -4432,6 +4760,27 @@ var BST = class extends BinaryTree {
|
|
|
4432
4760
|
|
|
4433
4761
|
|
|
4434
4762
|
|
|
4763
|
+
|
|
4764
|
+
|
|
4765
|
+
|
|
4766
|
+
|
|
4767
|
+
|
|
4768
|
+
|
|
4769
|
+
|
|
4770
|
+
|
|
4771
|
+
|
|
4772
|
+
|
|
4773
|
+
|
|
4774
|
+
|
|
4775
|
+
|
|
4776
|
+
|
|
4777
|
+
|
|
4778
|
+
|
|
4779
|
+
|
|
4780
|
+
|
|
4781
|
+
|
|
4782
|
+
|
|
4783
|
+
|
|
4435
4784
|
|
|
4436
4785
|
|
|
4437
4786
|
|
|
@@ -4467,6 +4816,7 @@ var BST = class extends BinaryTree {
|
|
|
4467
4816
|
this._setRoot(newNode);
|
|
4468
4817
|
if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
|
|
4469
4818
|
this._size++;
|
|
4819
|
+
this._updateCount(newNode);
|
|
4470
4820
|
return true;
|
|
4471
4821
|
}
|
|
4472
4822
|
let current = this._root;
|
|
@@ -4480,6 +4830,7 @@ var BST = class extends BinaryTree {
|
|
|
4480
4830
|
current.left = newNode;
|
|
4481
4831
|
if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
|
|
4482
4832
|
this._size++;
|
|
4833
|
+
this._updateCountAlongPath(newNode);
|
|
4483
4834
|
return true;
|
|
4484
4835
|
}
|
|
4485
4836
|
if (current.left !== null) current = current.left;
|
|
@@ -4488,6 +4839,7 @@ var BST = class extends BinaryTree {
|
|
|
4488
4839
|
current.right = newNode;
|
|
4489
4840
|
if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
|
|
4490
4841
|
this._size++;
|
|
4842
|
+
this._updateCountAlongPath(newNode);
|
|
4491
4843
|
return true;
|
|
4492
4844
|
}
|
|
4493
4845
|
if (current.right !== null) current = current.right;
|
|
@@ -4544,6 +4896,20 @@ var BST = class extends BinaryTree {
|
|
|
4544
4896
|
|
|
4545
4897
|
|
|
4546
4898
|
|
|
4899
|
+
|
|
4900
|
+
|
|
4901
|
+
|
|
4902
|
+
|
|
4903
|
+
|
|
4904
|
+
|
|
4905
|
+
|
|
4906
|
+
|
|
4907
|
+
|
|
4908
|
+
|
|
4909
|
+
|
|
4910
|
+
|
|
4911
|
+
|
|
4912
|
+
|
|
4547
4913
|
|
|
4548
4914
|
|
|
4549
4915
|
|
|
@@ -4835,6 +5201,13 @@ var BST = class extends BinaryTree {
|
|
|
4835
5201
|
|
|
4836
5202
|
|
|
4837
5203
|
|
|
5204
|
+
|
|
5205
|
+
|
|
5206
|
+
|
|
5207
|
+
|
|
5208
|
+
|
|
5209
|
+
|
|
5210
|
+
|
|
4838
5211
|
|
|
4839
5212
|
|
|
4840
5213
|
|
|
@@ -4901,6 +5274,13 @@ var BST = class extends BinaryTree {
|
|
|
4901
5274
|
|
|
4902
5275
|
|
|
4903
5276
|
|
|
5277
|
+
|
|
5278
|
+
|
|
5279
|
+
|
|
5280
|
+
|
|
5281
|
+
|
|
5282
|
+
|
|
5283
|
+
|
|
4904
5284
|
|
|
4905
5285
|
|
|
4906
5286
|
|
|
@@ -5010,6 +5390,20 @@ var BST = class extends BinaryTree {
|
|
|
5010
5390
|
|
|
5011
5391
|
|
|
5012
5392
|
|
|
5393
|
+
|
|
5394
|
+
|
|
5395
|
+
|
|
5396
|
+
|
|
5397
|
+
|
|
5398
|
+
|
|
5399
|
+
|
|
5400
|
+
|
|
5401
|
+
|
|
5402
|
+
|
|
5403
|
+
|
|
5404
|
+
|
|
5405
|
+
|
|
5406
|
+
|
|
5013
5407
|
|
|
5014
5408
|
|
|
5015
5409
|
|
|
@@ -5077,12 +5471,11 @@ var BST = class extends BinaryTree {
|
|
|
5077
5471
|
*/
|
|
5078
5472
|
deleteWhere(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
|
|
5079
5473
|
const toDelete = this.search(keyNodeEntryOrPredicate, onlyOne, (node) => node, startNode, iterationType);
|
|
5080
|
-
let
|
|
5474
|
+
let deleted = false;
|
|
5081
5475
|
for (const node of toDelete) {
|
|
5082
|
-
|
|
5083
|
-
results = results.concat(deleteInfo);
|
|
5476
|
+
if (this.delete(node)) deleted = true;
|
|
5084
5477
|
}
|
|
5085
|
-
return
|
|
5478
|
+
return deleted;
|
|
5086
5479
|
}
|
|
5087
5480
|
/**
|
|
5088
5481
|
* (Protected) Creates the default comparator function for keys that don't have a custom comparator.
|
|
@@ -5099,13 +5492,11 @@ var BST = class extends BinaryTree {
|
|
|
5099
5492
|
if (a instanceof Date && b instanceof Date) {
|
|
5100
5493
|
const ta = a.getTime();
|
|
5101
5494
|
const tb = b.getTime();
|
|
5102
|
-
if (Number.isNaN(ta) || Number.isNaN(tb))
|
|
5495
|
+
if (Number.isNaN(ta) || Number.isNaN(tb)) raise(TypeError, ERR.invalidDate("BST"));
|
|
5103
5496
|
return ta > tb ? 1 : ta < tb ? -1 : 0;
|
|
5104
5497
|
}
|
|
5105
5498
|
if (typeof a === "object" || typeof b === "object") {
|
|
5106
|
-
|
|
5107
|
-
ERR.comparatorRequired("BST")
|
|
5108
|
-
);
|
|
5499
|
+
raise(TypeError, ERR.comparatorRequired("BST"));
|
|
5109
5500
|
}
|
|
5110
5501
|
return 0;
|
|
5111
5502
|
};
|
|
@@ -5424,7 +5815,8 @@ var BST = class extends BinaryTree {
|
|
|
5424
5815
|
_snapshotOptions() {
|
|
5425
5816
|
return {
|
|
5426
5817
|
...super._snapshotOptions(),
|
|
5427
|
-
comparator: this._comparator
|
|
5818
|
+
comparator: this._comparator,
|
|
5819
|
+
enableOrderStatistic: this._enableOrderStatistic
|
|
5428
5820
|
};
|
|
5429
5821
|
}
|
|
5430
5822
|
/**
|
|
@@ -5446,6 +5838,113 @@ var BST = class extends BinaryTree {
|
|
|
5446
5838
|
*
|
|
5447
5839
|
* @param v - The node to set as root.
|
|
5448
5840
|
*/
|
|
5841
|
+
/**
|
|
5842
|
+
* (Protected) Recalculates the subtree count for a single node.
|
|
5843
|
+
* @remarks Time O(1). Only active when enableOrderStatistic is true.
|
|
5844
|
+
*/
|
|
5845
|
+
_updateCount(node) {
|
|
5846
|
+
if (!this._enableOrderStatistic) return;
|
|
5847
|
+
node._count = 1 + (this.isRealNode(node.left) ? node.left._count : 0) + (this.isRealNode(node.right) ? node.right._count : 0);
|
|
5848
|
+
}
|
|
5849
|
+
/**
|
|
5850
|
+
* (Protected) Updates subtree counts from a node up to the root.
|
|
5851
|
+
* @remarks Time O(log n). Only active when enableOrderStatistic is true.
|
|
5852
|
+
*/
|
|
5853
|
+
_updateCountAlongPath(node) {
|
|
5854
|
+
if (!this._enableOrderStatistic) return;
|
|
5855
|
+
let current = node;
|
|
5856
|
+
while (current) {
|
|
5857
|
+
this._updateCount(current);
|
|
5858
|
+
current = current.parent;
|
|
5859
|
+
}
|
|
5860
|
+
}
|
|
5861
|
+
/**
|
|
5862
|
+
* (Protected) Finds the node at position k in tree order (iterative).
|
|
5863
|
+
* @remarks Time O(log n), Space O(1)
|
|
5864
|
+
*/
|
|
5865
|
+
_getByRankIterative(node, k) {
|
|
5866
|
+
let current = node;
|
|
5867
|
+
let remaining = k;
|
|
5868
|
+
while (current) {
|
|
5869
|
+
const leftCount = this.isRealNode(current.left) ? current.left._count : 0;
|
|
5870
|
+
if (remaining < leftCount) {
|
|
5871
|
+
current = current.left;
|
|
5872
|
+
} else if (remaining === leftCount) {
|
|
5873
|
+
return current;
|
|
5874
|
+
} else {
|
|
5875
|
+
remaining = remaining - leftCount - 1;
|
|
5876
|
+
current = current.right;
|
|
5877
|
+
}
|
|
5878
|
+
}
|
|
5879
|
+
return void 0;
|
|
5880
|
+
}
|
|
5881
|
+
/**
|
|
5882
|
+
* (Protected) Finds the node at position k in tree order (recursive).
|
|
5883
|
+
* @remarks Time O(log n), Space O(log n) call stack
|
|
5884
|
+
*/
|
|
5885
|
+
_getByRankRecursive(node, k) {
|
|
5886
|
+
if (!node) return void 0;
|
|
5887
|
+
const leftCount = this.isRealNode(node.left) ? node.left._count : 0;
|
|
5888
|
+
if (k < leftCount) return this._getByRankRecursive(node.left, k);
|
|
5889
|
+
if (k === leftCount) return node;
|
|
5890
|
+
return this._getByRankRecursive(node.right, k - leftCount - 1);
|
|
5891
|
+
}
|
|
5892
|
+
/**
|
|
5893
|
+
* (Protected) Computes the rank of a key iteratively.
|
|
5894
|
+
* @remarks Time O(log n), Space O(1)
|
|
5895
|
+
*/
|
|
5896
|
+
_getRankIterative(node, key) {
|
|
5897
|
+
let rank = 0;
|
|
5898
|
+
let current = node;
|
|
5899
|
+
while (this.isRealNode(current)) {
|
|
5900
|
+
const cmp = this._compare(current.key, key);
|
|
5901
|
+
if (cmp > 0) {
|
|
5902
|
+
current = current.left;
|
|
5903
|
+
} else if (cmp < 0) {
|
|
5904
|
+
rank += (this.isRealNode(current.left) ? current.left._count : 0) + 1;
|
|
5905
|
+
current = current.right;
|
|
5906
|
+
} else {
|
|
5907
|
+
rank += this.isRealNode(current.left) ? current.left._count : 0;
|
|
5908
|
+
return rank;
|
|
5909
|
+
}
|
|
5910
|
+
}
|
|
5911
|
+
return rank;
|
|
5912
|
+
}
|
|
5913
|
+
/**
|
|
5914
|
+
* (Protected) Computes the rank of a key recursively.
|
|
5915
|
+
* @remarks Time O(log n), Space O(log n) call stack
|
|
5916
|
+
*/
|
|
5917
|
+
_getRankRecursive(node, key) {
|
|
5918
|
+
if (!node) return 0;
|
|
5919
|
+
const cmp = this._compare(node.key, key);
|
|
5920
|
+
if (cmp > 0) {
|
|
5921
|
+
return this._getRankRecursive(node.left, key);
|
|
5922
|
+
} else if (cmp < 0) {
|
|
5923
|
+
return (this.isRealNode(node.left) ? node.left._count : 0) + 1 + this._getRankRecursive(node.right, key);
|
|
5924
|
+
} else {
|
|
5925
|
+
return this.isRealNode(node.left) ? node.left._count : 0;
|
|
5926
|
+
}
|
|
5927
|
+
}
|
|
5928
|
+
/**
|
|
5929
|
+
* (Protected) Finds the in-order successor of a node.
|
|
5930
|
+
* @remarks Time O(log n), Space O(1)
|
|
5931
|
+
*/
|
|
5932
|
+
_next(node) {
|
|
5933
|
+
if (this.isRealNode(node.right)) {
|
|
5934
|
+
let current2 = node.right;
|
|
5935
|
+
while (this.isRealNode(current2.left)) {
|
|
5936
|
+
current2 = current2.left;
|
|
5937
|
+
}
|
|
5938
|
+
return current2;
|
|
5939
|
+
}
|
|
5940
|
+
let current = node;
|
|
5941
|
+
let parent = current.parent;
|
|
5942
|
+
while (parent && current === parent.right) {
|
|
5943
|
+
current = parent;
|
|
5944
|
+
parent = parent.parent;
|
|
5945
|
+
}
|
|
5946
|
+
return parent;
|
|
5947
|
+
}
|
|
5449
5948
|
_setRoot(v) {
|
|
5450
5949
|
if (v) v.parent = void 0;
|
|
5451
5950
|
this._root = v;
|
|
@@ -5492,21 +5991,28 @@ var BST = class extends BinaryTree {
|
|
|
5492
5991
|
while (x.left !== void 0 && x.left !== null) x = x.left;
|
|
5493
5992
|
return x;
|
|
5494
5993
|
}, "minNode");
|
|
5994
|
+
let countUpdateStart;
|
|
5495
5995
|
if (node.left === void 0) {
|
|
5996
|
+
countUpdateStart = node.parent;
|
|
5496
5997
|
transplant(node, node.right);
|
|
5497
5998
|
} else if (node.right === void 0) {
|
|
5999
|
+
countUpdateStart = node.parent;
|
|
5498
6000
|
transplant(node, node.left);
|
|
5499
6001
|
} else {
|
|
5500
6002
|
const succ = minNode(node.right);
|
|
5501
6003
|
if (succ.parent !== node) {
|
|
6004
|
+
countUpdateStart = succ.parent;
|
|
5502
6005
|
transplant(succ, succ.right);
|
|
5503
6006
|
succ.right = node.right;
|
|
5504
6007
|
if (succ.right) succ.right.parent = succ;
|
|
6008
|
+
} else {
|
|
6009
|
+
countUpdateStart = succ;
|
|
5505
6010
|
}
|
|
5506
6011
|
transplant(node, succ);
|
|
5507
6012
|
succ.left = node.left;
|
|
5508
6013
|
if (succ.left) succ.left.parent = succ;
|
|
5509
6014
|
}
|
|
6015
|
+
this._updateCountAlongPath(countUpdateStart);
|
|
5510
6016
|
this._size = Math.max(0, this._size - 1);
|
|
5511
6017
|
return true;
|
|
5512
6018
|
}
|
|
@@ -5526,5 +6032,6 @@ exports.BinaryTreeNode = BinaryTreeNode;
|
|
|
5526
6032
|
exports.DFSOperation = DFSOperation;
|
|
5527
6033
|
exports.ERR = ERR;
|
|
5528
6034
|
exports.Range = Range;
|
|
6035
|
+
exports.raise = raise;
|
|
5529
6036
|
//# sourceMappingURL=index.cjs.map
|
|
5530
6037
|
//# sourceMappingURL=index.cjs.map
|