binary-tree-typed 2.5.1 → 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 (73) hide show
  1. package/dist/cjs/index.cjs +146 -52
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +145 -51
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +146 -53
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +145 -52
  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 +36 -0
  12. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +42 -0
  13. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +77 -2
  14. package/dist/types/data-structures/binary-tree/bst.d.ts +171 -0
  15. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +57 -0
  16. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +18 -0
  17. package/dist/types/data-structures/binary-tree/tree-map.d.ts +409 -0
  18. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +411 -6
  19. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +339 -6
  20. package/dist/types/data-structures/binary-tree/tree-set.d.ts +391 -0
  21. package/dist/types/data-structures/graph/directed-graph.d.ts +30 -0
  22. package/dist/types/data-structures/graph/undirected-graph.d.ts +27 -0
  23. package/dist/types/data-structures/hash/hash-map.d.ts +33 -0
  24. package/dist/types/data-structures/heap/heap.d.ts +42 -0
  25. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +51 -0
  26. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +45 -0
  27. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +54 -0
  28. package/dist/types/data-structures/matrix/matrix.d.ts +24 -0
  29. package/dist/types/data-structures/queue/deque.d.ts +45 -0
  30. package/dist/types/data-structures/queue/queue.d.ts +36 -0
  31. package/dist/types/data-structures/stack/stack.d.ts +30 -0
  32. package/dist/types/data-structures/trie/trie.d.ts +36 -0
  33. package/dist/types/types/data-structures/binary-tree/bst.d.ts +1 -0
  34. package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +5 -0
  35. package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +4 -0
  36. package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +4 -0
  37. package/dist/umd/binary-tree-typed.js +143 -50
  38. package/dist/umd/binary-tree-typed.js.map +1 -1
  39. package/dist/umd/binary-tree-typed.min.js +3 -3
  40. package/dist/umd/binary-tree-typed.min.js.map +1 -1
  41. package/package.json +2 -2
  42. package/src/common/error.ts +19 -1
  43. package/src/common/index.ts +1 -1
  44. package/src/data-structures/base/iterable-element-base.ts +3 -2
  45. package/src/data-structures/binary-tree/avl-tree.ts +47 -0
  46. package/src/data-structures/binary-tree/binary-indexed-tree.ts +46 -4
  47. package/src/data-structures/binary-tree/binary-tree.ts +79 -4
  48. package/src/data-structures/binary-tree/bst.ts +441 -6
  49. package/src/data-structures/binary-tree/red-black-tree.ts +73 -0
  50. package/src/data-structures/binary-tree/segment-tree.ts +18 -0
  51. package/src/data-structures/binary-tree/tree-map.ts +434 -9
  52. package/src/data-structures/binary-tree/tree-multi-map.ts +426 -5
  53. package/src/data-structures/binary-tree/tree-multi-set.ts +350 -6
  54. package/src/data-structures/binary-tree/tree-set.ts +410 -8
  55. package/src/data-structures/graph/abstract-graph.ts +2 -2
  56. package/src/data-structures/graph/directed-graph.ts +30 -0
  57. package/src/data-structures/graph/undirected-graph.ts +27 -0
  58. package/src/data-structures/hash/hash-map.ts +35 -4
  59. package/src/data-structures/heap/heap.ts +46 -4
  60. package/src/data-structures/heap/max-heap.ts +2 -2
  61. package/src/data-structures/linked-list/doubly-linked-list.ts +51 -0
  62. package/src/data-structures/linked-list/singly-linked-list.ts +45 -0
  63. package/src/data-structures/linked-list/skip-linked-list.ts +59 -5
  64. package/src/data-structures/matrix/matrix.ts +33 -9
  65. package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
  66. package/src/data-structures/queue/deque.ts +45 -0
  67. package/src/data-structures/queue/queue.ts +36 -0
  68. package/src/data-structures/stack/stack.ts +30 -0
  69. package/src/data-structures/trie/trie.ts +38 -2
  70. package/src/types/data-structures/binary-tree/bst.ts +1 -0
  71. package/src/types/data-structures/binary-tree/tree-map.ts +6 -0
  72. package/src/types/data-structures/binary-tree/tree-multi-set.ts +5 -0
  73. package/src/types/data-structures/binary-tree/tree-set.ts +5 -0
@@ -8,6 +8,11 @@ export interface TreeMapOptions<K, V, R = [K, V]> {
8
8
  * - `false`: store values on tree nodes (Node Mode).
9
9
  */
10
10
  isMapMode?: boolean;
11
+ /**
12
+ * Enable order-statistic operations (select, rank, rangeByRank).
13
+ * When true, subtree counts are maintained on every node.
14
+ */
15
+ enableOrderStatistic?: boolean;
11
16
  /**
12
17
  * Transform raw elements into `[key, value]` entries.
13
18
  * When provided, the constructor accepts `Iterable<R>` instead of `Iterable<[K, V]>`.
@@ -8,6 +8,10 @@ export interface TreeMultiSetOptions<K, R = K> {
8
8
  * - `false`: Node Mode.
9
9
  */
10
10
  isMapMode?: boolean;
11
+ /**
12
+ * Enable order-statistic operations (select, rank, rangeByRank).
13
+ */
14
+ enableOrderStatistic?: boolean;
11
15
  /**
12
16
  * Transform raw elements into keys.
13
17
  * When provided, the constructor accepts `Iterable<R>` instead of `Iterable<K>`.
@@ -8,6 +8,10 @@ export interface TreeSetOptions<K, R = K> {
8
8
  * - `false`: store values on tree nodes (Node Mode).
9
9
  */
10
10
  isMapMode?: boolean;
11
+ /**
12
+ * Enable order-statistic operations (select, rank, rangeByRank).
13
+ */
14
+ enableOrderStatistic?: boolean;
11
15
  /**
12
16
  * Transform raw elements into keys.
13
17
  * When provided, the constructor accepts `Iterable<R>` instead of `Iterable<K>`.
@@ -27,7 +27,8 @@ var binaryTreeTyped = (() => {
27
27
  BinaryTreeNode: () => BinaryTreeNode,
28
28
  DFSOperation: () => DFSOperation,
29
29
  ERR: () => ERR,
30
- Range: () => Range
30
+ Range: () => Range,
31
+ raise: () => raise
31
32
  });
32
33
 
33
34
  // src/utils/utils.ts
@@ -81,6 +82,57 @@ var binaryTreeTyped = (() => {
81
82
  return (...args) => trampoline(fn(...args));
82
83
  }
83
84
 
85
+ // src/common/error.ts
86
+ function raise(ErrorClass, message) {
87
+ throw new ErrorClass(message);
88
+ }
89
+ var ERR = {
90
+ // Range / index
91
+ indexOutOfRange: (index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`,
92
+ invalidIndex: (ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`,
93
+ // Type / argument
94
+ invalidArgument: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
95
+ comparatorRequired: (ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`,
96
+ invalidKey: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
97
+ notAFunction: (name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`,
98
+ invalidEntry: (ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`,
99
+ invalidNaN: (ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`,
100
+ invalidDate: (ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`,
101
+ reduceEmpty: (ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`,
102
+ callbackReturnType: (expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`,
103
+ // State / operation
104
+ invalidOperation: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
105
+ // Matrix
106
+ matrixDimensionMismatch: (op) => `Matrix: Dimensions must be compatible for ${op}.`,
107
+ matrixSingular: () => "Matrix: Singular matrix, inverse does not exist.",
108
+ matrixNotSquare: () => "Matrix: Must be square for inversion.",
109
+ matrixNotRectangular: () => "Matrix: Must be rectangular for transposition.",
110
+ matrixRowMismatch: (expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`,
111
+ // Order statistic
112
+ orderStatisticNotEnabled: (method, ctx) => `${ctx ? ctx + ": " : ""}${method}() requires enableOrderStatistic: true.`
113
+ };
114
+
115
+ // src/common/index.ts
116
+ var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
117
+ DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
118
+ DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
119
+ return DFSOperation2;
120
+ })(DFSOperation || {});
121
+ var Range = class {
122
+ constructor(low, high, includeLow = true, includeHigh = true) {
123
+ this.low = low;
124
+ this.high = high;
125
+ this.includeLow = includeLow;
126
+ this.includeHigh = includeHigh;
127
+ }
128
+ // Determine whether a key is within the range
129
+ isInRange(key, comparator) {
130
+ const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
131
+ const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
132
+ return lowCheck && highCheck;
133
+ }
134
+ };
135
+
84
136
  // src/data-structures/base/iterable-element-base.ts
85
137
  var IterableElementBase = class {
86
138
  /**
@@ -103,7 +155,7 @@ var binaryTreeTyped = (() => {
103
155
  if (options) {
104
156
  const { toElementFn } = options;
105
157
  if (typeof toElementFn === "function") this._toElementFn = toElementFn;
106
- else if (toElementFn) throw new TypeError("toElementFn must be a function type");
158
+ else if (toElementFn) raise(TypeError, "toElementFn must be a function type");
107
159
  }
108
160
  }
109
161
  /**
@@ -259,7 +311,7 @@ var binaryTreeTyped = (() => {
259
311
  acc = initialValue;
260
312
  } else {
261
313
  const first = iter.next();
262
- if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
314
+ if (first.done) raise(TypeError, "Reduce of empty structure with no initial value");
263
315
  acc = first.value;
264
316
  index = 1;
265
317
  }
@@ -491,52 +543,6 @@ var binaryTreeTyped = (() => {
491
543
  }
492
544
  };
493
545
 
494
- // src/common/error.ts
495
- var ERR = {
496
- // Range / index
497
- indexOutOfRange: (index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`,
498
- invalidIndex: (ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`,
499
- // Type / argument
500
- invalidArgument: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
501
- comparatorRequired: (ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`,
502
- invalidKey: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
503
- notAFunction: (name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`,
504
- invalidEntry: (ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`,
505
- invalidNaN: (ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`,
506
- invalidDate: (ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`,
507
- reduceEmpty: (ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`,
508
- callbackReturnType: (expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`,
509
- // State / operation
510
- invalidOperation: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
511
- // Matrix
512
- matrixDimensionMismatch: (op) => `Matrix: Dimensions must be compatible for ${op}.`,
513
- matrixSingular: () => "Matrix: Singular matrix, inverse does not exist.",
514
- matrixNotSquare: () => "Matrix: Must be square for inversion.",
515
- matrixNotRectangular: () => "Matrix: Must be rectangular for transposition.",
516
- matrixRowMismatch: (expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`
517
- };
518
-
519
- // src/common/index.ts
520
- var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
521
- DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
522
- DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
523
- return DFSOperation2;
524
- })(DFSOperation || {});
525
- var Range = class {
526
- constructor(low, high, includeLow = true, includeHigh = true) {
527
- this.low = low;
528
- this.high = high;
529
- this.includeLow = includeLow;
530
- this.includeHigh = includeHigh;
531
- }
532
- // Determine whether a key is within the range
533
- isInRange(key, comparator) {
534
- const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
535
- const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
536
- return lowCheck && highCheck;
537
- }
538
- };
539
-
540
546
  // src/data-structures/base/iterable-entry-base.ts
541
547
  var IterableEntryBase = class {
542
548
  /**
@@ -801,6 +807,9 @@ var binaryTreeTyped = (() => {
801
807
 
802
808
 
803
809
 
810
+
811
+
812
+
804
813
 
805
814
 
806
815
 
@@ -848,6 +857,9 @@ var binaryTreeTyped = (() => {
848
857
 
849
858
 
850
859
 
860
+
861
+
862
+
851
863
 
852
864
 
853
865
 
@@ -911,6 +923,9 @@ var binaryTreeTyped = (() => {
911
923
 
912
924
 
913
925
 
926
+
927
+
928
+
914
929
 
915
930
 
916
931
 
@@ -970,6 +985,9 @@ var binaryTreeTyped = (() => {
970
985
 
971
986
 
972
987
 
988
+
989
+
990
+
973
991
 
974
992
 
975
993
 
@@ -1036,6 +1054,9 @@ var binaryTreeTyped = (() => {
1036
1054
 
1037
1055
 
1038
1056
 
1057
+
1058
+
1059
+
1039
1060
 
1040
1061
 
1041
1062
 
@@ -1092,6 +1113,9 @@ var binaryTreeTyped = (() => {
1092
1113
 
1093
1114
 
1094
1115
 
1116
+
1117
+
1118
+
1095
1119
 
1096
1120
 
1097
1121
 
@@ -1141,6 +1165,9 @@ var binaryTreeTyped = (() => {
1141
1165
 
1142
1166
 
1143
1167
 
1168
+
1169
+
1170
+
1144
1171
 
1145
1172
 
1146
1173
 
@@ -1231,6 +1258,9 @@ var binaryTreeTyped = (() => {
1231
1258
 
1232
1259
 
1233
1260
 
1261
+
1262
+
1263
+
1234
1264
 
1235
1265
 
1236
1266
 
@@ -1274,6 +1304,9 @@ var binaryTreeTyped = (() => {
1274
1304
 
1275
1305
 
1276
1306
 
1307
+
1308
+
1309
+
1277
1310
 
1278
1311
 
1279
1312
 
@@ -1340,6 +1373,9 @@ var binaryTreeTyped = (() => {
1340
1373
 
1341
1374
 
1342
1375
 
1376
+
1377
+
1378
+
1343
1379
 
1344
1380
 
1345
1381
 
@@ -1390,6 +1426,9 @@ var binaryTreeTyped = (() => {
1390
1426
 
1391
1427
 
1392
1428
 
1429
+
1430
+
1431
+
1393
1432
 
1394
1433
 
1395
1434
 
@@ -1444,6 +1483,9 @@ var binaryTreeTyped = (() => {
1444
1483
 
1445
1484
 
1446
1485
 
1486
+
1487
+
1488
+
1447
1489
 
1448
1490
 
1449
1491
 
@@ -1712,7 +1754,7 @@ var binaryTreeTyped = (() => {
1712
1754
  if (isMapMode !== void 0) this._isMapMode = isMapMode;
1713
1755
  if (isDuplicate !== void 0) this._isDuplicate = isDuplicate;
1714
1756
  if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
1715
- else if (toEntryFn) throw new TypeError(ERR.notAFunction("toEntryFn", "BinaryTree"));
1757
+ else if (toEntryFn) raise(TypeError, ERR.notAFunction("toEntryFn", "BinaryTree"));
1716
1758
  }
1717
1759
  if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
1718
1760
  }
@@ -1945,6 +1987,9 @@ var binaryTreeTyped = (() => {
1945
1987
 
1946
1988
 
1947
1989
 
1990
+
1991
+
1992
+
1948
1993
 
1949
1994
 
1950
1995
 
@@ -1996,6 +2041,9 @@ var binaryTreeTyped = (() => {
1996
2041
 
1997
2042
 
1998
2043
 
2044
+
2045
+
2046
+
1999
2047
 
2000
2048
 
2001
2049
 
@@ -2099,6 +2147,9 @@ var binaryTreeTyped = (() => {
2099
2147
 
2100
2148
 
2101
2149
 
2150
+
2151
+
2152
+
2102
2153
 
2103
2154
 
2104
2155
 
@@ -2138,6 +2189,9 @@ var binaryTreeTyped = (() => {
2138
2189
 
2139
2190
 
2140
2191
 
2192
+
2193
+
2194
+
2141
2195
 
2142
2196
 
2143
2197
 
@@ -2198,6 +2252,9 @@ var binaryTreeTyped = (() => {
2198
2252
 
2199
2253
 
2200
2254
 
2255
+
2256
+
2257
+
2201
2258
 
2202
2259
 
2203
2260
 
@@ -2257,6 +2314,9 @@ var binaryTreeTyped = (() => {
2257
2314
 
2258
2315
 
2259
2316
 
2317
+
2318
+
2319
+
2260
2320
 
2261
2321
 
2262
2322
 
@@ -2394,6 +2454,9 @@ var binaryTreeTyped = (() => {
2394
2454
 
2395
2455
 
2396
2456
 
2457
+
2458
+
2459
+
2397
2460
 
2398
2461
 
2399
2462
 
@@ -2449,6 +2512,9 @@ var binaryTreeTyped = (() => {
2449
2512
 
2450
2513
 
2451
2514
 
2515
+
2516
+
2517
+
2452
2518
 
2453
2519
 
2454
2520
 
@@ -2507,6 +2573,9 @@ var binaryTreeTyped = (() => {
2507
2573
 
2508
2574
 
2509
2575
 
2576
+
2577
+
2578
+
2510
2579
 
2511
2580
 
2512
2581
 
@@ -2552,6 +2621,9 @@ var binaryTreeTyped = (() => {
2552
2621
 
2553
2622
 
2554
2623
 
2624
+
2625
+
2626
+
2555
2627
 
2556
2628
 
2557
2629
 
@@ -2606,6 +2678,9 @@ var binaryTreeTyped = (() => {
2606
2678
 
2607
2679
 
2608
2680
 
2681
+
2682
+
2683
+
2609
2684
 
2610
2685
 
2611
2686
 
@@ -2687,6 +2762,9 @@ var binaryTreeTyped = (() => {
2687
2762
 
2688
2763
 
2689
2764
 
2765
+
2766
+
2767
+
2690
2768
 
2691
2769
 
2692
2770
 
@@ -2745,6 +2823,9 @@ var binaryTreeTyped = (() => {
2745
2823
 
2746
2824
 
2747
2825
 
2826
+
2827
+
2828
+
2748
2829
 
2749
2830
 
2750
2831
 
@@ -3219,6 +3300,9 @@ var binaryTreeTyped = (() => {
3219
3300
 
3220
3301
 
3221
3302
 
3303
+
3304
+
3305
+
3222
3306
 
3223
3307
 
3224
3308
 
@@ -3268,6 +3352,9 @@ var binaryTreeTyped = (() => {
3268
3352
 
3269
3353
 
3270
3354
 
3355
+
3356
+
3357
+
3271
3358
 
3272
3359
 
3273
3360
 
@@ -3321,6 +3408,9 @@ var binaryTreeTyped = (() => {
3321
3408
 
3322
3409
 
3323
3410
 
3411
+
3412
+
3413
+
3324
3414
 
3325
3415
 
3326
3416
 
@@ -3399,6 +3489,9 @@ var binaryTreeTyped = (() => {
3399
3489
 
3400
3490
 
3401
3491
 
3492
+
3493
+
3494
+
3402
3495
 
3403
3496
 
3404
3497