undirected-graph-typed 1.51.9 → 1.52.2

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 (106) 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 +13 -13
  8. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +6 -6
  9. package/dist/data-structures/binary-tree/avl-tree.d.ts +13 -13
  10. package/dist/data-structures/binary-tree/avl-tree.js +6 -6
  11. package/dist/data-structures/binary-tree/binary-tree.d.ts +99 -99
  12. package/dist/data-structures/binary-tree/binary-tree.js +56 -54
  13. package/dist/data-structures/binary-tree/bst.d.ts +37 -45
  14. package/dist/data-structures/binary-tree/bst.js +19 -27
  15. package/dist/data-structures/binary-tree/rb-tree.d.ts +10 -10
  16. package/dist/data-structures/binary-tree/rb-tree.js +6 -6
  17. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +12 -12
  18. package/dist/data-structures/binary-tree/tree-multi-map.js +5 -5
  19. package/dist/data-structures/graph/directed-graph.js +2 -1
  20. package/dist/data-structures/hash/hash-map.d.ts +2 -2
  21. package/dist/data-structures/heap/heap.d.ts +43 -114
  22. package/dist/data-structures/heap/heap.js +59 -127
  23. package/dist/data-structures/heap/max-heap.d.ts +50 -4
  24. package/dist/data-structures/heap/max-heap.js +76 -10
  25. package/dist/data-structures/heap/min-heap.d.ts +51 -5
  26. package/dist/data-structures/heap/min-heap.js +68 -11
  27. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +22 -28
  28. package/dist/data-structures/linked-list/doubly-linked-list.js +26 -28
  29. package/dist/data-structures/linked-list/singly-linked-list.d.ts +22 -25
  30. package/dist/data-structures/linked-list/singly-linked-list.js +29 -26
  31. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +50 -4
  32. package/dist/data-structures/priority-queue/max-priority-queue.js +79 -10
  33. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +51 -5
  34. package/dist/data-structures/priority-queue/min-priority-queue.js +71 -11
  35. package/dist/data-structures/priority-queue/priority-queue.d.ts +50 -4
  36. package/dist/data-structures/priority-queue/priority-queue.js +70 -1
  37. package/dist/data-structures/queue/deque.d.ts +27 -18
  38. package/dist/data-structures/queue/deque.js +43 -21
  39. package/dist/data-structures/queue/queue.d.ts +26 -29
  40. package/dist/data-structures/queue/queue.js +47 -38
  41. package/dist/data-structures/stack/stack.d.ts +17 -22
  42. package/dist/data-structures/stack/stack.js +25 -24
  43. package/dist/data-structures/trie/trie.d.ts +18 -13
  44. package/dist/data-structures/trie/trie.js +26 -15
  45. package/dist/interfaces/binary-tree.d.ts +4 -4
  46. package/dist/types/common.d.ts +1 -22
  47. package/dist/types/data-structures/base/base.d.ts +5 -2
  48. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -3
  49. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -3
  50. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +20 -4
  51. package/dist/types/data-structures/binary-tree/bst.d.ts +5 -3
  52. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -3
  53. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -3
  54. package/dist/types/data-structures/heap/heap.d.ts +3 -2
  55. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
  56. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +2 -1
  57. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
  58. package/dist/types/data-structures/queue/deque.d.ts +4 -2
  59. package/dist/types/data-structures/queue/queue.d.ts +4 -1
  60. package/dist/types/data-structures/stack/stack.d.ts +2 -1
  61. package/dist/types/data-structures/trie/trie.d.ts +3 -2
  62. package/package.json +2 -2
  63. package/src/data-structures/base/index.ts +2 -1
  64. package/src/data-structures/base/iterable-element-base.ts +250 -0
  65. package/src/data-structures/base/{iterable-base.ts → iterable-entry-base.ts} +26 -217
  66. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +26 -26
  67. package/src/data-structures/binary-tree/avl-tree.ts +21 -21
  68. package/src/data-structures/binary-tree/binary-tree.ts +166 -161
  69. package/src/data-structures/binary-tree/bst.ts +61 -68
  70. package/src/data-structures/binary-tree/rb-tree.ts +19 -19
  71. package/src/data-structures/binary-tree/tree-multi-map.ts +19 -19
  72. package/src/data-structures/graph/abstract-graph.ts +15 -14
  73. package/src/data-structures/graph/directed-graph.ts +9 -7
  74. package/src/data-structures/graph/undirected-graph.ts +7 -6
  75. package/src/data-structures/hash/hash-map.ts +8 -8
  76. package/src/data-structures/heap/heap.ts +72 -153
  77. package/src/data-structures/heap/max-heap.ts +88 -13
  78. package/src/data-structures/heap/min-heap.ts +78 -15
  79. package/src/data-structures/linked-list/doubly-linked-list.ts +33 -33
  80. package/src/data-structures/linked-list/singly-linked-list.ts +38 -30
  81. package/src/data-structures/priority-queue/max-priority-queue.ts +94 -13
  82. package/src/data-structures/priority-queue/min-priority-queue.ts +84 -15
  83. package/src/data-structures/priority-queue/priority-queue.ts +81 -4
  84. package/src/data-structures/queue/deque.ts +53 -28
  85. package/src/data-structures/queue/queue.ts +61 -44
  86. package/src/data-structures/stack/stack.ts +32 -27
  87. package/src/data-structures/trie/trie.ts +34 -19
  88. package/src/interfaces/binary-tree.ts +4 -5
  89. package/src/types/common.ts +2 -24
  90. package/src/types/data-structures/base/base.ts +14 -6
  91. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +2 -3
  92. package/src/types/data-structures/binary-tree/avl-tree.ts +2 -3
  93. package/src/types/data-structures/binary-tree/binary-tree.ts +24 -5
  94. package/src/types/data-structures/binary-tree/bst.ts +9 -3
  95. package/src/types/data-structures/binary-tree/rb-tree.ts +2 -3
  96. package/src/types/data-structures/binary-tree/tree-multi-map.ts +2 -3
  97. package/src/types/data-structures/graph/abstract-graph.ts +8 -8
  98. package/src/types/data-structures/heap/heap.ts +4 -1
  99. package/src/types/data-structures/linked-list/doubly-linked-list.ts +3 -1
  100. package/src/types/data-structures/linked-list/singly-linked-list.ts +3 -1
  101. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
  102. package/src/types/data-structures/queue/deque.ts +6 -1
  103. package/src/types/data-structures/queue/queue.ts +5 -1
  104. package/src/types/data-structures/stack/stack.ts +3 -1
  105. package/src/types/data-structures/trie/trie.ts +3 -1
  106. package/src/types/utils/utils.ts +4 -4
@@ -46,13 +46,14 @@ export class DirectedEdge<E = any> extends AbstractEdge<E> {
46
46
  }
47
47
 
48
48
  export class DirectedGraph<
49
- V = any,
50
- E = any,
51
- VO extends DirectedVertex<V> = DirectedVertex<V>,
52
- EO extends DirectedEdge<E> = DirectedEdge<E>
53
- >
49
+ V = any,
50
+ E = any,
51
+ VO extends DirectedVertex<V> = DirectedVertex<V>,
52
+ EO extends DirectedEdge<E> = DirectedEdge<E>
53
+ >
54
54
  extends AbstractGraph<V, E, VO, EO>
55
- implements IGraph<V, E, VO, EO> {
55
+ implements IGraph<V, E, VO, EO>
56
+ {
56
57
  /**
57
58
  * The constructor function initializes an instance of a class.
58
59
  */
@@ -261,7 +262,8 @@ export class DirectedGraph<
261
262
  if (vertex) {
262
263
  const neighbors = this.getNeighbors(vertex);
263
264
  for (const neighbor of neighbors) {
264
- this._inEdgeMap.delete(neighbor);
265
+ // this._inEdgeMap.delete(neighbor);
266
+ this.deleteEdgeSrcToDest(vertex, neighbor);
265
267
  }
266
268
  this._outEdgeMap.delete(vertex);
267
269
  this._inEdgeMap.delete(vertex);
@@ -43,13 +43,14 @@ export class UndirectedEdge<E = number> extends AbstractEdge<E> {
43
43
  }
44
44
 
45
45
  export class UndirectedGraph<
46
- V = any,
47
- E = any,
48
- VO extends UndirectedVertex<V> = UndirectedVertex<V>,
49
- EO extends UndirectedEdge<E> = UndirectedEdge<E>
50
- >
46
+ V = any,
47
+ E = any,
48
+ VO extends UndirectedVertex<V> = UndirectedVertex<V>,
49
+ EO extends UndirectedEdge<E> = UndirectedEdge<E>
50
+ >
51
51
  extends AbstractGraph<V, E, VO, EO>
52
- implements IGraph<V, E, VO, EO> {
52
+ implements IGraph<V, E, VO, EO>
53
+ {
53
54
  /**
54
55
  * The constructor initializes a new Map object to store edgeMap.
55
56
  */
@@ -270,8 +270,8 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
270
270
  * @returns The `map` method is returning a new `HashMap` object with the transformed values based on
271
271
  * the provided callback function.
272
272
  */
273
- map<U>(callbackfn: EntryCallback<K, V, U>, thisArg?: any): HashMap<K, U> {
274
- const resultMap = new HashMap<K, U>();
273
+ map<VM>(callbackfn: EntryCallback<K, V, VM>, thisArg?: any): HashMap<K, VM> {
274
+ const resultMap = new HashMap<K, VM>();
275
275
  let index = 0;
276
276
  for (const [key, value] of this) {
277
277
  resultMap.set(key, callbackfn.call(thisArg, value, key, index++, this));
@@ -322,7 +322,7 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
322
322
  * The function returns an iterator that yields key-value pairs from both an object store and an
323
323
  * object map.
324
324
  */
325
- protected* _getIterator(): IterableIterator<[K, V]> {
325
+ protected *_getIterator(): IterableIterator<[K, V]> {
326
326
  for (const node of Object.values(this.store)) {
327
327
  yield [node.key, node.value] as [K, V];
328
328
  }
@@ -537,7 +537,7 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
537
537
  /**
538
538
  * The `begin()` function in TypeScript iterates over a linked list and yields key-value pairs.
539
539
  */
540
- * begin() {
540
+ *begin() {
541
541
  let node = this.head;
542
542
  while (node !== this._sentinel) {
543
543
  yield [node.key, node.value];
@@ -549,7 +549,7 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
549
549
  * The function `reverseBegin()` iterates over a linked list in reverse order, yielding each node's
550
550
  * key and value.
551
551
  */
552
- * reverseBegin() {
552
+ *reverseBegin() {
553
553
  let node = this.tail;
554
554
  while (node !== this._sentinel) {
555
555
  yield [node.key, node.value];
@@ -898,8 +898,8 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
898
898
  * @returns a new `LinkedHashMap` object with the values mapped according to the provided callback
899
899
  * function.
900
900
  */
901
- map<NV>(callback: EntryCallback<K, V, NV>, thisArg?: any): LinkedHashMap<K, NV> {
902
- const mappedMap = new LinkedHashMap<K, NV>();
901
+ map<VM>(callback: EntryCallback<K, V, VM>, thisArg?: any): LinkedHashMap<K, VM> {
902
+ const mappedMap = new LinkedHashMap<K, VM>();
903
903
  let index = 0;
904
904
  for (const [key, value] of this) {
905
905
  const newValue = callback.call(thisArg, value, key, index, this);
@@ -942,7 +942,7 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
942
942
  *
943
943
  * The above function is an iterator that yields key-value pairs from a linked list.
944
944
  */
945
- protected* _getIterator() {
945
+ protected *_getIterator() {
946
946
  let node = this.head;
947
947
  while (node !== this._sentinel) {
948
948
  yield [node.key, node.value] as [K, V];
@@ -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,20 +343,36 @@ 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
  */
426
- protected* _getIterator(): IterableIterator<E> {
370
+ protected *_getIterator(): IterableIterator<E> {
427
371
  for (const element of this.elements) {
428
372
  yield element;
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
  }