undirected-graph-typed 1.51.8 → 1.52.1

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 (108) hide show
  1. package/dist/data-structures/base/index.d.ts +2 -1
  2. package/dist/data-structures/base/index.js +2 -1
  3. package/dist/data-structures/base/iterable-element-base.d.ts +171 -0
  4. package/dist/data-structures/base/iterable-element-base.js +225 -0
  5. package/dist/data-structures/base/{iterable-base.d.ts → iterable-entry-base.d.ts} +4 -147
  6. package/dist/data-structures/base/{iterable-base.js → iterable-entry-base.js} +12 -189
  7. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +106 -68
  8. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +119 -87
  9. package/dist/data-structures/binary-tree/avl-tree.d.ts +82 -62
  10. package/dist/data-structures/binary-tree/avl-tree.js +78 -59
  11. package/dist/data-structures/binary-tree/binary-tree.d.ts +333 -241
  12. package/dist/data-structures/binary-tree/binary-tree.js +478 -366
  13. package/dist/data-structures/binary-tree/bst.d.ts +202 -212
  14. package/dist/data-structures/binary-tree/bst.js +208 -250
  15. package/dist/data-structures/binary-tree/rb-tree.d.ts +73 -74
  16. package/dist/data-structures/binary-tree/rb-tree.js +107 -98
  17. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +92 -75
  18. package/dist/data-structures/binary-tree/tree-multi-map.js +105 -93
  19. package/dist/data-structures/graph/abstract-graph.d.ts +10 -15
  20. package/dist/data-structures/graph/abstract-graph.js +10 -15
  21. package/dist/data-structures/graph/directed-graph.js +2 -1
  22. package/dist/data-structures/hash/hash-map.d.ts +33 -40
  23. package/dist/data-structures/hash/hash-map.js +40 -55
  24. package/dist/data-structures/heap/heap.d.ts +43 -114
  25. package/dist/data-structures/heap/heap.js +59 -127
  26. package/dist/data-structures/heap/max-heap.d.ts +50 -4
  27. package/dist/data-structures/heap/max-heap.js +76 -10
  28. package/dist/data-structures/heap/min-heap.d.ts +51 -5
  29. package/dist/data-structures/heap/min-heap.js +68 -11
  30. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +22 -28
  31. package/dist/data-structures/linked-list/doubly-linked-list.js +26 -28
  32. package/dist/data-structures/linked-list/singly-linked-list.d.ts +22 -25
  33. package/dist/data-structures/linked-list/singly-linked-list.js +29 -26
  34. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +50 -4
  35. package/dist/data-structures/priority-queue/max-priority-queue.js +79 -10
  36. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +51 -5
  37. package/dist/data-structures/priority-queue/min-priority-queue.js +71 -11
  38. package/dist/data-structures/priority-queue/priority-queue.d.ts +50 -4
  39. package/dist/data-structures/priority-queue/priority-queue.js +70 -1
  40. package/dist/data-structures/queue/deque.d.ts +28 -20
  41. package/dist/data-structures/queue/deque.js +45 -24
  42. package/dist/data-structures/queue/queue.d.ts +8 -29
  43. package/dist/data-structures/queue/queue.js +15 -32
  44. package/dist/data-structures/stack/stack.d.ts +17 -22
  45. package/dist/data-structures/stack/stack.js +25 -24
  46. package/dist/data-structures/trie/trie.d.ts +19 -14
  47. package/dist/data-structures/trie/trie.js +27 -16
  48. package/dist/interfaces/binary-tree.d.ts +7 -7
  49. package/dist/types/common.d.ts +1 -23
  50. package/dist/types/data-structures/base/base.d.ts +5 -2
  51. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -4
  52. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -4
  53. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +22 -5
  54. package/dist/types/data-structures/binary-tree/bst.d.ts +7 -5
  55. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -4
  56. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -4
  57. package/dist/types/data-structures/heap/heap.d.ts +3 -2
  58. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
  59. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +2 -1
  60. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
  61. package/dist/types/data-structures/queue/deque.d.ts +4 -2
  62. package/dist/types/data-structures/queue/queue.d.ts +2 -1
  63. package/dist/types/data-structures/stack/stack.d.ts +2 -1
  64. package/dist/types/data-structures/trie/trie.d.ts +3 -2
  65. package/dist/utils/utils.js +3 -5
  66. package/package.json +2 -2
  67. package/src/data-structures/base/index.ts +2 -1
  68. package/src/data-structures/base/iterable-element-base.ts +250 -0
  69. package/src/data-structures/base/{iterable-base.ts → iterable-entry-base.ts} +22 -213
  70. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +146 -97
  71. package/src/data-structures/binary-tree/avl-tree.ts +97 -70
  72. package/src/data-structures/binary-tree/binary-tree.ts +591 -455
  73. package/src/data-structures/binary-tree/bst.ts +266 -293
  74. package/src/data-structures/binary-tree/rb-tree.ts +124 -104
  75. package/src/data-structures/binary-tree/tree-multi-map.ts +128 -103
  76. package/src/data-structures/graph/abstract-graph.ts +10 -10
  77. package/src/data-structures/graph/directed-graph.ts +2 -1
  78. package/src/data-structures/hash/hash-map.ts +46 -53
  79. package/src/data-structures/heap/heap.ts +71 -152
  80. package/src/data-structures/heap/max-heap.ts +88 -13
  81. package/src/data-structures/heap/min-heap.ts +78 -15
  82. package/src/data-structures/linked-list/doubly-linked-list.ts +32 -32
  83. package/src/data-structures/linked-list/singly-linked-list.ts +37 -29
  84. package/src/data-structures/priority-queue/max-priority-queue.ts +94 -13
  85. package/src/data-structures/priority-queue/min-priority-queue.ts +84 -15
  86. package/src/data-structures/priority-queue/priority-queue.ts +81 -4
  87. package/src/data-structures/queue/deque.ts +52 -27
  88. package/src/data-structures/queue/queue.ts +23 -37
  89. package/src/data-structures/stack/stack.ts +31 -26
  90. package/src/data-structures/trie/trie.ts +35 -20
  91. package/src/interfaces/binary-tree.ts +10 -10
  92. package/src/types/common.ts +2 -25
  93. package/src/types/data-structures/base/base.ts +14 -6
  94. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +3 -4
  95. package/src/types/data-structures/binary-tree/avl-tree.ts +3 -4
  96. package/src/types/data-structures/binary-tree/binary-tree.ts +26 -6
  97. package/src/types/data-structures/binary-tree/bst.ts +11 -5
  98. package/src/types/data-structures/binary-tree/rb-tree.ts +3 -4
  99. package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -4
  100. package/src/types/data-structures/heap/heap.ts +4 -1
  101. package/src/types/data-structures/linked-list/doubly-linked-list.ts +3 -1
  102. package/src/types/data-structures/linked-list/singly-linked-list.ts +3 -1
  103. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
  104. package/src/types/data-structures/queue/deque.ts +6 -1
  105. package/src/types/data-structures/queue/queue.ts +3 -1
  106. package/src/types/data-structures/stack/stack.ts +3 -1
  107. package/src/types/data-structures/trie/trie.ts +3 -1
  108. package/src/utils/utils.ts +3 -3
@@ -18,21 +18,24 @@ import { IterableElementBase } from '../base';
18
18
  * 5. Managing Dynamic Data Sets: Heaps effectively manage dynamic data sets, especially when frequent access to the largest or smallest elements is required.
19
19
  * 6. Non-linear Search: While a heap allows rapid access to its largest or smallest element, it is less efficient for other operations, such as searching for a specific element, as it is not designed for these tasks.
20
20
  * 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
21
- * 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
21
+ * 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prime's minimum-spanning tree algorithm, which use heaps to improve performance.
22
22
  */
23
- export class Heap<E = any> extends IterableElementBase<E> {
23
+ export class Heap<E = any, R = any> extends IterableElementBase<E, R, Heap<E, R>> {
24
24
  /**
25
25
  * The constructor initializes a heap data structure with optional elements and options.
26
26
  * @param elements - The `elements` parameter is an iterable object that contains the initial
27
- * elements to be added to the heap. It is an optional parameter and if not provided, the heap will
27
+ * elements to be added to the heap.
28
+ * It is an optional parameter, and if not provided, the heap will
28
29
  * be initialized as empty.
29
30
  * @param [options] - The `options` parameter is an optional object that can contain additional
30
- * configuration options for the heap. In this case, it is used to specify a custom comparator
31
- * function for comparing elements in the heap. The comparator function is used to determine the
31
+ * configuration options for the heap.
32
+ * In this case, it is used to specify a custom comparator
33
+ * function for comparing elements in the heap.
34
+ * The comparator function is used to determine the
32
35
  * order of elements in the heap.
33
36
  */
34
- constructor(elements: Iterable<E> = [], options?: HeapOptions<E>) {
35
- super();
37
+ constructor(elements: Iterable<E> | Iterable<R> = [], options?: HeapOptions<E, R>) {
38
+ super(options);
36
39
 
37
40
  if (options) {
38
41
  const { comparator } = options;
@@ -41,32 +44,17 @@ export class Heap<E = any> extends IterableElementBase<E> {
41
44
 
42
45
  if (elements) {
43
46
  for (const el of elements) {
44
- this.add(el);
47
+ if (this.toElementFn) this.add(this.toElementFn(el as R));
48
+ else this.add(el as E);
45
49
  }
46
50
  }
47
51
  }
48
52
 
49
- protected _comparator = (a: E, b: E) => {
50
- if (!(typeof a === 'number' && typeof b === 'number')) {
51
- throw new Error('The a, b params of compare function must be number');
52
- } else {
53
- return a - b;
54
- }
55
- };
56
-
57
- /**
58
- * The function returns the value of the _comparator property.
59
- * @returns The `_comparator` property is being returned.
60
- */
61
- get comparator() {
62
- return this._comparator;
63
- }
64
-
65
53
  protected _elements: E[] = [];
66
54
 
67
55
  /**
68
56
  * The function returns an array of elements.
69
- * @returns The elements array is being returned.
57
+ * @returns The element array is being returned.
70
58
  */
71
59
  get elements(): E[] {
72
60
  return this._elements;
@@ -93,16 +81,10 @@ export class Heap<E = any> extends IterableElementBase<E> {
93
81
  * @param elements
94
82
  * @param options
95
83
  */
96
- static heapify<E>(elements: Iterable<E>, options: HeapOptions<E>): Heap<E> {
84
+ static heapify<E = any, R = any>(elements: Iterable<E>, options: HeapOptions<E, R>): Heap<E> {
97
85
  return new Heap<E>(elements, options);
98
86
  }
99
87
 
100
- /**
101
- * Time Complexity: O(log n)
102
- * Space Complexity: O(1)
103
- * where n is the number of elements in the heap.
104
- */
105
-
106
88
  /**
107
89
  * Time Complexity: O(log n)
108
90
  * Space Complexity: O(1)
@@ -115,17 +97,11 @@ export class Heap<E = any> extends IterableElementBase<E> {
115
97
  return this._bubbleUp(this.elements.length - 1);
116
98
  }
117
99
 
118
- /**
119
- * Time Complexity: O(log n)
120
- * Space Complexity: O(1)
121
- * where n is the number of elements in the heap.
122
- */
123
-
124
100
  /**
125
101
  * Time Complexity: O(log n)
126
102
  * Space Complexity: O(1)
127
103
  *
128
- * Remove and return the top element (smallest or largest element) from the heap.
104
+ * Remove and return the top element (the smallest or largest element) from the heap.
129
105
  * @returns The top element or undefined if the heap is empty.
130
106
  */
131
107
  poll(): E | undefined {
@@ -139,11 +115,6 @@ export class Heap<E = any> extends IterableElementBase<E> {
139
115
  return value;
140
116
  }
141
117
 
142
- /**
143
- * Time Complexity: O(1)
144
- * Space Complexity: O(1)
145
- */
146
-
147
118
  /**
148
119
  * Time Complexity: O(1)
149
120
  * Space Complexity: O(1)
@@ -170,11 +141,6 @@ export class Heap<E = any> extends IterableElementBase<E> {
170
141
  this._elements = [];
171
142
  }
172
143
 
173
- /**
174
- * Time Complexity: O(n)
175
- * Space Complexity: O(n)
176
- */
177
-
178
144
  /**
179
145
  * Time Complexity: O(n)
180
146
  * Space Complexity: O(n)
@@ -187,11 +153,6 @@ export class Heap<E = any> extends IterableElementBase<E> {
187
153
  return this.fix();
188
154
  }
189
155
 
190
- /**
191
- * Time Complexity: O(n)
192
- * Space Complexity: O(1)
193
- */
194
-
195
156
  /**
196
157
  * Time Complexity: O(n)
197
158
  * Space Complexity: O(1)
@@ -205,13 +166,7 @@ export class Heap<E = any> extends IterableElementBase<E> {
205
166
  }
206
167
 
207
168
  /**
208
- * Time Complexity: O(n)
209
- * Space Complexity: O(1)
210
- * The worst-case O(n) This is because, in the worst case, the element to be deleted is located at the end of the heap (not the root), and after deletion, we may need to reorganize the elements by performing a sinkDown operation.
211
- */
212
-
213
- /**
214
- * Time Complexity: O(n)
169
+ * Time Complexity: O(n)
215
170
  * Space Complexity: O(1)
216
171
  *
217
172
  * The `delete` function removes an element from an array-like data structure, maintaining the order
@@ -236,12 +191,6 @@ export class Heap<E = any> extends IterableElementBase<E> {
236
191
  return true;
237
192
  }
238
193
 
239
- /**
240
- * Time Complexity: O(n)
241
- * Space Complexity: O(log n)
242
- * where log n is the height of the heap.
243
- */
244
-
245
194
  /**
246
195
  * Time Complexity: O(n)
247
196
  * Space Complexity: O(log n)
@@ -279,11 +228,6 @@ export class Heap<E = any> extends IterableElementBase<E> {
279
228
  return result;
280
229
  }
281
230
 
282
- /**
283
- * Time Complexity: O(n)
284
- * Space Complexity: O(n)
285
- */
286
-
287
231
  /**
288
232
  * Time Complexity: O(n)
289
233
  * Space Complexity: O(n)
@@ -295,11 +239,6 @@ export class Heap<E = any> extends IterableElementBase<E> {
295
239
  return [...this.elements];
296
240
  }
297
241
 
298
- /**
299
- * Time Complexity: O(n)
300
- * Space Complexity: O(n)
301
- */
302
-
303
242
  /**
304
243
  * Time Complexity: O(n)
305
244
  * Space Complexity: O(n)
@@ -307,17 +246,10 @@ export class Heap<E = any> extends IterableElementBase<E> {
307
246
  * Clone the heap, creating a new heap with the same elements.
308
247
  * @returns A new Heap instance containing the same elements.
309
248
  */
310
- clone(): Heap<E> {
311
- const clonedHeap = new Heap<E>([], { comparator: this.comparator });
312
- clonedHeap._elements = [...this.elements];
313
- return clonedHeap;
249
+ clone(): Heap<E, R> {
250
+ return new Heap<E, R>(this, { comparator: this.comparator, toElementFn: this.toElementFn });
314
251
  }
315
252
 
316
- /**
317
- * Time Complexity: O(n log n)
318
- * Space Complexity: O(n)
319
- */
320
-
321
253
  /**
322
254
  * Time Complexity: O(n log n)
323
255
  * Space Complexity: O(n)
@@ -327,19 +259,14 @@ export class Heap<E = any> extends IterableElementBase<E> {
327
259
  */
328
260
  sort(): E[] {
329
261
  const visitedNode: E[] = [];
330
- const cloned = this.clone();
262
+ const cloned = new Heap<E, R>(this, { comparator: this.comparator });
331
263
  while (cloned.size !== 0) {
332
264
  const top = cloned.poll();
333
- if (top) visitedNode.push(top);
265
+ if (top !== undefined) visitedNode.push(top);
334
266
  }
335
267
  return visitedNode;
336
268
  }
337
269
 
338
- /**
339
- * Time Complexity: O(n log n)
340
- * Space Complexity: O(n)
341
- */
342
-
343
270
  /**
344
271
  * Time Complexity: O(n log n)
345
272
  * Space Complexity: O(n)
@@ -352,11 +279,6 @@ export class Heap<E = any> extends IterableElementBase<E> {
352
279
  return results;
353
280
  }
354
281
 
355
- /**
356
- * Time Complexity: O(n)
357
- * Space Complexity: O(n)
358
- */
359
-
360
282
  /**
361
283
  * Time Complexity: O(n)
362
284
  * Space Complexity: O(n)
@@ -373,8 +295,8 @@ export class Heap<E = any> extends IterableElementBase<E> {
373
295
  * @returns The `filter` method is returning a new `Heap` object that contains the elements that pass
374
296
  * the filter condition specified by the `callback` function.
375
297
  */
376
- filter(callback: ElementCallback<E, boolean>, thisArg?: any): Heap<E> {
377
- const filteredList = new Heap<E>();
298
+ filter(callback: ElementCallback<E, R, boolean, Heap<E, R>>, thisArg?: any): Heap<E, R> {
299
+ const filteredList = new Heap<E, R>([], { toElementFn: this.toElementFn, comparator: this.comparator });
378
300
  let index = 0;
379
301
  for (const current of this) {
380
302
  if (callback.call(thisArg, current, index, this)) {
@@ -386,32 +308,33 @@ export class Heap<E = any> extends IterableElementBase<E> {
386
308
  }
387
309
 
388
310
  /**
389
- * Time Complexity: O(n)
390
- * Space Complexity: O(n)
391
- */
392
-
393
- /**
394
- * Time Complexity: O(n)
311
+ * Time Complexity: O(n log n)
395
312
  * Space Complexity: O(n)
396
313
  *
397
314
  * The `map` function creates a new heap by applying a callback function to each element of the
398
315
  * original heap.
399
- * @param callback - The callback parameter is a function that will be called for each element in the
400
- * original heap. It takes three arguments: the current element, the index of the current element,
401
- * and the original heap itself. The callback function should return a value of type T, which will be
402
- * added to the mapped heap.
403
- * @param comparator - The `comparator` parameter is a function that is used to compare elements in
404
- * the heap. It takes two arguments, `a` and `b`, and returns a negative number if `a` is less than
405
- * `b`, a positive number if `a` is greater than `b`, or
316
+ * @param callback - The `callback` parameter is a function that will be called for each element in
317
+ * the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
318
+ * element), and `this` (the heap itself). The callback function should return a value of
319
+ * @param comparator - The `comparator` parameter is a function that defines the order of the
320
+ * elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
321
+ * if `a` should be placed before `b`, a positive number if `a` should be placed after
322
+ * @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
323
+ * element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
324
+ * returns a value of type `T`. This function is used to transform the elements of the original
406
325
  * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
407
- * specify the value of `this` within the callback function. It is used when you want to bind a
408
- * specific object as the context for the callback function. If `thisArg` is not provided,
409
- * `undefined` is used as
410
- * @returns a new instance of the Heap class, which is created using the mapped elements from the
411
- * original Heap.
412
- */
413
- map<T>(callback: ElementCallback<E, T>, comparator: Comparator<T>, thisArg?: any): Heap<T> {
414
- const mappedHeap: Heap<T> = new Heap<T>([], { comparator: comparator });
326
+ * specify the value of `this` within the callback function. It is used to set the context or scope
327
+ * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
328
+ * value of
329
+ * @returns a new instance of the `Heap` class with the mapped elements.
330
+ */
331
+ map<EM, RM>(
332
+ callback: ElementCallback<E, R, EM, Heap<E, R>>,
333
+ comparator: Comparator<EM>,
334
+ toElementFn?: (rawElement: RM) => EM,
335
+ thisArg?: any
336
+ ): Heap<EM, RM> {
337
+ const mappedHeap: Heap<EM, RM> = new Heap<EM, RM>([], { comparator, toElementFn });
415
338
  let index = 0;
416
339
  for (const el of this) {
417
340
  mappedHeap.add(callback.call(thisArg, el, index, this));
@@ -420,6 +343,27 @@ export class Heap<E = any> extends IterableElementBase<E> {
420
343
  return mappedHeap;
421
344
  }
422
345
 
346
+ protected _DEFAULT_COMPARATOR = (a: E, b: E): number => {
347
+ if (typeof a === 'object' || typeof b === 'object') {
348
+ throw TypeError(
349
+ `When comparing object types, a custom comparator must be defined in the constructor's options parameter.`
350
+ );
351
+ }
352
+ if (a > b) return 1;
353
+ if (a < b) return -1;
354
+ return 0;
355
+ };
356
+
357
+ protected _comparator: Comparator<E> = this._DEFAULT_COMPARATOR;
358
+
359
+ /**
360
+ * The function returns the value of the _comparator property.
361
+ * @returns The `_comparator` property is being returned.
362
+ */
363
+ get comparator() {
364
+ return this._comparator;
365
+ }
366
+
423
367
  /**
424
368
  * The function `_getIterator` returns an iterable iterator for the elements in the class.
425
369
  */
@@ -429,11 +373,6 @@ export class Heap<E = any> extends IterableElementBase<E> {
429
373
  }
430
374
  }
431
375
 
432
- /**
433
- * Time Complexity: O(log n)
434
- * Space Complexity: O(1)
435
- */
436
-
437
376
  /**
438
377
  * Time Complexity: O(log n)
439
378
  * Space Complexity: O(1)
@@ -454,11 +393,6 @@ export class Heap<E = any> extends IterableElementBase<E> {
454
393
  return true;
455
394
  }
456
395
 
457
- /**
458
- * Time Complexity: O(log n)
459
- * Space Complexity: O(1)
460
- */
461
-
462
396
  /**
463
397
  * Time Complexity: O(log n)
464
398
  * Space Complexity: O(1)
@@ -580,11 +514,6 @@ export class FibonacciHeap<E> {
580
514
  this._size = 0;
581
515
  }
582
516
 
583
- /**
584
- * Time Complexity: O(1)
585
- * Space Complexity: O(1)
586
- */
587
-
588
517
  /**
589
518
  * Time Complexity: O(1)
590
519
  * Space Complexity: O(1)
@@ -597,11 +526,6 @@ export class FibonacciHeap<E> {
597
526
  return this.push(element);
598
527
  }
599
528
 
600
- /**
601
- * Time Complexity: O(1)
602
- * Space Complexity: O(1)
603
- */
604
-
605
529
  /**
606
530
  * Time Complexity: O(1)
607
531
  * Space Complexity: O(1)
@@ -624,11 +548,6 @@ export class FibonacciHeap<E> {
624
548
  return this;
625
549
  }
626
550
 
627
- /**
628
- * Time Complexity: O(1)
629
- * Space Complexity: O(1)
630
- */
631
-
632
551
  /**
633
552
  * Time Complexity: O(1)
634
553
  * Space Complexity: O(1)
@@ -702,7 +621,7 @@ export class FibonacciHeap<E> {
702
621
  * Time Complexity: O(log n)
703
622
  * Space Complexity: O(1)
704
623
  *
705
- * Remove and return the top element (smallest or largest element) from the heap.
624
+ * Remove and return the top element (the smallest or largest element) from the heap.
706
625
  * @returns The top element or undefined if the heap is empty.
707
626
  */
708
627
  poll(): E | undefined {
@@ -718,7 +637,7 @@ export class FibonacciHeap<E> {
718
637
  * Time Complexity: O(log n)
719
638
  * Space Complexity: O(1)
720
639
  *
721
- * Remove and return the top element (smallest or largest element) from the heap.
640
+ * Remove and return the top element (the smallest or largest element) from the heap.
722
641
  * @returns The top element or undefined if the heap is empty.
723
642
  */
724
643
  pop(): E | undefined {
@@ -844,8 +763,8 @@ export class FibonacciHeap<E> {
844
763
  /**
845
764
  * Time Complexity: O(1)
846
765
  * Space Complexity: O(1)
847
- *.
848
- * Remove and return the top element (smallest or largest element) from the heap.
766
+ *
767
+ * Remove and return the top element (the smallest or largest element) from the heap.
849
768
  * @param node - The node to be removed.
850
769
  * @protected
851
770
  */
@@ -864,7 +783,7 @@ export class FibonacciHeap<E> {
864
783
  * Time Complexity: O(1)
865
784
  * Space Complexity: O(1)
866
785
  *
867
- * Remove and return the top element (smallest or largest element) from the heap.
786
+ * Remove and return the top element (the smallest or largest element) from the heap.
868
787
  * @param y
869
788
  * @param x
870
789
  * @protected
@@ -887,7 +806,7 @@ export class FibonacciHeap<E> {
887
806
  * Time Complexity: O(n log n)
888
807
  * Space Complexity: O(n)
889
808
  *
890
- * Remove and return the top element (smallest or largest element) from the heap.
809
+ * Remove and return the top element (the smallest or largest element) from the heap.
891
810
  * @protected
892
811
  */
893
812
  protected _consolidate(): void {
@@ -5,7 +5,7 @@
5
5
  * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { HeapOptions } from '../../types';
8
+ import type { Comparator, ElementCallback, HeapOptions } from '../../types';
9
9
  import { Heap } from './heap';
10
10
 
11
11
  /**
@@ -16,21 +16,96 @@ import { Heap } from './heap';
16
16
  * 5. Managing Dynamic Data Sets: Heaps effectively manage dynamic data sets, especially when frequent access to the largest or smallest elements is required.
17
17
  * 6. Non-linear Search: While a heap allows rapid access to its largest or smallest element, it is less efficient for other operations, such as searching for a specific element, as it is not designed for these tasks.
18
18
  * 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
19
- * 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
19
+ * 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum-spanning tree algorithm, which use heaps to improve performance.
20
20
  */
21
- export class MaxHeap<E = any> extends Heap<E> {
22
- constructor(
23
- elements: Iterable<E> = [],
24
- options: HeapOptions<E> = {
25
- comparator: (a: E, b: E) => {
26
- if (!(typeof a === 'number' && typeof b === 'number')) {
27
- throw new Error('The a, b params of compare function must be number');
28
- } else {
29
- return b - a;
21
+ export class MaxHeap<E = any, R = any> extends Heap<E, R> {
22
+ constructor(elements: Iterable<E> | Iterable<R> = [], options?: HeapOptions<E, R>) {
23
+ super(elements, {
24
+ comparator: (a: E, b: E): number => {
25
+ if (typeof a === 'object' || typeof b === 'object') {
26
+ throw TypeError(
27
+ `When comparing object types, a custom comparator must be defined in the constructor's options parameter.`
28
+ );
30
29
  }
30
+ if (a < b) return 1;
31
+ if (a > b) return -1;
32
+ return 0;
33
+ },
34
+ ...options
35
+ });
36
+ }
37
+
38
+ /**
39
+ * The `clone` function returns a new instance of the `MaxHeap` class with the same properties as the
40
+ * current instance.
41
+ * @returns The `clone()` method is returning a new instance of the `MaxHeap` class with the same
42
+ * properties as the current instance.
43
+ */
44
+ override clone(): MaxHeap<E, R> {
45
+ return new MaxHeap<E, R>(this, { comparator: this.comparator, toElementFn: this.toElementFn });
46
+ }
47
+
48
+ /**
49
+ * Time Complexity: O(n)
50
+ * Space Complexity: O(n)
51
+ *
52
+ * The `filter` function creates a new MaxHeap object containing elements that pass a given callback
53
+ * function.
54
+ * @param callback - The `callback` parameter is a function that will be called for each element in
55
+ * the heap. It takes three arguments: the current element, the index of the current element, and the
56
+ * heap itself. The callback function should return a boolean value indicating whether the current
57
+ * element should be included in the filtered list
58
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
59
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
60
+ * passed as the `this` value to the `callback` function. If `thisArg` is
61
+ * @returns The `filter` method is returning a new `MaxHeap` object that contains the elements that pass
62
+ * the filter condition specified by the `callback` function.
63
+ */
64
+ override filter(callback: ElementCallback<E, R, boolean, MaxHeap<E, R>>, thisArg?: any): MaxHeap<E, R> {
65
+ const filteredList = new MaxHeap<E, R>([], { toElementFn: this.toElementFn, comparator: this.comparator });
66
+ let index = 0;
67
+ for (const current of this) {
68
+ if (callback.call(thisArg, current, index, this)) {
69
+ filteredList.add(current);
31
70
  }
71
+ index++;
72
+ }
73
+ return filteredList;
74
+ }
75
+
76
+ /**
77
+ * Time Complexity: O(n log n)
78
+ * Space Complexity: O(n)
79
+ *
80
+ * The `map` function creates a new heap by applying a callback function to each element of the
81
+ * original heap.
82
+ * @param callback - The `callback` parameter is a function that will be called for each element in
83
+ * the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
84
+ * element), and `this` (the heap itself). The callback function should return a value of
85
+ * @param comparator - The `comparator` parameter is a function that defines the order of the
86
+ * elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
87
+ * if `a` should be placed before `b`, a positive number if `a` should be placed after
88
+ * @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
89
+ * element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
90
+ * returns a value of type `T`. This function is used to transform the elements of the original
91
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
92
+ * specify the value of `this` within the callback function. It is used to set the context or scope
93
+ * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
94
+ * value of
95
+ * @returns a new instance of the `MaxHeap` class with the mapped elements.
96
+ */
97
+ override map<EM, RM>(
98
+ callback: ElementCallback<E, R, EM, MaxHeap<E, R>>,
99
+ comparator: Comparator<EM>,
100
+ toElementFn?: (rawElement: RM) => EM,
101
+ thisArg?: any
102
+ ): MaxHeap<EM, RM> {
103
+ const mappedHeap: MaxHeap<EM, RM> = new MaxHeap<EM, RM>([], { comparator, toElementFn });
104
+ let index = 0;
105
+ for (const el of this) {
106
+ mappedHeap.add(callback.call(thisArg, el, index, this));
107
+ index++;
32
108
  }
33
- ) {
34
- super(elements, options);
109
+ return mappedHeap;
35
110
  }
36
111
  }
@@ -5,32 +5,95 @@
5
5
  * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { HeapOptions } from '../../types';
8
+ import type { Comparator, ElementCallback, HeapOptions } from '../../types';
9
9
  import { Heap } from './heap';
10
10
 
11
11
  /**
12
12
  * 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.
13
- * 2. Heap Properties: The value of each parent node is less than or equal to the value of its children.
13
+ * 2. MinHeap Properties: The value of each parent node is less than or equal to the value of its children.
14
14
  * 3. Root Node Access: In a heap, the largest element (in a max heap) or the smallest element (in a min heap) is always at the root of the tree.
15
15
  * 4. Efficient Insertion and Deletion: Due to its structure, a heap allows for insertion and deletion operations in logarithmic time (O(log n)).
16
16
  * 5. Managing Dynamic Data Sets: Heaps effectively manage dynamic data sets, especially when frequent access to the largest or smallest elements is required.
17
17
  * 6. Non-linear Search: While a heap allows rapid access to its largest or smallest element, it is less efficient for other operations, such as searching for a specific element, as it is not designed for these tasks.
18
- * 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
18
+ * 7. Efficient Sorting Algorithms: For example, heap sort. MinHeap sort uses the properties of a heap to sort elements.
19
19
  * 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
20
20
  */
21
- export class MinHeap<E = any> extends Heap<E> {
22
- constructor(
23
- elements: Iterable<E> = [],
24
- options: HeapOptions<E> = {
25
- comparator: (a: E, b: E) => {
26
- if (!(typeof a === 'number' && typeof b === 'number')) {
27
- throw new Error('The a, b params of compare function must be number');
28
- } else {
29
- return a - b;
30
- }
21
+ export class MinHeap<E = any, R = any> extends Heap<E, R> {
22
+ constructor(elements: Iterable<E> | Iterable<R> = [], options?: HeapOptions<E, R>) {
23
+ super(elements, options);
24
+ }
25
+
26
+ /**
27
+ * The `clone` function returns a new instance of the `MinHeap` class with the same comparator and
28
+ * toElementFn as the original instance.
29
+ * @returns The `clone()` method is returning a new instance of the `MinHeap` class with the same
30
+ * properties as the current instance.
31
+ */
32
+ override clone(): MinHeap<E, R> {
33
+ return new MinHeap<E, R>(this, { comparator: this.comparator, toElementFn: this.toElementFn });
34
+ }
35
+
36
+ /**
37
+ * Time Complexity: O(n)
38
+ * Space Complexity: O(n)
39
+ *
40
+ * The `filter` function creates a new MinHeap object containing elements that pass a given callback
41
+ * function.
42
+ * @param callback - The `callback` parameter is a function that will be called for each element in
43
+ * the heap. It takes three arguments: the current element, the index of the current element, and the
44
+ * heap itself. The callback function should return a boolean value indicating whether the current
45
+ * element should be included in the filtered list
46
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
47
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
48
+ * passed as the `this` value to the `callback` function. If `thisArg` is
49
+ * @returns The `filter` method is returning a new `MinHeap` object that contains the elements that pass
50
+ * the filter condition specified by the `callback` function.
51
+ */
52
+ override filter(callback: ElementCallback<E, R, boolean, MinHeap<E, R>>, thisArg?: any): MinHeap<E, R> {
53
+ const filteredList = new MinHeap<E, R>([], { toElementFn: this.toElementFn, comparator: this.comparator });
54
+ let index = 0;
55
+ for (const current of this) {
56
+ if (callback.call(thisArg, current, index, this)) {
57
+ filteredList.add(current);
31
58
  }
59
+ index++;
32
60
  }
33
- ) {
34
- super(elements, options);
61
+ return filteredList;
62
+ }
63
+
64
+ /**
65
+ * Time Complexity: O(n log n)
66
+ * Space Complexity: O(n)
67
+ *
68
+ * The `map` function creates a new heap by applying a callback function to each element of the
69
+ * original heap.
70
+ * @param callback - The `callback` parameter is a function that will be called for each element in
71
+ * the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
72
+ * element), and `this` (the heap itself). The callback function should return a value of
73
+ * @param comparator - The `comparator` parameter is a function that defines the order of the
74
+ * elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
75
+ * if `a` should be placed before `b`, a positive number if `a` should be placed after
76
+ * @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
77
+ * element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
78
+ * returns a value of type `T`. This function is used to transform the elements of the original
79
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
80
+ * specify the value of `this` within the callback function. It is used to set the context or scope
81
+ * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
82
+ * value of
83
+ * @returns a new instance of the `MinHeap` class with the mapped elements.
84
+ */
85
+ override map<EM, RM>(
86
+ callback: ElementCallback<E, R, EM, MinHeap<E, R>>,
87
+ comparator: Comparator<EM>,
88
+ toElementFn?: (rawElement: RM) => EM,
89
+ thisArg?: any
90
+ ): MinHeap<EM, RM> {
91
+ const mappedHeap: MinHeap<EM, RM> = new MinHeap<EM, RM>([], { comparator, toElementFn });
92
+ let index = 0;
93
+ for (const el of this) {
94
+ mappedHeap.add(callback.call(thisArg, el, index, this));
95
+ index++;
96
+ }
97
+ return mappedHeap;
35
98
  }
36
99
  }