avl-tree-typed 2.5.1 → 2.5.3

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 (75) hide show
  1. package/dist/cjs/index.cjs +714 -113
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +714 -112
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +714 -113
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +714 -112
  8. package/dist/esm-legacy/index.mjs.map +1 -1
  9. package/dist/types/common/error.d.ts +9 -0
  10. package/dist/types/common/index.d.ts +1 -1
  11. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +86 -2
  12. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +98 -0
  13. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +189 -13
  14. package/dist/types/data-structures/binary-tree/bst.d.ts +270 -3
  15. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +136 -8
  16. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +42 -0
  17. package/dist/types/data-structures/binary-tree/tree-map.d.ts +1089 -161
  18. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1243 -350
  19. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +980 -255
  20. package/dist/types/data-structures/binary-tree/tree-set.d.ts +1174 -284
  21. package/dist/types/data-structures/graph/directed-graph.d.ts +70 -0
  22. package/dist/types/data-structures/graph/undirected-graph.d.ts +63 -0
  23. package/dist/types/data-structures/hash/hash-map.d.ts +84 -6
  24. package/dist/types/data-structures/heap/heap.d.ts +140 -12
  25. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +126 -0
  26. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +106 -1
  27. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +126 -0
  28. package/dist/types/data-structures/matrix/matrix.d.ts +56 -0
  29. package/dist/types/data-structures/queue/deque.d.ts +127 -0
  30. package/dist/types/data-structures/queue/queue.d.ts +97 -0
  31. package/dist/types/data-structures/stack/stack.d.ts +72 -2
  32. package/dist/types/data-structures/trie/trie.d.ts +84 -0
  33. package/dist/types/interfaces/binary-tree.d.ts +2 -3
  34. package/dist/types/types/data-structures/binary-tree/bst.d.ts +1 -0
  35. package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +5 -0
  36. package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +4 -0
  37. package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +4 -0
  38. package/dist/umd/avl-tree-typed.js +711 -110
  39. package/dist/umd/avl-tree-typed.js.map +1 -1
  40. package/dist/umd/avl-tree-typed.min.js +3 -3
  41. package/dist/umd/avl-tree-typed.min.js.map +1 -1
  42. package/package.json +2 -2
  43. package/src/common/error.ts +19 -1
  44. package/src/common/index.ts +1 -1
  45. package/src/data-structures/base/iterable-element-base.ts +3 -2
  46. package/src/data-structures/binary-tree/avl-tree.ts +99 -5
  47. package/src/data-structures/binary-tree/binary-indexed-tree.ts +102 -4
  48. package/src/data-structures/binary-tree/binary-tree.ts +239 -78
  49. package/src/data-structures/binary-tree/bst.ts +542 -13
  50. package/src/data-structures/binary-tree/red-black-tree.ts +155 -15
  51. package/src/data-structures/binary-tree/segment-tree.ts +42 -0
  52. package/src/data-structures/binary-tree/tree-map.ts +1223 -261
  53. package/src/data-structures/binary-tree/tree-multi-map.ts +939 -30
  54. package/src/data-structures/binary-tree/tree-multi-set.ts +746 -10
  55. package/src/data-structures/binary-tree/tree-set.ts +1018 -99
  56. package/src/data-structures/graph/abstract-graph.ts +2 -2
  57. package/src/data-structures/graph/directed-graph.ts +71 -1
  58. package/src/data-structures/graph/undirected-graph.ts +64 -1
  59. package/src/data-structures/hash/hash-map.ts +102 -16
  60. package/src/data-structures/heap/heap.ts +153 -23
  61. package/src/data-structures/heap/max-heap.ts +2 -2
  62. package/src/data-structures/linked-list/doubly-linked-list.ts +139 -0
  63. package/src/data-structures/linked-list/singly-linked-list.ts +106 -1
  64. package/src/data-structures/linked-list/skip-linked-list.ts +131 -5
  65. package/src/data-structures/matrix/matrix.ts +65 -9
  66. package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
  67. package/src/data-structures/queue/deque.ts +130 -0
  68. package/src/data-structures/queue/queue.ts +109 -0
  69. package/src/data-structures/stack/stack.ts +75 -5
  70. package/src/data-structures/trie/trie.ts +86 -2
  71. package/src/interfaces/binary-tree.ts +1 -9
  72. package/src/types/data-structures/binary-tree/bst.ts +1 -0
  73. package/src/types/data-structures/binary-tree/tree-map.ts +6 -0
  74. package/src/types/data-structures/binary-tree/tree-multi-set.ts +5 -0
  75. package/src/types/data-structures/binary-tree/tree-set.ts +5 -0
@@ -59,6 +59,56 @@ 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 Range = class {
95
+ constructor(low, high, includeLow = true, includeHigh = true) {
96
+ this.low = low;
97
+ this.high = high;
98
+ this.includeLow = includeLow;
99
+ this.includeHigh = includeHigh;
100
+ }
101
+ static {
102
+ __name(this, "Range");
103
+ }
104
+ // Determine whether a key is within the range
105
+ isInRange(key, comparator) {
106
+ const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
107
+ const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
108
+ return lowCheck && highCheck;
109
+ }
110
+ };
111
+
62
112
  // src/data-structures/base/iterable-element-base.ts
63
113
  var IterableElementBase = class {
64
114
  static {
@@ -77,7 +127,7 @@ var IterableElementBase = class {
77
127
  if (options) {
78
128
  const { toElementFn } = options;
79
129
  if (typeof toElementFn === "function") this._toElementFn = toElementFn;
80
- else if (toElementFn) throw new TypeError("toElementFn must be a function type");
130
+ else if (toElementFn) raise(TypeError, "toElementFn must be a function type");
81
131
  }
82
132
  }
83
133
  /**
@@ -240,7 +290,7 @@ var IterableElementBase = class {
240
290
  acc = initialValue;
241
291
  } else {
242
292
  const first = iter.next();
243
- if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
293
+ if (first.done) raise(TypeError, "Reduce of empty structure with no initial value");
244
294
  acc = first.value;
245
295
  index = 1;
246
296
  }
@@ -475,50 +525,6 @@ var LinearBase = class _LinearBase extends IterableElementBase {
475
525
  }
476
526
  };
477
527
 
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 Range = class {
505
- constructor(low, high, includeLow = true, includeHigh = true) {
506
- this.low = low;
507
- this.high = high;
508
- this.includeLow = includeLow;
509
- this.includeHigh = includeHigh;
510
- }
511
- static {
512
- __name(this, "Range");
513
- }
514
- // Determine whether a key is within the range
515
- isInRange(key, comparator) {
516
- const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
517
- const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
518
- return lowCheck && highCheck;
519
- }
520
- };
521
-
522
528
  // src/data-structures/base/iterable-entry-base.ts
523
529
  var IterableEntryBase = class {
524
530
  static {
@@ -785,6 +791,13 @@ var Queue = class _Queue extends LinearBase {
785
791
 
786
792
 
787
793
 
794
+
795
+
796
+
797
+
798
+
799
+
800
+
788
801
 
789
802
 
790
803
 
@@ -832,6 +845,13 @@ var Queue = class _Queue extends LinearBase {
832
845
 
833
846
 
834
847
 
848
+
849
+
850
+
851
+
852
+
853
+
854
+
835
855
 
836
856
 
837
857
 
@@ -849,6 +869,14 @@ var Queue = class _Queue extends LinearBase {
849
869
  get first() {
850
870
  return this.length > 0 ? this.elements[this._offset] : void 0;
851
871
  }
872
+ /**
873
+ * Peek at the front element without removing it (alias for `first`).
874
+ * @remarks Time O(1), Space O(1)
875
+ * @returns Front element or undefined.
876
+ */
877
+ peek() {
878
+ return this.first;
879
+ }
852
880
  /**
853
881
  * Get the last element (back) without removing it.
854
882
  * @remarks Time O(1), Space O(1)
@@ -895,6 +923,13 @@ var Queue = class _Queue extends LinearBase {
895
923
 
896
924
 
897
925
 
926
+
927
+
928
+
929
+
930
+
931
+
932
+
898
933
 
899
934
 
900
935
 
@@ -954,6 +989,13 @@ var Queue = class _Queue extends LinearBase {
954
989
 
955
990
 
956
991
 
992
+
993
+
994
+
995
+
996
+
997
+
998
+
957
999
 
958
1000
 
959
1001
 
@@ -1020,6 +1062,13 @@ var Queue = class _Queue extends LinearBase {
1020
1062
 
1021
1063
 
1022
1064
 
1065
+
1066
+
1067
+
1068
+
1069
+
1070
+
1071
+
1023
1072
 
1024
1073
 
1025
1074
 
@@ -1076,6 +1125,13 @@ var Queue = class _Queue extends LinearBase {
1076
1125
 
1077
1126
 
1078
1127
 
1128
+
1129
+
1130
+
1131
+
1132
+
1133
+
1134
+
1079
1135
 
1080
1136
 
1081
1137
 
@@ -1125,6 +1181,13 @@ var Queue = class _Queue extends LinearBase {
1125
1181
 
1126
1182
 
1127
1183
 
1184
+
1185
+
1186
+
1187
+
1188
+
1189
+
1190
+
1128
1191
 
1129
1192
 
1130
1193
 
@@ -1179,6 +1242,21 @@ var Queue = class _Queue extends LinearBase {
1179
1242
  this._elements[this._offset + index] = newElement;
1180
1243
  return true;
1181
1244
  }
1245
+ /**
1246
+ * Delete the first element that satisfies a predicate.
1247
+ * @remarks Time O(N), Space O(N)
1248
+ * @param predicate - Function (value, index, queue) → boolean to decide deletion.
1249
+ * @returns True if a match was removed.
1250
+ */
1251
+ deleteWhere(predicate) {
1252
+ for (let i = 0; i < this.length; i++) {
1253
+ if (predicate(this._elements[this._offset + i], i, this)) {
1254
+ this.deleteAt(i);
1255
+ return true;
1256
+ }
1257
+ }
1258
+ return false;
1259
+ }
1182
1260
  /**
1183
1261
  * Reverse the queue in-place by compacting then reversing.
1184
1262
  * @remarks Time O(N), Space O(N)
@@ -1215,6 +1293,13 @@ var Queue = class _Queue extends LinearBase {
1215
1293
 
1216
1294
 
1217
1295
 
1296
+
1297
+
1298
+
1299
+
1300
+
1301
+
1302
+
1218
1303
 
1219
1304
 
1220
1305
 
@@ -1258,6 +1343,13 @@ var Queue = class _Queue extends LinearBase {
1258
1343
 
1259
1344
 
1260
1345
 
1346
+
1347
+
1348
+
1349
+
1350
+
1351
+
1352
+
1261
1353
 
1262
1354
 
1263
1355
 
@@ -1324,6 +1416,13 @@ var Queue = class _Queue extends LinearBase {
1324
1416
 
1325
1417
 
1326
1418
 
1419
+
1420
+
1421
+
1422
+
1423
+
1424
+
1425
+
1327
1426
 
1328
1427
 
1329
1428
 
@@ -1374,6 +1473,13 @@ var Queue = class _Queue extends LinearBase {
1374
1473
 
1375
1474
 
1376
1475
 
1476
+
1477
+
1478
+
1479
+
1480
+
1481
+
1482
+
1377
1483
 
1378
1484
 
1379
1485
 
@@ -1428,6 +1534,13 @@ var Queue = class _Queue extends LinearBase {
1428
1534
 
1429
1535
 
1430
1536
 
1537
+
1538
+
1539
+
1540
+
1541
+
1542
+
1543
+
1431
1544
 
1432
1545
 
1433
1546
 
@@ -1686,7 +1799,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
1686
1799
  if (isMapMode !== void 0) this._isMapMode = isMapMode;
1687
1800
  if (isDuplicate !== void 0) this._isDuplicate = isDuplicate;
1688
1801
  if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
1689
- else if (toEntryFn) throw new TypeError(ERR.notAFunction("toEntryFn", "BinaryTree"));
1802
+ else if (toEntryFn) raise(TypeError, ERR.notAFunction("toEntryFn", "BinaryTree"));
1690
1803
  }
1691
1804
  if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
1692
1805
  }
@@ -1902,7 +2015,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
1902
2015
  }
1903
2016
  /**
1904
2017
  * Adds a new node to the tree.
1905
- * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation adds the node at the first available position in a level-order (BFS) traversal. This is NOT a Binary Search Tree insertion. Time O(N), where N is the number of nodes. It must traverse level-by-level to find an empty slot. Space O(N) in the worst case for the BFS queue (e.g., a full last level).
2018
+ * @remarks Time O(N) level-order traversal to find an empty slot. Space O(N) for the BFS queue. BST/Red-Black Tree/AVL Tree subclasses override to O(log N).
1906
2019
  *
1907
2020
  * @param keyNodeOrEntry - The key, node, or entry to add.
1908
2021
  * @returns True if the addition was successful, false otherwise.
@@ -1925,6 +2038,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
1925
2038
 
1926
2039
 
1927
2040
 
2041
+
2042
+
2043
+
2044
+
2045
+
2046
+
2047
+
1928
2048
 
1929
2049
 
1930
2050
 
@@ -1947,7 +2067,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
1947
2067
  }
1948
2068
  /**
1949
2069
  * Adds or updates a new node to the tree.
1950
- * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation sets the node at the first available position in a level-order (BFS) traversal. This is NOT a Binary Search Tree insertion. Time O(N), where N is the number of nodes. It must traverse level-by-level to find an empty slot. Space O(N) in the worst case for the BFS queue (e.g., a full last level).
2070
+ * @remarks Time O(N) level-order traversal to find an empty slot. Space O(N) for the BFS queue. BST/Red-Black Tree/AVL Tree subclasses override to O(log N).
1951
2071
  *
1952
2072
  * @param keyNodeOrEntry - The key, node, or entry to set or update.
1953
2073
  * @param [value] - The value, if providing just a key.
@@ -1976,6 +2096,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
1976
2096
 
1977
2097
 
1978
2098
 
2099
+
2100
+
2101
+
2102
+
2103
+
2104
+
2105
+
1979
2106
 
1980
2107
 
1981
2108
 
@@ -2079,6 +2206,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2079
2206
 
2080
2207
 
2081
2208
 
2209
+
2210
+
2211
+
2212
+
2213
+
2214
+
2215
+
2082
2216
 
2083
2217
 
2084
2218
 
@@ -2118,6 +2252,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2118
2252
 
2119
2253
 
2120
2254
 
2255
+
2256
+
2257
+
2258
+
2259
+
2260
+
2261
+
2121
2262
 
2122
2263
 
2123
2264
 
@@ -2178,6 +2319,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2178
2319
 
2179
2320
 
2180
2321
 
2322
+
2323
+
2324
+
2325
+
2326
+
2327
+
2328
+
2181
2329
 
2182
2330
 
2183
2331
 
@@ -2197,22 +2345,69 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2197
2345
  this.setMany(anotherTree, []);
2198
2346
  }
2199
2347
  /**
2200
- * Clears the tree and refills it with new items.
2201
- * @remarks Time O(N) (for `clear`) + O(N * M) (for `setMany`) = O(N * M). Space O(M) (from `setMany`).
2348
+ * Deletes a node from the tree (internal, returns balancing metadata).
2349
+ * @remarks Time O(N) O(N) to find the node + O(H) for predecessor swap. Space O(1). BST/Red-Black Tree/AVL Tree subclasses override to O(log N).
2350
+ * @internal Used by AVL/BST subclasses that need balancing metadata after deletion.
2202
2351
  *
2203
- * @param keysNodesEntriesOrRaws - An iterable of items to set.
2204
- * @param [values] - An optional parallel iterable of values.
2352
+ * @param keyNodeEntryRawOrPredicate - The node to delete.
2353
+ * @returns An array containing deletion results with balancing metadata.
2205
2354
  */
2206
- refill(keysNodesEntriesOrRaws, values) {
2207
- this.clear();
2208
- this.setMany(keysNodesEntriesOrRaws, values);
2355
+ _deleteInternal(keyNodeEntryRawOrPredicate) {
2356
+ const deletedResult = [];
2357
+ if (!this._root) return deletedResult;
2358
+ const curr = this.getNode(keyNodeEntryRawOrPredicate);
2359
+ if (!curr) return deletedResult;
2360
+ const parent = curr?.parent;
2361
+ let needBalanced;
2362
+ let orgCurrent = curr;
2363
+ if (!curr.left && !curr.right && !parent) {
2364
+ this._setRoot(void 0);
2365
+ } else if (curr.left) {
2366
+ const leftSubTreeRightMost = this.getRightMost((node) => node, curr.left);
2367
+ if (leftSubTreeRightMost) {
2368
+ const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
2369
+ orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
2370
+ if (this._isMapMode) {
2371
+ this._store.set(curr.key, curr);
2372
+ this._store.set(leftSubTreeRightMost.key, leftSubTreeRightMost);
2373
+ }
2374
+ if (parentOfLeftSubTreeMax) {
2375
+ if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
2376
+ parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
2377
+ else parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
2378
+ needBalanced = parentOfLeftSubTreeMax;
2379
+ }
2380
+ }
2381
+ } else if (parent) {
2382
+ const { familyPosition: fp } = curr;
2383
+ if (fp === "LEFT" || fp === "ROOT_LEFT") {
2384
+ parent.left = curr.right;
2385
+ } else if (fp === "RIGHT" || fp === "ROOT_RIGHT") {
2386
+ parent.right = curr.right;
2387
+ }
2388
+ needBalanced = parent;
2389
+ } else {
2390
+ this._setRoot(curr.right);
2391
+ curr.right = void 0;
2392
+ }
2393
+ this._size = this._size - 1;
2394
+ deletedResult.push({ deleted: orgCurrent, needBalanced });
2395
+ if (this._isMapMode && orgCurrent) this._store.delete(orgCurrent.key);
2396
+ return deletedResult;
2209
2397
  }
2210
2398
  /**
2211
2399
  * Deletes a node from the tree.
2212
- * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation finds the node, and if it has two children, swaps it with the rightmost node of its left subtree (in-order predecessor) before deleting. Time O(N) in the worst case. O(N) to find the node (`getNode`) and O(H) (which is O(N) worst-case) to find the rightmost node. Space O(1) (if `getNode` is iterative, which it is).
2400
+ * @remarks Time O(N) O(N) to find the node + O(H) for predecessor swap. Space O(1). BST/Red-Black Tree/AVL Tree subclasses override to O(log N).
2213
2401
  *
2214
2402
  * @param keyNodeEntryRawOrPredicate - The node to delete.
2215
- * @returns An array containing deletion results (for compatibility with self-balancing trees).
2403
+ * @returns True if the node was found and deleted, false otherwise.
2404
+
2405
+
2406
+
2407
+
2408
+
2409
+
2410
+
2216
2411
 
2217
2412
 
2218
2413
 
@@ -2253,51 +2448,11 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2253
2448
  * console.log(tree.size); // 4;
2254
2449
  */
2255
2450
  delete(keyNodeEntryRawOrPredicate) {
2256
- const deletedResult = [];
2257
- if (!this._root) return deletedResult;
2258
- const curr = this.getNode(keyNodeEntryRawOrPredicate);
2259
- if (!curr) return deletedResult;
2260
- const parent = curr?.parent;
2261
- let needBalanced;
2262
- let orgCurrent = curr;
2263
- if (!curr.left && !curr.right && !parent) {
2264
- this._setRoot(void 0);
2265
- } else if (curr.left) {
2266
- const leftSubTreeRightMost = this.getRightMost((node) => node, curr.left);
2267
- if (leftSubTreeRightMost) {
2268
- const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
2269
- orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
2270
- if (this._isMapMode) {
2271
- this._store.set(curr.key, curr);
2272
- this._store.set(leftSubTreeRightMost.key, leftSubTreeRightMost);
2273
- }
2274
- if (parentOfLeftSubTreeMax) {
2275
- if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
2276
- parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
2277
- else parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
2278
- needBalanced = parentOfLeftSubTreeMax;
2279
- }
2280
- }
2281
- } else if (parent) {
2282
- const { familyPosition: fp } = curr;
2283
- if (fp === "LEFT" || fp === "ROOT_LEFT") {
2284
- parent.left = curr.right;
2285
- } else if (fp === "RIGHT" || fp === "ROOT_RIGHT") {
2286
- parent.right = curr.right;
2287
- }
2288
- needBalanced = parent;
2289
- } else {
2290
- this._setRoot(curr.right);
2291
- curr.right = void 0;
2292
- }
2293
- this._size = this._size - 1;
2294
- deletedResult.push({ deleted: orgCurrent, needBalanced });
2295
- if (this._isMapMode && orgCurrent) this._store.delete(orgCurrent.key);
2296
- return deletedResult;
2451
+ return this._deleteInternal(keyNodeEntryRawOrPredicate).length > 0;
2297
2452
  }
2298
2453
  /**
2299
2454
  * Searches the tree for nodes matching a predicate.
2300
- * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). Performs a full DFS (pre-order) scan of the tree. Time O(N), as it may visit every node. Space O(H) for the call stack (recursive) or explicit stack (iterative), where H is the tree height (O(N) worst-case).
2455
+ * @remarks Time O(N) full DFS scan; may visit every node. Space O(H) for call/explicit stack (O(N) worst-case). BST subclasses with key search override to O(log N).
2301
2456
  *
2302
2457
  * @template C - The type of the callback function.
2303
2458
  * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
@@ -2346,7 +2501,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2346
2501
  }
2347
2502
  /**
2348
2503
  * Gets the first node matching a predicate.
2349
- * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). Time O(N) in the worst case (via `search`). Space O(H) or O(N) (via `search`).
2504
+ * @remarks Time O(N) via `search`. Space O(H) or O(N). BST/Red-Black Tree/AVL Tree subclasses override to O(log N) for key lookups.
2350
2505
  *
2351
2506
  * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
2352
2507
  * @param [startNode=this._root] - The node to start the search from.
@@ -2374,6 +2529,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2374
2529
 
2375
2530
 
2376
2531
 
2532
+
2533
+
2534
+
2535
+
2536
+
2537
+
2538
+
2377
2539
 
2378
2540
 
2379
2541
 
@@ -2399,7 +2561,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2399
2561
  }
2400
2562
  /**
2401
2563
  * Gets the value associated with a key.
2402
- * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). Time O(1) if in Map mode. O(N) if not in Map mode (uses `getNode`). Space O(1) if in Map mode. O(H) or O(N) otherwise.
2564
+ * @remarks Time O(1) in Map mode, O(N) otherwise (via `getNode`). Space O(1) in Map mode, O(H) or O(N) otherwise. BST subclasses override non-Map-mode to O(log N).
2403
2565
  *
2404
2566
  * @param keyNodeEntryOrPredicate - The key, node, or entry to get the value for.
2405
2567
  * @param [startNode=this._root] - The node to start searching from (if not in Map mode).
@@ -2429,6 +2591,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2429
2591
 
2430
2592
 
2431
2593
 
2594
+
2595
+
2596
+
2597
+
2598
+
2599
+
2600
+
2432
2601
 
2433
2602
 
2434
2603
 
@@ -2486,6 +2655,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2486
2655
 
2487
2656
 
2488
2657
 
2658
+
2659
+
2660
+
2661
+
2662
+
2663
+
2664
+
2489
2665
 
2490
2666
 
2491
2667
 
@@ -2531,6 +2707,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2531
2707
 
2532
2708
 
2533
2709
 
2710
+
2711
+
2712
+
2713
+
2714
+
2715
+
2716
+
2534
2717
 
2535
2718
 
2536
2719
 
@@ -2585,6 +2768,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2585
2768
 
2586
2769
 
2587
2770
 
2771
+
2772
+
2773
+
2774
+
2775
+
2776
+
2777
+
2588
2778
 
2589
2779
 
2590
2780
 
@@ -2666,6 +2856,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2666
2856
 
2667
2857
 
2668
2858
 
2859
+
2860
+
2861
+
2862
+
2863
+
2864
+
2865
+
2669
2866
 
2670
2867
 
2671
2868
 
@@ -2724,6 +2921,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
2724
2921
 
2725
2922
 
2726
2923
 
2924
+
2925
+
2926
+
2927
+
2928
+
2929
+
2930
+
2727
2931
 
2728
2932
 
2729
2933
 
@@ -3198,6 +3402,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
3198
3402
 
3199
3403
 
3200
3404
 
3405
+
3406
+
3407
+
3408
+
3409
+
3410
+
3411
+
3201
3412
 
3202
3413
 
3203
3414
 
@@ -3247,6 +3458,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
3247
3458
 
3248
3459
 
3249
3460
 
3461
+
3462
+
3463
+
3464
+
3465
+
3466
+
3467
+
3250
3468
 
3251
3469
 
3252
3470
 
@@ -3300,6 +3518,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
3300
3518
 
3301
3519
 
3302
3520
 
3521
+
3522
+
3523
+
3524
+
3525
+
3526
+
3527
+
3303
3528
 
3304
3529
 
3305
3530
 
@@ -3378,6 +3603,13 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
3378
3603
 
3379
3604
 
3380
3605
 
3606
+
3607
+
3608
+
3609
+
3610
+
3611
+
3612
+
3381
3613
 
3382
3614
 
3383
3615
 
@@ -4017,12 +4249,16 @@ var BST = class extends BinaryTree {
4017
4249
  } else {
4018
4250
  this._comparator = this._createDefaultComparator();
4019
4251
  }
4252
+ if (options.enableOrderStatistic) {
4253
+ this._enableOrderStatistic = true;
4254
+ }
4020
4255
  } else {
4021
4256
  this._comparator = this._createDefaultComparator();
4022
4257
  }
4023
4258
  if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
4024
4259
  }
4025
4260
  _root = void 0;
4261
+ _enableOrderStatistic = false;
4026
4262
  /**
4027
4263
  * Gets the root node of the tree.
4028
4264
  * @remarks Time O(1)
@@ -4184,6 +4420,20 @@ var BST = class extends BinaryTree {
4184
4420
 
4185
4421
 
4186
4422
 
4423
+
4424
+
4425
+
4426
+
4427
+
4428
+
4429
+
4430
+
4431
+
4432
+
4433
+
4434
+
4435
+
4436
+
4187
4437
 
4188
4438
 
4189
4439
 
@@ -4353,6 +4603,84 @@ var BST = class extends BinaryTree {
4353
4603
  const searchRange = range instanceof Range ? range : new Range(range[0], range[1]);
4354
4604
  return this.search(searchRange, false, callback, startNode, iterationType);
4355
4605
  }
4606
+ getByRank(k, callback = this._DEFAULT_NODE_CALLBACK, iterationType = this.iterationType) {
4607
+ if (!this._enableOrderStatistic) {
4608
+ raise(Error, ERR.orderStatisticNotEnabled("getByRank"));
4609
+ }
4610
+ if (k < 0 || k >= this._size) return void 0;
4611
+ let actualCallback = void 0;
4612
+ let actualIterationType = this.iterationType;
4613
+ if (typeof callback === "string") {
4614
+ actualIterationType = callback;
4615
+ } else if (callback) {
4616
+ actualCallback = callback;
4617
+ if (iterationType) {
4618
+ actualIterationType = iterationType;
4619
+ }
4620
+ }
4621
+ const node = actualIterationType === "RECURSIVE" ? this._getByRankRecursive(this._root, k) : this._getByRankIterative(this._root, k);
4622
+ if (!node) return void 0;
4623
+ return actualCallback ? actualCallback(node) : node.key;
4624
+ }
4625
+ getRank(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
4626
+ if (!this._enableOrderStatistic) {
4627
+ raise(Error, ERR.orderStatisticNotEnabled("getRank"));
4628
+ }
4629
+ if (!this._root || this._size === 0) return -1;
4630
+ let actualIterationType = this.iterationType;
4631
+ if (iterationType) actualIterationType = iterationType;
4632
+ let key;
4633
+ if (typeof keyNodeEntryOrPredicate === "function") {
4634
+ const results = this.search(keyNodeEntryOrPredicate, true);
4635
+ if (results.length === 0 || results[0] === void 0) return -1;
4636
+ key = results[0];
4637
+ } else if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
4638
+ return -1;
4639
+ } else if (this.isNode(keyNodeEntryOrPredicate)) {
4640
+ key = keyNodeEntryOrPredicate.key;
4641
+ } else if (Array.isArray(keyNodeEntryOrPredicate)) {
4642
+ key = keyNodeEntryOrPredicate[0] ?? void 0;
4643
+ if (key === void 0 || key === null) return -1;
4644
+ } else {
4645
+ key = keyNodeEntryOrPredicate;
4646
+ }
4647
+ if (key === void 0) return -1;
4648
+ return actualIterationType === "RECURSIVE" ? this._getRankRecursive(this._root, key) : this._getRankIterative(this._root, key);
4649
+ }
4650
+ rangeByRank(start, end, callback = this._DEFAULT_NODE_CALLBACK, iterationType = this.iterationType) {
4651
+ if (!this._enableOrderStatistic) {
4652
+ raise(Error, ERR.orderStatisticNotEnabled("rangeByRank"));
4653
+ }
4654
+ if (this._size === 0) return [];
4655
+ const lo = Math.max(0, start);
4656
+ const hi = Math.min(this._size - 1, end);
4657
+ if (lo > hi) return [];
4658
+ let actualCallback = void 0;
4659
+ let actualIterationType = this.iterationType;
4660
+ if (typeof callback === "string") {
4661
+ actualIterationType = callback;
4662
+ } else if (callback) {
4663
+ actualCallback = callback;
4664
+ if (iterationType) {
4665
+ actualIterationType = iterationType;
4666
+ }
4667
+ }
4668
+ const results = [];
4669
+ const count = hi - lo + 1;
4670
+ const startNode = actualIterationType === "RECURSIVE" ? this._getByRankRecursive(this._root, lo) : this._getByRankIterative(this._root, lo);
4671
+ if (!startNode) return [];
4672
+ let collected = 0;
4673
+ const cb = actualCallback ?? this._DEFAULT_NODE_CALLBACK;
4674
+ let current = startNode;
4675
+ while (current && collected < count) {
4676
+ results.push(cb(current));
4677
+ collected++;
4678
+ if (collected < count) {
4679
+ current = this._next(current);
4680
+ }
4681
+ }
4682
+ return results;
4683
+ }
4356
4684
  /**
4357
4685
  * Adds a new node to the BST based on key comparison.
4358
4686
  * @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
@@ -4427,6 +4755,27 @@ var BST = class extends BinaryTree {
4427
4755
 
4428
4756
 
4429
4757
 
4758
+
4759
+
4760
+
4761
+
4762
+
4763
+
4764
+
4765
+
4766
+
4767
+
4768
+
4769
+
4770
+
4771
+
4772
+
4773
+
4774
+
4775
+
4776
+
4777
+
4778
+
4430
4779
 
4431
4780
 
4432
4781
 
@@ -4462,6 +4811,7 @@ var BST = class extends BinaryTree {
4462
4811
  this._setRoot(newNode);
4463
4812
  if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
4464
4813
  this._size++;
4814
+ this._updateCount(newNode);
4465
4815
  return true;
4466
4816
  }
4467
4817
  let current = this._root;
@@ -4475,6 +4825,7 @@ var BST = class extends BinaryTree {
4475
4825
  current.left = newNode;
4476
4826
  if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
4477
4827
  this._size++;
4828
+ this._updateCountAlongPath(newNode);
4478
4829
  return true;
4479
4830
  }
4480
4831
  if (current.left !== null) current = current.left;
@@ -4483,6 +4834,7 @@ var BST = class extends BinaryTree {
4483
4834
  current.right = newNode;
4484
4835
  if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
4485
4836
  this._size++;
4837
+ this._updateCountAlongPath(newNode);
4486
4838
  return true;
4487
4839
  }
4488
4840
  if (current.right !== null) current = current.right;
@@ -4539,6 +4891,20 @@ var BST = class extends BinaryTree {
4539
4891
 
4540
4892
 
4541
4893
 
4894
+
4895
+
4896
+
4897
+
4898
+
4899
+
4900
+
4901
+
4902
+
4903
+
4904
+
4905
+
4906
+
4907
+
4542
4908
 
4543
4909
 
4544
4910
 
@@ -4830,6 +5196,13 @@ var BST = class extends BinaryTree {
4830
5196
 
4831
5197
 
4832
5198
 
5199
+
5200
+
5201
+
5202
+
5203
+
5204
+
5205
+
4833
5206
 
4834
5207
 
4835
5208
 
@@ -4896,6 +5269,13 @@ var BST = class extends BinaryTree {
4896
5269
 
4897
5270
 
4898
5271
 
5272
+
5273
+
5274
+
5275
+
5276
+
5277
+
5278
+
4899
5279
 
4900
5280
 
4901
5281
 
@@ -5005,6 +5385,20 @@ var BST = class extends BinaryTree {
5005
5385
 
5006
5386
 
5007
5387
 
5388
+
5389
+
5390
+
5391
+
5392
+
5393
+
5394
+
5395
+
5396
+
5397
+
5398
+
5399
+
5400
+
5401
+
5008
5402
 
5009
5403
 
5010
5404
 
@@ -5072,12 +5466,11 @@ var BST = class extends BinaryTree {
5072
5466
  */
5073
5467
  deleteWhere(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
5074
5468
  const toDelete = this.search(keyNodeEntryOrPredicate, onlyOne, (node) => node, startNode, iterationType);
5075
- let results = [];
5469
+ let deleted = false;
5076
5470
  for (const node of toDelete) {
5077
- const deleteInfo = this.delete(node);
5078
- results = results.concat(deleteInfo);
5471
+ if (this.delete(node)) deleted = true;
5079
5472
  }
5080
- return results;
5473
+ return deleted;
5081
5474
  }
5082
5475
  /**
5083
5476
  * (Protected) Creates the default comparator function for keys that don't have a custom comparator.
@@ -5094,13 +5487,11 @@ var BST = class extends BinaryTree {
5094
5487
  if (a instanceof Date && b instanceof Date) {
5095
5488
  const ta = a.getTime();
5096
5489
  const tb = b.getTime();
5097
- if (Number.isNaN(ta) || Number.isNaN(tb)) throw new TypeError(ERR.invalidDate("BST"));
5490
+ if (Number.isNaN(ta) || Number.isNaN(tb)) raise(TypeError, ERR.invalidDate("BST"));
5098
5491
  return ta > tb ? 1 : ta < tb ? -1 : 0;
5099
5492
  }
5100
5493
  if (typeof a === "object" || typeof b === "object") {
5101
- throw new TypeError(
5102
- ERR.comparatorRequired("BST")
5103
- );
5494
+ raise(TypeError, ERR.comparatorRequired("BST"));
5104
5495
  }
5105
5496
  return 0;
5106
5497
  };
@@ -5419,7 +5810,8 @@ var BST = class extends BinaryTree {
5419
5810
  _snapshotOptions() {
5420
5811
  return {
5421
5812
  ...super._snapshotOptions(),
5422
- comparator: this._comparator
5813
+ comparator: this._comparator,
5814
+ enableOrderStatistic: this._enableOrderStatistic
5423
5815
  };
5424
5816
  }
5425
5817
  /**
@@ -5441,6 +5833,113 @@ var BST = class extends BinaryTree {
5441
5833
  *
5442
5834
  * @param v - The node to set as root.
5443
5835
  */
5836
+ /**
5837
+ * (Protected) Recalculates the subtree count for a single node.
5838
+ * @remarks Time O(1). Only active when enableOrderStatistic is true.
5839
+ */
5840
+ _updateCount(node) {
5841
+ if (!this._enableOrderStatistic) return;
5842
+ node._count = 1 + (this.isRealNode(node.left) ? node.left._count : 0) + (this.isRealNode(node.right) ? node.right._count : 0);
5843
+ }
5844
+ /**
5845
+ * (Protected) Updates subtree counts from a node up to the root.
5846
+ * @remarks Time O(log n). Only active when enableOrderStatistic is true.
5847
+ */
5848
+ _updateCountAlongPath(node) {
5849
+ if (!this._enableOrderStatistic) return;
5850
+ let current = node;
5851
+ while (current) {
5852
+ this._updateCount(current);
5853
+ current = current.parent;
5854
+ }
5855
+ }
5856
+ /**
5857
+ * (Protected) Finds the node at position k in tree order (iterative).
5858
+ * @remarks Time O(log n), Space O(1)
5859
+ */
5860
+ _getByRankIterative(node, k) {
5861
+ let current = node;
5862
+ let remaining = k;
5863
+ while (current) {
5864
+ const leftCount = this.isRealNode(current.left) ? current.left._count : 0;
5865
+ if (remaining < leftCount) {
5866
+ current = current.left;
5867
+ } else if (remaining === leftCount) {
5868
+ return current;
5869
+ } else {
5870
+ remaining = remaining - leftCount - 1;
5871
+ current = current.right;
5872
+ }
5873
+ }
5874
+ return void 0;
5875
+ }
5876
+ /**
5877
+ * (Protected) Finds the node at position k in tree order (recursive).
5878
+ * @remarks Time O(log n), Space O(log n) call stack
5879
+ */
5880
+ _getByRankRecursive(node, k) {
5881
+ if (!node) return void 0;
5882
+ const leftCount = this.isRealNode(node.left) ? node.left._count : 0;
5883
+ if (k < leftCount) return this._getByRankRecursive(node.left, k);
5884
+ if (k === leftCount) return node;
5885
+ return this._getByRankRecursive(node.right, k - leftCount - 1);
5886
+ }
5887
+ /**
5888
+ * (Protected) Computes the rank of a key iteratively.
5889
+ * @remarks Time O(log n), Space O(1)
5890
+ */
5891
+ _getRankIterative(node, key) {
5892
+ let rank = 0;
5893
+ let current = node;
5894
+ while (this.isRealNode(current)) {
5895
+ const cmp = this._compare(current.key, key);
5896
+ if (cmp > 0) {
5897
+ current = current.left;
5898
+ } else if (cmp < 0) {
5899
+ rank += (this.isRealNode(current.left) ? current.left._count : 0) + 1;
5900
+ current = current.right;
5901
+ } else {
5902
+ rank += this.isRealNode(current.left) ? current.left._count : 0;
5903
+ return rank;
5904
+ }
5905
+ }
5906
+ return rank;
5907
+ }
5908
+ /**
5909
+ * (Protected) Computes the rank of a key recursively.
5910
+ * @remarks Time O(log n), Space O(log n) call stack
5911
+ */
5912
+ _getRankRecursive(node, key) {
5913
+ if (!node) return 0;
5914
+ const cmp = this._compare(node.key, key);
5915
+ if (cmp > 0) {
5916
+ return this._getRankRecursive(node.left, key);
5917
+ } else if (cmp < 0) {
5918
+ return (this.isRealNode(node.left) ? node.left._count : 0) + 1 + this._getRankRecursive(node.right, key);
5919
+ } else {
5920
+ return this.isRealNode(node.left) ? node.left._count : 0;
5921
+ }
5922
+ }
5923
+ /**
5924
+ * (Protected) Finds the in-order successor of a node.
5925
+ * @remarks Time O(log n), Space O(1)
5926
+ */
5927
+ _next(node) {
5928
+ if (this.isRealNode(node.right)) {
5929
+ let current2 = node.right;
5930
+ while (this.isRealNode(current2.left)) {
5931
+ current2 = current2.left;
5932
+ }
5933
+ return current2;
5934
+ }
5935
+ let current = node;
5936
+ let parent = current.parent;
5937
+ while (parent && current === parent.right) {
5938
+ current = parent;
5939
+ parent = parent.parent;
5940
+ }
5941
+ return parent;
5942
+ }
5444
5943
  _setRoot(v) {
5445
5944
  if (v) v.parent = void 0;
5446
5945
  this._root = v;
@@ -5487,21 +5986,28 @@ var BST = class extends BinaryTree {
5487
5986
  while (x.left !== void 0 && x.left !== null) x = x.left;
5488
5987
  return x;
5489
5988
  }, "minNode");
5989
+ let countUpdateStart;
5490
5990
  if (node.left === void 0) {
5991
+ countUpdateStart = node.parent;
5491
5992
  transplant(node, node.right);
5492
5993
  } else if (node.right === void 0) {
5994
+ countUpdateStart = node.parent;
5493
5995
  transplant(node, node.left);
5494
5996
  } else {
5495
5997
  const succ = minNode(node.right);
5496
5998
  if (succ.parent !== node) {
5999
+ countUpdateStart = succ.parent;
5497
6000
  transplant(succ, succ.right);
5498
6001
  succ.right = node.right;
5499
6002
  if (succ.right) succ.right.parent = succ;
6003
+ } else {
6004
+ countUpdateStart = succ;
5500
6005
  }
5501
6006
  transplant(node, succ);
5502
6007
  succ.left = node.left;
5503
6008
  if (succ.left) succ.left.parent = succ;
5504
6009
  }
6010
+ this._updateCountAlongPath(countUpdateStart);
5505
6011
  this._size = Math.max(0, this._size - 1);
5506
6012
  return true;
5507
6013
  }
@@ -5780,6 +6286,34 @@ var AVLTree = class extends BST {
5780
6286
 
5781
6287
 
5782
6288
 
6289
+
6290
+
6291
+
6292
+
6293
+
6294
+
6295
+
6296
+
6297
+
6298
+
6299
+
6300
+
6301
+
6302
+
6303
+
6304
+
6305
+
6306
+
6307
+
6308
+
6309
+
6310
+
6311
+
6312
+
6313
+
6314
+
6315
+
6316
+
5783
6317
 
5784
6318
 
5785
6319
 
@@ -5895,6 +6429,27 @@ var AVLTree = class extends BST {
5895
6429
 
5896
6430
 
5897
6431
 
6432
+
6433
+
6434
+
6435
+
6436
+
6437
+
6438
+
6439
+
6440
+
6441
+
6442
+
6443
+
6444
+
6445
+
6446
+
6447
+
6448
+
6449
+
6450
+
6451
+
6452
+
5898
6453
 
5899
6454
 
5900
6455
 
@@ -5924,13 +6479,13 @@ var AVLTree = class extends BST {
5924
6479
  * console.log(avl.size); // 6;
5925
6480
  */
5926
6481
  delete(keyNodeOrEntry) {
5927
- const deletedResults = super.delete(keyNodeOrEntry);
6482
+ const deletedResults = this._deleteInternal(keyNodeOrEntry);
5928
6483
  for (const { needBalanced } of deletedResults) {
5929
6484
  if (needBalanced) {
5930
6485
  this._balancePath(needBalanced);
5931
6486
  }
5932
6487
  }
5933
- return deletedResults;
6488
+ return deletedResults.length > 0;
5934
6489
  }
5935
6490
  /**
5936
6491
  * Rebuilds the tree to be perfectly balanced.
@@ -5978,6 +6533,20 @@ var AVLTree = class extends BST {
5978
6533
 
5979
6534
 
5980
6535
 
6536
+
6537
+
6538
+
6539
+
6540
+
6541
+
6542
+
6543
+
6544
+
6545
+
6546
+
6547
+
6548
+
6549
+
5981
6550
 
5982
6551
 
5983
6552
 
@@ -6099,6 +6668,27 @@ var AVLTree = class extends BST {
6099
6668
 
6100
6669
 
6101
6670
 
6671
+
6672
+
6673
+
6674
+
6675
+
6676
+
6677
+
6678
+
6679
+
6680
+
6681
+
6682
+
6683
+
6684
+
6685
+
6686
+
6687
+
6688
+
6689
+
6690
+
6691
+
6102
6692
 
6103
6693
 
6104
6694
 
@@ -6238,6 +6828,8 @@ var AVLTree = class extends BST {
6238
6828
  }
6239
6829
  this._updateHeight(A);
6240
6830
  if (B) this._updateHeight(B);
6831
+ this._updateCount(A);
6832
+ if (B) this._updateCount(B);
6241
6833
  }
6242
6834
  /**
6243
6835
  * (Protected) Performs a Left-Right (LR) double rotation.
@@ -6283,6 +6875,9 @@ var AVLTree = class extends BST {
6283
6875
  this._updateHeight(A);
6284
6876
  if (B) this._updateHeight(B);
6285
6877
  if (C) this._updateHeight(C);
6878
+ this._updateCount(A);
6879
+ if (B) this._updateCount(B);
6880
+ if (C) this._updateCount(C);
6286
6881
  }
6287
6882
  /**
6288
6883
  * (Protected) Performs a Right-Right (RR) rotation (a single left rotation).
@@ -6317,6 +6912,8 @@ var AVLTree = class extends BST {
6317
6912
  }
6318
6913
  this._updateHeight(A);
6319
6914
  if (B) this._updateHeight(B);
6915
+ this._updateCount(A);
6916
+ if (B) this._updateCount(B);
6320
6917
  }
6321
6918
  /**
6322
6919
  * (Protected) Performs a Right-Left (RL) double rotation.
@@ -6360,6 +6957,9 @@ var AVLTree = class extends BST {
6360
6957
  this._updateHeight(A);
6361
6958
  if (B) this._updateHeight(B);
6362
6959
  if (C) this._updateHeight(C);
6960
+ this._updateCount(A);
6961
+ if (B) this._updateCount(B);
6962
+ if (C) this._updateCount(C);
6363
6963
  }
6364
6964
  /**
6365
6965
  * (Protected) Traverses up the tree from the specified node, updating heights and performing rotations as needed.
@@ -6374,6 +6974,7 @@ var AVLTree = class extends BST {
6374
6974
  const A = path[i];
6375
6975
  if (A) {
6376
6976
  this._updateHeight(A);
6977
+ this._updateCount(A);
6377
6978
  switch (this._balanceFactor(A)) {
6378
6979
  case -2:
6379
6980
  if (A && A.left) {