-
Notifications
You must be signed in to change notification settings - Fork 0
/
minheap.cpp
117 lines (101 loc) · 1.94 KB
/
minheap.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include<iostream>
using namespace std;
#define MAX 10000
int ceil(int val1, int val2){
if((double)val1/(double)val2==(double)(val1/val2))
return (int)(val1/val2);
return (int)(val1/val2)+1;
}
int swap(int &a, int &b){
int temp=a;
a=b;
b=temp;
}
void print(int arr[], int size){
for(int i=0;i<size;i++)
cout<<arr[i]<<" ";
cout<<"\n";
}
class priority_queue{
int arr[MAX];
int size;
void heapify(int arr[], int size){
int parent=ceil((size-1),2);
int child=size;
while(parent>0){
//check if parent is greater than inserted val
if(arr[parent-1]>arr[child-1]){
swap(arr[parent-1], arr[child-1]);
}
child=parent;
parent=ceil((parent-1),2);
}
}
void heapify_pop(int arr[], int size, int index){
//check smallest amongst all the three i.e parent, left and right child and
//then replace with the root/parent and move in that direction
while(1){
if(2*index+1>=size||index==-1)
break;
int smallest=arr[index];
int direction=-1;
int f=0, g=0;
if(2*index+1<size){
// left child exists
if(arr[index]>arr[2*index+1]){
g=1;
smallest=arr[2*index+1];
direction=2*index+1;
}
if(2*index+2<size){
// right child exists
if(smallest>arr[2*index+2]){
f=1;
smallest=arr[2*index+2];
direction=2*index+2;
swap(arr[index], arr[2*index+2]);
}
}
if(f==0&&g==1)
swap(arr[index], arr[2*index+1]);
index=direction;
}
}
}
public:
priority_queue(){
size=0;
}
void push(int val){
//heapify
arr[size]=val;
size++;
heapify(arr,size);
}
void pop(){
//replace 0th index with size-1 and initiate heapify
//decrease size of heap
arr[0]=arr[size-1];
size--;
heapify_pop(arr, size, 0);
}
void top(){
if(size>0)
cout<<"\nSIZE="<<size<<" "<<arr[0]<<"\n";
else
cout<<"Heap is empty\n";
}
};
int main(){
priority_queue q;
q.top();
q.push(4);
q.push(2);
q.push(0);
q.push(5);
q.pop();
q.top();
q.pop();
q.top();
return 0;
}