-
Notifications
You must be signed in to change notification settings - Fork 256
/
01 InsertinHeap.cpp
61 lines (49 loc) · 1.32 KB
/
01 InsertinHeap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <vector>
using namespace std;
// Lecture based
void InsertA(int A[], int n){
int i = n;
int temp = A[n];
while (i > 0 && temp > A[i % 2 == 0 ? (i/2)-1 : i/2]){
A[i] = A[i % 2 == 0 ? (i/2)-1 : i/2];
i = i % 2 == 0 ? (i/2)-1 : i/2;
}
A[i] = temp;
}
// STL vector based
void Insert(vector<int>& vec, int key){
// Insert key at the end
auto i = vec.size();
vec.emplace_back(key);
// Rearrange elements: Indices are not DRY :-(
while (i > 0 && key > vec[i % 2 == 0 ? (i/2)-1 : i/2]){
vec[i] = vec[i % 2 == 0 ? (i/2)-1 : i/2];
i = i % 2 == 0 ? (i/2)-1 : i/2;
}
vec[i] = key;
}
template <class T>
void Print(T& vec, int n){
cout << "Max Heap: [" << flush;
for (int i=0; i<n; i++){
cout << vec[i] << flush;
if (i < n-1){
cout << ", " << flush;
}
}
cout << "]" << endl;
}
int main() {
int a[] = {45, 35, 15, 30, 10, 12, 6, 5, 20, 50};
InsertA(a, 9);
Print(a, sizeof(a)/sizeof(a[0]));
cout << endl;
// STL based
vector<int> v = {45, 35, 15, 30, 10, 12, 6, 5, 20};
Print(v, v.size());
v.reserve(15); // Reserve space for 15 elements
Insert(v, 50);
Print(v, v.size());
return 0;
}