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
@@ -413,6 +413,35 @@ var IterableElementBase = class {
413
413
  for (const ele of this) if (ele === element) return true;
414
414
  return false;
415
415
  }
416
+ /**
417
+ * Check whether a value exists (Array-compatible alias for `has`).
418
+ * @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1).
419
+ * @param element - Element to search for (uses `===`).
420
+ * @returns `true` if found.
421
+ */
422
+ includes(element) {
423
+ return this.has(element);
424
+ }
425
+ /**
426
+ * Return an iterator of `[index, value]` pairs (Array-compatible).
427
+ * @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1) per step.
428
+ */
429
+ *entries() {
430
+ let index = 0;
431
+ for (const value of this) {
432
+ yield [index++, value];
433
+ }
434
+ }
435
+ /**
436
+ * Return an iterator of numeric indices (Array-compatible).
437
+ * @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1) per step.
438
+ */
439
+ *keys() {
440
+ let index = 0;
441
+ for (const _ of this) {
442
+ yield index++;
443
+ }
444
+ }
416
445
  /**
417
446
  * Reduces all elements to a single accumulated value.
418
447
  *
@@ -675,6 +704,16 @@ var LinearBase = class _LinearBase extends IterableElementBase {
675
704
  }
676
705
  return this;
677
706
  }
707
+ /**
708
+ * Return a new instance of the same type with elements in reverse order (non-mutating).
709
+ * @remarks Provided for familiarity when migrating from Array (ES2023 `toReversed`). Time O(n), Space O(n).
710
+ * @returns A new reversed instance.
711
+ */
712
+ toReversed() {
713
+ const cloned = this.clone();
714
+ cloned.reverse();
715
+ return cloned;
716
+ }
678
717
  };
679
718
 
680
719
  // src/data-structures/heap/heap.ts
@@ -738,6 +777,13 @@ var Heap = class _Heap extends IterableElementBase {
738
777
 
739
778
 
740
779
 
780
+
781
+
782
+
783
+
784
+
785
+
786
+
741
787
 
742
788
 
743
789
 
@@ -794,7 +840,7 @@ var Heap = class _Heap extends IterableElementBase {
794
840
  }
795
841
  /**
796
842
  * Insert an element.
797
- * @remarks Time O(1) amortized, Space O(1)
843
+ * @remarks Time O(log N) amortized, Space O(1)
798
844
  * @param element - Element to insert.
799
845
  * @returns True.
800
846
 
@@ -824,6 +870,13 @@ var Heap = class _Heap extends IterableElementBase {
824
870
 
825
871
 
826
872
 
873
+
874
+
875
+
876
+
877
+
878
+
879
+
827
880
 
828
881
 
829
882
 
@@ -881,6 +934,13 @@ var Heap = class _Heap extends IterableElementBase {
881
934
 
882
935
 
883
936
 
937
+
938
+
939
+
940
+
941
+
942
+
943
+
884
944
 
885
945
 
886
946
 
@@ -945,6 +1005,40 @@ var Heap = class _Heap extends IterableElementBase {
945
1005
 
946
1006
 
947
1007
 
1008
+
1009
+
1010
+
1011
+
1012
+
1013
+
1014
+ * @example
1015
+ * // Heap with custom comparator (MaxHeap behavior)
1016
+ * interface Task {
1017
+ * id: number;
1018
+ * priority: number;
1019
+ * name: string;
1020
+ * }
1021
+ *
1022
+ * // Custom comparator for max heap behavior (higher priority first)
1023
+ * const tasks: Task[] = [
1024
+ * { id: 1, priority: 5, name: 'Email' },
1025
+ * { id: 2, priority: 3, name: 'Chat' },
1026
+ * { id: 3, priority: 8, name: 'Alert' }
1027
+ * ];
1028
+ *
1029
+ * const maxHeap = new Heap(tasks, {
1030
+ * comparator: (a: Task, b: Task) => b.priority - a.priority
1031
+ * });
1032
+ *
1033
+ * console.log(maxHeap.size); // 3;
1034
+ *
1035
+ * // Peek returns highest priority task
1036
+ * const topTask = maxHeap.peek();
1037
+ * console.log(topTask?.priority); // 8;
1038
+ * console.log(topTask?.name); // 'Alert';
1039
+ */
1040
+ /**
1041
+ * @deprecated Use `pop` instead. Will be removed in a future major version.
948
1042
 
949
1043
 
950
1044
 
@@ -975,6 +1069,14 @@ var Heap = class _Heap extends IterableElementBase {
975
1069
  * console.log(topTask?.name); // 'Alert';
976
1070
  */
977
1071
  poll() {
1072
+ return this.pop();
1073
+ }
1074
+ /**
1075
+ * Remove and return the top element (min or max depending on comparator).
1076
+ * @remarks Time O(log N) amortized, Space O(1)
1077
+ * @returns The removed top element, or undefined if empty.
1078
+ */
1079
+ pop() {
978
1080
  if (this.elements.length === 0) return;
979
1081
  const value = this.elements[0];
980
1082
  const last = this.elements.pop();
@@ -1015,6 +1117,13 @@ var Heap = class _Heap extends IterableElementBase {
1015
1117
 
1016
1118
 
1017
1119
 
1120
+
1121
+
1122
+
1123
+
1124
+
1125
+
1126
+
1018
1127
 
1019
1128
 
1020
1129
 
@@ -1115,6 +1224,13 @@ var Heap = class _Heap extends IterableElementBase {
1115
1224
 
1116
1225
 
1117
1226
 
1227
+
1228
+
1229
+
1230
+
1231
+
1232
+
1233
+
1118
1234
 
1119
1235
 
1120
1236
 
@@ -1162,6 +1278,13 @@ var Heap = class _Heap extends IterableElementBase {
1162
1278
 
1163
1279
 
1164
1280
 
1281
+
1282
+
1283
+
1284
+
1285
+
1286
+
1287
+
1165
1288
 
1166
1289
 
1167
1290
 
@@ -1179,16 +1302,6 @@ var Heap = class _Heap extends IterableElementBase {
1179
1302
  clear() {
1180
1303
  this._elements = [];
1181
1304
  }
1182
- /**
1183
- * Replace the backing array and rebuild the heap.
1184
- * @remarks Time O(N), Space O(N)
1185
- * @param elements - Iterable used to refill the heap.
1186
- * @returns Array of per-node results from fixing steps.
1187
- */
1188
- refill(elements) {
1189
- this._elements = Array.from(elements);
1190
- return this.fix();
1191
- }
1192
1305
  /**
1193
1306
  * Check if an equal element exists in the heap.
1194
1307
  * @remarks Time O(N), Space O(1)
@@ -1212,6 +1325,13 @@ var Heap = class _Heap extends IterableElementBase {
1212
1325
 
1213
1326
 
1214
1327
 
1328
+
1329
+
1330
+
1331
+
1332
+
1333
+
1334
+
1215
1335
 
1216
1336
 
1217
1337
 
@@ -1259,6 +1379,13 @@ var Heap = class _Heap extends IterableElementBase {
1259
1379
 
1260
1380
 
1261
1381
 
1382
+
1383
+
1384
+
1385
+
1386
+
1387
+
1388
+
1262
1389
 
1263
1390
 
1264
1391
 
@@ -1283,7 +1410,7 @@ var Heap = class _Heap extends IterableElementBase {
1283
1410
  }
1284
1411
  if (index < 0) return false;
1285
1412
  if (index === 0) {
1286
- this.poll();
1413
+ this.pop();
1287
1414
  } else if (index === this.elements.length - 1) {
1288
1415
  this.elements.pop();
1289
1416
  } else {
@@ -1293,13 +1420,19 @@ var Heap = class _Heap extends IterableElementBase {
1293
1420
  }
1294
1421
  return true;
1295
1422
  }
1423
+ /**
1424
+ * @deprecated Use `deleteWhere` instead. Will be removed in a future major version.
1425
+ */
1426
+ deleteBy(predicate) {
1427
+ return this.deleteWhere(predicate);
1428
+ }
1296
1429
  /**
1297
1430
  * Delete the first element that matches a predicate.
1298
1431
  * @remarks Time O(N), Space O(1)
1299
1432
  * @param predicate - Function (element, index, heap) → boolean.
1300
1433
  * @returns True if an element was removed.
1301
1434
  */
1302
- deleteBy(predicate) {
1435
+ deleteWhere(predicate) {
1303
1436
  let idx = -1;
1304
1437
  for (let i = 0; i < this.elements.length; i++) {
1305
1438
  if (predicate(this.elements[i], i, this)) {
@@ -1309,7 +1442,7 @@ var Heap = class _Heap extends IterableElementBase {
1309
1442
  }
1310
1443
  if (idx < 0) return false;
1311
1444
  if (idx === 0) {
1312
- this.poll();
1445
+ this.pop();
1313
1446
  } else if (idx === this.elements.length - 1) {
1314
1447
  this.elements.pop();
1315
1448
  } else {
@@ -1352,6 +1485,13 @@ var Heap = class _Heap extends IterableElementBase {
1352
1485
 
1353
1486
 
1354
1487
 
1488
+
1489
+
1490
+
1491
+
1492
+
1493
+
1494
+
1355
1495
 
1356
1496
 
1357
1497
 
@@ -1432,6 +1572,13 @@ var Heap = class _Heap extends IterableElementBase {
1432
1572
 
1433
1573
 
1434
1574
 
1575
+
1576
+
1577
+
1578
+
1579
+
1580
+
1581
+
1435
1582
 
1436
1583
 
1437
1584
 
@@ -1485,6 +1632,13 @@ var Heap = class _Heap extends IterableElementBase {
1485
1632
 
1486
1633
 
1487
1634
 
1635
+
1636
+
1637
+
1638
+
1639
+
1640
+
1641
+
1488
1642
 
1489
1643
 
1490
1644
 
@@ -1537,6 +1691,13 @@ var Heap = class _Heap extends IterableElementBase {
1537
1691
 
1538
1692
 
1539
1693
 
1694
+
1695
+
1696
+
1697
+
1698
+
1699
+
1700
+
1540
1701
 
1541
1702
 
1542
1703
 
@@ -1596,6 +1757,13 @@ var Heap = class _Heap extends IterableElementBase {
1596
1757
 
1597
1758
 
1598
1759
 
1760
+
1761
+
1762
+
1763
+
1764
+
1765
+
1766
+
1599
1767
 
1600
1768
 
1601
1769
 
@@ -1809,6 +1977,13 @@ var Queue = class _Queue extends LinearBase {
1809
1977
 
1810
1978
 
1811
1979
 
1980
+
1981
+
1982
+
1983
+
1984
+
1985
+
1986
+
1812
1987
 
1813
1988
 
1814
1989
 
@@ -1859,6 +2034,13 @@ var Queue = class _Queue extends LinearBase {
1859
2034
 
1860
2035
 
1861
2036
 
2037
+
2038
+
2039
+
2040
+
2041
+
2042
+
2043
+
1862
2044
 
1863
2045
 
1864
2046
 
@@ -1876,6 +2058,14 @@ var Queue = class _Queue extends LinearBase {
1876
2058
  get first() {
1877
2059
  return this.length > 0 ? this.elements[this._offset] : void 0;
1878
2060
  }
2061
+ /**
2062
+ * Peek at the front element without removing it (alias for `first`).
2063
+ * @remarks Time O(1), Space O(1)
2064
+ * @returns Front element or undefined.
2065
+ */
2066
+ peek() {
2067
+ return this.first;
2068
+ }
1879
2069
  /**
1880
2070
  * Get the last element (back) without removing it.
1881
2071
  * @remarks Time O(1), Space O(1)
@@ -1925,6 +2115,13 @@ var Queue = class _Queue extends LinearBase {
1925
2115
 
1926
2116
 
1927
2117
 
2118
+
2119
+
2120
+
2121
+
2122
+
2123
+
2124
+
1928
2125
 
1929
2126
 
1930
2127
 
@@ -1987,6 +2184,13 @@ var Queue = class _Queue extends LinearBase {
1987
2184
 
1988
2185
 
1989
2186
 
2187
+
2188
+
2189
+
2190
+
2191
+
2192
+
2193
+
1990
2194
 
1991
2195
 
1992
2196
 
@@ -2056,6 +2260,13 @@ var Queue = class _Queue extends LinearBase {
2056
2260
 
2057
2261
 
2058
2262
 
2263
+
2264
+
2265
+
2266
+
2267
+
2268
+
2269
+
2059
2270
 
2060
2271
 
2061
2272
 
@@ -2115,6 +2326,13 @@ var Queue = class _Queue extends LinearBase {
2115
2326
 
2116
2327
 
2117
2328
 
2329
+
2330
+
2331
+
2332
+
2333
+
2334
+
2335
+
2118
2336
 
2119
2337
 
2120
2338
 
@@ -2167,6 +2385,13 @@ var Queue = class _Queue extends LinearBase {
2167
2385
 
2168
2386
 
2169
2387
 
2388
+
2389
+
2390
+
2391
+
2392
+
2393
+
2394
+
2170
2395
 
2171
2396
 
2172
2397
 
@@ -2221,6 +2446,21 @@ var Queue = class _Queue extends LinearBase {
2221
2446
  this._elements[this._offset + index] = newElement;
2222
2447
  return true;
2223
2448
  }
2449
+ /**
2450
+ * Delete the first element that satisfies a predicate.
2451
+ * @remarks Time O(N), Space O(N)
2452
+ * @param predicate - Function (value, index, queue) → boolean to decide deletion.
2453
+ * @returns True if a match was removed.
2454
+ */
2455
+ deleteWhere(predicate) {
2456
+ for (let i = 0; i < this.length; i++) {
2457
+ if (predicate(this._elements[this._offset + i], i, this)) {
2458
+ this.deleteAt(i);
2459
+ return true;
2460
+ }
2461
+ }
2462
+ return false;
2463
+ }
2224
2464
  /**
2225
2465
  * Reverse the queue in-place by compacting then reversing.
2226
2466
  * @remarks Time O(N), Space O(N)
@@ -2260,6 +2500,13 @@ var Queue = class _Queue extends LinearBase {
2260
2500
 
2261
2501
 
2262
2502
 
2503
+
2504
+
2505
+
2506
+
2507
+
2508
+
2509
+
2263
2510
 
2264
2511
 
2265
2512
 
@@ -2306,6 +2553,13 @@ var Queue = class _Queue extends LinearBase {
2306
2553
 
2307
2554
 
2308
2555
 
2556
+
2557
+
2558
+
2559
+
2560
+
2561
+
2562
+
2309
2563
 
2310
2564
 
2311
2565
 
@@ -2375,6 +2629,13 @@ var Queue = class _Queue extends LinearBase {
2375
2629
 
2376
2630
 
2377
2631
 
2632
+
2633
+
2634
+
2635
+
2636
+
2637
+
2638
+
2378
2639
 
2379
2640
 
2380
2641
 
@@ -2428,6 +2689,13 @@ var Queue = class _Queue extends LinearBase {
2428
2689
 
2429
2690
 
2430
2691
 
2692
+
2693
+
2694
+
2695
+
2696
+
2697
+
2698
+
2431
2699
 
2432
2700
 
2433
2701
 
@@ -2485,6 +2753,13 @@ var Queue = class _Queue extends LinearBase {
2485
2753
 
2486
2754
 
2487
2755
 
2756
+
2757
+
2758
+
2759
+
2760
+
2761
+
2762
+
2488
2763
 
2489
2764
 
2490
2765
 
@@ -3613,6 +3888,13 @@ var UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
3613
3888
 
3614
3889
 
3615
3890
 
3891
+
3892
+
3893
+
3894
+
3895
+
3896
+
3897
+
3616
3898
 
3617
3899
 
3618
3900
 
@@ -3697,6 +3979,13 @@ var UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
3697
3979
 
3698
3980
 
3699
3981
 
3982
+
3983
+
3984
+
3985
+
3986
+
3987
+
3988
+
3700
3989
 
3701
3990
 
3702
3991
 
@@ -3781,6 +4070,13 @@ var UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
3781
4070
 
3782
4071
 
3783
4072
 
4073
+
4074
+
4075
+
4076
+
4077
+
4078
+
4079
+
3784
4080
 
3785
4081
 
3786
4082
 
@@ -3879,6 +4175,13 @@ var UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
3879
4175
 
3880
4176
 
3881
4177
 
4178
+
4179
+
4180
+
4181
+
4182
+
4183
+
4184
+
3882
4185
 
3883
4186
 
3884
4187
 
@@ -3933,6 +4236,13 @@ var UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
3933
4236
 
3934
4237
 
3935
4238
 
4239
+
4240
+
4241
+
4242
+
4243
+
4244
+
4245
+
3936
4246
 
3937
4247
 
3938
4248
 
@@ -4057,6 +4367,13 @@ var UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
4057
4367
 
4058
4368
 
4059
4369
 
4370
+
4371
+
4372
+
4373
+
4374
+
4375
+
4376
+
4060
4377
 
4061
4378
 
4062
4379
 
@@ -4203,6 +4520,13 @@ var UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
4203
4520
 
4204
4521
 
4205
4522
 
4523
+
4524
+
4525
+
4526
+
4527
+
4528
+
4529
+
4206
4530
 
4207
4531
 
4208
4532
 
@@ -4271,6 +4595,13 @@ var UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
4271
4595
 
4272
4596
 
4273
4597
 
4598
+
4599
+
4600
+
4601
+
4602
+
4603
+
4604
+
4274
4605
 
4275
4606
 
4276
4607
 
@@ -4321,6 +4652,13 @@ var UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
4321
4652
 
4322
4653
 
4323
4654
 
4655
+
4656
+
4657
+
4658
+
4659
+
4660
+
4661
+
4324
4662
 
4325
4663
 
4326
4664