binary-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 +340 -107
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +339 -106
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +340 -108
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +339 -107
  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/binary-tree-typed.js +337 -105
  39. package/dist/umd/binary-tree-typed.js.map +1 -1
  40. package/dist/umd/binary-tree-typed.min.js +5 -5
  41. package/dist/umd/binary-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,60 @@ function makeTrampoline(fn) {
59
59
  }
60
60
  __name(makeTrampoline, "makeTrampoline");
61
61
 
62
+ // src/common/error.ts
63
+ function raise(ErrorClass, message) {
64
+ throw new ErrorClass(message);
65
+ }
66
+ __name(raise, "raise");
67
+ var ERR = {
68
+ // Range / index
69
+ indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
70
+ invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
71
+ // Type / argument
72
+ invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
73
+ comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
74
+ invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
75
+ notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
76
+ invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
77
+ invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
78
+ invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
79
+ reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
80
+ callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
81
+ // State / operation
82
+ invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
83
+ // Matrix
84
+ matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
85
+ matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
86
+ matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
87
+ matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
88
+ matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch"),
89
+ // Order statistic
90
+ orderStatisticNotEnabled: /* @__PURE__ */ __name((method, ctx) => `${ctx ? ctx + ": " : ""}${method}() requires enableOrderStatistic: true.`, "orderStatisticNotEnabled")
91
+ };
92
+
93
+ // src/common/index.ts
94
+ var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
95
+ DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
96
+ DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
97
+ return DFSOperation2;
98
+ })(DFSOperation || {});
99
+ var _Range = class _Range {
100
+ constructor(low, high, includeLow = true, includeHigh = true) {
101
+ this.low = low;
102
+ this.high = high;
103
+ this.includeLow = includeLow;
104
+ this.includeHigh = includeHigh;
105
+ }
106
+ // Determine whether a key is within the range
107
+ isInRange(key, comparator) {
108
+ const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
109
+ const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
110
+ return lowCheck && highCheck;
111
+ }
112
+ };
113
+ __name(_Range, "Range");
114
+ var Range = _Range;
115
+
62
116
  // src/data-structures/base/iterable-element-base.ts
63
117
  var _IterableElementBase = class _IterableElementBase {
64
118
  /**
@@ -81,7 +135,7 @@ var _IterableElementBase = class _IterableElementBase {
81
135
  if (options) {
82
136
  const { toElementFn } = options;
83
137
  if (typeof toElementFn === "function") this._toElementFn = toElementFn;
84
- else if (toElementFn) throw new TypeError("toElementFn must be a function type");
138
+ else if (toElementFn) raise(TypeError, "toElementFn must be a function type");
85
139
  }
86
140
  }
87
141
  /**
@@ -237,7 +291,7 @@ var _IterableElementBase = class _IterableElementBase {
237
291
  acc = initialValue;
238
292
  } else {
239
293
  const first = iter.next();
240
- if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
294
+ if (first.done) raise(TypeError, "Reduce of empty structure with no initial value");
241
295
  acc = first.value;
242
296
  index = 1;
243
297
  }
@@ -473,54 +527,6 @@ var _LinearBase = class _LinearBase extends IterableElementBase {
473
527
  __name(_LinearBase, "LinearBase");
474
528
  var LinearBase = _LinearBase;
475
529
 
476
- // src/common/error.ts
477
- var ERR = {
478
- // Range / index
479
- indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
480
- invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
481
- // Type / argument
482
- invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
483
- comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
484
- invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
485
- notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
486
- invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
487
- invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
488
- invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
489
- reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
490
- callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
491
- // State / operation
492
- invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
493
- // Matrix
494
- matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
495
- matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
496
- matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
497
- matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
498
- matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
499
- };
500
-
501
- // src/common/index.ts
502
- var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
503
- DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
504
- DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
505
- return DFSOperation2;
506
- })(DFSOperation || {});
507
- var _Range = class _Range {
508
- constructor(low, high, includeLow = true, includeHigh = true) {
509
- this.low = low;
510
- this.high = high;
511
- this.includeLow = includeLow;
512
- this.includeHigh = includeHigh;
513
- }
514
- // Determine whether a key is within the range
515
- isInRange(key, comparator) {
516
- const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
517
- const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
518
- return lowCheck && highCheck;
519
- }
520
- };
521
- __name(_Range, "Range");
522
- var Range = _Range;
523
-
524
530
  // src/data-structures/base/iterable-entry-base.ts
525
531
  var _IterableEntryBase = class _IterableEntryBase {
526
532
  /**
@@ -783,6 +789,13 @@ var _Queue = class _Queue extends LinearBase {
783
789
 
784
790
 
785
791
 
792
+
793
+
794
+
795
+
796
+
797
+
798
+
786
799
 
787
800
 
788
801
 
@@ -830,6 +843,13 @@ var _Queue = class _Queue extends LinearBase {
830
843
 
831
844
 
832
845
 
846
+
847
+
848
+
849
+
850
+
851
+
852
+
833
853
 
834
854
 
835
855
 
@@ -847,6 +867,14 @@ var _Queue = class _Queue extends LinearBase {
847
867
  get first() {
848
868
  return this.length > 0 ? this.elements[this._offset] : void 0;
849
869
  }
870
+ /**
871
+ * Peek at the front element without removing it (alias for `first`).
872
+ * @remarks Time O(1), Space O(1)
873
+ * @returns Front element or undefined.
874
+ */
875
+ peek() {
876
+ return this.first;
877
+ }
850
878
  /**
851
879
  * Get the last element (back) without removing it.
852
880
  * @remarks Time O(1), Space O(1)
@@ -893,6 +921,13 @@ var _Queue = class _Queue extends LinearBase {
893
921
 
894
922
 
895
923
 
924
+
925
+
926
+
927
+
928
+
929
+
930
+
896
931
 
897
932
 
898
933
 
@@ -952,6 +987,13 @@ var _Queue = class _Queue extends LinearBase {
952
987
 
953
988
 
954
989
 
990
+
991
+
992
+
993
+
994
+
995
+
996
+
955
997
 
956
998
 
957
999
 
@@ -1018,6 +1060,13 @@ var _Queue = class _Queue extends LinearBase {
1018
1060
 
1019
1061
 
1020
1062
 
1063
+
1064
+
1065
+
1066
+
1067
+
1068
+
1069
+
1021
1070
 
1022
1071
 
1023
1072
 
@@ -1074,6 +1123,13 @@ var _Queue = class _Queue extends LinearBase {
1074
1123
 
1075
1124
 
1076
1125
 
1126
+
1127
+
1128
+
1129
+
1130
+
1131
+
1132
+
1077
1133
 
1078
1134
 
1079
1135
 
@@ -1123,6 +1179,13 @@ var _Queue = class _Queue extends LinearBase {
1123
1179
 
1124
1180
 
1125
1181
 
1182
+
1183
+
1184
+
1185
+
1186
+
1187
+
1188
+
1126
1189
 
1127
1190
 
1128
1191
 
@@ -1177,6 +1240,21 @@ var _Queue = class _Queue extends LinearBase {
1177
1240
  this._elements[this._offset + index] = newElement;
1178
1241
  return true;
1179
1242
  }
1243
+ /**
1244
+ * Delete the first element that satisfies a predicate.
1245
+ * @remarks Time O(N), Space O(N)
1246
+ * @param predicate - Function (value, index, queue) → boolean to decide deletion.
1247
+ * @returns True if a match was removed.
1248
+ */
1249
+ deleteWhere(predicate) {
1250
+ for (let i = 0; i < this.length; i++) {
1251
+ if (predicate(this._elements[this._offset + i], i, this)) {
1252
+ this.deleteAt(i);
1253
+ return true;
1254
+ }
1255
+ }
1256
+ return false;
1257
+ }
1180
1258
  /**
1181
1259
  * Reverse the queue in-place by compacting then reversing.
1182
1260
  * @remarks Time O(N), Space O(N)
@@ -1213,6 +1291,13 @@ var _Queue = class _Queue extends LinearBase {
1213
1291
 
1214
1292
 
1215
1293
 
1294
+
1295
+
1296
+
1297
+
1298
+
1299
+
1300
+
1216
1301
 
1217
1302
 
1218
1303
 
@@ -1256,6 +1341,13 @@ var _Queue = class _Queue extends LinearBase {
1256
1341
 
1257
1342
 
1258
1343
 
1344
+
1345
+
1346
+
1347
+
1348
+
1349
+
1350
+
1259
1351
 
1260
1352
 
1261
1353
 
@@ -1322,6 +1414,13 @@ var _Queue = class _Queue extends LinearBase {
1322
1414
 
1323
1415
 
1324
1416
 
1417
+
1418
+
1419
+
1420
+
1421
+
1422
+
1423
+
1325
1424
 
1326
1425
 
1327
1426
 
@@ -1372,6 +1471,13 @@ var _Queue = class _Queue extends LinearBase {
1372
1471
 
1373
1472
 
1374
1473
 
1474
+
1475
+
1476
+
1477
+
1478
+
1479
+
1480
+
1375
1481
 
1376
1482
 
1377
1483
 
@@ -1426,6 +1532,13 @@ var _Queue = class _Queue extends LinearBase {
1426
1532
 
1427
1533
 
1428
1534
 
1535
+
1536
+
1537
+
1538
+
1539
+
1540
+
1541
+
1429
1542
 
1430
1543
 
1431
1544
 
@@ -1702,7 +1815,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1702
1815
  if (isMapMode !== void 0) this._isMapMode = isMapMode;
1703
1816
  if (isDuplicate !== void 0) this._isDuplicate = isDuplicate;
1704
1817
  if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
1705
- else if (toEntryFn) throw new TypeError(ERR.notAFunction("toEntryFn", "BinaryTree"));
1818
+ else if (toEntryFn) raise(TypeError, ERR.notAFunction("toEntryFn", "BinaryTree"));
1706
1819
  }
1707
1820
  if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
1708
1821
  }
@@ -1908,7 +2021,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1908
2021
  }
1909
2022
  /**
1910
2023
  * Adds a new node to the tree.
1911
- * @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).
2024
+ * @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).
1912
2025
  *
1913
2026
  * @param keyNodeOrEntry - The key, node, or entry to add.
1914
2027
  * @returns True if the addition was successful, false otherwise.
@@ -1931,6 +2044,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1931
2044
 
1932
2045
 
1933
2046
 
2047
+
2048
+
2049
+
2050
+
2051
+
2052
+
2053
+
1934
2054
 
1935
2055
 
1936
2056
 
@@ -1953,7 +2073,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1953
2073
  }
1954
2074
  /**
1955
2075
  * Adds or updates a new node to the tree.
1956
- * @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).
2076
+ * @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).
1957
2077
  *
1958
2078
  * @param keyNodeOrEntry - The key, node, or entry to set or update.
1959
2079
  * @param [value] - The value, if providing just a key.
@@ -1982,6 +2102,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1982
2102
 
1983
2103
 
1984
2104
 
2105
+
2106
+
2107
+
2108
+
2109
+
2110
+
2111
+
1985
2112
 
1986
2113
 
1987
2114
 
@@ -2085,6 +2212,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2085
2212
 
2086
2213
 
2087
2214
 
2215
+
2216
+
2217
+
2218
+
2219
+
2220
+
2221
+
2088
2222
 
2089
2223
 
2090
2224
 
@@ -2124,6 +2258,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2124
2258
 
2125
2259
 
2126
2260
 
2261
+
2262
+
2263
+
2264
+
2265
+
2266
+
2267
+
2127
2268
 
2128
2269
 
2129
2270
 
@@ -2184,6 +2325,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2184
2325
 
2185
2326
 
2186
2327
 
2328
+
2329
+
2330
+
2331
+
2332
+
2333
+
2334
+
2187
2335
 
2188
2336
 
2189
2337
 
@@ -2203,22 +2351,69 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2203
2351
  this.setMany(anotherTree, []);
2204
2352
  }
2205
2353
  /**
2206
- * Clears the tree and refills it with new items.
2207
- * @remarks Time O(N) (for `clear`) + O(N * M) (for `setMany`) = O(N * M). Space O(M) (from `setMany`).
2354
+ * Deletes a node from the tree (internal, returns balancing metadata).
2355
+ * @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).
2356
+ * @internal Used by AVL/BST subclasses that need balancing metadata after deletion.
2208
2357
  *
2209
- * @param keysNodesEntriesOrRaws - An iterable of items to set.
2210
- * @param [values] - An optional parallel iterable of values.
2358
+ * @param keyNodeEntryRawOrPredicate - The node to delete.
2359
+ * @returns An array containing deletion results with balancing metadata.
2211
2360
  */
2212
- refill(keysNodesEntriesOrRaws, values) {
2213
- this.clear();
2214
- this.setMany(keysNodesEntriesOrRaws, values);
2361
+ _deleteInternal(keyNodeEntryRawOrPredicate) {
2362
+ const deletedResult = [];
2363
+ if (!this._root) return deletedResult;
2364
+ const curr = this.getNode(keyNodeEntryRawOrPredicate);
2365
+ if (!curr) return deletedResult;
2366
+ const parent = curr == null ? void 0 : curr.parent;
2367
+ let needBalanced;
2368
+ let orgCurrent = curr;
2369
+ if (!curr.left && !curr.right && !parent) {
2370
+ this._setRoot(void 0);
2371
+ } else if (curr.left) {
2372
+ const leftSubTreeRightMost = this.getRightMost((node) => node, curr.left);
2373
+ if (leftSubTreeRightMost) {
2374
+ const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
2375
+ orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
2376
+ if (this._isMapMode) {
2377
+ this._store.set(curr.key, curr);
2378
+ this._store.set(leftSubTreeRightMost.key, leftSubTreeRightMost);
2379
+ }
2380
+ if (parentOfLeftSubTreeMax) {
2381
+ if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
2382
+ parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
2383
+ else parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
2384
+ needBalanced = parentOfLeftSubTreeMax;
2385
+ }
2386
+ }
2387
+ } else if (parent) {
2388
+ const { familyPosition: fp } = curr;
2389
+ if (fp === "LEFT" || fp === "ROOT_LEFT") {
2390
+ parent.left = curr.right;
2391
+ } else if (fp === "RIGHT" || fp === "ROOT_RIGHT") {
2392
+ parent.right = curr.right;
2393
+ }
2394
+ needBalanced = parent;
2395
+ } else {
2396
+ this._setRoot(curr.right);
2397
+ curr.right = void 0;
2398
+ }
2399
+ this._size = this._size - 1;
2400
+ deletedResult.push({ deleted: orgCurrent, needBalanced });
2401
+ if (this._isMapMode && orgCurrent) this._store.delete(orgCurrent.key);
2402
+ return deletedResult;
2215
2403
  }
2216
2404
  /**
2217
2405
  * Deletes a node from the tree.
2218
- * @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).
2406
+ * @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).
2219
2407
  *
2220
2408
  * @param keyNodeEntryRawOrPredicate - The node to delete.
2221
- * @returns An array containing deletion results (for compatibility with self-balancing trees).
2409
+ * @returns True if the node was found and deleted, false otherwise.
2410
+
2411
+
2412
+
2413
+
2414
+
2415
+
2416
+
2222
2417
 
2223
2418
 
2224
2419
 
@@ -2259,51 +2454,11 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2259
2454
  * console.log(tree.size); // 4;
2260
2455
  */
2261
2456
  delete(keyNodeEntryRawOrPredicate) {
2262
- const deletedResult = [];
2263
- if (!this._root) return deletedResult;
2264
- const curr = this.getNode(keyNodeEntryRawOrPredicate);
2265
- if (!curr) return deletedResult;
2266
- const parent = curr == null ? void 0 : curr.parent;
2267
- let needBalanced;
2268
- let orgCurrent = curr;
2269
- if (!curr.left && !curr.right && !parent) {
2270
- this._setRoot(void 0);
2271
- } else if (curr.left) {
2272
- const leftSubTreeRightMost = this.getRightMost((node) => node, curr.left);
2273
- if (leftSubTreeRightMost) {
2274
- const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
2275
- orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
2276
- if (this._isMapMode) {
2277
- this._store.set(curr.key, curr);
2278
- this._store.set(leftSubTreeRightMost.key, leftSubTreeRightMost);
2279
- }
2280
- if (parentOfLeftSubTreeMax) {
2281
- if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
2282
- parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
2283
- else parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
2284
- needBalanced = parentOfLeftSubTreeMax;
2285
- }
2286
- }
2287
- } else if (parent) {
2288
- const { familyPosition: fp } = curr;
2289
- if (fp === "LEFT" || fp === "ROOT_LEFT") {
2290
- parent.left = curr.right;
2291
- } else if (fp === "RIGHT" || fp === "ROOT_RIGHT") {
2292
- parent.right = curr.right;
2293
- }
2294
- needBalanced = parent;
2295
- } else {
2296
- this._setRoot(curr.right);
2297
- curr.right = void 0;
2298
- }
2299
- this._size = this._size - 1;
2300
- deletedResult.push({ deleted: orgCurrent, needBalanced });
2301
- if (this._isMapMode && orgCurrent) this._store.delete(orgCurrent.key);
2302
- return deletedResult;
2457
+ return this._deleteInternal(keyNodeEntryRawOrPredicate).length > 0;
2303
2458
  }
2304
2459
  /**
2305
2460
  * Searches the tree for nodes matching a predicate.
2306
- * @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).
2461
+ * @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).
2307
2462
  *
2308
2463
  * @template C - The type of the callback function.
2309
2464
  * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
@@ -2352,7 +2507,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2352
2507
  }
2353
2508
  /**
2354
2509
  * Gets the first node matching a predicate.
2355
- * @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`).
2510
+ * @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.
2356
2511
  *
2357
2512
  * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
2358
2513
  * @param [startNode=this._root] - The node to start the search from.
@@ -2380,6 +2535,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2380
2535
 
2381
2536
 
2382
2537
 
2538
+
2539
+
2540
+
2541
+
2542
+
2543
+
2544
+
2383
2545
 
2384
2546
 
2385
2547
 
@@ -2405,7 +2567,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2405
2567
  }
2406
2568
  /**
2407
2569
  * Gets the value associated with a key.
2408
- * @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.
2570
+ * @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).
2409
2571
  *
2410
2572
  * @param keyNodeEntryOrPredicate - The key, node, or entry to get the value for.
2411
2573
  * @param [startNode=this._root] - The node to start searching from (if not in Map mode).
@@ -2435,6 +2597,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2435
2597
 
2436
2598
 
2437
2599
 
2600
+
2601
+
2602
+
2603
+
2604
+
2605
+
2606
+
2438
2607
 
2439
2608
 
2440
2609
 
@@ -2493,6 +2662,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2493
2662
 
2494
2663
 
2495
2664
 
2665
+
2666
+
2667
+
2668
+
2669
+
2670
+
2671
+
2496
2672
 
2497
2673
 
2498
2674
 
@@ -2538,6 +2714,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2538
2714
 
2539
2715
 
2540
2716
 
2717
+
2718
+
2719
+
2720
+
2721
+
2722
+
2723
+
2541
2724
 
2542
2725
 
2543
2726
 
@@ -2592,6 +2775,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2592
2775
 
2593
2776
 
2594
2777
 
2778
+
2779
+
2780
+
2781
+
2782
+
2783
+
2784
+
2595
2785
 
2596
2786
 
2597
2787
 
@@ -2673,6 +2863,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2673
2863
 
2674
2864
 
2675
2865
 
2866
+
2867
+
2868
+
2869
+
2870
+
2871
+
2872
+
2676
2873
 
2677
2874
 
2678
2875
 
@@ -2731,6 +2928,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2731
2928
 
2732
2929
 
2733
2930
 
2931
+
2932
+
2933
+
2934
+
2935
+
2936
+
2937
+
2734
2938
 
2735
2939
 
2736
2940
 
@@ -3205,6 +3409,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
3205
3409
 
3206
3410
 
3207
3411
 
3412
+
3413
+
3414
+
3415
+
3416
+
3417
+
3418
+
3208
3419
 
3209
3420
 
3210
3421
 
@@ -3254,6 +3465,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
3254
3465
 
3255
3466
 
3256
3467
 
3468
+
3469
+
3470
+
3471
+
3472
+
3473
+
3474
+
3257
3475
 
3258
3476
 
3259
3477
 
@@ -3307,6 +3525,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
3307
3525
 
3308
3526
 
3309
3527
 
3528
+
3529
+
3530
+
3531
+
3532
+
3533
+
3534
+
3310
3535
 
3311
3536
 
3312
3537
 
@@ -3385,6 +3610,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
3385
3610
 
3386
3611
 
3387
3612
 
3613
+
3614
+
3615
+
3616
+
3617
+
3618
+
3619
+
3388
3620
 
3389
3621
 
3390
3622
 
@@ -3868,6 +4100,6 @@ var BinaryTree = _BinaryTree;
3868
4100
  * @license MIT License
3869
4101
  */
3870
4102
 
3871
- export { BinaryTree, BinaryTreeNode, DFSOperation, ERR, Range };
4103
+ export { BinaryTree, BinaryTreeNode, DFSOperation, ERR, Range, raise };
3872
4104
  //# sourceMappingURL=index.mjs.map
3873
4105
  //# sourceMappingURL=index.mjs.map