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
@@ -61,6 +61,60 @@ 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 DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
97
+ DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
98
+ DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
99
+ return DFSOperation2;
100
+ })(DFSOperation || {});
101
+ var _Range = class _Range {
102
+ constructor(low, high, includeLow = true, includeHigh = true) {
103
+ this.low = low;
104
+ this.high = high;
105
+ this.includeLow = includeLow;
106
+ this.includeHigh = includeHigh;
107
+ }
108
+ // Determine whether a key is within the range
109
+ isInRange(key, comparator) {
110
+ const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
111
+ const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
112
+ return lowCheck && highCheck;
113
+ }
114
+ };
115
+ __name(_Range, "Range");
116
+ var Range = _Range;
117
+
64
118
  // src/data-structures/base/iterable-element-base.ts
65
119
  var _IterableElementBase = class _IterableElementBase {
66
120
  /**
@@ -83,7 +137,7 @@ var _IterableElementBase = class _IterableElementBase {
83
137
  if (options) {
84
138
  const { toElementFn } = options;
85
139
  if (typeof toElementFn === "function") this._toElementFn = toElementFn;
86
- else if (toElementFn) throw new TypeError("toElementFn must be a function type");
140
+ else if (toElementFn) raise(TypeError, "toElementFn must be a function type");
87
141
  }
88
142
  }
89
143
  /**
@@ -239,7 +293,7 @@ var _IterableElementBase = class _IterableElementBase {
239
293
  acc = initialValue;
240
294
  } else {
241
295
  const first = iter.next();
242
- if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
296
+ if (first.done) raise(TypeError, "Reduce of empty structure with no initial value");
243
297
  acc = first.value;
244
298
  index = 1;
245
299
  }
@@ -475,54 +529,6 @@ var _LinearBase = class _LinearBase extends IterableElementBase {
475
529
  __name(_LinearBase, "LinearBase");
476
530
  var LinearBase = _LinearBase;
477
531
 
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 DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
505
- DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
506
- DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
507
- return DFSOperation2;
508
- })(DFSOperation || {});
509
- var _Range = class _Range {
510
- constructor(low, high, includeLow = true, includeHigh = true) {
511
- this.low = low;
512
- this.high = high;
513
- this.includeLow = includeLow;
514
- this.includeHigh = includeHigh;
515
- }
516
- // Determine whether a key is within the range
517
- isInRange(key, comparator) {
518
- const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
519
- const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
520
- return lowCheck && highCheck;
521
- }
522
- };
523
- __name(_Range, "Range");
524
- var Range = _Range;
525
-
526
532
  // src/data-structures/base/iterable-entry-base.ts
527
533
  var _IterableEntryBase = class _IterableEntryBase {
528
534
  /**
@@ -789,6 +795,9 @@ var _Queue = class _Queue extends LinearBase {
789
795
 
790
796
 
791
797
 
798
+
799
+
800
+
792
801
 
793
802
 
794
803
 
@@ -836,6 +845,9 @@ var _Queue = class _Queue extends LinearBase {
836
845
 
837
846
 
838
847
 
848
+
849
+
850
+
839
851
 
840
852
 
841
853
 
@@ -899,6 +911,9 @@ var _Queue = class _Queue extends LinearBase {
899
911
 
900
912
 
901
913
 
914
+
915
+
916
+
902
917
 
903
918
 
904
919
 
@@ -958,6 +973,9 @@ var _Queue = class _Queue extends LinearBase {
958
973
 
959
974
 
960
975
 
976
+
977
+
978
+
961
979
 
962
980
 
963
981
 
@@ -1024,6 +1042,9 @@ var _Queue = class _Queue extends LinearBase {
1024
1042
 
1025
1043
 
1026
1044
 
1045
+
1046
+
1047
+
1027
1048
 
1028
1049
 
1029
1050
 
@@ -1080,6 +1101,9 @@ var _Queue = class _Queue extends LinearBase {
1080
1101
 
1081
1102
 
1082
1103
 
1104
+
1105
+
1106
+
1083
1107
 
1084
1108
 
1085
1109
 
@@ -1129,6 +1153,9 @@ var _Queue = class _Queue extends LinearBase {
1129
1153
 
1130
1154
 
1131
1155
 
1156
+
1157
+
1158
+
1132
1159
 
1133
1160
 
1134
1161
 
@@ -1219,6 +1246,9 @@ var _Queue = class _Queue extends LinearBase {
1219
1246
 
1220
1247
 
1221
1248
 
1249
+
1250
+
1251
+
1222
1252
 
1223
1253
 
1224
1254
 
@@ -1262,6 +1292,9 @@ var _Queue = class _Queue extends LinearBase {
1262
1292
 
1263
1293
 
1264
1294
 
1295
+
1296
+
1297
+
1265
1298
 
1266
1299
 
1267
1300
 
@@ -1328,6 +1361,9 @@ var _Queue = class _Queue extends LinearBase {
1328
1361
 
1329
1362
 
1330
1363
 
1364
+
1365
+
1366
+
1331
1367
 
1332
1368
 
1333
1369
 
@@ -1378,6 +1414,9 @@ var _Queue = class _Queue extends LinearBase {
1378
1414
 
1379
1415
 
1380
1416
 
1417
+
1418
+
1419
+
1381
1420
 
1382
1421
 
1383
1422
 
@@ -1432,6 +1471,9 @@ var _Queue = class _Queue extends LinearBase {
1432
1471
 
1433
1472
 
1434
1473
 
1474
+
1475
+
1476
+
1435
1477
 
1436
1478
 
1437
1479
 
@@ -1704,7 +1746,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1704
1746
  if (isMapMode !== void 0) this._isMapMode = isMapMode;
1705
1747
  if (isDuplicate !== void 0) this._isDuplicate = isDuplicate;
1706
1748
  if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
1707
- else if (toEntryFn) throw new TypeError(ERR.notAFunction("toEntryFn", "BinaryTree"));
1749
+ else if (toEntryFn) raise(TypeError, ERR.notAFunction("toEntryFn", "BinaryTree"));
1708
1750
  }
1709
1751
  if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
1710
1752
  }
@@ -1937,6 +1979,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1937
1979
 
1938
1980
 
1939
1981
 
1982
+
1983
+
1984
+
1940
1985
 
1941
1986
 
1942
1987
 
@@ -1988,6 +2033,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1988
2033
 
1989
2034
 
1990
2035
 
2036
+
2037
+
2038
+
1991
2039
 
1992
2040
 
1993
2041
 
@@ -2091,6 +2139,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2091
2139
 
2092
2140
 
2093
2141
 
2142
+
2143
+
2144
+
2094
2145
 
2095
2146
 
2096
2147
 
@@ -2130,6 +2181,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2130
2181
 
2131
2182
 
2132
2183
 
2184
+
2185
+
2186
+
2133
2187
 
2134
2188
 
2135
2189
 
@@ -2190,6 +2244,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2190
2244
 
2191
2245
 
2192
2246
 
2247
+
2248
+
2249
+
2193
2250
 
2194
2251
 
2195
2252
 
@@ -2249,6 +2306,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2249
2306
 
2250
2307
 
2251
2308
 
2309
+
2310
+
2311
+
2252
2312
 
2253
2313
 
2254
2314
 
@@ -2386,6 +2446,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2386
2446
 
2387
2447
 
2388
2448
 
2449
+
2450
+
2451
+
2389
2452
 
2390
2453
 
2391
2454
 
@@ -2441,6 +2504,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2441
2504
 
2442
2505
 
2443
2506
 
2507
+
2508
+
2509
+
2444
2510
 
2445
2511
 
2446
2512
 
@@ -2499,6 +2565,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2499
2565
 
2500
2566
 
2501
2567
 
2568
+
2569
+
2570
+
2502
2571
 
2503
2572
 
2504
2573
 
@@ -2544,6 +2613,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2544
2613
 
2545
2614
 
2546
2615
 
2616
+
2617
+
2618
+
2547
2619
 
2548
2620
 
2549
2621
 
@@ -2598,6 +2670,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2598
2670
 
2599
2671
 
2600
2672
 
2673
+
2674
+
2675
+
2601
2676
 
2602
2677
 
2603
2678
 
@@ -2679,6 +2754,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2679
2754
 
2680
2755
 
2681
2756
 
2757
+
2758
+
2759
+
2682
2760
 
2683
2761
 
2684
2762
 
@@ -2737,6 +2815,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2737
2815
 
2738
2816
 
2739
2817
 
2818
+
2819
+
2820
+
2740
2821
 
2741
2822
 
2742
2823
 
@@ -3211,6 +3292,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
3211
3292
 
3212
3293
 
3213
3294
 
3295
+
3296
+
3297
+
3214
3298
 
3215
3299
 
3216
3300
 
@@ -3260,6 +3344,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
3260
3344
 
3261
3345
 
3262
3346
 
3347
+
3348
+
3349
+
3263
3350
 
3264
3351
 
3265
3352
 
@@ -3313,6 +3400,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
3313
3400
 
3314
3401
 
3315
3402
 
3403
+
3404
+
3405
+
3316
3406
 
3317
3407
 
3318
3408
 
@@ -3391,6 +3481,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
3391
3481
 
3392
3482
 
3393
3483
 
3484
+
3485
+
3486
+
3394
3487
 
3395
3488
 
3396
3489
 
@@ -4013,6 +4106,7 @@ var _BST = class _BST extends BinaryTree {
4013
4106
  constructor(keysNodesEntriesOrRaws = [], options) {
4014
4107
  super([], options);
4015
4108
  __publicField(this, "_root");
4109
+ __publicField(this, "_enableOrderStatistic", false);
4016
4110
  /**
4017
4111
  * The comparator function used to determine the order of keys in the tree.
4018
4112
 
@@ -4025,6 +4119,9 @@ var _BST = class _BST extends BinaryTree {
4025
4119
  } else {
4026
4120
  this._comparator = this._createDefaultComparator();
4027
4121
  }
4122
+ if (options.enableOrderStatistic) {
4123
+ this._enableOrderStatistic = true;
4124
+ }
4028
4125
  } else {
4029
4126
  this._comparator = this._createDefaultComparator();
4030
4127
  }
@@ -4194,6 +4291,12 @@ var _BST = class _BST extends BinaryTree {
4194
4291
 
4195
4292
 
4196
4293
 
4294
+
4295
+
4296
+
4297
+
4298
+
4299
+
4197
4300
 
4198
4301
 
4199
4302
 
@@ -4356,6 +4459,85 @@ var _BST = class _BST extends BinaryTree {
4356
4459
  const searchRange = range instanceof Range ? range : new Range(range[0], range[1]);
4357
4460
  return this.search(searchRange, false, callback, startNode, iterationType);
4358
4461
  }
4462
+ getByRank(k, callback = this._DEFAULT_NODE_CALLBACK, iterationType = this.iterationType) {
4463
+ if (!this._enableOrderStatistic) {
4464
+ raise(Error, ERR.orderStatisticNotEnabled("getByRank"));
4465
+ }
4466
+ if (k < 0 || k >= this._size) return void 0;
4467
+ let actualCallback = void 0;
4468
+ let actualIterationType = this.iterationType;
4469
+ if (typeof callback === "string") {
4470
+ actualIterationType = callback;
4471
+ } else if (callback) {
4472
+ actualCallback = callback;
4473
+ if (iterationType) {
4474
+ actualIterationType = iterationType;
4475
+ }
4476
+ }
4477
+ const node = actualIterationType === "RECURSIVE" ? this._getByRankRecursive(this._root, k) : this._getByRankIterative(this._root, k);
4478
+ if (!node) return void 0;
4479
+ return actualCallback ? actualCallback(node) : node.key;
4480
+ }
4481
+ getRank(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
4482
+ var _a;
4483
+ if (!this._enableOrderStatistic) {
4484
+ raise(Error, ERR.orderStatisticNotEnabled("getRank"));
4485
+ }
4486
+ if (!this._root || this._size === 0) return -1;
4487
+ let actualIterationType = this.iterationType;
4488
+ if (iterationType) actualIterationType = iterationType;
4489
+ let key;
4490
+ if (typeof keyNodeEntryOrPredicate === "function") {
4491
+ const results = this.search(keyNodeEntryOrPredicate, true);
4492
+ if (results.length === 0 || results[0] === void 0) return -1;
4493
+ key = results[0];
4494
+ } else if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
4495
+ return -1;
4496
+ } else if (this.isNode(keyNodeEntryOrPredicate)) {
4497
+ key = keyNodeEntryOrPredicate.key;
4498
+ } else if (Array.isArray(keyNodeEntryOrPredicate)) {
4499
+ key = (_a = keyNodeEntryOrPredicate[0]) != null ? _a : void 0;
4500
+ if (key === void 0 || key === null) return -1;
4501
+ } else {
4502
+ key = keyNodeEntryOrPredicate;
4503
+ }
4504
+ if (key === void 0) return -1;
4505
+ return actualIterationType === "RECURSIVE" ? this._getRankRecursive(this._root, key) : this._getRankIterative(this._root, key);
4506
+ }
4507
+ rangeByRank(start, end, callback = this._DEFAULT_NODE_CALLBACK, iterationType = this.iterationType) {
4508
+ if (!this._enableOrderStatistic) {
4509
+ raise(Error, ERR.orderStatisticNotEnabled("rangeByRank"));
4510
+ }
4511
+ if (this._size === 0) return [];
4512
+ const lo = Math.max(0, start);
4513
+ const hi = Math.min(this._size - 1, end);
4514
+ if (lo > hi) return [];
4515
+ let actualCallback = void 0;
4516
+ let actualIterationType = this.iterationType;
4517
+ if (typeof callback === "string") {
4518
+ actualIterationType = callback;
4519
+ } else if (callback) {
4520
+ actualCallback = callback;
4521
+ if (iterationType) {
4522
+ actualIterationType = iterationType;
4523
+ }
4524
+ }
4525
+ const results = [];
4526
+ const count = hi - lo + 1;
4527
+ const startNode = actualIterationType === "RECURSIVE" ? this._getByRankRecursive(this._root, lo) : this._getByRankIterative(this._root, lo);
4528
+ if (!startNode) return [];
4529
+ let collected = 0;
4530
+ const cb = actualCallback != null ? actualCallback : this._DEFAULT_NODE_CALLBACK;
4531
+ let current = startNode;
4532
+ while (current && collected < count) {
4533
+ results.push(cb(current));
4534
+ collected++;
4535
+ if (collected < count) {
4536
+ current = this._next(current);
4537
+ }
4538
+ }
4539
+ return results;
4540
+ }
4359
4541
  /**
4360
4542
  * Adds a new node to the BST based on key comparison.
4361
4543
  * @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
@@ -4441,6 +4623,15 @@ var _BST = class _BST extends BinaryTree {
4441
4623
 
4442
4624
 
4443
4625
 
4626
+
4627
+
4628
+
4629
+
4630
+
4631
+
4632
+
4633
+
4634
+
4444
4635
 
4445
4636
 
4446
4637
 
@@ -4465,6 +4656,7 @@ var _BST = class _BST extends BinaryTree {
4465
4656
  this._setRoot(newNode);
4466
4657
  if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
4467
4658
  this._size++;
4659
+ this._updateCount(newNode);
4468
4660
  return true;
4469
4661
  }
4470
4662
  let current = this._root;
@@ -4478,6 +4670,7 @@ var _BST = class _BST extends BinaryTree {
4478
4670
  current.left = newNode;
4479
4671
  if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
4480
4672
  this._size++;
4673
+ this._updateCountAlongPath(newNode);
4481
4674
  return true;
4482
4675
  }
4483
4676
  if (current.left !== null) current = current.left;
@@ -4486,6 +4679,7 @@ var _BST = class _BST extends BinaryTree {
4486
4679
  current.right = newNode;
4487
4680
  if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
4488
4681
  this._size++;
4682
+ this._updateCountAlongPath(newNode);
4489
4683
  return true;
4490
4684
  }
4491
4685
  if (current.right !== null) current = current.right;
@@ -4550,6 +4744,12 @@ var _BST = class _BST extends BinaryTree {
4550
4744
 
4551
4745
 
4552
4746
 
4747
+
4748
+
4749
+
4750
+
4751
+
4752
+
4553
4753
 
4554
4754
 
4555
4755
 
@@ -4837,6 +5037,9 @@ var _BST = class _BST extends BinaryTree {
4837
5037
 
4838
5038
 
4839
5039
 
5040
+
5041
+
5042
+
4840
5043
 
4841
5044
 
4842
5045
 
@@ -4903,6 +5106,9 @@ var _BST = class _BST extends BinaryTree {
4903
5106
 
4904
5107
 
4905
5108
 
5109
+
5110
+
5111
+
4906
5112
 
4907
5113
 
4908
5114
 
@@ -5016,6 +5222,12 @@ var _BST = class _BST extends BinaryTree {
5016
5222
 
5017
5223
 
5018
5224
 
5225
+
5226
+
5227
+
5228
+
5229
+
5230
+
5019
5231
 
5020
5232
 
5021
5233
 
@@ -5097,13 +5309,11 @@ var _BST = class _BST extends BinaryTree {
5097
5309
  if (a instanceof Date && b instanceof Date) {
5098
5310
  const ta = a.getTime();
5099
5311
  const tb = b.getTime();
5100
- if (Number.isNaN(ta) || Number.isNaN(tb)) throw new TypeError(ERR.invalidDate("BST"));
5312
+ if (Number.isNaN(ta) || Number.isNaN(tb)) raise(TypeError, ERR.invalidDate("BST"));
5101
5313
  return ta > tb ? 1 : ta < tb ? -1 : 0;
5102
5314
  }
5103
5315
  if (typeof a === "object" || typeof b === "object") {
5104
- throw new TypeError(
5105
- ERR.comparatorRequired("BST")
5106
- );
5316
+ raise(TypeError, ERR.comparatorRequired("BST"));
5107
5317
  }
5108
5318
  return 0;
5109
5319
  };
@@ -5425,7 +5635,8 @@ var _BST = class _BST extends BinaryTree {
5425
5635
  _snapshotOptions() {
5426
5636
  return {
5427
5637
  ...super._snapshotOptions(),
5428
- comparator: this._comparator
5638
+ comparator: this._comparator,
5639
+ enableOrderStatistic: this._enableOrderStatistic
5429
5640
  };
5430
5641
  }
5431
5642
  /**
@@ -5447,6 +5658,113 @@ var _BST = class _BST extends BinaryTree {
5447
5658
  *
5448
5659
  * @param v - The node to set as root.
5449
5660
  */
5661
+ /**
5662
+ * (Protected) Recalculates the subtree count for a single node.
5663
+ * @remarks Time O(1). Only active when enableOrderStatistic is true.
5664
+ */
5665
+ _updateCount(node) {
5666
+ if (!this._enableOrderStatistic) return;
5667
+ node._count = 1 + (this.isRealNode(node.left) ? node.left._count : 0) + (this.isRealNode(node.right) ? node.right._count : 0);
5668
+ }
5669
+ /**
5670
+ * (Protected) Updates subtree counts from a node up to the root.
5671
+ * @remarks Time O(log n). Only active when enableOrderStatistic is true.
5672
+ */
5673
+ _updateCountAlongPath(node) {
5674
+ if (!this._enableOrderStatistic) return;
5675
+ let current = node;
5676
+ while (current) {
5677
+ this._updateCount(current);
5678
+ current = current.parent;
5679
+ }
5680
+ }
5681
+ /**
5682
+ * (Protected) Finds the node at position k in tree order (iterative).
5683
+ * @remarks Time O(log n), Space O(1)
5684
+ */
5685
+ _getByRankIterative(node, k) {
5686
+ let current = node;
5687
+ let remaining = k;
5688
+ while (current) {
5689
+ const leftCount = this.isRealNode(current.left) ? current.left._count : 0;
5690
+ if (remaining < leftCount) {
5691
+ current = current.left;
5692
+ } else if (remaining === leftCount) {
5693
+ return current;
5694
+ } else {
5695
+ remaining = remaining - leftCount - 1;
5696
+ current = current.right;
5697
+ }
5698
+ }
5699
+ return void 0;
5700
+ }
5701
+ /**
5702
+ * (Protected) Finds the node at position k in tree order (recursive).
5703
+ * @remarks Time O(log n), Space O(log n) call stack
5704
+ */
5705
+ _getByRankRecursive(node, k) {
5706
+ if (!node) return void 0;
5707
+ const leftCount = this.isRealNode(node.left) ? node.left._count : 0;
5708
+ if (k < leftCount) return this._getByRankRecursive(node.left, k);
5709
+ if (k === leftCount) return node;
5710
+ return this._getByRankRecursive(node.right, k - leftCount - 1);
5711
+ }
5712
+ /**
5713
+ * (Protected) Computes the rank of a key iteratively.
5714
+ * @remarks Time O(log n), Space O(1)
5715
+ */
5716
+ _getRankIterative(node, key) {
5717
+ let rank = 0;
5718
+ let current = node;
5719
+ while (this.isRealNode(current)) {
5720
+ const cmp = this._compare(current.key, key);
5721
+ if (cmp > 0) {
5722
+ current = current.left;
5723
+ } else if (cmp < 0) {
5724
+ rank += (this.isRealNode(current.left) ? current.left._count : 0) + 1;
5725
+ current = current.right;
5726
+ } else {
5727
+ rank += this.isRealNode(current.left) ? current.left._count : 0;
5728
+ return rank;
5729
+ }
5730
+ }
5731
+ return rank;
5732
+ }
5733
+ /**
5734
+ * (Protected) Computes the rank of a key recursively.
5735
+ * @remarks Time O(log n), Space O(log n) call stack
5736
+ */
5737
+ _getRankRecursive(node, key) {
5738
+ if (!node) return 0;
5739
+ const cmp = this._compare(node.key, key);
5740
+ if (cmp > 0) {
5741
+ return this._getRankRecursive(node.left, key);
5742
+ } else if (cmp < 0) {
5743
+ return (this.isRealNode(node.left) ? node.left._count : 0) + 1 + this._getRankRecursive(node.right, key);
5744
+ } else {
5745
+ return this.isRealNode(node.left) ? node.left._count : 0;
5746
+ }
5747
+ }
5748
+ /**
5749
+ * (Protected) Finds the in-order successor of a node.
5750
+ * @remarks Time O(log n), Space O(1)
5751
+ */
5752
+ _next(node) {
5753
+ if (this.isRealNode(node.right)) {
5754
+ let current2 = node.right;
5755
+ while (this.isRealNode(current2.left)) {
5756
+ current2 = current2.left;
5757
+ }
5758
+ return current2;
5759
+ }
5760
+ let current = node;
5761
+ let parent = current.parent;
5762
+ while (parent && current === parent.right) {
5763
+ current = parent;
5764
+ parent = parent.parent;
5765
+ }
5766
+ return parent;
5767
+ }
5450
5768
  _setRoot(v) {
5451
5769
  if (v) v.parent = void 0;
5452
5770
  this._root = v;
@@ -5493,21 +5811,28 @@ var _BST = class _BST extends BinaryTree {
5493
5811
  while (x.left !== void 0 && x.left !== null) x = x.left;
5494
5812
  return x;
5495
5813
  }, "minNode");
5814
+ let countUpdateStart;
5496
5815
  if (node.left === void 0) {
5816
+ countUpdateStart = node.parent;
5497
5817
  transplant(node, node.right);
5498
5818
  } else if (node.right === void 0) {
5819
+ countUpdateStart = node.parent;
5499
5820
  transplant(node, node.left);
5500
5821
  } else {
5501
5822
  const succ = minNode(node.right);
5502
5823
  if (succ.parent !== node) {
5824
+ countUpdateStart = succ.parent;
5503
5825
  transplant(succ, succ.right);
5504
5826
  succ.right = node.right;
5505
5827
  if (succ.right) succ.right.parent = succ;
5828
+ } else {
5829
+ countUpdateStart = succ;
5506
5830
  }
5507
5831
  transplant(node, succ);
5508
5832
  succ.left = node.left;
5509
5833
  if (succ.left) succ.left.parent = succ;
5510
5834
  }
5835
+ this._updateCountAlongPath(countUpdateStart);
5511
5836
  this._size = Math.max(0, this._size - 1);
5512
5837
  return true;
5513
5838
  }
@@ -5529,5 +5854,6 @@ exports.BinaryTreeNode = BinaryTreeNode;
5529
5854
  exports.DFSOperation = DFSOperation;
5530
5855
  exports.ERR = ERR;
5531
5856
  exports.Range = Range;
5857
+ exports.raise = raise;
5532
5858
  //# sourceMappingURL=index.cjs.map
5533
5859
  //# sourceMappingURL=index.cjs.map