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
@@ -7,7 +7,7 @@
7
7
  */
8
8
 
9
9
  import type { Comparator, TreeMultiMapOptions } from '../../types';
10
- import { ERR, Range } from '../../common';
10
+ import { ERR, raise, Range } from '../../common';
11
11
  import { RedBlackTree, RedBlackTreeNode } from './red-black-tree';
12
12
  import { TreeSet } from './tree-set';
13
13
 
@@ -49,7 +49,7 @@ export class TreeMultiMap<K = any, V = any, R = any> implements Iterable<[K, V[]
49
49
  const comparator = options.comparator ?? TreeSet.createDefaultComparator<K>();
50
50
  this.#isDefaultComparator = options.comparator === undefined;
51
51
  const toEntryFn = options.toEntryFn;
52
- this.#core = new RedBlackTree<K, V[], R>([], { ...options, comparator, isMapMode: options.isMapMode });
52
+ this.#core = new RedBlackTree<K, V[], R>([], { ...options, comparator, isMapMode: options.isMapMode, enableOrderStatistic: options.enableOrderStatistic });
53
53
 
54
54
  for (const x of keysNodesEntriesOrRaws) {
55
55
  if (x === null || x === undefined) continue;
@@ -91,15 +91,15 @@ export class TreeMultiMap<K = any, V = any, R = any> implements Iterable<[K, V[]
91
91
  // reuse TreeSet strict validation (same policy)
92
92
  // NOTE: TreeSet._validateKey is private, so we replicate the checks.
93
93
  if (typeof key === 'number') {
94
- if (Number.isNaN(key)) throw new TypeError(ERR.invalidNaN('TreeMultiMap'));
94
+ if (Number.isNaN(key)) raise(TypeError, ERR.invalidNaN('TreeMultiMap'));
95
95
  return;
96
96
  }
97
97
  if (typeof key === 'string') return;
98
98
  if (key instanceof Date) {
99
- if (Number.isNaN(key.getTime())) throw new TypeError(ERR.invalidDate('TreeMultiMap'));
99
+ if (Number.isNaN(key.getTime())) raise(TypeError, ERR.invalidDate('TreeMultiMap'));
100
100
  return;
101
101
  }
102
- throw new TypeError(ERR.comparatorRequired('TreeMultiMap'));
102
+ raise(TypeError, ERR.comparatorRequired('TreeMultiMap'));
103
103
  }
104
104
 
105
105
  /**
@@ -240,6 +240,21 @@ export class TreeMultiMap<K = any, V = any, R = any> implements Iterable<[K, V[]
240
240
 
241
241
 
242
242
 
243
+
244
+
245
+
246
+
247
+
248
+
249
+
250
+
251
+
252
+
253
+
254
+
255
+
256
+
257
+
243
258
 
244
259
 
245
260
 
@@ -394,6 +409,21 @@ export class TreeMultiMap<K = any, V = any, R = any> implements Iterable<[K, V[]
394
409
 
395
410
 
396
411
 
412
+
413
+
414
+
415
+
416
+
417
+
418
+
419
+
420
+
421
+
422
+
423
+
424
+
425
+
426
+
397
427
 
398
428
 
399
429
 
@@ -443,6 +473,9 @@ export class TreeMultiMap<K = any, V = any, R = any> implements Iterable<[K, V[]
443
473
 
444
474
 
445
475
 
476
+
477
+
478
+
446
479
 
447
480
 
448
481
 
@@ -481,6 +514,9 @@ export class TreeMultiMap<K = any, V = any, R = any> implements Iterable<[K, V[]
481
514
 
482
515
 
483
516
 
517
+
518
+
519
+
484
520
 
485
521
 
486
522
 
@@ -661,6 +697,24 @@ export class TreeMultiMap<K = any, V = any, R = any> implements Iterable<[K, V[]
661
697
 
662
698
 
663
699
 
700
+
701
+
702
+
703
+
704
+
705
+
706
+
707
+
708
+
709
+
710
+
711
+
712
+
713
+
714
+
715
+
716
+
717
+
664
718
 
665
719
 
666
720
 
@@ -854,6 +908,24 @@ export class TreeMultiMap<K = any, V = any, R = any> implements Iterable<[K, V[]
854
908
 
855
909
 
856
910
 
911
+
912
+
913
+
914
+
915
+
916
+
917
+
918
+
919
+
920
+
921
+
922
+
923
+
924
+
925
+
926
+
927
+
928
+
857
929
 
858
930
 
859
931
 
@@ -1009,6 +1081,21 @@ export class TreeMultiMap<K = any, V = any, R = any> implements Iterable<[K, V[]
1009
1081
 
1010
1082
 
1011
1083
 
1084
+
1085
+
1086
+
1087
+
1088
+
1089
+
1090
+
1091
+
1092
+
1093
+
1094
+
1095
+
1096
+
1097
+
1098
+
1012
1099
 
1013
1100
 
1014
1101
 
@@ -1203,6 +1290,24 @@ export class TreeMultiMap<K = any, V = any, R = any> implements Iterable<[K, V[]
1203
1290
 
1204
1291
 
1205
1292
 
1293
+
1294
+
1295
+
1296
+
1297
+
1298
+
1299
+
1300
+
1301
+
1302
+
1303
+
1304
+
1305
+
1306
+
1307
+
1308
+
1309
+
1310
+
1206
1311
 
1207
1312
 
1208
1313
 
@@ -1416,6 +1521,24 @@ export class TreeMultiMap<K = any, V = any, R = any> implements Iterable<[K, V[]
1416
1521
 
1417
1522
 
1418
1523
 
1524
+
1525
+
1526
+
1527
+
1528
+
1529
+
1530
+
1531
+
1532
+
1533
+
1534
+
1535
+
1536
+
1537
+
1538
+
1539
+
1540
+
1541
+
1419
1542
 
1420
1543
 
1421
1544
 
@@ -1470,6 +1593,9 @@ export class TreeMultiMap<K = any, V = any, R = any> implements Iterable<[K, V[]
1470
1593
 
1471
1594
 
1472
1595
 
1596
+
1597
+
1598
+
1473
1599
 
1474
1600
 
1475
1601
 
@@ -1509,6 +1635,9 @@ export class TreeMultiMap<K = any, V = any, R = any> implements Iterable<[K, V[]
1509
1635
 
1510
1636
 
1511
1637
 
1638
+
1639
+
1640
+
1512
1641
 
1513
1642
 
1514
1643
 
@@ -1553,6 +1682,9 @@ export class TreeMultiMap<K = any, V = any, R = any> implements Iterable<[K, V[]
1553
1682
 
1554
1683
 
1555
1684
 
1685
+
1686
+
1687
+
1556
1688
 
1557
1689
 
1558
1690
 
@@ -1723,6 +1855,21 @@ export class TreeMultiMap<K = any, V = any, R = any> implements Iterable<[K, V[]
1723
1855
 
1724
1856
 
1725
1857
 
1858
+
1859
+
1860
+
1861
+
1862
+
1863
+
1864
+
1865
+
1866
+
1867
+
1868
+
1869
+
1870
+
1871
+
1872
+
1726
1873
 
1727
1874
 
1728
1875
 
@@ -1880,6 +2027,21 @@ export class TreeMultiMap<K = any, V = any, R = any> implements Iterable<[K, V[]
1880
2027
 
1881
2028
 
1882
2029
 
2030
+
2031
+
2032
+
2033
+
2034
+
2035
+
2036
+
2037
+
2038
+
2039
+
2040
+
2041
+
2042
+
2043
+
2044
+
1883
2045
 
1884
2046
 
1885
2047
 
@@ -1931,6 +2093,9 @@ export class TreeMultiMap<K = any, V = any, R = any> implements Iterable<[K, V[]
1931
2093
 
1932
2094
 
1933
2095
 
2096
+
2097
+
2098
+
1934
2099
 
1935
2100
 
1936
2101
 
@@ -1970,6 +2135,9 @@ export class TreeMultiMap<K = any, V = any, R = any> implements Iterable<[K, V[]
1970
2135
 
1971
2136
 
1972
2137
 
2138
+
2139
+
2140
+
1973
2141
 
1974
2142
 
1975
2143
 
@@ -2009,6 +2177,9 @@ export class TreeMultiMap<K = any, V = any, R = any> implements Iterable<[K, V[]
2009
2177
 
2010
2178
 
2011
2179
 
2180
+
2181
+
2182
+
2012
2183
 
2013
2184
 
2014
2185
 
@@ -2081,6 +2252,12 @@ export class TreeMultiMap<K = any, V = any, R = any> implements Iterable<[K, V[]
2081
2252
 
2082
2253
 
2083
2254
 
2255
+
2256
+
2257
+
2258
+
2259
+
2260
+
2084
2261
 
2085
2262
 
2086
2263
 
@@ -2154,6 +2331,12 @@ export class TreeMultiMap<K = any, V = any, R = any> implements Iterable<[K, V[]
2154
2331
 
2155
2332
 
2156
2333
 
2334
+
2335
+
2336
+
2337
+
2338
+
2339
+
2157
2340
 
2158
2341
 
2159
2342
 
@@ -2198,6 +2381,9 @@ export class TreeMultiMap<K = any, V = any, R = any> implements Iterable<[K, V[]
2198
2381
 
2199
2382
 
2200
2383
 
2384
+
2385
+
2386
+
2201
2387
 
2202
2388
 
2203
2389
 
@@ -2241,6 +2427,9 @@ export class TreeMultiMap<K = any, V = any, R = any> implements Iterable<[K, V[]
2241
2427
 
2242
2428
 
2243
2429
 
2430
+
2431
+
2432
+
2244
2433
 
2245
2434
 
2246
2435
 
@@ -2393,6 +2582,21 @@ export class TreeMultiMap<K = any, V = any, R = any> implements Iterable<[K, V[]
2393
2582
 
2394
2583
 
2395
2584
 
2585
+
2586
+
2587
+
2588
+
2589
+
2590
+
2591
+
2592
+
2593
+
2594
+
2595
+
2596
+
2597
+
2598
+
2599
+
2396
2600
 
2397
2601
 
2398
2602
 
@@ -2558,6 +2762,21 @@ export class TreeMultiMap<K = any, V = any, R = any> implements Iterable<[K, V[]
2558
2762
 
2559
2763
 
2560
2764
 
2765
+
2766
+
2767
+
2768
+
2769
+
2770
+
2771
+
2772
+
2773
+
2774
+
2775
+
2776
+
2777
+
2778
+
2779
+
2561
2780
 
2562
2781
 
2563
2782
 
@@ -2694,6 +2913,18 @@ export class TreeMultiMap<K = any, V = any, R = any> implements Iterable<[K, V[]
2694
2913
 
2695
2914
 
2696
2915
 
2916
+
2917
+
2918
+
2919
+
2920
+
2921
+
2922
+
2923
+
2924
+
2925
+
2926
+
2927
+
2697
2928
 
2698
2929
 
2699
2930
 
@@ -2826,6 +3057,18 @@ export class TreeMultiMap<K = any, V = any, R = any> implements Iterable<[K, V[]
2826
3057
 
2827
3058
 
2828
3059
 
3060
+
3061
+
3062
+
3063
+
3064
+
3065
+
3066
+
3067
+
3068
+
3069
+
3070
+
3071
+
2829
3072
 
2830
3073
 
2831
3074
 
@@ -2986,6 +3229,21 @@ export class TreeMultiMap<K = any, V = any, R = any> implements Iterable<[K, V[]
2986
3229
 
2987
3230
 
2988
3231
 
3232
+
3233
+
3234
+
3235
+
3236
+
3237
+
3238
+
3239
+
3240
+
3241
+
3242
+
3243
+
3244
+
3245
+
3246
+
2989
3247
 
2990
3248
 
2991
3249
 
@@ -3142,6 +3400,21 @@ export class TreeMultiMap<K = any, V = any, R = any> implements Iterable<[K, V[]
3142
3400
 
3143
3401
 
3144
3402
 
3403
+
3404
+
3405
+
3406
+
3407
+
3408
+
3409
+
3410
+
3411
+
3412
+
3413
+
3414
+
3415
+
3416
+
3417
+
3145
3418
 
3146
3419
 
3147
3420
 
@@ -3303,6 +3576,21 @@ export class TreeMultiMap<K = any, V = any, R = any> implements Iterable<[K, V[]
3303
3576
 
3304
3577
 
3305
3578
 
3579
+
3580
+
3581
+
3582
+
3583
+
3584
+
3585
+
3586
+
3587
+
3588
+
3589
+
3590
+
3591
+
3592
+
3593
+
3306
3594
 
3307
3595
 
3308
3596
 
@@ -3466,6 +3754,21 @@ export class TreeMultiMap<K = any, V = any, R = any> implements Iterable<[K, V[]
3466
3754
 
3467
3755
 
3468
3756
 
3757
+
3758
+
3759
+
3760
+
3761
+
3762
+
3763
+
3764
+
3765
+
3766
+
3767
+
3768
+
3769
+
3770
+
3771
+
3469
3772
 
3470
3773
 
3471
3774
 
@@ -3629,6 +3932,21 @@ export class TreeMultiMap<K = any, V = any, R = any> implements Iterable<[K, V[]
3629
3932
 
3630
3933
 
3631
3934
 
3935
+
3936
+
3937
+
3938
+
3939
+
3940
+
3941
+
3942
+
3943
+
3944
+
3945
+
3946
+
3947
+
3948
+
3949
+
3632
3950
 
3633
3951
 
3634
3952
 
@@ -3783,6 +4101,21 @@ export class TreeMultiMap<K = any, V = any, R = any> implements Iterable<[K, V[]
3783
4101
 
3784
4102
 
3785
4103
 
4104
+
4105
+
4106
+
4107
+
4108
+
4109
+
4110
+
4111
+
4112
+
4113
+
4114
+
4115
+
4116
+
4117
+
4118
+
3786
4119
 
3787
4120
 
3788
4121
 
@@ -3918,6 +4251,18 @@ export class TreeMultiMap<K = any, V = any, R = any> implements Iterable<[K, V[]
3918
4251
 
3919
4252
 
3920
4253
 
4254
+
4255
+
4256
+
4257
+
4258
+
4259
+
4260
+
4261
+
4262
+
4263
+
4264
+
4265
+
3921
4266
 
3922
4267
 
3923
4268
 
@@ -4090,6 +4435,82 @@ export class TreeMultiMap<K = any, V = any, R = any> implements Iterable<[K, V[]
4090
4435
 
4091
4436
 
4092
4437
 
4438
+
4439
+
4440
+
4441
+
4442
+
4443
+
4444
+
4445
+
4446
+
4447
+
4448
+
4449
+
4450
+
4451
+
4452
+
4453
+
4454
+ * @example
4455
+ * // Order-statistic on BST
4456
+ * const tree = new TreeMultiMap<number>([30, 10, 50, 20, 40], { enableOrderStatistic: true });
4457
+ * console.log(tree.getByRank(0)); // 10;
4458
+ * console.log(tree.getByRank(4)); // 50;
4459
+ * console.log(tree.getRank(30)); // 2;
4460
+ */
4461
+ // ─── Order-Statistic Methods ───────────────────────────
4462
+
4463
+ getByRank(k: number): [K, V[]] | undefined {
4464
+ const key = this.#core.getByRank(k);
4465
+ if (key === undefined) return undefined;
4466
+ return [key, this.#core.get(key) ?? []];
4467
+ }
4468
+
4469
+ /**
4470
+ * Get the rank of a key in sorted order
4471
+ * @example
4472
+ * // Get the rank of a key in sorted order
4473
+ * const tree = new TreeMultiMap<number>(
4474
+ * [10, 20, 30, 40, 50],
4475
+ * { enableOrderStatistic: true }
4476
+ * );
4477
+ * console.log(tree.getRank(10)); // 0; // smallest → rank 0
4478
+ * console.log(tree.getRank(30)); // 2; // 2 elements before 30 in tree order
4479
+ * console.log(tree.getRank(50)); // 4; // largest → rank 4
4480
+ * console.log(tree.getRank(25)); // 2;
4481
+ */
4482
+ getRank(key: K): number {
4483
+ return this.#core.getRank(key);
4484
+ }
4485
+
4486
+ /**
4487
+ * Get elements by rank range
4488
+
4489
+ * @example
4490
+ * // Pagination with rangeByRank
4491
+ * const tree = new TreeMultiMap<number>(
4492
+ * [10, 20, 30, 40, 50, 60, 70, 80, 90],
4493
+ * { enableOrderStatistic: true }
4494
+ * );
4495
+ * const pageSize = 3;
4496
+ *
4497
+ * // Page 1
4498
+ * console.log(tree.rangeByRank(0, pageSize - 1)); // [10, 20, 30];
4499
+ * // Page 2
4500
+ * console.log(tree.rangeByRank(pageSize, 2 * pageSize - 1)); // [40, 50, 60];
4501
+ * // Page 3
4502
+ * console.log(tree.rangeByRank(2 * pageSize, 3 * pageSize - 1)); // [70, 80, 90];
4503
+ */
4504
+ rangeByRank(start: number, end: number): Array<[K, V[]]> {
4505
+ const keys = this.#core.rangeByRank(start, end);
4506
+ return keys
4507
+ .filter((k): k is K => k !== undefined)
4508
+ .map(k => [k, this.#core.get(k) ?? []] as [K, V[]]);
4509
+ }
4510
+
4511
+ /**
4512
+ * Deep copy
4513
+
4093
4514
 
4094
4515
 
4095
4516