data-structure-typed 2.4.5 → 2.5.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 (71) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +15 -5
  3. package/dist/cjs/index.cjs +10240 -2079
  4. package/dist/cjs-legacy/index.cjs +10305 -2135
  5. package/dist/esm/index.mjs +10241 -2078
  6. package/dist/esm-legacy/index.mjs +10306 -2134
  7. package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
  8. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
  9. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
  10. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +429 -78
  11. package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
  12. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +212 -32
  13. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
  14. package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
  15. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
  16. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
  17. package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
  18. package/dist/types/data-structures/graph/directed-graph.d.ts +219 -47
  19. package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
  20. package/dist/types/data-structures/graph/undirected-graph.d.ts +204 -59
  21. package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
  22. package/dist/types/data-structures/heap/heap.d.ts +287 -99
  23. package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
  24. package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
  25. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
  26. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
  27. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
  28. package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
  29. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
  30. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
  31. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
  32. package/dist/types/data-structures/queue/deque.d.ts +272 -65
  33. package/dist/types/data-structures/queue/queue.d.ts +211 -42
  34. package/dist/types/data-structures/stack/stack.d.ts +174 -32
  35. package/dist/types/data-structures/trie/trie.d.ts +213 -43
  36. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
  37. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
  38. package/dist/umd/data-structure-typed.js +10308 -2133
  39. package/dist/umd/data-structure-typed.min.js +4 -4
  40. package/package.json +5 -4
  41. package/src/data-structures/base/iterable-element-base.ts +4 -5
  42. package/src/data-structures/binary-tree/avl-tree.ts +146 -51
  43. package/src/data-structures/binary-tree/binary-indexed-tree.ts +316 -247
  44. package/src/data-structures/binary-tree/binary-tree.ts +454 -79
  45. package/src/data-structures/binary-tree/bst.ts +359 -34
  46. package/src/data-structures/binary-tree/red-black-tree.ts +309 -97
  47. package/src/data-structures/binary-tree/segment-tree.ts +378 -248
  48. package/src/data-structures/binary-tree/tree-map.ts +1403 -6
  49. package/src/data-structures/binary-tree/tree-multi-map.ts +1214 -211
  50. package/src/data-structures/binary-tree/tree-multi-set.ts +954 -65
  51. package/src/data-structures/binary-tree/tree-set.ts +1250 -9
  52. package/src/data-structures/graph/directed-graph.ts +229 -47
  53. package/src/data-structures/graph/map-graph.ts +59 -1
  54. package/src/data-structures/graph/undirected-graph.ts +213 -59
  55. package/src/data-structures/hash/hash-map.ts +241 -77
  56. package/src/data-structures/heap/heap.ts +301 -99
  57. package/src/data-structures/heap/max-heap.ts +46 -0
  58. package/src/data-structures/heap/min-heap.ts +59 -0
  59. package/src/data-structures/linked-list/doubly-linked-list.ts +303 -44
  60. package/src/data-structures/linked-list/singly-linked-list.ts +293 -65
  61. package/src/data-structures/linked-list/skip-linked-list.ts +707 -90
  62. package/src/data-structures/matrix/matrix.ts +424 -12
  63. package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
  64. package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
  65. package/src/data-structures/priority-queue/priority-queue.ts +60 -0
  66. package/src/data-structures/queue/deque.ts +287 -65
  67. package/src/data-structures/queue/queue.ts +223 -42
  68. package/src/data-structures/stack/stack.ts +184 -32
  69. package/src/data-structures/trie/trie.ts +225 -43
  70. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
  71. package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
@@ -77,53 +77,6 @@ export class DirectedEdge<E = any> extends AbstractEdge<E> {
77
77
  * console.log(neighborsA[0].key); // 'B';
78
78
  * console.log(neighborsA[1].key); // 'C';
79
79
  * @example
80
- * // DirectedGraph deleteEdge and vertex operations
81
- * const graph = new DirectedGraph<string>();
82
- *
83
- * // Build a small graph
84
- * graph.addVertex('X');
85
- * graph.addVertex('Y');
86
- * graph.addVertex('Z');
87
- * graph.addEdge('X', 'Y', 1);
88
- * graph.addEdge('Y', 'Z', 2);
89
- *
90
- * // Delete an edge
91
- * graph.deleteEdgeSrcToDest('X', 'Y');
92
- * console.log(graph.hasEdge('X', 'Y')); // false;
93
- *
94
- * // Edge in other direction should not exist
95
- * console.log(graph.hasEdge('Y', 'X')); // false;
96
- *
97
- * // Other edges should remain
98
- * console.log(graph.hasEdge('Y', 'Z')); // true;
99
- *
100
- * // Delete a vertex
101
- * graph.deleteVertex('Y');
102
- * console.log(graph.hasVertex('Y')); // false;
103
- * console.log(graph.size); // 2;
104
- * @example
105
- * // DirectedGraph topologicalSort for task scheduling
106
- * const graph = new DirectedGraph<string>();
107
- *
108
- * // Build a DAG (Directed Acyclic Graph) for task dependencies
109
- * graph.addVertex('Design');
110
- * graph.addVertex('Implement');
111
- * graph.addVertex('Test');
112
- * graph.addVertex('Deploy');
113
- *
114
- * // Add dependency edges
115
- * graph.addEdge('Design', 'Implement', 1); // Design must come before Implement
116
- * graph.addEdge('Implement', 'Test', 1); // Implement must come before Test
117
- * graph.addEdge('Test', 'Deploy', 1); // Test must come before Deploy
118
- *
119
- * // Topological sort gives valid execution order
120
- * const executionOrder = graph.topologicalSort();
121
- * console.log(executionOrder); // defined;
122
- * console.log(executionOrder); // ['Design', 'Implement', 'Test', 'Deploy'];
123
- *
124
- * // All vertices should be included
125
- * console.log(executionOrder?.length); // 4;
126
- * @example
127
80
  * // DirectedGraph dijkstra shortest path for network routing
128
81
  * // Build a weighted directed graph representing network nodes and costs
129
82
  * const network = new DirectedGraph<string>();
@@ -264,6 +217,23 @@ export class DirectedGraph<
264
217
  * @param destOrKey - Destination vertex or key.
265
218
  * @returns Edge instance or `undefined`.
266
219
  * @remarks Time O(1) avg, Space O(1)
220
+
221
+
222
+
223
+
224
+
225
+
226
+
227
+
228
+
229
+ * @example
230
+ * // Get edge between vertices
231
+ * const g = new DirectedGraph();
232
+ * g.addVertex('A');
233
+ * g.addVertex('B');
234
+ * g.addEdge('A', 'B', 5);
235
+ * const edge = g.getEdge('A', 'B');
236
+ * console.log(edge?.weight); // 5;
267
237
  */
268
238
  getEdge(srcOrKey: VO | VertexKey | undefined, destOrKey: VO | VertexKey | undefined): EO | undefined {
269
239
  let edgeMap: EO[] = [];
@@ -316,6 +286,43 @@ export class DirectedGraph<
316
286
  * @param destVertexKey - Optional destination vertex/key when deleting by pair.
317
287
  * @returns Removed edge or `undefined`.
318
288
  * @remarks Time O(1) avg, Space O(1)
289
+
290
+
291
+
292
+
293
+
294
+
295
+
296
+
297
+
298
+
299
+
300
+
301
+ * @example
302
+ * // DirectedGraph deleteEdge and vertex operations
303
+ * const graph = new DirectedGraph<string>();
304
+ *
305
+ * // Build a small graph
306
+ * graph.addVertex('X');
307
+ * graph.addVertex('Y');
308
+ * graph.addVertex('Z');
309
+ * graph.addEdge('X', 'Y', 1);
310
+ * graph.addEdge('Y', 'Z', 2);
311
+ *
312
+ * // Delete an edge
313
+ * graph.deleteEdgeSrcToDest('X', 'Y');
314
+ * console.log(graph.hasEdge('X', 'Y')); // false;
315
+ *
316
+ * // Edge in other direction should not exist
317
+ * console.log(graph.hasEdge('Y', 'X')); // false;
318
+ *
319
+ * // Other edges should remain
320
+ * console.log(graph.hasEdge('Y', 'Z')); // true;
321
+ *
322
+ * // Delete a vertex
323
+ * graph.deleteVertex('Y');
324
+ * console.log(graph.hasVertex('Y')); // false;
325
+ * console.log(graph.size); // 2;
319
326
  */
320
327
  deleteEdge(edgeOrSrcVertexKey: EO | VertexKey, destVertexKey?: VertexKey): EO | undefined {
321
328
  let removed: EO | undefined = undefined;
@@ -347,6 +354,27 @@ export class DirectedGraph<
347
354
  return removed;
348
355
  }
349
356
 
357
+ /**
358
+ * Remove a vertex
359
+
360
+
361
+
362
+
363
+
364
+
365
+
366
+
367
+
368
+ * @example
369
+ * // Remove a vertex
370
+ * const g = new DirectedGraph();
371
+ * g.addVertex('A');
372
+ * g.addVertex('B');
373
+ * g.addEdge('A', 'B');
374
+ * g.deleteVertex('A');
375
+ * console.log(g.hasVertex('A')); // false;
376
+ * console.log(g.hasEdge('A', 'B')); // false;
377
+ */
350
378
  deleteVertex(vertexOrKey: VO | VertexKey): boolean {
351
379
  let vertexKey: VertexKey;
352
380
  let vertex: VO | undefined;
@@ -395,6 +423,24 @@ export class DirectedGraph<
395
423
  * @param vertexOrKey - Vertex or key.
396
424
  * @returns Array of incoming edges.
397
425
  * @remarks Time O(deg_in), Space O(deg_in)
426
+
427
+
428
+
429
+
430
+
431
+
432
+
433
+
434
+
435
+ * @example
436
+ * // Get incoming edges
437
+ * const g = new DirectedGraph();
438
+ * g.addVertex('A');
439
+ * g.addVertex('B');
440
+ * g.addVertex('C');
441
+ * g.addEdge('A', 'C');
442
+ * g.addEdge('B', 'C');
443
+ * console.log(g.incomingEdgesOf('C').length); // 2;
398
444
  */
399
445
  incomingEdgesOf(vertexOrKey: VO | VertexKey): EO[] {
400
446
  const target = this._getVertex(vertexOrKey);
@@ -409,6 +455,24 @@ export class DirectedGraph<
409
455
  * @param vertexOrKey - Vertex or key.
410
456
  * @returns Array of outgoing edges.
411
457
  * @remarks Time O(deg_out), Space O(deg_out)
458
+
459
+
460
+
461
+
462
+
463
+
464
+
465
+
466
+
467
+ * @example
468
+ * // Get outgoing edges
469
+ * const g = new DirectedGraph();
470
+ * g.addVertex('A');
471
+ * g.addVertex('B');
472
+ * g.addVertex('C');
473
+ * g.addEdge('A', 'B');
474
+ * g.addEdge('A', 'C');
475
+ * console.log(g.outgoingEdgesOf('A').length); // 2;
412
476
  */
413
477
  outgoingEdgesOf(vertexOrKey: VO | VertexKey): EO[] {
414
478
  const target = this._getVertex(vertexOrKey);
@@ -492,6 +556,40 @@ export class DirectedGraph<
492
556
  * @param propertyName - `'key'` to map to keys; `'vertex'` to keep instances.
493
557
  * @returns Array of keys/vertices, or `undefined` when cycle is found.
494
558
  * @remarks Time O(V + E), Space O(V)
559
+
560
+
561
+
562
+
563
+
564
+
565
+
566
+
567
+
568
+
569
+
570
+
571
+ * @example
572
+ * // DirectedGraph topologicalSort for task scheduling
573
+ * const graph = new DirectedGraph<string>();
574
+ *
575
+ * // Build a DAG (Directed Acyclic Graph) for task dependencies
576
+ * graph.addVertex('Design');
577
+ * graph.addVertex('Implement');
578
+ * graph.addVertex('Test');
579
+ * graph.addVertex('Deploy');
580
+ *
581
+ * // Add dependency edges
582
+ * graph.addEdge('Design', 'Implement', 1); // Design must come before Implement
583
+ * graph.addEdge('Implement', 'Test', 1); // Implement must come before Test
584
+ * graph.addEdge('Test', 'Deploy', 1); // Test must come before Deploy
585
+ *
586
+ * // Topological sort gives valid execution order
587
+ * const executionOrder = graph.topologicalSort();
588
+ * console.log(executionOrder); // defined;
589
+ * console.log(executionOrder); // ['Design', 'Implement', 'Test', 'Deploy'];
590
+ *
591
+ * // All vertices should be included
592
+ * console.log(executionOrder?.length); // 4;
495
593
  */
496
594
  topologicalSort(propertyName?: 'vertex' | 'key'): Array<VO | VertexKey> | undefined {
497
595
  propertyName = propertyName ?? 'key';
@@ -530,6 +628,25 @@ export class DirectedGraph<
530
628
  return sorted.reverse();
531
629
  }
532
630
 
631
+ /**
632
+ * Get all edges
633
+
634
+
635
+
636
+
637
+
638
+
639
+
640
+
641
+
642
+ * @example
643
+ * // Get all edges
644
+ * const g = new DirectedGraph();
645
+ * g.addVertex('A');
646
+ * g.addVertex('B');
647
+ * g.addEdge('A', 'B', 3);
648
+ * console.log(g.edgeSet().length); // 1;
649
+ */
533
650
  edgeSet(): EO[] {
534
651
  let edgeMap: EO[] = [];
535
652
  this._outEdgeMap.forEach(outEdges => {
@@ -538,6 +655,29 @@ export class DirectedGraph<
538
655
  return edgeMap;
539
656
  }
540
657
 
658
+ /**
659
+ * Get outgoing neighbors
660
+
661
+
662
+
663
+
664
+
665
+
666
+
667
+
668
+
669
+
670
+ * @example
671
+ * // Get outgoing neighbors
672
+ * const g = new DirectedGraph();
673
+ * g.addVertex('A');
674
+ * g.addVertex('B');
675
+ * g.addVertex('C');
676
+ * g.addEdge('A', 'B');
677
+ * g.addEdge('A', 'C');
678
+ * const neighbors = g.getNeighbors('A');
679
+ * console.log(neighbors.map(v => v.key).sort()); // ['B', 'C'];
680
+ */
541
681
  getNeighbors(vertexOrKey: VO | VertexKey): VO[] {
542
682
  const neighbors: VO[] = [];
543
683
  const vertex = this._getVertex(vertexOrKey);
@@ -604,6 +744,28 @@ export class DirectedGraph<
604
744
  * Tarjan's algorithm for strongly connected components.
605
745
  * @returns `{ dfnMap, lowMap, SCCs }`.
606
746
  * @remarks Time O(V + E), Space O(V + E)
747
+
748
+
749
+
750
+
751
+
752
+
753
+
754
+
755
+
756
+ * @example
757
+ * // Find strongly connected components
758
+ * const g = new DirectedGraph();
759
+ * g.addVertex('A');
760
+ * g.addVertex('B');
761
+ * g.addVertex('C');
762
+ * g.addEdge('A', 'B');
763
+ * g.addEdge('B', 'C');
764
+ * g.addEdge('C', 'A');
765
+ * const { SCCs } = g.tarjan();
766
+ * // A→B→C→A forms one SCC with 3 members
767
+ * const sccArrays = [...SCCs.values()];
768
+ * console.log(sccArrays.some(scc => scc.length === 3)); // true;
607
769
  */
608
770
  tarjan(): { dfnMap: Map<VO, number>; lowMap: Map<VO, number>; SCCs: Map<number, VO[]> } {
609
771
  const dfnMap = new Map<VO, number>();
@@ -678,6 +840,26 @@ export class DirectedGraph<
678
840
  * Strongly connected components computed by `tarjan()`.
679
841
  * @returns Map from SCC id to vertices.
680
842
  * @remarks Time O(#SCC + V), Space O(V)
843
+
844
+
845
+
846
+
847
+
848
+
849
+
850
+
851
+
852
+ * @example
853
+ * // Get strongly connected components
854
+ * const g = new DirectedGraph();
855
+ * g.addVertex(1);
856
+ * g.addVertex(2);
857
+ * g.addVertex(3);
858
+ * g.addEdge(1, 2);
859
+ * g.addEdge(2, 3);
860
+ * g.addEdge(3, 1);
861
+ * const sccs = g.getSCCs(); // Map<number, VO[]>
862
+ * console.log(sccs.size); // >= 1;
681
863
  */
682
864
  getSCCs(): Map<number, VO[]> {
683
865
  return this.tarjan().SCCs;
@@ -33,7 +33,65 @@ export class MapEdge<E = any> extends DirectedEdge<E> {
33
33
  * @template VO - Concrete vertex class (MapVertex<V>).
34
34
  * @template EO - Concrete edge class (MapEdge<E>).
35
35
  * @remarks Time O(1), Space O(1)
36
- * @example examples will be generated by unit test
36
+ * @example
37
+ * // City navigation with shortest path
38
+ * const map = new MapGraph([0, 0], [10, 10]);
39
+ *
40
+ * map.addVertex(new MapVertex('Home', '', 0, 0));
41
+ * map.addVertex(new MapVertex('Office', '', 3, 4));
42
+ * map.addVertex(new MapVertex('Cafe', '', 1, 2));
43
+ * map.addVertex(new MapVertex('Park', '', 2, 1));
44
+ *
45
+ * map.addEdge('Home', 'Cafe', 2.2);
46
+ * map.addEdge('Cafe', 'Office', 3.5);
47
+ * map.addEdge('Home', 'Park', 2.0);
48
+ * map.addEdge('Park', 'Office', 4.0);
49
+ * map.addEdge('Home', 'Office', 7.0);
50
+ *
51
+ * // Find shortest path
52
+ * const result = map.dijkstra('Home', 'Office', true, true);
53
+ * console.log(result?.minDist); // 5.7; // Home → Cafe → Office
54
+ * console.log(result?.minPath.map(v => v.key)); // ['Home', 'Cafe', 'Office'];
55
+ * @example
56
+ * // Delivery route optimization
57
+ * const routes = new MapGraph([0, 0], [10, 10]);
58
+ *
59
+ * routes.addVertex(new MapVertex('Warehouse', '', 0, 0));
60
+ * routes.addVertex(new MapVertex('Customer A', '', 2, 3));
61
+ * routes.addVertex(new MapVertex('Customer B', '', 5, 1));
62
+ * routes.addVertex(new MapVertex('Customer C', '', 3, 5));
63
+ *
64
+ * routes.addEdge('Warehouse', 'Customer A', 3.6);
65
+ * routes.addEdge('Warehouse', 'Customer B', 5.1);
66
+ * routes.addEdge('Customer A', 'Customer C', 2.2);
67
+ * routes.addEdge('Customer A', 'Customer B', 3.6);
68
+ * routes.addEdge('Customer B', 'Customer C', 4.5);
69
+ *
70
+ * // Check outgoing neighbors of Customer A
71
+ * const neighbors = routes.getNeighbors('Customer A');
72
+ * console.log(neighbors.map(n => n.key).sort()); // ['Customer B', 'Customer C'];
73
+ *
74
+ * // Shortest path from Warehouse to Customer C
75
+ * const path = routes.getMinPathBetween('Warehouse', 'Customer C', true);
76
+ * console.log(path?.map(v => v.key)); // ['Warehouse', 'Customer A', 'Customer C'];
77
+ * @example
78
+ * // Campus map with building connections
79
+ * const campus = new MapGraph([0, 0], [5, 5]);
80
+ *
81
+ * campus.addVertex(new MapVertex('Library', '', 0, 0));
82
+ * campus.addVertex(new MapVertex('Lab', '', 1, 1));
83
+ * campus.addVertex(new MapVertex('Cafeteria', '', 2, 0));
84
+ *
85
+ * campus.addEdge('Library', 'Lab', 5);
86
+ * campus.addEdge('Lab', 'Cafeteria', 3);
87
+ * campus.addEdge('Library', 'Cafeteria', 10);
88
+ *
89
+ * console.log(campus.hasVertex('Library')); // true;
90
+ * console.log(campus.hasVertex('Gym')); // false;
91
+ *
92
+ * // Direct distance vs shortest path
93
+ * const direct = campus.dijkstra('Library', 'Cafeteria', true, true);
94
+ * console.log(direct?.minDist); // 8;
37
95
  */
38
96
  export class MapGraph<
39
97
  V = any,