avl-tree-typed 2.0.4 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. package/dist/common/index.js +1 -1
  2. package/dist/constants/index.js +1 -1
  3. package/dist/data-structures/base/iterable-element-base.d.ts +186 -83
  4. package/dist/data-structures/base/iterable-element-base.js +149 -107
  5. package/dist/data-structures/base/iterable-entry-base.d.ts +95 -119
  6. package/dist/data-structures/base/iterable-entry-base.js +59 -116
  7. package/dist/data-structures/base/linear-base.d.ts +250 -192
  8. package/dist/data-structures/base/linear-base.js +137 -274
  9. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
  10. package/dist/data-structures/binary-tree/avl-tree-counter.js +171 -205
  11. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
  12. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -87
  13. package/dist/data-structures/binary-tree/avl-tree.d.ts +138 -149
  14. package/dist/data-structures/binary-tree/avl-tree.js +208 -195
  15. package/dist/data-structures/binary-tree/binary-tree.d.ts +476 -632
  16. package/dist/data-structures/binary-tree/binary-tree.js +612 -879
  17. package/dist/data-structures/binary-tree/bst.d.ts +258 -306
  18. package/dist/data-structures/binary-tree/bst.js +505 -481
  19. package/dist/data-structures/binary-tree/red-black-tree.d.ts +107 -179
  20. package/dist/data-structures/binary-tree/red-black-tree.js +114 -209
  21. package/dist/data-structures/binary-tree/tree-counter.d.ts +132 -154
  22. package/dist/data-structures/binary-tree/tree-counter.js +172 -203
  23. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
  24. package/dist/data-structures/binary-tree/tree-multi-map.js +105 -85
  25. package/dist/data-structures/graph/abstract-graph.d.ts +238 -233
  26. package/dist/data-structures/graph/abstract-graph.js +267 -237
  27. package/dist/data-structures/graph/directed-graph.d.ts +108 -224
  28. package/dist/data-structures/graph/directed-graph.js +146 -233
  29. package/dist/data-structures/graph/map-graph.d.ts +49 -55
  30. package/dist/data-structures/graph/map-graph.js +56 -59
  31. package/dist/data-structures/graph/undirected-graph.d.ts +103 -146
  32. package/dist/data-structures/graph/undirected-graph.js +129 -149
  33. package/dist/data-structures/hash/hash-map.d.ts +164 -338
  34. package/dist/data-structures/hash/hash-map.js +270 -457
  35. package/dist/data-structures/heap/heap.d.ts +214 -289
  36. package/dist/data-structures/heap/heap.js +340 -349
  37. package/dist/data-structures/heap/max-heap.d.ts +11 -47
  38. package/dist/data-structures/heap/max-heap.js +11 -66
  39. package/dist/data-structures/heap/min-heap.d.ts +12 -47
  40. package/dist/data-structures/heap/min-heap.js +11 -66
  41. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
  42. package/dist/data-structures/linked-list/doubly-linked-list.js +368 -494
  43. package/dist/data-structures/linked-list/singly-linked-list.d.ts +261 -310
  44. package/dist/data-structures/linked-list/singly-linked-list.js +447 -466
  45. package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -107
  46. package/dist/data-structures/linked-list/skip-linked-list.js +0 -100
  47. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
  48. package/dist/data-structures/priority-queue/max-priority-queue.js +11 -78
  49. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
  50. package/dist/data-structures/priority-queue/min-priority-queue.js +10 -79
  51. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -61
  52. package/dist/data-structures/priority-queue/priority-queue.js +8 -83
  53. package/dist/data-structures/queue/deque.d.ts +227 -254
  54. package/dist/data-structures/queue/deque.js +309 -348
  55. package/dist/data-structures/queue/queue.d.ts +180 -201
  56. package/dist/data-structures/queue/queue.js +265 -248
  57. package/dist/data-structures/stack/stack.d.ts +124 -102
  58. package/dist/data-structures/stack/stack.js +181 -125
  59. package/dist/data-structures/trie/trie.d.ts +164 -165
  60. package/dist/data-structures/trie/trie.js +189 -172
  61. package/dist/interfaces/binary-tree.d.ts +56 -6
  62. package/dist/interfaces/graph.d.ts +16 -0
  63. package/dist/types/data-structures/base/base.d.ts +1 -1
  64. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -0
  65. package/dist/types/utils/utils.d.ts +6 -6
  66. package/dist/utils/number.js +1 -2
  67. package/dist/utils/utils.d.ts +110 -49
  68. package/dist/utils/utils.js +149 -74
  69. package/package.json +15 -15
  70. package/src/data-structures/base/iterable-element-base.ts +238 -115
  71. package/src/data-structures/base/iterable-entry-base.ts +96 -120
  72. package/src/data-structures/base/linear-base.ts +271 -277
  73. package/src/data-structures/binary-tree/avl-tree-counter.ts +198 -216
  74. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +192 -101
  75. package/src/data-structures/binary-tree/avl-tree.ts +239 -206
  76. package/src/data-structures/binary-tree/binary-tree.ts +681 -905
  77. package/src/data-structures/binary-tree/bst.ts +568 -570
  78. package/src/data-structures/binary-tree/red-black-tree.ts +161 -222
  79. package/src/data-structures/binary-tree/tree-counter.ts +199 -218
  80. package/src/data-structures/binary-tree/tree-multi-map.ts +131 -97
  81. package/src/data-structures/graph/abstract-graph.ts +339 -264
  82. package/src/data-structures/graph/directed-graph.ts +146 -236
  83. package/src/data-structures/graph/map-graph.ts +63 -60
  84. package/src/data-structures/graph/undirected-graph.ts +129 -152
  85. package/src/data-structures/hash/hash-map.ts +274 -496
  86. package/src/data-structures/heap/heap.ts +389 -402
  87. package/src/data-structures/heap/max-heap.ts +12 -76
  88. package/src/data-structures/heap/min-heap.ts +13 -76
  89. package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
  90. package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
  91. package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
  92. package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
  93. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
  94. package/src/data-structures/priority-queue/priority-queue.ts +3 -92
  95. package/src/data-structures/queue/deque.ts +381 -357
  96. package/src/data-structures/queue/queue.ts +310 -264
  97. package/src/data-structures/stack/stack.ts +217 -131
  98. package/src/data-structures/trie/trie.ts +240 -175
  99. package/src/interfaces/binary-tree.ts +240 -6
  100. package/src/interfaces/graph.ts +37 -0
  101. package/src/types/data-structures/base/base.ts +5 -5
  102. package/src/types/data-structures/graph/abstract-graph.ts +5 -0
  103. package/src/types/utils/utils.ts +9 -5
  104. package/src/utils/utils.ts +152 -86
@@ -1,13 +1,17 @@
1
1
  /**
2
2
  * data-structure-typed
3
- *
4
3
  * @author Kirk Qi
5
4
  * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
6
5
  * @license MIT License
7
6
  */
8
- import type { Comparator, ElementCallback, HeapOptions } from '../../types';
7
+ import type { HeapOptions } from '../../types';
9
8
  import { Heap } from './heap';
10
9
  /**
10
+ * @template E
11
+ * @template R
12
+ * Max-oriented binary heap.
13
+ * Notes and typical use-cases are documented in {@link Heap}.
14
+ *
11
15
  * 1. Complete Binary Tree: Heaps are typically complete binary trees, meaning every level is fully filled except possibly for the last level, which has nodes as far left as possible.
12
16
  * 2. Heap Properties: The value of each parent node is greater than or equal to the value of its children.
13
17
  * 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.
@@ -16,53 +20,13 @@ import { Heap } from './heap';
16
20
  * 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
21
  * 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
18
22
  * 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum-spanning tree algorithm, which use heaps to improve performance.
23
+ * @example
19
24
  */
20
25
  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>, thisArg?: any): MaxHeap<E, R>;
46
26
  /**
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.
27
+ * Create a max-heap. For objects, supply a custom comparator.
28
+ * @param elements Optional initial elements.
29
+ * @param options Optional configuration.
66
30
  */
67
- map<EM, RM>(callback: ElementCallback<E, R, EM>, comparator: Comparator<EM>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): MaxHeap<EM, RM>;
31
+ constructor(elements?: Iterable<E> | Iterable<R>, options?: HeapOptions<E, R>);
68
32
  }
@@ -3,6 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MaxHeap = void 0;
4
4
  const heap_1 = require("./heap");
5
5
  /**
6
+ * @template E
7
+ * @template R
8
+ * Max-oriented binary heap.
9
+ * Notes and typical use-cases are documented in {@link Heap}.
10
+ *
6
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.
7
12
  * 2. Heap Properties: The value of each parent node is greater than or equal to the value of its children.
8
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.
@@ -11,8 +16,14 @@ const heap_1 = require("./heap");
11
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.
12
17
  * 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
13
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
+ * @example
14
20
  */
15
21
  class MaxHeap extends heap_1.Heap {
22
+ /**
23
+ * Create a max-heap. For objects, supply a custom comparator.
24
+ * @param elements Optional initial elements.
25
+ * @param options Optional configuration.
26
+ */
16
27
  constructor(elements = [], options) {
17
28
  super(elements, Object.assign({ comparator: (a, b) => {
18
29
  if (typeof a === 'object' || typeof b === 'object') {
@@ -25,71 +36,5 @@ class MaxHeap extends heap_1.Heap {
25
36
  return 0;
26
37
  } }, options));
27
38
  }
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);
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++;
91
- }
92
- return mappedHeap;
93
- }
94
39
  }
95
40
  exports.MaxHeap = MaxHeap;
@@ -1,13 +1,18 @@
1
1
  /**
2
+ * @remarks Time O(n log n), Space O(n).
2
3
  * data-structure-typed
3
- *
4
4
  * @author Kirk Qi
5
5
  * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { Comparator, ElementCallback, HeapOptions } from '../../types';
8
+ import type { HeapOptions } from '../../types';
9
9
  import { Heap } from './heap';
10
10
  /**
11
+ * @template E
12
+ * @template R
13
+ * Min-oriented binary heap.
14
+ * Notes and typical use-cases are documented in {@link Heap}.
15
+ *
11
16
  * 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
17
  * 2. MinHeap Properties: The value of each parent node is less than or equal to the value of its children.
13
18
  * 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.
@@ -16,53 +21,13 @@ import { Heap } from './heap';
16
21
  * 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
22
  * 7. Efficient Sorting Algorithms: For example, heap sort. MinHeap sort uses the properties of a heap to sort elements.
18
23
  * 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
24
+ * @example
19
25
  */
20
26
  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>, thisArg?: any): MinHeap<E, R>;
46
27
  /**
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.
28
+ * Create a min-heap.
29
+ * @param elements Optional initial elements.
30
+ * @param options Optional configuration.
66
31
  */
67
- map<EM, RM>(callback: ElementCallback<E, R, EM>, comparator: Comparator<EM>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): MinHeap<EM, RM>;
32
+ constructor(elements?: Iterable<E> | Iterable<R>, options?: HeapOptions<E, R>);
68
33
  }
@@ -3,6 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MinHeap = void 0;
4
4
  const heap_1 = require("./heap");
5
5
  /**
6
+ * @template E
7
+ * @template R
8
+ * Min-oriented binary heap.
9
+ * Notes and typical use-cases are documented in {@link Heap}.
10
+ *
6
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.
7
12
  * 2. MinHeap Properties: The value of each parent node is less than or equal to the value of its children.
8
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.
@@ -11,76 +16,16 @@ const heap_1 = require("./heap");
11
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.
12
17
  * 7. Efficient Sorting Algorithms: For example, heap sort. MinHeap sort uses the properties of a heap to sort elements.
13
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
+ * @example
14
20
  */
15
21
  class MinHeap extends heap_1.Heap {
16
- constructor(elements = [], options) {
17
- super(elements, options);
18
- }
19
- /**
20
- * The `clone` function returns a new instance of the `MinHeap` class with the same comparator and
21
- * toElementFn as the original instance.
22
- * @returns The `clone()` method is returning a new instance of the `MinHeap` class with the same
23
- * properties as the current instance.
24
- */
25
- clone() {
26
- return new MinHeap(this, { comparator: this.comparator, toElementFn: this.toElementFn });
27
- }
28
22
  /**
29
- * Time Complexity: O(n)
30
- * Space Complexity: O(n)
31
- *
32
- * The `filter` function creates a new MinHeap object containing elements that pass a given callback
33
- * function.
34
- * @param callback - The `callback` parameter is a function that will be called for each element in
35
- * the heap. It takes three arguments: the current element, the index of the current element, and the
36
- * heap itself. The callback function should return a boolean value indicating whether the current
37
- * element should be included in the filtered list
38
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
39
- * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
40
- * passed as the `this` value to the `callback` function. If `thisArg` is
41
- * @returns The `filter` method is returning a new `MinHeap` object that contains the elements that pass
42
- * the filter condition specified by the `callback` function.
23
+ * Create a min-heap.
24
+ * @param elements Optional initial elements.
25
+ * @param options Optional configuration.
43
26
  */
44
- filter(callback, thisArg) {
45
- const filteredList = new MinHeap([], { toElementFn: this.toElementFn, comparator: this.comparator });
46
- let index = 0;
47
- for (const current of this) {
48
- if (callback.call(thisArg, current, index, this)) {
49
- filteredList.add(current);
50
- }
51
- index++;
52
- }
53
- return filteredList;
54
- }
55
- /**
56
- * Time Complexity: O(n log n)
57
- * Space Complexity: O(n)
58
- *
59
- * The `map` function creates a new heap by applying a callback function to each element of the
60
- * original heap.
61
- * @param callback - The `callback` parameter is a function that will be called for each element in
62
- * the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
63
- * element), and `this` (the heap itself). The callback function should return a value of
64
- * @param comparator - The `comparator` parameter is a function that defines the order of the
65
- * elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
66
- * if `a` should be placed before `b`, a positive number if `a` should be placed after
67
- * @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
68
- * element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
69
- * returns a value of type `T`. This function is used to transform the elements of the original
70
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
71
- * specify the value of `this` within the callback function. It is used to set the context or scope
72
- * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
73
- * value of
74
- * @returns a new instance of the `MinHeap` class with the mapped elements.
75
- */
76
- map(callback, comparator, toElementFn, thisArg) {
77
- const mappedHeap = new MinHeap([], { comparator, toElementFn });
78
- let index = 0;
79
- for (const el of this) {
80
- mappedHeap.add(callback.call(thisArg, el, index, this));
81
- index++;
82
- }
83
- return mappedHeap;
27
+ constructor(elements = [], options) {
28
+ super(elements, options);
84
29
  }
85
30
  }
86
31
  exports.MinHeap = MinHeap;