data-structure-typed 1.49.2 → 1.49.4
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/README.md +69 -66
- package/README_zh-CN.md +43 -48
- package/benchmark/report.html +16 -16
- package/benchmark/report.json +187 -187
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +8 -17
- package/dist/cjs/data-structures/graph/abstract-graph.js +43 -29
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +2 -2
- package/dist/cjs/data-structures/graph/directed-graph.js +6 -2
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.js +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +43 -43
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +49 -49
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +25 -25
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +36 -36
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +33 -33
- package/dist/cjs/data-structures/queue/queue.js +40 -40
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +1 -1
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +8 -17
- package/dist/mjs/data-structures/graph/abstract-graph.js +43 -29
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +2 -2
- package/dist/mjs/data-structures/graph/directed-graph.js +6 -2
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +1 -1
- package/dist/mjs/data-structures/graph/undirected-graph.js +1 -1
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +43 -43
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +47 -47
- package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +25 -25
- package/dist/mjs/data-structures/linked-list/skip-linked-list.js +36 -36
- package/dist/mjs/data-structures/queue/queue.d.ts +33 -33
- package/dist/mjs/data-structures/queue/queue.js +39 -39
- package/dist/umd/data-structure-typed.js +176 -158
- 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/binary-tree.ts +1 -1
- package/src/data-structures/graph/abstract-graph.ts +56 -27
- package/src/data-structures/graph/directed-graph.ts +10 -5
- package/src/data-structures/graph/undirected-graph.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +53 -53
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -3
- package/src/data-structures/linked-list/skip-linked-list.ts +40 -40
- package/src/data-structures/queue/queue.ts +45 -45
- package/test/performance/data-structures/comparison/comparison.test.ts +12 -12
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +16 -27
- package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +4 -12
- package/test/performance/data-structures/queue/deque.test.ts +8 -8
- package/test/performance/data-structures/queue/queue.test.ts +5 -5
- package/test/performance/data-structures/stack/stack.test.ts +11 -11
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +15 -0
- package/test/unit/data-structures/graph/abstract-graph.test.ts +12 -1
- package/test/unit/data-structures/graph/directed-graph.test.ts +63 -5
- package/test/unit/data-structures/graph/undirected-graph.test.ts +61 -4
- package/test/unit/data-structures/hash/hash-map.test.ts +21 -0
- package/test/unit/data-structures/heap/heap.test.ts +6 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +27 -0
- package/test/utils/big-o.ts +14 -14
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "data-structure-typed",
|
|
3
|
-
"version": "1.49.
|
|
3
|
+
"version": "1.49.4",
|
|
4
4
|
"description": "Data Structures of Javascript & TypeScript. Heap, Binary Tree, Red Black Tree, Linked List, Deque, Trie, HashMap, Directed Graph, Undirected Graph, Binary Search Tree(BST), AVL Tree, Priority Queue, Graph, Queue, Tree Multiset, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue, Stack. Benchmark compared with C++ STL. API aligned with ES6 and Java.util. Usability is comparable to Python",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/mjs/index.js",
|
|
@@ -1957,7 +1957,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
1957
1957
|
}
|
|
1958
1958
|
}
|
|
1959
1959
|
|
|
1960
|
-
protected _defaultOneParamCallback = (node: N) => node.key;
|
|
1960
|
+
protected _defaultOneParamCallback = (node: N | null | undefined) => node ? node.key : undefined;
|
|
1961
1961
|
|
|
1962
1962
|
/**
|
|
1963
1963
|
* Swap the data of two nodes in the binary tree.
|
|
@@ -156,10 +156,10 @@ export abstract class AbstractGraph<
|
|
|
156
156
|
|
|
157
157
|
addVertex(keyOrVertex: VertexKey | VO, value?: V): boolean {
|
|
158
158
|
if (keyOrVertex instanceof AbstractVertex) {
|
|
159
|
-
return this.
|
|
159
|
+
return this._addVertex(keyOrVertex);
|
|
160
160
|
} else {
|
|
161
161
|
const newVertex = this.createVertex(keyOrVertex, value);
|
|
162
|
-
return this.
|
|
162
|
+
return this._addVertex(newVertex);
|
|
163
163
|
}
|
|
164
164
|
}
|
|
165
165
|
|
|
@@ -173,19 +173,7 @@ export abstract class AbstractGraph<
|
|
|
173
173
|
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
174
174
|
*/
|
|
175
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.
|
|
179
|
-
*
|
|
180
|
-
* The `deleteVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
|
|
181
|
-
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
182
|
-
* (`VertexKey`).
|
|
183
|
-
* @returns The method is returning a boolean value.
|
|
184
|
-
*/
|
|
185
|
-
deleteVertex(vertexOrKey: VO | VertexKey): boolean {
|
|
186
|
-
const vertexKey = this._getVertexKey(vertexOrKey);
|
|
187
|
-
return this._vertexMap.delete(vertexKey);
|
|
188
|
-
}
|
|
176
|
+
abstract deleteVertex(vertexOrKey: VO | VertexKey): boolean;
|
|
189
177
|
|
|
190
178
|
/**
|
|
191
179
|
* Time Complexity: O(K), where K is the number of vertexMap to be removed.
|
|
@@ -242,14 +230,14 @@ export abstract class AbstractGraph<
|
|
|
242
230
|
|
|
243
231
|
addEdge(srcOrEdge: VO | VertexKey | EO, dest?: VO | VertexKey, weight?: number, value?: E): boolean {
|
|
244
232
|
if (srcOrEdge instanceof AbstractEdge) {
|
|
245
|
-
return this.
|
|
233
|
+
return this._addEdge(srcOrEdge);
|
|
246
234
|
} else {
|
|
247
235
|
if (dest instanceof AbstractVertex || typeof dest === 'string' || typeof dest === 'number') {
|
|
248
236
|
if (!(this.hasVertex(srcOrEdge) && this.hasVertex(dest))) return false;
|
|
249
237
|
if (srcOrEdge instanceof AbstractVertex) srcOrEdge = srcOrEdge.key;
|
|
250
238
|
if (dest instanceof AbstractVertex) dest = dest.key;
|
|
251
239
|
const newEdge = this.createEdge(srcOrEdge, dest, weight, value);
|
|
252
|
-
return this.
|
|
240
|
+
return this._addEdge(newEdge);
|
|
253
241
|
} else {
|
|
254
242
|
throw new Error('dest must be a Vertex or vertex key while srcOrEdge is an Edge');
|
|
255
243
|
}
|
|
@@ -1147,14 +1135,6 @@ export abstract class AbstractGraph<
|
|
|
1147
1135
|
return this.tarjan(false, false, false, false).lowMap;
|
|
1148
1136
|
}
|
|
1149
1137
|
|
|
1150
|
-
/**
|
|
1151
|
-
* The function `getCycles` returns a map of cycles found using the Tarjan algorithm.
|
|
1152
|
-
* @returns The function `getCycles()` is returning a `Map<number, VO[]>`.
|
|
1153
|
-
*/
|
|
1154
|
-
getCycles(): Map<number, VO[]> {
|
|
1155
|
-
return this.tarjan(false, false, false, true).cycles;
|
|
1156
|
-
}
|
|
1157
|
-
|
|
1158
1138
|
/**
|
|
1159
1139
|
* The function "getCutVertexes" returns an array of cut vertexes using the Tarjan algorithm.
|
|
1160
1140
|
* @returns an array of VO objects, specifically the cut vertexes.
|
|
@@ -1180,6 +1160,55 @@ export abstract class AbstractGraph<
|
|
|
1180
1160
|
return this.tarjan(false, true, false, false).bridges;
|
|
1181
1161
|
}
|
|
1182
1162
|
|
|
1163
|
+
/**
|
|
1164
|
+
* O(V+E+C)
|
|
1165
|
+
* O(V+C)
|
|
1166
|
+
*/
|
|
1167
|
+
getCycles(isInclude2Cycle: boolean = false): VertexKey[][] {
|
|
1168
|
+
const cycles: VertexKey[][] = [];
|
|
1169
|
+
const visited: Set<VO> = new Set();
|
|
1170
|
+
|
|
1171
|
+
const dfs = (vertex: VO, currentPath: VertexKey[], visited: Set<VO>) => {
|
|
1172
|
+
if (visited.has(vertex)) {
|
|
1173
|
+
if ((!isInclude2Cycle && currentPath.length > 2 || isInclude2Cycle && currentPath.length >= 2) && currentPath[0] === vertex.key) {
|
|
1174
|
+
cycles.push([...currentPath]);
|
|
1175
|
+
}
|
|
1176
|
+
return;
|
|
1177
|
+
}
|
|
1178
|
+
|
|
1179
|
+
visited.add(vertex);
|
|
1180
|
+
currentPath.push(vertex.key);
|
|
1181
|
+
|
|
1182
|
+
for (const neighbor of this.getNeighbors(vertex)) {
|
|
1183
|
+
neighbor && dfs(neighbor, currentPath, visited);
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1186
|
+
visited.delete(vertex);
|
|
1187
|
+
currentPath.pop();
|
|
1188
|
+
};
|
|
1189
|
+
|
|
1190
|
+
for (const vertex of this.vertexMap.values()) {
|
|
1191
|
+
dfs(vertex, [], visited);
|
|
1192
|
+
}
|
|
1193
|
+
|
|
1194
|
+
// Use a set to eliminate duplicate cycles
|
|
1195
|
+
const uniqueCycles = new Map<string, VertexKey[]>();
|
|
1196
|
+
|
|
1197
|
+
for (const cycle of cycles) {
|
|
1198
|
+
const sorted = [...cycle].sort().toString()
|
|
1199
|
+
|
|
1200
|
+
if (uniqueCycles.has(sorted)) continue
|
|
1201
|
+
else {
|
|
1202
|
+
uniqueCycles.set(sorted, cycle)
|
|
1203
|
+
}
|
|
1204
|
+
}
|
|
1205
|
+
|
|
1206
|
+
// Convert the unique cycles back to an array
|
|
1207
|
+
return [...uniqueCycles].map(cycleString =>
|
|
1208
|
+
cycleString[1]
|
|
1209
|
+
);
|
|
1210
|
+
}
|
|
1211
|
+
|
|
1183
1212
|
/**
|
|
1184
1213
|
* Time Complexity: O(n)
|
|
1185
1214
|
* Space Complexity: O(n)
|
|
@@ -1247,9 +1276,9 @@ export abstract class AbstractGraph<
|
|
|
1247
1276
|
}
|
|
1248
1277
|
}
|
|
1249
1278
|
|
|
1250
|
-
protected abstract
|
|
1279
|
+
protected abstract _addEdge(edge: EO): boolean;
|
|
1251
1280
|
|
|
1252
|
-
protected
|
|
1281
|
+
protected _addVertex(newVertex: VO): boolean {
|
|
1253
1282
|
if (this.hasVertex(newVertex)) {
|
|
1254
1283
|
return false;
|
|
1255
1284
|
// throw (new Error('Duplicated vertex key is not allowed'));
|
|
@@ -240,7 +240,7 @@ export class DirectedGraph<
|
|
|
240
240
|
* (`VertexKey`).
|
|
241
241
|
* @returns The method is returning a boolean value.
|
|
242
242
|
*/
|
|
243
|
-
|
|
243
|
+
deleteVertex(vertexOrKey: VO | VertexKey): boolean {
|
|
244
244
|
let vertexKey: VertexKey;
|
|
245
245
|
let vertex: VO | undefined;
|
|
246
246
|
if (this.isVertexKey(vertexOrKey)) {
|
|
@@ -252,8 +252,12 @@ export class DirectedGraph<
|
|
|
252
252
|
}
|
|
253
253
|
|
|
254
254
|
if (vertex) {
|
|
255
|
-
this.
|
|
256
|
-
|
|
255
|
+
const neighbors = this.getNeighbors(vertex);
|
|
256
|
+
for (const neighbor of neighbors) {
|
|
257
|
+
this._inEdgeMap.delete(neighbor);
|
|
258
|
+
}
|
|
259
|
+
this._outEdgeMap.delete(vertex);
|
|
260
|
+
this._inEdgeMap.delete(vertex);
|
|
257
261
|
}
|
|
258
262
|
|
|
259
263
|
return this._vertexMap.delete(vertexKey);
|
|
@@ -596,6 +600,7 @@ export class DirectedGraph<
|
|
|
596
600
|
}
|
|
597
601
|
}
|
|
598
602
|
|
|
603
|
+
|
|
599
604
|
/**
|
|
600
605
|
* Time Complexity: O(1)
|
|
601
606
|
* Space Complexity: O(1)
|
|
@@ -605,13 +610,13 @@ export class DirectedGraph<
|
|
|
605
610
|
* Time Complexity: O(1)
|
|
606
611
|
* Space Complexity: O(1)
|
|
607
612
|
*
|
|
608
|
-
* The function `
|
|
613
|
+
* The function `_addEdge` adds an edge to a graph if the source and destination vertexMap exist.
|
|
609
614
|
* @param {EO} edge - The parameter `edge` is of type `EO`, which represents an edge in a graph. It is the edge that
|
|
610
615
|
* needs to be added to the graph.
|
|
611
616
|
* @returns a boolean value. It returns true if the edge was successfully added to the graph, and false if either the
|
|
612
617
|
* source or destination vertex does not exist in the graph.
|
|
613
618
|
*/
|
|
614
|
-
protected
|
|
619
|
+
protected _addEdge(edge: EO): boolean {
|
|
615
620
|
if (!(this.hasVertex(edge.src) && this.hasVertex(edge.dest))) {
|
|
616
621
|
return false;
|
|
617
622
|
}
|
|
@@ -212,7 +212,7 @@ export class UndirectedGraph<
|
|
|
212
212
|
* (`VertexKey`).
|
|
213
213
|
* @returns The method is returning a boolean value.
|
|
214
214
|
*/
|
|
215
|
-
|
|
215
|
+
deleteVertex(vertexOrKey: VO | VertexKey): boolean {
|
|
216
216
|
let vertexKey: VertexKey;
|
|
217
217
|
let vertex: VO | undefined;
|
|
218
218
|
if (this.isVertexKey(vertexOrKey)) {
|
|
@@ -381,7 +381,7 @@ export class UndirectedGraph<
|
|
|
381
381
|
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
382
382
|
* @returns a boolean value.
|
|
383
383
|
*/
|
|
384
|
-
protected
|
|
384
|
+
protected _addEdge(edge: EO): boolean {
|
|
385
385
|
for (const end of edge.vertexMap) {
|
|
386
386
|
const endVertex = this._getVertex(end);
|
|
387
387
|
if (endVertex === undefined) return false;
|
|
@@ -70,6 +70,38 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
70
70
|
* Space Complexity: O(n)
|
|
71
71
|
*/
|
|
72
72
|
|
|
73
|
+
/**
|
|
74
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
75
|
+
* Space Complexity: O(1)
|
|
76
|
+
*
|
|
77
|
+
* The `get first` function returns the first node in a doubly linked list, or undefined if the list is empty.
|
|
78
|
+
* @returns The method `get first()` returns the first node of the doubly linked list, or `undefined` if the list is empty.
|
|
79
|
+
*/
|
|
80
|
+
get first(): E | undefined {
|
|
81
|
+
return this.head?.value;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Time Complexity: O(1)
|
|
86
|
+
* Space Complexity: O(1)
|
|
87
|
+
*/
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
91
|
+
* Space Complexity: O(1)
|
|
92
|
+
*
|
|
93
|
+
* The `get last` function returns the last node in a doubly linked list, or undefined if the list is empty.
|
|
94
|
+
* @returns The method `get last()` returns the last node of the doubly linked list, or `undefined` if the list is empty.
|
|
95
|
+
*/
|
|
96
|
+
get last(): E | undefined {
|
|
97
|
+
return this.tail?.value;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Time Complexity: O(1)
|
|
102
|
+
* Space Complexity: O(1)
|
|
103
|
+
*/
|
|
104
|
+
|
|
73
105
|
/**
|
|
74
106
|
* Time Complexity: O(n), where n is the size of the input array.
|
|
75
107
|
* Space Complexity: O(n)
|
|
@@ -141,7 +173,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
141
173
|
}
|
|
142
174
|
|
|
143
175
|
/**
|
|
144
|
-
* Time Complexity: O(
|
|
176
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
145
177
|
* Space Complexity: O(1)
|
|
146
178
|
*/
|
|
147
179
|
|
|
@@ -168,7 +200,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
168
200
|
}
|
|
169
201
|
|
|
170
202
|
/**
|
|
171
|
-
* Time Complexity: O(
|
|
203
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
172
204
|
* Space Complexity: O(1)
|
|
173
205
|
*/
|
|
174
206
|
|
|
@@ -399,11 +431,6 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
399
431
|
return false;
|
|
400
432
|
}
|
|
401
433
|
|
|
402
|
-
/**
|
|
403
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
404
|
-
* Space Complexity: O(1)
|
|
405
|
-
*/
|
|
406
|
-
|
|
407
434
|
/**
|
|
408
435
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
409
436
|
* Space Complexity: O(1)
|
|
@@ -434,11 +461,6 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
434
461
|
return true;
|
|
435
462
|
}
|
|
436
463
|
|
|
437
|
-
/**
|
|
438
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
439
|
-
* Space Complexity: O(1)
|
|
440
|
-
*/
|
|
441
|
-
|
|
442
464
|
/**
|
|
443
465
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
444
466
|
* Space Complexity: O(1)
|
|
@@ -475,6 +497,11 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
475
497
|
return false;
|
|
476
498
|
}
|
|
477
499
|
|
|
500
|
+
/**
|
|
501
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
502
|
+
* Space Complexity: O(1)
|
|
503
|
+
*/
|
|
504
|
+
|
|
478
505
|
/**
|
|
479
506
|
* The function checks if a variable has a size greater than zero and returns a boolean value.
|
|
480
507
|
* @returns A boolean value is being returned.
|
|
@@ -483,6 +510,11 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
483
510
|
return this.size === 0;
|
|
484
511
|
}
|
|
485
512
|
|
|
513
|
+
/**
|
|
514
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
515
|
+
* Space Complexity: O(1)
|
|
516
|
+
*/
|
|
517
|
+
|
|
486
518
|
/**
|
|
487
519
|
* The `clear` function resets the linked list by setting the head, tail, and size to undefined and 0 respectively.
|
|
488
520
|
*/
|
|
@@ -548,7 +580,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
548
580
|
|
|
549
581
|
/**
|
|
550
582
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
551
|
-
* Space Complexity: O(
|
|
583
|
+
* Space Complexity: O(n)
|
|
552
584
|
*/
|
|
553
585
|
|
|
554
586
|
/**
|
|
@@ -575,7 +607,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
575
607
|
|
|
576
608
|
/**
|
|
577
609
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
578
|
-
* Space Complexity: O(
|
|
610
|
+
* Space Complexity: O(n)
|
|
579
611
|
*/
|
|
580
612
|
|
|
581
613
|
/**
|
|
@@ -596,7 +628,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
596
628
|
}
|
|
597
629
|
|
|
598
630
|
/**
|
|
599
|
-
* Time Complexity: O(n)
|
|
631
|
+
* Time Complexity: O(n)
|
|
600
632
|
* Space Complexity: O(n)
|
|
601
633
|
*/
|
|
602
634
|
|
|
@@ -640,8 +672,8 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
640
672
|
}
|
|
641
673
|
|
|
642
674
|
/**
|
|
643
|
-
* Time Complexity: O(
|
|
644
|
-
* Space Complexity: O(
|
|
675
|
+
* Time Complexity: O(1)
|
|
676
|
+
* Space Complexity: O(1)
|
|
645
677
|
*/
|
|
646
678
|
|
|
647
679
|
/**
|
|
@@ -674,8 +706,8 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
674
706
|
}
|
|
675
707
|
|
|
676
708
|
/**
|
|
677
|
-
* Time Complexity: O(
|
|
678
|
-
* Space Complexity: O(
|
|
709
|
+
* Time Complexity: O(1)
|
|
710
|
+
* Space Complexity: O(1)
|
|
679
711
|
*/
|
|
680
712
|
|
|
681
713
|
/**
|
|
@@ -740,7 +772,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
740
772
|
}
|
|
741
773
|
|
|
742
774
|
/**
|
|
743
|
-
* Time Complexity: O(
|
|
775
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
744
776
|
* Space Complexity: O(1)
|
|
745
777
|
*/
|
|
746
778
|
|
|
@@ -757,7 +789,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
757
789
|
}
|
|
758
790
|
|
|
759
791
|
/**
|
|
760
|
-
* Time Complexity: O(
|
|
792
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
761
793
|
* Space Complexity: O(1)
|
|
762
794
|
*/
|
|
763
795
|
|
|
@@ -773,38 +805,6 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
773
805
|
this.unshift(value);
|
|
774
806
|
}
|
|
775
807
|
|
|
776
|
-
/**
|
|
777
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
778
|
-
* Space Complexity: O(1)
|
|
779
|
-
*/
|
|
780
|
-
|
|
781
|
-
/**
|
|
782
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
783
|
-
* Space Complexity: O(1)
|
|
784
|
-
*
|
|
785
|
-
* The `get first` function returns the first node in a doubly linked list, or undefined if the list is empty.
|
|
786
|
-
* @returns The method `get first()` returns the first node of the doubly linked list, or `undefined` if the list is empty.
|
|
787
|
-
*/
|
|
788
|
-
get first(): E | undefined {
|
|
789
|
-
return this.head?.value;
|
|
790
|
-
}
|
|
791
|
-
|
|
792
|
-
/**
|
|
793
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
794
|
-
* Space Complexity: O(1)
|
|
795
|
-
*/
|
|
796
|
-
|
|
797
|
-
/**
|
|
798
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
799
|
-
* Space Complexity: O(1)
|
|
800
|
-
*
|
|
801
|
-
* The `get last` function returns the last node in a doubly linked list, or undefined if the list is empty.
|
|
802
|
-
* @returns The method `get last()` returns the last node of the doubly linked list, or `undefined` if the list is empty.
|
|
803
|
-
*/
|
|
804
|
-
get last(): E | undefined {
|
|
805
|
-
return this.tail?.value;
|
|
806
|
-
}
|
|
807
|
-
|
|
808
808
|
/**
|
|
809
809
|
* The function returns an iterator that iterates over the values of a linked list.
|
|
810
810
|
*/
|
|
@@ -33,8 +33,7 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
33
33
|
this._tail = undefined;
|
|
34
34
|
this._size = 0;
|
|
35
35
|
if (elements) {
|
|
36
|
-
for (const el of elements)
|
|
37
|
-
this.push(el);
|
|
36
|
+
for (const el of elements) this.push(el);
|
|
38
37
|
}
|
|
39
38
|
}
|
|
40
39
|
|
|
@@ -348,7 +347,7 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
348
347
|
* @returns The `delete` method returns a boolean value. It returns `true` if the value or node is found and
|
|
349
348
|
* successfully deleted from the linked list, and `false` if the value or node is not found in the linked list.
|
|
350
349
|
*/
|
|
351
|
-
delete(valueOrNode: E | SinglyLinkedListNode<E> | undefined
|
|
350
|
+
delete(valueOrNode: E | SinglyLinkedListNode<E> | undefined): boolean {
|
|
352
351
|
if (!valueOrNode) return false;
|
|
353
352
|
let value: E;
|
|
354
353
|
if (valueOrNode instanceof SinglyLinkedListNode) {
|
|
@@ -57,6 +57,45 @@ export class SkipList<K, V> {
|
|
|
57
57
|
return this._probability;
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
+
/**
|
|
61
|
+
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
62
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
63
|
+
*/
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Time Complexity: O(1) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
67
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
68
|
+
*
|
|
69
|
+
* Get the value of the first element (the smallest element) in the Skip List.
|
|
70
|
+
* @returns The value of the first element, or undefined if the Skip List is empty.
|
|
71
|
+
*/
|
|
72
|
+
get first(): V | undefined {
|
|
73
|
+
const firstNode = this.head.forward[0];
|
|
74
|
+
return firstNode ? firstNode.value : undefined;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
79
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
80
|
+
*/
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
84
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
85
|
+
*
|
|
86
|
+
* Get the value of the last element (the largest element) in the Skip List.
|
|
87
|
+
* @returns The value of the last element, or undefined if the Skip List is empty.
|
|
88
|
+
*/
|
|
89
|
+
get last(): V | undefined {
|
|
90
|
+
let current = this.head;
|
|
91
|
+
for (let i = this.level - 1; i >= 0; i--) {
|
|
92
|
+
while (current.forward[i]) {
|
|
93
|
+
current = current.forward[i];
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return current.value;
|
|
97
|
+
}
|
|
98
|
+
|
|
60
99
|
/**
|
|
61
100
|
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
62
101
|
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
@@ -125,7 +164,7 @@ export class SkipList<K, V> {
|
|
|
125
164
|
}
|
|
126
165
|
|
|
127
166
|
/**
|
|
128
|
-
* Time Complexity: O(
|
|
167
|
+
* Time Complexity: O(1) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
129
168
|
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
130
169
|
*/
|
|
131
170
|
|
|
@@ -181,45 +220,6 @@ export class SkipList<K, V> {
|
|
|
181
220
|
return false;
|
|
182
221
|
}
|
|
183
222
|
|
|
184
|
-
/**
|
|
185
|
-
* Time Complexity: O(1) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
186
|
-
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
187
|
-
*/
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* Time Complexity: O(1) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
191
|
-
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
192
|
-
*
|
|
193
|
-
* Get the value of the first element (the smallest element) in the Skip List.
|
|
194
|
-
* @returns The value of the first element, or undefined if the Skip List is empty.
|
|
195
|
-
*/
|
|
196
|
-
get first(): V | undefined {
|
|
197
|
-
const firstNode = this.head.forward[0];
|
|
198
|
-
return firstNode ? firstNode.value : undefined;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
203
|
-
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
204
|
-
*/
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
208
|
-
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
209
|
-
*
|
|
210
|
-
* Get the value of the last element (the largest element) in the Skip List.
|
|
211
|
-
* @returns The value of the last element, or undefined if the Skip List is empty.
|
|
212
|
-
*/
|
|
213
|
-
get last(): V | undefined {
|
|
214
|
-
let current = this.head;
|
|
215
|
-
for (let i = this.level - 1; i >= 0; i--) {
|
|
216
|
-
while (current.forward[i]) {
|
|
217
|
-
current = current.forward[i];
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
return current.value;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
223
|
/**
|
|
224
224
|
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
225
225
|
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
@@ -49,6 +49,40 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
49
49
|
return this.nodes.length - this.offset;
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
+
/**
|
|
53
|
+
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
54
|
+
* Space Complexity: O(1) - no additional space is used.
|
|
55
|
+
*
|
|
56
|
+
* The `first` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
|
|
57
|
+
* @returns The `get first()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
58
|
+
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
59
|
+
*/
|
|
60
|
+
get first(): E | undefined {
|
|
61
|
+
return this.size > 0 ? this.nodes[this.offset] : undefined;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Time Complexity: O(1) - constant time as it adds an element to the end of the array.
|
|
66
|
+
* Space Complexity: O(1) - no additional space is used.
|
|
67
|
+
*/
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
71
|
+
* Space Complexity: O(1) - no additional space is used.
|
|
72
|
+
*
|
|
73
|
+
* The `last` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
74
|
+
* @returns The method `get last()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
75
|
+
* array is empty, it returns `undefined`.
|
|
76
|
+
*/
|
|
77
|
+
get last(): E | undefined {
|
|
78
|
+
return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Time Complexity: O(n) - where n is the number of elements in the queue. In the worst case, it may need to shift all elements to update the offset.
|
|
83
|
+
* Space Complexity: O(1) - no additional space is used.
|
|
84
|
+
*/
|
|
85
|
+
|
|
52
86
|
/**
|
|
53
87
|
* The function "fromArray" creates a new Queue object from an array of elements.Creates a queue from an existing array.
|
|
54
88
|
* @public
|
|
@@ -62,7 +96,7 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
62
96
|
}
|
|
63
97
|
|
|
64
98
|
/**
|
|
65
|
-
* Time Complexity: O(1) - constant time as it
|
|
99
|
+
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
66
100
|
* Space Complexity: O(1) - no additional space is used.
|
|
67
101
|
*/
|
|
68
102
|
|
|
@@ -80,7 +114,7 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
80
114
|
}
|
|
81
115
|
|
|
82
116
|
/**
|
|
83
|
-
* Time Complexity: O(
|
|
117
|
+
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
84
118
|
* Space Complexity: O(1) - no additional space is used.
|
|
85
119
|
*/
|
|
86
120
|
|
|
@@ -107,23 +141,6 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
107
141
|
return first;
|
|
108
142
|
}
|
|
109
143
|
|
|
110
|
-
/**
|
|
111
|
-
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
112
|
-
* Space Complexity: O(1) - no additional space is used.
|
|
113
|
-
*/
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
117
|
-
* Space Complexity: O(1) - no additional space is used.
|
|
118
|
-
*
|
|
119
|
-
* The `first` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
|
|
120
|
-
* @returns The `get first()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
121
|
-
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
122
|
-
*/
|
|
123
|
-
get first(): E | undefined {
|
|
124
|
-
return this.size > 0 ? this.nodes[this.offset] : undefined;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
144
|
/**
|
|
128
145
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
129
146
|
* Space Complexity: O(1) - no additional space is used.
|
|
@@ -141,23 +158,6 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
141
158
|
return this.first;
|
|
142
159
|
}
|
|
143
160
|
|
|
144
|
-
/**
|
|
145
|
-
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
146
|
-
* Space Complexity: O(1) - no additional space is used.
|
|
147
|
-
*/
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
151
|
-
* Space Complexity: O(1) - no additional space is used.
|
|
152
|
-
*
|
|
153
|
-
* The `last` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
154
|
-
* @returns The method `get last()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
155
|
-
* array is empty, it returns `undefined`.
|
|
156
|
-
*/
|
|
157
|
-
get last(): E | undefined {
|
|
158
|
-
return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
161
|
/**
|
|
162
162
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
163
163
|
* Space Complexity: O(1) - no additional space is used.
|
|
@@ -358,12 +358,20 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
358
358
|
* 4. Frequent Enqueuing and Dequeuing Operations: If your application involves frequent enqueuing and dequeuing operations and is less concerned with random access, then LinkedListQueue is a good choice.
|
|
359
359
|
*/
|
|
360
360
|
export class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
|
|
361
|
+
/**
|
|
362
|
+
* The `get first` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
363
|
+
* @returns The `get first()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
364
|
+
*/
|
|
365
|
+
get first(): E | undefined {
|
|
366
|
+
return this.head?.value;
|
|
367
|
+
}
|
|
368
|
+
|
|
361
369
|
/**
|
|
362
370
|
* The enqueue function adds a value to the end of an array.
|
|
363
371
|
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
364
372
|
*/
|
|
365
373
|
enqueue(value: E): boolean {
|
|
366
|
-
return
|
|
374
|
+
return this.push(value);
|
|
367
375
|
}
|
|
368
376
|
|
|
369
377
|
/**
|
|
@@ -374,14 +382,6 @@ export class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
|
|
|
374
382
|
return this.shift();
|
|
375
383
|
}
|
|
376
384
|
|
|
377
|
-
/**
|
|
378
|
-
* The `get first` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
379
|
-
* @returns The `get first()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
380
|
-
*/
|
|
381
|
-
get first(): E | undefined {
|
|
382
|
-
return this.head?.value;
|
|
383
|
-
}
|
|
384
|
-
|
|
385
385
|
/**
|
|
386
386
|
* The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
387
387
|
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|