data-structure-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/.github/ISSUE_TEMPLATE/bug_report.md +10 -7
- package/.github/workflows/ci.yml +1 -1
- package/.github/workflows/release-package.yml +1 -1
- package/CHANGELOG.md +1 -1
- package/CODE_OF_CONDUCT.md +32 -10
- package/COMMANDS.md +3 -1
- package/CONTRIBUTING.md +4 -3
- package/README.md +188 -32
- package/SECURITY.md +1 -1
- package/benchmark/report.html +46 -1
- package/benchmark/report.json +563 -8
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +40 -22
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +45 -36
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +105 -113
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +133 -119
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +53 -44
- package/dist/cjs/data-structures/binary-tree/bst.js +137 -154
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +48 -15
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +70 -33
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/segment-tree.d.ts +6 -6
- package/dist/cjs/data-structures/binary-tree/segment-tree.js +7 -7
- package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +26 -37
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +58 -137
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +17 -17
- package/dist/cjs/data-structures/graph/abstract-graph.js +30 -30
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +24 -24
- package/dist/cjs/data-structures/graph/directed-graph.js +28 -28
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +14 -14
- package/dist/cjs/data-structures/graph/undirected-graph.js +18 -18
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/cjs/data-structures/hash/hash-map.js +5 -8
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +28 -28
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +33 -33
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +21 -21
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +27 -27
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +4 -4
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +13 -13
- package/dist/cjs/data-structures/queue/queue.js +13 -13
- package/dist/cjs/data-structures/stack/stack.d.ts +6 -6
- package/dist/cjs/data-structures/stack/stack.js +7 -7
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +3 -0
- package/dist/cjs/data-structures/trie/trie.js +19 -4
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/cjs/types/common.d.ts +6 -1
- package/dist/cjs/types/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +1 -2
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +40 -22
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +45 -36
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +105 -113
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +133 -128
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +53 -44
- package/dist/mjs/data-structures/binary-tree/bst.js +137 -154
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +48 -15
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +70 -33
- package/dist/mjs/data-structures/binary-tree/segment-tree.d.ts +6 -6
- package/dist/mjs/data-structures/binary-tree/segment-tree.js +7 -7
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +26 -37
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +59 -138
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +17 -17
- package/dist/mjs/data-structures/graph/abstract-graph.js +30 -30
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +24 -24
- package/dist/mjs/data-structures/graph/directed-graph.js +28 -28
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +14 -14
- package/dist/mjs/data-structures/graph/undirected-graph.js +18 -18
- package/dist/mjs/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/mjs/data-structures/hash/hash-map.js +5 -8
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +28 -28
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +33 -33
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +21 -21
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +27 -27
- package/dist/mjs/data-structures/linked-list/skip-linked-list.js +4 -4
- package/dist/mjs/data-structures/queue/queue.d.ts +13 -13
- package/dist/mjs/data-structures/queue/queue.js +13 -13
- package/dist/mjs/data-structures/stack/stack.d.ts +6 -6
- package/dist/mjs/data-structures/stack/stack.js +7 -7
- package/dist/mjs/data-structures/trie/trie.d.ts +3 -0
- package/dist/mjs/data-structures/trie/trie.js +20 -4
- package/dist/mjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/mjs/types/common.d.ts +6 -1
- package/dist/mjs/types/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +1 -2
- package/dist/umd/data-structure-typed.js +583 -627
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- 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
- package/test/integration/{all-in-one.ts → all-in-one.test.ts} +1 -1
- package/test/integration/index.html +158 -2
- package/test/performance/data-structures/comparison/comparison.test.ts +5 -5
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +1 -1
- package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +19 -19
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +50 -51
- package/test/unit/data-structures/binary-tree/bst.test.ts +49 -54
- package/test/unit/data-structures/binary-tree/overall.test.ts +17 -18
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/segment-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +118 -66
- package/test/unit/data-structures/graph/abstract-graph.test.ts +4 -4
- package/test/unit/data-structures/graph/directed-graph.test.ts +10 -10
- package/test/unit/data-structures/graph/undirected-graph.test.ts +3 -3
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +14 -14
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +3 -3
- package/test/unit/data-structures/linked-list/skip-list.test.ts +1 -1
- package/test/unit/data-structures/queue/deque.test.ts +1 -1
- package/test/unit/data-structures/stack/stack.test.ts +2 -2
- package/test/unit/unrestricted-interconversion.test.ts +61 -5
- package/tsconfig-cjs.json +1 -1
|
@@ -74,13 +74,13 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
74
74
|
* Space Complexity: O(1)
|
|
75
75
|
*
|
|
76
76
|
* The `getEdge` function retrieves an edge between two vertices based on their source and destination IDs.
|
|
77
|
-
* @param {VO | VertexKey |
|
|
78
|
-
* @param {VO | VertexKey |
|
|
79
|
-
* destination vertex of the edge. It can be either a vertex object (`VO`), a vertex ID (`VertexKey`), or `
|
|
77
|
+
* @param {VO | VertexKey | undefined} srcOrKey - The source vertex or its ID. It can be either a vertex object or a vertex ID.
|
|
78
|
+
* @param {VO | VertexKey | undefined} destOrKey - The `destOrKey` parameter in the `getEdge` function represents the
|
|
79
|
+
* destination vertex of the edge. It can be either a vertex object (`VO`), a vertex ID (`VertexKey`), or `undefined` if the
|
|
80
80
|
* destination is not specified.
|
|
81
|
-
* @returns the first edge found between the source and destination vertices, or
|
|
81
|
+
* @returns the first edge found between the source and destination vertices, or undefined if no such edge is found.
|
|
82
82
|
*/
|
|
83
|
-
getEdge(srcOrKey: VO | VertexKey |
|
|
83
|
+
getEdge(srcOrKey: VO | VertexKey | undefined, destOrKey: VO | VertexKey | undefined): EO | undefined;
|
|
84
84
|
/**
|
|
85
85
|
* Time Complexity: O(|E|) where |E| is the number of edges
|
|
86
86
|
* Space Complexity: O(1)
|
|
@@ -92,9 +92,9 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
92
92
|
* The function removes an edge between two vertices in a graph and returns the removed edge.
|
|
93
93
|
* @param {VO | VertexKey} srcOrKey - The source vertex or its ID.
|
|
94
94
|
* @param {VO | VertexKey} destOrKey - The `destOrKey` parameter represents the destination vertex or its ID.
|
|
95
|
-
* @returns the removed edge (EO) if it exists, or
|
|
95
|
+
* @returns the removed edge (EO) if it exists, or undefined if either the source or destination vertex does not exist.
|
|
96
96
|
*/
|
|
97
|
-
deleteEdgeSrcToDest(srcOrKey: VO | VertexKey, destOrKey: VO | VertexKey): EO |
|
|
97
|
+
deleteEdgeSrcToDest(srcOrKey: VO | VertexKey, destOrKey: VO | VertexKey): EO | undefined;
|
|
98
98
|
/**
|
|
99
99
|
* Time Complexity: O(|E|) where |E| is the number of edges
|
|
100
100
|
* Space Complexity: O(1)
|
|
@@ -103,12 +103,12 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
103
103
|
* Time Complexity: O(|E|) where |E| is the number of edges
|
|
104
104
|
* Space Complexity: O(1)
|
|
105
105
|
*
|
|
106
|
-
* The function removes an edge from a graph and returns the removed edge, or
|
|
106
|
+
* The function removes an edge from a graph and returns the removed edge, or undefined if the edge was not found.
|
|
107
107
|
* @param {EO} edge - The `edge` parameter is an object that represents an edge in a graph. It has two properties: `src`
|
|
108
108
|
* and `dest`, which represent the source and destination vertices of the edge, respectively.
|
|
109
|
-
* @returns The method `deleteEdge` returns the removed edge (`EO`) if it exists, or `
|
|
109
|
+
* @returns The method `deleteEdge` returns the removed edge (`EO`) if it exists, or `undefined` if the edge does not exist.
|
|
110
110
|
*/
|
|
111
|
-
deleteEdge(edge: EO): EO |
|
|
111
|
+
deleteEdge(edge: EO): EO | undefined;
|
|
112
112
|
/**
|
|
113
113
|
* Time Complexity: O(|E|) where |E| is the number of edges
|
|
114
114
|
* Space Complexity: O(1)
|
|
@@ -213,11 +213,11 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
213
213
|
* Time Complexity: O(1)
|
|
214
214
|
* Space Complexity: O(1)
|
|
215
215
|
*
|
|
216
|
-
* The function "getEdgeSrc" returns the source vertex of an edge, or
|
|
216
|
+
* The function "getEdgeSrc" returns the source vertex of an edge, or undefined if the edge does not exist.
|
|
217
217
|
* @param {EO} e - The parameter "e" is of type EO, which represents an edge in a graph.
|
|
218
|
-
* @returns either a vertex object (VO) or
|
|
218
|
+
* @returns either a vertex object (VO) or undefined.
|
|
219
219
|
*/
|
|
220
|
-
getEdgeSrc(e: EO): VO |
|
|
220
|
+
getEdgeSrc(e: EO): VO | undefined;
|
|
221
221
|
/**
|
|
222
222
|
* Time Complexity: O(1)
|
|
223
223
|
* Space Complexity: O(1)
|
|
@@ -228,9 +228,9 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
228
228
|
*
|
|
229
229
|
* The function "getEdgeDest" returns the destination vertex of an edge.
|
|
230
230
|
* @param {EO} e - The parameter "e" is of type "EO", which represents an edge in a graph.
|
|
231
|
-
* @returns either a vertex object of type VO or
|
|
231
|
+
* @returns either a vertex object of type VO or undefined.
|
|
232
232
|
*/
|
|
233
|
-
getEdgeDest(e: EO): VO |
|
|
233
|
+
getEdgeDest(e: EO): VO | undefined;
|
|
234
234
|
/**
|
|
235
235
|
* Time Complexity: O(|E|) where |E| is the number of edges
|
|
236
236
|
* Space Complexity: O(1)
|
|
@@ -240,11 +240,11 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
240
240
|
* Space Complexity: O(1)
|
|
241
241
|
*
|
|
242
242
|
* The function `getDestinations` returns an array of destination vertices connected to a given vertex.
|
|
243
|
-
* @param {VO | VertexKey |
|
|
244
|
-
* find the destinations. It can be either a `VO` object, a `VertexKey` value, or `
|
|
243
|
+
* @param {VO | VertexKey | undefined} vertex - The `vertex` parameter represents the starting vertex from which we want to
|
|
244
|
+
* find the destinations. It can be either a `VO` object, a `VertexKey` value, or `undefined`.
|
|
245
245
|
* @returns an array of vertices (VO[]).
|
|
246
246
|
*/
|
|
247
|
-
getDestinations(vertex: VO | VertexKey |
|
|
247
|
+
getDestinations(vertex: VO | VertexKey | undefined): VO[];
|
|
248
248
|
/**
|
|
249
249
|
* Time Complexity: O(|V| + |E|) where |V| is the number of vertices and |E| is the number of edges
|
|
250
250
|
* Space Complexity: O(|V|)
|
|
@@ -254,13 +254,13 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
254
254
|
* Space Complexity: O(|V|)
|
|
255
255
|
*
|
|
256
256
|
* The `topologicalSort` function performs a topological sort on a graph and returns an array of vertices or vertex IDs
|
|
257
|
-
* in the sorted order, or
|
|
257
|
+
* in the sorted order, or undefined if the graph contains a cycle.
|
|
258
258
|
* @param {'vertex' | 'key'} [propertyName] - The `propertyName` parameter is an optional parameter that specifies the
|
|
259
259
|
* property to use for sorting the vertices. It can have two possible values: 'vertex' or 'key'. If 'vertex' is
|
|
260
260
|
* specified, the vertices themselves will be used for sorting. If 'key' is specified, the ids of
|
|
261
|
-
* @returns an array of vertices or vertex IDs in topological order. If there is a cycle in the graph, it returns
|
|
261
|
+
* @returns an array of vertices or vertex IDs in topological order. If there is a cycle in the graph, it returns undefined.
|
|
262
262
|
*/
|
|
263
|
-
topologicalSort(propertyName?: 'vertex' | 'key'): Array<VO | VertexKey> |
|
|
263
|
+
topologicalSort(propertyName?: 'vertex' | 'key'): Array<VO | VertexKey> | undefined;
|
|
264
264
|
/**
|
|
265
265
|
* Time Complexity: O(|E|) where |E| is the number of edges
|
|
266
266
|
* Space Complexity: O(|E|)
|
|
@@ -296,12 +296,12 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
296
296
|
* Space Complexity: O(1)
|
|
297
297
|
*
|
|
298
298
|
* The function "getEndsOfEdge" returns the source and destination vertices of an edge if it exists in the graph,
|
|
299
|
-
* otherwise it returns
|
|
299
|
+
* otherwise it returns undefined.
|
|
300
300
|
* @param {EO} edge - The parameter `edge` is of type `EO`, which represents an edge in a graph.
|
|
301
301
|
* @returns The function `getEndsOfEdge` returns an array containing two vertices `[VO, VO]` if the edge exists in the
|
|
302
|
-
* graph. If the edge does not exist, it returns `
|
|
302
|
+
* graph. If the edge does not exist, it returns `undefined`.
|
|
303
303
|
*/
|
|
304
|
-
getEndsOfEdge(edge: EO): [VO, VO] |
|
|
304
|
+
getEndsOfEdge(edge: EO): [VO, VO] | undefined;
|
|
305
305
|
/**
|
|
306
306
|
* Time Complexity: O(1)
|
|
307
307
|
* Space Complexity: O(1)
|
|
@@ -96,15 +96,15 @@ export class DirectedGraph extends AbstractGraph {
|
|
|
96
96
|
* Space Complexity: O(1)
|
|
97
97
|
*
|
|
98
98
|
* The `getEdge` function retrieves an edge between two vertices based on their source and destination IDs.
|
|
99
|
-
* @param {VO | VertexKey |
|
|
100
|
-
* @param {VO | VertexKey |
|
|
101
|
-
* destination vertex of the edge. It can be either a vertex object (`VO`), a vertex ID (`VertexKey`), or `
|
|
99
|
+
* @param {VO | VertexKey | undefined} srcOrKey - The source vertex or its ID. It can be either a vertex object or a vertex ID.
|
|
100
|
+
* @param {VO | VertexKey | undefined} destOrKey - The `destOrKey` parameter in the `getEdge` function represents the
|
|
101
|
+
* destination vertex of the edge. It can be either a vertex object (`VO`), a vertex ID (`VertexKey`), or `undefined` if the
|
|
102
102
|
* destination is not specified.
|
|
103
|
-
* @returns the first edge found between the source and destination vertices, or
|
|
103
|
+
* @returns the first edge found between the source and destination vertices, or undefined if no such edge is found.
|
|
104
104
|
*/
|
|
105
105
|
getEdge(srcOrKey, destOrKey) {
|
|
106
106
|
let edges = [];
|
|
107
|
-
if (srcOrKey !==
|
|
107
|
+
if (srcOrKey !== undefined && destOrKey !== undefined) {
|
|
108
108
|
const src = this._getVertex(srcOrKey);
|
|
109
109
|
const dest = this._getVertex(destOrKey);
|
|
110
110
|
if (src && dest) {
|
|
@@ -114,7 +114,7 @@ export class DirectedGraph extends AbstractGraph {
|
|
|
114
114
|
}
|
|
115
115
|
}
|
|
116
116
|
}
|
|
117
|
-
return edges[0] ||
|
|
117
|
+
return edges[0] || undefined;
|
|
118
118
|
}
|
|
119
119
|
/**
|
|
120
120
|
* Time Complexity: O(|E|) where |E| is the number of edges
|
|
@@ -127,14 +127,14 @@ export class DirectedGraph extends AbstractGraph {
|
|
|
127
127
|
* The function removes an edge between two vertices in a graph and returns the removed edge.
|
|
128
128
|
* @param {VO | VertexKey} srcOrKey - The source vertex or its ID.
|
|
129
129
|
* @param {VO | VertexKey} destOrKey - The `destOrKey` parameter represents the destination vertex or its ID.
|
|
130
|
-
* @returns the removed edge (EO) if it exists, or
|
|
130
|
+
* @returns the removed edge (EO) if it exists, or undefined if either the source or destination vertex does not exist.
|
|
131
131
|
*/
|
|
132
132
|
deleteEdgeSrcToDest(srcOrKey, destOrKey) {
|
|
133
133
|
const src = this._getVertex(srcOrKey);
|
|
134
134
|
const dest = this._getVertex(destOrKey);
|
|
135
|
-
let removed =
|
|
135
|
+
let removed = undefined;
|
|
136
136
|
if (!src || !dest) {
|
|
137
|
-
return
|
|
137
|
+
return undefined;
|
|
138
138
|
}
|
|
139
139
|
const srcOutEdges = this._outEdgeMap.get(src);
|
|
140
140
|
if (srcOutEdges) {
|
|
@@ -142,7 +142,7 @@ export class DirectedGraph extends AbstractGraph {
|
|
|
142
142
|
}
|
|
143
143
|
const destInEdges = this._inEdgeMap.get(dest);
|
|
144
144
|
if (destInEdges) {
|
|
145
|
-
removed = arrayRemove(destInEdges, (edge) => edge.src === src.key)[0] ||
|
|
145
|
+
removed = arrayRemove(destInEdges, (edge) => edge.src === src.key)[0] || undefined;
|
|
146
146
|
}
|
|
147
147
|
return removed;
|
|
148
148
|
}
|
|
@@ -154,13 +154,13 @@ export class DirectedGraph extends AbstractGraph {
|
|
|
154
154
|
* Time Complexity: O(|E|) where |E| is the number of edges
|
|
155
155
|
* Space Complexity: O(1)
|
|
156
156
|
*
|
|
157
|
-
* The function removes an edge from a graph and returns the removed edge, or
|
|
157
|
+
* The function removes an edge from a graph and returns the removed edge, or undefined if the edge was not found.
|
|
158
158
|
* @param {EO} edge - The `edge` parameter is an object that represents an edge in a graph. It has two properties: `src`
|
|
159
159
|
* and `dest`, which represent the source and destination vertices of the edge, respectively.
|
|
160
|
-
* @returns The method `deleteEdge` returns the removed edge (`EO`) if it exists, or `
|
|
160
|
+
* @returns The method `deleteEdge` returns the removed edge (`EO`) if it exists, or `undefined` if the edge does not exist.
|
|
161
161
|
*/
|
|
162
162
|
deleteEdge(edge) {
|
|
163
|
-
let removed =
|
|
163
|
+
let removed = undefined;
|
|
164
164
|
const src = this._getVertex(edge.src);
|
|
165
165
|
const dest = this._getVertex(edge.dest);
|
|
166
166
|
if (src && dest) {
|
|
@@ -308,9 +308,9 @@ export class DirectedGraph extends AbstractGraph {
|
|
|
308
308
|
* Time Complexity: O(1)
|
|
309
309
|
* Space Complexity: O(1)
|
|
310
310
|
*
|
|
311
|
-
* The function "getEdgeSrc" returns the source vertex of an edge, or
|
|
311
|
+
* The function "getEdgeSrc" returns the source vertex of an edge, or undefined if the edge does not exist.
|
|
312
312
|
* @param {EO} e - The parameter "e" is of type EO, which represents an edge in a graph.
|
|
313
|
-
* @returns either a vertex object (VO) or
|
|
313
|
+
* @returns either a vertex object (VO) or undefined.
|
|
314
314
|
*/
|
|
315
315
|
getEdgeSrc(e) {
|
|
316
316
|
return this._getVertex(e.src);
|
|
@@ -325,7 +325,7 @@ export class DirectedGraph extends AbstractGraph {
|
|
|
325
325
|
*
|
|
326
326
|
* The function "getEdgeDest" returns the destination vertex of an edge.
|
|
327
327
|
* @param {EO} e - The parameter "e" is of type "EO", which represents an edge in a graph.
|
|
328
|
-
* @returns either a vertex object of type VO or
|
|
328
|
+
* @returns either a vertex object of type VO or undefined.
|
|
329
329
|
*/
|
|
330
330
|
getEdgeDest(e) {
|
|
331
331
|
return this._getVertex(e.dest);
|
|
@@ -339,12 +339,12 @@ export class DirectedGraph extends AbstractGraph {
|
|
|
339
339
|
* Space Complexity: O(1)
|
|
340
340
|
*
|
|
341
341
|
* The function `getDestinations` returns an array of destination vertices connected to a given vertex.
|
|
342
|
-
* @param {VO | VertexKey |
|
|
343
|
-
* find the destinations. It can be either a `VO` object, a `VertexKey` value, or `
|
|
342
|
+
* @param {VO | VertexKey | undefined} vertex - The `vertex` parameter represents the starting vertex from which we want to
|
|
343
|
+
* find the destinations. It can be either a `VO` object, a `VertexKey` value, or `undefined`.
|
|
344
344
|
* @returns an array of vertices (VO[]).
|
|
345
345
|
*/
|
|
346
346
|
getDestinations(vertex) {
|
|
347
|
-
if (vertex ===
|
|
347
|
+
if (vertex === undefined) {
|
|
348
348
|
return [];
|
|
349
349
|
}
|
|
350
350
|
const destinations = [];
|
|
@@ -366,11 +366,11 @@ export class DirectedGraph extends AbstractGraph {
|
|
|
366
366
|
* Space Complexity: O(|V|)
|
|
367
367
|
*
|
|
368
368
|
* The `topologicalSort` function performs a topological sort on a graph and returns an array of vertices or vertex IDs
|
|
369
|
-
* in the sorted order, or
|
|
369
|
+
* in the sorted order, or undefined if the graph contains a cycle.
|
|
370
370
|
* @param {'vertex' | 'key'} [propertyName] - The `propertyName` parameter is an optional parameter that specifies the
|
|
371
371
|
* property to use for sorting the vertices. It can have two possible values: 'vertex' or 'key'. If 'vertex' is
|
|
372
372
|
* specified, the vertices themselves will be used for sorting. If 'key' is specified, the ids of
|
|
373
|
-
* @returns an array of vertices or vertex IDs in topological order. If there is a cycle in the graph, it returns
|
|
373
|
+
* @returns an array of vertices or vertex IDs in topological order. If there is a cycle in the graph, it returns undefined.
|
|
374
374
|
*/
|
|
375
375
|
topologicalSort(propertyName) {
|
|
376
376
|
propertyName = propertyName ?? 'key';
|
|
@@ -403,7 +403,7 @@ export class DirectedGraph extends AbstractGraph {
|
|
|
403
403
|
}
|
|
404
404
|
}
|
|
405
405
|
if (hasCycle)
|
|
406
|
-
return
|
|
406
|
+
return undefined;
|
|
407
407
|
if (propertyName === 'key')
|
|
408
408
|
sorted = sorted.map(vertex => (vertex instanceof DirectedVertex ? vertex.key : vertex));
|
|
409
409
|
return sorted.reverse();
|
|
@@ -446,7 +446,7 @@ export class DirectedGraph extends AbstractGraph {
|
|
|
446
446
|
const outEdges = this.outgoingEdgesOf(vertex);
|
|
447
447
|
for (const outEdge of outEdges) {
|
|
448
448
|
const neighbor = this._getVertex(outEdge.dest);
|
|
449
|
-
// TODO after no-non-
|
|
449
|
+
// TODO after no-non-undefined-assertion not ensure the logic
|
|
450
450
|
if (neighbor) {
|
|
451
451
|
neighbors.push(neighbor);
|
|
452
452
|
}
|
|
@@ -463,14 +463,14 @@ export class DirectedGraph extends AbstractGraph {
|
|
|
463
463
|
* Space Complexity: O(1)
|
|
464
464
|
*
|
|
465
465
|
* The function "getEndsOfEdge" returns the source and destination vertices of an edge if it exists in the graph,
|
|
466
|
-
* otherwise it returns
|
|
466
|
+
* otherwise it returns undefined.
|
|
467
467
|
* @param {EO} edge - The parameter `edge` is of type `EO`, which represents an edge in a graph.
|
|
468
468
|
* @returns The function `getEndsOfEdge` returns an array containing two vertices `[VO, VO]` if the edge exists in the
|
|
469
|
-
* graph. If the edge does not exist, it returns `
|
|
469
|
+
* graph. If the edge does not exist, it returns `undefined`.
|
|
470
470
|
*/
|
|
471
471
|
getEndsOfEdge(edge) {
|
|
472
472
|
if (!this.hasEdge(edge.src, edge.dest)) {
|
|
473
|
-
return
|
|
473
|
+
return undefined;
|
|
474
474
|
}
|
|
475
475
|
const v1 = this._getVertex(edge.src);
|
|
476
476
|
const v2 = this._getVertex(edge.dest);
|
|
@@ -478,7 +478,7 @@ export class DirectedGraph extends AbstractGraph {
|
|
|
478
478
|
return [v1, v2];
|
|
479
479
|
}
|
|
480
480
|
else {
|
|
481
|
-
return
|
|
481
|
+
return undefined;
|
|
482
482
|
}
|
|
483
483
|
}
|
|
484
484
|
/**
|
|
@@ -501,7 +501,7 @@ export class DirectedGraph extends AbstractGraph {
|
|
|
501
501
|
}
|
|
502
502
|
const srcVertex = this._getVertex(edge.src);
|
|
503
503
|
const destVertex = this._getVertex(edge.dest);
|
|
504
|
-
// TODO after no-non-
|
|
504
|
+
// TODO after no-non-undefined-assertion not ensure the logic
|
|
505
505
|
if (srcVertex && destVertex) {
|
|
506
506
|
const srcOutEdges = this._outEdgeMap.get(srcVertex);
|
|
507
507
|
if (srcOutEdges) {
|
|
@@ -61,14 +61,14 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
|
|
|
61
61
|
* Time Complexity: O(|E|), where |E| is the number of edges incident to the given vertex.
|
|
62
62
|
* Space Complexity: O(1)
|
|
63
63
|
*
|
|
64
|
-
* The function `getEdge` returns the first edge that connects two vertices, or
|
|
65
|
-
* @param {VO | VertexKey |
|
|
66
|
-
* object), `
|
|
67
|
-
* @param {VO | VertexKey |
|
|
68
|
-
* object), `
|
|
69
|
-
* @returns an edge (EO) or
|
|
64
|
+
* The function `getEdge` returns the first edge that connects two vertices, or undefined if no such edge exists.
|
|
65
|
+
* @param {VO | VertexKey | undefined} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
66
|
+
* object), `undefined`, or `VertexKey` (a string or number representing the ID of a vertex).
|
|
67
|
+
* @param {VO | VertexKey | undefined} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
68
|
+
* object), `undefined`, or `VertexKey` (vertex ID).
|
|
69
|
+
* @returns an edge (EO) or undefined.
|
|
70
70
|
*/
|
|
71
|
-
getEdge(v1: VO | VertexKey |
|
|
71
|
+
getEdge(v1: VO | VertexKey | undefined, v2: VO | VertexKey | undefined): EO | undefined;
|
|
72
72
|
/**
|
|
73
73
|
* Time Complexity: O(|E|), where |E| is the number of edges incident to the given vertex.
|
|
74
74
|
* Space Complexity: O(1)
|
|
@@ -81,9 +81,9 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
|
|
|
81
81
|
* @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
|
|
82
82
|
* @param {VO | VertexKey} v2 - VO | VertexKey - This parameter can be either a vertex object (VO) or a vertex ID
|
|
83
83
|
* (VertexKey). It represents the second vertex of the edge that needs to be removed.
|
|
84
|
-
* @returns the removed edge (EO) if it exists, or
|
|
84
|
+
* @returns the removed edge (EO) if it exists, or undefined if either of the vertices (VO) does not exist.
|
|
85
85
|
*/
|
|
86
|
-
deleteEdgeBetween(v1: VO | VertexKey, v2: VO | VertexKey): EO |
|
|
86
|
+
deleteEdgeBetween(v1: VO | VertexKey, v2: VO | VertexKey): EO | undefined;
|
|
87
87
|
/**
|
|
88
88
|
* Time Complexity: O(|E|), where |E| is the number of edges incident to the given vertex.
|
|
89
89
|
* Space Complexity: O(1)
|
|
@@ -94,9 +94,9 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
|
|
|
94
94
|
*
|
|
95
95
|
* The deleteEdge function removes an edge between two vertices in a graph.
|
|
96
96
|
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
97
|
-
* @returns The method is returning either the removed edge (of type EO) or
|
|
97
|
+
* @returns The method is returning either the removed edge (of type EO) or undefined if the edge was not found.
|
|
98
98
|
*/
|
|
99
|
-
deleteEdge(edge: EO): EO |
|
|
99
|
+
deleteEdge(edge: EO): EO | undefined;
|
|
100
100
|
/**
|
|
101
101
|
* Time Complexity: O(1)
|
|
102
102
|
* Space Complexity: O(1)
|
|
@@ -161,12 +161,12 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
|
|
|
161
161
|
* Space Complexity: O(1)
|
|
162
162
|
*
|
|
163
163
|
* The function "getEndsOfEdge" returns the vertices at the ends of an edge if the edge exists in the graph, otherwise
|
|
164
|
-
* it returns
|
|
164
|
+
* it returns undefined.
|
|
165
165
|
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
166
166
|
* @returns The function `getEndsOfEdge` returns an array containing two vertices `[VO, VO]` if the edge exists in the
|
|
167
|
-
* graph. If the edge does not exist, it returns `
|
|
167
|
+
* graph. If the edge does not exist, it returns `undefined`.
|
|
168
168
|
*/
|
|
169
|
-
getEndsOfEdge(edge: EO): [VO, VO] |
|
|
169
|
+
getEndsOfEdge(edge: EO): [VO, VO] | undefined;
|
|
170
170
|
/**
|
|
171
171
|
* Time Complexity: O(1)
|
|
172
172
|
* Space Complexity: O(1)
|
|
@@ -81,23 +81,23 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
81
81
|
* Time Complexity: O(|E|), where |E| is the number of edges incident to the given vertex.
|
|
82
82
|
* Space Complexity: O(1)
|
|
83
83
|
*
|
|
84
|
-
* The function `getEdge` returns the first edge that connects two vertices, or
|
|
85
|
-
* @param {VO | VertexKey |
|
|
86
|
-
* object), `
|
|
87
|
-
* @param {VO | VertexKey |
|
|
88
|
-
* object), `
|
|
89
|
-
* @returns an edge (EO) or
|
|
84
|
+
* The function `getEdge` returns the first edge that connects two vertices, or undefined if no such edge exists.
|
|
85
|
+
* @param {VO | VertexKey | undefined} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
86
|
+
* object), `undefined`, or `VertexKey` (a string or number representing the ID of a vertex).
|
|
87
|
+
* @param {VO | VertexKey | undefined} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
88
|
+
* object), `undefined`, or `VertexKey` (vertex ID).
|
|
89
|
+
* @returns an edge (EO) or undefined.
|
|
90
90
|
*/
|
|
91
91
|
getEdge(v1, v2) {
|
|
92
92
|
let edges = [];
|
|
93
|
-
if (v1 !==
|
|
93
|
+
if (v1 !== undefined && v2 !== undefined) {
|
|
94
94
|
const vertex1 = this._getVertex(v1);
|
|
95
95
|
const vertex2 = this._getVertex(v2);
|
|
96
96
|
if (vertex1 && vertex2) {
|
|
97
97
|
edges = this._edges.get(vertex1)?.filter(e => e.vertices.includes(vertex2.key));
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
|
-
return edges ? edges[0] ||
|
|
100
|
+
return edges ? edges[0] || undefined : undefined;
|
|
101
101
|
}
|
|
102
102
|
/**
|
|
103
103
|
* Time Complexity: O(|E|), where |E| is the number of edges incident to the given vertex.
|
|
@@ -111,18 +111,18 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
111
111
|
* @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
|
|
112
112
|
* @param {VO | VertexKey} v2 - VO | VertexKey - This parameter can be either a vertex object (VO) or a vertex ID
|
|
113
113
|
* (VertexKey). It represents the second vertex of the edge that needs to be removed.
|
|
114
|
-
* @returns the removed edge (EO) if it exists, or
|
|
114
|
+
* @returns the removed edge (EO) if it exists, or undefined if either of the vertices (VO) does not exist.
|
|
115
115
|
*/
|
|
116
116
|
deleteEdgeBetween(v1, v2) {
|
|
117
117
|
const vertex1 = this._getVertex(v1);
|
|
118
118
|
const vertex2 = this._getVertex(v2);
|
|
119
119
|
if (!vertex1 || !vertex2) {
|
|
120
|
-
return
|
|
120
|
+
return undefined;
|
|
121
121
|
}
|
|
122
122
|
const v1Edges = this._edges.get(vertex1);
|
|
123
|
-
let removed =
|
|
123
|
+
let removed = undefined;
|
|
124
124
|
if (v1Edges) {
|
|
125
|
-
removed = arrayRemove(v1Edges, (e) => e.vertices.includes(vertex2.key))[0] ||
|
|
125
|
+
removed = arrayRemove(v1Edges, (e) => e.vertices.includes(vertex2.key))[0] || undefined;
|
|
126
126
|
}
|
|
127
127
|
const v2Edges = this._edges.get(vertex2);
|
|
128
128
|
if (v2Edges) {
|
|
@@ -140,7 +140,7 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
140
140
|
*
|
|
141
141
|
* The deleteEdge function removes an edge between two vertices in a graph.
|
|
142
142
|
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
143
|
-
* @returns The method is returning either the removed edge (of type EO) or
|
|
143
|
+
* @returns The method is returning either the removed edge (of type EO) or undefined if the edge was not found.
|
|
144
144
|
*/
|
|
145
145
|
deleteEdge(edge) {
|
|
146
146
|
return this.deleteEdgeBetween(edge.vertices[0], edge.vertices[1]);
|
|
@@ -246,14 +246,14 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
246
246
|
* Space Complexity: O(1)
|
|
247
247
|
*
|
|
248
248
|
* The function "getEndsOfEdge" returns the vertices at the ends of an edge if the edge exists in the graph, otherwise
|
|
249
|
-
* it returns
|
|
249
|
+
* it returns undefined.
|
|
250
250
|
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
251
251
|
* @returns The function `getEndsOfEdge` returns an array containing two vertices `[VO, VO]` if the edge exists in the
|
|
252
|
-
* graph. If the edge does not exist, it returns `
|
|
252
|
+
* graph. If the edge does not exist, it returns `undefined`.
|
|
253
253
|
*/
|
|
254
254
|
getEndsOfEdge(edge) {
|
|
255
255
|
if (!this.hasEdge(edge.vertices[0], edge.vertices[1])) {
|
|
256
|
-
return
|
|
256
|
+
return undefined;
|
|
257
257
|
}
|
|
258
258
|
const v1 = this._getVertex(edge.vertices[0]);
|
|
259
259
|
const v2 = this._getVertex(edge.vertices[1]);
|
|
@@ -261,7 +261,7 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
261
261
|
return [v1, v2];
|
|
262
262
|
}
|
|
263
263
|
else {
|
|
264
|
-
return
|
|
264
|
+
return undefined;
|
|
265
265
|
}
|
|
266
266
|
}
|
|
267
267
|
/**
|
|
@@ -279,7 +279,7 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
279
279
|
_addEdgeOnly(edge) {
|
|
280
280
|
for (const end of edge.vertices) {
|
|
281
281
|
const endVertex = this._getVertex(end);
|
|
282
|
-
if (endVertex ===
|
|
282
|
+
if (endVertex === undefined)
|
|
283
283
|
return false;
|
|
284
284
|
if (endVertex) {
|
|
285
285
|
const edges = this._edges.get(endVertex);
|
|
@@ -14,12 +14,7 @@ export declare class HashMap<K = any, V = any> {
|
|
|
14
14
|
protected readonly _sentinel: HashMapLinkedNode<K, V | undefined>;
|
|
15
15
|
protected _hashFn: (key: K) => string;
|
|
16
16
|
protected _objHashFn: (key: K) => object;
|
|
17
|
-
|
|
18
|
-
* The constructor initializes a HashMapLinkedNode with an optional iterable of key-value pairs.
|
|
19
|
-
* @param options - The `options` parameter is an object that contains the `elements` property. The
|
|
20
|
-
* `elements` property is an iterable that contains key-value pairs represented as arrays `[K, V]`.
|
|
21
|
-
*/
|
|
22
|
-
constructor(options?: HashMapOptions<K, V>);
|
|
17
|
+
constructor(elements?: Iterable<[K, V]>, options?: HashMapOptions<K>);
|
|
23
18
|
protected _size: number;
|
|
24
19
|
get size(): number;
|
|
25
20
|
/**
|
|
@@ -172,6 +167,7 @@ export declare class HashMap<K = any, V = any> {
|
|
|
172
167
|
* The above function is an iterator that yields key-value pairs from a linked list.
|
|
173
168
|
*/
|
|
174
169
|
[Symbol.iterator](): Generator<[K, V], void, unknown>;
|
|
170
|
+
print(): void;
|
|
175
171
|
/**
|
|
176
172
|
* Time Complexity: O(1)
|
|
177
173
|
* Space Complexity: O(1)
|
|
@@ -14,19 +14,13 @@ export class HashMap {
|
|
|
14
14
|
_sentinel;
|
|
15
15
|
_hashFn;
|
|
16
16
|
_objHashFn;
|
|
17
|
-
|
|
18
|
-
* The constructor initializes a HashMapLinkedNode with an optional iterable of key-value pairs.
|
|
19
|
-
* @param options - The `options` parameter is an object that contains the `elements` property. The
|
|
20
|
-
* `elements` property is an iterable that contains key-value pairs represented as arrays `[K, V]`.
|
|
21
|
-
*/
|
|
22
|
-
constructor(options = {
|
|
23
|
-
elements: [],
|
|
17
|
+
constructor(elements, options = {
|
|
24
18
|
hashFn: (key) => String(key),
|
|
25
19
|
objHashFn: (key) => key
|
|
26
20
|
}) {
|
|
27
21
|
this._sentinel = {};
|
|
28
22
|
this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
|
|
29
|
-
const {
|
|
23
|
+
const { hashFn, objHashFn } = options;
|
|
30
24
|
this._hashFn = hashFn;
|
|
31
25
|
this._objHashFn = objHashFn;
|
|
32
26
|
if (elements) {
|
|
@@ -349,6 +343,9 @@ export class HashMap {
|
|
|
349
343
|
node = node.next;
|
|
350
344
|
}
|
|
351
345
|
}
|
|
346
|
+
print() {
|
|
347
|
+
console.log([...this]);
|
|
348
|
+
}
|
|
352
349
|
/**
|
|
353
350
|
* Time Complexity: O(1)
|
|
354
351
|
* Space Complexity: O(1)
|