data-structure-typed 1.45.3 → 1.46.0
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/README.md +91 -40
- package/benchmark/report.html +17 -17
- package/benchmark/report.json +160 -142
- package/dist/cjs/data-structures/hash/hash-map.d.ts +6 -5
- package/dist/cjs/data-structures/hash/hash-map.js +9 -6
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +59 -47
- package/dist/cjs/data-structures/heap/heap.js +133 -123
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +131 -164
- package/dist/cjs/data-structures/queue/deque.js +543 -211
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +0 -9
- package/dist/cjs/types/helpers.d.ts +11 -0
- package/dist/cjs/utils/utils.d.ts +1 -0
- package/dist/cjs/utils/utils.js +3 -1
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/mjs/data-structures/hash/hash-map.d.ts +6 -5
- package/dist/mjs/data-structures/hash/hash-map.js +9 -6
- package/dist/mjs/data-structures/heap/heap.d.ts +59 -47
- package/dist/mjs/data-structures/heap/heap.js +133 -123
- package/dist/mjs/data-structures/queue/deque.d.ts +131 -164
- package/dist/mjs/data-structures/queue/deque.js +544 -201
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +0 -9
- package/dist/mjs/types/helpers.d.ts +11 -0
- package/dist/mjs/utils/utils.d.ts +1 -0
- package/dist/mjs/utils/utils.js +1 -0
- package/dist/umd/data-structure-typed.js +684 -328
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/hash/hash-map.ts +11 -7
- package/src/data-structures/heap/heap.ts +132 -128
- package/src/data-structures/queue/deque.ts +605 -226
- package/src/types/data-structures/hash/hash-map.ts +0 -11
- package/src/types/helpers.ts +15 -0
- package/src/utils/utils.ts +2 -0
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +21 -0
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +7 -7
- package/test/performance/data-structures/queue/deque.test.ts +24 -13
- package/test/unit/data-structures/hash/hash-map.test.ts +4 -4
- package/test/unit/data-structures/heap/heap.test.ts +2 -1
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +3 -3
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +3 -3
- package/test/unit/data-structures/queue/deque.test.ts +252 -240
- /package/test/performance/data-structures/{comparation.test.ts → comparison.test.ts} +0 -0
|
@@ -7,14 +7,14 @@
|
|
|
7
7
|
export class Heap {
|
|
8
8
|
constructor(options) {
|
|
9
9
|
this._comparator = options.comparator;
|
|
10
|
-
if (options.
|
|
11
|
-
this.
|
|
10
|
+
if (options.elements && options.elements.length > 0) {
|
|
11
|
+
this._elements = options.elements;
|
|
12
12
|
this.fix();
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
|
-
|
|
16
|
-
get
|
|
17
|
-
return this.
|
|
15
|
+
_elements = [];
|
|
16
|
+
get elements() {
|
|
17
|
+
return this._elements;
|
|
18
18
|
}
|
|
19
19
|
_comparator;
|
|
20
20
|
get comparator() {
|
|
@@ -24,17 +24,17 @@ export class Heap {
|
|
|
24
24
|
* Get the size (number of elements) of the heap.
|
|
25
25
|
*/
|
|
26
26
|
get size() {
|
|
27
|
-
return this.
|
|
27
|
+
return this.elements.length;
|
|
28
28
|
}
|
|
29
29
|
/**
|
|
30
30
|
* Get the last element in the heap, which is not necessarily a leaf node.
|
|
31
31
|
* @returns The last element or undefined if the heap is empty.
|
|
32
32
|
*/
|
|
33
33
|
get leaf() {
|
|
34
|
-
return this.
|
|
34
|
+
return this.elements[this.size - 1] ?? undefined;
|
|
35
35
|
}
|
|
36
36
|
/**
|
|
37
|
-
* Static method that creates a binary heap from an array of
|
|
37
|
+
* Static method that creates a binary heap from an array of elements and a comparison function.
|
|
38
38
|
* @returns A new Heap instance.
|
|
39
39
|
* @param options
|
|
40
40
|
*/
|
|
@@ -42,11 +42,11 @@ export class Heap {
|
|
|
42
42
|
return new Heap(options);
|
|
43
43
|
}
|
|
44
44
|
/**
|
|
45
|
-
* Time Complexity: O(log n), where n is the number of
|
|
45
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
46
46
|
* Space Complexity: O(1)
|
|
47
47
|
*/
|
|
48
48
|
/**
|
|
49
|
-
* Time Complexity: O(log n), where n is the number of
|
|
49
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
50
50
|
* Space Complexity: O(1)
|
|
51
51
|
*
|
|
52
52
|
* Insert an element into the heap and maintain the heap properties.
|
|
@@ -56,50 +56,49 @@ export class Heap {
|
|
|
56
56
|
return this.push(element);
|
|
57
57
|
}
|
|
58
58
|
/**
|
|
59
|
-
* Time Complexity: O(log n), where n is the number of
|
|
59
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
60
60
|
* Space Complexity: O(1)
|
|
61
61
|
*/
|
|
62
62
|
/**
|
|
63
|
-
* Time Complexity: O(log n), where n is the number of
|
|
63
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
64
64
|
* Space Complexity: O(1)
|
|
65
65
|
*
|
|
66
66
|
* Insert an element into the heap and maintain the heap properties.
|
|
67
67
|
* @param element - The element to be inserted.
|
|
68
68
|
*/
|
|
69
69
|
push(element) {
|
|
70
|
-
this.
|
|
71
|
-
this.
|
|
70
|
+
this._elements.push(element);
|
|
71
|
+
this._bubbleUp(this.elements.length - 1);
|
|
72
72
|
return this;
|
|
73
73
|
}
|
|
74
74
|
/**
|
|
75
|
-
* Time Complexity: O(log n), where n is the number of
|
|
75
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
76
76
|
* Space Complexity: O(1)
|
|
77
77
|
*/
|
|
78
78
|
/**
|
|
79
|
-
* Time Complexity: O(log n), where n is the number of
|
|
79
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
80
80
|
* Space Complexity: O(1)
|
|
81
81
|
*
|
|
82
82
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
83
83
|
* @returns The top element or undefined if the heap is empty.
|
|
84
84
|
*/
|
|
85
85
|
poll() {
|
|
86
|
-
if (this.
|
|
87
|
-
return
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
86
|
+
if (this.elements.length === 0)
|
|
87
|
+
return;
|
|
88
|
+
const value = this.elements[0];
|
|
89
|
+
const last = this.elements.pop();
|
|
90
|
+
if (this.elements.length) {
|
|
91
|
+
this.elements[0] = last;
|
|
92
|
+
this._sinkDown(0, this.elements.length >> 1);
|
|
91
93
|
}
|
|
92
|
-
|
|
93
|
-
this.nodes[0] = this.nodes.pop();
|
|
94
|
-
this.sinkDown(0);
|
|
95
|
-
return topValue;
|
|
94
|
+
return value;
|
|
96
95
|
}
|
|
97
96
|
/**
|
|
98
|
-
* Time Complexity: O(log n), where n is the number of
|
|
97
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
99
98
|
* Space Complexity: O(1)
|
|
100
99
|
*/
|
|
101
100
|
/**
|
|
102
|
-
* Time Complexity: O(log n), where n is the number of
|
|
101
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
103
102
|
* Space Complexity: O(1)
|
|
104
103
|
*
|
|
105
104
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
@@ -113,10 +112,7 @@ export class Heap {
|
|
|
113
112
|
* @returns The top element or undefined if the heap is empty.
|
|
114
113
|
*/
|
|
115
114
|
peek() {
|
|
116
|
-
|
|
117
|
-
return undefined;
|
|
118
|
-
}
|
|
119
|
-
return this.nodes[0];
|
|
115
|
+
return this.elements[0];
|
|
120
116
|
}
|
|
121
117
|
/**
|
|
122
118
|
* Check if the heap is empty.
|
|
@@ -126,32 +122,32 @@ export class Heap {
|
|
|
126
122
|
return this.size === 0;
|
|
127
123
|
}
|
|
128
124
|
/**
|
|
129
|
-
* Reset the
|
|
125
|
+
* Reset the elements of the heap. Make the elements empty.
|
|
130
126
|
*/
|
|
131
127
|
clear() {
|
|
132
|
-
this.
|
|
128
|
+
this._elements = [];
|
|
133
129
|
}
|
|
134
130
|
/**
|
|
135
|
-
* Time Complexity: O(n), where n is the number of elements in the
|
|
131
|
+
* Time Complexity: O(n), where n is the number of elements in the elements array.
|
|
136
132
|
* Space Complexity: O(n)
|
|
137
133
|
*/
|
|
138
134
|
/**
|
|
139
|
-
* Time Complexity: O(n), where n is the number of elements in the
|
|
135
|
+
* Time Complexity: O(n), where n is the number of elements in the elements array.
|
|
140
136
|
* Space Complexity: O(n)
|
|
141
137
|
*
|
|
142
|
-
* Clear and add
|
|
143
|
-
* @param
|
|
138
|
+
* Clear and add elements of the heap
|
|
139
|
+
* @param elements
|
|
144
140
|
*/
|
|
145
|
-
refill(
|
|
146
|
-
this.
|
|
141
|
+
refill(elements) {
|
|
142
|
+
this._elements = elements;
|
|
147
143
|
this.fix();
|
|
148
144
|
}
|
|
149
145
|
/**
|
|
150
|
-
* Time Complexity: O(n), where n is the number of
|
|
146
|
+
* Time Complexity: O(n), where n is the number of elements in the heap.
|
|
151
147
|
* Space Complexity: O(1)
|
|
152
148
|
*/
|
|
153
149
|
/**
|
|
154
|
-
* Time Complexity: O(n), where n is the number of
|
|
150
|
+
* Time Complexity: O(n), where n is the number of elements in the heap.
|
|
155
151
|
* Space Complexity: O(1)
|
|
156
152
|
*
|
|
157
153
|
* Use a comparison function to check whether a binary heap contains a specific element.
|
|
@@ -159,14 +155,46 @@ export class Heap {
|
|
|
159
155
|
* @returns Returns true if the specified element is contained; otherwise, returns false.
|
|
160
156
|
*/
|
|
161
157
|
has(element) {
|
|
162
|
-
return this.
|
|
158
|
+
return this.elements.includes(element);
|
|
163
159
|
}
|
|
164
160
|
/**
|
|
165
|
-
* Time Complexity: O(n), where n is the number of
|
|
161
|
+
* Time Complexity: O(n). The worst-case O(n), where n is the number of elements in the heap. This is because, in the worst case, the element to be deleted is located at the end of the heap (not the root), and after deletion, we may need to reorganize the elements by performing a sinkDown operation.
|
|
162
|
+
* Space Complexity: O(1)
|
|
163
|
+
*/
|
|
164
|
+
/**
|
|
165
|
+
* Time Complexity: O(n). The worst-case O(n), where n is the number of elements in the heap. This is because, in the worst case, the element to be deleted is located at the end of the heap (not the root), and after deletion, we may need to reorganize the elements by performing a sinkDown operation.
|
|
166
|
+
* Space Complexity: O(1)
|
|
167
|
+
*
|
|
168
|
+
* The `delete` function removes an element from an array-like data structure, maintaining the order
|
|
169
|
+
* and structure of the remaining elements.
|
|
170
|
+
* @param {E} element - The `element` parameter represents the element that you want to delete from
|
|
171
|
+
* the array `this.elements`.
|
|
172
|
+
* @returns The `delete` function is returning a boolean value. It returns `true` if the element was
|
|
173
|
+
* successfully deleted from the array, and `false` if the element was not found in the array.
|
|
174
|
+
*/
|
|
175
|
+
delete(element) {
|
|
176
|
+
const index = this.elements.indexOf(element);
|
|
177
|
+
if (index < 0)
|
|
178
|
+
return false;
|
|
179
|
+
if (index === 0) {
|
|
180
|
+
this.pop();
|
|
181
|
+
}
|
|
182
|
+
else if (index === this.elements.length - 1) {
|
|
183
|
+
this.elements.pop();
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
this.elements.splice(index, 1, this.elements.pop());
|
|
187
|
+
this._bubbleUp(index);
|
|
188
|
+
this._sinkDown(index, this.elements.length >> 1);
|
|
189
|
+
}
|
|
190
|
+
return true;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Time Complexity: O(n), where n is the number of elements in the heap.
|
|
166
194
|
* Space Complexity: O(h), where h is the height of the heap.
|
|
167
195
|
*/
|
|
168
196
|
/**
|
|
169
|
-
* Time Complexity: O(n), where n is the number of
|
|
197
|
+
* Time Complexity: O(n), where n is the number of elements in the heap.
|
|
170
198
|
* Space Complexity: O(h), where h is the height of the heap.
|
|
171
199
|
*
|
|
172
200
|
* Depth-first search (DFS) method, different traversal orders can be selected。
|
|
@@ -180,18 +208,18 @@ export class Heap {
|
|
|
180
208
|
if (index < this.size) {
|
|
181
209
|
if (order === 'in') {
|
|
182
210
|
dfsHelper(2 * index + 1);
|
|
183
|
-
result.push(this.
|
|
211
|
+
result.push(this.elements[index]);
|
|
184
212
|
dfsHelper(2 * index + 2);
|
|
185
213
|
}
|
|
186
214
|
else if (order === 'pre') {
|
|
187
|
-
result.push(this.
|
|
215
|
+
result.push(this.elements[index]);
|
|
188
216
|
dfsHelper(2 * index + 1);
|
|
189
217
|
dfsHelper(2 * index + 2);
|
|
190
218
|
}
|
|
191
219
|
else if (order === 'post') {
|
|
192
220
|
dfsHelper(2 * index + 1);
|
|
193
221
|
dfsHelper(2 * index + 2);
|
|
194
|
-
result.push(this.
|
|
222
|
+
result.push(this.elements[index]);
|
|
195
223
|
}
|
|
196
224
|
}
|
|
197
225
|
};
|
|
@@ -210,14 +238,7 @@ export class Heap {
|
|
|
210
238
|
* @returns An array containing the elements of the heap.
|
|
211
239
|
*/
|
|
212
240
|
toArray() {
|
|
213
|
-
return [...this.
|
|
214
|
-
}
|
|
215
|
-
/**
|
|
216
|
-
* Time Complexity: O(1)
|
|
217
|
-
* Space Complexity: O(1)
|
|
218
|
-
*/
|
|
219
|
-
getNodes() {
|
|
220
|
-
return this.nodes;
|
|
241
|
+
return [...this.elements];
|
|
221
242
|
}
|
|
222
243
|
/**
|
|
223
244
|
* Time Complexity: O(n)
|
|
@@ -232,7 +253,7 @@ export class Heap {
|
|
|
232
253
|
*/
|
|
233
254
|
clone() {
|
|
234
255
|
const clonedHeap = new Heap({ comparator: this.comparator });
|
|
235
|
-
clonedHeap.
|
|
256
|
+
clonedHeap._elements = [...this.elements];
|
|
236
257
|
return clonedHeap;
|
|
237
258
|
}
|
|
238
259
|
/**
|
|
@@ -261,35 +282,14 @@ export class Heap {
|
|
|
261
282
|
* Space Complexity: O(1)
|
|
262
283
|
*/
|
|
263
284
|
/**
|
|
264
|
-
* Time Complexity: O(
|
|
285
|
+
* Time Complexity: O(n)
|
|
265
286
|
* Space Complexity: O(1)
|
|
266
287
|
*
|
|
267
|
-
*
|
|
268
|
-
* @param index - The index of the newly added element.
|
|
288
|
+
* Fix the entire heap to maintain heap properties.
|
|
269
289
|
*/
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
// const parentIndex = (index - 1) >> 1;
|
|
274
|
-
// const parent = this.nodes[parentIndex];
|
|
275
|
-
// if (this.comparator(element, parent) < 0) {
|
|
276
|
-
// this.nodes[index] = parent;
|
|
277
|
-
// this.nodes[parentIndex] = element;
|
|
278
|
-
// index = parentIndex;
|
|
279
|
-
// } else {
|
|
280
|
-
// break;
|
|
281
|
-
// }
|
|
282
|
-
// }
|
|
283
|
-
const item = this.nodes[index];
|
|
284
|
-
while (index > 0) {
|
|
285
|
-
const parent = (index - 1) >> 1;
|
|
286
|
-
const parentItem = this.nodes[parent];
|
|
287
|
-
if (this.comparator(parentItem, item) <= 0)
|
|
288
|
-
break;
|
|
289
|
-
this.nodes[index] = parentItem;
|
|
290
|
-
index = parent;
|
|
291
|
-
}
|
|
292
|
-
this.nodes[index] = item;
|
|
290
|
+
fix() {
|
|
291
|
+
for (let i = Math.floor(this.size / 2); i >= 0; i--)
|
|
292
|
+
this._sinkDown(i, this.elements.length >> 1);
|
|
293
293
|
}
|
|
294
294
|
/**
|
|
295
295
|
* Time Complexity: O(log n)
|
|
@@ -299,40 +299,50 @@ export class Heap {
|
|
|
299
299
|
* Time Complexity: O(log n)
|
|
300
300
|
* Space Complexity: O(1)
|
|
301
301
|
*
|
|
302
|
-
*
|
|
303
|
-
* @param index - The index
|
|
302
|
+
* Float operation to maintain heap properties after adding an element.
|
|
303
|
+
* @param index - The index of the newly added element.
|
|
304
304
|
*/
|
|
305
|
-
|
|
306
|
-
const
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
targetIndex = rightChildIndex;
|
|
315
|
-
}
|
|
316
|
-
if (targetIndex !== index) {
|
|
317
|
-
const temp = this.nodes[index];
|
|
318
|
-
this.nodes[index] = this.nodes[targetIndex];
|
|
319
|
-
this.nodes[targetIndex] = temp;
|
|
320
|
-
this.sinkDown(targetIndex);
|
|
305
|
+
_bubbleUp(index) {
|
|
306
|
+
const element = this.elements[index];
|
|
307
|
+
while (index > 0) {
|
|
308
|
+
const parent = (index - 1) >> 1;
|
|
309
|
+
const parentItem = this.elements[parent];
|
|
310
|
+
if (this._comparator(parentItem, element) <= 0)
|
|
311
|
+
break;
|
|
312
|
+
this.elements[index] = parentItem;
|
|
313
|
+
index = parent;
|
|
321
314
|
}
|
|
315
|
+
this.elements[index] = element;
|
|
322
316
|
}
|
|
323
317
|
/**
|
|
324
318
|
* Time Complexity: O(n)
|
|
325
319
|
* Space Complexity: O(1)
|
|
326
320
|
*/
|
|
327
321
|
/**
|
|
328
|
-
* Time Complexity: O(n)
|
|
322
|
+
* Time Complexity: O(log n)
|
|
329
323
|
* Space Complexity: O(1)
|
|
330
324
|
*
|
|
331
|
-
*
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
325
|
+
* Sinking operation to maintain heap properties after removing the top element.
|
|
326
|
+
* @param index - The index from which to start sinking.
|
|
327
|
+
* @param halfLength
|
|
328
|
+
*/
|
|
329
|
+
_sinkDown(index, halfLength) {
|
|
330
|
+
const element = this.elements[index];
|
|
331
|
+
while (index < halfLength) {
|
|
332
|
+
let left = index << 1 | 1;
|
|
333
|
+
const right = left + 1;
|
|
334
|
+
let minItem = this.elements[left];
|
|
335
|
+
if (right < this.elements.length &&
|
|
336
|
+
this._comparator(minItem, this.elements[right]) > 0) {
|
|
337
|
+
left = right;
|
|
338
|
+
minItem = this.elements[right];
|
|
339
|
+
}
|
|
340
|
+
if (this._comparator(minItem, element) >= 0)
|
|
341
|
+
break;
|
|
342
|
+
this.elements[index] = minItem;
|
|
343
|
+
index = left;
|
|
344
|
+
}
|
|
345
|
+
this.elements[index] = element;
|
|
336
346
|
}
|
|
337
347
|
}
|
|
338
348
|
export class FibonacciHeapNode {
|
|
@@ -436,22 +446,22 @@ export class FibonacciHeap {
|
|
|
436
446
|
return this.min ? this.min.element : undefined;
|
|
437
447
|
}
|
|
438
448
|
/**
|
|
439
|
-
* Time Complexity: O(n), where n is the number of
|
|
449
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
440
450
|
* Space Complexity: O(1)
|
|
441
451
|
*/
|
|
442
452
|
/**
|
|
443
|
-
* Time Complexity: O(n), where n is the number of
|
|
453
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
444
454
|
* Space Complexity: O(1)
|
|
445
455
|
*
|
|
446
456
|
* Get the size (number of elements) of the heap.
|
|
447
457
|
* @param {FibonacciHeapNode<E>} head - The head of the linked list.
|
|
448
458
|
* @protected
|
|
449
|
-
* @returns FibonacciHeapNode<E>[] - An array containing the
|
|
459
|
+
* @returns FibonacciHeapNode<E>[] - An array containing the elements of the linked list.
|
|
450
460
|
*/
|
|
451
461
|
consumeLinkedList(head) {
|
|
452
|
-
const
|
|
462
|
+
const elements = [];
|
|
453
463
|
if (!head)
|
|
454
|
-
return
|
|
464
|
+
return elements;
|
|
455
465
|
let node = head;
|
|
456
466
|
let flag = false;
|
|
457
467
|
while (true) {
|
|
@@ -460,11 +470,11 @@ export class FibonacciHeap {
|
|
|
460
470
|
else if (node === head)
|
|
461
471
|
flag = true;
|
|
462
472
|
if (node) {
|
|
463
|
-
|
|
473
|
+
elements.push(node);
|
|
464
474
|
node = node.right;
|
|
465
475
|
}
|
|
466
476
|
}
|
|
467
|
-
return
|
|
477
|
+
return elements;
|
|
468
478
|
}
|
|
469
479
|
/**
|
|
470
480
|
* Time Complexity: O(1)
|
|
@@ -485,11 +495,11 @@ export class FibonacciHeap {
|
|
|
485
495
|
}
|
|
486
496
|
}
|
|
487
497
|
/**
|
|
488
|
-
* Time Complexity: O(log n), where n is the number of
|
|
498
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
489
499
|
* Space Complexity: O(1)
|
|
490
500
|
*/
|
|
491
501
|
/**
|
|
492
|
-
* Time Complexity: O(log n), where n is the number of
|
|
502
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
493
503
|
* Space Complexity: O(1)
|
|
494
504
|
*
|
|
495
505
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
@@ -499,11 +509,11 @@ export class FibonacciHeap {
|
|
|
499
509
|
return this.pop();
|
|
500
510
|
}
|
|
501
511
|
/**
|
|
502
|
-
* Time Complexity: O(log n), where n is the number of
|
|
512
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
503
513
|
* Space Complexity: O(1)
|
|
504
514
|
*/
|
|
505
515
|
/**
|
|
506
|
-
* Time Complexity: O(log n), where n is the number of
|
|
516
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
507
517
|
* Space Complexity: O(1)
|
|
508
518
|
*
|
|
509
519
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
@@ -514,8 +524,8 @@ export class FibonacciHeap {
|
|
|
514
524
|
return undefined;
|
|
515
525
|
const z = this.min;
|
|
516
526
|
if (z.child) {
|
|
517
|
-
const
|
|
518
|
-
for (const node of
|
|
527
|
+
const elements = this.consumeLinkedList(z.child);
|
|
528
|
+
for (const node of elements) {
|
|
519
529
|
this.mergeWithRoot(node);
|
|
520
530
|
node.parent = undefined;
|
|
521
531
|
}
|
|
@@ -652,11 +662,11 @@ export class FibonacciHeap {
|
|
|
652
662
|
y.parent = x;
|
|
653
663
|
}
|
|
654
664
|
/**
|
|
655
|
-
* Time Complexity: O(n log n), where n is the number of
|
|
665
|
+
* Time Complexity: O(n log n), where n is the number of elements in the heap.
|
|
656
666
|
* Space Complexity: O(n)
|
|
657
667
|
*/
|
|
658
668
|
/**
|
|
659
|
-
* Time Complexity: O(n log n), where n is the number of
|
|
669
|
+
* Time Complexity: O(n log n), where n is the number of elements in the heap.
|
|
660
670
|
* Space Complexity: O(n)
|
|
661
671
|
*
|
|
662
672
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
@@ -664,9 +674,9 @@ export class FibonacciHeap {
|
|
|
664
674
|
*/
|
|
665
675
|
consolidate() {
|
|
666
676
|
const A = new Array(this.size);
|
|
667
|
-
const
|
|
677
|
+
const elements = this.consumeLinkedList(this.root);
|
|
668
678
|
let x, y, d, t;
|
|
669
|
-
for (const node of
|
|
679
|
+
for (const node of elements) {
|
|
670
680
|
x = node;
|
|
671
681
|
d = x.degree;
|
|
672
682
|
while (A[d]) {
|