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
@@ -61,6 +61,55 @@ function makeTrampoline(fn) {
61
61
  }
62
62
  __name(makeTrampoline, "makeTrampoline");
63
63
 
64
+ // src/common/error.ts
65
+ function raise(ErrorClass, message) {
66
+ throw new ErrorClass(message);
67
+ }
68
+ __name(raise, "raise");
69
+ var ERR = {
70
+ // Range / index
71
+ indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
72
+ invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
73
+ // Type / argument
74
+ invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
75
+ comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
76
+ invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
77
+ notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
78
+ invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
79
+ invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
80
+ invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
81
+ reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
82
+ callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
83
+ // State / operation
84
+ invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
85
+ // Matrix
86
+ matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
87
+ matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
88
+ matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
89
+ matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
90
+ matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch"),
91
+ // Order statistic
92
+ orderStatisticNotEnabled: /* @__PURE__ */ __name((method, ctx) => `${ctx ? ctx + ": " : ""}${method}() requires enableOrderStatistic: true.`, "orderStatisticNotEnabled")
93
+ };
94
+
95
+ // src/common/index.ts
96
+ var _Range = class _Range {
97
+ constructor(low, high, includeLow = true, includeHigh = true) {
98
+ this.low = low;
99
+ this.high = high;
100
+ this.includeLow = includeLow;
101
+ this.includeHigh = includeHigh;
102
+ }
103
+ // Determine whether a key is within the range
104
+ isInRange(key, comparator) {
105
+ const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
106
+ const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
107
+ return lowCheck && highCheck;
108
+ }
109
+ };
110
+ __name(_Range, "Range");
111
+ var Range = _Range;
112
+
64
113
  // src/data-structures/base/iterable-element-base.ts
65
114
  var _IterableElementBase = class _IterableElementBase {
66
115
  /**
@@ -83,7 +132,7 @@ var _IterableElementBase = class _IterableElementBase {
83
132
  if (options) {
84
133
  const { toElementFn } = options;
85
134
  if (typeof toElementFn === "function") this._toElementFn = toElementFn;
86
- else if (toElementFn) throw new TypeError("toElementFn must be a function type");
135
+ else if (toElementFn) raise(TypeError, "toElementFn must be a function type");
87
136
  }
88
137
  }
89
138
  /**
@@ -239,7 +288,7 @@ var _IterableElementBase = class _IterableElementBase {
239
288
  acc = initialValue;
240
289
  } else {
241
290
  const first = iter.next();
242
- if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
291
+ if (first.done) raise(TypeError, "Reduce of empty structure with no initial value");
243
292
  acc = first.value;
244
293
  index = 1;
245
294
  }
@@ -475,49 +524,6 @@ var _LinearBase = class _LinearBase extends IterableElementBase {
475
524
  __name(_LinearBase, "LinearBase");
476
525
  var LinearBase = _LinearBase;
477
526
 
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 _Range {
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
- // Determine whether a key is within the range
512
- isInRange(key, comparator) {
513
- const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
514
- const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
515
- return lowCheck && highCheck;
516
- }
517
- };
518
- __name(_Range, "Range");
519
- var Range = _Range;
520
-
521
527
  // src/data-structures/base/iterable-entry-base.ts
522
528
  var _IterableEntryBase = class _IterableEntryBase {
523
529
  /**
@@ -784,6 +790,9 @@ var _Queue = class _Queue extends LinearBase {
784
790
 
785
791
 
786
792
 
793
+
794
+
795
+
787
796
 
788
797
 
789
798
 
@@ -831,6 +840,9 @@ var _Queue = class _Queue extends LinearBase {
831
840
 
832
841
 
833
842
 
843
+
844
+
845
+
834
846
 
835
847
 
836
848
 
@@ -894,6 +906,9 @@ var _Queue = class _Queue extends LinearBase {
894
906
 
895
907
 
896
908
 
909
+
910
+
911
+
897
912
 
898
913
 
899
914
 
@@ -953,6 +968,9 @@ var _Queue = class _Queue extends LinearBase {
953
968
 
954
969
 
955
970
 
971
+
972
+
973
+
956
974
 
957
975
 
958
976
 
@@ -1019,6 +1037,9 @@ var _Queue = class _Queue extends LinearBase {
1019
1037
 
1020
1038
 
1021
1039
 
1040
+
1041
+
1042
+
1022
1043
 
1023
1044
 
1024
1045
 
@@ -1075,6 +1096,9 @@ var _Queue = class _Queue extends LinearBase {
1075
1096
 
1076
1097
 
1077
1098
 
1099
+
1100
+
1101
+
1078
1102
 
1079
1103
 
1080
1104
 
@@ -1124,6 +1148,9 @@ var _Queue = class _Queue extends LinearBase {
1124
1148
 
1125
1149
 
1126
1150
 
1151
+
1152
+
1153
+
1127
1154
 
1128
1155
 
1129
1156
 
@@ -1214,6 +1241,9 @@ var _Queue = class _Queue extends LinearBase {
1214
1241
 
1215
1242
 
1216
1243
 
1244
+
1245
+
1246
+
1217
1247
 
1218
1248
 
1219
1249
 
@@ -1257,6 +1287,9 @@ var _Queue = class _Queue extends LinearBase {
1257
1287
 
1258
1288
 
1259
1289
 
1290
+
1291
+
1292
+
1260
1293
 
1261
1294
 
1262
1295
 
@@ -1323,6 +1356,9 @@ var _Queue = class _Queue extends LinearBase {
1323
1356
 
1324
1357
 
1325
1358
 
1359
+
1360
+
1361
+
1326
1362
 
1327
1363
 
1328
1364
 
@@ -1373,6 +1409,9 @@ var _Queue = class _Queue extends LinearBase {
1373
1409
 
1374
1410
 
1375
1411
 
1412
+
1413
+
1414
+
1376
1415
 
1377
1416
 
1378
1417
 
@@ -1427,6 +1466,9 @@ var _Queue = class _Queue extends LinearBase {
1427
1466
 
1428
1467
 
1429
1468
 
1469
+
1470
+
1471
+
1430
1472
 
1431
1473
 
1432
1474
 
@@ -1699,7 +1741,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1699
1741
  if (isMapMode !== void 0) this._isMapMode = isMapMode;
1700
1742
  if (isDuplicate !== void 0) this._isDuplicate = isDuplicate;
1701
1743
  if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
1702
- else if (toEntryFn) throw new TypeError(ERR.notAFunction("toEntryFn", "BinaryTree"));
1744
+ else if (toEntryFn) raise(TypeError, ERR.notAFunction("toEntryFn", "BinaryTree"));
1703
1745
  }
1704
1746
  if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
1705
1747
  }
@@ -1932,6 +1974,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1932
1974
 
1933
1975
 
1934
1976
 
1977
+
1978
+
1979
+
1935
1980
 
1936
1981
 
1937
1982
 
@@ -1983,6 +2028,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1983
2028
 
1984
2029
 
1985
2030
 
2031
+
2032
+
2033
+
1986
2034
 
1987
2035
 
1988
2036
 
@@ -2086,6 +2134,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2086
2134
 
2087
2135
 
2088
2136
 
2137
+
2138
+
2139
+
2089
2140
 
2090
2141
 
2091
2142
 
@@ -2125,6 +2176,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2125
2176
 
2126
2177
 
2127
2178
 
2179
+
2180
+
2181
+
2128
2182
 
2129
2183
 
2130
2184
 
@@ -2185,6 +2239,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2185
2239
 
2186
2240
 
2187
2241
 
2242
+
2243
+
2244
+
2188
2245
 
2189
2246
 
2190
2247
 
@@ -2244,6 +2301,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2244
2301
 
2245
2302
 
2246
2303
 
2304
+
2305
+
2306
+
2247
2307
 
2248
2308
 
2249
2309
 
@@ -2381,6 +2441,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2381
2441
 
2382
2442
 
2383
2443
 
2444
+
2445
+
2446
+
2384
2447
 
2385
2448
 
2386
2449
 
@@ -2436,6 +2499,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2436
2499
 
2437
2500
 
2438
2501
 
2502
+
2503
+
2504
+
2439
2505
 
2440
2506
 
2441
2507
 
@@ -2494,6 +2560,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2494
2560
 
2495
2561
 
2496
2562
 
2563
+
2564
+
2565
+
2497
2566
 
2498
2567
 
2499
2568
 
@@ -2539,6 +2608,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2539
2608
 
2540
2609
 
2541
2610
 
2611
+
2612
+
2613
+
2542
2614
 
2543
2615
 
2544
2616
 
@@ -2593,6 +2665,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2593
2665
 
2594
2666
 
2595
2667
 
2668
+
2669
+
2670
+
2596
2671
 
2597
2672
 
2598
2673
 
@@ -2674,6 +2749,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2674
2749
 
2675
2750
 
2676
2751
 
2752
+
2753
+
2754
+
2677
2755
 
2678
2756
 
2679
2757
 
@@ -2732,6 +2810,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2732
2810
 
2733
2811
 
2734
2812
 
2813
+
2814
+
2815
+
2735
2816
 
2736
2817
 
2737
2818
 
@@ -3206,6 +3287,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
3206
3287
 
3207
3288
 
3208
3289
 
3290
+
3291
+
3292
+
3209
3293
 
3210
3294
 
3211
3295
 
@@ -3255,6 +3339,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
3255
3339
 
3256
3340
 
3257
3341
 
3342
+
3343
+
3344
+
3258
3345
 
3259
3346
 
3260
3347
 
@@ -3308,6 +3395,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
3308
3395
 
3309
3396
 
3310
3397
 
3398
+
3399
+
3400
+
3311
3401
 
3312
3402
 
3313
3403
 
@@ -3386,6 +3476,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
3386
3476
 
3387
3477
 
3388
3478
 
3479
+
3480
+
3481
+
3389
3482
 
3390
3483
 
3391
3484
 
@@ -4008,6 +4101,7 @@ var _BST = class _BST extends BinaryTree {
4008
4101
  constructor(keysNodesEntriesOrRaws = [], options) {
4009
4102
  super([], options);
4010
4103
  __publicField(this, "_root");
4104
+ __publicField(this, "_enableOrderStatistic", false);
4011
4105
  /**
4012
4106
  * The comparator function used to determine the order of keys in the tree.
4013
4107
 
@@ -4020,6 +4114,9 @@ var _BST = class _BST extends BinaryTree {
4020
4114
  } else {
4021
4115
  this._comparator = this._createDefaultComparator();
4022
4116
  }
4117
+ if (options.enableOrderStatistic) {
4118
+ this._enableOrderStatistic = true;
4119
+ }
4023
4120
  } else {
4024
4121
  this._comparator = this._createDefaultComparator();
4025
4122
  }
@@ -4189,6 +4286,12 @@ var _BST = class _BST extends BinaryTree {
4189
4286
 
4190
4287
 
4191
4288
 
4289
+
4290
+
4291
+
4292
+
4293
+
4294
+
4192
4295
 
4193
4296
 
4194
4297
 
@@ -4351,6 +4454,85 @@ var _BST = class _BST extends BinaryTree {
4351
4454
  const searchRange = range instanceof Range ? range : new Range(range[0], range[1]);
4352
4455
  return this.search(searchRange, false, callback, startNode, iterationType);
4353
4456
  }
4457
+ getByRank(k, callback = this._DEFAULT_NODE_CALLBACK, iterationType = this.iterationType) {
4458
+ if (!this._enableOrderStatistic) {
4459
+ raise(Error, ERR.orderStatisticNotEnabled("getByRank"));
4460
+ }
4461
+ if (k < 0 || k >= this._size) return void 0;
4462
+ let actualCallback = void 0;
4463
+ let actualIterationType = this.iterationType;
4464
+ if (typeof callback === "string") {
4465
+ actualIterationType = callback;
4466
+ } else if (callback) {
4467
+ actualCallback = callback;
4468
+ if (iterationType) {
4469
+ actualIterationType = iterationType;
4470
+ }
4471
+ }
4472
+ const node = actualIterationType === "RECURSIVE" ? this._getByRankRecursive(this._root, k) : this._getByRankIterative(this._root, k);
4473
+ if (!node) return void 0;
4474
+ return actualCallback ? actualCallback(node) : node.key;
4475
+ }
4476
+ getRank(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
4477
+ var _a;
4478
+ if (!this._enableOrderStatistic) {
4479
+ raise(Error, ERR.orderStatisticNotEnabled("getRank"));
4480
+ }
4481
+ if (!this._root || this._size === 0) return -1;
4482
+ let actualIterationType = this.iterationType;
4483
+ if (iterationType) actualIterationType = iterationType;
4484
+ let key;
4485
+ if (typeof keyNodeEntryOrPredicate === "function") {
4486
+ const results = this.search(keyNodeEntryOrPredicate, true);
4487
+ if (results.length === 0 || results[0] === void 0) return -1;
4488
+ key = results[0];
4489
+ } else if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
4490
+ return -1;
4491
+ } else if (this.isNode(keyNodeEntryOrPredicate)) {
4492
+ key = keyNodeEntryOrPredicate.key;
4493
+ } else if (Array.isArray(keyNodeEntryOrPredicate)) {
4494
+ key = (_a = keyNodeEntryOrPredicate[0]) != null ? _a : void 0;
4495
+ if (key === void 0 || key === null) return -1;
4496
+ } else {
4497
+ key = keyNodeEntryOrPredicate;
4498
+ }
4499
+ if (key === void 0) return -1;
4500
+ return actualIterationType === "RECURSIVE" ? this._getRankRecursive(this._root, key) : this._getRankIterative(this._root, key);
4501
+ }
4502
+ rangeByRank(start, end, callback = this._DEFAULT_NODE_CALLBACK, iterationType = this.iterationType) {
4503
+ if (!this._enableOrderStatistic) {
4504
+ raise(Error, ERR.orderStatisticNotEnabled("rangeByRank"));
4505
+ }
4506
+ if (this._size === 0) return [];
4507
+ const lo = Math.max(0, start);
4508
+ const hi = Math.min(this._size - 1, end);
4509
+ if (lo > hi) return [];
4510
+ let actualCallback = void 0;
4511
+ let actualIterationType = this.iterationType;
4512
+ if (typeof callback === "string") {
4513
+ actualIterationType = callback;
4514
+ } else if (callback) {
4515
+ actualCallback = callback;
4516
+ if (iterationType) {
4517
+ actualIterationType = iterationType;
4518
+ }
4519
+ }
4520
+ const results = [];
4521
+ const count = hi - lo + 1;
4522
+ const startNode = actualIterationType === "RECURSIVE" ? this._getByRankRecursive(this._root, lo) : this._getByRankIterative(this._root, lo);
4523
+ if (!startNode) return [];
4524
+ let collected = 0;
4525
+ const cb = actualCallback != null ? actualCallback : this._DEFAULT_NODE_CALLBACK;
4526
+ let current = startNode;
4527
+ while (current && collected < count) {
4528
+ results.push(cb(current));
4529
+ collected++;
4530
+ if (collected < count) {
4531
+ current = this._next(current);
4532
+ }
4533
+ }
4534
+ return results;
4535
+ }
4354
4536
  /**
4355
4537
  * Adds a new node to the BST based on key comparison.
4356
4538
  * @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
@@ -4436,6 +4618,15 @@ var _BST = class _BST extends BinaryTree {
4436
4618
 
4437
4619
 
4438
4620
 
4621
+
4622
+
4623
+
4624
+
4625
+
4626
+
4627
+
4628
+
4629
+
4439
4630
 
4440
4631
 
4441
4632
 
@@ -4460,6 +4651,7 @@ var _BST = class _BST extends BinaryTree {
4460
4651
  this._setRoot(newNode);
4461
4652
  if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
4462
4653
  this._size++;
4654
+ this._updateCount(newNode);
4463
4655
  return true;
4464
4656
  }
4465
4657
  let current = this._root;
@@ -4473,6 +4665,7 @@ var _BST = class _BST extends BinaryTree {
4473
4665
  current.left = newNode;
4474
4666
  if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
4475
4667
  this._size++;
4668
+ this._updateCountAlongPath(newNode);
4476
4669
  return true;
4477
4670
  }
4478
4671
  if (current.left !== null) current = current.left;
@@ -4481,6 +4674,7 @@ var _BST = class _BST extends BinaryTree {
4481
4674
  current.right = newNode;
4482
4675
  if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
4483
4676
  this._size++;
4677
+ this._updateCountAlongPath(newNode);
4484
4678
  return true;
4485
4679
  }
4486
4680
  if (current.right !== null) current = current.right;
@@ -4545,6 +4739,12 @@ var _BST = class _BST extends BinaryTree {
4545
4739
 
4546
4740
 
4547
4741
 
4742
+
4743
+
4744
+
4745
+
4746
+
4747
+
4548
4748
 
4549
4749
 
4550
4750
 
@@ -4832,6 +5032,9 @@ var _BST = class _BST extends BinaryTree {
4832
5032
 
4833
5033
 
4834
5034
 
5035
+
5036
+
5037
+
4835
5038
 
4836
5039
 
4837
5040
 
@@ -4898,6 +5101,9 @@ var _BST = class _BST extends BinaryTree {
4898
5101
 
4899
5102
 
4900
5103
 
5104
+
5105
+
5106
+
4901
5107
 
4902
5108
 
4903
5109
 
@@ -5011,6 +5217,12 @@ var _BST = class _BST extends BinaryTree {
5011
5217
 
5012
5218
 
5013
5219
 
5220
+
5221
+
5222
+
5223
+
5224
+
5225
+
5014
5226
 
5015
5227
 
5016
5228
 
@@ -5092,13 +5304,11 @@ var _BST = class _BST extends BinaryTree {
5092
5304
  if (a instanceof Date && b instanceof Date) {
5093
5305
  const ta = a.getTime();
5094
5306
  const tb = b.getTime();
5095
- if (Number.isNaN(ta) || Number.isNaN(tb)) throw new TypeError(ERR.invalidDate("BST"));
5307
+ if (Number.isNaN(ta) || Number.isNaN(tb)) raise(TypeError, ERR.invalidDate("BST"));
5096
5308
  return ta > tb ? 1 : ta < tb ? -1 : 0;
5097
5309
  }
5098
5310
  if (typeof a === "object" || typeof b === "object") {
5099
- throw new TypeError(
5100
- ERR.comparatorRequired("BST")
5101
- );
5311
+ raise(TypeError, ERR.comparatorRequired("BST"));
5102
5312
  }
5103
5313
  return 0;
5104
5314
  };
@@ -5420,7 +5630,8 @@ var _BST = class _BST extends BinaryTree {
5420
5630
  _snapshotOptions() {
5421
5631
  return {
5422
5632
  ...super._snapshotOptions(),
5423
- comparator: this._comparator
5633
+ comparator: this._comparator,
5634
+ enableOrderStatistic: this._enableOrderStatistic
5424
5635
  };
5425
5636
  }
5426
5637
  /**
@@ -5442,6 +5653,113 @@ var _BST = class _BST extends BinaryTree {
5442
5653
  *
5443
5654
  * @param v - The node to set as root.
5444
5655
  */
5656
+ /**
5657
+ * (Protected) Recalculates the subtree count for a single node.
5658
+ * @remarks Time O(1). Only active when enableOrderStatistic is true.
5659
+ */
5660
+ _updateCount(node) {
5661
+ if (!this._enableOrderStatistic) return;
5662
+ node._count = 1 + (this.isRealNode(node.left) ? node.left._count : 0) + (this.isRealNode(node.right) ? node.right._count : 0);
5663
+ }
5664
+ /**
5665
+ * (Protected) Updates subtree counts from a node up to the root.
5666
+ * @remarks Time O(log n). Only active when enableOrderStatistic is true.
5667
+ */
5668
+ _updateCountAlongPath(node) {
5669
+ if (!this._enableOrderStatistic) return;
5670
+ let current = node;
5671
+ while (current) {
5672
+ this._updateCount(current);
5673
+ current = current.parent;
5674
+ }
5675
+ }
5676
+ /**
5677
+ * (Protected) Finds the node at position k in tree order (iterative).
5678
+ * @remarks Time O(log n), Space O(1)
5679
+ */
5680
+ _getByRankIterative(node, k) {
5681
+ let current = node;
5682
+ let remaining = k;
5683
+ while (current) {
5684
+ const leftCount = this.isRealNode(current.left) ? current.left._count : 0;
5685
+ if (remaining < leftCount) {
5686
+ current = current.left;
5687
+ } else if (remaining === leftCount) {
5688
+ return current;
5689
+ } else {
5690
+ remaining = remaining - leftCount - 1;
5691
+ current = current.right;
5692
+ }
5693
+ }
5694
+ return void 0;
5695
+ }
5696
+ /**
5697
+ * (Protected) Finds the node at position k in tree order (recursive).
5698
+ * @remarks Time O(log n), Space O(log n) call stack
5699
+ */
5700
+ _getByRankRecursive(node, k) {
5701
+ if (!node) return void 0;
5702
+ const leftCount = this.isRealNode(node.left) ? node.left._count : 0;
5703
+ if (k < leftCount) return this._getByRankRecursive(node.left, k);
5704
+ if (k === leftCount) return node;
5705
+ return this._getByRankRecursive(node.right, k - leftCount - 1);
5706
+ }
5707
+ /**
5708
+ * (Protected) Computes the rank of a key iteratively.
5709
+ * @remarks Time O(log n), Space O(1)
5710
+ */
5711
+ _getRankIterative(node, key) {
5712
+ let rank = 0;
5713
+ let current = node;
5714
+ while (this.isRealNode(current)) {
5715
+ const cmp = this._compare(current.key, key);
5716
+ if (cmp > 0) {
5717
+ current = current.left;
5718
+ } else if (cmp < 0) {
5719
+ rank += (this.isRealNode(current.left) ? current.left._count : 0) + 1;
5720
+ current = current.right;
5721
+ } else {
5722
+ rank += this.isRealNode(current.left) ? current.left._count : 0;
5723
+ return rank;
5724
+ }
5725
+ }
5726
+ return rank;
5727
+ }
5728
+ /**
5729
+ * (Protected) Computes the rank of a key recursively.
5730
+ * @remarks Time O(log n), Space O(log n) call stack
5731
+ */
5732
+ _getRankRecursive(node, key) {
5733
+ if (!node) return 0;
5734
+ const cmp = this._compare(node.key, key);
5735
+ if (cmp > 0) {
5736
+ return this._getRankRecursive(node.left, key);
5737
+ } else if (cmp < 0) {
5738
+ return (this.isRealNode(node.left) ? node.left._count : 0) + 1 + this._getRankRecursive(node.right, key);
5739
+ } else {
5740
+ return this.isRealNode(node.left) ? node.left._count : 0;
5741
+ }
5742
+ }
5743
+ /**
5744
+ * (Protected) Finds the in-order successor of a node.
5745
+ * @remarks Time O(log n), Space O(1)
5746
+ */
5747
+ _next(node) {
5748
+ if (this.isRealNode(node.right)) {
5749
+ let current2 = node.right;
5750
+ while (this.isRealNode(current2.left)) {
5751
+ current2 = current2.left;
5752
+ }
5753
+ return current2;
5754
+ }
5755
+ let current = node;
5756
+ let parent = current.parent;
5757
+ while (parent && current === parent.right) {
5758
+ current = parent;
5759
+ parent = parent.parent;
5760
+ }
5761
+ return parent;
5762
+ }
5445
5763
  _setRoot(v) {
5446
5764
  if (v) v.parent = void 0;
5447
5765
  this._root = v;
@@ -5488,21 +5806,28 @@ var _BST = class _BST extends BinaryTree {
5488
5806
  while (x.left !== void 0 && x.left !== null) x = x.left;
5489
5807
  return x;
5490
5808
  }, "minNode");
5809
+ let countUpdateStart;
5491
5810
  if (node.left === void 0) {
5811
+ countUpdateStart = node.parent;
5492
5812
  transplant(node, node.right);
5493
5813
  } else if (node.right === void 0) {
5814
+ countUpdateStart = node.parent;
5494
5815
  transplant(node, node.left);
5495
5816
  } else {
5496
5817
  const succ = minNode(node.right);
5497
5818
  if (succ.parent !== node) {
5819
+ countUpdateStart = succ.parent;
5498
5820
  transplant(succ, succ.right);
5499
5821
  succ.right = node.right;
5500
5822
  if (succ.right) succ.right.parent = succ;
5823
+ } else {
5824
+ countUpdateStart = succ;
5501
5825
  }
5502
5826
  transplant(node, succ);
5503
5827
  succ.left = node.left;
5504
5828
  if (succ.left) succ.left.parent = succ;
5505
5829
  }
5830
+ this._updateCountAlongPath(countUpdateStart);
5506
5831
  this._size = Math.max(0, this._size - 1);
5507
5832
  return true;
5508
5833
  }
@@ -5787,6 +6112,18 @@ var _AVLTree = class _AVLTree extends BST {
5787
6112
 
5788
6113
 
5789
6114
 
6115
+
6116
+
6117
+
6118
+
6119
+
6120
+
6121
+
6122
+
6123
+
6124
+
6125
+
6126
+
5790
6127
 
5791
6128
 
5792
6129
 
@@ -5905,6 +6242,15 @@ var _AVLTree = class _AVLTree extends BST {
5905
6242
 
5906
6243
 
5907
6244
 
6245
+
6246
+
6247
+
6248
+
6249
+
6250
+
6251
+
6252
+
6253
+
5908
6254
 
5909
6255
 
5910
6256
 
@@ -5985,6 +6331,12 @@ var _AVLTree = class _AVLTree extends BST {
5985
6331
 
5986
6332
 
5987
6333
 
6334
+
6335
+
6336
+
6337
+
6338
+
6339
+
5988
6340
 
5989
6341
 
5990
6342
 
@@ -6109,6 +6461,15 @@ var _AVLTree = class _AVLTree extends BST {
6109
6461
 
6110
6462
 
6111
6463
 
6464
+
6465
+
6466
+
6467
+
6468
+
6469
+
6470
+
6471
+
6472
+
6112
6473
 
6113
6474
 
6114
6475
 
@@ -6237,6 +6598,8 @@ var _AVLTree = class _AVLTree extends BST {
6237
6598
  }
6238
6599
  this._updateHeight(A);
6239
6600
  if (B) this._updateHeight(B);
6601
+ this._updateCount(A);
6602
+ if (B) this._updateCount(B);
6240
6603
  }
6241
6604
  /**
6242
6605
  * (Protected) Performs a Left-Right (LR) double rotation.
@@ -6282,6 +6645,9 @@ var _AVLTree = class _AVLTree extends BST {
6282
6645
  this._updateHeight(A);
6283
6646
  if (B) this._updateHeight(B);
6284
6647
  if (C) this._updateHeight(C);
6648
+ this._updateCount(A);
6649
+ if (B) this._updateCount(B);
6650
+ if (C) this._updateCount(C);
6285
6651
  }
6286
6652
  /**
6287
6653
  * (Protected) Performs a Right-Right (RR) rotation (a single left rotation).
@@ -6316,6 +6682,8 @@ var _AVLTree = class _AVLTree extends BST {
6316
6682
  }
6317
6683
  this._updateHeight(A);
6318
6684
  if (B) this._updateHeight(B);
6685
+ this._updateCount(A);
6686
+ if (B) this._updateCount(B);
6319
6687
  }
6320
6688
  /**
6321
6689
  * (Protected) Performs a Right-Left (RL) double rotation.
@@ -6359,6 +6727,9 @@ var _AVLTree = class _AVLTree extends BST {
6359
6727
  this._updateHeight(A);
6360
6728
  if (B) this._updateHeight(B);
6361
6729
  if (C) this._updateHeight(C);
6730
+ this._updateCount(A);
6731
+ if (B) this._updateCount(B);
6732
+ if (C) this._updateCount(C);
6362
6733
  }
6363
6734
  /**
6364
6735
  * (Protected) Traverses up the tree from the specified node, updating heights and performing rotations as needed.
@@ -6373,6 +6744,7 @@ var _AVLTree = class _AVLTree extends BST {
6373
6744
  const A = path[i];
6374
6745
  if (A) {
6375
6746
  this._updateHeight(A);
6747
+ this._updateCount(A);
6376
6748
  switch (this._balanceFactor(A)) {
6377
6749
  case -2:
6378
6750
  if (A && A.left) {