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.
- package/dist/data-structures/base/index.d.ts +1 -0
- package/dist/data-structures/base/index.js +17 -0
- package/dist/data-structures/base/iterable-base.d.ts +232 -0
- package/dist/data-structures/base/iterable-base.js +312 -0
- package/dist/data-structures/binary-tree/avl-tree.d.ts +16 -16
- package/dist/data-structures/binary-tree/avl-tree.js +7 -7
- package/dist/data-structures/binary-tree/binary-tree.d.ts +121 -152
- package/dist/data-structures/binary-tree/binary-tree.js +140 -182
- package/dist/data-structures/binary-tree/bst.d.ts +28 -47
- package/dist/data-structures/binary-tree/bst.js +54 -57
- package/dist/data-structures/binary-tree/rb-tree.d.ts +15 -15
- package/dist/data-structures/binary-tree/rb-tree.js +7 -7
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +22 -22
- package/dist/data-structures/binary-tree/tree-multimap.js +11 -11
- package/dist/data-structures/graph/abstract-graph.d.ts +44 -6
- package/dist/data-structures/graph/abstract-graph.js +50 -27
- package/dist/data-structures/hash/hash-map.d.ts +59 -100
- package/dist/data-structures/hash/hash-map.js +69 -173
- package/dist/data-structures/heap/heap.d.ts +50 -7
- package/dist/data-structures/heap/heap.js +60 -30
- package/dist/data-structures/index.d.ts +1 -0
- package/dist/data-structures/index.js +1 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
- package/dist/data-structures/linked-list/doubly-linked-list.js +46 -73
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +32 -51
- package/dist/data-structures/linked-list/singly-linked-list.js +40 -73
- package/dist/data-structures/queue/deque.d.ts +29 -51
- package/dist/data-structures/queue/deque.js +36 -71
- package/dist/data-structures/queue/queue.d.ts +49 -48
- package/dist/data-structures/queue/queue.js +69 -82
- package/dist/data-structures/stack/stack.d.ts +43 -10
- package/dist/data-structures/stack/stack.js +50 -31
- package/dist/data-structures/trie/trie.d.ts +41 -6
- package/dist/data-structures/trie/trie.js +53 -32
- package/dist/interfaces/binary-tree.d.ts +6 -6
- package/dist/types/common.d.ts +11 -8
- package/dist/types/common.js +6 -1
- package/dist/types/data-structures/base/base.d.ts +5 -0
- package/dist/types/data-structures/base/base.js +2 -0
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/index.js +17 -0
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +4 -4
- package/dist/types/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +3 -3
- package/dist/types/data-structures/index.d.ts +1 -0
- package/dist/types/data-structures/index.js +1 -0
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-base.ts +329 -0
- package/src/data-structures/binary-tree/avl-tree.ts +20 -21
- package/src/data-structures/binary-tree/binary-tree.ts +222 -267
- package/src/data-structures/binary-tree/bst.ts +86 -82
- package/src/data-structures/binary-tree/rb-tree.ts +25 -26
- package/src/data-structures/binary-tree/tree-multimap.ts +30 -35
- package/src/data-structures/graph/abstract-graph.ts +55 -28
- package/src/data-structures/hash/hash-map.ts +76 -185
- package/src/data-structures/heap/heap.ts +63 -36
- package/src/data-structures/index.ts +1 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +50 -79
- package/src/data-structures/linked-list/singly-linked-list.ts +45 -80
- package/src/data-structures/queue/deque.ts +40 -82
- package/src/data-structures/queue/queue.ts +72 -87
- package/src/data-structures/stack/stack.ts +53 -34
- package/src/data-structures/trie/trie.ts +58 -35
- package/src/interfaces/binary-tree.ts +5 -6
- package/src/types/common.ts +11 -8
- package/src/types/data-structures/base/base.ts +6 -0
- package/src/types/data-structures/base/index.ts +1 -0
- package/src/types/data-structures/binary-tree/avl-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +6 -5
- package/src/types/data-structures/binary-tree/bst.ts +6 -6
- package/src/types/data-structures/binary-tree/rb-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/tree-multimap.ts +3 -3
- package/src/types/data-structures/index.ts +1 -0
|
@@ -7,8 +7,9 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
|
|
10
|
-
import { IterableWithSizeOrLength } from "../../types";
|
|
10
|
+
import { ElementCallback, IterableWithSizeOrLength } from "../../types";
|
|
11
11
|
import { calcMinUnitsRequired, rangeCheck } from "../../utils";
|
|
12
|
+
import { IterableElementBase } from "../base";
|
|
12
13
|
|
|
13
14
|
/**
|
|
14
15
|
* Deque can provide random access with O(1) time complexity
|
|
@@ -17,7 +18,7 @@ import { calcMinUnitsRequired, rangeCheck } from "../../utils";
|
|
|
17
18
|
* Deque is implemented using a dynamic array. Inserting or deleting beyond both ends of the array may require moving elements or reallocating space.
|
|
18
19
|
*/
|
|
19
20
|
|
|
20
|
-
export class Deque<E> {
|
|
21
|
+
export class Deque<E> extends IterableElementBase<E> {
|
|
21
22
|
protected _bucketFirst = 0;
|
|
22
23
|
protected _firstInBucket = 0;
|
|
23
24
|
protected _bucketLast = 0;
|
|
@@ -35,7 +36,7 @@ export class Deque<E> {
|
|
|
35
36
|
* stored in each bucket. It determines the size of each bucket in the data structure.
|
|
36
37
|
*/
|
|
37
38
|
constructor(elements: IterableWithSizeOrLength<E> = [], bucketSize = (1 << 12)) {
|
|
38
|
-
|
|
39
|
+
super();
|
|
39
40
|
let _size: number;
|
|
40
41
|
if ('length' in elements) {
|
|
41
42
|
if (elements.length instanceof Function) _size = elements.length(); else _size = elements.length;
|
|
@@ -699,67 +700,31 @@ export class Deque<E> {
|
|
|
699
700
|
return arr;
|
|
700
701
|
}
|
|
701
702
|
|
|
702
|
-
/**
|
|
703
|
-
* Time Complexity: O(n)
|
|
704
|
-
* Space Complexity: O(1)
|
|
705
|
-
*/
|
|
706
|
-
|
|
707
|
-
/**
|
|
708
|
-
* Time Complexity: O(n)
|
|
709
|
-
* Space Complexity: O(1)
|
|
710
|
-
*
|
|
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.
|
|
713
|
-
*/
|
|
714
|
-
* [Symbol.iterator]() {
|
|
715
|
-
for (let i = 0; i < this.size; ++i) {
|
|
716
|
-
yield this.getAt(i);
|
|
717
|
-
}
|
|
718
|
-
}
|
|
719
|
-
|
|
720
|
-
/**
|
|
721
|
-
* Time Complexity: O(n)
|
|
722
|
-
* Space Complexity: O(1)
|
|
723
|
-
*/
|
|
724
|
-
|
|
725
|
-
/**
|
|
726
|
-
* Time Complexity: O(n)
|
|
727
|
-
* Space Complexity: O(1)
|
|
728
|
-
*
|
|
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:
|
|
733
|
-
*/
|
|
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++;
|
|
739
|
-
}
|
|
740
|
-
}
|
|
741
|
-
|
|
742
703
|
/**
|
|
743
704
|
* Time Complexity: O(n)
|
|
744
705
|
* Space Complexity: O(n)
|
|
745
706
|
*/
|
|
746
|
-
|
|
747
707
|
/**
|
|
748
708
|
* Time Complexity: O(n)
|
|
749
709
|
* Space Complexity: O(n)
|
|
750
710
|
*
|
|
751
|
-
* The `filter` function creates a new deque containing
|
|
752
|
-
* predicate function.
|
|
753
|
-
* @param predicate - The `predicate` parameter is a function that takes three arguments:
|
|
754
|
-
*
|
|
755
|
-
*
|
|
756
|
-
*
|
|
757
|
-
|
|
758
|
-
|
|
711
|
+
* The `filter` function creates a new deque containing elements from the original deque that satisfy
|
|
712
|
+
* a given predicate function.
|
|
713
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
714
|
+
* the current element being iterated over, the index of the current element, and the deque itself.
|
|
715
|
+
* It should return a boolean value indicating whether the element should be included in the filtered
|
|
716
|
+
* deque or not.
|
|
717
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
718
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
719
|
+
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
720
|
+
* @returns The `filter` method is returning a new `Deque` object that contains the elements that
|
|
721
|
+
* satisfy the given predicate function.
|
|
722
|
+
*/
|
|
723
|
+
filter(predicate: ElementCallback<E, boolean>, thisArg?: any): Deque<E> {
|
|
759
724
|
const newDeque = new Deque<E>([], this._bucketSize);
|
|
760
725
|
let index = 0;
|
|
761
726
|
for (const el of this) {
|
|
762
|
-
if (predicate(el, index, this)) {
|
|
727
|
+
if (predicate.call(thisArg, el, index, this)) {
|
|
763
728
|
newDeque.push(el);
|
|
764
729
|
}
|
|
765
730
|
index++;
|
|
@@ -771,21 +736,24 @@ export class Deque<E> {
|
|
|
771
736
|
* Time Complexity: O(n)
|
|
772
737
|
* Space Complexity: O(n)
|
|
773
738
|
*/
|
|
774
|
-
|
|
775
739
|
/**
|
|
776
740
|
* Time Complexity: O(n)
|
|
777
741
|
* Space Complexity: O(n)
|
|
778
742
|
*
|
|
779
|
-
* The `map` function
|
|
780
|
-
*
|
|
781
|
-
* @param callback - The `callback` parameter is a function that
|
|
782
|
-
*
|
|
783
|
-
|
|
784
|
-
|
|
743
|
+
* The `map` function creates a new Deque by applying a callback function to each element of the
|
|
744
|
+
* original Deque.
|
|
745
|
+
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
746
|
+
* the deque. It takes three arguments:
|
|
747
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
748
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
749
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
750
|
+
* @returns a new Deque object with the mapped values.
|
|
751
|
+
*/
|
|
752
|
+
map<T>(callback: ElementCallback<E, T>, thisArg?: any): Deque<T> {
|
|
785
753
|
const newDeque = new Deque<T>([], this._bucketSize);
|
|
786
754
|
let index = 0;
|
|
787
755
|
for (const el of this) {
|
|
788
|
-
newDeque.push(callback(el, index, this));
|
|
756
|
+
newDeque.push(callback.call(thisArg, el, index, this));
|
|
789
757
|
index++;
|
|
790
758
|
}
|
|
791
759
|
return newDeque;
|
|
@@ -793,34 +761,24 @@ export class Deque<E> {
|
|
|
793
761
|
|
|
794
762
|
/**
|
|
795
763
|
* Time Complexity: O(n)
|
|
796
|
-
* Space Complexity: O(
|
|
764
|
+
* Space Complexity: O(n)
|
|
797
765
|
*/
|
|
798
766
|
|
|
767
|
+
print(): void {
|
|
768
|
+
console.log([...this])
|
|
769
|
+
}
|
|
770
|
+
|
|
799
771
|
/**
|
|
800
772
|
* Time Complexity: O(n)
|
|
801
773
|
* Space Complexity: O(1)
|
|
802
774
|
*
|
|
803
|
-
* The
|
|
804
|
-
*
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
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.
|
|
811
|
-
*/
|
|
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++;
|
|
775
|
+
* The above function is an implementation of the iterator protocol in TypeScript, allowing the
|
|
776
|
+
* object to be iterated over using a for...of loop.
|
|
777
|
+
*/
|
|
778
|
+
protected* _getIterator() {
|
|
779
|
+
for (let i = 0; i < this.size; ++i) {
|
|
780
|
+
yield this.getAt(i);
|
|
818
781
|
}
|
|
819
|
-
return accumulator;
|
|
820
|
-
}
|
|
821
|
-
|
|
822
|
-
print(): void {
|
|
823
|
-
console.log([...this])
|
|
824
782
|
}
|
|
825
783
|
|
|
826
784
|
/**
|
|
@@ -4,42 +4,10 @@
|
|
|
4
4
|
* @class
|
|
5
5
|
*/
|
|
6
6
|
import { SinglyLinkedList } from '../linked-list';
|
|
7
|
+
import { IterableElementBase } from "../base";
|
|
8
|
+
import { ElementCallback } from "../../types";
|
|
7
9
|
|
|
8
|
-
export class
|
|
9
|
-
/**
|
|
10
|
-
* The enqueue function adds a value to the end of an array.
|
|
11
|
-
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
12
|
-
*/
|
|
13
|
-
enqueue(value: E) {
|
|
14
|
-
this.push(value);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
|
|
19
|
-
* @returns The method is returning the element at the front of the queue, or undefined if the queue is empty.
|
|
20
|
-
*/
|
|
21
|
-
dequeue(): E | undefined {
|
|
22
|
-
return this.shift();
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
27
|
-
* @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
28
|
-
*/
|
|
29
|
-
getFirst(): E | undefined {
|
|
30
|
-
return this.head?.value;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
35
|
-
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
36
|
-
*/
|
|
37
|
-
peek(): E | undefined {
|
|
38
|
-
return this.getFirst();
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export class Queue<E = any> {
|
|
10
|
+
export class Queue<E = any> extends IterableElementBase<E> {
|
|
43
11
|
/**
|
|
44
12
|
* The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
|
|
45
13
|
* @param {E[]} [elements] - The `elements` parameter is an optional array of elements of type `E`. If provided, it
|
|
@@ -47,6 +15,7 @@ export class Queue<E = any> {
|
|
|
47
15
|
* initialized as an empty array.
|
|
48
16
|
*/
|
|
49
17
|
constructor(elements?: E[]) {
|
|
18
|
+
super();
|
|
50
19
|
this._nodes = elements || [];
|
|
51
20
|
this._offset = 0;
|
|
52
21
|
}
|
|
@@ -304,34 +273,6 @@ export class Queue<E = any> {
|
|
|
304
273
|
console.log([...this]);
|
|
305
274
|
}
|
|
306
275
|
|
|
307
|
-
* [Symbol.iterator]() {
|
|
308
|
-
for (const item of this.nodes) {
|
|
309
|
-
yield item;
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
/**
|
|
314
|
-
* Time Complexity: O(n)
|
|
315
|
-
* Space Complexity: O(1)
|
|
316
|
-
*/
|
|
317
|
-
|
|
318
|
-
/**
|
|
319
|
-
* Time Complexity: O(n)
|
|
320
|
-
* Space Complexity: O(1)
|
|
321
|
-
*
|
|
322
|
-
* The `forEach` function iterates over each element in a deque and applies a callback function to
|
|
323
|
-
* each element.
|
|
324
|
-
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
325
|
-
* deque. It takes three parameters:
|
|
326
|
-
*/
|
|
327
|
-
forEach(callback: (element: E, index: number, queue: this) => void) {
|
|
328
|
-
let index = 0;
|
|
329
|
-
for (const el of this) {
|
|
330
|
-
callback(el, index, this);
|
|
331
|
-
index++;
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
|
|
335
276
|
/**
|
|
336
277
|
* Time Complexity: O(n)
|
|
337
278
|
* Space Complexity: O(n)
|
|
@@ -341,18 +282,23 @@ export class Queue<E = any> {
|
|
|
341
282
|
* Time Complexity: O(n)
|
|
342
283
|
* Space Complexity: O(n)
|
|
343
284
|
*
|
|
344
|
-
* The `filter` function creates a new
|
|
345
|
-
* predicate function.
|
|
346
|
-
* @param predicate - The `predicate` parameter is a function that takes three arguments:
|
|
347
|
-
*
|
|
348
|
-
*
|
|
349
|
-
*
|
|
350
|
-
|
|
351
|
-
|
|
285
|
+
* The `filter` function creates a new `Queue` object containing elements from the original `Queue`
|
|
286
|
+
* that satisfy a given predicate function.
|
|
287
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
288
|
+
* the current element being iterated over, the index of the current element, and the queue itself.
|
|
289
|
+
* It should return a boolean value indicating whether the element should be included in the filtered
|
|
290
|
+
* queue or not.
|
|
291
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
292
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
293
|
+
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
294
|
+
* @returns The `filter` method is returning a new `Queue` object that contains the elements that
|
|
295
|
+
* satisfy the given predicate function.
|
|
296
|
+
*/
|
|
297
|
+
filter(predicate: ElementCallback<E, boolean>, thisArg?: any): Queue<E> {
|
|
352
298
|
const newDeque = new Queue<E>([]);
|
|
353
299
|
let index = 0;
|
|
354
300
|
for (const el of this) {
|
|
355
|
-
if (predicate(el, index, this)) {
|
|
301
|
+
if (predicate.call(thisArg, el, index, this)) {
|
|
356
302
|
newDeque.push(el);
|
|
357
303
|
}
|
|
358
304
|
index++;
|
|
@@ -364,33 +310,72 @@ export class Queue<E = any> {
|
|
|
364
310
|
* Time Complexity: O(n)
|
|
365
311
|
* Space Complexity: O(n)
|
|
366
312
|
*/
|
|
367
|
-
|
|
368
313
|
/**
|
|
369
314
|
* Time Complexity: O(n)
|
|
370
315
|
* Space Complexity: O(n)
|
|
371
316
|
*
|
|
372
|
-
* The `map` function takes a callback function and applies it to each element in the
|
|
373
|
-
* returning a new
|
|
374
|
-
* @param callback - The
|
|
375
|
-
*
|
|
376
|
-
|
|
377
|
-
|
|
317
|
+
* The `map` function takes a callback function and applies it to each element in the queue,
|
|
318
|
+
* returning a new queue with the results.
|
|
319
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
320
|
+
* queue. It takes three arguments: the current element, the index of the current element, and the
|
|
321
|
+
* queue itself. The callback function should return a new value that will be added to the new queue.
|
|
322
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
323
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
324
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
325
|
+
* @returns The `map` function is returning a new `Queue` object with the transformed elements.
|
|
326
|
+
*/
|
|
327
|
+
map<T>(callback: ElementCallback<E, T>, thisArg?: any): Queue<T> {
|
|
378
328
|
const newDeque = new Queue<T>([]);
|
|
379
329
|
let index = 0;
|
|
380
330
|
for (const el of this) {
|
|
381
|
-
newDeque.push(callback(el, index, this));
|
|
331
|
+
newDeque.push(callback.call(thisArg, el, index, this));
|
|
382
332
|
index++;
|
|
383
333
|
}
|
|
384
334
|
return newDeque;
|
|
385
335
|
}
|
|
386
336
|
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
337
|
+
/**
|
|
338
|
+
* Time Complexity: O(n)
|
|
339
|
+
* Space Complexity: O(n)
|
|
340
|
+
*/
|
|
341
|
+
|
|
342
|
+
protected* _getIterator() {
|
|
343
|
+
for (const item of this.nodes) {
|
|
344
|
+
yield item;
|
|
393
345
|
}
|
|
394
|
-
return accumulator;
|
|
395
346
|
}
|
|
396
347
|
}
|
|
348
|
+
|
|
349
|
+
export class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
|
|
350
|
+
/**
|
|
351
|
+
* The enqueue function adds a value to the end of an array.
|
|
352
|
+
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
353
|
+
*/
|
|
354
|
+
enqueue(value: E) {
|
|
355
|
+
this.push(value);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
|
|
360
|
+
* @returns The method is returning the element at the front of the queue, or undefined if the queue is empty.
|
|
361
|
+
*/
|
|
362
|
+
dequeue(): E | undefined {
|
|
363
|
+
return this.shift();
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
368
|
+
* @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
369
|
+
*/
|
|
370
|
+
getFirst(): E | undefined {
|
|
371
|
+
return this.head?.value;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
376
|
+
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
377
|
+
*/
|
|
378
|
+
peek(): E | undefined {
|
|
379
|
+
return this.getFirst();
|
|
380
|
+
}
|
|
381
|
+
}
|
|
@@ -1,9 +1,12 @@
|
|
|
1
|
+
import { IterableElementBase } from "../base";
|
|
2
|
+
import { ElementCallback } from "../../types";
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
5
|
* @license MIT
|
|
3
6
|
* @copyright Tyler Zeng <zrwusa@gmail.com>
|
|
4
7
|
* @class
|
|
5
8
|
*/
|
|
6
|
-
export class Stack<E = any> {
|
|
9
|
+
export class Stack<E = any> extends IterableElementBase<E> {
|
|
7
10
|
/**
|
|
8
11
|
* The constructor initializes an array of elements, which can be provided as an optional parameter.
|
|
9
12
|
* @param {E[]} [elements] - The `elements` parameter is an optional parameter of type `E[]`, which represents an array
|
|
@@ -11,6 +14,7 @@ export class Stack<E = any> {
|
|
|
11
14
|
* is provided and is an array, it is assigned to the `_elements
|
|
12
15
|
*/
|
|
13
16
|
constructor(elements?: Iterable<E>) {
|
|
17
|
+
super();
|
|
14
18
|
this._elements = [];
|
|
15
19
|
if (elements) {
|
|
16
20
|
for (const el of elements) {
|
|
@@ -154,33 +158,31 @@ export class Stack<E = any> {
|
|
|
154
158
|
}
|
|
155
159
|
|
|
156
160
|
/**
|
|
157
|
-
*
|
|
158
|
-
*
|
|
161
|
+
* Time Complexity: O(n)
|
|
162
|
+
* Space Complexity: O(n)
|
|
159
163
|
*/
|
|
160
|
-
* [Symbol.iterator]() {
|
|
161
|
-
for (let i = 0; i < this.elements.length; i++) {
|
|
162
|
-
yield this.elements[i];
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
164
|
|
|
166
165
|
/**
|
|
167
|
-
*
|
|
168
|
-
*
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
166
|
+
* Time Complexity: O(n)
|
|
167
|
+
* Space Complexity: O(n)
|
|
168
|
+
*
|
|
169
|
+
* The `filter` function creates a new stack containing elements from the original stack that satisfy
|
|
170
|
+
* a given predicate function.
|
|
171
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
172
|
+
* the current element being iterated over, the index of the current element, and the stack itself.
|
|
173
|
+
* It should return a boolean value indicating whether the element should be included in the filtered
|
|
174
|
+
* stack or not.
|
|
175
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
176
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
177
|
+
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
178
|
+
* @returns The `filter` method is returning a new `Stack` object that contains the elements that
|
|
179
|
+
* satisfy the given predicate function.
|
|
180
|
+
*/
|
|
181
|
+
filter(predicate: ElementCallback<E, boolean>, thisArg?: any): Stack<E> {
|
|
180
182
|
const newStack = new Stack<E>();
|
|
181
183
|
let index = 0;
|
|
182
184
|
for (const el of this) {
|
|
183
|
-
if (predicate(el, index, this)) {
|
|
185
|
+
if (predicate.call(thisArg, el, index, this)) {
|
|
184
186
|
newStack.push(el);
|
|
185
187
|
}
|
|
186
188
|
index++;
|
|
@@ -188,28 +190,45 @@ export class Stack<E = any> {
|
|
|
188
190
|
return newStack;
|
|
189
191
|
}
|
|
190
192
|
|
|
193
|
+
/**
|
|
194
|
+
* Time Complexity: O(n)
|
|
195
|
+
* Space Complexity: O(n)
|
|
196
|
+
*/
|
|
191
197
|
|
|
192
|
-
|
|
198
|
+
/**
|
|
199
|
+
* Time Complexity: O(n)
|
|
200
|
+
* Space Complexity: O(n)
|
|
201
|
+
*
|
|
202
|
+
* The `map` function takes a callback function and applies it to each element in the stack,
|
|
203
|
+
* returning a new stack with the results.
|
|
204
|
+
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
205
|
+
* the stack. It takes three arguments:
|
|
206
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
207
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
208
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
209
|
+
* @returns The `map` method is returning a new `Stack` object.
|
|
210
|
+
*/
|
|
211
|
+
map<T>(callback: ElementCallback<E, T>, thisArg?: any): Stack<T> {
|
|
193
212
|
const newStack = new Stack<T>();
|
|
194
213
|
let index = 0;
|
|
195
214
|
for (const el of this) {
|
|
196
|
-
newStack.push(callback(el, index, this));
|
|
215
|
+
newStack.push(callback.call(thisArg, el, index, this));
|
|
197
216
|
index++;
|
|
198
217
|
}
|
|
199
218
|
return newStack;
|
|
200
219
|
}
|
|
201
220
|
|
|
202
|
-
reduce<T>(callback: (accumulator: T, element: E, index: number, stack: this) => T, initialValue: T): T {
|
|
203
|
-
let accumulator = initialValue;
|
|
204
|
-
let index = 0;
|
|
205
|
-
for (const el of this) {
|
|
206
|
-
accumulator = callback(accumulator, el, index, this);
|
|
207
|
-
index++;
|
|
208
|
-
}
|
|
209
|
-
return accumulator;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
221
|
print(): void {
|
|
213
222
|
console.log([...this]);
|
|
214
223
|
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Custom iterator for the Stack class.
|
|
227
|
+
* @returns An iterator object.
|
|
228
|
+
*/
|
|
229
|
+
protected* _getIterator() {
|
|
230
|
+
for (let i = 0; i < this.elements.length; i++) {
|
|
231
|
+
yield this.elements[i];
|
|
232
|
+
}
|
|
233
|
+
}
|
|
215
234
|
}
|
|
@@ -6,6 +6,9 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
+
import { IterableElementBase } from "../base";
|
|
10
|
+
import { ElementCallback } from "../../types";
|
|
11
|
+
|
|
9
12
|
/**
|
|
10
13
|
* TrieNode represents a node in the Trie data structure. It holds a character key, a map of children nodes,
|
|
11
14
|
* and a flag indicating whether it's the end of a word.
|
|
@@ -25,8 +28,9 @@ export class TrieNode {
|
|
|
25
28
|
/**
|
|
26
29
|
* Trie represents a Trie data structure. It provides basic Trie operations and additional methods.
|
|
27
30
|
*/
|
|
28
|
-
export class Trie {
|
|
31
|
+
export class Trie extends IterableElementBase<string> {
|
|
29
32
|
constructor(words?: string[], caseSensitive = true) {
|
|
33
|
+
super();
|
|
30
34
|
this._root = new TrieNode('');
|
|
31
35
|
this._caseSensitive = caseSensitive;
|
|
32
36
|
this._size = 0;
|
|
@@ -339,32 +343,30 @@ export class Trie {
|
|
|
339
343
|
return words;
|
|
340
344
|
}
|
|
341
345
|
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
}
|
|
347
|
-
for (const [char, childNode] of node.children) {
|
|
348
|
-
yield* _dfs(childNode, path + char);
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
yield* _dfs(this.root, '');
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
forEach(callback: (word: string, index: number, trie: this) => void): void {
|
|
356
|
-
let index = 0;
|
|
357
|
-
for (const word of this) {
|
|
358
|
-
callback(word, index, this);
|
|
359
|
-
index++;
|
|
360
|
-
}
|
|
361
|
-
}
|
|
346
|
+
/**
|
|
347
|
+
* Time Complexity: O(n)
|
|
348
|
+
* Space Complexity: O(n)
|
|
349
|
+
*/
|
|
362
350
|
|
|
363
|
-
|
|
351
|
+
/**
|
|
352
|
+
* Time Complexity: O(n)
|
|
353
|
+
* Space Complexity: O(n)
|
|
354
|
+
*
|
|
355
|
+
* The `filter` function takes a predicate function and returns a new array containing all the
|
|
356
|
+
* elements for which the predicate function returns true.
|
|
357
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
358
|
+
* `word`, `index`, and `this`. It should return a boolean value indicating whether the current
|
|
359
|
+
* element should be included in the filtered results or not.
|
|
360
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
361
|
+
* specify the value of `this` within the `predicate` function. It is used when you want to bind a
|
|
362
|
+
* specific object as the context for the `predicate` function. If `thisArg` is provided, it will be
|
|
363
|
+
* @returns The `filter` method is returning an array of strings (`string[]`).
|
|
364
|
+
*/
|
|
365
|
+
filter(predicate: ElementCallback<string, boolean>, thisArg?: any): string[] {
|
|
364
366
|
const results: string[] = [];
|
|
365
367
|
let index = 0;
|
|
366
368
|
for (const word of this) {
|
|
367
|
-
if (predicate(word, index, this)) {
|
|
369
|
+
if (predicate.call(thisArg, word, index, this)) {
|
|
368
370
|
results.push(word);
|
|
369
371
|
}
|
|
370
372
|
index++;
|
|
@@ -372,30 +374,51 @@ export class Trie {
|
|
|
372
374
|
return results;
|
|
373
375
|
}
|
|
374
376
|
|
|
375
|
-
|
|
377
|
+
/**
|
|
378
|
+
* Time Complexity: O(n)
|
|
379
|
+
* Space Complexity: O(n)
|
|
380
|
+
*/
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* Time Complexity: O(n)
|
|
384
|
+
* Space Complexity: O(n)
|
|
385
|
+
*
|
|
386
|
+
* The `map` function creates a new Trie by applying a callback function to each element in the Trie.
|
|
387
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
388
|
+
* Trie. It takes three arguments: the current element in the Trie, the index of the current element,
|
|
389
|
+
* and the Trie itself. The callback function should return a new value for the element.
|
|
390
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
391
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
392
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
393
|
+
* @returns The `map` function is returning a new Trie object.
|
|
394
|
+
*/
|
|
395
|
+
map(callback: ElementCallback<string, string>, thisArg?: any): Trie {
|
|
376
396
|
const newTrie = new Trie();
|
|
377
397
|
let index = 0;
|
|
378
398
|
for (const word of this) {
|
|
379
|
-
newTrie.add(callback(word, index, this));
|
|
399
|
+
newTrie.add(callback.call(thisArg, word, index, this));
|
|
380
400
|
index++;
|
|
381
401
|
}
|
|
382
402
|
return newTrie;
|
|
383
403
|
}
|
|
384
404
|
|
|
385
|
-
reduce<T>(callback: (accumulator: T, word: string, index: number, trie: this) => T, initialValue: T): T {
|
|
386
|
-
let accumulator = initialValue;
|
|
387
|
-
let index = 0;
|
|
388
|
-
for (const word of this) {
|
|
389
|
-
accumulator = callback(accumulator, word, index, this);
|
|
390
|
-
index++;
|
|
391
|
-
}
|
|
392
|
-
return accumulator;
|
|
393
|
-
}
|
|
394
|
-
|
|
395
405
|
print() {
|
|
396
406
|
console.log([...this]);
|
|
397
407
|
}
|
|
398
408
|
|
|
409
|
+
protected* _getIterator(): IterableIterator<string> {
|
|
410
|
+
function* _dfs(node: TrieNode, path: string): IterableIterator<string> {
|
|
411
|
+
if (node.isEnd) {
|
|
412
|
+
yield path;
|
|
413
|
+
}
|
|
414
|
+
for (const [char, childNode] of node.children) {
|
|
415
|
+
yield* _dfs(childNode, path + char);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
yield* _dfs(this.root, '');
|
|
420
|
+
}
|
|
421
|
+
|
|
399
422
|
/**
|
|
400
423
|
* Time Complexity: O(M), where M is the length of the input string.
|
|
401
424
|
* Space Complexity: O(1) - Constant space.
|