graph-typed 2.0.4 → 2.1.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/dist/data-structures/base/iterable-element-base.d.ts +186 -83
- package/dist/data-structures/base/iterable-element-base.js +149 -107
- package/dist/data-structures/base/iterable-entry-base.d.ts +95 -119
- package/dist/data-structures/base/iterable-entry-base.js +59 -116
- package/dist/data-structures/base/linear-base.d.ts +250 -192
- package/dist/data-structures/base/linear-base.js +137 -274
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
- package/dist/data-structures/binary-tree/avl-tree-counter.js +171 -205
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -87
- package/dist/data-structures/binary-tree/avl-tree.d.ts +138 -149
- package/dist/data-structures/binary-tree/avl-tree.js +208 -195
- package/dist/data-structures/binary-tree/binary-tree.d.ts +476 -632
- package/dist/data-structures/binary-tree/binary-tree.js +612 -879
- package/dist/data-structures/binary-tree/bst.d.ts +258 -306
- package/dist/data-structures/binary-tree/bst.js +505 -481
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +107 -179
- package/dist/data-structures/binary-tree/red-black-tree.js +114 -209
- package/dist/data-structures/binary-tree/tree-counter.d.ts +132 -154
- package/dist/data-structures/binary-tree/tree-counter.js +172 -203
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
- package/dist/data-structures/binary-tree/tree-multi-map.js +105 -85
- package/dist/data-structures/graph/abstract-graph.d.ts +238 -233
- package/dist/data-structures/graph/abstract-graph.js +267 -237
- package/dist/data-structures/graph/directed-graph.d.ts +108 -224
- package/dist/data-structures/graph/directed-graph.js +146 -233
- package/dist/data-structures/graph/map-graph.d.ts +49 -55
- package/dist/data-structures/graph/map-graph.js +56 -59
- package/dist/data-structures/graph/undirected-graph.d.ts +103 -146
- package/dist/data-structures/graph/undirected-graph.js +129 -149
- package/dist/data-structures/hash/hash-map.d.ts +164 -338
- package/dist/data-structures/hash/hash-map.js +270 -457
- package/dist/data-structures/heap/heap.d.ts +214 -289
- package/dist/data-structures/heap/heap.js +340 -349
- package/dist/data-structures/heap/max-heap.d.ts +11 -47
- package/dist/data-structures/heap/max-heap.js +11 -66
- package/dist/data-structures/heap/min-heap.d.ts +12 -47
- package/dist/data-structures/heap/min-heap.js +11 -66
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
- package/dist/data-structures/linked-list/doubly-linked-list.js +368 -494
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +261 -310
- package/dist/data-structures/linked-list/singly-linked-list.js +447 -466
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -107
- package/dist/data-structures/linked-list/skip-linked-list.js +0 -100
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
- package/dist/data-structures/priority-queue/max-priority-queue.js +11 -78
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
- package/dist/data-structures/priority-queue/min-priority-queue.js +10 -79
- package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -61
- package/dist/data-structures/priority-queue/priority-queue.js +8 -83
- package/dist/data-structures/queue/deque.d.ts +227 -254
- package/dist/data-structures/queue/deque.js +309 -348
- package/dist/data-structures/queue/queue.d.ts +180 -201
- package/dist/data-structures/queue/queue.js +265 -248
- package/dist/data-structures/stack/stack.d.ts +124 -102
- package/dist/data-structures/stack/stack.js +181 -125
- package/dist/data-structures/trie/trie.d.ts +164 -165
- package/dist/data-structures/trie/trie.js +189 -172
- package/dist/interfaces/binary-tree.d.ts +56 -6
- package/dist/interfaces/graph.d.ts +16 -0
- package/dist/types/data-structures/base/base.d.ts +1 -1
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -0
- package/dist/types/utils/utils.d.ts +6 -6
- package/dist/utils/utils.d.ts +110 -49
- package/dist/utils/utils.js +148 -73
- package/package.json +2 -2
- package/src/data-structures/base/iterable-element-base.ts +238 -115
- package/src/data-structures/base/iterable-entry-base.ts +96 -120
- package/src/data-structures/base/linear-base.ts +271 -277
- package/src/data-structures/binary-tree/avl-tree-counter.ts +198 -216
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +192 -101
- package/src/data-structures/binary-tree/avl-tree.ts +239 -206
- package/src/data-structures/binary-tree/binary-tree.ts +681 -905
- package/src/data-structures/binary-tree/bst.ts +568 -570
- package/src/data-structures/binary-tree/red-black-tree.ts +161 -222
- package/src/data-structures/binary-tree/tree-counter.ts +199 -218
- package/src/data-structures/binary-tree/tree-multi-map.ts +131 -97
- package/src/data-structures/graph/abstract-graph.ts +339 -264
- package/src/data-structures/graph/directed-graph.ts +146 -236
- package/src/data-structures/graph/map-graph.ts +63 -60
- package/src/data-structures/graph/undirected-graph.ts +129 -152
- package/src/data-structures/hash/hash-map.ts +274 -496
- package/src/data-structures/heap/heap.ts +389 -402
- package/src/data-structures/heap/max-heap.ts +12 -76
- package/src/data-structures/heap/min-heap.ts +13 -76
- package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
- package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
- package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
- package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
- package/src/data-structures/priority-queue/priority-queue.ts +3 -92
- package/src/data-structures/queue/deque.ts +381 -357
- package/src/data-structures/queue/queue.ts +310 -264
- package/src/data-structures/stack/stack.ts +217 -131
- package/src/data-structures/trie/trie.ts +240 -175
- package/src/interfaces/binary-tree.ts +240 -6
- package/src/interfaces/graph.ts +37 -0
- package/src/types/data-structures/base/base.ts +5 -5
- package/src/types/data-structures/graph/abstract-graph.ts +5 -0
- package/src/types/utils/utils.ts +9 -5
- package/src/utils/utils.ts +152 -86
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* data-structure-typed
|
|
3
|
-
*
|
|
4
|
-
* @
|
|
3
|
+
*
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
5
6
|
* @license MIT License
|
|
6
7
|
*/
|
|
7
8
|
import type { Comparator, DFSOrderPattern, ElementCallback, HeapOptions } from '../../types';
|
|
8
9
|
import { IterableElementBase } from '../base';
|
|
9
10
|
/**
|
|
11
|
+
* Binary heap with pluggable comparator; supports fast insertion and removal of the top element.
|
|
12
|
+
* @remarks Time O(1), Space O(1)
|
|
13
|
+
* @template E
|
|
14
|
+
* @template R
|
|
10
15
|
* 1. Complete Binary Tree: Heaps are typically complete binary trees, meaning every level is fully filled except possibly for the last level, which has nodes as far left as possible.
|
|
11
16
|
* 2. Heap Properties: Each node in a heap follows a specific order property, which varies depending on the type of heap:
|
|
12
17
|
* Max Heap: The value of each parent node is greater than or equal to the value of its children.
|
|
@@ -183,222 +188,230 @@ import { IterableElementBase } from '../base';
|
|
|
183
188
|
* ]);
|
|
184
189
|
* console.log(scheduleTasks(tasks, 2)); // expectedMap
|
|
185
190
|
*/
|
|
186
|
-
export declare class Heap<E =
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
*
|
|
190
|
-
*
|
|
191
|
-
*
|
|
192
|
-
*
|
|
193
|
-
* @
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
* function for comparing elements in the heap.
|
|
197
|
-
* The comparator function is used to determine the
|
|
198
|
-
* order of elements in the heap.
|
|
199
|
-
*/
|
|
200
|
-
constructor(elements?: Iterable<E> | Iterable<R>, options?: HeapOptions<E, R>);
|
|
191
|
+
export declare class Heap<E = unknown, R = never> extends IterableElementBase<E, R> {
|
|
192
|
+
protected _equals: (a: E, b: E) => boolean;
|
|
193
|
+
/**
|
|
194
|
+
* Create a Heap and optionally bulk-insert elements.
|
|
195
|
+
* @remarks Time O(N), Space O(N)
|
|
196
|
+
* @param [elements] - Iterable of elements (or raw values if toElementFn is set).
|
|
197
|
+
* @param [options] - Options such as comparator and toElementFn.
|
|
198
|
+
* @returns New Heap instance.
|
|
199
|
+
*/
|
|
200
|
+
constructor(elements?: Iterable<E | R>, options?: HeapOptions<E, R>);
|
|
201
201
|
protected _elements: E[];
|
|
202
202
|
/**
|
|
203
|
-
*
|
|
204
|
-
* @
|
|
203
|
+
* Get the backing array of the heap.
|
|
204
|
+
* @remarks Time O(1), Space O(1)
|
|
205
|
+
* @returns Internal elements array.
|
|
205
206
|
*/
|
|
206
207
|
get elements(): E[];
|
|
207
208
|
/**
|
|
208
|
-
* Get the
|
|
209
|
+
* Get the number of elements.
|
|
210
|
+
* @remarks Time O(1), Space O(1)
|
|
211
|
+
* @returns Heap size.
|
|
209
212
|
*/
|
|
210
213
|
get size(): number;
|
|
211
214
|
/**
|
|
212
|
-
* Get the last
|
|
213
|
-
* @
|
|
215
|
+
* Get the last leaf element.
|
|
216
|
+
* @remarks Time O(1), Space O(1)
|
|
217
|
+
* @returns Last element or undefined.
|
|
214
218
|
*/
|
|
215
219
|
get leaf(): E | undefined;
|
|
216
220
|
/**
|
|
217
|
-
*
|
|
218
|
-
* @
|
|
219
|
-
* @
|
|
220
|
-
* @
|
|
221
|
+
* Create a heap of the same class from an iterable.
|
|
222
|
+
* @remarks Time O(N), Space O(N)
|
|
223
|
+
* @template T
|
|
224
|
+
* @template R
|
|
225
|
+
* @template S
|
|
226
|
+
* @param [elements] - Iterable of elements or raw records.
|
|
227
|
+
* @param [options] - Heap options including comparator.
|
|
228
|
+
* @returns A new heap instance of this class.
|
|
229
|
+
*/
|
|
230
|
+
static from<T, R = never, S extends Heap<T, R> = Heap<T, R>>(this: new (elements?: Iterable<T | R>, options?: HeapOptions<T, R>) => S, elements?: Iterable<T | R>, options?: HeapOptions<T, R>): S;
|
|
231
|
+
/**
|
|
232
|
+
* Build a Heap from an iterable in linear time given a comparator.
|
|
233
|
+
* @remarks Time O(N), Space O(N)
|
|
234
|
+
* @template EE
|
|
235
|
+
* @template RR
|
|
236
|
+
* @param elements - Iterable of elements.
|
|
237
|
+
* @param options - Heap options including comparator.
|
|
238
|
+
* @returns A new Heap built from elements.
|
|
221
239
|
*/
|
|
222
|
-
static heapify<
|
|
240
|
+
static heapify<EE = unknown, RR = never>(elements: Iterable<EE>, options: HeapOptions<EE, RR>): Heap<EE, RR>;
|
|
223
241
|
/**
|
|
224
|
-
*
|
|
225
|
-
*
|
|
226
|
-
*
|
|
227
|
-
*
|
|
228
|
-
* @param {E} element - The `element` parameter represents the element that you want to add to the
|
|
229
|
-
* data structure.
|
|
230
|
-
* @returns The `add` method is returning a boolean value, which is the result of calling the
|
|
231
|
-
* `_bubbleUp` method with the index `this.elements.length - 1` as an argument.
|
|
242
|
+
* Insert an element.
|
|
243
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
244
|
+
* @param element - Element to insert.
|
|
245
|
+
* @returns True.
|
|
232
246
|
*/
|
|
233
247
|
add(element: E): boolean;
|
|
234
248
|
/**
|
|
235
|
-
*
|
|
236
|
-
*
|
|
237
|
-
*
|
|
238
|
-
*
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
*
|
|
243
|
-
*
|
|
244
|
-
* @returns
|
|
245
|
-
* in the input iterable was successfully added to the data structure.
|
|
246
|
-
*/
|
|
247
|
-
addMany(elements: Iterable<E> | Iterable<R>): boolean[];
|
|
248
|
-
/**
|
|
249
|
-
* Time Complexity: O(log n)
|
|
250
|
-
* Space Complexity: O(1)
|
|
251
|
-
*
|
|
252
|
-
* Remove and return the top element (the smallest or largest element) from the heap.
|
|
253
|
-
* @returns The top element or undefined if the heap is empty.
|
|
249
|
+
* Insert many elements from an iterable.
|
|
250
|
+
* @remarks Time O(N log N), Space O(1)
|
|
251
|
+
* @param elements - Iterable of elements or raw values.
|
|
252
|
+
* @returns Array of per-element success flags.
|
|
253
|
+
*/
|
|
254
|
+
addMany(elements: Iterable<E | R>): boolean[];
|
|
255
|
+
/**
|
|
256
|
+
* Remove and return the top element.
|
|
257
|
+
* @remarks Time O(log N), Space O(1)
|
|
258
|
+
* @returns Top element or undefined.
|
|
254
259
|
*/
|
|
255
260
|
poll(): E | undefined;
|
|
256
261
|
/**
|
|
257
|
-
*
|
|
258
|
-
*
|
|
259
|
-
*
|
|
260
|
-
* Peek at the top element of the heap without removing it.
|
|
261
|
-
* @returns The top element or undefined if the heap is empty.
|
|
262
|
+
* Get the current top element without removing it.
|
|
263
|
+
* @remarks Time O(1), Space O(1)
|
|
264
|
+
* @returns Top element or undefined.
|
|
262
265
|
*/
|
|
263
266
|
peek(): E | undefined;
|
|
264
267
|
/**
|
|
265
|
-
* Check
|
|
266
|
-
* @
|
|
268
|
+
* Check whether the heap is empty.
|
|
269
|
+
* @remarks Time O(1), Space O(1)
|
|
270
|
+
* @returns True if size is 0.
|
|
267
271
|
*/
|
|
268
272
|
isEmpty(): boolean;
|
|
269
273
|
/**
|
|
270
|
-
*
|
|
274
|
+
* Remove all elements.
|
|
275
|
+
* @remarks Time O(1), Space O(1)
|
|
276
|
+
* @returns void
|
|
271
277
|
*/
|
|
272
278
|
clear(): void;
|
|
273
279
|
/**
|
|
274
|
-
*
|
|
275
|
-
*
|
|
276
|
-
*
|
|
277
|
-
*
|
|
278
|
-
* @param elements
|
|
280
|
+
* Replace the backing array and rebuild the heap.
|
|
281
|
+
* @remarks Time O(N), Space O(N)
|
|
282
|
+
* @param elements - Iterable used to refill the heap.
|
|
283
|
+
* @returns Array of per-node results from fixing steps.
|
|
279
284
|
*/
|
|
280
|
-
refill(elements: E
|
|
285
|
+
refill(elements: Iterable<E>): boolean[];
|
|
281
286
|
/**
|
|
282
|
-
*
|
|
283
|
-
*
|
|
284
|
-
*
|
|
285
|
-
*
|
|
286
|
-
* @param element - the element to check.
|
|
287
|
-
* @returns Returns true if the specified element is contained; otherwise, returns false.
|
|
287
|
+
* Check if an equal element exists in the heap.
|
|
288
|
+
* @remarks Time O(N), Space O(1)
|
|
289
|
+
* @param element - Element to search for.
|
|
290
|
+
* @returns True if found.
|
|
288
291
|
*/
|
|
289
292
|
has(element: E): boolean;
|
|
290
293
|
/**
|
|
291
|
-
*
|
|
292
|
-
*
|
|
293
|
-
*
|
|
294
|
-
*
|
|
295
|
-
* and structure of the remaining elements.
|
|
296
|
-
* @param {E} element - The `element` parameter represents the element that you want to delete from
|
|
297
|
-
* the array `this.elements`.
|
|
298
|
-
* @returns The `delete` function is returning a boolean value. It returns `true` if the element was
|
|
299
|
-
* successfully deleted from the array, and `false` if the element was not found in the array.
|
|
294
|
+
* Delete one occurrence of an element.
|
|
295
|
+
* @remarks Time O(N), Space O(1)
|
|
296
|
+
* @param element - Element to delete.
|
|
297
|
+
* @returns True if an element was removed.
|
|
300
298
|
*/
|
|
301
299
|
delete(element: E): boolean;
|
|
302
300
|
/**
|
|
303
|
-
*
|
|
304
|
-
*
|
|
305
|
-
*
|
|
306
|
-
*
|
|
307
|
-
|
|
308
|
-
|
|
301
|
+
* Delete the first element that matches a predicate.
|
|
302
|
+
* @remarks Time O(N), Space O(1)
|
|
303
|
+
* @param predicate - Function (element, index, heap) → boolean.
|
|
304
|
+
* @returns True if an element was removed.
|
|
305
|
+
*/
|
|
306
|
+
deleteBy(predicate: (element: E, index: number, heap: this) => boolean): boolean;
|
|
307
|
+
/**
|
|
308
|
+
* Set the equality comparator used by has/delete operations.
|
|
309
|
+
* @remarks Time O(1), Space O(1)
|
|
310
|
+
* @param equals - Equality predicate (a, b) → boolean.
|
|
311
|
+
* @returns This heap.
|
|
312
|
+
*/
|
|
313
|
+
setEquality(equals: (a: E, b: E) => boolean): this;
|
|
314
|
+
/**
|
|
315
|
+
* Traverse the binary heap as a complete binary tree and collect elements.
|
|
316
|
+
* @remarks Time O(N), Space O(H)
|
|
317
|
+
* @param [order] - Traversal order: 'PRE' | 'IN' | 'POST'.
|
|
318
|
+
* @returns Array of visited elements.
|
|
309
319
|
*/
|
|
310
320
|
dfs(order?: DFSOrderPattern): E[];
|
|
311
321
|
/**
|
|
312
|
-
*
|
|
313
|
-
*
|
|
314
|
-
*
|
|
315
|
-
* Clone the heap, creating a new heap with the same elements.
|
|
316
|
-
* @returns A new Heap instance containing the same elements.
|
|
322
|
+
* Restore heap order bottom-up (heapify in-place).
|
|
323
|
+
* @remarks Time O(N), Space O(1)
|
|
324
|
+
* @returns Array of per-node results from fixing steps.
|
|
317
325
|
*/
|
|
318
|
-
|
|
326
|
+
fix(): boolean[];
|
|
319
327
|
/**
|
|
320
|
-
*
|
|
321
|
-
*
|
|
322
|
-
*
|
|
323
|
-
* Sort the elements in the heap and return them as an array.
|
|
324
|
-
* @returns An array containing the elements sorted in ascending order.
|
|
328
|
+
* Return all elements in ascending order by repeatedly polling.
|
|
329
|
+
* @remarks Time O(N log N), Space O(N)
|
|
330
|
+
* @returns Sorted array of elements.
|
|
325
331
|
*/
|
|
326
332
|
sort(): E[];
|
|
327
333
|
/**
|
|
328
|
-
*
|
|
329
|
-
*
|
|
330
|
-
*
|
|
331
|
-
* Fix the entire heap to maintain heap properties.
|
|
334
|
+
* Deep clone this heap.
|
|
335
|
+
* @remarks Time O(N), Space O(N)
|
|
336
|
+
* @returns A new heap with the same elements.
|
|
332
337
|
*/
|
|
333
|
-
|
|
338
|
+
clone(): this;
|
|
339
|
+
/**
|
|
340
|
+
* Filter elements into a new heap of the same class.
|
|
341
|
+
* @remarks Time O(N log N), Space O(N)
|
|
342
|
+
* @param callback - Predicate (element, index, heap) → boolean to keep element.
|
|
343
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
344
|
+
* @returns A new heap with the kept elements.
|
|
345
|
+
*/
|
|
346
|
+
filter(callback: ElementCallback<E, R, boolean>, thisArg?: unknown): this;
|
|
334
347
|
/**
|
|
335
|
-
*
|
|
336
|
-
*
|
|
337
|
-
*
|
|
338
|
-
*
|
|
339
|
-
* function.
|
|
340
|
-
* @param
|
|
341
|
-
*
|
|
342
|
-
*
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
*
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
*
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
* The `map` function creates a new heap by applying a callback function to each element of the
|
|
356
|
-
* original heap.
|
|
357
|
-
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
358
|
-
* the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
|
|
359
|
-
* element), and `this` (the heap itself). The callback function should return a value of
|
|
360
|
-
* @param comparator - The `comparator` parameter is a function that defines the order of the
|
|
361
|
-
* elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
|
|
362
|
-
* if `a` should be placed before `b`, a positive number if `a` should be placed after
|
|
363
|
-
* @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
|
|
364
|
-
* element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
|
|
365
|
-
* returns a value of type `T`. This function is used to transform the elements of the original
|
|
366
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
367
|
-
* specify the value of `this` within the callback function. It is used to set the context or scope
|
|
368
|
-
* in which the callback function will be executed. If `thisArg` is provided, it will be used as the
|
|
369
|
-
* value of
|
|
370
|
-
* @returns a new instance of the `Heap` class with the mapped elements.
|
|
371
|
-
*/
|
|
372
|
-
map<EM, RM>(callback: ElementCallback<E, R, EM>, comparator: Comparator<EM>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): Heap<EM, RM>;
|
|
348
|
+
* Map elements into a new heap of possibly different element type.
|
|
349
|
+
* @remarks Time O(N log N), Space O(N)
|
|
350
|
+
* @template EM
|
|
351
|
+
* @template RM
|
|
352
|
+
* @param callback - Mapping function (element, index, heap) → newElement.
|
|
353
|
+
* @param options - Options for the output heap, including comparator for EM.
|
|
354
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
355
|
+
* @returns A new heap with mapped elements.
|
|
356
|
+
*/
|
|
357
|
+
map<EM, RM>(callback: ElementCallback<E, R, EM>, options: HeapOptions<EM, RM> & {
|
|
358
|
+
comparator: Comparator<EM>;
|
|
359
|
+
}, thisArg?: unknown): Heap<EM, RM>;
|
|
360
|
+
/**
|
|
361
|
+
* Map elements into a new heap of the same element type.
|
|
362
|
+
* @remarks Time O(N log N), Space O(N)
|
|
363
|
+
* @param callback - Mapping function (element, index, heap) → element.
|
|
364
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
365
|
+
* @returns A new heap with mapped elements.
|
|
366
|
+
*/
|
|
367
|
+
mapSame(callback: ElementCallback<E, R, E>, thisArg?: unknown): this;
|
|
373
368
|
protected _DEFAULT_COMPARATOR: (a: E, b: E) => number;
|
|
374
|
-
protected _comparator: Comparator<E>;
|
|
369
|
+
protected _comparator: Comparator<E>; /**
|
|
370
|
+
* Get the comparator used to order elements.
|
|
371
|
+
* @remarks Time O(1), Space O(1)
|
|
372
|
+
* @returns Comparator function.
|
|
373
|
+
*/
|
|
375
374
|
/**
|
|
376
|
-
*
|
|
377
|
-
* @
|
|
375
|
+
* Get the comparator used to order elements.
|
|
376
|
+
* @remarks Time O(1), Space O(1)
|
|
377
|
+
* @returns Comparator function.
|
|
378
378
|
*/
|
|
379
379
|
get comparator(): Comparator<E>;
|
|
380
|
+
protected _getIterator(): IterableIterator<E>;
|
|
381
|
+
protected _bubbleUp(index: number): boolean;
|
|
382
|
+
protected _sinkDown(index: number, halfLength: number): boolean;
|
|
380
383
|
/**
|
|
381
|
-
*
|
|
384
|
+
* (Protected) Create an empty instance of the same concrete class.
|
|
385
|
+
* @remarks Time O(1), Space O(1)
|
|
386
|
+
* @param [options] - Options to override comparator or toElementFn.
|
|
387
|
+
* @returns A like-kind empty heap instance.
|
|
382
388
|
*/
|
|
383
|
-
protected
|
|
389
|
+
protected _createInstance(options?: HeapOptions<E, R>): this;
|
|
384
390
|
/**
|
|
385
|
-
*
|
|
386
|
-
*
|
|
387
|
-
*
|
|
388
|
-
*
|
|
389
|
-
* @param
|
|
391
|
+
* (Protected) Create a like-kind instance seeded by elements.
|
|
392
|
+
* @remarks Time O(N log N), Space O(N)
|
|
393
|
+
* @template EM
|
|
394
|
+
* @template RM
|
|
395
|
+
* @param [elements] - Iterable of elements or raw values to seed.
|
|
396
|
+
* @param [options] - Options forwarded to the constructor.
|
|
397
|
+
* @returns A like-kind heap instance.
|
|
390
398
|
*/
|
|
391
|
-
protected
|
|
399
|
+
protected _createLike<EM, RM>(elements?: Iterable<EM> | Iterable<RM>, options?: HeapOptions<EM, RM>): Heap<EM, RM>;
|
|
392
400
|
/**
|
|
393
|
-
*
|
|
394
|
-
*
|
|
395
|
-
*
|
|
396
|
-
*
|
|
397
|
-
* @param
|
|
398
|
-
* @
|
|
401
|
+
* (Protected) Spawn an empty like-kind heap instance.
|
|
402
|
+
* @remarks Time O(1), Space O(1)
|
|
403
|
+
* @template EM
|
|
404
|
+
* @template RM
|
|
405
|
+
* @param [options] - Options forwarded to the constructor.
|
|
406
|
+
* @returns An empty like-kind heap instance.
|
|
399
407
|
*/
|
|
400
|
-
protected
|
|
408
|
+
protected _spawnLike<EM, RM>(options?: HeapOptions<EM, RM>): Heap<EM, RM>;
|
|
401
409
|
}
|
|
410
|
+
/**
|
|
411
|
+
* Node container used by FibonacciHeap.
|
|
412
|
+
* @remarks Time O(1), Space O(1)
|
|
413
|
+
* @template E
|
|
414
|
+
*/
|
|
402
415
|
export declare class FibonacciHeapNode<E> {
|
|
403
416
|
element: E;
|
|
404
417
|
degree: number;
|
|
@@ -407,172 +420,84 @@ export declare class FibonacciHeapNode<E> {
|
|
|
407
420
|
child?: FibonacciHeapNode<E>;
|
|
408
421
|
parent?: FibonacciHeapNode<E>;
|
|
409
422
|
marked: boolean;
|
|
410
|
-
/**
|
|
411
|
-
* The constructor function initializes an object with an element and a degree, and sets the marked
|
|
412
|
-
* property to false.
|
|
413
|
-
* @param {E} element - The "element" parameter represents the value or data that will be stored in
|
|
414
|
-
* the node of a data structure. It can be any type of data, such as a number, string, object, or
|
|
415
|
-
* even another data structure.
|
|
416
|
-
* @param [degree=0] - The degree parameter represents the degree of the element in a data structure
|
|
417
|
-
* called a Fibonacci heap. The degree of a node is the number of children it has. By default, the
|
|
418
|
-
* degree is set to 0 when a new node is created.
|
|
419
|
-
*/
|
|
420
423
|
constructor(element: E, degree?: number);
|
|
421
424
|
}
|
|
425
|
+
/**
|
|
426
|
+
* Fibonacci heap (min-heap) optimized for fast merges and amortized operations.
|
|
427
|
+
* @remarks Time O(1), Space O(1)
|
|
428
|
+
* @template E
|
|
429
|
+
* @example examples will be generated by unit test
|
|
430
|
+
*/
|
|
422
431
|
export declare class FibonacciHeap<E> {
|
|
423
432
|
/**
|
|
424
|
-
*
|
|
425
|
-
* @
|
|
426
|
-
*
|
|
427
|
-
*
|
|
428
|
-
* provided, a default comparator function will be used.
|
|
433
|
+
* Create a FibonacciHeap.
|
|
434
|
+
* @remarks Time O(1), Space O(1)
|
|
435
|
+
* @param [comparator] - Comparator to order elements (min-heap by default).
|
|
436
|
+
* @returns New FibonacciHeap instance.
|
|
429
437
|
*/
|
|
430
438
|
constructor(comparator?: Comparator<E>);
|
|
431
439
|
protected _root?: FibonacciHeapNode<E>;
|
|
432
440
|
/**
|
|
433
|
-
*
|
|
434
|
-
* @
|
|
441
|
+
* Get the circular root list head.
|
|
442
|
+
* @remarks Time O(1), Space O(1)
|
|
443
|
+
* @returns Root node or undefined.
|
|
435
444
|
*/
|
|
436
445
|
get root(): FibonacciHeapNode<E> | undefined;
|
|
437
446
|
protected _size: number;
|
|
438
|
-
/**
|
|
439
|
-
* The function returns the size of an object.
|
|
440
|
-
* @returns The size of the object, which is a number.
|
|
441
|
-
*/
|
|
442
447
|
get size(): number;
|
|
443
448
|
protected _min?: FibonacciHeapNode<E>;
|
|
444
449
|
/**
|
|
445
|
-
*
|
|
446
|
-
* @
|
|
447
|
-
*
|
|
450
|
+
* Get the current minimum node.
|
|
451
|
+
* @remarks Time O(1), Space O(1)
|
|
452
|
+
* @returns Min node or undefined.
|
|
448
453
|
*/
|
|
449
454
|
get min(): FibonacciHeapNode<E> | undefined;
|
|
450
455
|
protected _comparator: Comparator<E>;
|
|
451
|
-
/**
|
|
452
|
-
* The function returns the comparator used for comparing elements.
|
|
453
|
-
* @returns The `_comparator` property of the object.
|
|
454
|
-
*/
|
|
455
456
|
get comparator(): Comparator<E>;
|
|
456
|
-
/**
|
|
457
|
-
* Get the size (number of elements) of the heap.
|
|
458
|
-
* @returns {number} The size of the heap. Returns 0 if the heap is empty. Returns -1 if the heap is invalid.
|
|
459
|
-
*/
|
|
460
457
|
clear(): void;
|
|
458
|
+
add(element: E): boolean;
|
|
461
459
|
/**
|
|
462
|
-
*
|
|
463
|
-
*
|
|
464
|
-
*
|
|
465
|
-
*
|
|
466
|
-
* @param element
|
|
467
|
-
* @returns {FibonacciHeap<E>} FibonacciHeap<E> - The heap itself.
|
|
468
|
-
*/
|
|
469
|
-
add(element: E): FibonacciHeap<E>;
|
|
470
|
-
/**
|
|
471
|
-
* Time Complexity: O(1)
|
|
472
|
-
* Space Complexity: O(1)
|
|
473
|
-
*
|
|
474
|
-
* Insert an element into the heap and maintain the heap properties.
|
|
475
|
-
* @param element
|
|
476
|
-
* @returns {FibonacciHeap<E>} FibonacciHeap<E> - The heap itself.
|
|
477
|
-
*/
|
|
478
|
-
push(element: E): FibonacciHeap<E>;
|
|
479
|
-
/**
|
|
480
|
-
* Time Complexity: O(1)
|
|
481
|
-
* Space Complexity: O(1)
|
|
482
|
-
*
|
|
483
|
-
* Peek at the top element of the heap without removing it.
|
|
484
|
-
* @returns The top element or undefined if the heap is empty.
|
|
485
|
-
* @protected
|
|
460
|
+
* Push an element into the root list.
|
|
461
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
462
|
+
* @param element - Element to insert.
|
|
463
|
+
* @returns This heap.
|
|
486
464
|
*/
|
|
465
|
+
push(element: E): this;
|
|
487
466
|
peek(): E | undefined;
|
|
488
467
|
/**
|
|
489
|
-
*
|
|
490
|
-
*
|
|
491
|
-
*
|
|
492
|
-
*
|
|
493
|
-
* @param {FibonacciHeapNode<E>} head - The head of the linked list.
|
|
494
|
-
* @protected
|
|
495
|
-
* @returns FibonacciHeapNode<E>[] - An array containing the elements of the linked list.
|
|
468
|
+
* Collect nodes from a circular doubly linked list starting at head.
|
|
469
|
+
* @remarks Time O(K), Space O(K)
|
|
470
|
+
* @param [head] - Start node of the circular list.
|
|
471
|
+
* @returns Array of nodes from the list.
|
|
496
472
|
*/
|
|
497
473
|
consumeLinkedList(head?: FibonacciHeapNode<E>): FibonacciHeapNode<E>[];
|
|
498
474
|
/**
|
|
499
|
-
*
|
|
500
|
-
*
|
|
501
|
-
*
|
|
502
|
-
* @param
|
|
503
|
-
* @
|
|
475
|
+
* Insert a node into a parent's child list (circular).
|
|
476
|
+
* @remarks Time O(1), Space O(1)
|
|
477
|
+
* @param parent - Parent node.
|
|
478
|
+
* @param node - Child node to insert.
|
|
479
|
+
* @returns void
|
|
504
480
|
*/
|
|
505
481
|
mergeWithChild(parent: FibonacciHeapNode<E>, node: FibonacciHeapNode<E>): void;
|
|
506
|
-
/**
|
|
507
|
-
* Time Complexity: O(log n)
|
|
508
|
-
* Space Complexity: O(1)
|
|
509
|
-
*
|
|
510
|
-
* Remove and return the top element (the smallest or largest element) from the heap.
|
|
511
|
-
* @returns The top element or undefined if the heap is empty.
|
|
512
|
-
*/
|
|
513
482
|
poll(): E | undefined;
|
|
514
483
|
/**
|
|
515
|
-
*
|
|
516
|
-
*
|
|
517
|
-
*
|
|
518
|
-
* Remove and return the top element (the smallest or largest element) from the heap.
|
|
519
|
-
* @returns The top element or undefined if the heap is empty.
|
|
484
|
+
* Remove and return the minimum element, consolidating the root list.
|
|
485
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
486
|
+
* @returns Minimum element or undefined.
|
|
520
487
|
*/
|
|
521
488
|
pop(): E | undefined;
|
|
522
489
|
/**
|
|
523
|
-
*
|
|
524
|
-
*
|
|
525
|
-
*
|
|
526
|
-
*
|
|
527
|
-
* @param heapToMerge
|
|
490
|
+
* Meld another heap into this heap.
|
|
491
|
+
* @remarks Time O(1), Space O(1)
|
|
492
|
+
* @param heapToMerge - Another FibonacciHeap to meld into this one.
|
|
493
|
+
* @returns void
|
|
528
494
|
*/
|
|
529
495
|
merge(heapToMerge: FibonacciHeap<E>): void;
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
* @param element
|
|
533
|
-
* @protected
|
|
534
|
-
*/
|
|
535
|
-
createNode(element: E): FibonacciHeapNode<E>;
|
|
536
|
-
/**
|
|
537
|
-
* Default comparator function used by the heap.
|
|
538
|
-
* @param {E} a
|
|
539
|
-
* @param {E} b
|
|
540
|
-
* @protected
|
|
541
|
-
*/
|
|
496
|
+
_createNode(element: E): FibonacciHeapNode<E>;
|
|
497
|
+
isEmpty(): boolean;
|
|
542
498
|
protected _defaultComparator(a: E, b: E): number;
|
|
543
|
-
/**
|
|
544
|
-
* Time Complexity: O(1)
|
|
545
|
-
* Space Complexity: O(1)
|
|
546
|
-
*
|
|
547
|
-
* Merge the given node with the root list.
|
|
548
|
-
* @param node - The node to be merged.
|
|
549
|
-
*/
|
|
550
499
|
protected mergeWithRoot(node: FibonacciHeapNode<E>): void;
|
|
551
|
-
/**
|
|
552
|
-
* Time Complexity: O(1)
|
|
553
|
-
* Space Complexity: O(1)
|
|
554
|
-
*
|
|
555
|
-
* Remove and return the top element (the smallest or largest element) from the heap.
|
|
556
|
-
* @param node - The node to be removed.
|
|
557
|
-
* @protected
|
|
558
|
-
*/
|
|
559
500
|
protected removeFromRoot(node: FibonacciHeapNode<E>): void;
|
|
560
|
-
/**
|
|
561
|
-
* Time Complexity: O(1)
|
|
562
|
-
* Space Complexity: O(1)
|
|
563
|
-
*
|
|
564
|
-
* Remove and return the top element (the smallest or largest element) from the heap.
|
|
565
|
-
* @param y
|
|
566
|
-
* @param x
|
|
567
|
-
* @protected
|
|
568
|
-
*/
|
|
569
501
|
protected _link(y: FibonacciHeapNode<E>, x: FibonacciHeapNode<E>): void;
|
|
570
|
-
/**
|
|
571
|
-
* Time Complexity: O(n log n)
|
|
572
|
-
* Space Complexity: O(n)
|
|
573
|
-
*
|
|
574
|
-
* Remove and return the top element (the smallest or largest element) from the heap.
|
|
575
|
-
* @protected
|
|
576
|
-
*/
|
|
577
502
|
protected _consolidate(): void;
|
|
578
503
|
}
|