data-structure-typed 1.51.9 → 1.52.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.
- package/CHANGELOG.md +1 -1
- package/README.md +13 -13
- package/benchmark/report.html +37 -1
- package/benchmark/report.json +385 -13
- package/dist/cjs/data-structures/base/index.d.ts +2 -1
- package/dist/cjs/data-structures/base/index.js +2 -1
- package/dist/cjs/data-structures/base/index.js.map +1 -1
- package/dist/cjs/data-structures/base/iterable-element-base.d.ts +171 -0
- package/dist/cjs/data-structures/base/iterable-element-base.js +226 -0
- package/dist/cjs/data-structures/base/iterable-element-base.js.map +1 -0
- package/dist/cjs/data-structures/base/{iterable-base.d.ts → iterable-entry-base.d.ts} +4 -147
- package/dist/cjs/data-structures/base/{iterable-base.js → iterable-entry-base.js} +13 -190
- package/dist/cjs/data-structures/base/iterable-entry-base.js.map +1 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -3
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +3 -3
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +4 -4
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +5 -3
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +3 -11
- package/dist/cjs/data-structures/binary-tree/bst.js +2 -10
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +3 -3
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +2 -2
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +43 -114
- package/dist/cjs/data-structures/heap/heap.js +59 -127
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.d.ts +50 -4
- package/dist/cjs/data-structures/heap/max-heap.js +76 -10
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.d.ts +51 -5
- package/dist/cjs/data-structures/heap/min-heap.js +68 -11
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +22 -28
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +26 -28
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +22 -25
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +29 -26
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +50 -4
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +79 -10
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +51 -5
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +71 -11
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +50 -4
- package/dist/cjs/data-structures/priority-queue/priority-queue.js +70 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +20 -18
- package/dist/cjs/data-structures/queue/deque.js +27 -20
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +8 -28
- package/dist/cjs/data-structures/queue/queue.js +15 -31
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +17 -22
- package/dist/cjs/data-structures/stack/stack.js +25 -24
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +18 -13
- package/dist/cjs/data-structures/trie/trie.js +26 -15
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +2 -2
- package/dist/cjs/types/data-structures/base/base.d.ts +5 -2
- package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -3
- package/dist/cjs/types/data-structures/binary-tree/avl-tree.d.ts +2 -3
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +2 -3
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +2 -3
- package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +2 -3
- package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -3
- package/dist/cjs/types/data-structures/heap/heap.d.ts +3 -2
- package/dist/cjs/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
- package/dist/cjs/types/data-structures/linked-list/singly-linked-list.d.ts +2 -1
- package/dist/cjs/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/cjs/types/data-structures/queue/deque.d.ts +3 -2
- package/dist/cjs/types/data-structures/queue/queue.d.ts +2 -1
- package/dist/cjs/types/data-structures/stack/stack.d.ts +2 -1
- package/dist/cjs/types/data-structures/trie/trie.d.ts +3 -2
- package/dist/mjs/data-structures/base/index.d.ts +2 -1
- package/dist/mjs/data-structures/base/index.js +2 -1
- package/dist/mjs/data-structures/base/iterable-element-base.d.ts +171 -0
- package/dist/mjs/data-structures/base/iterable-element-base.js +222 -0
- package/dist/mjs/data-structures/base/{iterable-base.d.ts → iterable-entry-base.d.ts} +4 -147
- package/dist/mjs/data-structures/base/{iterable-base.js → iterable-entry-base.js} +10 -186
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -3
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +3 -3
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +4 -4
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +5 -3
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +3 -11
- package/dist/mjs/data-structures/binary-tree/bst.js +2 -10
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +3 -3
- package/dist/mjs/data-structures/hash/hash-map.d.ts +2 -2
- package/dist/mjs/data-structures/heap/heap.d.ts +43 -114
- package/dist/mjs/data-structures/heap/heap.js +60 -128
- package/dist/mjs/data-structures/heap/max-heap.d.ts +50 -4
- package/dist/mjs/data-structures/heap/max-heap.js +79 -10
- package/dist/mjs/data-structures/heap/min-heap.d.ts +51 -5
- package/dist/mjs/data-structures/heap/min-heap.js +68 -11
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +22 -28
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +26 -28
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +22 -25
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +29 -26
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +50 -4
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +82 -10
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +51 -5
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +71 -11
- package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +50 -4
- package/dist/mjs/data-structures/priority-queue/priority-queue.js +70 -1
- package/dist/mjs/data-structures/queue/deque.d.ts +20 -18
- package/dist/mjs/data-structures/queue/deque.js +27 -20
- package/dist/mjs/data-structures/queue/queue.d.ts +8 -28
- package/dist/mjs/data-structures/queue/queue.js +15 -31
- package/dist/mjs/data-structures/stack/stack.d.ts +17 -22
- package/dist/mjs/data-structures/stack/stack.js +25 -24
- package/dist/mjs/data-structures/trie/trie.d.ts +18 -13
- package/dist/mjs/data-structures/trie/trie.js +26 -15
- package/dist/mjs/interfaces/binary-tree.d.ts +2 -2
- package/dist/mjs/types/data-structures/base/base.d.ts +5 -2
- package/dist/mjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -3
- package/dist/mjs/types/data-structures/binary-tree/avl-tree.d.ts +2 -3
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +2 -3
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +2 -3
- package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +2 -3
- package/dist/mjs/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -3
- package/dist/mjs/types/data-structures/heap/heap.d.ts +3 -2
- package/dist/mjs/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
- package/dist/mjs/types/data-structures/linked-list/singly-linked-list.d.ts +2 -1
- package/dist/mjs/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/mjs/types/data-structures/queue/deque.d.ts +3 -2
- package/dist/mjs/types/data-structures/queue/queue.d.ts +2 -1
- package/dist/mjs/types/data-structures/stack/stack.d.ts +2 -1
- package/dist/mjs/types/data-structures/trie/trie.d.ts +3 -2
- package/dist/umd/data-structure-typed.js +628 -319
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +6 -6
- package/src/data-structures/base/index.ts +2 -1
- package/src/data-structures/base/iterable-element-base.ts +250 -0
- package/src/data-structures/base/{iterable-base.ts → iterable-entry-base.ts} +22 -213
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +2 -3
- package/src/data-structures/binary-tree/avl-tree.ts +2 -3
- package/src/data-structures/binary-tree/binary-tree.ts +6 -6
- package/src/data-structures/binary-tree/bst.ts +8 -19
- package/src/data-structures/binary-tree/rb-tree.ts +2 -3
- package/src/data-structures/binary-tree/tree-multi-map.ts +2 -3
- package/src/data-structures/hash/hash-map.ts +4 -4
- package/src/data-structures/heap/heap.ts +71 -152
- package/src/data-structures/heap/max-heap.ts +88 -13
- package/src/data-structures/heap/min-heap.ts +78 -15
- package/src/data-structures/linked-list/doubly-linked-list.ts +32 -32
- package/src/data-structures/linked-list/singly-linked-list.ts +37 -29
- package/src/data-structures/priority-queue/max-priority-queue.ts +94 -13
- package/src/data-structures/priority-queue/min-priority-queue.ts +84 -15
- package/src/data-structures/priority-queue/priority-queue.ts +81 -4
- package/src/data-structures/queue/deque.ts +35 -24
- package/src/data-structures/queue/queue.ts +23 -36
- package/src/data-structures/stack/stack.ts +31 -26
- package/src/data-structures/trie/trie.ts +33 -18
- package/src/interfaces/binary-tree.ts +1 -2
- package/src/types/data-structures/base/base.ts +14 -6
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +2 -3
- package/src/types/data-structures/binary-tree/avl-tree.ts +2 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +3 -4
- package/src/types/data-structures/binary-tree/bst.ts +2 -3
- package/src/types/data-structures/binary-tree/rb-tree.ts +2 -3
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +2 -3
- package/src/types/data-structures/heap/heap.ts +4 -1
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +3 -1
- package/src/types/data-structures/linked-list/singly-linked-list.ts +3 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/types/data-structures/queue/deque.ts +3 -1
- package/src/types/data-structures/queue/queue.ts +3 -1
- package/src/types/data-structures/stack/stack.ts +3 -1
- package/src/types/data-structures/trie/trie.ts +3 -1
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +1 -1
- package/test/unit/data-structures/heap/heap.test.ts +80 -0
- package/test/unit/data-structures/heap/max-heap.test.ts +44 -1
- package/test/unit/data-structures/heap/min-heap.test.ts +18 -1
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +42 -0
- package/test/unit/data-structures/queue/deque.test.ts +30 -0
- package/test/unit/data-structures/queue/queue.test.ts +56 -0
- package/dist/cjs/data-structures/base/iterable-base.js.map +0 -1
|
@@ -15,21 +15,24 @@ import { IterableElementBase } from '../base';
|
|
|
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
|
|
18
|
+
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prime's minimum-spanning tree algorithm, which use heaps to improve performance.
|
|
19
19
|
*/
|
|
20
20
|
export class Heap extends IterableElementBase {
|
|
21
21
|
/**
|
|
22
22
|
* The constructor initializes a heap data structure with optional elements and options.
|
|
23
23
|
* @param elements - The `elements` parameter is an iterable object that contains the initial
|
|
24
|
-
* elements to be added to the heap.
|
|
24
|
+
* elements to be added to the heap.
|
|
25
|
+
* It is an optional parameter, and if not provided, the heap will
|
|
25
26
|
* be initialized as empty.
|
|
26
27
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
27
|
-
* configuration options for the heap.
|
|
28
|
-
*
|
|
28
|
+
* configuration options for the heap.
|
|
29
|
+
* In this case, it is used to specify a custom comparator
|
|
30
|
+
* function for comparing elements in the heap.
|
|
31
|
+
* The comparator function is used to determine the
|
|
29
32
|
* order of elements in the heap.
|
|
30
33
|
*/
|
|
31
34
|
constructor(elements = [], options) {
|
|
32
|
-
super();
|
|
35
|
+
super(options);
|
|
33
36
|
if (options) {
|
|
34
37
|
const { comparator } = options;
|
|
35
38
|
if (comparator)
|
|
@@ -37,29 +40,17 @@ export class Heap extends IterableElementBase {
|
|
|
37
40
|
}
|
|
38
41
|
if (elements) {
|
|
39
42
|
for (const el of elements) {
|
|
40
|
-
this.
|
|
43
|
+
if (this.toElementFn)
|
|
44
|
+
this.add(this.toElementFn(el));
|
|
45
|
+
else
|
|
46
|
+
this.add(el);
|
|
41
47
|
}
|
|
42
48
|
}
|
|
43
49
|
}
|
|
44
|
-
_comparator = (a, b) => {
|
|
45
|
-
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
46
|
-
throw new Error('The a, b params of compare function must be number');
|
|
47
|
-
}
|
|
48
|
-
else {
|
|
49
|
-
return a - b;
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
/**
|
|
53
|
-
* The function returns the value of the _comparator property.
|
|
54
|
-
* @returns The `_comparator` property is being returned.
|
|
55
|
-
*/
|
|
56
|
-
get comparator() {
|
|
57
|
-
return this._comparator;
|
|
58
|
-
}
|
|
59
50
|
_elements = [];
|
|
60
51
|
/**
|
|
61
52
|
* The function returns an array of elements.
|
|
62
|
-
* @returns The
|
|
53
|
+
* @returns The element array is being returned.
|
|
63
54
|
*/
|
|
64
55
|
get elements() {
|
|
65
56
|
return this._elements;
|
|
@@ -86,11 +77,6 @@ export class Heap extends IterableElementBase {
|
|
|
86
77
|
static heapify(elements, options) {
|
|
87
78
|
return new Heap(elements, options);
|
|
88
79
|
}
|
|
89
|
-
/**
|
|
90
|
-
* Time Complexity: O(log n)
|
|
91
|
-
* Space Complexity: O(1)
|
|
92
|
-
* where n is the number of elements in the heap.
|
|
93
|
-
*/
|
|
94
80
|
/**
|
|
95
81
|
* Time Complexity: O(log n)
|
|
96
82
|
* Space Complexity: O(1)
|
|
@@ -102,16 +88,11 @@ export class Heap extends IterableElementBase {
|
|
|
102
88
|
this._elements.push(element);
|
|
103
89
|
return this._bubbleUp(this.elements.length - 1);
|
|
104
90
|
}
|
|
105
|
-
/**
|
|
106
|
-
* Time Complexity: O(log n)
|
|
107
|
-
* Space Complexity: O(1)
|
|
108
|
-
* where n is the number of elements in the heap.
|
|
109
|
-
*/
|
|
110
91
|
/**
|
|
111
92
|
* Time Complexity: O(log n)
|
|
112
93
|
* Space Complexity: O(1)
|
|
113
94
|
*
|
|
114
|
-
* Remove and return the top element (smallest or largest element) from the heap.
|
|
95
|
+
* Remove and return the top element (the smallest or largest element) from the heap.
|
|
115
96
|
* @returns The top element or undefined if the heap is empty.
|
|
116
97
|
*/
|
|
117
98
|
poll() {
|
|
@@ -125,10 +106,6 @@ export class Heap extends IterableElementBase {
|
|
|
125
106
|
}
|
|
126
107
|
return value;
|
|
127
108
|
}
|
|
128
|
-
/**
|
|
129
|
-
* Time Complexity: O(1)
|
|
130
|
-
* Space Complexity: O(1)
|
|
131
|
-
*/
|
|
132
109
|
/**
|
|
133
110
|
* Time Complexity: O(1)
|
|
134
111
|
* Space Complexity: O(1)
|
|
@@ -152,10 +129,6 @@ export class Heap extends IterableElementBase {
|
|
|
152
129
|
clear() {
|
|
153
130
|
this._elements = [];
|
|
154
131
|
}
|
|
155
|
-
/**
|
|
156
|
-
* Time Complexity: O(n)
|
|
157
|
-
* Space Complexity: O(n)
|
|
158
|
-
*/
|
|
159
132
|
/**
|
|
160
133
|
* Time Complexity: O(n)
|
|
161
134
|
* Space Complexity: O(n)
|
|
@@ -167,10 +140,6 @@ export class Heap extends IterableElementBase {
|
|
|
167
140
|
this._elements = elements;
|
|
168
141
|
return this.fix();
|
|
169
142
|
}
|
|
170
|
-
/**
|
|
171
|
-
* Time Complexity: O(n)
|
|
172
|
-
* Space Complexity: O(1)
|
|
173
|
-
*/
|
|
174
143
|
/**
|
|
175
144
|
* Time Complexity: O(n)
|
|
176
145
|
* Space Complexity: O(1)
|
|
@@ -183,12 +152,7 @@ export class Heap extends IterableElementBase {
|
|
|
183
152
|
return this.elements.includes(element);
|
|
184
153
|
}
|
|
185
154
|
/**
|
|
186
|
-
* Time Complexity:
|
|
187
|
-
* Space Complexity: O(1)
|
|
188
|
-
* 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.
|
|
189
|
-
*/
|
|
190
|
-
/**
|
|
191
|
-
* Time Complexity: O(n)
|
|
155
|
+
* Time Complexity: O(n)
|
|
192
156
|
* Space Complexity: O(1)
|
|
193
157
|
*
|
|
194
158
|
* The `delete` function removes an element from an array-like data structure, maintaining the order
|
|
@@ -215,11 +179,6 @@ export class Heap extends IterableElementBase {
|
|
|
215
179
|
}
|
|
216
180
|
return true;
|
|
217
181
|
}
|
|
218
|
-
/**
|
|
219
|
-
* Time Complexity: O(n)
|
|
220
|
-
* Space Complexity: O(log n)
|
|
221
|
-
* where log n is the height of the heap.
|
|
222
|
-
*/
|
|
223
182
|
/**
|
|
224
183
|
* Time Complexity: O(n)
|
|
225
184
|
* Space Complexity: O(log n)
|
|
@@ -254,10 +213,6 @@ export class Heap extends IterableElementBase {
|
|
|
254
213
|
_dfs(0); // Traverse starting from the root node
|
|
255
214
|
return result;
|
|
256
215
|
}
|
|
257
|
-
/**
|
|
258
|
-
* Time Complexity: O(n)
|
|
259
|
-
* Space Complexity: O(n)
|
|
260
|
-
*/
|
|
261
216
|
/**
|
|
262
217
|
* Time Complexity: O(n)
|
|
263
218
|
* Space Complexity: O(n)
|
|
@@ -268,10 +223,6 @@ export class Heap extends IterableElementBase {
|
|
|
268
223
|
toArray() {
|
|
269
224
|
return [...this.elements];
|
|
270
225
|
}
|
|
271
|
-
/**
|
|
272
|
-
* Time Complexity: O(n)
|
|
273
|
-
* Space Complexity: O(n)
|
|
274
|
-
*/
|
|
275
226
|
/**
|
|
276
227
|
* Time Complexity: O(n)
|
|
277
228
|
* Space Complexity: O(n)
|
|
@@ -280,14 +231,8 @@ export class Heap extends IterableElementBase {
|
|
|
280
231
|
* @returns A new Heap instance containing the same elements.
|
|
281
232
|
*/
|
|
282
233
|
clone() {
|
|
283
|
-
|
|
284
|
-
clonedHeap._elements = [...this.elements];
|
|
285
|
-
return clonedHeap;
|
|
234
|
+
return new Heap(this, { comparator: this.comparator, toElementFn: this.toElementFn });
|
|
286
235
|
}
|
|
287
|
-
/**
|
|
288
|
-
* Time Complexity: O(n log n)
|
|
289
|
-
* Space Complexity: O(n)
|
|
290
|
-
*/
|
|
291
236
|
/**
|
|
292
237
|
* Time Complexity: O(n log n)
|
|
293
238
|
* Space Complexity: O(n)
|
|
@@ -297,18 +242,14 @@ export class Heap extends IterableElementBase {
|
|
|
297
242
|
*/
|
|
298
243
|
sort() {
|
|
299
244
|
const visitedNode = [];
|
|
300
|
-
const cloned = this.
|
|
245
|
+
const cloned = new Heap(this, { comparator: this.comparator });
|
|
301
246
|
while (cloned.size !== 0) {
|
|
302
247
|
const top = cloned.poll();
|
|
303
|
-
if (top)
|
|
248
|
+
if (top !== undefined)
|
|
304
249
|
visitedNode.push(top);
|
|
305
250
|
}
|
|
306
251
|
return visitedNode;
|
|
307
252
|
}
|
|
308
|
-
/**
|
|
309
|
-
* Time Complexity: O(n log n)
|
|
310
|
-
* Space Complexity: O(n)
|
|
311
|
-
*/
|
|
312
253
|
/**
|
|
313
254
|
* Time Complexity: O(n log n)
|
|
314
255
|
* Space Complexity: O(n)
|
|
@@ -321,10 +262,6 @@ export class Heap extends IterableElementBase {
|
|
|
321
262
|
results.push(this._sinkDown(i, this.elements.length >> 1));
|
|
322
263
|
return results;
|
|
323
264
|
}
|
|
324
|
-
/**
|
|
325
|
-
* Time Complexity: O(n)
|
|
326
|
-
* Space Complexity: O(n)
|
|
327
|
-
*/
|
|
328
265
|
/**
|
|
329
266
|
* Time Complexity: O(n)
|
|
330
267
|
* Space Complexity: O(n)
|
|
@@ -342,7 +279,7 @@ export class Heap extends IterableElementBase {
|
|
|
342
279
|
* the filter condition specified by the `callback` function.
|
|
343
280
|
*/
|
|
344
281
|
filter(callback, thisArg) {
|
|
345
|
-
const filteredList = new Heap();
|
|
282
|
+
const filteredList = new Heap([], { toElementFn: this.toElementFn, comparator: this.comparator });
|
|
346
283
|
let index = 0;
|
|
347
284
|
for (const current of this) {
|
|
348
285
|
if (callback.call(thisArg, current, index, this)) {
|
|
@@ -353,31 +290,28 @@ export class Heap extends IterableElementBase {
|
|
|
353
290
|
return filteredList;
|
|
354
291
|
}
|
|
355
292
|
/**
|
|
356
|
-
* Time Complexity: O(n)
|
|
357
|
-
* Space Complexity: O(n)
|
|
358
|
-
*/
|
|
359
|
-
/**
|
|
360
|
-
* Time Complexity: O(n)
|
|
293
|
+
* Time Complexity: O(n log n)
|
|
361
294
|
* Space Complexity: O(n)
|
|
362
295
|
*
|
|
363
296
|
* The `map` function creates a new heap by applying a callback function to each element of the
|
|
364
297
|
* original heap.
|
|
365
|
-
* @param callback - The callback parameter is a function that will be called for each element in
|
|
366
|
-
*
|
|
367
|
-
* and the
|
|
368
|
-
*
|
|
369
|
-
*
|
|
370
|
-
*
|
|
371
|
-
*
|
|
298
|
+
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
299
|
+
* the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
|
|
300
|
+
* element), and `this` (the heap itself). The callback function should return a value of
|
|
301
|
+
* @param comparator - The `comparator` parameter is a function that defines the order of the
|
|
302
|
+
* elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
|
|
303
|
+
* if `a` should be placed before `b`, a positive number if `a` should be placed after
|
|
304
|
+
* @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
|
|
305
|
+
* element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
|
|
306
|
+
* returns a value of type `T`. This function is used to transform the elements of the original
|
|
372
307
|
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
373
|
-
* specify the value of `this` within the callback function. It is used
|
|
374
|
-
*
|
|
375
|
-
*
|
|
376
|
-
* @returns a new instance of the Heap class
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
const mappedHeap = new Heap([], { comparator: comparator });
|
|
308
|
+
* specify the value of `this` within the callback function. It is used to set the context or scope
|
|
309
|
+
* in which the callback function will be executed. If `thisArg` is provided, it will be used as the
|
|
310
|
+
* value of
|
|
311
|
+
* @returns a new instance of the `Heap` class with the mapped elements.
|
|
312
|
+
*/
|
|
313
|
+
map(callback, comparator, toElementFn, thisArg) {
|
|
314
|
+
const mappedHeap = new Heap([], { comparator, toElementFn });
|
|
381
315
|
let index = 0;
|
|
382
316
|
for (const el of this) {
|
|
383
317
|
mappedHeap.add(callback.call(thisArg, el, index, this));
|
|
@@ -385,6 +319,24 @@ export class Heap extends IterableElementBase {
|
|
|
385
319
|
}
|
|
386
320
|
return mappedHeap;
|
|
387
321
|
}
|
|
322
|
+
_DEFAULT_COMPARATOR = (a, b) => {
|
|
323
|
+
if (typeof a === 'object' || typeof b === 'object') {
|
|
324
|
+
throw TypeError(`When comparing object types, a custom comparator must be defined in the constructor's options parameter.`);
|
|
325
|
+
}
|
|
326
|
+
if (a > b)
|
|
327
|
+
return 1;
|
|
328
|
+
if (a < b)
|
|
329
|
+
return -1;
|
|
330
|
+
return 0;
|
|
331
|
+
};
|
|
332
|
+
_comparator = this._DEFAULT_COMPARATOR;
|
|
333
|
+
/**
|
|
334
|
+
* The function returns the value of the _comparator property.
|
|
335
|
+
* @returns The `_comparator` property is being returned.
|
|
336
|
+
*/
|
|
337
|
+
get comparator() {
|
|
338
|
+
return this._comparator;
|
|
339
|
+
}
|
|
388
340
|
/**
|
|
389
341
|
* The function `_getIterator` returns an iterable iterator for the elements in the class.
|
|
390
342
|
*/
|
|
@@ -393,10 +345,6 @@ export class Heap extends IterableElementBase {
|
|
|
393
345
|
yield element;
|
|
394
346
|
}
|
|
395
347
|
}
|
|
396
|
-
/**
|
|
397
|
-
* Time Complexity: O(log n)
|
|
398
|
-
* Space Complexity: O(1)
|
|
399
|
-
*/
|
|
400
348
|
/**
|
|
401
349
|
* Time Complexity: O(log n)
|
|
402
350
|
* Space Complexity: O(1)
|
|
@@ -417,10 +365,6 @@ export class Heap extends IterableElementBase {
|
|
|
417
365
|
this.elements[index] = element;
|
|
418
366
|
return true;
|
|
419
367
|
}
|
|
420
|
-
/**
|
|
421
|
-
* Time Complexity: O(log n)
|
|
422
|
-
* Space Complexity: O(1)
|
|
423
|
-
*/
|
|
424
368
|
/**
|
|
425
369
|
* Time Complexity: O(log n)
|
|
426
370
|
* Space Complexity: O(1)
|
|
@@ -529,10 +473,6 @@ export class FibonacciHeap {
|
|
|
529
473
|
this._min = undefined;
|
|
530
474
|
this._size = 0;
|
|
531
475
|
}
|
|
532
|
-
/**
|
|
533
|
-
* Time Complexity: O(1)
|
|
534
|
-
* Space Complexity: O(1)
|
|
535
|
-
*/
|
|
536
476
|
/**
|
|
537
477
|
* Time Complexity: O(1)
|
|
538
478
|
* Space Complexity: O(1)
|
|
@@ -544,10 +484,6 @@ export class FibonacciHeap {
|
|
|
544
484
|
add(element) {
|
|
545
485
|
return this.push(element);
|
|
546
486
|
}
|
|
547
|
-
/**
|
|
548
|
-
* Time Complexity: O(1)
|
|
549
|
-
* Space Complexity: O(1)
|
|
550
|
-
*/
|
|
551
487
|
/**
|
|
552
488
|
* Time Complexity: O(1)
|
|
553
489
|
* Space Complexity: O(1)
|
|
@@ -567,10 +503,6 @@ export class FibonacciHeap {
|
|
|
567
503
|
this._size++;
|
|
568
504
|
return this;
|
|
569
505
|
}
|
|
570
|
-
/**
|
|
571
|
-
* Time Complexity: O(1)
|
|
572
|
-
* Space Complexity: O(1)
|
|
573
|
-
*/
|
|
574
506
|
/**
|
|
575
507
|
* Time Complexity: O(1)
|
|
576
508
|
* Space Complexity: O(1)
|
|
@@ -639,7 +571,7 @@ export class FibonacciHeap {
|
|
|
639
571
|
* Time Complexity: O(log n)
|
|
640
572
|
* Space Complexity: O(1)
|
|
641
573
|
*
|
|
642
|
-
* Remove and return the top element (smallest or largest element) from the heap.
|
|
574
|
+
* Remove and return the top element (the smallest or largest element) from the heap.
|
|
643
575
|
* @returns The top element or undefined if the heap is empty.
|
|
644
576
|
*/
|
|
645
577
|
poll() {
|
|
@@ -653,7 +585,7 @@ export class FibonacciHeap {
|
|
|
653
585
|
* Time Complexity: O(log n)
|
|
654
586
|
* Space Complexity: O(1)
|
|
655
587
|
*
|
|
656
|
-
* Remove and return the top element (smallest or largest element) from the heap.
|
|
588
|
+
* Remove and return the top element (the smallest or largest element) from the heap.
|
|
657
589
|
* @returns The top element or undefined if the heap is empty.
|
|
658
590
|
*/
|
|
659
591
|
pop() {
|
|
@@ -764,8 +696,8 @@ export class FibonacciHeap {
|
|
|
764
696
|
/**
|
|
765
697
|
* Time Complexity: O(1)
|
|
766
698
|
* Space Complexity: O(1)
|
|
767
|
-
|
|
768
|
-
* Remove and return the top element (smallest or largest element) from the heap.
|
|
699
|
+
*
|
|
700
|
+
* Remove and return the top element (the smallest or largest element) from the heap.
|
|
769
701
|
* @param node - The node to be removed.
|
|
770
702
|
* @protected
|
|
771
703
|
*/
|
|
@@ -785,7 +717,7 @@ export class FibonacciHeap {
|
|
|
785
717
|
* Time Complexity: O(1)
|
|
786
718
|
* Space Complexity: O(1)
|
|
787
719
|
*
|
|
788
|
-
* Remove and return the top element (smallest or largest element) from the heap.
|
|
720
|
+
* Remove and return the top element (the smallest or largest element) from the heap.
|
|
789
721
|
* @param y
|
|
790
722
|
* @param x
|
|
791
723
|
* @protected
|
|
@@ -806,7 +738,7 @@ export class FibonacciHeap {
|
|
|
806
738
|
* Time Complexity: O(n log n)
|
|
807
739
|
* Space Complexity: O(n)
|
|
808
740
|
*
|
|
809
|
-
* Remove and return the top element (smallest or largest element) from the heap.
|
|
741
|
+
* Remove and return the top element (the smallest or largest element) from the heap.
|
|
810
742
|
* @protected
|
|
811
743
|
*/
|
|
812
744
|
_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
|
|
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
|
}
|
|
@@ -7,19 +7,88 @@ import { Heap } from './heap';
|
|
|
7
7
|
* 5. Managing Dynamic Data Sets: Heaps effectively manage dynamic data sets, especially when frequent access to the largest or smallest elements is required.
|
|
8
8
|
* 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.
|
|
9
9
|
* 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
|
|
10
|
-
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum
|
|
10
|
+
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum-spanning tree algorithm, which use heaps to improve performance.
|
|
11
11
|
*/
|
|
12
12
|
export class MaxHeap extends Heap {
|
|
13
|
-
constructor(elements = [], options
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
13
|
+
constructor(elements = [], options) {
|
|
14
|
+
super(elements, {
|
|
15
|
+
comparator: (a, b) => {
|
|
16
|
+
if (typeof a === 'object' || typeof b === 'object') {
|
|
17
|
+
throw TypeError(`When comparing object types, a custom comparator must be defined in the constructor's options parameter.`);
|
|
18
|
+
}
|
|
19
|
+
if (a < b)
|
|
20
|
+
return 1;
|
|
21
|
+
if (a > b)
|
|
22
|
+
return -1;
|
|
23
|
+
return 0;
|
|
24
|
+
},
|
|
25
|
+
...options
|
|
26
|
+
});
|
|
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);
|
|
20
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++;
|
|
21
91
|
}
|
|
22
|
-
|
|
23
|
-
super(elements, options);
|
|
92
|
+
return mappedHeap;
|
|
24
93
|
}
|
|
25
94
|
}
|
|
@@ -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.
|
|
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.
|
|
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
|
}
|