binary-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 +320 -55
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +320 -55
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +320 -55
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +320 -55
- 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/binary-tree-typed.js +320 -55
- 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/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/esm/index.mjs
CHANGED
|
@@ -264,6 +264,35 @@ var IterableElementBase = class {
|
|
|
264
264
|
for (const ele of this) if (ele === element) return true;
|
|
265
265
|
return false;
|
|
266
266
|
}
|
|
267
|
+
/**
|
|
268
|
+
* Check whether a value exists (Array-compatible alias for `has`).
|
|
269
|
+
* @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1).
|
|
270
|
+
* @param element - Element to search for (uses `===`).
|
|
271
|
+
* @returns `true` if found.
|
|
272
|
+
*/
|
|
273
|
+
includes(element) {
|
|
274
|
+
return this.has(element);
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Return an iterator of `[index, value]` pairs (Array-compatible).
|
|
278
|
+
* @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1) per step.
|
|
279
|
+
*/
|
|
280
|
+
*entries() {
|
|
281
|
+
let index = 0;
|
|
282
|
+
for (const value of this) {
|
|
283
|
+
yield [index++, value];
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Return an iterator of numeric indices (Array-compatible).
|
|
288
|
+
* @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1) per step.
|
|
289
|
+
*/
|
|
290
|
+
*keys() {
|
|
291
|
+
let index = 0;
|
|
292
|
+
for (const _ of this) {
|
|
293
|
+
yield index++;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
267
296
|
/**
|
|
268
297
|
* Reduces all elements to a single accumulated value.
|
|
269
298
|
*
|
|
@@ -526,6 +555,16 @@ var LinearBase = class _LinearBase extends IterableElementBase {
|
|
|
526
555
|
}
|
|
527
556
|
return this;
|
|
528
557
|
}
|
|
558
|
+
/**
|
|
559
|
+
* Return a new instance of the same type with elements in reverse order (non-mutating).
|
|
560
|
+
* @remarks Provided for familiarity when migrating from Array (ES2023 `toReversed`). Time O(n), Space O(n).
|
|
561
|
+
* @returns A new reversed instance.
|
|
562
|
+
*/
|
|
563
|
+
toReversed() {
|
|
564
|
+
const cloned = this.clone();
|
|
565
|
+
cloned.reverse();
|
|
566
|
+
return cloned;
|
|
567
|
+
}
|
|
529
568
|
};
|
|
530
569
|
|
|
531
570
|
// src/data-structures/base/iterable-entry-base.ts
|
|
@@ -797,6 +836,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
797
836
|
|
|
798
837
|
|
|
799
838
|
|
|
839
|
+
|
|
840
|
+
|
|
841
|
+
|
|
842
|
+
|
|
843
|
+
|
|
844
|
+
|
|
845
|
+
|
|
800
846
|
|
|
801
847
|
|
|
802
848
|
|
|
@@ -847,6 +893,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
847
893
|
|
|
848
894
|
|
|
849
895
|
|
|
896
|
+
|
|
897
|
+
|
|
898
|
+
|
|
899
|
+
|
|
900
|
+
|
|
901
|
+
|
|
902
|
+
|
|
850
903
|
|
|
851
904
|
|
|
852
905
|
|
|
@@ -864,6 +917,14 @@ var Queue = class _Queue extends LinearBase {
|
|
|
864
917
|
get first() {
|
|
865
918
|
return this.length > 0 ? this.elements[this._offset] : void 0;
|
|
866
919
|
}
|
|
920
|
+
/**
|
|
921
|
+
* Peek at the front element without removing it (alias for `first`).
|
|
922
|
+
* @remarks Time O(1), Space O(1)
|
|
923
|
+
* @returns Front element or undefined.
|
|
924
|
+
*/
|
|
925
|
+
peek() {
|
|
926
|
+
return this.first;
|
|
927
|
+
}
|
|
867
928
|
/**
|
|
868
929
|
* Get the last element (back) without removing it.
|
|
869
930
|
* @remarks Time O(1), Space O(1)
|
|
@@ -913,6 +974,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
913
974
|
|
|
914
975
|
|
|
915
976
|
|
|
977
|
+
|
|
978
|
+
|
|
979
|
+
|
|
980
|
+
|
|
981
|
+
|
|
982
|
+
|
|
983
|
+
|
|
916
984
|
|
|
917
985
|
|
|
918
986
|
|
|
@@ -975,6 +1043,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
975
1043
|
|
|
976
1044
|
|
|
977
1045
|
|
|
1046
|
+
|
|
1047
|
+
|
|
1048
|
+
|
|
1049
|
+
|
|
1050
|
+
|
|
1051
|
+
|
|
1052
|
+
|
|
978
1053
|
|
|
979
1054
|
|
|
980
1055
|
|
|
@@ -1044,6 +1119,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1044
1119
|
|
|
1045
1120
|
|
|
1046
1121
|
|
|
1122
|
+
|
|
1123
|
+
|
|
1124
|
+
|
|
1125
|
+
|
|
1126
|
+
|
|
1127
|
+
|
|
1128
|
+
|
|
1047
1129
|
|
|
1048
1130
|
|
|
1049
1131
|
|
|
@@ -1103,6 +1185,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1103
1185
|
|
|
1104
1186
|
|
|
1105
1187
|
|
|
1188
|
+
|
|
1189
|
+
|
|
1190
|
+
|
|
1191
|
+
|
|
1192
|
+
|
|
1193
|
+
|
|
1194
|
+
|
|
1106
1195
|
|
|
1107
1196
|
|
|
1108
1197
|
|
|
@@ -1155,6 +1244,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1155
1244
|
|
|
1156
1245
|
|
|
1157
1246
|
|
|
1247
|
+
|
|
1248
|
+
|
|
1249
|
+
|
|
1250
|
+
|
|
1251
|
+
|
|
1252
|
+
|
|
1253
|
+
|
|
1158
1254
|
|
|
1159
1255
|
|
|
1160
1256
|
|
|
@@ -1209,6 +1305,21 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1209
1305
|
this._elements[this._offset + index] = newElement;
|
|
1210
1306
|
return true;
|
|
1211
1307
|
}
|
|
1308
|
+
/**
|
|
1309
|
+
* Delete the first element that satisfies a predicate.
|
|
1310
|
+
* @remarks Time O(N), Space O(N)
|
|
1311
|
+
* @param predicate - Function (value, index, queue) → boolean to decide deletion.
|
|
1312
|
+
* @returns True if a match was removed.
|
|
1313
|
+
*/
|
|
1314
|
+
deleteWhere(predicate) {
|
|
1315
|
+
for (let i = 0; i < this.length; i++) {
|
|
1316
|
+
if (predicate(this._elements[this._offset + i], i, this)) {
|
|
1317
|
+
this.deleteAt(i);
|
|
1318
|
+
return true;
|
|
1319
|
+
}
|
|
1320
|
+
}
|
|
1321
|
+
return false;
|
|
1322
|
+
}
|
|
1212
1323
|
/**
|
|
1213
1324
|
* Reverse the queue in-place by compacting then reversing.
|
|
1214
1325
|
* @remarks Time O(N), Space O(N)
|
|
@@ -1248,6 +1359,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1248
1359
|
|
|
1249
1360
|
|
|
1250
1361
|
|
|
1362
|
+
|
|
1363
|
+
|
|
1364
|
+
|
|
1365
|
+
|
|
1366
|
+
|
|
1367
|
+
|
|
1368
|
+
|
|
1251
1369
|
|
|
1252
1370
|
|
|
1253
1371
|
|
|
@@ -1294,6 +1412,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1294
1412
|
|
|
1295
1413
|
|
|
1296
1414
|
|
|
1415
|
+
|
|
1416
|
+
|
|
1417
|
+
|
|
1418
|
+
|
|
1419
|
+
|
|
1420
|
+
|
|
1421
|
+
|
|
1297
1422
|
|
|
1298
1423
|
|
|
1299
1424
|
|
|
@@ -1363,6 +1488,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1363
1488
|
|
|
1364
1489
|
|
|
1365
1490
|
|
|
1491
|
+
|
|
1492
|
+
|
|
1493
|
+
|
|
1494
|
+
|
|
1495
|
+
|
|
1496
|
+
|
|
1497
|
+
|
|
1366
1498
|
|
|
1367
1499
|
|
|
1368
1500
|
|
|
@@ -1416,6 +1548,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1416
1548
|
|
|
1417
1549
|
|
|
1418
1550
|
|
|
1551
|
+
|
|
1552
|
+
|
|
1553
|
+
|
|
1554
|
+
|
|
1555
|
+
|
|
1556
|
+
|
|
1557
|
+
|
|
1419
1558
|
|
|
1420
1559
|
|
|
1421
1560
|
|
|
@@ -1473,6 +1612,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1473
1612
|
|
|
1474
1613
|
|
|
1475
1614
|
|
|
1615
|
+
|
|
1616
|
+
|
|
1617
|
+
|
|
1618
|
+
|
|
1619
|
+
|
|
1620
|
+
|
|
1621
|
+
|
|
1476
1622
|
|
|
1477
1623
|
|
|
1478
1624
|
|
|
@@ -1947,7 +2093,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1947
2093
|
}
|
|
1948
2094
|
/**
|
|
1949
2095
|
* Adds a new node to the tree.
|
|
1950
|
-
* @remarks Time O(
|
|
2096
|
+
* @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).
|
|
1951
2097
|
*
|
|
1952
2098
|
* @param keyNodeOrEntry - The key, node, or entry to add.
|
|
1953
2099
|
* @returns True if the addition was successful, false otherwise.
|
|
@@ -1973,6 +2119,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1973
2119
|
|
|
1974
2120
|
|
|
1975
2121
|
|
|
2122
|
+
|
|
2123
|
+
|
|
2124
|
+
|
|
2125
|
+
|
|
2126
|
+
|
|
2127
|
+
|
|
2128
|
+
|
|
1976
2129
|
|
|
1977
2130
|
|
|
1978
2131
|
|
|
@@ -1995,7 +2148,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1995
2148
|
}
|
|
1996
2149
|
/**
|
|
1997
2150
|
* Adds or updates a new node to the tree.
|
|
1998
|
-
* @remarks Time O(
|
|
2151
|
+
* @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).
|
|
1999
2152
|
*
|
|
2000
2153
|
* @param keyNodeOrEntry - The key, node, or entry to set or update.
|
|
2001
2154
|
* @param [value] - The value, if providing just a key.
|
|
@@ -2027,6 +2180,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2027
2180
|
|
|
2028
2181
|
|
|
2029
2182
|
|
|
2183
|
+
|
|
2184
|
+
|
|
2185
|
+
|
|
2186
|
+
|
|
2187
|
+
|
|
2188
|
+
|
|
2189
|
+
|
|
2030
2190
|
|
|
2031
2191
|
|
|
2032
2192
|
|
|
@@ -2133,6 +2293,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2133
2293
|
|
|
2134
2294
|
|
|
2135
2295
|
|
|
2296
|
+
|
|
2297
|
+
|
|
2298
|
+
|
|
2299
|
+
|
|
2300
|
+
|
|
2301
|
+
|
|
2302
|
+
|
|
2136
2303
|
|
|
2137
2304
|
|
|
2138
2305
|
|
|
@@ -2175,6 +2342,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2175
2342
|
|
|
2176
2343
|
|
|
2177
2344
|
|
|
2345
|
+
|
|
2346
|
+
|
|
2347
|
+
|
|
2348
|
+
|
|
2349
|
+
|
|
2350
|
+
|
|
2351
|
+
|
|
2178
2352
|
|
|
2179
2353
|
|
|
2180
2354
|
|
|
@@ -2238,6 +2412,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2238
2412
|
|
|
2239
2413
|
|
|
2240
2414
|
|
|
2415
|
+
|
|
2416
|
+
|
|
2417
|
+
|
|
2418
|
+
|
|
2419
|
+
|
|
2420
|
+
|
|
2421
|
+
|
|
2241
2422
|
|
|
2242
2423
|
|
|
2243
2424
|
|
|
@@ -2257,22 +2438,69 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2257
2438
|
this.setMany(anotherTree, []);
|
|
2258
2439
|
}
|
|
2259
2440
|
/**
|
|
2260
|
-
*
|
|
2261
|
-
* @remarks Time O(N)
|
|
2441
|
+
* Deletes a node from the tree (internal, returns balancing metadata).
|
|
2442
|
+
* @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).
|
|
2443
|
+
* @internal Used by AVL/BST subclasses that need balancing metadata after deletion.
|
|
2262
2444
|
*
|
|
2263
|
-
* @param
|
|
2264
|
-
* @
|
|
2445
|
+
* @param keyNodeEntryRawOrPredicate - The node to delete.
|
|
2446
|
+
* @returns An array containing deletion results with balancing metadata.
|
|
2265
2447
|
*/
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
this.
|
|
2448
|
+
_deleteInternal(keyNodeEntryRawOrPredicate) {
|
|
2449
|
+
const deletedResult = [];
|
|
2450
|
+
if (!this._root) return deletedResult;
|
|
2451
|
+
const curr = this.getNode(keyNodeEntryRawOrPredicate);
|
|
2452
|
+
if (!curr) return deletedResult;
|
|
2453
|
+
const parent = curr?.parent;
|
|
2454
|
+
let needBalanced;
|
|
2455
|
+
let orgCurrent = curr;
|
|
2456
|
+
if (!curr.left && !curr.right && !parent) {
|
|
2457
|
+
this._setRoot(void 0);
|
|
2458
|
+
} else if (curr.left) {
|
|
2459
|
+
const leftSubTreeRightMost = this.getRightMost((node) => node, curr.left);
|
|
2460
|
+
if (leftSubTreeRightMost) {
|
|
2461
|
+
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
2462
|
+
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
|
|
2463
|
+
if (this._isMapMode) {
|
|
2464
|
+
this._store.set(curr.key, curr);
|
|
2465
|
+
this._store.set(leftSubTreeRightMost.key, leftSubTreeRightMost);
|
|
2466
|
+
}
|
|
2467
|
+
if (parentOfLeftSubTreeMax) {
|
|
2468
|
+
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
2469
|
+
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
2470
|
+
else parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
|
|
2471
|
+
needBalanced = parentOfLeftSubTreeMax;
|
|
2472
|
+
}
|
|
2473
|
+
}
|
|
2474
|
+
} else if (parent) {
|
|
2475
|
+
const { familyPosition: fp } = curr;
|
|
2476
|
+
if (fp === "LEFT" || fp === "ROOT_LEFT") {
|
|
2477
|
+
parent.left = curr.right;
|
|
2478
|
+
} else if (fp === "RIGHT" || fp === "ROOT_RIGHT") {
|
|
2479
|
+
parent.right = curr.right;
|
|
2480
|
+
}
|
|
2481
|
+
needBalanced = parent;
|
|
2482
|
+
} else {
|
|
2483
|
+
this._setRoot(curr.right);
|
|
2484
|
+
curr.right = void 0;
|
|
2485
|
+
}
|
|
2486
|
+
this._size = this._size - 1;
|
|
2487
|
+
deletedResult.push({ deleted: orgCurrent, needBalanced });
|
|
2488
|
+
if (this._isMapMode && orgCurrent) this._store.delete(orgCurrent.key);
|
|
2489
|
+
return deletedResult;
|
|
2269
2490
|
}
|
|
2270
2491
|
/**
|
|
2271
2492
|
* Deletes a node from the tree.
|
|
2272
|
-
* @remarks Time O(
|
|
2493
|
+
* @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).
|
|
2273
2494
|
*
|
|
2274
2495
|
* @param keyNodeEntryRawOrPredicate - The node to delete.
|
|
2275
|
-
* @returns
|
|
2496
|
+
* @returns True if the node was found and deleted, false otherwise.
|
|
2497
|
+
|
|
2498
|
+
|
|
2499
|
+
|
|
2500
|
+
|
|
2501
|
+
|
|
2502
|
+
|
|
2503
|
+
|
|
2276
2504
|
|
|
2277
2505
|
|
|
2278
2506
|
|
|
@@ -2316,51 +2544,11 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2316
2544
|
* console.log(tree.size); // 4;
|
|
2317
2545
|
*/
|
|
2318
2546
|
delete(keyNodeEntryRawOrPredicate) {
|
|
2319
|
-
|
|
2320
|
-
if (!this._root) return deletedResult;
|
|
2321
|
-
const curr = this.getNode(keyNodeEntryRawOrPredicate);
|
|
2322
|
-
if (!curr) return deletedResult;
|
|
2323
|
-
const parent = curr?.parent;
|
|
2324
|
-
let needBalanced;
|
|
2325
|
-
let orgCurrent = curr;
|
|
2326
|
-
if (!curr.left && !curr.right && !parent) {
|
|
2327
|
-
this._setRoot(void 0);
|
|
2328
|
-
} else if (curr.left) {
|
|
2329
|
-
const leftSubTreeRightMost = this.getRightMost((node) => node, curr.left);
|
|
2330
|
-
if (leftSubTreeRightMost) {
|
|
2331
|
-
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
2332
|
-
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
|
|
2333
|
-
if (this._isMapMode) {
|
|
2334
|
-
this._store.set(curr.key, curr);
|
|
2335
|
-
this._store.set(leftSubTreeRightMost.key, leftSubTreeRightMost);
|
|
2336
|
-
}
|
|
2337
|
-
if (parentOfLeftSubTreeMax) {
|
|
2338
|
-
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
2339
|
-
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
2340
|
-
else parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
|
|
2341
|
-
needBalanced = parentOfLeftSubTreeMax;
|
|
2342
|
-
}
|
|
2343
|
-
}
|
|
2344
|
-
} else if (parent) {
|
|
2345
|
-
const { familyPosition: fp } = curr;
|
|
2346
|
-
if (fp === "LEFT" || fp === "ROOT_LEFT") {
|
|
2347
|
-
parent.left = curr.right;
|
|
2348
|
-
} else if (fp === "RIGHT" || fp === "ROOT_RIGHT") {
|
|
2349
|
-
parent.right = curr.right;
|
|
2350
|
-
}
|
|
2351
|
-
needBalanced = parent;
|
|
2352
|
-
} else {
|
|
2353
|
-
this._setRoot(curr.right);
|
|
2354
|
-
curr.right = void 0;
|
|
2355
|
-
}
|
|
2356
|
-
this._size = this._size - 1;
|
|
2357
|
-
deletedResult.push({ deleted: orgCurrent, needBalanced });
|
|
2358
|
-
if (this._isMapMode && orgCurrent) this._store.delete(orgCurrent.key);
|
|
2359
|
-
return deletedResult;
|
|
2547
|
+
return this._deleteInternal(keyNodeEntryRawOrPredicate).length > 0;
|
|
2360
2548
|
}
|
|
2361
2549
|
/**
|
|
2362
2550
|
* Searches the tree for nodes matching a predicate.
|
|
2363
|
-
* @remarks Time O(
|
|
2551
|
+
* @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).
|
|
2364
2552
|
*
|
|
2365
2553
|
* @template C - The type of the callback function.
|
|
2366
2554
|
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
@@ -2409,7 +2597,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2409
2597
|
}
|
|
2410
2598
|
/**
|
|
2411
2599
|
* Gets the first node matching a predicate.
|
|
2412
|
-
* @remarks Time O(
|
|
2600
|
+
* @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.
|
|
2413
2601
|
*
|
|
2414
2602
|
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
2415
2603
|
* @param [startNode=this._root] - The node to start the search from.
|
|
@@ -2440,6 +2628,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2440
2628
|
|
|
2441
2629
|
|
|
2442
2630
|
|
|
2631
|
+
|
|
2632
|
+
|
|
2633
|
+
|
|
2634
|
+
|
|
2635
|
+
|
|
2636
|
+
|
|
2637
|
+
|
|
2443
2638
|
|
|
2444
2639
|
|
|
2445
2640
|
|
|
@@ -2465,7 +2660,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2465
2660
|
}
|
|
2466
2661
|
/**
|
|
2467
2662
|
* Gets the value associated with a key.
|
|
2468
|
-
* @remarks Time O(
|
|
2663
|
+
* @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).
|
|
2469
2664
|
*
|
|
2470
2665
|
* @param keyNodeEntryOrPredicate - The key, node, or entry to get the value for.
|
|
2471
2666
|
* @param [startNode=this._root] - The node to start searching from (if not in Map mode).
|
|
@@ -2498,6 +2693,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2498
2693
|
|
|
2499
2694
|
|
|
2500
2695
|
|
|
2696
|
+
|
|
2697
|
+
|
|
2698
|
+
|
|
2699
|
+
|
|
2700
|
+
|
|
2701
|
+
|
|
2702
|
+
|
|
2501
2703
|
|
|
2502
2704
|
|
|
2503
2705
|
|
|
@@ -2558,6 +2760,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2558
2760
|
|
|
2559
2761
|
|
|
2560
2762
|
|
|
2763
|
+
|
|
2764
|
+
|
|
2765
|
+
|
|
2766
|
+
|
|
2767
|
+
|
|
2768
|
+
|
|
2769
|
+
|
|
2561
2770
|
|
|
2562
2771
|
|
|
2563
2772
|
|
|
@@ -2606,6 +2815,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2606
2815
|
|
|
2607
2816
|
|
|
2608
2817
|
|
|
2818
|
+
|
|
2819
|
+
|
|
2820
|
+
|
|
2821
|
+
|
|
2822
|
+
|
|
2823
|
+
|
|
2824
|
+
|
|
2609
2825
|
|
|
2610
2826
|
|
|
2611
2827
|
|
|
@@ -2663,6 +2879,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2663
2879
|
|
|
2664
2880
|
|
|
2665
2881
|
|
|
2882
|
+
|
|
2883
|
+
|
|
2884
|
+
|
|
2885
|
+
|
|
2886
|
+
|
|
2887
|
+
|
|
2888
|
+
|
|
2666
2889
|
|
|
2667
2890
|
|
|
2668
2891
|
|
|
@@ -2747,6 +2970,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2747
2970
|
|
|
2748
2971
|
|
|
2749
2972
|
|
|
2973
|
+
|
|
2974
|
+
|
|
2975
|
+
|
|
2976
|
+
|
|
2977
|
+
|
|
2978
|
+
|
|
2979
|
+
|
|
2750
2980
|
|
|
2751
2981
|
|
|
2752
2982
|
|
|
@@ -2808,6 +3038,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2808
3038
|
|
|
2809
3039
|
|
|
2810
3040
|
|
|
3041
|
+
|
|
3042
|
+
|
|
3043
|
+
|
|
3044
|
+
|
|
3045
|
+
|
|
3046
|
+
|
|
3047
|
+
|
|
2811
3048
|
|
|
2812
3049
|
|
|
2813
3050
|
|
|
@@ -3285,6 +3522,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3285
3522
|
|
|
3286
3523
|
|
|
3287
3524
|
|
|
3525
|
+
|
|
3526
|
+
|
|
3527
|
+
|
|
3528
|
+
|
|
3529
|
+
|
|
3530
|
+
|
|
3531
|
+
|
|
3288
3532
|
|
|
3289
3533
|
|
|
3290
3534
|
|
|
@@ -3337,6 +3581,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3337
3581
|
|
|
3338
3582
|
|
|
3339
3583
|
|
|
3584
|
+
|
|
3585
|
+
|
|
3586
|
+
|
|
3587
|
+
|
|
3588
|
+
|
|
3589
|
+
|
|
3590
|
+
|
|
3340
3591
|
|
|
3341
3592
|
|
|
3342
3593
|
|
|
@@ -3393,6 +3644,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3393
3644
|
|
|
3394
3645
|
|
|
3395
3646
|
|
|
3647
|
+
|
|
3648
|
+
|
|
3649
|
+
|
|
3650
|
+
|
|
3651
|
+
|
|
3652
|
+
|
|
3653
|
+
|
|
3396
3654
|
|
|
3397
3655
|
|
|
3398
3656
|
|
|
@@ -3474,6 +3732,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3474
3732
|
|
|
3475
3733
|
|
|
3476
3734
|
|
|
3735
|
+
|
|
3736
|
+
|
|
3737
|
+
|
|
3738
|
+
|
|
3739
|
+
|
|
3740
|
+
|
|
3741
|
+
|
|
3477
3742
|
|
|
3478
3743
|
|
|
3479
3744
|
|