data-structure-typed 1.48.4 → 1.48.5
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/CHANGELOG.md +1 -1
- package/dist/cjs/data-structures/base/iterable-base.d.ts +6 -6
- package/dist/cjs/data-structures/base/iterable-base.js +3 -3
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +5 -3
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +6 -4
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +18 -15
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +16 -13
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +15 -11
- package/dist/cjs/data-structures/binary-tree/bst.js +17 -13
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +19 -13
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +20 -14
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +21 -14
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +25 -18
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +52 -52
- package/dist/cjs/data-structures/graph/abstract-graph.js +78 -78
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +47 -47
- package/dist/cjs/data-structures/graph/directed-graph.js +56 -56
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.d.ts +5 -5
- package/dist/cjs/data-structures/graph/map-graph.js +8 -8
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +29 -29
- package/dist/cjs/data-structures/graph/undirected-graph.js +57 -57
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +8 -8
- package/dist/cjs/data-structures/hash/hash-map.js +2 -2
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +1 -1
- package/dist/cjs/types/data-structures/base/base.d.ts +3 -3
- package/dist/mjs/data-structures/base/iterable-base.d.ts +6 -6
- package/dist/mjs/data-structures/base/iterable-base.js +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +5 -3
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +6 -4
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +18 -15
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +17 -14
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +15 -11
- package/dist/mjs/data-structures/binary-tree/bst.js +17 -13
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +19 -13
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +20 -14
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +21 -14
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +25 -18
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +52 -52
- package/dist/mjs/data-structures/graph/abstract-graph.js +79 -79
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +47 -47
- package/dist/mjs/data-structures/graph/directed-graph.js +56 -56
- package/dist/mjs/data-structures/graph/map-graph.d.ts +5 -5
- package/dist/mjs/data-structures/graph/map-graph.js +8 -8
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +29 -29
- package/dist/mjs/data-structures/graph/undirected-graph.js +59 -59
- package/dist/mjs/data-structures/hash/hash-map.d.ts +8 -8
- package/dist/mjs/data-structures/hash/hash-map.js +3 -3
- package/dist/mjs/interfaces/binary-tree.d.ts +1 -1
- package/dist/mjs/types/data-structures/base/base.d.ts +3 -3
- package/dist/umd/data-structure-typed.js +302 -280
- 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/base/iterable-base.ts +6 -6
- package/src/data-structures/binary-tree/avl-tree.ts +8 -5
- package/src/data-structures/binary-tree/binary-tree.ts +23 -19
- package/src/data-structures/binary-tree/bst.ts +19 -14
- package/src/data-structures/binary-tree/rb-tree.ts +20 -14
- package/src/data-structures/binary-tree/tree-multimap.ts +27 -19
- package/src/data-structures/graph/abstract-graph.ts +82 -82
- package/src/data-structures/graph/directed-graph.ts +56 -56
- package/src/data-structures/graph/map-graph.ts +8 -8
- package/src/data-structures/graph/undirected-graph.ts +59 -59
- package/src/data-structures/hash/hash-map.ts +8 -8
- package/src/interfaces/binary-tree.ts +1 -1
- package/src/types/data-structures/base/base.ts +3 -3
- package/test/integration/bst.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +3 -3
- package/test/unit/data-structures/graph/directed-graph.test.ts +15 -15
- package/test/unit/data-structures/graph/map-graph.test.ts +3 -3
- package/test/unit/data-structures/graph/undirected-graph.test.ts +12 -12
|
@@ -12,7 +12,7 @@ export declare class UndirectedVertex<V = any> extends AbstractVertex<V> {
|
|
|
12
12
|
constructor(key: VertexKey, value?: V);
|
|
13
13
|
}
|
|
14
14
|
export declare class UndirectedEdge<E = number> extends AbstractEdge<E> {
|
|
15
|
-
|
|
15
|
+
vertexMap: [VertexKey, VertexKey];
|
|
16
16
|
/**
|
|
17
17
|
* The constructor function creates an instance of a class with two vertex IDs, an optional weight, and an optional
|
|
18
18
|
* value.
|
|
@@ -27,11 +27,11 @@ export declare class UndirectedEdge<E = number> extends AbstractEdge<E> {
|
|
|
27
27
|
}
|
|
28
28
|
export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVertex<V> = UndirectedVertex<V>, EO extends UndirectedEdge<E> = UndirectedEdge<E>> extends AbstractGraph<V, E, VO, EO> implements IGraph<V, E, VO, EO> {
|
|
29
29
|
/**
|
|
30
|
-
* The constructor initializes a new Map object to store
|
|
30
|
+
* The constructor initializes a new Map object to store edgeMap.
|
|
31
31
|
*/
|
|
32
32
|
constructor();
|
|
33
|
-
protected
|
|
34
|
-
get
|
|
33
|
+
protected _edgeMap: Map<VO, EO[]>;
|
|
34
|
+
get edgeMap(): Map<VO, EO[]>;
|
|
35
35
|
/**
|
|
36
36
|
* The function creates a new vertex with an optional value and returns it.
|
|
37
37
|
* @param {VertexKey} key - The `key` parameter is the unique identifier for the vertex. It is used to distinguish one
|
|
@@ -43,7 +43,7 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
|
|
|
43
43
|
*/
|
|
44
44
|
createVertex(key: VertexKey, value?: VO['value']): VO;
|
|
45
45
|
/**
|
|
46
|
-
* The function creates an undirected edge between two
|
|
46
|
+
* The function creates an undirected edge between two vertexMap with an optional weight and value.
|
|
47
47
|
* @param {VertexKey} v1 - The parameter `v1` represents the first vertex of the edge.
|
|
48
48
|
* @param {VertexKey} v2 - The parameter `v2` represents the second vertex of the edge.
|
|
49
49
|
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge. If
|
|
@@ -54,14 +54,14 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
|
|
|
54
54
|
*/
|
|
55
55
|
createEdge(v1: VertexKey, v2: VertexKey, weight?: number, value?: EO['value']): EO;
|
|
56
56
|
/**
|
|
57
|
-
* Time Complexity: O(|E|), where |E| is the number of
|
|
57
|
+
* Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
|
|
58
58
|
* Space Complexity: O(1)
|
|
59
59
|
*/
|
|
60
60
|
/**
|
|
61
|
-
* Time Complexity: O(|E|), where |E| is the number of
|
|
61
|
+
* Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
|
|
62
62
|
* Space Complexity: O(1)
|
|
63
63
|
*
|
|
64
|
-
* The function `getEdge` returns the first edge that connects two
|
|
64
|
+
* The function `getEdge` returns the first edge that connects two vertexMap, or undefined if no such edge exists.
|
|
65
65
|
* @param {VO | VertexKey | undefined} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
66
66
|
* object), `undefined`, or `VertexKey` (a string or number representing the ID of a vertex).
|
|
67
67
|
* @param {VO | VertexKey | undefined} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
@@ -70,29 +70,29 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
|
|
|
70
70
|
*/
|
|
71
71
|
getEdge(v1: VO | VertexKey | undefined, v2: VO | VertexKey | undefined): EO | undefined;
|
|
72
72
|
/**
|
|
73
|
-
* Time Complexity: O(|E|), where |E| is the number of
|
|
73
|
+
* Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
|
|
74
74
|
* Space Complexity: O(1)
|
|
75
75
|
*/
|
|
76
76
|
/**
|
|
77
|
-
* Time Complexity: O(|E|), where |E| is the number of
|
|
77
|
+
* Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
|
|
78
78
|
* Space Complexity: O(1)
|
|
79
79
|
*
|
|
80
|
-
* The function removes an edge between two
|
|
80
|
+
* The function removes an edge between two vertexMap in a graph and returns the removed edge.
|
|
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 undefined if either of the
|
|
84
|
+
* @returns the removed edge (EO) if it exists, or undefined if either of the vertexMap (VO) does not exist.
|
|
85
85
|
*/
|
|
86
86
|
deleteEdgeBetween(v1: VO | VertexKey, v2: VO | VertexKey): EO | undefined;
|
|
87
87
|
/**
|
|
88
|
-
* Time Complexity: O(E), where E is the number of
|
|
88
|
+
* Time Complexity: O(E), where E is the number of edgeMap incident to the given vertex.
|
|
89
89
|
* Space Complexity: O(1)
|
|
90
90
|
*/
|
|
91
91
|
/**
|
|
92
|
-
* Time Complexity: O(E), where E is the number of
|
|
92
|
+
* Time Complexity: O(E), where E is the number of edgeMap incident to the given vertex.
|
|
93
93
|
* Space Complexity: O(1)
|
|
94
94
|
*
|
|
95
|
-
* The function `deleteEdge` deletes an edge between two
|
|
95
|
+
* The function `deleteEdge` deletes an edge between two vertexMap in a graph.
|
|
96
96
|
* @param {EO | VertexKey} edgeOrOneSideVertexKey - The parameter `edgeOrOneSideVertexKey` can be
|
|
97
97
|
* either an edge object or a vertex key.
|
|
98
98
|
* @param {VertexKey} [otherSideVertexKey] - The parameter `otherSideVertexKey` is an optional
|
|
@@ -124,11 +124,11 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
|
|
|
124
124
|
* Time Complexity: O(1)
|
|
125
125
|
* Space Complexity: O(1)
|
|
126
126
|
*
|
|
127
|
-
* The function `degreeOf` returns the degree of a vertex in a graph, which is the number of
|
|
127
|
+
* The function `degreeOf` returns the degree of a vertex in a graph, which is the number of edgeMap connected to that
|
|
128
128
|
* vertex.
|
|
129
129
|
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
|
|
130
130
|
* @returns The function `degreeOf` returns the degree of a vertex in a graph. The degree of a vertex is the number of
|
|
131
|
-
*
|
|
131
|
+
* edgeMap connected to that vertex.
|
|
132
132
|
*/
|
|
133
133
|
degreeOf(vertexOrKey: VertexKey | VO): number;
|
|
134
134
|
/**
|
|
@@ -139,36 +139,36 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
|
|
|
139
139
|
* Time Complexity: O(1)
|
|
140
140
|
* Space Complexity: O(1)
|
|
141
141
|
*
|
|
142
|
-
* The function returns the
|
|
142
|
+
* The function returns the edgeMap of a given vertex or vertex ID.
|
|
143
143
|
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`. A `VertexKey` is a
|
|
144
144
|
* unique identifier for a vertex in a graph, while `VO` represents the type of the vertex.
|
|
145
|
-
* @returns an array of
|
|
145
|
+
* @returns an array of edgeMap.
|
|
146
146
|
*/
|
|
147
147
|
edgesOf(vertexOrKey: VertexKey | VO): EO[];
|
|
148
148
|
/**
|
|
149
|
-
* Time Complexity: O(|V| + |E|), where |V| is the number of
|
|
149
|
+
* Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
|
|
150
150
|
* Space Complexity: O(|E|)
|
|
151
151
|
*/
|
|
152
152
|
/**
|
|
153
|
-
* Time Complexity: O(|V| + |E|), where |V| is the number of
|
|
153
|
+
* Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
|
|
154
154
|
* Space Complexity: O(|E|)
|
|
155
155
|
*
|
|
156
|
-
* The function "edgeSet" returns an array of unique
|
|
156
|
+
* The function "edgeSet" returns an array of unique edgeMap from a set of edgeMap.
|
|
157
157
|
* @returns The method `edgeSet()` returns an array of type `EO[]`.
|
|
158
158
|
*/
|
|
159
159
|
edgeSet(): EO[];
|
|
160
160
|
/**
|
|
161
|
-
* Time Complexity: O(|V| + |E|), where |V| is the number of
|
|
161
|
+
* Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
|
|
162
162
|
* Space Complexity: O(|E|)
|
|
163
163
|
*/
|
|
164
164
|
/**
|
|
165
|
-
* Time Complexity: O(|V| + |E|), where |V| is the number of
|
|
165
|
+
* Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
|
|
166
166
|
* Space Complexity: O(|E|)
|
|
167
167
|
*
|
|
168
|
-
* The function "getNeighbors" returns an array of neighboring
|
|
168
|
+
* The function "getNeighbors" returns an array of neighboring vertexMap for a given vertex or vertex ID.
|
|
169
169
|
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
170
170
|
* (`VertexKey`).
|
|
171
|
-
* @returns an array of
|
|
171
|
+
* @returns an array of vertexMap (VO[]).
|
|
172
172
|
*/
|
|
173
173
|
getNeighbors(vertexOrKey: VO | VertexKey): VO[];
|
|
174
174
|
/**
|
|
@@ -179,10 +179,10 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
|
|
|
179
179
|
* Time Complexity: O(1)
|
|
180
180
|
* Space Complexity: O(1)
|
|
181
181
|
*
|
|
182
|
-
* The function "getEndsOfEdge" returns the
|
|
182
|
+
* The function "getEndsOfEdge" returns the vertexMap at the ends of an edge if the edge exists in the graph, otherwise
|
|
183
183
|
* it returns undefined.
|
|
184
184
|
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
185
|
-
* @returns The function `getEndsOfEdge` returns an array containing two
|
|
185
|
+
* @returns The function `getEndsOfEdge` returns an array containing two vertexMap `[VO, VO]` if the edge exists in the
|
|
186
186
|
* graph. If the edge does not exist, it returns `undefined`.
|
|
187
187
|
*/
|
|
188
188
|
getEndsOfEdge(edge: EO): [VO, VO] | undefined;
|
|
@@ -194,7 +194,7 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
|
|
|
194
194
|
* Time Complexity: O(1)
|
|
195
195
|
* Space Complexity: O(1)
|
|
196
196
|
*
|
|
197
|
-
* The function adds an edge to the graph by updating the adjacency list with the
|
|
197
|
+
* The function adds an edge to the graph by updating the adjacency list with the vertexMap of the edge.
|
|
198
198
|
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
199
199
|
* @returns a boolean value.
|
|
200
200
|
*/
|
|
@@ -20,7 +20,7 @@ export class UndirectedVertex extends AbstractVertex {
|
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
export class UndirectedEdge extends AbstractEdge {
|
|
23
|
-
|
|
23
|
+
vertexMap;
|
|
24
24
|
/**
|
|
25
25
|
* The constructor function creates an instance of a class with two vertex IDs, an optional weight, and an optional
|
|
26
26
|
* value.
|
|
@@ -33,20 +33,20 @@ export class UndirectedEdge extends AbstractEdge {
|
|
|
33
33
|
*/
|
|
34
34
|
constructor(v1, v2, weight, value) {
|
|
35
35
|
super(weight, value);
|
|
36
|
-
this.
|
|
36
|
+
this.vertexMap = [v1, v2];
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
export class UndirectedGraph extends AbstractGraph {
|
|
40
40
|
/**
|
|
41
|
-
* The constructor initializes a new Map object to store
|
|
41
|
+
* The constructor initializes a new Map object to store edgeMap.
|
|
42
42
|
*/
|
|
43
43
|
constructor() {
|
|
44
44
|
super();
|
|
45
|
-
this.
|
|
45
|
+
this._edgeMap = new Map();
|
|
46
46
|
}
|
|
47
|
-
|
|
48
|
-
get
|
|
49
|
-
return this.
|
|
47
|
+
_edgeMap;
|
|
48
|
+
get edgeMap() {
|
|
49
|
+
return this._edgeMap;
|
|
50
50
|
}
|
|
51
51
|
/**
|
|
52
52
|
* The function creates a new vertex with an optional value and returns it.
|
|
@@ -61,7 +61,7 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
61
61
|
return new UndirectedVertex(key, value ?? key);
|
|
62
62
|
}
|
|
63
63
|
/**
|
|
64
|
-
* The function creates an undirected edge between two
|
|
64
|
+
* The function creates an undirected edge between two vertexMap with an optional weight and value.
|
|
65
65
|
* @param {VertexKey} v1 - The parameter `v1` represents the first vertex of the edge.
|
|
66
66
|
* @param {VertexKey} v2 - The parameter `v2` represents the second vertex of the edge.
|
|
67
67
|
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge. If
|
|
@@ -74,14 +74,14 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
74
74
|
return new UndirectedEdge(v1, v2, weight ?? 1, value);
|
|
75
75
|
}
|
|
76
76
|
/**
|
|
77
|
-
* Time Complexity: O(|E|), where |E| is the number of
|
|
77
|
+
* Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
|
|
78
78
|
* Space Complexity: O(1)
|
|
79
79
|
*/
|
|
80
80
|
/**
|
|
81
|
-
* Time Complexity: O(|E|), where |E| is the number of
|
|
81
|
+
* Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
|
|
82
82
|
* Space Complexity: O(1)
|
|
83
83
|
*
|
|
84
|
-
* The function `getEdge` returns the first edge that connects two
|
|
84
|
+
* The function `getEdge` returns the first edge that connects two vertexMap, or undefined if no such edge exists.
|
|
85
85
|
* @param {VO | VertexKey | undefined} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
86
86
|
* object), `undefined`, or `VertexKey` (a string or number representing the ID of a vertex).
|
|
87
87
|
* @param {VO | VertexKey | undefined} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
@@ -89,29 +89,29 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
89
89
|
* @returns an edge (EO) or undefined.
|
|
90
90
|
*/
|
|
91
91
|
getEdge(v1, v2) {
|
|
92
|
-
let
|
|
92
|
+
let edgeMap = [];
|
|
93
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
|
+
edgeMap = this._edgeMap.get(vertex1)?.filter(e => e.vertexMap.includes(vertex2.key));
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
|
-
return
|
|
100
|
+
return edgeMap ? edgeMap[0] || undefined : undefined;
|
|
101
101
|
}
|
|
102
102
|
/**
|
|
103
|
-
* Time Complexity: O(|E|), where |E| is the number of
|
|
103
|
+
* Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
|
|
104
104
|
* Space Complexity: O(1)
|
|
105
105
|
*/
|
|
106
106
|
/**
|
|
107
|
-
* Time Complexity: O(|E|), where |E| is the number of
|
|
107
|
+
* Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
|
|
108
108
|
* Space Complexity: O(1)
|
|
109
109
|
*
|
|
110
|
-
* The function removes an edge between two
|
|
110
|
+
* The function removes an edge between two vertexMap in a graph and returns the removed edge.
|
|
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 undefined if either of the
|
|
114
|
+
* @returns the removed edge (EO) if it exists, or undefined if either of the vertexMap (VO) does not exist.
|
|
115
115
|
*/
|
|
116
116
|
deleteEdgeBetween(v1, v2) {
|
|
117
117
|
const vertex1 = this._getVertex(v1);
|
|
@@ -119,26 +119,26 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
119
119
|
if (!vertex1 || !vertex2) {
|
|
120
120
|
return undefined;
|
|
121
121
|
}
|
|
122
|
-
const v1Edges = this.
|
|
122
|
+
const v1Edges = this._edgeMap.get(vertex1);
|
|
123
123
|
let removed = undefined;
|
|
124
124
|
if (v1Edges) {
|
|
125
|
-
removed = arrayRemove(v1Edges, (e) => e.
|
|
125
|
+
removed = arrayRemove(v1Edges, (e) => e.vertexMap.includes(vertex2.key))[0] || undefined;
|
|
126
126
|
}
|
|
127
|
-
const v2Edges = this.
|
|
127
|
+
const v2Edges = this._edgeMap.get(vertex2);
|
|
128
128
|
if (v2Edges) {
|
|
129
|
-
arrayRemove(v2Edges, (e) => e.
|
|
129
|
+
arrayRemove(v2Edges, (e) => e.vertexMap.includes(vertex1.key));
|
|
130
130
|
}
|
|
131
131
|
return removed;
|
|
132
132
|
}
|
|
133
133
|
/**
|
|
134
|
-
* Time Complexity: O(E), where E is the number of
|
|
134
|
+
* Time Complexity: O(E), where E is the number of edgeMap incident to the given vertex.
|
|
135
135
|
* Space Complexity: O(1)
|
|
136
136
|
*/
|
|
137
137
|
/**
|
|
138
|
-
* Time Complexity: O(E), where E is the number of
|
|
138
|
+
* Time Complexity: O(E), where E is the number of edgeMap incident to the given vertex.
|
|
139
139
|
* Space Complexity: O(1)
|
|
140
140
|
*
|
|
141
|
-
* The function `deleteEdge` deletes an edge between two
|
|
141
|
+
* The function `deleteEdge` deletes an edge between two vertexMap in a graph.
|
|
142
142
|
* @param {EO | VertexKey} edgeOrOneSideVertexKey - The parameter `edgeOrOneSideVertexKey` can be
|
|
143
143
|
* either an edge object or a vertex key.
|
|
144
144
|
* @param {VertexKey} [otherSideVertexKey] - The parameter `otherSideVertexKey` is an optional
|
|
@@ -159,8 +159,8 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
159
159
|
}
|
|
160
160
|
}
|
|
161
161
|
else {
|
|
162
|
-
oneSide = this._getVertex(edgeOrOneSideVertexKey.
|
|
163
|
-
otherSide = this._getVertex(edgeOrOneSideVertexKey.
|
|
162
|
+
oneSide = this._getVertex(edgeOrOneSideVertexKey.vertexMap[0]);
|
|
163
|
+
otherSide = this._getVertex(edgeOrOneSideVertexKey.vertexMap[1]);
|
|
164
164
|
}
|
|
165
165
|
if (oneSide && otherSide) {
|
|
166
166
|
return this.deleteEdgeBetween(oneSide, otherSide);
|
|
@@ -196,17 +196,17 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
196
196
|
const neighbors = this.getNeighbors(vertexOrKey);
|
|
197
197
|
if (vertex) {
|
|
198
198
|
neighbors.forEach(neighbor => {
|
|
199
|
-
const neighborEdges = this.
|
|
199
|
+
const neighborEdges = this._edgeMap.get(neighbor);
|
|
200
200
|
if (neighborEdges) {
|
|
201
201
|
const restEdges = neighborEdges.filter(edge => {
|
|
202
|
-
return !edge.
|
|
202
|
+
return !edge.vertexMap.includes(vertexKey);
|
|
203
203
|
});
|
|
204
|
-
this.
|
|
204
|
+
this._edgeMap.set(neighbor, restEdges);
|
|
205
205
|
}
|
|
206
206
|
});
|
|
207
|
-
this.
|
|
207
|
+
this._edgeMap.delete(vertex);
|
|
208
208
|
}
|
|
209
|
-
return this.
|
|
209
|
+
return this._vertexMap.delete(vertexKey);
|
|
210
210
|
}
|
|
211
211
|
/**
|
|
212
212
|
* Time Complexity: O(1)
|
|
@@ -216,16 +216,16 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
216
216
|
* Time Complexity: O(1)
|
|
217
217
|
* Space Complexity: O(1)
|
|
218
218
|
*
|
|
219
|
-
* The function `degreeOf` returns the degree of a vertex in a graph, which is the number of
|
|
219
|
+
* The function `degreeOf` returns the degree of a vertex in a graph, which is the number of edgeMap connected to that
|
|
220
220
|
* vertex.
|
|
221
221
|
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
|
|
222
222
|
* @returns The function `degreeOf` returns the degree of a vertex in a graph. The degree of a vertex is the number of
|
|
223
|
-
*
|
|
223
|
+
* edgeMap connected to that vertex.
|
|
224
224
|
*/
|
|
225
225
|
degreeOf(vertexOrKey) {
|
|
226
226
|
const vertex = this._getVertex(vertexOrKey);
|
|
227
227
|
if (vertex) {
|
|
228
|
-
return this.
|
|
228
|
+
return this._edgeMap.get(vertex)?.length || 0;
|
|
229
229
|
}
|
|
230
230
|
else {
|
|
231
231
|
return 0;
|
|
@@ -239,52 +239,52 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
239
239
|
* Time Complexity: O(1)
|
|
240
240
|
* Space Complexity: O(1)
|
|
241
241
|
*
|
|
242
|
-
* The function returns the
|
|
242
|
+
* The function returns the edgeMap of a given vertex or vertex ID.
|
|
243
243
|
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`. A `VertexKey` is a
|
|
244
244
|
* unique identifier for a vertex in a graph, while `VO` represents the type of the vertex.
|
|
245
|
-
* @returns an array of
|
|
245
|
+
* @returns an array of edgeMap.
|
|
246
246
|
*/
|
|
247
247
|
edgesOf(vertexOrKey) {
|
|
248
248
|
const vertex = this._getVertex(vertexOrKey);
|
|
249
249
|
if (vertex) {
|
|
250
|
-
return this.
|
|
250
|
+
return this._edgeMap.get(vertex) || [];
|
|
251
251
|
}
|
|
252
252
|
else {
|
|
253
253
|
return [];
|
|
254
254
|
}
|
|
255
255
|
}
|
|
256
256
|
/**
|
|
257
|
-
* Time Complexity: O(|V| + |E|), where |V| is the number of
|
|
257
|
+
* Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
|
|
258
258
|
* Space Complexity: O(|E|)
|
|
259
259
|
*/
|
|
260
260
|
/**
|
|
261
|
-
* Time Complexity: O(|V| + |E|), where |V| is the number of
|
|
261
|
+
* Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
|
|
262
262
|
* Space Complexity: O(|E|)
|
|
263
263
|
*
|
|
264
|
-
* The function "edgeSet" returns an array of unique
|
|
264
|
+
* The function "edgeSet" returns an array of unique edgeMap from a set of edgeMap.
|
|
265
265
|
* @returns The method `edgeSet()` returns an array of type `EO[]`.
|
|
266
266
|
*/
|
|
267
267
|
edgeSet() {
|
|
268
268
|
const edgeSet = new Set();
|
|
269
|
-
this.
|
|
270
|
-
|
|
269
|
+
this._edgeMap.forEach(edgeMap => {
|
|
270
|
+
edgeMap.forEach(edge => {
|
|
271
271
|
edgeSet.add(edge);
|
|
272
272
|
});
|
|
273
273
|
});
|
|
274
274
|
return [...edgeSet];
|
|
275
275
|
}
|
|
276
276
|
/**
|
|
277
|
-
* Time Complexity: O(|V| + |E|), where |V| is the number of
|
|
277
|
+
* Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
|
|
278
278
|
* Space Complexity: O(|E|)
|
|
279
279
|
*/
|
|
280
280
|
/**
|
|
281
|
-
* Time Complexity: O(|V| + |E|), where |V| is the number of
|
|
281
|
+
* Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
|
|
282
282
|
* Space Complexity: O(|E|)
|
|
283
283
|
*
|
|
284
|
-
* The function "getNeighbors" returns an array of neighboring
|
|
284
|
+
* The function "getNeighbors" returns an array of neighboring vertexMap for a given vertex or vertex ID.
|
|
285
285
|
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
286
286
|
* (`VertexKey`).
|
|
287
|
-
* @returns an array of
|
|
287
|
+
* @returns an array of vertexMap (VO[]).
|
|
288
288
|
*/
|
|
289
289
|
getNeighbors(vertexOrKey) {
|
|
290
290
|
const neighbors = [];
|
|
@@ -292,7 +292,7 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
292
292
|
if (vertex) {
|
|
293
293
|
const neighborEdges = this.edgesOf(vertex);
|
|
294
294
|
for (const edge of neighborEdges) {
|
|
295
|
-
const neighbor = this._getVertex(edge.
|
|
295
|
+
const neighbor = this._getVertex(edge.vertexMap.filter(e => e !== vertex.key)[0]);
|
|
296
296
|
if (neighbor) {
|
|
297
297
|
neighbors.push(neighbor);
|
|
298
298
|
}
|
|
@@ -308,18 +308,18 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
308
308
|
* Time Complexity: O(1)
|
|
309
309
|
* Space Complexity: O(1)
|
|
310
310
|
*
|
|
311
|
-
* The function "getEndsOfEdge" returns the
|
|
311
|
+
* The function "getEndsOfEdge" returns the vertexMap at the ends of an edge if the edge exists in the graph, otherwise
|
|
312
312
|
* it returns undefined.
|
|
313
313
|
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
314
|
-
* @returns The function `getEndsOfEdge` returns an array containing two
|
|
314
|
+
* @returns The function `getEndsOfEdge` returns an array containing two vertexMap `[VO, VO]` if the edge exists in the
|
|
315
315
|
* graph. If the edge does not exist, it returns `undefined`.
|
|
316
316
|
*/
|
|
317
317
|
getEndsOfEdge(edge) {
|
|
318
|
-
if (!this.hasEdge(edge.
|
|
318
|
+
if (!this.hasEdge(edge.vertexMap[0], edge.vertexMap[1])) {
|
|
319
319
|
return undefined;
|
|
320
320
|
}
|
|
321
|
-
const v1 = this._getVertex(edge.
|
|
322
|
-
const v2 = this._getVertex(edge.
|
|
321
|
+
const v1 = this._getVertex(edge.vertexMap[0]);
|
|
322
|
+
const v2 = this._getVertex(edge.vertexMap[1]);
|
|
323
323
|
if (v1 && v2) {
|
|
324
324
|
return [v1, v2];
|
|
325
325
|
}
|
|
@@ -335,22 +335,22 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
335
335
|
* Time Complexity: O(1)
|
|
336
336
|
* Space Complexity: O(1)
|
|
337
337
|
*
|
|
338
|
-
* The function adds an edge to the graph by updating the adjacency list with the
|
|
338
|
+
* The function adds an edge to the graph by updating the adjacency list with the vertexMap of the edge.
|
|
339
339
|
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
340
340
|
* @returns a boolean value.
|
|
341
341
|
*/
|
|
342
342
|
_addEdgeOnly(edge) {
|
|
343
|
-
for (const end of edge.
|
|
343
|
+
for (const end of edge.vertexMap) {
|
|
344
344
|
const endVertex = this._getVertex(end);
|
|
345
345
|
if (endVertex === undefined)
|
|
346
346
|
return false;
|
|
347
347
|
if (endVertex) {
|
|
348
|
-
const
|
|
349
|
-
if (
|
|
350
|
-
|
|
348
|
+
const edgeMap = this._edgeMap.get(endVertex);
|
|
349
|
+
if (edgeMap) {
|
|
350
|
+
edgeMap.push(edge);
|
|
351
351
|
}
|
|
352
352
|
else {
|
|
353
|
-
this.
|
|
353
|
+
this._edgeMap.set(endVertex, [edge]);
|
|
354
354
|
}
|
|
355
355
|
}
|
|
356
356
|
}
|
|
@@ -5,9 +5,9 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { HashMapLinkedNode, HashMapOptions, HashMapStoreItem
|
|
9
|
-
import {
|
|
10
|
-
export declare class HashMap<K = any, V = any> extends
|
|
8
|
+
import { EntryCallback, HashMapLinkedNode, HashMapOptions, HashMapStoreItem } from '../../types';
|
|
9
|
+
import { IterableEntryBase } from "../base";
|
|
10
|
+
export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
11
11
|
protected _store: {
|
|
12
12
|
[key: string]: HashMapStoreItem<K, V>;
|
|
13
13
|
};
|
|
@@ -85,7 +85,7 @@ export declare class HashMap<K = any, V = any> extends IterablePairBase<K, V> {
|
|
|
85
85
|
* @returns The `map` method is returning a new `HashMap` object with the transformed values based on
|
|
86
86
|
* the provided callback function.
|
|
87
87
|
*/
|
|
88
|
-
map<U>(callbackfn:
|
|
88
|
+
map<U>(callbackfn: EntryCallback<K, V, U>, thisArg?: any): HashMap<K, U>;
|
|
89
89
|
/**
|
|
90
90
|
* Time Complexity: O(n)
|
|
91
91
|
* Space Complexity: O(n)
|
|
@@ -106,7 +106,7 @@ export declare class HashMap<K = any, V = any> extends IterablePairBase<K, V> {
|
|
|
106
106
|
* @returns The `filter` method is returning a new `HashMap` object that contains the key-value pairs
|
|
107
107
|
* from the original `HashMap` that pass the provided `predicate` function.
|
|
108
108
|
*/
|
|
109
|
-
filter(predicate:
|
|
109
|
+
filter(predicate: EntryCallback<K, V, boolean>, thisArg?: any): HashMap<K, V>;
|
|
110
110
|
print(): void;
|
|
111
111
|
/**
|
|
112
112
|
* The function returns an iterator that yields key-value pairs from both an object store and an
|
|
@@ -117,7 +117,7 @@ export declare class HashMap<K = any, V = any> extends IterablePairBase<K, V> {
|
|
|
117
117
|
protected _isObjKey(key: any): key is (object | ((...args: any[]) => any));
|
|
118
118
|
protected _getNoObjKey(key: K): string;
|
|
119
119
|
}
|
|
120
|
-
export declare class LinkedHashMap<K = any, V = any> extends
|
|
120
|
+
export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
121
121
|
protected _noObjMap: Record<string, HashMapLinkedNode<K, V | undefined>>;
|
|
122
122
|
protected _objMap: WeakMap<object, HashMapLinkedNode<K, V | undefined>>;
|
|
123
123
|
protected _head: HashMapLinkedNode<K, V | undefined>;
|
|
@@ -253,7 +253,7 @@ export declare class LinkedHashMap<K = any, V = any> extends IterablePairBase<K,
|
|
|
253
253
|
* @returns a new `LinkedHashMap` object that contains the key-value pairs from the original
|
|
254
254
|
* `LinkedHashMap` object that satisfy the given predicate function.
|
|
255
255
|
*/
|
|
256
|
-
filter(predicate:
|
|
256
|
+
filter(predicate: EntryCallback<K, V, boolean>, thisArg?: any): LinkedHashMap<K, V>;
|
|
257
257
|
/**
|
|
258
258
|
* Time Complexity: O(n)
|
|
259
259
|
* Space Complexity: O(n)
|
|
@@ -275,7 +275,7 @@ export declare class LinkedHashMap<K = any, V = any> extends IterablePairBase<K,
|
|
|
275
275
|
* @returns a new `LinkedHashMap` object with the values mapped according to the provided callback
|
|
276
276
|
* function.
|
|
277
277
|
*/
|
|
278
|
-
map<NV>(callback:
|
|
278
|
+
map<NV>(callback: EntryCallback<K, V, NV>, thisArg?: any): LinkedHashMap<K, NV>;
|
|
279
279
|
print(): void;
|
|
280
280
|
/**
|
|
281
281
|
* Time Complexity: O(n), where n is the number of elements in the LinkedHashMap.
|
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { isWeakKey, rangeCheck } from '../../utils';
|
|
9
|
-
import {
|
|
10
|
-
export class HashMap extends
|
|
9
|
+
import { IterableEntryBase } from "../base";
|
|
10
|
+
export class HashMap extends IterableEntryBase {
|
|
11
11
|
_store = {};
|
|
12
12
|
_objMap = new Map();
|
|
13
13
|
/**
|
|
@@ -225,7 +225,7 @@ export class HashMap extends IterablePairBase {
|
|
|
225
225
|
return strKey;
|
|
226
226
|
}
|
|
227
227
|
}
|
|
228
|
-
export class LinkedHashMap extends
|
|
228
|
+
export class LinkedHashMap extends IterableEntryBase {
|
|
229
229
|
_noObjMap = {};
|
|
230
230
|
_objMap = new WeakMap();
|
|
231
231
|
_head;
|
|
@@ -3,7 +3,7 @@ import { BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BiTreeDelete
|
|
|
3
3
|
export interface IBinaryTree<K = number, V = any, N extends BinaryTreeNode<K, V, N> = BinaryTreeNodeNested<K, V>, TREE extends BinaryTree<K, V, N, TREE> = BinaryTreeNested<K, V, N>> {
|
|
4
4
|
createNode(key: K, value?: N['value']): N;
|
|
5
5
|
createTree(options?: Partial<BinaryTreeOptions<K>>): TREE;
|
|
6
|
-
add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, count?: number): N | null | undefined;
|
|
6
|
+
add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V, count?: number): N | null | undefined;
|
|
7
7
|
addMany(nodes: Iterable<BTNodeExemplar<K, V, N>>): (N | null | undefined)[];
|
|
8
8
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BiTreeDeleteResult<N>[];
|
|
9
9
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { IterableElementBase,
|
|
2
|
-
export type
|
|
1
|
+
import { IterableElementBase, IterableEntryBase } from "../../../data-structures";
|
|
2
|
+
export type EntryCallback<K, V, R> = (value: V, key: K, index: number, container: IterableEntryBase<K, V>) => R;
|
|
3
3
|
export type ElementCallback<V, R> = (element: V, index: number, container: IterableElementBase<V>) => R;
|
|
4
|
-
export type
|
|
4
|
+
export type ReduceEntryCallback<K, V, R> = (accumulator: R, value: V, key: K, index: number, container: IterableEntryBase<K, V>) => R;
|
|
5
5
|
export type ReduceElementCallback<V, R> = (accumulator: R, element: V, index: number, container: IterableElementBase<V>) => R;
|