linked-list-typed 1.51.9 → 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 (102) 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 +54 -52
  13. package/dist/data-structures/binary-tree/bst.d.ts +37 -45
  14. package/dist/data-structures/binary-tree/bst.js +17 -25
  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 +8 -29
  40. package/dist/data-structures/queue/queue.js +15 -32
  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 +2 -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} +22 -213
  66. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +14 -15
  67. package/src/data-structures/binary-tree/avl-tree.ts +13 -14
  68. package/src/data-structures/binary-tree/binary-tree.ts +156 -152
  69. package/src/data-structures/binary-tree/bst.ts +52 -60
  70. package/src/data-structures/binary-tree/rb-tree.ts +12 -13
  71. package/src/data-structures/binary-tree/tree-multi-map.ts +12 -13
  72. package/src/data-structures/graph/directed-graph.ts +2 -1
  73. package/src/data-structures/hash/hash-map.ts +4 -4
  74. package/src/data-structures/heap/heap.ts +71 -152
  75. package/src/data-structures/heap/max-heap.ts +88 -13
  76. package/src/data-structures/heap/min-heap.ts +78 -15
  77. package/src/data-structures/linked-list/doubly-linked-list.ts +32 -32
  78. package/src/data-structures/linked-list/singly-linked-list.ts +37 -29
  79. package/src/data-structures/priority-queue/max-priority-queue.ts +94 -13
  80. package/src/data-structures/priority-queue/min-priority-queue.ts +84 -15
  81. package/src/data-structures/priority-queue/priority-queue.ts +81 -4
  82. package/src/data-structures/queue/deque.ts +50 -25
  83. package/src/data-structures/queue/queue.ts +23 -37
  84. package/src/data-structures/stack/stack.ts +31 -26
  85. package/src/data-structures/trie/trie.ts +33 -18
  86. package/src/interfaces/binary-tree.ts +4 -5
  87. package/src/types/common.ts +2 -24
  88. package/src/types/data-structures/base/base.ts +14 -6
  89. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +2 -3
  90. package/src/types/data-structures/binary-tree/avl-tree.ts +2 -3
  91. package/src/types/data-structures/binary-tree/binary-tree.ts +24 -5
  92. package/src/types/data-structures/binary-tree/bst.ts +9 -3
  93. package/src/types/data-structures/binary-tree/rb-tree.ts +2 -3
  94. package/src/types/data-structures/binary-tree/tree-multi-map.ts +2 -3
  95. package/src/types/data-structures/heap/heap.ts +4 -1
  96. package/src/types/data-structures/linked-list/doubly-linked-list.ts +3 -1
  97. package/src/types/data-structures/linked-list/singly-linked-list.ts +3 -1
  98. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
  99. package/src/types/data-structures/queue/deque.ts +6 -1
  100. package/src/types/data-structures/queue/queue.ts +3 -1
  101. package/src/types/data-structures/stack/stack.ts +3 -1
  102. package/src/types/data-structures/trie/trie.ts +3 -1
@@ -18,30 +18,36 @@ const base_1 = require("../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
23
  class Heap extends base_1.IterableElementBase {
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
37
  constructor(elements = [], options) {
35
- super();
36
- this._comparator = (a, b) => {
37
- if (!(typeof a === 'number' && typeof b === 'number')) {
38
- throw new Error('The a, b params of compare function must be number');
39
- }
40
- else {
41
- return a - b;
38
+ super(options);
39
+ this._elements = [];
40
+ this._DEFAULT_COMPARATOR = (a, b) => {
41
+ if (typeof a === 'object' || typeof b === 'object') {
42
+ throw TypeError(`When comparing object types, a custom comparator must be defined in the constructor's options parameter.`);
42
43
  }
44
+ if (a > b)
45
+ return 1;
46
+ if (a < b)
47
+ return -1;
48
+ return 0;
43
49
  };
44
- this._elements = [];
50
+ this._comparator = this._DEFAULT_COMPARATOR;
45
51
  if (options) {
46
52
  const { comparator } = options;
47
53
  if (comparator)
@@ -49,20 +55,16 @@ class Heap extends base_1.IterableElementBase {
49
55
  }
50
56
  if (elements) {
51
57
  for (const el of elements) {
52
- this.add(el);
58
+ if (this.toElementFn)
59
+ this.add(this.toElementFn(el));
60
+ else
61
+ this.add(el);
53
62
  }
54
63
  }
55
64
  }
56
- /**
57
- * The function returns the value of the _comparator property.
58
- * @returns The `_comparator` property is being returned.
59
- */
60
- get comparator() {
61
- return this._comparator;
62
- }
63
65
  /**
64
66
  * The function returns an array of elements.
65
- * @returns The elements array is being returned.
67
+ * @returns The element array is being returned.
66
68
  */
67
69
  get elements() {
68
70
  return this._elements;
@@ -90,11 +92,6 @@ class Heap extends base_1.IterableElementBase {
90
92
  static heapify(elements, options) {
91
93
  return new Heap(elements, options);
92
94
  }
93
- /**
94
- * Time Complexity: O(log n)
95
- * Space Complexity: O(1)
96
- * where n is the number of elements in the heap.
97
- */
98
95
  /**
99
96
  * Time Complexity: O(log n)
100
97
  * Space Complexity: O(1)
@@ -106,16 +103,11 @@ class Heap extends base_1.IterableElementBase {
106
103
  this._elements.push(element);
107
104
  return this._bubbleUp(this.elements.length - 1);
108
105
  }
109
- /**
110
- * Time Complexity: O(log n)
111
- * Space Complexity: O(1)
112
- * where n is the number of elements in the heap.
113
- */
114
106
  /**
115
107
  * Time Complexity: O(log n)
116
108
  * Space Complexity: O(1)
117
109
  *
118
- * Remove and return the top element (smallest or largest element) from the heap.
110
+ * Remove and return the top element (the smallest or largest element) from the heap.
119
111
  * @returns The top element or undefined if the heap is empty.
120
112
  */
121
113
  poll() {
@@ -129,10 +121,6 @@ class Heap extends base_1.IterableElementBase {
129
121
  }
130
122
  return value;
131
123
  }
132
- /**
133
- * Time Complexity: O(1)
134
- * Space Complexity: O(1)
135
- */
136
124
  /**
137
125
  * Time Complexity: O(1)
138
126
  * Space Complexity: O(1)
@@ -156,10 +144,6 @@ class Heap extends base_1.IterableElementBase {
156
144
  clear() {
157
145
  this._elements = [];
158
146
  }
159
- /**
160
- * Time Complexity: O(n)
161
- * Space Complexity: O(n)
162
- */
163
147
  /**
164
148
  * Time Complexity: O(n)
165
149
  * Space Complexity: O(n)
@@ -171,10 +155,6 @@ class Heap extends base_1.IterableElementBase {
171
155
  this._elements = elements;
172
156
  return this.fix();
173
157
  }
174
- /**
175
- * Time Complexity: O(n)
176
- * Space Complexity: O(1)
177
- */
178
158
  /**
179
159
  * Time Complexity: O(n)
180
160
  * Space Complexity: O(1)
@@ -187,12 +167,7 @@ class Heap extends base_1.IterableElementBase {
187
167
  return this.elements.includes(element);
188
168
  }
189
169
  /**
190
- * Time Complexity: O(n)
191
- * Space Complexity: O(1)
192
- * 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.
193
- */
194
- /**
195
- * Time Complexity: O(n)
170
+ * Time Complexity: O(n)
196
171
  * Space Complexity: O(1)
197
172
  *
198
173
  * The `delete` function removes an element from an array-like data structure, maintaining the order
@@ -219,11 +194,6 @@ class Heap extends base_1.IterableElementBase {
219
194
  }
220
195
  return true;
221
196
  }
222
- /**
223
- * Time Complexity: O(n)
224
- * Space Complexity: O(log n)
225
- * where log n is the height of the heap.
226
- */
227
197
  /**
228
198
  * Time Complexity: O(n)
229
199
  * Space Complexity: O(log n)
@@ -258,10 +228,6 @@ class Heap extends base_1.IterableElementBase {
258
228
  _dfs(0); // Traverse starting from the root node
259
229
  return result;
260
230
  }
261
- /**
262
- * Time Complexity: O(n)
263
- * Space Complexity: O(n)
264
- */
265
231
  /**
266
232
  * Time Complexity: O(n)
267
233
  * Space Complexity: O(n)
@@ -272,10 +238,6 @@ class Heap extends base_1.IterableElementBase {
272
238
  toArray() {
273
239
  return [...this.elements];
274
240
  }
275
- /**
276
- * Time Complexity: O(n)
277
- * Space Complexity: O(n)
278
- */
279
241
  /**
280
242
  * Time Complexity: O(n)
281
243
  * Space Complexity: O(n)
@@ -284,14 +246,8 @@ class Heap extends base_1.IterableElementBase {
284
246
  * @returns A new Heap instance containing the same elements.
285
247
  */
286
248
  clone() {
287
- const clonedHeap = new Heap([], { comparator: this.comparator });
288
- clonedHeap._elements = [...this.elements];
289
- return clonedHeap;
249
+ return new Heap(this, { comparator: this.comparator, toElementFn: this.toElementFn });
290
250
  }
291
- /**
292
- * Time Complexity: O(n log n)
293
- * Space Complexity: O(n)
294
- */
295
251
  /**
296
252
  * Time Complexity: O(n log n)
297
253
  * Space Complexity: O(n)
@@ -301,18 +257,14 @@ class Heap extends base_1.IterableElementBase {
301
257
  */
302
258
  sort() {
303
259
  const visitedNode = [];
304
- const cloned = this.clone();
260
+ const cloned = new Heap(this, { comparator: this.comparator });
305
261
  while (cloned.size !== 0) {
306
262
  const top = cloned.poll();
307
- if (top)
263
+ if (top !== undefined)
308
264
  visitedNode.push(top);
309
265
  }
310
266
  return visitedNode;
311
267
  }
312
- /**
313
- * Time Complexity: O(n log n)
314
- * Space Complexity: O(n)
315
- */
316
268
  /**
317
269
  * Time Complexity: O(n log n)
318
270
  * Space Complexity: O(n)
@@ -325,10 +277,6 @@ class Heap extends base_1.IterableElementBase {
325
277
  results.push(this._sinkDown(i, this.elements.length >> 1));
326
278
  return results;
327
279
  }
328
- /**
329
- * Time Complexity: O(n)
330
- * Space Complexity: O(n)
331
- */
332
280
  /**
333
281
  * Time Complexity: O(n)
334
282
  * Space Complexity: O(n)
@@ -346,7 +294,7 @@ class Heap extends base_1.IterableElementBase {
346
294
  * the filter condition specified by the `callback` function.
347
295
  */
348
296
  filter(callback, thisArg) {
349
- const filteredList = new Heap();
297
+ const filteredList = new Heap([], { toElementFn: this.toElementFn, comparator: this.comparator });
350
298
  let index = 0;
351
299
  for (const current of this) {
352
300
  if (callback.call(thisArg, current, index, this)) {
@@ -357,31 +305,28 @@ class Heap extends base_1.IterableElementBase {
357
305
  return filteredList;
358
306
  }
359
307
  /**
360
- * Time Complexity: O(n)
361
- * Space Complexity: O(n)
362
- */
363
- /**
364
- * Time Complexity: O(n)
308
+ * Time Complexity: O(n log n)
365
309
  * Space Complexity: O(n)
366
310
  *
367
311
  * The `map` function creates a new heap by applying a callback function to each element of the
368
312
  * original heap.
369
- * @param callback - The callback parameter is a function that will be called for each element in the
370
- * original heap. It takes three arguments: the current element, the index of the current element,
371
- * and the original heap itself. The callback function should return a value of type T, which will be
372
- * added to the mapped heap.
373
- * @param comparator - The `comparator` parameter is a function that is used to compare elements in
374
- * the heap. It takes two arguments, `a` and `b`, and returns a negative number if `a` is less than
375
- * `b`, a positive number if `a` is greater than `b`, or
313
+ * @param callback - The `callback` parameter is a function that will be called for each element in
314
+ * the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
315
+ * element), and `this` (the heap itself). The callback function should return a value of
316
+ * @param comparator - The `comparator` parameter is a function that defines the order of the
317
+ * elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
318
+ * if `a` should be placed before `b`, a positive number if `a` should be placed after
319
+ * @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
320
+ * element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
321
+ * returns a value of type `T`. This function is used to transform the elements of the original
376
322
  * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
377
- * specify the value of `this` within the callback function. It is used when you want to bind a
378
- * specific object as the context for the callback function. If `thisArg` is not provided,
379
- * `undefined` is used as
380
- * @returns a new instance of the Heap class, which is created using the mapped elements from the
381
- * original Heap.
382
- */
383
- map(callback, comparator, thisArg) {
384
- const mappedHeap = new Heap([], { comparator: comparator });
323
+ * specify the value of `this` within the callback function. It is used to set the context or scope
324
+ * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
325
+ * value of
326
+ * @returns a new instance of the `Heap` class with the mapped elements.
327
+ */
328
+ map(callback, comparator, toElementFn, thisArg) {
329
+ const mappedHeap = new Heap([], { comparator, toElementFn });
385
330
  let index = 0;
386
331
  for (const el of this) {
387
332
  mappedHeap.add(callback.call(thisArg, el, index, this));
@@ -389,6 +334,13 @@ class Heap extends base_1.IterableElementBase {
389
334
  }
390
335
  return mappedHeap;
391
336
  }
337
+ /**
338
+ * The function returns the value of the _comparator property.
339
+ * @returns The `_comparator` property is being returned.
340
+ */
341
+ get comparator() {
342
+ return this._comparator;
343
+ }
392
344
  /**
393
345
  * The function `_getIterator` returns an iterable iterator for the elements in the class.
394
346
  */
@@ -397,10 +349,6 @@ class Heap extends base_1.IterableElementBase {
397
349
  yield element;
398
350
  }
399
351
  }
400
- /**
401
- * Time Complexity: O(log n)
402
- * Space Complexity: O(1)
403
- */
404
352
  /**
405
353
  * Time Complexity: O(log n)
406
354
  * Space Complexity: O(1)
@@ -421,10 +369,6 @@ class Heap extends base_1.IterableElementBase {
421
369
  this.elements[index] = element;
422
370
  return true;
423
371
  }
424
- /**
425
- * Time Complexity: O(log n)
426
- * Space Complexity: O(1)
427
- */
428
372
  /**
429
373
  * Time Complexity: O(log n)
430
374
  * Space Complexity: O(1)
@@ -525,10 +469,6 @@ class FibonacciHeap {
525
469
  this._min = undefined;
526
470
  this._size = 0;
527
471
  }
528
- /**
529
- * Time Complexity: O(1)
530
- * Space Complexity: O(1)
531
- */
532
472
  /**
533
473
  * Time Complexity: O(1)
534
474
  * Space Complexity: O(1)
@@ -540,10 +480,6 @@ class FibonacciHeap {
540
480
  add(element) {
541
481
  return this.push(element);
542
482
  }
543
- /**
544
- * Time Complexity: O(1)
545
- * Space Complexity: O(1)
546
- */
547
483
  /**
548
484
  * Time Complexity: O(1)
549
485
  * Space Complexity: O(1)
@@ -563,10 +499,6 @@ class FibonacciHeap {
563
499
  this._size++;
564
500
  return this;
565
501
  }
566
- /**
567
- * Time Complexity: O(1)
568
- * Space Complexity: O(1)
569
- */
570
502
  /**
571
503
  * Time Complexity: O(1)
572
504
  * Space Complexity: O(1)
@@ -635,7 +567,7 @@ class FibonacciHeap {
635
567
  * Time Complexity: O(log n)
636
568
  * Space Complexity: O(1)
637
569
  *
638
- * Remove and return the top element (smallest or largest element) from the heap.
570
+ * Remove and return the top element (the smallest or largest element) from the heap.
639
571
  * @returns The top element or undefined if the heap is empty.
640
572
  */
641
573
  poll() {
@@ -649,7 +581,7 @@ class FibonacciHeap {
649
581
  * Time Complexity: O(log n)
650
582
  * Space Complexity: O(1)
651
583
  *
652
- * Remove and return the top element (smallest or largest element) from the heap.
584
+ * Remove and return the top element (the smallest or largest element) from the heap.
653
585
  * @returns The top element or undefined if the heap is empty.
654
586
  */
655
587
  pop() {
@@ -760,8 +692,8 @@ class FibonacciHeap {
760
692
  /**
761
693
  * Time Complexity: O(1)
762
694
  * Space Complexity: O(1)
763
- *.
764
- * Remove and return the top element (smallest or largest element) from the heap.
695
+ *
696
+ * Remove and return the top element (the smallest or largest element) from the heap.
765
697
  * @param node - The node to be removed.
766
698
  * @protected
767
699
  */
@@ -781,7 +713,7 @@ class FibonacciHeap {
781
713
  * Time Complexity: O(1)
782
714
  * Space Complexity: O(1)
783
715
  *
784
- * Remove and return the top element (smallest or largest element) from the heap.
716
+ * Remove and return the top element (the smallest or largest element) from the heap.
785
717
  * @param y
786
718
  * @param x
787
719
  * @protected
@@ -802,7 +734,7 @@ class FibonacciHeap {
802
734
  * Time Complexity: O(n log n)
803
735
  * Space Complexity: O(n)
804
736
  *
805
- * Remove and return the top element (smallest or largest element) from the heap.
737
+ * Remove and return the top element (the smallest or largest element) from the heap.
806
738
  * @protected
807
739
  */
808
740
  _consolidate() {
@@ -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
  * 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.
@@ -15,8 +15,54 @@ import { Heap } from './heap';
15
15
  * 5. Managing Dynamic Data Sets: Heaps effectively manage dynamic data sets, especially when frequent access to the largest or smallest elements is required.
16
16
  * 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.
17
17
  * 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
18
- * 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
18
+ * 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum-spanning tree algorithm, which use heaps to improve performance.
19
19
  */
20
- export declare class MaxHeap<E = any> extends Heap<E> {
21
- constructor(elements?: Iterable<E>, options?: HeapOptions<E>);
20
+ export declare class MaxHeap<E = any, R = any> extends Heap<E, R> {
21
+ constructor(elements?: Iterable<E> | Iterable<R>, options?: HeapOptions<E, R>);
22
+ /**
23
+ * The `clone` function returns a new instance of the `MaxHeap` class with the same properties as the
24
+ * current instance.
25
+ * @returns The `clone()` method is returning a new instance of the `MaxHeap` class with the same
26
+ * properties as the current instance.
27
+ */
28
+ clone(): MaxHeap<E, R>;
29
+ /**
30
+ * Time Complexity: O(n)
31
+ * Space Complexity: O(n)
32
+ *
33
+ * The `filter` function creates a new MaxHeap object containing elements that pass a given callback
34
+ * function.
35
+ * @param callback - The `callback` parameter is a function that will be called for each element in
36
+ * the heap. It takes three arguments: the current element, the index of the current element, and the
37
+ * heap itself. The callback function should return a boolean value indicating whether the current
38
+ * element should be included in the filtered list
39
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
40
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
41
+ * passed as the `this` value to the `callback` function. If `thisArg` is
42
+ * @returns The `filter` method is returning a new `MaxHeap` object that contains the elements that pass
43
+ * the filter condition specified by the `callback` function.
44
+ */
45
+ filter(callback: ElementCallback<E, R, boolean, MaxHeap<E, R>>, thisArg?: any): MaxHeap<E, R>;
46
+ /**
47
+ * Time Complexity: O(n log n)
48
+ * Space Complexity: O(n)
49
+ *
50
+ * The `map` function creates a new heap by applying a callback function to each element of the
51
+ * original heap.
52
+ * @param callback - The `callback` parameter is a function that will be called for each element in
53
+ * the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
54
+ * element), and `this` (the heap itself). The callback function should return a value of
55
+ * @param comparator - The `comparator` parameter is a function that defines the order of the
56
+ * elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
57
+ * if `a` should be placed before `b`, a positive number if `a` should be placed after
58
+ * @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
59
+ * element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
60
+ * returns a value of type `T`. This function is used to transform the elements of the original
61
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
62
+ * specify the value of `this` within the callback function. It is used to set the context or scope
63
+ * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
64
+ * value of
65
+ * @returns a new instance of the `MaxHeap` class with the mapped elements.
66
+ */
67
+ map<EM, RM>(callback: ElementCallback<E, R, EM, MaxHeap<E, R>>, comparator: Comparator<EM>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): MaxHeap<EM, RM>;
22
68
  }
@@ -10,20 +10,86 @@ const heap_1 = require("./heap");
10
10
  * 5. Managing Dynamic Data Sets: Heaps effectively manage dynamic data sets, especially when frequent access to the largest or smallest elements is required.
11
11
  * 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.
12
12
  * 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
13
- * 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
13
+ * 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum-spanning tree algorithm, which use heaps to improve performance.
14
14
  */
15
15
  class MaxHeap extends heap_1.Heap {
16
- constructor(elements = [], options = {
17
- comparator: (a, b) => {
18
- if (!(typeof a === 'number' && typeof b === 'number')) {
19
- throw new Error('The a, b params of compare function must be number');
20
- }
21
- else {
22
- return b - a;
16
+ constructor(elements = [], options) {
17
+ super(elements, Object.assign({ comparator: (a, b) => {
18
+ if (typeof a === 'object' || typeof b === 'object') {
19
+ throw TypeError(`When comparing object types, a custom comparator must be defined in the constructor's options parameter.`);
20
+ }
21
+ if (a < b)
22
+ return 1;
23
+ if (a > b)
24
+ return -1;
25
+ return 0;
26
+ } }, options));
27
+ }
28
+ /**
29
+ * The `clone` function returns a new instance of the `MaxHeap` class with the same properties as the
30
+ * current instance.
31
+ * @returns The `clone()` method is returning a new instance of the `MaxHeap` class with the same
32
+ * properties as the current instance.
33
+ */
34
+ clone() {
35
+ return new MaxHeap(this, { comparator: this.comparator, toElementFn: this.toElementFn });
36
+ }
37
+ /**
38
+ * Time Complexity: O(n)
39
+ * Space Complexity: O(n)
40
+ *
41
+ * The `filter` function creates a new MaxHeap object containing elements that pass a given callback
42
+ * function.
43
+ * @param callback - The `callback` parameter is a function that will be called for each element in
44
+ * the heap. It takes three arguments: the current element, the index of the current element, and the
45
+ * heap itself. The callback function should return a boolean value indicating whether the current
46
+ * element should be included in the filtered list
47
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
48
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
49
+ * passed as the `this` value to the `callback` function. If `thisArg` is
50
+ * @returns The `filter` method is returning a new `MaxHeap` object that contains the elements that pass
51
+ * the filter condition specified by the `callback` function.
52
+ */
53
+ filter(callback, thisArg) {
54
+ const filteredList = new MaxHeap([], { toElementFn: this.toElementFn, comparator: this.comparator });
55
+ let index = 0;
56
+ for (const current of this) {
57
+ if (callback.call(thisArg, current, index, this)) {
58
+ filteredList.add(current);
23
59
  }
60
+ index++;
61
+ }
62
+ return filteredList;
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 `MaxHeap` class with the mapped elements.
84
+ */
85
+ map(callback, comparator, toElementFn, thisArg) {
86
+ const mappedHeap = new MaxHeap([], { comparator, toElementFn });
87
+ let index = 0;
88
+ for (const el of this) {
89
+ mappedHeap.add(callback.call(thisArg, el, index, this));
90
+ index++;
24
91
  }
25
- }) {
26
- super(elements, options);
92
+ return mappedHeap;
27
93
  }
28
94
  }
29
95
  exports.MaxHeap = MaxHeap;
@@ -5,18 +5,64 @@
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
  * 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.
12
- * 2. Heap Properties: The value of each parent node is less than or equal to the value of its children.
12
+ * 2. MinHeap Properties: The value of each parent node is less than or equal to the value of its children.
13
13
  * 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.
14
14
  * 4. Efficient Insertion and Deletion: Due to its structure, a heap allows for insertion and deletion operations in logarithmic time (O(log n)).
15
15
  * 5. Managing Dynamic Data Sets: Heaps effectively manage dynamic data sets, especially when frequent access to the largest or smallest elements is required.
16
16
  * 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.
17
- * 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
17
+ * 7. Efficient Sorting Algorithms: For example, heap sort. MinHeap sort uses the properties of a heap to sort elements.
18
18
  * 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
19
19
  */
20
- export declare class MinHeap<E = any> extends Heap<E> {
21
- constructor(elements?: Iterable<E>, options?: HeapOptions<E>);
20
+ export declare class MinHeap<E = any, R = any> extends Heap<E, R> {
21
+ constructor(elements?: Iterable<E> | Iterable<R>, options?: HeapOptions<E, R>);
22
+ /**
23
+ * The `clone` function returns a new instance of the `MinHeap` class with the same comparator and
24
+ * toElementFn as the original instance.
25
+ * @returns The `clone()` method is returning a new instance of the `MinHeap` class with the same
26
+ * properties as the current instance.
27
+ */
28
+ clone(): MinHeap<E, R>;
29
+ /**
30
+ * Time Complexity: O(n)
31
+ * Space Complexity: O(n)
32
+ *
33
+ * The `filter` function creates a new MinHeap object containing elements that pass a given callback
34
+ * function.
35
+ * @param callback - The `callback` parameter is a function that will be called for each element in
36
+ * the heap. It takes three arguments: the current element, the index of the current element, and the
37
+ * heap itself. The callback function should return a boolean value indicating whether the current
38
+ * element should be included in the filtered list
39
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
40
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
41
+ * passed as the `this` value to the `callback` function. If `thisArg` is
42
+ * @returns The `filter` method is returning a new `MinHeap` object that contains the elements that pass
43
+ * the filter condition specified by the `callback` function.
44
+ */
45
+ filter(callback: ElementCallback<E, R, boolean, MinHeap<E, R>>, thisArg?: any): MinHeap<E, R>;
46
+ /**
47
+ * Time Complexity: O(n log n)
48
+ * Space Complexity: O(n)
49
+ *
50
+ * The `map` function creates a new heap by applying a callback function to each element of the
51
+ * original heap.
52
+ * @param callback - The `callback` parameter is a function that will be called for each element in
53
+ * the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
54
+ * element), and `this` (the heap itself). The callback function should return a value of
55
+ * @param comparator - The `comparator` parameter is a function that defines the order of the
56
+ * elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
57
+ * if `a` should be placed before `b`, a positive number if `a` should be placed after
58
+ * @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
59
+ * element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
60
+ * returns a value of type `T`. This function is used to transform the elements of the original
61
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
62
+ * specify the value of `this` within the callback function. It is used to set the context or scope
63
+ * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
64
+ * value of
65
+ * @returns a new instance of the `MinHeap` class with the mapped elements.
66
+ */
67
+ map<EM, RM>(callback: ElementCallback<E, R, EM, MinHeap<E, R>>, comparator: Comparator<EM>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): MinHeap<EM, RM>;
22
68
  }