linked-list-typed 1.48.1 → 1.48.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/dist/data-structures/base/index.d.ts +1 -0
  2. package/dist/data-structures/base/index.js +17 -0
  3. package/dist/data-structures/base/iterable-base.d.ts +232 -0
  4. package/dist/data-structures/base/iterable-base.js +312 -0
  5. package/dist/data-structures/binary-tree/avl-tree.d.ts +16 -16
  6. package/dist/data-structures/binary-tree/avl-tree.js +7 -7
  7. package/dist/data-structures/binary-tree/binary-tree.d.ts +121 -152
  8. package/dist/data-structures/binary-tree/binary-tree.js +140 -182
  9. package/dist/data-structures/binary-tree/bst.d.ts +28 -47
  10. package/dist/data-structures/binary-tree/bst.js +54 -57
  11. package/dist/data-structures/binary-tree/rb-tree.d.ts +15 -15
  12. package/dist/data-structures/binary-tree/rb-tree.js +7 -7
  13. package/dist/data-structures/binary-tree/tree-multimap.d.ts +22 -22
  14. package/dist/data-structures/binary-tree/tree-multimap.js +11 -11
  15. package/dist/data-structures/graph/abstract-graph.d.ts +44 -6
  16. package/dist/data-structures/graph/abstract-graph.js +50 -27
  17. package/dist/data-structures/hash/hash-map.d.ts +59 -100
  18. package/dist/data-structures/hash/hash-map.js +69 -173
  19. package/dist/data-structures/heap/heap.d.ts +50 -7
  20. package/dist/data-structures/heap/heap.js +60 -30
  21. package/dist/data-structures/index.d.ts +1 -0
  22. package/dist/data-structures/index.js +1 -0
  23. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
  24. package/dist/data-structures/linked-list/doubly-linked-list.js +46 -73
  25. package/dist/data-structures/linked-list/singly-linked-list.d.ts +32 -51
  26. package/dist/data-structures/linked-list/singly-linked-list.js +40 -73
  27. package/dist/data-structures/queue/deque.d.ts +29 -51
  28. package/dist/data-structures/queue/deque.js +36 -71
  29. package/dist/data-structures/queue/queue.d.ts +49 -48
  30. package/dist/data-structures/queue/queue.js +69 -82
  31. package/dist/data-structures/stack/stack.d.ts +43 -10
  32. package/dist/data-structures/stack/stack.js +50 -31
  33. package/dist/data-structures/trie/trie.d.ts +41 -6
  34. package/dist/data-structures/trie/trie.js +53 -32
  35. package/dist/interfaces/binary-tree.d.ts +6 -6
  36. package/dist/types/common.d.ts +11 -8
  37. package/dist/types/common.js +6 -1
  38. package/dist/types/data-structures/base/base.d.ts +5 -0
  39. package/dist/types/data-structures/base/base.js +2 -0
  40. package/dist/types/data-structures/base/index.d.ts +1 -0
  41. package/dist/types/data-structures/base/index.js +17 -0
  42. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -3
  43. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +4 -4
  44. package/dist/types/data-structures/binary-tree/bst.d.ts +6 -6
  45. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -3
  46. package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +3 -3
  47. package/dist/types/data-structures/index.d.ts +1 -0
  48. package/dist/types/data-structures/index.js +1 -0
  49. package/package.json +2 -2
  50. package/src/data-structures/base/index.ts +1 -0
  51. package/src/data-structures/base/iterable-base.ts +329 -0
  52. package/src/data-structures/binary-tree/avl-tree.ts +20 -21
  53. package/src/data-structures/binary-tree/binary-tree.ts +222 -267
  54. package/src/data-structures/binary-tree/bst.ts +86 -82
  55. package/src/data-structures/binary-tree/rb-tree.ts +25 -26
  56. package/src/data-structures/binary-tree/tree-multimap.ts +30 -35
  57. package/src/data-structures/graph/abstract-graph.ts +55 -28
  58. package/src/data-structures/hash/hash-map.ts +76 -185
  59. package/src/data-structures/heap/heap.ts +63 -36
  60. package/src/data-structures/index.ts +1 -0
  61. package/src/data-structures/linked-list/doubly-linked-list.ts +50 -79
  62. package/src/data-structures/linked-list/singly-linked-list.ts +45 -80
  63. package/src/data-structures/queue/deque.ts +40 -82
  64. package/src/data-structures/queue/queue.ts +72 -87
  65. package/src/data-structures/stack/stack.ts +53 -34
  66. package/src/data-structures/trie/trie.ts +58 -35
  67. package/src/interfaces/binary-tree.ts +5 -6
  68. package/src/types/common.ts +11 -8
  69. package/src/types/data-structures/base/base.ts +6 -0
  70. package/src/types/data-structures/base/index.ts +1 -0
  71. package/src/types/data-structures/binary-tree/avl-tree.ts +3 -3
  72. package/src/types/data-structures/binary-tree/binary-tree.ts +6 -5
  73. package/src/types/data-structures/binary-tree/bst.ts +6 -6
  74. package/src/types/data-structures/binary-tree/rb-tree.ts +3 -3
  75. package/src/types/data-structures/binary-tree/tree-multimap.ts +3 -3
  76. package/src/types/data-structures/index.ts +1 -0
@@ -9,7 +9,8 @@
9
9
  Object.defineProperty(exports, "__esModule", { value: true });
10
10
  exports.LinkedHashMap = exports.HashMap = void 0;
11
11
  const utils_1 = require("../../utils");
12
- class HashMap {
12
+ const base_1 = require("../base");
13
+ class HashMap extends base_1.IterablePairBase {
13
14
  /**
14
15
  * The constructor function initializes a new instance of a class with optional elements and options.
15
16
  * @param elements - The `elements` parameter is an iterable containing key-value pairs `[K, V]`. It
@@ -19,6 +20,7 @@ class HashMap {
19
20
  * configuration options for the constructor. In this case, it has one property:
20
21
  */
21
22
  constructor(elements = [], options) {
23
+ super();
22
24
  this._store = {};
23
25
  this._objMap = new Map();
24
26
  this._size = 0;
@@ -135,95 +137,13 @@ class HashMap {
135
137
  }
136
138
  }
137
139
  /**
138
- * The function returns an iterator that yields key-value pairs from both an object store and an
139
- * object map.
140
- */
141
- *[Symbol.iterator]() {
142
- for (const node of Object.values(this._store)) {
143
- yield [node.key, node.value];
144
- }
145
- for (const node of this._objMap) {
146
- yield node;
147
- }
148
- }
149
- /**
150
- * The function returns an iterator that yields key-value pairs from the object.
151
- */
152
- *entries() {
153
- for (const item of this) {
154
- yield item;
155
- }
156
- }
157
- /**
158
- * The function `keys()` returns an iterator that yields all the keys of the object.
159
- */
160
- *keys() {
161
- for (const [key] of this) {
162
- yield key;
163
- }
164
- }
165
- *values() {
166
- for (const [, value] of this) {
167
- yield value;
168
- }
169
- }
170
- /**
171
- * The `every` function checks if every element in a HashMap satisfies a given predicate function.
172
- * @param predicate - The predicate parameter is a function that takes four arguments: value, key,
173
- * index, and map. It is used to test each element in the map against a condition. If the predicate
174
- * function returns false for any element, the every() method will return false. If the predicate
175
- * function returns true for all
176
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
177
- * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
178
- * passed as the `this` value to the `predicate` function. If `thisArg` is
179
- * @returns The method is returning a boolean value. It returns true if the predicate function
180
- * returns true for every element in the map, and false otherwise.
181
- */
182
- every(predicate, thisArg) {
183
- let index = 0;
184
- for (const [key, value] of this) {
185
- if (!predicate.call(thisArg, value, key, index++, this)) {
186
- return false;
187
- }
188
- }
189
- return true;
190
- }
191
- /**
192
- * The "some" function checks if at least one element in a HashMap satisfies a given predicate.
193
- * @param predicate - The `predicate` parameter is a function that takes four arguments: `value`,
194
- * `key`, `index`, and `map`. It is used to determine whether a specific condition is met for a given
195
- * key-value pair in the `HashMap`.
196
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
197
- * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
198
- * passed as the `this` value to the `predicate` function. If `thisArg` is
199
- * @returns a boolean value. It returns true if the predicate function returns true for any element
200
- * in the map, and false otherwise.
140
+ * Time Complexity: O(n)
141
+ * Space Complexity: O(n)
201
142
  */
202
- some(predicate, thisArg) {
203
- let index = 0;
204
- for (const [key, value] of this) {
205
- if (predicate.call(thisArg, value, key, index++, this)) {
206
- return true;
207
- }
208
- }
209
- return false;
210
- }
211
- /**
212
- * The `forEach` function iterates over the elements of a HashMap and applies a callback function to
213
- * each element.
214
- * @param callbackfn - A function that will be called for each key-value pair in the HashMap. It
215
- * takes four parameters:
216
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
217
- * to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
218
- * be passed as the `this` value inside the `callbackfn` function. If `thisArg
219
- */
220
- forEach(callbackfn, thisArg) {
221
- let index = 0;
222
- for (const [key, value] of this) {
223
- callbackfn.call(thisArg, value, key, index++, this);
224
- }
225
- }
226
143
  /**
144
+ * Time Complexity: O(n)
145
+ * Space Complexity: O(n)
146
+ *
227
147
  * The `map` function in TypeScript creates a new HashMap by applying a callback function to each
228
148
  * key-value pair in the original HashMap.
229
149
  * @param callbackfn - The callback function that will be called for each key-value pair in the
@@ -243,6 +163,13 @@ class HashMap {
243
163
  return resultMap;
244
164
  }
245
165
  /**
166
+ * Time Complexity: O(n)
167
+ * Space Complexity: O(n)
168
+ */
169
+ /**
170
+ * Time Complexity: O(n)
171
+ * Space Complexity: O(n)
172
+ *
246
173
  * The `filter` function creates a new HashMap containing key-value pairs from the original HashMap
247
174
  * that satisfy a given predicate function.
248
175
  * @param predicate - The predicate parameter is a function that takes four arguments: value, key,
@@ -265,27 +192,20 @@ class HashMap {
265
192
  }
266
193
  return filteredMap;
267
194
  }
195
+ print() {
196
+ console.log([...this.entries()]);
197
+ }
268
198
  /**
269
- * The `reduce` function iterates over the elements of a HashMap and applies a callback function to
270
- * each element, accumulating a single value.
271
- * @param callbackfn - The callback function that will be called for each element in the HashMap. It
272
- * takes five parameters:
273
- * @param {U} initialValue - The initialValue parameter is the initial value of the accumulator. It
274
- * is the value that will be used as the first argument of the callback function when reducing the
275
- * elements of the map.
276
- * @returns The `reduce` method is returning the final value of the accumulator after iterating over
277
- * all the elements in the `HashMap`.
199
+ * The function returns an iterator that yields key-value pairs from both an object store and an
200
+ * object map.
278
201
  */
279
- reduce(callbackfn, initialValue) {
280
- let accumulator = initialValue;
281
- let index = 0;
282
- for (const [key, value] of this) {
283
- accumulator = callbackfn(accumulator, value, key, index++, this);
202
+ *_getIterator() {
203
+ for (const node of Object.values(this._store)) {
204
+ yield [node.key, node.value];
205
+ }
206
+ for (const node of this._objMap) {
207
+ yield node;
284
208
  }
285
- return accumulator;
286
- }
287
- print() {
288
- console.log([...this.entries()]);
289
209
  }
290
210
  _isObjKey(key) {
291
211
  const keyType = typeof key;
@@ -310,11 +230,12 @@ class HashMap {
310
230
  }
311
231
  }
312
232
  exports.HashMap = HashMap;
313
- class LinkedHashMap {
233
+ class LinkedHashMap extends base_1.IterablePairBase {
314
234
  constructor(elements, options = {
315
235
  hashFn: (key) => String(key),
316
236
  objHashFn: (key) => key
317
237
  }) {
238
+ super();
318
239
  this._noObjMap = {};
319
240
  this._objMap = new WeakMap();
320
241
  this._size = 0;
@@ -450,18 +371,6 @@ class LinkedHashMap {
450
371
  this.set(key, value);
451
372
  }
452
373
  }
453
- keys() {
454
- const keys = [];
455
- for (const [key] of this)
456
- keys.push(key);
457
- return keys;
458
- }
459
- values() {
460
- const values = [];
461
- for (const [, value] of this)
462
- values.push(value);
463
- return values;
464
- }
465
374
  /**
466
375
  * Time Complexity: O(1)
467
376
  * Space Complexity: O(1)
@@ -591,35 +500,29 @@ class LinkedHashMap {
591
500
  return cloned;
592
501
  }
593
502
  /**
594
- * Time Complexity: O(n), where n is the number of elements in the LinkedHashMap.
595
- * Space Complexity: O(1)
596
- *
597
- * The `forEach` function iterates over each element in a LinkedHashMap and executes a callback function on
598
- * each element.
599
- * @param callback - The callback parameter is a function that will be called for each element in the
600
- * LinkedHashMap. It takes three arguments:
503
+ * Time Complexity: O(n)
504
+ * Space Complexity: O(n)
601
505
  */
602
- forEach(callback) {
603
- let index = 0;
604
- let node = this._head;
605
- while (node !== this._sentinel) {
606
- callback([node.key, node.value], index++, this);
607
- node = node.next;
608
- }
609
- }
610
506
  /**
611
- * The `filter` function takes a predicate function and returns a new LinkedHashMap containing only the
612
- * key-value pairs that satisfy the predicate.
613
- * @param predicate - The `predicate` parameter is a function that takes two arguments: `element` and
614
- * `map`.
615
- * @returns a new LinkedHashMap object that contains the key-value pairs from the original LinkedHashMap that
616
- * satisfy the given predicate function.
507
+ * Time Complexity: O(n)
508
+ * Space Complexity: O(n)
509
+ *
510
+ * The `filter` function creates a new `LinkedHashMap` containing key-value pairs from the original
511
+ * map that satisfy a given predicate function.
512
+ * @param predicate - The `predicate` parameter is a callback function that takes four arguments:
513
+ * `value`, `key`, `index`, and `this`. It should return a boolean value indicating whether the
514
+ * current element should be included in the filtered map or not.
515
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
516
+ * specify the value of `this` within the `predicate` function. It is used when you want to bind a
517
+ * specific object as the context for the `predicate` function. If `thisArg` is not provided, `this
518
+ * @returns a new `LinkedHashMap` object that contains the key-value pairs from the original
519
+ * `LinkedHashMap` object that satisfy the given predicate function.
617
520
  */
618
- filter(predicate) {
521
+ filter(predicate, thisArg) {
619
522
  const filteredMap = new LinkedHashMap();
620
523
  let index = 0;
621
524
  for (const [key, value] of this) {
622
- if (predicate([key, value], index, this)) {
525
+ if (predicate.call(thisArg, value, key, index, this)) {
623
526
  filteredMap.set(key, value);
624
527
  }
625
528
  index++;
@@ -627,42 +530,38 @@ class LinkedHashMap {
627
530
  return filteredMap;
628
531
  }
629
532
  /**
630
- * The `map` function takes a callback function and returns a new LinkedHashMap with the values transformed
631
- * by the callback.
632
- * @param callback - The `callback` parameter is a function that takes two arguments: `element` and
633
- * `map`.
634
- * @returns a new LinkedHashMap object with the values mapped according to the provided callback function.
533
+ * Time Complexity: O(n)
534
+ * Space Complexity: O(n)
635
535
  */
636
- map(callback) {
536
+ /**
537
+ * Time Complexity: O(n)
538
+ * Space Complexity: O(n)
539
+ *
540
+ * The `map` function in TypeScript creates a new `LinkedHashMap` by applying a callback function to
541
+ * each key-value pair in the original map.
542
+ * @param callback - The callback parameter is a function that will be called for each key-value pair
543
+ * in the map. It takes four arguments: the value of the current key-value pair, the key of the
544
+ * current key-value pair, the index of the current key-value pair, and the map itself. The callback
545
+ * function should
546
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
547
+ * specify the value of `this` within the callback function. If provided, the callback function will
548
+ * be called with `thisArg` as its `this` value. If not provided, `this` will refer to the current
549
+ * map
550
+ * @returns a new `LinkedHashMap` object with the values mapped according to the provided callback
551
+ * function.
552
+ */
553
+ map(callback, thisArg) {
637
554
  const mappedMap = new LinkedHashMap();
638
555
  let index = 0;
639
556
  for (const [key, value] of this) {
640
- const newValue = callback([key, value], index, this);
557
+ const newValue = callback.call(thisArg, value, key, index, this);
641
558
  mappedMap.set(key, newValue);
642
559
  index++;
643
560
  }
644
561
  return mappedMap;
645
562
  }
646
- /**
647
- * The `reduce` function iterates over the elements of a LinkedHashMap and applies a callback function to
648
- * each element, accumulating a single value.
649
- * @param callback - The callback parameter is a function that takes three arguments: accumulator,
650
- * element, and map. It is called for each element in the LinkedHashMap and is used to accumulate a single
651
- * result.
652
- * @param {A} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
653
- * is the value that will be passed as the first argument to the `callback` function when reducing
654
- * the elements of the map.
655
- * @returns The `reduce` function is returning the final value of the accumulator after iterating
656
- * over all the elements in the LinkedHashMap and applying the callback function to each element.
657
- */
658
- reduce(callback, initialValue) {
659
- let accumulator = initialValue;
660
- let index = 0;
661
- for (const entry of this) {
662
- accumulator = callback(accumulator, entry, index, this);
663
- index++;
664
- }
665
- return accumulator;
563
+ print() {
564
+ console.log([...this]);
666
565
  }
667
566
  /**
668
567
  * Time Complexity: O(n), where n is the number of elements in the LinkedHashMap.
@@ -670,16 +569,13 @@ class LinkedHashMap {
670
569
  *
671
570
  * The above function is an iterator that yields key-value pairs from a linked list.
672
571
  */
673
- *[Symbol.iterator]() {
572
+ *_getIterator() {
674
573
  let node = this._head;
675
574
  while (node !== this._sentinel) {
676
575
  yield [node.key, node.value];
677
576
  node = node.next;
678
577
  }
679
578
  }
680
- print() {
681
- console.log([...this]);
682
- }
683
579
  /**
684
580
  * Time Complexity: O(1)
685
581
  * Space Complexity: O(1)
@@ -4,9 +4,10 @@
4
4
  * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
5
5
  * @license MIT License
6
6
  */
7
- import type { Comparator, DFSOrderPattern } from '../../types';
7
+ import type { Comparator, DFSOrderPattern, ElementCallback } from '../../types';
8
8
  import { HeapOptions } from "../../types";
9
- export declare class Heap<E = any> {
9
+ import { IterableElementBase } from "../base";
10
+ export declare class Heap<E = any> extends IterableElementBase<E> {
10
11
  options: HeapOptions<E>;
11
12
  constructor(elements?: Iterable<E>, options?: HeapOptions<E>);
12
13
  protected _elements: E[];
@@ -192,16 +193,58 @@ export declare class Heap<E = any> {
192
193
  * Fix the entire heap to maintain heap properties.
193
194
  */
194
195
  fix(): void;
195
- [Symbol.iterator](): Generator<E, void, unknown>;
196
- forEach(callback: (element: E, index: number, heap: this) => void): void;
197
- filter(predicate: (element: E, index: number, heap: Heap<E>) => boolean): Heap<E>;
198
- map<T>(callback: (element: E, index: number, heap: Heap<E>) => T, comparator: Comparator<T>): Heap<T>;
199
- reduce<T>(callback: (accumulator: T, currentValue: E, currentIndex: number, heap: Heap<E>) => T, initialValue: T): T;
196
+ /**
197
+ * Time Complexity: O(n)
198
+ * Space Complexity: O(n)
199
+ */
200
+ /**
201
+ * Time Complexity: O(n)
202
+ * Space Complexity: O(n)
203
+ *
204
+ * The `filter` function creates a new Heap object containing elements that pass a given callback
205
+ * function.
206
+ * @param callback - The `callback` parameter is a function that will be called for each element in
207
+ * the heap. It takes three arguments: the current element, the index of the current element, and the
208
+ * heap itself. The callback function should return a boolean value indicating whether the current
209
+ * element should be included in the filtered list
210
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
211
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
212
+ * passed as the `this` value to the `callback` function. If `thisArg` is
213
+ * @returns The `filter` method is returning a new `Heap` object that contains the elements that pass
214
+ * the filter condition specified by the `callback` function.
215
+ */
216
+ filter(callback: ElementCallback<E, boolean>, thisArg?: any): Heap<E>;
217
+ /**
218
+ * Time Complexity: O(n)
219
+ * Space Complexity: O(n)
220
+ */
221
+ /**
222
+ * Time Complexity: O(n)
223
+ * Space Complexity: O(n)
224
+ *
225
+ * The `map` function creates a new heap by applying a callback function to each element of the
226
+ * original heap.
227
+ * @param callback - The callback parameter is a function that will be called for each element in the
228
+ * original heap. It takes three arguments: the current element, the index of the current element,
229
+ * and the original heap itself. The callback function should return a value of type T, which will be
230
+ * added to the mapped heap.
231
+ * @param comparator - The `comparator` parameter is a function that is used to compare elements in
232
+ * the heap. It takes two arguments, `a` and `b`, and returns a negative number if `a` is less than
233
+ * `b`, a positive number if `a` is greater than `b`, or
234
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
235
+ * specify the value of `this` within the callback function. It is used when you want to bind a
236
+ * specific object as the context for the callback function. If `thisArg` is not provided,
237
+ * `undefined` is used as
238
+ * @returns a new instance of the Heap class, which is created using the mapped elements from the
239
+ * original Heap.
240
+ */
241
+ map<T>(callback: ElementCallback<E, T>, comparator: Comparator<T>, thisArg?: any): Heap<T>;
200
242
  /**
201
243
  * Time Complexity: O(log n)
202
244
  * Space Complexity: O(1)
203
245
  */
204
246
  print(): void;
247
+ protected _getIterator(): Generator<E, void, unknown>;
205
248
  /**
206
249
  * Time Complexity: O(n)
207
250
  * Space Complexity: O(1)
@@ -7,8 +7,10 @@
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
9
  exports.FibonacciHeap = exports.FibonacciHeapNode = exports.Heap = void 0;
10
- class Heap {
10
+ const base_1 = require("../base");
11
+ class Heap extends base_1.IterableElementBase {
11
12
  constructor(elements, options) {
13
+ super();
12
14
  this._elements = [];
13
15
  const defaultComparator = (a, b) => {
14
16
  if (!(typeof a === 'number' && typeof b === 'number')) {
@@ -310,47 +312,70 @@ class Heap {
310
312
  for (let i = Math.floor(this.size / 2); i >= 0; i--)
311
313
  this._sinkDown(i, this.elements.length >> 1);
312
314
  }
313
- *[Symbol.iterator]() {
314
- for (const element of this.elements) {
315
- yield element;
316
- }
317
- }
318
- forEach(callback) {
319
- let index = 0;
320
- for (const el of this) {
321
- callback(el, index, this);
322
- index++;
323
- }
324
- }
325
- filter(predicate) {
326
- const filteredHeap = new Heap([], this.options);
315
+ /**
316
+ * Time Complexity: O(n)
317
+ * Space Complexity: O(n)
318
+ */
319
+ /**
320
+ * Time Complexity: O(n)
321
+ * Space Complexity: O(n)
322
+ *
323
+ * The `filter` function creates a new Heap object containing elements that pass a given callback
324
+ * function.
325
+ * @param callback - The `callback` parameter is a function that will be called for each element in
326
+ * the heap. It takes three arguments: the current element, the index of the current element, and the
327
+ * heap itself. The callback function should return a boolean value indicating whether the current
328
+ * element should be included in the filtered list
329
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
330
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
331
+ * passed as the `this` value to the `callback` function. If `thisArg` is
332
+ * @returns The `filter` method is returning a new `Heap` object that contains the elements that pass
333
+ * the filter condition specified by the `callback` function.
334
+ */
335
+ filter(callback, thisArg) {
336
+ const filteredList = new Heap();
327
337
  let index = 0;
328
- for (const el of this) {
329
- if (predicate(el, index, this)) {
330
- filteredHeap.push(el);
338
+ for (const current of this) {
339
+ if (callback.call(thisArg, current, index, this)) {
340
+ filteredList.push(current);
331
341
  }
332
342
  index++;
333
343
  }
334
- return filteredHeap;
344
+ return filteredList;
335
345
  }
336
- map(callback, comparator) {
346
+ /**
347
+ * Time Complexity: O(n)
348
+ * Space Complexity: O(n)
349
+ */
350
+ /**
351
+ * Time Complexity: O(n)
352
+ * Space Complexity: O(n)
353
+ *
354
+ * The `map` function creates a new heap by applying a callback function to each element of the
355
+ * original heap.
356
+ * @param callback - The callback parameter is a function that will be called for each element in the
357
+ * original heap. It takes three arguments: the current element, the index of the current element,
358
+ * and the original heap itself. The callback function should return a value of type T, which will be
359
+ * added to the mapped heap.
360
+ * @param comparator - The `comparator` parameter is a function that is used to compare elements in
361
+ * the heap. It takes two arguments, `a` and `b`, and returns a negative number if `a` is less than
362
+ * `b`, a positive number if `a` is greater than `b`, or
363
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
364
+ * specify the value of `this` within the callback function. It is used when you want to bind a
365
+ * specific object as the context for the callback function. If `thisArg` is not provided,
366
+ * `undefined` is used as
367
+ * @returns a new instance of the Heap class, which is created using the mapped elements from the
368
+ * original Heap.
369
+ */
370
+ map(callback, comparator, thisArg) {
337
371
  const mappedHeap = new Heap([], { comparator: comparator });
338
372
  let index = 0;
339
373
  for (const el of this) {
340
- mappedHeap.add(callback(el, index, this));
374
+ mappedHeap.add(callback.call(thisArg, el, index, this));
341
375
  index++;
342
376
  }
343
377
  return mappedHeap;
344
378
  }
345
- reduce(callback, initialValue) {
346
- let accumulator = initialValue;
347
- let index = 0;
348
- for (const el of this) {
349
- accumulator = callback(accumulator, el, index, this);
350
- index++;
351
- }
352
- return accumulator;
353
- }
354
379
  /**
355
380
  * Time Complexity: O(log n)
356
381
  * Space Complexity: O(1)
@@ -358,6 +383,11 @@ class Heap {
358
383
  print() {
359
384
  console.log([...this]);
360
385
  }
386
+ *_getIterator() {
387
+ for (const element of this.elements) {
388
+ yield element;
389
+ }
390
+ }
361
391
  /**
362
392
  * Time Complexity: O(n)
363
393
  * Space Complexity: O(1)
@@ -9,3 +9,4 @@ export * from './heap';
9
9
  export * from './priority-queue';
10
10
  export * from './matrix';
11
11
  export * from './trie';
12
+ export * from './base';
@@ -25,3 +25,4 @@ __exportStar(require("./heap"), exports);
25
25
  __exportStar(require("./priority-queue"), exports);
26
26
  __exportStar(require("./matrix"), exports);
27
27
  __exportStar(require("./trie"), exports);
28
+ __exportStar(require("./base"), exports);