min-heap-typed 1.50.1 → 1.50.3

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 (85) hide show
  1. package/dist/data-structures/base/iterable-base.d.ts +120 -9
  2. package/dist/data-structures/base/iterable-base.js +143 -7
  3. package/dist/data-structures/binary-tree/avl-tree.d.ts +72 -47
  4. package/dist/data-structures/binary-tree/avl-tree.js +101 -72
  5. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
  6. package/dist/data-structures/binary-tree/binary-indexed-tree.js +22 -0
  7. package/dist/data-structures/binary-tree/binary-tree.d.ts +244 -199
  8. package/dist/data-structures/binary-tree/binary-tree.js +484 -376
  9. package/dist/data-structures/binary-tree/bst.d.ts +92 -79
  10. package/dist/data-structures/binary-tree/bst.js +68 -76
  11. package/dist/data-structures/binary-tree/rb-tree.d.ts +127 -57
  12. package/dist/data-structures/binary-tree/rb-tree.js +152 -99
  13. package/dist/data-structures/binary-tree/segment-tree.d.ts +99 -6
  14. package/dist/data-structures/binary-tree/segment-tree.js +127 -10
  15. package/dist/data-structures/binary-tree/tree-multimap.d.ts +72 -58
  16. package/dist/data-structures/binary-tree/tree-multimap.js +102 -85
  17. package/dist/data-structures/graph/abstract-graph.d.ts +1 -78
  18. package/dist/data-structures/graph/abstract-graph.js +3 -189
  19. package/dist/data-structures/graph/directed-graph.d.ts +73 -0
  20. package/dist/data-structures/graph/directed-graph.js +131 -0
  21. package/dist/data-structures/graph/map-graph.d.ts +8 -0
  22. package/dist/data-structures/graph/map-graph.js +14 -0
  23. package/dist/data-structures/graph/undirected-graph.d.ts +76 -7
  24. package/dist/data-structures/graph/undirected-graph.js +151 -18
  25. package/dist/data-structures/hash/hash-map.d.ts +254 -28
  26. package/dist/data-structures/hash/hash-map.js +347 -78
  27. package/dist/data-structures/heap/heap.d.ts +95 -25
  28. package/dist/data-structures/heap/heap.js +95 -26
  29. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
  30. package/dist/data-structures/linked-list/doubly-linked-list.js +141 -77
  31. package/dist/data-structures/linked-list/singly-linked-list.d.ts +154 -106
  32. package/dist/data-structures/linked-list/singly-linked-list.js +164 -115
  33. package/dist/data-structures/linked-list/skip-linked-list.d.ts +63 -36
  34. package/dist/data-structures/linked-list/skip-linked-list.js +63 -36
  35. package/dist/data-structures/matrix/matrix.d.ts +35 -4
  36. package/dist/data-structures/matrix/matrix.js +50 -11
  37. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
  38. package/dist/data-structures/priority-queue/max-priority-queue.js +10 -0
  39. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
  40. package/dist/data-structures/priority-queue/min-priority-queue.js +11 -0
  41. package/dist/data-structures/priority-queue/priority-queue.d.ts +8 -0
  42. package/dist/data-structures/priority-queue/priority-queue.js +8 -0
  43. package/dist/data-structures/queue/deque.d.ts +139 -35
  44. package/dist/data-structures/queue/deque.js +200 -62
  45. package/dist/data-structures/queue/queue.d.ts +103 -49
  46. package/dist/data-structures/queue/queue.js +111 -49
  47. package/dist/data-structures/stack/stack.d.ts +51 -21
  48. package/dist/data-structures/stack/stack.js +58 -22
  49. package/dist/data-structures/tree/tree.d.ts +57 -3
  50. package/dist/data-structures/tree/tree.js +77 -11
  51. package/dist/data-structures/trie/trie.d.ts +135 -34
  52. package/dist/data-structures/trie/trie.js +153 -33
  53. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  54. package/dist/types/data-structures/hash/hash-map.d.ts +4 -3
  55. package/dist/types/utils/utils.d.ts +1 -0
  56. package/package.json +2 -2
  57. package/src/data-structures/base/iterable-base.ts +184 -19
  58. package/src/data-structures/binary-tree/avl-tree.ts +134 -100
  59. package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
  60. package/src/data-structures/binary-tree/binary-tree.ts +674 -671
  61. package/src/data-structures/binary-tree/bst.ts +127 -136
  62. package/src/data-structures/binary-tree/rb-tree.ts +199 -166
  63. package/src/data-structures/binary-tree/segment-tree.ts +145 -11
  64. package/src/data-structures/binary-tree/tree-multimap.ts +138 -115
  65. package/src/data-structures/graph/abstract-graph.ts +4 -211
  66. package/src/data-structures/graph/directed-graph.ts +152 -0
  67. package/src/data-structures/graph/map-graph.ts +15 -0
  68. package/src/data-structures/graph/undirected-graph.ts +171 -19
  69. package/src/data-structures/hash/hash-map.ts +389 -96
  70. package/src/data-structures/heap/heap.ts +97 -26
  71. package/src/data-structures/linked-list/doubly-linked-list.ts +156 -83
  72. package/src/data-structures/linked-list/singly-linked-list.ts +174 -120
  73. package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
  74. package/src/data-structures/matrix/matrix.ts +52 -12
  75. package/src/data-structures/priority-queue/max-priority-queue.ts +10 -0
  76. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -0
  77. package/src/data-structures/priority-queue/priority-queue.ts +8 -0
  78. package/src/data-structures/queue/deque.ts +225 -70
  79. package/src/data-structures/queue/queue.ts +118 -49
  80. package/src/data-structures/stack/stack.ts +63 -23
  81. package/src/data-structures/tree/tree.ts +89 -15
  82. package/src/data-structures/trie/trie.ts +173 -38
  83. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  84. package/src/types/data-structures/hash/hash-map.ts +4 -3
  85. package/src/types/utils/utils.ts +2 -0
@@ -24,7 +24,7 @@ export class UndirectedVertex<V = any> extends AbstractVertex<V> {
24
24
  }
25
25
 
26
26
  export class UndirectedEdge<E = number> extends AbstractEdge<E> {
27
- vertexMap: [VertexKey, VertexKey];
27
+ endpoints: [VertexKey, VertexKey];
28
28
 
29
29
  /**
30
30
  * The constructor function creates an instance of a class with two vertex IDs, an optional weight, and an optional
@@ -38,7 +38,7 @@ export class UndirectedEdge<E = number> extends AbstractEdge<E> {
38
38
  */
39
39
  constructor(v1: VertexKey, v2: VertexKey, weight?: number, value?: E) {
40
40
  super(weight, value);
41
- this.vertexMap = [v1, v2];
41
+ this.endpoints = [v1, v2];
42
42
  }
43
43
  }
44
44
 
@@ -64,6 +64,10 @@ export class UndirectedGraph<
64
64
  return this._edgeMap;
65
65
  }
66
66
 
67
+ set edgeMap(v: Map<VO, EO[]>) {
68
+ this._edgeMap = v;
69
+ }
70
+
67
71
  /**
68
72
  * The function creates a new vertex with an optional value and returns it.
69
73
  * @param {VertexKey} key - The `key` parameter is the unique identifier for the vertex. It is used to distinguish one
@@ -100,7 +104,7 @@ export class UndirectedGraph<
100
104
  * Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
101
105
  * Space Complexity: O(1)
102
106
  *
103
- * The function `getEdge` returns the first edge that connects two vertexMap, or undefined if no such edge exists.
107
+ * The function `getEdge` returns the first edge that connects two endpoints, or undefined if no such edge exists.
104
108
  * @param {VO | VertexKey | undefined} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `VO` (vertex
105
109
  * object), `undefined`, or `VertexKey` (a string or number representing the ID of a vertex).
106
110
  * @param {VO | VertexKey | undefined} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `VO` (vertex
@@ -115,7 +119,7 @@ export class UndirectedGraph<
115
119
  const vertex2: VO | undefined = this._getVertex(v2);
116
120
 
117
121
  if (vertex1 && vertex2) {
118
- edgeMap = this._edgeMap.get(vertex1)?.filter(e => e.vertexMap.includes(vertex2.key));
122
+ edgeMap = this._edgeMap.get(vertex1)?.filter(e => e.endpoints.includes(vertex2.key));
119
123
  }
120
124
  }
121
125
 
@@ -135,7 +139,7 @@ export class UndirectedGraph<
135
139
  * @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
136
140
  * @param {VO | VertexKey} v2 - VO | VertexKey - This parameter can be either a vertex object (VO) or a vertex ID
137
141
  * (VertexKey). It represents the second vertex of the edge that needs to be removed.
138
- * @returns the removed edge (EO) if it exists, or undefined if either of the vertexMap (VO) does not exist.
142
+ * @returns the removed edge (EO) if it exists, or undefined if either of the endpoints (VO) does not exist.
139
143
  */
140
144
  deleteEdgeBetween(v1: VO | VertexKey, v2: VO | VertexKey): EO | undefined {
141
145
  const vertex1: VO | undefined = this._getVertex(v1);
@@ -148,11 +152,11 @@ export class UndirectedGraph<
148
152
  const v1Edges = this._edgeMap.get(vertex1);
149
153
  let removed: EO | undefined = undefined;
150
154
  if (v1Edges) {
151
- removed = arrayRemove<EO>(v1Edges, (e: EO) => e.vertexMap.includes(vertex2.key))[0] || undefined;
155
+ removed = arrayRemove<EO>(v1Edges, (e: EO) => e.endpoints.includes(vertex2.key))[0] || undefined;
152
156
  }
153
157
  const v2Edges = this._edgeMap.get(vertex2);
154
158
  if (v2Edges) {
155
- arrayRemove<EO>(v2Edges, (e: EO) => e.vertexMap.includes(vertex1.key));
159
+ arrayRemove<EO>(v2Edges, (e: EO) => e.endpoints.includes(vertex1.key));
156
160
  }
157
161
  return removed;
158
162
  }
@@ -166,7 +170,7 @@ export class UndirectedGraph<
166
170
  * Time Complexity: O(E), where E is the number of edgeMap incident to the given vertex.
167
171
  * Space Complexity: O(1)
168
172
  *
169
- * The function `deleteEdge` deletes an edge between two vertexMap in a graph.
173
+ * The function `deleteEdge` deletes an edge between two endpoints in a graph.
170
174
  * @param {EO | VertexKey} edgeOrOneSideVertexKey - The parameter `edgeOrOneSideVertexKey` can be
171
175
  * either an edge object or a vertex key.
172
176
  * @param {VertexKey} [otherSideVertexKey] - The parameter `otherSideVertexKey` is an optional
@@ -185,8 +189,8 @@ export class UndirectedGraph<
185
189
  return;
186
190
  }
187
191
  } else {
188
- oneSide = this._getVertex(edgeOrOneSideVertexKey.vertexMap[0]);
189
- otherSide = this._getVertex(edgeOrOneSideVertexKey.vertexMap[1]);
192
+ oneSide = this._getVertex(edgeOrOneSideVertexKey.endpoints[0]);
193
+ otherSide = this._getVertex(edgeOrOneSideVertexKey.endpoints[1]);
190
194
  }
191
195
 
192
196
  if (oneSide && otherSide) {
@@ -228,7 +232,7 @@ export class UndirectedGraph<
228
232
  const neighborEdges = this._edgeMap.get(neighbor);
229
233
  if (neighborEdges) {
230
234
  const restEdges = neighborEdges.filter(edge => {
231
- return !edge.vertexMap.includes(vertexKey);
235
+ return !edge.endpoints.includes(vertexKey);
232
236
  });
233
237
  this._edgeMap.set(neighbor, restEdges);
234
238
  }
@@ -317,7 +321,7 @@ export class UndirectedGraph<
317
321
  * Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
318
322
  * Space Complexity: O(|E|)
319
323
  *
320
- * The function "getNeighbors" returns an array of neighboring vertexMap for a given vertex or vertex ID.
324
+ * The function "getNeighbors" returns an array of neighboring endpoints for a given vertex or vertex ID.
321
325
  * @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
322
326
  * (`VertexKey`).
323
327
  * @returns an array of vertexMap (VO[]).
@@ -328,7 +332,7 @@ export class UndirectedGraph<
328
332
  if (vertex) {
329
333
  const neighborEdges = this.edgesOf(vertex);
330
334
  for (const edge of neighborEdges) {
331
- const neighbor = this._getVertex(edge.vertexMap.filter(e => e !== vertex.key)[0]);
335
+ const neighbor = this._getVertex(edge.endpoints.filter(e => e !== vertex.key)[0]);
332
336
  if (neighbor) {
333
337
  neighbors.push(neighbor);
334
338
  }
@@ -346,18 +350,18 @@ export class UndirectedGraph<
346
350
  * Time Complexity: O(1)
347
351
  * Space Complexity: O(1)
348
352
  *
349
- * The function "getEndsOfEdge" returns the vertexMap at the ends of an edge if the edge exists in the graph, otherwise
353
+ * The function "getEndsOfEdge" returns the endpoints at the ends of an edge if the edge exists in the graph, otherwise
350
354
  * it returns undefined.
351
355
  * @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
352
- * @returns The function `getEndsOfEdge` returns an array containing two vertexMap `[VO, VO]` if the edge exists in the
356
+ * @returns The function `getEndsOfEdge` returns an array containing two endpoints `[VO, VO]` if the edge exists in the
353
357
  * graph. If the edge does not exist, it returns `undefined`.
354
358
  */
355
359
  getEndsOfEdge(edge: EO): [VO, VO] | undefined {
356
- if (!this.hasEdge(edge.vertexMap[0], edge.vertexMap[1])) {
360
+ if (!this.hasEdge(edge.endpoints[0], edge.endpoints[1])) {
357
361
  return undefined;
358
362
  }
359
- const v1 = this._getVertex(edge.vertexMap[0]);
360
- const v2 = this._getVertex(edge.vertexMap[1]);
363
+ const v1 = this._getVertex(edge.endpoints[0]);
364
+ const v2 = this._getVertex(edge.endpoints[1]);
361
365
  if (v1 && v2) {
362
366
  return [v1, v2];
363
367
  } else {
@@ -365,11 +369,159 @@ export class UndirectedGraph<
365
369
  }
366
370
  }
367
371
 
372
+ /**
373
+ * The isEmpty function checks if the graph is empty.
374
+ * @return True if the graph is empty and false otherwise
375
+ */
376
+ isEmpty(): boolean {
377
+ return this.vertexMap.size === 0 && this.edgeMap.size === 0;
378
+ }
379
+
380
+ /**
381
+ * Time Complexity: O(1)
382
+ * Space Complexity: O(1)
383
+ */
384
+
385
+ /**
386
+ * Time Complexity: O(1)
387
+ * Space Complexity: O(1)
388
+ *
389
+ * The clear function resets the vertex and edge maps to empty maps.
390
+ */
391
+ clear() {
392
+ this._vertexMap = new Map<VertexKey, VO>();
393
+ this._edgeMap = new Map<VO, EO[]>();
394
+ }
395
+
396
+ /**
397
+ * The clone function creates a new UndirectedGraph object and copies the
398
+ * vertexMap and edgeMap from this graph to the new one. This is done by
399
+ * assigning each of these properties to their respective counterparts in the
400
+ * cloned graph. The clone function returns a reference to this newly created,
401
+ * cloned UndirectedGraph object.
402
+ *
403
+ * @return A new instance of the undirectedgraph class
404
+ */
405
+ clone(): UndirectedGraph<V, E, VO, EO> {
406
+ const cloned = new UndirectedGraph<V, E, VO, EO>();
407
+ cloned.vertexMap = new Map<VertexKey, VO>(this.vertexMap);
408
+ cloned.edgeMap = new Map<VO, EO[]>(this.edgeMap);
409
+ return cloned;
410
+ }
411
+
368
412
  /**
369
413
  * Time Complexity: O(1)
370
414
  * Space Complexity: O(1)
371
415
  */
372
416
 
417
+ /**
418
+ * Time Complexity: O(V + E)
419
+ * Space Complexity: O(V)
420
+ * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
421
+ * 1. Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time
422
+ *
423
+ * The function `tarjan` implements the Tarjan's algorithm to find bridges and cut vertices in a
424
+ * graph.
425
+ * @returns The function `tarjan()` returns an object with the following properties:
426
+ */
427
+ tarjan(): { dfnMap: Map<VO, number>; lowMap: Map<VO, number>; bridges: EO[]; cutVertices: VO[] } {
428
+ const dfnMap = new Map<VO, number>();
429
+ const lowMap = new Map<VO, number>();
430
+ const bridges: EO[] = [];
431
+ const cutVertices: VO[] = [];
432
+
433
+ let time = 0;
434
+
435
+ const dfs = (vertex: VO, parent: VO | undefined) => {
436
+ dfnMap.set(vertex, time);
437
+ lowMap.set(vertex, time);
438
+ time++;
439
+
440
+ const neighbors = this.getNeighbors(vertex);
441
+ let childCount = 0;
442
+
443
+ for (const neighbor of neighbors) {
444
+ if (!dfnMap.has(neighbor)) {
445
+ childCount++;
446
+ dfs(neighbor, vertex);
447
+ lowMap.set(vertex, Math.min(lowMap.get(vertex)!, lowMap.get(neighbor)!));
448
+
449
+ if (lowMap.get(neighbor)! > dfnMap.get(vertex)!) {
450
+ // Found a bridge
451
+ const edge = this.getEdge(vertex, neighbor);
452
+ if (edge) {
453
+ bridges.push(edge);
454
+ }
455
+ }
456
+
457
+ if (parent !== undefined && lowMap.get(neighbor)! >= dfnMap.get(vertex)!) {
458
+ // Found an articulation point
459
+ cutVertices.push(vertex);
460
+ }
461
+ } else if (neighbor !== parent) {
462
+ lowMap.set(vertex, Math.min(lowMap.get(vertex)!, dfnMap.get(neighbor)!));
463
+ }
464
+ }
465
+
466
+ if (parent === undefined && childCount > 1) {
467
+ // Special case for root in DFS tree
468
+ cutVertices.push(vertex);
469
+ }
470
+ };
471
+
472
+ for (const vertex of this.vertexMap.values()) {
473
+ if (!dfnMap.has(vertex)) {
474
+ dfs(vertex, undefined);
475
+ }
476
+ }
477
+
478
+ return {
479
+ dfnMap,
480
+ lowMap,
481
+ bridges,
482
+ cutVertices
483
+ };
484
+ }
485
+
486
+ /**
487
+ * Time Complexity: O(V + E)
488
+ * Space Complexity: O(V)
489
+ * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
490
+ * 1. Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time
491
+ */
492
+
493
+ /**
494
+ * The function "getBridges" returns an array of bridges in a graph using the Tarjan's algorithm.
495
+ * @returns The function `getBridges()` is returning the bridges found using the Tarjan's algorithm.
496
+ */
497
+ getBridges() {
498
+ return this.tarjan().bridges;
499
+ }
500
+
501
+ /**
502
+ * The function "getCutVertices" returns an array of cut vertices using the Tarjan's algorithm.
503
+ * @returns the cut vertices found using the Tarjan's algorithm.
504
+ */
505
+ getCutVertices() {
506
+ return this.tarjan().cutVertices;
507
+ }
508
+
509
+ /**
510
+ * The function returns the dfnMap property of the result of the tarjan() function.
511
+ * @returns the `dfnMap` property of the result of calling the `tarjan()` function.
512
+ */
513
+ getDFNMap() {
514
+ return this.tarjan().dfnMap;
515
+ }
516
+
517
+ /**
518
+ * The function returns the lowMap property of the result of the tarjan() function.
519
+ * @returns the lowMap property of the result of calling the tarjan() function.
520
+ */
521
+ getLowMap() {
522
+ return this.tarjan().lowMap;
523
+ }
524
+
373
525
  /**
374
526
  * Time Complexity: O(1)
375
527
  * Space Complexity: O(1)
@@ -379,7 +531,7 @@ export class UndirectedGraph<
379
531
  * @returns a boolean value.
380
532
  */
381
533
  protected _addEdge(edge: EO): boolean {
382
- for (const end of edge.vertexMap) {
534
+ for (const end of edge.endpoints) {
383
535
  const endVertex = this._getVertex(end);
384
536
  if (endVertex === undefined) return false;
385
537
  if (endVertex) {