binary-tree-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 +340 -107
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +339 -106
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +340 -108
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +339 -107
- 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/binary-tree-typed.js +337 -105
- package/dist/umd/binary-tree-typed.js.map +1 -1
- package/dist/umd/binary-tree-typed.min.js +5 -5
- package/dist/umd/binary-tree-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/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 {
|
|
@@ -788,6 +794,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
788
794
|
|
|
789
795
|
|
|
790
796
|
|
|
797
|
+
|
|
798
|
+
|
|
799
|
+
|
|
800
|
+
|
|
801
|
+
|
|
802
|
+
|
|
803
|
+
|
|
791
804
|
|
|
792
805
|
|
|
793
806
|
|
|
@@ -835,6 +848,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
835
848
|
|
|
836
849
|
|
|
837
850
|
|
|
851
|
+
|
|
852
|
+
|
|
853
|
+
|
|
854
|
+
|
|
855
|
+
|
|
856
|
+
|
|
857
|
+
|
|
838
858
|
|
|
839
859
|
|
|
840
860
|
|
|
@@ -852,6 +872,14 @@ var Queue = class _Queue extends LinearBase {
|
|
|
852
872
|
get first() {
|
|
853
873
|
return this.length > 0 ? this.elements[this._offset] : void 0;
|
|
854
874
|
}
|
|
875
|
+
/**
|
|
876
|
+
* Peek at the front element without removing it (alias for `first`).
|
|
877
|
+
* @remarks Time O(1), Space O(1)
|
|
878
|
+
* @returns Front element or undefined.
|
|
879
|
+
*/
|
|
880
|
+
peek() {
|
|
881
|
+
return this.first;
|
|
882
|
+
}
|
|
855
883
|
/**
|
|
856
884
|
* Get the last element (back) without removing it.
|
|
857
885
|
* @remarks Time O(1), Space O(1)
|
|
@@ -898,6 +926,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
898
926
|
|
|
899
927
|
|
|
900
928
|
|
|
929
|
+
|
|
930
|
+
|
|
931
|
+
|
|
932
|
+
|
|
933
|
+
|
|
934
|
+
|
|
935
|
+
|
|
901
936
|
|
|
902
937
|
|
|
903
938
|
|
|
@@ -957,6 +992,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
957
992
|
|
|
958
993
|
|
|
959
994
|
|
|
995
|
+
|
|
996
|
+
|
|
997
|
+
|
|
998
|
+
|
|
999
|
+
|
|
1000
|
+
|
|
1001
|
+
|
|
960
1002
|
|
|
961
1003
|
|
|
962
1004
|
|
|
@@ -1023,6 +1065,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1023
1065
|
|
|
1024
1066
|
|
|
1025
1067
|
|
|
1068
|
+
|
|
1069
|
+
|
|
1070
|
+
|
|
1071
|
+
|
|
1072
|
+
|
|
1073
|
+
|
|
1074
|
+
|
|
1026
1075
|
|
|
1027
1076
|
|
|
1028
1077
|
|
|
@@ -1079,6 +1128,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1079
1128
|
|
|
1080
1129
|
|
|
1081
1130
|
|
|
1131
|
+
|
|
1132
|
+
|
|
1133
|
+
|
|
1134
|
+
|
|
1135
|
+
|
|
1136
|
+
|
|
1137
|
+
|
|
1082
1138
|
|
|
1083
1139
|
|
|
1084
1140
|
|
|
@@ -1128,6 +1184,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1128
1184
|
|
|
1129
1185
|
|
|
1130
1186
|
|
|
1187
|
+
|
|
1188
|
+
|
|
1189
|
+
|
|
1190
|
+
|
|
1191
|
+
|
|
1192
|
+
|
|
1193
|
+
|
|
1131
1194
|
|
|
1132
1195
|
|
|
1133
1196
|
|
|
@@ -1182,6 +1245,21 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1182
1245
|
this._elements[this._offset + index] = newElement;
|
|
1183
1246
|
return true;
|
|
1184
1247
|
}
|
|
1248
|
+
/**
|
|
1249
|
+
* Delete the first element that satisfies a predicate.
|
|
1250
|
+
* @remarks Time O(N), Space O(N)
|
|
1251
|
+
* @param predicate - Function (value, index, queue) → boolean to decide deletion.
|
|
1252
|
+
* @returns True if a match was removed.
|
|
1253
|
+
*/
|
|
1254
|
+
deleteWhere(predicate) {
|
|
1255
|
+
for (let i = 0; i < this.length; i++) {
|
|
1256
|
+
if (predicate(this._elements[this._offset + i], i, this)) {
|
|
1257
|
+
this.deleteAt(i);
|
|
1258
|
+
return true;
|
|
1259
|
+
}
|
|
1260
|
+
}
|
|
1261
|
+
return false;
|
|
1262
|
+
}
|
|
1185
1263
|
/**
|
|
1186
1264
|
* Reverse the queue in-place by compacting then reversing.
|
|
1187
1265
|
* @remarks Time O(N), Space O(N)
|
|
@@ -1218,6 +1296,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1218
1296
|
|
|
1219
1297
|
|
|
1220
1298
|
|
|
1299
|
+
|
|
1300
|
+
|
|
1301
|
+
|
|
1302
|
+
|
|
1303
|
+
|
|
1304
|
+
|
|
1305
|
+
|
|
1221
1306
|
|
|
1222
1307
|
|
|
1223
1308
|
|
|
@@ -1261,6 +1346,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1261
1346
|
|
|
1262
1347
|
|
|
1263
1348
|
|
|
1349
|
+
|
|
1350
|
+
|
|
1351
|
+
|
|
1352
|
+
|
|
1353
|
+
|
|
1354
|
+
|
|
1355
|
+
|
|
1264
1356
|
|
|
1265
1357
|
|
|
1266
1358
|
|
|
@@ -1327,6 +1419,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1327
1419
|
|
|
1328
1420
|
|
|
1329
1421
|
|
|
1422
|
+
|
|
1423
|
+
|
|
1424
|
+
|
|
1425
|
+
|
|
1426
|
+
|
|
1427
|
+
|
|
1428
|
+
|
|
1330
1429
|
|
|
1331
1430
|
|
|
1332
1431
|
|
|
@@ -1377,6 +1476,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1377
1476
|
|
|
1378
1477
|
|
|
1379
1478
|
|
|
1479
|
+
|
|
1480
|
+
|
|
1481
|
+
|
|
1482
|
+
|
|
1483
|
+
|
|
1484
|
+
|
|
1485
|
+
|
|
1380
1486
|
|
|
1381
1487
|
|
|
1382
1488
|
|
|
@@ -1431,6 +1537,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1431
1537
|
|
|
1432
1538
|
|
|
1433
1539
|
|
|
1540
|
+
|
|
1541
|
+
|
|
1542
|
+
|
|
1543
|
+
|
|
1544
|
+
|
|
1545
|
+
|
|
1546
|
+
|
|
1434
1547
|
|
|
1435
1548
|
|
|
1436
1549
|
|
|
@@ -1689,7 +1802,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1689
1802
|
if (isMapMode !== void 0) this._isMapMode = isMapMode;
|
|
1690
1803
|
if (isDuplicate !== void 0) this._isDuplicate = isDuplicate;
|
|
1691
1804
|
if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
|
|
1692
|
-
else if (toEntryFn)
|
|
1805
|
+
else if (toEntryFn) raise(TypeError, ERR.notAFunction("toEntryFn", "BinaryTree"));
|
|
1693
1806
|
}
|
|
1694
1807
|
if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
|
|
1695
1808
|
}
|
|
@@ -1905,7 +2018,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1905
2018
|
}
|
|
1906
2019
|
/**
|
|
1907
2020
|
* Adds a new node to the tree.
|
|
1908
|
-
* @remarks Time O(
|
|
2021
|
+
* @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).
|
|
1909
2022
|
*
|
|
1910
2023
|
* @param keyNodeOrEntry - The key, node, or entry to add.
|
|
1911
2024
|
* @returns True if the addition was successful, false otherwise.
|
|
@@ -1928,6 +2041,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1928
2041
|
|
|
1929
2042
|
|
|
1930
2043
|
|
|
2044
|
+
|
|
2045
|
+
|
|
2046
|
+
|
|
2047
|
+
|
|
2048
|
+
|
|
2049
|
+
|
|
2050
|
+
|
|
1931
2051
|
|
|
1932
2052
|
|
|
1933
2053
|
|
|
@@ -1950,7 +2070,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1950
2070
|
}
|
|
1951
2071
|
/**
|
|
1952
2072
|
* Adds or updates a new node to the tree.
|
|
1953
|
-
* @remarks Time O(
|
|
2073
|
+
* @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).
|
|
1954
2074
|
*
|
|
1955
2075
|
* @param keyNodeOrEntry - The key, node, or entry to set or update.
|
|
1956
2076
|
* @param [value] - The value, if providing just a key.
|
|
@@ -1979,6 +2099,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1979
2099
|
|
|
1980
2100
|
|
|
1981
2101
|
|
|
2102
|
+
|
|
2103
|
+
|
|
2104
|
+
|
|
2105
|
+
|
|
2106
|
+
|
|
2107
|
+
|
|
2108
|
+
|
|
1982
2109
|
|
|
1983
2110
|
|
|
1984
2111
|
|
|
@@ -2082,6 +2209,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2082
2209
|
|
|
2083
2210
|
|
|
2084
2211
|
|
|
2212
|
+
|
|
2213
|
+
|
|
2214
|
+
|
|
2215
|
+
|
|
2216
|
+
|
|
2217
|
+
|
|
2218
|
+
|
|
2085
2219
|
|
|
2086
2220
|
|
|
2087
2221
|
|
|
@@ -2121,6 +2255,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2121
2255
|
|
|
2122
2256
|
|
|
2123
2257
|
|
|
2258
|
+
|
|
2259
|
+
|
|
2260
|
+
|
|
2261
|
+
|
|
2262
|
+
|
|
2263
|
+
|
|
2264
|
+
|
|
2124
2265
|
|
|
2125
2266
|
|
|
2126
2267
|
|
|
@@ -2181,6 +2322,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2181
2322
|
|
|
2182
2323
|
|
|
2183
2324
|
|
|
2325
|
+
|
|
2326
|
+
|
|
2327
|
+
|
|
2328
|
+
|
|
2329
|
+
|
|
2330
|
+
|
|
2331
|
+
|
|
2184
2332
|
|
|
2185
2333
|
|
|
2186
2334
|
|
|
@@ -2200,22 +2348,69 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2200
2348
|
this.setMany(anotherTree, []);
|
|
2201
2349
|
}
|
|
2202
2350
|
/**
|
|
2203
|
-
*
|
|
2204
|
-
* @remarks Time O(N)
|
|
2351
|
+
* Deletes a node from the tree (internal, returns balancing metadata).
|
|
2352
|
+
* @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).
|
|
2353
|
+
* @internal Used by AVL/BST subclasses that need balancing metadata after deletion.
|
|
2205
2354
|
*
|
|
2206
|
-
* @param
|
|
2207
|
-
* @
|
|
2355
|
+
* @param keyNodeEntryRawOrPredicate - The node to delete.
|
|
2356
|
+
* @returns An array containing deletion results with balancing metadata.
|
|
2208
2357
|
*/
|
|
2209
|
-
|
|
2210
|
-
|
|
2211
|
-
this.
|
|
2358
|
+
_deleteInternal(keyNodeEntryRawOrPredicate) {
|
|
2359
|
+
const deletedResult = [];
|
|
2360
|
+
if (!this._root) return deletedResult;
|
|
2361
|
+
const curr = this.getNode(keyNodeEntryRawOrPredicate);
|
|
2362
|
+
if (!curr) return deletedResult;
|
|
2363
|
+
const parent = curr?.parent;
|
|
2364
|
+
let needBalanced;
|
|
2365
|
+
let orgCurrent = curr;
|
|
2366
|
+
if (!curr.left && !curr.right && !parent) {
|
|
2367
|
+
this._setRoot(void 0);
|
|
2368
|
+
} else if (curr.left) {
|
|
2369
|
+
const leftSubTreeRightMost = this.getRightMost((node) => node, curr.left);
|
|
2370
|
+
if (leftSubTreeRightMost) {
|
|
2371
|
+
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
2372
|
+
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
|
|
2373
|
+
if (this._isMapMode) {
|
|
2374
|
+
this._store.set(curr.key, curr);
|
|
2375
|
+
this._store.set(leftSubTreeRightMost.key, leftSubTreeRightMost);
|
|
2376
|
+
}
|
|
2377
|
+
if (parentOfLeftSubTreeMax) {
|
|
2378
|
+
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
2379
|
+
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
2380
|
+
else parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
|
|
2381
|
+
needBalanced = parentOfLeftSubTreeMax;
|
|
2382
|
+
}
|
|
2383
|
+
}
|
|
2384
|
+
} else if (parent) {
|
|
2385
|
+
const { familyPosition: fp } = curr;
|
|
2386
|
+
if (fp === "LEFT" || fp === "ROOT_LEFT") {
|
|
2387
|
+
parent.left = curr.right;
|
|
2388
|
+
} else if (fp === "RIGHT" || fp === "ROOT_RIGHT") {
|
|
2389
|
+
parent.right = curr.right;
|
|
2390
|
+
}
|
|
2391
|
+
needBalanced = parent;
|
|
2392
|
+
} else {
|
|
2393
|
+
this._setRoot(curr.right);
|
|
2394
|
+
curr.right = void 0;
|
|
2395
|
+
}
|
|
2396
|
+
this._size = this._size - 1;
|
|
2397
|
+
deletedResult.push({ deleted: orgCurrent, needBalanced });
|
|
2398
|
+
if (this._isMapMode && orgCurrent) this._store.delete(orgCurrent.key);
|
|
2399
|
+
return deletedResult;
|
|
2212
2400
|
}
|
|
2213
2401
|
/**
|
|
2214
2402
|
* Deletes a node from the tree.
|
|
2215
|
-
* @remarks Time O(
|
|
2403
|
+
* @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).
|
|
2216
2404
|
*
|
|
2217
2405
|
* @param keyNodeEntryRawOrPredicate - The node to delete.
|
|
2218
|
-
* @returns
|
|
2406
|
+
* @returns True if the node was found and deleted, false otherwise.
|
|
2407
|
+
|
|
2408
|
+
|
|
2409
|
+
|
|
2410
|
+
|
|
2411
|
+
|
|
2412
|
+
|
|
2413
|
+
|
|
2219
2414
|
|
|
2220
2415
|
|
|
2221
2416
|
|
|
@@ -2256,51 +2451,11 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2256
2451
|
* console.log(tree.size); // 4;
|
|
2257
2452
|
*/
|
|
2258
2453
|
delete(keyNodeEntryRawOrPredicate) {
|
|
2259
|
-
|
|
2260
|
-
if (!this._root) return deletedResult;
|
|
2261
|
-
const curr = this.getNode(keyNodeEntryRawOrPredicate);
|
|
2262
|
-
if (!curr) return deletedResult;
|
|
2263
|
-
const parent = curr?.parent;
|
|
2264
|
-
let needBalanced;
|
|
2265
|
-
let orgCurrent = curr;
|
|
2266
|
-
if (!curr.left && !curr.right && !parent) {
|
|
2267
|
-
this._setRoot(void 0);
|
|
2268
|
-
} else if (curr.left) {
|
|
2269
|
-
const leftSubTreeRightMost = this.getRightMost((node) => node, curr.left);
|
|
2270
|
-
if (leftSubTreeRightMost) {
|
|
2271
|
-
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
2272
|
-
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
|
|
2273
|
-
if (this._isMapMode) {
|
|
2274
|
-
this._store.set(curr.key, curr);
|
|
2275
|
-
this._store.set(leftSubTreeRightMost.key, leftSubTreeRightMost);
|
|
2276
|
-
}
|
|
2277
|
-
if (parentOfLeftSubTreeMax) {
|
|
2278
|
-
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
2279
|
-
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
2280
|
-
else parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
|
|
2281
|
-
needBalanced = parentOfLeftSubTreeMax;
|
|
2282
|
-
}
|
|
2283
|
-
}
|
|
2284
|
-
} else if (parent) {
|
|
2285
|
-
const { familyPosition: fp } = curr;
|
|
2286
|
-
if (fp === "LEFT" || fp === "ROOT_LEFT") {
|
|
2287
|
-
parent.left = curr.right;
|
|
2288
|
-
} else if (fp === "RIGHT" || fp === "ROOT_RIGHT") {
|
|
2289
|
-
parent.right = curr.right;
|
|
2290
|
-
}
|
|
2291
|
-
needBalanced = parent;
|
|
2292
|
-
} else {
|
|
2293
|
-
this._setRoot(curr.right);
|
|
2294
|
-
curr.right = void 0;
|
|
2295
|
-
}
|
|
2296
|
-
this._size = this._size - 1;
|
|
2297
|
-
deletedResult.push({ deleted: orgCurrent, needBalanced });
|
|
2298
|
-
if (this._isMapMode && orgCurrent) this._store.delete(orgCurrent.key);
|
|
2299
|
-
return deletedResult;
|
|
2454
|
+
return this._deleteInternal(keyNodeEntryRawOrPredicate).length > 0;
|
|
2300
2455
|
}
|
|
2301
2456
|
/**
|
|
2302
2457
|
* Searches the tree for nodes matching a predicate.
|
|
2303
|
-
* @remarks Time O(
|
|
2458
|
+
* @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).
|
|
2304
2459
|
*
|
|
2305
2460
|
* @template C - The type of the callback function.
|
|
2306
2461
|
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
@@ -2349,7 +2504,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2349
2504
|
}
|
|
2350
2505
|
/**
|
|
2351
2506
|
* Gets the first node matching a predicate.
|
|
2352
|
-
* @remarks Time O(
|
|
2507
|
+
* @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.
|
|
2353
2508
|
*
|
|
2354
2509
|
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
2355
2510
|
* @param [startNode=this._root] - The node to start the search from.
|
|
@@ -2377,6 +2532,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2377
2532
|
|
|
2378
2533
|
|
|
2379
2534
|
|
|
2535
|
+
|
|
2536
|
+
|
|
2537
|
+
|
|
2538
|
+
|
|
2539
|
+
|
|
2540
|
+
|
|
2541
|
+
|
|
2380
2542
|
|
|
2381
2543
|
|
|
2382
2544
|
|
|
@@ -2402,7 +2564,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2402
2564
|
}
|
|
2403
2565
|
/**
|
|
2404
2566
|
* Gets the value associated with a key.
|
|
2405
|
-
* @remarks Time O(
|
|
2567
|
+
* @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).
|
|
2406
2568
|
*
|
|
2407
2569
|
* @param keyNodeEntryOrPredicate - The key, node, or entry to get the value for.
|
|
2408
2570
|
* @param [startNode=this._root] - The node to start searching from (if not in Map mode).
|
|
@@ -2432,6 +2594,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2432
2594
|
|
|
2433
2595
|
|
|
2434
2596
|
|
|
2597
|
+
|
|
2598
|
+
|
|
2599
|
+
|
|
2600
|
+
|
|
2601
|
+
|
|
2602
|
+
|
|
2603
|
+
|
|
2435
2604
|
|
|
2436
2605
|
|
|
2437
2606
|
|
|
@@ -2489,6 +2658,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2489
2658
|
|
|
2490
2659
|
|
|
2491
2660
|
|
|
2661
|
+
|
|
2662
|
+
|
|
2663
|
+
|
|
2664
|
+
|
|
2665
|
+
|
|
2666
|
+
|
|
2667
|
+
|
|
2492
2668
|
|
|
2493
2669
|
|
|
2494
2670
|
|
|
@@ -2534,6 +2710,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2534
2710
|
|
|
2535
2711
|
|
|
2536
2712
|
|
|
2713
|
+
|
|
2714
|
+
|
|
2715
|
+
|
|
2716
|
+
|
|
2717
|
+
|
|
2718
|
+
|
|
2719
|
+
|
|
2537
2720
|
|
|
2538
2721
|
|
|
2539
2722
|
|
|
@@ -2588,6 +2771,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2588
2771
|
|
|
2589
2772
|
|
|
2590
2773
|
|
|
2774
|
+
|
|
2775
|
+
|
|
2776
|
+
|
|
2777
|
+
|
|
2778
|
+
|
|
2779
|
+
|
|
2780
|
+
|
|
2591
2781
|
|
|
2592
2782
|
|
|
2593
2783
|
|
|
@@ -2669,6 +2859,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2669
2859
|
|
|
2670
2860
|
|
|
2671
2861
|
|
|
2862
|
+
|
|
2863
|
+
|
|
2864
|
+
|
|
2865
|
+
|
|
2866
|
+
|
|
2867
|
+
|
|
2868
|
+
|
|
2672
2869
|
|
|
2673
2870
|
|
|
2674
2871
|
|
|
@@ -2727,6 +2924,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2727
2924
|
|
|
2728
2925
|
|
|
2729
2926
|
|
|
2927
|
+
|
|
2928
|
+
|
|
2929
|
+
|
|
2930
|
+
|
|
2931
|
+
|
|
2932
|
+
|
|
2933
|
+
|
|
2730
2934
|
|
|
2731
2935
|
|
|
2732
2936
|
|
|
@@ -3201,6 +3405,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3201
3405
|
|
|
3202
3406
|
|
|
3203
3407
|
|
|
3408
|
+
|
|
3409
|
+
|
|
3410
|
+
|
|
3411
|
+
|
|
3412
|
+
|
|
3413
|
+
|
|
3414
|
+
|
|
3204
3415
|
|
|
3205
3416
|
|
|
3206
3417
|
|
|
@@ -3250,6 +3461,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3250
3461
|
|
|
3251
3462
|
|
|
3252
3463
|
|
|
3464
|
+
|
|
3465
|
+
|
|
3466
|
+
|
|
3467
|
+
|
|
3468
|
+
|
|
3469
|
+
|
|
3470
|
+
|
|
3253
3471
|
|
|
3254
3472
|
|
|
3255
3473
|
|
|
@@ -3303,6 +3521,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3303
3521
|
|
|
3304
3522
|
|
|
3305
3523
|
|
|
3524
|
+
|
|
3525
|
+
|
|
3526
|
+
|
|
3527
|
+
|
|
3528
|
+
|
|
3529
|
+
|
|
3530
|
+
|
|
3306
3531
|
|
|
3307
3532
|
|
|
3308
3533
|
|
|
@@ -3381,6 +3606,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3381
3606
|
|
|
3382
3607
|
|
|
3383
3608
|
|
|
3609
|
+
|
|
3610
|
+
|
|
3611
|
+
|
|
3612
|
+
|
|
3613
|
+
|
|
3614
|
+
|
|
3615
|
+
|
|
3384
3616
|
|
|
3385
3617
|
|
|
3386
3618
|
|
|
@@ -3868,6 +4100,6 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3868
4100
|
* @license MIT License
|
|
3869
4101
|
*/
|
|
3870
4102
|
|
|
3871
|
-
export { BinaryTree, BinaryTreeNode, DFSOperation, ERR, Range };
|
|
4103
|
+
export { BinaryTree, BinaryTreeNode, DFSOperation, ERR, Range, raise };
|
|
3872
4104
|
//# sourceMappingURL=index.mjs.map
|
|
3873
4105
|
//# sourceMappingURL=index.mjs.map
|