min-heap-typed 1.42.7 → 1.42.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/avl-tree.d.ts +88 -23
- package/dist/data-structures/binary-tree/avl-tree.js +88 -23
- package/dist/data-structures/binary-tree/binary-tree.d.ts +180 -74
- package/dist/data-structures/binary-tree/binary-tree.js +388 -201
- package/dist/data-structures/binary-tree/bst.d.ts +121 -66
- package/dist/data-structures/binary-tree/bst.js +121 -67
- package/dist/data-structures/binary-tree/rb-tree.d.ts +72 -5
- package/dist/data-structures/binary-tree/rb-tree.js +95 -18
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +82 -43
- package/dist/data-structures/binary-tree/tree-multimap.js +82 -43
- package/dist/data-structures/graph/abstract-graph.d.ts +139 -36
- package/dist/data-structures/graph/abstract-graph.js +147 -36
- package/dist/data-structures/graph/directed-graph.d.ts +126 -0
- package/dist/data-structures/graph/directed-graph.js +126 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +63 -0
- package/dist/data-structures/graph/undirected-graph.js +63 -0
- package/dist/data-structures/heap/heap.d.ts +175 -12
- package/dist/data-structures/heap/heap.js +175 -12
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +203 -0
- package/dist/data-structures/linked-list/doubly-linked-list.js +203 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +182 -0
- package/dist/data-structures/linked-list/singly-linked-list.js +182 -0
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +64 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +64 -0
- package/dist/data-structures/queue/deque.d.ts +113 -3
- package/dist/data-structures/queue/deque.js +113 -3
- package/dist/data-structures/queue/queue.d.ts +87 -0
- package/dist/data-structures/queue/queue.js +87 -0
- package/dist/data-structures/stack/stack.d.ts +42 -0
- package/dist/data-structures/stack/stack.js +42 -0
- package/dist/data-structures/trie/trie.d.ts +76 -0
- package/dist/data-structures/trie/trie.js +76 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +97 -23
- package/src/data-structures/binary-tree/binary-tree.ts +419 -204
- package/src/data-structures/binary-tree/bst.ts +130 -68
- package/src/data-structures/binary-tree/rb-tree.ts +106 -19
- package/src/data-structures/binary-tree/tree-multimap.ts +88 -44
- package/src/data-structures/graph/abstract-graph.ts +133 -7
- package/src/data-structures/graph/directed-graph.ts +145 -1
- package/src/data-structures/graph/undirected-graph.ts +72 -0
- package/src/data-structures/heap/heap.ts +201 -12
- package/src/data-structures/linked-list/doubly-linked-list.ts +232 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +208 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +74 -0
- package/src/data-structures/queue/deque.ts +127 -3
- package/src/data-structures/queue/queue.ts +99 -0
- package/src/data-structures/stack/stack.ts +48 -0
- package/src/data-structures/trie/trie.ts +87 -4
|
@@ -33,21 +33,49 @@ export declare class Heap<E = any> {
|
|
|
33
33
|
comparator: Comparator<E>;
|
|
34
34
|
}): Heap<E>;
|
|
35
35
|
/**
|
|
36
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
37
|
+
* Space Complexity: O(1)
|
|
38
|
+
*/
|
|
39
|
+
/**
|
|
40
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
41
|
+
* Space Complexity: O(1)
|
|
42
|
+
*
|
|
36
43
|
* Insert an element into the heap and maintain the heap properties.
|
|
37
44
|
* @param element - The element to be inserted.
|
|
38
45
|
*/
|
|
39
46
|
add(element: E): Heap<E>;
|
|
40
47
|
/**
|
|
48
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
49
|
+
* Space Complexity: O(1)
|
|
50
|
+
*/
|
|
51
|
+
/**
|
|
52
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
53
|
+
* Space Complexity: O(1)
|
|
54
|
+
*
|
|
41
55
|
* Insert an element into the heap and maintain the heap properties.
|
|
42
56
|
* @param element - The element to be inserted.
|
|
43
57
|
*/
|
|
44
58
|
push(element: E): Heap<E>;
|
|
45
59
|
/**
|
|
60
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
61
|
+
* Space Complexity: O(1)
|
|
62
|
+
*/
|
|
63
|
+
/**
|
|
64
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
65
|
+
* Space Complexity: O(1)
|
|
66
|
+
*
|
|
46
67
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
47
68
|
* @returns The top element or undefined if the heap is empty.
|
|
48
69
|
*/
|
|
49
70
|
poll(): E | undefined;
|
|
50
71
|
/**
|
|
72
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
73
|
+
* Space Complexity: O(1)
|
|
74
|
+
*/
|
|
75
|
+
/**
|
|
76
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
77
|
+
* Space Complexity: O(1)
|
|
78
|
+
*
|
|
51
79
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
52
80
|
* @returns The top element or undefined if the heap is empty.
|
|
53
81
|
*/
|
|
@@ -67,49 +95,116 @@ export declare class Heap<E = any> {
|
|
|
67
95
|
*/
|
|
68
96
|
clear(): void;
|
|
69
97
|
/**
|
|
98
|
+
* Time Complexity: O(n), where n is the number of elements in the nodes array.
|
|
99
|
+
* Space Complexity: O(n)
|
|
100
|
+
*/
|
|
101
|
+
/**
|
|
102
|
+
* Time Complexity: O(n), where n is the number of elements in the nodes array.
|
|
103
|
+
* Space Complexity: O(n)
|
|
104
|
+
*
|
|
70
105
|
* Clear and add nodes of the heap
|
|
71
106
|
* @param nodes
|
|
72
107
|
*/
|
|
73
108
|
refill(nodes: E[]): void;
|
|
74
109
|
/**
|
|
110
|
+
* Time Complexity: O(n), where n is the number of nodes in the heap.
|
|
111
|
+
* Space Complexity: O(1)
|
|
112
|
+
*/
|
|
113
|
+
/**
|
|
114
|
+
* Time Complexity: O(n), where n is the number of nodes in the heap.
|
|
115
|
+
* Space Complexity: O(1)
|
|
116
|
+
*
|
|
75
117
|
* Use a comparison function to check whether a binary heap contains a specific element.
|
|
76
118
|
* @param element - the element to check.
|
|
77
119
|
* @returns Returns true if the specified element is contained; otherwise, returns false.
|
|
78
120
|
*/
|
|
79
121
|
has(element: E): boolean;
|
|
80
122
|
/**
|
|
123
|
+
* Time Complexity: O(n), where n is the number of nodes in the heap.
|
|
124
|
+
* Space Complexity: O(h), where h is the height of the heap.
|
|
125
|
+
*/
|
|
126
|
+
/**
|
|
127
|
+
* Time Complexity: O(n), where n is the number of nodes in the heap.
|
|
128
|
+
* Space Complexity: O(h), where h is the height of the heap.
|
|
129
|
+
*
|
|
81
130
|
* Depth-first search (DFS) method, different traversal orders can be selected。
|
|
82
131
|
* @param order - Traverse order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
|
|
83
132
|
* @returns An array containing elements traversed in the specified order.
|
|
84
133
|
*/
|
|
85
134
|
dfs(order: DFSOrderPattern): E[];
|
|
86
135
|
/**
|
|
136
|
+
* Time Complexity: O(n)
|
|
137
|
+
* Space Complexity: O(n)
|
|
138
|
+
*/
|
|
139
|
+
/**
|
|
140
|
+
* Time Complexity: O(n)
|
|
141
|
+
* Space Complexity: O(n)
|
|
142
|
+
*
|
|
87
143
|
* Convert the heap to an array.
|
|
88
144
|
* @returns An array containing the elements of the heap.
|
|
89
145
|
*/
|
|
90
146
|
toArray(): E[];
|
|
147
|
+
/**
|
|
148
|
+
* Time Complexity: O(1)
|
|
149
|
+
* Space Complexity: O(1)
|
|
150
|
+
*/
|
|
91
151
|
getNodes(): E[];
|
|
92
152
|
/**
|
|
153
|
+
* Time Complexity: O(n)
|
|
154
|
+
* Space Complexity: O(n)
|
|
155
|
+
*/
|
|
156
|
+
/**
|
|
157
|
+
* Time Complexity: O(n)
|
|
158
|
+
* Space Complexity: O(n)
|
|
159
|
+
*
|
|
93
160
|
* Clone the heap, creating a new heap with the same elements.
|
|
94
161
|
* @returns A new Heap instance containing the same elements.
|
|
95
162
|
*/
|
|
96
163
|
clone(): Heap<E>;
|
|
97
164
|
/**
|
|
165
|
+
* Time Complexity: O(n log n)
|
|
166
|
+
* Space Complexity: O(n)
|
|
167
|
+
*/
|
|
168
|
+
/**
|
|
169
|
+
* Time Complexity: O(n log n)
|
|
170
|
+
* Space Complexity: O(n)
|
|
171
|
+
*
|
|
98
172
|
* Sort the elements in the heap and return them as an array.
|
|
99
173
|
* @returns An array containing the elements sorted in ascending order.
|
|
100
174
|
*/
|
|
101
175
|
sort(): E[];
|
|
102
176
|
/**
|
|
177
|
+
* Time Complexity: O(log n)
|
|
178
|
+
* Space Complexity: O(1)
|
|
179
|
+
*/
|
|
180
|
+
/**
|
|
181
|
+
* Time Complexity: O(log n)
|
|
182
|
+
* Space Complexity: O(1)
|
|
183
|
+
*
|
|
103
184
|
* Float operation to maintain heap properties after adding an element.
|
|
104
185
|
* @param index - The index of the newly added element.
|
|
105
186
|
*/
|
|
106
187
|
protected bubbleUp(index: number): void;
|
|
107
188
|
/**
|
|
189
|
+
* Time Complexity: O(log n)
|
|
190
|
+
* Space Complexity: O(1)
|
|
191
|
+
*/
|
|
192
|
+
/**
|
|
193
|
+
* Time Complexity: O(log n)
|
|
194
|
+
* Space Complexity: O(1)
|
|
195
|
+
*
|
|
108
196
|
* Sinking operation to maintain heap properties after removing the top element.
|
|
109
197
|
* @param index - The index from which to start sinking.
|
|
110
198
|
*/
|
|
111
199
|
protected sinkDown(index: number): void;
|
|
112
200
|
/**
|
|
201
|
+
* Time Complexity: O(n)
|
|
202
|
+
* Space Complexity: O(1)
|
|
203
|
+
*/
|
|
204
|
+
/**
|
|
205
|
+
* Time Complexity: O(n)
|
|
206
|
+
* Space Complexity: O(1)
|
|
207
|
+
*
|
|
113
208
|
* Fix the entire heap to maintain heap properties.
|
|
114
209
|
*/
|
|
115
210
|
protected fix(): void;
|
|
@@ -140,28 +235,52 @@ export declare class FibonacciHeap<E> {
|
|
|
140
235
|
*/
|
|
141
236
|
clear(): void;
|
|
142
237
|
/**
|
|
143
|
-
* O(1)
|
|
238
|
+
* Time Complexity: O(1)
|
|
239
|
+
* Space Complexity: O(1)
|
|
240
|
+
*/
|
|
241
|
+
/**
|
|
242
|
+
* Time Complexity: O(1)
|
|
243
|
+
* Space Complexity: O(1)
|
|
244
|
+
*
|
|
144
245
|
* Insert an element into the heap and maintain the heap properties.
|
|
145
246
|
* @param element
|
|
146
247
|
* @returns {FibonacciHeap<E>} FibonacciHeap<E> - The heap itself.
|
|
147
248
|
*/
|
|
148
249
|
add(element: E): FibonacciHeap<E>;
|
|
149
250
|
/**
|
|
150
|
-
* O(1)
|
|
251
|
+
* Time Complexity: O(1)
|
|
252
|
+
* Space Complexity: O(1)
|
|
253
|
+
*/
|
|
254
|
+
/**
|
|
255
|
+
* Time Complexity: O(1)
|
|
256
|
+
* Space Complexity: O(1)
|
|
257
|
+
*
|
|
151
258
|
* Insert an element into the heap and maintain the heap properties.
|
|
152
259
|
* @param element
|
|
153
260
|
* @returns {FibonacciHeap<E>} FibonacciHeap<E> - The heap itself.
|
|
154
261
|
*/
|
|
155
262
|
push(element: E): FibonacciHeap<E>;
|
|
156
263
|
/**
|
|
157
|
-
* O(1)
|
|
264
|
+
* Time Complexity: O(1)
|
|
265
|
+
* Space Complexity: O(1)
|
|
266
|
+
*/
|
|
267
|
+
/**
|
|
268
|
+
* Time Complexity: O(1)
|
|
269
|
+
* Space Complexity: O(1)
|
|
270
|
+
*
|
|
158
271
|
* Peek at the top element of the heap without removing it.
|
|
159
272
|
* @returns The top element or undefined if the heap is empty.
|
|
160
273
|
* @protected
|
|
161
274
|
*/
|
|
162
275
|
peek(): E | undefined;
|
|
163
276
|
/**
|
|
164
|
-
* O(
|
|
277
|
+
* Time Complexity: O(n), where n is the number of nodes in the linked list.
|
|
278
|
+
* Space Complexity: O(1)
|
|
279
|
+
*/
|
|
280
|
+
/**
|
|
281
|
+
* Time Complexity: O(n), where n is the number of nodes in the linked list.
|
|
282
|
+
* Space Complexity: O(1)
|
|
283
|
+
*
|
|
165
284
|
* Get the size (number of elements) of the heap.
|
|
166
285
|
* @param {FibonacciHeapNode<E>} head - The head of the linked list.
|
|
167
286
|
* @protected
|
|
@@ -169,26 +288,45 @@ export declare class FibonacciHeap<E> {
|
|
|
169
288
|
*/
|
|
170
289
|
consumeLinkedList(head?: FibonacciHeapNode<E>): FibonacciHeapNode<E>[];
|
|
171
290
|
/**
|
|
172
|
-
* O(
|
|
173
|
-
*
|
|
291
|
+
* Time Complexity: O(1)
|
|
292
|
+
* Space Complexity: O(1)
|
|
293
|
+
*
|
|
174
294
|
* @param parent
|
|
175
295
|
* @param node
|
|
176
296
|
*/
|
|
177
297
|
mergeWithChild(parent: FibonacciHeapNode<E>, node: FibonacciHeapNode<E>): void;
|
|
178
298
|
/**
|
|
179
|
-
* O(log n)
|
|
299
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
300
|
+
* Space Complexity: O(1)
|
|
301
|
+
*/
|
|
302
|
+
/**
|
|
303
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
304
|
+
* Space Complexity: O(1)
|
|
305
|
+
*
|
|
180
306
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
181
307
|
* @returns The top element or undefined if the heap is empty.
|
|
182
308
|
*/
|
|
183
309
|
poll(): E | undefined;
|
|
184
310
|
/**
|
|
185
|
-
* O(log n)
|
|
311
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
312
|
+
* Space Complexity: O(1)
|
|
313
|
+
*/
|
|
314
|
+
/**
|
|
315
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
316
|
+
* Space Complexity: O(1)
|
|
317
|
+
*
|
|
186
318
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
187
319
|
* @returns The top element or undefined if the heap is empty.
|
|
188
320
|
*/
|
|
189
321
|
pop(): E | undefined;
|
|
190
322
|
/**
|
|
191
|
-
* O(
|
|
323
|
+
* Time Complexity: O(1)
|
|
324
|
+
* Space Complexity: O(1)
|
|
325
|
+
*/
|
|
326
|
+
/**
|
|
327
|
+
* Time Complexity: O(1)
|
|
328
|
+
* Space Complexity: O(1)
|
|
329
|
+
*
|
|
192
330
|
* merge two heaps. The heap that is merged will be cleared. The heap that is merged into will remain.
|
|
193
331
|
* @param heapToMerge
|
|
194
332
|
*/
|
|
@@ -207,19 +345,38 @@ export declare class FibonacciHeap<E> {
|
|
|
207
345
|
*/
|
|
208
346
|
protected createNode(element: E): FibonacciHeapNode<E>;
|
|
209
347
|
/**
|
|
348
|
+
* Time Complexity: O(1)
|
|
349
|
+
* Space Complexity: O(1)
|
|
350
|
+
*/
|
|
351
|
+
/**
|
|
352
|
+
* Time Complexity: O(1)
|
|
353
|
+
* Space Complexity: O(1)
|
|
354
|
+
*
|
|
210
355
|
* Merge the given node with the root list.
|
|
211
356
|
* @param node - The node to be merged.
|
|
212
357
|
*/
|
|
213
358
|
protected mergeWithRoot(node: FibonacciHeapNode<E>): void;
|
|
214
359
|
/**
|
|
215
|
-
* O(
|
|
360
|
+
* Time Complexity: O(1)
|
|
361
|
+
* Space Complexity: O(1)
|
|
362
|
+
*/
|
|
363
|
+
/**
|
|
364
|
+
* Time Complexity: O(1)
|
|
365
|
+
* Space Complexity: O(1)
|
|
366
|
+
*.
|
|
216
367
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
217
368
|
* @param node - The node to be removed.
|
|
218
369
|
* @protected
|
|
219
370
|
*/
|
|
220
371
|
protected removeFromRoot(node: FibonacciHeapNode<E>): void;
|
|
221
372
|
/**
|
|
222
|
-
* O(
|
|
373
|
+
* Time Complexity: O(1)
|
|
374
|
+
* Space Complexity: O(1)
|
|
375
|
+
*/
|
|
376
|
+
/**
|
|
377
|
+
* Time Complexity: O(1)
|
|
378
|
+
* Space Complexity: O(1)
|
|
379
|
+
*
|
|
223
380
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
224
381
|
* @param y
|
|
225
382
|
* @param x
|
|
@@ -227,7 +384,13 @@ export declare class FibonacciHeap<E> {
|
|
|
227
384
|
*/
|
|
228
385
|
protected link(y: FibonacciHeapNode<E>, x: FibonacciHeapNode<E>): void;
|
|
229
386
|
/**
|
|
230
|
-
* O(log n)
|
|
387
|
+
* Time Complexity: O(n log n), where n is the number of nodes in the heap.
|
|
388
|
+
* Space Complexity: O(n)
|
|
389
|
+
*/
|
|
390
|
+
/**
|
|
391
|
+
* Time Complexity: O(n log n), where n is the number of nodes in the heap.
|
|
392
|
+
* Space Complexity: O(n)
|
|
393
|
+
*
|
|
231
394
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
232
395
|
* @protected
|
|
233
396
|
*/
|
|
@@ -45,6 +45,13 @@ class Heap {
|
|
|
45
45
|
return new Heap(options);
|
|
46
46
|
}
|
|
47
47
|
/**
|
|
48
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
49
|
+
* Space Complexity: O(1)
|
|
50
|
+
*/
|
|
51
|
+
/**
|
|
52
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
53
|
+
* Space Complexity: O(1)
|
|
54
|
+
*
|
|
48
55
|
* Insert an element into the heap and maintain the heap properties.
|
|
49
56
|
* @param element - The element to be inserted.
|
|
50
57
|
*/
|
|
@@ -52,6 +59,13 @@ class Heap {
|
|
|
52
59
|
return this.push(element);
|
|
53
60
|
}
|
|
54
61
|
/**
|
|
62
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
63
|
+
* Space Complexity: O(1)
|
|
64
|
+
*/
|
|
65
|
+
/**
|
|
66
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
67
|
+
* Space Complexity: O(1)
|
|
68
|
+
*
|
|
55
69
|
* Insert an element into the heap and maintain the heap properties.
|
|
56
70
|
* @param element - The element to be inserted.
|
|
57
71
|
*/
|
|
@@ -61,6 +75,13 @@ class Heap {
|
|
|
61
75
|
return this;
|
|
62
76
|
}
|
|
63
77
|
/**
|
|
78
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
79
|
+
* Space Complexity: O(1)
|
|
80
|
+
*/
|
|
81
|
+
/**
|
|
82
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
83
|
+
* Space Complexity: O(1)
|
|
84
|
+
*
|
|
64
85
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
65
86
|
* @returns The top element or undefined if the heap is empty.
|
|
66
87
|
*/
|
|
@@ -77,6 +98,13 @@ class Heap {
|
|
|
77
98
|
return topValue;
|
|
78
99
|
}
|
|
79
100
|
/**
|
|
101
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
102
|
+
* Space Complexity: O(1)
|
|
103
|
+
*/
|
|
104
|
+
/**
|
|
105
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
106
|
+
* Space Complexity: O(1)
|
|
107
|
+
*
|
|
80
108
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
81
109
|
* @returns The top element or undefined if the heap is empty.
|
|
82
110
|
*/
|
|
@@ -107,6 +135,13 @@ class Heap {
|
|
|
107
135
|
this._nodes = [];
|
|
108
136
|
}
|
|
109
137
|
/**
|
|
138
|
+
* Time Complexity: O(n), where n is the number of elements in the nodes array.
|
|
139
|
+
* Space Complexity: O(n)
|
|
140
|
+
*/
|
|
141
|
+
/**
|
|
142
|
+
* Time Complexity: O(n), where n is the number of elements in the nodes array.
|
|
143
|
+
* Space Complexity: O(n)
|
|
144
|
+
*
|
|
110
145
|
* Clear and add nodes of the heap
|
|
111
146
|
* @param nodes
|
|
112
147
|
*/
|
|
@@ -115,6 +150,13 @@ class Heap {
|
|
|
115
150
|
this.fix();
|
|
116
151
|
}
|
|
117
152
|
/**
|
|
153
|
+
* Time Complexity: O(n), where n is the number of nodes in the heap.
|
|
154
|
+
* Space Complexity: O(1)
|
|
155
|
+
*/
|
|
156
|
+
/**
|
|
157
|
+
* Time Complexity: O(n), where n is the number of nodes in the heap.
|
|
158
|
+
* Space Complexity: O(1)
|
|
159
|
+
*
|
|
118
160
|
* Use a comparison function to check whether a binary heap contains a specific element.
|
|
119
161
|
* @param element - the element to check.
|
|
120
162
|
* @returns Returns true if the specified element is contained; otherwise, returns false.
|
|
@@ -123,6 +165,13 @@ class Heap {
|
|
|
123
165
|
return this.nodes.includes(element);
|
|
124
166
|
}
|
|
125
167
|
/**
|
|
168
|
+
* Time Complexity: O(n), where n is the number of nodes in the heap.
|
|
169
|
+
* Space Complexity: O(h), where h is the height of the heap.
|
|
170
|
+
*/
|
|
171
|
+
/**
|
|
172
|
+
* Time Complexity: O(n), where n is the number of nodes in the heap.
|
|
173
|
+
* Space Complexity: O(h), where h is the height of the heap.
|
|
174
|
+
*
|
|
126
175
|
* Depth-first search (DFS) method, different traversal orders can be selected。
|
|
127
176
|
* @param order - Traverse order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
|
|
128
177
|
* @returns An array containing elements traversed in the specified order.
|
|
@@ -153,16 +202,34 @@ class Heap {
|
|
|
153
202
|
return result;
|
|
154
203
|
}
|
|
155
204
|
/**
|
|
205
|
+
* Time Complexity: O(n)
|
|
206
|
+
* Space Complexity: O(n)
|
|
207
|
+
*/
|
|
208
|
+
/**
|
|
209
|
+
* Time Complexity: O(n)
|
|
210
|
+
* Space Complexity: O(n)
|
|
211
|
+
*
|
|
156
212
|
* Convert the heap to an array.
|
|
157
213
|
* @returns An array containing the elements of the heap.
|
|
158
214
|
*/
|
|
159
215
|
toArray() {
|
|
160
216
|
return [...this.nodes];
|
|
161
217
|
}
|
|
218
|
+
/**
|
|
219
|
+
* Time Complexity: O(1)
|
|
220
|
+
* Space Complexity: O(1)
|
|
221
|
+
*/
|
|
162
222
|
getNodes() {
|
|
163
223
|
return this.nodes;
|
|
164
224
|
}
|
|
165
225
|
/**
|
|
226
|
+
* Time Complexity: O(n)
|
|
227
|
+
* Space Complexity: O(n)
|
|
228
|
+
*/
|
|
229
|
+
/**
|
|
230
|
+
* Time Complexity: O(n)
|
|
231
|
+
* Space Complexity: O(n)
|
|
232
|
+
*
|
|
166
233
|
* Clone the heap, creating a new heap with the same elements.
|
|
167
234
|
* @returns A new Heap instance containing the same elements.
|
|
168
235
|
*/
|
|
@@ -172,6 +239,13 @@ class Heap {
|
|
|
172
239
|
return clonedHeap;
|
|
173
240
|
}
|
|
174
241
|
/**
|
|
242
|
+
* Time Complexity: O(n log n)
|
|
243
|
+
* Space Complexity: O(n)
|
|
244
|
+
*/
|
|
245
|
+
/**
|
|
246
|
+
* Time Complexity: O(n log n)
|
|
247
|
+
* Space Complexity: O(n)
|
|
248
|
+
*
|
|
175
249
|
* Sort the elements in the heap and return them as an array.
|
|
176
250
|
* @returns An array containing the elements sorted in ascending order.
|
|
177
251
|
*/
|
|
@@ -186,6 +260,13 @@ class Heap {
|
|
|
186
260
|
return visitedNode;
|
|
187
261
|
}
|
|
188
262
|
/**
|
|
263
|
+
* Time Complexity: O(log n)
|
|
264
|
+
* Space Complexity: O(1)
|
|
265
|
+
*/
|
|
266
|
+
/**
|
|
267
|
+
* Time Complexity: O(log n)
|
|
268
|
+
* Space Complexity: O(1)
|
|
269
|
+
*
|
|
189
270
|
* Float operation to maintain heap properties after adding an element.
|
|
190
271
|
* @param index - The index of the newly added element.
|
|
191
272
|
*/
|
|
@@ -205,6 +286,13 @@ class Heap {
|
|
|
205
286
|
}
|
|
206
287
|
}
|
|
207
288
|
/**
|
|
289
|
+
* Time Complexity: O(log n)
|
|
290
|
+
* Space Complexity: O(1)
|
|
291
|
+
*/
|
|
292
|
+
/**
|
|
293
|
+
* Time Complexity: O(log n)
|
|
294
|
+
* Space Complexity: O(1)
|
|
295
|
+
*
|
|
208
296
|
* Sinking operation to maintain heap properties after removing the top element.
|
|
209
297
|
* @param index - The index from which to start sinking.
|
|
210
298
|
*/
|
|
@@ -227,6 +315,13 @@ class Heap {
|
|
|
227
315
|
}
|
|
228
316
|
}
|
|
229
317
|
/**
|
|
318
|
+
* Time Complexity: O(n)
|
|
319
|
+
* Space Complexity: O(1)
|
|
320
|
+
*/
|
|
321
|
+
/**
|
|
322
|
+
* Time Complexity: O(n)
|
|
323
|
+
* Space Complexity: O(1)
|
|
324
|
+
*
|
|
230
325
|
* Fix the entire heap to maintain heap properties.
|
|
231
326
|
*/
|
|
232
327
|
fix() {
|
|
@@ -274,7 +369,13 @@ class FibonacciHeap {
|
|
|
274
369
|
this._size = 0;
|
|
275
370
|
}
|
|
276
371
|
/**
|
|
277
|
-
* O(1)
|
|
372
|
+
* Time Complexity: O(1)
|
|
373
|
+
* Space Complexity: O(1)
|
|
374
|
+
*/
|
|
375
|
+
/**
|
|
376
|
+
* Time Complexity: O(1)
|
|
377
|
+
* Space Complexity: O(1)
|
|
378
|
+
*
|
|
278
379
|
* Insert an element into the heap and maintain the heap properties.
|
|
279
380
|
* @param element
|
|
280
381
|
* @returns {FibonacciHeap<E>} FibonacciHeap<E> - The heap itself.
|
|
@@ -283,7 +384,13 @@ class FibonacciHeap {
|
|
|
283
384
|
return this.push(element);
|
|
284
385
|
}
|
|
285
386
|
/**
|
|
286
|
-
* O(1)
|
|
387
|
+
* Time Complexity: O(1)
|
|
388
|
+
* Space Complexity: O(1)
|
|
389
|
+
*/
|
|
390
|
+
/**
|
|
391
|
+
* Time Complexity: O(1)
|
|
392
|
+
* Space Complexity: O(1)
|
|
393
|
+
*
|
|
287
394
|
* Insert an element into the heap and maintain the heap properties.
|
|
288
395
|
* @param element
|
|
289
396
|
* @returns {FibonacciHeap<E>} FibonacciHeap<E> - The heap itself.
|
|
@@ -300,7 +407,13 @@ class FibonacciHeap {
|
|
|
300
407
|
return this;
|
|
301
408
|
}
|
|
302
409
|
/**
|
|
303
|
-
* O(1)
|
|
410
|
+
* Time Complexity: O(1)
|
|
411
|
+
* Space Complexity: O(1)
|
|
412
|
+
*/
|
|
413
|
+
/**
|
|
414
|
+
* Time Complexity: O(1)
|
|
415
|
+
* Space Complexity: O(1)
|
|
416
|
+
*
|
|
304
417
|
* Peek at the top element of the heap without removing it.
|
|
305
418
|
* @returns The top element or undefined if the heap is empty.
|
|
306
419
|
* @protected
|
|
@@ -309,7 +422,13 @@ class FibonacciHeap {
|
|
|
309
422
|
return this.min ? this.min.element : undefined;
|
|
310
423
|
}
|
|
311
424
|
/**
|
|
312
|
-
* O(
|
|
425
|
+
* Time Complexity: O(n), where n is the number of nodes in the linked list.
|
|
426
|
+
* Space Complexity: O(1)
|
|
427
|
+
*/
|
|
428
|
+
/**
|
|
429
|
+
* Time Complexity: O(n), where n is the number of nodes in the linked list.
|
|
430
|
+
* Space Complexity: O(1)
|
|
431
|
+
*
|
|
313
432
|
* Get the size (number of elements) of the heap.
|
|
314
433
|
* @param {FibonacciHeapNode<E>} head - The head of the linked list.
|
|
315
434
|
* @protected
|
|
@@ -334,8 +453,9 @@ class FibonacciHeap {
|
|
|
334
453
|
return nodes;
|
|
335
454
|
}
|
|
336
455
|
/**
|
|
337
|
-
* O(
|
|
338
|
-
*
|
|
456
|
+
* Time Complexity: O(1)
|
|
457
|
+
* Space Complexity: O(1)
|
|
458
|
+
*
|
|
339
459
|
* @param parent
|
|
340
460
|
* @param node
|
|
341
461
|
*/
|
|
@@ -351,7 +471,13 @@ class FibonacciHeap {
|
|
|
351
471
|
}
|
|
352
472
|
}
|
|
353
473
|
/**
|
|
354
|
-
* O(log n)
|
|
474
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
475
|
+
* Space Complexity: O(1)
|
|
476
|
+
*/
|
|
477
|
+
/**
|
|
478
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
479
|
+
* Space Complexity: O(1)
|
|
480
|
+
*
|
|
355
481
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
356
482
|
* @returns The top element or undefined if the heap is empty.
|
|
357
483
|
*/
|
|
@@ -359,7 +485,13 @@ class FibonacciHeap {
|
|
|
359
485
|
return this.pop();
|
|
360
486
|
}
|
|
361
487
|
/**
|
|
362
|
-
* O(log n)
|
|
488
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
489
|
+
* Space Complexity: O(1)
|
|
490
|
+
*/
|
|
491
|
+
/**
|
|
492
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
493
|
+
* Space Complexity: O(1)
|
|
494
|
+
*
|
|
363
495
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
364
496
|
* @returns The top element or undefined if the heap is empty.
|
|
365
497
|
*/
|
|
@@ -387,7 +519,13 @@ class FibonacciHeap {
|
|
|
387
519
|
return z.element;
|
|
388
520
|
}
|
|
389
521
|
/**
|
|
390
|
-
* O(
|
|
522
|
+
* Time Complexity: O(1)
|
|
523
|
+
* Space Complexity: O(1)
|
|
524
|
+
*/
|
|
525
|
+
/**
|
|
526
|
+
* Time Complexity: O(1)
|
|
527
|
+
* Space Complexity: O(1)
|
|
528
|
+
*
|
|
391
529
|
* merge two heaps. The heap that is merged will be cleared. The heap that is merged into will remain.
|
|
392
530
|
* @param heapToMerge
|
|
393
531
|
*/
|
|
@@ -437,6 +575,13 @@ class FibonacciHeap {
|
|
|
437
575
|
return new FibonacciHeapNode(element);
|
|
438
576
|
}
|
|
439
577
|
/**
|
|
578
|
+
* Time Complexity: O(1)
|
|
579
|
+
* Space Complexity: O(1)
|
|
580
|
+
*/
|
|
581
|
+
/**
|
|
582
|
+
* Time Complexity: O(1)
|
|
583
|
+
* Space Complexity: O(1)
|
|
584
|
+
*
|
|
440
585
|
* Merge the given node with the root list.
|
|
441
586
|
* @param node - The node to be merged.
|
|
442
587
|
*/
|
|
@@ -452,7 +597,13 @@ class FibonacciHeap {
|
|
|
452
597
|
}
|
|
453
598
|
}
|
|
454
599
|
/**
|
|
455
|
-
* O(
|
|
600
|
+
* Time Complexity: O(1)
|
|
601
|
+
* Space Complexity: O(1)
|
|
602
|
+
*/
|
|
603
|
+
/**
|
|
604
|
+
* Time Complexity: O(1)
|
|
605
|
+
* Space Complexity: O(1)
|
|
606
|
+
*.
|
|
456
607
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
457
608
|
* @param node - The node to be removed.
|
|
458
609
|
* @protected
|
|
@@ -466,7 +617,13 @@ class FibonacciHeap {
|
|
|
466
617
|
node.right.left = node.left;
|
|
467
618
|
}
|
|
468
619
|
/**
|
|
469
|
-
* O(
|
|
620
|
+
* Time Complexity: O(1)
|
|
621
|
+
* Space Complexity: O(1)
|
|
622
|
+
*/
|
|
623
|
+
/**
|
|
624
|
+
* Time Complexity: O(1)
|
|
625
|
+
* Space Complexity: O(1)
|
|
626
|
+
*
|
|
470
627
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
471
628
|
* @param y
|
|
472
629
|
* @param x
|
|
@@ -481,7 +638,13 @@ class FibonacciHeap {
|
|
|
481
638
|
y.parent = x;
|
|
482
639
|
}
|
|
483
640
|
/**
|
|
484
|
-
* O(log n)
|
|
641
|
+
* Time Complexity: O(n log n), where n is the number of nodes in the heap.
|
|
642
|
+
* Space Complexity: O(n)
|
|
643
|
+
*/
|
|
644
|
+
/**
|
|
645
|
+
* Time Complexity: O(n log n), where n is the number of nodes in the heap.
|
|
646
|
+
* Space Complexity: O(n)
|
|
647
|
+
*
|
|
485
648
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
486
649
|
* @protected
|
|
487
650
|
*/
|