binary-tree-typed 2.5.1 → 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 (75) hide show
  1. package/dist/cjs/index.cjs +340 -107
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +339 -106
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +340 -108
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +339 -107
  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 +86 -2
  12. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +98 -0
  13. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +189 -13
  14. package/dist/types/data-structures/binary-tree/bst.d.ts +270 -3
  15. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +136 -8
  16. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +42 -0
  17. package/dist/types/data-structures/binary-tree/tree-map.d.ts +1089 -161
  18. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1243 -350
  19. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +980 -255
  20. package/dist/types/data-structures/binary-tree/tree-set.d.ts +1174 -284
  21. package/dist/types/data-structures/graph/directed-graph.d.ts +70 -0
  22. package/dist/types/data-structures/graph/undirected-graph.d.ts +63 -0
  23. package/dist/types/data-structures/hash/hash-map.d.ts +84 -6
  24. package/dist/types/data-structures/heap/heap.d.ts +140 -12
  25. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +126 -0
  26. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +106 -1
  27. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +126 -0
  28. package/dist/types/data-structures/matrix/matrix.d.ts +56 -0
  29. package/dist/types/data-structures/queue/deque.d.ts +127 -0
  30. package/dist/types/data-structures/queue/queue.d.ts +97 -0
  31. package/dist/types/data-structures/stack/stack.d.ts +72 -2
  32. package/dist/types/data-structures/trie/trie.d.ts +84 -0
  33. package/dist/types/interfaces/binary-tree.d.ts +2 -3
  34. package/dist/types/types/data-structures/binary-tree/bst.d.ts +1 -0
  35. package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +5 -0
  36. package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +4 -0
  37. package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +4 -0
  38. package/dist/umd/binary-tree-typed.js +337 -105
  39. package/dist/umd/binary-tree-typed.js.map +1 -1
  40. package/dist/umd/binary-tree-typed.min.js +5 -5
  41. package/dist/umd/binary-tree-typed.min.js.map +1 -1
  42. package/package.json +2 -2
  43. package/src/common/error.ts +19 -1
  44. package/src/common/index.ts +1 -1
  45. package/src/data-structures/base/iterable-element-base.ts +3 -2
  46. package/src/data-structures/binary-tree/avl-tree.ts +99 -5
  47. package/src/data-structures/binary-tree/binary-indexed-tree.ts +102 -4
  48. package/src/data-structures/binary-tree/binary-tree.ts +239 -78
  49. package/src/data-structures/binary-tree/bst.ts +542 -13
  50. package/src/data-structures/binary-tree/red-black-tree.ts +155 -15
  51. package/src/data-structures/binary-tree/segment-tree.ts +42 -0
  52. package/src/data-structures/binary-tree/tree-map.ts +1223 -261
  53. package/src/data-structures/binary-tree/tree-multi-map.ts +939 -30
  54. package/src/data-structures/binary-tree/tree-multi-set.ts +746 -10
  55. package/src/data-structures/binary-tree/tree-set.ts +1018 -99
  56. package/src/data-structures/graph/abstract-graph.ts +2 -2
  57. package/src/data-structures/graph/directed-graph.ts +71 -1
  58. package/src/data-structures/graph/undirected-graph.ts +64 -1
  59. package/src/data-structures/hash/hash-map.ts +102 -16
  60. package/src/data-structures/heap/heap.ts +153 -23
  61. package/src/data-structures/heap/max-heap.ts +2 -2
  62. package/src/data-structures/linked-list/doubly-linked-list.ts +139 -0
  63. package/src/data-structures/linked-list/singly-linked-list.ts +106 -1
  64. package/src/data-structures/linked-list/skip-linked-list.ts +131 -5
  65. package/src/data-structures/matrix/matrix.ts +65 -9
  66. package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
  67. package/src/data-structures/queue/deque.ts +130 -0
  68. package/src/data-structures/queue/queue.ts +109 -0
  69. package/src/data-structures/stack/stack.ts +75 -5
  70. package/src/data-structures/trie/trie.ts +86 -2
  71. package/src/interfaces/binary-tree.ts +1 -9
  72. package/src/types/data-structures/binary-tree/bst.ts +1 -0
  73. package/src/types/data-structures/binary-tree/tree-map.ts +6 -0
  74. package/src/types/data-structures/binary-tree/tree-multi-set.ts +5 -0
  75. package/src/types/data-structures/binary-tree/tree-set.ts +5 -0
@@ -9,7 +9,7 @@
9
9
 
10
10
  import type { Comparator } from '../../types';
11
11
  import type { TreeSetElementCallback, TreeSetOptions, TreeSetRangeOptions, TreeSetReduceCallback } from '../../types';
12
- import { ERR } from '../../common';
12
+ import { ERR, raise } from '../../common';
13
13
  import { RedBlackTree } from './red-black-tree';
14
14
 
15
15
  /**
@@ -50,7 +50,7 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
50
50
  this.#isDefaultComparator = options.comparator === undefined;
51
51
 
52
52
  // RedBlackTree expects an iterable of keys/entries/nodes/raws; for TreeSet we only accept keys.
53
- this.#core = new RedBlackTree<K, undefined>([], { comparator, isMapMode: options.isMapMode });
53
+ this.#core = new RedBlackTree<K, undefined>([], { comparator, isMapMode: options.isMapMode, enableOrderStatistic: options.enableOrderStatistic });
54
54
 
55
55
  for (const item of elements) {
56
56
  const k = toElementFn ? toElementFn(item as R) : item as K;
@@ -73,7 +73,7 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
73
73
  // numbers
74
74
  if (typeof a === 'number' && typeof b === 'number') {
75
75
  /* istanbul ignore next -- _validateKey prevents NaN from entering the tree */
76
- if (Number.isNaN(a) || Number.isNaN(b)) throw new TypeError(ERR.invalidNaN('TreeSet'));
76
+ if (Number.isNaN(a) || Number.isNaN(b)) raise(TypeError, ERR.invalidNaN('TreeSet'));
77
77
  const aa = Object.is(a, -0) ? 0 : a;
78
78
  const bb = Object.is(b, -0) ? 0 : b;
79
79
  return aa > bb ? 1 : aa < bb ? -1 : 0;
@@ -87,11 +87,11 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
87
87
  const ta = a.getTime();
88
88
  const tb = b.getTime();
89
89
  /* istanbul ignore next -- _validateKey prevents invalid Date from entering the tree */
90
- if (Number.isNaN(ta) || Number.isNaN(tb)) throw new TypeError(ERR.invalidDate('TreeSet'));
90
+ if (Number.isNaN(ta) || Number.isNaN(tb)) raise(TypeError, ERR.invalidDate('TreeSet'));
91
91
  return ta > tb ? 1 : ta < tb ? -1 : 0;
92
92
  }
93
93
 
94
- throw new TypeError(ERR.comparatorRequired('TreeSet'));
94
+ raise(TypeError, ERR.comparatorRequired('TreeSet'));
95
95
  };
96
96
  }
97
97
 
@@ -226,6 +226,41 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
226
226
 
227
227
 
228
228
 
229
+
230
+
231
+
232
+
233
+
234
+
235
+
236
+
237
+
238
+
239
+
240
+
241
+
242
+
243
+
244
+
245
+
246
+
247
+
248
+
249
+
250
+
251
+
252
+
253
+
254
+
255
+
256
+
257
+
258
+
259
+
260
+
261
+
262
+
263
+
229
264
 
230
265
 
231
266
 
@@ -259,19 +294,19 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
259
294
  if (!this.#isDefaultComparator) return;
260
295
 
261
296
  if (typeof key === 'number') {
262
- if (Number.isNaN(key)) throw new TypeError(ERR.invalidNaN('TreeSet'));
297
+ if (Number.isNaN(key)) raise(TypeError, ERR.invalidNaN('TreeSet'));
263
298
  return;
264
299
  }
265
300
 
266
301
  if (typeof key === 'string') return;
267
302
 
268
303
  if (key instanceof Date) {
269
- if (Number.isNaN(key.getTime())) throw new TypeError(ERR.invalidDate('TreeSet'));
304
+ if (Number.isNaN(key.getTime())) raise(TypeError, ERR.invalidDate('TreeSet'));
270
305
  return;
271
306
  }
272
307
 
273
308
  // Other key types should have provided a comparator, so reaching here means misuse.
274
- throw new TypeError(ERR.comparatorRequired('TreeSet'));
309
+ raise(TypeError, ERR.comparatorRequired('TreeSet'));
275
310
  }
276
311
 
277
312
  /**
@@ -402,6 +437,41 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
402
437
 
403
438
 
404
439
 
440
+
441
+
442
+
443
+
444
+
445
+
446
+
447
+
448
+
449
+
450
+
451
+
452
+
453
+
454
+
455
+
456
+
457
+
458
+
459
+
460
+
461
+
462
+
463
+
464
+
465
+
466
+
467
+
468
+
469
+
470
+
471
+
472
+
473
+
474
+
405
475
 
406
476
 
407
477
 
@@ -442,6 +512,42 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
442
512
  return this;
443
513
  }
444
514
 
515
+ /**
516
+ * Add multiple keys at once.
517
+ * @remarks Expected time O(m log n), where m is the number of keys.
518
+ * @param keys - Iterable of keys to add.
519
+ * @returns Array of booleans indicating whether each key was newly added.
520
+
521
+
522
+
523
+
524
+
525
+
526
+
527
+
528
+
529
+
530
+
531
+
532
+
533
+
534
+
535
+
536
+ * @example
537
+ * // Add multiple keys
538
+ * const ts = new TreeSet<number>();
539
+ * ts.addMany([5, 3, 7, 1, 9]);
540
+ * console.log(ts.size); // 5;
541
+ */
542
+ addMany(keys: Iterable<K>): boolean[] {
543
+ const results: boolean[] = [];
544
+ for (const key of keys) {
545
+ this._validateKey(key);
546
+ results.push(this.#core.set(key, undefined));
547
+ }
548
+ return results;
549
+ }
550
+
445
551
  /**
446
552
  * Test whether a key exists.
447
553
  * @remarks Expected time O(log n)
@@ -578,6 +684,41 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
578
684
 
579
685
 
580
686
 
687
+
688
+
689
+
690
+
691
+
692
+
693
+
694
+
695
+
696
+
697
+
698
+
699
+
700
+
701
+
702
+
703
+
704
+
705
+
706
+
707
+
708
+
709
+
710
+
711
+
712
+
713
+
714
+
715
+
716
+
717
+
718
+
719
+
720
+
721
+
581
722
 
582
723
 
583
724
 
@@ -769,25 +910,6 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
769
910
 
770
911
 
771
912
 
772
- * @example
773
- * // Removing elements while maintaining order
774
- * const nums = new TreeSet<number>([1, 3, 5, 7, 9]);
775
- *
776
- * console.log(nums.delete(5)); // true;
777
- * console.log(nums.delete(5)); // false; // already gone
778
- * console.log([...nums]); // [1, 3, 7, 9];
779
- */
780
- delete(key: K): boolean {
781
- this._validateKey(key);
782
- const res = this.#core.delete(key);
783
- return Array.isArray(res) && res.length > 0 && !!res[0]?.deleted;
784
- }
785
-
786
- /**
787
- * Remove all keys.
788
-
789
-
790
-
791
913
 
792
914
 
793
915
 
@@ -823,6 +945,39 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
823
945
 
824
946
 
825
947
 
948
+ * @example
949
+ * // Removing elements while maintaining order
950
+ * const nums = new TreeSet<number>([1, 3, 5, 7, 9]);
951
+ *
952
+ * console.log(nums.delete(5)); // true;
953
+ * console.log(nums.delete(5)); // false; // already gone
954
+ * console.log([...nums]); // [1, 3, 7, 9];
955
+ */
956
+ delete(key: K): boolean {
957
+ this._validateKey(key);
958
+ return this.#core.delete(key);
959
+ }
960
+
961
+ /**
962
+ * Delete all keys matching a predicate.
963
+ * @remarks Time O(N), Space O(N)
964
+ * @param predicate - Function (key, index, set) → boolean; return true to delete.
965
+ * @returns True if at least one key was deleted.
966
+ */
967
+ deleteWhere(predicate: (key: K, index: number, set: this) => boolean): boolean {
968
+ let deleted = false;
969
+ let index = 0;
970
+ for (const key of this) {
971
+ if (predicate(key, index++, this)) {
972
+ this.delete(key);
973
+ deleted = true;
974
+ }
975
+ }
976
+ return deleted;
977
+ }
978
+
979
+ /**
980
+ * Remove all keys.
826
981
 
827
982
 
828
983
 
@@ -928,18 +1083,6 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
928
1083
 
929
1084
 
930
1085
 
931
- * @example
932
- * // Remove all
933
- * const ts = new TreeSet<number>([1, 2]);
934
- * ts.clear();
935
- * console.log(ts.isEmpty()); // true;
936
- */
937
- clear(): void {
938
- this.#core.clear();
939
- }
940
-
941
- /**
942
- * Iterate over keys in ascending order.
943
1086
 
944
1087
 
945
1088
 
@@ -1013,6 +1156,18 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
1013
1156
 
1014
1157
 
1015
1158
 
1159
+ * @example
1160
+ * // Remove all
1161
+ * const ts = new TreeSet<number>([1, 2]);
1162
+ * ts.clear();
1163
+ * console.log(ts.isEmpty()); // true;
1164
+ */
1165
+ clear(): void {
1166
+ this.#core.clear();
1167
+ }
1168
+
1169
+ /**
1170
+ * Iterate over keys in ascending order.
1016
1171
 
1017
1172
 
1018
1173
 
@@ -1083,19 +1238,6 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
1083
1238
 
1084
1239
 
1085
1240
 
1086
- * @example
1087
- * // Get sorted keys
1088
- * const ts = new TreeSet<number>([30, 10, 20]);
1089
- * console.log([...ts.keys()]); // [10, 20, 30];
1090
- */
1091
- keys(): IterableIterator<K> {
1092
- return this.#core.keys();
1093
- }
1094
-
1095
- /**
1096
- * Iterate over values in ascending order.
1097
- *
1098
- * Note: for Set-like containers, `values()` is the same as `keys()`.
1099
1241
 
1100
1242
 
1101
1243
 
@@ -1204,6 +1346,19 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
1204
1346
 
1205
1347
 
1206
1348
 
1349
+ * @example
1350
+ * // Get sorted keys
1351
+ * const ts = new TreeSet<number>([30, 10, 20]);
1352
+ * console.log([...ts.keys()]); // [10, 20, 30];
1353
+ */
1354
+ keys(): IterableIterator<K> {
1355
+ return this.#core.keys();
1356
+ }
1357
+
1358
+ /**
1359
+ * Iterate over values in ascending order.
1360
+ *
1361
+ * Note: for Set-like containers, `values()` is the same as `keys()`.
1207
1362
 
1208
1363
 
1209
1364
 
@@ -1239,19 +1394,6 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
1239
1394
 
1240
1395
 
1241
1396
 
1242
- * @example
1243
- * // Get values (same as keys for Set)
1244
- * const ts = new TreeSet<number>([2, 1, 3]);
1245
- * console.log([...ts.values()]); // [1, 2, 3];
1246
- */
1247
- values(): IterableIterator<K> {
1248
- return this.keys();
1249
- }
1250
-
1251
- /**
1252
- * Iterate over `[value, value]` pairs (native Set convention).
1253
- *
1254
- * Note: TreeSet stores only keys internally; `[k, k]` is created on-the-fly during iteration.
1255
1397
 
1256
1398
 
1257
1399
 
@@ -1396,22 +1538,18 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
1396
1538
 
1397
1539
 
1398
1540
  * @example
1399
- * // Iterate entries
1400
- * const ts = new TreeSet<number>([3, 1, 2]);
1401
- * console.log([...ts.entries()].map(([k]) => k)); // [1, 2, 3];
1541
+ * // Get values (same as keys for Set)
1542
+ * const ts = new TreeSet<number>([2, 1, 3]);
1543
+ * console.log([...ts.values()]); // [1, 2, 3];
1402
1544
  */
1403
- *entries(): IterableIterator<[K, K]> {
1404
- for (const k of this.keys()) yield [k, k];
1405
- }
1406
-
1407
- [Symbol.iterator](): IterableIterator<K> {
1545
+ values(): IterableIterator<K> {
1408
1546
  return this.keys();
1409
1547
  }
1410
1548
 
1411
1549
  /**
1412
- * Visit each value in ascending order.
1550
+ * Iterate over `[value, value]` pairs (native Set convention).
1413
1551
  *
1414
- * Callback follows native Set convention: `(value, value2, set)`.
1552
+ * Note: TreeSet stores only keys internally; `[k, k]` is created on-the-fly during iteration.
1415
1553
 
1416
1554
 
1417
1555
 
@@ -1555,18 +1693,248 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
1555
1693
 
1556
1694
 
1557
1695
 
1558
- * @example
1559
- * // Execute for each
1560
- * const ts = new TreeSet<number>([3, 1, 2]);
1561
- * const keys: number[] = [];
1562
- * ts.forEach(k => keys.push(k));
1563
- * console.log(keys); // [1, 2, 3];
1564
- */
1565
- forEach(cb: (value: K, value2: K, set: TreeSet<K>) => void, thisArg?: unknown): void {
1566
- for (const k of this) cb.call(thisArg, k, k, this);
1567
- }
1568
-
1569
- /**
1696
+
1697
+
1698
+
1699
+
1700
+
1701
+
1702
+
1703
+
1704
+
1705
+
1706
+
1707
+
1708
+
1709
+
1710
+
1711
+
1712
+
1713
+
1714
+
1715
+
1716
+
1717
+
1718
+
1719
+
1720
+
1721
+
1722
+
1723
+
1724
+
1725
+
1726
+
1727
+
1728
+
1729
+
1730
+
1731
+ * @example
1732
+ * // Iterate entries
1733
+ * const ts = new TreeSet<number>([3, 1, 2]);
1734
+ * console.log([...ts.entries()].map(([k]) => k)); // [1, 2, 3];
1735
+ */
1736
+ *entries(): IterableIterator<[K, K]> {
1737
+ for (const k of this.keys()) yield [k, k];
1738
+ }
1739
+
1740
+ [Symbol.iterator](): IterableIterator<K> {
1741
+ return this.keys();
1742
+ }
1743
+
1744
+ /**
1745
+ * Visit each value in ascending order.
1746
+ *
1747
+ * Callback follows native Set convention: `(value, value2, set)`.
1748
+
1749
+
1750
+
1751
+
1752
+
1753
+
1754
+
1755
+
1756
+
1757
+
1758
+
1759
+
1760
+
1761
+
1762
+
1763
+
1764
+
1765
+
1766
+
1767
+
1768
+
1769
+
1770
+
1771
+
1772
+
1773
+
1774
+
1775
+
1776
+
1777
+
1778
+
1779
+
1780
+
1781
+
1782
+
1783
+
1784
+
1785
+
1786
+
1787
+
1788
+
1789
+
1790
+
1791
+
1792
+
1793
+
1794
+
1795
+
1796
+
1797
+
1798
+
1799
+
1800
+
1801
+
1802
+
1803
+
1804
+
1805
+
1806
+
1807
+
1808
+
1809
+
1810
+
1811
+
1812
+
1813
+
1814
+
1815
+
1816
+
1817
+
1818
+
1819
+
1820
+
1821
+
1822
+
1823
+
1824
+
1825
+
1826
+
1827
+
1828
+
1829
+
1830
+
1831
+
1832
+
1833
+
1834
+
1835
+
1836
+
1837
+
1838
+
1839
+
1840
+
1841
+
1842
+
1843
+
1844
+
1845
+
1846
+
1847
+
1848
+
1849
+
1850
+
1851
+
1852
+
1853
+
1854
+
1855
+
1856
+
1857
+
1858
+
1859
+
1860
+
1861
+
1862
+
1863
+
1864
+
1865
+
1866
+
1867
+
1868
+
1869
+
1870
+
1871
+
1872
+
1873
+
1874
+
1875
+
1876
+
1877
+
1878
+
1879
+
1880
+
1881
+
1882
+
1883
+
1884
+
1885
+
1886
+
1887
+
1888
+
1889
+
1890
+
1891
+
1892
+
1893
+
1894
+
1895
+
1896
+
1897
+
1898
+
1899
+
1900
+
1901
+
1902
+
1903
+
1904
+
1905
+
1906
+
1907
+
1908
+
1909
+
1910
+
1911
+
1912
+
1913
+
1914
+
1915
+
1916
+
1917
+
1918
+
1919
+
1920
+
1921
+
1922
+
1923
+
1924
+
1925
+
1926
+ * @example
1927
+ * // Execute for each
1928
+ * const ts = new TreeSet<number>([3, 1, 2]);
1929
+ * const keys: number[] = [];
1930
+ * ts.forEach(k => keys.push(k));
1931
+ * console.log(keys); // [1, 2, 3];
1932
+ */
1933
+ forEach(cb: (value: K, value2: K, set: TreeSet<K>) => void, thisArg?: unknown): void {
1934
+ for (const k of this) cb.call(thisArg, k, k, this);
1935
+ }
1936
+
1937
+ /**
1570
1938
  * Create a new TreeSet by mapping each value to a new key.
1571
1939
  *
1572
1940
  * This mirrors `RedBlackTree.map`: mapping produces a new ordered container.
@@ -1693,6 +2061,41 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
1693
2061
 
1694
2062
 
1695
2063
 
2064
+
2065
+
2066
+
2067
+
2068
+
2069
+
2070
+
2071
+
2072
+
2073
+
2074
+
2075
+
2076
+
2077
+
2078
+
2079
+
2080
+
2081
+
2082
+
2083
+
2084
+
2085
+
2086
+
2087
+
2088
+
2089
+
2090
+
2091
+
2092
+
2093
+
2094
+
2095
+
2096
+
2097
+
2098
+
1696
2099
 
1697
2100
 
1698
2101
 
@@ -1861,6 +2264,41 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
1861
2264
 
1862
2265
 
1863
2266
 
2267
+
2268
+
2269
+
2270
+
2271
+
2272
+
2273
+
2274
+
2275
+
2276
+
2277
+
2278
+
2279
+
2280
+
2281
+
2282
+
2283
+
2284
+
2285
+
2286
+
2287
+
2288
+
2289
+
2290
+
2291
+
2292
+
2293
+
2294
+
2295
+
2296
+
2297
+
2298
+
2299
+
2300
+
2301
+
1864
2302
 
1865
2303
 
1866
2304
 
@@ -2025,6 +2463,41 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
2025
2463
 
2026
2464
 
2027
2465
 
2466
+
2467
+
2468
+
2469
+
2470
+
2471
+
2472
+
2473
+
2474
+
2475
+
2476
+
2477
+
2478
+
2479
+
2480
+
2481
+
2482
+
2483
+
2484
+
2485
+
2486
+
2487
+
2488
+
2489
+
2490
+
2491
+
2492
+
2493
+
2494
+
2495
+
2496
+
2497
+
2498
+
2499
+
2500
+
2028
2501
 
2029
2502
 
2030
2503
 
@@ -2182,6 +2655,41 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
2182
2655
 
2183
2656
 
2184
2657
 
2658
+
2659
+
2660
+
2661
+
2662
+
2663
+
2664
+
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
+
2690
+
2691
+
2692
+
2185
2693
 
2186
2694
 
2187
2695
 
@@ -2342,6 +2850,41 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
2342
2850
 
2343
2851
 
2344
2852
 
2853
+
2854
+
2855
+
2856
+
2857
+
2858
+
2859
+
2860
+
2861
+
2862
+
2863
+
2864
+
2865
+
2866
+
2867
+
2868
+
2869
+
2870
+
2871
+
2872
+
2873
+
2874
+
2875
+
2876
+
2877
+
2878
+
2879
+
2880
+
2881
+
2882
+
2883
+
2884
+
2885
+
2886
+
2887
+
2345
2888
 
2346
2889
 
2347
2890
 
@@ -2502,6 +3045,41 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
2502
3045
 
2503
3046
 
2504
3047
 
3048
+
3049
+
3050
+
3051
+
3052
+
3053
+
3054
+
3055
+
3056
+
3057
+
3058
+
3059
+
3060
+
3061
+
3062
+
3063
+
3064
+
3065
+
3066
+
3067
+
3068
+
3069
+
3070
+
3071
+
3072
+
3073
+
3074
+
3075
+
3076
+
3077
+
3078
+
3079
+
3080
+
3081
+
3082
+
2505
3083
 
2506
3084
 
2507
3085
 
@@ -2686,18 +3264,88 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
2686
3264
 
2687
3265
 
2688
3266
 
2689
- * @example
2690
- * // Convert to array
2691
- * const ts = new TreeSet<number>([3, 1, 2]);
2692
- * console.log(ts.toArray()); // [1, 2, 3];
2693
- */
2694
- toArray(): K[] {
2695
- return [...this];
2696
- }
2697
-
2698
- /**
2699
- * Print a human-friendly representation.
2700
- * @remarks Time O(n), Space O(n)
3267
+
3268
+
3269
+
3270
+
3271
+
3272
+
3273
+
3274
+
3275
+
3276
+
3277
+
3278
+
3279
+
3280
+
3281
+
3282
+
3283
+
3284
+
3285
+
3286
+
3287
+
3288
+
3289
+
3290
+
3291
+
3292
+
3293
+
3294
+
3295
+
3296
+
3297
+
3298
+
3299
+
3300
+
3301
+
3302
+ * @example
3303
+ * // Convert to array
3304
+ * const ts = new TreeSet<number>([3, 1, 2]);
3305
+ * console.log(ts.toArray()); // [1, 2, 3];
3306
+ */
3307
+ toArray(): K[] {
3308
+ return [...this];
3309
+ }
3310
+
3311
+ /**
3312
+ * Print a human-friendly representation.
3313
+ * @remarks Time O(n), Space O(n)
3314
+
3315
+
3316
+
3317
+
3318
+
3319
+
3320
+
3321
+
3322
+
3323
+
3324
+
3325
+
3326
+
3327
+
3328
+
3329
+
3330
+
3331
+
3332
+
3333
+
3334
+
3335
+
3336
+
3337
+
3338
+
3339
+
3340
+
3341
+
3342
+
3343
+
3344
+
3345
+
3346
+
3347
+
3348
+
2701
3349
 
2702
3350
 
2703
3351
 
@@ -2879,6 +3527,13 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
2879
3527
 
2880
3528
 
2881
3529
 
3530
+
3531
+
3532
+
3533
+
3534
+
3535
+
3536
+
2882
3537
 
2883
3538
 
2884
3539
 
@@ -2945,6 +3600,13 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
2945
3600
 
2946
3601
 
2947
3602
 
3603
+
3604
+
3605
+
3606
+
3607
+
3608
+
3609
+
2948
3610
 
2949
3611
 
2950
3612
 
@@ -2989,6 +3651,13 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
2989
3651
 
2990
3652
 
2991
3653
 
3654
+
3655
+
3656
+
3657
+
3658
+
3659
+
3660
+
2992
3661
 
2993
3662
 
2994
3663
 
@@ -3038,6 +3707,13 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
3038
3707
 
3039
3708
 
3040
3709
 
3710
+
3711
+
3712
+
3713
+
3714
+
3715
+
3716
+
3041
3717
 
3042
3718
 
3043
3719
 
@@ -3163,6 +3839,34 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
3163
3839
 
3164
3840
 
3165
3841
 
3842
+
3843
+
3844
+
3845
+
3846
+
3847
+
3848
+
3849
+
3850
+
3851
+
3852
+
3853
+
3854
+
3855
+
3856
+
3857
+
3858
+
3859
+
3860
+
3861
+
3862
+
3863
+
3864
+
3865
+
3866
+
3867
+
3868
+
3869
+
3166
3870
 
3167
3871
 
3168
3872
 
@@ -3309,6 +4013,34 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
3309
4013
 
3310
4014
 
3311
4015
 
4016
+
4017
+
4018
+
4019
+
4020
+
4021
+
4022
+
4023
+
4024
+
4025
+
4026
+
4027
+
4028
+
4029
+
4030
+
4031
+
4032
+
4033
+
4034
+
4035
+
4036
+
4037
+
4038
+
4039
+
4040
+
4041
+
4042
+
4043
+
3312
4044
 
3313
4045
 
3314
4046
 
@@ -3447,6 +4179,34 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
3447
4179
 
3448
4180
 
3449
4181
 
4182
+
4183
+
4184
+
4185
+
4186
+
4187
+
4188
+
4189
+
4190
+
4191
+
4192
+
4193
+
4194
+
4195
+
4196
+
4197
+
4198
+
4199
+
4200
+
4201
+
4202
+
4203
+
4204
+
4205
+
4206
+
4207
+
4208
+
4209
+
3450
4210
 
3451
4211
 
3452
4212
 
@@ -3583,6 +4343,34 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
3583
4343
 
3584
4344
 
3585
4345
 
4346
+
4347
+
4348
+
4349
+
4350
+
4351
+
4352
+
4353
+
4354
+
4355
+
4356
+
4357
+
4358
+
4359
+
4360
+
4361
+
4362
+
4363
+
4364
+
4365
+
4366
+
4367
+
4368
+
4369
+
4370
+
4371
+
4372
+
4373
+
3586
4374
 
3587
4375
 
3588
4376
 
@@ -3722,6 +4510,34 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
3722
4510
 
3723
4511
 
3724
4512
 
4513
+
4514
+
4515
+
4516
+
4517
+
4518
+
4519
+
4520
+
4521
+
4522
+
4523
+
4524
+
4525
+
4526
+
4527
+
4528
+
4529
+
4530
+
4531
+
4532
+
4533
+
4534
+
4535
+
4536
+
4537
+
4538
+
4539
+
4540
+
3725
4541
 
3726
4542
 
3727
4543
 
@@ -3782,6 +4598,74 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
3782
4598
  return out;
3783
4599
  }
3784
4600
 
4601
+ // ─── Order-Statistic Methods ───────────────────────────
4602
+
4603
+ /**
4604
+ * Returns the element at the k-th position in tree order (0-indexed).
4605
+ * @remarks Time O(log n). Requires `enableOrderStatistic: true`.
4606
+
4607
+
4608
+
4609
+ * @example
4610
+ * // Find k-th element in a TreeSet
4611
+ * const set = new TreeSet<number>([30, 10, 50, 20, 40], { enableOrderStatistic: true });
4612
+ * console.log(set.getByRank(0)); // 10;
4613
+ * console.log(set.getByRank(2)); // 30;
4614
+ * console.log(set.getRank(30)); // 2;
4615
+ */
4616
+ getByRank(k: number): K | undefined {
4617
+ return this.#core.getByRank(k);
4618
+ }
4619
+
4620
+ /**
4621
+ * Returns the 0-based rank of a key (number of elements that precede it in tree order).
4622
+ * @remarks Time O(log n). Requires `enableOrderStatistic: true`.
4623
+ * @example
4624
+ * // Get the rank of a key in sorted order
4625
+ * const tree = new TreeSet<number>(
4626
+ * [10, 20, 30, 40, 50],
4627
+ * { enableOrderStatistic: true }
4628
+ * );
4629
+ * console.log(tree.getRank(10)); // 0; // smallest → rank 0
4630
+ * console.log(tree.getRank(30)); // 2; // 2 elements before 30 in tree order
4631
+ * console.log(tree.getRank(50)); // 4; // largest → rank 4
4632
+ * console.log(tree.getRank(25)); // 2;
4633
+ */
4634
+ getRank(key: K): number {
4635
+ return this.#core.getRank(key);
4636
+ }
4637
+
4638
+ /**
4639
+ * Returns elements by rank range (0-indexed, inclusive on both ends).
4640
+ * @remarks Time O(log n + k). Requires `enableOrderStatistic: true`.
4641
+
4642
+
4643
+
4644
+
4645
+
4646
+
4647
+
4648
+
4649
+
4650
+ * @example
4651
+ * // Pagination by position in tree order
4652
+ * const tree = new TreeSet<number>(
4653
+ * [10, 20, 30, 40, 50, 60, 70, 80, 90],
4654
+ * { enableOrderStatistic: true }
4655
+ * );
4656
+ * const pageSize = 3;
4657
+ *
4658
+ * // Page 1
4659
+ * console.log(tree.rangeByRank(0, pageSize - 1)); // [10, 20, 30];
4660
+ * // Page 2
4661
+ * console.log(tree.rangeByRank(pageSize, 2 * pageSize - 1)); // [40, 50, 60];
4662
+ * // Page 3
4663
+ * console.log(tree.rangeByRank(2 * pageSize, 3 * pageSize - 1)); // [70, 80, 90];
4664
+ */
4665
+ rangeByRank(start: number, end: number): K[] {
4666
+ return this.#core.rangeByRank(start, end).filter((k): k is K => k !== undefined);
4667
+ }
4668
+
3785
4669
  /**
3786
4670
  * Creates a shallow clone of this set.
3787
4671
  * @remarks Time O(n log n), Space O(n)
@@ -3908,6 +4792,41 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
3908
4792
 
3909
4793
 
3910
4794
 
4795
+
4796
+
4797
+
4798
+
4799
+
4800
+
4801
+
4802
+
4803
+
4804
+
4805
+
4806
+
4807
+
4808
+
4809
+
4810
+
4811
+
4812
+
4813
+
4814
+
4815
+
4816
+
4817
+
4818
+
4819
+
4820
+
4821
+
4822
+
4823
+
4824
+
4825
+
4826
+
4827
+
4828
+
4829
+
3911
4830
 
3912
4831
 
3913
4832