binary-tree-typed 2.5.2 → 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.
Files changed (59) hide show
  1. package/dist/cjs/index.cjs +194 -55
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +194 -55
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +194 -55
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +194 -55
  8. package/dist/esm-legacy/index.mjs.map +1 -1
  9. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +50 -2
  10. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +56 -0
  11. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +116 -15
  12. package/dist/types/data-structures/binary-tree/bst.d.ts +99 -3
  13. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +79 -8
  14. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +24 -0
  15. package/dist/types/data-structures/binary-tree/tree-map.d.ts +520 -1
  16. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +489 -1
  17. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +393 -1
  18. package/dist/types/data-structures/binary-tree/tree-set.d.ts +500 -1
  19. package/dist/types/data-structures/graph/directed-graph.d.ts +40 -0
  20. package/dist/types/data-structures/graph/undirected-graph.d.ts +36 -0
  21. package/dist/types/data-structures/hash/hash-map.d.ts +51 -6
  22. package/dist/types/data-structures/heap/heap.d.ts +98 -12
  23. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +75 -0
  24. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +61 -1
  25. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +72 -0
  26. package/dist/types/data-structures/matrix/matrix.d.ts +32 -0
  27. package/dist/types/data-structures/queue/deque.d.ts +82 -0
  28. package/dist/types/data-structures/queue/queue.d.ts +61 -0
  29. package/dist/types/data-structures/stack/stack.d.ts +42 -2
  30. package/dist/types/data-structures/trie/trie.d.ts +48 -0
  31. package/dist/types/interfaces/binary-tree.d.ts +2 -3
  32. package/dist/umd/binary-tree-typed.js +194 -55
  33. package/dist/umd/binary-tree-typed.js.map +1 -1
  34. package/dist/umd/binary-tree-typed.min.js +5 -5
  35. package/dist/umd/binary-tree-typed.min.js.map +1 -1
  36. package/package.json +2 -2
  37. package/src/data-structures/binary-tree/avl-tree.ts +52 -5
  38. package/src/data-structures/binary-tree/binary-indexed-tree.ts +56 -0
  39. package/src/data-structures/binary-tree/binary-tree.ts +167 -81
  40. package/src/data-structures/binary-tree/bst.ts +101 -7
  41. package/src/data-structures/binary-tree/red-black-tree.ts +82 -15
  42. package/src/data-structures/binary-tree/segment-tree.ts +24 -0
  43. package/src/data-structures/binary-tree/tree-map.ts +540 -3
  44. package/src/data-structures/binary-tree/tree-multi-map.ts +490 -2
  45. package/src/data-structures/binary-tree/tree-multi-set.ts +393 -1
  46. package/src/data-structures/binary-tree/tree-set.ts +520 -3
  47. package/src/data-structures/graph/directed-graph.ts +41 -1
  48. package/src/data-structures/graph/undirected-graph.ts +37 -1
  49. package/src/data-structures/hash/hash-map.ts +67 -12
  50. package/src/data-structures/heap/heap.ts +107 -19
  51. package/src/data-structures/linked-list/doubly-linked-list.ts +88 -0
  52. package/src/data-structures/linked-list/singly-linked-list.ts +61 -1
  53. package/src/data-structures/linked-list/skip-linked-list.ts +72 -0
  54. package/src/data-structures/matrix/matrix.ts +32 -0
  55. package/src/data-structures/queue/deque.ts +85 -0
  56. package/src/data-structures/queue/queue.ts +73 -0
  57. package/src/data-structures/stack/stack.ts +45 -5
  58. package/src/data-structures/trie/trie.ts +48 -0
  59. package/src/interfaces/binary-tree.ts +1 -9
@@ -800,6 +800,10 @@ var Queue = class _Queue extends LinearBase {
800
800
 
801
801
 
802
802
 
803
+
804
+
805
+
806
+
803
807
 
804
808
 
805
809
 
@@ -850,6 +854,10 @@ var Queue = class _Queue extends LinearBase {
850
854
 
851
855
 
852
856
 
857
+
858
+
859
+
860
+
853
861
 
854
862
 
855
863
 
@@ -864,6 +872,14 @@ var Queue = class _Queue extends LinearBase {
864
872
  get first() {
865
873
  return this.length > 0 ? this.elements[this._offset] : void 0;
866
874
  }
875
+ /**
876
+ * Peek at the front element without removing it (alias for `first`).
877
+ * @remarks Time O(1), Space O(1)
878
+ * @returns Front element or undefined.
879
+ */
880
+ peek() {
881
+ return this.first;
882
+ }
867
883
  /**
868
884
  * Get the last element (back) without removing it.
869
885
  * @remarks Time O(1), Space O(1)
@@ -916,6 +932,10 @@ var Queue = class _Queue extends LinearBase {
916
932
 
917
933
 
918
934
 
935
+
936
+
937
+
938
+
919
939
 
920
940
 
921
941
 
@@ -978,6 +998,10 @@ var Queue = class _Queue extends LinearBase {
978
998
 
979
999
 
980
1000
 
1001
+
1002
+
1003
+
1004
+
981
1005
 
982
1006
 
983
1007
 
@@ -1047,6 +1071,10 @@ var Queue = class _Queue extends LinearBase {
1047
1071
 
1048
1072
 
1049
1073
 
1074
+
1075
+
1076
+
1077
+
1050
1078
 
1051
1079
 
1052
1080
 
@@ -1106,6 +1134,10 @@ var Queue = class _Queue extends LinearBase {
1106
1134
 
1107
1135
 
1108
1136
 
1137
+
1138
+
1139
+
1140
+
1109
1141
 
1110
1142
 
1111
1143
 
@@ -1158,6 +1190,10 @@ var Queue = class _Queue extends LinearBase {
1158
1190
 
1159
1191
 
1160
1192
 
1193
+
1194
+
1195
+
1196
+
1161
1197
 
1162
1198
 
1163
1199
 
@@ -1209,6 +1245,21 @@ var Queue = class _Queue extends LinearBase {
1209
1245
  this._elements[this._offset + index] = newElement;
1210
1246
  return true;
1211
1247
  }
1248
+ /**
1249
+ * Delete the first element that satisfies a predicate.
1250
+ * @remarks Time O(N), Space O(N)
1251
+ * @param predicate - Function (value, index, queue) → boolean to decide deletion.
1252
+ * @returns True if a match was removed.
1253
+ */
1254
+ deleteWhere(predicate) {
1255
+ for (let i = 0; i < this.length; i++) {
1256
+ if (predicate(this._elements[this._offset + i], i, this)) {
1257
+ this.deleteAt(i);
1258
+ return true;
1259
+ }
1260
+ }
1261
+ return false;
1262
+ }
1212
1263
  /**
1213
1264
  * Reverse the queue in-place by compacting then reversing.
1214
1265
  * @remarks Time O(N), Space O(N)
@@ -1251,6 +1302,10 @@ var Queue = class _Queue extends LinearBase {
1251
1302
 
1252
1303
 
1253
1304
 
1305
+
1306
+
1307
+
1308
+
1254
1309
 
1255
1310
 
1256
1311
 
@@ -1297,6 +1352,10 @@ var Queue = class _Queue extends LinearBase {
1297
1352
 
1298
1353
 
1299
1354
 
1355
+
1356
+
1357
+
1358
+
1300
1359
 
1301
1360
 
1302
1361
 
@@ -1366,6 +1425,10 @@ var Queue = class _Queue extends LinearBase {
1366
1425
 
1367
1426
 
1368
1427
 
1428
+
1429
+
1430
+
1431
+
1369
1432
 
1370
1433
 
1371
1434
 
@@ -1419,6 +1482,10 @@ var Queue = class _Queue extends LinearBase {
1419
1482
 
1420
1483
 
1421
1484
 
1485
+
1486
+
1487
+
1488
+
1422
1489
 
1423
1490
 
1424
1491
 
@@ -1476,6 +1543,10 @@ var Queue = class _Queue extends LinearBase {
1476
1543
 
1477
1544
 
1478
1545
 
1546
+
1547
+
1548
+
1549
+
1479
1550
 
1480
1551
 
1481
1552
 
@@ -1947,7 +2018,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
1947
2018
  }
1948
2019
  /**
1949
2020
  * Adds a new node to the tree.
1950
- * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation adds the node at the first available position in a level-order (BFS) traversal. This is NOT a Binary Search Tree insertion. Time O(N), where N is the number of nodes. It must traverse level-by-level to find an empty slot. Space O(N) in the worst case for the BFS queue (e.g., a full last level).
2021
+ * @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
2022
  *
1952
2023
  * @param keyNodeOrEntry - The key, node, or entry to add.
1953
2024
  * @returns True if the addition was successful, false otherwise.
@@ -1976,6 +2047,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
1976
2047
 
1977
2048
 
1978
2049
 
2050
+
2051
+
2052
+
2053
+
1979
2054
 
1980
2055
 
1981
2056
 
@@ -1995,7 +2070,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
1995
2070
  }
1996
2071
  /**
1997
2072
  * Adds or updates a new node to the tree.
1998
- * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation sets the node at the first available position in a level-order (BFS) traversal. This is NOT a Binary Search Tree insertion. Time O(N), where N is the number of nodes. It must traverse level-by-level to find an empty slot. Space O(N) in the worst case for the BFS queue (e.g., a full last level).
2073
+ * @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
2074
  *
2000
2075
  * @param keyNodeOrEntry - The key, node, or entry to set or update.
2001
2076
  * @param [value] - The value, if providing just a key.
@@ -2030,6 +2105,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2030
2105
 
2031
2106
 
2032
2107
 
2108
+
2109
+
2110
+
2111
+
2033
2112
 
2034
2113
 
2035
2114
 
@@ -2136,6 +2215,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2136
2215
 
2137
2216
 
2138
2217
 
2218
+
2219
+
2220
+
2221
+
2139
2222
 
2140
2223
 
2141
2224
 
@@ -2178,6 +2261,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2178
2261
 
2179
2262
 
2180
2263
 
2264
+
2265
+
2266
+
2267
+
2181
2268
 
2182
2269
 
2183
2270
 
@@ -2241,6 +2328,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2241
2328
 
2242
2329
 
2243
2330
 
2331
+
2332
+
2333
+
2334
+
2244
2335
 
2245
2336
 
2246
2337
 
@@ -2257,22 +2348,66 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2257
2348
  this.setMany(anotherTree, []);
2258
2349
  }
2259
2350
  /**
2260
- * Clears the tree and refills it with new items.
2261
- * @remarks Time O(N) (for `clear`) + O(N * M) (for `setMany`) = O(N * M). Space O(M) (from `setMany`).
2351
+ * Deletes a node from the tree (internal, returns balancing metadata).
2352
+ * @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).
2353
+ * @internal Used by AVL/BST subclasses that need balancing metadata after deletion.
2262
2354
  *
2263
- * @param keysNodesEntriesOrRaws - An iterable of items to set.
2264
- * @param [values] - An optional parallel iterable of values.
2355
+ * @param keyNodeEntryRawOrPredicate - The node to delete.
2356
+ * @returns An array containing deletion results with balancing metadata.
2265
2357
  */
2266
- refill(keysNodesEntriesOrRaws, values) {
2267
- this.clear();
2268
- this.setMany(keysNodesEntriesOrRaws, values);
2358
+ _deleteInternal(keyNodeEntryRawOrPredicate) {
2359
+ const deletedResult = [];
2360
+ if (!this._root) return deletedResult;
2361
+ const curr = this.getNode(keyNodeEntryRawOrPredicate);
2362
+ if (!curr) return deletedResult;
2363
+ const parent = curr?.parent;
2364
+ let needBalanced;
2365
+ let orgCurrent = curr;
2366
+ if (!curr.left && !curr.right && !parent) {
2367
+ this._setRoot(void 0);
2368
+ } else if (curr.left) {
2369
+ const leftSubTreeRightMost = this.getRightMost((node) => node, curr.left);
2370
+ if (leftSubTreeRightMost) {
2371
+ const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
2372
+ orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
2373
+ if (this._isMapMode) {
2374
+ this._store.set(curr.key, curr);
2375
+ this._store.set(leftSubTreeRightMost.key, leftSubTreeRightMost);
2376
+ }
2377
+ if (parentOfLeftSubTreeMax) {
2378
+ if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
2379
+ parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
2380
+ else parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
2381
+ needBalanced = parentOfLeftSubTreeMax;
2382
+ }
2383
+ }
2384
+ } else if (parent) {
2385
+ const { familyPosition: fp } = curr;
2386
+ if (fp === "LEFT" || fp === "ROOT_LEFT") {
2387
+ parent.left = curr.right;
2388
+ } else if (fp === "RIGHT" || fp === "ROOT_RIGHT") {
2389
+ parent.right = curr.right;
2390
+ }
2391
+ needBalanced = parent;
2392
+ } else {
2393
+ this._setRoot(curr.right);
2394
+ curr.right = void 0;
2395
+ }
2396
+ this._size = this._size - 1;
2397
+ deletedResult.push({ deleted: orgCurrent, needBalanced });
2398
+ if (this._isMapMode && orgCurrent) this._store.delete(orgCurrent.key);
2399
+ return deletedResult;
2269
2400
  }
2270
2401
  /**
2271
2402
  * Deletes a node from the tree.
2272
- * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation finds the node, and if it has two children, swaps it with the rightmost node of its left subtree (in-order predecessor) before deleting. Time O(N) in the worst case. O(N) to find the node (`getNode`) and O(H) (which is O(N) worst-case) to find the rightmost node. Space O(1) (if `getNode` is iterative, which it is).
2403
+ * @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
2404
  *
2274
2405
  * @param keyNodeEntryRawOrPredicate - The node to delete.
2275
- * @returns An array containing deletion results (for compatibility with self-balancing trees).
2406
+ * @returns True if the node was found and deleted, false otherwise.
2407
+
2408
+
2409
+
2410
+
2276
2411
 
2277
2412
 
2278
2413
 
@@ -2316,51 +2451,11 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2316
2451
  * console.log(tree.size); // 4;
2317
2452
  */
2318
2453
  delete(keyNodeEntryRawOrPredicate) {
2319
- const deletedResult = [];
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;
2454
+ return this._deleteInternal(keyNodeEntryRawOrPredicate).length > 0;
2360
2455
  }
2361
2456
  /**
2362
2457
  * Searches the tree for nodes matching a predicate.
2363
- * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). Performs a full DFS (pre-order) scan of the tree. Time O(N), as it may visit every node. Space O(H) for the call stack (recursive) or explicit stack (iterative), where H is the tree height (O(N) worst-case).
2458
+ * @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
2459
  *
2365
2460
  * @template C - The type of the callback function.
2366
2461
  * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
@@ -2409,7 +2504,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2409
2504
  }
2410
2505
  /**
2411
2506
  * Gets the first node matching a predicate.
2412
- * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). Time O(N) in the worst case (via `search`). Space O(H) or O(N) (via `search`).
2507
+ * @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
2508
  *
2414
2509
  * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
2415
2510
  * @param [startNode=this._root] - The node to start the search from.
@@ -2443,6 +2538,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2443
2538
 
2444
2539
 
2445
2540
 
2541
+
2542
+
2543
+
2544
+
2446
2545
 
2447
2546
 
2448
2547
 
@@ -2465,7 +2564,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2465
2564
  }
2466
2565
  /**
2467
2566
  * Gets the value associated with a key.
2468
- * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). Time O(1) if in Map mode. O(N) if not in Map mode (uses `getNode`). Space O(1) if in Map mode. O(H) or O(N) otherwise.
2567
+ * @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
2568
  *
2470
2569
  * @param keyNodeEntryOrPredicate - The key, node, or entry to get the value for.
2471
2570
  * @param [startNode=this._root] - The node to start searching from (if not in Map mode).
@@ -2501,6 +2600,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2501
2600
 
2502
2601
 
2503
2602
 
2603
+
2604
+
2605
+
2606
+
2504
2607
 
2505
2608
 
2506
2609
 
@@ -2561,6 +2664,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2561
2664
 
2562
2665
 
2563
2666
 
2667
+
2668
+
2669
+
2670
+
2564
2671
 
2565
2672
 
2566
2673
 
@@ -2609,6 +2716,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2609
2716
 
2610
2717
 
2611
2718
 
2719
+
2720
+
2721
+
2722
+
2612
2723
 
2613
2724
 
2614
2725
 
@@ -2666,6 +2777,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2666
2777
 
2667
2778
 
2668
2779
 
2780
+
2781
+
2782
+
2783
+
2669
2784
 
2670
2785
 
2671
2786
 
@@ -2750,6 +2865,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2750
2865
 
2751
2866
 
2752
2867
 
2868
+
2869
+
2870
+
2871
+
2753
2872
 
2754
2873
 
2755
2874
 
@@ -2811,6 +2930,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2811
2930
 
2812
2931
 
2813
2932
 
2933
+
2934
+
2935
+
2936
+
2814
2937
 
2815
2938
 
2816
2939
 
@@ -3288,6 +3411,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
3288
3411
 
3289
3412
 
3290
3413
 
3414
+
3415
+
3416
+
3417
+
3291
3418
 
3292
3419
 
3293
3420
 
@@ -3340,6 +3467,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
3340
3467
 
3341
3468
 
3342
3469
 
3470
+
3471
+
3472
+
3473
+
3343
3474
 
3344
3475
 
3345
3476
 
@@ -3396,6 +3527,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
3396
3527
 
3397
3528
 
3398
3529
 
3530
+
3531
+
3532
+
3533
+
3399
3534
 
3400
3535
 
3401
3536
 
@@ -3477,6 +3612,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
3477
3612
 
3478
3613
 
3479
3614
 
3615
+
3616
+
3617
+
3618
+
3480
3619
 
3481
3620
 
3482
3621