data-structure-typed 1.47.7 → 1.47.9
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 +98 -17
- package/dist/cjs/data-structures/binary-tree/segment-tree.d.ts +6 -6
- package/dist/cjs/data-structures/binary-tree/segment-tree.js +7 -7
- package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +22 -17
- package/dist/cjs/data-structures/graph/abstract-graph.js +71 -30
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +24 -24
- package/dist/cjs/data-structures/graph/directed-graph.js +29 -29
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +14 -14
- package/dist/cjs/data-structures/graph/undirected-graph.js +18 -18
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +28 -28
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +33 -33
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +21 -21
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +27 -27
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +4 -4
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +13 -13
- package/dist/cjs/data-structures/queue/queue.js +13 -13
- package/dist/cjs/data-structures/stack/stack.d.ts +6 -6
- package/dist/cjs/data-structures/stack/stack.js +7 -7
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/types/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/mjs/data-structures/binary-tree/segment-tree.d.ts +6 -6
- package/dist/mjs/data-structures/binary-tree/segment-tree.js +7 -7
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +22 -17
- package/dist/mjs/data-structures/graph/abstract-graph.js +71 -30
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +24 -24
- package/dist/mjs/data-structures/graph/directed-graph.js +29 -29
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +14 -14
- package/dist/mjs/data-structures/graph/undirected-graph.js +18 -18
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +28 -28
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +33 -33
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +21 -21
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +27 -27
- package/dist/mjs/data-structures/linked-list/skip-linked-list.js +4 -4
- package/dist/mjs/data-structures/queue/queue.d.ts +13 -13
- package/dist/mjs/data-structures/queue/queue.js +13 -13
- package/dist/mjs/data-structures/stack/stack.d.ts +6 -6
- package/dist/mjs/data-structures/stack/stack.js +7 -7
- package/dist/mjs/types/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/umd/data-structure-typed.js +203 -162
- 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/segment-tree.ts +10 -10
- package/src/data-structures/graph/abstract-graph.ts +92 -46
- package/src/data-structures/graph/directed-graph.ts +41 -41
- package/src/data-structures/graph/undirected-graph.ts +26 -26
- package/src/data-structures/linked-list/doubly-linked-list.ts +45 -45
- package/src/data-structures/linked-list/singly-linked-list.ts +38 -38
- package/src/data-structures/linked-list/skip-linked-list.ts +4 -4
- package/src/data-structures/queue/queue.ts +13 -13
- package/src/data-structures/stack/stack.ts +9 -9
- package/src/types/data-structures/graph/abstract-graph.ts +2 -2
- package/test/integration/index.html +102 -33
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +1 -1
- package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/segment-tree.test.ts +1 -1
- package/test/unit/data-structures/graph/abstract-graph.test.ts +4 -4
- package/test/unit/data-structures/graph/directed-graph.test.ts +51 -10
- package/test/unit/data-structures/graph/undirected-graph.test.ts +3 -3
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +14 -14
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +3 -3
- package/test/unit/data-structures/linked-list/skip-list.test.ts +1 -1
- package/test/unit/data-structures/queue/deque.test.ts +1 -1
- package/test/unit/data-structures/stack/stack.test.ts +2 -2
|
@@ -100,26 +100,26 @@ export class UndirectedGraph<
|
|
|
100
100
|
* Time Complexity: O(|E|), where |E| is the number of edges incident to the given vertex.
|
|
101
101
|
* Space Complexity: O(1)
|
|
102
102
|
*
|
|
103
|
-
* The function `getEdge` returns the first edge that connects two vertices, or
|
|
104
|
-
* @param {VO | VertexKey |
|
|
105
|
-
* object), `
|
|
106
|
-
* @param {VO | VertexKey |
|
|
107
|
-
* object), `
|
|
108
|
-
* @returns an edge (EO) or
|
|
103
|
+
* The function `getEdge` returns the first edge that connects two vertices, or undefined if no such edge exists.
|
|
104
|
+
* @param {VO | VertexKey | undefined} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
105
|
+
* object), `undefined`, or `VertexKey` (a string or number representing the ID of a vertex).
|
|
106
|
+
* @param {VO | VertexKey | undefined} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
107
|
+
* object), `undefined`, or `VertexKey` (vertex ID).
|
|
108
|
+
* @returns an edge (EO) or undefined.
|
|
109
109
|
*/
|
|
110
|
-
getEdge(v1: VO | VertexKey |
|
|
110
|
+
getEdge(v1: VO | VertexKey | undefined, v2: VO | VertexKey | undefined): EO | undefined {
|
|
111
111
|
let edges: EO[] | undefined = [];
|
|
112
112
|
|
|
113
|
-
if (v1 !==
|
|
114
|
-
const vertex1: VO |
|
|
115
|
-
const vertex2: VO |
|
|
113
|
+
if (v1 !== undefined && v2 !== undefined) {
|
|
114
|
+
const vertex1: VO | undefined = this._getVertex(v1);
|
|
115
|
+
const vertex2: VO | undefined = this._getVertex(v2);
|
|
116
116
|
|
|
117
117
|
if (vertex1 && vertex2) {
|
|
118
118
|
edges = this._edges.get(vertex1)?.filter(e => e.vertices.includes(vertex2.key));
|
|
119
119
|
}
|
|
120
120
|
}
|
|
121
121
|
|
|
122
|
-
return edges ? edges[0] ||
|
|
122
|
+
return edges ? edges[0] || undefined : undefined;
|
|
123
123
|
}
|
|
124
124
|
|
|
125
125
|
/**
|
|
@@ -135,20 +135,20 @@ export class UndirectedGraph<
|
|
|
135
135
|
* @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
|
|
136
136
|
* @param {VO | VertexKey} v2 - VO | VertexKey - This parameter can be either a vertex object (VO) or a vertex ID
|
|
137
137
|
* (VertexKey). It represents the second vertex of the edge that needs to be removed.
|
|
138
|
-
* @returns the removed edge (EO) if it exists, or
|
|
138
|
+
* @returns the removed edge (EO) if it exists, or undefined if either of the vertices (VO) does not exist.
|
|
139
139
|
*/
|
|
140
|
-
deleteEdgeBetween(v1: VO | VertexKey, v2: VO | VertexKey): EO |
|
|
141
|
-
const vertex1: VO |
|
|
142
|
-
const vertex2: VO |
|
|
140
|
+
deleteEdgeBetween(v1: VO | VertexKey, v2: VO | VertexKey): EO | undefined {
|
|
141
|
+
const vertex1: VO | undefined = this._getVertex(v1);
|
|
142
|
+
const vertex2: VO | undefined = this._getVertex(v2);
|
|
143
143
|
|
|
144
144
|
if (!vertex1 || !vertex2) {
|
|
145
|
-
return
|
|
145
|
+
return undefined;
|
|
146
146
|
}
|
|
147
147
|
|
|
148
148
|
const v1Edges = this._edges.get(vertex1);
|
|
149
|
-
let removed: EO |
|
|
149
|
+
let removed: EO | undefined = undefined;
|
|
150
150
|
if (v1Edges) {
|
|
151
|
-
removed = arrayRemove<EO>(v1Edges, (e: EO) => e.vertices.includes(vertex2.key))[0] ||
|
|
151
|
+
removed = arrayRemove<EO>(v1Edges, (e: EO) => e.vertices.includes(vertex2.key))[0] || undefined;
|
|
152
152
|
}
|
|
153
153
|
const v2Edges = this._edges.get(vertex2);
|
|
154
154
|
if (v2Edges) {
|
|
@@ -168,9 +168,9 @@ export class UndirectedGraph<
|
|
|
168
168
|
*
|
|
169
169
|
* The deleteEdge function removes an edge between two vertices in a graph.
|
|
170
170
|
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
171
|
-
* @returns The method is returning either the removed edge (of type EO) or
|
|
171
|
+
* @returns The method is returning either the removed edge (of type EO) or undefined if the edge was not found.
|
|
172
172
|
*/
|
|
173
|
-
deleteEdge(edge: EO): EO |
|
|
173
|
+
deleteEdge(edge: EO): EO | undefined {
|
|
174
174
|
return this.deleteEdgeBetween(edge.vertices[0], edge.vertices[1]);
|
|
175
175
|
}
|
|
176
176
|
|
|
@@ -282,21 +282,21 @@ export class UndirectedGraph<
|
|
|
282
282
|
* Space Complexity: O(1)
|
|
283
283
|
*
|
|
284
284
|
* The function "getEndsOfEdge" returns the vertices at the ends of an edge if the edge exists in the graph, otherwise
|
|
285
|
-
* it returns
|
|
285
|
+
* it returns undefined.
|
|
286
286
|
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
287
287
|
* @returns The function `getEndsOfEdge` returns an array containing two vertices `[VO, VO]` if the edge exists in the
|
|
288
|
-
* graph. If the edge does not exist, it returns `
|
|
288
|
+
* graph. If the edge does not exist, it returns `undefined`.
|
|
289
289
|
*/
|
|
290
|
-
getEndsOfEdge(edge: EO): [VO, VO] |
|
|
290
|
+
getEndsOfEdge(edge: EO): [VO, VO] | undefined {
|
|
291
291
|
if (!this.hasEdge(edge.vertices[0], edge.vertices[1])) {
|
|
292
|
-
return
|
|
292
|
+
return undefined;
|
|
293
293
|
}
|
|
294
294
|
const v1 = this._getVertex(edge.vertices[0]);
|
|
295
295
|
const v2 = this._getVertex(edge.vertices[1]);
|
|
296
296
|
if (v1 && v2) {
|
|
297
297
|
return [v1, v2];
|
|
298
298
|
} else {
|
|
299
|
-
return
|
|
299
|
+
return undefined;
|
|
300
300
|
}
|
|
301
301
|
}
|
|
302
302
|
|
|
@@ -316,7 +316,7 @@ export class UndirectedGraph<
|
|
|
316
316
|
protected _addEdgeOnly(edge: EO): boolean {
|
|
317
317
|
for (const end of edge.vertices) {
|
|
318
318
|
const endVertex = this._getVertex(end);
|
|
319
|
-
if (endVertex ===
|
|
319
|
+
if (endVertex === undefined) return false;
|
|
320
320
|
if (endVertex) {
|
|
321
321
|
const edges = this._edges.get(endVertex);
|
|
322
322
|
if (edges) {
|
|
@@ -7,8 +7,8 @@
|
|
|
7
7
|
*/
|
|
8
8
|
export class DoublyLinkedListNode<E = any> {
|
|
9
9
|
value: E;
|
|
10
|
-
next: DoublyLinkedListNode<E> |
|
|
11
|
-
prev: DoublyLinkedListNode<E> |
|
|
10
|
+
next: DoublyLinkedListNode<E> | undefined;
|
|
11
|
+
prev: DoublyLinkedListNode<E> | undefined;
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* The constructor function initializes the value, next, and previous properties of an object.
|
|
@@ -17,8 +17,8 @@ export class DoublyLinkedListNode<E = any> {
|
|
|
17
17
|
*/
|
|
18
18
|
constructor(value: E) {
|
|
19
19
|
this.value = value;
|
|
20
|
-
this.next =
|
|
21
|
-
this.prev =
|
|
20
|
+
this.next = undefined;
|
|
21
|
+
this.prev = undefined;
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
|
|
@@ -27,8 +27,8 @@ export class DoublyLinkedList<E = any> {
|
|
|
27
27
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
28
28
|
*/
|
|
29
29
|
constructor(elements?: Iterable<E>) {
|
|
30
|
-
this._head =
|
|
31
|
-
this._tail =
|
|
30
|
+
this._head = undefined;
|
|
31
|
+
this._tail = undefined;
|
|
32
32
|
this._length = 0;
|
|
33
33
|
if (elements) {
|
|
34
34
|
for (const el of elements) {
|
|
@@ -37,15 +37,15 @@ export class DoublyLinkedList<E = any> {
|
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
protected _head: DoublyLinkedListNode<E> |
|
|
40
|
+
protected _head: DoublyLinkedListNode<E> | undefined;
|
|
41
41
|
|
|
42
|
-
get head(): DoublyLinkedListNode<E> |
|
|
42
|
+
get head(): DoublyLinkedListNode<E> | undefined {
|
|
43
43
|
return this._head;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
protected _tail: DoublyLinkedListNode<E> |
|
|
46
|
+
protected _tail: DoublyLinkedListNode<E> | undefined;
|
|
47
47
|
|
|
48
|
-
get tail(): DoublyLinkedListNode<E> |
|
|
48
|
+
get tail(): DoublyLinkedListNode<E> | undefined {
|
|
49
49
|
return this._tail;
|
|
50
50
|
}
|
|
51
51
|
|
|
@@ -133,17 +133,17 @@ export class DoublyLinkedList<E = any> {
|
|
|
133
133
|
*
|
|
134
134
|
* The `pop()` function removes and returns the value of the last node in a doubly linked list.
|
|
135
135
|
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
|
136
|
-
* list is empty, it returns
|
|
136
|
+
* list is empty, it returns undefined.
|
|
137
137
|
*/
|
|
138
138
|
pop(): E | undefined {
|
|
139
139
|
if (!this.tail) return undefined;
|
|
140
140
|
const removedNode = this.tail;
|
|
141
141
|
if (this.head === this.tail) {
|
|
142
|
-
this._head =
|
|
143
|
-
this._tail =
|
|
142
|
+
this._head = undefined;
|
|
143
|
+
this._tail = undefined;
|
|
144
144
|
} else {
|
|
145
145
|
this._tail = removedNode.prev;
|
|
146
|
-
this.tail!.next =
|
|
146
|
+
this.tail!.next = undefined;
|
|
147
147
|
}
|
|
148
148
|
this._length--;
|
|
149
149
|
return removedNode.value;
|
|
@@ -160,7 +160,7 @@ export class DoublyLinkedList<E = any> {
|
|
|
160
160
|
*
|
|
161
161
|
* The `popLast()` function removes and returns the value of the last node in a doubly linked list.
|
|
162
162
|
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
|
163
|
-
* list is empty, it returns
|
|
163
|
+
* list is empty, it returns undefined.
|
|
164
164
|
*/
|
|
165
165
|
popLast(): E | undefined {
|
|
166
166
|
return this.pop();
|
|
@@ -183,11 +183,11 @@ export class DoublyLinkedList<E = any> {
|
|
|
183
183
|
if (!this.head) return undefined;
|
|
184
184
|
const removedNode = this.head;
|
|
185
185
|
if (this.head === this.tail) {
|
|
186
|
-
this._head =
|
|
187
|
-
this._tail =
|
|
186
|
+
this._head = undefined;
|
|
187
|
+
this._tail = undefined;
|
|
188
188
|
} else {
|
|
189
189
|
this._head = removedNode.next;
|
|
190
|
-
this.head!.prev =
|
|
190
|
+
this.head!.prev = undefined;
|
|
191
191
|
}
|
|
192
192
|
this._length--;
|
|
193
193
|
return removedNode.value;
|
|
@@ -262,8 +262,8 @@ export class DoublyLinkedList<E = any> {
|
|
|
262
262
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
263
263
|
* Space Complexity: O(1)
|
|
264
264
|
*
|
|
265
|
-
* The `getFirst` function returns the first node in a doubly linked list, or
|
|
266
|
-
* @returns The method `getFirst()` returns the first node of the doubly linked list, or `
|
|
265
|
+
* The `getFirst` function returns the first node in a doubly linked list, or undefined if the list is empty.
|
|
266
|
+
* @returns The method `getFirst()` returns the first node of the doubly linked list, or `undefined` if the list is empty.
|
|
267
267
|
*/
|
|
268
268
|
getFirst(): E | undefined {
|
|
269
269
|
return this.head?.value;
|
|
@@ -278,8 +278,8 @@ export class DoublyLinkedList<E = any> {
|
|
|
278
278
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
279
279
|
* Space Complexity: O(1)
|
|
280
280
|
*
|
|
281
|
-
* The `getLast` function returns the last node in a doubly linked list, or
|
|
282
|
-
* @returns The method `getLast()` returns the last node of the doubly linked list, or `
|
|
281
|
+
* The `getLast` function returns the last node in a doubly linked list, or undefined if the list is empty.
|
|
282
|
+
* @returns The method `getLast()` returns the last node of the doubly linked list, or `undefined` if the list is empty.
|
|
283
283
|
*/
|
|
284
284
|
getLast(): E | undefined {
|
|
285
285
|
return this.tail?.value;
|
|
@@ -294,11 +294,11 @@ export class DoublyLinkedList<E = any> {
|
|
|
294
294
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
295
295
|
* Space Complexity: O(1)
|
|
296
296
|
*
|
|
297
|
-
* The `getAt` function returns the value at a specified index in a linked list, or
|
|
297
|
+
* The `getAt` function returns the value at a specified index in a linked list, or undefined if the index is out of bounds.
|
|
298
298
|
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
|
299
299
|
* retrieve from the list.
|
|
300
300
|
* @returns The method is returning the value at the specified index in the linked list. If the index is out of bounds
|
|
301
|
-
* or the linked list is empty, it will return
|
|
301
|
+
* or the linked list is empty, it will return undefined.
|
|
302
302
|
*/
|
|
303
303
|
getAt(index: number): E | undefined {
|
|
304
304
|
if (index < 0 || index >= this.length) return undefined;
|
|
@@ -318,15 +318,15 @@ export class DoublyLinkedList<E = any> {
|
|
|
318
318
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
319
319
|
* Space Complexity: O(1)
|
|
320
320
|
*
|
|
321
|
-
* The function `getNodeAt` returns the node at a given index in a doubly linked list, or
|
|
321
|
+
* The function `getNodeAt` returns the node at a given index in a doubly linked list, or undefined if the index is out of
|
|
322
322
|
* range.
|
|
323
323
|
* @param {number} index - The `index` parameter is a number that represents the position of the node we want to
|
|
324
324
|
* retrieve from the doubly linked list. It indicates the zero-based index of the node we want to access.
|
|
325
325
|
* @returns The method `getNodeAt(index: number)` returns a `DoublyLinkedListNode<E>` object if the index is within the
|
|
326
|
-
* valid range of the linked list, otherwise it returns `
|
|
326
|
+
* valid range of the linked list, otherwise it returns `undefined`.
|
|
327
327
|
*/
|
|
328
|
-
getNodeAt(index: number): DoublyLinkedListNode<E> |
|
|
329
|
-
if (index < 0 || index >= this.length) return
|
|
328
|
+
getNodeAt(index: number): DoublyLinkedListNode<E> | undefined {
|
|
329
|
+
if (index < 0 || index >= this.length) return undefined;
|
|
330
330
|
let current = this.head;
|
|
331
331
|
for (let i = 0; i < index; i++) {
|
|
332
332
|
current = current!.next;
|
|
@@ -344,12 +344,12 @@ export class DoublyLinkedList<E = any> {
|
|
|
344
344
|
* Space Complexity: O(1)
|
|
345
345
|
*
|
|
346
346
|
* The function `findNodeByValue` searches for a node with a specific value in a doubly linked list and returns the
|
|
347
|
-
* node if found, otherwise it returns
|
|
347
|
+
* node if found, otherwise it returns undefined.
|
|
348
348
|
* @param {E} value - The `value` parameter is the value that we want to search for in the doubly linked list.
|
|
349
349
|
* @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<E>` if a node with the specified value `value`
|
|
350
|
-
* is found in the linked list. If no such node is found, it returns `
|
|
350
|
+
* is found in the linked list. If no such node is found, it returns `undefined`.
|
|
351
351
|
*/
|
|
352
|
-
getNode(value: E |
|
|
352
|
+
getNode(value: E | undefined): DoublyLinkedListNode<E> | undefined {
|
|
353
353
|
let current = this.head;
|
|
354
354
|
|
|
355
355
|
while (current) {
|
|
@@ -359,7 +359,7 @@ export class DoublyLinkedList<E = any> {
|
|
|
359
359
|
current = current.next;
|
|
360
360
|
}
|
|
361
361
|
|
|
362
|
-
return
|
|
362
|
+
return undefined;
|
|
363
363
|
}
|
|
364
364
|
|
|
365
365
|
/**
|
|
@@ -502,7 +502,7 @@ export class DoublyLinkedList<E = any> {
|
|
|
502
502
|
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
|
|
503
503
|
* @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
|
|
504
504
|
* data structure. It is of type number.
|
|
505
|
-
* @returns The method `deleteAt` returns the value of the node that was deleted, or `
|
|
505
|
+
* @returns The method `deleteAt` returns the value of the node that was deleted, or `undefined` if the index is out of
|
|
506
506
|
* bounds.
|
|
507
507
|
*/
|
|
508
508
|
deleteAt(index: number): E | undefined {
|
|
@@ -534,8 +534,8 @@ export class DoublyLinkedList<E = any> {
|
|
|
534
534
|
* @returns The `delete` method returns a boolean value. It returns `true` if the value or node was successfully
|
|
535
535
|
* deleted from the doubly linked list, and `false` if the value or node was not found in the list.
|
|
536
536
|
*/
|
|
537
|
-
delete(valOrNode: E | DoublyLinkedListNode<E> |
|
|
538
|
-
let node: DoublyLinkedListNode<E> |
|
|
537
|
+
delete(valOrNode: E | DoublyLinkedListNode<E> | undefined): boolean {
|
|
538
|
+
let node: DoublyLinkedListNode<E> | undefined;
|
|
539
539
|
|
|
540
540
|
if (valOrNode instanceof DoublyLinkedListNode) {
|
|
541
541
|
node = valOrNode;
|
|
@@ -569,11 +569,11 @@ export class DoublyLinkedList<E = any> {
|
|
|
569
569
|
}
|
|
570
570
|
|
|
571
571
|
/**
|
|
572
|
-
* The `clear` function resets the linked list by setting the head, tail, and length to
|
|
572
|
+
* The `clear` function resets the linked list by setting the head, tail, and length to undefined and 0 respectively.
|
|
573
573
|
*/
|
|
574
574
|
clear(): void {
|
|
575
|
-
this._head =
|
|
576
|
-
this._tail =
|
|
575
|
+
this._head = undefined;
|
|
576
|
+
this._tail = undefined;
|
|
577
577
|
this._length = 0;
|
|
578
578
|
}
|
|
579
579
|
|
|
@@ -590,9 +590,9 @@ export class DoublyLinkedList<E = any> {
|
|
|
590
590
|
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
|
|
591
591
|
* function is used to determine whether a particular value in the linked list satisfies a certain condition.
|
|
592
592
|
* @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
|
|
593
|
-
* the callback function. If no element satisfies the condition, it returns `
|
|
593
|
+
* the callback function. If no element satisfies the condition, it returns `undefined`.
|
|
594
594
|
*/
|
|
595
|
-
find(callback: (value: E) => boolean): E |
|
|
595
|
+
find(callback: (value: E) => boolean): E | undefined {
|
|
596
596
|
let current = this.head;
|
|
597
597
|
while (current) {
|
|
598
598
|
if (callback(current.value)) {
|
|
@@ -600,7 +600,7 @@ export class DoublyLinkedList<E = any> {
|
|
|
600
600
|
}
|
|
601
601
|
current = current.next;
|
|
602
602
|
}
|
|
603
|
-
return
|
|
603
|
+
return undefined;
|
|
604
604
|
}
|
|
605
605
|
|
|
606
606
|
/**
|
|
@@ -641,13 +641,13 @@ export class DoublyLinkedList<E = any> {
|
|
|
641
641
|
* Space Complexity: O(1)
|
|
642
642
|
*
|
|
643
643
|
* The `findBackward` function iterates through a linked list from the last node to the first node and returns the last
|
|
644
|
-
* value that satisfies the given callback function, or
|
|
644
|
+
* value that satisfies the given callback function, or undefined if no value satisfies the callback.
|
|
645
645
|
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
|
|
646
646
|
* function is used to determine whether a given value satisfies a certain condition.
|
|
647
647
|
* @returns The method `findBackward` returns the last value in the linked list that satisfies the condition specified by
|
|
648
|
-
* the callback function. If no value satisfies the condition, it returns `
|
|
648
|
+
* the callback function. If no value satisfies the condition, it returns `undefined`.
|
|
649
649
|
*/
|
|
650
|
-
findBackward(callback: (value: E) => boolean): E |
|
|
650
|
+
findBackward(callback: (value: E) => boolean): E | undefined {
|
|
651
651
|
let current = this.tail;
|
|
652
652
|
while (current) {
|
|
653
653
|
if (callback(current.value)) {
|
|
@@ -655,7 +655,7 @@ export class DoublyLinkedList<E = any> {
|
|
|
655
655
|
}
|
|
656
656
|
current = current.prev;
|
|
657
657
|
}
|
|
658
|
-
return
|
|
658
|
+
return undefined;
|
|
659
659
|
}
|
|
660
660
|
|
|
661
661
|
/**
|
|
@@ -7,16 +7,16 @@
|
|
|
7
7
|
*/
|
|
8
8
|
export class SinglyLinkedListNode<E = any> {
|
|
9
9
|
value: E;
|
|
10
|
-
next: SinglyLinkedListNode<E> |
|
|
10
|
+
next: SinglyLinkedListNode<E> | undefined;
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
|
-
* The constructor function initializes an instance of a class with a given value and sets the next property to
|
|
13
|
+
* The constructor function initializes an instance of a class with a given value and sets the next property to undefined.
|
|
14
14
|
* @param {E} value - The "value" parameter is of type E, which means it can be any data type. It represents the value that
|
|
15
15
|
* will be stored in the node of a linked list.
|
|
16
16
|
*/
|
|
17
17
|
constructor(value: E) {
|
|
18
18
|
this.value = value;
|
|
19
|
-
this.next =
|
|
19
|
+
this.next = undefined;
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
|
|
@@ -25,8 +25,8 @@ export class SinglyLinkedList<E = any> {
|
|
|
25
25
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
26
26
|
*/
|
|
27
27
|
constructor(elements?: Iterable<E>) {
|
|
28
|
-
this._head =
|
|
29
|
-
this._tail =
|
|
28
|
+
this._head = undefined;
|
|
29
|
+
this._tail = undefined;
|
|
30
30
|
this._length = 0;
|
|
31
31
|
if (elements) {
|
|
32
32
|
for (const el of elements)
|
|
@@ -34,15 +34,15 @@ export class SinglyLinkedList<E = any> {
|
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
protected _head: SinglyLinkedListNode<E> |
|
|
37
|
+
protected _head: SinglyLinkedListNode<E> | undefined;
|
|
38
38
|
|
|
39
|
-
get head(): SinglyLinkedListNode<E> |
|
|
39
|
+
get head(): SinglyLinkedListNode<E> | undefined {
|
|
40
40
|
return this._head;
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
protected _tail: SinglyLinkedListNode<E> |
|
|
43
|
+
protected _tail: SinglyLinkedListNode<E> | undefined;
|
|
44
44
|
|
|
45
|
-
get tail(): SinglyLinkedListNode<E> |
|
|
45
|
+
get tail(): SinglyLinkedListNode<E> | undefined {
|
|
46
46
|
return this._tail;
|
|
47
47
|
}
|
|
48
48
|
|
|
@@ -128,14 +128,14 @@ export class SinglyLinkedList<E = any> {
|
|
|
128
128
|
* The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
129
129
|
* pointers accordingly.
|
|
130
130
|
* @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
|
|
131
|
-
* the linked list is empty, it returns `
|
|
131
|
+
* the linked list is empty, it returns `undefined`.
|
|
132
132
|
*/
|
|
133
133
|
pop(): E | undefined {
|
|
134
134
|
if (!this.head) return undefined;
|
|
135
135
|
if (this.head === this.tail) {
|
|
136
136
|
const value = this.head.value;
|
|
137
|
-
this._head =
|
|
138
|
-
this._tail =
|
|
137
|
+
this._head = undefined;
|
|
138
|
+
this._tail = undefined;
|
|
139
139
|
this._length--;
|
|
140
140
|
return value;
|
|
141
141
|
}
|
|
@@ -145,7 +145,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
145
145
|
current = current.next!;
|
|
146
146
|
}
|
|
147
147
|
const value = this.tail!.value;
|
|
148
|
-
current.next =
|
|
148
|
+
current.next = undefined;
|
|
149
149
|
this._tail = current;
|
|
150
150
|
this._length--;
|
|
151
151
|
return value;
|
|
@@ -163,7 +163,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
163
163
|
* The `popLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
164
164
|
* pointers accordingly.
|
|
165
165
|
* @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
|
|
166
|
-
* the linked list is empty, it returns `
|
|
166
|
+
* the linked list is empty, it returns `undefined`.
|
|
167
167
|
*/
|
|
168
168
|
popLast(): E | undefined {
|
|
169
169
|
return this.pop();
|
|
@@ -256,11 +256,11 @@ export class SinglyLinkedList<E = any> {
|
|
|
256
256
|
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
|
|
257
257
|
* Space Complexity: O(1) - Constant space.
|
|
258
258
|
*
|
|
259
|
-
* The function `getAt` returns the value at a specified index in a linked list, or
|
|
259
|
+
* The function `getAt` returns the value at a specified index in a linked list, or undefined if the index is out of range.
|
|
260
260
|
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
|
261
261
|
* retrieve from the list.
|
|
262
|
-
* @returns The method `getAt(index: number): E |
|
|
263
|
-
* `
|
|
262
|
+
* @returns The method `getAt(index: number): E | undefined` returns the value at the specified index in the linked list, or
|
|
263
|
+
* `undefined` if the index is out of bounds.
|
|
264
264
|
*/
|
|
265
265
|
getAt(index: number): E | undefined {
|
|
266
266
|
if (index < 0 || index >= this.length) return undefined;
|
|
@@ -284,9 +284,9 @@ export class SinglyLinkedList<E = any> {
|
|
|
284
284
|
* @param {number} index - The `index` parameter is a number that represents the position of the node we want to
|
|
285
285
|
* retrieve from the linked list. It indicates the zero-based index of the node we want to access.
|
|
286
286
|
* @returns The method `getNodeAt(index: number)` returns a `SinglyLinkedListNode<E>` object if the node at the
|
|
287
|
-
* specified index exists, or `
|
|
287
|
+
* specified index exists, or `undefined` if the index is out of bounds.
|
|
288
288
|
*/
|
|
289
|
-
getNodeAt(index: number): SinglyLinkedListNode<E> |
|
|
289
|
+
getNodeAt(index: number): SinglyLinkedListNode<E> | undefined {
|
|
290
290
|
let current = this.head;
|
|
291
291
|
for (let i = 0; i < index; i++) {
|
|
292
292
|
current = current!.next;
|
|
@@ -306,7 +306,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
306
306
|
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
|
|
307
307
|
* @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
|
|
308
308
|
* data structure. It is of type number.
|
|
309
|
-
* @returns The method `deleteAt` returns the value of the node that was deleted, or `
|
|
309
|
+
* @returns The method `deleteAt` returns the value of the node that was deleted, or `undefined` if the index is out of
|
|
310
310
|
* bounds.
|
|
311
311
|
*/
|
|
312
312
|
deleteAt(index: number): E | undefined {
|
|
@@ -336,7 +336,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
336
336
|
* @returns The `delete` method returns a boolean value. It returns `true` if the value or node is found and
|
|
337
337
|
* successfully deleted from the linked list, and `false` if the value or node is not found in the linked list.
|
|
338
338
|
*/
|
|
339
|
-
delete(valueOrNode: E | SinglyLinkedListNode<E> |
|
|
339
|
+
delete(valueOrNode: E | SinglyLinkedListNode<E> | undefined | undefined): boolean {
|
|
340
340
|
if (!valueOrNode) return false;
|
|
341
341
|
let value: E;
|
|
342
342
|
if (valueOrNode instanceof SinglyLinkedListNode) {
|
|
@@ -345,14 +345,14 @@ export class SinglyLinkedList<E = any> {
|
|
|
345
345
|
value = valueOrNode;
|
|
346
346
|
}
|
|
347
347
|
let current = this.head,
|
|
348
|
-
prev =
|
|
348
|
+
prev = undefined;
|
|
349
349
|
|
|
350
350
|
while (current) {
|
|
351
351
|
if (current.value === value) {
|
|
352
|
-
if (prev ===
|
|
352
|
+
if (prev === undefined) {
|
|
353
353
|
this._head = current.next;
|
|
354
354
|
if (current === this.tail) {
|
|
355
|
-
this._tail =
|
|
355
|
+
this._tail = undefined;
|
|
356
356
|
}
|
|
357
357
|
} else {
|
|
358
358
|
prev.next = current.next;
|
|
@@ -416,11 +416,11 @@ export class SinglyLinkedList<E = any> {
|
|
|
416
416
|
}
|
|
417
417
|
|
|
418
418
|
/**
|
|
419
|
-
* The `clear` function resets the linked list by setting the head, tail, and length to
|
|
419
|
+
* The `clear` function resets the linked list by setting the head, tail, and length to undefined and 0 respectively.
|
|
420
420
|
*/
|
|
421
421
|
clear(): void {
|
|
422
|
-
this._head =
|
|
423
|
-
this._tail =
|
|
422
|
+
this._head = undefined;
|
|
423
|
+
this._tail = undefined;
|
|
424
424
|
this._length = 0;
|
|
425
425
|
}
|
|
426
426
|
|
|
@@ -461,9 +461,9 @@ export class SinglyLinkedList<E = any> {
|
|
|
461
461
|
reverse(): void {
|
|
462
462
|
if (!this.head || this.head === this.tail) return;
|
|
463
463
|
|
|
464
|
-
let prev: SinglyLinkedListNode<E> |
|
|
465
|
-
let current: SinglyLinkedListNode<E> |
|
|
466
|
-
let next: SinglyLinkedListNode<E> |
|
|
464
|
+
let prev: SinglyLinkedListNode<E> | undefined = undefined;
|
|
465
|
+
let current: SinglyLinkedListNode<E> | undefined = this.head;
|
|
466
|
+
let next: SinglyLinkedListNode<E> | undefined = undefined;
|
|
467
467
|
|
|
468
468
|
while (current) {
|
|
469
469
|
next = current.next;
|
|
@@ -488,9 +488,9 @@ export class SinglyLinkedList<E = any> {
|
|
|
488
488
|
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
|
|
489
489
|
* function is used to determine whether a particular value in the linked list satisfies a certain condition.
|
|
490
490
|
* @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
|
|
491
|
-
* the callback function. If no element satisfies the condition, it returns `
|
|
491
|
+
* the callback function. If no element satisfies the condition, it returns `undefined`.
|
|
492
492
|
*/
|
|
493
|
-
find(callback: (value: E) => boolean): E |
|
|
493
|
+
find(callback: (value: E) => boolean): E | undefined {
|
|
494
494
|
let current = this.head;
|
|
495
495
|
while (current) {
|
|
496
496
|
if (callback(current.value)) {
|
|
@@ -498,7 +498,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
498
498
|
}
|
|
499
499
|
current = current.next;
|
|
500
500
|
}
|
|
501
|
-
return
|
|
501
|
+
return undefined;
|
|
502
502
|
}
|
|
503
503
|
|
|
504
504
|
/**
|
|
@@ -540,12 +540,12 @@ export class SinglyLinkedList<E = any> {
|
|
|
540
540
|
* Space Complexity: O(1) - Constant space.
|
|
541
541
|
*
|
|
542
542
|
* The function finds a node in a singly linked list by its value and returns the node if found, otherwise returns
|
|
543
|
-
*
|
|
543
|
+
* undefined.
|
|
544
544
|
* @param {E} value - The value parameter is the value that we want to search for in the linked list.
|
|
545
545
|
* @returns a `SinglyLinkedListNode<E>` if a node with the specified value is found in the linked list. If no node with
|
|
546
|
-
* the specified value is found, the function returns `
|
|
546
|
+
* the specified value is found, the function returns `undefined`.
|
|
547
547
|
*/
|
|
548
|
-
getNode(value: E): SinglyLinkedListNode<E> |
|
|
548
|
+
getNode(value: E): SinglyLinkedListNode<E> | undefined {
|
|
549
549
|
let current = this.head;
|
|
550
550
|
|
|
551
551
|
while (current) {
|
|
@@ -555,7 +555,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
555
555
|
current = current.next;
|
|
556
556
|
}
|
|
557
557
|
|
|
558
|
-
return
|
|
558
|
+
return undefined;
|
|
559
559
|
}
|
|
560
560
|
|
|
561
561
|
/**
|
|
@@ -620,7 +620,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
620
620
|
* existing value or node, and false if the existing value or node was not found in the linked list.
|
|
621
621
|
*/
|
|
622
622
|
insertAfter(existingValueOrNode: E | SinglyLinkedListNode<E>, newValue: E): boolean {
|
|
623
|
-
let existingNode: E | SinglyLinkedListNode<E> |
|
|
623
|
+
let existingNode: E | SinglyLinkedListNode<E> | undefined;
|
|
624
624
|
|
|
625
625
|
if (existingValueOrNode instanceof SinglyLinkedListNode) {
|
|
626
626
|
existingNode = existingValueOrNode;
|
|
@@ -27,7 +27,7 @@ export class SkipList<K, V> {
|
|
|
27
27
|
* level in the skip list. It is used to determine the height of each node in the skip list.
|
|
28
28
|
*/
|
|
29
29
|
constructor(maxLevel = 16, probability = 0.5) {
|
|
30
|
-
this._head = new SkipListNode<K, V>(
|
|
30
|
+
this._head = new SkipListNode<K, V>(undefined as any, undefined as any, maxLevel);
|
|
31
31
|
this._level = 0;
|
|
32
32
|
this._maxLevel = maxLevel;
|
|
33
33
|
this._probability = probability;
|
|
@@ -88,7 +88,7 @@ export class SkipList<K, V> {
|
|
|
88
88
|
update[i].forward[i] = newNode;
|
|
89
89
|
}
|
|
90
90
|
|
|
91
|
-
if (newNode.forward[0]
|
|
91
|
+
if (!newNode.forward[0]) {
|
|
92
92
|
this._level = Math.max(this.level, newNode.forward.length);
|
|
93
93
|
}
|
|
94
94
|
}
|
|
@@ -172,7 +172,7 @@ export class SkipList<K, V> {
|
|
|
172
172
|
}
|
|
173
173
|
update[i].forward[i] = current.forward[i];
|
|
174
174
|
}
|
|
175
|
-
while (this.level > 0 && this.head.forward[this.level - 1]
|
|
175
|
+
while (this.level > 0 && !this.head.forward[this.level - 1]) {
|
|
176
176
|
this._level--;
|
|
177
177
|
}
|
|
178
178
|
return true;
|
|
@@ -259,7 +259,7 @@ export class SkipList<K, V> {
|
|
|
259
259
|
*/
|
|
260
260
|
lower(key: K): V | undefined {
|
|
261
261
|
let current = this.head;
|
|
262
|
-
let lastLess =
|
|
262
|
+
let lastLess = undefined;
|
|
263
263
|
|
|
264
264
|
for (let i = this.level - 1; i >= 0; i--) {
|
|
265
265
|
while (current.forward[i] && current.forward[i].key < key) {
|