avl-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 +423 -52
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +423 -51
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +423 -52
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +423 -51
  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/avl-tree-typed.js +420 -49
  38. package/dist/umd/avl-tree-typed.js.map +1 -1
  39. package/dist/umd/avl-tree-typed.min.js +3 -3
  40. package/dist/umd/avl-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
@@ -59,6 +59,56 @@ function makeTrampoline(fn) {
59
59
  }
60
60
  __name(makeTrampoline, "makeTrampoline");
61
61
 
62
+ // src/common/error.ts
63
+ function raise(ErrorClass, message) {
64
+ throw new ErrorClass(message);
65
+ }
66
+ __name(raise, "raise");
67
+ var ERR = {
68
+ // Range / index
69
+ indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
70
+ invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
71
+ // Type / argument
72
+ invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
73
+ comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
74
+ invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
75
+ notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
76
+ invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
77
+ invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
78
+ invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
79
+ reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
80
+ callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
81
+ // State / operation
82
+ invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
83
+ // Matrix
84
+ matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
85
+ matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
86
+ matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
87
+ matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
88
+ matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch"),
89
+ // Order statistic
90
+ orderStatisticNotEnabled: /* @__PURE__ */ __name((method, ctx) => `${ctx ? ctx + ": " : ""}${method}() requires enableOrderStatistic: true.`, "orderStatisticNotEnabled")
91
+ };
92
+
93
+ // src/common/index.ts
94
+ var Range = class {
95
+ constructor(low, high, includeLow = true, includeHigh = true) {
96
+ this.low = low;
97
+ this.high = high;
98
+ this.includeLow = includeLow;
99
+ this.includeHigh = includeHigh;
100
+ }
101
+ static {
102
+ __name(this, "Range");
103
+ }
104
+ // Determine whether a key is within the range
105
+ isInRange(key, comparator) {
106
+ const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
107
+ const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
108
+ return lowCheck && highCheck;
109
+ }
110
+ };
111
+
62
112
  // src/data-structures/base/iterable-element-base.ts
63
113
  var IterableElementBase = class {
64
114
  static {
@@ -77,7 +127,7 @@ var IterableElementBase = class {
77
127
  if (options) {
78
128
  const { toElementFn } = options;
79
129
  if (typeof toElementFn === "function") this._toElementFn = toElementFn;
80
- else if (toElementFn) throw new TypeError("toElementFn must be a function type");
130
+ else if (toElementFn) raise(TypeError, "toElementFn must be a function type");
81
131
  }
82
132
  }
83
133
  /**
@@ -240,7 +290,7 @@ var IterableElementBase = class {
240
290
  acc = initialValue;
241
291
  } else {
242
292
  const first = iter.next();
243
- if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
293
+ if (first.done) raise(TypeError, "Reduce of empty structure with no initial value");
244
294
  acc = first.value;
245
295
  index = 1;
246
296
  }
@@ -475,50 +525,6 @@ var LinearBase = class _LinearBase extends IterableElementBase {
475
525
  }
476
526
  };
477
527
 
478
- // src/common/error.ts
479
- var ERR = {
480
- // Range / index
481
- indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
482
- invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
483
- // Type / argument
484
- invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
485
- comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
486
- invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
487
- notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
488
- invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
489
- invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
490
- invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
491
- reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
492
- callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
493
- // State / operation
494
- invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
495
- // Matrix
496
- matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
497
- matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
498
- matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
499
- matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
500
- matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
501
- };
502
-
503
- // src/common/index.ts
504
- var Range = class {
505
- constructor(low, high, includeLow = true, includeHigh = true) {
506
- this.low = low;
507
- this.high = high;
508
- this.includeLow = includeLow;
509
- this.includeHigh = includeHigh;
510
- }
511
- static {
512
- __name(this, "Range");
513
- }
514
- // Determine whether a key is within the range
515
- isInRange(key, comparator) {
516
- const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
517
- const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
518
- return lowCheck && highCheck;
519
- }
520
- };
521
-
522
528
  // src/data-structures/base/iterable-entry-base.ts
523
529
  var IterableEntryBase = class {
524
530
  static {
@@ -789,6 +795,9 @@ var Queue = class _Queue extends LinearBase {
789
795
 
790
796
 
791
797
 
798
+
799
+
800
+
792
801
 
793
802
 
794
803
 
@@ -836,6 +845,9 @@ var Queue = class _Queue extends LinearBase {
836
845
 
837
846
 
838
847
 
848
+
849
+
850
+
839
851
 
840
852
 
841
853
 
@@ -899,6 +911,9 @@ var Queue = class _Queue extends LinearBase {
899
911
 
900
912
 
901
913
 
914
+
915
+
916
+
902
917
 
903
918
 
904
919
 
@@ -958,6 +973,9 @@ var Queue = class _Queue extends LinearBase {
958
973
 
959
974
 
960
975
 
976
+
977
+
978
+
961
979
 
962
980
 
963
981
 
@@ -1024,6 +1042,9 @@ var Queue = class _Queue extends LinearBase {
1024
1042
 
1025
1043
 
1026
1044
 
1045
+
1046
+
1047
+
1027
1048
 
1028
1049
 
1029
1050
 
@@ -1080,6 +1101,9 @@ var Queue = class _Queue extends LinearBase {
1080
1101
 
1081
1102
 
1082
1103
 
1104
+
1105
+
1106
+
1083
1107
 
1084
1108
 
1085
1109
 
@@ -1129,6 +1153,9 @@ var Queue = class _Queue extends LinearBase {
1129
1153
 
1130
1154
 
1131
1155
 
1156
+
1157
+
1158
+
1132
1159
 
1133
1160
 
1134
1161
 
@@ -1219,6 +1246,9 @@ var Queue = class _Queue extends LinearBase {
1219
1246
 
1220
1247
 
1221
1248
 
1249
+
1250
+
1251
+
1222
1252
 
1223
1253
 
1224
1254
 
@@ -1262,6 +1292,9 @@ var Queue = class _Queue extends LinearBase {
1262
1292
 
1263
1293
 
1264
1294
 
1295
+
1296
+
1297
+
1265
1298
 
1266
1299
 
1267
1300
 
@@ -1328,6 +1361,9 @@ var Queue = class _Queue extends LinearBase {
1328
1361
 
1329
1362
 
1330
1363
 
1364
+
1365
+
1366
+
1331
1367
 
1332
1368
 
1333
1369
 
@@ -1378,6 +1414,9 @@ var Queue = class _Queue extends LinearBase {
1378
1414
 
1379
1415
 
1380
1416
 
1417
+
1418
+
1419
+
1381
1420
 
1382
1421
 
1383
1422
 
@@ -1432,6 +1471,9 @@ var Queue = class _Queue extends LinearBase {
1432
1471
 
1433
1472
 
1434
1473
 
1474
+
1475
+
1476
+
1435
1477
 
1436
1478
 
1437
1479
 
@@ -1686,7 +1728,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
1686
1728
  if (isMapMode !== void 0) this._isMapMode = isMapMode;
1687
1729
  if (isDuplicate !== void 0) this._isDuplicate = isDuplicate;
1688
1730
  if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
1689
- else if (toEntryFn) throw new TypeError(ERR.notAFunction("toEntryFn", "BinaryTree"));
1731
+ else if (toEntryFn) raise(TypeError, ERR.notAFunction("toEntryFn", "BinaryTree"));
1690
1732
  }
1691
1733
  if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
1692
1734
  }
@@ -1929,6 +1971,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
1929
1971
 
1930
1972
 
1931
1973
 
1974
+
1975
+
1976
+
1932
1977
 
1933
1978
 
1934
1979
 
@@ -1980,6 +2025,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
1980
2025
 
1981
2026
 
1982
2027
 
2028
+
2029
+
2030
+
1983
2031
 
1984
2032
 
1985
2033
 
@@ -2083,6 +2131,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2083
2131
 
2084
2132
 
2085
2133
 
2134
+
2135
+
2136
+
2086
2137
 
2087
2138
 
2088
2139
 
@@ -2122,6 +2173,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2122
2173
 
2123
2174
 
2124
2175
 
2176
+
2177
+
2178
+
2125
2179
 
2126
2180
 
2127
2181
 
@@ -2182,6 +2236,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2182
2236
 
2183
2237
 
2184
2238
 
2239
+
2240
+
2241
+
2185
2242
 
2186
2243
 
2187
2244
 
@@ -2241,6 +2298,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2241
2298
 
2242
2299
 
2243
2300
 
2301
+
2302
+
2303
+
2244
2304
 
2245
2305
 
2246
2306
 
@@ -2378,6 +2438,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2378
2438
 
2379
2439
 
2380
2440
 
2441
+
2442
+
2443
+
2381
2444
 
2382
2445
 
2383
2446
 
@@ -2433,6 +2496,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2433
2496
 
2434
2497
 
2435
2498
 
2499
+
2500
+
2501
+
2436
2502
 
2437
2503
 
2438
2504
 
@@ -2490,6 +2556,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2490
2556
 
2491
2557
 
2492
2558
 
2559
+
2560
+
2561
+
2493
2562
 
2494
2563
 
2495
2564
 
@@ -2535,6 +2604,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2535
2604
 
2536
2605
 
2537
2606
 
2607
+
2608
+
2609
+
2538
2610
 
2539
2611
 
2540
2612
 
@@ -2589,6 +2661,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2589
2661
 
2590
2662
 
2591
2663
 
2664
+
2665
+
2666
+
2592
2667
 
2593
2668
 
2594
2669
 
@@ -2670,6 +2745,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2670
2745
 
2671
2746
 
2672
2747
 
2748
+
2749
+
2750
+
2673
2751
 
2674
2752
 
2675
2753
 
@@ -2728,6 +2806,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2728
2806
 
2729
2807
 
2730
2808
 
2809
+
2810
+
2811
+
2731
2812
 
2732
2813
 
2733
2814
 
@@ -3202,6 +3283,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
3202
3283
 
3203
3284
 
3204
3285
 
3286
+
3287
+
3288
+
3205
3289
 
3206
3290
 
3207
3291
 
@@ -3251,6 +3335,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
3251
3335
 
3252
3336
 
3253
3337
 
3338
+
3339
+
3340
+
3254
3341
 
3255
3342
 
3256
3343
 
@@ -3304,6 +3391,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
3304
3391
 
3305
3392
 
3306
3393
 
3394
+
3395
+
3396
+
3307
3397
 
3308
3398
 
3309
3399
 
@@ -3382,6 +3472,9 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
3382
3472
 
3383
3473
 
3384
3474
 
3475
+
3476
+
3477
+
3385
3478
 
3386
3479
 
3387
3480
 
@@ -4017,12 +4110,16 @@ var BST = class extends BinaryTree {
4017
4110
  } else {
4018
4111
  this._comparator = this._createDefaultComparator();
4019
4112
  }
4113
+ if (options.enableOrderStatistic) {
4114
+ this._enableOrderStatistic = true;
4115
+ }
4020
4116
  } else {
4021
4117
  this._comparator = this._createDefaultComparator();
4022
4118
  }
4023
4119
  if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
4024
4120
  }
4025
4121
  _root = void 0;
4122
+ _enableOrderStatistic = false;
4026
4123
  /**
4027
4124
  * Gets the root node of the tree.
4028
4125
  * @remarks Time O(1)
@@ -4192,6 +4289,12 @@ var BST = class extends BinaryTree {
4192
4289
 
4193
4290
 
4194
4291
 
4292
+
4293
+
4294
+
4295
+
4296
+
4297
+
4195
4298
 
4196
4299
 
4197
4300
 
@@ -4353,6 +4456,84 @@ var BST = class extends BinaryTree {
4353
4456
  const searchRange = range instanceof Range ? range : new Range(range[0], range[1]);
4354
4457
  return this.search(searchRange, false, callback, startNode, iterationType);
4355
4458
  }
4459
+ getByRank(k, callback = this._DEFAULT_NODE_CALLBACK, iterationType = this.iterationType) {
4460
+ if (!this._enableOrderStatistic) {
4461
+ raise(Error, ERR.orderStatisticNotEnabled("getByRank"));
4462
+ }
4463
+ if (k < 0 || k >= this._size) return void 0;
4464
+ let actualCallback = void 0;
4465
+ let actualIterationType = this.iterationType;
4466
+ if (typeof callback === "string") {
4467
+ actualIterationType = callback;
4468
+ } else if (callback) {
4469
+ actualCallback = callback;
4470
+ if (iterationType) {
4471
+ actualIterationType = iterationType;
4472
+ }
4473
+ }
4474
+ const node = actualIterationType === "RECURSIVE" ? this._getByRankRecursive(this._root, k) : this._getByRankIterative(this._root, k);
4475
+ if (!node) return void 0;
4476
+ return actualCallback ? actualCallback(node) : node.key;
4477
+ }
4478
+ getRank(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
4479
+ if (!this._enableOrderStatistic) {
4480
+ raise(Error, ERR.orderStatisticNotEnabled("getRank"));
4481
+ }
4482
+ if (!this._root || this._size === 0) return -1;
4483
+ let actualIterationType = this.iterationType;
4484
+ if (iterationType) actualIterationType = iterationType;
4485
+ let key;
4486
+ if (typeof keyNodeEntryOrPredicate === "function") {
4487
+ const results = this.search(keyNodeEntryOrPredicate, true);
4488
+ if (results.length === 0 || results[0] === void 0) return -1;
4489
+ key = results[0];
4490
+ } else if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
4491
+ return -1;
4492
+ } else if (this.isNode(keyNodeEntryOrPredicate)) {
4493
+ key = keyNodeEntryOrPredicate.key;
4494
+ } else if (Array.isArray(keyNodeEntryOrPredicate)) {
4495
+ key = keyNodeEntryOrPredicate[0] ?? void 0;
4496
+ if (key === void 0 || key === null) return -1;
4497
+ } else {
4498
+ key = keyNodeEntryOrPredicate;
4499
+ }
4500
+ if (key === void 0) return -1;
4501
+ return actualIterationType === "RECURSIVE" ? this._getRankRecursive(this._root, key) : this._getRankIterative(this._root, key);
4502
+ }
4503
+ rangeByRank(start, end, callback = this._DEFAULT_NODE_CALLBACK, iterationType = this.iterationType) {
4504
+ if (!this._enableOrderStatistic) {
4505
+ raise(Error, ERR.orderStatisticNotEnabled("rangeByRank"));
4506
+ }
4507
+ if (this._size === 0) return [];
4508
+ const lo = Math.max(0, start);
4509
+ const hi = Math.min(this._size - 1, end);
4510
+ if (lo > hi) return [];
4511
+ let actualCallback = void 0;
4512
+ let actualIterationType = this.iterationType;
4513
+ if (typeof callback === "string") {
4514
+ actualIterationType = callback;
4515
+ } else if (callback) {
4516
+ actualCallback = callback;
4517
+ if (iterationType) {
4518
+ actualIterationType = iterationType;
4519
+ }
4520
+ }
4521
+ const results = [];
4522
+ const count = hi - lo + 1;
4523
+ const startNode = actualIterationType === "RECURSIVE" ? this._getByRankRecursive(this._root, lo) : this._getByRankIterative(this._root, lo);
4524
+ if (!startNode) return [];
4525
+ let collected = 0;
4526
+ const cb = actualCallback ?? this._DEFAULT_NODE_CALLBACK;
4527
+ let current = startNode;
4528
+ while (current && collected < count) {
4529
+ results.push(cb(current));
4530
+ collected++;
4531
+ if (collected < count) {
4532
+ current = this._next(current);
4533
+ }
4534
+ }
4535
+ return results;
4536
+ }
4356
4537
  /**
4357
4538
  * Adds a new node to the BST based on key comparison.
4358
4539
  * @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
@@ -4438,6 +4619,15 @@ var BST = class extends BinaryTree {
4438
4619
 
4439
4620
 
4440
4621
 
4622
+
4623
+
4624
+
4625
+
4626
+
4627
+
4628
+
4629
+
4630
+
4441
4631
 
4442
4632
 
4443
4633
 
@@ -4462,6 +4652,7 @@ var BST = class extends BinaryTree {
4462
4652
  this._setRoot(newNode);
4463
4653
  if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
4464
4654
  this._size++;
4655
+ this._updateCount(newNode);
4465
4656
  return true;
4466
4657
  }
4467
4658
  let current = this._root;
@@ -4475,6 +4666,7 @@ var BST = class extends BinaryTree {
4475
4666
  current.left = newNode;
4476
4667
  if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
4477
4668
  this._size++;
4669
+ this._updateCountAlongPath(newNode);
4478
4670
  return true;
4479
4671
  }
4480
4672
  if (current.left !== null) current = current.left;
@@ -4483,6 +4675,7 @@ var BST = class extends BinaryTree {
4483
4675
  current.right = newNode;
4484
4676
  if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
4485
4677
  this._size++;
4678
+ this._updateCountAlongPath(newNode);
4486
4679
  return true;
4487
4680
  }
4488
4681
  if (current.right !== null) current = current.right;
@@ -4547,6 +4740,12 @@ var BST = class extends BinaryTree {
4547
4740
 
4548
4741
 
4549
4742
 
4743
+
4744
+
4745
+
4746
+
4747
+
4748
+
4550
4749
 
4551
4750
 
4552
4751
 
@@ -4834,6 +5033,9 @@ var BST = class extends BinaryTree {
4834
5033
 
4835
5034
 
4836
5035
 
5036
+
5037
+
5038
+
4837
5039
 
4838
5040
 
4839
5041
 
@@ -4900,6 +5102,9 @@ var BST = class extends BinaryTree {
4900
5102
 
4901
5103
 
4902
5104
 
5105
+
5106
+
5107
+
4903
5108
 
4904
5109
 
4905
5110
 
@@ -5013,6 +5218,12 @@ var BST = class extends BinaryTree {
5013
5218
 
5014
5219
 
5015
5220
 
5221
+
5222
+
5223
+
5224
+
5225
+
5226
+
5016
5227
 
5017
5228
 
5018
5229
 
@@ -5094,13 +5305,11 @@ var BST = class extends BinaryTree {
5094
5305
  if (a instanceof Date && b instanceof Date) {
5095
5306
  const ta = a.getTime();
5096
5307
  const tb = b.getTime();
5097
- if (Number.isNaN(ta) || Number.isNaN(tb)) throw new TypeError(ERR.invalidDate("BST"));
5308
+ if (Number.isNaN(ta) || Number.isNaN(tb)) raise(TypeError, ERR.invalidDate("BST"));
5098
5309
  return ta > tb ? 1 : ta < tb ? -1 : 0;
5099
5310
  }
5100
5311
  if (typeof a === "object" || typeof b === "object") {
5101
- throw new TypeError(
5102
- ERR.comparatorRequired("BST")
5103
- );
5312
+ raise(TypeError, ERR.comparatorRequired("BST"));
5104
5313
  }
5105
5314
  return 0;
5106
5315
  };
@@ -5419,7 +5628,8 @@ var BST = class extends BinaryTree {
5419
5628
  _snapshotOptions() {
5420
5629
  return {
5421
5630
  ...super._snapshotOptions(),
5422
- comparator: this._comparator
5631
+ comparator: this._comparator,
5632
+ enableOrderStatistic: this._enableOrderStatistic
5423
5633
  };
5424
5634
  }
5425
5635
  /**
@@ -5441,6 +5651,113 @@ var BST = class extends BinaryTree {
5441
5651
  *
5442
5652
  * @param v - The node to set as root.
5443
5653
  */
5654
+ /**
5655
+ * (Protected) Recalculates the subtree count for a single node.
5656
+ * @remarks Time O(1). Only active when enableOrderStatistic is true.
5657
+ */
5658
+ _updateCount(node) {
5659
+ if (!this._enableOrderStatistic) return;
5660
+ node._count = 1 + (this.isRealNode(node.left) ? node.left._count : 0) + (this.isRealNode(node.right) ? node.right._count : 0);
5661
+ }
5662
+ /**
5663
+ * (Protected) Updates subtree counts from a node up to the root.
5664
+ * @remarks Time O(log n). Only active when enableOrderStatistic is true.
5665
+ */
5666
+ _updateCountAlongPath(node) {
5667
+ if (!this._enableOrderStatistic) return;
5668
+ let current = node;
5669
+ while (current) {
5670
+ this._updateCount(current);
5671
+ current = current.parent;
5672
+ }
5673
+ }
5674
+ /**
5675
+ * (Protected) Finds the node at position k in tree order (iterative).
5676
+ * @remarks Time O(log n), Space O(1)
5677
+ */
5678
+ _getByRankIterative(node, k) {
5679
+ let current = node;
5680
+ let remaining = k;
5681
+ while (current) {
5682
+ const leftCount = this.isRealNode(current.left) ? current.left._count : 0;
5683
+ if (remaining < leftCount) {
5684
+ current = current.left;
5685
+ } else if (remaining === leftCount) {
5686
+ return current;
5687
+ } else {
5688
+ remaining = remaining - leftCount - 1;
5689
+ current = current.right;
5690
+ }
5691
+ }
5692
+ return void 0;
5693
+ }
5694
+ /**
5695
+ * (Protected) Finds the node at position k in tree order (recursive).
5696
+ * @remarks Time O(log n), Space O(log n) call stack
5697
+ */
5698
+ _getByRankRecursive(node, k) {
5699
+ if (!node) return void 0;
5700
+ const leftCount = this.isRealNode(node.left) ? node.left._count : 0;
5701
+ if (k < leftCount) return this._getByRankRecursive(node.left, k);
5702
+ if (k === leftCount) return node;
5703
+ return this._getByRankRecursive(node.right, k - leftCount - 1);
5704
+ }
5705
+ /**
5706
+ * (Protected) Computes the rank of a key iteratively.
5707
+ * @remarks Time O(log n), Space O(1)
5708
+ */
5709
+ _getRankIterative(node, key) {
5710
+ let rank = 0;
5711
+ let current = node;
5712
+ while (this.isRealNode(current)) {
5713
+ const cmp = this._compare(current.key, key);
5714
+ if (cmp > 0) {
5715
+ current = current.left;
5716
+ } else if (cmp < 0) {
5717
+ rank += (this.isRealNode(current.left) ? current.left._count : 0) + 1;
5718
+ current = current.right;
5719
+ } else {
5720
+ rank += this.isRealNode(current.left) ? current.left._count : 0;
5721
+ return rank;
5722
+ }
5723
+ }
5724
+ return rank;
5725
+ }
5726
+ /**
5727
+ * (Protected) Computes the rank of a key recursively.
5728
+ * @remarks Time O(log n), Space O(log n) call stack
5729
+ */
5730
+ _getRankRecursive(node, key) {
5731
+ if (!node) return 0;
5732
+ const cmp = this._compare(node.key, key);
5733
+ if (cmp > 0) {
5734
+ return this._getRankRecursive(node.left, key);
5735
+ } else if (cmp < 0) {
5736
+ return (this.isRealNode(node.left) ? node.left._count : 0) + 1 + this._getRankRecursive(node.right, key);
5737
+ } else {
5738
+ return this.isRealNode(node.left) ? node.left._count : 0;
5739
+ }
5740
+ }
5741
+ /**
5742
+ * (Protected) Finds the in-order successor of a node.
5743
+ * @remarks Time O(log n), Space O(1)
5744
+ */
5745
+ _next(node) {
5746
+ if (this.isRealNode(node.right)) {
5747
+ let current2 = node.right;
5748
+ while (this.isRealNode(current2.left)) {
5749
+ current2 = current2.left;
5750
+ }
5751
+ return current2;
5752
+ }
5753
+ let current = node;
5754
+ let parent = current.parent;
5755
+ while (parent && current === parent.right) {
5756
+ current = parent;
5757
+ parent = parent.parent;
5758
+ }
5759
+ return parent;
5760
+ }
5444
5761
  _setRoot(v) {
5445
5762
  if (v) v.parent = void 0;
5446
5763
  this._root = v;
@@ -5487,21 +5804,28 @@ var BST = class extends BinaryTree {
5487
5804
  while (x.left !== void 0 && x.left !== null) x = x.left;
5488
5805
  return x;
5489
5806
  }, "minNode");
5807
+ let countUpdateStart;
5490
5808
  if (node.left === void 0) {
5809
+ countUpdateStart = node.parent;
5491
5810
  transplant(node, node.right);
5492
5811
  } else if (node.right === void 0) {
5812
+ countUpdateStart = node.parent;
5493
5813
  transplant(node, node.left);
5494
5814
  } else {
5495
5815
  const succ = minNode(node.right);
5496
5816
  if (succ.parent !== node) {
5817
+ countUpdateStart = succ.parent;
5497
5818
  transplant(succ, succ.right);
5498
5819
  succ.right = node.right;
5499
5820
  if (succ.right) succ.right.parent = succ;
5821
+ } else {
5822
+ countUpdateStart = succ;
5500
5823
  }
5501
5824
  transplant(node, succ);
5502
5825
  succ.left = node.left;
5503
5826
  if (succ.left) succ.left.parent = succ;
5504
5827
  }
5828
+ this._updateCountAlongPath(countUpdateStart);
5505
5829
  this._size = Math.max(0, this._size - 1);
5506
5830
  return true;
5507
5831
  }
@@ -5788,6 +6112,18 @@ var AVLTree = class extends BST {
5788
6112
 
5789
6113
 
5790
6114
 
6115
+
6116
+
6117
+
6118
+
6119
+
6120
+
6121
+
6122
+
6123
+
6124
+
6125
+
6126
+
5791
6127
 
5792
6128
 
5793
6129
 
@@ -5906,6 +6242,15 @@ var AVLTree = class extends BST {
5906
6242
 
5907
6243
 
5908
6244
 
6245
+
6246
+
6247
+
6248
+
6249
+
6250
+
6251
+
6252
+
6253
+
5909
6254
 
5910
6255
 
5911
6256
 
@@ -5986,6 +6331,12 @@ var AVLTree = class extends BST {
5986
6331
 
5987
6332
 
5988
6333
 
6334
+
6335
+
6336
+
6337
+
6338
+
6339
+
5989
6340
 
5990
6341
 
5991
6342
 
@@ -6110,6 +6461,15 @@ var AVLTree = class extends BST {
6110
6461
 
6111
6462
 
6112
6463
 
6464
+
6465
+
6466
+
6467
+
6468
+
6469
+
6470
+
6471
+
6472
+
6113
6473
 
6114
6474
 
6115
6475
 
@@ -6238,6 +6598,8 @@ var AVLTree = class extends BST {
6238
6598
  }
6239
6599
  this._updateHeight(A);
6240
6600
  if (B) this._updateHeight(B);
6601
+ this._updateCount(A);
6602
+ if (B) this._updateCount(B);
6241
6603
  }
6242
6604
  /**
6243
6605
  * (Protected) Performs a Left-Right (LR) double rotation.
@@ -6283,6 +6645,9 @@ var AVLTree = class extends BST {
6283
6645
  this._updateHeight(A);
6284
6646
  if (B) this._updateHeight(B);
6285
6647
  if (C) this._updateHeight(C);
6648
+ this._updateCount(A);
6649
+ if (B) this._updateCount(B);
6650
+ if (C) this._updateCount(C);
6286
6651
  }
6287
6652
  /**
6288
6653
  * (Protected) Performs a Right-Right (RR) rotation (a single left rotation).
@@ -6317,6 +6682,8 @@ var AVLTree = class extends BST {
6317
6682
  }
6318
6683
  this._updateHeight(A);
6319
6684
  if (B) this._updateHeight(B);
6685
+ this._updateCount(A);
6686
+ if (B) this._updateCount(B);
6320
6687
  }
6321
6688
  /**
6322
6689
  * (Protected) Performs a Right-Left (RL) double rotation.
@@ -6360,6 +6727,9 @@ var AVLTree = class extends BST {
6360
6727
  this._updateHeight(A);
6361
6728
  if (B) this._updateHeight(B);
6362
6729
  if (C) this._updateHeight(C);
6730
+ this._updateCount(A);
6731
+ if (B) this._updateCount(B);
6732
+ if (C) this._updateCount(C);
6363
6733
  }
6364
6734
  /**
6365
6735
  * (Protected) Traverses up the tree from the specified node, updating heights and performing rotations as needed.
@@ -6374,6 +6744,7 @@ var AVLTree = class extends BST {
6374
6744
  const A = path[i];
6375
6745
  if (A) {
6376
6746
  this._updateHeight(A);
6747
+ this._updateCount(A);
6377
6748
  switch (this._balanceFactor(A)) {
6378
6749
  case -2:
6379
6750
  if (A && A.left) {