graph-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.
- package/dist/data-structures/binary-tree/avl-tree.d.ts +8 -8
- package/dist/data-structures/binary-tree/avl-tree.js +23 -15
- package/dist/data-structures/binary-tree/binary-tree.d.ts +65 -28
- package/dist/data-structures/binary-tree/binary-tree.js +66 -82
- package/dist/data-structures/binary-tree/bst.d.ts +38 -37
- package/dist/data-structures/binary-tree/bst.js +56 -40
- package/dist/data-structures/binary-tree/rb-tree.d.ts +11 -7
- package/dist/data-structures/binary-tree/rb-tree.js +26 -17
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +16 -16
- package/dist/data-structures/binary-tree/tree-multimap.js +31 -22
- package/dist/data-structures/graph/abstract-graph.js +1 -1
- 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 +25 -22
- package/dist/data-structures/heap/heap.js +101 -41
- package/dist/data-structures/heap/max-heap.d.ts +2 -5
- package/dist/data-structures/heap/max-heap.js +2 -2
- package/dist/data-structures/heap/min-heap.d.ts +2 -5
- package/dist/data-structures/heap/min-heap.js +2 -2
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +58 -57
- package/dist/data-structures/linked-list/doubly-linked-list.js +125 -119
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +38 -37
- package/dist/data-structures/linked-list/singly-linked-list.js +65 -60
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
- package/dist/data-structures/priority-queue/max-priority-queue.js +2 -2
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
- package/dist/data-structures/priority-queue/min-priority-queue.js +2 -2
- package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -5
- package/dist/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/data-structures/queue/deque.d.ts +50 -49
- package/dist/data-structures/queue/deque.js +81 -71
- package/dist/data-structures/queue/queue.d.ts +46 -0
- package/dist/data-structures/queue/queue.js +80 -0
- package/dist/data-structures/stack/stack.d.ts +20 -6
- package/dist/data-structures/stack/stack.js +65 -8
- package/dist/data-structures/trie/trie.d.ts +5 -0
- package/dist/data-structures/trie/trie.js +47 -0
- package/dist/interfaces/binary-tree.d.ts +3 -1
- package/dist/types/common.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/types/data-structures/heap/heap.d.ts +4 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +27 -17
- package/src/data-structures/binary-tree/binary-tree.ts +114 -97
- package/src/data-structures/binary-tree/bst.ts +67 -47
- package/src/data-structures/binary-tree/rb-tree.ts +34 -20
- package/src/data-structures/binary-tree/tree-multimap.ts +43 -25
- package/src/data-structures/graph/abstract-graph.ts +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 +115 -46
- package/src/data-structures/heap/max-heap.ts +5 -5
- package/src/data-structures/heap/min-heap.ts +5 -5
- package/src/data-structures/linked-list/doubly-linked-list.ts +137 -128
- package/src/data-structures/linked-list/singly-linked-list.ts +72 -64
- package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +12 -12
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/src/data-structures/queue/deque.ts +86 -75
- package/src/data-structures/queue/queue.ts +88 -0
- package/src/data-structures/stack/stack.ts +75 -10
- package/src/data-structures/trie/trie.ts +53 -0
- package/src/interfaces/binary-tree.ts +13 -1
- package/src/types/common.ts +5 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +2 -3
- package/src/types/data-structures/heap/heap.ts +3 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +3 -1
|
@@ -637,15 +637,21 @@ export class Deque<E> {
|
|
|
637
637
|
* Time Complexity: O(n)
|
|
638
638
|
* Space Complexity: O(1)
|
|
639
639
|
*
|
|
640
|
-
* The `
|
|
641
|
-
*
|
|
642
|
-
* @param callback -
|
|
643
|
-
*
|
|
640
|
+
* The `find` function iterates over the elements in a deque and returns the first element for which
|
|
641
|
+
* the callback function returns true, or undefined if no such element is found.
|
|
642
|
+
* @param callback - A function that takes three parameters: element, index, and deque. It should
|
|
643
|
+
* return a boolean value indicating whether the element satisfies a certain condition.
|
|
644
|
+
* @returns The method `find` returns the first element in the deque that satisfies the condition
|
|
645
|
+
* specified by the callback function. If no element satisfies the condition, it returns `undefined`.
|
|
644
646
|
*/
|
|
645
|
-
|
|
647
|
+
find(callback: (element: E, index: number, deque: Deque<E>) => boolean): E | undefined {
|
|
646
648
|
for (let i = 0; i < this.size; ++i) {
|
|
647
|
-
|
|
649
|
+
const element = this.getAt(i);
|
|
650
|
+
if (callback(element, i, this)) {
|
|
651
|
+
return element;
|
|
652
|
+
}
|
|
648
653
|
}
|
|
654
|
+
return undefined;
|
|
649
655
|
}
|
|
650
656
|
|
|
651
657
|
/**
|
|
@@ -657,21 +663,20 @@ export class Deque<E> {
|
|
|
657
663
|
* Time Complexity: O(n)
|
|
658
664
|
* Space Complexity: O(1)
|
|
659
665
|
*
|
|
660
|
-
* The
|
|
661
|
-
*
|
|
662
|
-
* @param
|
|
663
|
-
*
|
|
664
|
-
* @returns The
|
|
665
|
-
*
|
|
666
|
+
* The function "indexOf" returns the index of the first occurrence of a given element in an array,
|
|
667
|
+
* or -1 if the element is not found.
|
|
668
|
+
* @param {E} element - The "element" parameter represents the element that you want to find the
|
|
669
|
+
* index of in the data structure.
|
|
670
|
+
* @returns The indexOf function returns the index of the first occurrence of the specified element
|
|
671
|
+
* in the data structure. If the element is not found, it returns -1.
|
|
666
672
|
*/
|
|
667
|
-
|
|
673
|
+
indexOf(element: E): number {
|
|
668
674
|
for (let i = 0; i < this.size; ++i) {
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
return element;
|
|
675
|
+
if (this.getAt(i) === element) {
|
|
676
|
+
return i;
|
|
672
677
|
}
|
|
673
678
|
}
|
|
674
|
-
return
|
|
679
|
+
return -1;
|
|
675
680
|
}
|
|
676
681
|
|
|
677
682
|
/**
|
|
@@ -696,24 +701,42 @@ export class Deque<E> {
|
|
|
696
701
|
|
|
697
702
|
/**
|
|
698
703
|
* Time Complexity: O(n)
|
|
699
|
-
* Space Complexity: O(
|
|
704
|
+
* Space Complexity: O(1)
|
|
700
705
|
*/
|
|
701
706
|
|
|
702
707
|
/**
|
|
703
708
|
* Time Complexity: O(n)
|
|
704
|
-
* Space Complexity: O(
|
|
709
|
+
* Space Complexity: O(1)
|
|
705
710
|
*
|
|
706
|
-
* The
|
|
707
|
-
*
|
|
708
|
-
* @param callback - The `callback` parameter is a function that takes three arguments:
|
|
709
|
-
* @returns The `map` method is returning a new `Deque` object with the transformed elements.
|
|
711
|
+
* The above function is an implementation of the iterator protocol in TypeScript, allowing the
|
|
712
|
+
* object to be iterated over using a for...of loop.
|
|
710
713
|
*/
|
|
711
|
-
|
|
712
|
-
const newDeque = new Deque<T>([], this._bucketSize);
|
|
714
|
+
* [Symbol.iterator]() {
|
|
713
715
|
for (let i = 0; i < this.size; ++i) {
|
|
714
|
-
|
|
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++;
|
|
715
739
|
}
|
|
716
|
-
return newDeque;
|
|
717
740
|
}
|
|
718
741
|
|
|
719
742
|
/**
|
|
@@ -732,41 +755,40 @@ export class Deque<E> {
|
|
|
732
755
|
* @returns The `filter` method is returning a new `Deque` object that contains only the elements
|
|
733
756
|
* that satisfy the given `predicate` function.
|
|
734
757
|
*/
|
|
735
|
-
filter(predicate: (element: E, index: number, deque:
|
|
758
|
+
filter(predicate: (element: E, index: number, deque: this) => boolean): Deque<E> {
|
|
736
759
|
const newDeque = new Deque<E>([], this._bucketSize);
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
if (predicate(
|
|
740
|
-
newDeque.push(
|
|
760
|
+
let index = 0;
|
|
761
|
+
for (const el of this) {
|
|
762
|
+
if (predicate(el, index, this)) {
|
|
763
|
+
newDeque.push(el);
|
|
741
764
|
}
|
|
765
|
+
index++;
|
|
742
766
|
}
|
|
743
767
|
return newDeque;
|
|
744
768
|
}
|
|
745
769
|
|
|
746
770
|
/**
|
|
747
771
|
* Time Complexity: O(n)
|
|
748
|
-
* Space Complexity: O(
|
|
772
|
+
* Space Complexity: O(n)
|
|
749
773
|
*/
|
|
750
774
|
|
|
751
775
|
/**
|
|
752
776
|
* Time Complexity: O(n)
|
|
753
|
-
* Space Complexity: O(
|
|
777
|
+
* Space Complexity: O(n)
|
|
754
778
|
*
|
|
755
|
-
* The `
|
|
756
|
-
*
|
|
757
|
-
* @param callback - The `callback` parameter is a function that takes
|
|
758
|
-
* @
|
|
759
|
-
* is the value that will be passed as the first argument to the `callback` function when reducing
|
|
760
|
-
* the elements of the deque.
|
|
761
|
-
* @returns the final value of the accumulator after iterating over all elements in the deque and
|
|
762
|
-
* applying the callback function to each element.
|
|
779
|
+
* The `map` function takes a callback function and applies it to each element in the deque,
|
|
780
|
+
* returning a new deque with the results.
|
|
781
|
+
* @param callback - The `callback` parameter is a function that takes three arguments:
|
|
782
|
+
* @returns The `map` method is returning a new `Deque` object with the transformed elements.
|
|
763
783
|
*/
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
784
|
+
map<T>(callback: (element: E, index: number, deque: this) => T): Deque<T> {
|
|
785
|
+
const newDeque = new Deque<T>([], this._bucketSize);
|
|
786
|
+
let index = 0;
|
|
787
|
+
for (const el of this) {
|
|
788
|
+
newDeque.push(callback(el, index, this));
|
|
789
|
+
index++;
|
|
768
790
|
}
|
|
769
|
-
return
|
|
791
|
+
return newDeque;
|
|
770
792
|
}
|
|
771
793
|
|
|
772
794
|
/**
|
|
@@ -778,38 +800,27 @@ export class Deque<E> {
|
|
|
778
800
|
* Time Complexity: O(n)
|
|
779
801
|
* Space Complexity: O(1)
|
|
780
802
|
*
|
|
781
|
-
* The function
|
|
782
|
-
*
|
|
783
|
-
* @param
|
|
784
|
-
*
|
|
785
|
-
*
|
|
786
|
-
*
|
|
803
|
+
* The `reduce` function iterates over the elements of a deque and applies a callback function to
|
|
804
|
+
* each element, accumulating a single value.
|
|
805
|
+
* @param callback - The `callback` parameter is a function that takes four arguments:
|
|
806
|
+
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
|
|
807
|
+
* is the value that will be passed as the first argument to the `callback` function when reducing
|
|
808
|
+
* the elements of the deque.
|
|
809
|
+
* @returns the final value of the accumulator after iterating over all elements in the deque and
|
|
810
|
+
* applying the callback function to each element.
|
|
787
811
|
*/
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
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++;
|
|
793
818
|
}
|
|
794
|
-
return
|
|
819
|
+
return accumulator;
|
|
795
820
|
}
|
|
796
821
|
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
* Space Complexity: O(1)
|
|
800
|
-
*/
|
|
801
|
-
|
|
802
|
-
/**
|
|
803
|
-
* Time Complexity: O(n)
|
|
804
|
-
* Space Complexity: O(1)
|
|
805
|
-
*
|
|
806
|
-
* The above function is an implementation of the iterator protocol in TypeScript, allowing the
|
|
807
|
-
* object to be iterated over using a for...of loop.
|
|
808
|
-
*/
|
|
809
|
-
* [Symbol.iterator]() {
|
|
810
|
-
for (let i = 0; i < this.size; ++i) {
|
|
811
|
-
yield this.getAt(i);
|
|
812
|
-
}
|
|
822
|
+
print(): void {
|
|
823
|
+
console.log([...this])
|
|
813
824
|
}
|
|
814
825
|
|
|
815
826
|
/**
|
|
@@ -300,9 +300,97 @@ export class Queue<E = any> {
|
|
|
300
300
|
return new Queue(this.nodes.slice(this.offset));
|
|
301
301
|
}
|
|
302
302
|
|
|
303
|
+
print(): void {
|
|
304
|
+
console.log([...this]);
|
|
305
|
+
}
|
|
306
|
+
|
|
303
307
|
* [Symbol.iterator]() {
|
|
304
308
|
for (const item of this.nodes) {
|
|
305
309
|
yield item;
|
|
306
310
|
}
|
|
307
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
|
+
/**
|
|
336
|
+
* Time Complexity: O(n)
|
|
337
|
+
* Space Complexity: O(n)
|
|
338
|
+
*/
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Time Complexity: O(n)
|
|
342
|
+
* Space Complexity: O(n)
|
|
343
|
+
*
|
|
344
|
+
* The `filter` function creates a new deque containing only the elements that satisfy the given
|
|
345
|
+
* predicate function.
|
|
346
|
+
* @param predicate - The `predicate` parameter is a function that takes three arguments: `element`,
|
|
347
|
+
* `index`, and `deque`.
|
|
348
|
+
* @returns The `filter` method is returning a new `Queue` object that contains only the elements
|
|
349
|
+
* that satisfy the given `predicate` function.
|
|
350
|
+
*/
|
|
351
|
+
filter(predicate: (element: E, index: number, queue: this) => boolean): Queue<E> {
|
|
352
|
+
const newDeque = new Queue<E>([]);
|
|
353
|
+
let index = 0;
|
|
354
|
+
for (const el of this) {
|
|
355
|
+
if (predicate(el, index, this)) {
|
|
356
|
+
newDeque.push(el);
|
|
357
|
+
}
|
|
358
|
+
index++;
|
|
359
|
+
}
|
|
360
|
+
return newDeque;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Time Complexity: O(n)
|
|
365
|
+
* Space Complexity: O(n)
|
|
366
|
+
*/
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* Time Complexity: O(n)
|
|
370
|
+
* Space Complexity: O(n)
|
|
371
|
+
*
|
|
372
|
+
* The `map` function takes a callback function and applies it to each element in the deque,
|
|
373
|
+
* returning a new deque with the results.
|
|
374
|
+
* @param callback - The `callback` parameter is a function that takes three arguments:
|
|
375
|
+
* @returns The `map` method is returning a new `Queue` object with the transformed elements.
|
|
376
|
+
*/
|
|
377
|
+
map<T>(callback: (element: E, index: number, queue: this) => T): Queue<T> {
|
|
378
|
+
const newDeque = new Queue<T>([]);
|
|
379
|
+
let index = 0;
|
|
380
|
+
for (const el of this) {
|
|
381
|
+
newDeque.push(callback(el, index, this));
|
|
382
|
+
index++;
|
|
383
|
+
}
|
|
384
|
+
return newDeque;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
reduce<T>(callback: (accumulator: T, element: E, index: number, queue: this) => T, initialValue: T): T {
|
|
388
|
+
let accumulator = initialValue;
|
|
389
|
+
let index = 0;
|
|
390
|
+
for (const el of this) {
|
|
391
|
+
accumulator = callback(accumulator, el, index, this);
|
|
392
|
+
index++;
|
|
393
|
+
}
|
|
394
|
+
return accumulator;
|
|
395
|
+
}
|
|
308
396
|
}
|
|
@@ -10,8 +10,13 @@ export class Stack<E = any> {
|
|
|
10
10
|
* of elements of type `E`. It is used to initialize the `_elements` property of the class. If the `elements` parameter
|
|
11
11
|
* is provided and is an array, it is assigned to the `_elements
|
|
12
12
|
*/
|
|
13
|
-
constructor(elements?: E
|
|
14
|
-
this._elements =
|
|
13
|
+
constructor(elements?: Iterable<E>) {
|
|
14
|
+
this._elements = [];
|
|
15
|
+
if (elements) {
|
|
16
|
+
for (const el of elements) {
|
|
17
|
+
this.push(el);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
15
20
|
}
|
|
16
21
|
|
|
17
22
|
protected _elements: E[];
|
|
@@ -25,6 +30,14 @@ export class Stack<E = any> {
|
|
|
25
30
|
* Space Complexity: O(n), as it creates a new stack with the elements from the input array.
|
|
26
31
|
*/
|
|
27
32
|
|
|
33
|
+
/**
|
|
34
|
+
* The size() function returns the number of elements in an array.
|
|
35
|
+
* @returns The size of the elements array.
|
|
36
|
+
*/
|
|
37
|
+
get size(): number {
|
|
38
|
+
return this.elements.length;
|
|
39
|
+
}
|
|
40
|
+
|
|
28
41
|
/**
|
|
29
42
|
* Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element.
|
|
30
43
|
* Space Complexity: O(n), as it creates a new stack with the elements from the input array.
|
|
@@ -46,14 +59,6 @@ export class Stack<E = any> {
|
|
|
46
59
|
return this.elements.length === 0;
|
|
47
60
|
}
|
|
48
61
|
|
|
49
|
-
/**
|
|
50
|
-
* The size() function returns the number of elements in an array.
|
|
51
|
-
* @returns The size of the elements array.
|
|
52
|
-
*/
|
|
53
|
-
size(): number {
|
|
54
|
-
return this.elements.length;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
62
|
/**
|
|
58
63
|
* Time Complexity: O(1), as it only involves accessing the last element of the array.
|
|
59
64
|
* Space Complexity: O(1), as it does not use any additional space.
|
|
@@ -147,4 +152,64 @@ export class Stack<E = any> {
|
|
|
147
152
|
clone(): Stack<E> {
|
|
148
153
|
return new Stack(this.elements.slice());
|
|
149
154
|
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Custom iterator for the Stack class.
|
|
158
|
+
* @returns An iterator object.
|
|
159
|
+
*/
|
|
160
|
+
* [Symbol.iterator]() {
|
|
161
|
+
for (let i = 0; i < this.elements.length; i++) {
|
|
162
|
+
yield this.elements[i];
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Applies a function to each element of the stack.
|
|
168
|
+
* @param {function(E): void} callback - A function to apply to each element.
|
|
169
|
+
*/
|
|
170
|
+
forEach(callback: (element: E, index: number, stack: this) => void): void {
|
|
171
|
+
let index = 0;
|
|
172
|
+
for (const el of this) {
|
|
173
|
+
callback(el, index, this);
|
|
174
|
+
index++;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
filter(predicate: (element: E, index: number, stack: this) => boolean): Stack<E> {
|
|
180
|
+
const newStack = new Stack<E>();
|
|
181
|
+
let index = 0;
|
|
182
|
+
for (const el of this) {
|
|
183
|
+
if (predicate(el, index, this)) {
|
|
184
|
+
newStack.push(el);
|
|
185
|
+
}
|
|
186
|
+
index++;
|
|
187
|
+
}
|
|
188
|
+
return newStack;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
map<T>(callback: (element: E, index: number, stack: this) => T): Stack<T> {
|
|
193
|
+
const newStack = new Stack<T>();
|
|
194
|
+
let index = 0;
|
|
195
|
+
for (const el of this) {
|
|
196
|
+
newStack.push(callback(el, index, this));
|
|
197
|
+
index++;
|
|
198
|
+
}
|
|
199
|
+
return newStack;
|
|
200
|
+
}
|
|
201
|
+
|
|
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
|
+
print(): void {
|
|
213
|
+
console.log([...this]);
|
|
214
|
+
}
|
|
150
215
|
}
|
|
@@ -324,6 +324,59 @@ export class Trie {
|
|
|
324
324
|
return words;
|
|
325
325
|
}
|
|
326
326
|
|
|
327
|
+
* [Symbol.iterator](): IterableIterator<string> {
|
|
328
|
+
function* _dfs(node: TrieNode, path: string): IterableIterator<string> {
|
|
329
|
+
if (node.isEnd) {
|
|
330
|
+
yield path;
|
|
331
|
+
}
|
|
332
|
+
for (const [char, childNode] of node.children) {
|
|
333
|
+
yield* _dfs(childNode, path + char);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
yield* _dfs(this.root, '');
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
forEach(callback: (word: string, index: number, trie: this) => void): void {
|
|
341
|
+
let index = 0;
|
|
342
|
+
for (const word of this) {
|
|
343
|
+
callback(word, index, this);
|
|
344
|
+
index++;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
filter(predicate: (word: string, index: number, trie: this) => boolean): string[] {
|
|
349
|
+
const results: string[] = [];
|
|
350
|
+
let index = 0;
|
|
351
|
+
for (const word of this) {
|
|
352
|
+
if (predicate(word, index, this)) {
|
|
353
|
+
results.push(word);
|
|
354
|
+
}
|
|
355
|
+
index++;
|
|
356
|
+
}
|
|
357
|
+
return results;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
map(callback: (word: string, index: number, trie: this) => string): Trie {
|
|
361
|
+
const newTrie = new Trie();
|
|
362
|
+
let index = 0;
|
|
363
|
+
for (const word of this) {
|
|
364
|
+
newTrie.add(callback(word, index, this));
|
|
365
|
+
index++;
|
|
366
|
+
}
|
|
367
|
+
return newTrie;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
reduce<T>(callback: (accumulator: T, word: string, index: number, trie: this) => T, initialValue: T): T {
|
|
371
|
+
let accumulator = initialValue;
|
|
372
|
+
let index = 0;
|
|
373
|
+
for (const word of this) {
|
|
374
|
+
accumulator = callback(accumulator, word, index, this);
|
|
375
|
+
index++;
|
|
376
|
+
}
|
|
377
|
+
return accumulator;
|
|
378
|
+
}
|
|
379
|
+
|
|
327
380
|
/**
|
|
328
381
|
* Time Complexity: O(M), where M is the length of the input string.
|
|
329
382
|
* Space Complexity: O(1) - Constant space.
|
|
@@ -1,9 +1,21 @@
|
|
|
1
1
|
import { BinaryTree, BinaryTreeNode } from '../data-structures';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
BinaryTreeNested,
|
|
4
|
+
BinaryTreeNodeNested,
|
|
5
|
+
BinaryTreeOptions,
|
|
6
|
+
BiTreeDeleteResult,
|
|
7
|
+
BTNCallback,
|
|
8
|
+
BTNKey,
|
|
9
|
+
IterableEntriesOrKeys
|
|
10
|
+
} from '../types';
|
|
3
11
|
|
|
4
12
|
export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>, TREE extends BinaryTree<V, N, TREE> = BinaryTreeNested<V, N>> {
|
|
5
13
|
createNode(key: BTNKey, value?: N['value']): N;
|
|
6
14
|
|
|
15
|
+
createTree(options?: Partial<BinaryTreeOptions>): TREE;
|
|
16
|
+
|
|
17
|
+
init(elements: IterableEntriesOrKeys<V>): void;
|
|
18
|
+
|
|
7
19
|
add(keyOrNode: BTNKey | N | null, value?: N['value']): N | null | undefined;
|
|
8
20
|
|
|
9
21
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BiTreeDeleteResult<N>[];
|
package/src/types/common.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { BTNKey } from "./data-structures";
|
|
2
|
+
|
|
1
3
|
export type Comparator<T> = (a: T, b: T) => number;
|
|
2
4
|
|
|
3
5
|
export type DFSOrderPattern = 'pre' | 'in' | 'post';
|
|
@@ -20,4 +22,6 @@ export interface IterableWithLength<T> extends Iterable<T> {
|
|
|
20
22
|
|
|
21
23
|
export type IterableWithSizeOrLength<T> = IterableWithSize<T> | IterableWithLength<T>
|
|
22
24
|
|
|
23
|
-
export type BinaryTreePrintOptions = { isShowUndefined?: boolean, isShowNull?: boolean, isShowRedBlackNIL?: boolean }
|
|
25
|
+
export type BinaryTreePrintOptions = { isShowUndefined?: boolean, isShowNull?: boolean, isShowRedBlackNIL?: boolean }
|
|
26
|
+
|
|
27
|
+
export type IterableEntriesOrKeys<T> = Iterable<[BTNKey, T | undefined] | BTNKey>
|
|
@@ -30,6 +30,6 @@ export type BinaryTreeNodeNested<T> = BinaryTreeNode<T, BinaryTreeNode<T, Binary
|
|
|
30
30
|
|
|
31
31
|
export type BinaryTreeNested<T, N extends BinaryTreeNode<T, N>> = BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
32
32
|
|
|
33
|
-
export type BinaryTreeOptions = { iterationType
|
|
33
|
+
export type BinaryTreeOptions = { iterationType: IterationType }
|
|
34
34
|
|
|
35
35
|
export type NodeDisplayLayout = [string[], number, number, number];
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { BST, BSTNode } from '../../../data-structures';
|
|
2
2
|
import type { BinaryTreeOptions, BTNKey } from './binary-tree';
|
|
3
|
-
|
|
4
|
-
export type BSTComparator = (a: BTNKey, b: BTNKey) => number;
|
|
3
|
+
import { Comparator } from "../../common";
|
|
5
4
|
|
|
6
5
|
// prettier-ignore
|
|
7
6
|
export type BSTNodeNested<T> = BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
@@ -9,5 +8,5 @@ export type BSTNodeNested<T> = BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNo
|
|
|
9
8
|
export type BSTNested<T, N extends BSTNode<T, N>> = BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
10
9
|
|
|
11
10
|
export type BSTOptions = BinaryTreeOptions & {
|
|
12
|
-
comparator
|
|
11
|
+
comparator: Comparator<BTNKey>
|
|
13
12
|
}
|