red-black-tree-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 +1795 -166
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +1782 -152
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +1795 -167
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +1782 -153
  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/red-black-tree-typed.js +1784 -155
  47. package/dist/umd/red-black-tree-typed.js.map +1 -1
  48. package/dist/umd/red-black-tree-typed.min.js +3 -3
  49. package/dist/umd/red-black-tree-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,61 @@ 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 {
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
+ static {
107
+ __name(this, "Range");
108
+ }
109
+ // Determine whether a key is within the range
110
+ isInRange(key, comparator) {
111
+ const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
112
+ const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
113
+ return lowCheck && highCheck;
114
+ }
115
+ };
116
+
62
117
  // src/data-structures/base/iterable-element-base.ts
63
118
  var IterableElementBase = class {
64
119
  static {
@@ -77,7 +132,7 @@ var IterableElementBase = class {
77
132
  if (options) {
78
133
  const { toElementFn } = options;
79
134
  if (typeof toElementFn === "function") this._toElementFn = toElementFn;
80
- else if (toElementFn) throw new TypeError("toElementFn must be a function type");
135
+ else if (toElementFn) raise(TypeError, "toElementFn must be a function type");
81
136
  }
82
137
  }
83
138
  /**
@@ -240,7 +295,7 @@ var IterableElementBase = class {
240
295
  acc = initialValue;
241
296
  } else {
242
297
  const first = iter.next();
243
- if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
298
+ if (first.done) raise(TypeError, "Reduce of empty structure with no initial value");
244
299
  acc = first.value;
245
300
  index = 1;
246
301
  }
@@ -475,55 +530,6 @@ var LinearBase = class _LinearBase extends IterableElementBase {
475
530
  }
476
531
  };
477
532
 
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 {
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
- static {
517
- __name(this, "Range");
518
- }
519
- // Determine whether a key is within the range
520
- isInRange(key, comparator) {
521
- const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
522
- const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
523
- return lowCheck && highCheck;
524
- }
525
- };
526
-
527
533
  // src/data-structures/base/iterable-entry-base.ts
528
534
  var IterableEntryBase = class {
529
535
  static {
@@ -777,6 +783,30 @@ var Queue = class _Queue extends LinearBase {
777
783
 
778
784
 
779
785
 
786
+
787
+
788
+
789
+
790
+
791
+
792
+
793
+
794
+
795
+
796
+
797
+
798
+
799
+
800
+
801
+
802
+
803
+
804
+
805
+
806
+
807
+
808
+
809
+
780
810
  * @example
781
811
  * // Track queue length
782
812
  * const q = new Queue<number>();
@@ -803,6 +833,30 @@ var Queue = class _Queue extends LinearBase {
803
833
 
804
834
 
805
835
 
836
+
837
+
838
+
839
+
840
+
841
+
842
+
843
+
844
+
845
+
846
+
847
+
848
+
849
+
850
+
851
+
852
+
853
+
854
+
855
+
856
+
857
+
858
+
859
+
806
860
  * @example
807
861
  * // View the front element
808
862
  * const q = new Queue<string>(['first', 'second', 'third']);
@@ -845,6 +899,30 @@ var Queue = class _Queue extends LinearBase {
845
899
 
846
900
 
847
901
 
902
+
903
+
904
+
905
+
906
+
907
+
908
+
909
+
910
+
911
+
912
+
913
+
914
+
915
+
916
+
917
+
918
+
919
+
920
+
921
+
922
+
923
+
924
+
925
+
848
926
  * @example
849
927
  * // Queue for...of iteration and isEmpty check
850
928
  * const queue = new Queue<string>(['A', 'B', 'C', 'D']);
@@ -883,6 +961,30 @@ var Queue = class _Queue extends LinearBase {
883
961
 
884
962
 
885
963
 
964
+
965
+
966
+
967
+
968
+
969
+
970
+
971
+
972
+
973
+
974
+
975
+
976
+
977
+
978
+
979
+
980
+
981
+
982
+
983
+
984
+
985
+
986
+
987
+
886
988
  * @example
887
989
  * // basic Queue creation and push operation
888
990
  * // Create a simple Queue with initial values
@@ -928,6 +1030,30 @@ var Queue = class _Queue extends LinearBase {
928
1030
 
929
1031
 
930
1032
 
1033
+
1034
+
1035
+
1036
+
1037
+
1038
+
1039
+
1040
+
1041
+
1042
+
1043
+
1044
+
1045
+
1046
+
1047
+
1048
+
1049
+
1050
+
1051
+
1052
+
1053
+
1054
+
1055
+
1056
+
931
1057
  * @example
932
1058
  * // Queue shift and peek operations
933
1059
  * const queue = new Queue<number>([10, 20, 30, 40]);
@@ -963,6 +1089,30 @@ var Queue = class _Queue extends LinearBase {
963
1089
 
964
1090
 
965
1091
 
1092
+
1093
+
1094
+
1095
+
1096
+
1097
+
1098
+
1099
+
1100
+
1101
+
1102
+
1103
+
1104
+
1105
+
1106
+
1107
+
1108
+
1109
+
1110
+
1111
+
1112
+
1113
+
1114
+
1115
+
966
1116
  * @example
967
1117
  * // Remove specific element
968
1118
  * const q = new Queue<number>([1, 2, 3, 2]);
@@ -991,21 +1141,45 @@ var Queue = class _Queue extends LinearBase {
991
1141
 
992
1142
 
993
1143
 
994
- * @example
995
- * // Access element by index
996
- * const q = new Queue<string>(['a', 'b', 'c']);
997
- * console.log(q.at(0)); // 'a';
998
- * console.log(q.at(2)); // 'c';
999
- */
1000
- at(index) {
1001
- if (index < 0 || index >= this.length) return void 0;
1002
- return this._elements[this._offset + index];
1003
- }
1004
- /**
1005
- * Delete the element at a given index.
1006
- * @remarks Time O(N), Space O(1)
1007
- * @param index - Zero-based index from the front.
1008
- * @returns Removed element or undefined.
1144
+
1145
+
1146
+
1147
+
1148
+
1149
+
1150
+
1151
+
1152
+
1153
+
1154
+
1155
+
1156
+
1157
+
1158
+
1159
+
1160
+
1161
+
1162
+
1163
+
1164
+
1165
+
1166
+
1167
+
1168
+ * @example
1169
+ * // Access element by index
1170
+ * const q = new Queue<string>(['a', 'b', 'c']);
1171
+ * console.log(q.at(0)); // 'a';
1172
+ * console.log(q.at(2)); // 'c';
1173
+ */
1174
+ at(index) {
1175
+ if (index < 0 || index >= this.length) return void 0;
1176
+ return this._elements[this._offset + index];
1177
+ }
1178
+ /**
1179
+ * Delete the element at a given index.
1180
+ * @remarks Time O(N), Space O(1)
1181
+ * @param index - Zero-based index from the front.
1182
+ * @returns Removed element or undefined.
1009
1183
  */
1010
1184
  deleteAt(index) {
1011
1185
  if (index < 0 || index >= this.length) return void 0;
@@ -1060,6 +1234,30 @@ var Queue = class _Queue extends LinearBase {
1060
1234
 
1061
1235
 
1062
1236
 
1237
+
1238
+
1239
+
1240
+
1241
+
1242
+
1243
+
1244
+
1245
+
1246
+
1247
+
1248
+
1249
+
1250
+
1251
+
1252
+
1253
+
1254
+
1255
+
1256
+
1257
+
1258
+
1259
+
1260
+
1063
1261
  * @example
1064
1262
  * // Remove all elements
1065
1263
  * const q = new Queue<number>([1, 2, 3]);
@@ -1082,6 +1280,30 @@ var Queue = class _Queue extends LinearBase {
1082
1280
 
1083
1281
 
1084
1282
 
1283
+
1284
+
1285
+
1286
+
1287
+
1288
+
1289
+
1290
+
1291
+
1292
+
1293
+
1294
+
1295
+
1296
+
1297
+
1298
+
1299
+
1300
+
1301
+
1302
+
1303
+
1304
+
1305
+
1306
+
1085
1307
  * @example
1086
1308
  * // Reclaim unused memory
1087
1309
  * const q = new Queue<number>([1, 2, 3, 4, 5]);
@@ -1127,6 +1349,30 @@ var Queue = class _Queue extends LinearBase {
1127
1349
 
1128
1350
 
1129
1351
 
1352
+
1353
+
1354
+
1355
+
1356
+
1357
+
1358
+
1359
+
1360
+
1361
+
1362
+
1363
+
1364
+
1365
+
1366
+
1367
+
1368
+
1369
+
1370
+
1371
+
1372
+
1373
+
1374
+
1375
+
1130
1376
  * @example
1131
1377
  * // Create independent copy
1132
1378
  * const q = new Queue<number>([1, 2, 3]);
@@ -1156,6 +1402,30 @@ var Queue = class _Queue extends LinearBase {
1156
1402
 
1157
1403
 
1158
1404
 
1405
+
1406
+
1407
+
1408
+
1409
+
1410
+
1411
+
1412
+
1413
+
1414
+
1415
+
1416
+
1417
+
1418
+
1419
+
1420
+
1421
+
1422
+
1423
+
1424
+
1425
+
1426
+
1427
+
1428
+
1159
1429
  * @example
1160
1430
  * // Filter elements
1161
1431
  * const q = new Queue<number>([1, 2, 3, 4, 5]);
@@ -1189,6 +1459,30 @@ var Queue = class _Queue extends LinearBase {
1189
1459
 
1190
1460
 
1191
1461
 
1462
+
1463
+
1464
+
1465
+
1466
+
1467
+
1468
+
1469
+
1470
+
1471
+
1472
+
1473
+
1474
+
1475
+
1476
+
1477
+
1478
+
1479
+
1480
+
1481
+
1482
+
1483
+
1484
+
1485
+
1192
1486
  * @example
1193
1487
  * // Transform elements
1194
1488
  * const q = new Queue<number>([1, 2, 3]);
@@ -1439,7 +1733,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
1439
1733
  if (isMapMode !== void 0) this._isMapMode = isMapMode;
1440
1734
  if (isDuplicate !== void 0) this._isDuplicate = isDuplicate;
1441
1735
  if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
1442
- else if (toEntryFn) throw new TypeError(ERR.notAFunction("toEntryFn", "BinaryTree"));
1736
+ else if (toEntryFn) raise(TypeError, ERR.notAFunction("toEntryFn", "BinaryTree"));
1443
1737
  }
1444
1738
  if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
1445
1739
  }
@@ -1665,6 +1959,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
1665
1959
 
1666
1960
 
1667
1961
 
1962
+
1963
+
1964
+
1965
+
1966
+
1967
+
1968
+
1969
+
1970
+
1971
+
1972
+
1973
+
1974
+
1975
+
1976
+
1977
+
1978
+
1979
+
1980
+
1981
+
1982
+
1983
+
1984
+
1985
+
1668
1986
  * @example
1669
1987
  * // Add a single node
1670
1988
  * const tree = new BinaryTree<number>();
@@ -1695,6 +2013,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
1695
2013
 
1696
2014
 
1697
2015
 
2016
+
2017
+
2018
+
2019
+
2020
+
2021
+
2022
+
2023
+
2024
+
2025
+
2026
+
2027
+
2028
+
2029
+
2030
+
2031
+
2032
+
2033
+
2034
+
2035
+
2036
+
2037
+
2038
+
2039
+
1698
2040
  * @example
1699
2041
  * // basic BinaryTree creation and insertion
1700
2042
  * // Create a BinaryTree with entries
@@ -1777,6 +2119,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
1777
2119
 
1778
2120
 
1779
2121
 
2122
+
2123
+
2124
+
2125
+
2126
+
2127
+
2128
+
2129
+
2130
+
2131
+
2132
+
2133
+
2134
+
2135
+
2136
+
2137
+
2138
+
2139
+
2140
+
2141
+
2142
+
2143
+
2144
+
2145
+
1780
2146
  * @example
1781
2147
  * // Bulk add
1782
2148
  * const tree = new BinaryTree<number>();
@@ -1795,6 +2161,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
1795
2161
  * @returns An array of booleans indicating the success of each individual `set` operation.
1796
2162
 
1797
2163
 
2164
+
2165
+
2166
+
2167
+
2168
+
2169
+
2170
+
2171
+
2172
+
2173
+
2174
+
2175
+
2176
+
2177
+
2178
+
2179
+
2180
+
2181
+
2182
+
2183
+
2184
+
2185
+
2186
+
2187
+
1798
2188
  * @example
1799
2189
  * // Set multiple entries
1800
2190
  * const tree = new BinaryTree<number, string>();
@@ -1834,7 +2224,31 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
1834
2224
 
1835
2225
 
1836
2226
 
1837
- * @example
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
+ * @example
1838
2252
  * // Combine trees
1839
2253
  * const t1 = new BinaryTree<number>([1, 2]);
1840
2254
  * const t2 = new BinaryTree<number>([3, 4]);
@@ -1872,6 +2286,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
1872
2286
 
1873
2287
 
1874
2288
 
2289
+
2290
+
2291
+
2292
+
2293
+
2294
+
2295
+
2296
+
2297
+
2298
+
2299
+
2300
+
2301
+
2302
+
2303
+
2304
+
2305
+
2306
+
2307
+
2308
+
2309
+
2310
+
2311
+
2312
+
1875
2313
  * @example
1876
2314
  * // Remove a node
1877
2315
  * const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
@@ -1988,6 +2426,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
1988
2426
 
1989
2427
 
1990
2428
 
2429
+
2430
+
2431
+
2432
+
2433
+
2434
+
2435
+
2436
+
2437
+
2438
+
2439
+
2440
+
2441
+
2442
+
2443
+
2444
+
2445
+
2446
+
2447
+
2448
+
2449
+
2450
+
2451
+
2452
+
1991
2453
  * @example
1992
2454
  * // Get node by key
1993
2455
  * const tree = new BinaryTree<number, string>([[1, 'root'], [2, 'child']]);
@@ -2022,6 +2484,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2022
2484
 
2023
2485
 
2024
2486
 
2487
+
2488
+
2489
+
2490
+
2491
+
2492
+
2493
+
2494
+
2495
+
2496
+
2497
+
2498
+
2499
+
2500
+
2501
+
2502
+
2503
+
2504
+
2505
+
2506
+
2507
+
2508
+
2509
+
2510
+
2025
2511
  * @example
2026
2512
  * // Retrieve value by key
2027
2513
  * const tree = new BinaryTree<number, string>([[1, 'root'], [2, 'left'], [3, 'right']]);
@@ -2058,6 +2544,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2058
2544
 
2059
2545
 
2060
2546
 
2547
+
2548
+
2549
+
2550
+
2551
+
2552
+
2553
+
2554
+
2555
+
2556
+
2557
+
2558
+
2559
+
2560
+
2561
+
2562
+
2563
+
2564
+
2565
+
2566
+
2567
+
2568
+
2569
+
2570
+
2061
2571
  * @example
2062
2572
  * // Remove all nodes
2063
2573
  * const tree = new BinaryTree<number>([1, 2, 3]);
@@ -2082,6 +2592,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2082
2592
 
2083
2593
 
2084
2594
 
2595
+
2596
+
2597
+
2598
+
2599
+
2600
+
2601
+
2602
+
2603
+
2604
+
2605
+
2606
+
2607
+
2608
+
2609
+
2610
+
2611
+
2612
+
2613
+
2614
+
2615
+
2616
+
2617
+
2618
+
2085
2619
  * @example
2086
2620
  * // Check empty
2087
2621
  * console.log(new BinaryTree().isEmpty()); // true;
@@ -2115,6 +2649,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2115
2649
 
2116
2650
 
2117
2651
 
2652
+
2653
+
2654
+
2655
+
2656
+
2657
+
2658
+
2659
+
2660
+
2661
+
2662
+
2663
+
2664
+
2665
+
2666
+
2667
+
2668
+
2669
+
2670
+
2671
+
2672
+
2673
+
2674
+
2675
+
2118
2676
  * @example
2119
2677
  * // Check BST property
2120
2678
  * const tree = new BinaryTree<number>([1, 2, 3]);
@@ -2175,6 +2733,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2175
2733
 
2176
2734
 
2177
2735
 
2736
+
2737
+
2738
+
2739
+
2740
+
2741
+
2742
+
2743
+
2744
+
2745
+
2746
+
2747
+
2748
+
2749
+
2750
+
2751
+
2752
+
2753
+
2754
+
2755
+
2756
+
2757
+
2758
+
2759
+
2178
2760
  * @example
2179
2761
  * // Get depth of a node
2180
2762
  * const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
@@ -2212,6 +2794,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2212
2794
 
2213
2795
 
2214
2796
 
2797
+
2798
+
2799
+
2800
+
2801
+
2802
+
2803
+
2804
+
2805
+
2806
+
2807
+
2808
+
2809
+
2810
+
2811
+
2812
+
2813
+
2814
+
2815
+
2816
+
2817
+
2818
+
2819
+
2820
+
2215
2821
  * @example
2216
2822
  * // Get tree height
2217
2823
  * const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
@@ -2665,6 +3271,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2665
3271
 
2666
3272
 
2667
3273
 
3274
+
3275
+
3276
+
3277
+
3278
+
3279
+
3280
+
3281
+
3282
+
3283
+
3284
+
3285
+
3286
+
3287
+
3288
+
3289
+
3290
+
3291
+
3292
+
3293
+
3294
+
3295
+
3296
+
3297
+
2668
3298
  * @example
2669
3299
  * // Deep copy
2670
3300
  * const tree = new BinaryTree<number>([1, 2, 3]);
@@ -2693,21 +3323,45 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2693
3323
 
2694
3324
 
2695
3325
 
2696
- * @example
2697
- * // Filter nodes by condition
2698
- * const tree = new BinaryTree<number>([1, 2, 3, 4]);
2699
- * const result = tree.filter((_, key) => key > 2);
2700
- * console.log(result.size); // 2;
2701
- */
2702
- filter(predicate, thisArg) {
2703
- const out = this._createInstance();
2704
- let i = 0;
2705
- for (const [k, v] of this) if (predicate.call(thisArg, v, k, i++, this)) out.set([k, v]);
2706
- return out;
2707
- }
2708
- /**
2709
- * Creates a new tree by mapping each [key, value] pair to a new entry.
2710
- * @remarks Time O(N * M), where N is nodes in this tree, and M is size of the new tree during insertion. Space O(N) for the new tree.
3326
+
3327
+
3328
+
3329
+
3330
+
3331
+
3332
+
3333
+
3334
+
3335
+
3336
+
3337
+
3338
+
3339
+
3340
+
3341
+
3342
+
3343
+
3344
+
3345
+
3346
+
3347
+
3348
+
3349
+
3350
+ * @example
3351
+ * // Filter nodes by condition
3352
+ * const tree = new BinaryTree<number>([1, 2, 3, 4]);
3353
+ * const result = tree.filter((_, key) => key > 2);
3354
+ * console.log(result.size); // 2;
3355
+ */
3356
+ filter(predicate, thisArg) {
3357
+ const out = this._createInstance();
3358
+ let i = 0;
3359
+ for (const [k, v] of this) if (predicate.call(thisArg, v, k, i++, this)) out.set([k, v]);
3360
+ return out;
3361
+ }
3362
+ /**
3363
+ * Creates a new tree by mapping each [key, value] pair to a new entry.
3364
+ * @remarks Time O(N * M), where N is nodes in this tree, and M is size of the new tree during insertion. Space O(N) for the new tree.
2711
3365
  *
2712
3366
  * @template MK - New key type.
2713
3367
  * @template MV - New value type.
@@ -2725,6 +3379,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2725
3379
 
2726
3380
 
2727
3381
 
3382
+
3383
+
3384
+
3385
+
3386
+
3387
+
3388
+
3389
+
3390
+
3391
+
3392
+
3393
+
3394
+
3395
+
3396
+
3397
+
3398
+
3399
+
3400
+
3401
+
3402
+
3403
+
3404
+
3405
+
2728
3406
  * @example
2729
3407
  * // Transform to new tree
2730
3408
  * const tree = new BinaryTree<number, number>([[1, 10], [2, 20]]);
@@ -2782,6 +3460,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2782
3460
 
2783
3461
 
2784
3462
 
3463
+
3464
+
3465
+
3466
+
3467
+
3468
+
3469
+
3470
+
3471
+
3472
+
3473
+
3474
+
3475
+
3476
+
3477
+
3478
+
3479
+
3480
+
3481
+
3482
+
3483
+
3484
+
3485
+
3486
+
2785
3487
  * @example
2786
3488
  * // Display tree
2787
3489
  * const tree = new BinaryTree<number>([1, 2, 3]);
@@ -3413,12 +4115,16 @@ var BST = class extends BinaryTree {
3413
4115
  } else {
3414
4116
  this._comparator = this._createDefaultComparator();
3415
4117
  }
4118
+ if (options.enableOrderStatistic) {
4119
+ this._enableOrderStatistic = true;
4120
+ }
3416
4121
  } else {
3417
4122
  this._comparator = this._createDefaultComparator();
3418
4123
  }
3419
4124
  if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
3420
4125
  }
3421
4126
  _root = void 0;
4127
+ _enableOrderStatistic = false;
3422
4128
  /**
3423
4129
  * Gets the root node of the tree.
3424
4130
  * @remarks Time O(1)
@@ -3553,6 +4259,54 @@ var BST = class extends BinaryTree {
3553
4259
 
3554
4260
 
3555
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
+
4308
+
4309
+
3556
4310
  * @example
3557
4311
  * // Get node object by key
3558
4312
  * const bst = new BST<number, string>([[5, 'root'], [3, 'left'], [7, 'right']]);
@@ -3707,6 +4461,84 @@ var BST = class extends BinaryTree {
3707
4461
  const searchRange = range instanceof Range ? range : new Range(range[0], range[1]);
3708
4462
  return this.search(searchRange, false, callback, startNode, iterationType);
3709
4463
  }
4464
+ getByRank(k, callback = this._DEFAULT_NODE_CALLBACK, iterationType = this.iterationType) {
4465
+ if (!this._enableOrderStatistic) {
4466
+ raise(Error, ERR.orderStatisticNotEnabled("getByRank"));
4467
+ }
4468
+ if (k < 0 || k >= this._size) return void 0;
4469
+ let actualCallback = void 0;
4470
+ let actualIterationType = this.iterationType;
4471
+ if (typeof callback === "string") {
4472
+ actualIterationType = callback;
4473
+ } else if (callback) {
4474
+ actualCallback = callback;
4475
+ if (iterationType) {
4476
+ actualIterationType = iterationType;
4477
+ }
4478
+ }
4479
+ const node = actualIterationType === "RECURSIVE" ? this._getByRankRecursive(this._root, k) : this._getByRankIterative(this._root, k);
4480
+ if (!node) return void 0;
4481
+ return actualCallback ? actualCallback(node) : node.key;
4482
+ }
4483
+ getRank(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
4484
+ if (!this._enableOrderStatistic) {
4485
+ raise(Error, ERR.orderStatisticNotEnabled("getRank"));
4486
+ }
4487
+ if (!this._root || this._size === 0) return -1;
4488
+ let actualIterationType = this.iterationType;
4489
+ if (iterationType) actualIterationType = iterationType;
4490
+ let key;
4491
+ if (typeof keyNodeEntryOrPredicate === "function") {
4492
+ const results = this.search(keyNodeEntryOrPredicate, true);
4493
+ if (results.length === 0 || results[0] === void 0) return -1;
4494
+ key = results[0];
4495
+ } else if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
4496
+ return -1;
4497
+ } else if (this.isNode(keyNodeEntryOrPredicate)) {
4498
+ key = keyNodeEntryOrPredicate.key;
4499
+ } else if (Array.isArray(keyNodeEntryOrPredicate)) {
4500
+ key = keyNodeEntryOrPredicate[0] ?? void 0;
4501
+ if (key === void 0 || key === null) return -1;
4502
+ } else {
4503
+ key = keyNodeEntryOrPredicate;
4504
+ }
4505
+ if (key === void 0) return -1;
4506
+ return actualIterationType === "RECURSIVE" ? this._getRankRecursive(this._root, key) : this._getRankIterative(this._root, key);
4507
+ }
4508
+ rangeByRank(start, end, callback = this._DEFAULT_NODE_CALLBACK, iterationType = this.iterationType) {
4509
+ if (!this._enableOrderStatistic) {
4510
+ raise(Error, ERR.orderStatisticNotEnabled("rangeByRank"));
4511
+ }
4512
+ if (this._size === 0) return [];
4513
+ const lo = Math.max(0, start);
4514
+ const hi = Math.min(this._size - 1, end);
4515
+ if (lo > hi) return [];
4516
+ let actualCallback = void 0;
4517
+ let actualIterationType = this.iterationType;
4518
+ if (typeof callback === "string") {
4519
+ actualIterationType = callback;
4520
+ } else if (callback) {
4521
+ actualCallback = callback;
4522
+ if (iterationType) {
4523
+ actualIterationType = iterationType;
4524
+ }
4525
+ }
4526
+ const results = [];
4527
+ const count = hi - lo + 1;
4528
+ const startNode = actualIterationType === "RECURSIVE" ? this._getByRankRecursive(this._root, lo) : this._getByRankIterative(this._root, lo);
4529
+ if (!startNode) return [];
4530
+ let collected = 0;
4531
+ const cb = actualCallback ?? this._DEFAULT_NODE_CALLBACK;
4532
+ let current = startNode;
4533
+ while (current && collected < count) {
4534
+ results.push(cb(current));
4535
+ collected++;
4536
+ if (collected < count) {
4537
+ current = this._next(current);
4538
+ }
4539
+ }
4540
+ return results;
4541
+ }
3710
4542
  /**
3711
4543
  * Adds a new node to the BST based on key comparison.
3712
4544
  * @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
@@ -3734,6 +4566,78 @@ var BST = class extends BinaryTree {
3734
4566
 
3735
4567
 
3736
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
+
4639
+
4640
+
3737
4641
 
3738
4642
 
3739
4643
 
@@ -3753,6 +4657,7 @@ var BST = class extends BinaryTree {
3753
4657
  this._setRoot(newNode);
3754
4658
  if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
3755
4659
  this._size++;
4660
+ this._updateCount(newNode);
3756
4661
  return true;
3757
4662
  }
3758
4663
  let current = this._root;
@@ -3766,6 +4671,7 @@ var BST = class extends BinaryTree {
3766
4671
  current.left = newNode;
3767
4672
  if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
3768
4673
  this._size++;
4674
+ this._updateCountAlongPath(newNode);
3769
4675
  return true;
3770
4676
  }
3771
4677
  if (current.left !== null) current = current.left;
@@ -3774,6 +4680,7 @@ var BST = class extends BinaryTree {
3774
4680
  current.right = newNode;
3775
4681
  if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
3776
4682
  this._size++;
4683
+ this._updateCountAlongPath(newNode);
3777
4684
  return true;
3778
4685
  }
3779
4686
  if (current.right !== null) current = current.right;
@@ -3803,20 +4710,68 @@ var BST = class extends BinaryTree {
3803
4710
 
3804
4711
 
3805
4712
 
3806
- * @example
3807
- * // Set multiple key-value pairs
3808
- * const bst = new BST<number, string>();
3809
- * bst.setMany([[1, 'a'], [2, 'b'], [3, 'c']]);
3810
- * console.log(bst.size); // 3;
3811
- * console.log(bst.get(2)); // 'b';
3812
- */
3813
- setMany(keysNodesEntriesOrRaws, values, isBalanceAdd = true, iterationType = this.iterationType) {
3814
- const inserted = [];
3815
- const valuesIterator = values?.[Symbol.iterator]();
3816
- if (!isBalanceAdd) {
3817
- for (let kve of keysNodesEntriesOrRaws) {
3818
- const val = valuesIterator?.next().value;
3819
- if (this.isRaw(kve)) kve = this._toEntryFn(kve);
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
+
4759
+
4760
+
4761
+ * @example
4762
+ * // Set multiple key-value pairs
4763
+ * const bst = new BST<number, string>();
4764
+ * bst.setMany([[1, 'a'], [2, 'b'], [3, 'c']]);
4765
+ * console.log(bst.size); // 3;
4766
+ * console.log(bst.get(2)); // 'b';
4767
+ */
4768
+ setMany(keysNodesEntriesOrRaws, values, isBalanceAdd = true, iterationType = this.iterationType) {
4769
+ const inserted = [];
4770
+ const valuesIterator = values?.[Symbol.iterator]();
4771
+ if (!isBalanceAdd) {
4772
+ for (let kve of keysNodesEntriesOrRaws) {
4773
+ const val = valuesIterator?.next().value;
4774
+ if (this.isRaw(kve)) kve = this._toEntryFn(kve);
3820
4775
  inserted.push(this.set(kve, val));
3821
4776
  }
3822
4777
  return inserted;
@@ -4066,6 +5021,30 @@ var BST = class extends BinaryTree {
4066
5021
 
4067
5022
 
4068
5023
 
5024
+
5025
+
5026
+
5027
+
5028
+
5029
+
5030
+
5031
+
5032
+
5033
+
5034
+
5035
+
5036
+
5037
+
5038
+
5039
+
5040
+
5041
+
5042
+
5043
+
5044
+
5045
+
5046
+
5047
+
4069
5048
  * @example
4070
5049
  * // Rebalance the tree
4071
5050
  * const bst = new BST<number>();
@@ -4111,6 +5090,30 @@ var BST = class extends BinaryTree {
4111
5090
 
4112
5091
 
4113
5092
 
5093
+
5094
+
5095
+
5096
+
5097
+
5098
+
5099
+
5100
+
5101
+
5102
+
5103
+
5104
+
5105
+
5106
+
5107
+
5108
+
5109
+
5110
+
5111
+
5112
+
5113
+
5114
+
5115
+
5116
+
4114
5117
  * @example
4115
5118
  * // Check if tree is height-balanced
4116
5119
  * const bst = new BST<number>([3, 1, 5, 2, 4]);
@@ -4185,6 +5188,54 @@ var BST = class extends BinaryTree {
4185
5188
 
4186
5189
 
4187
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
+
5237
+
5238
+
4188
5239
  * @example
4189
5240
  * // Transform to new tree
4190
5241
  * const bst = new BST<number, number>([[1, 10], [2, 20], [3, 30]]);
@@ -4259,13 +5310,11 @@ var BST = class extends BinaryTree {
4259
5310
  if (a instanceof Date && b instanceof Date) {
4260
5311
  const ta = a.getTime();
4261
5312
  const tb = b.getTime();
4262
- if (Number.isNaN(ta) || Number.isNaN(tb)) throw new TypeError(ERR.invalidDate("BST"));
5313
+ if (Number.isNaN(ta) || Number.isNaN(tb)) raise(TypeError, ERR.invalidDate("BST"));
4263
5314
  return ta > tb ? 1 : ta < tb ? -1 : 0;
4264
5315
  }
4265
5316
  if (typeof a === "object" || typeof b === "object") {
4266
- throw new TypeError(
4267
- ERR.comparatorRequired("BST")
4268
- );
5317
+ raise(TypeError, ERR.comparatorRequired("BST"));
4269
5318
  }
4270
5319
  return 0;
4271
5320
  };
@@ -4584,7 +5633,8 @@ var BST = class extends BinaryTree {
4584
5633
  _snapshotOptions() {
4585
5634
  return {
4586
5635
  ...super._snapshotOptions(),
4587
- comparator: this._comparator
5636
+ comparator: this._comparator,
5637
+ enableOrderStatistic: this._enableOrderStatistic
4588
5638
  };
4589
5639
  }
4590
5640
  /**
@@ -4606,6 +5656,113 @@ var BST = class extends BinaryTree {
4606
5656
  *
4607
5657
  * @param v - The node to set as root.
4608
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
+ }
4609
5766
  _setRoot(v) {
4610
5767
  if (v) v.parent = void 0;
4611
5768
  this._root = v;
@@ -4652,21 +5809,28 @@ var BST = class extends BinaryTree {
4652
5809
  while (x.left !== void 0 && x.left !== null) x = x.left;
4653
5810
  return x;
4654
5811
  }, "minNode");
5812
+ let countUpdateStart;
4655
5813
  if (node.left === void 0) {
5814
+ countUpdateStart = node.parent;
4656
5815
  transplant(node, node.right);
4657
5816
  } else if (node.right === void 0) {
5817
+ countUpdateStart = node.parent;
4658
5818
  transplant(node, node.left);
4659
5819
  } else {
4660
5820
  const succ = minNode(node.right);
4661
5821
  if (succ.parent !== node) {
5822
+ countUpdateStart = succ.parent;
4662
5823
  transplant(succ, succ.right);
4663
5824
  succ.right = node.right;
4664
5825
  if (succ.right) succ.right.parent = succ;
5826
+ } else {
5827
+ countUpdateStart = succ;
4665
5828
  }
4666
5829
  transplant(node, succ);
4667
5830
  succ.left = node.left;
4668
5831
  if (succ.left) succ.left.parent = succ;
4669
5832
  }
5833
+ this._updateCountAlongPath(countUpdateStart);
4670
5834
  this._size = Math.max(0, this._size - 1);
4671
5835
  return true;
4672
5836
  }
@@ -4902,65 +6066,161 @@ var RedBlackTree = class extends BST {
4902
6066
 
4903
6067
 
4904
6068
 
4905
- * @example
4906
- * // Remove all entries
4907
- * const rbt = new RedBlackTree<number>([1, 2, 3]);
4908
- * rbt.clear();
4909
- * console.log(rbt.isEmpty()); // true;
4910
- */
4911
- clear() {
4912
- super.clear();
4913
- this._root = this.NIL;
4914
- this._header.parent = this.NIL;
4915
- this._setMinCache(void 0);
4916
- this._setMaxCache(void 0);
4917
- }
4918
- /**
4919
- * (Internal) Find a node by key using a tight BST walk (no allocations).
4920
- *
4921
- * NOTE: This uses `header.parent` as the canonical root pointer.
4922
- * @remarks Time O(log n) average, Space O(1)
4923
- */
4924
- _findNodeByKey(key) {
4925
- const NIL = this.NIL;
4926
- const cmp = this._compare.bind(this);
4927
- let cur = this._header.parent ?? NIL;
4928
- while (cur !== NIL) {
4929
- const c = cmp(key, cur.key);
4930
- if (c < 0) cur = cur.left ?? NIL;
4931
- else if (c > 0) cur = cur.right ?? NIL;
4932
- else return cur;
4933
- }
4934
- return void 0;
4935
- }
4936
- /**
4937
- * (Internal) In-order predecessor of a node in a BST.
4938
- * @remarks Time O(log n) average, Space O(1)
4939
- */
4940
- _predecessorOf(node) {
4941
- const NIL = this.NIL;
4942
- if (node.left && node.left !== NIL) {
4943
- let cur2 = node.left;
4944
- while (cur2.right && cur2.right !== NIL) cur2 = cur2.right;
4945
- return cur2;
4946
- }
4947
- let cur = node;
4948
- let p = node.parent;
4949
- while (p && cur === p.left) {
4950
- cur = p;
4951
- p = p.parent;
4952
- }
4953
- return p;
4954
- }
4955
- /**
4956
- * (Internal) In-order successor of a node in a BST.
4957
- * @remarks Time O(log n) average, Space O(1)
4958
- */
4959
- _successorOf(node) {
4960
- const NIL = this.NIL;
4961
- if (node.right && node.right !== NIL) {
4962
- let cur2 = node.right;
4963
- while (cur2.left && cur2.left !== NIL) cur2 = cur2.left;
6069
+
6070
+
6071
+
6072
+
6073
+
6074
+
6075
+
6076
+
6077
+
6078
+
6079
+
6080
+
6081
+
6082
+
6083
+
6084
+
6085
+
6086
+
6087
+
6088
+
6089
+
6090
+
6091
+
6092
+
6093
+
6094
+
6095
+
6096
+
6097
+
6098
+
6099
+
6100
+
6101
+
6102
+
6103
+
6104
+
6105
+
6106
+
6107
+
6108
+
6109
+
6110
+
6111
+
6112
+
6113
+
6114
+
6115
+
6116
+
6117
+
6118
+
6119
+
6120
+
6121
+
6122
+
6123
+
6124
+
6125
+
6126
+
6127
+
6128
+
6129
+
6130
+
6131
+
6132
+
6133
+
6134
+
6135
+
6136
+
6137
+
6138
+
6139
+
6140
+
6141
+
6142
+
6143
+
6144
+
6145
+
6146
+
6147
+
6148
+
6149
+
6150
+
6151
+
6152
+
6153
+
6154
+
6155
+
6156
+
6157
+
6158
+
6159
+
6160
+
6161
+
6162
+
6163
+
6164
+
6165
+ * @example
6166
+ * // Remove all entries
6167
+ * const rbt = new RedBlackTree<number>([1, 2, 3]);
6168
+ * rbt.clear();
6169
+ * console.log(rbt.isEmpty()); // true;
6170
+ */
6171
+ clear() {
6172
+ super.clear();
6173
+ this._root = this.NIL;
6174
+ this._header.parent = this.NIL;
6175
+ this._setMinCache(void 0);
6176
+ this._setMaxCache(void 0);
6177
+ }
6178
+ /**
6179
+ * (Internal) Find a node by key using a tight BST walk (no allocations).
6180
+ *
6181
+ * NOTE: This uses `header.parent` as the canonical root pointer.
6182
+ * @remarks Time O(log n) average, Space O(1)
6183
+ */
6184
+ _findNodeByKey(key) {
6185
+ const NIL = this.NIL;
6186
+ const cmp = this._compare.bind(this);
6187
+ let cur = this._header.parent ?? NIL;
6188
+ while (cur !== NIL) {
6189
+ const c = cmp(key, cur.key);
6190
+ if (c < 0) cur = cur.left ?? NIL;
6191
+ else if (c > 0) cur = cur.right ?? NIL;
6192
+ else return cur;
6193
+ }
6194
+ return void 0;
6195
+ }
6196
+ /**
6197
+ * (Internal) In-order predecessor of a node in a BST.
6198
+ * @remarks Time O(log n) average, Space O(1)
6199
+ */
6200
+ _predecessorOf(node) {
6201
+ const NIL = this.NIL;
6202
+ if (node.left && node.left !== NIL) {
6203
+ let cur2 = node.left;
6204
+ while (cur2.right && cur2.right !== NIL) cur2 = cur2.right;
6205
+ return cur2;
6206
+ }
6207
+ let cur = node;
6208
+ let p = node.parent;
6209
+ while (p && cur === p.left) {
6210
+ cur = p;
6211
+ p = p.parent;
6212
+ }
6213
+ return p;
6214
+ }
6215
+ /**
6216
+ * (Internal) In-order successor of a node in a BST.
6217
+ * @remarks Time O(log n) average, Space O(1)
6218
+ */
6219
+ _successorOf(node) {
6220
+ const NIL = this.NIL;
6221
+ if (node.right && node.right !== NIL) {
6222
+ let cur2 = node.right;
6223
+ while (cur2.left && cur2.left !== NIL) cur2 = cur2.left;
4964
6224
  return cur2;
4965
6225
  }
4966
6226
  let cur = node;
@@ -4991,6 +6251,7 @@ var RedBlackTree = class extends BST {
4991
6251
  node.left = NIL;
4992
6252
  node.right = NIL;
4993
6253
  node.color = "RED";
6254
+ this._updateCountAlongPath(node);
4994
6255
  this._insertFixup(node);
4995
6256
  if (this.isRealNode(this._root)) this._root.color = "BLACK";
4996
6257
  }
@@ -5100,6 +6361,7 @@ var RedBlackTree = class extends BST {
5100
6361
  newNode.left = NIL;
5101
6362
  newNode.right = NIL;
5102
6363
  newNode.color = "RED";
6364
+ this._updateCountAlongPath(newNode);
5103
6365
  this._insertFixup(newNode);
5104
6366
  if (this.isRealNode(this._root)) this._root.color = "BLACK";
5105
6367
  else return void 0;
@@ -5258,6 +6520,102 @@ var RedBlackTree = class extends BST {
5258
6520
 
5259
6521
 
5260
6522
 
6523
+
6524
+
6525
+
6526
+
6527
+
6528
+
6529
+
6530
+
6531
+
6532
+
6533
+
6534
+
6535
+
6536
+
6537
+
6538
+
6539
+
6540
+
6541
+
6542
+
6543
+
6544
+
6545
+
6546
+
6547
+
6548
+
6549
+
6550
+
6551
+
6552
+
6553
+
6554
+
6555
+
6556
+
6557
+
6558
+
6559
+
6560
+
6561
+
6562
+
6563
+
6564
+
6565
+
6566
+
6567
+
6568
+
6569
+
6570
+
6571
+
6572
+
6573
+
6574
+
6575
+
6576
+
6577
+
6578
+
6579
+
6580
+
6581
+
6582
+
6583
+
6584
+
6585
+
6586
+
6587
+
6588
+
6589
+
6590
+
6591
+
6592
+
6593
+
6594
+
6595
+
6596
+
6597
+
6598
+
6599
+
6600
+
6601
+
6602
+
6603
+
6604
+
6605
+
6606
+
6607
+
6608
+
6609
+
6610
+
6611
+
6612
+
6613
+
6614
+
6615
+
6616
+
6617
+
6618
+
5261
6619
 
5262
6620
 
5263
6621
 
@@ -5382,11 +6740,107 @@ var RedBlackTree = class extends BST {
5382
6740
 
5383
6741
 
5384
6742
 
5385
- * @example
5386
- * // Remove and rebalance
5387
- * const rbt = new RedBlackTree<number>([10, 5, 15, 3, 7]);
5388
- * rbt.delete(5);
5389
- * console.log(rbt.has(5)); // false;
6743
+
6744
+
6745
+
6746
+
6747
+
6748
+
6749
+
6750
+
6751
+
6752
+
6753
+
6754
+
6755
+
6756
+
6757
+
6758
+
6759
+
6760
+
6761
+
6762
+
6763
+
6764
+
6765
+
6766
+
6767
+
6768
+
6769
+
6770
+
6771
+
6772
+
6773
+
6774
+
6775
+
6776
+
6777
+
6778
+
6779
+
6780
+
6781
+
6782
+
6783
+
6784
+
6785
+
6786
+
6787
+
6788
+
6789
+
6790
+
6791
+
6792
+
6793
+
6794
+
6795
+
6796
+
6797
+
6798
+
6799
+
6800
+
6801
+
6802
+
6803
+
6804
+
6805
+
6806
+
6807
+
6808
+
6809
+
6810
+
6811
+
6812
+
6813
+
6814
+
6815
+
6816
+
6817
+
6818
+
6819
+
6820
+
6821
+
6822
+
6823
+
6824
+
6825
+
6826
+
6827
+
6828
+
6829
+
6830
+
6831
+
6832
+
6833
+
6834
+
6835
+
6836
+
6837
+
6838
+
6839
+ * @example
6840
+ * // Remove and rebalance
6841
+ * const rbt = new RedBlackTree<number>([10, 5, 15, 3, 7]);
6842
+ * rbt.delete(5);
6843
+ * console.log(rbt.has(5)); // false;
5390
6844
  * console.log(rbt.size); // 4;
5391
6845
  */
5392
6846
  delete(keyNodeEntryRawOrPredicate) {
@@ -5435,6 +6889,7 @@ var RedBlackTree = class extends BST {
5435
6889
  }
5436
6890
  if (this._isMapMode) this._store.delete(nodeToDelete.key);
5437
6891
  this._size--;
6892
+ this._updateCountAlongPath(replacementNode?.parent ?? replacementNode);
5438
6893
  if (this._size <= 0) {
5439
6894
  this._setMinCache(void 0);
5440
6895
  this._setMaxCache(void 0);
@@ -5489,6 +6944,78 @@ var RedBlackTree = class extends BST {
5489
6944
 
5490
6945
 
5491
6946
 
6947
+
6948
+
6949
+
6950
+
6951
+
6952
+
6953
+
6954
+
6955
+
6956
+
6957
+
6958
+
6959
+
6960
+
6961
+
6962
+
6963
+
6964
+
6965
+
6966
+
6967
+
6968
+
6969
+
6970
+
6971
+
6972
+
6973
+
6974
+
6975
+
6976
+
6977
+
6978
+
6979
+
6980
+
6981
+
6982
+
6983
+
6984
+
6985
+
6986
+
6987
+
6988
+
6989
+
6990
+
6991
+
6992
+
6993
+
6994
+
6995
+
6996
+
6997
+
6998
+
6999
+
7000
+
7001
+
7002
+
7003
+
7004
+
7005
+
7006
+
7007
+
7008
+
7009
+
7010
+
7011
+
7012
+
7013
+
7014
+
7015
+
7016
+
7017
+
7018
+
5492
7019
 
5493
7020
 
5494
7021
 
@@ -5529,6 +7056,102 @@ var RedBlackTree = class extends BST {
5529
7056
 
5530
7057
 
5531
7058
 
7059
+
7060
+
7061
+
7062
+
7063
+
7064
+
7065
+
7066
+
7067
+
7068
+
7069
+
7070
+
7071
+
7072
+
7073
+
7074
+
7075
+
7076
+
7077
+
7078
+
7079
+
7080
+
7081
+
7082
+
7083
+
7084
+
7085
+
7086
+
7087
+
7088
+
7089
+
7090
+
7091
+
7092
+
7093
+
7094
+
7095
+
7096
+
7097
+
7098
+
7099
+
7100
+
7101
+
7102
+
7103
+
7104
+
7105
+
7106
+
7107
+
7108
+
7109
+
7110
+
7111
+
7112
+
7113
+
7114
+
7115
+
7116
+
7117
+
7118
+
7119
+
7120
+
7121
+
7122
+
7123
+
7124
+
7125
+
7126
+
7127
+
7128
+
7129
+
7130
+
7131
+
7132
+
7133
+
7134
+
7135
+
7136
+
7137
+
7138
+
7139
+
7140
+
7141
+
7142
+
7143
+
7144
+
7145
+
7146
+
7147
+
7148
+
7149
+
7150
+
7151
+
7152
+
7153
+
7154
+
5532
7155
 
5533
7156
 
5534
7157
 
@@ -5635,6 +7258,7 @@ var RedBlackTree = class extends BST {
5635
7258
  node.left = NIL;
5636
7259
  node.right = NIL;
5637
7260
  node.color = "RED";
7261
+ this._updateCountAlongPath(node);
5638
7262
  this._insertFixup(node);
5639
7263
  return "CREATED";
5640
7264
  }
@@ -5803,6 +7427,8 @@ var RedBlackTree = class extends BST {
5803
7427
  }
5804
7428
  y.left = x;
5805
7429
  x.parent = y;
7430
+ this._updateCount(x);
7431
+ this._updateCount(y);
5806
7432
  }
5807
7433
  /**
5808
7434
  * (Protected) Perform a right rotation around y.
@@ -5829,6 +7455,8 @@ var RedBlackTree = class extends BST {
5829
7455
  }
5830
7456
  x.right = y;
5831
7457
  y.parent = x;
7458
+ this._updateCount(y);
7459
+ this._updateCount(x);
5832
7460
  }
5833
7461
  };
5834
7462
  /**
@@ -5848,5 +7476,6 @@ exports.ERR = ERR;
5848
7476
  exports.Range = Range;
5849
7477
  exports.RedBlackTree = RedBlackTree;
5850
7478
  exports.RedBlackTreeNode = RedBlackTreeNode;
7479
+ exports.raise = raise;
5851
7480
  //# sourceMappingURL=index.cjs.map
5852
7481
  //# sourceMappingURL=index.cjs.map