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/cjs/index.cjs
CHANGED
|
@@ -266,6 +266,35 @@ var IterableElementBase = class {
|
|
|
266
266
|
for (const ele of this) if (ele === element) return true;
|
|
267
267
|
return false;
|
|
268
268
|
}
|
|
269
|
+
/**
|
|
270
|
+
* Check whether a value exists (Array-compatible alias for `has`).
|
|
271
|
+
* @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1).
|
|
272
|
+
* @param element - Element to search for (uses `===`).
|
|
273
|
+
* @returns `true` if found.
|
|
274
|
+
*/
|
|
275
|
+
includes(element) {
|
|
276
|
+
return this.has(element);
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Return an iterator of `[index, value]` pairs (Array-compatible).
|
|
280
|
+
* @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1) per step.
|
|
281
|
+
*/
|
|
282
|
+
*entries() {
|
|
283
|
+
let index = 0;
|
|
284
|
+
for (const value of this) {
|
|
285
|
+
yield [index++, value];
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Return an iterator of numeric indices (Array-compatible).
|
|
290
|
+
* @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1) per step.
|
|
291
|
+
*/
|
|
292
|
+
*keys() {
|
|
293
|
+
let index = 0;
|
|
294
|
+
for (const _ of this) {
|
|
295
|
+
yield index++;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
269
298
|
/**
|
|
270
299
|
* Reduces all elements to a single accumulated value.
|
|
271
300
|
*
|
|
@@ -528,6 +557,16 @@ var LinearBase = class _LinearBase extends IterableElementBase {
|
|
|
528
557
|
}
|
|
529
558
|
return this;
|
|
530
559
|
}
|
|
560
|
+
/**
|
|
561
|
+
* Return a new instance of the same type with elements in reverse order (non-mutating).
|
|
562
|
+
* @remarks Provided for familiarity when migrating from Array (ES2023 `toReversed`). Time O(n), Space O(n).
|
|
563
|
+
* @returns A new reversed instance.
|
|
564
|
+
*/
|
|
565
|
+
toReversed() {
|
|
566
|
+
const cloned = this.clone();
|
|
567
|
+
cloned.reverse();
|
|
568
|
+
return cloned;
|
|
569
|
+
}
|
|
531
570
|
};
|
|
532
571
|
|
|
533
572
|
// src/data-structures/base/iterable-entry-base.ts
|
|
@@ -799,6 +838,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
799
838
|
|
|
800
839
|
|
|
801
840
|
|
|
841
|
+
|
|
842
|
+
|
|
843
|
+
|
|
844
|
+
|
|
845
|
+
|
|
846
|
+
|
|
847
|
+
|
|
802
848
|
|
|
803
849
|
|
|
804
850
|
|
|
@@ -849,6 +895,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
849
895
|
|
|
850
896
|
|
|
851
897
|
|
|
898
|
+
|
|
899
|
+
|
|
900
|
+
|
|
901
|
+
|
|
902
|
+
|
|
903
|
+
|
|
904
|
+
|
|
852
905
|
|
|
853
906
|
|
|
854
907
|
|
|
@@ -866,6 +919,14 @@ var Queue = class _Queue extends LinearBase {
|
|
|
866
919
|
get first() {
|
|
867
920
|
return this.length > 0 ? this.elements[this._offset] : void 0;
|
|
868
921
|
}
|
|
922
|
+
/**
|
|
923
|
+
* Peek at the front element without removing it (alias for `first`).
|
|
924
|
+
* @remarks Time O(1), Space O(1)
|
|
925
|
+
* @returns Front element or undefined.
|
|
926
|
+
*/
|
|
927
|
+
peek() {
|
|
928
|
+
return this.first;
|
|
929
|
+
}
|
|
869
930
|
/**
|
|
870
931
|
* Get the last element (back) without removing it.
|
|
871
932
|
* @remarks Time O(1), Space O(1)
|
|
@@ -915,6 +976,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
915
976
|
|
|
916
977
|
|
|
917
978
|
|
|
979
|
+
|
|
980
|
+
|
|
981
|
+
|
|
982
|
+
|
|
983
|
+
|
|
984
|
+
|
|
985
|
+
|
|
918
986
|
|
|
919
987
|
|
|
920
988
|
|
|
@@ -977,6 +1045,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
977
1045
|
|
|
978
1046
|
|
|
979
1047
|
|
|
1048
|
+
|
|
1049
|
+
|
|
1050
|
+
|
|
1051
|
+
|
|
1052
|
+
|
|
1053
|
+
|
|
1054
|
+
|
|
980
1055
|
|
|
981
1056
|
|
|
982
1057
|
|
|
@@ -1046,6 +1121,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1046
1121
|
|
|
1047
1122
|
|
|
1048
1123
|
|
|
1124
|
+
|
|
1125
|
+
|
|
1126
|
+
|
|
1127
|
+
|
|
1128
|
+
|
|
1129
|
+
|
|
1130
|
+
|
|
1049
1131
|
|
|
1050
1132
|
|
|
1051
1133
|
|
|
@@ -1105,6 +1187,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1105
1187
|
|
|
1106
1188
|
|
|
1107
1189
|
|
|
1190
|
+
|
|
1191
|
+
|
|
1192
|
+
|
|
1193
|
+
|
|
1194
|
+
|
|
1195
|
+
|
|
1196
|
+
|
|
1108
1197
|
|
|
1109
1198
|
|
|
1110
1199
|
|
|
@@ -1157,6 +1246,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1157
1246
|
|
|
1158
1247
|
|
|
1159
1248
|
|
|
1249
|
+
|
|
1250
|
+
|
|
1251
|
+
|
|
1252
|
+
|
|
1253
|
+
|
|
1254
|
+
|
|
1255
|
+
|
|
1160
1256
|
|
|
1161
1257
|
|
|
1162
1258
|
|
|
@@ -1211,6 +1307,21 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1211
1307
|
this._elements[this._offset + index] = newElement;
|
|
1212
1308
|
return true;
|
|
1213
1309
|
}
|
|
1310
|
+
/**
|
|
1311
|
+
* Delete the first element that satisfies a predicate.
|
|
1312
|
+
* @remarks Time O(N), Space O(N)
|
|
1313
|
+
* @param predicate - Function (value, index, queue) → boolean to decide deletion.
|
|
1314
|
+
* @returns True if a match was removed.
|
|
1315
|
+
*/
|
|
1316
|
+
deleteWhere(predicate) {
|
|
1317
|
+
for (let i = 0; i < this.length; i++) {
|
|
1318
|
+
if (predicate(this._elements[this._offset + i], i, this)) {
|
|
1319
|
+
this.deleteAt(i);
|
|
1320
|
+
return true;
|
|
1321
|
+
}
|
|
1322
|
+
}
|
|
1323
|
+
return false;
|
|
1324
|
+
}
|
|
1214
1325
|
/**
|
|
1215
1326
|
* Reverse the queue in-place by compacting then reversing.
|
|
1216
1327
|
* @remarks Time O(N), Space O(N)
|
|
@@ -1250,6 +1361,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1250
1361
|
|
|
1251
1362
|
|
|
1252
1363
|
|
|
1364
|
+
|
|
1365
|
+
|
|
1366
|
+
|
|
1367
|
+
|
|
1368
|
+
|
|
1369
|
+
|
|
1370
|
+
|
|
1253
1371
|
|
|
1254
1372
|
|
|
1255
1373
|
|
|
@@ -1296,6 +1414,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1296
1414
|
|
|
1297
1415
|
|
|
1298
1416
|
|
|
1417
|
+
|
|
1418
|
+
|
|
1419
|
+
|
|
1420
|
+
|
|
1421
|
+
|
|
1422
|
+
|
|
1423
|
+
|
|
1299
1424
|
|
|
1300
1425
|
|
|
1301
1426
|
|
|
@@ -1365,6 +1490,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1365
1490
|
|
|
1366
1491
|
|
|
1367
1492
|
|
|
1493
|
+
|
|
1494
|
+
|
|
1495
|
+
|
|
1496
|
+
|
|
1497
|
+
|
|
1498
|
+
|
|
1499
|
+
|
|
1368
1500
|
|
|
1369
1501
|
|
|
1370
1502
|
|
|
@@ -1418,6 +1550,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1418
1550
|
|
|
1419
1551
|
|
|
1420
1552
|
|
|
1553
|
+
|
|
1554
|
+
|
|
1555
|
+
|
|
1556
|
+
|
|
1557
|
+
|
|
1558
|
+
|
|
1559
|
+
|
|
1421
1560
|
|
|
1422
1561
|
|
|
1423
1562
|
|
|
@@ -1475,6 +1614,13 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1475
1614
|
|
|
1476
1615
|
|
|
1477
1616
|
|
|
1617
|
+
|
|
1618
|
+
|
|
1619
|
+
|
|
1620
|
+
|
|
1621
|
+
|
|
1622
|
+
|
|
1623
|
+
|
|
1478
1624
|
|
|
1479
1625
|
|
|
1480
1626
|
|
|
@@ -1949,7 +2095,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1949
2095
|
}
|
|
1950
2096
|
/**
|
|
1951
2097
|
* Adds a new node to the tree.
|
|
1952
|
-
* @remarks Time O(
|
|
2098
|
+
* @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).
|
|
1953
2099
|
*
|
|
1954
2100
|
* @param keyNodeOrEntry - The key, node, or entry to add.
|
|
1955
2101
|
* @returns True if the addition was successful, false otherwise.
|
|
@@ -1975,6 +2121,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1975
2121
|
|
|
1976
2122
|
|
|
1977
2123
|
|
|
2124
|
+
|
|
2125
|
+
|
|
2126
|
+
|
|
2127
|
+
|
|
2128
|
+
|
|
2129
|
+
|
|
2130
|
+
|
|
1978
2131
|
|
|
1979
2132
|
|
|
1980
2133
|
|
|
@@ -1997,7 +2150,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1997
2150
|
}
|
|
1998
2151
|
/**
|
|
1999
2152
|
* Adds or updates a new node to the tree.
|
|
2000
|
-
* @remarks Time O(
|
|
2153
|
+
* @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).
|
|
2001
2154
|
*
|
|
2002
2155
|
* @param keyNodeOrEntry - The key, node, or entry to set or update.
|
|
2003
2156
|
* @param [value] - The value, if providing just a key.
|
|
@@ -2029,6 +2182,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2029
2182
|
|
|
2030
2183
|
|
|
2031
2184
|
|
|
2185
|
+
|
|
2186
|
+
|
|
2187
|
+
|
|
2188
|
+
|
|
2189
|
+
|
|
2190
|
+
|
|
2191
|
+
|
|
2032
2192
|
|
|
2033
2193
|
|
|
2034
2194
|
|
|
@@ -2135,6 +2295,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2135
2295
|
|
|
2136
2296
|
|
|
2137
2297
|
|
|
2298
|
+
|
|
2299
|
+
|
|
2300
|
+
|
|
2301
|
+
|
|
2302
|
+
|
|
2303
|
+
|
|
2304
|
+
|
|
2138
2305
|
|
|
2139
2306
|
|
|
2140
2307
|
|
|
@@ -2177,6 +2344,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2177
2344
|
|
|
2178
2345
|
|
|
2179
2346
|
|
|
2347
|
+
|
|
2348
|
+
|
|
2349
|
+
|
|
2350
|
+
|
|
2351
|
+
|
|
2352
|
+
|
|
2353
|
+
|
|
2180
2354
|
|
|
2181
2355
|
|
|
2182
2356
|
|
|
@@ -2240,6 +2414,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2240
2414
|
|
|
2241
2415
|
|
|
2242
2416
|
|
|
2417
|
+
|
|
2418
|
+
|
|
2419
|
+
|
|
2420
|
+
|
|
2421
|
+
|
|
2422
|
+
|
|
2423
|
+
|
|
2243
2424
|
|
|
2244
2425
|
|
|
2245
2426
|
|
|
@@ -2259,22 +2440,69 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2259
2440
|
this.setMany(anotherTree, []);
|
|
2260
2441
|
}
|
|
2261
2442
|
/**
|
|
2262
|
-
*
|
|
2263
|
-
* @remarks Time O(N)
|
|
2443
|
+
* Deletes a node from the tree (internal, returns balancing metadata).
|
|
2444
|
+
* @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).
|
|
2445
|
+
* @internal Used by AVL/BST subclasses that need balancing metadata after deletion.
|
|
2264
2446
|
*
|
|
2265
|
-
* @param
|
|
2266
|
-
* @
|
|
2447
|
+
* @param keyNodeEntryRawOrPredicate - The node to delete.
|
|
2448
|
+
* @returns An array containing deletion results with balancing metadata.
|
|
2267
2449
|
*/
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
this.
|
|
2450
|
+
_deleteInternal(keyNodeEntryRawOrPredicate) {
|
|
2451
|
+
const deletedResult = [];
|
|
2452
|
+
if (!this._root) return deletedResult;
|
|
2453
|
+
const curr = this.getNode(keyNodeEntryRawOrPredicate);
|
|
2454
|
+
if (!curr) return deletedResult;
|
|
2455
|
+
const parent = curr?.parent;
|
|
2456
|
+
let needBalanced;
|
|
2457
|
+
let orgCurrent = curr;
|
|
2458
|
+
if (!curr.left && !curr.right && !parent) {
|
|
2459
|
+
this._setRoot(void 0);
|
|
2460
|
+
} else if (curr.left) {
|
|
2461
|
+
const leftSubTreeRightMost = this.getRightMost((node) => node, curr.left);
|
|
2462
|
+
if (leftSubTreeRightMost) {
|
|
2463
|
+
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
2464
|
+
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
|
|
2465
|
+
if (this._isMapMode) {
|
|
2466
|
+
this._store.set(curr.key, curr);
|
|
2467
|
+
this._store.set(leftSubTreeRightMost.key, leftSubTreeRightMost);
|
|
2468
|
+
}
|
|
2469
|
+
if (parentOfLeftSubTreeMax) {
|
|
2470
|
+
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
2471
|
+
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
2472
|
+
else parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
|
|
2473
|
+
needBalanced = parentOfLeftSubTreeMax;
|
|
2474
|
+
}
|
|
2475
|
+
}
|
|
2476
|
+
} else if (parent) {
|
|
2477
|
+
const { familyPosition: fp } = curr;
|
|
2478
|
+
if (fp === "LEFT" || fp === "ROOT_LEFT") {
|
|
2479
|
+
parent.left = curr.right;
|
|
2480
|
+
} else if (fp === "RIGHT" || fp === "ROOT_RIGHT") {
|
|
2481
|
+
parent.right = curr.right;
|
|
2482
|
+
}
|
|
2483
|
+
needBalanced = parent;
|
|
2484
|
+
} else {
|
|
2485
|
+
this._setRoot(curr.right);
|
|
2486
|
+
curr.right = void 0;
|
|
2487
|
+
}
|
|
2488
|
+
this._size = this._size - 1;
|
|
2489
|
+
deletedResult.push({ deleted: orgCurrent, needBalanced });
|
|
2490
|
+
if (this._isMapMode && orgCurrent) this._store.delete(orgCurrent.key);
|
|
2491
|
+
return deletedResult;
|
|
2271
2492
|
}
|
|
2272
2493
|
/**
|
|
2273
2494
|
* Deletes a node from the tree.
|
|
2274
|
-
* @remarks Time O(
|
|
2495
|
+
* @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).
|
|
2275
2496
|
*
|
|
2276
2497
|
* @param keyNodeEntryRawOrPredicate - The node to delete.
|
|
2277
|
-
* @returns
|
|
2498
|
+
* @returns True if the node was found and deleted, false otherwise.
|
|
2499
|
+
|
|
2500
|
+
|
|
2501
|
+
|
|
2502
|
+
|
|
2503
|
+
|
|
2504
|
+
|
|
2505
|
+
|
|
2278
2506
|
|
|
2279
2507
|
|
|
2280
2508
|
|
|
@@ -2318,51 +2546,11 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2318
2546
|
* console.log(tree.size); // 4;
|
|
2319
2547
|
*/
|
|
2320
2548
|
delete(keyNodeEntryRawOrPredicate) {
|
|
2321
|
-
|
|
2322
|
-
if (!this._root) return deletedResult;
|
|
2323
|
-
const curr = this.getNode(keyNodeEntryRawOrPredicate);
|
|
2324
|
-
if (!curr) return deletedResult;
|
|
2325
|
-
const parent = curr?.parent;
|
|
2326
|
-
let needBalanced;
|
|
2327
|
-
let orgCurrent = curr;
|
|
2328
|
-
if (!curr.left && !curr.right && !parent) {
|
|
2329
|
-
this._setRoot(void 0);
|
|
2330
|
-
} else if (curr.left) {
|
|
2331
|
-
const leftSubTreeRightMost = this.getRightMost((node) => node, curr.left);
|
|
2332
|
-
if (leftSubTreeRightMost) {
|
|
2333
|
-
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
2334
|
-
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
|
|
2335
|
-
if (this._isMapMode) {
|
|
2336
|
-
this._store.set(curr.key, curr);
|
|
2337
|
-
this._store.set(leftSubTreeRightMost.key, leftSubTreeRightMost);
|
|
2338
|
-
}
|
|
2339
|
-
if (parentOfLeftSubTreeMax) {
|
|
2340
|
-
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
2341
|
-
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
2342
|
-
else parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
|
|
2343
|
-
needBalanced = parentOfLeftSubTreeMax;
|
|
2344
|
-
}
|
|
2345
|
-
}
|
|
2346
|
-
} else if (parent) {
|
|
2347
|
-
const { familyPosition: fp } = curr;
|
|
2348
|
-
if (fp === "LEFT" || fp === "ROOT_LEFT") {
|
|
2349
|
-
parent.left = curr.right;
|
|
2350
|
-
} else if (fp === "RIGHT" || fp === "ROOT_RIGHT") {
|
|
2351
|
-
parent.right = curr.right;
|
|
2352
|
-
}
|
|
2353
|
-
needBalanced = parent;
|
|
2354
|
-
} else {
|
|
2355
|
-
this._setRoot(curr.right);
|
|
2356
|
-
curr.right = void 0;
|
|
2357
|
-
}
|
|
2358
|
-
this._size = this._size - 1;
|
|
2359
|
-
deletedResult.push({ deleted: orgCurrent, needBalanced });
|
|
2360
|
-
if (this._isMapMode && orgCurrent) this._store.delete(orgCurrent.key);
|
|
2361
|
-
return deletedResult;
|
|
2549
|
+
return this._deleteInternal(keyNodeEntryRawOrPredicate).length > 0;
|
|
2362
2550
|
}
|
|
2363
2551
|
/**
|
|
2364
2552
|
* Searches the tree for nodes matching a predicate.
|
|
2365
|
-
* @remarks Time O(
|
|
2553
|
+
* @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).
|
|
2366
2554
|
*
|
|
2367
2555
|
* @template C - The type of the callback function.
|
|
2368
2556
|
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
@@ -2411,7 +2599,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2411
2599
|
}
|
|
2412
2600
|
/**
|
|
2413
2601
|
* Gets the first node matching a predicate.
|
|
2414
|
-
* @remarks Time O(
|
|
2602
|
+
* @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.
|
|
2415
2603
|
*
|
|
2416
2604
|
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
2417
2605
|
* @param [startNode=this._root] - The node to start the search from.
|
|
@@ -2442,6 +2630,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2442
2630
|
|
|
2443
2631
|
|
|
2444
2632
|
|
|
2633
|
+
|
|
2634
|
+
|
|
2635
|
+
|
|
2636
|
+
|
|
2637
|
+
|
|
2638
|
+
|
|
2639
|
+
|
|
2445
2640
|
|
|
2446
2641
|
|
|
2447
2642
|
|
|
@@ -2467,7 +2662,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2467
2662
|
}
|
|
2468
2663
|
/**
|
|
2469
2664
|
* Gets the value associated with a key.
|
|
2470
|
-
* @remarks Time O(
|
|
2665
|
+
* @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).
|
|
2471
2666
|
*
|
|
2472
2667
|
* @param keyNodeEntryOrPredicate - The key, node, or entry to get the value for.
|
|
2473
2668
|
* @param [startNode=this._root] - The node to start searching from (if not in Map mode).
|
|
@@ -2500,6 +2695,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2500
2695
|
|
|
2501
2696
|
|
|
2502
2697
|
|
|
2698
|
+
|
|
2699
|
+
|
|
2700
|
+
|
|
2701
|
+
|
|
2702
|
+
|
|
2703
|
+
|
|
2704
|
+
|
|
2503
2705
|
|
|
2504
2706
|
|
|
2505
2707
|
|
|
@@ -2560,6 +2762,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2560
2762
|
|
|
2561
2763
|
|
|
2562
2764
|
|
|
2765
|
+
|
|
2766
|
+
|
|
2767
|
+
|
|
2768
|
+
|
|
2769
|
+
|
|
2770
|
+
|
|
2771
|
+
|
|
2563
2772
|
|
|
2564
2773
|
|
|
2565
2774
|
|
|
@@ -2608,6 +2817,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2608
2817
|
|
|
2609
2818
|
|
|
2610
2819
|
|
|
2820
|
+
|
|
2821
|
+
|
|
2822
|
+
|
|
2823
|
+
|
|
2824
|
+
|
|
2825
|
+
|
|
2826
|
+
|
|
2611
2827
|
|
|
2612
2828
|
|
|
2613
2829
|
|
|
@@ -2665,6 +2881,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2665
2881
|
|
|
2666
2882
|
|
|
2667
2883
|
|
|
2884
|
+
|
|
2885
|
+
|
|
2886
|
+
|
|
2887
|
+
|
|
2888
|
+
|
|
2889
|
+
|
|
2890
|
+
|
|
2668
2891
|
|
|
2669
2892
|
|
|
2670
2893
|
|
|
@@ -2749,6 +2972,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2749
2972
|
|
|
2750
2973
|
|
|
2751
2974
|
|
|
2975
|
+
|
|
2976
|
+
|
|
2977
|
+
|
|
2978
|
+
|
|
2979
|
+
|
|
2980
|
+
|
|
2981
|
+
|
|
2752
2982
|
|
|
2753
2983
|
|
|
2754
2984
|
|
|
@@ -2810,6 +3040,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2810
3040
|
|
|
2811
3041
|
|
|
2812
3042
|
|
|
3043
|
+
|
|
3044
|
+
|
|
3045
|
+
|
|
3046
|
+
|
|
3047
|
+
|
|
3048
|
+
|
|
3049
|
+
|
|
2813
3050
|
|
|
2814
3051
|
|
|
2815
3052
|
|
|
@@ -3287,6 +3524,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3287
3524
|
|
|
3288
3525
|
|
|
3289
3526
|
|
|
3527
|
+
|
|
3528
|
+
|
|
3529
|
+
|
|
3530
|
+
|
|
3531
|
+
|
|
3532
|
+
|
|
3533
|
+
|
|
3290
3534
|
|
|
3291
3535
|
|
|
3292
3536
|
|
|
@@ -3339,6 +3583,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3339
3583
|
|
|
3340
3584
|
|
|
3341
3585
|
|
|
3586
|
+
|
|
3587
|
+
|
|
3588
|
+
|
|
3589
|
+
|
|
3590
|
+
|
|
3591
|
+
|
|
3592
|
+
|
|
3342
3593
|
|
|
3343
3594
|
|
|
3344
3595
|
|
|
@@ -3395,6 +3646,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3395
3646
|
|
|
3396
3647
|
|
|
3397
3648
|
|
|
3649
|
+
|
|
3650
|
+
|
|
3651
|
+
|
|
3652
|
+
|
|
3653
|
+
|
|
3654
|
+
|
|
3655
|
+
|
|
3398
3656
|
|
|
3399
3657
|
|
|
3400
3658
|
|
|
@@ -3476,6 +3734,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3476
3734
|
|
|
3477
3735
|
|
|
3478
3736
|
|
|
3737
|
+
|
|
3738
|
+
|
|
3739
|
+
|
|
3740
|
+
|
|
3741
|
+
|
|
3742
|
+
|
|
3743
|
+
|
|
3479
3744
|
|
|
3480
3745
|
|
|
3481
3746
|
|