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/esm/index.mjs
CHANGED
|
@@ -57,6 +57,61 @@ function makeTrampoline(fn) {
|
|
|
57
57
|
}
|
|
58
58
|
__name(makeTrampoline, "makeTrampoline");
|
|
59
59
|
|
|
60
|
+
// src/common/error.ts
|
|
61
|
+
function raise(ErrorClass, message) {
|
|
62
|
+
throw new ErrorClass(message);
|
|
63
|
+
}
|
|
64
|
+
__name(raise, "raise");
|
|
65
|
+
var ERR = {
|
|
66
|
+
// Range / index
|
|
67
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
68
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
69
|
+
// Type / argument
|
|
70
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
71
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
72
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
73
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
74
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
75
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
76
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
77
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
78
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
79
|
+
// State / operation
|
|
80
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
81
|
+
// Matrix
|
|
82
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
83
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
84
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
85
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
86
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch"),
|
|
87
|
+
// Order statistic
|
|
88
|
+
orderStatisticNotEnabled: /* @__PURE__ */ __name((method, ctx) => `${ctx ? ctx + ": " : ""}${method}() requires enableOrderStatistic: true.`, "orderStatisticNotEnabled")
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// src/common/index.ts
|
|
92
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
93
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
94
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
95
|
+
return DFSOperation2;
|
|
96
|
+
})(DFSOperation || {});
|
|
97
|
+
var Range = class {
|
|
98
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
99
|
+
this.low = low;
|
|
100
|
+
this.high = high;
|
|
101
|
+
this.includeLow = includeLow;
|
|
102
|
+
this.includeHigh = includeHigh;
|
|
103
|
+
}
|
|
104
|
+
static {
|
|
105
|
+
__name(this, "Range");
|
|
106
|
+
}
|
|
107
|
+
// Determine whether a key is within the range
|
|
108
|
+
isInRange(key, comparator) {
|
|
109
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
110
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
111
|
+
return lowCheck && highCheck;
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
|
|
60
115
|
// src/data-structures/base/iterable-element-base.ts
|
|
61
116
|
var IterableElementBase = class {
|
|
62
117
|
static {
|
|
@@ -75,7 +130,7 @@ var IterableElementBase = class {
|
|
|
75
130
|
if (options) {
|
|
76
131
|
const { toElementFn } = options;
|
|
77
132
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
78
|
-
else if (toElementFn)
|
|
133
|
+
else if (toElementFn) raise(TypeError, "toElementFn must be a function type");
|
|
79
134
|
}
|
|
80
135
|
}
|
|
81
136
|
/**
|
|
@@ -238,7 +293,7 @@ var IterableElementBase = class {
|
|
|
238
293
|
acc = initialValue;
|
|
239
294
|
} else {
|
|
240
295
|
const first = iter.next();
|
|
241
|
-
if (first.done)
|
|
296
|
+
if (first.done) raise(TypeError, "Reduce of empty structure with no initial value");
|
|
242
297
|
acc = first.value;
|
|
243
298
|
index = 1;
|
|
244
299
|
}
|
|
@@ -473,55 +528,6 @@ var LinearBase = class _LinearBase extends IterableElementBase {
|
|
|
473
528
|
}
|
|
474
529
|
};
|
|
475
530
|
|
|
476
|
-
// src/common/error.ts
|
|
477
|
-
var ERR = {
|
|
478
|
-
// Range / index
|
|
479
|
-
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
480
|
-
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
481
|
-
// Type / argument
|
|
482
|
-
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
483
|
-
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
484
|
-
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
485
|
-
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
486
|
-
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
487
|
-
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
488
|
-
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
489
|
-
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
490
|
-
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
491
|
-
// State / operation
|
|
492
|
-
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
493
|
-
// Matrix
|
|
494
|
-
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
495
|
-
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
496
|
-
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
497
|
-
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
498
|
-
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
499
|
-
};
|
|
500
|
-
|
|
501
|
-
// src/common/index.ts
|
|
502
|
-
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
503
|
-
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
504
|
-
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
505
|
-
return DFSOperation2;
|
|
506
|
-
})(DFSOperation || {});
|
|
507
|
-
var Range = class {
|
|
508
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
509
|
-
this.low = low;
|
|
510
|
-
this.high = high;
|
|
511
|
-
this.includeLow = includeLow;
|
|
512
|
-
this.includeHigh = includeHigh;
|
|
513
|
-
}
|
|
514
|
-
static {
|
|
515
|
-
__name(this, "Range");
|
|
516
|
-
}
|
|
517
|
-
// Determine whether a key is within the range
|
|
518
|
-
isInRange(key, comparator) {
|
|
519
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
520
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
521
|
-
return lowCheck && highCheck;
|
|
522
|
-
}
|
|
523
|
-
};
|
|
524
|
-
|
|
525
531
|
// src/data-structures/base/iterable-entry-base.ts
|
|
526
532
|
var IterableEntryBase = class {
|
|
527
533
|
static {
|
|
@@ -792,6 +798,9 @@ var Queue = class _Queue extends LinearBase {
|
|
|
792
798
|
|
|
793
799
|
|
|
794
800
|
|
|
801
|
+
|
|
802
|
+
|
|
803
|
+
|
|
795
804
|
|
|
796
805
|
|
|
797
806
|
|
|
@@ -839,6 +848,9 @@ var Queue = class _Queue extends LinearBase {
|
|
|
839
848
|
|
|
840
849
|
|
|
841
850
|
|
|
851
|
+
|
|
852
|
+
|
|
853
|
+
|
|
842
854
|
|
|
843
855
|
|
|
844
856
|
|
|
@@ -902,6 +914,9 @@ var Queue = class _Queue extends LinearBase {
|
|
|
902
914
|
|
|
903
915
|
|
|
904
916
|
|
|
917
|
+
|
|
918
|
+
|
|
919
|
+
|
|
905
920
|
|
|
906
921
|
|
|
907
922
|
|
|
@@ -961,6 +976,9 @@ var Queue = class _Queue extends LinearBase {
|
|
|
961
976
|
|
|
962
977
|
|
|
963
978
|
|
|
979
|
+
|
|
980
|
+
|
|
981
|
+
|
|
964
982
|
|
|
965
983
|
|
|
966
984
|
|
|
@@ -1027,6 +1045,9 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1027
1045
|
|
|
1028
1046
|
|
|
1029
1047
|
|
|
1048
|
+
|
|
1049
|
+
|
|
1050
|
+
|
|
1030
1051
|
|
|
1031
1052
|
|
|
1032
1053
|
|
|
@@ -1083,6 +1104,9 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1083
1104
|
|
|
1084
1105
|
|
|
1085
1106
|
|
|
1107
|
+
|
|
1108
|
+
|
|
1109
|
+
|
|
1086
1110
|
|
|
1087
1111
|
|
|
1088
1112
|
|
|
@@ -1132,6 +1156,9 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1132
1156
|
|
|
1133
1157
|
|
|
1134
1158
|
|
|
1159
|
+
|
|
1160
|
+
|
|
1161
|
+
|
|
1135
1162
|
|
|
1136
1163
|
|
|
1137
1164
|
|
|
@@ -1222,6 +1249,9 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1222
1249
|
|
|
1223
1250
|
|
|
1224
1251
|
|
|
1252
|
+
|
|
1253
|
+
|
|
1254
|
+
|
|
1225
1255
|
|
|
1226
1256
|
|
|
1227
1257
|
|
|
@@ -1265,6 +1295,9 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1265
1295
|
|
|
1266
1296
|
|
|
1267
1297
|
|
|
1298
|
+
|
|
1299
|
+
|
|
1300
|
+
|
|
1268
1301
|
|
|
1269
1302
|
|
|
1270
1303
|
|
|
@@ -1331,6 +1364,9 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1331
1364
|
|
|
1332
1365
|
|
|
1333
1366
|
|
|
1367
|
+
|
|
1368
|
+
|
|
1369
|
+
|
|
1334
1370
|
|
|
1335
1371
|
|
|
1336
1372
|
|
|
@@ -1381,6 +1417,9 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1381
1417
|
|
|
1382
1418
|
|
|
1383
1419
|
|
|
1420
|
+
|
|
1421
|
+
|
|
1422
|
+
|
|
1384
1423
|
|
|
1385
1424
|
|
|
1386
1425
|
|
|
@@ -1435,6 +1474,9 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1435
1474
|
|
|
1436
1475
|
|
|
1437
1476
|
|
|
1477
|
+
|
|
1478
|
+
|
|
1479
|
+
|
|
1438
1480
|
|
|
1439
1481
|
|
|
1440
1482
|
|
|
@@ -1689,7 +1731,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1689
1731
|
if (isMapMode !== void 0) this._isMapMode = isMapMode;
|
|
1690
1732
|
if (isDuplicate !== void 0) this._isDuplicate = isDuplicate;
|
|
1691
1733
|
if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
|
|
1692
|
-
else if (toEntryFn)
|
|
1734
|
+
else if (toEntryFn) raise(TypeError, ERR.notAFunction("toEntryFn", "BinaryTree"));
|
|
1693
1735
|
}
|
|
1694
1736
|
if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
|
|
1695
1737
|
}
|
|
@@ -1932,6 +1974,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1932
1974
|
|
|
1933
1975
|
|
|
1934
1976
|
|
|
1977
|
+
|
|
1978
|
+
|
|
1979
|
+
|
|
1935
1980
|
|
|
1936
1981
|
|
|
1937
1982
|
|
|
@@ -1983,6 +2028,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1983
2028
|
|
|
1984
2029
|
|
|
1985
2030
|
|
|
2031
|
+
|
|
2032
|
+
|
|
2033
|
+
|
|
1986
2034
|
|
|
1987
2035
|
|
|
1988
2036
|
|
|
@@ -2086,6 +2134,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2086
2134
|
|
|
2087
2135
|
|
|
2088
2136
|
|
|
2137
|
+
|
|
2138
|
+
|
|
2139
|
+
|
|
2089
2140
|
|
|
2090
2141
|
|
|
2091
2142
|
|
|
@@ -2125,6 +2176,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2125
2176
|
|
|
2126
2177
|
|
|
2127
2178
|
|
|
2179
|
+
|
|
2180
|
+
|
|
2181
|
+
|
|
2128
2182
|
|
|
2129
2183
|
|
|
2130
2184
|
|
|
@@ -2185,6 +2239,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2185
2239
|
|
|
2186
2240
|
|
|
2187
2241
|
|
|
2242
|
+
|
|
2243
|
+
|
|
2244
|
+
|
|
2188
2245
|
|
|
2189
2246
|
|
|
2190
2247
|
|
|
@@ -2244,6 +2301,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2244
2301
|
|
|
2245
2302
|
|
|
2246
2303
|
|
|
2304
|
+
|
|
2305
|
+
|
|
2306
|
+
|
|
2247
2307
|
|
|
2248
2308
|
|
|
2249
2309
|
|
|
@@ -2381,6 +2441,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2381
2441
|
|
|
2382
2442
|
|
|
2383
2443
|
|
|
2444
|
+
|
|
2445
|
+
|
|
2446
|
+
|
|
2384
2447
|
|
|
2385
2448
|
|
|
2386
2449
|
|
|
@@ -2436,6 +2499,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2436
2499
|
|
|
2437
2500
|
|
|
2438
2501
|
|
|
2502
|
+
|
|
2503
|
+
|
|
2504
|
+
|
|
2439
2505
|
|
|
2440
2506
|
|
|
2441
2507
|
|
|
@@ -2493,6 +2559,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2493
2559
|
|
|
2494
2560
|
|
|
2495
2561
|
|
|
2562
|
+
|
|
2563
|
+
|
|
2564
|
+
|
|
2496
2565
|
|
|
2497
2566
|
|
|
2498
2567
|
|
|
@@ -2538,6 +2607,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2538
2607
|
|
|
2539
2608
|
|
|
2540
2609
|
|
|
2610
|
+
|
|
2611
|
+
|
|
2612
|
+
|
|
2541
2613
|
|
|
2542
2614
|
|
|
2543
2615
|
|
|
@@ -2592,6 +2664,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2592
2664
|
|
|
2593
2665
|
|
|
2594
2666
|
|
|
2667
|
+
|
|
2668
|
+
|
|
2669
|
+
|
|
2595
2670
|
|
|
2596
2671
|
|
|
2597
2672
|
|
|
@@ -2673,6 +2748,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2673
2748
|
|
|
2674
2749
|
|
|
2675
2750
|
|
|
2751
|
+
|
|
2752
|
+
|
|
2753
|
+
|
|
2676
2754
|
|
|
2677
2755
|
|
|
2678
2756
|
|
|
@@ -2731,6 +2809,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2731
2809
|
|
|
2732
2810
|
|
|
2733
2811
|
|
|
2812
|
+
|
|
2813
|
+
|
|
2814
|
+
|
|
2734
2815
|
|
|
2735
2816
|
|
|
2736
2817
|
|
|
@@ -3205,6 +3286,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3205
3286
|
|
|
3206
3287
|
|
|
3207
3288
|
|
|
3289
|
+
|
|
3290
|
+
|
|
3291
|
+
|
|
3208
3292
|
|
|
3209
3293
|
|
|
3210
3294
|
|
|
@@ -3254,6 +3338,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3254
3338
|
|
|
3255
3339
|
|
|
3256
3340
|
|
|
3341
|
+
|
|
3342
|
+
|
|
3343
|
+
|
|
3257
3344
|
|
|
3258
3345
|
|
|
3259
3346
|
|
|
@@ -3307,6 +3394,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3307
3394
|
|
|
3308
3395
|
|
|
3309
3396
|
|
|
3397
|
+
|
|
3398
|
+
|
|
3399
|
+
|
|
3310
3400
|
|
|
3311
3401
|
|
|
3312
3402
|
|
|
@@ -3385,6 +3475,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3385
3475
|
|
|
3386
3476
|
|
|
3387
3477
|
|
|
3478
|
+
|
|
3479
|
+
|
|
3480
|
+
|
|
3388
3481
|
|
|
3389
3482
|
|
|
3390
3483
|
|
|
@@ -4020,12 +4113,16 @@ var BST = class extends BinaryTree {
|
|
|
4020
4113
|
} else {
|
|
4021
4114
|
this._comparator = this._createDefaultComparator();
|
|
4022
4115
|
}
|
|
4116
|
+
if (options.enableOrderStatistic) {
|
|
4117
|
+
this._enableOrderStatistic = true;
|
|
4118
|
+
}
|
|
4023
4119
|
} else {
|
|
4024
4120
|
this._comparator = this._createDefaultComparator();
|
|
4025
4121
|
}
|
|
4026
4122
|
if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
|
|
4027
4123
|
}
|
|
4028
4124
|
_root = void 0;
|
|
4125
|
+
_enableOrderStatistic = false;
|
|
4029
4126
|
/**
|
|
4030
4127
|
* Gets the root node of the tree.
|
|
4031
4128
|
* @remarks Time O(1)
|
|
@@ -4195,6 +4292,12 @@ var BST = class extends BinaryTree {
|
|
|
4195
4292
|
|
|
4196
4293
|
|
|
4197
4294
|
|
|
4295
|
+
|
|
4296
|
+
|
|
4297
|
+
|
|
4298
|
+
|
|
4299
|
+
|
|
4300
|
+
|
|
4198
4301
|
|
|
4199
4302
|
|
|
4200
4303
|
|
|
@@ -4356,6 +4459,84 @@ var BST = class extends BinaryTree {
|
|
|
4356
4459
|
const searchRange = range instanceof Range ? range : new Range(range[0], range[1]);
|
|
4357
4460
|
return this.search(searchRange, false, callback, startNode, iterationType);
|
|
4358
4461
|
}
|
|
4462
|
+
getByRank(k, callback = this._DEFAULT_NODE_CALLBACK, iterationType = this.iterationType) {
|
|
4463
|
+
if (!this._enableOrderStatistic) {
|
|
4464
|
+
raise(Error, ERR.orderStatisticNotEnabled("getByRank"));
|
|
4465
|
+
}
|
|
4466
|
+
if (k < 0 || k >= this._size) return void 0;
|
|
4467
|
+
let actualCallback = void 0;
|
|
4468
|
+
let actualIterationType = this.iterationType;
|
|
4469
|
+
if (typeof callback === "string") {
|
|
4470
|
+
actualIterationType = callback;
|
|
4471
|
+
} else if (callback) {
|
|
4472
|
+
actualCallback = callback;
|
|
4473
|
+
if (iterationType) {
|
|
4474
|
+
actualIterationType = iterationType;
|
|
4475
|
+
}
|
|
4476
|
+
}
|
|
4477
|
+
const node = actualIterationType === "RECURSIVE" ? this._getByRankRecursive(this._root, k) : this._getByRankIterative(this._root, k);
|
|
4478
|
+
if (!node) return void 0;
|
|
4479
|
+
return actualCallback ? actualCallback(node) : node.key;
|
|
4480
|
+
}
|
|
4481
|
+
getRank(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
4482
|
+
if (!this._enableOrderStatistic) {
|
|
4483
|
+
raise(Error, ERR.orderStatisticNotEnabled("getRank"));
|
|
4484
|
+
}
|
|
4485
|
+
if (!this._root || this._size === 0) return -1;
|
|
4486
|
+
let actualIterationType = this.iterationType;
|
|
4487
|
+
if (iterationType) actualIterationType = iterationType;
|
|
4488
|
+
let key;
|
|
4489
|
+
if (typeof keyNodeEntryOrPredicate === "function") {
|
|
4490
|
+
const results = this.search(keyNodeEntryOrPredicate, true);
|
|
4491
|
+
if (results.length === 0 || results[0] === void 0) return -1;
|
|
4492
|
+
key = results[0];
|
|
4493
|
+
} else if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
4494
|
+
return -1;
|
|
4495
|
+
} else if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
4496
|
+
key = keyNodeEntryOrPredicate.key;
|
|
4497
|
+
} else if (Array.isArray(keyNodeEntryOrPredicate)) {
|
|
4498
|
+
key = keyNodeEntryOrPredicate[0] ?? void 0;
|
|
4499
|
+
if (key === void 0 || key === null) return -1;
|
|
4500
|
+
} else {
|
|
4501
|
+
key = keyNodeEntryOrPredicate;
|
|
4502
|
+
}
|
|
4503
|
+
if (key === void 0) return -1;
|
|
4504
|
+
return actualIterationType === "RECURSIVE" ? this._getRankRecursive(this._root, key) : this._getRankIterative(this._root, key);
|
|
4505
|
+
}
|
|
4506
|
+
rangeByRank(start, end, callback = this._DEFAULT_NODE_CALLBACK, iterationType = this.iterationType) {
|
|
4507
|
+
if (!this._enableOrderStatistic) {
|
|
4508
|
+
raise(Error, ERR.orderStatisticNotEnabled("rangeByRank"));
|
|
4509
|
+
}
|
|
4510
|
+
if (this._size === 0) return [];
|
|
4511
|
+
const lo = Math.max(0, start);
|
|
4512
|
+
const hi = Math.min(this._size - 1, end);
|
|
4513
|
+
if (lo > hi) return [];
|
|
4514
|
+
let actualCallback = void 0;
|
|
4515
|
+
let actualIterationType = this.iterationType;
|
|
4516
|
+
if (typeof callback === "string") {
|
|
4517
|
+
actualIterationType = callback;
|
|
4518
|
+
} else if (callback) {
|
|
4519
|
+
actualCallback = callback;
|
|
4520
|
+
if (iterationType) {
|
|
4521
|
+
actualIterationType = iterationType;
|
|
4522
|
+
}
|
|
4523
|
+
}
|
|
4524
|
+
const results = [];
|
|
4525
|
+
const count = hi - lo + 1;
|
|
4526
|
+
const startNode = actualIterationType === "RECURSIVE" ? this._getByRankRecursive(this._root, lo) : this._getByRankIterative(this._root, lo);
|
|
4527
|
+
if (!startNode) return [];
|
|
4528
|
+
let collected = 0;
|
|
4529
|
+
const cb = actualCallback ?? this._DEFAULT_NODE_CALLBACK;
|
|
4530
|
+
let current = startNode;
|
|
4531
|
+
while (current && collected < count) {
|
|
4532
|
+
results.push(cb(current));
|
|
4533
|
+
collected++;
|
|
4534
|
+
if (collected < count) {
|
|
4535
|
+
current = this._next(current);
|
|
4536
|
+
}
|
|
4537
|
+
}
|
|
4538
|
+
return results;
|
|
4539
|
+
}
|
|
4359
4540
|
/**
|
|
4360
4541
|
* Adds a new node to the BST based on key comparison.
|
|
4361
4542
|
* @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
|
|
@@ -4441,6 +4622,15 @@ var BST = class extends BinaryTree {
|
|
|
4441
4622
|
|
|
4442
4623
|
|
|
4443
4624
|
|
|
4625
|
+
|
|
4626
|
+
|
|
4627
|
+
|
|
4628
|
+
|
|
4629
|
+
|
|
4630
|
+
|
|
4631
|
+
|
|
4632
|
+
|
|
4633
|
+
|
|
4444
4634
|
|
|
4445
4635
|
|
|
4446
4636
|
|
|
@@ -4465,6 +4655,7 @@ var BST = class extends BinaryTree {
|
|
|
4465
4655
|
this._setRoot(newNode);
|
|
4466
4656
|
if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
|
|
4467
4657
|
this._size++;
|
|
4658
|
+
this._updateCount(newNode);
|
|
4468
4659
|
return true;
|
|
4469
4660
|
}
|
|
4470
4661
|
let current = this._root;
|
|
@@ -4478,6 +4669,7 @@ var BST = class extends BinaryTree {
|
|
|
4478
4669
|
current.left = newNode;
|
|
4479
4670
|
if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
|
|
4480
4671
|
this._size++;
|
|
4672
|
+
this._updateCountAlongPath(newNode);
|
|
4481
4673
|
return true;
|
|
4482
4674
|
}
|
|
4483
4675
|
if (current.left !== null) current = current.left;
|
|
@@ -4486,6 +4678,7 @@ var BST = class extends BinaryTree {
|
|
|
4486
4678
|
current.right = newNode;
|
|
4487
4679
|
if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
|
|
4488
4680
|
this._size++;
|
|
4681
|
+
this._updateCountAlongPath(newNode);
|
|
4489
4682
|
return true;
|
|
4490
4683
|
}
|
|
4491
4684
|
if (current.right !== null) current = current.right;
|
|
@@ -4550,6 +4743,12 @@ var BST = class extends BinaryTree {
|
|
|
4550
4743
|
|
|
4551
4744
|
|
|
4552
4745
|
|
|
4746
|
+
|
|
4747
|
+
|
|
4748
|
+
|
|
4749
|
+
|
|
4750
|
+
|
|
4751
|
+
|
|
4553
4752
|
|
|
4554
4753
|
|
|
4555
4754
|
|
|
@@ -4837,6 +5036,9 @@ var BST = class extends BinaryTree {
|
|
|
4837
5036
|
|
|
4838
5037
|
|
|
4839
5038
|
|
|
5039
|
+
|
|
5040
|
+
|
|
5041
|
+
|
|
4840
5042
|
|
|
4841
5043
|
|
|
4842
5044
|
|
|
@@ -4903,6 +5105,9 @@ var BST = class extends BinaryTree {
|
|
|
4903
5105
|
|
|
4904
5106
|
|
|
4905
5107
|
|
|
5108
|
+
|
|
5109
|
+
|
|
5110
|
+
|
|
4906
5111
|
|
|
4907
5112
|
|
|
4908
5113
|
|
|
@@ -5016,6 +5221,12 @@ var BST = class extends BinaryTree {
|
|
|
5016
5221
|
|
|
5017
5222
|
|
|
5018
5223
|
|
|
5224
|
+
|
|
5225
|
+
|
|
5226
|
+
|
|
5227
|
+
|
|
5228
|
+
|
|
5229
|
+
|
|
5019
5230
|
|
|
5020
5231
|
|
|
5021
5232
|
|
|
@@ -5097,13 +5308,11 @@ var BST = class extends BinaryTree {
|
|
|
5097
5308
|
if (a instanceof Date && b instanceof Date) {
|
|
5098
5309
|
const ta = a.getTime();
|
|
5099
5310
|
const tb = b.getTime();
|
|
5100
|
-
if (Number.isNaN(ta) || Number.isNaN(tb))
|
|
5311
|
+
if (Number.isNaN(ta) || Number.isNaN(tb)) raise(TypeError, ERR.invalidDate("BST"));
|
|
5101
5312
|
return ta > tb ? 1 : ta < tb ? -1 : 0;
|
|
5102
5313
|
}
|
|
5103
5314
|
if (typeof a === "object" || typeof b === "object") {
|
|
5104
|
-
|
|
5105
|
-
ERR.comparatorRequired("BST")
|
|
5106
|
-
);
|
|
5315
|
+
raise(TypeError, ERR.comparatorRequired("BST"));
|
|
5107
5316
|
}
|
|
5108
5317
|
return 0;
|
|
5109
5318
|
};
|
|
@@ -5422,7 +5631,8 @@ var BST = class extends BinaryTree {
|
|
|
5422
5631
|
_snapshotOptions() {
|
|
5423
5632
|
return {
|
|
5424
5633
|
...super._snapshotOptions(),
|
|
5425
|
-
comparator: this._comparator
|
|
5634
|
+
comparator: this._comparator,
|
|
5635
|
+
enableOrderStatistic: this._enableOrderStatistic
|
|
5426
5636
|
};
|
|
5427
5637
|
}
|
|
5428
5638
|
/**
|
|
@@ -5444,6 +5654,113 @@ var BST = class extends BinaryTree {
|
|
|
5444
5654
|
*
|
|
5445
5655
|
* @param v - The node to set as root.
|
|
5446
5656
|
*/
|
|
5657
|
+
/**
|
|
5658
|
+
* (Protected) Recalculates the subtree count for a single node.
|
|
5659
|
+
* @remarks Time O(1). Only active when enableOrderStatistic is true.
|
|
5660
|
+
*/
|
|
5661
|
+
_updateCount(node) {
|
|
5662
|
+
if (!this._enableOrderStatistic) return;
|
|
5663
|
+
node._count = 1 + (this.isRealNode(node.left) ? node.left._count : 0) + (this.isRealNode(node.right) ? node.right._count : 0);
|
|
5664
|
+
}
|
|
5665
|
+
/**
|
|
5666
|
+
* (Protected) Updates subtree counts from a node up to the root.
|
|
5667
|
+
* @remarks Time O(log n). Only active when enableOrderStatistic is true.
|
|
5668
|
+
*/
|
|
5669
|
+
_updateCountAlongPath(node) {
|
|
5670
|
+
if (!this._enableOrderStatistic) return;
|
|
5671
|
+
let current = node;
|
|
5672
|
+
while (current) {
|
|
5673
|
+
this._updateCount(current);
|
|
5674
|
+
current = current.parent;
|
|
5675
|
+
}
|
|
5676
|
+
}
|
|
5677
|
+
/**
|
|
5678
|
+
* (Protected) Finds the node at position k in tree order (iterative).
|
|
5679
|
+
* @remarks Time O(log n), Space O(1)
|
|
5680
|
+
*/
|
|
5681
|
+
_getByRankIterative(node, k) {
|
|
5682
|
+
let current = node;
|
|
5683
|
+
let remaining = k;
|
|
5684
|
+
while (current) {
|
|
5685
|
+
const leftCount = this.isRealNode(current.left) ? current.left._count : 0;
|
|
5686
|
+
if (remaining < leftCount) {
|
|
5687
|
+
current = current.left;
|
|
5688
|
+
} else if (remaining === leftCount) {
|
|
5689
|
+
return current;
|
|
5690
|
+
} else {
|
|
5691
|
+
remaining = remaining - leftCount - 1;
|
|
5692
|
+
current = current.right;
|
|
5693
|
+
}
|
|
5694
|
+
}
|
|
5695
|
+
return void 0;
|
|
5696
|
+
}
|
|
5697
|
+
/**
|
|
5698
|
+
* (Protected) Finds the node at position k in tree order (recursive).
|
|
5699
|
+
* @remarks Time O(log n), Space O(log n) call stack
|
|
5700
|
+
*/
|
|
5701
|
+
_getByRankRecursive(node, k) {
|
|
5702
|
+
if (!node) return void 0;
|
|
5703
|
+
const leftCount = this.isRealNode(node.left) ? node.left._count : 0;
|
|
5704
|
+
if (k < leftCount) return this._getByRankRecursive(node.left, k);
|
|
5705
|
+
if (k === leftCount) return node;
|
|
5706
|
+
return this._getByRankRecursive(node.right, k - leftCount - 1);
|
|
5707
|
+
}
|
|
5708
|
+
/**
|
|
5709
|
+
* (Protected) Computes the rank of a key iteratively.
|
|
5710
|
+
* @remarks Time O(log n), Space O(1)
|
|
5711
|
+
*/
|
|
5712
|
+
_getRankIterative(node, key) {
|
|
5713
|
+
let rank = 0;
|
|
5714
|
+
let current = node;
|
|
5715
|
+
while (this.isRealNode(current)) {
|
|
5716
|
+
const cmp = this._compare(current.key, key);
|
|
5717
|
+
if (cmp > 0) {
|
|
5718
|
+
current = current.left;
|
|
5719
|
+
} else if (cmp < 0) {
|
|
5720
|
+
rank += (this.isRealNode(current.left) ? current.left._count : 0) + 1;
|
|
5721
|
+
current = current.right;
|
|
5722
|
+
} else {
|
|
5723
|
+
rank += this.isRealNode(current.left) ? current.left._count : 0;
|
|
5724
|
+
return rank;
|
|
5725
|
+
}
|
|
5726
|
+
}
|
|
5727
|
+
return rank;
|
|
5728
|
+
}
|
|
5729
|
+
/**
|
|
5730
|
+
* (Protected) Computes the rank of a key recursively.
|
|
5731
|
+
* @remarks Time O(log n), Space O(log n) call stack
|
|
5732
|
+
*/
|
|
5733
|
+
_getRankRecursive(node, key) {
|
|
5734
|
+
if (!node) return 0;
|
|
5735
|
+
const cmp = this._compare(node.key, key);
|
|
5736
|
+
if (cmp > 0) {
|
|
5737
|
+
return this._getRankRecursive(node.left, key);
|
|
5738
|
+
} else if (cmp < 0) {
|
|
5739
|
+
return (this.isRealNode(node.left) ? node.left._count : 0) + 1 + this._getRankRecursive(node.right, key);
|
|
5740
|
+
} else {
|
|
5741
|
+
return this.isRealNode(node.left) ? node.left._count : 0;
|
|
5742
|
+
}
|
|
5743
|
+
}
|
|
5744
|
+
/**
|
|
5745
|
+
* (Protected) Finds the in-order successor of a node.
|
|
5746
|
+
* @remarks Time O(log n), Space O(1)
|
|
5747
|
+
*/
|
|
5748
|
+
_next(node) {
|
|
5749
|
+
if (this.isRealNode(node.right)) {
|
|
5750
|
+
let current2 = node.right;
|
|
5751
|
+
while (this.isRealNode(current2.left)) {
|
|
5752
|
+
current2 = current2.left;
|
|
5753
|
+
}
|
|
5754
|
+
return current2;
|
|
5755
|
+
}
|
|
5756
|
+
let current = node;
|
|
5757
|
+
let parent = current.parent;
|
|
5758
|
+
while (parent && current === parent.right) {
|
|
5759
|
+
current = parent;
|
|
5760
|
+
parent = parent.parent;
|
|
5761
|
+
}
|
|
5762
|
+
return parent;
|
|
5763
|
+
}
|
|
5447
5764
|
_setRoot(v) {
|
|
5448
5765
|
if (v) v.parent = void 0;
|
|
5449
5766
|
this._root = v;
|
|
@@ -5490,21 +5807,28 @@ var BST = class extends BinaryTree {
|
|
|
5490
5807
|
while (x.left !== void 0 && x.left !== null) x = x.left;
|
|
5491
5808
|
return x;
|
|
5492
5809
|
}, "minNode");
|
|
5810
|
+
let countUpdateStart;
|
|
5493
5811
|
if (node.left === void 0) {
|
|
5812
|
+
countUpdateStart = node.parent;
|
|
5494
5813
|
transplant(node, node.right);
|
|
5495
5814
|
} else if (node.right === void 0) {
|
|
5815
|
+
countUpdateStart = node.parent;
|
|
5496
5816
|
transplant(node, node.left);
|
|
5497
5817
|
} else {
|
|
5498
5818
|
const succ = minNode(node.right);
|
|
5499
5819
|
if (succ.parent !== node) {
|
|
5820
|
+
countUpdateStart = succ.parent;
|
|
5500
5821
|
transplant(succ, succ.right);
|
|
5501
5822
|
succ.right = node.right;
|
|
5502
5823
|
if (succ.right) succ.right.parent = succ;
|
|
5824
|
+
} else {
|
|
5825
|
+
countUpdateStart = succ;
|
|
5503
5826
|
}
|
|
5504
5827
|
transplant(node, succ);
|
|
5505
5828
|
succ.left = node.left;
|
|
5506
5829
|
if (succ.left) succ.left.parent = succ;
|
|
5507
5830
|
}
|
|
5831
|
+
this._updateCountAlongPath(countUpdateStart);
|
|
5508
5832
|
this._size = Math.max(0, this._size - 1);
|
|
5509
5833
|
return true;
|
|
5510
5834
|
}
|
|
@@ -5517,6 +5841,6 @@ var BST = class extends BinaryTree {
|
|
|
5517
5841
|
* @license MIT License
|
|
5518
5842
|
*/
|
|
5519
5843
|
|
|
5520
|
-
export { BST, BSTNode, BinaryTree, BinaryTreeNode, DFSOperation, ERR, Range };
|
|
5844
|
+
export { BST, BSTNode, BinaryTree, BinaryTreeNode, DFSOperation, ERR, Range, raise };
|
|
5521
5845
|
//# sourceMappingURL=index.mjs.map
|
|
5522
5846
|
//# sourceMappingURL=index.mjs.map
|