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
@@ -809,6 +809,10 @@ var binaryTreeTyped = (() => {
809
809
 
810
810
 
811
811
 
812
+
813
+
814
+
815
+
812
816
 
813
817
 
814
818
 
@@ -859,6 +863,10 @@ var binaryTreeTyped = (() => {
859
863
 
860
864
 
861
865
 
866
+
867
+
868
+
869
+
862
870
 
863
871
 
864
872
 
@@ -873,6 +881,14 @@ var binaryTreeTyped = (() => {
873
881
  get first() {
874
882
  return this.length > 0 ? this.elements[this._offset] : void 0;
875
883
  }
884
+ /**
885
+ * Peek at the front element without removing it (alias for `first`).
886
+ * @remarks Time O(1), Space O(1)
887
+ * @returns Front element or undefined.
888
+ */
889
+ peek() {
890
+ return this.first;
891
+ }
876
892
  /**
877
893
  * Get the last element (back) without removing it.
878
894
  * @remarks Time O(1), Space O(1)
@@ -925,6 +941,10 @@ var binaryTreeTyped = (() => {
925
941
 
926
942
 
927
943
 
944
+
945
+
946
+
947
+
928
948
 
929
949
 
930
950
 
@@ -987,6 +1007,10 @@ var binaryTreeTyped = (() => {
987
1007
 
988
1008
 
989
1009
 
1010
+
1011
+
1012
+
1013
+
990
1014
 
991
1015
 
992
1016
 
@@ -1056,6 +1080,10 @@ var binaryTreeTyped = (() => {
1056
1080
 
1057
1081
 
1058
1082
 
1083
+
1084
+
1085
+
1086
+
1059
1087
 
1060
1088
 
1061
1089
 
@@ -1115,6 +1143,10 @@ var binaryTreeTyped = (() => {
1115
1143
 
1116
1144
 
1117
1145
 
1146
+
1147
+
1148
+
1149
+
1118
1150
 
1119
1151
 
1120
1152
 
@@ -1167,6 +1199,10 @@ var binaryTreeTyped = (() => {
1167
1199
 
1168
1200
 
1169
1201
 
1202
+
1203
+
1204
+
1205
+
1170
1206
 
1171
1207
 
1172
1208
 
@@ -1218,6 +1254,21 @@ var binaryTreeTyped = (() => {
1218
1254
  this._elements[this._offset + index] = newElement;
1219
1255
  return true;
1220
1256
  }
1257
+ /**
1258
+ * Delete the first element that satisfies a predicate.
1259
+ * @remarks Time O(N), Space O(N)
1260
+ * @param predicate - Function (value, index, queue) → boolean to decide deletion.
1261
+ * @returns True if a match was removed.
1262
+ */
1263
+ deleteWhere(predicate) {
1264
+ for (let i = 0; i < this.length; i++) {
1265
+ if (predicate(this._elements[this._offset + i], i, this)) {
1266
+ this.deleteAt(i);
1267
+ return true;
1268
+ }
1269
+ }
1270
+ return false;
1271
+ }
1221
1272
  /**
1222
1273
  * Reverse the queue in-place by compacting then reversing.
1223
1274
  * @remarks Time O(N), Space O(N)
@@ -1260,6 +1311,10 @@ var binaryTreeTyped = (() => {
1260
1311
 
1261
1312
 
1262
1313
 
1314
+
1315
+
1316
+
1317
+
1263
1318
 
1264
1319
 
1265
1320
 
@@ -1306,6 +1361,10 @@ var binaryTreeTyped = (() => {
1306
1361
 
1307
1362
 
1308
1363
 
1364
+
1365
+
1366
+
1367
+
1309
1368
 
1310
1369
 
1311
1370
 
@@ -1375,6 +1434,10 @@ var binaryTreeTyped = (() => {
1375
1434
 
1376
1435
 
1377
1436
 
1437
+
1438
+
1439
+
1440
+
1378
1441
 
1379
1442
 
1380
1443
 
@@ -1428,6 +1491,10 @@ var binaryTreeTyped = (() => {
1428
1491
 
1429
1492
 
1430
1493
 
1494
+
1495
+
1496
+
1497
+
1431
1498
 
1432
1499
 
1433
1500
 
@@ -1485,6 +1552,10 @@ var binaryTreeTyped = (() => {
1485
1552
 
1486
1553
 
1487
1554
 
1555
+
1556
+
1557
+
1558
+
1488
1559
 
1489
1560
 
1490
1561
 
@@ -1960,7 +2031,7 @@ var binaryTreeTyped = (() => {
1960
2031
  }
1961
2032
  /**
1962
2033
  * Adds a new node to the tree.
1963
- * @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).
2034
+ * @remarks Time O(N) level-order traversal to find an empty slot. Space O(N) for the BFS queue. BST/Red-Black Tree/AVL Tree subclasses override to O(log N).
1964
2035
  *
1965
2036
  * @param keyNodeOrEntry - The key, node, or entry to add.
1966
2037
  * @returns True if the addition was successful, false otherwise.
@@ -1989,6 +2060,10 @@ var binaryTreeTyped = (() => {
1989
2060
 
1990
2061
 
1991
2062
 
2063
+
2064
+
2065
+
2066
+
1992
2067
 
1993
2068
 
1994
2069
 
@@ -2008,7 +2083,7 @@ var binaryTreeTyped = (() => {
2008
2083
  }
2009
2084
  /**
2010
2085
  * Adds or updates a new node to the tree.
2011
- * @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).
2086
+ * @remarks Time O(N) level-order traversal to find an empty slot. Space O(N) for the BFS queue. BST/Red-Black Tree/AVL Tree subclasses override to O(log N).
2012
2087
  *
2013
2088
  * @param keyNodeOrEntry - The key, node, or entry to set or update.
2014
2089
  * @param [value] - The value, if providing just a key.
@@ -2043,6 +2118,10 @@ var binaryTreeTyped = (() => {
2043
2118
 
2044
2119
 
2045
2120
 
2121
+
2122
+
2123
+
2124
+
2046
2125
 
2047
2126
 
2048
2127
 
@@ -2149,6 +2228,10 @@ var binaryTreeTyped = (() => {
2149
2228
 
2150
2229
 
2151
2230
 
2231
+
2232
+
2233
+
2234
+
2152
2235
 
2153
2236
 
2154
2237
 
@@ -2191,6 +2274,10 @@ var binaryTreeTyped = (() => {
2191
2274
 
2192
2275
 
2193
2276
 
2277
+
2278
+
2279
+
2280
+
2194
2281
 
2195
2282
 
2196
2283
 
@@ -2254,6 +2341,10 @@ var binaryTreeTyped = (() => {
2254
2341
 
2255
2342
 
2256
2343
 
2344
+
2345
+
2346
+
2347
+
2257
2348
 
2258
2349
 
2259
2350
 
@@ -2270,22 +2361,66 @@ var binaryTreeTyped = (() => {
2270
2361
  this.setMany(anotherTree, []);
2271
2362
  }
2272
2363
  /**
2273
- * Clears the tree and refills it with new items.
2274
- * @remarks Time O(N) (for `clear`) + O(N * M) (for `setMany`) = O(N * M). Space O(M) (from `setMany`).
2364
+ * Deletes a node from the tree (internal, returns balancing metadata).
2365
+ * @remarks Time O(N) O(N) to find the node + O(H) for predecessor swap. Space O(1). BST/Red-Black Tree/AVL Tree subclasses override to O(log N).
2366
+ * @internal Used by AVL/BST subclasses that need balancing metadata after deletion.
2275
2367
  *
2276
- * @param keysNodesEntriesOrRaws - An iterable of items to set.
2277
- * @param [values] - An optional parallel iterable of values.
2368
+ * @param keyNodeEntryRawOrPredicate - The node to delete.
2369
+ * @returns An array containing deletion results with balancing metadata.
2278
2370
  */
2279
- refill(keysNodesEntriesOrRaws, values) {
2280
- this.clear();
2281
- this.setMany(keysNodesEntriesOrRaws, values);
2371
+ _deleteInternal(keyNodeEntryRawOrPredicate) {
2372
+ const deletedResult = [];
2373
+ if (!this._root) return deletedResult;
2374
+ const curr = this.getNode(keyNodeEntryRawOrPredicate);
2375
+ if (!curr) return deletedResult;
2376
+ const parent = curr == null ? void 0 : curr.parent;
2377
+ let needBalanced;
2378
+ let orgCurrent = curr;
2379
+ if (!curr.left && !curr.right && !parent) {
2380
+ this._setRoot(void 0);
2381
+ } else if (curr.left) {
2382
+ const leftSubTreeRightMost = this.getRightMost((node) => node, curr.left);
2383
+ if (leftSubTreeRightMost) {
2384
+ const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
2385
+ orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
2386
+ if (this._isMapMode) {
2387
+ this._store.set(curr.key, curr);
2388
+ this._store.set(leftSubTreeRightMost.key, leftSubTreeRightMost);
2389
+ }
2390
+ if (parentOfLeftSubTreeMax) {
2391
+ if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
2392
+ parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
2393
+ else parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
2394
+ needBalanced = parentOfLeftSubTreeMax;
2395
+ }
2396
+ }
2397
+ } else if (parent) {
2398
+ const { familyPosition: fp } = curr;
2399
+ if (fp === "LEFT" || fp === "ROOT_LEFT") {
2400
+ parent.left = curr.right;
2401
+ } else if (fp === "RIGHT" || fp === "ROOT_RIGHT") {
2402
+ parent.right = curr.right;
2403
+ }
2404
+ needBalanced = parent;
2405
+ } else {
2406
+ this._setRoot(curr.right);
2407
+ curr.right = void 0;
2408
+ }
2409
+ this._size = this._size - 1;
2410
+ deletedResult.push({ deleted: orgCurrent, needBalanced });
2411
+ if (this._isMapMode && orgCurrent) this._store.delete(orgCurrent.key);
2412
+ return deletedResult;
2282
2413
  }
2283
2414
  /**
2284
2415
  * Deletes a node from the tree.
2285
- * @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).
2416
+ * @remarks Time O(N) O(N) to find the node + O(H) for predecessor swap. Space O(1). BST/Red-Black Tree/AVL Tree subclasses override to O(log N).
2286
2417
  *
2287
2418
  * @param keyNodeEntryRawOrPredicate - The node to delete.
2288
- * @returns An array containing deletion results (for compatibility with self-balancing trees).
2419
+ * @returns True if the node was found and deleted, false otherwise.
2420
+
2421
+
2422
+
2423
+
2289
2424
 
2290
2425
 
2291
2426
 
@@ -2329,51 +2464,11 @@ var binaryTreeTyped = (() => {
2329
2464
  * console.log(tree.size); // 4;
2330
2465
  */
2331
2466
  delete(keyNodeEntryRawOrPredicate) {
2332
- const deletedResult = [];
2333
- if (!this._root) return deletedResult;
2334
- const curr = this.getNode(keyNodeEntryRawOrPredicate);
2335
- if (!curr) return deletedResult;
2336
- const parent = curr == null ? void 0 : curr.parent;
2337
- let needBalanced;
2338
- let orgCurrent = curr;
2339
- if (!curr.left && !curr.right && !parent) {
2340
- this._setRoot(void 0);
2341
- } else if (curr.left) {
2342
- const leftSubTreeRightMost = this.getRightMost((node) => node, curr.left);
2343
- if (leftSubTreeRightMost) {
2344
- const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
2345
- orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
2346
- if (this._isMapMode) {
2347
- this._store.set(curr.key, curr);
2348
- this._store.set(leftSubTreeRightMost.key, leftSubTreeRightMost);
2349
- }
2350
- if (parentOfLeftSubTreeMax) {
2351
- if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
2352
- parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
2353
- else parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
2354
- needBalanced = parentOfLeftSubTreeMax;
2355
- }
2356
- }
2357
- } else if (parent) {
2358
- const { familyPosition: fp } = curr;
2359
- if (fp === "LEFT" || fp === "ROOT_LEFT") {
2360
- parent.left = curr.right;
2361
- } else if (fp === "RIGHT" || fp === "ROOT_RIGHT") {
2362
- parent.right = curr.right;
2363
- }
2364
- needBalanced = parent;
2365
- } else {
2366
- this._setRoot(curr.right);
2367
- curr.right = void 0;
2368
- }
2369
- this._size = this._size - 1;
2370
- deletedResult.push({ deleted: orgCurrent, needBalanced });
2371
- if (this._isMapMode && orgCurrent) this._store.delete(orgCurrent.key);
2372
- return deletedResult;
2467
+ return this._deleteInternal(keyNodeEntryRawOrPredicate).length > 0;
2373
2468
  }
2374
2469
  /**
2375
2470
  * Searches the tree for nodes matching a predicate.
2376
- * @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).
2471
+ * @remarks Time O(N) full DFS scan; may visit every node. Space O(H) for call/explicit stack (O(N) worst-case). BST subclasses with key search override to O(log N).
2377
2472
  *
2378
2473
  * @template C - The type of the callback function.
2379
2474
  * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
@@ -2422,7 +2517,7 @@ var binaryTreeTyped = (() => {
2422
2517
  }
2423
2518
  /**
2424
2519
  * Gets the first node matching a predicate.
2425
- * @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`).
2520
+ * @remarks Time O(N) via `search`. Space O(H) or O(N). BST/Red-Black Tree/AVL Tree subclasses override to O(log N) for key lookups.
2426
2521
  *
2427
2522
  * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
2428
2523
  * @param [startNode=this._root] - The node to start the search from.
@@ -2456,6 +2551,10 @@ var binaryTreeTyped = (() => {
2456
2551
 
2457
2552
 
2458
2553
 
2554
+
2555
+
2556
+
2557
+
2459
2558
 
2460
2559
 
2461
2560
 
@@ -2478,7 +2577,7 @@ var binaryTreeTyped = (() => {
2478
2577
  }
2479
2578
  /**
2480
2579
  * Gets the value associated with a key.
2481
- * @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.
2580
+ * @remarks Time O(1) in Map mode, O(N) otherwise (via `getNode`). Space O(1) in Map mode, O(H) or O(N) otherwise. BST subclasses override non-Map-mode to O(log N).
2482
2581
  *
2483
2582
  * @param keyNodeEntryOrPredicate - The key, node, or entry to get the value for.
2484
2583
  * @param [startNode=this._root] - The node to start searching from (if not in Map mode).
@@ -2514,6 +2613,10 @@ var binaryTreeTyped = (() => {
2514
2613
 
2515
2614
 
2516
2615
 
2616
+
2617
+
2618
+
2619
+
2517
2620
 
2518
2621
 
2519
2622
 
@@ -2575,6 +2678,10 @@ var binaryTreeTyped = (() => {
2575
2678
 
2576
2679
 
2577
2680
 
2681
+
2682
+
2683
+
2684
+
2578
2685
 
2579
2686
 
2580
2687
 
@@ -2623,6 +2730,10 @@ var binaryTreeTyped = (() => {
2623
2730
 
2624
2731
 
2625
2732
 
2733
+
2734
+
2735
+
2736
+
2626
2737
 
2627
2738
 
2628
2739
 
@@ -2680,6 +2791,10 @@ var binaryTreeTyped = (() => {
2680
2791
 
2681
2792
 
2682
2793
 
2794
+
2795
+
2796
+
2797
+
2683
2798
 
2684
2799
 
2685
2800
 
@@ -2764,6 +2879,10 @@ var binaryTreeTyped = (() => {
2764
2879
 
2765
2880
 
2766
2881
 
2882
+
2883
+
2884
+
2885
+
2767
2886
 
2768
2887
 
2769
2888
 
@@ -2825,6 +2944,10 @@ var binaryTreeTyped = (() => {
2825
2944
 
2826
2945
 
2827
2946
 
2947
+
2948
+
2949
+
2950
+
2828
2951
 
2829
2952
 
2830
2953
 
@@ -3302,6 +3425,10 @@ var binaryTreeTyped = (() => {
3302
3425
 
3303
3426
 
3304
3427
 
3428
+
3429
+
3430
+
3431
+
3305
3432
 
3306
3433
 
3307
3434
 
@@ -3354,6 +3481,10 @@ var binaryTreeTyped = (() => {
3354
3481
 
3355
3482
 
3356
3483
 
3484
+
3485
+
3486
+
3487
+
3357
3488
 
3358
3489
 
3359
3490
 
@@ -3410,6 +3541,10 @@ var binaryTreeTyped = (() => {
3410
3541
 
3411
3542
 
3412
3543
 
3544
+
3545
+
3546
+
3547
+
3413
3548
 
3414
3549
 
3415
3550
 
@@ -3491,6 +3626,10 @@ var binaryTreeTyped = (() => {
3491
3626
 
3492
3627
 
3493
3628
 
3629
+
3630
+
3631
+
3632
+
3494
3633
 
3495
3634
 
3496
3635