data-structure-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/CHANGELOG.md +1 -1
- package/benchmark/report.html +2 -2
- package/benchmark/report.json +12 -18
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +8 -8
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +23 -15
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +65 -28
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +66 -82
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +38 -37
- package/dist/cjs/data-structures/binary-tree/bst.js +56 -40
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +11 -7
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +26 -17
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +16 -16
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +31 -22
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +3 -3
- package/dist/cjs/data-structures/hash/hash-map.js +10 -4
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-table.d.ts +9 -4
- package/dist/cjs/data-structures/hash/hash-table.js +50 -5
- package/dist/cjs/data-structures/hash/hash-table.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +25 -22
- package/dist/cjs/data-structures/heap/heap.js +101 -41
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.d.ts +2 -5
- package/dist/cjs/data-structures/heap/max-heap.js +2 -2
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.d.ts +2 -5
- package/dist/cjs/data-structures/heap/min-heap.js +2 -2
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +58 -57
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +125 -119
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +38 -37
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +65 -60
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +2 -2
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +2 -2
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +2 -5
- package/dist/cjs/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +50 -49
- package/dist/cjs/data-structures/queue/deque.js +81 -71
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +46 -0
- package/dist/cjs/data-structures/queue/queue.js +80 -0
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +20 -6
- package/dist/cjs/data-structures/stack/stack.js +65 -8
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +5 -0
- package/dist/cjs/data-structures/trie/trie.js +47 -0
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +3 -1
- package/dist/cjs/types/common.d.ts +2 -0
- package/dist/cjs/types/common.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/cjs/types/data-structures/heap/heap.d.ts +4 -1
- package/dist/cjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +8 -8
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +26 -16
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +65 -28
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +66 -83
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +38 -37
- package/dist/mjs/data-structures/binary-tree/bst.js +59 -41
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +11 -7
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +30 -19
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +16 -16
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +34 -23
- package/dist/mjs/data-structures/graph/abstract-graph.js +1 -1
- package/dist/mjs/data-structures/hash/hash-map.d.ts +3 -3
- package/dist/mjs/data-structures/hash/hash-map.js +10 -4
- package/dist/mjs/data-structures/hash/hash-table.d.ts +9 -4
- package/dist/mjs/data-structures/hash/hash-table.js +50 -5
- package/dist/mjs/data-structures/heap/heap.d.ts +25 -22
- package/dist/mjs/data-structures/heap/heap.js +102 -42
- package/dist/mjs/data-structures/heap/max-heap.d.ts +2 -5
- package/dist/mjs/data-structures/heap/max-heap.js +2 -2
- package/dist/mjs/data-structures/heap/min-heap.d.ts +2 -5
- package/dist/mjs/data-structures/heap/min-heap.js +2 -2
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +58 -57
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +125 -119
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +38 -37
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +65 -60
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +2 -2
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +2 -2
- package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +2 -5
- package/dist/mjs/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/mjs/data-structures/queue/deque.d.ts +50 -49
- package/dist/mjs/data-structures/queue/deque.js +81 -71
- package/dist/mjs/data-structures/queue/queue.d.ts +46 -0
- package/dist/mjs/data-structures/queue/queue.js +80 -0
- package/dist/mjs/data-structures/stack/stack.d.ts +20 -6
- package/dist/mjs/data-structures/stack/stack.js +65 -8
- package/dist/mjs/data-structures/trie/trie.d.ts +5 -0
- package/dist/mjs/data-structures/trie/trie.js +47 -0
- package/dist/mjs/interfaces/binary-tree.d.ts +3 -1
- package/dist/mjs/types/common.d.ts +2 -0
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/mjs/types/data-structures/heap/heap.d.ts +4 -1
- package/dist/mjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
- package/dist/umd/data-structure-typed.js +856 -509
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- 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
- package/test/performance/data-structures/comparison/comparison.test.ts +7 -6
- package/test/performance/data-structures/heap/heap.test.ts +14 -5
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +6 -6
- package/test/unit/data-structures/binary-tree/bst.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +1 -1
- package/test/unit/data-structures/hash/hash-table.test.ts +58 -2
- package/test/unit/data-structures/heap/heap.test.ts +2 -2
- package/test/unit/data-structures/heap/max-heap.test.ts +1 -1
- package/test/unit/data-structures/heap/min-heap.test.ts +49 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +2 -2
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +4 -3
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +9 -10
- package/test/unit/data-structures/queue/queue.test.ts +37 -0
- package/test/unit/data-structures/stack/stack.test.ts +55 -5
- package/test/unit/data-structures/trie/trie.test.ts +33 -0
- package/test/unit/unrestricted-interconversion.test.ts +100 -0
|
@@ -22,10 +22,14 @@ export class SinglyLinkedList {
|
|
|
22
22
|
/**
|
|
23
23
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
24
24
|
*/
|
|
25
|
-
constructor() {
|
|
25
|
+
constructor(elements) {
|
|
26
26
|
this._head = null;
|
|
27
27
|
this._tail = null;
|
|
28
28
|
this._length = 0;
|
|
29
|
+
if (elements) {
|
|
30
|
+
for (const el of elements)
|
|
31
|
+
this.push(el);
|
|
32
|
+
}
|
|
29
33
|
}
|
|
30
34
|
_head;
|
|
31
35
|
get head() {
|
|
@@ -606,58 +610,42 @@ export class SinglyLinkedList {
|
|
|
606
610
|
return count;
|
|
607
611
|
}
|
|
608
612
|
/**
|
|
609
|
-
*
|
|
610
|
-
* Space Complexity: O(1) - Constant space.
|
|
611
|
-
*/
|
|
612
|
-
/**
|
|
613
|
-
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
614
|
-
* Space Complexity: O(1) - Constant space.
|
|
615
|
-
*
|
|
616
|
-
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
617
|
-
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
|
|
618
|
-
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
619
|
-
* current node in the linked list.
|
|
613
|
+
* The function returns an iterator that iterates over the values of a linked list.
|
|
620
614
|
*/
|
|
621
|
-
|
|
615
|
+
*[Symbol.iterator]() {
|
|
622
616
|
let current = this.head;
|
|
623
|
-
let index = 0;
|
|
624
617
|
while (current) {
|
|
625
|
-
|
|
618
|
+
yield current.value;
|
|
626
619
|
current = current.next;
|
|
627
|
-
index++;
|
|
628
620
|
}
|
|
629
621
|
}
|
|
630
622
|
/**
|
|
631
|
-
* Time Complexity: O(n)
|
|
632
|
-
* Space Complexity: O(
|
|
623
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
624
|
+
* Space Complexity: O(1)
|
|
633
625
|
*/
|
|
634
626
|
/**
|
|
635
|
-
* Time Complexity: O(n)
|
|
636
|
-
* Space Complexity: O(
|
|
627
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
628
|
+
* Space Complexity: O(1)
|
|
637
629
|
*
|
|
638
|
-
* The `
|
|
639
|
-
*
|
|
640
|
-
*
|
|
641
|
-
*
|
|
642
|
-
* SinglyLinkedList).
|
|
643
|
-
* @returns The `map` function is returning a new instance of `SinglyLinkedList<U>` that contains the mapped values.
|
|
630
|
+
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
631
|
+
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
|
|
632
|
+
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
633
|
+
* current node in the linked list.
|
|
644
634
|
*/
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
current = current.next;
|
|
635
|
+
forEach(callback) {
|
|
636
|
+
let index = 0;
|
|
637
|
+
for (const el of this) {
|
|
638
|
+
callback(el, index, this);
|
|
639
|
+
index++;
|
|
651
640
|
}
|
|
652
|
-
return mappedList;
|
|
653
641
|
}
|
|
654
642
|
/**
|
|
655
|
-
* Time Complexity: O(n)
|
|
656
|
-
* Space Complexity: O(n)
|
|
643
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
644
|
+
* Space Complexity: O(n)
|
|
657
645
|
*/
|
|
658
646
|
/**
|
|
659
|
-
* Time Complexity: O(n)
|
|
660
|
-
* Space Complexity: O(n)
|
|
647
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
648
|
+
* Space Complexity: O(n)
|
|
661
649
|
*
|
|
662
650
|
* The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the
|
|
663
651
|
* elements that satisfy the given callback function.
|
|
@@ -667,49 +655,66 @@ export class SinglyLinkedList {
|
|
|
667
655
|
*/
|
|
668
656
|
filter(callback) {
|
|
669
657
|
const filteredList = new SinglyLinkedList();
|
|
670
|
-
let
|
|
671
|
-
|
|
672
|
-
if (callback(current
|
|
673
|
-
filteredList.push(current
|
|
658
|
+
let index = 0;
|
|
659
|
+
for (const current of this) {
|
|
660
|
+
if (callback(current, index, this)) {
|
|
661
|
+
filteredList.push(current);
|
|
674
662
|
}
|
|
675
|
-
|
|
663
|
+
index++;
|
|
676
664
|
}
|
|
677
665
|
return filteredList;
|
|
678
666
|
}
|
|
679
667
|
/**
|
|
680
|
-
* Time Complexity: O(n)
|
|
681
|
-
* Space Complexity: O(n)
|
|
668
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
669
|
+
* Space Complexity: O(n)
|
|
682
670
|
*/
|
|
683
671
|
/**
|
|
684
|
-
* Time Complexity: O(n)
|
|
685
|
-
* Space Complexity: O(n)
|
|
672
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
673
|
+
* Space Complexity: O(n)
|
|
674
|
+
*
|
|
675
|
+
* The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new
|
|
676
|
+
* SinglyLinkedList with the transformed values.
|
|
677
|
+
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
|
|
678
|
+
* the original SinglyLinkedList) and returns a value of type T (the type of values that will be stored in the mapped
|
|
679
|
+
* SinglyLinkedList).
|
|
680
|
+
* @returns The `map` function is returning a new instance of `SinglyLinkedList<T>` that contains the mapped values.
|
|
681
|
+
*/
|
|
682
|
+
map(callback) {
|
|
683
|
+
const mappedList = new SinglyLinkedList();
|
|
684
|
+
let index = 0;
|
|
685
|
+
for (const current of this) {
|
|
686
|
+
mappedList.push(callback(current, index, this));
|
|
687
|
+
index++;
|
|
688
|
+
}
|
|
689
|
+
return mappedList;
|
|
690
|
+
}
|
|
691
|
+
/**
|
|
692
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
693
|
+
* Space Complexity: O(n)
|
|
694
|
+
*/
|
|
695
|
+
/**
|
|
696
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
697
|
+
* Space Complexity: O(n)
|
|
686
698
|
*
|
|
687
699
|
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
688
700
|
* single value.
|
|
689
701
|
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
|
|
690
702
|
* used to perform a specific operation on each element of the linked list.
|
|
691
|
-
* @param {
|
|
703
|
+
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
692
704
|
* point for the reduction operation.
|
|
693
705
|
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
694
706
|
* elements in the linked list.
|
|
695
707
|
*/
|
|
696
708
|
reduce(callback, initialValue) {
|
|
697
709
|
let accumulator = initialValue;
|
|
698
|
-
let
|
|
699
|
-
|
|
700
|
-
accumulator = callback(accumulator, current
|
|
701
|
-
|
|
710
|
+
let index = 0;
|
|
711
|
+
for (const current of this) {
|
|
712
|
+
accumulator = callback(accumulator, current, index, this);
|
|
713
|
+
index++;
|
|
702
714
|
}
|
|
703
715
|
return accumulator;
|
|
704
716
|
}
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
*/
|
|
708
|
-
*[Symbol.iterator]() {
|
|
709
|
-
let current = this.head;
|
|
710
|
-
while (current) {
|
|
711
|
-
yield current.value;
|
|
712
|
-
current = current.next;
|
|
713
|
-
}
|
|
717
|
+
print() {
|
|
718
|
+
console.log([...this]);
|
|
714
719
|
}
|
|
715
720
|
}
|
|
@@ -6,10 +6,7 @@
|
|
|
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
|
export declare class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
11
|
-
constructor(options?:
|
|
12
|
-
comparator: Comparator<E>;
|
|
13
|
-
nodes?: E[];
|
|
14
|
-
});
|
|
11
|
+
constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>);
|
|
15
12
|
}
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { PriorityQueue } from './priority-queue';
|
|
9
9
|
export class MaxPriorityQueue extends PriorityQueue {
|
|
10
|
-
constructor(options = {
|
|
10
|
+
constructor(elements, options = {
|
|
11
11
|
comparator: (a, b) => {
|
|
12
12
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
13
13
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -17,6 +17,6 @@ export class MaxPriorityQueue extends PriorityQueue {
|
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
}) {
|
|
20
|
-
super(options);
|
|
20
|
+
super(elements, options);
|
|
21
21
|
}
|
|
22
22
|
}
|
|
@@ -6,10 +6,7 @@
|
|
|
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
|
export declare class MinPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
11
|
-
constructor(options?:
|
|
12
|
-
comparator: Comparator<E>;
|
|
13
|
-
nodes?: E[];
|
|
14
|
-
});
|
|
11
|
+
constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>);
|
|
15
12
|
}
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { PriorityQueue } from './priority-queue';
|
|
9
9
|
export class MinPriorityQueue extends PriorityQueue {
|
|
10
|
-
constructor(options = {
|
|
10
|
+
constructor(elements, options = {
|
|
11
11
|
comparator: (a, b) => {
|
|
12
12
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
13
13
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -17,6 +17,6 @@ export class MinPriorityQueue extends PriorityQueue {
|
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
}) {
|
|
20
|
-
super(options);
|
|
20
|
+
super(elements, options);
|
|
21
21
|
}
|
|
22
22
|
}
|
|
@@ -6,10 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { Heap } from '../heap';
|
|
9
|
-
import {
|
|
9
|
+
import { PriorityQueueOptions } from '../../types';
|
|
10
10
|
export declare class PriorityQueue<E = any> extends Heap<E> {
|
|
11
|
-
constructor(options
|
|
12
|
-
comparator: Comparator<E>;
|
|
13
|
-
nodes?: E[];
|
|
14
|
-
});
|
|
11
|
+
constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>);
|
|
15
12
|
}
|
|
@@ -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,8 @@ 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
|
+
print(): void;
|
|
431
432
|
/**
|
|
432
433
|
* Time Complexity: O(n)
|
|
433
434
|
* Space Complexity: O(n)
|
|
@@ -592,15 +592,21 @@ export class Deque {
|
|
|
592
592
|
* Time Complexity: O(n)
|
|
593
593
|
* Space Complexity: O(1)
|
|
594
594
|
*
|
|
595
|
-
* The `
|
|
596
|
-
*
|
|
597
|
-
* @param callback -
|
|
598
|
-
*
|
|
595
|
+
* The `find` function iterates over the elements in a deque and returns the first element for which
|
|
596
|
+
* the callback function returns true, or undefined if no such element is found.
|
|
597
|
+
* @param callback - A function that takes three parameters: element, index, and deque. It should
|
|
598
|
+
* return a boolean value indicating whether the element satisfies a certain condition.
|
|
599
|
+
* @returns The method `find` returns the first element in the deque that satisfies the condition
|
|
600
|
+
* specified by the callback function. If no element satisfies the condition, it returns `undefined`.
|
|
599
601
|
*/
|
|
600
|
-
|
|
602
|
+
find(callback) {
|
|
601
603
|
for (let i = 0; i < this.size; ++i) {
|
|
602
|
-
|
|
604
|
+
const element = this.getAt(i);
|
|
605
|
+
if (callback(element, i, this)) {
|
|
606
|
+
return element;
|
|
607
|
+
}
|
|
603
608
|
}
|
|
609
|
+
return undefined;
|
|
604
610
|
}
|
|
605
611
|
/**
|
|
606
612
|
* Time Complexity: O(n)
|
|
@@ -610,21 +616,20 @@ export class Deque {
|
|
|
610
616
|
* Time Complexity: O(n)
|
|
611
617
|
* Space Complexity: O(1)
|
|
612
618
|
*
|
|
613
|
-
* The
|
|
614
|
-
*
|
|
615
|
-
* @param
|
|
616
|
-
*
|
|
617
|
-
* @returns The
|
|
618
|
-
*
|
|
619
|
+
* The function "indexOf" returns the index of the first occurrence of a given element in an array,
|
|
620
|
+
* or -1 if the element is not found.
|
|
621
|
+
* @param {E} element - The "element" parameter represents the element that you want to find the
|
|
622
|
+
* index of in the data structure.
|
|
623
|
+
* @returns The indexOf function returns the index of the first occurrence of the specified element
|
|
624
|
+
* in the data structure. If the element is not found, it returns -1.
|
|
619
625
|
*/
|
|
620
|
-
|
|
626
|
+
indexOf(element) {
|
|
621
627
|
for (let i = 0; i < this.size; ++i) {
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
return element;
|
|
628
|
+
if (this.getAt(i) === element) {
|
|
629
|
+
return i;
|
|
625
630
|
}
|
|
626
631
|
}
|
|
627
|
-
return
|
|
632
|
+
return -1;
|
|
628
633
|
}
|
|
629
634
|
/**
|
|
630
635
|
* Time Complexity: O(n)
|
|
@@ -646,23 +651,39 @@ export class Deque {
|
|
|
646
651
|
}
|
|
647
652
|
/**
|
|
648
653
|
* Time Complexity: O(n)
|
|
649
|
-
* Space Complexity: O(
|
|
654
|
+
* Space Complexity: O(1)
|
|
650
655
|
*/
|
|
651
656
|
/**
|
|
652
657
|
* Time Complexity: O(n)
|
|
653
|
-
* Space Complexity: O(
|
|
658
|
+
* Space Complexity: O(1)
|
|
654
659
|
*
|
|
655
|
-
* The
|
|
656
|
-
*
|
|
657
|
-
* @param callback - The `callback` parameter is a function that takes three arguments:
|
|
658
|
-
* @returns The `map` method is returning a new `Deque` object with the transformed elements.
|
|
660
|
+
* The above function is an implementation of the iterator protocol in TypeScript, allowing the
|
|
661
|
+
* object to be iterated over using a for...of loop.
|
|
659
662
|
*/
|
|
660
|
-
|
|
661
|
-
const newDeque = new Deque([], this._bucketSize);
|
|
663
|
+
*[Symbol.iterator]() {
|
|
662
664
|
for (let i = 0; i < this.size; ++i) {
|
|
663
|
-
|
|
665
|
+
yield this.getAt(i);
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
/**
|
|
669
|
+
* Time Complexity: O(n)
|
|
670
|
+
* Space Complexity: O(1)
|
|
671
|
+
*/
|
|
672
|
+
/**
|
|
673
|
+
* Time Complexity: O(n)
|
|
674
|
+
* Space Complexity: O(1)
|
|
675
|
+
*
|
|
676
|
+
* The `forEach` function iterates over each element in a deque and applies a callback function to
|
|
677
|
+
* each element.
|
|
678
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
679
|
+
* deque. It takes three parameters:
|
|
680
|
+
*/
|
|
681
|
+
forEach(callback) {
|
|
682
|
+
let index = 0;
|
|
683
|
+
for (const el of this) {
|
|
684
|
+
callback(el, index, this);
|
|
685
|
+
index++;
|
|
664
686
|
}
|
|
665
|
-
return newDeque;
|
|
666
687
|
}
|
|
667
688
|
/**
|
|
668
689
|
* Time Complexity: O(n)
|
|
@@ -681,11 +702,34 @@ export class Deque {
|
|
|
681
702
|
*/
|
|
682
703
|
filter(predicate) {
|
|
683
704
|
const newDeque = new Deque([], this._bucketSize);
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
if (predicate(
|
|
687
|
-
newDeque.push(
|
|
705
|
+
let index = 0;
|
|
706
|
+
for (const el of this) {
|
|
707
|
+
if (predicate(el, index, this)) {
|
|
708
|
+
newDeque.push(el);
|
|
688
709
|
}
|
|
710
|
+
index++;
|
|
711
|
+
}
|
|
712
|
+
return newDeque;
|
|
713
|
+
}
|
|
714
|
+
/**
|
|
715
|
+
* Time Complexity: O(n)
|
|
716
|
+
* Space Complexity: O(n)
|
|
717
|
+
*/
|
|
718
|
+
/**
|
|
719
|
+
* Time Complexity: O(n)
|
|
720
|
+
* Space Complexity: O(n)
|
|
721
|
+
*
|
|
722
|
+
* The `map` function takes a callback function and applies it to each element in the deque,
|
|
723
|
+
* returning a new deque with the results.
|
|
724
|
+
* @param callback - The `callback` parameter is a function that takes three arguments:
|
|
725
|
+
* @returns The `map` method is returning a new `Deque` object with the transformed elements.
|
|
726
|
+
*/
|
|
727
|
+
map(callback) {
|
|
728
|
+
const newDeque = new Deque([], this._bucketSize);
|
|
729
|
+
let index = 0;
|
|
730
|
+
for (const el of this) {
|
|
731
|
+
newDeque.push(callback(el, index, this));
|
|
732
|
+
index++;
|
|
689
733
|
}
|
|
690
734
|
return newDeque;
|
|
691
735
|
}
|
|
@@ -708,49 +752,15 @@ export class Deque {
|
|
|
708
752
|
*/
|
|
709
753
|
reduce(callback, initialValue) {
|
|
710
754
|
let accumulator = initialValue;
|
|
711
|
-
|
|
712
|
-
|
|
755
|
+
let index = 0;
|
|
756
|
+
for (const el of this) {
|
|
757
|
+
accumulator = callback(accumulator, el, index, this);
|
|
758
|
+
index++;
|
|
713
759
|
}
|
|
714
760
|
return accumulator;
|
|
715
761
|
}
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
* Space Complexity: O(1)
|
|
719
|
-
*/
|
|
720
|
-
/**
|
|
721
|
-
* Time Complexity: O(n)
|
|
722
|
-
* Space Complexity: O(1)
|
|
723
|
-
*
|
|
724
|
-
* The function "indexOf" returns the index of the first occurrence of a given element in an array,
|
|
725
|
-
* or -1 if the element is not found.
|
|
726
|
-
* @param {E} element - The "element" parameter represents the element that you want to find the
|
|
727
|
-
* index of in the data structure.
|
|
728
|
-
* @returns The indexOf function returns the index of the first occurrence of the specified element
|
|
729
|
-
* in the data structure. If the element is not found, it returns -1.
|
|
730
|
-
*/
|
|
731
|
-
indexOf(element) {
|
|
732
|
-
for (let i = 0; i < this.size; ++i) {
|
|
733
|
-
if (this.getAt(i) === element) {
|
|
734
|
-
return i;
|
|
735
|
-
}
|
|
736
|
-
}
|
|
737
|
-
return -1;
|
|
738
|
-
}
|
|
739
|
-
/**
|
|
740
|
-
* Time Complexity: O(n)
|
|
741
|
-
* Space Complexity: O(1)
|
|
742
|
-
*/
|
|
743
|
-
/**
|
|
744
|
-
* Time Complexity: O(n)
|
|
745
|
-
* Space Complexity: O(1)
|
|
746
|
-
*
|
|
747
|
-
* The above function is an implementation of the iterator protocol in TypeScript, allowing the
|
|
748
|
-
* object to be iterated over using a for...of loop.
|
|
749
|
-
*/
|
|
750
|
-
*[Symbol.iterator]() {
|
|
751
|
-
for (let i = 0; i < this.size; ++i) {
|
|
752
|
-
yield this.getAt(i);
|
|
753
|
-
}
|
|
762
|
+
print() {
|
|
763
|
+
console.log([...this]);
|
|
754
764
|
}
|
|
755
765
|
/**
|
|
756
766
|
* Time Complexity: O(n)
|