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
|
@@ -27,7 +27,8 @@ var binaryTreeTyped = (() => {
|
|
|
27
27
|
BinaryTreeNode: () => BinaryTreeNode,
|
|
28
28
|
DFSOperation: () => DFSOperation,
|
|
29
29
|
ERR: () => ERR,
|
|
30
|
-
Range: () => Range
|
|
30
|
+
Range: () => Range,
|
|
31
|
+
raise: () => raise
|
|
31
32
|
});
|
|
32
33
|
|
|
33
34
|
// src/utils/utils.ts
|
|
@@ -81,6 +82,57 @@ var binaryTreeTyped = (() => {
|
|
|
81
82
|
return (...args) => trampoline(fn(...args));
|
|
82
83
|
}
|
|
83
84
|
|
|
85
|
+
// src/common/error.ts
|
|
86
|
+
function raise(ErrorClass, message) {
|
|
87
|
+
throw new ErrorClass(message);
|
|
88
|
+
}
|
|
89
|
+
var ERR = {
|
|
90
|
+
// Range / index
|
|
91
|
+
indexOutOfRange: (index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`,
|
|
92
|
+
invalidIndex: (ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`,
|
|
93
|
+
// Type / argument
|
|
94
|
+
invalidArgument: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
95
|
+
comparatorRequired: (ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`,
|
|
96
|
+
invalidKey: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
97
|
+
notAFunction: (name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`,
|
|
98
|
+
invalidEntry: (ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`,
|
|
99
|
+
invalidNaN: (ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`,
|
|
100
|
+
invalidDate: (ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`,
|
|
101
|
+
reduceEmpty: (ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`,
|
|
102
|
+
callbackReturnType: (expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`,
|
|
103
|
+
// State / operation
|
|
104
|
+
invalidOperation: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
105
|
+
// Matrix
|
|
106
|
+
matrixDimensionMismatch: (op) => `Matrix: Dimensions must be compatible for ${op}.`,
|
|
107
|
+
matrixSingular: () => "Matrix: Singular matrix, inverse does not exist.",
|
|
108
|
+
matrixNotSquare: () => "Matrix: Must be square for inversion.",
|
|
109
|
+
matrixNotRectangular: () => "Matrix: Must be rectangular for transposition.",
|
|
110
|
+
matrixRowMismatch: (expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`,
|
|
111
|
+
// Order statistic
|
|
112
|
+
orderStatisticNotEnabled: (method, ctx) => `${ctx ? ctx + ": " : ""}${method}() requires enableOrderStatistic: true.`
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
// src/common/index.ts
|
|
116
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
117
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
118
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
119
|
+
return DFSOperation2;
|
|
120
|
+
})(DFSOperation || {});
|
|
121
|
+
var Range = class {
|
|
122
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
123
|
+
this.low = low;
|
|
124
|
+
this.high = high;
|
|
125
|
+
this.includeLow = includeLow;
|
|
126
|
+
this.includeHigh = includeHigh;
|
|
127
|
+
}
|
|
128
|
+
// Determine whether a key is within the range
|
|
129
|
+
isInRange(key, comparator) {
|
|
130
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
131
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
132
|
+
return lowCheck && highCheck;
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
|
|
84
136
|
// src/data-structures/base/iterable-element-base.ts
|
|
85
137
|
var IterableElementBase = class {
|
|
86
138
|
/**
|
|
@@ -103,7 +155,7 @@ var binaryTreeTyped = (() => {
|
|
|
103
155
|
if (options) {
|
|
104
156
|
const { toElementFn } = options;
|
|
105
157
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
106
|
-
else if (toElementFn)
|
|
158
|
+
else if (toElementFn) raise(TypeError, "toElementFn must be a function type");
|
|
107
159
|
}
|
|
108
160
|
}
|
|
109
161
|
/**
|
|
@@ -259,7 +311,7 @@ var binaryTreeTyped = (() => {
|
|
|
259
311
|
acc = initialValue;
|
|
260
312
|
} else {
|
|
261
313
|
const first = iter.next();
|
|
262
|
-
if (first.done)
|
|
314
|
+
if (first.done) raise(TypeError, "Reduce of empty structure with no initial value");
|
|
263
315
|
acc = first.value;
|
|
264
316
|
index = 1;
|
|
265
317
|
}
|
|
@@ -491,52 +543,6 @@ var binaryTreeTyped = (() => {
|
|
|
491
543
|
}
|
|
492
544
|
};
|
|
493
545
|
|
|
494
|
-
// src/common/error.ts
|
|
495
|
-
var ERR = {
|
|
496
|
-
// Range / index
|
|
497
|
-
indexOutOfRange: (index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`,
|
|
498
|
-
invalidIndex: (ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`,
|
|
499
|
-
// Type / argument
|
|
500
|
-
invalidArgument: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
501
|
-
comparatorRequired: (ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`,
|
|
502
|
-
invalidKey: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
503
|
-
notAFunction: (name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`,
|
|
504
|
-
invalidEntry: (ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`,
|
|
505
|
-
invalidNaN: (ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`,
|
|
506
|
-
invalidDate: (ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`,
|
|
507
|
-
reduceEmpty: (ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`,
|
|
508
|
-
callbackReturnType: (expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`,
|
|
509
|
-
// State / operation
|
|
510
|
-
invalidOperation: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
511
|
-
// Matrix
|
|
512
|
-
matrixDimensionMismatch: (op) => `Matrix: Dimensions must be compatible for ${op}.`,
|
|
513
|
-
matrixSingular: () => "Matrix: Singular matrix, inverse does not exist.",
|
|
514
|
-
matrixNotSquare: () => "Matrix: Must be square for inversion.",
|
|
515
|
-
matrixNotRectangular: () => "Matrix: Must be rectangular for transposition.",
|
|
516
|
-
matrixRowMismatch: (expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`
|
|
517
|
-
};
|
|
518
|
-
|
|
519
|
-
// src/common/index.ts
|
|
520
|
-
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
521
|
-
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
522
|
-
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
523
|
-
return DFSOperation2;
|
|
524
|
-
})(DFSOperation || {});
|
|
525
|
-
var Range = class {
|
|
526
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
527
|
-
this.low = low;
|
|
528
|
-
this.high = high;
|
|
529
|
-
this.includeLow = includeLow;
|
|
530
|
-
this.includeHigh = includeHigh;
|
|
531
|
-
}
|
|
532
|
-
// Determine whether a key is within the range
|
|
533
|
-
isInRange(key, comparator) {
|
|
534
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
535
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
536
|
-
return lowCheck && highCheck;
|
|
537
|
-
}
|
|
538
|
-
};
|
|
539
|
-
|
|
540
546
|
// src/data-structures/base/iterable-entry-base.ts
|
|
541
547
|
var IterableEntryBase = class {
|
|
542
548
|
/**
|
|
@@ -797,6 +803,13 @@ var binaryTreeTyped = (() => {
|
|
|
797
803
|
|
|
798
804
|
|
|
799
805
|
|
|
806
|
+
|
|
807
|
+
|
|
808
|
+
|
|
809
|
+
|
|
810
|
+
|
|
811
|
+
|
|
812
|
+
|
|
800
813
|
|
|
801
814
|
|
|
802
815
|
|
|
@@ -844,6 +857,13 @@ var binaryTreeTyped = (() => {
|
|
|
844
857
|
|
|
845
858
|
|
|
846
859
|
|
|
860
|
+
|
|
861
|
+
|
|
862
|
+
|
|
863
|
+
|
|
864
|
+
|
|
865
|
+
|
|
866
|
+
|
|
847
867
|
|
|
848
868
|
|
|
849
869
|
|
|
@@ -861,6 +881,14 @@ var binaryTreeTyped = (() => {
|
|
|
861
881
|
get first() {
|
|
862
882
|
return this.length > 0 ? this.elements[this._offset] : void 0;
|
|
863
883
|
}
|
|
884
|
+
/**
|
|
885
|
+
* Peek at the front element without removing it (alias for `first`).
|
|
886
|
+
* @remarks Time O(1), Space O(1)
|
|
887
|
+
* @returns Front element or undefined.
|
|
888
|
+
*/
|
|
889
|
+
peek() {
|
|
890
|
+
return this.first;
|
|
891
|
+
}
|
|
864
892
|
/**
|
|
865
893
|
* Get the last element (back) without removing it.
|
|
866
894
|
* @remarks Time O(1), Space O(1)
|
|
@@ -907,6 +935,13 @@ var binaryTreeTyped = (() => {
|
|
|
907
935
|
|
|
908
936
|
|
|
909
937
|
|
|
938
|
+
|
|
939
|
+
|
|
940
|
+
|
|
941
|
+
|
|
942
|
+
|
|
943
|
+
|
|
944
|
+
|
|
910
945
|
|
|
911
946
|
|
|
912
947
|
|
|
@@ -966,6 +1001,13 @@ var binaryTreeTyped = (() => {
|
|
|
966
1001
|
|
|
967
1002
|
|
|
968
1003
|
|
|
1004
|
+
|
|
1005
|
+
|
|
1006
|
+
|
|
1007
|
+
|
|
1008
|
+
|
|
1009
|
+
|
|
1010
|
+
|
|
969
1011
|
|
|
970
1012
|
|
|
971
1013
|
|
|
@@ -1032,6 +1074,13 @@ var binaryTreeTyped = (() => {
|
|
|
1032
1074
|
|
|
1033
1075
|
|
|
1034
1076
|
|
|
1077
|
+
|
|
1078
|
+
|
|
1079
|
+
|
|
1080
|
+
|
|
1081
|
+
|
|
1082
|
+
|
|
1083
|
+
|
|
1035
1084
|
|
|
1036
1085
|
|
|
1037
1086
|
|
|
@@ -1088,6 +1137,13 @@ var binaryTreeTyped = (() => {
|
|
|
1088
1137
|
|
|
1089
1138
|
|
|
1090
1139
|
|
|
1140
|
+
|
|
1141
|
+
|
|
1142
|
+
|
|
1143
|
+
|
|
1144
|
+
|
|
1145
|
+
|
|
1146
|
+
|
|
1091
1147
|
|
|
1092
1148
|
|
|
1093
1149
|
|
|
@@ -1137,6 +1193,13 @@ var binaryTreeTyped = (() => {
|
|
|
1137
1193
|
|
|
1138
1194
|
|
|
1139
1195
|
|
|
1196
|
+
|
|
1197
|
+
|
|
1198
|
+
|
|
1199
|
+
|
|
1200
|
+
|
|
1201
|
+
|
|
1202
|
+
|
|
1140
1203
|
|
|
1141
1204
|
|
|
1142
1205
|
|
|
@@ -1191,6 +1254,21 @@ var binaryTreeTyped = (() => {
|
|
|
1191
1254
|
this._elements[this._offset + index] = newElement;
|
|
1192
1255
|
return true;
|
|
1193
1256
|
}
|
|
1257
|
+
/**
|
|
1258
|
+
* Delete the first element that satisfies a predicate.
|
|
1259
|
+
* @remarks Time O(N), Space O(N)
|
|
1260
|
+
* @param predicate - Function (value, index, queue) → boolean to decide deletion.
|
|
1261
|
+
* @returns True if a match was removed.
|
|
1262
|
+
*/
|
|
1263
|
+
deleteWhere(predicate) {
|
|
1264
|
+
for (let i = 0; i < this.length; i++) {
|
|
1265
|
+
if (predicate(this._elements[this._offset + i], i, this)) {
|
|
1266
|
+
this.deleteAt(i);
|
|
1267
|
+
return true;
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
return false;
|
|
1271
|
+
}
|
|
1194
1272
|
/**
|
|
1195
1273
|
* Reverse the queue in-place by compacting then reversing.
|
|
1196
1274
|
* @remarks Time O(N), Space O(N)
|
|
@@ -1227,6 +1305,13 @@ var binaryTreeTyped = (() => {
|
|
|
1227
1305
|
|
|
1228
1306
|
|
|
1229
1307
|
|
|
1308
|
+
|
|
1309
|
+
|
|
1310
|
+
|
|
1311
|
+
|
|
1312
|
+
|
|
1313
|
+
|
|
1314
|
+
|
|
1230
1315
|
|
|
1231
1316
|
|
|
1232
1317
|
|
|
@@ -1270,6 +1355,13 @@ var binaryTreeTyped = (() => {
|
|
|
1270
1355
|
|
|
1271
1356
|
|
|
1272
1357
|
|
|
1358
|
+
|
|
1359
|
+
|
|
1360
|
+
|
|
1361
|
+
|
|
1362
|
+
|
|
1363
|
+
|
|
1364
|
+
|
|
1273
1365
|
|
|
1274
1366
|
|
|
1275
1367
|
|
|
@@ -1336,6 +1428,13 @@ var binaryTreeTyped = (() => {
|
|
|
1336
1428
|
|
|
1337
1429
|
|
|
1338
1430
|
|
|
1431
|
+
|
|
1432
|
+
|
|
1433
|
+
|
|
1434
|
+
|
|
1435
|
+
|
|
1436
|
+
|
|
1437
|
+
|
|
1339
1438
|
|
|
1340
1439
|
|
|
1341
1440
|
|
|
@@ -1386,6 +1485,13 @@ var binaryTreeTyped = (() => {
|
|
|
1386
1485
|
|
|
1387
1486
|
|
|
1388
1487
|
|
|
1488
|
+
|
|
1489
|
+
|
|
1490
|
+
|
|
1491
|
+
|
|
1492
|
+
|
|
1493
|
+
|
|
1494
|
+
|
|
1389
1495
|
|
|
1390
1496
|
|
|
1391
1497
|
|
|
@@ -1440,6 +1546,13 @@ var binaryTreeTyped = (() => {
|
|
|
1440
1546
|
|
|
1441
1547
|
|
|
1442
1548
|
|
|
1549
|
+
|
|
1550
|
+
|
|
1551
|
+
|
|
1552
|
+
|
|
1553
|
+
|
|
1554
|
+
|
|
1555
|
+
|
|
1443
1556
|
|
|
1444
1557
|
|
|
1445
1558
|
|
|
@@ -1712,7 +1825,7 @@ var binaryTreeTyped = (() => {
|
|
|
1712
1825
|
if (isMapMode !== void 0) this._isMapMode = isMapMode;
|
|
1713
1826
|
if (isDuplicate !== void 0) this._isDuplicate = isDuplicate;
|
|
1714
1827
|
if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
|
|
1715
|
-
else if (toEntryFn)
|
|
1828
|
+
else if (toEntryFn) raise(TypeError, ERR.notAFunction("toEntryFn", "BinaryTree"));
|
|
1716
1829
|
}
|
|
1717
1830
|
if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
|
|
1718
1831
|
}
|
|
@@ -1918,7 +2031,7 @@ var binaryTreeTyped = (() => {
|
|
|
1918
2031
|
}
|
|
1919
2032
|
/**
|
|
1920
2033
|
* Adds a new node to the tree.
|
|
1921
|
-
* @remarks Time O(
|
|
2034
|
+
* @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).
|
|
1922
2035
|
*
|
|
1923
2036
|
* @param keyNodeOrEntry - The key, node, or entry to add.
|
|
1924
2037
|
* @returns True if the addition was successful, false otherwise.
|
|
@@ -1941,6 +2054,13 @@ var binaryTreeTyped = (() => {
|
|
|
1941
2054
|
|
|
1942
2055
|
|
|
1943
2056
|
|
|
2057
|
+
|
|
2058
|
+
|
|
2059
|
+
|
|
2060
|
+
|
|
2061
|
+
|
|
2062
|
+
|
|
2063
|
+
|
|
1944
2064
|
|
|
1945
2065
|
|
|
1946
2066
|
|
|
@@ -1963,7 +2083,7 @@ var binaryTreeTyped = (() => {
|
|
|
1963
2083
|
}
|
|
1964
2084
|
/**
|
|
1965
2085
|
* Adds or updates a new node to the tree.
|
|
1966
|
-
* @remarks Time O(
|
|
2086
|
+
* @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).
|
|
1967
2087
|
*
|
|
1968
2088
|
* @param keyNodeOrEntry - The key, node, or entry to set or update.
|
|
1969
2089
|
* @param [value] - The value, if providing just a key.
|
|
@@ -1992,6 +2112,13 @@ var binaryTreeTyped = (() => {
|
|
|
1992
2112
|
|
|
1993
2113
|
|
|
1994
2114
|
|
|
2115
|
+
|
|
2116
|
+
|
|
2117
|
+
|
|
2118
|
+
|
|
2119
|
+
|
|
2120
|
+
|
|
2121
|
+
|
|
1995
2122
|
|
|
1996
2123
|
|
|
1997
2124
|
|
|
@@ -2095,6 +2222,13 @@ var binaryTreeTyped = (() => {
|
|
|
2095
2222
|
|
|
2096
2223
|
|
|
2097
2224
|
|
|
2225
|
+
|
|
2226
|
+
|
|
2227
|
+
|
|
2228
|
+
|
|
2229
|
+
|
|
2230
|
+
|
|
2231
|
+
|
|
2098
2232
|
|
|
2099
2233
|
|
|
2100
2234
|
|
|
@@ -2134,6 +2268,13 @@ var binaryTreeTyped = (() => {
|
|
|
2134
2268
|
|
|
2135
2269
|
|
|
2136
2270
|
|
|
2271
|
+
|
|
2272
|
+
|
|
2273
|
+
|
|
2274
|
+
|
|
2275
|
+
|
|
2276
|
+
|
|
2277
|
+
|
|
2137
2278
|
|
|
2138
2279
|
|
|
2139
2280
|
|
|
@@ -2194,6 +2335,13 @@ var binaryTreeTyped = (() => {
|
|
|
2194
2335
|
|
|
2195
2336
|
|
|
2196
2337
|
|
|
2338
|
+
|
|
2339
|
+
|
|
2340
|
+
|
|
2341
|
+
|
|
2342
|
+
|
|
2343
|
+
|
|
2344
|
+
|
|
2197
2345
|
|
|
2198
2346
|
|
|
2199
2347
|
|
|
@@ -2213,22 +2361,69 @@ var binaryTreeTyped = (() => {
|
|
|
2213
2361
|
this.setMany(anotherTree, []);
|
|
2214
2362
|
}
|
|
2215
2363
|
/**
|
|
2216
|
-
*
|
|
2217
|
-
* @remarks Time O(N)
|
|
2364
|
+
* Deletes a node from the tree (internal, returns balancing metadata).
|
|
2365
|
+
* @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).
|
|
2366
|
+
* @internal Used by AVL/BST subclasses that need balancing metadata after deletion.
|
|
2218
2367
|
*
|
|
2219
|
-
* @param
|
|
2220
|
-
* @
|
|
2368
|
+
* @param keyNodeEntryRawOrPredicate - The node to delete.
|
|
2369
|
+
* @returns An array containing deletion results with balancing metadata.
|
|
2221
2370
|
*/
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
this.
|
|
2371
|
+
_deleteInternal(keyNodeEntryRawOrPredicate) {
|
|
2372
|
+
const deletedResult = [];
|
|
2373
|
+
if (!this._root) return deletedResult;
|
|
2374
|
+
const curr = this.getNode(keyNodeEntryRawOrPredicate);
|
|
2375
|
+
if (!curr) return deletedResult;
|
|
2376
|
+
const parent = curr == null ? void 0 : curr.parent;
|
|
2377
|
+
let needBalanced;
|
|
2378
|
+
let orgCurrent = curr;
|
|
2379
|
+
if (!curr.left && !curr.right && !parent) {
|
|
2380
|
+
this._setRoot(void 0);
|
|
2381
|
+
} else if (curr.left) {
|
|
2382
|
+
const leftSubTreeRightMost = this.getRightMost((node) => node, curr.left);
|
|
2383
|
+
if (leftSubTreeRightMost) {
|
|
2384
|
+
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
2385
|
+
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
|
|
2386
|
+
if (this._isMapMode) {
|
|
2387
|
+
this._store.set(curr.key, curr);
|
|
2388
|
+
this._store.set(leftSubTreeRightMost.key, leftSubTreeRightMost);
|
|
2389
|
+
}
|
|
2390
|
+
if (parentOfLeftSubTreeMax) {
|
|
2391
|
+
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
2392
|
+
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
2393
|
+
else parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
|
|
2394
|
+
needBalanced = parentOfLeftSubTreeMax;
|
|
2395
|
+
}
|
|
2396
|
+
}
|
|
2397
|
+
} else if (parent) {
|
|
2398
|
+
const { familyPosition: fp } = curr;
|
|
2399
|
+
if (fp === "LEFT" || fp === "ROOT_LEFT") {
|
|
2400
|
+
parent.left = curr.right;
|
|
2401
|
+
} else if (fp === "RIGHT" || fp === "ROOT_RIGHT") {
|
|
2402
|
+
parent.right = curr.right;
|
|
2403
|
+
}
|
|
2404
|
+
needBalanced = parent;
|
|
2405
|
+
} else {
|
|
2406
|
+
this._setRoot(curr.right);
|
|
2407
|
+
curr.right = void 0;
|
|
2408
|
+
}
|
|
2409
|
+
this._size = this._size - 1;
|
|
2410
|
+
deletedResult.push({ deleted: orgCurrent, needBalanced });
|
|
2411
|
+
if (this._isMapMode && orgCurrent) this._store.delete(orgCurrent.key);
|
|
2412
|
+
return deletedResult;
|
|
2225
2413
|
}
|
|
2226
2414
|
/**
|
|
2227
2415
|
* Deletes a node from the tree.
|
|
2228
|
-
* @remarks Time O(
|
|
2416
|
+
* @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).
|
|
2229
2417
|
*
|
|
2230
2418
|
* @param keyNodeEntryRawOrPredicate - The node to delete.
|
|
2231
|
-
* @returns
|
|
2419
|
+
* @returns True if the node was found and deleted, false otherwise.
|
|
2420
|
+
|
|
2421
|
+
|
|
2422
|
+
|
|
2423
|
+
|
|
2424
|
+
|
|
2425
|
+
|
|
2426
|
+
|
|
2232
2427
|
|
|
2233
2428
|
|
|
2234
2429
|
|
|
@@ -2269,51 +2464,11 @@ var binaryTreeTyped = (() => {
|
|
|
2269
2464
|
* console.log(tree.size); // 4;
|
|
2270
2465
|
*/
|
|
2271
2466
|
delete(keyNodeEntryRawOrPredicate) {
|
|
2272
|
-
|
|
2273
|
-
if (!this._root) return deletedResult;
|
|
2274
|
-
const curr = this.getNode(keyNodeEntryRawOrPredicate);
|
|
2275
|
-
if (!curr) return deletedResult;
|
|
2276
|
-
const parent = curr == null ? void 0 : curr.parent;
|
|
2277
|
-
let needBalanced;
|
|
2278
|
-
let orgCurrent = curr;
|
|
2279
|
-
if (!curr.left && !curr.right && !parent) {
|
|
2280
|
-
this._setRoot(void 0);
|
|
2281
|
-
} else if (curr.left) {
|
|
2282
|
-
const leftSubTreeRightMost = this.getRightMost((node) => node, curr.left);
|
|
2283
|
-
if (leftSubTreeRightMost) {
|
|
2284
|
-
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
2285
|
-
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
|
|
2286
|
-
if (this._isMapMode) {
|
|
2287
|
-
this._store.set(curr.key, curr);
|
|
2288
|
-
this._store.set(leftSubTreeRightMost.key, leftSubTreeRightMost);
|
|
2289
|
-
}
|
|
2290
|
-
if (parentOfLeftSubTreeMax) {
|
|
2291
|
-
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
2292
|
-
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
2293
|
-
else parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
|
|
2294
|
-
needBalanced = parentOfLeftSubTreeMax;
|
|
2295
|
-
}
|
|
2296
|
-
}
|
|
2297
|
-
} else if (parent) {
|
|
2298
|
-
const { familyPosition: fp } = curr;
|
|
2299
|
-
if (fp === "LEFT" || fp === "ROOT_LEFT") {
|
|
2300
|
-
parent.left = curr.right;
|
|
2301
|
-
} else if (fp === "RIGHT" || fp === "ROOT_RIGHT") {
|
|
2302
|
-
parent.right = curr.right;
|
|
2303
|
-
}
|
|
2304
|
-
needBalanced = parent;
|
|
2305
|
-
} else {
|
|
2306
|
-
this._setRoot(curr.right);
|
|
2307
|
-
curr.right = void 0;
|
|
2308
|
-
}
|
|
2309
|
-
this._size = this._size - 1;
|
|
2310
|
-
deletedResult.push({ deleted: orgCurrent, needBalanced });
|
|
2311
|
-
if (this._isMapMode && orgCurrent) this._store.delete(orgCurrent.key);
|
|
2312
|
-
return deletedResult;
|
|
2467
|
+
return this._deleteInternal(keyNodeEntryRawOrPredicate).length > 0;
|
|
2313
2468
|
}
|
|
2314
2469
|
/**
|
|
2315
2470
|
* Searches the tree for nodes matching a predicate.
|
|
2316
|
-
* @remarks Time O(
|
|
2471
|
+
* @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).
|
|
2317
2472
|
*
|
|
2318
2473
|
* @template C - The type of the callback function.
|
|
2319
2474
|
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
@@ -2362,7 +2517,7 @@ var binaryTreeTyped = (() => {
|
|
|
2362
2517
|
}
|
|
2363
2518
|
/**
|
|
2364
2519
|
* Gets the first node matching a predicate.
|
|
2365
|
-
* @remarks Time O(
|
|
2520
|
+
* @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.
|
|
2366
2521
|
*
|
|
2367
2522
|
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
2368
2523
|
* @param [startNode=this._root] - The node to start the search from.
|
|
@@ -2390,6 +2545,13 @@ var binaryTreeTyped = (() => {
|
|
|
2390
2545
|
|
|
2391
2546
|
|
|
2392
2547
|
|
|
2548
|
+
|
|
2549
|
+
|
|
2550
|
+
|
|
2551
|
+
|
|
2552
|
+
|
|
2553
|
+
|
|
2554
|
+
|
|
2393
2555
|
|
|
2394
2556
|
|
|
2395
2557
|
|
|
@@ -2415,7 +2577,7 @@ var binaryTreeTyped = (() => {
|
|
|
2415
2577
|
}
|
|
2416
2578
|
/**
|
|
2417
2579
|
* Gets the value associated with a key.
|
|
2418
|
-
* @remarks Time O(
|
|
2580
|
+
* @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).
|
|
2419
2581
|
*
|
|
2420
2582
|
* @param keyNodeEntryOrPredicate - The key, node, or entry to get the value for.
|
|
2421
2583
|
* @param [startNode=this._root] - The node to start searching from (if not in Map mode).
|
|
@@ -2445,6 +2607,13 @@ var binaryTreeTyped = (() => {
|
|
|
2445
2607
|
|
|
2446
2608
|
|
|
2447
2609
|
|
|
2610
|
+
|
|
2611
|
+
|
|
2612
|
+
|
|
2613
|
+
|
|
2614
|
+
|
|
2615
|
+
|
|
2616
|
+
|
|
2448
2617
|
|
|
2449
2618
|
|
|
2450
2619
|
|
|
@@ -2503,6 +2672,13 @@ var binaryTreeTyped = (() => {
|
|
|
2503
2672
|
|
|
2504
2673
|
|
|
2505
2674
|
|
|
2675
|
+
|
|
2676
|
+
|
|
2677
|
+
|
|
2678
|
+
|
|
2679
|
+
|
|
2680
|
+
|
|
2681
|
+
|
|
2506
2682
|
|
|
2507
2683
|
|
|
2508
2684
|
|
|
@@ -2548,6 +2724,13 @@ var binaryTreeTyped = (() => {
|
|
|
2548
2724
|
|
|
2549
2725
|
|
|
2550
2726
|
|
|
2727
|
+
|
|
2728
|
+
|
|
2729
|
+
|
|
2730
|
+
|
|
2731
|
+
|
|
2732
|
+
|
|
2733
|
+
|
|
2551
2734
|
|
|
2552
2735
|
|
|
2553
2736
|
|
|
@@ -2602,6 +2785,13 @@ var binaryTreeTyped = (() => {
|
|
|
2602
2785
|
|
|
2603
2786
|
|
|
2604
2787
|
|
|
2788
|
+
|
|
2789
|
+
|
|
2790
|
+
|
|
2791
|
+
|
|
2792
|
+
|
|
2793
|
+
|
|
2794
|
+
|
|
2605
2795
|
|
|
2606
2796
|
|
|
2607
2797
|
|
|
@@ -2683,6 +2873,13 @@ var binaryTreeTyped = (() => {
|
|
|
2683
2873
|
|
|
2684
2874
|
|
|
2685
2875
|
|
|
2876
|
+
|
|
2877
|
+
|
|
2878
|
+
|
|
2879
|
+
|
|
2880
|
+
|
|
2881
|
+
|
|
2882
|
+
|
|
2686
2883
|
|
|
2687
2884
|
|
|
2688
2885
|
|
|
@@ -2741,6 +2938,13 @@ var binaryTreeTyped = (() => {
|
|
|
2741
2938
|
|
|
2742
2939
|
|
|
2743
2940
|
|
|
2941
|
+
|
|
2942
|
+
|
|
2943
|
+
|
|
2944
|
+
|
|
2945
|
+
|
|
2946
|
+
|
|
2947
|
+
|
|
2744
2948
|
|
|
2745
2949
|
|
|
2746
2950
|
|
|
@@ -3215,6 +3419,13 @@ var binaryTreeTyped = (() => {
|
|
|
3215
3419
|
|
|
3216
3420
|
|
|
3217
3421
|
|
|
3422
|
+
|
|
3423
|
+
|
|
3424
|
+
|
|
3425
|
+
|
|
3426
|
+
|
|
3427
|
+
|
|
3428
|
+
|
|
3218
3429
|
|
|
3219
3430
|
|
|
3220
3431
|
|
|
@@ -3264,6 +3475,13 @@ var binaryTreeTyped = (() => {
|
|
|
3264
3475
|
|
|
3265
3476
|
|
|
3266
3477
|
|
|
3478
|
+
|
|
3479
|
+
|
|
3480
|
+
|
|
3481
|
+
|
|
3482
|
+
|
|
3483
|
+
|
|
3484
|
+
|
|
3267
3485
|
|
|
3268
3486
|
|
|
3269
3487
|
|
|
@@ -3317,6 +3535,13 @@ var binaryTreeTyped = (() => {
|
|
|
3317
3535
|
|
|
3318
3536
|
|
|
3319
3537
|
|
|
3538
|
+
|
|
3539
|
+
|
|
3540
|
+
|
|
3541
|
+
|
|
3542
|
+
|
|
3543
|
+
|
|
3544
|
+
|
|
3320
3545
|
|
|
3321
3546
|
|
|
3322
3547
|
|
|
@@ -3395,6 +3620,13 @@ var binaryTreeTyped = (() => {
|
|
|
3395
3620
|
|
|
3396
3621
|
|
|
3397
3622
|
|
|
3623
|
+
|
|
3624
|
+
|
|
3625
|
+
|
|
3626
|
+
|
|
3627
|
+
|
|
3628
|
+
|
|
3629
|
+
|
|
3398
3630
|
|
|
3399
3631
|
|
|
3400
3632
|
|