Skip to content

Commit e8d78af

Browse files
committed
Add heapify method
1 parent e120d12 commit e8d78af

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

data_structures/non_linear/trees/heaps/max_binary_heap.dart

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ import '../../../../utils/list_extension.dart';
55
class MaxBinaryHeap {
66
final List<num> _heap = [];
77

8+
/// (Converting an array into a MaxBinaryHeap)
9+
/// Pseudocode:
10+
/// Create a new heap.
11+
/// Iterate over the array and invoke your insert function.
12+
/// return the values property on the heap.
13+
List<num> heapify(List<num> array) {
14+
for (final num in array) {
15+
insert(num);
16+
}
17+
return _heap;
18+
}
19+
820
/// Pseudocode:
921
/// - Push the value into the values property on the heap.
1022
/// - Sift-Up:
@@ -81,7 +93,7 @@ void main() {
8193
// 41
8294
// 39 33
8395
// 18 27 12
84-
heap._heap.addAll([41, 39, 33, 18, 27, 12]);
96+
heap.heapify([41, 39, 33, 18, 27, 12]);
8597

8698
// 55
8799
// 39 41
@@ -103,7 +115,7 @@ void main() {
103115
// 41
104116
// 39 33
105117
// 18 27 12
106-
heap._heap.addAll([41, 39, 33, 18, 27, 12]);
118+
heap.heapify([41, 39, 33, 18, 27, 12]);
107119

108120
// 39
109121
// 27 33
@@ -118,4 +130,14 @@ void main() {
118130
expect(heap.extract(), isNull);
119131
});
120132
});
133+
134+
group('heapify', () {
135+
test('should convert the list into MaxBinaryHeap', () {
136+
final heap = MaxBinaryHeap();
137+
// 6
138+
// 5 3
139+
// 1 2
140+
expect(heap.heapify([2, 3, 6, 1, 5]), [6, 5, 3, 1, 2]);
141+
});
142+
});
121143
}

data_structures/non_linear/trees/heaps/min_binary_heap.dart

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ import '../../../../utils/list_extension.dart';
55
class MinBinaryHeap {
66
final List<num> _heap = [];
77

8+
/// (Converting an array into a MinBinaryHeap)
9+
/// Pseudocode:
10+
/// Create a new heap.
11+
/// Iterate over the array and invoke your insert function.
12+
/// return the values property on the heap.
13+
List<num> heapify(List<num> array) {
14+
for (final num in array) {
15+
insert(num);
16+
}
17+
return _heap;
18+
}
19+
820
/// Pseudocode:
921
/// - Push the value into the values property on the heap.
1022
/// - Sift-Up:
@@ -81,7 +93,7 @@ void main() {
8193
// 6
8294
// 12 15
8395
// 18 16 20
84-
heap._heap.addAll([6, 12, 15, 18, 16, 20]);
96+
heap.heapify([6, 12, 15, 18, 16, 20]);
8597

8698
// 3
8799
// 4 6
@@ -103,7 +115,7 @@ void main() {
103115
// 6
104116
// 12 15
105117
// 18 16 20
106-
heap._heap.addAll([6, 12, 15, 18, 16, 20]);
118+
heap.heapify([6, 12, 15, 18, 16, 20]);
107119

108120
// 12
109121
// 16 15
@@ -118,4 +130,14 @@ void main() {
118130
expect(heap.extract(), isNull);
119131
});
120132
});
133+
134+
group('heapify', () {
135+
test('should convert the list into MinBinaryHeap', () {
136+
final heap = MinBinaryHeap();
137+
// 1
138+
// 2 5
139+
// 6 3
140+
expect(heap.heapify([6, 5, 3, 1, 2]), [1, 2, 5, 6, 3]);
141+
});
142+
});
121143
}

0 commit comments

Comments
 (0)