bst-typed 2.5.1 → 2.5.2
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 +382 -57
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +382 -56
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +382 -58
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +382 -57
- 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 +36 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +42 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +77 -2
- package/dist/types/data-structures/binary-tree/bst.d.ts +171 -0
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +57 -0
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +18 -0
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +409 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +411 -6
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +339 -6
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +391 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +30 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +27 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +33 -0
- package/dist/types/data-structures/heap/heap.d.ts +42 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +51 -0
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +45 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +54 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +24 -0
- package/dist/types/data-structures/queue/deque.d.ts +45 -0
- package/dist/types/data-structures/queue/queue.d.ts +36 -0
- package/dist/types/data-structures/stack/stack.d.ts +30 -0
- package/dist/types/data-structures/trie/trie.d.ts +36 -0
- 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 +380 -55
- 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 +47 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +46 -4
- package/src/data-structures/binary-tree/binary-tree.ts +79 -4
- package/src/data-structures/binary-tree/bst.ts +441 -6
- package/src/data-structures/binary-tree/red-black-tree.ts +73 -0
- package/src/data-structures/binary-tree/segment-tree.ts +18 -0
- package/src/data-structures/binary-tree/tree-map.ts +434 -9
- package/src/data-structures/binary-tree/tree-multi-map.ts +426 -5
- package/src/data-structures/binary-tree/tree-multi-set.ts +350 -6
- package/src/data-structures/binary-tree/tree-set.ts +410 -8
- package/src/data-structures/graph/abstract-graph.ts +2 -2
- package/src/data-structures/graph/directed-graph.ts +30 -0
- package/src/data-structures/graph/undirected-graph.ts +27 -0
- package/src/data-structures/hash/hash-map.ts +35 -4
- package/src/data-structures/heap/heap.ts +46 -4
- package/src/data-structures/heap/max-heap.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +51 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +45 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +59 -5
- package/src/data-structures/matrix/matrix.ts +33 -9
- package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
- package/src/data-structures/queue/deque.ts +45 -0
- package/src/data-structures/queue/queue.ts +36 -0
- package/src/data-structures/stack/stack.ts +30 -0
- package/src/data-structures/trie/trie.ts +38 -2
- 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 {
|
|
@@ -794,6 +800,9 @@ var Queue = class _Queue extends LinearBase {
|
|
|
794
800
|
|
|
795
801
|
|
|
796
802
|
|
|
803
|
+
|
|
804
|
+
|
|
805
|
+
|
|
797
806
|
|
|
798
807
|
|
|
799
808
|
|
|
@@ -841,6 +850,9 @@ var Queue = class _Queue extends LinearBase {
|
|
|
841
850
|
|
|
842
851
|
|
|
843
852
|
|
|
853
|
+
|
|
854
|
+
|
|
855
|
+
|
|
844
856
|
|
|
845
857
|
|
|
846
858
|
|
|
@@ -904,6 +916,9 @@ var Queue = class _Queue extends LinearBase {
|
|
|
904
916
|
|
|
905
917
|
|
|
906
918
|
|
|
919
|
+
|
|
920
|
+
|
|
921
|
+
|
|
907
922
|
|
|
908
923
|
|
|
909
924
|
|
|
@@ -963,6 +978,9 @@ var Queue = class _Queue extends LinearBase {
|
|
|
963
978
|
|
|
964
979
|
|
|
965
980
|
|
|
981
|
+
|
|
982
|
+
|
|
983
|
+
|
|
966
984
|
|
|
967
985
|
|
|
968
986
|
|
|
@@ -1029,6 +1047,9 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1029
1047
|
|
|
1030
1048
|
|
|
1031
1049
|
|
|
1050
|
+
|
|
1051
|
+
|
|
1052
|
+
|
|
1032
1053
|
|
|
1033
1054
|
|
|
1034
1055
|
|
|
@@ -1085,6 +1106,9 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1085
1106
|
|
|
1086
1107
|
|
|
1087
1108
|
|
|
1109
|
+
|
|
1110
|
+
|
|
1111
|
+
|
|
1088
1112
|
|
|
1089
1113
|
|
|
1090
1114
|
|
|
@@ -1134,6 +1158,9 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1134
1158
|
|
|
1135
1159
|
|
|
1136
1160
|
|
|
1161
|
+
|
|
1162
|
+
|
|
1163
|
+
|
|
1137
1164
|
|
|
1138
1165
|
|
|
1139
1166
|
|
|
@@ -1224,6 +1251,9 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1224
1251
|
|
|
1225
1252
|
|
|
1226
1253
|
|
|
1254
|
+
|
|
1255
|
+
|
|
1256
|
+
|
|
1227
1257
|
|
|
1228
1258
|
|
|
1229
1259
|
|
|
@@ -1267,6 +1297,9 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1267
1297
|
|
|
1268
1298
|
|
|
1269
1299
|
|
|
1300
|
+
|
|
1301
|
+
|
|
1302
|
+
|
|
1270
1303
|
|
|
1271
1304
|
|
|
1272
1305
|
|
|
@@ -1333,6 +1366,9 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1333
1366
|
|
|
1334
1367
|
|
|
1335
1368
|
|
|
1369
|
+
|
|
1370
|
+
|
|
1371
|
+
|
|
1336
1372
|
|
|
1337
1373
|
|
|
1338
1374
|
|
|
@@ -1383,6 +1419,9 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1383
1419
|
|
|
1384
1420
|
|
|
1385
1421
|
|
|
1422
|
+
|
|
1423
|
+
|
|
1424
|
+
|
|
1386
1425
|
|
|
1387
1426
|
|
|
1388
1427
|
|
|
@@ -1437,6 +1476,9 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1437
1476
|
|
|
1438
1477
|
|
|
1439
1478
|
|
|
1479
|
+
|
|
1480
|
+
|
|
1481
|
+
|
|
1440
1482
|
|
|
1441
1483
|
|
|
1442
1484
|
|
|
@@ -1691,7 +1733,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1691
1733
|
if (isMapMode !== void 0) this._isMapMode = isMapMode;
|
|
1692
1734
|
if (isDuplicate !== void 0) this._isDuplicate = isDuplicate;
|
|
1693
1735
|
if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
|
|
1694
|
-
else if (toEntryFn)
|
|
1736
|
+
else if (toEntryFn) raise(TypeError, ERR.notAFunction("toEntryFn", "BinaryTree"));
|
|
1695
1737
|
}
|
|
1696
1738
|
if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
|
|
1697
1739
|
}
|
|
@@ -1934,6 +1976,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1934
1976
|
|
|
1935
1977
|
|
|
1936
1978
|
|
|
1979
|
+
|
|
1980
|
+
|
|
1981
|
+
|
|
1937
1982
|
|
|
1938
1983
|
|
|
1939
1984
|
|
|
@@ -1985,6 +2030,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1985
2030
|
|
|
1986
2031
|
|
|
1987
2032
|
|
|
2033
|
+
|
|
2034
|
+
|
|
2035
|
+
|
|
1988
2036
|
|
|
1989
2037
|
|
|
1990
2038
|
|
|
@@ -2088,6 +2136,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2088
2136
|
|
|
2089
2137
|
|
|
2090
2138
|
|
|
2139
|
+
|
|
2140
|
+
|
|
2141
|
+
|
|
2091
2142
|
|
|
2092
2143
|
|
|
2093
2144
|
|
|
@@ -2127,6 +2178,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2127
2178
|
|
|
2128
2179
|
|
|
2129
2180
|
|
|
2181
|
+
|
|
2182
|
+
|
|
2183
|
+
|
|
2130
2184
|
|
|
2131
2185
|
|
|
2132
2186
|
|
|
@@ -2187,6 +2241,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2187
2241
|
|
|
2188
2242
|
|
|
2189
2243
|
|
|
2244
|
+
|
|
2245
|
+
|
|
2246
|
+
|
|
2190
2247
|
|
|
2191
2248
|
|
|
2192
2249
|
|
|
@@ -2246,6 +2303,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2246
2303
|
|
|
2247
2304
|
|
|
2248
2305
|
|
|
2306
|
+
|
|
2307
|
+
|
|
2308
|
+
|
|
2249
2309
|
|
|
2250
2310
|
|
|
2251
2311
|
|
|
@@ -2383,6 +2443,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2383
2443
|
|
|
2384
2444
|
|
|
2385
2445
|
|
|
2446
|
+
|
|
2447
|
+
|
|
2448
|
+
|
|
2386
2449
|
|
|
2387
2450
|
|
|
2388
2451
|
|
|
@@ -2438,6 +2501,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2438
2501
|
|
|
2439
2502
|
|
|
2440
2503
|
|
|
2504
|
+
|
|
2505
|
+
|
|
2506
|
+
|
|
2441
2507
|
|
|
2442
2508
|
|
|
2443
2509
|
|
|
@@ -2495,6 +2561,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2495
2561
|
|
|
2496
2562
|
|
|
2497
2563
|
|
|
2564
|
+
|
|
2565
|
+
|
|
2566
|
+
|
|
2498
2567
|
|
|
2499
2568
|
|
|
2500
2569
|
|
|
@@ -2540,6 +2609,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2540
2609
|
|
|
2541
2610
|
|
|
2542
2611
|
|
|
2612
|
+
|
|
2613
|
+
|
|
2614
|
+
|
|
2543
2615
|
|
|
2544
2616
|
|
|
2545
2617
|
|
|
@@ -2594,6 +2666,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2594
2666
|
|
|
2595
2667
|
|
|
2596
2668
|
|
|
2669
|
+
|
|
2670
|
+
|
|
2671
|
+
|
|
2597
2672
|
|
|
2598
2673
|
|
|
2599
2674
|
|
|
@@ -2675,6 +2750,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2675
2750
|
|
|
2676
2751
|
|
|
2677
2752
|
|
|
2753
|
+
|
|
2754
|
+
|
|
2755
|
+
|
|
2678
2756
|
|
|
2679
2757
|
|
|
2680
2758
|
|
|
@@ -2733,6 +2811,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2733
2811
|
|
|
2734
2812
|
|
|
2735
2813
|
|
|
2814
|
+
|
|
2815
|
+
|
|
2816
|
+
|
|
2736
2817
|
|
|
2737
2818
|
|
|
2738
2819
|
|
|
@@ -3207,6 +3288,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3207
3288
|
|
|
3208
3289
|
|
|
3209
3290
|
|
|
3291
|
+
|
|
3292
|
+
|
|
3293
|
+
|
|
3210
3294
|
|
|
3211
3295
|
|
|
3212
3296
|
|
|
@@ -3256,6 +3340,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3256
3340
|
|
|
3257
3341
|
|
|
3258
3342
|
|
|
3343
|
+
|
|
3344
|
+
|
|
3345
|
+
|
|
3259
3346
|
|
|
3260
3347
|
|
|
3261
3348
|
|
|
@@ -3309,6 +3396,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3309
3396
|
|
|
3310
3397
|
|
|
3311
3398
|
|
|
3399
|
+
|
|
3400
|
+
|
|
3401
|
+
|
|
3312
3402
|
|
|
3313
3403
|
|
|
3314
3404
|
|
|
@@ -3387,6 +3477,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3387
3477
|
|
|
3388
3478
|
|
|
3389
3479
|
|
|
3480
|
+
|
|
3481
|
+
|
|
3482
|
+
|
|
3390
3483
|
|
|
3391
3484
|
|
|
3392
3485
|
|
|
@@ -4022,12 +4115,16 @@ var BST = class extends BinaryTree {
|
|
|
4022
4115
|
} else {
|
|
4023
4116
|
this._comparator = this._createDefaultComparator();
|
|
4024
4117
|
}
|
|
4118
|
+
if (options.enableOrderStatistic) {
|
|
4119
|
+
this._enableOrderStatistic = true;
|
|
4120
|
+
}
|
|
4025
4121
|
} else {
|
|
4026
4122
|
this._comparator = this._createDefaultComparator();
|
|
4027
4123
|
}
|
|
4028
4124
|
if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
|
|
4029
4125
|
}
|
|
4030
4126
|
_root = void 0;
|
|
4127
|
+
_enableOrderStatistic = false;
|
|
4031
4128
|
/**
|
|
4032
4129
|
* Gets the root node of the tree.
|
|
4033
4130
|
* @remarks Time O(1)
|
|
@@ -4197,6 +4294,12 @@ var BST = class extends BinaryTree {
|
|
|
4197
4294
|
|
|
4198
4295
|
|
|
4199
4296
|
|
|
4297
|
+
|
|
4298
|
+
|
|
4299
|
+
|
|
4300
|
+
|
|
4301
|
+
|
|
4302
|
+
|
|
4200
4303
|
|
|
4201
4304
|
|
|
4202
4305
|
|
|
@@ -4358,6 +4461,84 @@ var BST = class extends BinaryTree {
|
|
|
4358
4461
|
const searchRange = range instanceof Range ? range : new Range(range[0], range[1]);
|
|
4359
4462
|
return this.search(searchRange, false, callback, startNode, iterationType);
|
|
4360
4463
|
}
|
|
4464
|
+
getByRank(k, callback = this._DEFAULT_NODE_CALLBACK, iterationType = this.iterationType) {
|
|
4465
|
+
if (!this._enableOrderStatistic) {
|
|
4466
|
+
raise(Error, ERR.orderStatisticNotEnabled("getByRank"));
|
|
4467
|
+
}
|
|
4468
|
+
if (k < 0 || k >= this._size) return void 0;
|
|
4469
|
+
let actualCallback = void 0;
|
|
4470
|
+
let actualIterationType = this.iterationType;
|
|
4471
|
+
if (typeof callback === "string") {
|
|
4472
|
+
actualIterationType = callback;
|
|
4473
|
+
} else if (callback) {
|
|
4474
|
+
actualCallback = callback;
|
|
4475
|
+
if (iterationType) {
|
|
4476
|
+
actualIterationType = iterationType;
|
|
4477
|
+
}
|
|
4478
|
+
}
|
|
4479
|
+
const node = actualIterationType === "RECURSIVE" ? this._getByRankRecursive(this._root, k) : this._getByRankIterative(this._root, k);
|
|
4480
|
+
if (!node) return void 0;
|
|
4481
|
+
return actualCallback ? actualCallback(node) : node.key;
|
|
4482
|
+
}
|
|
4483
|
+
getRank(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
4484
|
+
if (!this._enableOrderStatistic) {
|
|
4485
|
+
raise(Error, ERR.orderStatisticNotEnabled("getRank"));
|
|
4486
|
+
}
|
|
4487
|
+
if (!this._root || this._size === 0) return -1;
|
|
4488
|
+
let actualIterationType = this.iterationType;
|
|
4489
|
+
if (iterationType) actualIterationType = iterationType;
|
|
4490
|
+
let key;
|
|
4491
|
+
if (typeof keyNodeEntryOrPredicate === "function") {
|
|
4492
|
+
const results = this.search(keyNodeEntryOrPredicate, true);
|
|
4493
|
+
if (results.length === 0 || results[0] === void 0) return -1;
|
|
4494
|
+
key = results[0];
|
|
4495
|
+
} else if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
4496
|
+
return -1;
|
|
4497
|
+
} else if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
4498
|
+
key = keyNodeEntryOrPredicate.key;
|
|
4499
|
+
} else if (Array.isArray(keyNodeEntryOrPredicate)) {
|
|
4500
|
+
key = keyNodeEntryOrPredicate[0] ?? void 0;
|
|
4501
|
+
if (key === void 0 || key === null) return -1;
|
|
4502
|
+
} else {
|
|
4503
|
+
key = keyNodeEntryOrPredicate;
|
|
4504
|
+
}
|
|
4505
|
+
if (key === void 0) return -1;
|
|
4506
|
+
return actualIterationType === "RECURSIVE" ? this._getRankRecursive(this._root, key) : this._getRankIterative(this._root, key);
|
|
4507
|
+
}
|
|
4508
|
+
rangeByRank(start, end, callback = this._DEFAULT_NODE_CALLBACK, iterationType = this.iterationType) {
|
|
4509
|
+
if (!this._enableOrderStatistic) {
|
|
4510
|
+
raise(Error, ERR.orderStatisticNotEnabled("rangeByRank"));
|
|
4511
|
+
}
|
|
4512
|
+
if (this._size === 0) return [];
|
|
4513
|
+
const lo = Math.max(0, start);
|
|
4514
|
+
const hi = Math.min(this._size - 1, end);
|
|
4515
|
+
if (lo > hi) return [];
|
|
4516
|
+
let actualCallback = void 0;
|
|
4517
|
+
let actualIterationType = this.iterationType;
|
|
4518
|
+
if (typeof callback === "string") {
|
|
4519
|
+
actualIterationType = callback;
|
|
4520
|
+
} else if (callback) {
|
|
4521
|
+
actualCallback = callback;
|
|
4522
|
+
if (iterationType) {
|
|
4523
|
+
actualIterationType = iterationType;
|
|
4524
|
+
}
|
|
4525
|
+
}
|
|
4526
|
+
const results = [];
|
|
4527
|
+
const count = hi - lo + 1;
|
|
4528
|
+
const startNode = actualIterationType === "RECURSIVE" ? this._getByRankRecursive(this._root, lo) : this._getByRankIterative(this._root, lo);
|
|
4529
|
+
if (!startNode) return [];
|
|
4530
|
+
let collected = 0;
|
|
4531
|
+
const cb = actualCallback ?? this._DEFAULT_NODE_CALLBACK;
|
|
4532
|
+
let current = startNode;
|
|
4533
|
+
while (current && collected < count) {
|
|
4534
|
+
results.push(cb(current));
|
|
4535
|
+
collected++;
|
|
4536
|
+
if (collected < count) {
|
|
4537
|
+
current = this._next(current);
|
|
4538
|
+
}
|
|
4539
|
+
}
|
|
4540
|
+
return results;
|
|
4541
|
+
}
|
|
4361
4542
|
/**
|
|
4362
4543
|
* Adds a new node to the BST based on key comparison.
|
|
4363
4544
|
* @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
|
|
@@ -4443,6 +4624,15 @@ var BST = class extends BinaryTree {
|
|
|
4443
4624
|
|
|
4444
4625
|
|
|
4445
4626
|
|
|
4627
|
+
|
|
4628
|
+
|
|
4629
|
+
|
|
4630
|
+
|
|
4631
|
+
|
|
4632
|
+
|
|
4633
|
+
|
|
4634
|
+
|
|
4635
|
+
|
|
4446
4636
|
|
|
4447
4637
|
|
|
4448
4638
|
|
|
@@ -4467,6 +4657,7 @@ var BST = class extends BinaryTree {
|
|
|
4467
4657
|
this._setRoot(newNode);
|
|
4468
4658
|
if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
|
|
4469
4659
|
this._size++;
|
|
4660
|
+
this._updateCount(newNode);
|
|
4470
4661
|
return true;
|
|
4471
4662
|
}
|
|
4472
4663
|
let current = this._root;
|
|
@@ -4480,6 +4671,7 @@ var BST = class extends BinaryTree {
|
|
|
4480
4671
|
current.left = newNode;
|
|
4481
4672
|
if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
|
|
4482
4673
|
this._size++;
|
|
4674
|
+
this._updateCountAlongPath(newNode);
|
|
4483
4675
|
return true;
|
|
4484
4676
|
}
|
|
4485
4677
|
if (current.left !== null) current = current.left;
|
|
@@ -4488,6 +4680,7 @@ var BST = class extends BinaryTree {
|
|
|
4488
4680
|
current.right = newNode;
|
|
4489
4681
|
if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
|
|
4490
4682
|
this._size++;
|
|
4683
|
+
this._updateCountAlongPath(newNode);
|
|
4491
4684
|
return true;
|
|
4492
4685
|
}
|
|
4493
4686
|
if (current.right !== null) current = current.right;
|
|
@@ -4552,6 +4745,12 @@ var BST = class extends BinaryTree {
|
|
|
4552
4745
|
|
|
4553
4746
|
|
|
4554
4747
|
|
|
4748
|
+
|
|
4749
|
+
|
|
4750
|
+
|
|
4751
|
+
|
|
4752
|
+
|
|
4753
|
+
|
|
4555
4754
|
|
|
4556
4755
|
|
|
4557
4756
|
|
|
@@ -4839,6 +5038,9 @@ var BST = class extends BinaryTree {
|
|
|
4839
5038
|
|
|
4840
5039
|
|
|
4841
5040
|
|
|
5041
|
+
|
|
5042
|
+
|
|
5043
|
+
|
|
4842
5044
|
|
|
4843
5045
|
|
|
4844
5046
|
|
|
@@ -4905,6 +5107,9 @@ var BST = class extends BinaryTree {
|
|
|
4905
5107
|
|
|
4906
5108
|
|
|
4907
5109
|
|
|
5110
|
+
|
|
5111
|
+
|
|
5112
|
+
|
|
4908
5113
|
|
|
4909
5114
|
|
|
4910
5115
|
|
|
@@ -5018,6 +5223,12 @@ var BST = class extends BinaryTree {
|
|
|
5018
5223
|
|
|
5019
5224
|
|
|
5020
5225
|
|
|
5226
|
+
|
|
5227
|
+
|
|
5228
|
+
|
|
5229
|
+
|
|
5230
|
+
|
|
5231
|
+
|
|
5021
5232
|
|
|
5022
5233
|
|
|
5023
5234
|
|
|
@@ -5099,13 +5310,11 @@ var BST = class extends BinaryTree {
|
|
|
5099
5310
|
if (a instanceof Date && b instanceof Date) {
|
|
5100
5311
|
const ta = a.getTime();
|
|
5101
5312
|
const tb = b.getTime();
|
|
5102
|
-
if (Number.isNaN(ta) || Number.isNaN(tb))
|
|
5313
|
+
if (Number.isNaN(ta) || Number.isNaN(tb)) raise(TypeError, ERR.invalidDate("BST"));
|
|
5103
5314
|
return ta > tb ? 1 : ta < tb ? -1 : 0;
|
|
5104
5315
|
}
|
|
5105
5316
|
if (typeof a === "object" || typeof b === "object") {
|
|
5106
|
-
|
|
5107
|
-
ERR.comparatorRequired("BST")
|
|
5108
|
-
);
|
|
5317
|
+
raise(TypeError, ERR.comparatorRequired("BST"));
|
|
5109
5318
|
}
|
|
5110
5319
|
return 0;
|
|
5111
5320
|
};
|
|
@@ -5424,7 +5633,8 @@ var BST = class extends BinaryTree {
|
|
|
5424
5633
|
_snapshotOptions() {
|
|
5425
5634
|
return {
|
|
5426
5635
|
...super._snapshotOptions(),
|
|
5427
|
-
comparator: this._comparator
|
|
5636
|
+
comparator: this._comparator,
|
|
5637
|
+
enableOrderStatistic: this._enableOrderStatistic
|
|
5428
5638
|
};
|
|
5429
5639
|
}
|
|
5430
5640
|
/**
|
|
@@ -5446,6 +5656,113 @@ var BST = class extends BinaryTree {
|
|
|
5446
5656
|
*
|
|
5447
5657
|
* @param v - The node to set as root.
|
|
5448
5658
|
*/
|
|
5659
|
+
/**
|
|
5660
|
+
* (Protected) Recalculates the subtree count for a single node.
|
|
5661
|
+
* @remarks Time O(1). Only active when enableOrderStatistic is true.
|
|
5662
|
+
*/
|
|
5663
|
+
_updateCount(node) {
|
|
5664
|
+
if (!this._enableOrderStatistic) return;
|
|
5665
|
+
node._count = 1 + (this.isRealNode(node.left) ? node.left._count : 0) + (this.isRealNode(node.right) ? node.right._count : 0);
|
|
5666
|
+
}
|
|
5667
|
+
/**
|
|
5668
|
+
* (Protected) Updates subtree counts from a node up to the root.
|
|
5669
|
+
* @remarks Time O(log n). Only active when enableOrderStatistic is true.
|
|
5670
|
+
*/
|
|
5671
|
+
_updateCountAlongPath(node) {
|
|
5672
|
+
if (!this._enableOrderStatistic) return;
|
|
5673
|
+
let current = node;
|
|
5674
|
+
while (current) {
|
|
5675
|
+
this._updateCount(current);
|
|
5676
|
+
current = current.parent;
|
|
5677
|
+
}
|
|
5678
|
+
}
|
|
5679
|
+
/**
|
|
5680
|
+
* (Protected) Finds the node at position k in tree order (iterative).
|
|
5681
|
+
* @remarks Time O(log n), Space O(1)
|
|
5682
|
+
*/
|
|
5683
|
+
_getByRankIterative(node, k) {
|
|
5684
|
+
let current = node;
|
|
5685
|
+
let remaining = k;
|
|
5686
|
+
while (current) {
|
|
5687
|
+
const leftCount = this.isRealNode(current.left) ? current.left._count : 0;
|
|
5688
|
+
if (remaining < leftCount) {
|
|
5689
|
+
current = current.left;
|
|
5690
|
+
} else if (remaining === leftCount) {
|
|
5691
|
+
return current;
|
|
5692
|
+
} else {
|
|
5693
|
+
remaining = remaining - leftCount - 1;
|
|
5694
|
+
current = current.right;
|
|
5695
|
+
}
|
|
5696
|
+
}
|
|
5697
|
+
return void 0;
|
|
5698
|
+
}
|
|
5699
|
+
/**
|
|
5700
|
+
* (Protected) Finds the node at position k in tree order (recursive).
|
|
5701
|
+
* @remarks Time O(log n), Space O(log n) call stack
|
|
5702
|
+
*/
|
|
5703
|
+
_getByRankRecursive(node, k) {
|
|
5704
|
+
if (!node) return void 0;
|
|
5705
|
+
const leftCount = this.isRealNode(node.left) ? node.left._count : 0;
|
|
5706
|
+
if (k < leftCount) return this._getByRankRecursive(node.left, k);
|
|
5707
|
+
if (k === leftCount) return node;
|
|
5708
|
+
return this._getByRankRecursive(node.right, k - leftCount - 1);
|
|
5709
|
+
}
|
|
5710
|
+
/**
|
|
5711
|
+
* (Protected) Computes the rank of a key iteratively.
|
|
5712
|
+
* @remarks Time O(log n), Space O(1)
|
|
5713
|
+
*/
|
|
5714
|
+
_getRankIterative(node, key) {
|
|
5715
|
+
let rank = 0;
|
|
5716
|
+
let current = node;
|
|
5717
|
+
while (this.isRealNode(current)) {
|
|
5718
|
+
const cmp = this._compare(current.key, key);
|
|
5719
|
+
if (cmp > 0) {
|
|
5720
|
+
current = current.left;
|
|
5721
|
+
} else if (cmp < 0) {
|
|
5722
|
+
rank += (this.isRealNode(current.left) ? current.left._count : 0) + 1;
|
|
5723
|
+
current = current.right;
|
|
5724
|
+
} else {
|
|
5725
|
+
rank += this.isRealNode(current.left) ? current.left._count : 0;
|
|
5726
|
+
return rank;
|
|
5727
|
+
}
|
|
5728
|
+
}
|
|
5729
|
+
return rank;
|
|
5730
|
+
}
|
|
5731
|
+
/**
|
|
5732
|
+
* (Protected) Computes the rank of a key recursively.
|
|
5733
|
+
* @remarks Time O(log n), Space O(log n) call stack
|
|
5734
|
+
*/
|
|
5735
|
+
_getRankRecursive(node, key) {
|
|
5736
|
+
if (!node) return 0;
|
|
5737
|
+
const cmp = this._compare(node.key, key);
|
|
5738
|
+
if (cmp > 0) {
|
|
5739
|
+
return this._getRankRecursive(node.left, key);
|
|
5740
|
+
} else if (cmp < 0) {
|
|
5741
|
+
return (this.isRealNode(node.left) ? node.left._count : 0) + 1 + this._getRankRecursive(node.right, key);
|
|
5742
|
+
} else {
|
|
5743
|
+
return this.isRealNode(node.left) ? node.left._count : 0;
|
|
5744
|
+
}
|
|
5745
|
+
}
|
|
5746
|
+
/**
|
|
5747
|
+
* (Protected) Finds the in-order successor of a node.
|
|
5748
|
+
* @remarks Time O(log n), Space O(1)
|
|
5749
|
+
*/
|
|
5750
|
+
_next(node) {
|
|
5751
|
+
if (this.isRealNode(node.right)) {
|
|
5752
|
+
let current2 = node.right;
|
|
5753
|
+
while (this.isRealNode(current2.left)) {
|
|
5754
|
+
current2 = current2.left;
|
|
5755
|
+
}
|
|
5756
|
+
return current2;
|
|
5757
|
+
}
|
|
5758
|
+
let current = node;
|
|
5759
|
+
let parent = current.parent;
|
|
5760
|
+
while (parent && current === parent.right) {
|
|
5761
|
+
current = parent;
|
|
5762
|
+
parent = parent.parent;
|
|
5763
|
+
}
|
|
5764
|
+
return parent;
|
|
5765
|
+
}
|
|
5449
5766
|
_setRoot(v) {
|
|
5450
5767
|
if (v) v.parent = void 0;
|
|
5451
5768
|
this._root = v;
|
|
@@ -5492,21 +5809,28 @@ var BST = class extends BinaryTree {
|
|
|
5492
5809
|
while (x.left !== void 0 && x.left !== null) x = x.left;
|
|
5493
5810
|
return x;
|
|
5494
5811
|
}, "minNode");
|
|
5812
|
+
let countUpdateStart;
|
|
5495
5813
|
if (node.left === void 0) {
|
|
5814
|
+
countUpdateStart = node.parent;
|
|
5496
5815
|
transplant(node, node.right);
|
|
5497
5816
|
} else if (node.right === void 0) {
|
|
5817
|
+
countUpdateStart = node.parent;
|
|
5498
5818
|
transplant(node, node.left);
|
|
5499
5819
|
} else {
|
|
5500
5820
|
const succ = minNode(node.right);
|
|
5501
5821
|
if (succ.parent !== node) {
|
|
5822
|
+
countUpdateStart = succ.parent;
|
|
5502
5823
|
transplant(succ, succ.right);
|
|
5503
5824
|
succ.right = node.right;
|
|
5504
5825
|
if (succ.right) succ.right.parent = succ;
|
|
5826
|
+
} else {
|
|
5827
|
+
countUpdateStart = succ;
|
|
5505
5828
|
}
|
|
5506
5829
|
transplant(node, succ);
|
|
5507
5830
|
succ.left = node.left;
|
|
5508
5831
|
if (succ.left) succ.left.parent = succ;
|
|
5509
5832
|
}
|
|
5833
|
+
this._updateCountAlongPath(countUpdateStart);
|
|
5510
5834
|
this._size = Math.max(0, this._size - 1);
|
|
5511
5835
|
return true;
|
|
5512
5836
|
}
|
|
@@ -5526,5 +5850,6 @@ exports.BinaryTreeNode = BinaryTreeNode;
|
|
|
5526
5850
|
exports.DFSOperation = DFSOperation;
|
|
5527
5851
|
exports.ERR = ERR;
|
|
5528
5852
|
exports.Range = Range;
|
|
5853
|
+
exports.raise = raise;
|
|
5529
5854
|
//# sourceMappingURL=index.cjs.map
|
|
5530
5855
|
//# sourceMappingURL=index.cjs.map
|