undirected-graph-typed 2.5.2 → 2.6.0

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 (63) hide show
  1. package/dist/cjs/index.cjs +352 -14
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +352 -14
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +352 -14
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +352 -14
  8. package/dist/esm-legacy/index.mjs.map +1 -1
  9. package/dist/types/data-structures/base/iterable-element-base.d.ts +17 -0
  10. package/dist/types/data-structures/base/linear-base.d.ts +6 -0
  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 +191 -15
  14. package/dist/types/data-structures/binary-tree/bst.d.ts +171 -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 +1061 -167
  18. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1232 -355
  19. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +916 -194
  20. package/dist/types/data-structures/binary-tree/tree-set.d.ts +1078 -141
  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 +150 -2
  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 +171 -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/umd/undirected-graph-typed.js +352 -14
  35. package/dist/umd/undirected-graph-typed.js.map +1 -1
  36. package/dist/umd/undirected-graph-typed.min.js +1 -1
  37. package/dist/umd/undirected-graph-typed.min.js.map +1 -1
  38. package/package.json +2 -2
  39. package/src/data-structures/base/iterable-element-base.ts +32 -0
  40. package/src/data-structures/base/linear-base.ts +11 -0
  41. package/src/data-structures/binary-tree/avl-tree.ts +88 -5
  42. package/src/data-structures/binary-tree/binary-indexed-tree.ts +98 -0
  43. package/src/data-structures/binary-tree/binary-tree.ts +242 -81
  44. package/src/data-structures/binary-tree/bst.ts +173 -7
  45. package/src/data-structures/binary-tree/red-black-tree.ts +139 -15
  46. package/src/data-structures/binary-tree/segment-tree.ts +42 -0
  47. package/src/data-structures/binary-tree/tree-map.ts +948 -36
  48. package/src/data-structures/binary-tree/tree-multi-map.ts +893 -13
  49. package/src/data-structures/binary-tree/tree-multi-set.ts +761 -33
  50. package/src/data-structures/binary-tree/tree-set.ts +1260 -251
  51. package/src/data-structures/graph/directed-graph.ts +71 -1
  52. package/src/data-structures/graph/undirected-graph.ts +64 -1
  53. package/src/data-structures/hash/hash-map.ts +100 -12
  54. package/src/data-structures/heap/heap.ts +149 -19
  55. package/src/data-structures/linked-list/doubly-linked-list.ts +178 -2
  56. package/src/data-structures/linked-list/singly-linked-list.ts +106 -1
  57. package/src/data-structures/linked-list/skip-linked-list.ts +126 -0
  58. package/src/data-structures/matrix/matrix.ts +56 -0
  59. package/src/data-structures/queue/deque.ts +187 -0
  60. package/src/data-structures/queue/queue.ts +109 -0
  61. package/src/data-structures/stack/stack.ts +75 -5
  62. package/src/data-structures/trie/trie.ts +84 -0
  63. package/src/interfaces/binary-tree.ts +1 -9
@@ -411,6 +411,35 @@ var IterableElementBase = class {
411
411
  for (const ele of this) if (ele === element) return true;
412
412
  return false;
413
413
  }
414
+ /**
415
+ * Check whether a value exists (Array-compatible alias for `has`).
416
+ * @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1).
417
+ * @param element - Element to search for (uses `===`).
418
+ * @returns `true` if found.
419
+ */
420
+ includes(element) {
421
+ return this.has(element);
422
+ }
423
+ /**
424
+ * Return an iterator of `[index, value]` pairs (Array-compatible).
425
+ * @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1) per step.
426
+ */
427
+ *entries() {
428
+ let index = 0;
429
+ for (const value of this) {
430
+ yield [index++, value];
431
+ }
432
+ }
433
+ /**
434
+ * Return an iterator of numeric indices (Array-compatible).
435
+ * @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1) per step.
436
+ */
437
+ *keys() {
438
+ let index = 0;
439
+ for (const _ of this) {
440
+ yield index++;
441
+ }
442
+ }
414
443
  /**
415
444
  * Reduces all elements to a single accumulated value.
416
445
  *
@@ -673,6 +702,16 @@ var LinearBase = class _LinearBase extends IterableElementBase {
673
702
  }
674
703
  return this;
675
704
  }
705
+ /**
706
+ * Return a new instance of the same type with elements in reverse order (non-mutating).
707
+ * @remarks Provided for familiarity when migrating from Array (ES2023 `toReversed`). Time O(n), Space O(n).
708
+ * @returns A new reversed instance.
709
+ */
710
+ toReversed() {
711
+ const cloned = this.clone();
712
+ cloned.reverse();
713
+ return cloned;
714
+ }
676
715
  };
677
716
 
678
717
  // src/data-structures/heap/heap.ts
@@ -736,6 +775,13 @@ var Heap = class _Heap extends IterableElementBase {
736
775
 
737
776
 
738
777
 
778
+
779
+
780
+
781
+
782
+
783
+
784
+
739
785
 
740
786
 
741
787
 
@@ -792,7 +838,7 @@ var Heap = class _Heap extends IterableElementBase {
792
838
  }
793
839
  /**
794
840
  * Insert an element.
795
- * @remarks Time O(1) amortized, Space O(1)
841
+ * @remarks Time O(log N) amortized, Space O(1)
796
842
  * @param element - Element to insert.
797
843
  * @returns True.
798
844
 
@@ -822,6 +868,13 @@ var Heap = class _Heap extends IterableElementBase {
822
868
 
823
869
 
824
870
 
871
+
872
+
873
+
874
+
875
+
876
+
877
+
825
878
 
826
879
 
827
880
 
@@ -879,6 +932,13 @@ var Heap = class _Heap extends IterableElementBase {
879
932
 
880
933
 
881
934
 
935
+
936
+
937
+
938
+
939
+
940
+
941
+
882
942
 
883
943
 
884
944
 
@@ -943,6 +1003,40 @@ var Heap = class _Heap extends IterableElementBase {
943
1003
 
944
1004
 
945
1005
 
1006
+
1007
+
1008
+
1009
+
1010
+
1011
+
1012
+ * @example
1013
+ * // Heap with custom comparator (MaxHeap behavior)
1014
+ * interface Task {
1015
+ * id: number;
1016
+ * priority: number;
1017
+ * name: string;
1018
+ * }
1019
+ *
1020
+ * // Custom comparator for max heap behavior (higher priority first)
1021
+ * const tasks: Task[] = [
1022
+ * { id: 1, priority: 5, name: 'Email' },
1023
+ * { id: 2, priority: 3, name: 'Chat' },
1024
+ * { id: 3, priority: 8, name: 'Alert' }
1025
+ * ];
1026
+ *
1027
+ * const maxHeap = new Heap(tasks, {
1028
+ * comparator: (a: Task, b: Task) => b.priority - a.priority
1029
+ * });
1030
+ *
1031
+ * console.log(maxHeap.size); // 3;
1032
+ *
1033
+ * // Peek returns highest priority task
1034
+ * const topTask = maxHeap.peek();
1035
+ * console.log(topTask?.priority); // 8;
1036
+ * console.log(topTask?.name); // 'Alert';
1037
+ */
1038
+ /**
1039
+ * @deprecated Use `pop` instead. Will be removed in a future major version.
946
1040
 
947
1041
 
948
1042
 
@@ -973,6 +1067,14 @@ var Heap = class _Heap extends IterableElementBase {
973
1067
  * console.log(topTask?.name); // 'Alert';
974
1068
  */
975
1069
  poll() {
1070
+ return this.pop();
1071
+ }
1072
+ /**
1073
+ * Remove and return the top element (min or max depending on comparator).
1074
+ * @remarks Time O(log N) amortized, Space O(1)
1075
+ * @returns The removed top element, or undefined if empty.
1076
+ */
1077
+ pop() {
976
1078
  if (this.elements.length === 0) return;
977
1079
  const value = this.elements[0];
978
1080
  const last = this.elements.pop();
@@ -1013,6 +1115,13 @@ var Heap = class _Heap extends IterableElementBase {
1013
1115
 
1014
1116
 
1015
1117
 
1118
+
1119
+
1120
+
1121
+
1122
+
1123
+
1124
+
1016
1125
 
1017
1126
 
1018
1127
 
@@ -1113,6 +1222,13 @@ var Heap = class _Heap extends IterableElementBase {
1113
1222
 
1114
1223
 
1115
1224
 
1225
+
1226
+
1227
+
1228
+
1229
+
1230
+
1231
+
1116
1232
 
1117
1233
 
1118
1234
 
@@ -1160,6 +1276,13 @@ var Heap = class _Heap extends IterableElementBase {
1160
1276
 
1161
1277
 
1162
1278
 
1279
+
1280
+
1281
+
1282
+
1283
+
1284
+
1285
+
1163
1286
 
1164
1287
 
1165
1288
 
@@ -1177,16 +1300,6 @@ var Heap = class _Heap extends IterableElementBase {
1177
1300
  clear() {
1178
1301
  this._elements = [];
1179
1302
  }
1180
- /**
1181
- * Replace the backing array and rebuild the heap.
1182
- * @remarks Time O(N), Space O(N)
1183
- * @param elements - Iterable used to refill the heap.
1184
- * @returns Array of per-node results from fixing steps.
1185
- */
1186
- refill(elements) {
1187
- this._elements = Array.from(elements);
1188
- return this.fix();
1189
- }
1190
1303
  /**
1191
1304
  * Check if an equal element exists in the heap.
1192
1305
  * @remarks Time O(N), Space O(1)
@@ -1210,6 +1323,13 @@ var Heap = class _Heap extends IterableElementBase {
1210
1323
 
1211
1324
 
1212
1325
 
1326
+
1327
+
1328
+
1329
+
1330
+
1331
+
1332
+
1213
1333
 
1214
1334
 
1215
1335
 
@@ -1257,6 +1377,13 @@ var Heap = class _Heap extends IterableElementBase {
1257
1377
 
1258
1378
 
1259
1379
 
1380
+
1381
+
1382
+
1383
+
1384
+
1385
+
1386
+
1260
1387
 
1261
1388
 
1262
1389
 
@@ -1281,7 +1408,7 @@ var Heap = class _Heap extends IterableElementBase {
1281
1408
  }
1282
1409
  if (index < 0) return false;
1283
1410
  if (index === 0) {
1284
- this.poll();
1411
+ this.pop();
1285
1412
  } else if (index === this.elements.length - 1) {
1286
1413
  this.elements.pop();
1287
1414
  } else {
@@ -1291,13 +1418,19 @@ var Heap = class _Heap extends IterableElementBase {
1291
1418
  }
1292
1419
  return true;
1293
1420
  }
1421
+ /**
1422
+ * @deprecated Use `deleteWhere` instead. Will be removed in a future major version.
1423
+ */
1424
+ deleteBy(predicate) {
1425
+ return this.deleteWhere(predicate);
1426
+ }
1294
1427
  /**
1295
1428
  * Delete the first element that matches a predicate.
1296
1429
  * @remarks Time O(N), Space O(1)
1297
1430
  * @param predicate - Function (element, index, heap) → boolean.
1298
1431
  * @returns True if an element was removed.
1299
1432
  */
1300
- deleteBy(predicate) {
1433
+ deleteWhere(predicate) {
1301
1434
  let idx = -1;
1302
1435
  for (let i = 0; i < this.elements.length; i++) {
1303
1436
  if (predicate(this.elements[i], i, this)) {
@@ -1307,7 +1440,7 @@ var Heap = class _Heap extends IterableElementBase {
1307
1440
  }
1308
1441
  if (idx < 0) return false;
1309
1442
  if (idx === 0) {
1310
- this.poll();
1443
+ this.pop();
1311
1444
  } else if (idx === this.elements.length - 1) {
1312
1445
  this.elements.pop();
1313
1446
  } else {
@@ -1350,6 +1483,13 @@ var Heap = class _Heap extends IterableElementBase {
1350
1483
 
1351
1484
 
1352
1485
 
1486
+
1487
+
1488
+
1489
+
1490
+
1491
+
1492
+
1353
1493
 
1354
1494
 
1355
1495
 
@@ -1430,6 +1570,13 @@ var Heap = class _Heap extends IterableElementBase {
1430
1570
 
1431
1571
 
1432
1572
 
1573
+
1574
+
1575
+
1576
+
1577
+
1578
+
1579
+
1433
1580
 
1434
1581
 
1435
1582
 
@@ -1483,6 +1630,13 @@ var Heap = class _Heap extends IterableElementBase {
1483
1630
 
1484
1631
 
1485
1632
 
1633
+
1634
+
1635
+
1636
+
1637
+
1638
+
1639
+
1486
1640
 
1487
1641
 
1488
1642
 
@@ -1535,6 +1689,13 @@ var Heap = class _Heap extends IterableElementBase {
1535
1689
 
1536
1690
 
1537
1691
 
1692
+
1693
+
1694
+
1695
+
1696
+
1697
+
1698
+
1538
1699
 
1539
1700
 
1540
1701
 
@@ -1594,6 +1755,13 @@ var Heap = class _Heap extends IterableElementBase {
1594
1755
 
1595
1756
 
1596
1757
 
1758
+
1759
+
1760
+
1761
+
1762
+
1763
+
1764
+
1597
1765
 
1598
1766
 
1599
1767
 
@@ -1807,6 +1975,13 @@ var Queue = class _Queue extends LinearBase {
1807
1975
 
1808
1976
 
1809
1977
 
1978
+
1979
+
1980
+
1981
+
1982
+
1983
+
1984
+
1810
1985
 
1811
1986
 
1812
1987
 
@@ -1857,6 +2032,13 @@ var Queue = class _Queue extends LinearBase {
1857
2032
 
1858
2033
 
1859
2034
 
2035
+
2036
+
2037
+
2038
+
2039
+
2040
+
2041
+
1860
2042
 
1861
2043
 
1862
2044
 
@@ -1874,6 +2056,14 @@ var Queue = class _Queue extends LinearBase {
1874
2056
  get first() {
1875
2057
  return this.length > 0 ? this.elements[this._offset] : void 0;
1876
2058
  }
2059
+ /**
2060
+ * Peek at the front element without removing it (alias for `first`).
2061
+ * @remarks Time O(1), Space O(1)
2062
+ * @returns Front element or undefined.
2063
+ */
2064
+ peek() {
2065
+ return this.first;
2066
+ }
1877
2067
  /**
1878
2068
  * Get the last element (back) without removing it.
1879
2069
  * @remarks Time O(1), Space O(1)
@@ -1923,6 +2113,13 @@ var Queue = class _Queue extends LinearBase {
1923
2113
 
1924
2114
 
1925
2115
 
2116
+
2117
+
2118
+
2119
+
2120
+
2121
+
2122
+
1926
2123
 
1927
2124
 
1928
2125
 
@@ -1985,6 +2182,13 @@ var Queue = class _Queue extends LinearBase {
1985
2182
 
1986
2183
 
1987
2184
 
2185
+
2186
+
2187
+
2188
+
2189
+
2190
+
2191
+
1988
2192
 
1989
2193
 
1990
2194
 
@@ -2054,6 +2258,13 @@ var Queue = class _Queue extends LinearBase {
2054
2258
 
2055
2259
 
2056
2260
 
2261
+
2262
+
2263
+
2264
+
2265
+
2266
+
2267
+
2057
2268
 
2058
2269
 
2059
2270
 
@@ -2113,6 +2324,13 @@ var Queue = class _Queue extends LinearBase {
2113
2324
 
2114
2325
 
2115
2326
 
2327
+
2328
+
2329
+
2330
+
2331
+
2332
+
2333
+
2116
2334
 
2117
2335
 
2118
2336
 
@@ -2165,6 +2383,13 @@ var Queue = class _Queue extends LinearBase {
2165
2383
 
2166
2384
 
2167
2385
 
2386
+
2387
+
2388
+
2389
+
2390
+
2391
+
2392
+
2168
2393
 
2169
2394
 
2170
2395
 
@@ -2219,6 +2444,21 @@ var Queue = class _Queue extends LinearBase {
2219
2444
  this._elements[this._offset + index] = newElement;
2220
2445
  return true;
2221
2446
  }
2447
+ /**
2448
+ * Delete the first element that satisfies a predicate.
2449
+ * @remarks Time O(N), Space O(N)
2450
+ * @param predicate - Function (value, index, queue) → boolean to decide deletion.
2451
+ * @returns True if a match was removed.
2452
+ */
2453
+ deleteWhere(predicate) {
2454
+ for (let i = 0; i < this.length; i++) {
2455
+ if (predicate(this._elements[this._offset + i], i, this)) {
2456
+ this.deleteAt(i);
2457
+ return true;
2458
+ }
2459
+ }
2460
+ return false;
2461
+ }
2222
2462
  /**
2223
2463
  * Reverse the queue in-place by compacting then reversing.
2224
2464
  * @remarks Time O(N), Space O(N)
@@ -2258,6 +2498,13 @@ var Queue = class _Queue extends LinearBase {
2258
2498
 
2259
2499
 
2260
2500
 
2501
+
2502
+
2503
+
2504
+
2505
+
2506
+
2507
+
2261
2508
 
2262
2509
 
2263
2510
 
@@ -2304,6 +2551,13 @@ var Queue = class _Queue extends LinearBase {
2304
2551
 
2305
2552
 
2306
2553
 
2554
+
2555
+
2556
+
2557
+
2558
+
2559
+
2560
+
2307
2561
 
2308
2562
 
2309
2563
 
@@ -2373,6 +2627,13 @@ var Queue = class _Queue extends LinearBase {
2373
2627
 
2374
2628
 
2375
2629
 
2630
+
2631
+
2632
+
2633
+
2634
+
2635
+
2636
+
2376
2637
 
2377
2638
 
2378
2639
 
@@ -2426,6 +2687,13 @@ var Queue = class _Queue extends LinearBase {
2426
2687
 
2427
2688
 
2428
2689
 
2690
+
2691
+
2692
+
2693
+
2694
+
2695
+
2696
+
2429
2697
 
2430
2698
 
2431
2699
 
@@ -2483,6 +2751,13 @@ var Queue = class _Queue extends LinearBase {
2483
2751
 
2484
2752
 
2485
2753
 
2754
+
2755
+
2756
+
2757
+
2758
+
2759
+
2760
+
2486
2761
 
2487
2762
 
2488
2763
 
@@ -3611,6 +3886,13 @@ var UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
3611
3886
 
3612
3887
 
3613
3888
 
3889
+
3890
+
3891
+
3892
+
3893
+
3894
+
3895
+
3614
3896
 
3615
3897
 
3616
3898
 
@@ -3695,6 +3977,13 @@ var UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
3695
3977
 
3696
3978
 
3697
3979
 
3980
+
3981
+
3982
+
3983
+
3984
+
3985
+
3986
+
3698
3987
 
3699
3988
 
3700
3989
 
@@ -3779,6 +4068,13 @@ var UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
3779
4068
 
3780
4069
 
3781
4070
 
4071
+
4072
+
4073
+
4074
+
4075
+
4076
+
4077
+
3782
4078
 
3783
4079
 
3784
4080
 
@@ -3877,6 +4173,13 @@ var UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
3877
4173
 
3878
4174
 
3879
4175
 
4176
+
4177
+
4178
+
4179
+
4180
+
4181
+
4182
+
3880
4183
 
3881
4184
 
3882
4185
 
@@ -3931,6 +4234,13 @@ var UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
3931
4234
 
3932
4235
 
3933
4236
 
4237
+
4238
+
4239
+
4240
+
4241
+
4242
+
4243
+
3934
4244
 
3935
4245
 
3936
4246
 
@@ -4055,6 +4365,13 @@ var UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
4055
4365
 
4056
4366
 
4057
4367
 
4368
+
4369
+
4370
+
4371
+
4372
+
4373
+
4374
+
4058
4375
 
4059
4376
 
4060
4377
 
@@ -4201,6 +4518,13 @@ var UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
4201
4518
 
4202
4519
 
4203
4520
 
4521
+
4522
+
4523
+
4524
+
4525
+
4526
+
4527
+
4204
4528
 
4205
4529
 
4206
4530
 
@@ -4269,6 +4593,13 @@ var UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
4269
4593
 
4270
4594
 
4271
4595
 
4596
+
4597
+
4598
+
4599
+
4600
+
4601
+
4602
+
4272
4603
 
4273
4604
 
4274
4605
 
@@ -4319,6 +4650,13 @@ var UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
4319
4650
 
4320
4651
 
4321
4652
 
4653
+
4654
+
4655
+
4656
+
4657
+
4658
+
4659
+
4322
4660
 
4323
4661
 
4324
4662