undirected-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
|
@@ -26,10 +26,15 @@ export class DoublyLinkedList<E = any> {
|
|
|
26
26
|
/**
|
|
27
27
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
28
28
|
*/
|
|
29
|
-
constructor() {
|
|
29
|
+
constructor(elements?: Iterable<E>) {
|
|
30
30
|
this._head = null;
|
|
31
31
|
this._tail = null;
|
|
32
32
|
this._length = 0;
|
|
33
|
+
if (elements) {
|
|
34
|
+
for (const el of elements) {
|
|
35
|
+
this.push(el);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
33
38
|
}
|
|
34
39
|
|
|
35
40
|
protected _head: DoublyLinkedListNode<E> | null;
|
|
@@ -441,6 +446,50 @@ export class DoublyLinkedList<E = any> {
|
|
|
441
446
|
return false;
|
|
442
447
|
}
|
|
443
448
|
|
|
449
|
+
/**
|
|
450
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
451
|
+
* Space Complexity: O(1)
|
|
452
|
+
*/
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
456
|
+
* Space Complexity: O(1)
|
|
457
|
+
*
|
|
458
|
+
* The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
|
|
459
|
+
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
460
|
+
* after which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
461
|
+
* itself.
|
|
462
|
+
* @param {E} newValue - The value that you want to insert into the doubly linked list.
|
|
463
|
+
* @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the
|
|
464
|
+
* existing value or node is not found in the doubly linked list.
|
|
465
|
+
*/
|
|
466
|
+
insertAfter(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean {
|
|
467
|
+
let existingNode;
|
|
468
|
+
|
|
469
|
+
if (existingValueOrNode instanceof DoublyLinkedListNode) {
|
|
470
|
+
existingNode = existingValueOrNode;
|
|
471
|
+
} else {
|
|
472
|
+
existingNode = this.getNode(existingValueOrNode);
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
if (existingNode) {
|
|
476
|
+
const newNode = new DoublyLinkedListNode(newValue);
|
|
477
|
+
newNode.next = existingNode.next;
|
|
478
|
+
if (existingNode.next) {
|
|
479
|
+
existingNode.next.prev = newNode;
|
|
480
|
+
}
|
|
481
|
+
newNode.prev = existingNode;
|
|
482
|
+
existingNode.next = newNode;
|
|
483
|
+
if (existingNode === this.tail) {
|
|
484
|
+
this._tail = newNode;
|
|
485
|
+
}
|
|
486
|
+
this._length++;
|
|
487
|
+
return true;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
return false;
|
|
491
|
+
}
|
|
492
|
+
|
|
444
493
|
/**
|
|
445
494
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
446
495
|
* Space Complexity: O(1)
|
|
@@ -511,28 +560,6 @@ export class DoublyLinkedList<E = any> {
|
|
|
511
560
|
return false;
|
|
512
561
|
}
|
|
513
562
|
|
|
514
|
-
/**
|
|
515
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
516
|
-
* Space Complexity: O(n)
|
|
517
|
-
*/
|
|
518
|
-
|
|
519
|
-
/**
|
|
520
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
521
|
-
* Space Complexity: O(n)
|
|
522
|
-
*
|
|
523
|
-
* The `toArray` function converts a linked list into an array.
|
|
524
|
-
* @returns The `toArray()` method is returning an array of type `E[]`.
|
|
525
|
-
*/
|
|
526
|
-
toArray(): E[] {
|
|
527
|
-
const array: E[] = [];
|
|
528
|
-
let current = this.head;
|
|
529
|
-
while (current) {
|
|
530
|
-
array.push(current.value);
|
|
531
|
-
current = current.next;
|
|
532
|
-
}
|
|
533
|
-
return array;
|
|
534
|
-
}
|
|
535
|
-
|
|
536
563
|
/**
|
|
537
564
|
* The function checks if a variable has a length greater than zero and returns a boolean value.
|
|
538
565
|
* @returns A boolean value is being returned.
|
|
@@ -631,28 +658,6 @@ export class DoublyLinkedList<E = any> {
|
|
|
631
658
|
return null;
|
|
632
659
|
}
|
|
633
660
|
|
|
634
|
-
/**
|
|
635
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
636
|
-
* Space Complexity: O(n)
|
|
637
|
-
*/
|
|
638
|
-
|
|
639
|
-
/**
|
|
640
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
641
|
-
* Space Complexity: O(n)
|
|
642
|
-
*
|
|
643
|
-
* The `toArrayBackward` function converts a doubly linked list into an array in reverse order.
|
|
644
|
-
* @returns The `toArrayBackward()` function returns an array of type `E[]`.
|
|
645
|
-
*/
|
|
646
|
-
toArrayBackward(): E[] {
|
|
647
|
-
const array: E[] = [];
|
|
648
|
-
let current = this.tail;
|
|
649
|
-
while (current) {
|
|
650
|
-
array.push(current.value);
|
|
651
|
-
current = current.prev;
|
|
652
|
-
}
|
|
653
|
-
return array;
|
|
654
|
-
}
|
|
655
|
-
|
|
656
661
|
/**
|
|
657
662
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
658
663
|
* Space Complexity: O(1)
|
|
@@ -676,26 +681,24 @@ export class DoublyLinkedList<E = any> {
|
|
|
676
681
|
|
|
677
682
|
/**
|
|
678
683
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
679
|
-
* Space Complexity: O(
|
|
684
|
+
* Space Complexity: O(n)
|
|
680
685
|
*/
|
|
681
686
|
|
|
682
687
|
/**
|
|
683
688
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
684
|
-
* Space Complexity: O(
|
|
689
|
+
* Space Complexity: O(n)
|
|
685
690
|
*
|
|
686
|
-
* The `
|
|
687
|
-
* @
|
|
688
|
-
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
689
|
-
* current node in the linked list.
|
|
691
|
+
* The `toArray` function converts a linked list into an array.
|
|
692
|
+
* @returns The `toArray()` method is returning an array of type `E[]`.
|
|
690
693
|
*/
|
|
691
|
-
|
|
694
|
+
toArray(): E[] {
|
|
695
|
+
const array: E[] = [];
|
|
692
696
|
let current = this.head;
|
|
693
|
-
let index = 0;
|
|
694
697
|
while (current) {
|
|
695
|
-
|
|
698
|
+
array.push(current.value);
|
|
696
699
|
current = current.next;
|
|
697
|
-
index++;
|
|
698
700
|
}
|
|
701
|
+
return array;
|
|
699
702
|
}
|
|
700
703
|
|
|
701
704
|
/**
|
|
@@ -707,21 +710,51 @@ export class DoublyLinkedList<E = any> {
|
|
|
707
710
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
708
711
|
* Space Complexity: O(n)
|
|
709
712
|
*
|
|
710
|
-
* The `
|
|
711
|
-
*
|
|
712
|
-
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
|
|
713
|
-
* the original DoublyLinkedList) and returns a value of type U (the type of values that will be stored in the mapped
|
|
714
|
-
* DoublyLinkedList).
|
|
715
|
-
* @returns The `map` function is returning a new instance of `DoublyLinkedList<U>` that contains the mapped values.
|
|
713
|
+
* The `toReversedArray` function converts a doubly linked list into an array in reverse order.
|
|
714
|
+
* @returns The `toReversedArray()` function returns an array of type `E[]`.
|
|
716
715
|
*/
|
|
717
|
-
|
|
718
|
-
const
|
|
716
|
+
toReversedArray(): E[] {
|
|
717
|
+
const array: E[] = [];
|
|
718
|
+
let current = this.tail;
|
|
719
|
+
while (current) {
|
|
720
|
+
array.push(current.value);
|
|
721
|
+
current = current.prev;
|
|
722
|
+
}
|
|
723
|
+
return array;
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
/**
|
|
727
|
+
* The function returns an iterator that iterates over the values of a linked list.
|
|
728
|
+
*/
|
|
729
|
+
* [Symbol.iterator]() {
|
|
719
730
|
let current = this.head;
|
|
731
|
+
|
|
720
732
|
while (current) {
|
|
721
|
-
|
|
733
|
+
yield current.value;
|
|
722
734
|
current = current.next;
|
|
723
735
|
}
|
|
724
|
-
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
/**
|
|
739
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
740
|
+
* Space Complexity: O(1)
|
|
741
|
+
*/
|
|
742
|
+
|
|
743
|
+
/**
|
|
744
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
745
|
+
* Space Complexity: O(1)
|
|
746
|
+
*
|
|
747
|
+
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
748
|
+
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
|
|
749
|
+
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
750
|
+
* current node in the linked list.
|
|
751
|
+
*/
|
|
752
|
+
forEach(callback: (value: E, index: number, list: DoublyLinkedList<E>) => void): void {
|
|
753
|
+
let index = 0;
|
|
754
|
+
for (const el of this) {
|
|
755
|
+
callback(el, index, this);
|
|
756
|
+
index++;
|
|
757
|
+
}
|
|
725
758
|
}
|
|
726
759
|
|
|
727
760
|
/**
|
|
@@ -739,14 +772,14 @@ export class DoublyLinkedList<E = any> {
|
|
|
739
772
|
* It is used to determine whether a value should be included in the filtered list or not.
|
|
740
773
|
* @returns The filtered list, which is an instance of the DoublyLinkedList class.
|
|
741
774
|
*/
|
|
742
|
-
filter(callback: (value: E) => boolean): DoublyLinkedList<E> {
|
|
775
|
+
filter(callback: (value: E, index: number, list: DoublyLinkedList<E>) => boolean): DoublyLinkedList<E> {
|
|
743
776
|
const filteredList = new DoublyLinkedList<E>();
|
|
744
|
-
let
|
|
745
|
-
|
|
746
|
-
if (callback(current
|
|
747
|
-
filteredList.push(current
|
|
777
|
+
let index = 0;
|
|
778
|
+
for (const current of this) {
|
|
779
|
+
if (callback(current, index, this)) {
|
|
780
|
+
filteredList.push(current);
|
|
748
781
|
}
|
|
749
|
-
|
|
782
|
+
index++;
|
|
750
783
|
}
|
|
751
784
|
return filteredList;
|
|
752
785
|
}
|
|
@@ -760,78 +793,54 @@ export class DoublyLinkedList<E = any> {
|
|
|
760
793
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
761
794
|
* Space Complexity: O(n)
|
|
762
795
|
*
|
|
763
|
-
* The `
|
|
764
|
-
*
|
|
765
|
-
* @param callback - The
|
|
766
|
-
*
|
|
767
|
-
*
|
|
768
|
-
*
|
|
769
|
-
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
770
|
-
* elements in the linked list.
|
|
796
|
+
* The `map` function takes a callback function and applies it to each element in the DoublyLinkedList, returning a new
|
|
797
|
+
* DoublyLinkedList with the transformed values.
|
|
798
|
+
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
|
|
799
|
+
* the original DoublyLinkedList) and returns a value of type T (the type of values that will be stored in the mapped
|
|
800
|
+
* DoublyLinkedList).
|
|
801
|
+
* @returns The `map` function is returning a new instance of `DoublyLinkedList<T>` that contains the mapped values.
|
|
771
802
|
*/
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
let
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
803
|
+
map<T>(callback: (value: E, index: number, list: DoublyLinkedList<E>) => T): DoublyLinkedList<T> {
|
|
804
|
+
const mappedList = new DoublyLinkedList<T>();
|
|
805
|
+
let index = 0;
|
|
806
|
+
for (const current of this) {
|
|
807
|
+
mappedList.push(callback(current, index, this));
|
|
808
|
+
index++;
|
|
778
809
|
}
|
|
779
|
-
|
|
810
|
+
|
|
811
|
+
return mappedList;
|
|
780
812
|
}
|
|
781
813
|
|
|
782
814
|
/**
|
|
783
815
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
784
|
-
* Space Complexity: O(
|
|
816
|
+
* Space Complexity: O(n)
|
|
785
817
|
*/
|
|
786
818
|
|
|
787
819
|
/**
|
|
788
820
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
789
|
-
* Space Complexity: O(
|
|
821
|
+
* Space Complexity: O(n)
|
|
790
822
|
*
|
|
791
|
-
* The `
|
|
792
|
-
*
|
|
793
|
-
*
|
|
794
|
-
*
|
|
795
|
-
* @param {
|
|
796
|
-
*
|
|
797
|
-
*
|
|
823
|
+
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
824
|
+
* single value.
|
|
825
|
+
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
|
|
826
|
+
* used to perform a specific operation on each element of the linked list.
|
|
827
|
+
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
828
|
+
* point for the reduction operation.
|
|
829
|
+
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
830
|
+
* elements in the linked list.
|
|
798
831
|
*/
|
|
799
|
-
|
|
800
|
-
let
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
existingNode = this.getNode(existingValueOrNode);
|
|
806
|
-
}
|
|
807
|
-
|
|
808
|
-
if (existingNode) {
|
|
809
|
-
const newNode = new DoublyLinkedListNode(newValue);
|
|
810
|
-
newNode.next = existingNode.next;
|
|
811
|
-
if (existingNode.next) {
|
|
812
|
-
existingNode.next.prev = newNode;
|
|
813
|
-
}
|
|
814
|
-
newNode.prev = existingNode;
|
|
815
|
-
existingNode.next = newNode;
|
|
816
|
-
if (existingNode === this.tail) {
|
|
817
|
-
this._tail = newNode;
|
|
818
|
-
}
|
|
819
|
-
this._length++;
|
|
820
|
-
return true;
|
|
832
|
+
reduce<T>(callback: (accumulator: T, value: E, index: number, list: DoublyLinkedList<E>) => T, initialValue: T): T {
|
|
833
|
+
let accumulator = initialValue;
|
|
834
|
+
let index = 0;
|
|
835
|
+
for (const current of this) {
|
|
836
|
+
accumulator = callback(accumulator, current, index, this);
|
|
837
|
+
index++;
|
|
821
838
|
}
|
|
822
839
|
|
|
823
|
-
return
|
|
840
|
+
return accumulator;
|
|
824
841
|
}
|
|
825
842
|
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
*/
|
|
829
|
-
* [Symbol.iterator]() {
|
|
830
|
-
let current = this.head;
|
|
831
|
-
|
|
832
|
-
while (current) {
|
|
833
|
-
yield current.value;
|
|
834
|
-
current = current.next;
|
|
835
|
-
}
|
|
843
|
+
print(): void {
|
|
844
|
+
console.log([...this]);
|
|
836
845
|
}
|
|
837
846
|
}
|
|
@@ -24,10 +24,14 @@ export class SinglyLinkedList<E = any> {
|
|
|
24
24
|
/**
|
|
25
25
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
26
26
|
*/
|
|
27
|
-
constructor() {
|
|
27
|
+
constructor(elements?: Iterable<E>) {
|
|
28
28
|
this._head = null;
|
|
29
29
|
this._tail = null;
|
|
30
30
|
this._length = 0;
|
|
31
|
+
if (elements) {
|
|
32
|
+
for (const el of elements)
|
|
33
|
+
this.push(el);
|
|
34
|
+
}
|
|
31
35
|
}
|
|
32
36
|
|
|
33
37
|
protected _head: SinglyLinkedListNode<E> | null;
|
|
@@ -666,63 +670,47 @@ export class SinglyLinkedList<E = any> {
|
|
|
666
670
|
}
|
|
667
671
|
|
|
668
672
|
/**
|
|
669
|
-
*
|
|
670
|
-
* Space Complexity: O(1) - Constant space.
|
|
671
|
-
*/
|
|
672
|
-
|
|
673
|
-
/**
|
|
674
|
-
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
675
|
-
* Space Complexity: O(1) - Constant space.
|
|
676
|
-
*
|
|
677
|
-
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
678
|
-
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
|
|
679
|
-
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
680
|
-
* current node in the linked list.
|
|
673
|
+
* The function returns an iterator that iterates over the values of a linked list.
|
|
681
674
|
*/
|
|
682
|
-
|
|
675
|
+
* [Symbol.iterator]() {
|
|
683
676
|
let current = this.head;
|
|
684
|
-
|
|
677
|
+
|
|
685
678
|
while (current) {
|
|
686
|
-
|
|
679
|
+
yield current.value;
|
|
687
680
|
current = current.next;
|
|
688
|
-
index++;
|
|
689
681
|
}
|
|
690
682
|
}
|
|
691
683
|
|
|
692
684
|
/**
|
|
693
|
-
* Time Complexity: O(n)
|
|
694
|
-
* Space Complexity: O(
|
|
685
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
686
|
+
* Space Complexity: O(1)
|
|
695
687
|
*/
|
|
696
688
|
|
|
697
689
|
/**
|
|
698
|
-
* Time Complexity: O(n)
|
|
699
|
-
* Space Complexity: O(
|
|
690
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
691
|
+
* Space Complexity: O(1)
|
|
700
692
|
*
|
|
701
|
-
* The `
|
|
702
|
-
*
|
|
703
|
-
*
|
|
704
|
-
*
|
|
705
|
-
* SinglyLinkedList).
|
|
706
|
-
* @returns The `map` function is returning a new instance of `SinglyLinkedList<U>` that contains the mapped values.
|
|
693
|
+
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
694
|
+
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
|
|
695
|
+
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
696
|
+
* current node in the linked list.
|
|
707
697
|
*/
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
current = current.next;
|
|
698
|
+
forEach(callback: (value: E, index: number, list: SinglyLinkedList<E>) => void): void {
|
|
699
|
+
let index = 0;
|
|
700
|
+
for (const el of this) {
|
|
701
|
+
callback(el, index, this);
|
|
702
|
+
index++;
|
|
714
703
|
}
|
|
715
|
-
return mappedList;
|
|
716
704
|
}
|
|
717
705
|
|
|
718
706
|
/**
|
|
719
|
-
* Time Complexity: O(n)
|
|
720
|
-
* Space Complexity: O(n)
|
|
707
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
708
|
+
* Space Complexity: O(n)
|
|
721
709
|
*/
|
|
722
710
|
|
|
723
711
|
/**
|
|
724
|
-
* Time Complexity: O(n)
|
|
725
|
-
* Space Complexity: O(n)
|
|
712
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
713
|
+
* Space Complexity: O(n)
|
|
726
714
|
*
|
|
727
715
|
* The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the
|
|
728
716
|
* elements that satisfy the given callback function.
|
|
@@ -730,55 +718,75 @@ export class SinglyLinkedList<E = any> {
|
|
|
730
718
|
* It is used to determine whether a value should be included in the filtered list or not.
|
|
731
719
|
* @returns The filtered list, which is an instance of the SinglyLinkedList class.
|
|
732
720
|
*/
|
|
733
|
-
filter(callback: (value: E) => boolean): SinglyLinkedList<E> {
|
|
721
|
+
filter(callback: (value: E, index: number, list: SinglyLinkedList<E>) => boolean): SinglyLinkedList<E> {
|
|
734
722
|
const filteredList = new SinglyLinkedList<E>();
|
|
735
|
-
let
|
|
736
|
-
|
|
737
|
-
if (callback(current
|
|
738
|
-
filteredList.push(current
|
|
723
|
+
let index = 0;
|
|
724
|
+
for (const current of this) {
|
|
725
|
+
if (callback(current, index, this)) {
|
|
726
|
+
filteredList.push(current);
|
|
739
727
|
}
|
|
740
|
-
|
|
728
|
+
index++;
|
|
741
729
|
}
|
|
742
730
|
return filteredList;
|
|
743
731
|
}
|
|
744
732
|
|
|
745
733
|
/**
|
|
746
|
-
* Time Complexity: O(n)
|
|
747
|
-
* Space Complexity: O(n)
|
|
734
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
735
|
+
* Space Complexity: O(n)
|
|
736
|
+
*/
|
|
737
|
+
|
|
738
|
+
/**
|
|
739
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
740
|
+
* Space Complexity: O(n)
|
|
741
|
+
*
|
|
742
|
+
* The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new
|
|
743
|
+
* SinglyLinkedList with the transformed values.
|
|
744
|
+
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
|
|
745
|
+
* the original SinglyLinkedList) and returns a value of type T (the type of values that will be stored in the mapped
|
|
746
|
+
* SinglyLinkedList).
|
|
747
|
+
* @returns The `map` function is returning a new instance of `SinglyLinkedList<T>` that contains the mapped values.
|
|
748
|
+
*/
|
|
749
|
+
map<T>(callback: (value: E, index: number, list: SinglyLinkedList<E>) => T): SinglyLinkedList<T> {
|
|
750
|
+
const mappedList = new SinglyLinkedList<T>();
|
|
751
|
+
let index = 0;
|
|
752
|
+
for (const current of this) {
|
|
753
|
+
mappedList.push(callback(current, index, this));
|
|
754
|
+
index++;
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
return mappedList;
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
/**
|
|
761
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
762
|
+
* Space Complexity: O(n)
|
|
748
763
|
*/
|
|
749
764
|
|
|
750
765
|
/**
|
|
751
|
-
* Time Complexity: O(n)
|
|
752
|
-
* Space Complexity: O(n)
|
|
766
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
767
|
+
* Space Complexity: O(n)
|
|
753
768
|
*
|
|
754
769
|
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
755
770
|
* single value.
|
|
756
771
|
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
|
|
757
772
|
* used to perform a specific operation on each element of the linked list.
|
|
758
|
-
* @param {
|
|
773
|
+
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
759
774
|
* point for the reduction operation.
|
|
760
775
|
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
761
776
|
* elements in the linked list.
|
|
762
777
|
*/
|
|
763
|
-
reduce<
|
|
778
|
+
reduce<T>(callback: (accumulator: T, value: E, index: number, list: SinglyLinkedList<E>) => T, initialValue: T): T {
|
|
764
779
|
let accumulator = initialValue;
|
|
765
|
-
let
|
|
766
|
-
|
|
767
|
-
accumulator = callback(accumulator, current
|
|
768
|
-
|
|
780
|
+
let index = 0;
|
|
781
|
+
for (const current of this) {
|
|
782
|
+
accumulator = callback(accumulator, current, index, this);
|
|
783
|
+
index++;
|
|
769
784
|
}
|
|
785
|
+
|
|
770
786
|
return accumulator;
|
|
771
787
|
}
|
|
772
788
|
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
*/
|
|
776
|
-
* [Symbol.iterator]() {
|
|
777
|
-
let current = this.head;
|
|
778
|
-
|
|
779
|
-
while (current) {
|
|
780
|
-
yield current.value;
|
|
781
|
-
current = current.next;
|
|
782
|
-
}
|
|
789
|
+
print(): void {
|
|
790
|
+
console.log([...this]);
|
|
783
791
|
}
|
|
784
792
|
}
|
|
@@ -6,11 +6,12 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { PriorityQueue } from './priority-queue';
|
|
9
|
-
import type {
|
|
9
|
+
import type { PriorityQueueOptions } from '../../types';
|
|
10
10
|
|
|
11
11
|
export class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
12
12
|
constructor(
|
|
13
|
-
|
|
13
|
+
elements?: Iterable<E>,
|
|
14
|
+
options: PriorityQueueOptions<E> = {
|
|
14
15
|
comparator: (a: E, b: E) => {
|
|
15
16
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
17
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -20,6 +21,6 @@ export class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
|
20
21
|
}
|
|
21
22
|
}
|
|
22
23
|
) {
|
|
23
|
-
super(options);
|
|
24
|
+
super(elements, options);
|
|
24
25
|
}
|
|
25
26
|
}
|
|
@@ -6,20 +6,20 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { PriorityQueue } from './priority-queue';
|
|
9
|
-
import type {
|
|
9
|
+
import type { PriorityQueueOptions } from '../../types';
|
|
10
10
|
|
|
11
11
|
export class MinPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
12
|
-
constructor(
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
12
|
+
constructor(elements?: Iterable<E>,
|
|
13
|
+
options: PriorityQueueOptions<E> = {
|
|
14
|
+
comparator: (a: E, b: E) => {
|
|
15
|
+
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
|
+
throw new Error('The a, b params of compare function must be number');
|
|
17
|
+
} else {
|
|
18
|
+
return a - b;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
22
|
) {
|
|
23
|
-
super(options);
|
|
23
|
+
super(elements, options);
|
|
24
24
|
}
|
|
25
25
|
}
|
|
@@ -7,10 +7,10 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import { Heap } from '../heap';
|
|
10
|
-
import {
|
|
10
|
+
import { PriorityQueueOptions } from '../../types';
|
|
11
11
|
|
|
12
12
|
export class PriorityQueue<E = any> extends Heap<E> {
|
|
13
|
-
constructor(
|
|
14
|
-
super(options);
|
|
13
|
+
constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>) {
|
|
14
|
+
super(elements, options);
|
|
15
15
|
}
|
|
16
16
|
}
|