priority-queue-typed 1.47.6 → 1.47.8
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.
- package/dist/data-structures/binary-tree/avl-tree.d.ts +40 -22
- package/dist/data-structures/binary-tree/avl-tree.js +45 -36
- package/dist/data-structures/binary-tree/binary-tree.d.ts +105 -113
- package/dist/data-structures/binary-tree/binary-tree.js +133 -119
- package/dist/data-structures/binary-tree/bst.d.ts +53 -44
- package/dist/data-structures/binary-tree/bst.js +137 -154
- package/dist/data-structures/binary-tree/rb-tree.d.ts +48 -15
- package/dist/data-structures/binary-tree/rb-tree.js +70 -33
- package/dist/data-structures/binary-tree/segment-tree.d.ts +6 -6
- package/dist/data-structures/binary-tree/segment-tree.js +7 -7
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +26 -37
- package/dist/data-structures/binary-tree/tree-multimap.js +58 -137
- package/dist/data-structures/graph/abstract-graph.d.ts +17 -17
- package/dist/data-structures/graph/abstract-graph.js +30 -30
- package/dist/data-structures/graph/directed-graph.d.ts +24 -24
- package/dist/data-structures/graph/directed-graph.js +28 -28
- package/dist/data-structures/graph/undirected-graph.d.ts +14 -14
- package/dist/data-structures/graph/undirected-graph.js +18 -18
- package/dist/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/data-structures/hash/hash-map.js +5 -8
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +28 -28
- package/dist/data-structures/linked-list/doubly-linked-list.js +33 -33
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +21 -21
- package/dist/data-structures/linked-list/singly-linked-list.js +27 -27
- package/dist/data-structures/linked-list/skip-linked-list.js +4 -4
- package/dist/data-structures/queue/queue.d.ts +13 -13
- package/dist/data-structures/queue/queue.js +13 -13
- package/dist/data-structures/stack/stack.d.ts +6 -6
- package/dist/data-structures/stack/stack.js +7 -7
- package/dist/data-structures/trie/trie.d.ts +3 -0
- package/dist/data-structures/trie/trie.js +19 -4
- package/dist/interfaces/binary-tree.d.ts +3 -3
- package/dist/types/common.d.ts +6 -1
- package/dist/types/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/types/data-structures/hash/hash-map.d.ts +1 -2
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +59 -39
- package/src/data-structures/binary-tree/binary-tree.ts +192 -180
- package/src/data-structures/binary-tree/bst.ts +157 -154
- package/src/data-structures/binary-tree/rb-tree.ts +78 -37
- package/src/data-structures/binary-tree/segment-tree.ts +10 -10
- package/src/data-structures/binary-tree/tree-multimap.ts +67 -145
- package/src/data-structures/graph/abstract-graph.ts +46 -46
- package/src/data-structures/graph/directed-graph.ts +40 -40
- package/src/data-structures/graph/undirected-graph.ts +26 -26
- package/src/data-structures/hash/hash-map.ts +8 -8
- package/src/data-structures/linked-list/doubly-linked-list.ts +45 -45
- package/src/data-structures/linked-list/singly-linked-list.ts +38 -38
- package/src/data-structures/linked-list/skip-linked-list.ts +4 -4
- package/src/data-structures/queue/queue.ts +13 -13
- package/src/data-structures/stack/stack.ts +9 -9
- package/src/data-structures/trie/trie.ts +23 -4
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +11 -1
- package/src/types/data-structures/graph/abstract-graph.ts +2 -2
- package/src/types/data-structures/hash/hash-map.ts +1 -2
|
@@ -119,18 +119,18 @@ export class DirectedGraph<
|
|
|
119
119
|
* Space Complexity: O(1)
|
|
120
120
|
*
|
|
121
121
|
* The `getEdge` function retrieves an edge between two vertices based on their source and destination IDs.
|
|
122
|
-
* @param {VO | VertexKey |
|
|
123
|
-
* @param {VO | VertexKey |
|
|
124
|
-
* destination vertex of the edge. It can be either a vertex object (`VO`), a vertex ID (`VertexKey`), or `
|
|
122
|
+
* @param {VO | VertexKey | undefined} srcOrKey - The source vertex or its ID. It can be either a vertex object or a vertex ID.
|
|
123
|
+
* @param {VO | VertexKey | undefined} destOrKey - The `destOrKey` parameter in the `getEdge` function represents the
|
|
124
|
+
* destination vertex of the edge. It can be either a vertex object (`VO`), a vertex ID (`VertexKey`), or `undefined` if the
|
|
125
125
|
* destination is not specified.
|
|
126
|
-
* @returns the first edge found between the source and destination vertices, or
|
|
126
|
+
* @returns the first edge found between the source and destination vertices, or undefined if no such edge is found.
|
|
127
127
|
*/
|
|
128
|
-
getEdge(srcOrKey: VO | VertexKey |
|
|
128
|
+
getEdge(srcOrKey: VO | VertexKey | undefined, destOrKey: VO | VertexKey | undefined): EO | undefined {
|
|
129
129
|
let edges: EO[] = [];
|
|
130
130
|
|
|
131
|
-
if (srcOrKey !==
|
|
132
|
-
const src: VO |
|
|
133
|
-
const dest: VO |
|
|
131
|
+
if (srcOrKey !== undefined && destOrKey !== undefined) {
|
|
132
|
+
const src: VO | undefined = this._getVertex(srcOrKey);
|
|
133
|
+
const dest: VO | undefined = this._getVertex(destOrKey);
|
|
134
134
|
|
|
135
135
|
if (src && dest) {
|
|
136
136
|
const srcOutEdges = this._outEdgeMap.get(src);
|
|
@@ -140,7 +140,7 @@ export class DirectedGraph<
|
|
|
140
140
|
}
|
|
141
141
|
}
|
|
142
142
|
|
|
143
|
-
return edges[0] ||
|
|
143
|
+
return edges[0] || undefined;
|
|
144
144
|
}
|
|
145
145
|
|
|
146
146
|
/**
|
|
@@ -155,14 +155,14 @@ export class DirectedGraph<
|
|
|
155
155
|
* The function removes an edge between two vertices in a graph and returns the removed edge.
|
|
156
156
|
* @param {VO | VertexKey} srcOrKey - The source vertex or its ID.
|
|
157
157
|
* @param {VO | VertexKey} destOrKey - The `destOrKey` parameter represents the destination vertex or its ID.
|
|
158
|
-
* @returns the removed edge (EO) if it exists, or
|
|
158
|
+
* @returns the removed edge (EO) if it exists, or undefined if either the source or destination vertex does not exist.
|
|
159
159
|
*/
|
|
160
|
-
deleteEdgeSrcToDest(srcOrKey: VO | VertexKey, destOrKey: VO | VertexKey): EO |
|
|
161
|
-
const src: VO |
|
|
162
|
-
const dest: VO |
|
|
163
|
-
let removed: EO |
|
|
160
|
+
deleteEdgeSrcToDest(srcOrKey: VO | VertexKey, destOrKey: VO | VertexKey): EO | undefined {
|
|
161
|
+
const src: VO | undefined = this._getVertex(srcOrKey);
|
|
162
|
+
const dest: VO | undefined = this._getVertex(destOrKey);
|
|
163
|
+
let removed: EO | undefined = undefined;
|
|
164
164
|
if (!src || !dest) {
|
|
165
|
-
return
|
|
165
|
+
return undefined;
|
|
166
166
|
}
|
|
167
167
|
|
|
168
168
|
const srcOutEdges = this._outEdgeMap.get(src);
|
|
@@ -172,7 +172,7 @@ export class DirectedGraph<
|
|
|
172
172
|
|
|
173
173
|
const destInEdges = this._inEdgeMap.get(dest);
|
|
174
174
|
if (destInEdges) {
|
|
175
|
-
removed = arrayRemove<EO>(destInEdges, (edge: EO) => edge.src === src.key)[0] ||
|
|
175
|
+
removed = arrayRemove<EO>(destInEdges, (edge: EO) => edge.src === src.key)[0] || undefined;
|
|
176
176
|
}
|
|
177
177
|
return removed;
|
|
178
178
|
}
|
|
@@ -186,13 +186,13 @@ export class DirectedGraph<
|
|
|
186
186
|
* Time Complexity: O(|E|) where |E| is the number of edges
|
|
187
187
|
* Space Complexity: O(1)
|
|
188
188
|
*
|
|
189
|
-
* The function removes an edge from a graph and returns the removed edge, or
|
|
189
|
+
* The function removes an edge from a graph and returns the removed edge, or undefined if the edge was not found.
|
|
190
190
|
* @param {EO} edge - The `edge` parameter is an object that represents an edge in a graph. It has two properties: `src`
|
|
191
191
|
* and `dest`, which represent the source and destination vertices of the edge, respectively.
|
|
192
|
-
* @returns The method `deleteEdge` returns the removed edge (`EO`) if it exists, or `
|
|
192
|
+
* @returns The method `deleteEdge` returns the removed edge (`EO`) if it exists, or `undefined` if the edge does not exist.
|
|
193
193
|
*/
|
|
194
|
-
deleteEdge(edge: EO): EO |
|
|
195
|
-
let removed: EO |
|
|
194
|
+
deleteEdge(edge: EO): EO | undefined {
|
|
195
|
+
let removed: EO | undefined = undefined;
|
|
196
196
|
const src = this._getVertex(edge.src);
|
|
197
197
|
const dest = this._getVertex(edge.dest);
|
|
198
198
|
if (src && dest) {
|
|
@@ -361,11 +361,11 @@ export class DirectedGraph<
|
|
|
361
361
|
* Time Complexity: O(1)
|
|
362
362
|
* Space Complexity: O(1)
|
|
363
363
|
*
|
|
364
|
-
* The function "getEdgeSrc" returns the source vertex of an edge, or
|
|
364
|
+
* The function "getEdgeSrc" returns the source vertex of an edge, or undefined if the edge does not exist.
|
|
365
365
|
* @param {EO} e - The parameter "e" is of type EO, which represents an edge in a graph.
|
|
366
|
-
* @returns either a vertex object (VO) or
|
|
366
|
+
* @returns either a vertex object (VO) or undefined.
|
|
367
367
|
*/
|
|
368
|
-
getEdgeSrc(e: EO): VO |
|
|
368
|
+
getEdgeSrc(e: EO): VO | undefined {
|
|
369
369
|
return this._getVertex(e.src);
|
|
370
370
|
}
|
|
371
371
|
|
|
@@ -380,9 +380,9 @@ export class DirectedGraph<
|
|
|
380
380
|
*
|
|
381
381
|
* The function "getEdgeDest" returns the destination vertex of an edge.
|
|
382
382
|
* @param {EO} e - The parameter "e" is of type "EO", which represents an edge in a graph.
|
|
383
|
-
* @returns either a vertex object of type VO or
|
|
383
|
+
* @returns either a vertex object of type VO or undefined.
|
|
384
384
|
*/
|
|
385
|
-
getEdgeDest(e: EO): VO |
|
|
385
|
+
getEdgeDest(e: EO): VO | undefined {
|
|
386
386
|
return this._getVertex(e.dest);
|
|
387
387
|
}
|
|
388
388
|
|
|
@@ -396,12 +396,12 @@ export class DirectedGraph<
|
|
|
396
396
|
* Space Complexity: O(1)
|
|
397
397
|
*
|
|
398
398
|
* The function `getDestinations` returns an array of destination vertices connected to a given vertex.
|
|
399
|
-
* @param {VO | VertexKey |
|
|
400
|
-
* find the destinations. It can be either a `VO` object, a `VertexKey` value, or `
|
|
399
|
+
* @param {VO | VertexKey | undefined} vertex - The `vertex` parameter represents the starting vertex from which we want to
|
|
400
|
+
* find the destinations. It can be either a `VO` object, a `VertexKey` value, or `undefined`.
|
|
401
401
|
* @returns an array of vertices (VO[]).
|
|
402
402
|
*/
|
|
403
|
-
getDestinations(vertex: VO | VertexKey |
|
|
404
|
-
if (vertex ===
|
|
403
|
+
getDestinations(vertex: VO | VertexKey | undefined): VO[] {
|
|
404
|
+
if (vertex === undefined) {
|
|
405
405
|
return [];
|
|
406
406
|
}
|
|
407
407
|
const destinations: VO[] = [];
|
|
@@ -425,13 +425,13 @@ export class DirectedGraph<
|
|
|
425
425
|
* Space Complexity: O(|V|)
|
|
426
426
|
*
|
|
427
427
|
* The `topologicalSort` function performs a topological sort on a graph and returns an array of vertices or vertex IDs
|
|
428
|
-
* in the sorted order, or
|
|
428
|
+
* in the sorted order, or undefined if the graph contains a cycle.
|
|
429
429
|
* @param {'vertex' | 'key'} [propertyName] - The `propertyName` parameter is an optional parameter that specifies the
|
|
430
430
|
* property to use for sorting the vertices. It can have two possible values: 'vertex' or 'key'. If 'vertex' is
|
|
431
431
|
* specified, the vertices themselves will be used for sorting. If 'key' is specified, the ids of
|
|
432
|
-
* @returns an array of vertices or vertex IDs in topological order. If there is a cycle in the graph, it returns
|
|
432
|
+
* @returns an array of vertices or vertex IDs in topological order. If there is a cycle in the graph, it returns undefined.
|
|
433
433
|
*/
|
|
434
|
-
topologicalSort(propertyName?: 'vertex' | 'key'): Array<VO | VertexKey> |
|
|
434
|
+
topologicalSort(propertyName?: 'vertex' | 'key'): Array<VO | VertexKey> | undefined {
|
|
435
435
|
propertyName = propertyName ?? 'key';
|
|
436
436
|
// When judging whether there is a cycle in the undirected graph, all nodes with degree of **<= 1** are enqueued
|
|
437
437
|
// When judging whether there is a cycle in the directed graph, all nodes with **in degree = 0** are enqueued
|
|
@@ -463,7 +463,7 @@ export class DirectedGraph<
|
|
|
463
463
|
}
|
|
464
464
|
}
|
|
465
465
|
|
|
466
|
-
if (hasCycle) return
|
|
466
|
+
if (hasCycle) return undefined;
|
|
467
467
|
|
|
468
468
|
if (propertyName === 'key') sorted = sorted.map(vertex => (vertex instanceof DirectedVertex ? vertex.key : vertex));
|
|
469
469
|
return sorted.reverse();
|
|
@@ -510,7 +510,7 @@ export class DirectedGraph<
|
|
|
510
510
|
const outEdges = this.outgoingEdgesOf(vertex);
|
|
511
511
|
for (const outEdge of outEdges) {
|
|
512
512
|
const neighbor = this._getVertex(outEdge.dest);
|
|
513
|
-
// TODO after no-non-
|
|
513
|
+
// TODO after no-non-undefined-assertion not ensure the logic
|
|
514
514
|
if (neighbor) {
|
|
515
515
|
neighbors.push(neighbor);
|
|
516
516
|
}
|
|
@@ -529,21 +529,21 @@ export class DirectedGraph<
|
|
|
529
529
|
* Space Complexity: O(1)
|
|
530
530
|
*
|
|
531
531
|
* The function "getEndsOfEdge" returns the source and destination vertices of an edge if it exists in the graph,
|
|
532
|
-
* otherwise it returns
|
|
532
|
+
* otherwise it returns undefined.
|
|
533
533
|
* @param {EO} edge - The parameter `edge` is of type `EO`, which represents an edge in a graph.
|
|
534
534
|
* @returns The function `getEndsOfEdge` returns an array containing two vertices `[VO, VO]` if the edge exists in the
|
|
535
|
-
* graph. If the edge does not exist, it returns `
|
|
535
|
+
* graph. If the edge does not exist, it returns `undefined`.
|
|
536
536
|
*/
|
|
537
|
-
getEndsOfEdge(edge: EO): [VO, VO] |
|
|
537
|
+
getEndsOfEdge(edge: EO): [VO, VO] | undefined {
|
|
538
538
|
if (!this.hasEdge(edge.src, edge.dest)) {
|
|
539
|
-
return
|
|
539
|
+
return undefined;
|
|
540
540
|
}
|
|
541
541
|
const v1 = this._getVertex(edge.src);
|
|
542
542
|
const v2 = this._getVertex(edge.dest);
|
|
543
543
|
if (v1 && v2) {
|
|
544
544
|
return [v1, v2];
|
|
545
545
|
} else {
|
|
546
|
-
return
|
|
546
|
+
return undefined;
|
|
547
547
|
}
|
|
548
548
|
}
|
|
549
549
|
|
|
@@ -570,7 +570,7 @@ export class DirectedGraph<
|
|
|
570
570
|
const srcVertex = this._getVertex(edge.src);
|
|
571
571
|
const destVertex = this._getVertex(edge.dest);
|
|
572
572
|
|
|
573
|
-
// TODO after no-non-
|
|
573
|
+
// TODO after no-non-undefined-assertion not ensure the logic
|
|
574
574
|
if (srcVertex && destVertex) {
|
|
575
575
|
const srcOutEdges = this._outEdgeMap.get(srcVertex);
|
|
576
576
|
if (srcOutEdges) {
|
|
@@ -100,26 +100,26 @@ export class UndirectedGraph<
|
|
|
100
100
|
* Time Complexity: O(|E|), where |E| is the number of edges incident to the given vertex.
|
|
101
101
|
* Space Complexity: O(1)
|
|
102
102
|
*
|
|
103
|
-
* The function `getEdge` returns the first edge that connects two vertices, or
|
|
104
|
-
* @param {VO | VertexKey |
|
|
105
|
-
* object), `
|
|
106
|
-
* @param {VO | VertexKey |
|
|
107
|
-
* object), `
|
|
108
|
-
* @returns an edge (EO) or
|
|
103
|
+
* The function `getEdge` returns the first edge that connects two vertices, or undefined if no such edge exists.
|
|
104
|
+
* @param {VO | VertexKey | undefined} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
105
|
+
* object), `undefined`, or `VertexKey` (a string or number representing the ID of a vertex).
|
|
106
|
+
* @param {VO | VertexKey | undefined} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
107
|
+
* object), `undefined`, or `VertexKey` (vertex ID).
|
|
108
|
+
* @returns an edge (EO) or undefined.
|
|
109
109
|
*/
|
|
110
|
-
getEdge(v1: VO | VertexKey |
|
|
110
|
+
getEdge(v1: VO | VertexKey | undefined, v2: VO | VertexKey | undefined): EO | undefined {
|
|
111
111
|
let edges: EO[] | undefined = [];
|
|
112
112
|
|
|
113
|
-
if (v1 !==
|
|
114
|
-
const vertex1: VO |
|
|
115
|
-
const vertex2: VO |
|
|
113
|
+
if (v1 !== undefined && v2 !== undefined) {
|
|
114
|
+
const vertex1: VO | undefined = this._getVertex(v1);
|
|
115
|
+
const vertex2: VO | undefined = this._getVertex(v2);
|
|
116
116
|
|
|
117
117
|
if (vertex1 && vertex2) {
|
|
118
118
|
edges = this._edges.get(vertex1)?.filter(e => e.vertices.includes(vertex2.key));
|
|
119
119
|
}
|
|
120
120
|
}
|
|
121
121
|
|
|
122
|
-
return edges ? edges[0] ||
|
|
122
|
+
return edges ? edges[0] || undefined : undefined;
|
|
123
123
|
}
|
|
124
124
|
|
|
125
125
|
/**
|
|
@@ -135,20 +135,20 @@ export class UndirectedGraph<
|
|
|
135
135
|
* @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
|
|
136
136
|
* @param {VO | VertexKey} v2 - VO | VertexKey - This parameter can be either a vertex object (VO) or a vertex ID
|
|
137
137
|
* (VertexKey). It represents the second vertex of the edge that needs to be removed.
|
|
138
|
-
* @returns the removed edge (EO) if it exists, or
|
|
138
|
+
* @returns the removed edge (EO) if it exists, or undefined if either of the vertices (VO) does not exist.
|
|
139
139
|
*/
|
|
140
|
-
deleteEdgeBetween(v1: VO | VertexKey, v2: VO | VertexKey): EO |
|
|
141
|
-
const vertex1: VO |
|
|
142
|
-
const vertex2: VO |
|
|
140
|
+
deleteEdgeBetween(v1: VO | VertexKey, v2: VO | VertexKey): EO | undefined {
|
|
141
|
+
const vertex1: VO | undefined = this._getVertex(v1);
|
|
142
|
+
const vertex2: VO | undefined = this._getVertex(v2);
|
|
143
143
|
|
|
144
144
|
if (!vertex1 || !vertex2) {
|
|
145
|
-
return
|
|
145
|
+
return undefined;
|
|
146
146
|
}
|
|
147
147
|
|
|
148
148
|
const v1Edges = this._edges.get(vertex1);
|
|
149
|
-
let removed: EO |
|
|
149
|
+
let removed: EO | undefined = undefined;
|
|
150
150
|
if (v1Edges) {
|
|
151
|
-
removed = arrayRemove<EO>(v1Edges, (e: EO) => e.vertices.includes(vertex2.key))[0] ||
|
|
151
|
+
removed = arrayRemove<EO>(v1Edges, (e: EO) => e.vertices.includes(vertex2.key))[0] || undefined;
|
|
152
152
|
}
|
|
153
153
|
const v2Edges = this._edges.get(vertex2);
|
|
154
154
|
if (v2Edges) {
|
|
@@ -168,9 +168,9 @@ export class UndirectedGraph<
|
|
|
168
168
|
*
|
|
169
169
|
* The deleteEdge function removes an edge between two vertices in a graph.
|
|
170
170
|
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
171
|
-
* @returns The method is returning either the removed edge (of type EO) or
|
|
171
|
+
* @returns The method is returning either the removed edge (of type EO) or undefined if the edge was not found.
|
|
172
172
|
*/
|
|
173
|
-
deleteEdge(edge: EO): EO |
|
|
173
|
+
deleteEdge(edge: EO): EO | undefined {
|
|
174
174
|
return this.deleteEdgeBetween(edge.vertices[0], edge.vertices[1]);
|
|
175
175
|
}
|
|
176
176
|
|
|
@@ -282,21 +282,21 @@ export class UndirectedGraph<
|
|
|
282
282
|
* Space Complexity: O(1)
|
|
283
283
|
*
|
|
284
284
|
* The function "getEndsOfEdge" returns the vertices at the ends of an edge if the edge exists in the graph, otherwise
|
|
285
|
-
* it returns
|
|
285
|
+
* it returns undefined.
|
|
286
286
|
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
287
287
|
* @returns The function `getEndsOfEdge` returns an array containing two vertices `[VO, VO]` if the edge exists in the
|
|
288
|
-
* graph. If the edge does not exist, it returns `
|
|
288
|
+
* graph. If the edge does not exist, it returns `undefined`.
|
|
289
289
|
*/
|
|
290
|
-
getEndsOfEdge(edge: EO): [VO, VO] |
|
|
290
|
+
getEndsOfEdge(edge: EO): [VO, VO] | undefined {
|
|
291
291
|
if (!this.hasEdge(edge.vertices[0], edge.vertices[1])) {
|
|
292
|
-
return
|
|
292
|
+
return undefined;
|
|
293
293
|
}
|
|
294
294
|
const v1 = this._getVertex(edge.vertices[0]);
|
|
295
295
|
const v2 = this._getVertex(edge.vertices[1]);
|
|
296
296
|
if (v1 && v2) {
|
|
297
297
|
return [v1, v2];
|
|
298
298
|
} else {
|
|
299
|
-
return
|
|
299
|
+
return undefined;
|
|
300
300
|
}
|
|
301
301
|
}
|
|
302
302
|
|
|
@@ -316,7 +316,7 @@ export class UndirectedGraph<
|
|
|
316
316
|
protected _addEdgeOnly(edge: EO): boolean {
|
|
317
317
|
for (const end of edge.vertices) {
|
|
318
318
|
const endVertex = this._getVertex(end);
|
|
319
|
-
if (endVertex ===
|
|
319
|
+
if (endVertex === undefined) return false;
|
|
320
320
|
if (endVertex) {
|
|
321
321
|
const edges = this._edges.get(endVertex);
|
|
322
322
|
if (edges) {
|
|
@@ -19,20 +19,16 @@ export class HashMap<K = any, V = any> {
|
|
|
19
19
|
protected _hashFn: (key: K) => string;
|
|
20
20
|
protected _objHashFn: (key: K) => object;
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
* `elements` property is an iterable that contains key-value pairs represented as arrays `[K, V]`.
|
|
26
|
-
*/
|
|
27
|
-
constructor(options: HashMapOptions<K, V> = {
|
|
28
|
-
elements: [],
|
|
22
|
+
|
|
23
|
+
constructor(elements?: Iterable<[K, V]>, options: HashMapOptions<K> = {
|
|
24
|
+
|
|
29
25
|
hashFn: (key: K) => String(key),
|
|
30
26
|
objHashFn: (key: K) => (<object>key)
|
|
31
27
|
}) {
|
|
32
28
|
this._sentinel = <HashMapLinkedNode<K, V>>{};
|
|
33
29
|
this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
|
|
34
30
|
|
|
35
|
-
const {
|
|
31
|
+
const { hashFn, objHashFn } = options;
|
|
36
32
|
this._hashFn = hashFn;
|
|
37
33
|
this._objHashFn = objHashFn;
|
|
38
34
|
if (elements) {
|
|
@@ -379,6 +375,10 @@ export class HashMap<K = any, V = any> {
|
|
|
379
375
|
}
|
|
380
376
|
}
|
|
381
377
|
|
|
378
|
+
print() {
|
|
379
|
+
console.log([...this]);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
382
|
/**
|
|
383
383
|
* Time Complexity: O(1)
|
|
384
384
|
* Space Complexity: O(1)
|
|
@@ -7,8 +7,8 @@
|
|
|
7
7
|
*/
|
|
8
8
|
export class DoublyLinkedListNode<E = any> {
|
|
9
9
|
value: E;
|
|
10
|
-
next: DoublyLinkedListNode<E> |
|
|
11
|
-
prev: DoublyLinkedListNode<E> |
|
|
10
|
+
next: DoublyLinkedListNode<E> | undefined;
|
|
11
|
+
prev: DoublyLinkedListNode<E> | undefined;
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* The constructor function initializes the value, next, and previous properties of an object.
|
|
@@ -17,8 +17,8 @@ export class DoublyLinkedListNode<E = any> {
|
|
|
17
17
|
*/
|
|
18
18
|
constructor(value: E) {
|
|
19
19
|
this.value = value;
|
|
20
|
-
this.next =
|
|
21
|
-
this.prev =
|
|
20
|
+
this.next = undefined;
|
|
21
|
+
this.prev = undefined;
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
|
|
@@ -27,8 +27,8 @@ export class DoublyLinkedList<E = any> {
|
|
|
27
27
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
28
28
|
*/
|
|
29
29
|
constructor(elements?: Iterable<E>) {
|
|
30
|
-
this._head =
|
|
31
|
-
this._tail =
|
|
30
|
+
this._head = undefined;
|
|
31
|
+
this._tail = undefined;
|
|
32
32
|
this._length = 0;
|
|
33
33
|
if (elements) {
|
|
34
34
|
for (const el of elements) {
|
|
@@ -37,15 +37,15 @@ export class DoublyLinkedList<E = any> {
|
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
protected _head: DoublyLinkedListNode<E> |
|
|
40
|
+
protected _head: DoublyLinkedListNode<E> | undefined;
|
|
41
41
|
|
|
42
|
-
get head(): DoublyLinkedListNode<E> |
|
|
42
|
+
get head(): DoublyLinkedListNode<E> | undefined {
|
|
43
43
|
return this._head;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
protected _tail: DoublyLinkedListNode<E> |
|
|
46
|
+
protected _tail: DoublyLinkedListNode<E> | undefined;
|
|
47
47
|
|
|
48
|
-
get tail(): DoublyLinkedListNode<E> |
|
|
48
|
+
get tail(): DoublyLinkedListNode<E> | undefined {
|
|
49
49
|
return this._tail;
|
|
50
50
|
}
|
|
51
51
|
|
|
@@ -133,17 +133,17 @@ export class DoublyLinkedList<E = any> {
|
|
|
133
133
|
*
|
|
134
134
|
* The `pop()` function removes and returns the value of the last node in a doubly linked list.
|
|
135
135
|
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
|
136
|
-
* list is empty, it returns
|
|
136
|
+
* list is empty, it returns undefined.
|
|
137
137
|
*/
|
|
138
138
|
pop(): E | undefined {
|
|
139
139
|
if (!this.tail) return undefined;
|
|
140
140
|
const removedNode = this.tail;
|
|
141
141
|
if (this.head === this.tail) {
|
|
142
|
-
this._head =
|
|
143
|
-
this._tail =
|
|
142
|
+
this._head = undefined;
|
|
143
|
+
this._tail = undefined;
|
|
144
144
|
} else {
|
|
145
145
|
this._tail = removedNode.prev;
|
|
146
|
-
this.tail!.next =
|
|
146
|
+
this.tail!.next = undefined;
|
|
147
147
|
}
|
|
148
148
|
this._length--;
|
|
149
149
|
return removedNode.value;
|
|
@@ -160,7 +160,7 @@ export class DoublyLinkedList<E = any> {
|
|
|
160
160
|
*
|
|
161
161
|
* The `popLast()` function removes and returns the value of the last node in a doubly linked list.
|
|
162
162
|
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
|
163
|
-
* list is empty, it returns
|
|
163
|
+
* list is empty, it returns undefined.
|
|
164
164
|
*/
|
|
165
165
|
popLast(): E | undefined {
|
|
166
166
|
return this.pop();
|
|
@@ -183,11 +183,11 @@ export class DoublyLinkedList<E = any> {
|
|
|
183
183
|
if (!this.head) return undefined;
|
|
184
184
|
const removedNode = this.head;
|
|
185
185
|
if (this.head === this.tail) {
|
|
186
|
-
this._head =
|
|
187
|
-
this._tail =
|
|
186
|
+
this._head = undefined;
|
|
187
|
+
this._tail = undefined;
|
|
188
188
|
} else {
|
|
189
189
|
this._head = removedNode.next;
|
|
190
|
-
this.head!.prev =
|
|
190
|
+
this.head!.prev = undefined;
|
|
191
191
|
}
|
|
192
192
|
this._length--;
|
|
193
193
|
return removedNode.value;
|
|
@@ -262,8 +262,8 @@ export class DoublyLinkedList<E = any> {
|
|
|
262
262
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
263
263
|
* Space Complexity: O(1)
|
|
264
264
|
*
|
|
265
|
-
* The `getFirst` function returns the first node in a doubly linked list, or
|
|
266
|
-
* @returns The method `getFirst()` returns the first node of the doubly linked list, or `
|
|
265
|
+
* The `getFirst` function returns the first node in a doubly linked list, or undefined if the list is empty.
|
|
266
|
+
* @returns The method `getFirst()` returns the first node of the doubly linked list, or `undefined` if the list is empty.
|
|
267
267
|
*/
|
|
268
268
|
getFirst(): E | undefined {
|
|
269
269
|
return this.head?.value;
|
|
@@ -278,8 +278,8 @@ export class DoublyLinkedList<E = any> {
|
|
|
278
278
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
279
279
|
* Space Complexity: O(1)
|
|
280
280
|
*
|
|
281
|
-
* The `getLast` function returns the last node in a doubly linked list, or
|
|
282
|
-
* @returns The method `getLast()` returns the last node of the doubly linked list, or `
|
|
281
|
+
* The `getLast` function returns the last node in a doubly linked list, or undefined if the list is empty.
|
|
282
|
+
* @returns The method `getLast()` returns the last node of the doubly linked list, or `undefined` if the list is empty.
|
|
283
283
|
*/
|
|
284
284
|
getLast(): E | undefined {
|
|
285
285
|
return this.tail?.value;
|
|
@@ -294,11 +294,11 @@ export class DoublyLinkedList<E = any> {
|
|
|
294
294
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
295
295
|
* Space Complexity: O(1)
|
|
296
296
|
*
|
|
297
|
-
* The `getAt` function returns the value at a specified index in a linked list, or
|
|
297
|
+
* The `getAt` function returns the value at a specified index in a linked list, or undefined if the index is out of bounds.
|
|
298
298
|
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
|
299
299
|
* retrieve from the list.
|
|
300
300
|
* @returns The method is returning the value at the specified index in the linked list. If the index is out of bounds
|
|
301
|
-
* or the linked list is empty, it will return
|
|
301
|
+
* or the linked list is empty, it will return undefined.
|
|
302
302
|
*/
|
|
303
303
|
getAt(index: number): E | undefined {
|
|
304
304
|
if (index < 0 || index >= this.length) return undefined;
|
|
@@ -318,15 +318,15 @@ export class DoublyLinkedList<E = any> {
|
|
|
318
318
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
319
319
|
* Space Complexity: O(1)
|
|
320
320
|
*
|
|
321
|
-
* The function `getNodeAt` returns the node at a given index in a doubly linked list, or
|
|
321
|
+
* The function `getNodeAt` returns the node at a given index in a doubly linked list, or undefined if the index is out of
|
|
322
322
|
* range.
|
|
323
323
|
* @param {number} index - The `index` parameter is a number that represents the position of the node we want to
|
|
324
324
|
* retrieve from the doubly linked list. It indicates the zero-based index of the node we want to access.
|
|
325
325
|
* @returns The method `getNodeAt(index: number)` returns a `DoublyLinkedListNode<E>` object if the index is within the
|
|
326
|
-
* valid range of the linked list, otherwise it returns `
|
|
326
|
+
* valid range of the linked list, otherwise it returns `undefined`.
|
|
327
327
|
*/
|
|
328
|
-
getNodeAt(index: number): DoublyLinkedListNode<E> |
|
|
329
|
-
if (index < 0 || index >= this.length) return
|
|
328
|
+
getNodeAt(index: number): DoublyLinkedListNode<E> | undefined {
|
|
329
|
+
if (index < 0 || index >= this.length) return undefined;
|
|
330
330
|
let current = this.head;
|
|
331
331
|
for (let i = 0; i < index; i++) {
|
|
332
332
|
current = current!.next;
|
|
@@ -344,12 +344,12 @@ export class DoublyLinkedList<E = any> {
|
|
|
344
344
|
* Space Complexity: O(1)
|
|
345
345
|
*
|
|
346
346
|
* The function `findNodeByValue` searches for a node with a specific value in a doubly linked list and returns the
|
|
347
|
-
* node if found, otherwise it returns
|
|
347
|
+
* node if found, otherwise it returns undefined.
|
|
348
348
|
* @param {E} value - The `value` parameter is the value that we want to search for in the doubly linked list.
|
|
349
349
|
* @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<E>` if a node with the specified value `value`
|
|
350
|
-
* is found in the linked list. If no such node is found, it returns `
|
|
350
|
+
* is found in the linked list. If no such node is found, it returns `undefined`.
|
|
351
351
|
*/
|
|
352
|
-
getNode(value: E |
|
|
352
|
+
getNode(value: E | undefined): DoublyLinkedListNode<E> | undefined {
|
|
353
353
|
let current = this.head;
|
|
354
354
|
|
|
355
355
|
while (current) {
|
|
@@ -359,7 +359,7 @@ export class DoublyLinkedList<E = any> {
|
|
|
359
359
|
current = current.next;
|
|
360
360
|
}
|
|
361
361
|
|
|
362
|
-
return
|
|
362
|
+
return undefined;
|
|
363
363
|
}
|
|
364
364
|
|
|
365
365
|
/**
|
|
@@ -502,7 +502,7 @@ export class DoublyLinkedList<E = any> {
|
|
|
502
502
|
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
|
|
503
503
|
* @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
|
|
504
504
|
* data structure. It is of type number.
|
|
505
|
-
* @returns The method `deleteAt` returns the value of the node that was deleted, or `
|
|
505
|
+
* @returns The method `deleteAt` returns the value of the node that was deleted, or `undefined` if the index is out of
|
|
506
506
|
* bounds.
|
|
507
507
|
*/
|
|
508
508
|
deleteAt(index: number): E | undefined {
|
|
@@ -534,8 +534,8 @@ export class DoublyLinkedList<E = any> {
|
|
|
534
534
|
* @returns The `delete` method returns a boolean value. It returns `true` if the value or node was successfully
|
|
535
535
|
* deleted from the doubly linked list, and `false` if the value or node was not found in the list.
|
|
536
536
|
*/
|
|
537
|
-
delete(valOrNode: E | DoublyLinkedListNode<E> |
|
|
538
|
-
let node: DoublyLinkedListNode<E> |
|
|
537
|
+
delete(valOrNode: E | DoublyLinkedListNode<E> | undefined): boolean {
|
|
538
|
+
let node: DoublyLinkedListNode<E> | undefined;
|
|
539
539
|
|
|
540
540
|
if (valOrNode instanceof DoublyLinkedListNode) {
|
|
541
541
|
node = valOrNode;
|
|
@@ -569,11 +569,11 @@ export class DoublyLinkedList<E = any> {
|
|
|
569
569
|
}
|
|
570
570
|
|
|
571
571
|
/**
|
|
572
|
-
* The `clear` function resets the linked list by setting the head, tail, and length to
|
|
572
|
+
* The `clear` function resets the linked list by setting the head, tail, and length to undefined and 0 respectively.
|
|
573
573
|
*/
|
|
574
574
|
clear(): void {
|
|
575
|
-
this._head =
|
|
576
|
-
this._tail =
|
|
575
|
+
this._head = undefined;
|
|
576
|
+
this._tail = undefined;
|
|
577
577
|
this._length = 0;
|
|
578
578
|
}
|
|
579
579
|
|
|
@@ -590,9 +590,9 @@ export class DoublyLinkedList<E = any> {
|
|
|
590
590
|
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
|
|
591
591
|
* function is used to determine whether a particular value in the linked list satisfies a certain condition.
|
|
592
592
|
* @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
|
|
593
|
-
* the callback function. If no element satisfies the condition, it returns `
|
|
593
|
+
* the callback function. If no element satisfies the condition, it returns `undefined`.
|
|
594
594
|
*/
|
|
595
|
-
find(callback: (value: E) => boolean): E |
|
|
595
|
+
find(callback: (value: E) => boolean): E | undefined {
|
|
596
596
|
let current = this.head;
|
|
597
597
|
while (current) {
|
|
598
598
|
if (callback(current.value)) {
|
|
@@ -600,7 +600,7 @@ export class DoublyLinkedList<E = any> {
|
|
|
600
600
|
}
|
|
601
601
|
current = current.next;
|
|
602
602
|
}
|
|
603
|
-
return
|
|
603
|
+
return undefined;
|
|
604
604
|
}
|
|
605
605
|
|
|
606
606
|
/**
|
|
@@ -641,13 +641,13 @@ export class DoublyLinkedList<E = any> {
|
|
|
641
641
|
* Space Complexity: O(1)
|
|
642
642
|
*
|
|
643
643
|
* The `findBackward` function iterates through a linked list from the last node to the first node and returns the last
|
|
644
|
-
* value that satisfies the given callback function, or
|
|
644
|
+
* value that satisfies the given callback function, or undefined if no value satisfies the callback.
|
|
645
645
|
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
|
|
646
646
|
* function is used to determine whether a given value satisfies a certain condition.
|
|
647
647
|
* @returns The method `findBackward` returns the last value in the linked list that satisfies the condition specified by
|
|
648
|
-
* the callback function. If no value satisfies the condition, it returns `
|
|
648
|
+
* the callback function. If no value satisfies the condition, it returns `undefined`.
|
|
649
649
|
*/
|
|
650
|
-
findBackward(callback: (value: E) => boolean): E |
|
|
650
|
+
findBackward(callback: (value: E) => boolean): E | undefined {
|
|
651
651
|
let current = this.tail;
|
|
652
652
|
while (current) {
|
|
653
653
|
if (callback(current.value)) {
|
|
@@ -655,7 +655,7 @@ export class DoublyLinkedList<E = any> {
|
|
|
655
655
|
}
|
|
656
656
|
current = current.prev;
|
|
657
657
|
}
|
|
658
|
-
return
|
|
658
|
+
return undefined;
|
|
659
659
|
}
|
|
660
660
|
|
|
661
661
|
/**
|