graph-typed 1.47.4 → 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.
- package/dist/data-structures/hash/hash-map.d.ts +3 -3
- package/dist/data-structures/hash/hash-map.js +10 -4
- package/dist/data-structures/hash/hash-table.d.ts +9 -4
- package/dist/data-structures/hash/hash-table.js +50 -5
- package/dist/data-structures/heap/heap.d.ts +6 -1
- package/dist/data-structures/heap/heap.js +51 -9
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +56 -56
- package/dist/data-structures/linked-list/doubly-linked-list.js +117 -119
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +36 -36
- package/dist/data-structures/linked-list/singly-linked-list.js +58 -60
- package/dist/data-structures/queue/deque.d.ts +49 -49
- package/dist/data-structures/queue/deque.js +79 -72
- package/dist/data-structures/queue/queue.d.ts +45 -0
- package/dist/data-structures/queue/queue.js +77 -0
- package/dist/data-structures/stack/stack.d.ts +18 -5
- package/dist/data-structures/stack/stack.js +56 -7
- package/dist/data-structures/trie/trie.d.ts +5 -0
- package/dist/data-structures/trie/trie.js +47 -0
- package/package.json +1 -1
- package/src/data-structures/hash/hash-map.ts +13 -7
- package/src/data-structures/hash/hash-table.ts +59 -9
- package/src/data-structures/heap/heap.ts +60 -9
- package/src/data-structures/linked-list/doubly-linked-list.ts +129 -129
- package/src/data-structures/linked-list/singly-linked-list.ts +65 -65
- package/src/data-structures/queue/deque.ts +84 -77
- package/src/data-structures/queue/queue.ts +84 -0
- package/src/data-structures/stack/stack.ts +64 -8
- package/src/data-structures/trie/trie.ts +53 -0
|
@@ -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 `
|
|
322
|
-
*
|
|
323
|
-
* @param callback -
|
|
324
|
-
*
|
|
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
|
-
|
|
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
|
|
336
|
-
*
|
|
337
|
-
* @param
|
|
338
|
-
*
|
|
339
|
-
* @returns The
|
|
340
|
-
*
|
|
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
|
-
|
|
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(
|
|
359
|
+
* Space Complexity: O(1)
|
|
358
360
|
*/
|
|
359
361
|
/**
|
|
360
362
|
* Time Complexity: O(n)
|
|
361
|
-
* Space Complexity: O(
|
|
363
|
+
* Space Complexity: O(1)
|
|
362
364
|
*
|
|
363
|
-
* The
|
|
364
|
-
*
|
|
365
|
-
|
|
366
|
-
|
|
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
|
-
|
|
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:
|
|
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,7 @@ 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:
|
|
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
431
|
/**
|
|
432
432
|
* Time Complexity: O(n)
|
|
433
433
|
* 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 `
|
|
598
|
-
*
|
|
599
|
-
* @param callback -
|
|
600
|
-
*
|
|
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
|
-
|
|
604
|
+
find(callback) {
|
|
603
605
|
for (let i = 0; i < this.size; ++i) {
|
|
604
|
-
|
|
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
|
|
616
|
-
*
|
|
617
|
-
* @param
|
|
618
|
-
*
|
|
619
|
-
* @returns The
|
|
620
|
-
*
|
|
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
|
-
|
|
628
|
+
indexOf(element) {
|
|
623
629
|
for (let i = 0; i < this.size; ++i) {
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
return element;
|
|
630
|
+
if (this.getAt(i) === element) {
|
|
631
|
+
return i;
|
|
627
632
|
}
|
|
628
633
|
}
|
|
629
|
-
return
|
|
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(
|
|
656
|
+
* Space Complexity: O(1)
|
|
652
657
|
*/
|
|
653
658
|
/**
|
|
654
659
|
* Time Complexity: O(n)
|
|
655
|
-
* Space Complexity: O(
|
|
660
|
+
* Space Complexity: O(1)
|
|
656
661
|
*
|
|
657
|
-
* The
|
|
658
|
-
*
|
|
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
|
-
|
|
663
|
-
const newDeque = new Deque([], this._bucketSize);
|
|
665
|
+
*[Symbol.iterator]() {
|
|
664
666
|
for (let i = 0; i < this.size; ++i) {
|
|
665
|
-
|
|
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
|
-
|
|
687
|
-
|
|
688
|
-
if (predicate(
|
|
689
|
-
newDeque.push(
|
|
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,50 +754,13 @@ class Deque {
|
|
|
710
754
|
*/
|
|
711
755
|
reduce(callback, initialValue) {
|
|
712
756
|
let accumulator = initialValue;
|
|
713
|
-
|
|
714
|
-
|
|
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
|
-
}
|
|
756
|
-
}
|
|
757
764
|
/**
|
|
758
765
|
* Time Complexity: O(n)
|
|
759
766
|
* Space Complexity: O(n)
|
|
@@ -206,4 +206,49 @@ export declare class Queue<E = any> {
|
|
|
206
206
|
*/
|
|
207
207
|
clone(): Queue<E>;
|
|
208
208
|
[Symbol.iterator](): Generator<E, void, unknown>;
|
|
209
|
+
/**
|
|
210
|
+
* Time Complexity: O(n)
|
|
211
|
+
* Space Complexity: O(1)
|
|
212
|
+
*/
|
|
213
|
+
/**
|
|
214
|
+
* Time Complexity: O(n)
|
|
215
|
+
* Space Complexity: O(1)
|
|
216
|
+
*
|
|
217
|
+
* The `forEach` function iterates over each element in a deque and applies a callback function to
|
|
218
|
+
* each element.
|
|
219
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
220
|
+
* deque. It takes three parameters:
|
|
221
|
+
*/
|
|
222
|
+
forEach(callback: (element: E, index: number, queue: this) => void): void;
|
|
223
|
+
/**
|
|
224
|
+
* Time Complexity: O(n)
|
|
225
|
+
* Space Complexity: O(n)
|
|
226
|
+
*/
|
|
227
|
+
/**
|
|
228
|
+
* Time Complexity: O(n)
|
|
229
|
+
* Space Complexity: O(n)
|
|
230
|
+
*
|
|
231
|
+
* The `filter` function creates a new deque containing only the elements that satisfy the given
|
|
232
|
+
* predicate function.
|
|
233
|
+
* @param predicate - The `predicate` parameter is a function that takes three arguments: `element`,
|
|
234
|
+
* `index`, and `deque`.
|
|
235
|
+
* @returns The `filter` method is returning a new `Queue` object that contains only the elements
|
|
236
|
+
* that satisfy the given `predicate` function.
|
|
237
|
+
*/
|
|
238
|
+
filter(predicate: (element: E, index: number, queue: this) => boolean): Queue<E>;
|
|
239
|
+
/**
|
|
240
|
+
* Time Complexity: O(n)
|
|
241
|
+
* Space Complexity: O(n)
|
|
242
|
+
*/
|
|
243
|
+
/**
|
|
244
|
+
* Time Complexity: O(n)
|
|
245
|
+
* Space Complexity: O(n)
|
|
246
|
+
*
|
|
247
|
+
* The `map` function takes a callback function and applies it to each element in the deque,
|
|
248
|
+
* returning a new deque with the results.
|
|
249
|
+
* @param callback - The `callback` parameter is a function that takes three arguments:
|
|
250
|
+
* @returns The `map` method is returning a new `Queue` object with the transformed elements.
|
|
251
|
+
*/
|
|
252
|
+
map<T>(callback: (element: E, index: number, queue: this) => T): Queue<T>;
|
|
253
|
+
reduce<T>(callback: (accumulator: T, element: E, index: number, queue: this) => T, initialValue: T): T;
|
|
209
254
|
}
|
|
@@ -270,5 +270,82 @@ class Queue {
|
|
|
270
270
|
yield item;
|
|
271
271
|
}
|
|
272
272
|
}
|
|
273
|
+
/**
|
|
274
|
+
* Time Complexity: O(n)
|
|
275
|
+
* Space Complexity: O(1)
|
|
276
|
+
*/
|
|
277
|
+
/**
|
|
278
|
+
* Time Complexity: O(n)
|
|
279
|
+
* Space Complexity: O(1)
|
|
280
|
+
*
|
|
281
|
+
* The `forEach` function iterates over each element in a deque and applies a callback function to
|
|
282
|
+
* each element.
|
|
283
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
284
|
+
* deque. It takes three parameters:
|
|
285
|
+
*/
|
|
286
|
+
forEach(callback) {
|
|
287
|
+
let index = 0;
|
|
288
|
+
for (const el of this) {
|
|
289
|
+
callback(el, index, this);
|
|
290
|
+
index++;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Time Complexity: O(n)
|
|
295
|
+
* Space Complexity: O(n)
|
|
296
|
+
*/
|
|
297
|
+
/**
|
|
298
|
+
* Time Complexity: O(n)
|
|
299
|
+
* Space Complexity: O(n)
|
|
300
|
+
*
|
|
301
|
+
* The `filter` function creates a new deque containing only the elements that satisfy the given
|
|
302
|
+
* predicate function.
|
|
303
|
+
* @param predicate - The `predicate` parameter is a function that takes three arguments: `element`,
|
|
304
|
+
* `index`, and `deque`.
|
|
305
|
+
* @returns The `filter` method is returning a new `Queue` object that contains only the elements
|
|
306
|
+
* that satisfy the given `predicate` function.
|
|
307
|
+
*/
|
|
308
|
+
filter(predicate) {
|
|
309
|
+
const newDeque = new Queue([]);
|
|
310
|
+
let index = 0;
|
|
311
|
+
for (const el of this) {
|
|
312
|
+
if (predicate(el, index, this)) {
|
|
313
|
+
newDeque.push(el);
|
|
314
|
+
}
|
|
315
|
+
index++;
|
|
316
|
+
}
|
|
317
|
+
return newDeque;
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Time Complexity: O(n)
|
|
321
|
+
* Space Complexity: O(n)
|
|
322
|
+
*/
|
|
323
|
+
/**
|
|
324
|
+
* Time Complexity: O(n)
|
|
325
|
+
* Space Complexity: O(n)
|
|
326
|
+
*
|
|
327
|
+
* The `map` function takes a callback function and applies it to each element in the deque,
|
|
328
|
+
* returning a new deque with the results.
|
|
329
|
+
* @param callback - The `callback` parameter is a function that takes three arguments:
|
|
330
|
+
* @returns The `map` method is returning a new `Queue` object with the transformed elements.
|
|
331
|
+
*/
|
|
332
|
+
map(callback) {
|
|
333
|
+
const newDeque = new Queue([]);
|
|
334
|
+
let index = 0;
|
|
335
|
+
for (const el of this) {
|
|
336
|
+
newDeque.push(callback(el, index, this));
|
|
337
|
+
index++;
|
|
338
|
+
}
|
|
339
|
+
return newDeque;
|
|
340
|
+
}
|
|
341
|
+
reduce(callback, initialValue) {
|
|
342
|
+
let accumulator = initialValue;
|
|
343
|
+
let index = 0;
|
|
344
|
+
for (const el of this) {
|
|
345
|
+
accumulator = callback(accumulator, el, index, this);
|
|
346
|
+
index++;
|
|
347
|
+
}
|
|
348
|
+
return accumulator;
|
|
349
|
+
}
|
|
273
350
|
}
|
|
274
351
|
exports.Queue = Queue;
|
|
@@ -17,6 +17,11 @@ export declare class Stack<E = any> {
|
|
|
17
17
|
* 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.
|
|
18
18
|
* Space Complexity: O(n), as it creates a new stack with the elements from the input array.
|
|
19
19
|
*/
|
|
20
|
+
/**
|
|
21
|
+
* The size() function returns the number of elements in an array.
|
|
22
|
+
* @returns The size of the elements array.
|
|
23
|
+
*/
|
|
24
|
+
get size(): number;
|
|
20
25
|
/**
|
|
21
26
|
* 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.
|
|
22
27
|
* Space Complexity: O(n), as it creates a new stack with the elements from the input array.
|
|
@@ -32,11 +37,6 @@ export declare class Stack<E = any> {
|
|
|
32
37
|
* @returns A boolean value indicating whether the `_elements` array is empty or not.
|
|
33
38
|
*/
|
|
34
39
|
isEmpty(): boolean;
|
|
35
|
-
/**
|
|
36
|
-
* The size() function returns the number of elements in an array.
|
|
37
|
-
* @returns The size of the elements array.
|
|
38
|
-
*/
|
|
39
|
-
size(): number;
|
|
40
40
|
/**
|
|
41
41
|
* Time Complexity: O(1), as it only involves accessing the last element of the array.
|
|
42
42
|
* Space Complexity: O(1), as it does not use any additional space.
|
|
@@ -103,4 +103,17 @@ export declare class Stack<E = any> {
|
|
|
103
103
|
* @returns The `clone()` method is returning a new `Stack` object with a copy of the `_elements` array.
|
|
104
104
|
*/
|
|
105
105
|
clone(): Stack<E>;
|
|
106
|
+
/**
|
|
107
|
+
* Custom iterator for the Stack class.
|
|
108
|
+
* @returns An iterator object.
|
|
109
|
+
*/
|
|
110
|
+
[Symbol.iterator](): Generator<E, void, unknown>;
|
|
111
|
+
/**
|
|
112
|
+
* Applies a function to each element of the stack.
|
|
113
|
+
* @param {function(E): void} callback - A function to apply to each element.
|
|
114
|
+
*/
|
|
115
|
+
forEach(callback: (element: E, index: number, stack: this) => void): void;
|
|
116
|
+
filter(predicate: (element: E, index: number, stack: this) => boolean): Stack<E>;
|
|
117
|
+
map<T>(callback: (element: E, index: number, stack: this) => T): Stack<T>;
|
|
118
|
+
reduce<T>(callback: (accumulator: T, element: E, index: number, stack: this) => T, initialValue: T): T;
|
|
106
119
|
}
|
|
@@ -23,6 +23,13 @@ class Stack {
|
|
|
23
23
|
* 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.
|
|
24
24
|
* Space Complexity: O(n), as it creates a new stack with the elements from the input array.
|
|
25
25
|
*/
|
|
26
|
+
/**
|
|
27
|
+
* The size() function returns the number of elements in an array.
|
|
28
|
+
* @returns The size of the elements array.
|
|
29
|
+
*/
|
|
30
|
+
get size() {
|
|
31
|
+
return this.elements.length;
|
|
32
|
+
}
|
|
26
33
|
/**
|
|
27
34
|
* 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.
|
|
28
35
|
* Space Complexity: O(n), as it creates a new stack with the elements from the input array.
|
|
@@ -42,13 +49,6 @@ class Stack {
|
|
|
42
49
|
isEmpty() {
|
|
43
50
|
return this.elements.length === 0;
|
|
44
51
|
}
|
|
45
|
-
/**
|
|
46
|
-
* The size() function returns the number of elements in an array.
|
|
47
|
-
* @returns The size of the elements array.
|
|
48
|
-
*/
|
|
49
|
-
size() {
|
|
50
|
-
return this.elements.length;
|
|
51
|
-
}
|
|
52
52
|
/**
|
|
53
53
|
* Time Complexity: O(1), as it only involves accessing the last element of the array.
|
|
54
54
|
* Space Complexity: O(1), as it does not use any additional space.
|
|
@@ -132,5 +132,54 @@ class Stack {
|
|
|
132
132
|
clone() {
|
|
133
133
|
return new Stack(this.elements.slice());
|
|
134
134
|
}
|
|
135
|
+
/**
|
|
136
|
+
* Custom iterator for the Stack class.
|
|
137
|
+
* @returns An iterator object.
|
|
138
|
+
*/
|
|
139
|
+
*[Symbol.iterator]() {
|
|
140
|
+
for (let i = this.elements.length - 1; i >= 0; i--) {
|
|
141
|
+
yield this.elements[i];
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Applies a function to each element of the stack.
|
|
146
|
+
* @param {function(E): void} callback - A function to apply to each element.
|
|
147
|
+
*/
|
|
148
|
+
forEach(callback) {
|
|
149
|
+
let index = 0;
|
|
150
|
+
for (const el of this) {
|
|
151
|
+
callback(el, index, this);
|
|
152
|
+
index++;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
filter(predicate) {
|
|
156
|
+
const newStack = new Stack();
|
|
157
|
+
let index = 0;
|
|
158
|
+
for (const el of this) {
|
|
159
|
+
if (predicate(el, index, this)) {
|
|
160
|
+
newStack.push(el);
|
|
161
|
+
}
|
|
162
|
+
index++;
|
|
163
|
+
}
|
|
164
|
+
return newStack;
|
|
165
|
+
}
|
|
166
|
+
map(callback) {
|
|
167
|
+
const newStack = new Stack();
|
|
168
|
+
let index = 0;
|
|
169
|
+
for (const el of this) {
|
|
170
|
+
newStack.push(callback(el, index, this));
|
|
171
|
+
index++;
|
|
172
|
+
}
|
|
173
|
+
return newStack;
|
|
174
|
+
}
|
|
175
|
+
reduce(callback, initialValue) {
|
|
176
|
+
let accumulator = initialValue;
|
|
177
|
+
let index = 0;
|
|
178
|
+
for (const el of this) {
|
|
179
|
+
accumulator = callback(accumulator, el, index, this);
|
|
180
|
+
index++;
|
|
181
|
+
}
|
|
182
|
+
return accumulator;
|
|
183
|
+
}
|
|
135
184
|
}
|
|
136
185
|
exports.Stack = Stack;
|
|
@@ -140,6 +140,11 @@ export declare class Trie {
|
|
|
140
140
|
* @returns {string[]} an array of strings.
|
|
141
141
|
*/
|
|
142
142
|
getWords(prefix?: string, max?: number, isAllWhenEmptyPrefix?: boolean): string[];
|
|
143
|
+
[Symbol.iterator](): IterableIterator<string>;
|
|
144
|
+
forEach(callback: (word: string, index: number, trie: this) => void): void;
|
|
145
|
+
filter(predicate: (word: string, index: number, trie: this) => boolean): string[];
|
|
146
|
+
map(callback: (word: string, index: number, trie: this) => string): Trie;
|
|
147
|
+
reduce<T>(callback: (accumulator: T, word: string, index: number, trie: this) => T, initialValue: T): T;
|
|
143
148
|
/**
|
|
144
149
|
* Time Complexity: O(M), where M is the length of the input string.
|
|
145
150
|
* Space Complexity: O(1) - Constant space.
|
|
@@ -305,6 +305,53 @@ class Trie {
|
|
|
305
305
|
dfs(startNode, prefix);
|
|
306
306
|
return words;
|
|
307
307
|
}
|
|
308
|
+
*[Symbol.iterator]() {
|
|
309
|
+
function* _dfs(node, path) {
|
|
310
|
+
if (node.isEnd) {
|
|
311
|
+
yield path;
|
|
312
|
+
}
|
|
313
|
+
for (const [char, childNode] of node.children) {
|
|
314
|
+
yield* _dfs(childNode, path + char);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
yield* _dfs(this.root, '');
|
|
318
|
+
}
|
|
319
|
+
forEach(callback) {
|
|
320
|
+
let index = 0;
|
|
321
|
+
for (const word of this) {
|
|
322
|
+
callback(word, index, this);
|
|
323
|
+
index++;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
filter(predicate) {
|
|
327
|
+
const results = [];
|
|
328
|
+
let index = 0;
|
|
329
|
+
for (const word of this) {
|
|
330
|
+
if (predicate(word, index, this)) {
|
|
331
|
+
results.push(word);
|
|
332
|
+
}
|
|
333
|
+
index++;
|
|
334
|
+
}
|
|
335
|
+
return results;
|
|
336
|
+
}
|
|
337
|
+
map(callback) {
|
|
338
|
+
const newTrie = new Trie();
|
|
339
|
+
let index = 0;
|
|
340
|
+
for (const word of this) {
|
|
341
|
+
newTrie.add(callback(word, index, this));
|
|
342
|
+
index++;
|
|
343
|
+
}
|
|
344
|
+
return newTrie;
|
|
345
|
+
}
|
|
346
|
+
reduce(callback, initialValue) {
|
|
347
|
+
let accumulator = initialValue;
|
|
348
|
+
let index = 0;
|
|
349
|
+
for (const word of this) {
|
|
350
|
+
accumulator = callback(accumulator, word, index, this);
|
|
351
|
+
index++;
|
|
352
|
+
}
|
|
353
|
+
return accumulator;
|
|
354
|
+
}
|
|
308
355
|
/**
|
|
309
356
|
* Time Complexity: O(M), where M is the length of the input string.
|
|
310
357
|
* Space Complexity: O(1) - Constant space.
|