binary-tree-typed 2.5.0 → 2.5.2

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 (90) hide show
  1. package/dist/cjs/index.cjs +771 -68
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +771 -68
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +771 -69
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +771 -69
  8. package/dist/esm-legacy/index.mjs.map +1 -1
  9. package/dist/types/common/error.d.ts +9 -0
  10. package/dist/types/common/index.d.ts +1 -1
  11. package/dist/types/data-structures/base/index.d.ts +1 -0
  12. package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
  13. package/dist/types/data-structures/base/linear-base.d.ts +3 -3
  14. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +288 -0
  15. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +336 -0
  16. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +618 -18
  17. package/dist/types/data-structures/binary-tree/bst.d.ts +676 -1
  18. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +456 -0
  19. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +144 -1
  20. package/dist/types/data-structures/binary-tree/tree-map.d.ts +3307 -399
  21. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3285 -360
  22. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2674 -325
  23. package/dist/types/data-structures/binary-tree/tree-set.d.ts +3072 -287
  24. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
  25. package/dist/types/data-structures/graph/directed-graph.d.ts +240 -0
  26. package/dist/types/data-structures/graph/undirected-graph.d.ts +216 -0
  27. package/dist/types/data-structures/hash/hash-map.d.ts +274 -10
  28. package/dist/types/data-structures/heap/heap.d.ts +336 -0
  29. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +411 -3
  30. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +363 -3
  31. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +434 -2
  32. package/dist/types/data-structures/matrix/matrix.d.ts +192 -0
  33. package/dist/types/data-structures/queue/deque.d.ts +364 -4
  34. package/dist/types/data-structures/queue/queue.d.ts +288 -0
  35. package/dist/types/data-structures/stack/stack.d.ts +240 -0
  36. package/dist/types/data-structures/trie/trie.d.ts +292 -4
  37. package/dist/types/interfaces/graph.d.ts +1 -1
  38. package/dist/types/types/common.d.ts +2 -2
  39. package/dist/types/types/data-structures/binary-tree/bst.d.ts +1 -0
  40. package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +5 -0
  41. package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +4 -0
  42. package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +4 -0
  43. package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
  44. package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
  45. package/dist/types/types/utils/validate-type.d.ts +4 -4
  46. package/dist/umd/binary-tree-typed.js +773 -71
  47. package/dist/umd/binary-tree-typed.js.map +1 -1
  48. package/dist/umd/binary-tree-typed.min.js +5 -5
  49. package/dist/umd/binary-tree-typed.min.js.map +1 -1
  50. package/package.json +2 -2
  51. package/src/common/error.ts +19 -1
  52. package/src/common/index.ts +1 -1
  53. package/src/data-structures/base/index.ts +1 -0
  54. package/src/data-structures/base/iterable-element-base.ts +3 -2
  55. package/src/data-structures/base/iterable-entry-base.ts +8 -8
  56. package/src/data-structures/base/linear-base.ts +3 -3
  57. package/src/data-structures/binary-tree/avl-tree.ts +299 -0
  58. package/src/data-structures/binary-tree/binary-indexed-tree.ts +341 -5
  59. package/src/data-structures/binary-tree/binary-tree.ts +606 -6
  60. package/src/data-structures/binary-tree/bst.ts +946 -7
  61. package/src/data-structures/binary-tree/red-black-tree.ts +472 -0
  62. package/src/data-structures/binary-tree/segment-tree.ts +145 -2
  63. package/src/data-structures/binary-tree/tree-map.ts +3423 -499
  64. package/src/data-structures/binary-tree/tree-multi-map.ts +3537 -596
  65. package/src/data-structures/binary-tree/tree-multi-set.ts +2855 -495
  66. package/src/data-structures/binary-tree/tree-set.ts +3209 -413
  67. package/src/data-structures/graph/abstract-graph.ts +6 -6
  68. package/src/data-structures/graph/directed-graph.ts +240 -0
  69. package/src/data-structures/graph/undirected-graph.ts +216 -0
  70. package/src/data-structures/hash/hash-map.ts +281 -19
  71. package/src/data-structures/heap/heap.ts +340 -4
  72. package/src/data-structures/heap/max-heap.ts +2 -2
  73. package/src/data-structures/linked-list/doubly-linked-list.ts +411 -3
  74. package/src/data-structures/linked-list/singly-linked-list.ts +363 -3
  75. package/src/data-structures/linked-list/skip-linked-list.ts +439 -7
  76. package/src/data-structures/matrix/matrix.ts +202 -10
  77. package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
  78. package/src/data-structures/queue/deque.ts +365 -5
  79. package/src/data-structures/queue/queue.ts +288 -0
  80. package/src/data-structures/stack/stack.ts +240 -0
  81. package/src/data-structures/trie/trie.ts +295 -7
  82. package/src/interfaces/graph.ts +1 -1
  83. package/src/types/common.ts +2 -2
  84. package/src/types/data-structures/binary-tree/bst.ts +1 -0
  85. package/src/types/data-structures/binary-tree/tree-map.ts +6 -0
  86. package/src/types/data-structures/binary-tree/tree-multi-set.ts +5 -0
  87. package/src/types/data-structures/binary-tree/tree-set.ts +5 -0
  88. package/src/types/data-structures/heap/heap.ts +1 -0
  89. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
  90. package/src/types/utils/validate-type.ts +4 -4
@@ -28,7 +28,7 @@ import { IBinaryTree } from '../../interfaces';
28
28
  import { isComparable, makeTrampoline, makeTrampolineThunk } from '../../utils';
29
29
  import { Queue } from '../queue';
30
30
  import { IterableEntryBase } from '../base';
31
- import { DFSOperation, ERR, Range } from '../../common';
31
+ import { DFSOperation, ERR, raise, Range } from '../../common';
32
32
 
33
33
  /**
34
34
  * @template K - The type of the key.
@@ -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) throw new Error('Invalid node');
219
+ * if (!node) raise(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
- * throw new Error(`Unsupported operator: ${node.key}`);
264
+ * raise(Error, `Unsupported operator: ${node.key}`);
265
265
  * }
266
266
  * }
267
267
  *
@@ -293,7 +293,7 @@ export class BinaryTree<K = any, V = any, R = any>
293
293
  if (isMapMode !== undefined) this._isMapMode = isMapMode;
294
294
  if (isDuplicate !== undefined) this._isDuplicate = isDuplicate;
295
295
  if (typeof toEntryFn === 'function') this._toEntryFn = toEntryFn;
296
- else if (toEntryFn) throw new TypeError(ERR.notAFunction('toEntryFn', 'BinaryTree'));
296
+ else if (toEntryFn) raise(TypeError, ERR.notAFunction('toEntryFn', 'BinaryTree'));
297
297
  }
298
298
 
299
299
  if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
@@ -555,7 +555,7 @@ export class BinaryTree<K = any, V = any, R = any>
555
555
  * @param key - The key to validate.
556
556
  * @returns True if the key is valid, false otherwise.
557
557
  */
558
- isValidKey(key: any): key is K {
558
+ isValidKey(key: unknown): key is K {
559
559
  if (key === null) return true;
560
560
  return isComparable(key);
561
561
  }
@@ -572,6 +572,30 @@ export class BinaryTree<K = any, V = any, R = any>
572
572
 
573
573
 
574
574
 
575
+
576
+
577
+
578
+
579
+
580
+
581
+
582
+
583
+
584
+
585
+
586
+
587
+
588
+
589
+
590
+
591
+
592
+
593
+
594
+
595
+
596
+
597
+
598
+
575
599
  * @example
576
600
  * // Add a single node
577
601
  * const tree = new BinaryTree<number>();
@@ -605,6 +629,30 @@ export class BinaryTree<K = any, V = any, R = any>
605
629
 
606
630
 
607
631
 
632
+
633
+
634
+
635
+
636
+
637
+
638
+
639
+
640
+
641
+
642
+
643
+
644
+
645
+
646
+
647
+
648
+
649
+
650
+
651
+
652
+
653
+
654
+
655
+
608
656
  * @example
609
657
  * // basic BinaryTree creation and insertion
610
658
  * // Create a BinaryTree with entries
@@ -700,6 +748,30 @@ export class BinaryTree<K = any, V = any, R = any>
700
748
 
701
749
 
702
750
 
751
+
752
+
753
+
754
+
755
+
756
+
757
+
758
+
759
+
760
+
761
+
762
+
763
+
764
+
765
+
766
+
767
+
768
+
769
+
770
+
771
+
772
+
773
+
774
+
703
775
  * @example
704
776
  * // Bulk add
705
777
  * const tree = new BinaryTree<number>();
@@ -723,6 +795,30 @@ export class BinaryTree<K = any, V = any, R = any>
723
795
  * @returns An array of booleans indicating the success of each individual `set` operation.
724
796
 
725
797
 
798
+
799
+
800
+
801
+
802
+
803
+
804
+
805
+
806
+
807
+
808
+
809
+
810
+
811
+
812
+
813
+
814
+
815
+
816
+
817
+
818
+
819
+
820
+
821
+
726
822
  * @example
727
823
  * // Set multiple entries
728
824
  * const tree = new BinaryTree<number, string>();
@@ -772,6 +868,30 @@ export class BinaryTree<K = any, V = any, R = any>
772
868
 
773
869
 
774
870
 
871
+
872
+
873
+
874
+
875
+
876
+
877
+
878
+
879
+
880
+
881
+
882
+
883
+
884
+
885
+
886
+
887
+
888
+
889
+
890
+
891
+
892
+
893
+
894
+
775
895
  * @example
776
896
  * // Combine trees
777
897
  * const t1 = new BinaryTree<number>([1, 2]);
@@ -817,6 +937,30 @@ export class BinaryTree<K = any, V = any, R = any>
817
937
 
818
938
 
819
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
+
820
964
  * @example
821
965
  * // Remove a node
822
966
  * const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
@@ -892,6 +1036,30 @@ export class BinaryTree<K = any, V = any, R = any>
892
1036
  * Search by predicate
893
1037
 
894
1038
 
1039
+
1040
+
1041
+
1042
+
1043
+
1044
+
1045
+
1046
+
1047
+
1048
+
1049
+
1050
+
1051
+
1052
+
1053
+
1054
+
1055
+
1056
+
1057
+
1058
+
1059
+
1060
+
1061
+
1062
+
895
1063
  * @example
896
1064
  * // Search by predicate
897
1065
  * const tree = new BinaryTree<number>([5, 3, 7, 1, 9]);
@@ -1004,6 +1172,30 @@ export class BinaryTree<K = any, V = any, R = any>
1004
1172
 
1005
1173
 
1006
1174
 
1175
+
1176
+
1177
+
1178
+
1179
+
1180
+
1181
+
1182
+
1183
+
1184
+
1185
+
1186
+
1187
+
1188
+
1189
+
1190
+
1191
+
1192
+
1193
+
1194
+
1195
+
1196
+
1197
+
1198
+
1007
1199
  * @example
1008
1200
  * // Get nodes by condition
1009
1201
  * const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
@@ -1055,6 +1247,30 @@ export class BinaryTree<K = any, V = any, R = any>
1055
1247
 
1056
1248
 
1057
1249
 
1250
+
1251
+
1252
+
1253
+
1254
+
1255
+
1256
+
1257
+
1258
+
1259
+
1260
+
1261
+
1262
+
1263
+
1264
+
1265
+
1266
+
1267
+
1268
+
1269
+
1270
+
1271
+
1272
+
1273
+
1058
1274
  * @example
1059
1275
  * // Get node by key
1060
1276
  * const tree = new BinaryTree<number, string>([[1, 'root'], [2, 'child']]);
@@ -1100,6 +1316,30 @@ export class BinaryTree<K = any, V = any, R = any>
1100
1316
 
1101
1317
 
1102
1318
 
1319
+
1320
+
1321
+
1322
+
1323
+
1324
+
1325
+
1326
+
1327
+
1328
+
1329
+
1330
+
1331
+
1332
+
1333
+
1334
+
1335
+
1336
+
1337
+
1338
+
1339
+
1340
+
1341
+
1342
+
1103
1343
  * @example
1104
1344
  * // Retrieve value by key
1105
1345
  * const tree = new BinaryTree<number, string>([[1, 'root'], [2, 'left'], [3, 'right']]);
@@ -1138,6 +1378,30 @@ export class BinaryTree<K = any, V = any, R = any>
1138
1378
 
1139
1379
 
1140
1380
 
1381
+
1382
+
1383
+
1384
+
1385
+
1386
+
1387
+
1388
+
1389
+
1390
+
1391
+
1392
+
1393
+
1394
+
1395
+
1396
+
1397
+
1398
+
1399
+
1400
+
1401
+
1402
+
1403
+
1404
+
1141
1405
  * @example
1142
1406
  * // BinaryTree get and has operations
1143
1407
  * const tree = new BinaryTree(
@@ -1212,6 +1476,30 @@ export class BinaryTree<K = any, V = any, R = any>
1212
1476
 
1213
1477
 
1214
1478
 
1479
+
1480
+
1481
+
1482
+
1483
+
1484
+
1485
+
1486
+
1487
+
1488
+
1489
+
1490
+
1491
+
1492
+
1493
+
1494
+
1495
+
1496
+
1497
+
1498
+
1499
+
1500
+
1501
+
1502
+
1215
1503
  * @example
1216
1504
  * // Remove all nodes
1217
1505
  * const tree = new BinaryTree<number>([1, 2, 3]);
@@ -1237,6 +1525,30 @@ export class BinaryTree<K = any, V = any, R = any>
1237
1525
 
1238
1526
 
1239
1527
 
1528
+
1529
+
1530
+
1531
+
1532
+
1533
+
1534
+
1535
+
1536
+
1537
+
1538
+
1539
+
1540
+
1541
+
1542
+
1543
+
1544
+
1545
+
1546
+
1547
+
1548
+
1549
+
1550
+
1551
+
1240
1552
  * @example
1241
1553
  * // Check empty
1242
1554
  * console.log(new BinaryTree().isEmpty()); // true;
@@ -1274,6 +1586,30 @@ export class BinaryTree<K = any, V = any, R = any>
1274
1586
 
1275
1587
 
1276
1588
 
1589
+
1590
+
1591
+
1592
+
1593
+
1594
+
1595
+
1596
+
1597
+
1598
+
1599
+
1600
+
1601
+
1602
+
1603
+
1604
+
1605
+
1606
+
1607
+
1608
+
1609
+
1610
+
1611
+
1612
+
1277
1613
  * @example
1278
1614
  * // Check BST property
1279
1615
  * const tree = new BinaryTree<number>([1, 2, 3]);
@@ -1341,6 +1677,30 @@ export class BinaryTree<K = any, V = any, R = any>
1341
1677
 
1342
1678
 
1343
1679
 
1680
+
1681
+
1682
+
1683
+
1684
+
1685
+
1686
+
1687
+
1688
+
1689
+
1690
+
1691
+
1692
+
1693
+
1694
+
1695
+
1696
+
1697
+
1698
+
1699
+
1700
+
1701
+
1702
+
1703
+
1344
1704
  * @example
1345
1705
  * // Get depth of a node
1346
1706
  * const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
@@ -1382,6 +1742,30 @@ export class BinaryTree<K = any, V = any, R = any>
1382
1742
 
1383
1743
 
1384
1744
 
1745
+
1746
+
1747
+
1748
+
1749
+
1750
+
1751
+
1752
+
1753
+
1754
+
1755
+
1756
+
1757
+
1758
+
1759
+
1760
+
1761
+
1762
+
1763
+
1764
+
1765
+
1766
+
1767
+
1768
+
1385
1769
  * @example
1386
1770
  * // Get tree height
1387
1771
  * const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
@@ -1664,6 +2048,30 @@ export class BinaryTree<K = any, V = any, R = any>
1664
2048
 
1665
2049
 
1666
2050
 
2051
+
2052
+
2053
+
2054
+
2055
+
2056
+
2057
+
2058
+
2059
+
2060
+
2061
+
2062
+
2063
+
2064
+
2065
+
2066
+
2067
+
2068
+
2069
+
2070
+
2071
+
2072
+
2073
+
2074
+
1667
2075
  * @example
1668
2076
  * // Depth-first search traversal
1669
2077
  * const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
@@ -1728,6 +2136,30 @@ export class BinaryTree<K = any, V = any, R = any>
1728
2136
 
1729
2137
 
1730
2138
 
2139
+
2140
+
2141
+
2142
+
2143
+
2144
+
2145
+
2146
+
2147
+
2148
+
2149
+
2150
+
2151
+
2152
+
2153
+
2154
+
2155
+
2156
+
2157
+
2158
+
2159
+
2160
+
2161
+
2162
+
1731
2163
  * @example
1732
2164
  * // BinaryTree level-order traversal
1733
2165
  * const tree = new BinaryTree([
@@ -1851,6 +2283,30 @@ export class BinaryTree<K = any, V = any, R = any>
1851
2283
 
1852
2284
 
1853
2285
 
2286
+
2287
+
2288
+
2289
+
2290
+
2291
+
2292
+
2293
+
2294
+
2295
+
2296
+
2297
+
2298
+
2299
+
2300
+
2301
+
2302
+
2303
+
2304
+
2305
+
2306
+
2307
+
2308
+
2309
+
1854
2310
  * @example
1855
2311
  * // Get leaf nodes
1856
2312
  * const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
@@ -1927,6 +2383,30 @@ export class BinaryTree<K = any, V = any, R = any>
1927
2383
 
1928
2384
 
1929
2385
 
2386
+
2387
+
2388
+
2389
+
2390
+
2391
+
2392
+
2393
+
2394
+
2395
+
2396
+
2397
+
2398
+
2399
+
2400
+
2401
+
2402
+
2403
+
2404
+
2405
+
2406
+
2407
+
2408
+
2409
+
1930
2410
  * @example
1931
2411
  * // Level-order grouping
1932
2412
  * const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
@@ -2021,6 +2501,30 @@ export class BinaryTree<K = any, V = any, R = any>
2021
2501
 
2022
2502
 
2023
2503
 
2504
+
2505
+
2506
+
2507
+
2508
+
2509
+
2510
+
2511
+
2512
+
2513
+
2514
+
2515
+
2516
+
2517
+
2518
+
2519
+
2520
+
2521
+
2522
+
2523
+
2524
+
2525
+
2526
+
2527
+
2024
2528
  * @example
2025
2529
  * // Morris traversal (O(1) space)
2026
2530
  * const tree = new BinaryTree<number>([1, 2, 3]);
@@ -2159,6 +2663,30 @@ export class BinaryTree<K = any, V = any, R = any>
2159
2663
 
2160
2664
 
2161
2665
 
2666
+
2667
+
2668
+
2669
+
2670
+
2671
+
2672
+
2673
+
2674
+
2675
+
2676
+
2677
+
2678
+
2679
+
2680
+
2681
+
2682
+
2683
+
2684
+
2685
+
2686
+
2687
+
2688
+
2689
+
2162
2690
  * @example
2163
2691
  * // Deep copy
2164
2692
  * const tree = new BinaryTree<number>([1, 2, 3]);
@@ -2188,6 +2716,30 @@ export class BinaryTree<K = any, V = any, R = any>
2188
2716
 
2189
2717
 
2190
2718
 
2719
+
2720
+
2721
+
2722
+
2723
+
2724
+
2725
+
2726
+
2727
+
2728
+
2729
+
2730
+
2731
+
2732
+
2733
+
2734
+
2735
+
2736
+
2737
+
2738
+
2739
+
2740
+
2741
+
2742
+
2191
2743
  * @example
2192
2744
  * // Filter nodes by condition
2193
2745
  * const tree = new BinaryTree<number>([1, 2, 3, 4]);
@@ -2221,6 +2773,30 @@ export class BinaryTree<K = any, V = any, R = any>
2221
2773
 
2222
2774
 
2223
2775
 
2776
+
2777
+
2778
+
2779
+
2780
+
2781
+
2782
+
2783
+
2784
+
2785
+
2786
+
2787
+
2788
+
2789
+
2790
+
2791
+
2792
+
2793
+
2794
+
2795
+
2796
+
2797
+
2798
+
2799
+
2224
2800
  * @example
2225
2801
  * // Transform to new tree
2226
2802
  * const tree = new BinaryTree<number, number>([[1, 10], [2, 20]]);
@@ -2287,6 +2863,30 @@ export class BinaryTree<K = any, V = any, R = any>
2287
2863
 
2288
2864
 
2289
2865
 
2866
+
2867
+
2868
+
2869
+
2870
+
2871
+
2872
+
2873
+
2874
+
2875
+
2876
+
2877
+
2878
+
2879
+
2880
+
2881
+
2882
+
2883
+
2884
+
2885
+
2886
+
2887
+
2888
+
2889
+
2290
2890
  * @example
2291
2891
  * // Display tree
2292
2892
  * const tree = new BinaryTree<number>([1, 2, 3]);
@@ -2877,7 +3477,7 @@ export class BinaryTree<K = any, V = any, R = any>
2877
3477
  * @param p - The item to check.
2878
3478
  * @returns True if it's a function.
2879
3479
  */
2880
- protected _isPredicate(p: any): p is NodePredicate<BinaryTreeNode<K, V>> {
3480
+ protected _isPredicate(p: unknown): p is NodePredicate<BinaryTreeNode<K, V>> {
2881
3481
  return typeof p === 'function';
2882
3482
  }
2883
3483