-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy pathheap.cpp
81 lines (77 loc) · 1.53 KB
/
heap.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<iostream>
#include<vector>
using namespace std;
//Max heap
void upheapify(vector<int> &heap,int idx){
if(idx==0){
return;
}
int parentidx=(idx-1)/2;
if(heap[parentidx]<heap[idx]){
swap(heap[parentidx],heap[idx]);
upheapify(heap,parentidx);
}
else{
return;
}
}
void downheapify(vector<int> &heap,int idx){
int leftidx=2*idx+1;
int rightidx=2*idx+2;
if(leftidx>=heap.size() && rightidx>=heap.size()){
return;
}
int largestidx=idx;
if(leftidx<heap.size()&&heap[leftidx]>heap[idx]){
largestidx=leftidx;
}
if(rightidx<heap.size()&&heap[rightidx]>heap[idx]){
largestidx=rightidx;
}
if(largestidx==idx){
return;
}
swap(heap[idx],heap[largestidx]);
downheapify(heap,largestidx);
}
void buildheap(vector<int> &heap){
for(int i=0;i<heap.size();i++){
upheapify(heap,i);
}
}
void buildheapOptimised(vector<int> &heap){
for(int i=heap.size()-1;i>=0;i--){
downheapify(heap,i);
}
}
//delete peak element
void deletepeek(vector<int> &heap){
swap(heap[0],heap[heap.size()-1]);
heap.pop_back();
downheapify(heap,0);
}
void insert(vector<int> &heap,int key){
heap.push_back(key);
upheapify(heap,heap.size()-1);
}
void display(vector<int> heap){
for(int i=0;i<heap.size();i++){
cout<<heap[i]<<" ";
}
}
int main(){
vector<int> heap;
int n;
cin>>n;
for(int i=0;i<n;i++){
int x;
cin>>x;
heap.push_back(x);
// insert(heap,x);
}
buildheapOptimised(heap);
display(heap);
// cout<<endl;
// deletepeek(heap);
// display(heap);
}