bst-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 +382 -57
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +382 -56
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +382 -58
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +382 -57
  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/bst-typed.js +380 -55
  38. package/dist/umd/bst-typed.js.map +1 -1
  39. package/dist/umd/bst-typed.min.js +3 -3
  40. package/dist/umd/bst-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
@@ -29,7 +29,8 @@ var bstTyped = (() => {
29
29
  BinaryTreeNode: () => BinaryTreeNode,
30
30
  DFSOperation: () => DFSOperation,
31
31
  ERR: () => ERR,
32
- Range: () => Range
32
+ Range: () => Range,
33
+ raise: () => raise
33
34
  });
34
35
 
35
36
  // src/utils/utils.ts
@@ -83,6 +84,57 @@ var bstTyped = (() => {
83
84
  return (...args) => trampoline(fn(...args));
84
85
  }
85
86
 
87
+ // src/common/error.ts
88
+ function raise(ErrorClass, message) {
89
+ throw new ErrorClass(message);
90
+ }
91
+ var ERR = {
92
+ // Range / index
93
+ indexOutOfRange: (index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`,
94
+ invalidIndex: (ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`,
95
+ // Type / argument
96
+ invalidArgument: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
97
+ comparatorRequired: (ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`,
98
+ invalidKey: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
99
+ notAFunction: (name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`,
100
+ invalidEntry: (ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`,
101
+ invalidNaN: (ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`,
102
+ invalidDate: (ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`,
103
+ reduceEmpty: (ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`,
104
+ callbackReturnType: (expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`,
105
+ // State / operation
106
+ invalidOperation: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
107
+ // Matrix
108
+ matrixDimensionMismatch: (op) => `Matrix: Dimensions must be compatible for ${op}.`,
109
+ matrixSingular: () => "Matrix: Singular matrix, inverse does not exist.",
110
+ matrixNotSquare: () => "Matrix: Must be square for inversion.",
111
+ matrixNotRectangular: () => "Matrix: Must be rectangular for transposition.",
112
+ matrixRowMismatch: (expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`,
113
+ // Order statistic
114
+ orderStatisticNotEnabled: (method, ctx) => `${ctx ? ctx + ": " : ""}${method}() requires enableOrderStatistic: true.`
115
+ };
116
+
117
+ // src/common/index.ts
118
+ var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
119
+ DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
120
+ DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
121
+ return DFSOperation2;
122
+ })(DFSOperation || {});
123
+ var Range = class {
124
+ constructor(low, high, includeLow = true, includeHigh = true) {
125
+ this.low = low;
126
+ this.high = high;
127
+ this.includeLow = includeLow;
128
+ this.includeHigh = includeHigh;
129
+ }
130
+ // Determine whether a key is within the range
131
+ isInRange(key, comparator) {
132
+ const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
133
+ const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
134
+ return lowCheck && highCheck;
135
+ }
136
+ };
137
+
86
138
  // src/data-structures/base/iterable-element-base.ts
87
139
  var IterableElementBase = class {
88
140
  /**
@@ -105,7 +157,7 @@ var bstTyped = (() => {
105
157
  if (options) {
106
158
  const { toElementFn } = options;
107
159
  if (typeof toElementFn === "function") this._toElementFn = toElementFn;
108
- else if (toElementFn) throw new TypeError("toElementFn must be a function type");
160
+ else if (toElementFn) raise(TypeError, "toElementFn must be a function type");
109
161
  }
110
162
  }
111
163
  /**
@@ -261,7 +313,7 @@ var bstTyped = (() => {
261
313
  acc = initialValue;
262
314
  } else {
263
315
  const first = iter.next();
264
- if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
316
+ if (first.done) raise(TypeError, "Reduce of empty structure with no initial value");
265
317
  acc = first.value;
266
318
  index = 1;
267
319
  }
@@ -493,52 +545,6 @@ var bstTyped = (() => {
493
545
  }
494
546
  };
495
547
 
496
- // src/common/error.ts
497
- var ERR = {
498
- // Range / index
499
- indexOutOfRange: (index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`,
500
- invalidIndex: (ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`,
501
- // Type / argument
502
- invalidArgument: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
503
- comparatorRequired: (ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`,
504
- invalidKey: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
505
- notAFunction: (name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`,
506
- invalidEntry: (ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`,
507
- invalidNaN: (ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`,
508
- invalidDate: (ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`,
509
- reduceEmpty: (ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`,
510
- callbackReturnType: (expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`,
511
- // State / operation
512
- invalidOperation: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
513
- // Matrix
514
- matrixDimensionMismatch: (op) => `Matrix: Dimensions must be compatible for ${op}.`,
515
- matrixSingular: () => "Matrix: Singular matrix, inverse does not exist.",
516
- matrixNotSquare: () => "Matrix: Must be square for inversion.",
517
- matrixNotRectangular: () => "Matrix: Must be rectangular for transposition.",
518
- matrixRowMismatch: (expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`
519
- };
520
-
521
- // src/common/index.ts
522
- var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
523
- DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
524
- DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
525
- return DFSOperation2;
526
- })(DFSOperation || {});
527
- var Range = class {
528
- constructor(low, high, includeLow = true, includeHigh = true) {
529
- this.low = low;
530
- this.high = high;
531
- this.includeLow = includeLow;
532
- this.includeHigh = includeHigh;
533
- }
534
- // Determine whether a key is within the range
535
- isInRange(key, comparator) {
536
- const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
537
- const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
538
- return lowCheck && highCheck;
539
- }
540
- };
541
-
542
548
  // src/data-structures/base/iterable-entry-base.ts
543
549
  var IterableEntryBase = class {
544
550
  /**
@@ -803,6 +809,9 @@ var bstTyped = (() => {
803
809
 
804
810
 
805
811
 
812
+
813
+
814
+
806
815
 
807
816
 
808
817
 
@@ -850,6 +859,9 @@ var bstTyped = (() => {
850
859
 
851
860
 
852
861
 
862
+
863
+
864
+
853
865
 
854
866
 
855
867
 
@@ -913,6 +925,9 @@ var bstTyped = (() => {
913
925
 
914
926
 
915
927
 
928
+
929
+
930
+
916
931
 
917
932
 
918
933
 
@@ -972,6 +987,9 @@ var bstTyped = (() => {
972
987
 
973
988
 
974
989
 
990
+
991
+
992
+
975
993
 
976
994
 
977
995
 
@@ -1038,6 +1056,9 @@ var bstTyped = (() => {
1038
1056
 
1039
1057
 
1040
1058
 
1059
+
1060
+
1061
+
1041
1062
 
1042
1063
 
1043
1064
 
@@ -1094,6 +1115,9 @@ var bstTyped = (() => {
1094
1115
 
1095
1116
 
1096
1117
 
1118
+
1119
+
1120
+
1097
1121
 
1098
1122
 
1099
1123
 
@@ -1143,6 +1167,9 @@ var bstTyped = (() => {
1143
1167
 
1144
1168
 
1145
1169
 
1170
+
1171
+
1172
+
1146
1173
 
1147
1174
 
1148
1175
 
@@ -1233,6 +1260,9 @@ var bstTyped = (() => {
1233
1260
 
1234
1261
 
1235
1262
 
1263
+
1264
+
1265
+
1236
1266
 
1237
1267
 
1238
1268
 
@@ -1276,6 +1306,9 @@ var bstTyped = (() => {
1276
1306
 
1277
1307
 
1278
1308
 
1309
+
1310
+
1311
+
1279
1312
 
1280
1313
 
1281
1314
 
@@ -1342,6 +1375,9 @@ var bstTyped = (() => {
1342
1375
 
1343
1376
 
1344
1377
 
1378
+
1379
+
1380
+
1345
1381
 
1346
1382
 
1347
1383
 
@@ -1392,6 +1428,9 @@ var bstTyped = (() => {
1392
1428
 
1393
1429
 
1394
1430
 
1431
+
1432
+
1433
+
1395
1434
 
1396
1435
 
1397
1436
 
@@ -1446,6 +1485,9 @@ var bstTyped = (() => {
1446
1485
 
1447
1486
 
1448
1487
 
1488
+
1489
+
1490
+
1449
1491
 
1450
1492
 
1451
1493
 
@@ -1714,7 +1756,7 @@ var bstTyped = (() => {
1714
1756
  if (isMapMode !== void 0) this._isMapMode = isMapMode;
1715
1757
  if (isDuplicate !== void 0) this._isDuplicate = isDuplicate;
1716
1758
  if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
1717
- else if (toEntryFn) throw new TypeError(ERR.notAFunction("toEntryFn", "BinaryTree"));
1759
+ else if (toEntryFn) raise(TypeError, ERR.notAFunction("toEntryFn", "BinaryTree"));
1718
1760
  }
1719
1761
  if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
1720
1762
  }
@@ -1947,6 +1989,9 @@ var bstTyped = (() => {
1947
1989
 
1948
1990
 
1949
1991
 
1992
+
1993
+
1994
+
1950
1995
 
1951
1996
 
1952
1997
 
@@ -1998,6 +2043,9 @@ var bstTyped = (() => {
1998
2043
 
1999
2044
 
2000
2045
 
2046
+
2047
+
2048
+
2001
2049
 
2002
2050
 
2003
2051
 
@@ -2101,6 +2149,9 @@ var bstTyped = (() => {
2101
2149
 
2102
2150
 
2103
2151
 
2152
+
2153
+
2154
+
2104
2155
 
2105
2156
 
2106
2157
 
@@ -2140,6 +2191,9 @@ var bstTyped = (() => {
2140
2191
 
2141
2192
 
2142
2193
 
2194
+
2195
+
2196
+
2143
2197
 
2144
2198
 
2145
2199
 
@@ -2200,6 +2254,9 @@ var bstTyped = (() => {
2200
2254
 
2201
2255
 
2202
2256
 
2257
+
2258
+
2259
+
2203
2260
 
2204
2261
 
2205
2262
 
@@ -2259,6 +2316,9 @@ var bstTyped = (() => {
2259
2316
 
2260
2317
 
2261
2318
 
2319
+
2320
+
2321
+
2262
2322
 
2263
2323
 
2264
2324
 
@@ -2396,6 +2456,9 @@ var bstTyped = (() => {
2396
2456
 
2397
2457
 
2398
2458
 
2459
+
2460
+
2461
+
2399
2462
 
2400
2463
 
2401
2464
 
@@ -2451,6 +2514,9 @@ var bstTyped = (() => {
2451
2514
 
2452
2515
 
2453
2516
 
2517
+
2518
+
2519
+
2454
2520
 
2455
2521
 
2456
2522
 
@@ -2509,6 +2575,9 @@ var bstTyped = (() => {
2509
2575
 
2510
2576
 
2511
2577
 
2578
+
2579
+
2580
+
2512
2581
 
2513
2582
 
2514
2583
 
@@ -2554,6 +2623,9 @@ var bstTyped = (() => {
2554
2623
 
2555
2624
 
2556
2625
 
2626
+
2627
+
2628
+
2557
2629
 
2558
2630
 
2559
2631
 
@@ -2608,6 +2680,9 @@ var bstTyped = (() => {
2608
2680
 
2609
2681
 
2610
2682
 
2683
+
2684
+
2685
+
2611
2686
 
2612
2687
 
2613
2688
 
@@ -2689,6 +2764,9 @@ var bstTyped = (() => {
2689
2764
 
2690
2765
 
2691
2766
 
2767
+
2768
+
2769
+
2692
2770
 
2693
2771
 
2694
2772
 
@@ -2747,6 +2825,9 @@ var bstTyped = (() => {
2747
2825
 
2748
2826
 
2749
2827
 
2828
+
2829
+
2830
+
2750
2831
 
2751
2832
 
2752
2833
 
@@ -3221,6 +3302,9 @@ var bstTyped = (() => {
3221
3302
 
3222
3303
 
3223
3304
 
3305
+
3306
+
3307
+
3224
3308
 
3225
3309
 
3226
3310
 
@@ -3270,6 +3354,9 @@ var bstTyped = (() => {
3270
3354
 
3271
3355
 
3272
3356
 
3357
+
3358
+
3359
+
3273
3360
 
3274
3361
 
3275
3362
 
@@ -3323,6 +3410,9 @@ var bstTyped = (() => {
3323
3410
 
3324
3411
 
3325
3412
 
3413
+
3414
+
3415
+
3326
3416
 
3327
3417
 
3328
3418
 
@@ -3401,6 +3491,9 @@ var bstTyped = (() => {
3401
3491
 
3402
3492
 
3403
3493
 
3494
+
3495
+
3496
+
3404
3497
 
3405
3498
 
3406
3499
 
@@ -4019,6 +4112,7 @@ var bstTyped = (() => {
4019
4112
  constructor(keysNodesEntriesOrRaws = [], options) {
4020
4113
  super([], options);
4021
4114
  __publicField(this, "_root");
4115
+ __publicField(this, "_enableOrderStatistic", false);
4022
4116
  /**
4023
4117
  * The comparator function used to determine the order of keys in the tree.
4024
4118
 
@@ -4031,6 +4125,9 @@ var bstTyped = (() => {
4031
4125
  } else {
4032
4126
  this._comparator = this._createDefaultComparator();
4033
4127
  }
4128
+ if (options.enableOrderStatistic) {
4129
+ this._enableOrderStatistic = true;
4130
+ }
4034
4131
  } else {
4035
4132
  this._comparator = this._createDefaultComparator();
4036
4133
  }
@@ -4200,6 +4297,12 @@ var bstTyped = (() => {
4200
4297
 
4201
4298
 
4202
4299
 
4300
+
4301
+
4302
+
4303
+
4304
+
4305
+
4203
4306
 
4204
4307
 
4205
4308
 
@@ -4362,6 +4465,85 @@ var bstTyped = (() => {
4362
4465
  const searchRange = range instanceof Range ? range : new Range(range[0], range[1]);
4363
4466
  return this.search(searchRange, false, callback, startNode, iterationType);
4364
4467
  }
4468
+ getByRank(k, callback = this._DEFAULT_NODE_CALLBACK, iterationType = this.iterationType) {
4469
+ if (!this._enableOrderStatistic) {
4470
+ raise(Error, ERR.orderStatisticNotEnabled("getByRank"));
4471
+ }
4472
+ if (k < 0 || k >= this._size) return void 0;
4473
+ let actualCallback = void 0;
4474
+ let actualIterationType = this.iterationType;
4475
+ if (typeof callback === "string") {
4476
+ actualIterationType = callback;
4477
+ } else if (callback) {
4478
+ actualCallback = callback;
4479
+ if (iterationType) {
4480
+ actualIterationType = iterationType;
4481
+ }
4482
+ }
4483
+ const node = actualIterationType === "RECURSIVE" ? this._getByRankRecursive(this._root, k) : this._getByRankIterative(this._root, k);
4484
+ if (!node) return void 0;
4485
+ return actualCallback ? actualCallback(node) : node.key;
4486
+ }
4487
+ getRank(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
4488
+ var _a;
4489
+ if (!this._enableOrderStatistic) {
4490
+ raise(Error, ERR.orderStatisticNotEnabled("getRank"));
4491
+ }
4492
+ if (!this._root || this._size === 0) return -1;
4493
+ let actualIterationType = this.iterationType;
4494
+ if (iterationType) actualIterationType = iterationType;
4495
+ let key;
4496
+ if (typeof keyNodeEntryOrPredicate === "function") {
4497
+ const results = this.search(keyNodeEntryOrPredicate, true);
4498
+ if (results.length === 0 || results[0] === void 0) return -1;
4499
+ key = results[0];
4500
+ } else if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
4501
+ return -1;
4502
+ } else if (this.isNode(keyNodeEntryOrPredicate)) {
4503
+ key = keyNodeEntryOrPredicate.key;
4504
+ } else if (Array.isArray(keyNodeEntryOrPredicate)) {
4505
+ key = (_a = keyNodeEntryOrPredicate[0]) != null ? _a : void 0;
4506
+ if (key === void 0 || key === null) return -1;
4507
+ } else {
4508
+ key = keyNodeEntryOrPredicate;
4509
+ }
4510
+ if (key === void 0) return -1;
4511
+ return actualIterationType === "RECURSIVE" ? this._getRankRecursive(this._root, key) : this._getRankIterative(this._root, key);
4512
+ }
4513
+ rangeByRank(start, end, callback = this._DEFAULT_NODE_CALLBACK, iterationType = this.iterationType) {
4514
+ if (!this._enableOrderStatistic) {
4515
+ raise(Error, ERR.orderStatisticNotEnabled("rangeByRank"));
4516
+ }
4517
+ if (this._size === 0) return [];
4518
+ const lo = Math.max(0, start);
4519
+ const hi = Math.min(this._size - 1, end);
4520
+ if (lo > hi) return [];
4521
+ let actualCallback = void 0;
4522
+ let actualIterationType = this.iterationType;
4523
+ if (typeof callback === "string") {
4524
+ actualIterationType = callback;
4525
+ } else if (callback) {
4526
+ actualCallback = callback;
4527
+ if (iterationType) {
4528
+ actualIterationType = iterationType;
4529
+ }
4530
+ }
4531
+ const results = [];
4532
+ const count = hi - lo + 1;
4533
+ const startNode = actualIterationType === "RECURSIVE" ? this._getByRankRecursive(this._root, lo) : this._getByRankIterative(this._root, lo);
4534
+ if (!startNode) return [];
4535
+ let collected = 0;
4536
+ const cb = actualCallback != null ? actualCallback : this._DEFAULT_NODE_CALLBACK;
4537
+ let current = startNode;
4538
+ while (current && collected < count) {
4539
+ results.push(cb(current));
4540
+ collected++;
4541
+ if (collected < count) {
4542
+ current = this._next(current);
4543
+ }
4544
+ }
4545
+ return results;
4546
+ }
4365
4547
  /**
4366
4548
  * Adds a new node to the BST based on key comparison.
4367
4549
  * @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
@@ -4447,6 +4629,15 @@ var bstTyped = (() => {
4447
4629
 
4448
4630
 
4449
4631
 
4632
+
4633
+
4634
+
4635
+
4636
+
4637
+
4638
+
4639
+
4640
+
4450
4641
 
4451
4642
 
4452
4643
 
@@ -4471,6 +4662,7 @@ var bstTyped = (() => {
4471
4662
  this._setRoot(newNode);
4472
4663
  if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
4473
4664
  this._size++;
4665
+ this._updateCount(newNode);
4474
4666
  return true;
4475
4667
  }
4476
4668
  let current = this._root;
@@ -4484,6 +4676,7 @@ var bstTyped = (() => {
4484
4676
  current.left = newNode;
4485
4677
  if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
4486
4678
  this._size++;
4679
+ this._updateCountAlongPath(newNode);
4487
4680
  return true;
4488
4681
  }
4489
4682
  if (current.left !== null) current = current.left;
@@ -4492,6 +4685,7 @@ var bstTyped = (() => {
4492
4685
  current.right = newNode;
4493
4686
  if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
4494
4687
  this._size++;
4688
+ this._updateCountAlongPath(newNode);
4495
4689
  return true;
4496
4690
  }
4497
4691
  if (current.right !== null) current = current.right;
@@ -4556,6 +4750,12 @@ var bstTyped = (() => {
4556
4750
 
4557
4751
 
4558
4752
 
4753
+
4754
+
4755
+
4756
+
4757
+
4758
+
4559
4759
 
4560
4760
 
4561
4761
 
@@ -4843,6 +5043,9 @@ var bstTyped = (() => {
4843
5043
 
4844
5044
 
4845
5045
 
5046
+
5047
+
5048
+
4846
5049
 
4847
5050
 
4848
5051
 
@@ -4909,6 +5112,9 @@ var bstTyped = (() => {
4909
5112
 
4910
5113
 
4911
5114
 
5115
+
5116
+
5117
+
4912
5118
 
4913
5119
 
4914
5120
 
@@ -5022,6 +5228,12 @@ var bstTyped = (() => {
5022
5228
 
5023
5229
 
5024
5230
 
5231
+
5232
+
5233
+
5234
+
5235
+
5236
+
5025
5237
 
5026
5238
 
5027
5239
 
@@ -5103,13 +5315,11 @@ var bstTyped = (() => {
5103
5315
  if (a instanceof Date && b instanceof Date) {
5104
5316
  const ta = a.getTime();
5105
5317
  const tb = b.getTime();
5106
- if (Number.isNaN(ta) || Number.isNaN(tb)) throw new TypeError(ERR.invalidDate("BST"));
5318
+ if (Number.isNaN(ta) || Number.isNaN(tb)) raise(TypeError, ERR.invalidDate("BST"));
5107
5319
  return ta > tb ? 1 : ta < tb ? -1 : 0;
5108
5320
  }
5109
5321
  if (typeof a === "object" || typeof b === "object") {
5110
- throw new TypeError(
5111
- ERR.comparatorRequired("BST")
5112
- );
5322
+ raise(TypeError, ERR.comparatorRequired("BST"));
5113
5323
  }
5114
5324
  return 0;
5115
5325
  };
@@ -5431,7 +5641,8 @@ var bstTyped = (() => {
5431
5641
  _snapshotOptions() {
5432
5642
  return {
5433
5643
  ...super._snapshotOptions(),
5434
- comparator: this._comparator
5644
+ comparator: this._comparator,
5645
+ enableOrderStatistic: this._enableOrderStatistic
5435
5646
  };
5436
5647
  }
5437
5648
  /**
@@ -5453,6 +5664,113 @@ var bstTyped = (() => {
5453
5664
  *
5454
5665
  * @param v - The node to set as root.
5455
5666
  */
5667
+ /**
5668
+ * (Protected) Recalculates the subtree count for a single node.
5669
+ * @remarks Time O(1). Only active when enableOrderStatistic is true.
5670
+ */
5671
+ _updateCount(node) {
5672
+ if (!this._enableOrderStatistic) return;
5673
+ node._count = 1 + (this.isRealNode(node.left) ? node.left._count : 0) + (this.isRealNode(node.right) ? node.right._count : 0);
5674
+ }
5675
+ /**
5676
+ * (Protected) Updates subtree counts from a node up to the root.
5677
+ * @remarks Time O(log n). Only active when enableOrderStatistic is true.
5678
+ */
5679
+ _updateCountAlongPath(node) {
5680
+ if (!this._enableOrderStatistic) return;
5681
+ let current = node;
5682
+ while (current) {
5683
+ this._updateCount(current);
5684
+ current = current.parent;
5685
+ }
5686
+ }
5687
+ /**
5688
+ * (Protected) Finds the node at position k in tree order (iterative).
5689
+ * @remarks Time O(log n), Space O(1)
5690
+ */
5691
+ _getByRankIterative(node, k) {
5692
+ let current = node;
5693
+ let remaining = k;
5694
+ while (current) {
5695
+ const leftCount = this.isRealNode(current.left) ? current.left._count : 0;
5696
+ if (remaining < leftCount) {
5697
+ current = current.left;
5698
+ } else if (remaining === leftCount) {
5699
+ return current;
5700
+ } else {
5701
+ remaining = remaining - leftCount - 1;
5702
+ current = current.right;
5703
+ }
5704
+ }
5705
+ return void 0;
5706
+ }
5707
+ /**
5708
+ * (Protected) Finds the node at position k in tree order (recursive).
5709
+ * @remarks Time O(log n), Space O(log n) call stack
5710
+ */
5711
+ _getByRankRecursive(node, k) {
5712
+ if (!node) return void 0;
5713
+ const leftCount = this.isRealNode(node.left) ? node.left._count : 0;
5714
+ if (k < leftCount) return this._getByRankRecursive(node.left, k);
5715
+ if (k === leftCount) return node;
5716
+ return this._getByRankRecursive(node.right, k - leftCount - 1);
5717
+ }
5718
+ /**
5719
+ * (Protected) Computes the rank of a key iteratively.
5720
+ * @remarks Time O(log n), Space O(1)
5721
+ */
5722
+ _getRankIterative(node, key) {
5723
+ let rank = 0;
5724
+ let current = node;
5725
+ while (this.isRealNode(current)) {
5726
+ const cmp = this._compare(current.key, key);
5727
+ if (cmp > 0) {
5728
+ current = current.left;
5729
+ } else if (cmp < 0) {
5730
+ rank += (this.isRealNode(current.left) ? current.left._count : 0) + 1;
5731
+ current = current.right;
5732
+ } else {
5733
+ rank += this.isRealNode(current.left) ? current.left._count : 0;
5734
+ return rank;
5735
+ }
5736
+ }
5737
+ return rank;
5738
+ }
5739
+ /**
5740
+ * (Protected) Computes the rank of a key recursively.
5741
+ * @remarks Time O(log n), Space O(log n) call stack
5742
+ */
5743
+ _getRankRecursive(node, key) {
5744
+ if (!node) return 0;
5745
+ const cmp = this._compare(node.key, key);
5746
+ if (cmp > 0) {
5747
+ return this._getRankRecursive(node.left, key);
5748
+ } else if (cmp < 0) {
5749
+ return (this.isRealNode(node.left) ? node.left._count : 0) + 1 + this._getRankRecursive(node.right, key);
5750
+ } else {
5751
+ return this.isRealNode(node.left) ? node.left._count : 0;
5752
+ }
5753
+ }
5754
+ /**
5755
+ * (Protected) Finds the in-order successor of a node.
5756
+ * @remarks Time O(log n), Space O(1)
5757
+ */
5758
+ _next(node) {
5759
+ if (this.isRealNode(node.right)) {
5760
+ let current2 = node.right;
5761
+ while (this.isRealNode(current2.left)) {
5762
+ current2 = current2.left;
5763
+ }
5764
+ return current2;
5765
+ }
5766
+ let current = node;
5767
+ let parent = current.parent;
5768
+ while (parent && current === parent.right) {
5769
+ current = parent;
5770
+ parent = parent.parent;
5771
+ }
5772
+ return parent;
5773
+ }
5456
5774
  _setRoot(v) {
5457
5775
  if (v) v.parent = void 0;
5458
5776
  this._root = v;
@@ -5499,21 +5817,28 @@ var bstTyped = (() => {
5499
5817
  while (x.left !== void 0 && x.left !== null) x = x.left;
5500
5818
  return x;
5501
5819
  };
5820
+ let countUpdateStart;
5502
5821
  if (node.left === void 0) {
5822
+ countUpdateStart = node.parent;
5503
5823
  transplant(node, node.right);
5504
5824
  } else if (node.right === void 0) {
5825
+ countUpdateStart = node.parent;
5505
5826
  transplant(node, node.left);
5506
5827
  } else {
5507
5828
  const succ = minNode(node.right);
5508
5829
  if (succ.parent !== node) {
5830
+ countUpdateStart = succ.parent;
5509
5831
  transplant(succ, succ.right);
5510
5832
  succ.right = node.right;
5511
5833
  if (succ.right) succ.right.parent = succ;
5834
+ } else {
5835
+ countUpdateStart = succ;
5512
5836
  }
5513
5837
  transplant(node, succ);
5514
5838
  succ.left = node.left;
5515
5839
  if (succ.left) succ.left.parent = succ;
5516
5840
  }
5841
+ this._updateCountAlongPath(countUpdateStart);
5517
5842
  this._size = Math.max(0, this._size - 1);
5518
5843
  return true;
5519
5844
  }