linked-list-typed 1.48.3 → 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/dist/data-structures/base/iterable-base.d.ts +6 -6
- package/dist/data-structures/base/iterable-base.js +3 -3
- package/dist/data-structures/binary-tree/avl-tree.d.ts +5 -3
- package/dist/data-structures/binary-tree/avl-tree.js +6 -4
- package/dist/data-structures/binary-tree/binary-tree.d.ts +18 -15
- package/dist/data-structures/binary-tree/binary-tree.js +16 -13
- package/dist/data-structures/binary-tree/bst.d.ts +15 -11
- package/dist/data-structures/binary-tree/bst.js +17 -13
- package/dist/data-structures/binary-tree/rb-tree.d.ts +19 -13
- package/dist/data-structures/binary-tree/rb-tree.js +20 -14
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +21 -14
- package/dist/data-structures/binary-tree/tree-multimap.js +25 -18
- package/dist/data-structures/graph/abstract-graph.d.ts +53 -52
- package/dist/data-structures/graph/abstract-graph.js +82 -78
- package/dist/data-structures/graph/directed-graph.d.ts +70 -52
- package/dist/data-structures/graph/directed-graph.js +111 -65
- package/dist/data-structures/graph/map-graph.d.ts +5 -5
- package/dist/data-structures/graph/map-graph.js +8 -8
- package/dist/data-structures/graph/undirected-graph.d.ts +51 -32
- package/dist/data-structures/graph/undirected-graph.js +117 -54
- package/dist/data-structures/hash/hash-map.d.ts +8 -8
- package/dist/data-structures/hash/hash-map.js +2 -2
- package/dist/interfaces/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/base/base.d.ts +3 -3
- package/package.json +2 -2
- 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 +87 -82
- package/src/data-structures/graph/directed-graph.ts +114 -65
- package/src/data-structures/graph/map-graph.ts +8 -8
- package/src/data-structures/graph/undirected-graph.ts +124 -56
- 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
|
@@ -36,20 +36,20 @@ class UndirectedEdge extends abstract_graph_1.AbstractEdge {
|
|
|
36
36
|
*/
|
|
37
37
|
constructor(v1, v2, weight, value) {
|
|
38
38
|
super(weight, value);
|
|
39
|
-
this.
|
|
39
|
+
this.vertexMap = [v1, v2];
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
42
|
exports.UndirectedEdge = UndirectedEdge;
|
|
43
43
|
class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
44
44
|
/**
|
|
45
|
-
* The constructor initializes a new Map object to store
|
|
45
|
+
* The constructor initializes a new Map object to store edgeMap.
|
|
46
46
|
*/
|
|
47
47
|
constructor() {
|
|
48
48
|
super();
|
|
49
|
-
this.
|
|
49
|
+
this._edgeMap = new Map();
|
|
50
50
|
}
|
|
51
|
-
get
|
|
52
|
-
return this.
|
|
51
|
+
get edgeMap() {
|
|
52
|
+
return this._edgeMap;
|
|
53
53
|
}
|
|
54
54
|
/**
|
|
55
55
|
* The function creates a new vertex with an optional value and returns it.
|
|
@@ -64,7 +64,7 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
64
64
|
return new UndirectedVertex(key, value !== null && value !== void 0 ? value : key);
|
|
65
65
|
}
|
|
66
66
|
/**
|
|
67
|
-
* The function creates an undirected edge between two
|
|
67
|
+
* The function creates an undirected edge between two vertexMap with an optional weight and value.
|
|
68
68
|
* @param {VertexKey} v1 - The parameter `v1` represents the first vertex of the edge.
|
|
69
69
|
* @param {VertexKey} v2 - The parameter `v2` represents the second vertex of the edge.
|
|
70
70
|
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge. If
|
|
@@ -77,14 +77,14 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
77
77
|
return new UndirectedEdge(v1, v2, weight !== null && weight !== void 0 ? weight : 1, value);
|
|
78
78
|
}
|
|
79
79
|
/**
|
|
80
|
-
* Time Complexity: O(|E|), where |E| is the number of
|
|
80
|
+
* Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
|
|
81
81
|
* Space Complexity: O(1)
|
|
82
82
|
*/
|
|
83
83
|
/**
|
|
84
|
-
* Time Complexity: O(|E|), where |E| is the number of
|
|
84
|
+
* Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
|
|
85
85
|
* Space Complexity: O(1)
|
|
86
86
|
*
|
|
87
|
-
* The function `getEdge` returns the first edge that connects two
|
|
87
|
+
* The function `getEdge` returns the first edge that connects two vertexMap, or undefined if no such edge exists.
|
|
88
88
|
* @param {VO | VertexKey | undefined} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
89
89
|
* object), `undefined`, or `VertexKey` (a string or number representing the ID of a vertex).
|
|
90
90
|
* @param {VO | VertexKey | undefined} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
@@ -93,29 +93,29 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
93
93
|
*/
|
|
94
94
|
getEdge(v1, v2) {
|
|
95
95
|
var _a;
|
|
96
|
-
let
|
|
96
|
+
let edgeMap = [];
|
|
97
97
|
if (v1 !== undefined && v2 !== undefined) {
|
|
98
98
|
const vertex1 = this._getVertex(v1);
|
|
99
99
|
const vertex2 = this._getVertex(v2);
|
|
100
100
|
if (vertex1 && vertex2) {
|
|
101
|
-
|
|
101
|
+
edgeMap = (_a = this._edgeMap.get(vertex1)) === null || _a === void 0 ? void 0 : _a.filter(e => e.vertexMap.includes(vertex2.key));
|
|
102
102
|
}
|
|
103
103
|
}
|
|
104
|
-
return
|
|
104
|
+
return edgeMap ? edgeMap[0] || undefined : undefined;
|
|
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
110
|
/**
|
|
111
|
-
* Time Complexity: O(|E|), where |E| is the number of
|
|
111
|
+
* Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
|
|
112
112
|
* Space Complexity: O(1)
|
|
113
113
|
*
|
|
114
|
-
* The function removes an edge between two
|
|
114
|
+
* The function removes an edge between two vertexMap in a graph and returns the removed edge.
|
|
115
115
|
* @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
|
|
116
116
|
* @param {VO | VertexKey} v2 - VO | VertexKey - This parameter can be either a vertex object (VO) or a vertex ID
|
|
117
117
|
* (VertexKey). It represents the second vertex of the edge that needs to be removed.
|
|
118
|
-
* @returns the removed edge (EO) if it exists, or undefined if either of the
|
|
118
|
+
* @returns the removed edge (EO) if it exists, or undefined if either of the vertexMap (VO) does not exist.
|
|
119
119
|
*/
|
|
120
120
|
deleteEdgeBetween(v1, v2) {
|
|
121
121
|
const vertex1 = this._getVertex(v1);
|
|
@@ -123,31 +123,94 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
123
123
|
if (!vertex1 || !vertex2) {
|
|
124
124
|
return undefined;
|
|
125
125
|
}
|
|
126
|
-
const v1Edges = this.
|
|
126
|
+
const v1Edges = this._edgeMap.get(vertex1);
|
|
127
127
|
let removed = undefined;
|
|
128
128
|
if (v1Edges) {
|
|
129
|
-
removed = (0, utils_1.arrayRemove)(v1Edges, (e) => e.
|
|
129
|
+
removed = (0, utils_1.arrayRemove)(v1Edges, (e) => e.vertexMap.includes(vertex2.key))[0] || undefined;
|
|
130
130
|
}
|
|
131
|
-
const v2Edges = this.
|
|
131
|
+
const v2Edges = this._edgeMap.get(vertex2);
|
|
132
132
|
if (v2Edges) {
|
|
133
|
-
(0, utils_1.arrayRemove)(v2Edges, (e) => e.
|
|
133
|
+
(0, utils_1.arrayRemove)(v2Edges, (e) => e.vertexMap.includes(vertex1.key));
|
|
134
134
|
}
|
|
135
135
|
return removed;
|
|
136
136
|
}
|
|
137
137
|
/**
|
|
138
|
-
* Time Complexity: O(
|
|
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
141
|
/**
|
|
142
|
-
* Time Complexity: O(
|
|
142
|
+
* Time Complexity: O(E), where E is the number of edgeMap incident to the given vertex.
|
|
143
143
|
* Space Complexity: O(1)
|
|
144
144
|
*
|
|
145
|
-
* The deleteEdge
|
|
146
|
-
* @param {EO}
|
|
147
|
-
*
|
|
145
|
+
* The function `deleteEdge` deletes an edge between two vertexMap in a graph.
|
|
146
|
+
* @param {EO | VertexKey} edgeOrOneSideVertexKey - The parameter `edgeOrOneSideVertexKey` can be
|
|
147
|
+
* either an edge object or a vertex key.
|
|
148
|
+
* @param {VertexKey} [otherSideVertexKey] - The parameter `otherSideVertexKey` is an optional
|
|
149
|
+
* parameter that represents the key of the vertex on the other side of the edge. It is used when the
|
|
150
|
+
* `edgeOrOneSideVertexKey` parameter is a vertex key, and it specifies the key of the vertex on the
|
|
151
|
+
* other side of the
|
|
152
|
+
* @returns The `deleteEdge` function returns either the deleted edge object (EO) or `undefined`.
|
|
153
|
+
*/
|
|
154
|
+
deleteEdge(edgeOrOneSideVertexKey, otherSideVertexKey) {
|
|
155
|
+
let oneSide, otherSide;
|
|
156
|
+
if (this.isVertexKey(edgeOrOneSideVertexKey)) {
|
|
157
|
+
if (this.isVertexKey(otherSideVertexKey)) {
|
|
158
|
+
oneSide = this._getVertex(edgeOrOneSideVertexKey);
|
|
159
|
+
otherSide = this._getVertex(otherSideVertexKey);
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
oneSide = this._getVertex(edgeOrOneSideVertexKey.vertexMap[0]);
|
|
167
|
+
otherSide = this._getVertex(edgeOrOneSideVertexKey.vertexMap[1]);
|
|
168
|
+
}
|
|
169
|
+
if (oneSide && otherSide) {
|
|
170
|
+
return this.deleteEdgeBetween(oneSide, otherSide);
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Time Complexity: O(1) - Constant time for Map operations.
|
|
178
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
148
179
|
*/
|
|
149
|
-
|
|
150
|
-
|
|
180
|
+
/**
|
|
181
|
+
* Time Complexity: O(1) - Constant time for Map operations.
|
|
182
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
183
|
+
*
|
|
184
|
+
* The `deleteVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
|
|
185
|
+
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
186
|
+
* (`VertexKey`).
|
|
187
|
+
* @returns The method is returning a boolean value.
|
|
188
|
+
*/
|
|
189
|
+
deleteVertex(vertexOrKey) {
|
|
190
|
+
let vertexKey;
|
|
191
|
+
let vertex;
|
|
192
|
+
if (this.isVertexKey(vertexOrKey)) {
|
|
193
|
+
vertex = this.getVertex(vertexOrKey);
|
|
194
|
+
vertexKey = vertexOrKey;
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
vertex = vertexOrKey;
|
|
198
|
+
vertexKey = this._getVertexKey(vertexOrKey);
|
|
199
|
+
}
|
|
200
|
+
const neighbors = this.getNeighbors(vertexOrKey);
|
|
201
|
+
if (vertex) {
|
|
202
|
+
neighbors.forEach(neighbor => {
|
|
203
|
+
const neighborEdges = this._edgeMap.get(neighbor);
|
|
204
|
+
if (neighborEdges) {
|
|
205
|
+
const restEdges = neighborEdges.filter(edge => {
|
|
206
|
+
return !edge.vertexMap.includes(vertexKey);
|
|
207
|
+
});
|
|
208
|
+
this._edgeMap.set(neighbor, restEdges);
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
this._edgeMap.delete(vertex);
|
|
212
|
+
}
|
|
213
|
+
return this._vertexMap.delete(vertexKey);
|
|
151
214
|
}
|
|
152
215
|
/**
|
|
153
216
|
* Time Complexity: O(1)
|
|
@@ -157,17 +220,17 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
157
220
|
* Time Complexity: O(1)
|
|
158
221
|
* Space Complexity: O(1)
|
|
159
222
|
*
|
|
160
|
-
* The function `degreeOf` returns the degree of a vertex in a graph, which is the number of
|
|
223
|
+
* The function `degreeOf` returns the degree of a vertex in a graph, which is the number of edgeMap connected to that
|
|
161
224
|
* vertex.
|
|
162
225
|
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
|
|
163
226
|
* @returns The function `degreeOf` returns the degree of a vertex in a graph. The degree of a vertex is the number of
|
|
164
|
-
*
|
|
227
|
+
* edgeMap connected to that vertex.
|
|
165
228
|
*/
|
|
166
229
|
degreeOf(vertexOrKey) {
|
|
167
230
|
var _a;
|
|
168
231
|
const vertex = this._getVertex(vertexOrKey);
|
|
169
232
|
if (vertex) {
|
|
170
|
-
return ((_a = this.
|
|
233
|
+
return ((_a = this._edgeMap.get(vertex)) === null || _a === void 0 ? void 0 : _a.length) || 0;
|
|
171
234
|
}
|
|
172
235
|
else {
|
|
173
236
|
return 0;
|
|
@@ -181,52 +244,52 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
181
244
|
* Time Complexity: O(1)
|
|
182
245
|
* Space Complexity: O(1)
|
|
183
246
|
*
|
|
184
|
-
* The function returns the
|
|
247
|
+
* The function returns the edgeMap of a given vertex or vertex ID.
|
|
185
248
|
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`. A `VertexKey` is a
|
|
186
249
|
* unique identifier for a vertex in a graph, while `VO` represents the type of the vertex.
|
|
187
|
-
* @returns an array of
|
|
250
|
+
* @returns an array of edgeMap.
|
|
188
251
|
*/
|
|
189
252
|
edgesOf(vertexOrKey) {
|
|
190
253
|
const vertex = this._getVertex(vertexOrKey);
|
|
191
254
|
if (vertex) {
|
|
192
|
-
return this.
|
|
255
|
+
return this._edgeMap.get(vertex) || [];
|
|
193
256
|
}
|
|
194
257
|
else {
|
|
195
258
|
return [];
|
|
196
259
|
}
|
|
197
260
|
}
|
|
198
261
|
/**
|
|
199
|
-
* Time Complexity: O(|V| + |E|), where |V| is the number of
|
|
262
|
+
* Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
|
|
200
263
|
* Space Complexity: O(|E|)
|
|
201
264
|
*/
|
|
202
265
|
/**
|
|
203
|
-
* Time Complexity: O(|V| + |E|), where |V| is the number of
|
|
266
|
+
* Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
|
|
204
267
|
* Space Complexity: O(|E|)
|
|
205
268
|
*
|
|
206
|
-
* The function "edgeSet" returns an array of unique
|
|
269
|
+
* The function "edgeSet" returns an array of unique edgeMap from a set of edgeMap.
|
|
207
270
|
* @returns The method `edgeSet()` returns an array of type `EO[]`.
|
|
208
271
|
*/
|
|
209
272
|
edgeSet() {
|
|
210
273
|
const edgeSet = new Set();
|
|
211
|
-
this.
|
|
212
|
-
|
|
274
|
+
this._edgeMap.forEach(edgeMap => {
|
|
275
|
+
edgeMap.forEach(edge => {
|
|
213
276
|
edgeSet.add(edge);
|
|
214
277
|
});
|
|
215
278
|
});
|
|
216
279
|
return [...edgeSet];
|
|
217
280
|
}
|
|
218
281
|
/**
|
|
219
|
-
* Time Complexity: O(|V| + |E|), where |V| is the number of
|
|
282
|
+
* Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
|
|
220
283
|
* Space Complexity: O(|E|)
|
|
221
284
|
*/
|
|
222
285
|
/**
|
|
223
|
-
* Time Complexity: O(|V| + |E|), where |V| is the number of
|
|
286
|
+
* Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
|
|
224
287
|
* Space Complexity: O(|E|)
|
|
225
288
|
*
|
|
226
|
-
* The function "getNeighbors" returns an array of neighboring
|
|
289
|
+
* The function "getNeighbors" returns an array of neighboring vertexMap for a given vertex or vertex ID.
|
|
227
290
|
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
228
291
|
* (`VertexKey`).
|
|
229
|
-
* @returns an array of
|
|
292
|
+
* @returns an array of vertexMap (VO[]).
|
|
230
293
|
*/
|
|
231
294
|
getNeighbors(vertexOrKey) {
|
|
232
295
|
const neighbors = [];
|
|
@@ -234,7 +297,7 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
234
297
|
if (vertex) {
|
|
235
298
|
const neighborEdges = this.edgesOf(vertex);
|
|
236
299
|
for (const edge of neighborEdges) {
|
|
237
|
-
const neighbor = this._getVertex(edge.
|
|
300
|
+
const neighbor = this._getVertex(edge.vertexMap.filter(e => e !== vertex.key)[0]);
|
|
238
301
|
if (neighbor) {
|
|
239
302
|
neighbors.push(neighbor);
|
|
240
303
|
}
|
|
@@ -250,18 +313,18 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
250
313
|
* Time Complexity: O(1)
|
|
251
314
|
* Space Complexity: O(1)
|
|
252
315
|
*
|
|
253
|
-
* The function "getEndsOfEdge" returns the
|
|
316
|
+
* The function "getEndsOfEdge" returns the vertexMap at the ends of an edge if the edge exists in the graph, otherwise
|
|
254
317
|
* it returns undefined.
|
|
255
318
|
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
256
|
-
* @returns The function `getEndsOfEdge` returns an array containing two
|
|
319
|
+
* @returns The function `getEndsOfEdge` returns an array containing two vertexMap `[VO, VO]` if the edge exists in the
|
|
257
320
|
* graph. If the edge does not exist, it returns `undefined`.
|
|
258
321
|
*/
|
|
259
322
|
getEndsOfEdge(edge) {
|
|
260
|
-
if (!this.hasEdge(edge.
|
|
323
|
+
if (!this.hasEdge(edge.vertexMap[0], edge.vertexMap[1])) {
|
|
261
324
|
return undefined;
|
|
262
325
|
}
|
|
263
|
-
const v1 = this._getVertex(edge.
|
|
264
|
-
const v2 = this._getVertex(edge.
|
|
326
|
+
const v1 = this._getVertex(edge.vertexMap[0]);
|
|
327
|
+
const v2 = this._getVertex(edge.vertexMap[1]);
|
|
265
328
|
if (v1 && v2) {
|
|
266
329
|
return [v1, v2];
|
|
267
330
|
}
|
|
@@ -277,22 +340,22 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
277
340
|
* Time Complexity: O(1)
|
|
278
341
|
* Space Complexity: O(1)
|
|
279
342
|
*
|
|
280
|
-
* The function adds an edge to the graph by updating the adjacency list with the
|
|
343
|
+
* The function adds an edge to the graph by updating the adjacency list with the vertexMap of the edge.
|
|
281
344
|
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
282
345
|
* @returns a boolean value.
|
|
283
346
|
*/
|
|
284
347
|
_addEdgeOnly(edge) {
|
|
285
|
-
for (const end of edge.
|
|
348
|
+
for (const end of edge.vertexMap) {
|
|
286
349
|
const endVertex = this._getVertex(end);
|
|
287
350
|
if (endVertex === undefined)
|
|
288
351
|
return false;
|
|
289
352
|
if (endVertex) {
|
|
290
|
-
const
|
|
291
|
-
if (
|
|
292
|
-
|
|
353
|
+
const edgeMap = this._edgeMap.get(endVertex);
|
|
354
|
+
if (edgeMap) {
|
|
355
|
+
edgeMap.push(edge);
|
|
293
356
|
}
|
|
294
357
|
else {
|
|
295
|
-
this.
|
|
358
|
+
this._edgeMap.set(endVertex, [edge]);
|
|
296
359
|
}
|
|
297
360
|
}
|
|
298
361
|
}
|
|
@@ -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.
|
|
@@ -10,7 +10,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
10
10
|
exports.LinkedHashMap = exports.HashMap = void 0;
|
|
11
11
|
const utils_1 = require("../../utils");
|
|
12
12
|
const base_1 = require("../base");
|
|
13
|
-
class HashMap extends base_1.
|
|
13
|
+
class HashMap extends base_1.IterableEntryBase {
|
|
14
14
|
/**
|
|
15
15
|
* The constructor function initializes a new instance of a class with optional elements and options.
|
|
16
16
|
* @param elements - The `elements` parameter is an iterable containing key-value pairs `[K, V]`. It
|
|
@@ -230,7 +230,7 @@ class HashMap extends base_1.IterablePairBase {
|
|
|
230
230
|
}
|
|
231
231
|
}
|
|
232
232
|
exports.HashMap = HashMap;
|
|
233
|
-
class LinkedHashMap extends base_1.
|
|
233
|
+
class LinkedHashMap extends base_1.IterableEntryBase {
|
|
234
234
|
constructor(elements, options = {
|
|
235
235
|
hashFn: (key) => String(key),
|
|
236
236
|
objHashFn: (key) => key
|
|
@@ -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;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "linked-list-typed",
|
|
3
|
-
"version": "1.48.
|
|
3
|
+
"version": "1.48.5",
|
|
4
4
|
"description": "Linked List, Doubly Linked List, Singly Linked List. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -66,6 +66,6 @@
|
|
|
66
66
|
"typescript": "^4.9.5"
|
|
67
67
|
},
|
|
68
68
|
"dependencies": {
|
|
69
|
-
"data-structure-typed": "^1.48.
|
|
69
|
+
"data-structure-typed": "^1.48.5"
|
|
70
70
|
}
|
|
71
71
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { ElementCallback,
|
|
1
|
+
import { ElementCallback, EntryCallback, ReduceElementCallback, ReduceEntryCallback } from "../../types";
|
|
2
2
|
|
|
3
|
-
export abstract class
|
|
3
|
+
export abstract class IterableEntryBase<K = any, V = any> {
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Time Complexity: O(n)
|
|
@@ -87,7 +87,7 @@ export abstract class IterablePairBase<K = any, V = any> {
|
|
|
87
87
|
* @returns The `every` method is returning a boolean value. It returns `true` if every element in
|
|
88
88
|
* the collection satisfies the provided predicate function, and `false` otherwise.
|
|
89
89
|
*/
|
|
90
|
-
every(predicate:
|
|
90
|
+
every(predicate: EntryCallback<K, V, boolean>, thisArg?: any): boolean {
|
|
91
91
|
let index = 0;
|
|
92
92
|
for (const item of this) {
|
|
93
93
|
if (!predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
@@ -116,7 +116,7 @@ export abstract class IterablePairBase<K = any, V = any> {
|
|
|
116
116
|
* @returns a boolean value. It returns true if the predicate function returns true for any pair in
|
|
117
117
|
* the collection, and false otherwise.
|
|
118
118
|
*/
|
|
119
|
-
some(predicate:
|
|
119
|
+
some(predicate: EntryCallback<K, V, boolean>, thisArg?: any): boolean {
|
|
120
120
|
let index = 0;
|
|
121
121
|
for (const item of this) {
|
|
122
122
|
if (predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
@@ -143,7 +143,7 @@ export abstract class IterablePairBase<K = any, V = any> {
|
|
|
143
143
|
* specify the value of `this` within the callback function. If `thisArg` is provided, it will be
|
|
144
144
|
* used as the `this` value when calling the callback function. If `thisArg` is not provided, `
|
|
145
145
|
*/
|
|
146
|
-
forEach(callbackfn:
|
|
146
|
+
forEach(callbackfn: EntryCallback<K, V, void>, thisArg?: any): void {
|
|
147
147
|
let index = 0;
|
|
148
148
|
for (const item of this) {
|
|
149
149
|
const [key, value] = item;
|
|
@@ -171,7 +171,7 @@ export abstract class IterablePairBase<K = any, V = any> {
|
|
|
171
171
|
* @returns The `reduce` method is returning the final value of the accumulator after iterating over
|
|
172
172
|
* all the elements in the collection.
|
|
173
173
|
*/
|
|
174
|
-
reduce<U>(callbackfn:
|
|
174
|
+
reduce<U>(callbackfn: ReduceEntryCallback<K, V, U>, initialValue: U): U {
|
|
175
175
|
let accumulator = initialValue;
|
|
176
176
|
let index = 0;
|
|
177
177
|
for (const item of this) {
|
|
@@ -95,19 +95,22 @@ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeN
|
|
|
95
95
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
96
96
|
*/
|
|
97
97
|
|
|
98
|
+
|
|
98
99
|
/**
|
|
99
100
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
100
101
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
101
|
-
*
|
|
102
|
+
*
|
|
102
103
|
* The function overrides the add method of a binary tree node and balances the tree after inserting
|
|
103
104
|
* a new node.
|
|
104
|
-
* @param keyOrNodeOrEntry - The
|
|
105
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
|
|
105
106
|
* entry.
|
|
106
|
-
* @
|
|
107
|
+
* @param {V} [value] - The `value` parameter represents the value associated with the key that is
|
|
108
|
+
* being added to the binary tree.
|
|
109
|
+
* @returns The method is returning either the inserted node or undefined.
|
|
107
110
|
*/
|
|
108
|
-
override add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N
|
|
111
|
+
override add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V): N | undefined {
|
|
109
112
|
if (keyOrNodeOrEntry === null) return undefined;
|
|
110
|
-
const inserted = super.add(keyOrNodeOrEntry);
|
|
113
|
+
const inserted = super.add(keyOrNodeOrEntry, value);
|
|
111
114
|
if (inserted) this._balancePath(inserted);
|
|
112
115
|
return inserted;
|
|
113
116
|
}
|
|
@@ -19,15 +19,15 @@ import {
|
|
|
19
19
|
BinaryTreePrintOptions,
|
|
20
20
|
BiTreeDeleteResult,
|
|
21
21
|
DFSOrderPattern,
|
|
22
|
+
EntryCallback,
|
|
22
23
|
FamilyPosition,
|
|
23
24
|
IterationType,
|
|
24
|
-
NodeDisplayLayout
|
|
25
|
-
PairCallback
|
|
25
|
+
NodeDisplayLayout
|
|
26
26
|
} from '../../types';
|
|
27
27
|
import { IBinaryTree } from '../../interfaces';
|
|
28
28
|
import { trampoline } from '../../utils';
|
|
29
29
|
import { Queue } from '../queue';
|
|
30
|
-
import {
|
|
30
|
+
import { IterableEntryBase } from "../base";
|
|
31
31
|
|
|
32
32
|
/**
|
|
33
33
|
* Represents a node in a binary tree.
|
|
@@ -104,7 +104,7 @@ export class BinaryTreeNode<K = any, V = any, N extends BinaryTreeNode<K, V, N>
|
|
|
104
104
|
* 9. Complete Trees: All levels are fully filled except possibly the last, filled from left to right.
|
|
105
105
|
*/
|
|
106
106
|
|
|
107
|
-
export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>, TREE extends BinaryTree<K, V, N, TREE> = BinaryTree<K, V, N, BinaryTreeNested<K, V, N>>> extends
|
|
107
|
+
export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>, TREE extends BinaryTree<K, V, N, TREE> = BinaryTree<K, V, N, BinaryTreeNested<K, V, N>>> extends IterableEntryBase<K, V | undefined>
|
|
108
108
|
|
|
109
109
|
implements IBinaryTree<K, V, N, TREE> {
|
|
110
110
|
iterationType = IterationType.ITERATIVE
|
|
@@ -184,13 +184,14 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
184
184
|
}
|
|
185
185
|
|
|
186
186
|
/**
|
|
187
|
-
* The function `exemplarToNode` converts an exemplar
|
|
188
|
-
*
|
|
189
|
-
* @param
|
|
190
|
-
* function. It
|
|
191
|
-
*
|
|
187
|
+
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
188
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
|
|
189
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
190
|
+
* `exemplarToNode` function. It represents the value associated with the exemplar node. If no value
|
|
191
|
+
* is provided, it will be `undefined`.
|
|
192
|
+
* @returns a value of type N (node), or null, or undefined.
|
|
192
193
|
*/
|
|
193
|
-
exemplarToNode(exemplar: BTNodeExemplar<K, V, N
|
|
194
|
+
exemplarToNode(exemplar: BTNodeExemplar<K, V, N>, value?: V): N | null | undefined {
|
|
194
195
|
if (exemplar === undefined) return;
|
|
195
196
|
|
|
196
197
|
let node: N | null | undefined;
|
|
@@ -208,7 +209,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
208
209
|
} else if (this.isNode(exemplar)) {
|
|
209
210
|
node = exemplar;
|
|
210
211
|
} else if (this.isNotNodeInstance(exemplar)) {
|
|
211
|
-
node = this.createNode(exemplar);
|
|
212
|
+
node = this.createNode(exemplar, value);
|
|
212
213
|
} else {
|
|
213
214
|
return;
|
|
214
215
|
}
|
|
@@ -230,18 +231,21 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
230
231
|
* Space Complexity O(1)
|
|
231
232
|
*/
|
|
232
233
|
|
|
234
|
+
|
|
233
235
|
/**
|
|
234
236
|
* Time Complexity O(log n) - O(n)
|
|
235
237
|
* Space Complexity O(1)
|
|
236
|
-
*
|
|
237
|
-
* The `add` function adds a new node to a binary tree, either by
|
|
238
|
-
*
|
|
239
|
-
* @
|
|
238
|
+
*
|
|
239
|
+
* The `add` function adds a new node to a binary tree, either by creating a new node or replacing an
|
|
240
|
+
* existing node with the same key.
|
|
241
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
|
|
242
|
+
* @param {V} [value] - The value to be inserted into the binary tree.
|
|
243
|
+
* @returns The function `add` returns either a node (`N`), `null`, or `undefined`.
|
|
240
244
|
*/
|
|
241
|
-
add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N
|
|
245
|
+
add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V): N | null | undefined {
|
|
242
246
|
|
|
243
247
|
let inserted: N | null | undefined;
|
|
244
|
-
const newNode = this.exemplarToNode(keyOrNodeOrEntry);
|
|
248
|
+
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
245
249
|
if (newNode === undefined) return;
|
|
246
250
|
|
|
247
251
|
const _bfs = (root: N, newNode: N | null): N | undefined | null => {
|
|
@@ -1775,7 +1779,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
1775
1779
|
* @returns The `filter` method is returning a new tree object that contains the key-value pairs that
|
|
1776
1780
|
* pass the given predicate function.
|
|
1777
1781
|
*/
|
|
1778
|
-
filter(predicate:
|
|
1782
|
+
filter(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: any) {
|
|
1779
1783
|
const newTree = this.createTree();
|
|
1780
1784
|
let index = 0;
|
|
1781
1785
|
for (const [key, value] of this) {
|
|
@@ -1806,7 +1810,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
1806
1810
|
* will be used as the `this` value when the callback function is called. If you don't pass a value
|
|
1807
1811
|
* @returns The `map` method is returning a new tree object.
|
|
1808
1812
|
*/
|
|
1809
|
-
map(callback:
|
|
1813
|
+
map(callback: EntryCallback<K, V | undefined, V>, thisArg?: any) {
|
|
1810
1814
|
const newTree = this.createTree();
|
|
1811
1815
|
let index = 0;
|
|
1812
1816
|
for (const [key, value] of this) {
|