data-structure-typed 1.47.5 → 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/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/heap/heap.d.ts +19 -21
- package/dist/cjs/data-structures/heap/heap.js +52 -34
- 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 +2 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +9 -1
- 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 +2 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +8 -1
- 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 +1 -0
- package/dist/cjs/data-structures/queue/deque.js +3 -0
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +1 -0
- package/dist/cjs/data-structures/queue/queue.js +3 -0
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +2 -1
- package/dist/cjs/data-structures/stack/stack.js +10 -2
- package/dist/cjs/data-structures/stack/stack.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/heap/heap.d.ts +19 -21
- package/dist/mjs/data-structures/heap/heap.js +53 -35
- 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 +2 -1
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +9 -1
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +2 -1
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +8 -1
- 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 +1 -0
- package/dist/mjs/data-structures/queue/deque.js +3 -0
- package/dist/mjs/data-structures/queue/queue.d.ts +1 -0
- package/dist/mjs/data-structures/queue/queue.js +3 -0
- package/dist/mjs/data-structures/stack/stack.d.ts +2 -1
- package/dist/mjs/data-structures/stack/stack.js +10 -2
- 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 +307 -229
- 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/heap/heap.ts +57 -39
- 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 +10 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +9 -1
- 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 +4 -0
- package/src/data-structures/queue/queue.ts +4 -0
- package/src/data-structures/stack/stack.ts +12 -3
- 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 +2 -2
- 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/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 +1 -1
- 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/stack/stack.test.ts +2 -2
- package/test/unit/unrestricted-interconversion.test.ts +100 -0
- package/test/integration/conversion.test.ts +0 -0
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { BTNKey, TreeMultimapNodeNested, TreeMultimapOptions } from '../../types';
|
|
9
|
-
import { BiTreeDeleteResult, BTNCallback, IterationType, TreeMultimapNested } from '../../types';
|
|
9
|
+
import { BiTreeDeleteResult, BTNCallback, IterableEntriesOrKeys, IterationType, TreeMultimapNested } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
12
12
|
export declare class TreeMultimapNode<V = any, N extends TreeMultimapNode<V, N> = TreeMultimapNodeNested<V>> extends AVLTreeNode<V, N> {
|
|
@@ -27,14 +27,13 @@ export declare class TreeMultimapNode<V = any, N extends TreeMultimapNode<V, N>
|
|
|
27
27
|
* The only distinction between a TreeMultimap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
28
28
|
*/
|
|
29
29
|
export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultimapNode<V, TreeMultimapNodeNested<V>>, TREE extends TreeMultimap<V, N, TREE> = TreeMultimap<V, N, TreeMultimapNested<V, N>>> extends AVLTree<V, N, TREE> implements IBinaryTree<V, N, TREE> {
|
|
30
|
-
options: TreeMultimapOptions;
|
|
31
30
|
/**
|
|
32
31
|
* The constructor function for a TreeMultimap class in TypeScript, which extends another class and sets an option to
|
|
33
32
|
* merge duplicated values.
|
|
34
33
|
* @param {TreeMultimapOptions} [options] - An optional object that contains additional configuration options for the
|
|
35
34
|
* TreeMultimap.
|
|
36
35
|
*/
|
|
37
|
-
constructor(options?: TreeMultimapOptions);
|
|
36
|
+
constructor(elements?: IterableEntriesOrKeys<V>, options?: Partial<TreeMultimapOptions>);
|
|
38
37
|
private _count;
|
|
39
38
|
get count(): number;
|
|
40
39
|
/**
|
|
@@ -48,10 +47,6 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
|
|
|
48
47
|
*/
|
|
49
48
|
createNode(key: BTNKey, value?: V, count?: number): N;
|
|
50
49
|
createTree(options?: TreeMultimapOptions): TREE;
|
|
51
|
-
/**
|
|
52
|
-
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
53
|
-
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
54
|
-
*/
|
|
55
50
|
/**
|
|
56
51
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
57
52
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -68,8 +63,8 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
|
|
|
68
63
|
*/
|
|
69
64
|
add(keyOrNode: BTNKey | N | null | undefined, value?: V, count?: number): N | undefined;
|
|
70
65
|
/**
|
|
71
|
-
* Time Complexity: O(
|
|
72
|
-
* Space Complexity: O(1) - constant space, as it
|
|
66
|
+
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
67
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
73
68
|
*/
|
|
74
69
|
/**
|
|
75
70
|
* Time Complexity: O(k log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each.
|
|
@@ -86,8 +81,8 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
|
|
|
86
81
|
*/
|
|
87
82
|
addMany(keysOrNodes: (BTNKey | N | undefined)[], data?: V[]): (N | undefined)[];
|
|
88
83
|
/**
|
|
89
|
-
* Time Complexity: O(
|
|
90
|
-
* Space Complexity: O(1) - constant space, as it
|
|
84
|
+
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
85
|
+
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
91
86
|
*/
|
|
92
87
|
/**
|
|
93
88
|
* Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
|
|
@@ -100,10 +95,10 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
|
|
|
100
95
|
* values:
|
|
101
96
|
* @returns a boolean value.
|
|
102
97
|
*/
|
|
103
|
-
perfectlyBalance(iterationType?: IterationType
|
|
98
|
+
perfectlyBalance(iterationType?: IterationType): boolean;
|
|
104
99
|
/**
|
|
105
|
-
* Time Complexity: O(
|
|
106
|
-
* Space Complexity: O(
|
|
100
|
+
* Time Complexity: O(k log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each.
|
|
101
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
107
102
|
*/
|
|
108
103
|
/**
|
|
109
104
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (AVLTree) has logarithmic time complexity.
|
|
@@ -126,13 +121,18 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
|
|
|
126
121
|
*/
|
|
127
122
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback?: C, ignoreCount?: boolean): BiTreeDeleteResult<N>[];
|
|
128
123
|
/**
|
|
129
|
-
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree.
|
|
130
|
-
* Space Complexity: O(
|
|
124
|
+
* Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
|
|
125
|
+
* Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
|
|
131
126
|
*/
|
|
132
127
|
/**
|
|
133
128
|
* The clear() function clears the contents of a data structure and sets the count to zero.
|
|
134
129
|
*/
|
|
135
130
|
clear(): void;
|
|
131
|
+
/**
|
|
132
|
+
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (AVLTree) has logarithmic time complexity.
|
|
133
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
134
|
+
*/
|
|
135
|
+
init(elements: IterableEntriesOrKeys<V>): void;
|
|
136
136
|
/**
|
|
137
137
|
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
138
138
|
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
@@ -21,21 +21,16 @@ export class TreeMultimapNode extends AVLTreeNode {
|
|
|
21
21
|
* The only distinction between a TreeMultimap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
22
22
|
*/
|
|
23
23
|
export class TreeMultimap extends AVLTree {
|
|
24
|
-
options;
|
|
25
24
|
/**
|
|
26
25
|
* The constructor function for a TreeMultimap class in TypeScript, which extends another class and sets an option to
|
|
27
26
|
* merge duplicated values.
|
|
28
27
|
* @param {TreeMultimapOptions} [options] - An optional object that contains additional configuration options for the
|
|
29
28
|
* TreeMultimap.
|
|
30
29
|
*/
|
|
31
|
-
constructor(options
|
|
32
|
-
super(options);
|
|
33
|
-
if (
|
|
34
|
-
this.
|
|
35
|
-
}
|
|
36
|
-
else {
|
|
37
|
-
this.options = { iterationType: IterationType.ITERATIVE, comparator: (a, b) => a - b };
|
|
38
|
-
}
|
|
30
|
+
constructor(elements, options) {
|
|
31
|
+
super([], options);
|
|
32
|
+
if (elements)
|
|
33
|
+
this.init(elements);
|
|
39
34
|
}
|
|
40
35
|
_count = 0;
|
|
41
36
|
get count() {
|
|
@@ -54,12 +49,11 @@ export class TreeMultimap extends AVLTree {
|
|
|
54
49
|
return new TreeMultimapNode(key, value, count);
|
|
55
50
|
}
|
|
56
51
|
createTree(options) {
|
|
57
|
-
return new TreeMultimap(
|
|
52
|
+
return new TreeMultimap([], {
|
|
53
|
+
iterationType: this.iterationType,
|
|
54
|
+
comparator: this.comparator, ...options
|
|
55
|
+
});
|
|
58
56
|
}
|
|
59
|
-
/**
|
|
60
|
-
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
61
|
-
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
62
|
-
*/
|
|
63
57
|
/**
|
|
64
58
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
65
59
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -154,8 +148,8 @@ export class TreeMultimap extends AVLTree {
|
|
|
154
148
|
return inserted;
|
|
155
149
|
}
|
|
156
150
|
/**
|
|
157
|
-
* Time Complexity: O(
|
|
158
|
-
* Space Complexity: O(1) - constant space, as it
|
|
151
|
+
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
152
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
159
153
|
*/
|
|
160
154
|
/**
|
|
161
155
|
* Time Complexity: O(k log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each.
|
|
@@ -187,8 +181,8 @@ export class TreeMultimap extends AVLTree {
|
|
|
187
181
|
return inserted;
|
|
188
182
|
}
|
|
189
183
|
/**
|
|
190
|
-
* Time Complexity: O(
|
|
191
|
-
* Space Complexity: O(1) - constant space, as it
|
|
184
|
+
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
185
|
+
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
192
186
|
*/
|
|
193
187
|
/**
|
|
194
188
|
* Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
|
|
@@ -201,7 +195,7 @@ export class TreeMultimap extends AVLTree {
|
|
|
201
195
|
* values:
|
|
202
196
|
* @returns a boolean value.
|
|
203
197
|
*/
|
|
204
|
-
perfectlyBalance(iterationType = this.
|
|
198
|
+
perfectlyBalance(iterationType = this.iterationType) {
|
|
205
199
|
const sorted = this.dfs(node => node, 'in'), n = sorted.length;
|
|
206
200
|
if (sorted.length < 1)
|
|
207
201
|
return false;
|
|
@@ -238,8 +232,8 @@ export class TreeMultimap extends AVLTree {
|
|
|
238
232
|
}
|
|
239
233
|
}
|
|
240
234
|
/**
|
|
241
|
-
* Time Complexity: O(
|
|
242
|
-
* Space Complexity: O(
|
|
235
|
+
* Time Complexity: O(k log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each.
|
|
236
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
243
237
|
*/
|
|
244
238
|
/**
|
|
245
239
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (AVLTree) has logarithmic time complexity.
|
|
@@ -318,8 +312,8 @@ export class TreeMultimap extends AVLTree {
|
|
|
318
312
|
return deletedResult;
|
|
319
313
|
}
|
|
320
314
|
/**
|
|
321
|
-
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree.
|
|
322
|
-
* Space Complexity: O(
|
|
315
|
+
* Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
|
|
316
|
+
* Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
|
|
323
317
|
*/
|
|
324
318
|
/**
|
|
325
319
|
* The clear() function clears the contents of a data structure and sets the count to zero.
|
|
@@ -328,6 +322,23 @@ export class TreeMultimap extends AVLTree {
|
|
|
328
322
|
super.clear();
|
|
329
323
|
this._count = 0;
|
|
330
324
|
}
|
|
325
|
+
/**
|
|
326
|
+
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (AVLTree) has logarithmic time complexity.
|
|
327
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
328
|
+
*/
|
|
329
|
+
init(elements) {
|
|
330
|
+
if (elements) {
|
|
331
|
+
for (const entryOrKey of elements) {
|
|
332
|
+
if (Array.isArray(entryOrKey)) {
|
|
333
|
+
const [key, value] = entryOrKey;
|
|
334
|
+
this.add(key, value);
|
|
335
|
+
}
|
|
336
|
+
else {
|
|
337
|
+
this.add(entryOrKey);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
331
342
|
/**
|
|
332
343
|
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
333
344
|
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
@@ -589,7 +589,7 @@ export class AbstractGraph {
|
|
|
589
589
|
if (vertexOrKey instanceof AbstractVertex)
|
|
590
590
|
distMap.set(vertexOrKey, Infinity);
|
|
591
591
|
}
|
|
592
|
-
const heap = new PriorityQueue({ comparator: (a, b) => a.key - b.key });
|
|
592
|
+
const heap = new PriorityQueue([], { comparator: (a, b) => a.key - b.key });
|
|
593
593
|
heap.add({ key: 0, value: srcVertex });
|
|
594
594
|
distMap.set(srcVertex, 0);
|
|
595
595
|
preMap.set(srcVertex, null);
|
|
@@ -5,15 +5,12 @@
|
|
|
5
5
|
* @license MIT License
|
|
6
6
|
*/
|
|
7
7
|
import type { Comparator, DFSOrderPattern } from '../../types';
|
|
8
|
+
import { HeapOptions } from "../../types";
|
|
8
9
|
export declare class Heap<E = any> {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
elements?: E[];
|
|
12
|
-
});
|
|
10
|
+
options: HeapOptions<E>;
|
|
11
|
+
constructor(elements?: Iterable<E>, options?: HeapOptions<E>);
|
|
13
12
|
protected _elements: E[];
|
|
14
13
|
get elements(): E[];
|
|
15
|
-
protected _comparator: Comparator<E>;
|
|
16
|
-
get comparator(): Comparator<E>;
|
|
17
14
|
/**
|
|
18
15
|
* Get the size (number of elements) of the heap.
|
|
19
16
|
*/
|
|
@@ -26,10 +23,10 @@ export declare class Heap<E = any> {
|
|
|
26
23
|
/**
|
|
27
24
|
* Static method that creates a binary heap from an array of elements and a comparison function.
|
|
28
25
|
* @returns A new Heap instance.
|
|
26
|
+
* @param elements
|
|
29
27
|
* @param options
|
|
30
28
|
*/
|
|
31
|
-
static heapify<E>(options: {
|
|
32
|
-
elements: E[];
|
|
29
|
+
static heapify<E>(elements: Iterable<E>, options: {
|
|
33
30
|
comparator: Comparator<E>;
|
|
34
31
|
}): Heap<E>;
|
|
35
32
|
/**
|
|
@@ -204,6 +201,11 @@ export declare class Heap<E = any> {
|
|
|
204
201
|
* Time Complexity: O(log n)
|
|
205
202
|
* Space Complexity: O(1)
|
|
206
203
|
*/
|
|
204
|
+
print(): void;
|
|
205
|
+
/**
|
|
206
|
+
* Time Complexity: O(n)
|
|
207
|
+
* Space Complexity: O(1)
|
|
208
|
+
*/
|
|
207
209
|
/**
|
|
208
210
|
* Time Complexity: O(log n)
|
|
209
211
|
* Space Complexity: O(1)
|
|
@@ -212,10 +214,6 @@ export declare class Heap<E = any> {
|
|
|
212
214
|
* @param index - The index of the newly added element.
|
|
213
215
|
*/
|
|
214
216
|
protected _bubbleUp(index: number): void;
|
|
215
|
-
/**
|
|
216
|
-
* Time Complexity: O(n)
|
|
217
|
-
* Space Complexity: O(1)
|
|
218
|
-
*/
|
|
219
217
|
/**
|
|
220
218
|
* Time Complexity: O(log n)
|
|
221
219
|
* Space Complexity: O(1)
|
|
@@ -349,18 +347,18 @@ export declare class FibonacciHeap<E> {
|
|
|
349
347
|
*/
|
|
350
348
|
merge(heapToMerge: FibonacciHeap<E>): void;
|
|
351
349
|
/**
|
|
352
|
-
*
|
|
353
|
-
* @param
|
|
354
|
-
* @param {E} b
|
|
350
|
+
* Create a new node.
|
|
351
|
+
* @param element
|
|
355
352
|
* @protected
|
|
356
353
|
*/
|
|
357
|
-
|
|
354
|
+
createNode(element: E): FibonacciHeapNode<E>;
|
|
358
355
|
/**
|
|
359
|
-
*
|
|
360
|
-
* @param
|
|
356
|
+
* Default comparator function used by the heap.
|
|
357
|
+
* @param {E} a
|
|
358
|
+
* @param {E} b
|
|
361
359
|
* @protected
|
|
362
360
|
*/
|
|
363
|
-
protected
|
|
361
|
+
protected _defaultComparator(a: E, b: E): number;
|
|
364
362
|
/**
|
|
365
363
|
* Time Complexity: O(1)
|
|
366
364
|
* Space Complexity: O(1)
|
|
@@ -399,7 +397,7 @@ export declare class FibonacciHeap<E> {
|
|
|
399
397
|
* @param x
|
|
400
398
|
* @protected
|
|
401
399
|
*/
|
|
402
|
-
protected
|
|
400
|
+
protected _link(y: FibonacciHeapNode<E>, x: FibonacciHeapNode<E>): void;
|
|
403
401
|
/**
|
|
404
402
|
* Time Complexity: O(n log n), where n is the number of elements in the heap.
|
|
405
403
|
* Space Complexity: O(n)
|
|
@@ -411,5 +409,5 @@ export declare class FibonacciHeap<E> {
|
|
|
411
409
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
412
410
|
* @protected
|
|
413
411
|
*/
|
|
414
|
-
protected
|
|
412
|
+
protected _consolidate(): void;
|
|
415
413
|
}
|
|
@@ -5,21 +5,35 @@
|
|
|
5
5
|
* @license MIT License
|
|
6
6
|
*/
|
|
7
7
|
export class Heap {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
options;
|
|
9
|
+
constructor(elements, options) {
|
|
10
|
+
const defaultComparator = (a, b) => {
|
|
11
|
+
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
12
|
+
throw new Error('The a, b params of compare function must be number');
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
return a - b;
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
if (options) {
|
|
19
|
+
this.options = options;
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
this.options = {
|
|
23
|
+
comparator: defaultComparator
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
if (elements) {
|
|
27
|
+
for (const el of elements) {
|
|
28
|
+
this.push(el);
|
|
29
|
+
}
|
|
30
|
+
// this.fix();
|
|
13
31
|
}
|
|
14
32
|
}
|
|
15
33
|
_elements = [];
|
|
16
34
|
get elements() {
|
|
17
35
|
return this._elements;
|
|
18
36
|
}
|
|
19
|
-
_comparator;
|
|
20
|
-
get comparator() {
|
|
21
|
-
return this._comparator;
|
|
22
|
-
}
|
|
23
37
|
/**
|
|
24
38
|
* Get the size (number of elements) of the heap.
|
|
25
39
|
*/
|
|
@@ -36,10 +50,11 @@ export class Heap {
|
|
|
36
50
|
/**
|
|
37
51
|
* Static method that creates a binary heap from an array of elements and a comparison function.
|
|
38
52
|
* @returns A new Heap instance.
|
|
53
|
+
* @param elements
|
|
39
54
|
* @param options
|
|
40
55
|
*/
|
|
41
|
-
static heapify(options) {
|
|
42
|
-
return new Heap(options);
|
|
56
|
+
static heapify(elements, options) {
|
|
57
|
+
return new Heap(elements, options);
|
|
43
58
|
}
|
|
44
59
|
/**
|
|
45
60
|
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
@@ -253,7 +268,7 @@ export class Heap {
|
|
|
253
268
|
* @returns A new Heap instance containing the same elements.
|
|
254
269
|
*/
|
|
255
270
|
clone() {
|
|
256
|
-
const clonedHeap = new Heap(
|
|
271
|
+
const clonedHeap = new Heap([], this.options);
|
|
257
272
|
clonedHeap._elements = [...this.elements];
|
|
258
273
|
return clonedHeap;
|
|
259
274
|
}
|
|
@@ -305,7 +320,7 @@ export class Heap {
|
|
|
305
320
|
}
|
|
306
321
|
}
|
|
307
322
|
filter(predicate) {
|
|
308
|
-
const filteredHeap = new Heap(
|
|
323
|
+
const filteredHeap = new Heap([], this.options);
|
|
309
324
|
let index = 0;
|
|
310
325
|
for (const el of this) {
|
|
311
326
|
if (predicate(el, index, this)) {
|
|
@@ -316,7 +331,7 @@ export class Heap {
|
|
|
316
331
|
return filteredHeap;
|
|
317
332
|
}
|
|
318
333
|
map(callback, comparator) {
|
|
319
|
-
const mappedHeap = new Heap({ comparator: comparator });
|
|
334
|
+
const mappedHeap = new Heap([], { comparator: comparator });
|
|
320
335
|
let index = 0;
|
|
321
336
|
for (const el of this) {
|
|
322
337
|
mappedHeap.add(callback(el, index, this));
|
|
@@ -337,6 +352,13 @@ export class Heap {
|
|
|
337
352
|
* Time Complexity: O(log n)
|
|
338
353
|
* Space Complexity: O(1)
|
|
339
354
|
*/
|
|
355
|
+
print() {
|
|
356
|
+
console.log([...this]);
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Time Complexity: O(n)
|
|
360
|
+
* Space Complexity: O(1)
|
|
361
|
+
*/
|
|
340
362
|
/**
|
|
341
363
|
* Time Complexity: O(log n)
|
|
342
364
|
* Space Complexity: O(1)
|
|
@@ -349,17 +371,13 @@ export class Heap {
|
|
|
349
371
|
while (index > 0) {
|
|
350
372
|
const parent = (index - 1) >> 1;
|
|
351
373
|
const parentItem = this.elements[parent];
|
|
352
|
-
if (this.
|
|
374
|
+
if (this.options.comparator(parentItem, element) <= 0)
|
|
353
375
|
break;
|
|
354
376
|
this.elements[index] = parentItem;
|
|
355
377
|
index = parent;
|
|
356
378
|
}
|
|
357
379
|
this.elements[index] = element;
|
|
358
380
|
}
|
|
359
|
-
/**
|
|
360
|
-
* Time Complexity: O(n)
|
|
361
|
-
* Space Complexity: O(1)
|
|
362
|
-
*/
|
|
363
381
|
/**
|
|
364
382
|
* Time Complexity: O(log n)
|
|
365
383
|
* Space Complexity: O(1)
|
|
@@ -375,11 +393,11 @@ export class Heap {
|
|
|
375
393
|
const right = left + 1;
|
|
376
394
|
let minItem = this.elements[left];
|
|
377
395
|
if (right < this.elements.length &&
|
|
378
|
-
this.
|
|
396
|
+
this.options.comparator(minItem, this.elements[right]) > 0) {
|
|
379
397
|
left = right;
|
|
380
398
|
minItem = this.elements[right];
|
|
381
399
|
}
|
|
382
|
-
if (this.
|
|
400
|
+
if (this.options.comparator(minItem, element) >= 0)
|
|
383
401
|
break;
|
|
384
402
|
this.elements[index] = minItem;
|
|
385
403
|
index = left;
|
|
@@ -404,7 +422,7 @@ export class FibonacciHeapNode {
|
|
|
404
422
|
export class FibonacciHeap {
|
|
405
423
|
constructor(comparator) {
|
|
406
424
|
this.clear();
|
|
407
|
-
this._comparator = comparator || this.
|
|
425
|
+
this._comparator = comparator || this._defaultComparator;
|
|
408
426
|
if (typeof this.comparator !== 'function') {
|
|
409
427
|
throw new Error('FibonacciHeap constructor: given comparator should be a function.');
|
|
410
428
|
}
|
|
@@ -579,7 +597,7 @@ export class FibonacciHeap {
|
|
|
579
597
|
}
|
|
580
598
|
else {
|
|
581
599
|
this._min = z.right;
|
|
582
|
-
this.
|
|
600
|
+
this._consolidate();
|
|
583
601
|
}
|
|
584
602
|
this._size--;
|
|
585
603
|
return z.element;
|
|
@@ -619,27 +637,27 @@ export class FibonacciHeap {
|
|
|
619
637
|
// Clear the heap that was merged
|
|
620
638
|
heapToMerge.clear();
|
|
621
639
|
}
|
|
640
|
+
/**
|
|
641
|
+
* Create a new node.
|
|
642
|
+
* @param element
|
|
643
|
+
* @protected
|
|
644
|
+
*/
|
|
645
|
+
createNode(element) {
|
|
646
|
+
return new FibonacciHeapNode(element);
|
|
647
|
+
}
|
|
622
648
|
/**
|
|
623
649
|
* Default comparator function used by the heap.
|
|
624
650
|
* @param {E} a
|
|
625
651
|
* @param {E} b
|
|
626
652
|
* @protected
|
|
627
653
|
*/
|
|
628
|
-
|
|
654
|
+
_defaultComparator(a, b) {
|
|
629
655
|
if (a < b)
|
|
630
656
|
return -1;
|
|
631
657
|
if (a > b)
|
|
632
658
|
return 1;
|
|
633
659
|
return 0;
|
|
634
660
|
}
|
|
635
|
-
/**
|
|
636
|
-
* Create a new node.
|
|
637
|
-
* @param element
|
|
638
|
-
* @protected
|
|
639
|
-
*/
|
|
640
|
-
createNode(element) {
|
|
641
|
-
return new FibonacciHeapNode(element);
|
|
642
|
-
}
|
|
643
661
|
/**
|
|
644
662
|
* Time Complexity: O(1)
|
|
645
663
|
* Space Complexity: O(1)
|
|
@@ -695,7 +713,7 @@ export class FibonacciHeap {
|
|
|
695
713
|
* @param x
|
|
696
714
|
* @protected
|
|
697
715
|
*/
|
|
698
|
-
|
|
716
|
+
_link(y, x) {
|
|
699
717
|
this.removeFromRoot(y);
|
|
700
718
|
y.left = y;
|
|
701
719
|
y.right = y;
|
|
@@ -714,7 +732,7 @@ export class FibonacciHeap {
|
|
|
714
732
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
715
733
|
* @protected
|
|
716
734
|
*/
|
|
717
|
-
|
|
735
|
+
_consolidate() {
|
|
718
736
|
const A = new Array(this.size);
|
|
719
737
|
const elements = this.consumeLinkedList(this.root);
|
|
720
738
|
let x, y, d, t;
|
|
@@ -728,7 +746,7 @@ export class FibonacciHeap {
|
|
|
728
746
|
x = y;
|
|
729
747
|
y = t;
|
|
730
748
|
}
|
|
731
|
-
this.
|
|
749
|
+
this._link(y, x);
|
|
732
750
|
A[d] = undefined;
|
|
733
751
|
d++;
|
|
734
752
|
}
|
|
@@ -6,10 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { Heap } from './heap';
|
|
9
|
-
import type {
|
|
9
|
+
import type { HeapOptions } from '../../types';
|
|
10
10
|
export declare class MaxHeap<E = any> extends Heap<E> {
|
|
11
|
-
constructor(options?:
|
|
12
|
-
comparator: Comparator<E>;
|
|
13
|
-
nodes?: E[];
|
|
14
|
-
});
|
|
11
|
+
constructor(elements?: Iterable<E>, options?: HeapOptions<E>);
|
|
15
12
|
}
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { Heap } from './heap';
|
|
9
9
|
export class MaxHeap extends Heap {
|
|
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 MaxHeap extends Heap {
|
|
|
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 type {
|
|
9
|
+
import type { HeapOptions } from '../../types';
|
|
10
10
|
export declare class MinHeap<E = any> extends Heap<E> {
|
|
11
|
-
constructor(options?:
|
|
12
|
-
comparator: Comparator<E>;
|
|
13
|
-
nodes?: E[];
|
|
14
|
-
});
|
|
11
|
+
constructor(elements?: Iterable<E>, options?: HeapOptions<E>);
|
|
15
12
|
}
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { Heap } from './heap';
|
|
9
9
|
export class MinHeap extends Heap {
|
|
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 MinHeap extends Heap {
|
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
}) {
|
|
20
|
-
super(options);
|
|
20
|
+
super(elements, options);
|
|
21
21
|
}
|
|
22
22
|
}
|
|
@@ -20,7 +20,7 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
20
20
|
/**
|
|
21
21
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
22
22
|
*/
|
|
23
|
-
constructor();
|
|
23
|
+
constructor(elements?: Iterable<E>);
|
|
24
24
|
protected _head: DoublyLinkedListNode<E> | null;
|
|
25
25
|
get head(): DoublyLinkedListNode<E> | null;
|
|
26
26
|
protected _tail: DoublyLinkedListNode<E> | null;
|
|
@@ -453,4 +453,5 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
453
453
|
* elements in the linked list.
|
|
454
454
|
*/
|
|
455
455
|
reduce<T>(callback: (accumulator: T, value: E, index: number, list: DoublyLinkedList<E>) => T, initialValue: T): T;
|
|
456
|
+
print(): void;
|
|
456
457
|
}
|
|
@@ -24,10 +24,15 @@ export class DoublyLinkedList {
|
|
|
24
24
|
/**
|
|
25
25
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
26
26
|
*/
|
|
27
|
-
constructor() {
|
|
27
|
+
constructor(elements) {
|
|
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
|
+
}
|
|
35
|
+
}
|
|
31
36
|
}
|
|
32
37
|
_head;
|
|
33
38
|
get head() {
|
|
@@ -766,4 +771,7 @@ export class DoublyLinkedList {
|
|
|
766
771
|
}
|
|
767
772
|
return accumulator;
|
|
768
773
|
}
|
|
774
|
+
print() {
|
|
775
|
+
console.log([...this]);
|
|
776
|
+
}
|
|
769
777
|
}
|
|
@@ -19,7 +19,7 @@ export declare class SinglyLinkedList<E = any> {
|
|
|
19
19
|
/**
|
|
20
20
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
21
21
|
*/
|
|
22
|
-
constructor();
|
|
22
|
+
constructor(elements?: Iterable<E>);
|
|
23
23
|
protected _head: SinglyLinkedListNode<E> | null;
|
|
24
24
|
get head(): SinglyLinkedListNode<E> | null;
|
|
25
25
|
protected _tail: SinglyLinkedListNode<E> | null;
|
|
@@ -411,4 +411,5 @@ export declare class SinglyLinkedList<E = any> {
|
|
|
411
411
|
* elements in the linked list.
|
|
412
412
|
*/
|
|
413
413
|
reduce<T>(callback: (accumulator: T, value: E, index: number, list: SinglyLinkedList<E>) => T, initialValue: T): T;
|
|
414
|
+
print(): void;
|
|
414
415
|
}
|