min-heap-typed 1.49.1 → 1.49.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/base/iterable-base.d.ts +11 -0
- package/dist/data-structures/base/iterable-base.js +21 -0
- package/dist/data-structures/graph/abstract-graph.d.ts +7 -7
- package/dist/data-structures/graph/abstract-graph.js +43 -12
- package/dist/data-structures/graph/directed-graph.d.ts +2 -2
- package/dist/data-structures/graph/directed-graph.js +2 -2
- package/dist/data-structures/graph/undirected-graph.d.ts +1 -1
- package/dist/data-structures/graph/undirected-graph.js +1 -1
- package/dist/data-structures/hash/hash-map.d.ts +9 -9
- package/dist/data-structures/hash/hash-map.js +16 -15
- package/dist/data-structures/heap/heap.d.ts +6 -35
- package/dist/data-structures/heap/heap.js +10 -42
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +99 -105
- package/dist/data-structures/linked-list/doubly-linked-list.js +143 -146
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +16 -21
- package/dist/data-structures/linked-list/singly-linked-list.js +42 -42
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +25 -25
- package/dist/data-structures/linked-list/skip-linked-list.js +36 -36
- package/dist/data-structures/queue/deque.d.ts +70 -75
- package/dist/data-structures/queue/deque.js +100 -110
- package/dist/data-structures/queue/queue.d.ts +37 -38
- package/dist/data-structures/queue/queue.js +46 -49
- package/dist/data-structures/stack/stack.d.ts +2 -3
- package/dist/data-structures/stack/stack.js +2 -5
- package/dist/data-structures/trie/trie.d.ts +1 -2
- package/dist/data-structures/trie/trie.js +2 -5
- package/package.json +2 -2
- package/src/data-structures/base/iterable-base.ts +24 -0
- package/src/data-structures/graph/abstract-graph.ts +55 -14
- package/src/data-structures/graph/directed-graph.ts +3 -2
- package/src/data-structures/graph/undirected-graph.ts +1 -1
- package/src/data-structures/hash/hash-map.ts +27 -28
- package/src/data-structures/heap/heap.ts +19 -57
- package/src/data-structures/linked-list/doubly-linked-list.ts +157 -161
- package/src/data-structures/linked-list/singly-linked-list.ts +49 -49
- package/src/data-structures/linked-list/skip-linked-list.ts +40 -40
- package/src/data-structures/queue/deque.ts +122 -135
- package/src/data-structures/queue/queue.ts +54 -58
- package/src/data-structures/stack/stack.ts +4 -8
- package/src/data-structures/trie/trie.ts +5 -9
|
@@ -126,6 +126,12 @@ export declare abstract class IterableEntryBase<K = any, V = any> {
|
|
|
126
126
|
* all the elements in the collection.
|
|
127
127
|
*/
|
|
128
128
|
reduce<U>(callbackfn: ReduceEntryCallback<K, V, U>, initialValue: U): U;
|
|
129
|
+
hasValue(value: V): boolean;
|
|
130
|
+
/**
|
|
131
|
+
* Time Complexity: O(n)
|
|
132
|
+
* Space Complexity: O(n)
|
|
133
|
+
*/
|
|
134
|
+
print(): void;
|
|
129
135
|
protected abstract _getIterator(...args: any[]): IterableIterator<[K, V]>;
|
|
130
136
|
}
|
|
131
137
|
export declare abstract class IterableElementBase<V> {
|
|
@@ -228,5 +234,10 @@ export declare abstract class IterableElementBase<V> {
|
|
|
228
234
|
* all the elements in the array and applying the callback function to each element.
|
|
229
235
|
*/
|
|
230
236
|
reduce<U>(callbackfn: ReduceElementCallback<V, U>, initialValue: U): U;
|
|
237
|
+
/**
|
|
238
|
+
* Time Complexity: O(n)
|
|
239
|
+
* Space Complexity: O(n)
|
|
240
|
+
*/
|
|
241
|
+
print(): void;
|
|
231
242
|
protected abstract _getIterator(...args: any[]): IterableIterator<V>;
|
|
232
243
|
}
|
|
@@ -172,6 +172,20 @@ class IterableEntryBase {
|
|
|
172
172
|
}
|
|
173
173
|
return accumulator;
|
|
174
174
|
}
|
|
175
|
+
hasValue(value) {
|
|
176
|
+
for (const [, elementValue] of this) {
|
|
177
|
+
if (elementValue === value)
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Time Complexity: O(n)
|
|
184
|
+
* Space Complexity: O(n)
|
|
185
|
+
*/
|
|
186
|
+
print() {
|
|
187
|
+
console.log([...this]);
|
|
188
|
+
}
|
|
175
189
|
}
|
|
176
190
|
exports.IterableEntryBase = IterableEntryBase;
|
|
177
191
|
class IterableElementBase {
|
|
@@ -308,5 +322,12 @@ class IterableElementBase {
|
|
|
308
322
|
}
|
|
309
323
|
return accumulator;
|
|
310
324
|
}
|
|
325
|
+
/**
|
|
326
|
+
* Time Complexity: O(n)
|
|
327
|
+
* Space Complexity: O(n)
|
|
328
|
+
*/
|
|
329
|
+
print() {
|
|
330
|
+
console.log([...this]);
|
|
331
|
+
}
|
|
311
332
|
}
|
|
312
333
|
exports.IterableElementBase = IterableElementBase;
|
|
@@ -432,11 +432,6 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
432
432
|
* type `number`.
|
|
433
433
|
*/
|
|
434
434
|
getLowMap(): Map<VO, number>;
|
|
435
|
-
/**
|
|
436
|
-
* The function `getCycles` returns a map of cycles found using the Tarjan algorithm.
|
|
437
|
-
* @returns The function `getCycles()` is returning a `Map<number, VO[]>`.
|
|
438
|
-
*/
|
|
439
|
-
getCycles(): Map<number, VO[]>;
|
|
440
435
|
/**
|
|
441
436
|
* The function "getCutVertexes" returns an array of cut vertexes using the Tarjan algorithm.
|
|
442
437
|
* @returns an array of VO objects, specifically the cut vertexes.
|
|
@@ -453,6 +448,11 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
453
448
|
* @returns the bridges found using the Tarjan algorithm.
|
|
454
449
|
*/
|
|
455
450
|
getBridges(): EO[];
|
|
451
|
+
/**
|
|
452
|
+
* O(V+E+C)
|
|
453
|
+
* O(V+C)
|
|
454
|
+
*/
|
|
455
|
+
getCycles(isInclude2Cycle?: boolean): VertexKey[][];
|
|
456
456
|
/**
|
|
457
457
|
* Time Complexity: O(n)
|
|
458
458
|
* Space Complexity: O(n)
|
|
@@ -493,8 +493,8 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
493
493
|
*/
|
|
494
494
|
map<T>(callback: EntryCallback<VertexKey, V | undefined, T>, thisArg?: any): T[];
|
|
495
495
|
protected _getIterator(): IterableIterator<[VertexKey, V | undefined]>;
|
|
496
|
-
protected abstract
|
|
497
|
-
protected
|
|
496
|
+
protected abstract _addEdge(edge: EO): boolean;
|
|
497
|
+
protected _addVertex(newVertex: VO): boolean;
|
|
498
498
|
protected _getVertex(vertexOrKey: VertexKey | VO): VO | undefined;
|
|
499
499
|
protected _getVertexKey(vertexOrKey: VO | VertexKey): VertexKey;
|
|
500
500
|
}
|
|
@@ -86,11 +86,11 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
86
86
|
*/
|
|
87
87
|
addVertex(keyOrVertex, value) {
|
|
88
88
|
if (keyOrVertex instanceof AbstractVertex) {
|
|
89
|
-
return this.
|
|
89
|
+
return this._addVertex(keyOrVertex);
|
|
90
90
|
}
|
|
91
91
|
else {
|
|
92
92
|
const newVertex = this.createVertex(keyOrVertex, value);
|
|
93
|
-
return this.
|
|
93
|
+
return this._addVertex(newVertex);
|
|
94
94
|
}
|
|
95
95
|
}
|
|
96
96
|
isVertexKey(potentialKey) {
|
|
@@ -160,7 +160,7 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
160
160
|
*/
|
|
161
161
|
addEdge(srcOrEdge, dest, weight, value) {
|
|
162
162
|
if (srcOrEdge instanceof AbstractEdge) {
|
|
163
|
-
return this.
|
|
163
|
+
return this._addEdge(srcOrEdge);
|
|
164
164
|
}
|
|
165
165
|
else {
|
|
166
166
|
if (dest instanceof AbstractVertex || typeof dest === 'string' || typeof dest === 'number') {
|
|
@@ -171,7 +171,7 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
171
171
|
if (dest instanceof AbstractVertex)
|
|
172
172
|
dest = dest.key;
|
|
173
173
|
const newEdge = this.createEdge(srcOrEdge, dest, weight, value);
|
|
174
|
-
return this.
|
|
174
|
+
return this._addEdge(newEdge);
|
|
175
175
|
}
|
|
176
176
|
else {
|
|
177
177
|
throw new Error('dest must be a Vertex or vertex key while srcOrEdge is an Edge');
|
|
@@ -1001,13 +1001,6 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
1001
1001
|
getLowMap() {
|
|
1002
1002
|
return this.tarjan(false, false, false, false).lowMap;
|
|
1003
1003
|
}
|
|
1004
|
-
/**
|
|
1005
|
-
* The function `getCycles` returns a map of cycles found using the Tarjan algorithm.
|
|
1006
|
-
* @returns The function `getCycles()` is returning a `Map<number, VO[]>`.
|
|
1007
|
-
*/
|
|
1008
|
-
getCycles() {
|
|
1009
|
-
return this.tarjan(false, false, false, true).cycles;
|
|
1010
|
-
}
|
|
1011
1004
|
/**
|
|
1012
1005
|
* The function "getCutVertexes" returns an array of cut vertexes using the Tarjan algorithm.
|
|
1013
1006
|
* @returns an array of VO objects, specifically the cut vertexes.
|
|
@@ -1030,6 +1023,44 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
1030
1023
|
getBridges() {
|
|
1031
1024
|
return this.tarjan(false, true, false, false).bridges;
|
|
1032
1025
|
}
|
|
1026
|
+
/**
|
|
1027
|
+
* O(V+E+C)
|
|
1028
|
+
* O(V+C)
|
|
1029
|
+
*/
|
|
1030
|
+
getCycles(isInclude2Cycle = false) {
|
|
1031
|
+
const cycles = [];
|
|
1032
|
+
const visited = new Set();
|
|
1033
|
+
const dfs = (vertex, currentPath, visited) => {
|
|
1034
|
+
if (visited.has(vertex)) {
|
|
1035
|
+
if ((!isInclude2Cycle && currentPath.length > 2 || isInclude2Cycle && currentPath.length >= 2) && currentPath[0] === vertex.key) {
|
|
1036
|
+
cycles.push([...currentPath]);
|
|
1037
|
+
}
|
|
1038
|
+
return;
|
|
1039
|
+
}
|
|
1040
|
+
visited.add(vertex);
|
|
1041
|
+
currentPath.push(vertex.key);
|
|
1042
|
+
for (const neighbor of this.getNeighbors(vertex)) {
|
|
1043
|
+
neighbor && dfs(neighbor, currentPath, visited);
|
|
1044
|
+
}
|
|
1045
|
+
visited.delete(vertex);
|
|
1046
|
+
currentPath.pop();
|
|
1047
|
+
};
|
|
1048
|
+
for (const vertex of this.vertexMap.values()) {
|
|
1049
|
+
dfs(vertex, [], visited);
|
|
1050
|
+
}
|
|
1051
|
+
// Use a set to eliminate duplicate cycles
|
|
1052
|
+
const uniqueCycles = new Map();
|
|
1053
|
+
for (const cycle of cycles) {
|
|
1054
|
+
const sorted = [...cycle].sort().toString();
|
|
1055
|
+
if (uniqueCycles.has(sorted))
|
|
1056
|
+
continue;
|
|
1057
|
+
else {
|
|
1058
|
+
uniqueCycles.set(sorted, cycle);
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
// Convert the unique cycles back to an array
|
|
1062
|
+
return [...uniqueCycles].map(cycleString => cycleString[1]);
|
|
1063
|
+
}
|
|
1033
1064
|
/**
|
|
1034
1065
|
* Time Complexity: O(n)
|
|
1035
1066
|
* Space Complexity: O(n)
|
|
@@ -1092,7 +1123,7 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
1092
1123
|
yield [vertex.key, vertex.value];
|
|
1093
1124
|
}
|
|
1094
1125
|
}
|
|
1095
|
-
|
|
1126
|
+
_addVertex(newVertex) {
|
|
1096
1127
|
if (this.hasVertex(newVertex)) {
|
|
1097
1128
|
return false;
|
|
1098
1129
|
// throw (new Error('Duplicated vertex key is not allowed'));
|
|
@@ -335,11 +335,11 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
335
335
|
* Time Complexity: O(1)
|
|
336
336
|
* Space Complexity: O(1)
|
|
337
337
|
*
|
|
338
|
-
* The function `
|
|
338
|
+
* The function `_addEdge` adds an edge to a graph if the source and destination vertexMap exist.
|
|
339
339
|
* @param {EO} edge - The parameter `edge` is of type `EO`, which represents an edge in a graph. It is the edge that
|
|
340
340
|
* needs to be added to the graph.
|
|
341
341
|
* @returns a boolean value. It returns true if the edge was successfully added to the graph, and false if either the
|
|
342
342
|
* source or destination vertex does not exist in the graph.
|
|
343
343
|
*/
|
|
344
|
-
protected
|
|
344
|
+
protected _addEdge(edge: EO): boolean;
|
|
345
345
|
}
|
|
@@ -531,13 +531,13 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
531
531
|
* Time Complexity: O(1)
|
|
532
532
|
* Space Complexity: O(1)
|
|
533
533
|
*
|
|
534
|
-
* The function `
|
|
534
|
+
* The function `_addEdge` adds an edge to a graph if the source and destination vertexMap exist.
|
|
535
535
|
* @param {EO} edge - The parameter `edge` is of type `EO`, which represents an edge in a graph. It is the edge that
|
|
536
536
|
* needs to be added to the graph.
|
|
537
537
|
* @returns a boolean value. It returns true if the edge was successfully added to the graph, and false if either the
|
|
538
538
|
* source or destination vertex does not exist in the graph.
|
|
539
539
|
*/
|
|
540
|
-
|
|
540
|
+
_addEdge(edge) {
|
|
541
541
|
if (!(this.hasVertex(edge.src) && this.hasVertex(edge.dest))) {
|
|
542
542
|
return false;
|
|
543
543
|
}
|
|
@@ -205,5 +205,5 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
|
|
|
205
205
|
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
206
206
|
* @returns a boolean value.
|
|
207
207
|
*/
|
|
208
|
-
protected
|
|
208
|
+
protected _addEdge(edge: EO): boolean;
|
|
209
209
|
}
|
|
@@ -337,7 +337,7 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
337
337
|
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
338
338
|
* @returns a boolean value.
|
|
339
339
|
*/
|
|
340
|
-
|
|
340
|
+
_addEdge(edge) {
|
|
341
341
|
for (const end of edge.vertexMap) {
|
|
342
342
|
const endVertex = this._getVertex(end);
|
|
343
343
|
if (endVertex === undefined)
|
|
@@ -42,13 +42,13 @@ export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
42
42
|
* @param {V} value - The value parameter represents the value that you want to associate with the
|
|
43
43
|
* key in the data structure.
|
|
44
44
|
*/
|
|
45
|
-
set(key: K, value: V):
|
|
45
|
+
set(key: K, value: V): boolean;
|
|
46
46
|
/**
|
|
47
47
|
* The function "setMany" sets multiple key-value pairs in a map.
|
|
48
48
|
* @param elements - The `elements` parameter is an iterable containing key-value pairs. Each
|
|
49
49
|
* key-value pair is represented as an array with two elements: the key and the value.
|
|
50
50
|
*/
|
|
51
|
-
setMany(elements: Iterable<[K, V]>):
|
|
51
|
+
setMany(elements: Iterable<[K, V]>): boolean[];
|
|
52
52
|
/**
|
|
53
53
|
* The `get` function retrieves a value from a map based on a given key, either from an object map or
|
|
54
54
|
* a string map.
|
|
@@ -114,6 +114,7 @@ export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
114
114
|
*/
|
|
115
115
|
filter(predicate: EntryCallback<K, V, boolean>, thisArg?: any): HashMap<K, V>;
|
|
116
116
|
print(): void;
|
|
117
|
+
put(key: K, value: V): boolean;
|
|
117
118
|
/**
|
|
118
119
|
* The function returns an iterator that yields key-value pairs from both an object store and an
|
|
119
120
|
* object map.
|
|
@@ -178,10 +179,9 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
|
|
|
178
179
|
* value associated with the key being set in the data structure.
|
|
179
180
|
* @returns the size of the data structure after the key-value pair has been set.
|
|
180
181
|
*/
|
|
181
|
-
set(key: K, value?: V):
|
|
182
|
+
set(key: K, value?: V): boolean;
|
|
182
183
|
has(key: K): boolean;
|
|
183
|
-
|
|
184
|
-
setMany(entries: Iterable<[K, V]>): void;
|
|
184
|
+
setMany(entries: Iterable<[K, V]>): boolean[];
|
|
185
185
|
/**
|
|
186
186
|
* Time Complexity: O(1)
|
|
187
187
|
* Space Complexity: O(1)
|
|
@@ -207,7 +207,7 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
|
|
|
207
207
|
* the specified index in the data structure. The key-value pair is represented as a tuple `[K, V]`,
|
|
208
208
|
* where `K` is the key and `V` is the value.
|
|
209
209
|
*/
|
|
210
|
-
getAt(index: number):
|
|
210
|
+
getAt(index: number): V | undefined;
|
|
211
211
|
/**
|
|
212
212
|
* Time Complexity: O(1)
|
|
213
213
|
* Space Complexity: O(1)
|
|
@@ -228,7 +228,7 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
|
|
|
228
228
|
* deleted in the linked list.
|
|
229
229
|
* @returns The size of the list after deleting the element at the specified index.
|
|
230
230
|
*/
|
|
231
|
-
deleteAt(index: number):
|
|
231
|
+
deleteAt(index: number): boolean;
|
|
232
232
|
/**
|
|
233
233
|
* Time Complexity: O(1)
|
|
234
234
|
* Space Complexity: O(1)
|
|
@@ -288,7 +288,7 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
|
|
|
288
288
|
* function.
|
|
289
289
|
*/
|
|
290
290
|
map<NV>(callback: EntryCallback<K, V, NV>, thisArg?: any): LinkedHashMap<K, NV>;
|
|
291
|
-
|
|
291
|
+
put(key: K, value: V): boolean;
|
|
292
292
|
/**
|
|
293
293
|
* Time Complexity: O(n), where n is the number of elements in the LinkedHashMap.
|
|
294
294
|
* Space Complexity: O(1)
|
|
@@ -306,5 +306,5 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
|
|
|
306
306
|
* represents a node in a linked list. It contains a key-value pair and references to the previous
|
|
307
307
|
* and next nodes in the list.
|
|
308
308
|
*/
|
|
309
|
-
protected _deleteNode(node: HashMapLinkedNode<K, V | undefined>):
|
|
309
|
+
protected _deleteNode(node: HashMapLinkedNode<K, V | undefined>): boolean;
|
|
310
310
|
}
|
|
@@ -68,6 +68,7 @@ class HashMap extends base_1.IterableEntryBase {
|
|
|
68
68
|
}
|
|
69
69
|
this._store[strKey] = { key, value };
|
|
70
70
|
}
|
|
71
|
+
return true;
|
|
71
72
|
}
|
|
72
73
|
/**
|
|
73
74
|
* The function "setMany" sets multiple key-value pairs in a map.
|
|
@@ -75,8 +76,10 @@ class HashMap extends base_1.IterableEntryBase {
|
|
|
75
76
|
* key-value pair is represented as an array with two elements: the key and the value.
|
|
76
77
|
*/
|
|
77
78
|
setMany(elements) {
|
|
79
|
+
const results = [];
|
|
78
80
|
for (const [key, value] of elements)
|
|
79
|
-
this.set(key, value);
|
|
81
|
+
results.push(this.set(key, value));
|
|
82
|
+
return results;
|
|
80
83
|
}
|
|
81
84
|
/**
|
|
82
85
|
* The `get` function retrieves a value from a map based on a given key, either from an object map or
|
|
@@ -194,6 +197,9 @@ class HashMap extends base_1.IterableEntryBase {
|
|
|
194
197
|
print() {
|
|
195
198
|
console.log([...this.entries()]);
|
|
196
199
|
}
|
|
200
|
+
put(key, value) {
|
|
201
|
+
return this.set(key, value);
|
|
202
|
+
}
|
|
197
203
|
/**
|
|
198
204
|
* The function returns an iterator that yields key-value pairs from both an object store and an
|
|
199
205
|
* object map.
|
|
@@ -357,7 +363,7 @@ class LinkedHashMap extends base_1.IterableEntryBase {
|
|
|
357
363
|
this._sentinel.prev = node;
|
|
358
364
|
this._size++;
|
|
359
365
|
}
|
|
360
|
-
return
|
|
366
|
+
return true;
|
|
361
367
|
}
|
|
362
368
|
has(key) {
|
|
363
369
|
if ((0, utils_1.isWeakKey)(key)) {
|
|
@@ -369,18 +375,13 @@ class LinkedHashMap extends base_1.IterableEntryBase {
|
|
|
369
375
|
return hash in this._noObjMap;
|
|
370
376
|
}
|
|
371
377
|
}
|
|
372
|
-
hasValue(value) {
|
|
373
|
-
for (const [, elementValue] of this) {
|
|
374
|
-
if (elementValue === value)
|
|
375
|
-
return true;
|
|
376
|
-
}
|
|
377
|
-
return false;
|
|
378
|
-
}
|
|
379
378
|
setMany(entries) {
|
|
379
|
+
const results = [];
|
|
380
380
|
for (const entry of entries) {
|
|
381
381
|
const [key, value] = entry;
|
|
382
|
-
this.set(key, value);
|
|
382
|
+
results.push(this.set(key, value));
|
|
383
383
|
}
|
|
384
|
+
return results;
|
|
384
385
|
}
|
|
385
386
|
/**
|
|
386
387
|
* Time Complexity: O(1)
|
|
@@ -424,7 +425,7 @@ class LinkedHashMap extends base_1.IterableEntryBase {
|
|
|
424
425
|
while (index--) {
|
|
425
426
|
node = node.next;
|
|
426
427
|
}
|
|
427
|
-
return
|
|
428
|
+
return node.value;
|
|
428
429
|
}
|
|
429
430
|
/**
|
|
430
431
|
* Time Complexity: O(1)
|
|
@@ -477,8 +478,7 @@ class LinkedHashMap extends base_1.IterableEntryBase {
|
|
|
477
478
|
while (index--) {
|
|
478
479
|
node = node.next;
|
|
479
480
|
}
|
|
480
|
-
this._deleteNode(node);
|
|
481
|
-
return this._size;
|
|
481
|
+
return this._deleteNode(node);
|
|
482
482
|
}
|
|
483
483
|
/**
|
|
484
484
|
* Time Complexity: O(1)
|
|
@@ -571,8 +571,8 @@ class LinkedHashMap extends base_1.IterableEntryBase {
|
|
|
571
571
|
}
|
|
572
572
|
return mappedMap;
|
|
573
573
|
}
|
|
574
|
-
|
|
575
|
-
|
|
574
|
+
put(key, value) {
|
|
575
|
+
return this.set(key, value);
|
|
576
576
|
}
|
|
577
577
|
/**
|
|
578
578
|
* Time Complexity: O(n), where n is the number of elements in the LinkedHashMap.
|
|
@@ -608,6 +608,7 @@ class LinkedHashMap extends base_1.IterableEntryBase {
|
|
|
608
608
|
this._tail = prev;
|
|
609
609
|
}
|
|
610
610
|
this._size -= 1;
|
|
611
|
+
return true;
|
|
611
612
|
}
|
|
612
613
|
}
|
|
613
614
|
exports.LinkedHashMap = LinkedHashMap;
|
|
@@ -52,19 +52,7 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
|
|
|
52
52
|
* Insert an element into the heap and maintain the heap properties.
|
|
53
53
|
* @param element - The element to be inserted.
|
|
54
54
|
*/
|
|
55
|
-
add(element: E):
|
|
56
|
-
/**
|
|
57
|
-
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
58
|
-
* Space Complexity: O(1)
|
|
59
|
-
*/
|
|
60
|
-
/**
|
|
61
|
-
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
62
|
-
* Space Complexity: O(1)
|
|
63
|
-
*
|
|
64
|
-
* Insert an element into the heap and maintain the heap properties.
|
|
65
|
-
* @param element - The element to be inserted.
|
|
66
|
-
*/
|
|
67
|
-
push(element: E): Heap<E>;
|
|
55
|
+
add(element: E): boolean;
|
|
68
56
|
/**
|
|
69
57
|
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
70
58
|
* Space Complexity: O(1)
|
|
@@ -77,18 +65,6 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
|
|
|
77
65
|
* @returns The top element or undefined if the heap is empty.
|
|
78
66
|
*/
|
|
79
67
|
poll(): E | undefined;
|
|
80
|
-
/**
|
|
81
|
-
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
82
|
-
* Space Complexity: O(1)
|
|
83
|
-
*/
|
|
84
|
-
/**
|
|
85
|
-
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
86
|
-
* Space Complexity: O(1)
|
|
87
|
-
*
|
|
88
|
-
* Remove and return the top element (smallest or largest element) from the heap.
|
|
89
|
-
* @returns The top element or undefined if the heap is empty.
|
|
90
|
-
*/
|
|
91
|
-
pop(): E | undefined;
|
|
92
68
|
/**
|
|
93
69
|
* Peek at the top element of the heap without removing it.
|
|
94
70
|
* @returns The top element or undefined if the heap is empty.
|
|
@@ -114,7 +90,7 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
|
|
|
114
90
|
* Clear and add elements of the heap
|
|
115
91
|
* @param elements
|
|
116
92
|
*/
|
|
117
|
-
refill(elements: E[]):
|
|
93
|
+
refill(elements: E[]): boolean[];
|
|
118
94
|
/**
|
|
119
95
|
* Time Complexity: O(n), where n is the number of elements in the heap.
|
|
120
96
|
* Space Complexity: O(1)
|
|
@@ -203,7 +179,7 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
|
|
|
203
179
|
*
|
|
204
180
|
* Fix the entire heap to maintain heap properties.
|
|
205
181
|
*/
|
|
206
|
-
fix():
|
|
182
|
+
fix(): boolean[];
|
|
207
183
|
/**
|
|
208
184
|
* Time Complexity: O(n)
|
|
209
185
|
* Space Complexity: O(n)
|
|
@@ -250,12 +226,7 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
|
|
|
250
226
|
* original Heap.
|
|
251
227
|
*/
|
|
252
228
|
map<T>(callback: ElementCallback<E, T>, comparator: Comparator<T>, thisArg?: any): Heap<T>;
|
|
253
|
-
|
|
254
|
-
* Time Complexity: O(log n)
|
|
255
|
-
* Space Complexity: O(1)
|
|
256
|
-
*/
|
|
257
|
-
print(): void;
|
|
258
|
-
protected _getIterator(): Generator<E, void, unknown>;
|
|
229
|
+
protected _getIterator(): IterableIterator<E>;
|
|
259
230
|
/**
|
|
260
231
|
* Time Complexity: O(n)
|
|
261
232
|
* Space Complexity: O(1)
|
|
@@ -267,7 +238,7 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
|
|
|
267
238
|
* Float operation to maintain heap properties after adding an element.
|
|
268
239
|
* @param index - The index of the newly added element.
|
|
269
240
|
*/
|
|
270
|
-
protected _bubbleUp(index: number):
|
|
241
|
+
protected _bubbleUp(index: number): boolean;
|
|
271
242
|
/**
|
|
272
243
|
* Time Complexity: O(log n)
|
|
273
244
|
* Space Complexity: O(1)
|
|
@@ -276,7 +247,7 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
|
|
|
276
247
|
* @param index - The index from which to start sinking.
|
|
277
248
|
* @param halfLength
|
|
278
249
|
*/
|
|
279
|
-
protected _sinkDown(index: number, halfLength: number):
|
|
250
|
+
protected _sinkDown(index: number, halfLength: number): boolean;
|
|
280
251
|
}
|
|
281
252
|
export declare class FibonacciHeapNode<E> {
|
|
282
253
|
element: E;
|
|
@@ -42,7 +42,7 @@ class Heap extends base_1.IterableElementBase {
|
|
|
42
42
|
}
|
|
43
43
|
if (elements) {
|
|
44
44
|
for (const el of elements) {
|
|
45
|
-
this.
|
|
45
|
+
this.add(el);
|
|
46
46
|
}
|
|
47
47
|
// this.fix();
|
|
48
48
|
}
|
|
@@ -85,23 +85,8 @@ class Heap extends base_1.IterableElementBase {
|
|
|
85
85
|
* @param element - The element to be inserted.
|
|
86
86
|
*/
|
|
87
87
|
add(element) {
|
|
88
|
-
return this.push(element);
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
92
|
-
* Space Complexity: O(1)
|
|
93
|
-
*/
|
|
94
|
-
/**
|
|
95
|
-
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
96
|
-
* Space Complexity: O(1)
|
|
97
|
-
*
|
|
98
|
-
* Insert an element into the heap and maintain the heap properties.
|
|
99
|
-
* @param element - The element to be inserted.
|
|
100
|
-
*/
|
|
101
|
-
push(element) {
|
|
102
88
|
this._elements.push(element);
|
|
103
|
-
this._bubbleUp(this.elements.length - 1);
|
|
104
|
-
return this;
|
|
89
|
+
return this._bubbleUp(this.elements.length - 1);
|
|
105
90
|
}
|
|
106
91
|
/**
|
|
107
92
|
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
@@ -125,20 +110,6 @@ class Heap extends base_1.IterableElementBase {
|
|
|
125
110
|
}
|
|
126
111
|
return value;
|
|
127
112
|
}
|
|
128
|
-
/**
|
|
129
|
-
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
130
|
-
* Space Complexity: O(1)
|
|
131
|
-
*/
|
|
132
|
-
/**
|
|
133
|
-
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
134
|
-
* Space Complexity: O(1)
|
|
135
|
-
*
|
|
136
|
-
* Remove and return the top element (smallest or largest element) from the heap.
|
|
137
|
-
* @returns The top element or undefined if the heap is empty.
|
|
138
|
-
*/
|
|
139
|
-
pop() {
|
|
140
|
-
return this.poll();
|
|
141
|
-
}
|
|
142
113
|
/**
|
|
143
114
|
* Peek at the top element of the heap without removing it.
|
|
144
115
|
* @returns The top element or undefined if the heap is empty.
|
|
@@ -172,7 +143,7 @@ class Heap extends base_1.IterableElementBase {
|
|
|
172
143
|
*/
|
|
173
144
|
refill(elements) {
|
|
174
145
|
this._elements = elements;
|
|
175
|
-
this.fix();
|
|
146
|
+
return this.fix();
|
|
176
147
|
}
|
|
177
148
|
/**
|
|
178
149
|
* Time Complexity: O(n), where n is the number of elements in the heap.
|
|
@@ -209,7 +180,7 @@ class Heap extends base_1.IterableElementBase {
|
|
|
209
180
|
if (index < 0)
|
|
210
181
|
return false;
|
|
211
182
|
if (index === 0) {
|
|
212
|
-
this.
|
|
183
|
+
this.poll();
|
|
213
184
|
}
|
|
214
185
|
else if (index === this.elements.length - 1) {
|
|
215
186
|
this.elements.pop();
|
|
@@ -321,8 +292,10 @@ class Heap extends base_1.IterableElementBase {
|
|
|
321
292
|
* Fix the entire heap to maintain heap properties.
|
|
322
293
|
*/
|
|
323
294
|
fix() {
|
|
295
|
+
const results = [];
|
|
324
296
|
for (let i = Math.floor(this.size / 2); i >= 0; i--)
|
|
325
|
-
this._sinkDown(i, this.elements.length >> 1);
|
|
297
|
+
results.push(this._sinkDown(i, this.elements.length >> 1));
|
|
298
|
+
return results;
|
|
326
299
|
}
|
|
327
300
|
/**
|
|
328
301
|
* Time Complexity: O(n)
|
|
@@ -349,7 +322,7 @@ class Heap extends base_1.IterableElementBase {
|
|
|
349
322
|
let index = 0;
|
|
350
323
|
for (const current of this) {
|
|
351
324
|
if (callback.call(thisArg, current, index, this)) {
|
|
352
|
-
filteredList.
|
|
325
|
+
filteredList.add(current);
|
|
353
326
|
}
|
|
354
327
|
index++;
|
|
355
328
|
}
|
|
@@ -388,13 +361,6 @@ class Heap extends base_1.IterableElementBase {
|
|
|
388
361
|
}
|
|
389
362
|
return mappedHeap;
|
|
390
363
|
}
|
|
391
|
-
/**
|
|
392
|
-
* Time Complexity: O(log n)
|
|
393
|
-
* Space Complexity: O(1)
|
|
394
|
-
*/
|
|
395
|
-
print() {
|
|
396
|
-
console.log([...this]);
|
|
397
|
-
}
|
|
398
364
|
*_getIterator() {
|
|
399
365
|
for (const element of this.elements) {
|
|
400
366
|
yield element;
|
|
@@ -422,6 +388,7 @@ class Heap extends base_1.IterableElementBase {
|
|
|
422
388
|
index = parent;
|
|
423
389
|
}
|
|
424
390
|
this.elements[index] = element;
|
|
391
|
+
return true;
|
|
425
392
|
}
|
|
426
393
|
/**
|
|
427
394
|
* Time Complexity: O(log n)
|
|
@@ -448,6 +415,7 @@ class Heap extends base_1.IterableElementBase {
|
|
|
448
415
|
index = left;
|
|
449
416
|
}
|
|
450
417
|
this.elements[index] = element;
|
|
418
|
+
return true;
|
|
451
419
|
}
|
|
452
420
|
}
|
|
453
421
|
exports.Heap = Heap;
|