queue-typed 1.47.3 → 1.47.5

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 (29) hide show
  1. package/README.md +410 -353
  2. package/dist/data-structures/hash/hash-map.d.ts +3 -3
  3. package/dist/data-structures/hash/hash-map.js +10 -4
  4. package/dist/data-structures/hash/hash-table.d.ts +9 -4
  5. package/dist/data-structures/hash/hash-table.js +50 -5
  6. package/dist/data-structures/heap/heap.d.ts +6 -1
  7. package/dist/data-structures/heap/heap.js +51 -9
  8. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +56 -56
  9. package/dist/data-structures/linked-list/doubly-linked-list.js +117 -119
  10. package/dist/data-structures/linked-list/singly-linked-list.d.ts +36 -36
  11. package/dist/data-structures/linked-list/singly-linked-list.js +58 -60
  12. package/dist/data-structures/queue/deque.d.ts +49 -49
  13. package/dist/data-structures/queue/deque.js +79 -72
  14. package/dist/data-structures/queue/queue.d.ts +45 -0
  15. package/dist/data-structures/queue/queue.js +77 -0
  16. package/dist/data-structures/stack/stack.d.ts +18 -5
  17. package/dist/data-structures/stack/stack.js +56 -7
  18. package/dist/data-structures/trie/trie.d.ts +5 -0
  19. package/dist/data-structures/trie/trie.js +47 -0
  20. package/package.json +2 -2
  21. package/src/data-structures/hash/hash-map.ts +13 -7
  22. package/src/data-structures/hash/hash-table.ts +59 -9
  23. package/src/data-structures/heap/heap.ts +60 -9
  24. package/src/data-structures/linked-list/doubly-linked-list.ts +129 -129
  25. package/src/data-structures/linked-list/singly-linked-list.ts +65 -65
  26. package/src/data-structures/queue/deque.ts +84 -77
  27. package/src/data-structures/queue/queue.ts +84 -0
  28. package/src/data-structures/stack/stack.ts +64 -8
  29. package/src/data-structures/trie/trie.ts +53 -0
@@ -666,63 +666,47 @@ export class SinglyLinkedList<E = any> {
666
666
  }
667
667
 
668
668
  /**
669
- * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
670
- * Space Complexity: O(1) - Constant space.
671
- */
672
-
673
- /**
674
- * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
675
- * Space Complexity: O(1) - Constant space.
676
- *
677
- * The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
678
- * @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
679
- * represents the value of the current node in the linked list, and the index argument represents the index of the
680
- * current node in the linked list.
669
+ * The function returns an iterator that iterates over the values of a linked list.
681
670
  */
682
- forEach(callback: (value: E, index: number) => void): void {
671
+ * [Symbol.iterator]() {
683
672
  let current = this.head;
684
- let index = 0;
673
+
685
674
  while (current) {
686
- callback(current.value, index);
675
+ yield current.value;
687
676
  current = current.next;
688
- index++;
689
677
  }
690
678
  }
691
679
 
692
680
  /**
693
- * Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list.
694
- * Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
681
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
682
+ * Space Complexity: O(1)
695
683
  */
696
684
 
697
685
  /**
698
- * Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list.
699
- * Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
686
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
687
+ * Space Complexity: O(1)
700
688
  *
701
- * The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new
702
- * SinglyLinkedList with the transformed values.
703
- * @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
704
- * the original SinglyLinkedList) and returns a value of type U (the type of values that will be stored in the mapped
705
- * SinglyLinkedList).
706
- * @returns The `map` function is returning a new instance of `SinglyLinkedList<U>` that contains the mapped values.
689
+ * The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
690
+ * @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
691
+ * represents the value of the current node in the linked list, and the index argument represents the index of the
692
+ * current node in the linked list.
707
693
  */
708
- map<U>(callback: (value: E) => U): SinglyLinkedList<U> {
709
- const mappedList = new SinglyLinkedList<U>();
710
- let current = this.head;
711
- while (current) {
712
- mappedList.push(callback(current.value));
713
- current = current.next;
694
+ forEach(callback: (value: E, index: number, list: SinglyLinkedList<E>) => void): void {
695
+ let index = 0;
696
+ for (const el of this) {
697
+ callback(el, index, this);
698
+ index++;
714
699
  }
715
- return mappedList;
716
700
  }
717
701
 
718
702
  /**
719
- * Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list.
720
- * Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
703
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
704
+ * Space Complexity: O(n)
721
705
  */
722
706
 
723
707
  /**
724
- * Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list.
725
- * Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
708
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
709
+ * Space Complexity: O(n)
726
710
  *
727
711
  * The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the
728
712
  * elements that satisfy the given callback function.
@@ -730,55 +714,71 @@ export class SinglyLinkedList<E = any> {
730
714
  * It is used to determine whether a value should be included in the filtered list or not.
731
715
  * @returns The filtered list, which is an instance of the SinglyLinkedList class.
732
716
  */
733
- filter(callback: (value: E) => boolean): SinglyLinkedList<E> {
717
+ filter(callback: (value: E, index: number, list: SinglyLinkedList<E>) => boolean): SinglyLinkedList<E> {
734
718
  const filteredList = new SinglyLinkedList<E>();
735
- let current = this.head;
736
- while (current) {
737
- if (callback(current.value)) {
738
- filteredList.push(current.value);
719
+ let index = 0;
720
+ for (const current of this) {
721
+ if (callback(current, index, this)) {
722
+ filteredList.push(current);
739
723
  }
740
- current = current.next;
724
+ index++;
741
725
  }
742
726
  return filteredList;
743
727
  }
744
728
 
745
729
  /**
746
- * Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list.
747
- * Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
730
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
731
+ * Space Complexity: O(n)
732
+ */
733
+
734
+ /**
735
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
736
+ * Space Complexity: O(n)
737
+ *
738
+ * The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new
739
+ * SinglyLinkedList with the transformed values.
740
+ * @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
741
+ * the original SinglyLinkedList) and returns a value of type T (the type of values that will be stored in the mapped
742
+ * SinglyLinkedList).
743
+ * @returns The `map` function is returning a new instance of `SinglyLinkedList<T>` that contains the mapped values.
744
+ */
745
+ map<T>(callback: (value: E, index: number, list: SinglyLinkedList<E>) => T): SinglyLinkedList<T> {
746
+ const mappedList = new SinglyLinkedList<T>();
747
+ let index = 0;
748
+ for (const current of this) {
749
+ mappedList.push(callback(current, index, this));
750
+ index++;
751
+ }
752
+
753
+ return mappedList;
754
+ }
755
+
756
+ /**
757
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
758
+ * Space Complexity: O(n)
748
759
  */
749
760
 
750
761
  /**
751
- * Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list.
752
- * Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
762
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
763
+ * Space Complexity: O(n)
753
764
  *
754
765
  * The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
755
766
  * single value.
756
767
  * @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
757
768
  * used to perform a specific operation on each element of the linked list.
758
- * @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
769
+ * @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
759
770
  * point for the reduction operation.
760
771
  * @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
761
772
  * elements in the linked list.
762
773
  */
763
- reduce<U>(callback: (accumulator: U, value: E) => U, initialValue: U): U {
774
+ reduce<T>(callback: (accumulator: T, value: E, index: number, list: SinglyLinkedList<E>) => T, initialValue: T): T {
764
775
  let accumulator = initialValue;
765
- let current = this.head;
766
- while (current) {
767
- accumulator = callback(accumulator, current.value);
768
- current = current.next;
776
+ let index = 0;
777
+ for (const current of this) {
778
+ accumulator = callback(accumulator, current, index, this);
779
+ index++;
769
780
  }
770
- return accumulator;
771
- }
772
-
773
- /**
774
- * The function returns an iterator that iterates over the values of a linked list.
775
- */
776
- * [Symbol.iterator]() {
777
- let current = this.head;
778
781
 
779
- while (current) {
780
- yield current.value;
781
- current = current.next;
782
- }
782
+ return accumulator;
783
783
  }
784
784
  }
@@ -637,15 +637,21 @@ export class Deque<E> {
637
637
  * Time Complexity: O(n)
638
638
  * Space Complexity: O(1)
639
639
  *
640
- * The `forEach` function iterates over each element in a deque and applies a callback function to
641
- * each element.
642
- * @param callback - The callback parameter is a function that will be called for each element in the
643
- * deque. It takes three parameters:
640
+ * The `find` function iterates over the elements in a deque and returns the first element for which
641
+ * the callback function returns true, or undefined if no such element is found.
642
+ * @param callback - A function that takes three parameters: element, index, and deque. It should
643
+ * return a boolean value indicating whether the element satisfies a certain condition.
644
+ * @returns The method `find` returns the first element in the deque that satisfies the condition
645
+ * specified by the callback function. If no element satisfies the condition, it returns `undefined`.
644
646
  */
645
- forEach(callback: (element: E, index: number, deque: Deque<E>) => void) {
647
+ find(callback: (element: E, index: number, deque: Deque<E>) => boolean): E | undefined {
646
648
  for (let i = 0; i < this.size; ++i) {
647
- callback(this.getAt(i), i, this);
649
+ const element = this.getAt(i);
650
+ if (callback(element, i, this)) {
651
+ return element;
652
+ }
648
653
  }
654
+ return undefined;
649
655
  }
650
656
 
651
657
  /**
@@ -657,21 +663,20 @@ export class Deque<E> {
657
663
  * Time Complexity: O(n)
658
664
  * Space Complexity: O(1)
659
665
  *
660
- * The `find` function iterates over the elements in a deque and returns the first element for which
661
- * the callback function returns true, or undefined if no such element is found.
662
- * @param callback - A function that takes three parameters: element, index, and deque. It should
663
- * return a boolean value indicating whether the element satisfies a certain condition.
664
- * @returns The method `find` returns the first element in the deque that satisfies the condition
665
- * specified by the callback function. If no element satisfies the condition, it returns `undefined`.
666
+ * The function "indexOf" returns the index of the first occurrence of a given element in an array,
667
+ * or -1 if the element is not found.
668
+ * @param {E} element - The "element" parameter represents the element that you want to find the
669
+ * index of in the data structure.
670
+ * @returns The indexOf function returns the index of the first occurrence of the specified element
671
+ * in the data structure. If the element is not found, it returns -1.
666
672
  */
667
- find(callback: (element: E, index: number, deque: Deque<E>) => boolean): E | undefined {
673
+ indexOf(element: E): number {
668
674
  for (let i = 0; i < this.size; ++i) {
669
- const element = this.getAt(i);
670
- if (callback(element, i, this)) {
671
- return element;
675
+ if (this.getAt(i) === element) {
676
+ return i;
672
677
  }
673
678
  }
674
- return undefined;
679
+ return -1;
675
680
  }
676
681
 
677
682
  /**
@@ -696,102 +701,94 @@ export class Deque<E> {
696
701
 
697
702
  /**
698
703
  * Time Complexity: O(n)
699
- * Space Complexity: O(n)
704
+ * Space Complexity: O(1)
700
705
  */
701
706
 
702
707
  /**
703
708
  * Time Complexity: O(n)
704
- * Space Complexity: O(n)
709
+ * Space Complexity: O(1)
705
710
  *
706
- * The `map` function takes a callback function and applies it to each element in the deque,
707
- * returning a new deque with the results.
708
- * @param callback - The `callback` parameter is a function that takes three arguments:
709
- * @returns The `map` method is returning a new `Deque` object with the transformed elements.
711
+ * The above function is an implementation of the iterator protocol in TypeScript, allowing the
712
+ * object to be iterated over using a for...of loop.
710
713
  */
711
- map<T>(callback: (element: E, index: number, deque: Deque<E>) => T): Deque<T> {
712
- const newDeque = new Deque<T>([], this._bucketSize);
714
+ * [Symbol.iterator]() {
713
715
  for (let i = 0; i < this.size; ++i) {
714
- newDeque.push(callback(this.getAt(i), i, this));
716
+ yield this.getAt(i);
715
717
  }
716
- return newDeque;
717
718
  }
718
719
 
719
720
  /**
720
721
  * Time Complexity: O(n)
721
- * Space Complexity: O(n)
722
+ * Space Complexity: O(1)
722
723
  */
723
724
 
724
725
  /**
725
726
  * Time Complexity: O(n)
726
- * Space Complexity: O(n)
727
+ * Space Complexity: O(1)
727
728
  *
728
- * The `filter` function creates a new deque containing only the elements that satisfy the given
729
- * predicate function.
730
- * @param predicate - The `predicate` parameter is a function that takes three arguments: `element`,
731
- * `index`, and `deque`.
732
- * @returns The `filter` method is returning a new `Deque` object that contains only the elements
733
- * that satisfy the given `predicate` function.
729
+ * The `forEach` function iterates over each element in a deque and applies a callback function to
730
+ * each element.
731
+ * @param callback - The callback parameter is a function that will be called for each element in the
732
+ * deque. It takes three parameters:
734
733
  */
735
- filter(predicate: (element: E, index: number, deque: Deque<E>) => boolean): Deque<E> {
736
- const newDeque = new Deque<E>([], this._bucketSize);
737
- for (let i = 0; i < this.size; ++i) {
738
- const element = this.getAt(i);
739
- if (predicate(element, i, this)) {
740
- newDeque.push(element);
741
- }
734
+ forEach(callback: (element: E, index: number, deque: this) => void) {
735
+ let index = 0;
736
+ for (const el of this) {
737
+ callback(el, index, this);
738
+ index++;
742
739
  }
743
- return newDeque;
744
740
  }
745
741
 
746
742
  /**
747
743
  * Time Complexity: O(n)
748
- * Space Complexity: O(1)
744
+ * Space Complexity: O(n)
749
745
  */
750
746
 
751
747
  /**
752
748
  * Time Complexity: O(n)
753
- * Space Complexity: O(1)
749
+ * Space Complexity: O(n)
754
750
  *
755
- * The `reduce` function iterates over the elements of a deque and applies a callback function to
756
- * each element, accumulating a single value.
757
- * @param callback - The `callback` parameter is a function that takes four arguments:
758
- * @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
759
- * is the value that will be passed as the first argument to the `callback` function when reducing
760
- * the elements of the deque.
761
- * @returns the final value of the accumulator after iterating over all elements in the deque and
762
- * applying the callback function to each element.
751
+ * The `filter` function creates a new deque containing only the elements that satisfy the given
752
+ * predicate function.
753
+ * @param predicate - The `predicate` parameter is a function that takes three arguments: `element`,
754
+ * `index`, and `deque`.
755
+ * @returns The `filter` method is returning a new `Deque` object that contains only the elements
756
+ * that satisfy the given `predicate` function.
763
757
  */
764
- reduce<T>(callback: (accumulator: T, element: E, index: number, deque: Deque<E>) => T, initialValue: T): T {
765
- let accumulator = initialValue;
766
- for (let i = 0; i < this.size; ++i) {
767
- accumulator = callback(accumulator, this.getAt(i), i, this);
758
+ filter(predicate: (element: E, index: number, deque: this) => boolean): Deque<E> {
759
+ const newDeque = new Deque<E>([], this._bucketSize);
760
+ let index = 0;
761
+ for (const el of this) {
762
+ if (predicate(el, index, this)) {
763
+ newDeque.push(el);
764
+ }
765
+ index++;
768
766
  }
769
- return accumulator;
767
+ return newDeque;
770
768
  }
771
769
 
772
770
  /**
773
771
  * Time Complexity: O(n)
774
- * Space Complexity: O(1)
772
+ * Space Complexity: O(n)
775
773
  */
776
774
 
777
775
  /**
778
776
  * Time Complexity: O(n)
779
- * Space Complexity: O(1)
777
+ * Space Complexity: O(n)
780
778
  *
781
- * The function "indexOf" returns the index of the first occurrence of a given element in an array,
782
- * or -1 if the element is not found.
783
- * @param {E} element - The "element" parameter represents the element that you want to find the
784
- * index of in the data structure.
785
- * @returns The indexOf function returns the index of the first occurrence of the specified element
786
- * in the data structure. If the element is not found, it returns -1.
779
+ * The `map` function takes a callback function and applies it to each element in the deque,
780
+ * returning a new deque with the results.
781
+ * @param callback - The `callback` parameter is a function that takes three arguments:
782
+ * @returns The `map` method is returning a new `Deque` object with the transformed elements.
787
783
  */
788
- indexOf(element: E): number {
789
- for (let i = 0; i < this.size; ++i) {
790
- if (this.getAt(i) === element) {
791
- return i;
792
- }
784
+ map<T>(callback: (element: E, index: number, deque: this) => T): Deque<T> {
785
+ const newDeque = new Deque<T>([], this._bucketSize);
786
+ let index = 0;
787
+ for (const el of this) {
788
+ newDeque.push(callback(el, index, this));
789
+ index++;
793
790
  }
794
- return -1;
791
+ return newDeque;
795
792
  }
796
793
 
797
794
  /**
@@ -803,13 +800,23 @@ export class Deque<E> {
803
800
  * Time Complexity: O(n)
804
801
  * Space Complexity: O(1)
805
802
  *
806
- * The above function is an implementation of the iterator protocol in TypeScript, allowing the
807
- * object to be iterated over using a for...of loop.
803
+ * The `reduce` function iterates over the elements of a deque and applies a callback function to
804
+ * each element, accumulating a single value.
805
+ * @param callback - The `callback` parameter is a function that takes four arguments:
806
+ * @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
807
+ * is the value that will be passed as the first argument to the `callback` function when reducing
808
+ * the elements of the deque.
809
+ * @returns the final value of the accumulator after iterating over all elements in the deque and
810
+ * applying the callback function to each element.
808
811
  */
809
- * [Symbol.iterator]() {
810
- for (let i = 0; i < this.size; ++i) {
811
- yield this.getAt(i);
812
+ reduce<T>(callback: (accumulator: T, element: E, index: number, deque: this) => T, initialValue: T): T {
813
+ let accumulator = initialValue;
814
+ let index = 0;
815
+ for (const el of this) {
816
+ accumulator = callback(accumulator, el, index, this);
817
+ index++;
812
818
  }
819
+ return accumulator;
813
820
  }
814
821
 
815
822
  /**
@@ -305,4 +305,88 @@ export class Queue<E = any> {
305
305
  yield item;
306
306
  }
307
307
  }
308
+
309
+ /**
310
+ * Time Complexity: O(n)
311
+ * Space Complexity: O(1)
312
+ */
313
+
314
+ /**
315
+ * Time Complexity: O(n)
316
+ * Space Complexity: O(1)
317
+ *
318
+ * The `forEach` function iterates over each element in a deque and applies a callback function to
319
+ * each element.
320
+ * @param callback - The callback parameter is a function that will be called for each element in the
321
+ * deque. It takes three parameters:
322
+ */
323
+ forEach(callback: (element: E, index: number, queue: this) => void) {
324
+ let index = 0;
325
+ for (const el of this) {
326
+ callback(el, index, this);
327
+ index++;
328
+ }
329
+ }
330
+
331
+ /**
332
+ * Time Complexity: O(n)
333
+ * Space Complexity: O(n)
334
+ */
335
+
336
+ /**
337
+ * Time Complexity: O(n)
338
+ * Space Complexity: O(n)
339
+ *
340
+ * The `filter` function creates a new deque containing only the elements that satisfy the given
341
+ * predicate function.
342
+ * @param predicate - The `predicate` parameter is a function that takes three arguments: `element`,
343
+ * `index`, and `deque`.
344
+ * @returns The `filter` method is returning a new `Queue` object that contains only the elements
345
+ * that satisfy the given `predicate` function.
346
+ */
347
+ filter(predicate: (element: E, index: number, queue: this) => boolean): Queue<E> {
348
+ const newDeque = new Queue<E>([]);
349
+ let index = 0;
350
+ for (const el of this) {
351
+ if (predicate(el, index, this)) {
352
+ newDeque.push(el);
353
+ }
354
+ index++;
355
+ }
356
+ return newDeque;
357
+ }
358
+
359
+ /**
360
+ * Time Complexity: O(n)
361
+ * Space Complexity: O(n)
362
+ */
363
+
364
+ /**
365
+ * Time Complexity: O(n)
366
+ * Space Complexity: O(n)
367
+ *
368
+ * The `map` function takes a callback function and applies it to each element in the deque,
369
+ * returning a new deque with the results.
370
+ * @param callback - The `callback` parameter is a function that takes three arguments:
371
+ * @returns The `map` method is returning a new `Queue` object with the transformed elements.
372
+ */
373
+ map<T>(callback: (element: E, index: number, queue: this) => T): Queue<T> {
374
+ const newDeque = new Queue<T>([]);
375
+ let index = 0;
376
+ for (const el of this) {
377
+ newDeque.push(callback(el, index, this));
378
+ index++;
379
+ }
380
+ return newDeque;
381
+ }
382
+
383
+ reduce<T>(callback: (accumulator: T, element: E, index: number, queue: this) => T, initialValue: T): T {
384
+ let accumulator = initialValue;
385
+ let index = 0;
386
+ for (const el of this) {
387
+ accumulator = callback(accumulator, el, index, this);
388
+ index++;
389
+ }
390
+ return accumulator;
391
+ }
308
392
  }
@@ -25,6 +25,14 @@ export class Stack<E = any> {
25
25
  * Space Complexity: O(n), as it creates a new stack with the elements from the input array.
26
26
  */
27
27
 
28
+ /**
29
+ * The size() function returns the number of elements in an array.
30
+ * @returns The size of the elements array.
31
+ */
32
+ get size(): number {
33
+ return this.elements.length;
34
+ }
35
+
28
36
  /**
29
37
  * Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element.
30
38
  * Space Complexity: O(n), as it creates a new stack with the elements from the input array.
@@ -46,14 +54,6 @@ export class Stack<E = any> {
46
54
  return this.elements.length === 0;
47
55
  }
48
56
 
49
- /**
50
- * The size() function returns the number of elements in an array.
51
- * @returns The size of the elements array.
52
- */
53
- size(): number {
54
- return this.elements.length;
55
- }
56
-
57
57
  /**
58
58
  * Time Complexity: O(1), as it only involves accessing the last element of the array.
59
59
  * Space Complexity: O(1), as it does not use any additional space.
@@ -147,4 +147,60 @@ export class Stack<E = any> {
147
147
  clone(): Stack<E> {
148
148
  return new Stack(this.elements.slice());
149
149
  }
150
+
151
+ /**
152
+ * Custom iterator for the Stack class.
153
+ * @returns An iterator object.
154
+ */
155
+ * [Symbol.iterator]() {
156
+ for (let i = this.elements.length - 1; i >= 0; i--) {
157
+ yield this.elements[i];
158
+ }
159
+ }
160
+
161
+ /**
162
+ * Applies a function to each element of the stack.
163
+ * @param {function(E): void} callback - A function to apply to each element.
164
+ */
165
+ forEach(callback: (element: E, index: number, stack: this) => void): void {
166
+ let index = 0;
167
+ for (const el of this) {
168
+ callback(el, index, this);
169
+ index++;
170
+ }
171
+ }
172
+
173
+
174
+ filter(predicate: (element: E, index: number, stack: this) => boolean): Stack<E> {
175
+ const newStack = new Stack<E>();
176
+ let index = 0;
177
+ for (const el of this) {
178
+ if (predicate(el, index, this)) {
179
+ newStack.push(el);
180
+ }
181
+ index++;
182
+ }
183
+ return newStack;
184
+ }
185
+
186
+
187
+ map<T>(callback: (element: E, index: number, stack: this) => T): Stack<T> {
188
+ const newStack = new Stack<T>();
189
+ let index = 0;
190
+ for (const el of this) {
191
+ newStack.push(callback(el, index, this));
192
+ index++;
193
+ }
194
+ return newStack;
195
+ }
196
+
197
+ reduce<T>(callback: (accumulator: T, element: E, index: number, stack: this) => T, initialValue: T): T {
198
+ let accumulator = initialValue;
199
+ let index = 0;
200
+ for (const el of this) {
201
+ accumulator = callback(accumulator, el, index, this);
202
+ index++;
203
+ }
204
+ return accumulator;
205
+ }
150
206
  }
@@ -324,6 +324,59 @@ export class Trie {
324
324
  return words;
325
325
  }
326
326
 
327
+ * [Symbol.iterator](): IterableIterator<string> {
328
+ function* _dfs(node: TrieNode, path: string): IterableIterator<string> {
329
+ if (node.isEnd) {
330
+ yield path;
331
+ }
332
+ for (const [char, childNode] of node.children) {
333
+ yield* _dfs(childNode, path + char);
334
+ }
335
+ }
336
+
337
+ yield* _dfs(this.root, '');
338
+ }
339
+
340
+ forEach(callback: (word: string, index: number, trie: this) => void): void {
341
+ let index = 0;
342
+ for (const word of this) {
343
+ callback(word, index, this);
344
+ index++;
345
+ }
346
+ }
347
+
348
+ filter(predicate: (word: string, index: number, trie: this) => boolean): string[] {
349
+ const results: string[] = [];
350
+ let index = 0;
351
+ for (const word of this) {
352
+ if (predicate(word, index, this)) {
353
+ results.push(word);
354
+ }
355
+ index++;
356
+ }
357
+ return results;
358
+ }
359
+
360
+ map(callback: (word: string, index: number, trie: this) => string): Trie {
361
+ const newTrie = new Trie();
362
+ let index = 0;
363
+ for (const word of this) {
364
+ newTrie.add(callback(word, index, this));
365
+ index++;
366
+ }
367
+ return newTrie;
368
+ }
369
+
370
+ reduce<T>(callback: (accumulator: T, word: string, index: number, trie: this) => T, initialValue: T): T {
371
+ let accumulator = initialValue;
372
+ let index = 0;
373
+ for (const word of this) {
374
+ accumulator = callback(accumulator, word, index, this);
375
+ index++;
376
+ }
377
+ return accumulator;
378
+ }
379
+
327
380
  /**
328
381
  * Time Complexity: O(M), where M is the length of the input string.
329
382
  * Space Complexity: O(1) - Constant space.