binary-tree-typed 2.5.1 → 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 (73) hide show
  1. package/dist/cjs/index.cjs +146 -52
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +145 -51
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +146 -53
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +145 -52
  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/binary-tree/avl-tree.d.ts +36 -0
  12. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +42 -0
  13. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +77 -2
  14. package/dist/types/data-structures/binary-tree/bst.d.ts +171 -0
  15. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +57 -0
  16. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +18 -0
  17. package/dist/types/data-structures/binary-tree/tree-map.d.ts +409 -0
  18. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +411 -6
  19. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +339 -6
  20. package/dist/types/data-structures/binary-tree/tree-set.d.ts +391 -0
  21. package/dist/types/data-structures/graph/directed-graph.d.ts +30 -0
  22. package/dist/types/data-structures/graph/undirected-graph.d.ts +27 -0
  23. package/dist/types/data-structures/hash/hash-map.d.ts +33 -0
  24. package/dist/types/data-structures/heap/heap.d.ts +42 -0
  25. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +51 -0
  26. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +45 -0
  27. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +54 -0
  28. package/dist/types/data-structures/matrix/matrix.d.ts +24 -0
  29. package/dist/types/data-structures/queue/deque.d.ts +45 -0
  30. package/dist/types/data-structures/queue/queue.d.ts +36 -0
  31. package/dist/types/data-structures/stack/stack.d.ts +30 -0
  32. package/dist/types/data-structures/trie/trie.d.ts +36 -0
  33. package/dist/types/types/data-structures/binary-tree/bst.d.ts +1 -0
  34. package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +5 -0
  35. package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +4 -0
  36. package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +4 -0
  37. package/dist/umd/binary-tree-typed.js +143 -50
  38. package/dist/umd/binary-tree-typed.js.map +1 -1
  39. package/dist/umd/binary-tree-typed.min.js +3 -3
  40. package/dist/umd/binary-tree-typed.min.js.map +1 -1
  41. package/package.json +2 -2
  42. package/src/common/error.ts +19 -1
  43. package/src/common/index.ts +1 -1
  44. package/src/data-structures/base/iterable-element-base.ts +3 -2
  45. package/src/data-structures/binary-tree/avl-tree.ts +47 -0
  46. package/src/data-structures/binary-tree/binary-indexed-tree.ts +46 -4
  47. package/src/data-structures/binary-tree/binary-tree.ts +79 -4
  48. package/src/data-structures/binary-tree/bst.ts +441 -6
  49. package/src/data-structures/binary-tree/red-black-tree.ts +73 -0
  50. package/src/data-structures/binary-tree/segment-tree.ts +18 -0
  51. package/src/data-structures/binary-tree/tree-map.ts +434 -9
  52. package/src/data-structures/binary-tree/tree-multi-map.ts +426 -5
  53. package/src/data-structures/binary-tree/tree-multi-set.ts +350 -6
  54. package/src/data-structures/binary-tree/tree-set.ts +410 -8
  55. package/src/data-structures/graph/abstract-graph.ts +2 -2
  56. package/src/data-structures/graph/directed-graph.ts +30 -0
  57. package/src/data-structures/graph/undirected-graph.ts +27 -0
  58. package/src/data-structures/hash/hash-map.ts +35 -4
  59. package/src/data-structures/heap/heap.ts +46 -4
  60. package/src/data-structures/heap/max-heap.ts +2 -2
  61. package/src/data-structures/linked-list/doubly-linked-list.ts +51 -0
  62. package/src/data-structures/linked-list/singly-linked-list.ts +45 -0
  63. package/src/data-structures/linked-list/skip-linked-list.ts +59 -5
  64. package/src/data-structures/matrix/matrix.ts +33 -9
  65. package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
  66. package/src/data-structures/queue/deque.ts +45 -0
  67. package/src/data-structures/queue/queue.ts +36 -0
  68. package/src/data-structures/stack/stack.ts +30 -0
  69. package/src/data-structures/trie/trie.ts +38 -2
  70. package/src/types/data-structures/binary-tree/bst.ts +1 -0
  71. package/src/types/data-structures/binary-tree/tree-map.ts +6 -0
  72. package/src/types/data-structures/binary-tree/tree-multi-set.ts +5 -0
  73. package/src/types/data-structures/binary-tree/tree-set.ts +5 -0
@@ -9,7 +9,7 @@
9
9
  */
10
10
 
11
11
  import type { Comparator, TreeMultiSetOptions } from '../../types';
12
- import { ERR } from '../../common';
12
+ import { ERR, raise } from '../../common';
13
13
  import { RedBlackTree } from './red-black-tree';
14
14
  import { TreeSet } from './tree-set';
15
15
 
@@ -35,7 +35,7 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
35
35
  const toElementFn = options.toElementFn;
36
36
  const comparator = options.comparator ?? TreeSet.createDefaultComparator<K>();
37
37
  this.#isDefaultComparator = options.comparator === undefined;
38
- this.#core = new RedBlackTree<K, number>([], { comparator, isMapMode: options.isMapMode });
38
+ this.#core = new RedBlackTree<K, number>([], { comparator, isMapMode: options.isMapMode, enableOrderStatistic: options.enableOrderStatistic });
39
39
 
40
40
  for (const item of elements) {
41
41
  const k = toElementFn ? toElementFn(item as R) : (item as K);
@@ -51,18 +51,18 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
51
51
  if (!this.#isDefaultComparator) return;
52
52
 
53
53
  if (typeof key === 'number') {
54
- if (Number.isNaN(key)) throw new TypeError(ERR.invalidNaN('TreeMultiSet'));
54
+ if (Number.isNaN(key)) raise(TypeError, ERR.invalidNaN('TreeMultiSet'));
55
55
  return;
56
56
  }
57
57
 
58
58
  if (typeof key === 'string') return;
59
59
 
60
60
  if (key instanceof Date) {
61
- if (Number.isNaN(key.getTime())) throw new TypeError(ERR.invalidDate('TreeMultiSet'));
61
+ if (Number.isNaN(key.getTime())) raise(TypeError, ERR.invalidDate('TreeMultiSet'));
62
62
  return;
63
63
  }
64
64
 
65
- throw new TypeError(ERR.comparatorRequired('TreeMultiSet'));
65
+ raise(TypeError, ERR.comparatorRequired('TreeMultiSet'));
66
66
  }
67
67
 
68
68
  /**
@@ -70,7 +70,7 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
70
70
  * @remarks Time O(1), Space O(1)
71
71
  */
72
72
  private _validateCount(n: number): void {
73
- if (!Number.isSafeInteger(n) || n < 0) throw new RangeError(ERR.invalidArgument('count must be a safe integer >= 0.', 'TreeMultiSet'));
73
+ if (!Number.isSafeInteger(n) || n < 0) raise(RangeError, ERR.invalidArgument('count must be a safe integer >= 0.', 'TreeMultiSet'));
74
74
  }
75
75
 
76
76
  /**
@@ -103,6 +103,9 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
103
103
 
104
104
 
105
105
 
106
+
107
+
108
+
106
109
 
107
110
 
108
111
 
@@ -248,6 +251,21 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
248
251
 
249
252
 
250
253
 
254
+
255
+
256
+
257
+
258
+
259
+
260
+
261
+
262
+
263
+
264
+
265
+
266
+
267
+
268
+
251
269
 
252
270
 
253
271
 
@@ -405,6 +423,21 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
405
423
 
406
424
 
407
425
 
426
+
427
+
428
+
429
+
430
+
431
+
432
+
433
+
434
+
435
+
436
+
437
+
438
+
439
+
440
+
408
441
 
409
442
 
410
443
 
@@ -455,6 +488,9 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
455
488
 
456
489
 
457
490
 
491
+
492
+
493
+
458
494
 
459
495
 
460
496
 
@@ -595,6 +631,21 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
595
631
 
596
632
 
597
633
 
634
+
635
+
636
+
637
+
638
+
639
+
640
+
641
+
642
+
643
+
644
+
645
+
646
+
647
+
648
+
598
649
 
599
650
 
600
651
 
@@ -655,6 +706,9 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
655
706
 
656
707
 
657
708
 
709
+
710
+
711
+
658
712
 
659
713
 
660
714
 
@@ -816,6 +870,21 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
816
870
 
817
871
 
818
872
 
873
+
874
+
875
+
876
+
877
+
878
+
879
+
880
+
881
+
882
+
883
+
884
+
885
+
886
+
887
+
819
888
 
820
889
 
821
890
 
@@ -880,6 +949,9 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
880
949
 
881
950
 
882
951
 
952
+
953
+
954
+
883
955
 
884
956
 
885
957
 
@@ -922,6 +994,9 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
922
994
 
923
995
 
924
996
 
997
+
998
+
999
+
925
1000
 
926
1001
 
927
1002
 
@@ -1067,6 +1142,21 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
1067
1142
 
1068
1143
 
1069
1144
 
1145
+
1146
+
1147
+
1148
+
1149
+
1150
+
1151
+
1152
+
1153
+
1154
+
1155
+
1156
+
1157
+
1158
+
1159
+
1070
1160
 
1071
1161
 
1072
1162
 
@@ -1235,6 +1325,21 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
1235
1325
 
1236
1326
 
1237
1327
 
1328
+
1329
+
1330
+
1331
+
1332
+
1333
+
1334
+
1335
+
1336
+
1337
+
1338
+
1339
+
1340
+
1341
+
1342
+
1238
1343
 
1239
1344
 
1240
1345
 
@@ -1284,6 +1389,9 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
1284
1389
 
1285
1390
 
1286
1391
 
1392
+
1393
+
1394
+
1287
1395
 
1288
1396
 
1289
1397
 
@@ -1321,6 +1429,9 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
1321
1429
 
1322
1430
 
1323
1431
 
1432
+
1433
+
1434
+
1324
1435
 
1325
1436
 
1326
1437
 
@@ -1477,6 +1588,21 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
1477
1588
 
1478
1589
 
1479
1590
 
1591
+
1592
+
1593
+
1594
+
1595
+
1596
+
1597
+
1598
+
1599
+
1600
+
1601
+
1602
+
1603
+
1604
+
1605
+
1480
1606
 
1481
1607
 
1482
1608
 
@@ -1530,6 +1656,9 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
1530
1656
 
1531
1657
 
1532
1658
 
1659
+
1660
+
1661
+
1533
1662
 
1534
1663
 
1535
1664
 
@@ -1568,6 +1697,9 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
1568
1697
 
1569
1698
 
1570
1699
 
1700
+
1701
+
1702
+
1571
1703
 
1572
1704
 
1573
1705
 
@@ -1606,6 +1738,9 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
1606
1738
 
1607
1739
 
1608
1740
 
1741
+
1742
+
1743
+
1609
1744
 
1610
1745
 
1611
1746
 
@@ -1648,6 +1783,9 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
1648
1783
 
1649
1784
 
1650
1785
 
1786
+
1787
+
1788
+
1651
1789
 
1652
1790
 
1653
1791
 
@@ -1770,6 +1908,18 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
1770
1908
 
1771
1909
 
1772
1910
 
1911
+
1912
+
1913
+
1914
+
1915
+
1916
+
1917
+
1918
+
1919
+
1920
+
1921
+
1922
+
1773
1923
 
1774
1924
 
1775
1925
 
@@ -1900,6 +2050,18 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
1900
2050
 
1901
2051
 
1902
2052
 
2053
+
2054
+
2055
+
2056
+
2057
+
2058
+
2059
+
2060
+
2061
+
2062
+
2063
+
2064
+
1903
2065
 
1904
2066
 
1905
2067
 
@@ -2030,6 +2192,18 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
2030
2192
 
2031
2193
 
2032
2194
 
2195
+
2196
+
2197
+
2198
+
2199
+
2200
+
2201
+
2202
+
2203
+
2204
+
2205
+
2206
+
2033
2207
 
2034
2208
 
2035
2209
 
@@ -2159,6 +2333,18 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
2159
2333
 
2160
2334
 
2161
2335
 
2336
+
2337
+
2338
+
2339
+
2340
+
2341
+
2342
+
2343
+
2344
+
2345
+
2346
+
2347
+
2162
2348
 
2163
2349
 
2164
2350
 
@@ -2317,6 +2503,21 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
2317
2503
 
2318
2504
 
2319
2505
 
2506
+
2507
+
2508
+
2509
+
2510
+
2511
+
2512
+
2513
+
2514
+
2515
+
2516
+
2517
+
2518
+
2519
+
2520
+
2320
2521
 
2321
2522
 
2322
2523
 
@@ -2479,6 +2680,21 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
2479
2680
 
2480
2681
 
2481
2682
 
2683
+
2684
+
2685
+
2686
+
2687
+
2688
+
2689
+
2690
+
2691
+
2692
+
2693
+
2694
+
2695
+
2696
+
2697
+
2482
2698
 
2483
2699
 
2484
2700
 
@@ -2648,6 +2864,21 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
2648
2864
 
2649
2865
 
2650
2866
 
2867
+
2868
+
2869
+
2870
+
2871
+
2872
+
2873
+
2874
+
2875
+
2876
+
2877
+
2878
+
2879
+
2880
+
2881
+
2651
2882
 
2652
2883
 
2653
2884
 
@@ -2812,6 +3043,21 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
2812
3043
 
2813
3044
 
2814
3045
 
3046
+
3047
+
3048
+
3049
+
3050
+
3051
+
3052
+
3053
+
3054
+
3055
+
3056
+
3057
+
3058
+
3059
+
3060
+
2815
3061
 
2816
3062
 
2817
3063
 
@@ -2995,6 +3241,77 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
2995
3241
 
2996
3242
 
2997
3243
 
3244
+
3245
+
3246
+
3247
+
3248
+
3249
+
3250
+
3251
+
3252
+
3253
+
3254
+
3255
+
3256
+
3257
+
3258
+
3259
+
3260
+ * @example
3261
+ * // Order-statistic on BST
3262
+ * const tree = new TreeMultiSet<number>([30, 10, 50, 20, 40], { enableOrderStatistic: true });
3263
+ * console.log(tree.getByRank(0)); // 10;
3264
+ * console.log(tree.getByRank(4)); // 50;
3265
+ * console.log(tree.getRank(30)); // 2;
3266
+ */
3267
+ // ─── Order-Statistic Methods ───────────────────────────
3268
+
3269
+ getByRank(k: number): K | undefined {
3270
+ return this.#core.getByRank(k);
3271
+ }
3272
+
3273
+ /**
3274
+ * Get the rank of a key in sorted order
3275
+ * @example
3276
+ * // Get the rank of a key in sorted order
3277
+ * const tree = new TreeMultiSet<number>(
3278
+ * [10, 20, 30, 40, 50],
3279
+ * { enableOrderStatistic: true }
3280
+ * );
3281
+ * console.log(tree.getRank(10)); // 0; // smallest → rank 0
3282
+ * console.log(tree.getRank(30)); // 2; // 2 elements before 30 in tree order
3283
+ * console.log(tree.getRank(50)); // 4; // largest → rank 4
3284
+ * console.log(tree.getRank(25)); // 2;
3285
+ */
3286
+ getRank(key: K): number {
3287
+ return this.#core.getRank(key);
3288
+ }
3289
+
3290
+ /**
3291
+ * Get elements by rank range
3292
+
3293
+ * @example
3294
+ * // Pagination with rangeByRank
3295
+ * const tree = new TreeMultiSet<number>(
3296
+ * [10, 20, 30, 40, 50, 60, 70, 80, 90],
3297
+ * { enableOrderStatistic: true }
3298
+ * );
3299
+ * const pageSize = 3;
3300
+ *
3301
+ * // Page 1
3302
+ * console.log(tree.rangeByRank(0, pageSize - 1)); // [10, 20, 30];
3303
+ * // Page 2
3304
+ * console.log(tree.rangeByRank(pageSize, 2 * pageSize - 1)); // [40, 50, 60];
3305
+ * // Page 3
3306
+ * console.log(tree.rangeByRank(2 * pageSize, 3 * pageSize - 1)); // [70, 80, 90];
3307
+ */
3308
+ rangeByRank(start: number, end: number): K[] {
3309
+ return this.#core.rangeByRank(start, end).filter((k): k is K => k !== undefined);
3310
+ }
3311
+
3312
+ /**
3313
+ * Deep copy
3314
+
2998
3315
 
2999
3316
 
3000
3317
 
@@ -3123,6 +3440,18 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
3123
3440
 
3124
3441
 
3125
3442
 
3443
+
3444
+
3445
+
3446
+
3447
+
3448
+
3449
+
3450
+
3451
+
3452
+
3453
+
3454
+
3126
3455
 
3127
3456
 
3128
3457
 
@@ -3284,6 +3613,21 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
3284
3613
 
3285
3614
 
3286
3615
 
3616
+
3617
+
3618
+
3619
+
3620
+
3621
+
3622
+
3623
+
3624
+
3625
+
3626
+
3627
+
3628
+
3629
+
3630
+
3287
3631
 
3288
3632
 
3289
3633