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
|
@@ -61,6 +61,60 @@ function makeTrampoline(fn) {
|
|
|
61
61
|
}
|
|
62
62
|
__name(makeTrampoline, "makeTrampoline");
|
|
63
63
|
|
|
64
|
+
// src/common/error.ts
|
|
65
|
+
function raise(ErrorClass, message) {
|
|
66
|
+
throw new ErrorClass(message);
|
|
67
|
+
}
|
|
68
|
+
__name(raise, "raise");
|
|
69
|
+
var ERR = {
|
|
70
|
+
// Range / index
|
|
71
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
72
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
73
|
+
// Type / argument
|
|
74
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
75
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
76
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
77
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
78
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
79
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
80
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
81
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
82
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
83
|
+
// State / operation
|
|
84
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
85
|
+
// Matrix
|
|
86
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
87
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
88
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
89
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
90
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch"),
|
|
91
|
+
// Order statistic
|
|
92
|
+
orderStatisticNotEnabled: /* @__PURE__ */ __name((method, ctx) => `${ctx ? ctx + ": " : ""}${method}() requires enableOrderStatistic: true.`, "orderStatisticNotEnabled")
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
// src/common/index.ts
|
|
96
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
97
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
98
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
99
|
+
return DFSOperation2;
|
|
100
|
+
})(DFSOperation || {});
|
|
101
|
+
var _Range = class _Range {
|
|
102
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
103
|
+
this.low = low;
|
|
104
|
+
this.high = high;
|
|
105
|
+
this.includeLow = includeLow;
|
|
106
|
+
this.includeHigh = includeHigh;
|
|
107
|
+
}
|
|
108
|
+
// Determine whether a key is within the range
|
|
109
|
+
isInRange(key, comparator) {
|
|
110
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
111
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
112
|
+
return lowCheck && highCheck;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
__name(_Range, "Range");
|
|
116
|
+
var Range = _Range;
|
|
117
|
+
|
|
64
118
|
// src/data-structures/base/iterable-element-base.ts
|
|
65
119
|
var _IterableElementBase = class _IterableElementBase {
|
|
66
120
|
/**
|
|
@@ -83,7 +137,7 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
83
137
|
if (options) {
|
|
84
138
|
const { toElementFn } = options;
|
|
85
139
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
86
|
-
else if (toElementFn)
|
|
140
|
+
else if (toElementFn) raise(TypeError, "toElementFn must be a function type");
|
|
87
141
|
}
|
|
88
142
|
}
|
|
89
143
|
/**
|
|
@@ -239,7 +293,7 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
239
293
|
acc = initialValue;
|
|
240
294
|
} else {
|
|
241
295
|
const first = iter.next();
|
|
242
|
-
if (first.done)
|
|
296
|
+
if (first.done) raise(TypeError, "Reduce of empty structure with no initial value");
|
|
243
297
|
acc = first.value;
|
|
244
298
|
index = 1;
|
|
245
299
|
}
|
|
@@ -475,54 +529,6 @@ var _LinearBase = class _LinearBase extends IterableElementBase {
|
|
|
475
529
|
__name(_LinearBase, "LinearBase");
|
|
476
530
|
var LinearBase = _LinearBase;
|
|
477
531
|
|
|
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 _Range {
|
|
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
|
-
// Determine whether a key is within the range
|
|
517
|
-
isInRange(key, comparator) {
|
|
518
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
519
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
520
|
-
return lowCheck && highCheck;
|
|
521
|
-
}
|
|
522
|
-
};
|
|
523
|
-
__name(_Range, "Range");
|
|
524
|
-
var Range = _Range;
|
|
525
|
-
|
|
526
532
|
// src/data-structures/base/iterable-entry-base.ts
|
|
527
533
|
var _IterableEntryBase = class _IterableEntryBase {
|
|
528
534
|
/**
|
|
@@ -785,6 +791,13 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
785
791
|
|
|
786
792
|
|
|
787
793
|
|
|
794
|
+
|
|
795
|
+
|
|
796
|
+
|
|
797
|
+
|
|
798
|
+
|
|
799
|
+
|
|
800
|
+
|
|
788
801
|
|
|
789
802
|
|
|
790
803
|
|
|
@@ -832,6 +845,13 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
832
845
|
|
|
833
846
|
|
|
834
847
|
|
|
848
|
+
|
|
849
|
+
|
|
850
|
+
|
|
851
|
+
|
|
852
|
+
|
|
853
|
+
|
|
854
|
+
|
|
835
855
|
|
|
836
856
|
|
|
837
857
|
|
|
@@ -849,6 +869,14 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
849
869
|
get first() {
|
|
850
870
|
return this.length > 0 ? this.elements[this._offset] : void 0;
|
|
851
871
|
}
|
|
872
|
+
/**
|
|
873
|
+
* Peek at the front element without removing it (alias for `first`).
|
|
874
|
+
* @remarks Time O(1), Space O(1)
|
|
875
|
+
* @returns Front element or undefined.
|
|
876
|
+
*/
|
|
877
|
+
peek() {
|
|
878
|
+
return this.first;
|
|
879
|
+
}
|
|
852
880
|
/**
|
|
853
881
|
* Get the last element (back) without removing it.
|
|
854
882
|
* @remarks Time O(1), Space O(1)
|
|
@@ -895,6 +923,13 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
895
923
|
|
|
896
924
|
|
|
897
925
|
|
|
926
|
+
|
|
927
|
+
|
|
928
|
+
|
|
929
|
+
|
|
930
|
+
|
|
931
|
+
|
|
932
|
+
|
|
898
933
|
|
|
899
934
|
|
|
900
935
|
|
|
@@ -954,6 +989,13 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
954
989
|
|
|
955
990
|
|
|
956
991
|
|
|
992
|
+
|
|
993
|
+
|
|
994
|
+
|
|
995
|
+
|
|
996
|
+
|
|
997
|
+
|
|
998
|
+
|
|
957
999
|
|
|
958
1000
|
|
|
959
1001
|
|
|
@@ -1020,6 +1062,13 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1020
1062
|
|
|
1021
1063
|
|
|
1022
1064
|
|
|
1065
|
+
|
|
1066
|
+
|
|
1067
|
+
|
|
1068
|
+
|
|
1069
|
+
|
|
1070
|
+
|
|
1071
|
+
|
|
1023
1072
|
|
|
1024
1073
|
|
|
1025
1074
|
|
|
@@ -1076,6 +1125,13 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1076
1125
|
|
|
1077
1126
|
|
|
1078
1127
|
|
|
1128
|
+
|
|
1129
|
+
|
|
1130
|
+
|
|
1131
|
+
|
|
1132
|
+
|
|
1133
|
+
|
|
1134
|
+
|
|
1079
1135
|
|
|
1080
1136
|
|
|
1081
1137
|
|
|
@@ -1125,6 +1181,13 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1125
1181
|
|
|
1126
1182
|
|
|
1127
1183
|
|
|
1184
|
+
|
|
1185
|
+
|
|
1186
|
+
|
|
1187
|
+
|
|
1188
|
+
|
|
1189
|
+
|
|
1190
|
+
|
|
1128
1191
|
|
|
1129
1192
|
|
|
1130
1193
|
|
|
@@ -1179,6 +1242,21 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1179
1242
|
this._elements[this._offset + index] = newElement;
|
|
1180
1243
|
return true;
|
|
1181
1244
|
}
|
|
1245
|
+
/**
|
|
1246
|
+
* Delete the first element that satisfies a predicate.
|
|
1247
|
+
* @remarks Time O(N), Space O(N)
|
|
1248
|
+
* @param predicate - Function (value, index, queue) → boolean to decide deletion.
|
|
1249
|
+
* @returns True if a match was removed.
|
|
1250
|
+
*/
|
|
1251
|
+
deleteWhere(predicate) {
|
|
1252
|
+
for (let i = 0; i < this.length; i++) {
|
|
1253
|
+
if (predicate(this._elements[this._offset + i], i, this)) {
|
|
1254
|
+
this.deleteAt(i);
|
|
1255
|
+
return true;
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
return false;
|
|
1259
|
+
}
|
|
1182
1260
|
/**
|
|
1183
1261
|
* Reverse the queue in-place by compacting then reversing.
|
|
1184
1262
|
* @remarks Time O(N), Space O(N)
|
|
@@ -1215,6 +1293,13 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1215
1293
|
|
|
1216
1294
|
|
|
1217
1295
|
|
|
1296
|
+
|
|
1297
|
+
|
|
1298
|
+
|
|
1299
|
+
|
|
1300
|
+
|
|
1301
|
+
|
|
1302
|
+
|
|
1218
1303
|
|
|
1219
1304
|
|
|
1220
1305
|
|
|
@@ -1258,6 +1343,13 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1258
1343
|
|
|
1259
1344
|
|
|
1260
1345
|
|
|
1346
|
+
|
|
1347
|
+
|
|
1348
|
+
|
|
1349
|
+
|
|
1350
|
+
|
|
1351
|
+
|
|
1352
|
+
|
|
1261
1353
|
|
|
1262
1354
|
|
|
1263
1355
|
|
|
@@ -1324,6 +1416,13 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1324
1416
|
|
|
1325
1417
|
|
|
1326
1418
|
|
|
1419
|
+
|
|
1420
|
+
|
|
1421
|
+
|
|
1422
|
+
|
|
1423
|
+
|
|
1424
|
+
|
|
1425
|
+
|
|
1327
1426
|
|
|
1328
1427
|
|
|
1329
1428
|
|
|
@@ -1374,6 +1473,13 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1374
1473
|
|
|
1375
1474
|
|
|
1376
1475
|
|
|
1476
|
+
|
|
1477
|
+
|
|
1478
|
+
|
|
1479
|
+
|
|
1480
|
+
|
|
1481
|
+
|
|
1482
|
+
|
|
1377
1483
|
|
|
1378
1484
|
|
|
1379
1485
|
|
|
@@ -1428,6 +1534,13 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1428
1534
|
|
|
1429
1535
|
|
|
1430
1536
|
|
|
1537
|
+
|
|
1538
|
+
|
|
1539
|
+
|
|
1540
|
+
|
|
1541
|
+
|
|
1542
|
+
|
|
1543
|
+
|
|
1431
1544
|
|
|
1432
1545
|
|
|
1433
1546
|
|
|
@@ -1704,7 +1817,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1704
1817
|
if (isMapMode !== void 0) this._isMapMode = isMapMode;
|
|
1705
1818
|
if (isDuplicate !== void 0) this._isDuplicate = isDuplicate;
|
|
1706
1819
|
if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
|
|
1707
|
-
else if (toEntryFn)
|
|
1820
|
+
else if (toEntryFn) raise(TypeError, ERR.notAFunction("toEntryFn", "BinaryTree"));
|
|
1708
1821
|
}
|
|
1709
1822
|
if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
|
|
1710
1823
|
}
|
|
@@ -1910,7 +2023,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1910
2023
|
}
|
|
1911
2024
|
/**
|
|
1912
2025
|
* Adds a new node to the tree.
|
|
1913
|
-
* @remarks Time O(
|
|
2026
|
+
* @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).
|
|
1914
2027
|
*
|
|
1915
2028
|
* @param keyNodeOrEntry - The key, node, or entry to add.
|
|
1916
2029
|
* @returns True if the addition was successful, false otherwise.
|
|
@@ -1933,6 +2046,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1933
2046
|
|
|
1934
2047
|
|
|
1935
2048
|
|
|
2049
|
+
|
|
2050
|
+
|
|
2051
|
+
|
|
2052
|
+
|
|
2053
|
+
|
|
2054
|
+
|
|
2055
|
+
|
|
1936
2056
|
|
|
1937
2057
|
|
|
1938
2058
|
|
|
@@ -1955,7 +2075,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1955
2075
|
}
|
|
1956
2076
|
/**
|
|
1957
2077
|
* Adds or updates a new node to the tree.
|
|
1958
|
-
* @remarks Time O(
|
|
2078
|
+
* @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).
|
|
1959
2079
|
*
|
|
1960
2080
|
* @param keyNodeOrEntry - The key, node, or entry to set or update.
|
|
1961
2081
|
* @param [value] - The value, if providing just a key.
|
|
@@ -1984,6 +2104,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1984
2104
|
|
|
1985
2105
|
|
|
1986
2106
|
|
|
2107
|
+
|
|
2108
|
+
|
|
2109
|
+
|
|
2110
|
+
|
|
2111
|
+
|
|
2112
|
+
|
|
2113
|
+
|
|
1987
2114
|
|
|
1988
2115
|
|
|
1989
2116
|
|
|
@@ -2087,6 +2214,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2087
2214
|
|
|
2088
2215
|
|
|
2089
2216
|
|
|
2217
|
+
|
|
2218
|
+
|
|
2219
|
+
|
|
2220
|
+
|
|
2221
|
+
|
|
2222
|
+
|
|
2223
|
+
|
|
2090
2224
|
|
|
2091
2225
|
|
|
2092
2226
|
|
|
@@ -2126,6 +2260,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2126
2260
|
|
|
2127
2261
|
|
|
2128
2262
|
|
|
2263
|
+
|
|
2264
|
+
|
|
2265
|
+
|
|
2266
|
+
|
|
2267
|
+
|
|
2268
|
+
|
|
2269
|
+
|
|
2129
2270
|
|
|
2130
2271
|
|
|
2131
2272
|
|
|
@@ -2186,6 +2327,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2186
2327
|
|
|
2187
2328
|
|
|
2188
2329
|
|
|
2330
|
+
|
|
2331
|
+
|
|
2332
|
+
|
|
2333
|
+
|
|
2334
|
+
|
|
2335
|
+
|
|
2336
|
+
|
|
2189
2337
|
|
|
2190
2338
|
|
|
2191
2339
|
|
|
@@ -2205,22 +2353,69 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2205
2353
|
this.setMany(anotherTree, []);
|
|
2206
2354
|
}
|
|
2207
2355
|
/**
|
|
2208
|
-
*
|
|
2209
|
-
* @remarks Time O(N)
|
|
2356
|
+
* Deletes a node from the tree (internal, returns balancing metadata).
|
|
2357
|
+
* @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).
|
|
2358
|
+
* @internal Used by AVL/BST subclasses that need balancing metadata after deletion.
|
|
2210
2359
|
*
|
|
2211
|
-
* @param
|
|
2212
|
-
* @
|
|
2360
|
+
* @param keyNodeEntryRawOrPredicate - The node to delete.
|
|
2361
|
+
* @returns An array containing deletion results with balancing metadata.
|
|
2213
2362
|
*/
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
this.
|
|
2363
|
+
_deleteInternal(keyNodeEntryRawOrPredicate) {
|
|
2364
|
+
const deletedResult = [];
|
|
2365
|
+
if (!this._root) return deletedResult;
|
|
2366
|
+
const curr = this.getNode(keyNodeEntryRawOrPredicate);
|
|
2367
|
+
if (!curr) return deletedResult;
|
|
2368
|
+
const parent = curr == null ? void 0 : curr.parent;
|
|
2369
|
+
let needBalanced;
|
|
2370
|
+
let orgCurrent = curr;
|
|
2371
|
+
if (!curr.left && !curr.right && !parent) {
|
|
2372
|
+
this._setRoot(void 0);
|
|
2373
|
+
} else if (curr.left) {
|
|
2374
|
+
const leftSubTreeRightMost = this.getRightMost((node) => node, curr.left);
|
|
2375
|
+
if (leftSubTreeRightMost) {
|
|
2376
|
+
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
2377
|
+
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
|
|
2378
|
+
if (this._isMapMode) {
|
|
2379
|
+
this._store.set(curr.key, curr);
|
|
2380
|
+
this._store.set(leftSubTreeRightMost.key, leftSubTreeRightMost);
|
|
2381
|
+
}
|
|
2382
|
+
if (parentOfLeftSubTreeMax) {
|
|
2383
|
+
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
2384
|
+
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
2385
|
+
else parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
|
|
2386
|
+
needBalanced = parentOfLeftSubTreeMax;
|
|
2387
|
+
}
|
|
2388
|
+
}
|
|
2389
|
+
} else if (parent) {
|
|
2390
|
+
const { familyPosition: fp } = curr;
|
|
2391
|
+
if (fp === "LEFT" || fp === "ROOT_LEFT") {
|
|
2392
|
+
parent.left = curr.right;
|
|
2393
|
+
} else if (fp === "RIGHT" || fp === "ROOT_RIGHT") {
|
|
2394
|
+
parent.right = curr.right;
|
|
2395
|
+
}
|
|
2396
|
+
needBalanced = parent;
|
|
2397
|
+
} else {
|
|
2398
|
+
this._setRoot(curr.right);
|
|
2399
|
+
curr.right = void 0;
|
|
2400
|
+
}
|
|
2401
|
+
this._size = this._size - 1;
|
|
2402
|
+
deletedResult.push({ deleted: orgCurrent, needBalanced });
|
|
2403
|
+
if (this._isMapMode && orgCurrent) this._store.delete(orgCurrent.key);
|
|
2404
|
+
return deletedResult;
|
|
2217
2405
|
}
|
|
2218
2406
|
/**
|
|
2219
2407
|
* Deletes a node from the tree.
|
|
2220
|
-
* @remarks Time O(
|
|
2408
|
+
* @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).
|
|
2221
2409
|
*
|
|
2222
2410
|
* @param keyNodeEntryRawOrPredicate - The node to delete.
|
|
2223
|
-
* @returns
|
|
2411
|
+
* @returns True if the node was found and deleted, false otherwise.
|
|
2412
|
+
|
|
2413
|
+
|
|
2414
|
+
|
|
2415
|
+
|
|
2416
|
+
|
|
2417
|
+
|
|
2418
|
+
|
|
2224
2419
|
|
|
2225
2420
|
|
|
2226
2421
|
|
|
@@ -2261,51 +2456,11 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2261
2456
|
* console.log(tree.size); // 4;
|
|
2262
2457
|
*/
|
|
2263
2458
|
delete(keyNodeEntryRawOrPredicate) {
|
|
2264
|
-
|
|
2265
|
-
if (!this._root) return deletedResult;
|
|
2266
|
-
const curr = this.getNode(keyNodeEntryRawOrPredicate);
|
|
2267
|
-
if (!curr) return deletedResult;
|
|
2268
|
-
const parent = curr == null ? void 0 : curr.parent;
|
|
2269
|
-
let needBalanced;
|
|
2270
|
-
let orgCurrent = curr;
|
|
2271
|
-
if (!curr.left && !curr.right && !parent) {
|
|
2272
|
-
this._setRoot(void 0);
|
|
2273
|
-
} else if (curr.left) {
|
|
2274
|
-
const leftSubTreeRightMost = this.getRightMost((node) => node, curr.left);
|
|
2275
|
-
if (leftSubTreeRightMost) {
|
|
2276
|
-
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
2277
|
-
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
|
|
2278
|
-
if (this._isMapMode) {
|
|
2279
|
-
this._store.set(curr.key, curr);
|
|
2280
|
-
this._store.set(leftSubTreeRightMost.key, leftSubTreeRightMost);
|
|
2281
|
-
}
|
|
2282
|
-
if (parentOfLeftSubTreeMax) {
|
|
2283
|
-
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
2284
|
-
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
2285
|
-
else parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
|
|
2286
|
-
needBalanced = parentOfLeftSubTreeMax;
|
|
2287
|
-
}
|
|
2288
|
-
}
|
|
2289
|
-
} else if (parent) {
|
|
2290
|
-
const { familyPosition: fp } = curr;
|
|
2291
|
-
if (fp === "LEFT" || fp === "ROOT_LEFT") {
|
|
2292
|
-
parent.left = curr.right;
|
|
2293
|
-
} else if (fp === "RIGHT" || fp === "ROOT_RIGHT") {
|
|
2294
|
-
parent.right = curr.right;
|
|
2295
|
-
}
|
|
2296
|
-
needBalanced = parent;
|
|
2297
|
-
} else {
|
|
2298
|
-
this._setRoot(curr.right);
|
|
2299
|
-
curr.right = void 0;
|
|
2300
|
-
}
|
|
2301
|
-
this._size = this._size - 1;
|
|
2302
|
-
deletedResult.push({ deleted: orgCurrent, needBalanced });
|
|
2303
|
-
if (this._isMapMode && orgCurrent) this._store.delete(orgCurrent.key);
|
|
2304
|
-
return deletedResult;
|
|
2459
|
+
return this._deleteInternal(keyNodeEntryRawOrPredicate).length > 0;
|
|
2305
2460
|
}
|
|
2306
2461
|
/**
|
|
2307
2462
|
* Searches the tree for nodes matching a predicate.
|
|
2308
|
-
* @remarks Time O(
|
|
2463
|
+
* @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).
|
|
2309
2464
|
*
|
|
2310
2465
|
* @template C - The type of the callback function.
|
|
2311
2466
|
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
@@ -2354,7 +2509,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2354
2509
|
}
|
|
2355
2510
|
/**
|
|
2356
2511
|
* Gets the first node matching a predicate.
|
|
2357
|
-
* @remarks Time O(
|
|
2512
|
+
* @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.
|
|
2358
2513
|
*
|
|
2359
2514
|
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
2360
2515
|
* @param [startNode=this._root] - The node to start the search from.
|
|
@@ -2382,6 +2537,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2382
2537
|
|
|
2383
2538
|
|
|
2384
2539
|
|
|
2540
|
+
|
|
2541
|
+
|
|
2542
|
+
|
|
2543
|
+
|
|
2544
|
+
|
|
2545
|
+
|
|
2546
|
+
|
|
2385
2547
|
|
|
2386
2548
|
|
|
2387
2549
|
|
|
@@ -2407,7 +2569,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2407
2569
|
}
|
|
2408
2570
|
/**
|
|
2409
2571
|
* Gets the value associated with a key.
|
|
2410
|
-
* @remarks Time O(
|
|
2572
|
+
* @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).
|
|
2411
2573
|
*
|
|
2412
2574
|
* @param keyNodeEntryOrPredicate - The key, node, or entry to get the value for.
|
|
2413
2575
|
* @param [startNode=this._root] - The node to start searching from (if not in Map mode).
|
|
@@ -2437,6 +2599,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2437
2599
|
|
|
2438
2600
|
|
|
2439
2601
|
|
|
2602
|
+
|
|
2603
|
+
|
|
2604
|
+
|
|
2605
|
+
|
|
2606
|
+
|
|
2607
|
+
|
|
2608
|
+
|
|
2440
2609
|
|
|
2441
2610
|
|
|
2442
2611
|
|
|
@@ -2495,6 +2664,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2495
2664
|
|
|
2496
2665
|
|
|
2497
2666
|
|
|
2667
|
+
|
|
2668
|
+
|
|
2669
|
+
|
|
2670
|
+
|
|
2671
|
+
|
|
2672
|
+
|
|
2673
|
+
|
|
2498
2674
|
|
|
2499
2675
|
|
|
2500
2676
|
|
|
@@ -2540,6 +2716,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2540
2716
|
|
|
2541
2717
|
|
|
2542
2718
|
|
|
2719
|
+
|
|
2720
|
+
|
|
2721
|
+
|
|
2722
|
+
|
|
2723
|
+
|
|
2724
|
+
|
|
2725
|
+
|
|
2543
2726
|
|
|
2544
2727
|
|
|
2545
2728
|
|
|
@@ -2594,6 +2777,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2594
2777
|
|
|
2595
2778
|
|
|
2596
2779
|
|
|
2780
|
+
|
|
2781
|
+
|
|
2782
|
+
|
|
2783
|
+
|
|
2784
|
+
|
|
2785
|
+
|
|
2786
|
+
|
|
2597
2787
|
|
|
2598
2788
|
|
|
2599
2789
|
|
|
@@ -2675,6 +2865,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2675
2865
|
|
|
2676
2866
|
|
|
2677
2867
|
|
|
2868
|
+
|
|
2869
|
+
|
|
2870
|
+
|
|
2871
|
+
|
|
2872
|
+
|
|
2873
|
+
|
|
2874
|
+
|
|
2678
2875
|
|
|
2679
2876
|
|
|
2680
2877
|
|
|
@@ -2733,6 +2930,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2733
2930
|
|
|
2734
2931
|
|
|
2735
2932
|
|
|
2933
|
+
|
|
2934
|
+
|
|
2935
|
+
|
|
2936
|
+
|
|
2937
|
+
|
|
2938
|
+
|
|
2939
|
+
|
|
2736
2940
|
|
|
2737
2941
|
|
|
2738
2942
|
|
|
@@ -3207,6 +3411,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3207
3411
|
|
|
3208
3412
|
|
|
3209
3413
|
|
|
3414
|
+
|
|
3415
|
+
|
|
3416
|
+
|
|
3417
|
+
|
|
3418
|
+
|
|
3419
|
+
|
|
3420
|
+
|
|
3210
3421
|
|
|
3211
3422
|
|
|
3212
3423
|
|
|
@@ -3256,6 +3467,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3256
3467
|
|
|
3257
3468
|
|
|
3258
3469
|
|
|
3470
|
+
|
|
3471
|
+
|
|
3472
|
+
|
|
3473
|
+
|
|
3474
|
+
|
|
3475
|
+
|
|
3476
|
+
|
|
3259
3477
|
|
|
3260
3478
|
|
|
3261
3479
|
|
|
@@ -3309,6 +3527,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3309
3527
|
|
|
3310
3528
|
|
|
3311
3529
|
|
|
3530
|
+
|
|
3531
|
+
|
|
3532
|
+
|
|
3533
|
+
|
|
3534
|
+
|
|
3535
|
+
|
|
3536
|
+
|
|
3312
3537
|
|
|
3313
3538
|
|
|
3314
3539
|
|
|
@@ -3387,6 +3612,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3387
3612
|
|
|
3388
3613
|
|
|
3389
3614
|
|
|
3615
|
+
|
|
3616
|
+
|
|
3617
|
+
|
|
3618
|
+
|
|
3619
|
+
|
|
3620
|
+
|
|
3621
|
+
|
|
3390
3622
|
|
|
3391
3623
|
|
|
3392
3624
|
|
|
@@ -3875,5 +4107,6 @@ exports.BinaryTreeNode = BinaryTreeNode;
|
|
|
3875
4107
|
exports.DFSOperation = DFSOperation;
|
|
3876
4108
|
exports.ERR = ERR;
|
|
3877
4109
|
exports.Range = Range;
|
|
4110
|
+
exports.raise = raise;
|
|
3878
4111
|
//# sourceMappingURL=index.cjs.map
|
|
3879
4112
|
//# sourceMappingURL=index.cjs.map
|