min-heap-typed 1.50.4 → 1.50.6

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 (28) hide show
  1. package/dist/data-structures/base/iterable-base.d.ts +10 -8
  2. package/dist/data-structures/base/iterable-base.js +8 -12
  3. package/dist/data-structures/binary-tree/binary-tree.js +19 -19
  4. package/dist/data-structures/binary-tree/rb-tree.d.ts +158 -135
  5. package/dist/data-structures/binary-tree/rb-tree.js +415 -386
  6. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +1 -0
  7. package/dist/data-structures/binary-tree/tree-multi-map.js +84 -76
  8. package/dist/data-structures/graph/abstract-graph.d.ts +1 -0
  9. package/dist/data-structures/graph/abstract-graph.js +3 -0
  10. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +14 -76
  11. package/dist/data-structures/linked-list/doubly-linked-list.js +16 -86
  12. package/dist/data-structures/linked-list/singly-linked-list.d.ts +27 -69
  13. package/dist/data-structures/linked-list/singly-linked-list.js +35 -79
  14. package/dist/data-structures/queue/deque.d.ts +0 -53
  15. package/dist/data-structures/queue/deque.js +0 -61
  16. package/dist/data-structures/queue/queue.d.ts +0 -70
  17. package/dist/data-structures/queue/queue.js +0 -87
  18. package/package.json +2 -2
  19. package/src/data-structures/base/iterable-base.ts +14 -10
  20. package/src/data-structures/binary-tree/binary-tree.ts +19 -19
  21. package/src/data-structures/binary-tree/rb-tree.ts +437 -395
  22. package/src/data-structures/binary-tree/tree-multi-map.ts +85 -82
  23. package/src/data-structures/graph/abstract-graph.ts +4 -0
  24. package/src/data-structures/heap/heap.ts +1 -1
  25. package/src/data-structures/linked-list/doubly-linked-list.ts +16 -94
  26. package/src/data-structures/linked-list/singly-linked-list.ts +35 -87
  27. package/src/data-structures/queue/deque.ts +0 -67
  28. package/src/data-structures/queue/queue.ts +0 -98
@@ -88,10 +88,13 @@ export class TreeMultiMap<
88
88
  * @returns the sum of the count property of all nodes in the tree.
89
89
  */
90
90
  get count(): number {
91
+ return this._count;
92
+ }
93
+
94
+ getMutableCount(): number {
91
95
  let sum = 0;
92
96
  this.dfs(node => (sum += node.count));
93
97
  return sum;
94
- // return this._count;
95
98
  }
96
99
 
97
100
  /**
@@ -192,14 +195,15 @@ export class TreeMultiMap<
192
195
  */
193
196
  override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V, count = 1): boolean {
194
197
  const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value, count);
195
- if (newNode === undefined) return false;
198
+ const orgCount = newNode?.count || 0;
199
+ const isSuccessAdded = super.add(newNode);
196
200
 
197
- const orgNodeCount = newNode?.count || 0;
198
- const inserted = super.add(newNode);
199
- if (inserted) {
200
- this._count += orgNodeCount;
201
+ if (isSuccessAdded) {
202
+ this._count += orgCount;
203
+ return true;
204
+ } else {
205
+ return false;
201
206
  }
202
- return true;
203
207
  }
204
208
 
205
209
  /**
@@ -232,92 +236,91 @@ export class TreeMultiMap<
232
236
  callback: C = this._defaultOneParamCallback as C,
233
237
  ignoreCount = false
234
238
  ): BinaryTreeDeleteResult<NODE>[] {
235
- const deleteResults: BinaryTreeDeleteResult<NODE>[] = [];
236
- if (identifier === null) return deleteResults;
237
-
238
- // Helper function to perform deletion
239
- const deleteHelper = (node: NODE | undefined): void => {
240
- // Initialize targetNode to the sentinel node
241
- let targetNode: NODE = this._Sentinel;
242
- let currentNode: NODE | undefined;
243
-
244
- // Find the node to be deleted based on the identifier
245
- while (node !== this._Sentinel) {
246
- // Update targetNode if the current node matches the identifier
247
- if (node && callback(node) === identifier) {
248
- targetNode = node;
249
- }
239
+ if (identifier === null) return [];
240
+ const results: BinaryTreeDeleteResult<NODE>[] = [];
250
241
 
251
- // Move to the right or left based on the comparison with the identifier
252
- if (node && identifier && callback(node) <= identifier) {
253
- node = node.right;
254
- } else {
255
- node = node?.left;
256
- }
257
- }
242
+ const nodeToDelete = this.isRealNode(identifier) ? identifier : this.getNode(identifier, callback);
258
243
 
259
- // If the target node is not found, decrement size and return
260
- if (targetNode === this._Sentinel) {
261
- return;
262
- }
244
+ if (!nodeToDelete) {
245
+ return results;
246
+ }
263
247
 
264
- if (ignoreCount || targetNode.count <= 1) {
265
- // Store the parent of the target node and its original color
266
- let parentNode = targetNode;
267
- let parentNodeOriginalColor: number = parentNode.color;
268
-
269
- // Handle deletion based on the number of children of the target node
270
- if (targetNode.left === this._Sentinel) {
271
- // Target node has no left child - deletion case 1
272
- currentNode = targetNode.right;
273
- this._rbTransplant(targetNode, targetNode.right!);
274
- } else if (targetNode.right === this._Sentinel) {
275
- // Target node has no right child - deletion case 2
276
- currentNode = targetNode.left;
277
- this._rbTransplant(targetNode, targetNode.left!);
248
+ let originalColor = nodeToDelete.color;
249
+ let replacementNode: NODE | undefined;
250
+
251
+ if (!this.isRealNode(nodeToDelete.left)) {
252
+ replacementNode = nodeToDelete.right;
253
+ if (ignoreCount || nodeToDelete.count <= 1) {
254
+ this._transplant(nodeToDelete, nodeToDelete.right);
255
+ this._count -= nodeToDelete.count;
256
+ } else {
257
+ nodeToDelete.count--;
258
+ this._count--;
259
+ results.push({ deleted: nodeToDelete, needBalanced: undefined });
260
+ return results;
261
+ }
262
+ } else if (!this.isRealNode(nodeToDelete.right)) {
263
+ replacementNode = nodeToDelete.left;
264
+ if (ignoreCount || nodeToDelete.count <= 1) {
265
+ this._transplant(nodeToDelete, nodeToDelete.left);
266
+ this._count -= nodeToDelete.count;
267
+ } else {
268
+ nodeToDelete.count--;
269
+ this._count--;
270
+ results.push({ deleted: nodeToDelete, needBalanced: undefined });
271
+ return results;
272
+ }
273
+ } else {
274
+ const successor = this.getLeftMost(nodeToDelete.right);
275
+ if (successor) {
276
+ originalColor = successor.color;
277
+ replacementNode = successor.right;
278
+
279
+ if (successor.parent === nodeToDelete) {
280
+ if (this.isRealNode(replacementNode)) {
281
+ replacementNode.parent = successor;
282
+ }
278
283
  } else {
279
- // Target node has both left and right children - deletion case 3
280
- parentNode = this.getLeftMost(targetNode.right)!;
281
- parentNodeOriginalColor = parentNode.color;
282
- currentNode = parentNode.right;
283
-
284
- if (parentNode.parent === targetNode) {
285
- // Target node's right child becomes its parent's left child
286
- currentNode!.parent = parentNode;
284
+ if (ignoreCount || nodeToDelete.count <= 1) {
285
+ this._transplant(successor, successor.right);
286
+ this._count -= nodeToDelete.count;
287
287
  } else {
288
- // Replace parentNode with its right child and update connections
289
- this._rbTransplant(parentNode, parentNode.right!);
290
- parentNode.right = targetNode.right;
291
- parentNode.right!.parent = parentNode;
288
+ nodeToDelete.count--;
289
+ this._count--;
290
+ results.push({ deleted: nodeToDelete, needBalanced: undefined });
291
+ return results;
292
+ }
293
+ successor.right = nodeToDelete.right;
294
+ if (this.isRealNode(successor.right)) {
295
+ successor.right.parent = successor;
292
296
  }
293
-
294
- // Replace the target node with its in-order successor
295
- this._rbTransplant(targetNode, parentNode);
296
- parentNode.left = targetNode.left;
297
- parentNode.left!.parent = parentNode;
298
- parentNode.color = targetNode.color;
299
297
  }
300
-
301
- // Fix the Red-Black Tree properties after deletion
302
- if (parentNodeOriginalColor === RBTNColor.BLACK) {
303
- this._fixDelete(currentNode!);
298
+ if (ignoreCount || nodeToDelete.count <= 1) {
299
+ this._transplant(nodeToDelete, successor);
300
+ this._count -= nodeToDelete.count;
301
+ } else {
302
+ nodeToDelete.count--;
303
+ this._count--;
304
+ results.push({ deleted: nodeToDelete, needBalanced: undefined });
305
+ return results;
304
306
  }
305
-
306
- // Decrement the size and store information about the deleted node
307
- this._size--;
308
- this._count -= targetNode.count;
309
- deleteResults.push({ deleted: targetNode, needBalanced: undefined });
310
- } else {
311
- targetNode.count--;
312
- this._count--;
307
+ successor.left = nodeToDelete.left;
308
+ if (this.isRealNode(successor.left)) {
309
+ successor.left.parent = successor;
310
+ }
311
+ successor.color = nodeToDelete.color;
313
312
  }
314
- };
313
+ }
314
+ this._size--;
315
+
316
+ // If the original color was black, fix the tree
317
+ if (originalColor === RBTNColor.BLACK) {
318
+ this._deleteFixup(replacementNode);
319
+ }
315
320
 
316
- // Call the helper function with the root of the tree
317
- deleteHelper(this.root);
321
+ results.push({ deleted: nodeToDelete, needBalanced: undefined });
318
322
 
319
- // Return the result array
320
- return deleteResults;
323
+ return results;
321
324
  }
322
325
 
323
326
  /**
@@ -82,6 +82,10 @@ export abstract class AbstractGraph<
82
82
  this._vertexMap = v;
83
83
  }
84
84
 
85
+ get size(): number {
86
+ return this._vertexMap.size;
87
+ }
88
+
85
89
  /**
86
90
  * In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
87
91
  * This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
@@ -200,7 +200,7 @@ export class Heap<E = any> extends IterableElementBase<E> {
200
200
  * @param element - the element to check.
201
201
  * @returns Returns true if the specified element is contained; otherwise, returns false.
202
202
  */
203
- has(element: E): boolean {
203
+ override has(element: E): boolean {
204
204
  return this.elements.includes(element);
205
205
  }
206
206
 
@@ -194,14 +194,13 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
194
194
  */
195
195
 
196
196
  /**
197
- * Time Complexity: O(1)
198
- * Space Complexity: O(1)
199
- *
200
- * The push function adds a new node with the given value to the end of the doubly linked list.
201
- * @param {E} value - The value to be added to the linked list.
197
+ * The push function adds a new element to the end of a doubly linked list.
198
+ * @param {E} element - The "element" parameter represents the value that you want to add to the
199
+ * doubly linked list.
200
+ * @returns The `push` method is returning a boolean value, `true`.
202
201
  */
203
- push(value: E): boolean {
204
- const newNode = new DoublyLinkedListNode(value);
202
+ push(element: E): boolean {
203
+ const newNode = new DoublyLinkedListNode(element);
205
204
  if (!this.head) {
206
205
  this._head = newNode;
207
206
  this._tail = newNode;
@@ -220,12 +219,8 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
220
219
  */
221
220
 
222
221
  /**
223
- * Time Complexity: O(1)
224
- * Space Complexity: O(1)
225
- *
226
- * The `pop()` function removes and returns the value of the last node in a doubly linked list.
227
- * @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
228
- * list is empty, it returns undefined.
222
+ * The `pop()` function removes and returns the value of the last element in a linked list.
223
+ * @returns The method is returning the value of the removed node.
229
224
  */
230
225
  pop(): E | undefined {
231
226
  if (!this.tail) return undefined;
@@ -247,12 +242,8 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
247
242
  */
248
243
 
249
244
  /**
250
- * Time Complexity: O(1)
251
- * Space Complexity: O(1)
252
- *
253
- * The `shift()` function removes and returns the value of the first node in a doubly linked list.
254
- * @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
255
- * list.
245
+ * The `shift()` function removes and returns the value of the first element in a doubly linked list.
246
+ * @returns The value of the removed node.
256
247
  */
257
248
  shift(): E | undefined {
258
249
  if (!this.head) return undefined;
@@ -274,15 +265,13 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
274
265
  */
275
266
 
276
267
  /**
277
- * Time Complexity: O(1)
278
- * Space Complexity: O(1)
279
- *
280
- * The unshift function adds a new node with the given value to the beginning of a doubly linked list.
281
- * @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
282
- * doubly linked list.
268
+ * The unshift function adds a new element to the beginning of a doubly linked list.
269
+ * @param {E} element - The "element" parameter represents the value of the element that you want to
270
+ * add to the beginning of the doubly linked list.
271
+ * @returns The `unshift` method is returning a boolean value, `true`.
283
272
  */
284
- unshift(value: E): boolean {
285
- const newNode = new DoublyLinkedListNode(value);
273
+ unshift(element: E): boolean {
274
+ const newNode = new DoublyLinkedListNode(element);
286
275
  if (!this.head) {
287
276
  this._head = newNode;
288
277
  this._tail = newNode;
@@ -811,73 +800,6 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
811
800
  return mappedList;
812
801
  }
813
802
 
814
- /**
815
- * Time Complexity: O(1)
816
- * Space Complexity: O(1)
817
- */
818
-
819
- /**
820
- * Time Complexity: O(1)
821
- * Space Complexity: O(1)
822
- *
823
- * The addLast function adds a new node with the given value to the end of the doubly linked list.
824
- * @param {E} value - The value to be added to the linked list.
825
- */
826
- addLast(value: E): boolean {
827
- return this.push(value);
828
- }
829
-
830
- /**
831
- * Time Complexity: O(1)
832
- * Space Complexity: O(1)
833
- */
834
-
835
- /**
836
- * Time Complexity: O(1)
837
- * Space Complexity: O(1)
838
- *
839
- * The `pollLast()` function removes and returns the value of the last node in a doubly linked list.
840
- * @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
841
- * list is empty, it returns undefined.
842
- */
843
- pollLast(): E | undefined {
844
- return this.pop();
845
- }
846
-
847
- /**
848
- * Time Complexity: O(1)
849
- * Space Complexity: O(1)
850
- */
851
-
852
- /**
853
- * Time Complexity: O(1)
854
- * Space Complexity: O(1)
855
- *
856
- * The `pollFirst()` function removes and returns the value of the first node in a doubly linked list.
857
- * @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
858
- * list.
859
- */
860
- pollFirst(): E | undefined {
861
- return this.shift();
862
- }
863
-
864
- /**
865
- * Time Complexity: O(1)
866
- * Space Complexity: O(1)
867
- */
868
-
869
- /**
870
- * Time Complexity: O(1)
871
- * Space Complexity: O(1)
872
- *
873
- * The addFirst function adds a new node with the given value to the beginning of a doubly linked list.
874
- * @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
875
- * doubly linked list.
876
- */
877
- addFirst(value: E): void {
878
- this.unshift(value);
879
- }
880
-
881
803
  /**
882
804
  * The function returns an iterator that iterates over the values of a linked list.
883
805
  */
@@ -93,6 +93,24 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
93
93
  return this._tail;
94
94
  }
95
95
 
96
+ /**
97
+ * The above function returns the value of the first element in a linked list, or undefined if the
98
+ * list is empty.
99
+ * @returns The value of the first node in the linked list, or undefined if the linked list is empty.
100
+ */
101
+ get first(): E | undefined {
102
+ return this.head?.value;
103
+ }
104
+
105
+ /**
106
+ * The function returns the value of the last element in a linked list, or undefined if the list is
107
+ * empty.
108
+ * @returns The value of the last node in the linked list, or undefined if the linked list is empty.
109
+ */
110
+ get last(): E | undefined {
111
+ return this.tail?.value;
112
+ }
113
+
96
114
  protected _size: number = 0;
97
115
 
98
116
  /**
@@ -130,20 +148,19 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
130
148
  /**
131
149
  * Time Complexity: O(1)
132
150
  * Space Complexity: O(1)
133
- * Constant time, as it involves basic pointer adjustments.
134
- * Constant space, as it only creates a new node.
135
151
  */
136
152
 
137
153
  /**
138
154
  * Time Complexity: O(1)
139
155
  * Space Complexity: O(1)
140
156
  *
141
- * The `push` function adds a new node with the given value to the end of a singly linked list.
142
- * @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
143
- * any type (E) as specified in the generic type declaration of the class or function.
157
+ * The push function adds a new element to the end of a singly linked list.
158
+ * @param {E} element - The "element" parameter represents the value of the element that you want to
159
+ * add to the linked list.
160
+ * @returns The `push` method is returning a boolean value, `true`.
144
161
  */
145
- push(value: E): boolean {
146
- const newNode = new SinglyLinkedListNode(value);
162
+ push(element: E): boolean {
163
+ const newNode = new SinglyLinkedListNode(element);
147
164
  if (!this.head) {
148
165
  this._head = newNode;
149
166
  this._tail = newNode;
@@ -155,23 +172,6 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
155
172
  return true;
156
173
  }
157
174
 
158
- /**
159
- * Time Complexity: O(1)
160
- * Space Complexity: O(1)
161
- */
162
-
163
- /**
164
- * Time Complexity: O(1)
165
- * Space Complexity: O(1)
166
- *
167
- * The `push` function adds a new node with the given value to the end of a singly linked list.
168
- * @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
169
- * any type (E) as specified in the generic type declaration of the class or function.
170
- */
171
- addLast(value: E): boolean {
172
- return this.push(value);
173
- }
174
-
175
175
  /**
176
176
  * Time Complexity: O(n)
177
177
  * Space Complexity: O(1)
@@ -182,10 +182,9 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
182
182
  * Time Complexity: O(n)
183
183
  * Space Complexity: O(1)
184
184
  *
185
- * The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
186
- * pointers accordingly.
187
- * @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
188
- * the linked list is empty, it returns `undefined`.
185
+ * The `pop` function removes and returns the value of the last element in a linked list.
186
+ * @returns The method is returning the value of the element that is being popped from the end of the
187
+ * list.
189
188
  */
190
189
  pop(): E | undefined {
191
190
  if (!this.head) return undefined;
@@ -208,24 +207,6 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
208
207
  return value;
209
208
  }
210
209
 
211
- /**
212
- * Time Complexity: O(n)
213
- * Space Complexity: O(1)
214
- */
215
-
216
- /**
217
- * Time Complexity: O(n)
218
- * Space Complexity: O(1)
219
- *
220
- * The `pollLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
221
- * pointers accordingly.
222
- * @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
223
- * the linked list is empty, it returns `undefined`.
224
- */
225
- pollLast(): E | undefined {
226
- return this.pop();
227
- }
228
-
229
210
  /**
230
211
  * Time Complexity: O(1)
231
212
  * Space Complexity: O(1)
@@ -235,8 +216,8 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
235
216
  * Time Complexity: O(1)
236
217
  * Space Complexity: O(1)
237
218
  *
238
- * The `shift()` function removes and returns the value of the first node in a linked list.
239
- * @returns The value of the node that is being removed from the beginning of the linked list.
219
+ * The `shift()` function removes and returns the value of the first element in a linked list.
220
+ * @returns The value of the removed node.
240
221
  */
241
222
  shift(): E | undefined {
242
223
  if (!this.head) return undefined;
@@ -255,28 +236,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
255
236
  * Time Complexity: O(1)
256
237
  * Space Complexity: O(1)
257
238
  *
258
- * The `pollFirst()` function removes and returns the value of the first node in a linked list.
259
- * @returns The value of the node that is being removed from the beginning of the linked list.
239
+ * The unshift function adds a new element to the beginning of a singly linked list.
240
+ * @param {E} element - The "element" parameter represents the value of the element that you want to
241
+ * add to the beginning of the singly linked list.
242
+ * @returns The `unshift` method is returning a boolean value, `true`.
260
243
  */
261
- pollFirst(): E | undefined {
262
- return this.shift();
263
- }
264
-
265
- /**
266
- * Time Complexity: O(1)
267
- * Space Complexity: O(1)
268
- */
269
-
270
- /**
271
- * Time Complexity: O(1)
272
- * Space Complexity: O(1)
273
- *
274
- * The unshift function adds a new node with the given value to the beginning of a singly linked list.
275
- * @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
276
- * linked list.
277
- */
278
- unshift(value: E): boolean {
279
- const newNode = new SinglyLinkedListNode(value);
244
+ unshift(element: E): boolean {
245
+ const newNode = new SinglyLinkedListNode(element);
280
246
  if (!this.head) {
281
247
  this._head = newNode;
282
248
  this._tail = newNode;
@@ -288,27 +254,9 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
288
254
  return true;
289
255
  }
290
256
 
291
- /**
292
- * Time Complexity: O(1)
293
- * Space Complexity: O(1)
294
- */
295
-
296
- /**
297
- * Time Complexity: O(1)
298
- * Space Complexity: O(1)
299
- *
300
- * The addFirst function adds a new node with the given value to the beginning of a singly linked list.
301
- * @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
302
- * linked list.
303
- */
304
- addFirst(value: E): boolean {
305
- return this.unshift(value);
306
- }
307
-
308
257
  /**
309
258
  * Time Complexity: O(n)
310
259
  * Space Complexity: O(1)
311
- * Linear time, where n is the index, as it may need to traverse the list to find the desired node.
312
260
  */
313
261
 
314
262
  /**
@@ -811,73 +811,6 @@ export class Deque<E> extends IterableElementBase<E> {
811
811
  return newDeque;
812
812
  }
813
813
 
814
- /**
815
- * Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
816
- * Space Complexity: O(n) - Due to potential resizing.
817
- */
818
-
819
- /**
820
- * Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
821
- * Space Complexity: O(n) - Due to potential resizing.
822
- *
823
- * The addLast function adds an element to the end of an array.
824
- * @param {E} element - The element parameter represents the element that you want to add to the end of the
825
- * data structure.
826
- */
827
- addLast(element: E): boolean {
828
- return this.push(element);
829
- }
830
-
831
- /**
832
- * Time Complexity: O(1)
833
- * Space Complexity: O(1)
834
- */
835
-
836
- /**
837
- * Time Complexity: O(1)
838
- * Space Complexity: O(1)
839
- *
840
- * The function "pollLast" removes and returns the last element of an array.
841
- * @returns The last element of the array is being returned.
842
- */
843
- pollLast(): E | undefined {
844
- return this.pop();
845
- }
846
-
847
- /**
848
- * Time Complexity: O(1)
849
- * Space Complexity: O(1)
850
- * /
851
-
852
- /**
853
- * Time Complexity: O(1)
854
- * Space Complexity: O(1)
855
- *
856
- * The "addFirst" function adds an element to the beginning of an array.
857
- * @param {E} element - The parameter "element" represents the element that you want to add to the
858
- * beginning of the data structure.
859
- */
860
- addFirst(element: E): boolean {
861
- return this.unshift(element);
862
- }
863
-
864
- /**
865
- * Time Complexity: O(1)
866
- * Space Complexity: O(1)
867
- * /
868
-
869
- /**
870
- * Time Complexity: O(1)
871
- * Space Complexity: O(1)
872
- *
873
- * The function "pollFirst" removes and returns the first element of an array.
874
- * @returns The method `pollFirst()` is returning the first element of the array after removing it
875
- * from the beginning. If the array is empty, it will return `undefined`.
876
- */
877
- pollFirst(): E | undefined {
878
- return this.shift();
879
- }
880
-
881
814
  /**
882
815
  * Time Complexity: O(n)
883
816
  * Space Complexity: O(1)