queue-typed 1.47.4 → 1.47.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 (72) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +8 -8
  2. package/dist/data-structures/binary-tree/avl-tree.js +23 -15
  3. package/dist/data-structures/binary-tree/binary-tree.d.ts +65 -28
  4. package/dist/data-structures/binary-tree/binary-tree.js +66 -82
  5. package/dist/data-structures/binary-tree/bst.d.ts +38 -37
  6. package/dist/data-structures/binary-tree/bst.js +56 -40
  7. package/dist/data-structures/binary-tree/rb-tree.d.ts +11 -7
  8. package/dist/data-structures/binary-tree/rb-tree.js +26 -17
  9. package/dist/data-structures/binary-tree/tree-multimap.d.ts +16 -16
  10. package/dist/data-structures/binary-tree/tree-multimap.js +31 -22
  11. package/dist/data-structures/graph/abstract-graph.js +1 -1
  12. package/dist/data-structures/hash/hash-map.d.ts +3 -3
  13. package/dist/data-structures/hash/hash-map.js +10 -4
  14. package/dist/data-structures/hash/hash-table.d.ts +9 -4
  15. package/dist/data-structures/hash/hash-table.js +50 -5
  16. package/dist/data-structures/heap/heap.d.ts +25 -22
  17. package/dist/data-structures/heap/heap.js +101 -41
  18. package/dist/data-structures/heap/max-heap.d.ts +2 -5
  19. package/dist/data-structures/heap/max-heap.js +2 -2
  20. package/dist/data-structures/heap/min-heap.d.ts +2 -5
  21. package/dist/data-structures/heap/min-heap.js +2 -2
  22. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +58 -57
  23. package/dist/data-structures/linked-list/doubly-linked-list.js +125 -119
  24. package/dist/data-structures/linked-list/singly-linked-list.d.ts +38 -37
  25. package/dist/data-structures/linked-list/singly-linked-list.js +65 -60
  26. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
  27. package/dist/data-structures/priority-queue/max-priority-queue.js +2 -2
  28. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
  29. package/dist/data-structures/priority-queue/min-priority-queue.js +2 -2
  30. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -5
  31. package/dist/data-structures/priority-queue/priority-queue.js +2 -2
  32. package/dist/data-structures/queue/deque.d.ts +50 -49
  33. package/dist/data-structures/queue/deque.js +81 -71
  34. package/dist/data-structures/queue/queue.d.ts +46 -0
  35. package/dist/data-structures/queue/queue.js +80 -0
  36. package/dist/data-structures/stack/stack.d.ts +20 -6
  37. package/dist/data-structures/stack/stack.js +65 -8
  38. package/dist/data-structures/trie/trie.d.ts +5 -0
  39. package/dist/data-structures/trie/trie.js +47 -0
  40. package/dist/interfaces/binary-tree.d.ts +3 -1
  41. package/dist/types/common.d.ts +2 -0
  42. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  43. package/dist/types/data-structures/binary-tree/bst.d.ts +2 -2
  44. package/dist/types/data-structures/heap/heap.d.ts +4 -1
  45. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
  46. package/package.json +2 -2
  47. package/src/data-structures/binary-tree/avl-tree.ts +27 -17
  48. package/src/data-structures/binary-tree/binary-tree.ts +114 -97
  49. package/src/data-structures/binary-tree/bst.ts +67 -47
  50. package/src/data-structures/binary-tree/rb-tree.ts +34 -20
  51. package/src/data-structures/binary-tree/tree-multimap.ts +43 -25
  52. package/src/data-structures/graph/abstract-graph.ts +1 -1
  53. package/src/data-structures/hash/hash-map.ts +13 -7
  54. package/src/data-structures/hash/hash-table.ts +59 -9
  55. package/src/data-structures/heap/heap.ts +115 -46
  56. package/src/data-structures/heap/max-heap.ts +5 -5
  57. package/src/data-structures/heap/min-heap.ts +5 -5
  58. package/src/data-structures/linked-list/doubly-linked-list.ts +137 -128
  59. package/src/data-structures/linked-list/singly-linked-list.ts +72 -64
  60. package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
  61. package/src/data-structures/priority-queue/min-priority-queue.ts +12 -12
  62. package/src/data-structures/priority-queue/priority-queue.ts +3 -3
  63. package/src/data-structures/queue/deque.ts +86 -75
  64. package/src/data-structures/queue/queue.ts +88 -0
  65. package/src/data-structures/stack/stack.ts +75 -10
  66. package/src/data-structures/trie/trie.ts +53 -0
  67. package/src/interfaces/binary-tree.ts +13 -1
  68. package/src/types/common.ts +5 -1
  69. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  70. package/src/types/data-structures/binary-tree/bst.ts +2 -3
  71. package/src/types/data-structures/heap/heap.ts +3 -1
  72. package/src/types/data-structures/priority-queue/priority-queue.ts +3 -1
@@ -24,10 +24,14 @@ class SinglyLinkedList {
24
24
  /**
25
25
  * The constructor initializes the linked list with an empty head, tail, and length.
26
26
  */
27
- constructor() {
27
+ constructor(elements) {
28
28
  this._head = null;
29
29
  this._tail = null;
30
30
  this._length = 0;
31
+ if (elements) {
32
+ for (const el of elements)
33
+ this.push(el);
34
+ }
31
35
  }
32
36
  get head() {
33
37
  return this._head;
@@ -605,58 +609,42 @@ class SinglyLinkedList {
605
609
  return count;
606
610
  }
607
611
  /**
608
- * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
609
- * Space Complexity: O(1) - Constant space.
610
- */
611
- /**
612
- * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
613
- * Space Complexity: O(1) - Constant space.
614
- *
615
- * The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
616
- * @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
617
- * represents the value of the current node in the linked list, and the index argument represents the index of the
618
- * current node in the linked list.
612
+ * The function returns an iterator that iterates over the values of a linked list.
619
613
  */
620
- forEach(callback) {
614
+ *[Symbol.iterator]() {
621
615
  let current = this.head;
622
- let index = 0;
623
616
  while (current) {
624
- callback(current.value, index);
617
+ yield current.value;
625
618
  current = current.next;
626
- index++;
627
619
  }
628
620
  }
629
621
  /**
630
- * 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.
631
- * Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
622
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
623
+ * Space Complexity: O(1)
632
624
  */
633
625
  /**
634
- * 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.
635
- * Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
626
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
627
+ * Space Complexity: O(1)
636
628
  *
637
- * The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new
638
- * SinglyLinkedList with the transformed values.
639
- * @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
640
- * the original SinglyLinkedList) and returns a value of type U (the type of values that will be stored in the mapped
641
- * SinglyLinkedList).
642
- * @returns The `map` function is returning a new instance of `SinglyLinkedList<U>` that contains the mapped values.
629
+ * The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
630
+ * @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
631
+ * represents the value of the current node in the linked list, and the index argument represents the index of the
632
+ * current node in the linked list.
643
633
  */
644
- map(callback) {
645
- const mappedList = new SinglyLinkedList();
646
- let current = this.head;
647
- while (current) {
648
- mappedList.push(callback(current.value));
649
- current = current.next;
634
+ forEach(callback) {
635
+ let index = 0;
636
+ for (const el of this) {
637
+ callback(el, index, this);
638
+ index++;
650
639
  }
651
- return mappedList;
652
640
  }
653
641
  /**
654
- * 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.
655
- * Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
642
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
643
+ * Space Complexity: O(n)
656
644
  */
657
645
  /**
658
- * 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.
659
- * Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
646
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
647
+ * Space Complexity: O(n)
660
648
  *
661
649
  * The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the
662
650
  * elements that satisfy the given callback function.
@@ -666,50 +654,67 @@ class SinglyLinkedList {
666
654
  */
667
655
  filter(callback) {
668
656
  const filteredList = new SinglyLinkedList();
669
- let current = this.head;
670
- while (current) {
671
- if (callback(current.value)) {
672
- filteredList.push(current.value);
657
+ let index = 0;
658
+ for (const current of this) {
659
+ if (callback(current, index, this)) {
660
+ filteredList.push(current);
673
661
  }
674
- current = current.next;
662
+ index++;
675
663
  }
676
664
  return filteredList;
677
665
  }
678
666
  /**
679
- * 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.
680
- * Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
667
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
668
+ * Space Complexity: O(n)
681
669
  */
682
670
  /**
683
- * 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.
684
- * Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
671
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
672
+ * Space Complexity: O(n)
673
+ *
674
+ * The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new
675
+ * SinglyLinkedList with the transformed values.
676
+ * @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
677
+ * the original SinglyLinkedList) and returns a value of type T (the type of values that will be stored in the mapped
678
+ * SinglyLinkedList).
679
+ * @returns The `map` function is returning a new instance of `SinglyLinkedList<T>` that contains the mapped values.
680
+ */
681
+ map(callback) {
682
+ const mappedList = new SinglyLinkedList();
683
+ let index = 0;
684
+ for (const current of this) {
685
+ mappedList.push(callback(current, index, this));
686
+ index++;
687
+ }
688
+ return mappedList;
689
+ }
690
+ /**
691
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
692
+ * Space Complexity: O(n)
693
+ */
694
+ /**
695
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
696
+ * Space Complexity: O(n)
685
697
  *
686
698
  * The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
687
699
  * single value.
688
700
  * @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
689
701
  * used to perform a specific operation on each element of the linked list.
690
- * @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
702
+ * @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
691
703
  * point for the reduction operation.
692
704
  * @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
693
705
  * elements in the linked list.
694
706
  */
695
707
  reduce(callback, initialValue) {
696
708
  let accumulator = initialValue;
697
- let current = this.head;
698
- while (current) {
699
- accumulator = callback(accumulator, current.value);
700
- current = current.next;
709
+ let index = 0;
710
+ for (const current of this) {
711
+ accumulator = callback(accumulator, current, index, this);
712
+ index++;
701
713
  }
702
714
  return accumulator;
703
715
  }
704
- /**
705
- * The function returns an iterator that iterates over the values of a linked list.
706
- */
707
- *[Symbol.iterator]() {
708
- let current = this.head;
709
- while (current) {
710
- yield current.value;
711
- current = current.next;
712
- }
716
+ print() {
717
+ console.log([...this]);
713
718
  }
714
719
  }
715
720
  exports.SinglyLinkedList = SinglyLinkedList;
@@ -6,10 +6,7 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import { PriorityQueue } from './priority-queue';
9
- import type { Comparator } from '../../types';
9
+ import type { PriorityQueueOptions } from '../../types';
10
10
  export declare class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
11
- constructor(options?: {
12
- comparator: Comparator<E>;
13
- nodes?: E[];
14
- });
11
+ constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>);
15
12
  }
@@ -10,7 +10,7 @@ exports.MaxPriorityQueue = void 0;
10
10
  */
11
11
  const priority_queue_1 = require("./priority-queue");
12
12
  class MaxPriorityQueue extends priority_queue_1.PriorityQueue {
13
- constructor(options = {
13
+ constructor(elements, options = {
14
14
  comparator: (a, b) => {
15
15
  if (!(typeof a === 'number' && typeof b === 'number')) {
16
16
  throw new Error('The a, b params of compare function must be number');
@@ -20,7 +20,7 @@ class MaxPriorityQueue extends priority_queue_1.PriorityQueue {
20
20
  }
21
21
  }
22
22
  }) {
23
- super(options);
23
+ super(elements, options);
24
24
  }
25
25
  }
26
26
  exports.MaxPriorityQueue = MaxPriorityQueue;
@@ -6,10 +6,7 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import { PriorityQueue } from './priority-queue';
9
- import type { Comparator } from '../../types';
9
+ import type { PriorityQueueOptions } from '../../types';
10
10
  export declare class MinPriorityQueue<E = any> extends PriorityQueue<E> {
11
- constructor(options?: {
12
- comparator: Comparator<E>;
13
- nodes?: E[];
14
- });
11
+ constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>);
15
12
  }
@@ -10,7 +10,7 @@ exports.MinPriorityQueue = void 0;
10
10
  */
11
11
  const priority_queue_1 = require("./priority-queue");
12
12
  class MinPriorityQueue extends priority_queue_1.PriorityQueue {
13
- constructor(options = {
13
+ constructor(elements, options = {
14
14
  comparator: (a, b) => {
15
15
  if (!(typeof a === 'number' && typeof b === 'number')) {
16
16
  throw new Error('The a, b params of compare function must be number');
@@ -20,7 +20,7 @@ class MinPriorityQueue extends priority_queue_1.PriorityQueue {
20
20
  }
21
21
  }
22
22
  }) {
23
- super(options);
23
+ super(elements, options);
24
24
  }
25
25
  }
26
26
  exports.MinPriorityQueue = MinPriorityQueue;
@@ -6,10 +6,7 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import { Heap } from '../heap';
9
- import { Comparator } from '../../types';
9
+ import { PriorityQueueOptions } from '../../types';
10
10
  export declare class PriorityQueue<E = any> extends Heap<E> {
11
- constructor(options: {
12
- comparator: Comparator<E>;
13
- nodes?: E[];
14
- });
11
+ constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>);
15
12
  }
@@ -10,8 +10,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
10
10
  exports.PriorityQueue = void 0;
11
11
  const heap_1 = require("../heap");
12
12
  class PriorityQueue extends heap_1.Heap {
13
- constructor(options) {
14
- super(options);
13
+ constructor(elements, options) {
14
+ super(elements, options);
15
15
  }
16
16
  }
17
17
  exports.PriorityQueue = PriorityQueue;
@@ -318,12 +318,14 @@ export declare class Deque<E> {
318
318
  * Time Complexity: O(n)
319
319
  * Space Complexity: O(1)
320
320
  *
321
- * The `forEach` function iterates over each element in a deque and applies a callback function to
322
- * each element.
323
- * @param callback - The callback parameter is a function that will be called for each element in the
324
- * deque. It takes three parameters:
321
+ * The `find` function iterates over the elements in a deque and returns the first element for which
322
+ * the callback function returns true, or undefined if no such element is found.
323
+ * @param callback - A function that takes three parameters: element, index, and deque. It should
324
+ * return a boolean value indicating whether the element satisfies a certain condition.
325
+ * @returns The method `find` returns the first element in the deque that satisfies the condition
326
+ * specified by the callback function. If no element satisfies the condition, it returns `undefined`.
325
327
  */
326
- forEach(callback: (element: E, index: number, deque: Deque<E>) => void): void;
328
+ find(callback: (element: E, index: number, deque: Deque<E>) => boolean): E | undefined;
327
329
  /**
328
330
  * Time Complexity: O(n)
329
331
  * Space Complexity: O(1)
@@ -332,14 +334,14 @@ export declare class Deque<E> {
332
334
  * Time Complexity: O(n)
333
335
  * Space Complexity: O(1)
334
336
  *
335
- * The `find` function iterates over the elements in a deque and returns the first element for which
336
- * the callback function returns true, or undefined if no such element is found.
337
- * @param callback - A function that takes three parameters: element, index, and deque. It should
338
- * return a boolean value indicating whether the element satisfies a certain condition.
339
- * @returns The method `find` returns the first element in the deque that satisfies the condition
340
- * specified by the callback function. If no element satisfies the condition, it returns `undefined`.
337
+ * The function "indexOf" returns the index of the first occurrence of a given element in an array,
338
+ * or -1 if the element is not found.
339
+ * @param {E} element - The "element" parameter represents the element that you want to find the
340
+ * index of in the data structure.
341
+ * @returns The indexOf function returns the index of the first occurrence of the specified element
342
+ * in the data structure. If the element is not found, it returns -1.
341
343
  */
342
- find(callback: (element: E, index: number, deque: Deque<E>) => boolean): E | undefined;
344
+ indexOf(element: E): number;
343
345
  /**
344
346
  * Time Complexity: O(n)
345
347
  * Space Complexity: O(n)
@@ -354,18 +356,30 @@ export declare class Deque<E> {
354
356
  toArray(): E[];
355
357
  /**
356
358
  * Time Complexity: O(n)
357
- * Space Complexity: O(n)
359
+ * Space Complexity: O(1)
358
360
  */
359
361
  /**
360
362
  * Time Complexity: O(n)
361
- * Space Complexity: O(n)
363
+ * Space Complexity: O(1)
362
364
  *
363
- * The `map` function takes a callback function and applies it to each element in the deque,
364
- * returning a new deque with the results.
365
- * @param callback - The `callback` parameter is a function that takes three arguments:
366
- * @returns The `map` method is returning a new `Deque` object with the transformed elements.
365
+ * The above function is an implementation of the iterator protocol in TypeScript, allowing the
366
+ * object to be iterated over using a for...of loop.
367
+ */
368
+ [Symbol.iterator](): Generator<E, void, unknown>;
369
+ /**
370
+ * Time Complexity: O(n)
371
+ * Space Complexity: O(1)
372
+ */
373
+ /**
374
+ * Time Complexity: O(n)
375
+ * Space Complexity: O(1)
376
+ *
377
+ * The `forEach` function iterates over each element in a deque and applies a callback function to
378
+ * each element.
379
+ * @param callback - The callback parameter is a function that will be called for each element in the
380
+ * deque. It takes three parameters:
367
381
  */
368
- map<T>(callback: (element: E, index: number, deque: Deque<E>) => T): Deque<T>;
382
+ forEach(callback: (element: E, index: number, deque: this) => void): void;
369
383
  /**
370
384
  * Time Complexity: O(n)
371
385
  * Space Complexity: O(n)
@@ -381,7 +395,21 @@ export declare class Deque<E> {
381
395
  * @returns The `filter` method is returning a new `Deque` object that contains only the elements
382
396
  * that satisfy the given `predicate` function.
383
397
  */
384
- filter(predicate: (element: E, index: number, deque: Deque<E>) => boolean): Deque<E>;
398
+ filter(predicate: (element: E, index: number, deque: this) => boolean): Deque<E>;
399
+ /**
400
+ * Time Complexity: O(n)
401
+ * Space Complexity: O(n)
402
+ */
403
+ /**
404
+ * Time Complexity: O(n)
405
+ * Space Complexity: O(n)
406
+ *
407
+ * The `map` function takes a callback function and applies it to each element in the deque,
408
+ * returning a new deque with the results.
409
+ * @param callback - The `callback` parameter is a function that takes three arguments:
410
+ * @returns The `map` method is returning a new `Deque` object with the transformed elements.
411
+ */
412
+ map<T>(callback: (element: E, index: number, deque: this) => T): Deque<T>;
385
413
  /**
386
414
  * Time Complexity: O(n)
387
415
  * Space Complexity: O(1)
@@ -399,35 +427,8 @@ export declare class Deque<E> {
399
427
  * @returns the final value of the accumulator after iterating over all elements in the deque and
400
428
  * applying the callback function to each element.
401
429
  */
402
- reduce<T>(callback: (accumulator: T, element: E, index: number, deque: Deque<E>) => T, initialValue: T): T;
403
- /**
404
- * Time Complexity: O(n)
405
- * Space Complexity: O(1)
406
- */
407
- /**
408
- * Time Complexity: O(n)
409
- * Space Complexity: O(1)
410
- *
411
- * The function "indexOf" returns the index of the first occurrence of a given element in an array,
412
- * or -1 if the element is not found.
413
- * @param {E} element - The "element" parameter represents the element that you want to find the
414
- * index of in the data structure.
415
- * @returns The indexOf function returns the index of the first occurrence of the specified element
416
- * in the data structure. If the element is not found, it returns -1.
417
- */
418
- indexOf(element: E): number;
419
- /**
420
- * Time Complexity: O(n)
421
- * Space Complexity: O(1)
422
- */
423
- /**
424
- * Time Complexity: O(n)
425
- * Space Complexity: O(1)
426
- *
427
- * The above function is an implementation of the iterator protocol in TypeScript, allowing the
428
- * object to be iterated over using a for...of loop.
429
- */
430
- [Symbol.iterator](): Generator<E, void, unknown>;
430
+ reduce<T>(callback: (accumulator: T, element: E, index: number, deque: this) => T, initialValue: T): T;
431
+ print(): void;
431
432
  /**
432
433
  * Time Complexity: O(n)
433
434
  * Space Complexity: O(n)
@@ -594,15 +594,21 @@ class Deque {
594
594
  * Time Complexity: O(n)
595
595
  * Space Complexity: O(1)
596
596
  *
597
- * The `forEach` function iterates over each element in a deque and applies a callback function to
598
- * each element.
599
- * @param callback - The callback parameter is a function that will be called for each element in the
600
- * deque. It takes three parameters:
597
+ * The `find` function iterates over the elements in a deque and returns the first element for which
598
+ * the callback function returns true, or undefined if no such element is found.
599
+ * @param callback - A function that takes three parameters: element, index, and deque. It should
600
+ * return a boolean value indicating whether the element satisfies a certain condition.
601
+ * @returns The method `find` returns the first element in the deque that satisfies the condition
602
+ * specified by the callback function. If no element satisfies the condition, it returns `undefined`.
601
603
  */
602
- forEach(callback) {
604
+ find(callback) {
603
605
  for (let i = 0; i < this.size; ++i) {
604
- callback(this.getAt(i), i, this);
606
+ const element = this.getAt(i);
607
+ if (callback(element, i, this)) {
608
+ return element;
609
+ }
605
610
  }
611
+ return undefined;
606
612
  }
607
613
  /**
608
614
  * Time Complexity: O(n)
@@ -612,21 +618,20 @@ class Deque {
612
618
  * Time Complexity: O(n)
613
619
  * Space Complexity: O(1)
614
620
  *
615
- * The `find` function iterates over the elements in a deque and returns the first element for which
616
- * the callback function returns true, or undefined if no such element is found.
617
- * @param callback - A function that takes three parameters: element, index, and deque. It should
618
- * return a boolean value indicating whether the element satisfies a certain condition.
619
- * @returns The method `find` returns the first element in the deque that satisfies the condition
620
- * specified by the callback function. If no element satisfies the condition, it returns `undefined`.
621
+ * The function "indexOf" returns the index of the first occurrence of a given element in an array,
622
+ * or -1 if the element is not found.
623
+ * @param {E} element - The "element" parameter represents the element that you want to find the
624
+ * index of in the data structure.
625
+ * @returns The indexOf function returns the index of the first occurrence of the specified element
626
+ * in the data structure. If the element is not found, it returns -1.
621
627
  */
622
- find(callback) {
628
+ indexOf(element) {
623
629
  for (let i = 0; i < this.size; ++i) {
624
- const element = this.getAt(i);
625
- if (callback(element, i, this)) {
626
- return element;
630
+ if (this.getAt(i) === element) {
631
+ return i;
627
632
  }
628
633
  }
629
- return undefined;
634
+ return -1;
630
635
  }
631
636
  /**
632
637
  * Time Complexity: O(n)
@@ -648,23 +653,39 @@ class Deque {
648
653
  }
649
654
  /**
650
655
  * Time Complexity: O(n)
651
- * Space Complexity: O(n)
656
+ * Space Complexity: O(1)
652
657
  */
653
658
  /**
654
659
  * Time Complexity: O(n)
655
- * Space Complexity: O(n)
660
+ * Space Complexity: O(1)
656
661
  *
657
- * The `map` function takes a callback function and applies it to each element in the deque,
658
- * returning a new deque with the results.
659
- * @param callback - The `callback` parameter is a function that takes three arguments:
660
- * @returns The `map` method is returning a new `Deque` object with the transformed elements.
662
+ * The above function is an implementation of the iterator protocol in TypeScript, allowing the
663
+ * object to be iterated over using a for...of loop.
661
664
  */
662
- map(callback) {
663
- const newDeque = new Deque([], this._bucketSize);
665
+ *[Symbol.iterator]() {
664
666
  for (let i = 0; i < this.size; ++i) {
665
- newDeque.push(callback(this.getAt(i), i, this));
667
+ yield this.getAt(i);
668
+ }
669
+ }
670
+ /**
671
+ * Time Complexity: O(n)
672
+ * Space Complexity: O(1)
673
+ */
674
+ /**
675
+ * Time Complexity: O(n)
676
+ * Space Complexity: O(1)
677
+ *
678
+ * The `forEach` function iterates over each element in a deque and applies a callback function to
679
+ * each element.
680
+ * @param callback - The callback parameter is a function that will be called for each element in the
681
+ * deque. It takes three parameters:
682
+ */
683
+ forEach(callback) {
684
+ let index = 0;
685
+ for (const el of this) {
686
+ callback(el, index, this);
687
+ index++;
666
688
  }
667
- return newDeque;
668
689
  }
669
690
  /**
670
691
  * Time Complexity: O(n)
@@ -683,11 +704,34 @@ class Deque {
683
704
  */
684
705
  filter(predicate) {
685
706
  const newDeque = new Deque([], this._bucketSize);
686
- for (let i = 0; i < this.size; ++i) {
687
- const element = this.getAt(i);
688
- if (predicate(element, i, this)) {
689
- newDeque.push(element);
707
+ let index = 0;
708
+ for (const el of this) {
709
+ if (predicate(el, index, this)) {
710
+ newDeque.push(el);
690
711
  }
712
+ index++;
713
+ }
714
+ return newDeque;
715
+ }
716
+ /**
717
+ * Time Complexity: O(n)
718
+ * Space Complexity: O(n)
719
+ */
720
+ /**
721
+ * Time Complexity: O(n)
722
+ * Space Complexity: O(n)
723
+ *
724
+ * The `map` function takes a callback function and applies it to each element in the deque,
725
+ * returning a new deque with the results.
726
+ * @param callback - The `callback` parameter is a function that takes three arguments:
727
+ * @returns The `map` method is returning a new `Deque` object with the transformed elements.
728
+ */
729
+ map(callback) {
730
+ const newDeque = new Deque([], this._bucketSize);
731
+ let index = 0;
732
+ for (const el of this) {
733
+ newDeque.push(callback(el, index, this));
734
+ index++;
691
735
  }
692
736
  return newDeque;
693
737
  }
@@ -710,49 +754,15 @@ class Deque {
710
754
  */
711
755
  reduce(callback, initialValue) {
712
756
  let accumulator = initialValue;
713
- for (let i = 0; i < this.size; ++i) {
714
- accumulator = callback(accumulator, this.getAt(i), i, this);
757
+ let index = 0;
758
+ for (const el of this) {
759
+ accumulator = callback(accumulator, el, index, this);
760
+ index++;
715
761
  }
716
762
  return accumulator;
717
763
  }
718
- /**
719
- * Time Complexity: O(n)
720
- * Space Complexity: O(1)
721
- */
722
- /**
723
- * Time Complexity: O(n)
724
- * Space Complexity: O(1)
725
- *
726
- * The function "indexOf" returns the index of the first occurrence of a given element in an array,
727
- * or -1 if the element is not found.
728
- * @param {E} element - The "element" parameter represents the element that you want to find the
729
- * index of in the data structure.
730
- * @returns The indexOf function returns the index of the first occurrence of the specified element
731
- * in the data structure. If the element is not found, it returns -1.
732
- */
733
- indexOf(element) {
734
- for (let i = 0; i < this.size; ++i) {
735
- if (this.getAt(i) === element) {
736
- return i;
737
- }
738
- }
739
- return -1;
740
- }
741
- /**
742
- * Time Complexity: O(n)
743
- * Space Complexity: O(1)
744
- */
745
- /**
746
- * Time Complexity: O(n)
747
- * Space Complexity: O(1)
748
- *
749
- * The above function is an implementation of the iterator protocol in TypeScript, allowing the
750
- * object to be iterated over using a for...of loop.
751
- */
752
- *[Symbol.iterator]() {
753
- for (let i = 0; i < this.size; ++i) {
754
- yield this.getAt(i);
755
- }
764
+ print() {
765
+ console.log([...this]);
756
766
  }
757
767
  /**
758
768
  * Time Complexity: O(n)