avl-tree-typed 2.5.2 → 2.6.0
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 +486 -61
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +486 -61
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +486 -61
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +486 -61
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/iterable-element-base.d.ts +17 -0
- package/dist/types/data-structures/base/linear-base.d.ts +6 -0
- 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 +191 -15
- package/dist/types/data-structures/binary-tree/bst.d.ts +171 -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 +1061 -167
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1232 -355
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +916 -194
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +1078 -141
- 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 +150 -2
- 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 +171 -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/umd/avl-tree-typed.js +486 -61
- package/dist/umd/avl-tree-typed.js.map +1 -1
- package/dist/umd/avl-tree-typed.min.js +2 -2
- package/dist/umd/avl-tree-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/base/iterable-element-base.ts +32 -0
- package/src/data-structures/base/linear-base.ts +11 -0
- package/src/data-structures/binary-tree/avl-tree.ts +88 -5
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +98 -0
- package/src/data-structures/binary-tree/binary-tree.ts +242 -81
- package/src/data-structures/binary-tree/bst.ts +173 -7
- package/src/data-structures/binary-tree/red-black-tree.ts +139 -15
- package/src/data-structures/binary-tree/segment-tree.ts +42 -0
- package/src/data-structures/binary-tree/tree-map.ts +948 -36
- package/src/data-structures/binary-tree/tree-multi-map.ts +893 -13
- package/src/data-structures/binary-tree/tree-multi-set.ts +761 -33
- package/src/data-structures/binary-tree/tree-set.ts +1260 -251
- 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 +100 -12
- package/src/data-structures/heap/heap.ts +149 -19
- package/src/data-structures/linked-list/doubly-linked-list.ts +178 -2
- package/src/data-structures/linked-list/singly-linked-list.ts +106 -1
- package/src/data-structures/linked-list/skip-linked-list.ts +126 -0
- package/src/data-structures/matrix/matrix.ts +56 -0
- package/src/data-structures/queue/deque.ts +187 -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 +84 -0
- package/src/interfaces/binary-tree.ts +1 -9
package/dist/cjs/index.cjs
CHANGED
|
@@ -261,6 +261,35 @@ var IterableElementBase = class {
|
|
|
261
261
|
for (const ele of this) if (ele === element) return true;
|
|
262
262
|
return false;
|
|
263
263
|
}
|
|
264
|
+
/**
|
|
265
|
+
* Check whether a value exists (Array-compatible alias for `has`).
|
|
266
|
+
* @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1).
|
|
267
|
+
* @param element - Element to search for (uses `===`).
|
|
268
|
+
* @returns `true` if found.
|
|
269
|
+
*/
|
|
270
|
+
includes(element) {
|
|
271
|
+
return this.has(element);
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Return an iterator of `[index, value]` pairs (Array-compatible).
|
|
275
|
+
* @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1) per step.
|
|
276
|
+
*/
|
|
277
|
+
*entries() {
|
|
278
|
+
let index = 0;
|
|
279
|
+
for (const value of this) {
|
|
280
|
+
yield [index++, value];
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Return an iterator of numeric indices (Array-compatible).
|
|
285
|
+
* @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1) per step.
|
|
286
|
+
*/
|
|
287
|
+
*keys() {
|
|
288
|
+
let index = 0;
|
|
289
|
+
for (const _ of this) {
|
|
290
|
+
yield index++;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
264
293
|
/**
|
|
265
294
|
* Reduces all elements to a single accumulated value.
|
|
266
295
|
*
|
|
@@ -523,6 +552,16 @@ var LinearBase = class _LinearBase extends IterableElementBase {
|
|
|
523
552
|
}
|
|
524
553
|
return this;
|
|
525
554
|
}
|
|
555
|
+
/**
|
|
556
|
+
* Return a new instance of the same type with elements in reverse order (non-mutating).
|
|
557
|
+
* @remarks Provided for familiarity when migrating from Array (ES2023 `toReversed`). Time O(n), Space O(n).
|
|
558
|
+
* @returns A new reversed instance.
|
|
559
|
+
*/
|
|
560
|
+
toReversed() {
|
|
561
|
+
const cloned = this.clone();
|
|
562
|
+
cloned.reverse();
|
|
563
|
+
return cloned;
|
|
564
|
+
}
|
|
526
565
|
};
|
|
527
566
|
|
|
528
567
|
// src/data-structures/base/iterable-entry-base.ts
|
|
@@ -794,6 +833,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
794
833
|
|
|
795
834
|
|
|
796
835
|
|
|
836
|
+
|
|
837
|
+
|
|
838
|
+
|
|
839
|
+
|
|
840
|
+
|
|
841
|
+
|
|
842
|
+
|
|
797
843
|
|
|
798
844
|
|
|
799
845
|
|
|
@@ -844,6 +890,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
844
890
|
|
|
845
891
|
|
|
846
892
|
|
|
893
|
+
|
|
894
|
+
|
|
895
|
+
|
|
896
|
+
|
|
897
|
+
|
|
898
|
+
|
|
899
|
+
|
|
847
900
|
|
|
848
901
|
|
|
849
902
|
|
|
@@ -861,6 +914,14 @@ var Queue = class _Queue extends LinearBase {
|
|
|
861
914
|
get first() {
|
|
862
915
|
return this.length > 0 ? this.elements[this._offset] : void 0;
|
|
863
916
|
}
|
|
917
|
+
/**
|
|
918
|
+
* Peek at the front element without removing it (alias for `first`).
|
|
919
|
+
* @remarks Time O(1), Space O(1)
|
|
920
|
+
* @returns Front element or undefined.
|
|
921
|
+
*/
|
|
922
|
+
peek() {
|
|
923
|
+
return this.first;
|
|
924
|
+
}
|
|
864
925
|
/**
|
|
865
926
|
* Get the last element (back) without removing it.
|
|
866
927
|
* @remarks Time O(1), Space O(1)
|
|
@@ -910,6 +971,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
910
971
|
|
|
911
972
|
|
|
912
973
|
|
|
974
|
+
|
|
975
|
+
|
|
976
|
+
|
|
977
|
+
|
|
978
|
+
|
|
979
|
+
|
|
980
|
+
|
|
913
981
|
|
|
914
982
|
|
|
915
983
|
|
|
@@ -972,6 +1040,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
972
1040
|
|
|
973
1041
|
|
|
974
1042
|
|
|
1043
|
+
|
|
1044
|
+
|
|
1045
|
+
|
|
1046
|
+
|
|
1047
|
+
|
|
1048
|
+
|
|
1049
|
+
|
|
975
1050
|
|
|
976
1051
|
|
|
977
1052
|
|
|
@@ -1041,6 +1116,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1041
1116
|
|
|
1042
1117
|
|
|
1043
1118
|
|
|
1119
|
+
|
|
1120
|
+
|
|
1121
|
+
|
|
1122
|
+
|
|
1123
|
+
|
|
1124
|
+
|
|
1125
|
+
|
|
1044
1126
|
|
|
1045
1127
|
|
|
1046
1128
|
|
|
@@ -1100,6 +1182,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1100
1182
|
|
|
1101
1183
|
|
|
1102
1184
|
|
|
1185
|
+
|
|
1186
|
+
|
|
1187
|
+
|
|
1188
|
+
|
|
1189
|
+
|
|
1190
|
+
|
|
1191
|
+
|
|
1103
1192
|
|
|
1104
1193
|
|
|
1105
1194
|
|
|
@@ -1152,6 +1241,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1152
1241
|
|
|
1153
1242
|
|
|
1154
1243
|
|
|
1244
|
+
|
|
1245
|
+
|
|
1246
|
+
|
|
1247
|
+
|
|
1248
|
+
|
|
1249
|
+
|
|
1250
|
+
|
|
1155
1251
|
|
|
1156
1252
|
|
|
1157
1253
|
|
|
@@ -1206,6 +1302,21 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1206
1302
|
this._elements[this._offset + index] = newElement;
|
|
1207
1303
|
return true;
|
|
1208
1304
|
}
|
|
1305
|
+
/**
|
|
1306
|
+
* Delete the first element that satisfies a predicate.
|
|
1307
|
+
* @remarks Time O(N), Space O(N)
|
|
1308
|
+
* @param predicate - Function (value, index, queue) → boolean to decide deletion.
|
|
1309
|
+
* @returns True if a match was removed.
|
|
1310
|
+
*/
|
|
1311
|
+
deleteWhere(predicate) {
|
|
1312
|
+
for (let i = 0; i < this.length; i++) {
|
|
1313
|
+
if (predicate(this._elements[this._offset + i], i, this)) {
|
|
1314
|
+
this.deleteAt(i);
|
|
1315
|
+
return true;
|
|
1316
|
+
}
|
|
1317
|
+
}
|
|
1318
|
+
return false;
|
|
1319
|
+
}
|
|
1209
1320
|
/**
|
|
1210
1321
|
* Reverse the queue in-place by compacting then reversing.
|
|
1211
1322
|
* @remarks Time O(N), Space O(N)
|
|
@@ -1245,6 +1356,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1245
1356
|
|
|
1246
1357
|
|
|
1247
1358
|
|
|
1359
|
+
|
|
1360
|
+
|
|
1361
|
+
|
|
1362
|
+
|
|
1363
|
+
|
|
1364
|
+
|
|
1365
|
+
|
|
1248
1366
|
|
|
1249
1367
|
|
|
1250
1368
|
|
|
@@ -1291,6 +1409,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1291
1409
|
|
|
1292
1410
|
|
|
1293
1411
|
|
|
1412
|
+
|
|
1413
|
+
|
|
1414
|
+
|
|
1415
|
+
|
|
1416
|
+
|
|
1417
|
+
|
|
1418
|
+
|
|
1294
1419
|
|
|
1295
1420
|
|
|
1296
1421
|
|
|
@@ -1360,6 +1485,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1360
1485
|
|
|
1361
1486
|
|
|
1362
1487
|
|
|
1488
|
+
|
|
1489
|
+
|
|
1490
|
+
|
|
1491
|
+
|
|
1492
|
+
|
|
1493
|
+
|
|
1494
|
+
|
|
1363
1495
|
|
|
1364
1496
|
|
|
1365
1497
|
|
|
@@ -1413,6 +1545,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1413
1545
|
|
|
1414
1546
|
|
|
1415
1547
|
|
|
1548
|
+
|
|
1549
|
+
|
|
1550
|
+
|
|
1551
|
+
|
|
1552
|
+
|
|
1553
|
+
|
|
1554
|
+
|
|
1416
1555
|
|
|
1417
1556
|
|
|
1418
1557
|
|
|
@@ -1470,6 +1609,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1470
1609
|
|
|
1471
1610
|
|
|
1472
1611
|
|
|
1612
|
+
|
|
1613
|
+
|
|
1614
|
+
|
|
1615
|
+
|
|
1616
|
+
|
|
1617
|
+
|
|
1618
|
+
|
|
1473
1619
|
|
|
1474
1620
|
|
|
1475
1621
|
|
|
@@ -1944,7 +2090,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1944
2090
|
}
|
|
1945
2091
|
/**
|
|
1946
2092
|
* Adds a new node to the tree.
|
|
1947
|
-
* @remarks Time O(
|
|
2093
|
+
* @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).
|
|
1948
2094
|
*
|
|
1949
2095
|
* @param keyNodeOrEntry - The key, node, or entry to add.
|
|
1950
2096
|
* @returns True if the addition was successful, false otherwise.
|
|
@@ -1970,6 +2116,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1970
2116
|
|
|
1971
2117
|
|
|
1972
2118
|
|
|
2119
|
+
|
|
2120
|
+
|
|
2121
|
+
|
|
2122
|
+
|
|
2123
|
+
|
|
2124
|
+
|
|
2125
|
+
|
|
1973
2126
|
|
|
1974
2127
|
|
|
1975
2128
|
|
|
@@ -1992,7 +2145,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1992
2145
|
}
|
|
1993
2146
|
/**
|
|
1994
2147
|
* Adds or updates a new node to the tree.
|
|
1995
|
-
* @remarks Time O(
|
|
2148
|
+
* @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).
|
|
1996
2149
|
*
|
|
1997
2150
|
* @param keyNodeOrEntry - The key, node, or entry to set or update.
|
|
1998
2151
|
* @param [value] - The value, if providing just a key.
|
|
@@ -2024,6 +2177,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2024
2177
|
|
|
2025
2178
|
|
|
2026
2179
|
|
|
2180
|
+
|
|
2181
|
+
|
|
2182
|
+
|
|
2183
|
+
|
|
2184
|
+
|
|
2185
|
+
|
|
2186
|
+
|
|
2027
2187
|
|
|
2028
2188
|
|
|
2029
2189
|
|
|
@@ -2130,6 +2290,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2130
2290
|
|
|
2131
2291
|
|
|
2132
2292
|
|
|
2293
|
+
|
|
2294
|
+
|
|
2295
|
+
|
|
2296
|
+
|
|
2297
|
+
|
|
2298
|
+
|
|
2299
|
+
|
|
2133
2300
|
|
|
2134
2301
|
|
|
2135
2302
|
|
|
@@ -2172,6 +2339,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2172
2339
|
|
|
2173
2340
|
|
|
2174
2341
|
|
|
2342
|
+
|
|
2343
|
+
|
|
2344
|
+
|
|
2345
|
+
|
|
2346
|
+
|
|
2347
|
+
|
|
2348
|
+
|
|
2175
2349
|
|
|
2176
2350
|
|
|
2177
2351
|
|
|
@@ -2235,6 +2409,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2235
2409
|
|
|
2236
2410
|
|
|
2237
2411
|
|
|
2412
|
+
|
|
2413
|
+
|
|
2414
|
+
|
|
2415
|
+
|
|
2416
|
+
|
|
2417
|
+
|
|
2418
|
+
|
|
2238
2419
|
|
|
2239
2420
|
|
|
2240
2421
|
|
|
@@ -2254,22 +2435,69 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2254
2435
|
this.setMany(anotherTree, []);
|
|
2255
2436
|
}
|
|
2256
2437
|
/**
|
|
2257
|
-
*
|
|
2258
|
-
* @remarks Time O(N)
|
|
2438
|
+
* Deletes a node from the tree (internal, returns balancing metadata).
|
|
2439
|
+
* @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).
|
|
2440
|
+
* @internal Used by AVL/BST subclasses that need balancing metadata after deletion.
|
|
2259
2441
|
*
|
|
2260
|
-
* @param
|
|
2261
|
-
* @
|
|
2442
|
+
* @param keyNodeEntryRawOrPredicate - The node to delete.
|
|
2443
|
+
* @returns An array containing deletion results with balancing metadata.
|
|
2262
2444
|
*/
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
this.
|
|
2445
|
+
_deleteInternal(keyNodeEntryRawOrPredicate) {
|
|
2446
|
+
const deletedResult = [];
|
|
2447
|
+
if (!this._root) return deletedResult;
|
|
2448
|
+
const curr = this.getNode(keyNodeEntryRawOrPredicate);
|
|
2449
|
+
if (!curr) return deletedResult;
|
|
2450
|
+
const parent = curr?.parent;
|
|
2451
|
+
let needBalanced;
|
|
2452
|
+
let orgCurrent = curr;
|
|
2453
|
+
if (!curr.left && !curr.right && !parent) {
|
|
2454
|
+
this._setRoot(void 0);
|
|
2455
|
+
} else if (curr.left) {
|
|
2456
|
+
const leftSubTreeRightMost = this.getRightMost((node) => node, curr.left);
|
|
2457
|
+
if (leftSubTreeRightMost) {
|
|
2458
|
+
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
2459
|
+
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
|
|
2460
|
+
if (this._isMapMode) {
|
|
2461
|
+
this._store.set(curr.key, curr);
|
|
2462
|
+
this._store.set(leftSubTreeRightMost.key, leftSubTreeRightMost);
|
|
2463
|
+
}
|
|
2464
|
+
if (parentOfLeftSubTreeMax) {
|
|
2465
|
+
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
2466
|
+
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
2467
|
+
else parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
|
|
2468
|
+
needBalanced = parentOfLeftSubTreeMax;
|
|
2469
|
+
}
|
|
2470
|
+
}
|
|
2471
|
+
} else if (parent) {
|
|
2472
|
+
const { familyPosition: fp } = curr;
|
|
2473
|
+
if (fp === "LEFT" || fp === "ROOT_LEFT") {
|
|
2474
|
+
parent.left = curr.right;
|
|
2475
|
+
} else if (fp === "RIGHT" || fp === "ROOT_RIGHT") {
|
|
2476
|
+
parent.right = curr.right;
|
|
2477
|
+
}
|
|
2478
|
+
needBalanced = parent;
|
|
2479
|
+
} else {
|
|
2480
|
+
this._setRoot(curr.right);
|
|
2481
|
+
curr.right = void 0;
|
|
2482
|
+
}
|
|
2483
|
+
this._size = this._size - 1;
|
|
2484
|
+
deletedResult.push({ deleted: orgCurrent, needBalanced });
|
|
2485
|
+
if (this._isMapMode && orgCurrent) this._store.delete(orgCurrent.key);
|
|
2486
|
+
return deletedResult;
|
|
2266
2487
|
}
|
|
2267
2488
|
/**
|
|
2268
2489
|
* Deletes a node from the tree.
|
|
2269
|
-
* @remarks Time O(
|
|
2490
|
+
* @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).
|
|
2270
2491
|
*
|
|
2271
2492
|
* @param keyNodeEntryRawOrPredicate - The node to delete.
|
|
2272
|
-
* @returns
|
|
2493
|
+
* @returns True if the node was found and deleted, false otherwise.
|
|
2494
|
+
|
|
2495
|
+
|
|
2496
|
+
|
|
2497
|
+
|
|
2498
|
+
|
|
2499
|
+
|
|
2500
|
+
|
|
2273
2501
|
|
|
2274
2502
|
|
|
2275
2503
|
|
|
@@ -2313,51 +2541,11 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2313
2541
|
* console.log(tree.size); // 4;
|
|
2314
2542
|
*/
|
|
2315
2543
|
delete(keyNodeEntryRawOrPredicate) {
|
|
2316
|
-
|
|
2317
|
-
if (!this._root) return deletedResult;
|
|
2318
|
-
const curr = this.getNode(keyNodeEntryRawOrPredicate);
|
|
2319
|
-
if (!curr) return deletedResult;
|
|
2320
|
-
const parent = curr?.parent;
|
|
2321
|
-
let needBalanced;
|
|
2322
|
-
let orgCurrent = curr;
|
|
2323
|
-
if (!curr.left && !curr.right && !parent) {
|
|
2324
|
-
this._setRoot(void 0);
|
|
2325
|
-
} else if (curr.left) {
|
|
2326
|
-
const leftSubTreeRightMost = this.getRightMost((node) => node, curr.left);
|
|
2327
|
-
if (leftSubTreeRightMost) {
|
|
2328
|
-
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
2329
|
-
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
|
|
2330
|
-
if (this._isMapMode) {
|
|
2331
|
-
this._store.set(curr.key, curr);
|
|
2332
|
-
this._store.set(leftSubTreeRightMost.key, leftSubTreeRightMost);
|
|
2333
|
-
}
|
|
2334
|
-
if (parentOfLeftSubTreeMax) {
|
|
2335
|
-
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
2336
|
-
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
2337
|
-
else parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
|
|
2338
|
-
needBalanced = parentOfLeftSubTreeMax;
|
|
2339
|
-
}
|
|
2340
|
-
}
|
|
2341
|
-
} else if (parent) {
|
|
2342
|
-
const { familyPosition: fp } = curr;
|
|
2343
|
-
if (fp === "LEFT" || fp === "ROOT_LEFT") {
|
|
2344
|
-
parent.left = curr.right;
|
|
2345
|
-
} else if (fp === "RIGHT" || fp === "ROOT_RIGHT") {
|
|
2346
|
-
parent.right = curr.right;
|
|
2347
|
-
}
|
|
2348
|
-
needBalanced = parent;
|
|
2349
|
-
} else {
|
|
2350
|
-
this._setRoot(curr.right);
|
|
2351
|
-
curr.right = void 0;
|
|
2352
|
-
}
|
|
2353
|
-
this._size = this._size - 1;
|
|
2354
|
-
deletedResult.push({ deleted: orgCurrent, needBalanced });
|
|
2355
|
-
if (this._isMapMode && orgCurrent) this._store.delete(orgCurrent.key);
|
|
2356
|
-
return deletedResult;
|
|
2544
|
+
return this._deleteInternal(keyNodeEntryRawOrPredicate).length > 0;
|
|
2357
2545
|
}
|
|
2358
2546
|
/**
|
|
2359
2547
|
* Searches the tree for nodes matching a predicate.
|
|
2360
|
-
* @remarks Time O(
|
|
2548
|
+
* @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).
|
|
2361
2549
|
*
|
|
2362
2550
|
* @template C - The type of the callback function.
|
|
2363
2551
|
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
@@ -2406,7 +2594,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2406
2594
|
}
|
|
2407
2595
|
/**
|
|
2408
2596
|
* Gets the first node matching a predicate.
|
|
2409
|
-
* @remarks Time O(
|
|
2597
|
+
* @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.
|
|
2410
2598
|
*
|
|
2411
2599
|
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
2412
2600
|
* @param [startNode=this._root] - The node to start the search from.
|
|
@@ -2437,6 +2625,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2437
2625
|
|
|
2438
2626
|
|
|
2439
2627
|
|
|
2628
|
+
|
|
2629
|
+
|
|
2630
|
+
|
|
2631
|
+
|
|
2632
|
+
|
|
2633
|
+
|
|
2634
|
+
|
|
2440
2635
|
|
|
2441
2636
|
|
|
2442
2637
|
|
|
@@ -2462,7 +2657,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2462
2657
|
}
|
|
2463
2658
|
/**
|
|
2464
2659
|
* Gets the value associated with a key.
|
|
2465
|
-
* @remarks Time O(
|
|
2660
|
+
* @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).
|
|
2466
2661
|
*
|
|
2467
2662
|
* @param keyNodeEntryOrPredicate - The key, node, or entry to get the value for.
|
|
2468
2663
|
* @param [startNode=this._root] - The node to start searching from (if not in Map mode).
|
|
@@ -2495,6 +2690,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2495
2690
|
|
|
2496
2691
|
|
|
2497
2692
|
|
|
2693
|
+
|
|
2694
|
+
|
|
2695
|
+
|
|
2696
|
+
|
|
2697
|
+
|
|
2698
|
+
|
|
2699
|
+
|
|
2498
2700
|
|
|
2499
2701
|
|
|
2500
2702
|
|
|
@@ -2555,6 +2757,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2555
2757
|
|
|
2556
2758
|
|
|
2557
2759
|
|
|
2760
|
+
|
|
2761
|
+
|
|
2762
|
+
|
|
2763
|
+
|
|
2764
|
+
|
|
2765
|
+
|
|
2766
|
+
|
|
2558
2767
|
|
|
2559
2768
|
|
|
2560
2769
|
|
|
@@ -2603,6 +2812,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2603
2812
|
|
|
2604
2813
|
|
|
2605
2814
|
|
|
2815
|
+
|
|
2816
|
+
|
|
2817
|
+
|
|
2818
|
+
|
|
2819
|
+
|
|
2820
|
+
|
|
2821
|
+
|
|
2606
2822
|
|
|
2607
2823
|
|
|
2608
2824
|
|
|
@@ -2660,6 +2876,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2660
2876
|
|
|
2661
2877
|
|
|
2662
2878
|
|
|
2879
|
+
|
|
2880
|
+
|
|
2881
|
+
|
|
2882
|
+
|
|
2883
|
+
|
|
2884
|
+
|
|
2885
|
+
|
|
2663
2886
|
|
|
2664
2887
|
|
|
2665
2888
|
|
|
@@ -2744,6 +2967,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2744
2967
|
|
|
2745
2968
|
|
|
2746
2969
|
|
|
2970
|
+
|
|
2971
|
+
|
|
2972
|
+
|
|
2973
|
+
|
|
2974
|
+
|
|
2975
|
+
|
|
2976
|
+
|
|
2747
2977
|
|
|
2748
2978
|
|
|
2749
2979
|
|
|
@@ -2805,6 +3035,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2805
3035
|
|
|
2806
3036
|
|
|
2807
3037
|
|
|
3038
|
+
|
|
3039
|
+
|
|
3040
|
+
|
|
3041
|
+
|
|
3042
|
+
|
|
3043
|
+
|
|
3044
|
+
|
|
2808
3045
|
|
|
2809
3046
|
|
|
2810
3047
|
|
|
@@ -3282,6 +3519,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3282
3519
|
|
|
3283
3520
|
|
|
3284
3521
|
|
|
3522
|
+
|
|
3523
|
+
|
|
3524
|
+
|
|
3525
|
+
|
|
3526
|
+
|
|
3527
|
+
|
|
3528
|
+
|
|
3285
3529
|
|
|
3286
3530
|
|
|
3287
3531
|
|
|
@@ -3334,6 +3578,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3334
3578
|
|
|
3335
3579
|
|
|
3336
3580
|
|
|
3581
|
+
|
|
3582
|
+
|
|
3583
|
+
|
|
3584
|
+
|
|
3585
|
+
|
|
3586
|
+
|
|
3587
|
+
|
|
3337
3588
|
|
|
3338
3589
|
|
|
3339
3590
|
|
|
@@ -3390,6 +3641,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3390
3641
|
|
|
3391
3642
|
|
|
3392
3643
|
|
|
3644
|
+
|
|
3645
|
+
|
|
3646
|
+
|
|
3647
|
+
|
|
3648
|
+
|
|
3649
|
+
|
|
3650
|
+
|
|
3393
3651
|
|
|
3394
3652
|
|
|
3395
3653
|
|
|
@@ -3471,6 +3729,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3471
3729
|
|
|
3472
3730
|
|
|
3473
3731
|
|
|
3732
|
+
|
|
3733
|
+
|
|
3734
|
+
|
|
3735
|
+
|
|
3736
|
+
|
|
3737
|
+
|
|
3738
|
+
|
|
3474
3739
|
|
|
3475
3740
|
|
|
3476
3741
|
|
|
@@ -4287,6 +4552,20 @@ var BST = class extends BinaryTree {
|
|
|
4287
4552
|
|
|
4288
4553
|
|
|
4289
4554
|
|
|
4555
|
+
|
|
4556
|
+
|
|
4557
|
+
|
|
4558
|
+
|
|
4559
|
+
|
|
4560
|
+
|
|
4561
|
+
|
|
4562
|
+
|
|
4563
|
+
|
|
4564
|
+
|
|
4565
|
+
|
|
4566
|
+
|
|
4567
|
+
|
|
4568
|
+
|
|
4290
4569
|
|
|
4291
4570
|
|
|
4292
4571
|
|
|
@@ -4617,6 +4896,27 @@ var BST = class extends BinaryTree {
|
|
|
4617
4896
|
|
|
4618
4897
|
|
|
4619
4898
|
|
|
4899
|
+
|
|
4900
|
+
|
|
4901
|
+
|
|
4902
|
+
|
|
4903
|
+
|
|
4904
|
+
|
|
4905
|
+
|
|
4906
|
+
|
|
4907
|
+
|
|
4908
|
+
|
|
4909
|
+
|
|
4910
|
+
|
|
4911
|
+
|
|
4912
|
+
|
|
4913
|
+
|
|
4914
|
+
|
|
4915
|
+
|
|
4916
|
+
|
|
4917
|
+
|
|
4918
|
+
|
|
4919
|
+
|
|
4620
4920
|
|
|
4621
4921
|
|
|
4622
4922
|
|
|
@@ -4738,6 +5038,20 @@ var BST = class extends BinaryTree {
|
|
|
4738
5038
|
|
|
4739
5039
|
|
|
4740
5040
|
|
|
5041
|
+
|
|
5042
|
+
|
|
5043
|
+
|
|
5044
|
+
|
|
5045
|
+
|
|
5046
|
+
|
|
5047
|
+
|
|
5048
|
+
|
|
5049
|
+
|
|
5050
|
+
|
|
5051
|
+
|
|
5052
|
+
|
|
5053
|
+
|
|
5054
|
+
|
|
4741
5055
|
|
|
4742
5056
|
|
|
4743
5057
|
|
|
@@ -5032,6 +5346,13 @@ var BST = class extends BinaryTree {
|
|
|
5032
5346
|
|
|
5033
5347
|
|
|
5034
5348
|
|
|
5349
|
+
|
|
5350
|
+
|
|
5351
|
+
|
|
5352
|
+
|
|
5353
|
+
|
|
5354
|
+
|
|
5355
|
+
|
|
5035
5356
|
|
|
5036
5357
|
|
|
5037
5358
|
|
|
@@ -5101,6 +5422,13 @@ var BST = class extends BinaryTree {
|
|
|
5101
5422
|
|
|
5102
5423
|
|
|
5103
5424
|
|
|
5425
|
+
|
|
5426
|
+
|
|
5427
|
+
|
|
5428
|
+
|
|
5429
|
+
|
|
5430
|
+
|
|
5431
|
+
|
|
5104
5432
|
|
|
5105
5433
|
|
|
5106
5434
|
|
|
@@ -5216,6 +5544,20 @@ var BST = class extends BinaryTree {
|
|
|
5216
5544
|
|
|
5217
5545
|
|
|
5218
5546
|
|
|
5547
|
+
|
|
5548
|
+
|
|
5549
|
+
|
|
5550
|
+
|
|
5551
|
+
|
|
5552
|
+
|
|
5553
|
+
|
|
5554
|
+
|
|
5555
|
+
|
|
5556
|
+
|
|
5557
|
+
|
|
5558
|
+
|
|
5559
|
+
|
|
5560
|
+
|
|
5219
5561
|
|
|
5220
5562
|
|
|
5221
5563
|
|
|
@@ -5283,12 +5625,11 @@ var BST = class extends BinaryTree {
|
|
|
5283
5625
|
*/
|
|
5284
5626
|
deleteWhere(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
|
|
5285
5627
|
const toDelete = this.search(keyNodeEntryOrPredicate, onlyOne, (node) => node, startNode, iterationType);
|
|
5286
|
-
let
|
|
5628
|
+
let deleted = false;
|
|
5287
5629
|
for (const node of toDelete) {
|
|
5288
|
-
|
|
5289
|
-
results = results.concat(deleteInfo);
|
|
5630
|
+
if (this.delete(node)) deleted = true;
|
|
5290
5631
|
}
|
|
5291
|
-
return
|
|
5632
|
+
return deleted;
|
|
5292
5633
|
}
|
|
5293
5634
|
/**
|
|
5294
5635
|
* (Protected) Creates the default comparator function for keys that don't have a custom comparator.
|
|
@@ -6116,6 +6457,34 @@ var AVLTree = class extends BST {
|
|
|
6116
6457
|
|
|
6117
6458
|
|
|
6118
6459
|
|
|
6460
|
+
|
|
6461
|
+
|
|
6462
|
+
|
|
6463
|
+
|
|
6464
|
+
|
|
6465
|
+
|
|
6466
|
+
|
|
6467
|
+
|
|
6468
|
+
|
|
6469
|
+
|
|
6470
|
+
|
|
6471
|
+
|
|
6472
|
+
|
|
6473
|
+
|
|
6474
|
+
|
|
6475
|
+
|
|
6476
|
+
|
|
6477
|
+
|
|
6478
|
+
|
|
6479
|
+
|
|
6480
|
+
|
|
6481
|
+
|
|
6482
|
+
|
|
6483
|
+
|
|
6484
|
+
|
|
6485
|
+
|
|
6486
|
+
|
|
6487
|
+
|
|
6119
6488
|
|
|
6120
6489
|
|
|
6121
6490
|
|
|
@@ -6240,6 +6609,27 @@ var AVLTree = class extends BST {
|
|
|
6240
6609
|
|
|
6241
6610
|
|
|
6242
6611
|
|
|
6612
|
+
|
|
6613
|
+
|
|
6614
|
+
|
|
6615
|
+
|
|
6616
|
+
|
|
6617
|
+
|
|
6618
|
+
|
|
6619
|
+
|
|
6620
|
+
|
|
6621
|
+
|
|
6622
|
+
|
|
6623
|
+
|
|
6624
|
+
|
|
6625
|
+
|
|
6626
|
+
|
|
6627
|
+
|
|
6628
|
+
|
|
6629
|
+
|
|
6630
|
+
|
|
6631
|
+
|
|
6632
|
+
|
|
6243
6633
|
|
|
6244
6634
|
|
|
6245
6635
|
|
|
@@ -6269,13 +6659,13 @@ var AVLTree = class extends BST {
|
|
|
6269
6659
|
* console.log(avl.size); // 6;
|
|
6270
6660
|
*/
|
|
6271
6661
|
delete(keyNodeOrEntry) {
|
|
6272
|
-
const deletedResults =
|
|
6662
|
+
const deletedResults = this._deleteInternal(keyNodeOrEntry);
|
|
6273
6663
|
for (const { needBalanced } of deletedResults) {
|
|
6274
6664
|
if (needBalanced) {
|
|
6275
6665
|
this._balancePath(needBalanced);
|
|
6276
6666
|
}
|
|
6277
6667
|
}
|
|
6278
|
-
return deletedResults;
|
|
6668
|
+
return deletedResults.length > 0;
|
|
6279
6669
|
}
|
|
6280
6670
|
/**
|
|
6281
6671
|
* Rebuilds the tree to be perfectly balanced.
|
|
@@ -6329,6 +6719,20 @@ var AVLTree = class extends BST {
|
|
|
6329
6719
|
|
|
6330
6720
|
|
|
6331
6721
|
|
|
6722
|
+
|
|
6723
|
+
|
|
6724
|
+
|
|
6725
|
+
|
|
6726
|
+
|
|
6727
|
+
|
|
6728
|
+
|
|
6729
|
+
|
|
6730
|
+
|
|
6731
|
+
|
|
6732
|
+
|
|
6733
|
+
|
|
6734
|
+
|
|
6735
|
+
|
|
6332
6736
|
|
|
6333
6737
|
|
|
6334
6738
|
|
|
@@ -6459,6 +6863,27 @@ var AVLTree = class extends BST {
|
|
|
6459
6863
|
|
|
6460
6864
|
|
|
6461
6865
|
|
|
6866
|
+
|
|
6867
|
+
|
|
6868
|
+
|
|
6869
|
+
|
|
6870
|
+
|
|
6871
|
+
|
|
6872
|
+
|
|
6873
|
+
|
|
6874
|
+
|
|
6875
|
+
|
|
6876
|
+
|
|
6877
|
+
|
|
6878
|
+
|
|
6879
|
+
|
|
6880
|
+
|
|
6881
|
+
|
|
6882
|
+
|
|
6883
|
+
|
|
6884
|
+
|
|
6885
|
+
|
|
6886
|
+
|
|
6462
6887
|
|
|
6463
6888
|
|
|
6464
6889
|
|