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
@@ -59,6 +59,60 @@ 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 DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
95
+ DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
96
+ DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
97
+ return DFSOperation2;
98
+ })(DFSOperation || {});
99
+ var _Range = class _Range {
100
+ constructor(low, high, includeLow = true, includeHigh = true) {
101
+ this.low = low;
102
+ this.high = high;
103
+ this.includeLow = includeLow;
104
+ this.includeHigh = includeHigh;
105
+ }
106
+ // Determine whether a key is within the range
107
+ isInRange(key, comparator) {
108
+ const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
109
+ const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
110
+ return lowCheck && highCheck;
111
+ }
112
+ };
113
+ __name(_Range, "Range");
114
+ var Range = _Range;
115
+
62
116
  // src/data-structures/base/iterable-element-base.ts
63
117
  var _IterableElementBase = class _IterableElementBase {
64
118
  /**
@@ -81,7 +135,7 @@ var _IterableElementBase = class _IterableElementBase {
81
135
  if (options) {
82
136
  const { toElementFn } = options;
83
137
  if (typeof toElementFn === "function") this._toElementFn = toElementFn;
84
- else if (toElementFn) throw new TypeError("toElementFn must be a function type");
138
+ else if (toElementFn) raise(TypeError, "toElementFn must be a function type");
85
139
  }
86
140
  }
87
141
  /**
@@ -237,7 +291,7 @@ var _IterableElementBase = class _IterableElementBase {
237
291
  acc = initialValue;
238
292
  } else {
239
293
  const first = iter.next();
240
- if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
294
+ if (first.done) raise(TypeError, "Reduce of empty structure with no initial value");
241
295
  acc = first.value;
242
296
  index = 1;
243
297
  }
@@ -473,54 +527,6 @@ var _LinearBase = class _LinearBase extends IterableElementBase {
473
527
  __name(_LinearBase, "LinearBase");
474
528
  var LinearBase = _LinearBase;
475
529
 
476
- // src/common/error.ts
477
- var ERR = {
478
- // Range / index
479
- indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
480
- invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
481
- // Type / argument
482
- invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
483
- comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
484
- invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
485
- notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
486
- invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
487
- invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
488
- invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
489
- reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
490
- callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
491
- // State / operation
492
- invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
493
- // Matrix
494
- matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
495
- matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
496
- matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
497
- matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
498
- matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
499
- };
500
-
501
- // src/common/index.ts
502
- var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
503
- DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
504
- DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
505
- return DFSOperation2;
506
- })(DFSOperation || {});
507
- var _Range = class _Range {
508
- constructor(low, high, includeLow = true, includeHigh = true) {
509
- this.low = low;
510
- this.high = high;
511
- this.includeLow = includeLow;
512
- this.includeHigh = includeHigh;
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
- __name(_Range, "Range");
522
- var Range = _Range;
523
-
524
530
  // src/data-structures/base/iterable-entry-base.ts
525
531
  var _IterableEntryBase = class _IterableEntryBase {
526
532
  /**
@@ -787,6 +793,9 @@ var _Queue = class _Queue extends LinearBase {
787
793
 
788
794
 
789
795
 
796
+
797
+
798
+
790
799
 
791
800
 
792
801
 
@@ -834,6 +843,9 @@ var _Queue = class _Queue extends LinearBase {
834
843
 
835
844
 
836
845
 
846
+
847
+
848
+
837
849
 
838
850
 
839
851
 
@@ -897,6 +909,9 @@ var _Queue = class _Queue extends LinearBase {
897
909
 
898
910
 
899
911
 
912
+
913
+
914
+
900
915
 
901
916
 
902
917
 
@@ -956,6 +971,9 @@ var _Queue = class _Queue extends LinearBase {
956
971
 
957
972
 
958
973
 
974
+
975
+
976
+
959
977
 
960
978
 
961
979
 
@@ -1022,6 +1040,9 @@ var _Queue = class _Queue extends LinearBase {
1022
1040
 
1023
1041
 
1024
1042
 
1043
+
1044
+
1045
+
1025
1046
 
1026
1047
 
1027
1048
 
@@ -1078,6 +1099,9 @@ var _Queue = class _Queue extends LinearBase {
1078
1099
 
1079
1100
 
1080
1101
 
1102
+
1103
+
1104
+
1081
1105
 
1082
1106
 
1083
1107
 
@@ -1127,6 +1151,9 @@ var _Queue = class _Queue extends LinearBase {
1127
1151
 
1128
1152
 
1129
1153
 
1154
+
1155
+
1156
+
1130
1157
 
1131
1158
 
1132
1159
 
@@ -1217,6 +1244,9 @@ var _Queue = class _Queue extends LinearBase {
1217
1244
 
1218
1245
 
1219
1246
 
1247
+
1248
+
1249
+
1220
1250
 
1221
1251
 
1222
1252
 
@@ -1260,6 +1290,9 @@ var _Queue = class _Queue extends LinearBase {
1260
1290
 
1261
1291
 
1262
1292
 
1293
+
1294
+
1295
+
1263
1296
 
1264
1297
 
1265
1298
 
@@ -1326,6 +1359,9 @@ var _Queue = class _Queue extends LinearBase {
1326
1359
 
1327
1360
 
1328
1361
 
1362
+
1363
+
1364
+
1329
1365
 
1330
1366
 
1331
1367
 
@@ -1376,6 +1412,9 @@ var _Queue = class _Queue extends LinearBase {
1376
1412
 
1377
1413
 
1378
1414
 
1415
+
1416
+
1417
+
1379
1418
 
1380
1419
 
1381
1420
 
@@ -1430,6 +1469,9 @@ var _Queue = class _Queue extends LinearBase {
1430
1469
 
1431
1470
 
1432
1471
 
1472
+
1473
+
1474
+
1433
1475
 
1434
1476
 
1435
1477
 
@@ -1702,7 +1744,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1702
1744
  if (isMapMode !== void 0) this._isMapMode = isMapMode;
1703
1745
  if (isDuplicate !== void 0) this._isDuplicate = isDuplicate;
1704
1746
  if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
1705
- else if (toEntryFn) throw new TypeError(ERR.notAFunction("toEntryFn", "BinaryTree"));
1747
+ else if (toEntryFn) raise(TypeError, ERR.notAFunction("toEntryFn", "BinaryTree"));
1706
1748
  }
1707
1749
  if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
1708
1750
  }
@@ -1935,6 +1977,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1935
1977
 
1936
1978
 
1937
1979
 
1980
+
1981
+
1982
+
1938
1983
 
1939
1984
 
1940
1985
 
@@ -1986,6 +2031,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1986
2031
 
1987
2032
 
1988
2033
 
2034
+
2035
+
2036
+
1989
2037
 
1990
2038
 
1991
2039
 
@@ -2089,6 +2137,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2089
2137
 
2090
2138
 
2091
2139
 
2140
+
2141
+
2142
+
2092
2143
 
2093
2144
 
2094
2145
 
@@ -2128,6 +2179,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2128
2179
 
2129
2180
 
2130
2181
 
2182
+
2183
+
2184
+
2131
2185
 
2132
2186
 
2133
2187
 
@@ -2188,6 +2242,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2188
2242
 
2189
2243
 
2190
2244
 
2245
+
2246
+
2247
+
2191
2248
 
2192
2249
 
2193
2250
 
@@ -2247,6 +2304,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2247
2304
 
2248
2305
 
2249
2306
 
2307
+
2308
+
2309
+
2250
2310
 
2251
2311
 
2252
2312
 
@@ -2384,6 +2444,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2384
2444
 
2385
2445
 
2386
2446
 
2447
+
2448
+
2449
+
2387
2450
 
2388
2451
 
2389
2452
 
@@ -2439,6 +2502,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2439
2502
 
2440
2503
 
2441
2504
 
2505
+
2506
+
2507
+
2442
2508
 
2443
2509
 
2444
2510
 
@@ -2497,6 +2563,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2497
2563
 
2498
2564
 
2499
2565
 
2566
+
2567
+
2568
+
2500
2569
 
2501
2570
 
2502
2571
 
@@ -2542,6 +2611,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2542
2611
 
2543
2612
 
2544
2613
 
2614
+
2615
+
2616
+
2545
2617
 
2546
2618
 
2547
2619
 
@@ -2596,6 +2668,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2596
2668
 
2597
2669
 
2598
2670
 
2671
+
2672
+
2673
+
2599
2674
 
2600
2675
 
2601
2676
 
@@ -2677,6 +2752,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2677
2752
 
2678
2753
 
2679
2754
 
2755
+
2756
+
2757
+
2680
2758
 
2681
2759
 
2682
2760
 
@@ -2735,6 +2813,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2735
2813
 
2736
2814
 
2737
2815
 
2816
+
2817
+
2818
+
2738
2819
 
2739
2820
 
2740
2821
 
@@ -3209,6 +3290,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
3209
3290
 
3210
3291
 
3211
3292
 
3293
+
3294
+
3295
+
3212
3296
 
3213
3297
 
3214
3298
 
@@ -3258,6 +3342,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
3258
3342
 
3259
3343
 
3260
3344
 
3345
+
3346
+
3347
+
3261
3348
 
3262
3349
 
3263
3350
 
@@ -3311,6 +3398,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
3311
3398
 
3312
3399
 
3313
3400
 
3401
+
3402
+
3403
+
3314
3404
 
3315
3405
 
3316
3406
 
@@ -3389,6 +3479,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
3389
3479
 
3390
3480
 
3391
3481
 
3482
+
3483
+
3484
+
3392
3485
 
3393
3486
 
3394
3487
 
@@ -4011,6 +4104,7 @@ var _BST = class _BST extends BinaryTree {
4011
4104
  constructor(keysNodesEntriesOrRaws = [], options) {
4012
4105
  super([], options);
4013
4106
  __publicField(this, "_root");
4107
+ __publicField(this, "_enableOrderStatistic", false);
4014
4108
  /**
4015
4109
  * The comparator function used to determine the order of keys in the tree.
4016
4110
 
@@ -4023,6 +4117,9 @@ var _BST = class _BST extends BinaryTree {
4023
4117
  } else {
4024
4118
  this._comparator = this._createDefaultComparator();
4025
4119
  }
4120
+ if (options.enableOrderStatistic) {
4121
+ this._enableOrderStatistic = true;
4122
+ }
4026
4123
  } else {
4027
4124
  this._comparator = this._createDefaultComparator();
4028
4125
  }
@@ -4192,6 +4289,12 @@ var _BST = class _BST extends BinaryTree {
4192
4289
 
4193
4290
 
4194
4291
 
4292
+
4293
+
4294
+
4295
+
4296
+
4297
+
4195
4298
 
4196
4299
 
4197
4300
 
@@ -4354,6 +4457,85 @@ var _BST = class _BST extends BinaryTree {
4354
4457
  const searchRange = range instanceof Range ? range : new Range(range[0], range[1]);
4355
4458
  return this.search(searchRange, false, callback, startNode, iterationType);
4356
4459
  }
4460
+ getByRank(k, callback = this._DEFAULT_NODE_CALLBACK, iterationType = this.iterationType) {
4461
+ if (!this._enableOrderStatistic) {
4462
+ raise(Error, ERR.orderStatisticNotEnabled("getByRank"));
4463
+ }
4464
+ if (k < 0 || k >= this._size) return void 0;
4465
+ let actualCallback = void 0;
4466
+ let actualIterationType = this.iterationType;
4467
+ if (typeof callback === "string") {
4468
+ actualIterationType = callback;
4469
+ } else if (callback) {
4470
+ actualCallback = callback;
4471
+ if (iterationType) {
4472
+ actualIterationType = iterationType;
4473
+ }
4474
+ }
4475
+ const node = actualIterationType === "RECURSIVE" ? this._getByRankRecursive(this._root, k) : this._getByRankIterative(this._root, k);
4476
+ if (!node) return void 0;
4477
+ return actualCallback ? actualCallback(node) : node.key;
4478
+ }
4479
+ getRank(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
4480
+ var _a;
4481
+ if (!this._enableOrderStatistic) {
4482
+ raise(Error, ERR.orderStatisticNotEnabled("getRank"));
4483
+ }
4484
+ if (!this._root || this._size === 0) return -1;
4485
+ let actualIterationType = this.iterationType;
4486
+ if (iterationType) actualIterationType = iterationType;
4487
+ let key;
4488
+ if (typeof keyNodeEntryOrPredicate === "function") {
4489
+ const results = this.search(keyNodeEntryOrPredicate, true);
4490
+ if (results.length === 0 || results[0] === void 0) return -1;
4491
+ key = results[0];
4492
+ } else if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
4493
+ return -1;
4494
+ } else if (this.isNode(keyNodeEntryOrPredicate)) {
4495
+ key = keyNodeEntryOrPredicate.key;
4496
+ } else if (Array.isArray(keyNodeEntryOrPredicate)) {
4497
+ key = (_a = keyNodeEntryOrPredicate[0]) != null ? _a : void 0;
4498
+ if (key === void 0 || key === null) return -1;
4499
+ } else {
4500
+ key = keyNodeEntryOrPredicate;
4501
+ }
4502
+ if (key === void 0) return -1;
4503
+ return actualIterationType === "RECURSIVE" ? this._getRankRecursive(this._root, key) : this._getRankIterative(this._root, key);
4504
+ }
4505
+ rangeByRank(start, end, callback = this._DEFAULT_NODE_CALLBACK, iterationType = this.iterationType) {
4506
+ if (!this._enableOrderStatistic) {
4507
+ raise(Error, ERR.orderStatisticNotEnabled("rangeByRank"));
4508
+ }
4509
+ if (this._size === 0) return [];
4510
+ const lo = Math.max(0, start);
4511
+ const hi = Math.min(this._size - 1, end);
4512
+ if (lo > hi) return [];
4513
+ let actualCallback = void 0;
4514
+ let actualIterationType = this.iterationType;
4515
+ if (typeof callback === "string") {
4516
+ actualIterationType = callback;
4517
+ } else if (callback) {
4518
+ actualCallback = callback;
4519
+ if (iterationType) {
4520
+ actualIterationType = iterationType;
4521
+ }
4522
+ }
4523
+ const results = [];
4524
+ const count = hi - lo + 1;
4525
+ const startNode = actualIterationType === "RECURSIVE" ? this._getByRankRecursive(this._root, lo) : this._getByRankIterative(this._root, lo);
4526
+ if (!startNode) return [];
4527
+ let collected = 0;
4528
+ const cb = actualCallback != null ? actualCallback : this._DEFAULT_NODE_CALLBACK;
4529
+ let current = startNode;
4530
+ while (current && collected < count) {
4531
+ results.push(cb(current));
4532
+ collected++;
4533
+ if (collected < count) {
4534
+ current = this._next(current);
4535
+ }
4536
+ }
4537
+ return results;
4538
+ }
4357
4539
  /**
4358
4540
  * Adds a new node to the BST based on key comparison.
4359
4541
  * @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
@@ -4439,6 +4621,15 @@ var _BST = class _BST extends BinaryTree {
4439
4621
 
4440
4622
 
4441
4623
 
4624
+
4625
+
4626
+
4627
+
4628
+
4629
+
4630
+
4631
+
4632
+
4442
4633
 
4443
4634
 
4444
4635
 
@@ -4463,6 +4654,7 @@ var _BST = class _BST extends BinaryTree {
4463
4654
  this._setRoot(newNode);
4464
4655
  if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
4465
4656
  this._size++;
4657
+ this._updateCount(newNode);
4466
4658
  return true;
4467
4659
  }
4468
4660
  let current = this._root;
@@ -4476,6 +4668,7 @@ var _BST = class _BST extends BinaryTree {
4476
4668
  current.left = newNode;
4477
4669
  if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
4478
4670
  this._size++;
4671
+ this._updateCountAlongPath(newNode);
4479
4672
  return true;
4480
4673
  }
4481
4674
  if (current.left !== null) current = current.left;
@@ -4484,6 +4677,7 @@ var _BST = class _BST extends BinaryTree {
4484
4677
  current.right = newNode;
4485
4678
  if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
4486
4679
  this._size++;
4680
+ this._updateCountAlongPath(newNode);
4487
4681
  return true;
4488
4682
  }
4489
4683
  if (current.right !== null) current = current.right;
@@ -4548,6 +4742,12 @@ var _BST = class _BST extends BinaryTree {
4548
4742
 
4549
4743
 
4550
4744
 
4745
+
4746
+
4747
+
4748
+
4749
+
4750
+
4551
4751
 
4552
4752
 
4553
4753
 
@@ -4835,6 +5035,9 @@ var _BST = class _BST extends BinaryTree {
4835
5035
 
4836
5036
 
4837
5037
 
5038
+
5039
+
5040
+
4838
5041
 
4839
5042
 
4840
5043
 
@@ -4901,6 +5104,9 @@ var _BST = class _BST extends BinaryTree {
4901
5104
 
4902
5105
 
4903
5106
 
5107
+
5108
+
5109
+
4904
5110
 
4905
5111
 
4906
5112
 
@@ -5014,6 +5220,12 @@ var _BST = class _BST extends BinaryTree {
5014
5220
 
5015
5221
 
5016
5222
 
5223
+
5224
+
5225
+
5226
+
5227
+
5228
+
5017
5229
 
5018
5230
 
5019
5231
 
@@ -5095,13 +5307,11 @@ var _BST = class _BST extends BinaryTree {
5095
5307
  if (a instanceof Date && b instanceof Date) {
5096
5308
  const ta = a.getTime();
5097
5309
  const tb = b.getTime();
5098
- if (Number.isNaN(ta) || Number.isNaN(tb)) throw new TypeError(ERR.invalidDate("BST"));
5310
+ if (Number.isNaN(ta) || Number.isNaN(tb)) raise(TypeError, ERR.invalidDate("BST"));
5099
5311
  return ta > tb ? 1 : ta < tb ? -1 : 0;
5100
5312
  }
5101
5313
  if (typeof a === "object" || typeof b === "object") {
5102
- throw new TypeError(
5103
- ERR.comparatorRequired("BST")
5104
- );
5314
+ raise(TypeError, ERR.comparatorRequired("BST"));
5105
5315
  }
5106
5316
  return 0;
5107
5317
  };
@@ -5423,7 +5633,8 @@ var _BST = class _BST extends BinaryTree {
5423
5633
  _snapshotOptions() {
5424
5634
  return {
5425
5635
  ...super._snapshotOptions(),
5426
- comparator: this._comparator
5636
+ comparator: this._comparator,
5637
+ enableOrderStatistic: this._enableOrderStatistic
5427
5638
  };
5428
5639
  }
5429
5640
  /**
@@ -5445,6 +5656,113 @@ var _BST = class _BST extends BinaryTree {
5445
5656
  *
5446
5657
  * @param v - The node to set as root.
5447
5658
  */
5659
+ /**
5660
+ * (Protected) Recalculates the subtree count for a single node.
5661
+ * @remarks Time O(1). Only active when enableOrderStatistic is true.
5662
+ */
5663
+ _updateCount(node) {
5664
+ if (!this._enableOrderStatistic) return;
5665
+ node._count = 1 + (this.isRealNode(node.left) ? node.left._count : 0) + (this.isRealNode(node.right) ? node.right._count : 0);
5666
+ }
5667
+ /**
5668
+ * (Protected) Updates subtree counts from a node up to the root.
5669
+ * @remarks Time O(log n). Only active when enableOrderStatistic is true.
5670
+ */
5671
+ _updateCountAlongPath(node) {
5672
+ if (!this._enableOrderStatistic) return;
5673
+ let current = node;
5674
+ while (current) {
5675
+ this._updateCount(current);
5676
+ current = current.parent;
5677
+ }
5678
+ }
5679
+ /**
5680
+ * (Protected) Finds the node at position k in tree order (iterative).
5681
+ * @remarks Time O(log n), Space O(1)
5682
+ */
5683
+ _getByRankIterative(node, k) {
5684
+ let current = node;
5685
+ let remaining = k;
5686
+ while (current) {
5687
+ const leftCount = this.isRealNode(current.left) ? current.left._count : 0;
5688
+ if (remaining < leftCount) {
5689
+ current = current.left;
5690
+ } else if (remaining === leftCount) {
5691
+ return current;
5692
+ } else {
5693
+ remaining = remaining - leftCount - 1;
5694
+ current = current.right;
5695
+ }
5696
+ }
5697
+ return void 0;
5698
+ }
5699
+ /**
5700
+ * (Protected) Finds the node at position k in tree order (recursive).
5701
+ * @remarks Time O(log n), Space O(log n) call stack
5702
+ */
5703
+ _getByRankRecursive(node, k) {
5704
+ if (!node) return void 0;
5705
+ const leftCount = this.isRealNode(node.left) ? node.left._count : 0;
5706
+ if (k < leftCount) return this._getByRankRecursive(node.left, k);
5707
+ if (k === leftCount) return node;
5708
+ return this._getByRankRecursive(node.right, k - leftCount - 1);
5709
+ }
5710
+ /**
5711
+ * (Protected) Computes the rank of a key iteratively.
5712
+ * @remarks Time O(log n), Space O(1)
5713
+ */
5714
+ _getRankIterative(node, key) {
5715
+ let rank = 0;
5716
+ let current = node;
5717
+ while (this.isRealNode(current)) {
5718
+ const cmp = this._compare(current.key, key);
5719
+ if (cmp > 0) {
5720
+ current = current.left;
5721
+ } else if (cmp < 0) {
5722
+ rank += (this.isRealNode(current.left) ? current.left._count : 0) + 1;
5723
+ current = current.right;
5724
+ } else {
5725
+ rank += this.isRealNode(current.left) ? current.left._count : 0;
5726
+ return rank;
5727
+ }
5728
+ }
5729
+ return rank;
5730
+ }
5731
+ /**
5732
+ * (Protected) Computes the rank of a key recursively.
5733
+ * @remarks Time O(log n), Space O(log n) call stack
5734
+ */
5735
+ _getRankRecursive(node, key) {
5736
+ if (!node) return 0;
5737
+ const cmp = this._compare(node.key, key);
5738
+ if (cmp > 0) {
5739
+ return this._getRankRecursive(node.left, key);
5740
+ } else if (cmp < 0) {
5741
+ return (this.isRealNode(node.left) ? node.left._count : 0) + 1 + this._getRankRecursive(node.right, key);
5742
+ } else {
5743
+ return this.isRealNode(node.left) ? node.left._count : 0;
5744
+ }
5745
+ }
5746
+ /**
5747
+ * (Protected) Finds the in-order successor of a node.
5748
+ * @remarks Time O(log n), Space O(1)
5749
+ */
5750
+ _next(node) {
5751
+ if (this.isRealNode(node.right)) {
5752
+ let current2 = node.right;
5753
+ while (this.isRealNode(current2.left)) {
5754
+ current2 = current2.left;
5755
+ }
5756
+ return current2;
5757
+ }
5758
+ let current = node;
5759
+ let parent = current.parent;
5760
+ while (parent && current === parent.right) {
5761
+ current = parent;
5762
+ parent = parent.parent;
5763
+ }
5764
+ return parent;
5765
+ }
5448
5766
  _setRoot(v) {
5449
5767
  if (v) v.parent = void 0;
5450
5768
  this._root = v;
@@ -5491,21 +5809,28 @@ var _BST = class _BST extends BinaryTree {
5491
5809
  while (x.left !== void 0 && x.left !== null) x = x.left;
5492
5810
  return x;
5493
5811
  }, "minNode");
5812
+ let countUpdateStart;
5494
5813
  if (node.left === void 0) {
5814
+ countUpdateStart = node.parent;
5495
5815
  transplant(node, node.right);
5496
5816
  } else if (node.right === void 0) {
5817
+ countUpdateStart = node.parent;
5497
5818
  transplant(node, node.left);
5498
5819
  } else {
5499
5820
  const succ = minNode(node.right);
5500
5821
  if (succ.parent !== node) {
5822
+ countUpdateStart = succ.parent;
5501
5823
  transplant(succ, succ.right);
5502
5824
  succ.right = node.right;
5503
5825
  if (succ.right) succ.right.parent = succ;
5826
+ } else {
5827
+ countUpdateStart = succ;
5504
5828
  }
5505
5829
  transplant(node, succ);
5506
5830
  succ.left = node.left;
5507
5831
  if (succ.left) succ.left.parent = succ;
5508
5832
  }
5833
+ this._updateCountAlongPath(countUpdateStart);
5509
5834
  this._size = Math.max(0, this._size - 1);
5510
5835
  return true;
5511
5836
  }
@@ -5520,6 +5845,6 @@ var BST = _BST;
5520
5845
  * @license MIT License
5521
5846
  */
5522
5847
 
5523
- export { BST, BSTNode, BinaryTree, BinaryTreeNode, DFSOperation, ERR, Range };
5848
+ export { BST, BSTNode, BinaryTree, BinaryTreeNode, DFSOperation, ERR, Range, raise };
5524
5849
  //# sourceMappingURL=index.mjs.map
5525
5850
  //# sourceMappingURL=index.mjs.map