bst-typed 2.5.0 → 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 (90) hide show
  1. package/dist/cjs/index.cjs +1222 -57
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +1222 -56
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +1222 -58
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +1222 -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/base/index.d.ts +1 -0
  12. package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
  13. package/dist/types/data-structures/base/linear-base.d.ts +3 -3
  14. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +288 -0
  15. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +336 -0
  16. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +618 -18
  17. package/dist/types/data-structures/binary-tree/bst.d.ts +676 -1
  18. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +456 -0
  19. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +144 -1
  20. package/dist/types/data-structures/binary-tree/tree-map.d.ts +3307 -399
  21. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3285 -360
  22. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2674 -325
  23. package/dist/types/data-structures/binary-tree/tree-set.d.ts +3072 -287
  24. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
  25. package/dist/types/data-structures/graph/directed-graph.d.ts +240 -0
  26. package/dist/types/data-structures/graph/undirected-graph.d.ts +216 -0
  27. package/dist/types/data-structures/hash/hash-map.d.ts +274 -10
  28. package/dist/types/data-structures/heap/heap.d.ts +336 -0
  29. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +411 -3
  30. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +363 -3
  31. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +434 -2
  32. package/dist/types/data-structures/matrix/matrix.d.ts +192 -0
  33. package/dist/types/data-structures/queue/deque.d.ts +364 -4
  34. package/dist/types/data-structures/queue/queue.d.ts +288 -0
  35. package/dist/types/data-structures/stack/stack.d.ts +240 -0
  36. package/dist/types/data-structures/trie/trie.d.ts +292 -4
  37. package/dist/types/interfaces/graph.d.ts +1 -1
  38. package/dist/types/types/common.d.ts +2 -2
  39. package/dist/types/types/data-structures/binary-tree/bst.d.ts +1 -0
  40. package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +5 -0
  41. package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +4 -0
  42. package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +4 -0
  43. package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
  44. package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
  45. package/dist/types/types/utils/validate-type.d.ts +4 -4
  46. package/dist/umd/bst-typed.js +1220 -55
  47. package/dist/umd/bst-typed.js.map +1 -1
  48. package/dist/umd/bst-typed.min.js +3 -3
  49. package/dist/umd/bst-typed.min.js.map +1 -1
  50. package/package.json +2 -2
  51. package/src/common/error.ts +19 -1
  52. package/src/common/index.ts +1 -1
  53. package/src/data-structures/base/index.ts +1 -0
  54. package/src/data-structures/base/iterable-element-base.ts +3 -2
  55. package/src/data-structures/base/iterable-entry-base.ts +8 -8
  56. package/src/data-structures/base/linear-base.ts +3 -3
  57. package/src/data-structures/binary-tree/avl-tree.ts +299 -0
  58. package/src/data-structures/binary-tree/binary-indexed-tree.ts +341 -5
  59. package/src/data-structures/binary-tree/binary-tree.ts +606 -6
  60. package/src/data-structures/binary-tree/bst.ts +946 -7
  61. package/src/data-structures/binary-tree/red-black-tree.ts +472 -0
  62. package/src/data-structures/binary-tree/segment-tree.ts +145 -2
  63. package/src/data-structures/binary-tree/tree-map.ts +3423 -499
  64. package/src/data-structures/binary-tree/tree-multi-map.ts +3537 -596
  65. package/src/data-structures/binary-tree/tree-multi-set.ts +2855 -495
  66. package/src/data-structures/binary-tree/tree-set.ts +3209 -413
  67. package/src/data-structures/graph/abstract-graph.ts +6 -6
  68. package/src/data-structures/graph/directed-graph.ts +240 -0
  69. package/src/data-structures/graph/undirected-graph.ts +216 -0
  70. package/src/data-structures/hash/hash-map.ts +281 -19
  71. package/src/data-structures/heap/heap.ts +340 -4
  72. package/src/data-structures/heap/max-heap.ts +2 -2
  73. package/src/data-structures/linked-list/doubly-linked-list.ts +411 -3
  74. package/src/data-structures/linked-list/singly-linked-list.ts +363 -3
  75. package/src/data-structures/linked-list/skip-linked-list.ts +439 -7
  76. package/src/data-structures/matrix/matrix.ts +202 -10
  77. package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
  78. package/src/data-structures/queue/deque.ts +365 -5
  79. package/src/data-structures/queue/queue.ts +288 -0
  80. package/src/data-structures/stack/stack.ts +240 -0
  81. package/src/data-structures/trie/trie.ts +295 -7
  82. package/src/interfaces/graph.ts +1 -1
  83. package/src/types/common.ts +2 -2
  84. package/src/types/data-structures/binary-tree/bst.ts +1 -0
  85. package/src/types/data-structures/binary-tree/tree-map.ts +6 -0
  86. package/src/types/data-structures/binary-tree/tree-multi-set.ts +5 -0
  87. package/src/types/data-structures/binary-tree/tree-set.ts +5 -0
  88. package/src/types/data-structures/heap/heap.ts +1 -0
  89. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
  90. package/src/types/utils/validate-type.ts +4 -4
@@ -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
  /**
@@ -770,6 +776,30 @@ var _Queue = class _Queue extends LinearBase {
770
776
 
771
777
 
772
778
 
779
+
780
+
781
+
782
+
783
+
784
+
785
+
786
+
787
+
788
+
789
+
790
+
791
+
792
+
793
+
794
+
795
+
796
+
797
+
798
+
799
+
800
+
801
+
802
+
773
803
  * @example
774
804
  * // Track queue length
775
805
  * const q = new Queue<number>();
@@ -796,6 +826,30 @@ var _Queue = class _Queue extends LinearBase {
796
826
 
797
827
 
798
828
 
829
+
830
+
831
+
832
+
833
+
834
+
835
+
836
+
837
+
838
+
839
+
840
+
841
+
842
+
843
+
844
+
845
+
846
+
847
+
848
+
849
+
850
+
851
+
852
+
799
853
  * @example
800
854
  * // View the front element
801
855
  * const q = new Queue<string>(['first', 'second', 'third']);
@@ -838,6 +892,30 @@ var _Queue = class _Queue extends LinearBase {
838
892
 
839
893
 
840
894
 
895
+
896
+
897
+
898
+
899
+
900
+
901
+
902
+
903
+
904
+
905
+
906
+
907
+
908
+
909
+
910
+
911
+
912
+
913
+
914
+
915
+
916
+
917
+
918
+
841
919
  * @example
842
920
  * // Queue for...of iteration and isEmpty check
843
921
  * const queue = new Queue<string>(['A', 'B', 'C', 'D']);
@@ -876,6 +954,30 @@ var _Queue = class _Queue extends LinearBase {
876
954
 
877
955
 
878
956
 
957
+
958
+
959
+
960
+
961
+
962
+
963
+
964
+
965
+
966
+
967
+
968
+
969
+
970
+
971
+
972
+
973
+
974
+
975
+
976
+
977
+
978
+
979
+
980
+
879
981
  * @example
880
982
  * // basic Queue creation and push operation
881
983
  * // Create a simple Queue with initial values
@@ -921,6 +1023,30 @@ var _Queue = class _Queue extends LinearBase {
921
1023
 
922
1024
 
923
1025
 
1026
+
1027
+
1028
+
1029
+
1030
+
1031
+
1032
+
1033
+
1034
+
1035
+
1036
+
1037
+
1038
+
1039
+
1040
+
1041
+
1042
+
1043
+
1044
+
1045
+
1046
+
1047
+
1048
+
1049
+
924
1050
  * @example
925
1051
  * // Queue shift and peek operations
926
1052
  * const queue = new Queue<number>([10, 20, 30, 40]);
@@ -956,6 +1082,30 @@ var _Queue = class _Queue extends LinearBase {
956
1082
 
957
1083
 
958
1084
 
1085
+
1086
+
1087
+
1088
+
1089
+
1090
+
1091
+
1092
+
1093
+
1094
+
1095
+
1096
+
1097
+
1098
+
1099
+
1100
+
1101
+
1102
+
1103
+
1104
+
1105
+
1106
+
1107
+
1108
+
959
1109
  * @example
960
1110
  * // Remove specific element
961
1111
  * const q = new Queue<number>([1, 2, 3, 2]);
@@ -984,6 +1134,30 @@ var _Queue = class _Queue extends LinearBase {
984
1134
 
985
1135
 
986
1136
 
1137
+
1138
+
1139
+
1140
+
1141
+
1142
+
1143
+
1144
+
1145
+
1146
+
1147
+
1148
+
1149
+
1150
+
1151
+
1152
+
1153
+
1154
+
1155
+
1156
+
1157
+
1158
+
1159
+
1160
+
987
1161
  * @example
988
1162
  * // Access element by index
989
1163
  * const q = new Queue<string>(['a', 'b', 'c']);
@@ -1053,6 +1227,30 @@ var _Queue = class _Queue extends LinearBase {
1053
1227
 
1054
1228
 
1055
1229
 
1230
+
1231
+
1232
+
1233
+
1234
+
1235
+
1236
+
1237
+
1238
+
1239
+
1240
+
1241
+
1242
+
1243
+
1244
+
1245
+
1246
+
1247
+
1248
+
1249
+
1250
+
1251
+
1252
+
1253
+
1056
1254
  * @example
1057
1255
  * // Remove all elements
1058
1256
  * const q = new Queue<number>([1, 2, 3]);
@@ -1075,6 +1273,30 @@ var _Queue = class _Queue extends LinearBase {
1075
1273
 
1076
1274
 
1077
1275
 
1276
+
1277
+
1278
+
1279
+
1280
+
1281
+
1282
+
1283
+
1284
+
1285
+
1286
+
1287
+
1288
+
1289
+
1290
+
1291
+
1292
+
1293
+
1294
+
1295
+
1296
+
1297
+
1298
+
1299
+
1078
1300
  * @example
1079
1301
  * // Reclaim unused memory
1080
1302
  * const q = new Queue<number>([1, 2, 3, 4, 5]);
@@ -1120,6 +1342,30 @@ var _Queue = class _Queue extends LinearBase {
1120
1342
 
1121
1343
 
1122
1344
 
1345
+
1346
+
1347
+
1348
+
1349
+
1350
+
1351
+
1352
+
1353
+
1354
+
1355
+
1356
+
1357
+
1358
+
1359
+
1360
+
1361
+
1362
+
1363
+
1364
+
1365
+
1366
+
1367
+
1368
+
1123
1369
  * @example
1124
1370
  * // Create independent copy
1125
1371
  * const q = new Queue<number>([1, 2, 3]);
@@ -1149,6 +1395,30 @@ var _Queue = class _Queue extends LinearBase {
1149
1395
 
1150
1396
 
1151
1397
 
1398
+
1399
+
1400
+
1401
+
1402
+
1403
+
1404
+
1405
+
1406
+
1407
+
1408
+
1409
+
1410
+
1411
+
1412
+
1413
+
1414
+
1415
+
1416
+
1417
+
1418
+
1419
+
1420
+
1421
+
1152
1422
  * @example
1153
1423
  * // Filter elements
1154
1424
  * const q = new Queue<number>([1, 2, 3, 4, 5]);
@@ -1182,6 +1452,30 @@ var _Queue = class _Queue extends LinearBase {
1182
1452
 
1183
1453
 
1184
1454
 
1455
+
1456
+
1457
+
1458
+
1459
+
1460
+
1461
+
1462
+
1463
+
1464
+
1465
+
1466
+
1467
+
1468
+
1469
+
1470
+
1471
+
1472
+
1473
+
1474
+
1475
+
1476
+
1477
+
1478
+
1185
1479
  * @example
1186
1480
  * // Transform elements
1187
1481
  * const q = new Queue<number>([1, 2, 3]);
@@ -1450,7 +1744,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1450
1744
  if (isMapMode !== void 0) this._isMapMode = isMapMode;
1451
1745
  if (isDuplicate !== void 0) this._isDuplicate = isDuplicate;
1452
1746
  if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
1453
- else if (toEntryFn) throw new TypeError(ERR.notAFunction("toEntryFn", "BinaryTree"));
1747
+ else if (toEntryFn) raise(TypeError, ERR.notAFunction("toEntryFn", "BinaryTree"));
1454
1748
  }
1455
1749
  if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
1456
1750
  }
@@ -1666,6 +1960,30 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1666
1960
 
1667
1961
 
1668
1962
 
1963
+
1964
+
1965
+
1966
+
1967
+
1968
+
1969
+
1970
+
1971
+
1972
+
1973
+
1974
+
1975
+
1976
+
1977
+
1978
+
1979
+
1980
+
1981
+
1982
+
1983
+
1984
+
1985
+
1986
+
1669
1987
  * @example
1670
1988
  * // Add a single node
1671
1989
  * const tree = new BinaryTree<number>();
@@ -1696,6 +2014,30 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1696
2014
 
1697
2015
 
1698
2016
 
2017
+
2018
+
2019
+
2020
+
2021
+
2022
+
2023
+
2024
+
2025
+
2026
+
2027
+
2028
+
2029
+
2030
+
2031
+
2032
+
2033
+
2034
+
2035
+
2036
+
2037
+
2038
+
2039
+
2040
+
1699
2041
  * @example
1700
2042
  * // basic BinaryTree creation and insertion
1701
2043
  * // Create a BinaryTree with entries
@@ -1778,6 +2120,30 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1778
2120
 
1779
2121
 
1780
2122
 
2123
+
2124
+
2125
+
2126
+
2127
+
2128
+
2129
+
2130
+
2131
+
2132
+
2133
+
2134
+
2135
+
2136
+
2137
+
2138
+
2139
+
2140
+
2141
+
2142
+
2143
+
2144
+
2145
+
2146
+
1781
2147
  * @example
1782
2148
  * // Bulk add
1783
2149
  * const tree = new BinaryTree<number>();
@@ -1796,6 +2162,30 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1796
2162
  * @returns An array of booleans indicating the success of each individual `set` operation.
1797
2163
 
1798
2164
 
2165
+
2166
+
2167
+
2168
+
2169
+
2170
+
2171
+
2172
+
2173
+
2174
+
2175
+
2176
+
2177
+
2178
+
2179
+
2180
+
2181
+
2182
+
2183
+
2184
+
2185
+
2186
+
2187
+
2188
+
1799
2189
  * @example
1800
2190
  * // Set multiple entries
1801
2191
  * const tree = new BinaryTree<number, string>();
@@ -1835,6 +2225,30 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1835
2225
 
1836
2226
 
1837
2227
 
2228
+
2229
+
2230
+
2231
+
2232
+
2233
+
2234
+
2235
+
2236
+
2237
+
2238
+
2239
+
2240
+
2241
+
2242
+
2243
+
2244
+
2245
+
2246
+
2247
+
2248
+
2249
+
2250
+
2251
+
1838
2252
  * @example
1839
2253
  * // Combine trees
1840
2254
  * const t1 = new BinaryTree<number>([1, 2]);
@@ -1873,6 +2287,30 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1873
2287
 
1874
2288
 
1875
2289
 
2290
+
2291
+
2292
+
2293
+
2294
+
2295
+
2296
+
2297
+
2298
+
2299
+
2300
+
2301
+
2302
+
2303
+
2304
+
2305
+
2306
+
2307
+
2308
+
2309
+
2310
+
2311
+
2312
+
2313
+
1876
2314
  * @example
1877
2315
  * // Remove a node
1878
2316
  * const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
@@ -1989,6 +2427,30 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1989
2427
 
1990
2428
 
1991
2429
 
2430
+
2431
+
2432
+
2433
+
2434
+
2435
+
2436
+
2437
+
2438
+
2439
+
2440
+
2441
+
2442
+
2443
+
2444
+
2445
+
2446
+
2447
+
2448
+
2449
+
2450
+
2451
+
2452
+
2453
+
1992
2454
  * @example
1993
2455
  * // Get node by key
1994
2456
  * const tree = new BinaryTree<number, string>([[1, 'root'], [2, 'child']]);
@@ -2023,6 +2485,30 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2023
2485
 
2024
2486
 
2025
2487
 
2488
+
2489
+
2490
+
2491
+
2492
+
2493
+
2494
+
2495
+
2496
+
2497
+
2498
+
2499
+
2500
+
2501
+
2502
+
2503
+
2504
+
2505
+
2506
+
2507
+
2508
+
2509
+
2510
+
2511
+
2026
2512
  * @example
2027
2513
  * // Retrieve value by key
2028
2514
  * const tree = new BinaryTree<number, string>([[1, 'root'], [2, 'left'], [3, 'right']]);
@@ -2060,6 +2546,30 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2060
2546
 
2061
2547
 
2062
2548
 
2549
+
2550
+
2551
+
2552
+
2553
+
2554
+
2555
+
2556
+
2557
+
2558
+
2559
+
2560
+
2561
+
2562
+
2563
+
2564
+
2565
+
2566
+
2567
+
2568
+
2569
+
2570
+
2571
+
2572
+
2063
2573
  * @example
2064
2574
  * // Remove all nodes
2065
2575
  * const tree = new BinaryTree<number>([1, 2, 3]);
@@ -2084,6 +2594,30 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2084
2594
 
2085
2595
 
2086
2596
 
2597
+
2598
+
2599
+
2600
+
2601
+
2602
+
2603
+
2604
+
2605
+
2606
+
2607
+
2608
+
2609
+
2610
+
2611
+
2612
+
2613
+
2614
+
2615
+
2616
+
2617
+
2618
+
2619
+
2620
+
2087
2621
  * @example
2088
2622
  * // Check empty
2089
2623
  * console.log(new BinaryTree().isEmpty()); // true;
@@ -2117,6 +2651,30 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2117
2651
 
2118
2652
 
2119
2653
 
2654
+
2655
+
2656
+
2657
+
2658
+
2659
+
2660
+
2661
+
2662
+
2663
+
2664
+
2665
+
2666
+
2667
+
2668
+
2669
+
2670
+
2671
+
2672
+
2673
+
2674
+
2675
+
2676
+
2677
+
2120
2678
  * @example
2121
2679
  * // Check BST property
2122
2680
  * const tree = new BinaryTree<number>([1, 2, 3]);
@@ -2177,6 +2735,30 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2177
2735
 
2178
2736
 
2179
2737
 
2738
+
2739
+
2740
+
2741
+
2742
+
2743
+
2744
+
2745
+
2746
+
2747
+
2748
+
2749
+
2750
+
2751
+
2752
+
2753
+
2754
+
2755
+
2756
+
2757
+
2758
+
2759
+
2760
+
2761
+
2180
2762
  * @example
2181
2763
  * // Get depth of a node
2182
2764
  * const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
@@ -2214,6 +2796,30 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2214
2796
 
2215
2797
 
2216
2798
 
2799
+
2800
+
2801
+
2802
+
2803
+
2804
+
2805
+
2806
+
2807
+
2808
+
2809
+
2810
+
2811
+
2812
+
2813
+
2814
+
2815
+
2816
+
2817
+
2818
+
2819
+
2820
+
2821
+
2822
+
2217
2823
  * @example
2218
2824
  * // Get tree height
2219
2825
  * const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
@@ -2667,6 +3273,30 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2667
3273
 
2668
3274
 
2669
3275
 
3276
+
3277
+
3278
+
3279
+
3280
+
3281
+
3282
+
3283
+
3284
+
3285
+
3286
+
3287
+
3288
+
3289
+
3290
+
3291
+
3292
+
3293
+
3294
+
3295
+
3296
+
3297
+
3298
+
3299
+
2670
3300
  * @example
2671
3301
  * // Deep copy
2672
3302
  * const tree = new BinaryTree<number>([1, 2, 3]);
@@ -2695,6 +3325,30 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2695
3325
 
2696
3326
 
2697
3327
 
3328
+
3329
+
3330
+
3331
+
3332
+
3333
+
3334
+
3335
+
3336
+
3337
+
3338
+
3339
+
3340
+
3341
+
3342
+
3343
+
3344
+
3345
+
3346
+
3347
+
3348
+
3349
+
3350
+
3351
+
2698
3352
  * @example
2699
3353
  * // Filter nodes by condition
2700
3354
  * const tree = new BinaryTree<number>([1, 2, 3, 4]);
@@ -2727,6 +3381,30 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2727
3381
 
2728
3382
 
2729
3383
 
3384
+
3385
+
3386
+
3387
+
3388
+
3389
+
3390
+
3391
+
3392
+
3393
+
3394
+
3395
+
3396
+
3397
+
3398
+
3399
+
3400
+
3401
+
3402
+
3403
+
3404
+
3405
+
3406
+
3407
+
2730
3408
  * @example
2731
3409
  * // Transform to new tree
2732
3410
  * const tree = new BinaryTree<number, number>([[1, 10], [2, 20]]);
@@ -2784,6 +3462,30 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2784
3462
 
2785
3463
 
2786
3464
 
3465
+
3466
+
3467
+
3468
+
3469
+
3470
+
3471
+
3472
+
3473
+
3474
+
3475
+
3476
+
3477
+
3478
+
3479
+
3480
+
3481
+
3482
+
3483
+
3484
+
3485
+
3486
+
3487
+
3488
+
2787
3489
  * @example
2788
3490
  * // Display tree
2789
3491
  * const tree = new BinaryTree<number>([1, 2, 3]);
@@ -3402,6 +4104,7 @@ var _BST = class _BST extends BinaryTree {
3402
4104
  constructor(keysNodesEntriesOrRaws = [], options) {
3403
4105
  super([], options);
3404
4106
  __publicField(this, "_root");
4107
+ __publicField(this, "_enableOrderStatistic", false);
3405
4108
  /**
3406
4109
  * The comparator function used to determine the order of keys in the tree.
3407
4110
 
@@ -3414,6 +4117,9 @@ var _BST = class _BST extends BinaryTree {
3414
4117
  } else {
3415
4118
  this._comparator = this._createDefaultComparator();
3416
4119
  }
4120
+ if (options.enableOrderStatistic) {
4121
+ this._enableOrderStatistic = true;
4122
+ }
3417
4123
  } else {
3418
4124
  this._comparator = this._createDefaultComparator();
3419
4125
  }
@@ -3548,6 +4254,54 @@ var _BST = class _BST extends BinaryTree {
3548
4254
 
3549
4255
 
3550
4256
 
4257
+
4258
+
4259
+
4260
+
4261
+
4262
+
4263
+
4264
+
4265
+
4266
+
4267
+
4268
+
4269
+
4270
+
4271
+
4272
+
4273
+
4274
+
4275
+
4276
+
4277
+
4278
+
4279
+
4280
+
4281
+
4282
+
4283
+
4284
+
4285
+
4286
+
4287
+
4288
+
4289
+
4290
+
4291
+
4292
+
4293
+
4294
+
4295
+
4296
+
4297
+
4298
+
4299
+
4300
+
4301
+
4302
+
4303
+
4304
+
3551
4305
  * @example
3552
4306
  * // Get node object by key
3553
4307
  * const bst = new BST<number, string>([[5, 'root'], [3, 'left'], [7, 'right']]);
@@ -3703,6 +4457,85 @@ var _BST = class _BST extends BinaryTree {
3703
4457
  const searchRange = range instanceof Range ? range : new Range(range[0], range[1]);
3704
4458
  return this.search(searchRange, false, callback, startNode, iterationType);
3705
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
+ }
3706
4539
  /**
3707
4540
  * Adds a new node to the BST based on key comparison.
3708
4541
  * @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
@@ -3730,6 +4563,78 @@ var _BST = class _BST extends BinaryTree {
3730
4563
 
3731
4564
 
3732
4565
 
4566
+
4567
+
4568
+
4569
+
4570
+
4571
+
4572
+
4573
+
4574
+
4575
+
4576
+
4577
+
4578
+
4579
+
4580
+
4581
+
4582
+
4583
+
4584
+
4585
+
4586
+
4587
+
4588
+
4589
+
4590
+
4591
+
4592
+
4593
+
4594
+
4595
+
4596
+
4597
+
4598
+
4599
+
4600
+
4601
+
4602
+
4603
+
4604
+
4605
+
4606
+
4607
+
4608
+
4609
+
4610
+
4611
+
4612
+
4613
+
4614
+
4615
+
4616
+
4617
+
4618
+
4619
+
4620
+
4621
+
4622
+
4623
+
4624
+
4625
+
4626
+
4627
+
4628
+
4629
+
4630
+
4631
+
4632
+
4633
+
4634
+
4635
+
4636
+
4637
+
3733
4638
 
3734
4639
 
3735
4640
 
@@ -3749,6 +4654,7 @@ var _BST = class _BST extends BinaryTree {
3749
4654
  this._setRoot(newNode);
3750
4655
  if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
3751
4656
  this._size++;
4657
+ this._updateCount(newNode);
3752
4658
  return true;
3753
4659
  }
3754
4660
  let current = this._root;
@@ -3762,6 +4668,7 @@ var _BST = class _BST extends BinaryTree {
3762
4668
  current.left = newNode;
3763
4669
  if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
3764
4670
  this._size++;
4671
+ this._updateCountAlongPath(newNode);
3765
4672
  return true;
3766
4673
  }
3767
4674
  if (current.left !== null) current = current.left;
@@ -3770,6 +4677,7 @@ var _BST = class _BST extends BinaryTree {
3770
4677
  current.right = newNode;
3771
4678
  if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
3772
4679
  this._size++;
4680
+ this._updateCountAlongPath(newNode);
3773
4681
  return true;
3774
4682
  }
3775
4683
  if (current.right !== null) current = current.right;
@@ -3799,6 +4707,54 @@ var _BST = class _BST extends BinaryTree {
3799
4707
 
3800
4708
 
3801
4709
 
4710
+
4711
+
4712
+
4713
+
4714
+
4715
+
4716
+
4717
+
4718
+
4719
+
4720
+
4721
+
4722
+
4723
+
4724
+
4725
+
4726
+
4727
+
4728
+
4729
+
4730
+
4731
+
4732
+
4733
+
4734
+
4735
+
4736
+
4737
+
4738
+
4739
+
4740
+
4741
+
4742
+
4743
+
4744
+
4745
+
4746
+
4747
+
4748
+
4749
+
4750
+
4751
+
4752
+
4753
+
4754
+
4755
+
4756
+
4757
+
3802
4758
  * @example
3803
4759
  * // Set multiple key-value pairs
3804
4760
  * const bst = new BST<number, string>();
@@ -4062,6 +5018,30 @@ var _BST = class _BST extends BinaryTree {
4062
5018
 
4063
5019
 
4064
5020
 
5021
+
5022
+
5023
+
5024
+
5025
+
5026
+
5027
+
5028
+
5029
+
5030
+
5031
+
5032
+
5033
+
5034
+
5035
+
5036
+
5037
+
5038
+
5039
+
5040
+
5041
+
5042
+
5043
+
5044
+
4065
5045
  * @example
4066
5046
  * // Rebalance the tree
4067
5047
  * const bst = new BST<number>();
@@ -4107,6 +5087,30 @@ var _BST = class _BST extends BinaryTree {
4107
5087
 
4108
5088
 
4109
5089
 
5090
+
5091
+
5092
+
5093
+
5094
+
5095
+
5096
+
5097
+
5098
+
5099
+
5100
+
5101
+
5102
+
5103
+
5104
+
5105
+
5106
+
5107
+
5108
+
5109
+
5110
+
5111
+
5112
+
5113
+
4110
5114
  * @example
4111
5115
  * // Check if tree is height-balanced
4112
5116
  * const bst = new BST<number>([3, 1, 5, 2, 4]);
@@ -4181,6 +5185,54 @@ var _BST = class _BST extends BinaryTree {
4181
5185
 
4182
5186
 
4183
5187
 
5188
+
5189
+
5190
+
5191
+
5192
+
5193
+
5194
+
5195
+
5196
+
5197
+
5198
+
5199
+
5200
+
5201
+
5202
+
5203
+
5204
+
5205
+
5206
+
5207
+
5208
+
5209
+
5210
+
5211
+
5212
+
5213
+
5214
+
5215
+
5216
+
5217
+
5218
+
5219
+
5220
+
5221
+
5222
+
5223
+
5224
+
5225
+
5226
+
5227
+
5228
+
5229
+
5230
+
5231
+
5232
+
5233
+
5234
+
5235
+
4184
5236
  * @example
4185
5237
  * // Transform to new tree
4186
5238
  * const bst = new BST<number, number>([[1, 10], [2, 20], [3, 30]]);
@@ -4255,13 +5307,11 @@ var _BST = class _BST extends BinaryTree {
4255
5307
  if (a instanceof Date && b instanceof Date) {
4256
5308
  const ta = a.getTime();
4257
5309
  const tb = b.getTime();
4258
- 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"));
4259
5311
  return ta > tb ? 1 : ta < tb ? -1 : 0;
4260
5312
  }
4261
5313
  if (typeof a === "object" || typeof b === "object") {
4262
- throw new TypeError(
4263
- ERR.comparatorRequired("BST")
4264
- );
5314
+ raise(TypeError, ERR.comparatorRequired("BST"));
4265
5315
  }
4266
5316
  return 0;
4267
5317
  };
@@ -4583,7 +5633,8 @@ var _BST = class _BST extends BinaryTree {
4583
5633
  _snapshotOptions() {
4584
5634
  return {
4585
5635
  ...super._snapshotOptions(),
4586
- comparator: this._comparator
5636
+ comparator: this._comparator,
5637
+ enableOrderStatistic: this._enableOrderStatistic
4587
5638
  };
4588
5639
  }
4589
5640
  /**
@@ -4605,6 +5656,113 @@ var _BST = class _BST extends BinaryTree {
4605
5656
  *
4606
5657
  * @param v - The node to set as root.
4607
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
+ }
4608
5766
  _setRoot(v) {
4609
5767
  if (v) v.parent = void 0;
4610
5768
  this._root = v;
@@ -4651,21 +5809,28 @@ var _BST = class _BST extends BinaryTree {
4651
5809
  while (x.left !== void 0 && x.left !== null) x = x.left;
4652
5810
  return x;
4653
5811
  }, "minNode");
5812
+ let countUpdateStart;
4654
5813
  if (node.left === void 0) {
5814
+ countUpdateStart = node.parent;
4655
5815
  transplant(node, node.right);
4656
5816
  } else if (node.right === void 0) {
5817
+ countUpdateStart = node.parent;
4657
5818
  transplant(node, node.left);
4658
5819
  } else {
4659
5820
  const succ = minNode(node.right);
4660
5821
  if (succ.parent !== node) {
5822
+ countUpdateStart = succ.parent;
4661
5823
  transplant(succ, succ.right);
4662
5824
  succ.right = node.right;
4663
5825
  if (succ.right) succ.right.parent = succ;
5826
+ } else {
5827
+ countUpdateStart = succ;
4664
5828
  }
4665
5829
  transplant(node, succ);
4666
5830
  succ.left = node.left;
4667
5831
  if (succ.left) succ.left.parent = succ;
4668
5832
  }
5833
+ this._updateCountAlongPath(countUpdateStart);
4669
5834
  this._size = Math.max(0, this._size - 1);
4670
5835
  return true;
4671
5836
  }
@@ -4680,6 +5845,6 @@ var BST = _BST;
4680
5845
  * @license MIT License
4681
5846
  */
4682
5847
 
4683
- export { BST, BSTNode, BinaryTree, BinaryTreeNode, DFSOperation, ERR, Range };
5848
+ export { BST, BSTNode, BinaryTree, BinaryTreeNode, DFSOperation, ERR, Range, raise };
4684
5849
  //# sourceMappingURL=index.mjs.map
4685
5850
  //# sourceMappingURL=index.mjs.map