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 +422 -14
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +422 -14
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +422 -14
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +422 -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/graph-typed.js +422 -14
  35. package/dist/umd/graph-typed.js.map +1 -1
  36. package/dist/umd/graph-typed.min.js +2 -2
  37. package/dist/umd/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
@@ -12,7 +12,7 @@ import { ERR, raise } from '../../common';
12
12
 
13
13
  /**
14
14
  * Binary heap with pluggable comparator; supports fast insertion and removal of the top element.
15
- * @remarks Time O(1), Space O(1)
15
+ * @remarks Typical operations: O(log N) insert/remove, O(1) peek. Space O(N).
16
16
  * @template E
17
17
  * @template R
18
18
  * 1. Complete Binary Tree: Heaps are typically complete binary trees, meaning every level is fully filled except possibly for the last level, which has nodes as far left as possible.
@@ -212,6 +212,13 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
212
212
 
213
213
 
214
214
 
215
+
216
+
217
+
218
+
219
+
220
+
221
+
215
222
 
216
223
 
217
224
 
@@ -280,7 +287,7 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
280
287
 
281
288
  /**
282
289
  * Insert an element.
283
- * @remarks Time O(1) amortized, Space O(1)
290
+ * @remarks Time O(log N) amortized, Space O(1)
284
291
  * @param element - Element to insert.
285
292
  * @returns True.
286
293
 
@@ -310,6 +317,13 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
310
317
 
311
318
 
312
319
 
320
+
321
+
322
+
323
+
324
+
325
+
326
+
313
327
 
314
328
 
315
329
 
@@ -369,6 +383,13 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
369
383
 
370
384
 
371
385
 
386
+
387
+
388
+
389
+
390
+
391
+
392
+
372
393
 
373
394
 
374
395
 
@@ -434,6 +455,9 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
434
455
 
435
456
 
436
457
 
458
+
459
+
460
+
437
461
 
438
462
 
439
463
 
@@ -465,7 +489,47 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
465
489
  * console.log(topTask?.name); // 'Alert';
466
490
  */
467
491
 
492
+ /**
493
+ * @deprecated Use `pop` instead. Will be removed in a future major version.
494
+
495
+
496
+
497
+ * @example
498
+ * // Heap with custom comparator (MaxHeap behavior)
499
+ * interface Task {
500
+ * id: number;
501
+ * priority: number;
502
+ * name: string;
503
+ * }
504
+ *
505
+ * // Custom comparator for max heap behavior (higher priority first)
506
+ * const tasks: Task[] = [
507
+ * { id: 1, priority: 5, name: 'Email' },
508
+ * { id: 2, priority: 3, name: 'Chat' },
509
+ * { id: 3, priority: 8, name: 'Alert' }
510
+ * ];
511
+ *
512
+ * const maxHeap = new Heap(tasks, {
513
+ * comparator: (a: Task, b: Task) => b.priority - a.priority
514
+ * });
515
+ *
516
+ * console.log(maxHeap.size); // 3;
517
+ *
518
+ * // Peek returns highest priority task
519
+ * const topTask = maxHeap.peek();
520
+ * console.log(topTask?.priority); // 8;
521
+ * console.log(topTask?.name); // 'Alert';
522
+ */
468
523
  poll(): E | undefined {
524
+ return this.pop();
525
+ }
526
+
527
+ /**
528
+ * Remove and return the top element (min or max depending on comparator).
529
+ * @remarks Time O(log N) amortized, Space O(1)
530
+ * @returns The removed top element, or undefined if empty.
531
+ */
532
+ pop(): E | undefined {
469
533
  if (this.elements.length === 0) return;
470
534
  const value = this.elements[0];
471
535
  const last = this.elements.pop()!;
@@ -507,6 +571,13 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
507
571
 
508
572
 
509
573
 
574
+
575
+
576
+
577
+
578
+
579
+
580
+
510
581
 
511
582
 
512
583
 
@@ -609,6 +680,13 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
609
680
 
610
681
 
611
682
 
683
+
684
+
685
+
686
+
687
+
688
+
689
+
612
690
 
613
691
 
614
692
 
@@ -658,6 +736,13 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
658
736
 
659
737
 
660
738
 
739
+
740
+
741
+
742
+
743
+
744
+
745
+
661
746
 
662
747
 
663
748
 
@@ -677,17 +762,7 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
677
762
  this._elements = [];
678
763
  }
679
764
 
680
- /**
681
- * Replace the backing array and rebuild the heap.
682
- * @remarks Time O(N), Space O(N)
683
- * @param elements - Iterable used to refill the heap.
684
- * @returns Array of per-node results from fixing steps.
685
- */
686
765
 
687
- refill(elements: Iterable<E>): boolean[] {
688
- this._elements = Array.from(elements);
689
- return this.fix();
690
- }
691
766
 
692
767
  /**
693
768
  * Check if an equal element exists in the heap.
@@ -712,6 +787,13 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
712
787
 
713
788
 
714
789
 
790
+
791
+
792
+
793
+
794
+
795
+
796
+
715
797
 
716
798
 
717
799
 
@@ -761,6 +843,13 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
761
843
 
762
844
 
763
845
 
846
+
847
+
848
+
849
+
850
+
851
+
852
+
764
853
 
765
854
 
766
855
 
@@ -786,7 +875,7 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
786
875
  }
787
876
  if (index < 0) return false;
788
877
  if (index === 0) {
789
- this.poll();
878
+ this.pop();
790
879
  } else if (index === this.elements.length - 1) {
791
880
  this.elements.pop();
792
881
  } else {
@@ -797,14 +886,20 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
797
886
  return true;
798
887
  }
799
888
 
889
+ /**
890
+ * @deprecated Use `deleteWhere` instead. Will be removed in a future major version.
891
+ */
892
+ deleteBy(predicate: (element: E, index: number, heap: this) => boolean): boolean {
893
+ return this.deleteWhere(predicate);
894
+ }
895
+
800
896
  /**
801
897
  * Delete the first element that matches a predicate.
802
898
  * @remarks Time O(N), Space O(1)
803
899
  * @param predicate - Function (element, index, heap) → boolean.
804
900
  * @returns True if an element was removed.
805
901
  */
806
-
807
- deleteBy(predicate: (element: E, index: number, heap: this) => boolean): boolean {
902
+ deleteWhere(predicate: (element: E, index: number, heap: this) => boolean): boolean {
808
903
  let idx = -1;
809
904
  for (let i = 0; i < this.elements.length; i++) {
810
905
  if (predicate(this.elements[i], i, this)) {
@@ -814,7 +909,7 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
814
909
  }
815
910
  if (idx < 0) return false;
816
911
  if (idx === 0) {
817
- this.poll();
912
+ this.pop();
818
913
  } else if (idx === this.elements.length - 1) {
819
914
  this.elements.pop();
820
915
  } else {
@@ -860,6 +955,13 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
860
955
 
861
956
 
862
957
 
958
+
959
+
960
+
961
+
962
+
963
+
964
+
863
965
 
864
966
 
865
967
 
@@ -945,6 +1047,13 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
945
1047
 
946
1048
 
947
1049
 
1050
+
1051
+
1052
+
1053
+
1054
+
1055
+
1056
+
948
1057
 
949
1058
 
950
1059
 
@@ -1000,6 +1109,13 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
1000
1109
 
1001
1110
 
1002
1111
 
1112
+
1113
+
1114
+
1115
+
1116
+
1117
+
1118
+
1003
1119
 
1004
1120
 
1005
1121
 
@@ -1054,6 +1170,13 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
1054
1170
 
1055
1171
 
1056
1172
 
1173
+
1174
+
1175
+
1176
+
1177
+
1178
+
1179
+
1057
1180
 
1058
1181
 
1059
1182
 
@@ -1115,6 +1238,13 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
1115
1238
 
1116
1239
 
1117
1240
 
1241
+
1242
+
1243
+
1244
+
1245
+
1246
+
1247
+
1118
1248
 
1119
1249
 
1120
1250
 
@@ -1361,17 +1491,17 @@ export class FibonacciHeap<E> {
1361
1491
  * Push an element into the root list.
1362
1492
  * @remarks Time O(1) amortized, Space O(1)
1363
1493
  * @param element - Element to insert.
1364
- * @returns This heap.
1494
+ * @returns True when the element is added.
1365
1495
  */
1366
1496
 
1367
- push(element: E): this {
1497
+ push(element: E): boolean {
1368
1498
  const node = this.createNode(element);
1369
1499
  node.left = node;
1370
1500
  node.right = node;
1371
1501
  this.mergeWithRoot(node);
1372
1502
  if (!this.min || this.comparator(node.element, this.min.element) <= 0) this._min = node;
1373
1503
  this._size++;
1374
- return this;
1504
+ return true;
1375
1505
  }
1376
1506
 
1377
1507
  peek(): E | undefined {
@@ -295,6 +295,13 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
295
295
 
296
296
 
297
297
 
298
+
299
+
300
+
301
+
302
+
303
+
304
+
298
305
 
299
306
 
300
307
 
@@ -366,6 +373,13 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
366
373
 
367
374
 
368
375
 
376
+
377
+
378
+
379
+
380
+
381
+
382
+
369
383
 
370
384
 
371
385
 
@@ -436,6 +450,13 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
436
450
 
437
451
 
438
452
 
453
+
454
+
455
+
456
+
457
+
458
+
459
+
439
460
 
440
461
 
441
462
 
@@ -497,6 +518,13 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
497
518
 
498
519
 
499
520
 
521
+
522
+
523
+
524
+
525
+
526
+
527
+
500
528
 
501
529
 
502
530
 
@@ -591,6 +619,13 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
591
619
 
592
620
 
593
621
 
622
+
623
+
624
+
625
+
626
+
627
+
628
+
594
629
 
595
630
 
596
631
 
@@ -642,6 +677,13 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
642
677
 
643
678
 
644
679
 
680
+
681
+
682
+
683
+
684
+
685
+
686
+
645
687
 
646
688
 
647
689
 
@@ -732,6 +774,13 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
732
774
 
733
775
 
734
776
 
777
+
778
+
779
+
780
+
781
+
782
+
783
+
735
784
 
736
785
 
737
786
 
@@ -858,6 +907,13 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
858
907
 
859
908
 
860
909
 
910
+
911
+
912
+
913
+
914
+
915
+
916
+
861
917
 
862
918
 
863
919
 
@@ -916,6 +972,13 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
916
972
 
917
973
 
918
974
 
975
+
976
+
977
+
978
+
979
+
980
+
981
+
919
982
 
920
983
 
921
984
 
@@ -976,6 +1039,13 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
976
1039
 
977
1040
 
978
1041
 
1042
+
1043
+
1044
+
1045
+
1046
+
1047
+
1048
+
979
1049
 
980
1050
 
981
1051
 
@@ -1022,6 +1092,13 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
1022
1092
 
1023
1093
 
1024
1094
 
1095
+
1096
+
1097
+
1098
+
1099
+
1100
+
1101
+
1025
1102
 
1026
1103
 
1027
1104
 
@@ -1072,6 +1149,13 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
1072
1149
 
1073
1150
 
1074
1151
 
1152
+
1153
+
1154
+
1155
+
1156
+
1157
+
1158
+
1075
1159
 
1076
1160
 
1077
1161
 
@@ -1131,6 +1215,10 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
1131
1215
 
1132
1216
 
1133
1217
 
1218
+
1219
+
1220
+
1221
+
1134
1222
 
1135
1223
 
1136
1224
 
@@ -1139,13 +1227,36 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
1139
1227
  * @example
1140
1228
  * // Find value scanning from tail
1141
1229
  * const list = new DoublyLinkedList<number>([1, 2, 3, 4]);
1142
- * // getBackward scans from tail to head, returns first match
1143
- * const found = list.getBackward(node => node.value < 4);
1230
+ * // findLast scans from tail to head, returns first match
1231
+ * const found = list.findLast(node => node.value < 4);
1144
1232
  * console.log(found); // 3;
1145
1233
  */
1146
1234
 
1235
+ /**
1236
+ * @deprecated Use `findLast` instead. Will be removed in a future major version.
1237
+ */
1147
1238
  getBackward(
1148
1239
  elementNodeOrPredicate: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)
1240
+ ): E | undefined {
1241
+ return this.findLast(elementNodeOrPredicate);
1242
+ }
1243
+
1244
+ /**
1245
+ * Find the first value matching a predicate scanning backward (tail → head).
1246
+ * @remarks Time O(N), Space O(1)
1247
+ * @param elementNodeOrPredicate - Element, node, or predicate to match.
1248
+ * @returns Matching value or undefined.
1249
+
1250
+
1251
+ * @example
1252
+ * // Find value scanning from tail
1253
+ * const list = new DoublyLinkedList<number>([1, 2, 3, 4]);
1254
+ * // findLast scans from tail to head, returns first match
1255
+ * const found = list.findLast(node => node.value < 4);
1256
+ * console.log(found); // 3;
1257
+ */
1258
+ findLast(
1259
+ elementNodeOrPredicate: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)
1149
1260
  ): E | undefined {
1150
1261
  const predicate = this._ensurePredicate(elementNodeOrPredicate);
1151
1262
  let current = this.tail;
@@ -1156,6 +1267,23 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
1156
1267
  return undefined;
1157
1268
  }
1158
1269
 
1270
+ /**
1271
+ * Find the index of the last value matching a predicate (scans tail → head).
1272
+ * @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1).
1273
+ * @param predicate - Function called with (value, index, list).
1274
+ * @returns Matching index, or -1 if not found.
1275
+ */
1276
+ findLastIndex(predicate: (value: E, index: number, list: this) => boolean): number {
1277
+ let current = this.tail;
1278
+ let index = this.length - 1;
1279
+ while (current) {
1280
+ if (predicate(current.value, index, this)) return index;
1281
+ current = current.prev;
1282
+ index--;
1283
+ }
1284
+ return -1;
1285
+ }
1286
+
1159
1287
  /**
1160
1288
  * Reverse the list in place.
1161
1289
  * @remarks Time O(N), Space O(1)
@@ -1187,6 +1315,13 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
1187
1315
 
1188
1316
 
1189
1317
 
1318
+
1319
+
1320
+
1321
+
1322
+
1323
+
1324
+
1190
1325
 
1191
1326
 
1192
1327
 
@@ -1213,6 +1348,26 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
1213
1348
  return this;
1214
1349
  }
1215
1350
 
1351
+ /**
1352
+ * Delete the first element that satisfies a predicate.
1353
+ * @remarks Time O(N), Space O(1)
1354
+ * @param predicate - Function (value, index, list) → boolean to decide deletion.
1355
+ * @returns True if a match was removed.
1356
+ */
1357
+ deleteWhere(predicate: (value: E, index: number, list: this) => boolean): boolean {
1358
+ let current = this.head;
1359
+ let index = 0;
1360
+ while (current) {
1361
+ if (predicate(current.value, index, this)) {
1362
+ this.delete(current);
1363
+ return true;
1364
+ }
1365
+ current = current.next;
1366
+ index++;
1367
+ }
1368
+ return false;
1369
+ }
1370
+
1216
1371
  /**
1217
1372
  * Set the equality comparator used to compare values.
1218
1373
  * @remarks Time O(1), Space O(1)
@@ -1254,6 +1409,13 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
1254
1409
 
1255
1410
 
1256
1411
 
1412
+
1413
+
1414
+
1415
+
1416
+
1417
+
1418
+
1257
1419
 
1258
1420
 
1259
1421
 
@@ -1309,6 +1471,13 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
1309
1471
 
1310
1472
 
1311
1473
 
1474
+
1475
+
1476
+
1477
+
1478
+
1479
+
1480
+
1312
1481
 
1313
1482
 
1314
1483
 
@@ -1385,6 +1554,13 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
1385
1554
 
1386
1555
 
1387
1556
 
1557
+
1558
+
1559
+
1560
+
1561
+
1562
+
1563
+
1388
1564
 
1389
1565
 
1390
1566