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
@@ -57,6 +57,61 @@ function makeTrampoline(fn) {
57
57
  }
58
58
  __name(makeTrampoline, "makeTrampoline");
59
59
 
60
+ // src/common/error.ts
61
+ function raise(ErrorClass, message) {
62
+ throw new ErrorClass(message);
63
+ }
64
+ __name(raise, "raise");
65
+ var ERR = {
66
+ // Range / index
67
+ indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
68
+ invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
69
+ // Type / argument
70
+ invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
71
+ comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
72
+ invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
73
+ notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
74
+ invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
75
+ invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
76
+ invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
77
+ reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
78
+ callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
79
+ // State / operation
80
+ invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
81
+ // Matrix
82
+ matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
83
+ matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
84
+ matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
85
+ matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
86
+ matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch"),
87
+ // Order statistic
88
+ orderStatisticNotEnabled: /* @__PURE__ */ __name((method, ctx) => `${ctx ? ctx + ": " : ""}${method}() requires enableOrderStatistic: true.`, "orderStatisticNotEnabled")
89
+ };
90
+
91
+ // src/common/index.ts
92
+ var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
93
+ DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
94
+ DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
95
+ return DFSOperation2;
96
+ })(DFSOperation || {});
97
+ var Range = class {
98
+ constructor(low, high, includeLow = true, includeHigh = true) {
99
+ this.low = low;
100
+ this.high = high;
101
+ this.includeLow = includeLow;
102
+ this.includeHigh = includeHigh;
103
+ }
104
+ static {
105
+ __name(this, "Range");
106
+ }
107
+ // Determine whether a key is within the range
108
+ isInRange(key, comparator) {
109
+ const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
110
+ const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
111
+ return lowCheck && highCheck;
112
+ }
113
+ };
114
+
60
115
  // src/data-structures/base/iterable-element-base.ts
61
116
  var IterableElementBase = class {
62
117
  static {
@@ -75,7 +130,7 @@ var IterableElementBase = class {
75
130
  if (options) {
76
131
  const { toElementFn } = options;
77
132
  if (typeof toElementFn === "function") this._toElementFn = toElementFn;
78
- else if (toElementFn) throw new TypeError("toElementFn must be a function type");
133
+ else if (toElementFn) raise(TypeError, "toElementFn must be a function type");
79
134
  }
80
135
  }
81
136
  /**
@@ -238,7 +293,7 @@ var IterableElementBase = class {
238
293
  acc = initialValue;
239
294
  } else {
240
295
  const first = iter.next();
241
- 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");
242
297
  acc = first.value;
243
298
  index = 1;
244
299
  }
@@ -473,55 +528,6 @@ var LinearBase = class _LinearBase extends IterableElementBase {
473
528
  }
474
529
  };
475
530
 
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 {
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
- static {
515
- __name(this, "Range");
516
- }
517
- // Determine whether a key is within the range
518
- isInRange(key, comparator) {
519
- const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
520
- const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
521
- return lowCheck && highCheck;
522
- }
523
- };
524
-
525
531
  // src/data-structures/base/iterable-entry-base.ts
526
532
  var IterableEntryBase = class {
527
533
  static {
@@ -775,6 +781,30 @@ var Queue = class _Queue extends LinearBase {
775
781
 
776
782
 
777
783
 
784
+
785
+
786
+
787
+
788
+
789
+
790
+
791
+
792
+
793
+
794
+
795
+
796
+
797
+
798
+
799
+
800
+
801
+
802
+
803
+
804
+
805
+
806
+
807
+
778
808
  * @example
779
809
  * // Track queue length
780
810
  * const q = new Queue<number>();
@@ -801,6 +831,30 @@ var Queue = class _Queue extends LinearBase {
801
831
 
802
832
 
803
833
 
834
+
835
+
836
+
837
+
838
+
839
+
840
+
841
+
842
+
843
+
844
+
845
+
846
+
847
+
848
+
849
+
850
+
851
+
852
+
853
+
854
+
855
+
856
+
857
+
804
858
  * @example
805
859
  * // View the front element
806
860
  * const q = new Queue<string>(['first', 'second', 'third']);
@@ -843,6 +897,30 @@ var Queue = class _Queue extends LinearBase {
843
897
 
844
898
 
845
899
 
900
+
901
+
902
+
903
+
904
+
905
+
906
+
907
+
908
+
909
+
910
+
911
+
912
+
913
+
914
+
915
+
916
+
917
+
918
+
919
+
920
+
921
+
922
+
923
+
846
924
  * @example
847
925
  * // Queue for...of iteration and isEmpty check
848
926
  * const queue = new Queue<string>(['A', 'B', 'C', 'D']);
@@ -881,6 +959,30 @@ var Queue = class _Queue extends LinearBase {
881
959
 
882
960
 
883
961
 
962
+
963
+
964
+
965
+
966
+
967
+
968
+
969
+
970
+
971
+
972
+
973
+
974
+
975
+
976
+
977
+
978
+
979
+
980
+
981
+
982
+
983
+
984
+
985
+
884
986
  * @example
885
987
  * // basic Queue creation and push operation
886
988
  * // Create a simple Queue with initial values
@@ -926,6 +1028,30 @@ var Queue = class _Queue extends LinearBase {
926
1028
 
927
1029
 
928
1030
 
1031
+
1032
+
1033
+
1034
+
1035
+
1036
+
1037
+
1038
+
1039
+
1040
+
1041
+
1042
+
1043
+
1044
+
1045
+
1046
+
1047
+
1048
+
1049
+
1050
+
1051
+
1052
+
1053
+
1054
+
929
1055
  * @example
930
1056
  * // Queue shift and peek operations
931
1057
  * const queue = new Queue<number>([10, 20, 30, 40]);
@@ -961,6 +1087,30 @@ var Queue = class _Queue extends LinearBase {
961
1087
 
962
1088
 
963
1089
 
1090
+
1091
+
1092
+
1093
+
1094
+
1095
+
1096
+
1097
+
1098
+
1099
+
1100
+
1101
+
1102
+
1103
+
1104
+
1105
+
1106
+
1107
+
1108
+
1109
+
1110
+
1111
+
1112
+
1113
+
964
1114
  * @example
965
1115
  * // Remove specific element
966
1116
  * const q = new Queue<number>([1, 2, 3, 2]);
@@ -989,6 +1139,30 @@ var Queue = class _Queue extends LinearBase {
989
1139
 
990
1140
 
991
1141
 
1142
+
1143
+
1144
+
1145
+
1146
+
1147
+
1148
+
1149
+
1150
+
1151
+
1152
+
1153
+
1154
+
1155
+
1156
+
1157
+
1158
+
1159
+
1160
+
1161
+
1162
+
1163
+
1164
+
1165
+
992
1166
  * @example
993
1167
  * // Access element by index
994
1168
  * const q = new Queue<string>(['a', 'b', 'c']);
@@ -1058,6 +1232,30 @@ var Queue = class _Queue extends LinearBase {
1058
1232
 
1059
1233
 
1060
1234
 
1235
+
1236
+
1237
+
1238
+
1239
+
1240
+
1241
+
1242
+
1243
+
1244
+
1245
+
1246
+
1247
+
1248
+
1249
+
1250
+
1251
+
1252
+
1253
+
1254
+
1255
+
1256
+
1257
+
1258
+
1061
1259
  * @example
1062
1260
  * // Remove all elements
1063
1261
  * const q = new Queue<number>([1, 2, 3]);
@@ -1080,6 +1278,30 @@ var Queue = class _Queue extends LinearBase {
1080
1278
 
1081
1279
 
1082
1280
 
1281
+
1282
+
1283
+
1284
+
1285
+
1286
+
1287
+
1288
+
1289
+
1290
+
1291
+
1292
+
1293
+
1294
+
1295
+
1296
+
1297
+
1298
+
1299
+
1300
+
1301
+
1302
+
1303
+
1304
+
1083
1305
  * @example
1084
1306
  * // Reclaim unused memory
1085
1307
  * const q = new Queue<number>([1, 2, 3, 4, 5]);
@@ -1125,6 +1347,30 @@ var Queue = class _Queue extends LinearBase {
1125
1347
 
1126
1348
 
1127
1349
 
1350
+
1351
+
1352
+
1353
+
1354
+
1355
+
1356
+
1357
+
1358
+
1359
+
1360
+
1361
+
1362
+
1363
+
1364
+
1365
+
1366
+
1367
+
1368
+
1369
+
1370
+
1371
+
1372
+
1373
+
1128
1374
  * @example
1129
1375
  * // Create independent copy
1130
1376
  * const q = new Queue<number>([1, 2, 3]);
@@ -1154,6 +1400,30 @@ var Queue = class _Queue extends LinearBase {
1154
1400
 
1155
1401
 
1156
1402
 
1403
+
1404
+
1405
+
1406
+
1407
+
1408
+
1409
+
1410
+
1411
+
1412
+
1413
+
1414
+
1415
+
1416
+
1417
+
1418
+
1419
+
1420
+
1421
+
1422
+
1423
+
1424
+
1425
+
1426
+
1157
1427
  * @example
1158
1428
  * // Filter elements
1159
1429
  * const q = new Queue<number>([1, 2, 3, 4, 5]);
@@ -1187,6 +1457,30 @@ var Queue = class _Queue extends LinearBase {
1187
1457
 
1188
1458
 
1189
1459
 
1460
+
1461
+
1462
+
1463
+
1464
+
1465
+
1466
+
1467
+
1468
+
1469
+
1470
+
1471
+
1472
+
1473
+
1474
+
1475
+
1476
+
1477
+
1478
+
1479
+
1480
+
1481
+
1482
+
1483
+
1190
1484
  * @example
1191
1485
  * // Transform elements
1192
1486
  * const q = new Queue<number>([1, 2, 3]);
@@ -1437,7 +1731,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
1437
1731
  if (isMapMode !== void 0) this._isMapMode = isMapMode;
1438
1732
  if (isDuplicate !== void 0) this._isDuplicate = isDuplicate;
1439
1733
  if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
1440
- else if (toEntryFn) throw new TypeError(ERR.notAFunction("toEntryFn", "BinaryTree"));
1734
+ else if (toEntryFn) raise(TypeError, ERR.notAFunction("toEntryFn", "BinaryTree"));
1441
1735
  }
1442
1736
  if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
1443
1737
  }
@@ -1663,6 +1957,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
1663
1957
 
1664
1958
 
1665
1959
 
1960
+
1961
+
1962
+
1963
+
1964
+
1965
+
1966
+
1967
+
1968
+
1969
+
1970
+
1971
+
1972
+
1973
+
1974
+
1975
+
1976
+
1977
+
1978
+
1979
+
1980
+
1981
+
1982
+
1983
+
1666
1984
  * @example
1667
1985
  * // Add a single node
1668
1986
  * const tree = new BinaryTree<number>();
@@ -1693,6 +2011,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
1693
2011
 
1694
2012
 
1695
2013
 
2014
+
2015
+
2016
+
2017
+
2018
+
2019
+
2020
+
2021
+
2022
+
2023
+
2024
+
2025
+
2026
+
2027
+
2028
+
2029
+
2030
+
2031
+
2032
+
2033
+
2034
+
2035
+
2036
+
2037
+
1696
2038
  * @example
1697
2039
  * // basic BinaryTree creation and insertion
1698
2040
  * // Create a BinaryTree with entries
@@ -1775,6 +2117,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
1775
2117
 
1776
2118
 
1777
2119
 
2120
+
2121
+
2122
+
2123
+
2124
+
2125
+
2126
+
2127
+
2128
+
2129
+
2130
+
2131
+
2132
+
2133
+
2134
+
2135
+
2136
+
2137
+
2138
+
2139
+
2140
+
2141
+
2142
+
2143
+
1778
2144
  * @example
1779
2145
  * // Bulk add
1780
2146
  * const tree = new BinaryTree<number>();
@@ -1793,6 +2159,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
1793
2159
  * @returns An array of booleans indicating the success of each individual `set` operation.
1794
2160
 
1795
2161
 
2162
+
2163
+
2164
+
2165
+
2166
+
2167
+
2168
+
2169
+
2170
+
2171
+
2172
+
2173
+
2174
+
2175
+
2176
+
2177
+
2178
+
2179
+
2180
+
2181
+
2182
+
2183
+
2184
+
2185
+
1796
2186
  * @example
1797
2187
  * // Set multiple entries
1798
2188
  * const tree = new BinaryTree<number, string>();
@@ -1832,6 +2222,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
1832
2222
 
1833
2223
 
1834
2224
 
2225
+
2226
+
2227
+
2228
+
2229
+
2230
+
2231
+
2232
+
2233
+
2234
+
2235
+
2236
+
2237
+
2238
+
2239
+
2240
+
2241
+
2242
+
2243
+
2244
+
2245
+
2246
+
2247
+
2248
+
1835
2249
  * @example
1836
2250
  * // Combine trees
1837
2251
  * const t1 = new BinaryTree<number>([1, 2]);
@@ -1870,6 +2284,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
1870
2284
 
1871
2285
 
1872
2286
 
2287
+
2288
+
2289
+
2290
+
2291
+
2292
+
2293
+
2294
+
2295
+
2296
+
2297
+
2298
+
2299
+
2300
+
2301
+
2302
+
2303
+
2304
+
2305
+
2306
+
2307
+
2308
+
2309
+
2310
+
1873
2311
  * @example
1874
2312
  * // Remove a node
1875
2313
  * const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
@@ -1986,6 +2424,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
1986
2424
 
1987
2425
 
1988
2426
 
2427
+
2428
+
2429
+
2430
+
2431
+
2432
+
2433
+
2434
+
2435
+
2436
+
2437
+
2438
+
2439
+
2440
+
2441
+
2442
+
2443
+
2444
+
2445
+
2446
+
2447
+
2448
+
2449
+
2450
+
1989
2451
  * @example
1990
2452
  * // Get node by key
1991
2453
  * const tree = new BinaryTree<number, string>([[1, 'root'], [2, 'child']]);
@@ -2020,6 +2482,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2020
2482
 
2021
2483
 
2022
2484
 
2485
+
2486
+
2487
+
2488
+
2489
+
2490
+
2491
+
2492
+
2493
+
2494
+
2495
+
2496
+
2497
+
2498
+
2499
+
2500
+
2501
+
2502
+
2503
+
2504
+
2505
+
2506
+
2507
+
2508
+
2023
2509
  * @example
2024
2510
  * // Retrieve value by key
2025
2511
  * const tree = new BinaryTree<number, string>([[1, 'root'], [2, 'left'], [3, 'right']]);
@@ -2056,6 +2542,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2056
2542
 
2057
2543
 
2058
2544
 
2545
+
2546
+
2547
+
2548
+
2549
+
2550
+
2551
+
2552
+
2553
+
2554
+
2555
+
2556
+
2557
+
2558
+
2559
+
2560
+
2561
+
2562
+
2563
+
2564
+
2565
+
2566
+
2567
+
2568
+
2059
2569
  * @example
2060
2570
  * // Remove all nodes
2061
2571
  * const tree = new BinaryTree<number>([1, 2, 3]);
@@ -2080,6 +2590,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2080
2590
 
2081
2591
 
2082
2592
 
2593
+
2594
+
2595
+
2596
+
2597
+
2598
+
2599
+
2600
+
2601
+
2602
+
2603
+
2604
+
2605
+
2606
+
2607
+
2608
+
2609
+
2610
+
2611
+
2612
+
2613
+
2614
+
2615
+
2616
+
2083
2617
  * @example
2084
2618
  * // Check empty
2085
2619
  * console.log(new BinaryTree().isEmpty()); // true;
@@ -2113,6 +2647,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2113
2647
 
2114
2648
 
2115
2649
 
2650
+
2651
+
2652
+
2653
+
2654
+
2655
+
2656
+
2657
+
2658
+
2659
+
2660
+
2661
+
2662
+
2663
+
2664
+
2665
+
2666
+
2667
+
2668
+
2669
+
2670
+
2671
+
2672
+
2673
+
2116
2674
  * @example
2117
2675
  * // Check BST property
2118
2676
  * const tree = new BinaryTree<number>([1, 2, 3]);
@@ -2173,6 +2731,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2173
2731
 
2174
2732
 
2175
2733
 
2734
+
2735
+
2736
+
2737
+
2738
+
2739
+
2740
+
2741
+
2742
+
2743
+
2744
+
2745
+
2746
+
2747
+
2748
+
2749
+
2750
+
2751
+
2752
+
2753
+
2754
+
2755
+
2756
+
2757
+
2176
2758
  * @example
2177
2759
  * // Get depth of a node
2178
2760
  * const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
@@ -2210,6 +2792,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2210
2792
 
2211
2793
 
2212
2794
 
2795
+
2796
+
2797
+
2798
+
2799
+
2800
+
2801
+
2802
+
2803
+
2804
+
2805
+
2806
+
2807
+
2808
+
2809
+
2810
+
2811
+
2812
+
2813
+
2814
+
2815
+
2816
+
2817
+
2818
+
2213
2819
  * @example
2214
2820
  * // Get tree height
2215
2821
  * const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
@@ -2663,6 +3269,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2663
3269
 
2664
3270
 
2665
3271
 
3272
+
3273
+
3274
+
3275
+
3276
+
3277
+
3278
+
3279
+
3280
+
3281
+
3282
+
3283
+
3284
+
3285
+
3286
+
3287
+
3288
+
3289
+
3290
+
3291
+
3292
+
3293
+
3294
+
3295
+
2666
3296
  * @example
2667
3297
  * // Deep copy
2668
3298
  * const tree = new BinaryTree<number>([1, 2, 3]);
@@ -2691,6 +3321,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2691
3321
 
2692
3322
 
2693
3323
 
3324
+
3325
+
3326
+
3327
+
3328
+
3329
+
3330
+
3331
+
3332
+
3333
+
3334
+
3335
+
3336
+
3337
+
3338
+
3339
+
3340
+
3341
+
3342
+
3343
+
3344
+
3345
+
3346
+
3347
+
2694
3348
  * @example
2695
3349
  * // Filter nodes by condition
2696
3350
  * const tree = new BinaryTree<number>([1, 2, 3, 4]);
@@ -2723,6 +3377,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2723
3377
 
2724
3378
 
2725
3379
 
3380
+
3381
+
3382
+
3383
+
3384
+
3385
+
3386
+
3387
+
3388
+
3389
+
3390
+
3391
+
3392
+
3393
+
3394
+
3395
+
3396
+
3397
+
3398
+
3399
+
3400
+
3401
+
3402
+
3403
+
2726
3404
  * @example
2727
3405
  * // Transform to new tree
2728
3406
  * const tree = new BinaryTree<number, number>([[1, 10], [2, 20]]);
@@ -2780,6 +3458,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2780
3458
 
2781
3459
 
2782
3460
 
3461
+
3462
+
3463
+
3464
+
3465
+
3466
+
3467
+
3468
+
3469
+
3470
+
3471
+
3472
+
3473
+
3474
+
3475
+
3476
+
3477
+
3478
+
3479
+
3480
+
3481
+
3482
+
3483
+
3484
+
2783
3485
  * @example
2784
3486
  * // Display tree
2785
3487
  * const tree = new BinaryTree<number>([1, 2, 3]);
@@ -3411,12 +4113,16 @@ var BST = class extends BinaryTree {
3411
4113
  } else {
3412
4114
  this._comparator = this._createDefaultComparator();
3413
4115
  }
4116
+ if (options.enableOrderStatistic) {
4117
+ this._enableOrderStatistic = true;
4118
+ }
3414
4119
  } else {
3415
4120
  this._comparator = this._createDefaultComparator();
3416
4121
  }
3417
4122
  if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
3418
4123
  }
3419
4124
  _root = void 0;
4125
+ _enableOrderStatistic = false;
3420
4126
  /**
3421
4127
  * Gets the root node of the tree.
3422
4128
  * @remarks Time O(1)
@@ -3551,6 +4257,54 @@ var BST = class extends BinaryTree {
3551
4257
 
3552
4258
 
3553
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
+
4305
+
4306
+
4307
+
3554
4308
  * @example
3555
4309
  * // Get node object by key
3556
4310
  * const bst = new BST<number, string>([[5, 'root'], [3, 'left'], [7, 'right']]);
@@ -3705,6 +4459,84 @@ var BST = class extends BinaryTree {
3705
4459
  const searchRange = range instanceof Range ? range : new Range(range[0], range[1]);
3706
4460
  return this.search(searchRange, false, callback, startNode, iterationType);
3707
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
+ if (!this._enableOrderStatistic) {
4483
+ raise(Error, ERR.orderStatisticNotEnabled("getRank"));
4484
+ }
4485
+ if (!this._root || this._size === 0) return -1;
4486
+ let actualIterationType = this.iterationType;
4487
+ if (iterationType) actualIterationType = iterationType;
4488
+ let key;
4489
+ if (typeof keyNodeEntryOrPredicate === "function") {
4490
+ const results = this.search(keyNodeEntryOrPredicate, true);
4491
+ if (results.length === 0 || results[0] === void 0) return -1;
4492
+ key = results[0];
4493
+ } else if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
4494
+ return -1;
4495
+ } else if (this.isNode(keyNodeEntryOrPredicate)) {
4496
+ key = keyNodeEntryOrPredicate.key;
4497
+ } else if (Array.isArray(keyNodeEntryOrPredicate)) {
4498
+ key = keyNodeEntryOrPredicate[0] ?? void 0;
4499
+ if (key === void 0 || key === null) return -1;
4500
+ } else {
4501
+ key = keyNodeEntryOrPredicate;
4502
+ }
4503
+ if (key === void 0) return -1;
4504
+ return actualIterationType === "RECURSIVE" ? this._getRankRecursive(this._root, key) : this._getRankIterative(this._root, key);
4505
+ }
4506
+ rangeByRank(start, end, callback = this._DEFAULT_NODE_CALLBACK, iterationType = this.iterationType) {
4507
+ if (!this._enableOrderStatistic) {
4508
+ raise(Error, ERR.orderStatisticNotEnabled("rangeByRank"));
4509
+ }
4510
+ if (this._size === 0) return [];
4511
+ const lo = Math.max(0, start);
4512
+ const hi = Math.min(this._size - 1, end);
4513
+ if (lo > hi) return [];
4514
+ let actualCallback = void 0;
4515
+ let actualIterationType = this.iterationType;
4516
+ if (typeof callback === "string") {
4517
+ actualIterationType = callback;
4518
+ } else if (callback) {
4519
+ actualCallback = callback;
4520
+ if (iterationType) {
4521
+ actualIterationType = iterationType;
4522
+ }
4523
+ }
4524
+ const results = [];
4525
+ const count = hi - lo + 1;
4526
+ const startNode = actualIterationType === "RECURSIVE" ? this._getByRankRecursive(this._root, lo) : this._getByRankIterative(this._root, lo);
4527
+ if (!startNode) return [];
4528
+ let collected = 0;
4529
+ const cb = actualCallback ?? this._DEFAULT_NODE_CALLBACK;
4530
+ let current = startNode;
4531
+ while (current && collected < count) {
4532
+ results.push(cb(current));
4533
+ collected++;
4534
+ if (collected < count) {
4535
+ current = this._next(current);
4536
+ }
4537
+ }
4538
+ return results;
4539
+ }
3708
4540
  /**
3709
4541
  * Adds a new node to the BST based on key comparison.
3710
4542
  * @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
@@ -3732,6 +4564,78 @@ var BST = class extends BinaryTree {
3732
4564
 
3733
4565
 
3734
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
+
4638
+
3735
4639
 
3736
4640
 
3737
4641
 
@@ -3751,6 +4655,7 @@ var BST = class extends BinaryTree {
3751
4655
  this._setRoot(newNode);
3752
4656
  if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
3753
4657
  this._size++;
4658
+ this._updateCount(newNode);
3754
4659
  return true;
3755
4660
  }
3756
4661
  let current = this._root;
@@ -3764,6 +4669,7 @@ var BST = class extends BinaryTree {
3764
4669
  current.left = newNode;
3765
4670
  if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
3766
4671
  this._size++;
4672
+ this._updateCountAlongPath(newNode);
3767
4673
  return true;
3768
4674
  }
3769
4675
  if (current.left !== null) current = current.left;
@@ -3772,6 +4678,7 @@ var BST = class extends BinaryTree {
3772
4678
  current.right = newNode;
3773
4679
  if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
3774
4680
  this._size++;
4681
+ this._updateCountAlongPath(newNode);
3775
4682
  return true;
3776
4683
  }
3777
4684
  if (current.right !== null) current = current.right;
@@ -3801,6 +4708,54 @@ var BST = class extends BinaryTree {
3801
4708
 
3802
4709
 
3803
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
+
4758
+
3804
4759
  * @example
3805
4760
  * // Set multiple key-value pairs
3806
4761
  * const bst = new BST<number, string>();
@@ -4064,6 +5019,30 @@ var BST = class extends BinaryTree {
4064
5019
 
4065
5020
 
4066
5021
 
5022
+
5023
+
5024
+
5025
+
5026
+
5027
+
5028
+
5029
+
5030
+
5031
+
5032
+
5033
+
5034
+
5035
+
5036
+
5037
+
5038
+
5039
+
5040
+
5041
+
5042
+
5043
+
5044
+
5045
+
4067
5046
  * @example
4068
5047
  * // Rebalance the tree
4069
5048
  * const bst = new BST<number>();
@@ -4109,6 +5088,30 @@ var BST = class extends BinaryTree {
4109
5088
 
4110
5089
 
4111
5090
 
5091
+
5092
+
5093
+
5094
+
5095
+
5096
+
5097
+
5098
+
5099
+
5100
+
5101
+
5102
+
5103
+
5104
+
5105
+
5106
+
5107
+
5108
+
5109
+
5110
+
5111
+
5112
+
5113
+
5114
+
4112
5115
  * @example
4113
5116
  * // Check if tree is height-balanced
4114
5117
  * const bst = new BST<number>([3, 1, 5, 2, 4]);
@@ -4183,6 +5186,54 @@ var BST = class extends BinaryTree {
4183
5186
 
4184
5187
 
4185
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
+
5236
+
4186
5237
  * @example
4187
5238
  * // Transform to new tree
4188
5239
  * const bst = new BST<number, number>([[1, 10], [2, 20], [3, 30]]);
@@ -4257,13 +5308,11 @@ var BST = class extends BinaryTree {
4257
5308
  if (a instanceof Date && b instanceof Date) {
4258
5309
  const ta = a.getTime();
4259
5310
  const tb = b.getTime();
4260
- if (Number.isNaN(ta) || Number.isNaN(tb)) throw new TypeError(ERR.invalidDate("BST"));
5311
+ if (Number.isNaN(ta) || Number.isNaN(tb)) raise(TypeError, ERR.invalidDate("BST"));
4261
5312
  return ta > tb ? 1 : ta < tb ? -1 : 0;
4262
5313
  }
4263
5314
  if (typeof a === "object" || typeof b === "object") {
4264
- throw new TypeError(
4265
- ERR.comparatorRequired("BST")
4266
- );
5315
+ raise(TypeError, ERR.comparatorRequired("BST"));
4267
5316
  }
4268
5317
  return 0;
4269
5318
  };
@@ -4582,7 +5631,8 @@ var BST = class extends BinaryTree {
4582
5631
  _snapshotOptions() {
4583
5632
  return {
4584
5633
  ...super._snapshotOptions(),
4585
- comparator: this._comparator
5634
+ comparator: this._comparator,
5635
+ enableOrderStatistic: this._enableOrderStatistic
4586
5636
  };
4587
5637
  }
4588
5638
  /**
@@ -4604,6 +5654,113 @@ var BST = class extends BinaryTree {
4604
5654
  *
4605
5655
  * @param v - The node to set as root.
4606
5656
  */
5657
+ /**
5658
+ * (Protected) Recalculates the subtree count for a single node.
5659
+ * @remarks Time O(1). Only active when enableOrderStatistic is true.
5660
+ */
5661
+ _updateCount(node) {
5662
+ if (!this._enableOrderStatistic) return;
5663
+ node._count = 1 + (this.isRealNode(node.left) ? node.left._count : 0) + (this.isRealNode(node.right) ? node.right._count : 0);
5664
+ }
5665
+ /**
5666
+ * (Protected) Updates subtree counts from a node up to the root.
5667
+ * @remarks Time O(log n). Only active when enableOrderStatistic is true.
5668
+ */
5669
+ _updateCountAlongPath(node) {
5670
+ if (!this._enableOrderStatistic) return;
5671
+ let current = node;
5672
+ while (current) {
5673
+ this._updateCount(current);
5674
+ current = current.parent;
5675
+ }
5676
+ }
5677
+ /**
5678
+ * (Protected) Finds the node at position k in tree order (iterative).
5679
+ * @remarks Time O(log n), Space O(1)
5680
+ */
5681
+ _getByRankIterative(node, k) {
5682
+ let current = node;
5683
+ let remaining = k;
5684
+ while (current) {
5685
+ const leftCount = this.isRealNode(current.left) ? current.left._count : 0;
5686
+ if (remaining < leftCount) {
5687
+ current = current.left;
5688
+ } else if (remaining === leftCount) {
5689
+ return current;
5690
+ } else {
5691
+ remaining = remaining - leftCount - 1;
5692
+ current = current.right;
5693
+ }
5694
+ }
5695
+ return void 0;
5696
+ }
5697
+ /**
5698
+ * (Protected) Finds the node at position k in tree order (recursive).
5699
+ * @remarks Time O(log n), Space O(log n) call stack
5700
+ */
5701
+ _getByRankRecursive(node, k) {
5702
+ if (!node) return void 0;
5703
+ const leftCount = this.isRealNode(node.left) ? node.left._count : 0;
5704
+ if (k < leftCount) return this._getByRankRecursive(node.left, k);
5705
+ if (k === leftCount) return node;
5706
+ return this._getByRankRecursive(node.right, k - leftCount - 1);
5707
+ }
5708
+ /**
5709
+ * (Protected) Computes the rank of a key iteratively.
5710
+ * @remarks Time O(log n), Space O(1)
5711
+ */
5712
+ _getRankIterative(node, key) {
5713
+ let rank = 0;
5714
+ let current = node;
5715
+ while (this.isRealNode(current)) {
5716
+ const cmp = this._compare(current.key, key);
5717
+ if (cmp > 0) {
5718
+ current = current.left;
5719
+ } else if (cmp < 0) {
5720
+ rank += (this.isRealNode(current.left) ? current.left._count : 0) + 1;
5721
+ current = current.right;
5722
+ } else {
5723
+ rank += this.isRealNode(current.left) ? current.left._count : 0;
5724
+ return rank;
5725
+ }
5726
+ }
5727
+ return rank;
5728
+ }
5729
+ /**
5730
+ * (Protected) Computes the rank of a key recursively.
5731
+ * @remarks Time O(log n), Space O(log n) call stack
5732
+ */
5733
+ _getRankRecursive(node, key) {
5734
+ if (!node) return 0;
5735
+ const cmp = this._compare(node.key, key);
5736
+ if (cmp > 0) {
5737
+ return this._getRankRecursive(node.left, key);
5738
+ } else if (cmp < 0) {
5739
+ return (this.isRealNode(node.left) ? node.left._count : 0) + 1 + this._getRankRecursive(node.right, key);
5740
+ } else {
5741
+ return this.isRealNode(node.left) ? node.left._count : 0;
5742
+ }
5743
+ }
5744
+ /**
5745
+ * (Protected) Finds the in-order successor of a node.
5746
+ * @remarks Time O(log n), Space O(1)
5747
+ */
5748
+ _next(node) {
5749
+ if (this.isRealNode(node.right)) {
5750
+ let current2 = node.right;
5751
+ while (this.isRealNode(current2.left)) {
5752
+ current2 = current2.left;
5753
+ }
5754
+ return current2;
5755
+ }
5756
+ let current = node;
5757
+ let parent = current.parent;
5758
+ while (parent && current === parent.right) {
5759
+ current = parent;
5760
+ parent = parent.parent;
5761
+ }
5762
+ return parent;
5763
+ }
4607
5764
  _setRoot(v) {
4608
5765
  if (v) v.parent = void 0;
4609
5766
  this._root = v;
@@ -4650,21 +5807,28 @@ var BST = class extends BinaryTree {
4650
5807
  while (x.left !== void 0 && x.left !== null) x = x.left;
4651
5808
  return x;
4652
5809
  }, "minNode");
5810
+ let countUpdateStart;
4653
5811
  if (node.left === void 0) {
5812
+ countUpdateStart = node.parent;
4654
5813
  transplant(node, node.right);
4655
5814
  } else if (node.right === void 0) {
5815
+ countUpdateStart = node.parent;
4656
5816
  transplant(node, node.left);
4657
5817
  } else {
4658
5818
  const succ = minNode(node.right);
4659
5819
  if (succ.parent !== node) {
5820
+ countUpdateStart = succ.parent;
4660
5821
  transplant(succ, succ.right);
4661
5822
  succ.right = node.right;
4662
5823
  if (succ.right) succ.right.parent = succ;
5824
+ } else {
5825
+ countUpdateStart = succ;
4663
5826
  }
4664
5827
  transplant(node, succ);
4665
5828
  succ.left = node.left;
4666
5829
  if (succ.left) succ.left.parent = succ;
4667
5830
  }
5831
+ this._updateCountAlongPath(countUpdateStart);
4668
5832
  this._size = Math.max(0, this._size - 1);
4669
5833
  return true;
4670
5834
  }
@@ -4677,6 +5841,6 @@ var BST = class extends BinaryTree {
4677
5841
  * @license MIT License
4678
5842
  */
4679
5843
 
4680
- export { BST, BSTNode, BinaryTree, BinaryTreeNode, DFSOperation, ERR, Range };
5844
+ export { BST, BSTNode, BinaryTree, BinaryTreeNode, DFSOperation, ERR, Range, raise };
4681
5845
  //# sourceMappingURL=index.mjs.map
4682
5846
  //# sourceMappingURL=index.mjs.map