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
@@ -216,7 +216,7 @@ export class BinaryTreeNode<K = any, V = any> {
216
216
  * node?: BinaryTreeNode<string> | null,
217
217
  * conditions?: { [key: string]: boolean }
218
218
  * ): string {
219
- * if (!node) raise(Error, 'Invalid node');
219
+ * if (!node) throw new Error('Invalid node');
220
220
  *
221
221
  * // If it's a leaf node, return the decision result
222
222
  * if (!node.left && !node.right) return node.key;
@@ -261,7 +261,7 @@ export class BinaryTreeNode<K = any, V = any> {
261
261
  * case '/':
262
262
  * return rightValue !== 0 ? leftValue / rightValue : 0; // Handle division by zero
263
263
  * default:
264
- * raise(Error, `Unsupported operator: ${node.key}`);
264
+ * throw new Error(`Unsupported operator: ${node.key}`);
265
265
  * }
266
266
  * }
267
267
  *
@@ -562,7 +562,7 @@ export class BinaryTree<K = any, V = any, R = any>
562
562
 
563
563
  /**
564
564
  * Adds a new node to the tree.
565
- * @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).
565
+ * @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).
566
566
  *
567
567
  * @param keyNodeOrEntry - The key, node, or entry to add.
568
568
  * @returns True if the addition was successful, false otherwise.
@@ -591,6 +591,10 @@ export class BinaryTree<K = any, V = any, R = any>
591
591
 
592
592
 
593
593
 
594
+
595
+
596
+
597
+
594
598
 
595
599
 
596
600
 
@@ -613,7 +617,7 @@ export class BinaryTree<K = any, V = any, R = any>
613
617
 
614
618
  /**
615
619
  * Adds or updates a new node to the tree.
616
- * @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).
620
+ * @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).
617
621
  *
618
622
  * @param keyNodeOrEntry - The key, node, or entry to set or update.
619
623
  * @param [value] - The value, if providing just a key.
@@ -648,6 +652,10 @@ export class BinaryTree<K = any, V = any, R = any>
648
652
 
649
653
 
650
654
 
655
+
656
+
657
+
658
+
651
659
 
652
660
 
653
661
 
@@ -767,6 +775,10 @@ export class BinaryTree<K = any, V = any, R = any>
767
775
 
768
776
 
769
777
 
778
+
779
+
780
+
781
+
770
782
 
771
783
 
772
784
 
@@ -814,6 +826,10 @@ export class BinaryTree<K = any, V = any, R = any>
814
826
 
815
827
 
816
828
 
829
+
830
+
831
+
832
+
817
833
 
818
834
 
819
835
 
@@ -887,6 +903,10 @@ export class BinaryTree<K = any, V = any, R = any>
887
903
 
888
904
 
889
905
 
906
+
907
+
908
+
909
+
890
910
 
891
911
 
892
912
 
@@ -904,71 +924,14 @@ export class BinaryTree<K = any, V = any, R = any>
904
924
  }
905
925
 
906
926
  /**
907
- * Clears the tree and refills it with new items.
908
- * @remarks Time O(N) (for `clear`) + O(N * M) (for `setMany`) = O(N * M). Space O(M) (from `setMany`).
909
- *
910
- * @param keysNodesEntriesOrRaws - An iterable of items to set.
911
- * @param [values] - An optional parallel iterable of values.
912
- */
913
- refill(
914
- keysNodesEntriesOrRaws: Iterable<
915
- K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R
916
- >,
917
- values?: Iterable<V | undefined>
918
- ): void {
919
- this.clear();
920
- this.setMany(keysNodesEntriesOrRaws, values);
921
- }
922
-
923
- /**
924
- * Deletes a node from the tree.
925
- * @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).
927
+ * Deletes a node from the tree (internal, returns balancing metadata).
928
+ * @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).
929
+ * @internal Used by AVL/BST subclasses that need balancing metadata after deletion.
926
930
  *
927
931
  * @param keyNodeEntryRawOrPredicate - The node to delete.
928
- * @returns An array containing deletion results (for compatibility with self-balancing trees).
929
-
930
-
931
-
932
-
933
-
934
-
935
-
936
-
937
-
938
-
939
-
940
-
941
-
942
-
943
-
944
-
945
-
946
-
947
-
948
-
949
-
950
-
951
-
952
-
953
-
954
-
955
-
956
-
957
-
958
-
959
-
960
-
961
-
962
-
963
-
964
- * @example
965
- * // Remove a node
966
- * const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
967
- * tree.delete(3);
968
- * console.log(tree.has(3)); // false;
969
- * console.log(tree.size); // 4;
932
+ * @returns An array containing deletion results with balancing metadata.
970
933
  */
971
- delete(
934
+ protected _deleteInternal(
972
935
  keyNodeEntryRawOrPredicate: BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V> | null>
973
936
  ): BinaryTreeDeleteResult<BinaryTreeNode<K, V>>[] {
974
937
  const deletedResult: BinaryTreeDeleteResult<BinaryTreeNode<K, V>>[] = [];
@@ -982,26 +945,19 @@ export class BinaryTree<K = any, V = any, R = any>
982
945
  let orgCurrent: BinaryTreeNode<K, V> | undefined = curr;
983
946
 
984
947
  if (!curr.left && !curr.right && !parent) {
985
- // Deleting the root with no children
986
948
  this._setRoot(undefined);
987
949
  } else if (curr.left) {
988
- // Node has a left child (or two children)
989
- // Find the rightmost node in the left subtree
990
950
  const leftSubTreeRightMost = this.getRightMost(node => node, curr.left);
991
951
  if (leftSubTreeRightMost) {
992
952
  const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
993
- // Swap properties
994
953
  orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
995
954
 
996
- // Map mode store tracks key -> node reference; after swapping keys we must re-index both nodes.
997
955
  if (this._isMapMode) {
998
956
  this._store.set(curr.key, curr);
999
957
  this._store.set(leftSubTreeRightMost.key, leftSubTreeRightMost);
1000
958
  }
1001
959
 
1002
- // `orgCurrent` is now the node to be physically deleted (which was the rightmost)
1003
960
  if (parentOfLeftSubTreeMax) {
1004
- // Unlink the rightmost node
1005
961
  if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
1006
962
  parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
1007
963
  else parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
@@ -1009,8 +965,6 @@ export class BinaryTree<K = any, V = any, R = any>
1009
965
  }
1010
966
  }
1011
967
  } else if (parent) {
1012
- // Node has no left child, but has a parent
1013
- // Promote the right child (which could be null)
1014
968
  const { familyPosition: fp } = curr;
1015
969
  if (fp === 'LEFT' || fp === 'ROOT_LEFT') {
1016
970
  parent.left = curr.right;
@@ -1019,8 +973,6 @@ export class BinaryTree<K = any, V = any, R = any>
1019
973
  }
1020
974
  needBalanced = parent;
1021
975
  } else {
1022
- // Deleting the root, which has no left child
1023
- // Promote the right child as the new root
1024
976
  this._setRoot(curr.right);
1025
977
  curr.right = undefined;
1026
978
  }
@@ -1032,6 +984,64 @@ export class BinaryTree<K = any, V = any, R = any>
1032
984
  return deletedResult;
1033
985
  }
1034
986
 
987
+ /**
988
+ * Deletes a node from the tree.
989
+ * @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).
990
+ *
991
+ * @param keyNodeEntryRawOrPredicate - The node to delete.
992
+ * @returns True if the node was found and deleted, false otherwise.
993
+
994
+
995
+
996
+
997
+
998
+
999
+
1000
+
1001
+
1002
+
1003
+
1004
+
1005
+
1006
+
1007
+
1008
+
1009
+
1010
+
1011
+
1012
+
1013
+
1014
+
1015
+
1016
+
1017
+
1018
+
1019
+
1020
+
1021
+
1022
+
1023
+
1024
+
1025
+
1026
+
1027
+
1028
+
1029
+
1030
+
1031
+
1032
+ * @example
1033
+ * // Remove a node
1034
+ * const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
1035
+ * tree.delete(3);
1036
+ * console.log(tree.has(3)); // false;
1037
+ * console.log(tree.size); // 4;
1038
+ */
1039
+ delete(
1040
+ keyNodeEntryRawOrPredicate: BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V> | null>
1041
+ ): boolean {
1042
+ return this._deleteInternal(keyNodeEntryRawOrPredicate).length > 0;
1043
+ }
1044
+
1035
1045
  /**
1036
1046
  * Search by predicate
1037
1047
 
@@ -1055,6 +1065,10 @@ export class BinaryTree<K = any, V = any, R = any>
1055
1065
 
1056
1066
 
1057
1067
 
1068
+
1069
+
1070
+
1071
+
1058
1072
 
1059
1073
 
1060
1074
 
@@ -1093,7 +1107,7 @@ export class BinaryTree<K = any, V = any, R = any>
1093
1107
 
1094
1108
  /**
1095
1109
  * Searches the tree for nodes matching a predicate.
1096
- * @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).
1110
+ * @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).
1097
1111
  *
1098
1112
  * @template C - The type of the callback function.
1099
1113
  * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
@@ -1191,6 +1205,10 @@ export class BinaryTree<K = any, V = any, R = any>
1191
1205
 
1192
1206
 
1193
1207
 
1208
+
1209
+
1210
+
1211
+
1194
1212
 
1195
1213
 
1196
1214
 
@@ -1232,7 +1250,7 @@ export class BinaryTree<K = any, V = any, R = any>
1232
1250
 
1233
1251
  /**
1234
1252
  * Gets the first node matching a predicate.
1235
- * @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`).
1253
+ * @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.
1236
1254
  *
1237
1255
  * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
1238
1256
  * @param [startNode=this._root] - The node to start the search from.
@@ -1266,6 +1284,10 @@ export class BinaryTree<K = any, V = any, R = any>
1266
1284
 
1267
1285
 
1268
1286
 
1287
+
1288
+
1289
+
1290
+
1269
1291
 
1270
1292
 
1271
1293
 
@@ -1299,7 +1321,7 @@ export class BinaryTree<K = any, V = any, R = any>
1299
1321
 
1300
1322
  /**
1301
1323
  * Gets the value associated with a key.
1302
- * @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.
1324
+ * @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).
1303
1325
  *
1304
1326
  * @param keyNodeEntryOrPredicate - The key, node, or entry to get the value for.
1305
1327
  * @param [startNode=this._root] - The node to start searching from (if not in Map mode).
@@ -1335,6 +1357,10 @@ export class BinaryTree<K = any, V = any, R = any>
1335
1357
 
1336
1358
 
1337
1359
 
1360
+
1361
+
1362
+
1363
+
1338
1364
 
1339
1365
 
1340
1366
 
@@ -1361,7 +1387,7 @@ export class BinaryTree<K = any, V = any, R = any>
1361
1387
 
1362
1388
  /**
1363
1389
  * Checks if a node matching the predicate exists in the tree.
1364
- * @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`).
1390
+ * @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.
1365
1391
  *
1366
1392
  * @param [keyNodeEntryOrPredicate] - The key, node, entry, or predicate to check for.
1367
1393
  * @param [startNode] - The node to start the search from.
@@ -1397,6 +1423,10 @@ export class BinaryTree<K = any, V = any, R = any>
1397
1423
 
1398
1424
 
1399
1425
 
1426
+
1427
+
1428
+
1429
+
1400
1430
 
1401
1431
 
1402
1432
 
@@ -1495,6 +1525,10 @@ export class BinaryTree<K = any, V = any, R = any>
1495
1525
 
1496
1526
 
1497
1527
 
1528
+
1529
+
1530
+
1531
+
1498
1532
 
1499
1533
 
1500
1534
 
@@ -1544,6 +1578,10 @@ export class BinaryTree<K = any, V = any, R = any>
1544
1578
 
1545
1579
 
1546
1580
 
1581
+
1582
+
1583
+
1584
+
1547
1585
 
1548
1586
 
1549
1587
 
@@ -1605,6 +1643,10 @@ export class BinaryTree<K = any, V = any, R = any>
1605
1643
 
1606
1644
 
1607
1645
 
1646
+
1647
+
1648
+
1649
+
1608
1650
 
1609
1651
 
1610
1652
 
@@ -1696,6 +1738,10 @@ export class BinaryTree<K = any, V = any, R = any>
1696
1738
 
1697
1739
 
1698
1740
 
1741
+
1742
+
1743
+
1744
+
1699
1745
 
1700
1746
 
1701
1747
 
@@ -1761,6 +1807,10 @@ export class BinaryTree<K = any, V = any, R = any>
1761
1807
 
1762
1808
 
1763
1809
 
1810
+
1811
+
1812
+
1813
+
1764
1814
 
1765
1815
 
1766
1816
 
@@ -2067,6 +2117,10 @@ export class BinaryTree<K = any, V = any, R = any>
2067
2117
 
2068
2118
 
2069
2119
 
2120
+
2121
+
2122
+
2123
+
2070
2124
 
2071
2125
 
2072
2126
 
@@ -2155,6 +2209,10 @@ export class BinaryTree<K = any, V = any, R = any>
2155
2209
 
2156
2210
 
2157
2211
 
2212
+
2213
+
2214
+
2215
+
2158
2216
 
2159
2217
 
2160
2218
 
@@ -2302,6 +2360,10 @@ export class BinaryTree<K = any, V = any, R = any>
2302
2360
 
2303
2361
 
2304
2362
 
2363
+
2364
+
2365
+
2366
+
2305
2367
 
2306
2368
 
2307
2369
 
@@ -2402,6 +2464,10 @@ export class BinaryTree<K = any, V = any, R = any>
2402
2464
 
2403
2465
 
2404
2466
 
2467
+
2468
+
2469
+
2470
+
2405
2471
 
2406
2472
 
2407
2473
 
@@ -2520,6 +2586,10 @@ export class BinaryTree<K = any, V = any, R = any>
2520
2586
 
2521
2587
 
2522
2588
 
2589
+
2590
+
2591
+
2592
+
2523
2593
 
2524
2594
 
2525
2595
 
@@ -2682,6 +2752,10 @@ export class BinaryTree<K = any, V = any, R = any>
2682
2752
 
2683
2753
 
2684
2754
 
2755
+
2756
+
2757
+
2758
+
2685
2759
 
2686
2760
 
2687
2761
 
@@ -2735,6 +2809,10 @@ export class BinaryTree<K = any, V = any, R = any>
2735
2809
 
2736
2810
 
2737
2811
 
2812
+
2813
+
2814
+
2815
+
2738
2816
 
2739
2817
 
2740
2818
 
@@ -2792,6 +2870,10 @@ export class BinaryTree<K = any, V = any, R = any>
2792
2870
 
2793
2871
 
2794
2872
 
2873
+
2874
+
2875
+
2876
+
2795
2877
 
2796
2878
 
2797
2879
 
@@ -2882,6 +2964,10 @@ export class BinaryTree<K = any, V = any, R = any>
2882
2964
 
2883
2965
 
2884
2966
 
2967
+
2968
+
2969
+
2970
+
2885
2971
 
2886
2972
 
2887
2973