data-structure-typed 1.49.2 → 1.49.3

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.
Files changed (46) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +7 -7
  3. package/dist/cjs/data-structures/graph/abstract-graph.js +43 -12
  4. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  5. package/dist/cjs/data-structures/graph/directed-graph.d.ts +2 -2
  6. package/dist/cjs/data-structures/graph/directed-graph.js +2 -2
  7. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  8. package/dist/cjs/data-structures/graph/undirected-graph.d.ts +1 -1
  9. package/dist/cjs/data-structures/graph/undirected-graph.js +1 -1
  10. package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
  11. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +43 -43
  12. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +49 -49
  13. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  14. package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +25 -25
  15. package/dist/cjs/data-structures/linked-list/skip-linked-list.js +36 -36
  16. package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
  17. package/dist/cjs/data-structures/queue/queue.d.ts +33 -33
  18. package/dist/cjs/data-structures/queue/queue.js +40 -40
  19. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  20. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +7 -7
  21. package/dist/mjs/data-structures/graph/abstract-graph.js +43 -12
  22. package/dist/mjs/data-structures/graph/directed-graph.d.ts +2 -2
  23. package/dist/mjs/data-structures/graph/directed-graph.js +2 -2
  24. package/dist/mjs/data-structures/graph/undirected-graph.d.ts +1 -1
  25. package/dist/mjs/data-structures/graph/undirected-graph.js +1 -1
  26. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +43 -43
  27. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +47 -47
  28. package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +25 -25
  29. package/dist/mjs/data-structures/linked-list/skip-linked-list.js +36 -36
  30. package/dist/mjs/data-structures/queue/queue.d.ts +33 -33
  31. package/dist/mjs/data-structures/queue/queue.js +39 -39
  32. package/dist/umd/data-structure-typed.js +171 -140
  33. package/dist/umd/data-structure-typed.min.js +2 -2
  34. package/dist/umd/data-structure-typed.min.js.map +1 -1
  35. package/package.json +1 -1
  36. package/src/data-structures/graph/abstract-graph.ts +55 -14
  37. package/src/data-structures/graph/directed-graph.ts +3 -2
  38. package/src/data-structures/graph/undirected-graph.ts +1 -1
  39. package/src/data-structures/linked-list/doubly-linked-list.ts +53 -53
  40. package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
  41. package/src/data-structures/linked-list/skip-linked-list.ts +40 -40
  42. package/src/data-structures/queue/queue.ts +45 -45
  43. package/test/unit/data-structures/graph/abstract-graph.test.ts +1 -1
  44. package/test/unit/data-structures/graph/directed-graph.test.ts +48 -3
  45. package/test/unit/data-structures/graph/undirected-graph.test.ts +48 -4
  46. package/test/unit/data-structures/heap/heap.test.ts +6 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "data-structure-typed",
3
- "version": "1.49.2",
3
+ "version": "1.49.3",
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",
@@ -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._addVertexOnly(keyOrVertex);
159
+ return this._addVertex(keyOrVertex);
160
160
  } else {
161
161
  const newVertex = this.createVertex(keyOrVertex, value);
162
- return this._addVertexOnly(newVertex);
162
+ return this._addVertex(newVertex);
163
163
  }
164
164
  }
165
165
 
@@ -242,14 +242,14 @@ export abstract class AbstractGraph<
242
242
 
243
243
  addEdge(srcOrEdge: VO | VertexKey | EO, dest?: VO | VertexKey, weight?: number, value?: E): boolean {
244
244
  if (srcOrEdge instanceof AbstractEdge) {
245
- return this._addEdgeOnly(srcOrEdge);
245
+ return this._addEdge(srcOrEdge);
246
246
  } else {
247
247
  if (dest instanceof AbstractVertex || typeof dest === 'string' || typeof dest === 'number') {
248
248
  if (!(this.hasVertex(srcOrEdge) && this.hasVertex(dest))) return false;
249
249
  if (srcOrEdge instanceof AbstractVertex) srcOrEdge = srcOrEdge.key;
250
250
  if (dest instanceof AbstractVertex) dest = dest.key;
251
251
  const newEdge = this.createEdge(srcOrEdge, dest, weight, value);
252
- return this._addEdgeOnly(newEdge);
252
+ return this._addEdge(newEdge);
253
253
  } else {
254
254
  throw new Error('dest must be a Vertex or vertex key while srcOrEdge is an Edge');
255
255
  }
@@ -1147,14 +1147,6 @@ export abstract class AbstractGraph<
1147
1147
  return this.tarjan(false, false, false, false).lowMap;
1148
1148
  }
1149
1149
 
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
1150
  /**
1159
1151
  * The function "getCutVertexes" returns an array of cut vertexes using the Tarjan algorithm.
1160
1152
  * @returns an array of VO objects, specifically the cut vertexes.
@@ -1180,6 +1172,55 @@ export abstract class AbstractGraph<
1180
1172
  return this.tarjan(false, true, false, false).bridges;
1181
1173
  }
1182
1174
 
1175
+ /**
1176
+ * O(V+E+C)
1177
+ * O(V+C)
1178
+ */
1179
+ getCycles(isInclude2Cycle: boolean = false): VertexKey[][] {
1180
+ const cycles: VertexKey[][] = [];
1181
+ const visited: Set<VO> = new Set();
1182
+
1183
+ const dfs = (vertex: VO, currentPath: VertexKey[], visited: Set<VO>) => {
1184
+ if (visited.has(vertex)) {
1185
+ if ((!isInclude2Cycle && currentPath.length > 2 || isInclude2Cycle && currentPath.length >= 2) && currentPath[0] === vertex.key) {
1186
+ cycles.push([...currentPath]);
1187
+ }
1188
+ return;
1189
+ }
1190
+
1191
+ visited.add(vertex);
1192
+ currentPath.push(vertex.key);
1193
+
1194
+ for (const neighbor of this.getNeighbors(vertex)) {
1195
+ neighbor && dfs(neighbor, currentPath, visited);
1196
+ }
1197
+
1198
+ visited.delete(vertex);
1199
+ currentPath.pop();
1200
+ };
1201
+
1202
+ for (const vertex of this.vertexMap.values()) {
1203
+ dfs(vertex, [], visited);
1204
+ }
1205
+
1206
+ // Use a set to eliminate duplicate cycles
1207
+ const uniqueCycles = new Map<string, VertexKey[]>();
1208
+
1209
+ for (const cycle of cycles) {
1210
+ const sorted = [...cycle].sort().toString()
1211
+
1212
+ if (uniqueCycles.has(sorted)) continue
1213
+ else {
1214
+ uniqueCycles.set(sorted, cycle)
1215
+ }
1216
+ }
1217
+
1218
+ // Convert the unique cycles back to an array
1219
+ return [...uniqueCycles].map(cycleString =>
1220
+ cycleString[1]
1221
+ );
1222
+ }
1223
+
1183
1224
  /**
1184
1225
  * Time Complexity: O(n)
1185
1226
  * Space Complexity: O(n)
@@ -1247,9 +1288,9 @@ export abstract class AbstractGraph<
1247
1288
  }
1248
1289
  }
1249
1290
 
1250
- protected abstract _addEdgeOnly(edge: EO): boolean;
1291
+ protected abstract _addEdge(edge: EO): boolean;
1251
1292
 
1252
- protected _addVertexOnly(newVertex: VO): boolean {
1293
+ protected _addVertex(newVertex: VO): boolean {
1253
1294
  if (this.hasVertex(newVertex)) {
1254
1295
  return false;
1255
1296
  // throw (new Error('Duplicated vertex key is not allowed'));
@@ -596,6 +596,7 @@ export class DirectedGraph<
596
596
  }
597
597
  }
598
598
 
599
+
599
600
  /**
600
601
  * Time Complexity: O(1)
601
602
  * Space Complexity: O(1)
@@ -605,13 +606,13 @@ export class DirectedGraph<
605
606
  * Time Complexity: O(1)
606
607
  * Space Complexity: O(1)
607
608
  *
608
- * The function `_addEdgeOnly` adds an edge to a graph if the source and destination vertexMap exist.
609
+ * The function `_addEdge` adds an edge to a graph if the source and destination vertexMap exist.
609
610
  * @param {EO} edge - The parameter `edge` is of type `EO`, which represents an edge in a graph. It is the edge that
610
611
  * needs to be added to the graph.
611
612
  * @returns a boolean value. It returns true if the edge was successfully added to the graph, and false if either the
612
613
  * source or destination vertex does not exist in the graph.
613
614
  */
614
- protected _addEdgeOnly(edge: EO): boolean {
615
+ protected _addEdge(edge: EO): boolean {
615
616
  if (!(this.hasVertex(edge.src) && this.hasVertex(edge.dest))) {
616
617
  return false;
617
618
  }
@@ -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 _addEdgeOnly(edge: EO): boolean {
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(1)
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(1)
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(1)
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(1)
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), where n is the number of elements in the linked list.
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(n)
644
- * Space Complexity: O(n)
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(n), where n is the number of elements in the linked list.
678
- * Space Complexity: O(n)
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(1)
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(1)
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
  */
@@ -348,7 +348,7 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
348
348
  * @returns The `delete` method returns a boolean value. It returns `true` if the value or node is found and
349
349
  * successfully deleted from the linked list, and `false` if the value or node is not found in the linked list.
350
350
  */
351
- delete(valueOrNode: E | SinglyLinkedListNode<E> | undefined ): boolean {
351
+ delete(valueOrNode: E | SinglyLinkedListNode<E> | undefined): boolean {
352
352
  if (!valueOrNode) return false;
353
353
  let value: E;
354
354
  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(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
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 adds an element to the end of the array.
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(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.
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 this.push(value);
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`.
@@ -66,7 +66,7 @@ class MyGraph<
66
66
  return edge ? undefined : undefined;
67
67
  }
68
68
 
69
- protected _addEdgeOnly(edge: EO): boolean {
69
+ protected _addEdge(edge: EO): boolean {
70
70
  return edge ? true : true;
71
71
  }
72
72
  }