deque-typed 2.0.5 → 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.
Files changed (101) hide show
  1. package/dist/data-structures/base/iterable-element-base.d.ts +186 -83
  2. package/dist/data-structures/base/iterable-element-base.js +149 -107
  3. package/dist/data-structures/base/iterable-entry-base.d.ts +95 -119
  4. package/dist/data-structures/base/iterable-entry-base.js +59 -116
  5. package/dist/data-structures/base/linear-base.d.ts +250 -192
  6. package/dist/data-structures/base/linear-base.js +137 -274
  7. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
  8. package/dist/data-structures/binary-tree/avl-tree-counter.js +171 -205
  9. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
  10. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -87
  11. package/dist/data-structures/binary-tree/avl-tree.d.ts +138 -149
  12. package/dist/data-structures/binary-tree/avl-tree.js +208 -195
  13. package/dist/data-structures/binary-tree/binary-tree.d.ts +476 -632
  14. package/dist/data-structures/binary-tree/binary-tree.js +598 -869
  15. package/dist/data-structures/binary-tree/bst.d.ts +258 -306
  16. package/dist/data-structures/binary-tree/bst.js +505 -481
  17. package/dist/data-structures/binary-tree/red-black-tree.d.ts +107 -179
  18. package/dist/data-structures/binary-tree/red-black-tree.js +114 -209
  19. package/dist/data-structures/binary-tree/tree-counter.d.ts +132 -154
  20. package/dist/data-structures/binary-tree/tree-counter.js +172 -203
  21. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
  22. package/dist/data-structures/binary-tree/tree-multi-map.js +105 -85
  23. package/dist/data-structures/graph/abstract-graph.d.ts +238 -233
  24. package/dist/data-structures/graph/abstract-graph.js +267 -237
  25. package/dist/data-structures/graph/directed-graph.d.ts +108 -224
  26. package/dist/data-structures/graph/directed-graph.js +146 -233
  27. package/dist/data-structures/graph/map-graph.d.ts +49 -55
  28. package/dist/data-structures/graph/map-graph.js +56 -59
  29. package/dist/data-structures/graph/undirected-graph.d.ts +103 -146
  30. package/dist/data-structures/graph/undirected-graph.js +129 -149
  31. package/dist/data-structures/hash/hash-map.d.ts +164 -338
  32. package/dist/data-structures/hash/hash-map.js +270 -457
  33. package/dist/data-structures/heap/heap.d.ts +214 -289
  34. package/dist/data-structures/heap/heap.js +340 -349
  35. package/dist/data-structures/heap/max-heap.d.ts +11 -47
  36. package/dist/data-structures/heap/max-heap.js +11 -66
  37. package/dist/data-structures/heap/min-heap.d.ts +12 -47
  38. package/dist/data-structures/heap/min-heap.js +11 -66
  39. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
  40. package/dist/data-structures/linked-list/doubly-linked-list.js +368 -494
  41. package/dist/data-structures/linked-list/singly-linked-list.d.ts +261 -310
  42. package/dist/data-structures/linked-list/singly-linked-list.js +447 -466
  43. package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -107
  44. package/dist/data-structures/linked-list/skip-linked-list.js +0 -100
  45. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
  46. package/dist/data-structures/priority-queue/max-priority-queue.js +11 -78
  47. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
  48. package/dist/data-structures/priority-queue/min-priority-queue.js +10 -79
  49. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -61
  50. package/dist/data-structures/priority-queue/priority-queue.js +8 -83
  51. package/dist/data-structures/queue/deque.d.ts +227 -254
  52. package/dist/data-structures/queue/deque.js +309 -348
  53. package/dist/data-structures/queue/queue.d.ts +180 -201
  54. package/dist/data-structures/queue/queue.js +265 -248
  55. package/dist/data-structures/stack/stack.d.ts +124 -102
  56. package/dist/data-structures/stack/stack.js +181 -125
  57. package/dist/data-structures/trie/trie.d.ts +164 -165
  58. package/dist/data-structures/trie/trie.js +189 -172
  59. package/dist/interfaces/binary-tree.d.ts +56 -6
  60. package/dist/interfaces/graph.d.ts +16 -0
  61. package/dist/types/data-structures/base/base.d.ts +1 -1
  62. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -0
  63. package/dist/types/utils/utils.d.ts +1 -0
  64. package/dist/utils/utils.d.ts +1 -1
  65. package/dist/utils/utils.js +2 -1
  66. package/package.json +2 -2
  67. package/src/data-structures/base/iterable-element-base.ts +238 -115
  68. package/src/data-structures/base/iterable-entry-base.ts +96 -120
  69. package/src/data-structures/base/linear-base.ts +271 -277
  70. package/src/data-structures/binary-tree/avl-tree-counter.ts +198 -216
  71. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +192 -101
  72. package/src/data-structures/binary-tree/avl-tree.ts +239 -206
  73. package/src/data-structures/binary-tree/binary-tree.ts +664 -893
  74. package/src/data-structures/binary-tree/bst.ts +568 -570
  75. package/src/data-structures/binary-tree/red-black-tree.ts +161 -222
  76. package/src/data-structures/binary-tree/tree-counter.ts +199 -218
  77. package/src/data-structures/binary-tree/tree-multi-map.ts +131 -97
  78. package/src/data-structures/graph/abstract-graph.ts +339 -264
  79. package/src/data-structures/graph/directed-graph.ts +146 -236
  80. package/src/data-structures/graph/map-graph.ts +63 -60
  81. package/src/data-structures/graph/undirected-graph.ts +129 -152
  82. package/src/data-structures/hash/hash-map.ts +274 -496
  83. package/src/data-structures/heap/heap.ts +389 -402
  84. package/src/data-structures/heap/max-heap.ts +12 -76
  85. package/src/data-structures/heap/min-heap.ts +13 -76
  86. package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
  87. package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
  88. package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
  89. package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
  90. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
  91. package/src/data-structures/priority-queue/priority-queue.ts +3 -92
  92. package/src/data-structures/queue/deque.ts +381 -357
  93. package/src/data-structures/queue/queue.ts +310 -264
  94. package/src/data-structures/stack/stack.ts +217 -131
  95. package/src/data-structures/trie/trie.ts +240 -175
  96. package/src/interfaces/binary-tree.ts +240 -6
  97. package/src/interfaces/graph.ts +37 -0
  98. package/src/types/data-structures/base/base.ts +5 -5
  99. package/src/types/data-structures/graph/abstract-graph.ts +5 -0
  100. package/src/types/utils/utils.ts +2 -0
  101. package/src/utils/utils.ts +9 -14
@@ -1,12 +1,17 @@
1
1
  /**
2
2
  * data-structure-typed
3
- * @author Kirk Qi
4
- * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
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 = any, R = any> extends IterableElementBase<E, R> {
187
- /**
188
- * The constructor initializes a heap data structure with optional elements and options.
189
- * @param elements - The `elements` parameter is an iterable object that contains the initial
190
- * elements to be added to the heap.
191
- * It is an optional parameter, and if not provided, the heap will
192
- * be initialized as empty.
193
- * @param [options] - The `options` parameter is an optional object that can contain additional
194
- * configuration options for the heap.
195
- * In this case, it is used to specify a custom comparator
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
- * The function returns an array of elements.
204
- * @returns The element array is being returned.
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 size (number of elements) of the heap.
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 element in the heap, which is not necessarily a leaf node.
213
- * @returns The last element or undefined if the heap is empty.
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
- * Static method that creates a binary heap from an array of elements and a comparison function.
218
- * @returns A new Heap instance.
219
- * @param elements
220
- * @param options
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<E = any, R = any>(elements: Iterable<E>, options: HeapOptions<E, R>): Heap<E>;
240
+ static heapify<EE = unknown, RR = never>(elements: Iterable<EE>, options: HeapOptions<EE, RR>): Heap<EE, RR>;
223
241
  /**
224
- * Time Complexity: O(log n)
225
- * Space Complexity: O(1)
226
- *
227
- * The add function pushes an element into an array and then triggers a bubble-up operation.
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
- * Time Complexity: O(k log n)
236
- * Space Complexity: O(1)
237
- *
238
- * The `addMany` function iterates over elements and adds them to a collection, returning an array of
239
- * boolean values indicating success or failure.
240
- * @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the `addMany` method is
241
- * an iterable containing elements of type `E` or `R`. The method iterates over each element in the
242
- * iterable and adds them to the data structure. If a transformation function `_toElementFn` is
243
- * provided, it transforms the element
244
- * @returns The `addMany` method returns an array of boolean values indicating whether each element
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
- * Time Complexity: O(1)
258
- * Space Complexity: O(1)
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 if the heap is empty.
266
- * @returns True if the heap is empty, otherwise false.
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
- * Reset the elements of the heap. Make the elements empty.
274
+ * Remove all elements.
275
+ * @remarks Time O(1), Space O(1)
276
+ * @returns void
271
277
  */
272
278
  clear(): void;
273
279
  /**
274
- * Time Complexity: O(n)
275
- * Space Complexity: O(n)
276
- *
277
- * Clear and add elements of the heap
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[]): boolean[];
285
+ refill(elements: Iterable<E>): boolean[];
281
286
  /**
282
- * Time Complexity: O(n)
283
- * Space Complexity: O(1)
284
- *
285
- * Use a comparison function to check whether a binary heap contains a specific element.
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
- * Time Complexity: O(n)
292
- * Space Complexity: O(1)
293
- *
294
- * The `delete` function removes an element from an array-like data structure, maintaining the order
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
- * Time Complexity: O(n)
304
- * Space Complexity: O(log n)
305
- *
306
- * Depth-first search (DFS) method, different traversal orders can be selected。
307
- * @param order - Traverse order parameter: 'IN' (in-order), 'PRE' (pre-order) or 'POST' (post-order).
308
- * @returns An array containing elements traversed in the specified order.
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
- * Time Complexity: O(n)
313
- * Space Complexity: O(n)
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
- clone(): Heap<E, R>;
326
+ fix(): boolean[];
319
327
  /**
320
- * Time Complexity: O(n log n)
321
- * Space Complexity: O(n)
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
- * Time Complexity: O(n log n)
329
- * Space Complexity: O(n)
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
- fix(): boolean[];
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
- * Time Complexity: O(n)
336
- * Space Complexity: O(n)
337
- *
338
- * The `filter` function creates a new Heap object containing elements that pass a given callback
339
- * function.
340
- * @param callback - The `callback` parameter is a function that will be called for each element in
341
- * the heap. It takes three arguments: the current element, the index of the current element, and the
342
- * heap itself. The callback function should return a boolean value indicating whether the current
343
- * element should be included in the filtered list
344
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
345
- * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
346
- * passed as the `this` value to the `callback` function. If `thisArg` is
347
- * @returns The `filter` method is returning a new `Heap` object that contains the elements that pass
348
- * the filter condition specified by the `callback` function.
349
- */
350
- filter(callback: ElementCallback<E, R, boolean>, thisArg?: any): Heap<E, R>;
351
- /**
352
- * Time Complexity: O(n)
353
- * Space Complexity: O(n)
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
- * The function returns the value of the _comparator property.
377
- * @returns The `_comparator` property is being returned.
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
- * The function `_getIterator` returns an iterable iterator for the elements in the class.
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 _getIterator(): IterableIterator<E>;
389
+ protected _createInstance(options?: HeapOptions<E, R>): this;
384
390
  /**
385
- * Time Complexity: O(log n)
386
- * Space Complexity: O(1)
387
- *
388
- * Float operation to maintain heap properties after adding an element.
389
- * @param index - The index of the newly added element.
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 _bubbleUp(index: number): boolean;
399
+ protected _createLike<EM, RM>(elements?: Iterable<EM> | Iterable<RM>, options?: HeapOptions<EM, RM>): Heap<EM, RM>;
392
400
  /**
393
- * Time Complexity: O(log n)
394
- * Space Complexity: O(1)
395
- *
396
- * Sinking operation to maintain heap properties after removing the top element.
397
- * @param index - The index from which to start sinking.
398
- * @param halfLength
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 _sinkDown(index: number, halfLength: number): boolean;
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
- * The constructor function initializes a FibonacciHeap object with an optional comparator function.
425
- * @param [comparator] - The `comparator` parameter is an optional argument that represents a
426
- * function used to compare elements in the FibonacciHeap. If a comparator function is provided, it
427
- * will be used to determine the order of elements in the heap. If no comparator function is
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
- * The function returns the root node of a Fibonacci heap.
434
- * @returns The method is returning either a FibonacciHeapNode object or undefined.
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
- * The function returns the minimum node in a Fibonacci heap.
446
- * @returns The method is returning the minimum node of the Fibonacci heap, which is of type
447
- * `FibonacciHeapNode<E>`. If there is no minimum node, it will return `undefined`.
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
- * Time Complexity: O(1)
463
- * Space Complexity: O(1)
464
- *
465
- * Insert an element into the heap and maintain the heap properties.
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
- * Time Complexity: O(n), where n is the number of elements in the linked list.
490
- * Space Complexity: O(1)
491
- *
492
- * Get the size (number of elements) of the heap.
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
- * Time Complexity: O(1)
500
- * Space Complexity: O(1)
501
- *
502
- * @param parent
503
- * @param node
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
- * Time Complexity: O(log n)
516
- * Space Complexity: O(1)
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
- * Time Complexity: O(1)
524
- * Space Complexity: O(1)
525
- *
526
- * merge two heaps. The heap that is merged will be cleared. The heap that is merged into will remain.
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
- * Create a new node.
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
  }