linked-list-typed 1.49.1 → 1.49.2

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 (31) hide show
  1. package/dist/data-structures/base/iterable-base.d.ts +11 -0
  2. package/dist/data-structures/base/iterable-base.js +21 -0
  3. package/dist/data-structures/hash/hash-map.d.ts +9 -9
  4. package/dist/data-structures/hash/hash-map.js +16 -15
  5. package/dist/data-structures/heap/heap.d.ts +6 -35
  6. package/dist/data-structures/heap/heap.js +10 -42
  7. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +87 -93
  8. package/dist/data-structures/linked-list/doubly-linked-list.js +126 -129
  9. package/dist/data-structures/linked-list/singly-linked-list.d.ts +16 -21
  10. package/dist/data-structures/linked-list/singly-linked-list.js +42 -42
  11. package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -2
  12. package/dist/data-structures/linked-list/skip-linked-list.js +2 -2
  13. package/dist/data-structures/queue/deque.d.ts +70 -75
  14. package/dist/data-structures/queue/deque.js +100 -110
  15. package/dist/data-structures/queue/queue.d.ts +13 -14
  16. package/dist/data-structures/queue/queue.js +15 -18
  17. package/dist/data-structures/stack/stack.d.ts +2 -3
  18. package/dist/data-structures/stack/stack.js +2 -5
  19. package/dist/data-structures/trie/trie.d.ts +1 -2
  20. package/dist/data-structures/trie/trie.js +2 -5
  21. package/package.json +1 -1
  22. package/src/data-structures/base/iterable-base.ts +24 -0
  23. package/src/data-structures/hash/hash-map.ts +27 -28
  24. package/src/data-structures/heap/heap.ts +19 -57
  25. package/src/data-structures/linked-list/doubly-linked-list.ts +138 -142
  26. package/src/data-structures/linked-list/singly-linked-list.ts +49 -49
  27. package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
  28. package/src/data-structures/queue/deque.ts +122 -135
  29. package/src/data-structures/queue/queue.ts +19 -23
  30. package/src/data-structures/stack/stack.ts +4 -8
  31. package/src/data-structures/trie/trie.ts +5 -9
@@ -83,106 +83,6 @@ export class Deque<E> extends IterableElementBase<E> {
83
83
  return this._buckets[this._bucketLast][this._lastInBucket];
84
84
  }
85
85
 
86
- /**
87
- * Time Complexity: O(1) - Removes the last element.
88
- * Space Complexity: O(1) - Operates in-place.
89
- */
90
-
91
- isEmpty() {
92
- return this.size === 0;
93
- }
94
-
95
- /**
96
- * Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
97
- * Space Complexity: O(n) - Due to potential resizing.
98
- */
99
-
100
- /**
101
- * Time Complexity: O(1)
102
- * Space Complexity: O(n) - In worst case, resizing doubles the array size.
103
- *
104
- * The addLast function adds an element to the end of an array.
105
- * @param {E} element - The element parameter represents the element that you want to add to the end of the
106
- * data structure.
107
- */
108
- addLast(element: E): void {
109
- this.push(element);
110
- }
111
-
112
- /**
113
- * Time Complexity: O(1) - Removes the first element.
114
- * Space Complexity: O(1) - In-place operation.
115
- */
116
-
117
- /**
118
- * Time Complexity: O(1) - Removes the last element.
119
- * Space Complexity: O(1) - Operates in-place.
120
- *
121
- * The function "pollLast" removes and returns the last element of an array.
122
- * @returns The last element of the array is being returned.
123
- */
124
- pollLast(): E | undefined {
125
- return this.pop();
126
- }
127
-
128
- /**
129
- * Time Complexity: O(1).
130
- * Space Complexity: O(n) - Due to potential resizing.
131
- *
132
- * The "addFirst" function adds an element to the beginning of an array.
133
- * @param {E} element - The parameter "element" represents the element that you want to add to the
134
- * beginning of the data structure.
135
- */
136
- addFirst(element: E): void {
137
- this.unshift(element);
138
- }
139
-
140
- /**
141
- * Time Complexity: O(1) - Removes the first element.
142
- * Space Complexity: O(1) - In-place operation.
143
- *
144
- * The function "pollFirst" removes and returns the first element of an array.
145
- * @returns The method `pollFirst()` is returning the first element of the array after removing it
146
- * from the beginning. If the array is empty, it will return `undefined`.
147
- */
148
- pollFirst(): E | undefined {
149
- return this.shift();
150
- }
151
-
152
- /**
153
- * The clear() function resets the state of the object by initializing all variables to their default
154
- * values.
155
- */
156
- clear() {
157
- this._buckets = [new Array(this._bucketSize)];
158
- this._bucketCount = 1;
159
- this._bucketFirst = this._bucketLast = this._size = 0;
160
- this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
161
- }
162
-
163
- /**
164
- * The below function is a generator that yields elements from a collection one by one.
165
- */
166
- * begin(): Generator<E> {
167
- let index = 0;
168
- while (index < this.size) {
169
- yield this.getAt(index);
170
- index++;
171
- }
172
- }
173
-
174
- /**
175
- * The function `reverseBegin()` is a generator that yields elements in reverse order starting from
176
- * the last element.
177
- */
178
- * reverseBegin(): Generator<E> {
179
- let index = this.size - 1;
180
- while (index >= 0) {
181
- yield this.getAt(index);
182
- index--;
183
- }
184
- }
185
-
186
86
  /**
187
87
  * Time Complexity - Amortized O(1) (possible reallocation)
188
88
  * Space Complexity - O(n) (due to potential resizing).
@@ -197,7 +97,7 @@ export class Deque<E> extends IterableElementBase<E> {
197
97
  * structure.
198
98
  * @returns The size of the data structure after the element has been pushed.
199
99
  */
200
- push(element: E) {
100
+ push(element: E): boolean {
201
101
  if (this.size) {
202
102
  if (this._lastInBucket < this._bucketSize - 1) {
203
103
  this._lastInBucket += 1;
@@ -215,7 +115,7 @@ export class Deque<E> extends IterableElementBase<E> {
215
115
  }
216
116
  this._size += 1;
217
117
  this._buckets[this._bucketLast][this._lastInBucket] = element;
218
- return this.size;
118
+ return true;
219
119
  }
220
120
 
221
121
  /**
@@ -231,7 +131,7 @@ export class Deque<E> extends IterableElementBase<E> {
231
131
  * internal state variables accordingly.
232
132
  * @returns The element that was removed from the data structure is being returned.
233
133
  */
234
- pop() {
134
+ pop(): E | undefined {
235
135
  if (this.size === 0) return;
236
136
  const element = this._buckets[this._bucketLast][this._lastInBucket];
237
137
  if (this.size !== 1) {
@@ -264,7 +164,7 @@ export class Deque<E> extends IterableElementBase<E> {
264
164
  * beginning of the data structure.
265
165
  * @returns The size of the data structure after the element has been added.
266
166
  */
267
- unshift(element: E) {
167
+ unshift(element: E): boolean {
268
168
  if (this.size) {
269
169
  if (this._firstInBucket > 0) {
270
170
  this._firstInBucket -= 1;
@@ -282,10 +182,9 @@ export class Deque<E> extends IterableElementBase<E> {
282
182
  }
283
183
  this._size += 1;
284
184
  this._buckets[this._bucketFirst][this._firstInBucket] = element;
285
- return this.size;
185
+ return true;
286
186
  }
287
187
 
288
-
289
188
  /**
290
189
  * Time Complexity: O(1)
291
190
  * Space Complexity: O(1)
@@ -300,7 +199,7 @@ export class Deque<E> extends IterableElementBase<E> {
300
199
  * @returns The element that is being removed from the beginning of the data structure is being
301
200
  * returned.
302
201
  */
303
- shift() {
202
+ shift(): E | undefined {
304
203
  if (this.size === 0) return;
305
204
  const element = this._buckets[this._bucketFirst][this._firstInBucket];
306
205
  if (this.size !== 1) {
@@ -318,6 +217,49 @@ export class Deque<E> extends IterableElementBase<E> {
318
217
  return element;
319
218
  }
320
219
 
220
+ /**
221
+ * Time Complexity: O(1) - Removes the last element.
222
+ * Space Complexity: O(1) - Operates in-place.
223
+ */
224
+
225
+ isEmpty(): boolean {
226
+ return this.size === 0;
227
+ }
228
+
229
+ /**
230
+ * The clear() function resets the state of the object by initializing all variables to their default
231
+ * values.
232
+ */
233
+ clear(): void {
234
+ this._buckets = [new Array(this._bucketSize)];
235
+ this._bucketCount = 1;
236
+ this._bucketFirst = this._bucketLast = this._size = 0;
237
+ this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
238
+ }
239
+
240
+ /**
241
+ * The below function is a generator that yields elements from a collection one by one.
242
+ */
243
+ * begin(): Generator<E> {
244
+ let index = 0;
245
+ while (index < this.size) {
246
+ yield this.getAt(index);
247
+ index++;
248
+ }
249
+ }
250
+
251
+ /**
252
+ * The function `reverseBegin()` is a generator that yields elements in reverse order starting from
253
+ * the last element.
254
+ */
255
+ * reverseBegin(): Generator<E> {
256
+ let index = this.size - 1;
257
+ while (index >= 0) {
258
+ yield this.getAt(index);
259
+ index--;
260
+ }
261
+ }
262
+
321
263
 
322
264
  /**
323
265
  * Time Complexity: O(1)
@@ -359,13 +301,14 @@ export class Deque<E> extends IterableElementBase<E> {
359
301
  * @param {E} element - The `element` parameter is the value that you want to set at the specified
360
302
  * position in the data structure.
361
303
  */
362
- setAt(pos: number, element: E) {
304
+ setAt(pos: number, element: E): boolean {
363
305
  rangeCheck(pos, 0, this.size - 1);
364
306
  const {
365
307
  bucketIndex,
366
308
  indexInBucket
367
309
  } = this._getBucketAndPosition(pos);
368
310
  this._buckets[bucketIndex][indexInBucket] = element;
311
+ return true;
369
312
  }
370
313
 
371
314
  /**
@@ -377,7 +320,7 @@ export class Deque<E> extends IterableElementBase<E> {
377
320
  * Time Complexity: O(n)
378
321
  * Space Complexity: O(n)
379
322
  *
380
- * The `insertAt` function inserts one or more elements at a specified position in an array-like data
323
+ * The `addAt` function inserts one or more elements at a specified position in an array-like data
381
324
  * structure.
382
325
  * @param {number} pos - The `pos` parameter represents the position at which the element(s) should
383
326
  * be inserted. It is of type `number`.
@@ -388,7 +331,7 @@ export class Deque<E> extends IterableElementBase<E> {
388
331
  * will be inserted once. However, you can provide a different value for `num` if you want
389
332
  * @returns The size of the array after the insertion is being returned.
390
333
  */
391
- insertAt(pos: number, element: E, num = 1) {
334
+ addAt(pos: number, element: E, num = 1): boolean {
392
335
  const length = this.size;
393
336
  rangeCheck(pos, 0, length);
394
337
  if (pos === 0) {
@@ -404,7 +347,7 @@ export class Deque<E> extends IterableElementBase<E> {
404
347
  for (let i = 0; i < num; ++i) this.push(element);
405
348
  for (let i = 0; i < arr.length; ++i) this.push(arr[i]);
406
349
  }
407
- return this.size;
350
+ return true;
408
351
  }
409
352
 
410
353
  /**
@@ -422,7 +365,7 @@ export class Deque<E> extends IterableElementBase<E> {
422
365
  * cut. It is a number that indicates the index of the character where the cut should be made.
423
366
  * @returns The method is returning the updated size of the data structure.
424
367
  */
425
- cut(pos: number) {
368
+ cut(pos: number): number {
426
369
  if (pos < 0) {
427
370
  this.clear();
428
371
  return 0;
@@ -453,7 +396,7 @@ export class Deque<E> extends IterableElementBase<E> {
453
396
  * the index of the element to be deleted.
454
397
  * @returns The size of the data structure after the deletion operation is performed.
455
398
  */
456
- deleteAt(pos: number) {
399
+ deleteAt(pos: number): boolean {
457
400
  rangeCheck(pos, 0, this.size - 1);
458
401
  if (pos === 0) this.shift();
459
402
  else if (pos === this.size - 1) this.pop();
@@ -474,7 +417,7 @@ export class Deque<E> extends IterableElementBase<E> {
474
417
  }
475
418
  this.pop();
476
419
  }
477
- return this.size;
420
+ return true;
478
421
  }
479
422
 
480
423
  /**
@@ -492,9 +435,9 @@ export class Deque<E> extends IterableElementBase<E> {
492
435
  * the data structure.
493
436
  * @returns The size of the data structure after the element has been deleted.
494
437
  */
495
- delete(element: E) {
438
+ delete(element: E): boolean {
496
439
  const size = this.size;
497
- if (size === 0) return 0;
440
+ if (size === 0) return false;
498
441
  let i = 0;
499
442
  let index = 0;
500
443
  while (i < size) {
@@ -506,7 +449,7 @@ export class Deque<E> extends IterableElementBase<E> {
506
449
  i += 1;
507
450
  }
508
451
  this.cut(index - 1);
509
- return this.size;
452
+ return true;
510
453
  }
511
454
 
512
455
  /**
@@ -523,7 +466,7 @@ export class Deque<E> extends IterableElementBase<E> {
523
466
  * @returns The reverse() method is returning the object itself (this) after performing the reverse
524
467
  * operation on the buckets and updating the relevant properties.
525
468
  */
526
- reverse() {
469
+ reverse(): this {
527
470
  this._buckets.reverse().forEach(function (bucket) {
528
471
  bucket.reverse();
529
472
  });
@@ -548,9 +491,9 @@ export class Deque<E> extends IterableElementBase<E> {
548
491
  * the number of unique elements.
549
492
  * @returns The size of the modified array is being returned.
550
493
  */
551
- unique() {
494
+ unique(): this {
552
495
  if (this.size <= 1) {
553
- return this.size;
496
+ return this;
554
497
  }
555
498
  let index = 1;
556
499
  let prev = this.getAt(0);
@@ -562,7 +505,7 @@ export class Deque<E> extends IterableElementBase<E> {
562
505
  }
563
506
  }
564
507
  this.cut(index - 1);
565
- return this.size;
508
+ return this;
566
509
  }
567
510
 
568
511
  /**
@@ -578,9 +521,9 @@ export class Deque<E> extends IterableElementBase<E> {
578
521
  * @param [comparator] - The `comparator` parameter is a function that takes in two elements `x` and
579
522
  * `y` of type `E` and returns a number. The comparator function is used to determine the order of
580
523
  * the elements in the sorted array.
581
- * @returns The method is returning the sorted instance of the object on which the method is called.
524
+ * @returns Deque<E>
582
525
  */
583
- sort(comparator?: (x: E, y: E) => number) {
526
+ sort(comparator?: (x: E, y: E) => number): this {
584
527
  const arr: E[] = [];
585
528
  for (let i = 0; i < this.size; ++i) {
586
529
  arr.push(this.getAt(i));
@@ -606,7 +549,7 @@ export class Deque<E> extends IterableElementBase<E> {
606
549
  * @returns Nothing is being returned. The function is using the `return` statement to exit early if
607
550
  * `this.size` is 0, but it does not return any value.
608
551
  */
609
- shrinkToFit() {
552
+ shrinkToFit(): void {
610
553
  if (this.size === 0) return;
611
554
  const newBuckets = [];
612
555
  if (this._bucketFirst === this._bucketLast) return;
@@ -650,7 +593,7 @@ export class Deque<E> extends IterableElementBase<E> {
650
593
  return element;
651
594
  }
652
595
  }
653
- return undefined;
596
+ return;
654
597
  }
655
598
 
656
599
  /**
@@ -691,11 +634,7 @@ export class Deque<E> extends IterableElementBase<E> {
691
634
  * @returns The `toArray()` method is returning an array of elements of type `E`.
692
635
  */
693
636
  toArray(): E[] {
694
- const arr: E[] = [];
695
- for (let i = 0; i < this.size; ++i) {
696
- arr.push(this.getAt(i));
697
- }
698
- return arr;
637
+ return [...this];
699
638
  }
700
639
 
701
640
  /**
@@ -758,12 +697,60 @@ export class Deque<E> extends IterableElementBase<E> {
758
697
  }
759
698
 
760
699
  /**
761
- * Time Complexity: O(n)
762
- * Space Complexity: O(n)
700
+ * Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
701
+ * Space Complexity: O(n) - Due to potential resizing.
763
702
  */
764
703
 
765
- print(): void {
766
- console.log([...this])
704
+ /**
705
+ * Time Complexity: O(1)
706
+ * Space Complexity: O(n) - In worst case, resizing doubles the array size.
707
+ *
708
+ * The addLast function adds an element to the end of an array.
709
+ * @param {E} element - The element parameter represents the element that you want to add to the end of the
710
+ * data structure.
711
+ */
712
+ addLast(element: E): boolean {
713
+ return this.push(element);
714
+ }
715
+
716
+ /**
717
+ * Time Complexity: O(1) - Removes the first element.
718
+ * Space Complexity: O(1) - In-place operation.
719
+ */
720
+
721
+ /**
722
+ * Time Complexity: O(1) - Removes the last element.
723
+ * Space Complexity: O(1) - Operates in-place.
724
+ *
725
+ * The function "pollLast" removes and returns the last element of an array.
726
+ * @returns The last element of the array is being returned.
727
+ */
728
+ pollLast(): E | undefined {
729
+ return this.pop();
730
+ }
731
+
732
+ /**
733
+ * Time Complexity: O(1).
734
+ * Space Complexity: O(n) - Due to potential resizing.
735
+ *
736
+ * The "addFirst" function adds an element to the beginning of an array.
737
+ * @param {E} element - The parameter "element" represents the element that you want to add to the
738
+ * beginning of the data structure.
739
+ */
740
+ addFirst(element: E): boolean {
741
+ return this.unshift(element);
742
+ }
743
+
744
+ /**
745
+ * Time Complexity: O(1) - Removes the first element.
746
+ * Space Complexity: O(1) - In-place operation.
747
+ *
748
+ * The function "pollFirst" removes and returns the first element of an array.
749
+ * @returns The method `pollFirst()` is returning the first element of the array after removing it
750
+ * from the beginning. If the array is empty, it will return `undefined`.
751
+ */
752
+ pollFirst(): E | undefined {
753
+ return this.shift();
767
754
  }
768
755
 
769
756
  /**
@@ -773,7 +760,7 @@ export class Deque<E> extends IterableElementBase<E> {
773
760
  * The above function is an implementation of the iterator protocol in TypeScript, allowing the
774
761
  * object to be iterated over using a for...of loop.
775
762
  */
776
- protected* _getIterator() {
763
+ protected* _getIterator(): IterableIterator<E> {
777
764
  for (let i = 0; i < this.size; ++i) {
778
765
  yield this.getAt(i);
779
766
  }
@@ -74,9 +74,9 @@ export class Queue<E = any> extends IterableElementBase<E> {
74
74
  * @param {E} element - The `element` parameter represents the element that you want to add to the queue.
75
75
  * @returns The `add` method is returning a `Queue<E>` object.
76
76
  */
77
- push(element: E): Queue<E> {
77
+ push(element: E): boolean {
78
78
  this.nodes.push(element);
79
- return this;
79
+ return true;
80
80
  }
81
81
 
82
82
  /**
@@ -95,7 +95,7 @@ export class Queue<E = any> extends IterableElementBase<E> {
95
95
  shift(): E | undefined {
96
96
  if (this.size === 0) return undefined;
97
97
 
98
- const first = this.getFirst();
98
+ const first = this.first;
99
99
  this._offset += 1;
100
100
 
101
101
  if (this.offset * 2 < this.nodes.length) return first;
@@ -116,11 +116,11 @@ export class Queue<E = any> extends IterableElementBase<E> {
116
116
  * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
117
117
  * Space Complexity: O(1) - no additional space is used.
118
118
  *
119
- * The `getFirst` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
120
- * @returns The `getFirst()` method returns the first element of the data structure, represented by the `_nodes` array at
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
121
  * the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
122
122
  */
123
- getFirst(): E | undefined {
123
+ get first(): E | undefined {
124
124
  return this.size > 0 ? this.nodes[this.offset] : undefined;
125
125
  }
126
126
 
@@ -138,7 +138,7 @@ export class Queue<E = any> extends IterableElementBase<E> {
138
138
  * the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
139
139
  */
140
140
  peek(): E | undefined {
141
- return this.getFirst();
141
+ return this.first;
142
142
  }
143
143
 
144
144
  /**
@@ -150,11 +150,11 @@ export class Queue<E = any> extends IterableElementBase<E> {
150
150
  * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
151
151
  * Space Complexity: O(1) - no additional space is used.
152
152
  *
153
- * The `getLast` function returns the last element in an array-like data structure, or undefined if the structure is empty.
154
- * @returns The method `getLast()` returns the last element of the `_nodes` array if the array is not empty. If the
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
155
  * array is empty, it returns `undefined`.
156
156
  */
157
- getLast(): E | undefined {
157
+ get last(): E | undefined {
158
158
  return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined;
159
159
  }
160
160
 
@@ -172,7 +172,7 @@ export class Queue<E = any> extends IterableElementBase<E> {
172
172
  * array is empty, it returns `undefined`.
173
173
  */
174
174
  peekLast(): E | undefined {
175
- return this.getLast();
175
+ return this.last;
176
176
  }
177
177
 
178
178
  /**
@@ -187,7 +187,7 @@ export class Queue<E = any> extends IterableElementBase<E> {
187
187
  * The enqueue function adds a value to the end of a queue.
188
188
  * @param {E} value - The value parameter represents the value that you want to add to the queue.
189
189
  */
190
- enqueue(value: E) {
190
+ enqueue(value: E): boolean {
191
191
  return this.push(value);
192
192
  }
193
193
 
@@ -278,10 +278,6 @@ export class Queue<E = any> extends IterableElementBase<E> {
278
278
  return new Queue(this.nodes.slice(this.offset));
279
279
  }
280
280
 
281
- print(): void {
282
- console.log([...this]);
283
- }
284
-
285
281
  /**
286
282
  * Time Complexity: O(n)
287
283
  * Space Complexity: O(n)
@@ -348,7 +344,7 @@ export class Queue<E = any> extends IterableElementBase<E> {
348
344
  * Space Complexity: O(n)
349
345
  */
350
346
 
351
- protected* _getIterator() {
347
+ protected* _getIterator(): IterableIterator<E> {
352
348
  for (const item of this.nodes) {
353
349
  yield item;
354
350
  }
@@ -366,8 +362,8 @@ export class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
366
362
  * The enqueue function adds a value to the end of an array.
367
363
  * @param {E} value - The value parameter represents the value that you want to add to the queue.
368
364
  */
369
- enqueue(value: E) {
370
- this.push(value);
365
+ enqueue(value: E): boolean {
366
+ return this.push(value);
371
367
  }
372
368
 
373
369
  /**
@@ -379,10 +375,10 @@ export class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
379
375
  }
380
376
 
381
377
  /**
382
- * The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
383
- * @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
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`.
384
380
  */
385
- getFirst(): E | undefined {
381
+ get first(): E | undefined {
386
382
  return this.head?.value;
387
383
  }
388
384
 
@@ -391,6 +387,6 @@ export class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
391
387
  * @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
392
388
  */
393
389
  peek(): E | undefined {
394
- return this.getFirst();
390
+ return this.first;
395
391
  }
396
392
  }
@@ -104,9 +104,9 @@ export class Stack<E = any> extends IterableElementBase<E> {
104
104
  * @param {E} element - The parameter "element" is of type E, which means it can be any data type.
105
105
  * @returns The `push` method is returning the updated `Stack<E>` object.
106
106
  */
107
- push(element: E): Stack<E> {
107
+ push(element: E): boolean {
108
108
  this.elements.push(element);
109
- return this;
109
+ return true;
110
110
  }
111
111
 
112
112
  /**
@@ -123,7 +123,7 @@ export class Stack<E = any> extends IterableElementBase<E> {
123
123
  * array is empty, it returns `undefined`.
124
124
  */
125
125
  pop(): E | undefined {
126
- if (this.isEmpty()) return undefined;
126
+ if (this.isEmpty()) return;
127
127
 
128
128
  return this.elements.pop();
129
129
  }
@@ -228,15 +228,11 @@ export class Stack<E = any> extends IterableElementBase<E> {
228
228
  return newStack;
229
229
  }
230
230
 
231
- print(): void {
232
- console.log([...this]);
233
- }
234
-
235
231
  /**
236
232
  * Custom iterator for the Stack class.
237
233
  * @returns An iterator object.
238
234
  */
239
- protected* _getIterator() {
235
+ protected* _getIterator(): IterableIterator<E> {
240
236
  for (let i = 0; i < this.elements.length; i++) {
241
237
  yield this.elements[i];
242
238
  }
@@ -138,7 +138,7 @@ export class Trie extends IterableElementBase<string> {
138
138
  * @param{string} word - The word to delete.
139
139
  * @returns {boolean} True if the word was successfully removed.
140
140
  */
141
- delete(word: string) {
141
+ delete(word: string): boolean {
142
142
  word = this._caseProcess(word);
143
143
  let isDeleted = false;
144
144
  const dfs = (cur: TrieNode, i: number): boolean => {
@@ -184,7 +184,7 @@ export class Trie extends IterableElementBase<string> {
184
184
  * Space Complexity: O(1) - Constant space.
185
185
  *
186
186
  */
187
- getHeight() {
187
+ getHeight(): number {
188
188
  const beginRoot = this.root;
189
189
  let maxDepth = 0;
190
190
  if (beginRoot) {
@@ -371,12 +371,12 @@ export class Trie extends IterableElementBase<string> {
371
371
  * specific object as the context for the `predicate` function. If `thisArg` is provided, it will be
372
372
  * @returns The `filter` method is returning an array of strings (`string[]`).
373
373
  */
374
- filter(predicate: ElementCallback<string, boolean>, thisArg?: any): string[] {
375
- const results: string[] = [];
374
+ filter(predicate: ElementCallback<string, boolean>, thisArg?: any): Trie {
375
+ const results: Trie = new Trie();
376
376
  let index = 0;
377
377
  for (const word of this) {
378
378
  if (predicate.call(thisArg, word, index, this)) {
379
- results.push(word);
379
+ results.add(word);
380
380
  }
381
381
  index++;
382
382
  }
@@ -411,10 +411,6 @@ export class Trie extends IterableElementBase<string> {
411
411
  return newTrie;
412
412
  }
413
413
 
414
- print() {
415
- console.log([...this]);
416
- }
417
-
418
414
  protected* _getIterator(): IterableIterator<string> {
419
415
  function* _dfs(node: TrieNode, path: string): IterableIterator<string> {
420
416
  if (node.isEnd) {