max-priority-queue-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 (76) hide show
  1. package/README.md +63 -0
  2. package/dist/cjs/index.cjs +400 -119
  3. package/dist/cjs/index.cjs.map +1 -1
  4. package/dist/cjs-legacy/index.cjs +399 -118
  5. package/dist/cjs-legacy/index.cjs.map +1 -1
  6. package/dist/esm/index.mjs +400 -119
  7. package/dist/esm/index.mjs.map +1 -1
  8. package/dist/esm-legacy/index.mjs +399 -118
  9. package/dist/esm-legacy/index.mjs.map +1 -1
  10. package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
  11. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
  12. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
  13. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +429 -78
  14. package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
  15. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +212 -32
  16. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
  17. package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
  18. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
  19. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
  20. package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
  21. package/dist/types/data-structures/graph/directed-graph.d.ts +219 -47
  22. package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
  23. package/dist/types/data-structures/graph/undirected-graph.d.ts +204 -59
  24. package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
  25. package/dist/types/data-structures/heap/heap.d.ts +287 -99
  26. package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
  27. package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
  28. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
  29. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
  30. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
  31. package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
  32. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
  33. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
  34. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
  35. package/dist/types/data-structures/queue/deque.d.ts +272 -65
  36. package/dist/types/data-structures/queue/queue.d.ts +211 -42
  37. package/dist/types/data-structures/stack/stack.d.ts +174 -32
  38. package/dist/types/data-structures/trie/trie.d.ts +213 -43
  39. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
  40. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
  41. package/dist/umd/max-priority-queue-typed.js +397 -116
  42. package/dist/umd/max-priority-queue-typed.js.map +1 -1
  43. package/dist/umd/max-priority-queue-typed.min.js +1 -1
  44. package/dist/umd/max-priority-queue-typed.min.js.map +1 -1
  45. package/package.json +2 -2
  46. package/src/data-structures/base/iterable-element-base.ts +4 -5
  47. package/src/data-structures/binary-tree/avl-tree.ts +134 -51
  48. package/src/data-structures/binary-tree/binary-indexed-tree.ts +302 -247
  49. package/src/data-structures/binary-tree/binary-tree.ts +429 -79
  50. package/src/data-structures/binary-tree/bst.ts +335 -34
  51. package/src/data-structures/binary-tree/red-black-tree.ts +290 -97
  52. package/src/data-structures/binary-tree/segment-tree.ts +372 -248
  53. package/src/data-structures/binary-tree/tree-map.ts +1284 -6
  54. package/src/data-structures/binary-tree/tree-multi-map.ts +1094 -211
  55. package/src/data-structures/binary-tree/tree-multi-set.ts +858 -65
  56. package/src/data-structures/binary-tree/tree-set.ts +1136 -9
  57. package/src/data-structures/graph/directed-graph.ts +219 -47
  58. package/src/data-structures/graph/map-graph.ts +59 -1
  59. package/src/data-structures/graph/undirected-graph.ts +204 -59
  60. package/src/data-structures/hash/hash-map.ts +230 -77
  61. package/src/data-structures/heap/heap.ts +287 -99
  62. package/src/data-structures/heap/max-heap.ts +46 -0
  63. package/src/data-structures/heap/min-heap.ts +59 -0
  64. package/src/data-structures/linked-list/doubly-linked-list.ts +286 -44
  65. package/src/data-structures/linked-list/singly-linked-list.ts +278 -65
  66. package/src/data-structures/linked-list/skip-linked-list.ts +689 -90
  67. package/src/data-structures/matrix/matrix.ts +416 -12
  68. package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
  69. package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
  70. package/src/data-structures/priority-queue/priority-queue.ts +60 -0
  71. package/src/data-structures/queue/deque.ts +272 -65
  72. package/src/data-structures/queue/queue.ts +211 -42
  73. package/src/data-structures/stack/stack.ts +174 -32
  74. package/src/data-structures/trie/trie.ts +213 -43
  75. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
  76. 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,22 @@ 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
+ * @example
229
+ * // Get edge between vertices
230
+ * const g = new DirectedGraph();
231
+ * g.addVertex('A');
232
+ * g.addVertex('B');
233
+ * g.addEdge('A', 'B', 5);
234
+ * const edge = g.getEdge('A', 'B');
235
+ * console.log(edge?.weight); // 5;
267
236
  */
268
237
  getEdge(srcOrKey: VO | VertexKey | undefined, destOrKey: VO | VertexKey | undefined): EO | undefined {
269
238
  let edgeMap: EO[] = [];
@@ -316,6 +285,42 @@ export class DirectedGraph<
316
285
  * @param destVertexKey - Optional destination vertex/key when deleting by pair.
317
286
  * @returns Removed edge or `undefined`.
318
287
  * @remarks Time O(1) avg, Space O(1)
288
+
289
+
290
+
291
+
292
+
293
+
294
+
295
+
296
+
297
+
298
+
299
+ * @example
300
+ * // DirectedGraph deleteEdge and vertex operations
301
+ * const graph = new DirectedGraph<string>();
302
+ *
303
+ * // Build a small graph
304
+ * graph.addVertex('X');
305
+ * graph.addVertex('Y');
306
+ * graph.addVertex('Z');
307
+ * graph.addEdge('X', 'Y', 1);
308
+ * graph.addEdge('Y', 'Z', 2);
309
+ *
310
+ * // Delete an edge
311
+ * graph.deleteEdgeSrcToDest('X', 'Y');
312
+ * console.log(graph.hasEdge('X', 'Y')); // false;
313
+ *
314
+ * // Edge in other direction should not exist
315
+ * console.log(graph.hasEdge('Y', 'X')); // false;
316
+ *
317
+ * // Other edges should remain
318
+ * console.log(graph.hasEdge('Y', 'Z')); // true;
319
+ *
320
+ * // Delete a vertex
321
+ * graph.deleteVertex('Y');
322
+ * console.log(graph.hasVertex('Y')); // false;
323
+ * console.log(graph.size); // 2;
319
324
  */
320
325
  deleteEdge(edgeOrSrcVertexKey: EO | VertexKey, destVertexKey?: VertexKey): EO | undefined {
321
326
  let removed: EO | undefined = undefined;
@@ -347,6 +352,26 @@ export class DirectedGraph<
347
352
  return removed;
348
353
  }
349
354
 
355
+ /**
356
+ * Remove a vertex
357
+
358
+
359
+
360
+
361
+
362
+
363
+
364
+
365
+ * @example
366
+ * // Remove a vertex
367
+ * const g = new DirectedGraph();
368
+ * g.addVertex('A');
369
+ * g.addVertex('B');
370
+ * g.addEdge('A', 'B');
371
+ * g.deleteVertex('A');
372
+ * console.log(g.hasVertex('A')); // false;
373
+ * console.log(g.hasEdge('A', 'B')); // false;
374
+ */
350
375
  deleteVertex(vertexOrKey: VO | VertexKey): boolean {
351
376
  let vertexKey: VertexKey;
352
377
  let vertex: VO | undefined;
@@ -395,6 +420,23 @@ export class DirectedGraph<
395
420
  * @param vertexOrKey - Vertex or key.
396
421
  * @returns Array of incoming edges.
397
422
  * @remarks Time O(deg_in), Space O(deg_in)
423
+
424
+
425
+
426
+
427
+
428
+
429
+
430
+
431
+ * @example
432
+ * // Get incoming edges
433
+ * const g = new DirectedGraph();
434
+ * g.addVertex('A');
435
+ * g.addVertex('B');
436
+ * g.addVertex('C');
437
+ * g.addEdge('A', 'C');
438
+ * g.addEdge('B', 'C');
439
+ * console.log(g.incomingEdgesOf('C').length); // 2;
398
440
  */
399
441
  incomingEdgesOf(vertexOrKey: VO | VertexKey): EO[] {
400
442
  const target = this._getVertex(vertexOrKey);
@@ -409,6 +451,23 @@ export class DirectedGraph<
409
451
  * @param vertexOrKey - Vertex or key.
410
452
  * @returns Array of outgoing edges.
411
453
  * @remarks Time O(deg_out), Space O(deg_out)
454
+
455
+
456
+
457
+
458
+
459
+
460
+
461
+
462
+ * @example
463
+ * // Get outgoing edges
464
+ * const g = new DirectedGraph();
465
+ * g.addVertex('A');
466
+ * g.addVertex('B');
467
+ * g.addVertex('C');
468
+ * g.addEdge('A', 'B');
469
+ * g.addEdge('A', 'C');
470
+ * console.log(g.outgoingEdgesOf('A').length); // 2;
412
471
  */
413
472
  outgoingEdgesOf(vertexOrKey: VO | VertexKey): EO[] {
414
473
  const target = this._getVertex(vertexOrKey);
@@ -492,6 +551,39 @@ export class DirectedGraph<
492
551
  * @param propertyName - `'key'` to map to keys; `'vertex'` to keep instances.
493
552
  * @returns Array of keys/vertices, or `undefined` when cycle is found.
494
553
  * @remarks Time O(V + E), Space O(V)
554
+
555
+
556
+
557
+
558
+
559
+
560
+
561
+
562
+
563
+
564
+
565
+ * @example
566
+ * // DirectedGraph topologicalSort for task scheduling
567
+ * const graph = new DirectedGraph<string>();
568
+ *
569
+ * // Build a DAG (Directed Acyclic Graph) for task dependencies
570
+ * graph.addVertex('Design');
571
+ * graph.addVertex('Implement');
572
+ * graph.addVertex('Test');
573
+ * graph.addVertex('Deploy');
574
+ *
575
+ * // Add dependency edges
576
+ * graph.addEdge('Design', 'Implement', 1); // Design must come before Implement
577
+ * graph.addEdge('Implement', 'Test', 1); // Implement must come before Test
578
+ * graph.addEdge('Test', 'Deploy', 1); // Test must come before Deploy
579
+ *
580
+ * // Topological sort gives valid execution order
581
+ * const executionOrder = graph.topologicalSort();
582
+ * console.log(executionOrder); // defined;
583
+ * console.log(executionOrder); // ['Design', 'Implement', 'Test', 'Deploy'];
584
+ *
585
+ * // All vertices should be included
586
+ * console.log(executionOrder?.length); // 4;
495
587
  */
496
588
  topologicalSort(propertyName?: 'vertex' | 'key'): Array<VO | VertexKey> | undefined {
497
589
  propertyName = propertyName ?? 'key';
@@ -530,6 +622,24 @@ export class DirectedGraph<
530
622
  return sorted.reverse();
531
623
  }
532
624
 
625
+ /**
626
+ * Get all edges
627
+
628
+
629
+
630
+
631
+
632
+
633
+
634
+
635
+ * @example
636
+ * // Get all edges
637
+ * const g = new DirectedGraph();
638
+ * g.addVertex('A');
639
+ * g.addVertex('B');
640
+ * g.addEdge('A', 'B', 3);
641
+ * console.log(g.edgeSet().length); // 1;
642
+ */
533
643
  edgeSet(): EO[] {
534
644
  let edgeMap: EO[] = [];
535
645
  this._outEdgeMap.forEach(outEdges => {
@@ -538,6 +648,28 @@ export class DirectedGraph<
538
648
  return edgeMap;
539
649
  }
540
650
 
651
+ /**
652
+ * Get outgoing neighbors
653
+
654
+
655
+
656
+
657
+
658
+
659
+
660
+
661
+
662
+ * @example
663
+ * // Get outgoing neighbors
664
+ * const g = new DirectedGraph();
665
+ * g.addVertex('A');
666
+ * g.addVertex('B');
667
+ * g.addVertex('C');
668
+ * g.addEdge('A', 'B');
669
+ * g.addEdge('A', 'C');
670
+ * const neighbors = g.getNeighbors('A');
671
+ * console.log(neighbors.map(v => v.key).sort()); // ['B', 'C'];
672
+ */
541
673
  getNeighbors(vertexOrKey: VO | VertexKey): VO[] {
542
674
  const neighbors: VO[] = [];
543
675
  const vertex = this._getVertex(vertexOrKey);
@@ -604,6 +736,27 @@ export class DirectedGraph<
604
736
  * Tarjan's algorithm for strongly connected components.
605
737
  * @returns `{ dfnMap, lowMap, SCCs }`.
606
738
  * @remarks Time O(V + E), Space O(V + E)
739
+
740
+
741
+
742
+
743
+
744
+
745
+
746
+
747
+ * @example
748
+ * // Find strongly connected components
749
+ * const g = new DirectedGraph();
750
+ * g.addVertex('A');
751
+ * g.addVertex('B');
752
+ * g.addVertex('C');
753
+ * g.addEdge('A', 'B');
754
+ * g.addEdge('B', 'C');
755
+ * g.addEdge('C', 'A');
756
+ * const { SCCs } = g.tarjan();
757
+ * // A→B→C→A forms one SCC with 3 members
758
+ * const sccArrays = [...SCCs.values()];
759
+ * console.log(sccArrays.some(scc => scc.length === 3)); // true;
607
760
  */
608
761
  tarjan(): { dfnMap: Map<VO, number>; lowMap: Map<VO, number>; SCCs: Map<number, VO[]> } {
609
762
  const dfnMap = new Map<VO, number>();
@@ -678,6 +831,25 @@ export class DirectedGraph<
678
831
  * Strongly connected components computed by `tarjan()`.
679
832
  * @returns Map from SCC id to vertices.
680
833
  * @remarks Time O(#SCC + V), Space O(V)
834
+
835
+
836
+
837
+
838
+
839
+
840
+
841
+
842
+ * @example
843
+ * // Get strongly connected components
844
+ * const g = new DirectedGraph();
845
+ * g.addVertex(1);
846
+ * g.addVertex(2);
847
+ * g.addVertex(3);
848
+ * g.addEdge(1, 2);
849
+ * g.addEdge(2, 3);
850
+ * g.addEdge(3, 1);
851
+ * const sccs = g.getSCCs(); // Map<number, VO[]>
852
+ * console.log(sccs.size); // >= 1;
681
853
  */
682
854
  getSCCs(): Map<number, VO[]> {
683
855
  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,