-
Notifications
You must be signed in to change notification settings - Fork 0
/
priorityQ.cpp
109 lines (101 loc) · 2.54 KB
/
priorityQ.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
#include <iostream>
#include <vector>
#include <climits>
using namespace std;
void swap(int &a,int &b) {
int temp = a;
a = b;
b = temp;
}
void heapify(vector<int> &arr,int i) {
int left = 2*i + 1;
int right = 2*i + 2;
int small = i;
int n = arr.size();
if(left < n && arr[left] < arr[small])
small = left;
if(right < n && arr[right] < arr[small])
small = right;
if(small != i) {
swap(arr[i],arr[small]);
heapify(arr,small);
}
}
int getMin(vector<int> &arr) {
if(arr.size() > 0)
return arr[0];
else
return INT_MAX;
}
int extractMin(vector<int> &arr) {
int n = arr.size();
if(n<1)
return INT_MAX;
int min = arr[0];
swap(arr[0],arr[n-1]);
arr.erase(arr.end()-1);
heapify(arr,0);
return min;
}
void decreaseKey(vector<int> &arr,int i, int key) {
if(i<0) {
cout<<"Invalid index\n";
return;
}
if(key >= arr[i]) {
cout<<"Key greater than existing key\n";
return;
}
arr[i] = key;
int parent = (i-1)/2;
while(i>0 && arr[parent] > arr[i]) {
swap(arr[parent],arr[i]);
i = parent;
parent = (i-1)/2;
}
}
void insertIntoQ(vector<int> &arr,int key) {
arr.push_back(INT_MAX);
decreaseKey(arr,arr.size()-1,key);
}
void traverse(vector<int> &arr) {
int n = arr.size();
for(int i=0; i<n; i++)
cout<<arr[i]<<" ";
cout<<endl;
}
int main() {
int ch,data,index,res;
vector<int> arr;
while(1) {
cout<<"1. Insert 2. GetMinimum 3. ExtractMin 4. DecreaseKey 5. Traverse 6. Exit\n";
cin>>ch;
switch(ch) {
case 1: cout<<"Enter data\n";
cin>>data;
insertIntoQ(arr,data);
break;
case 2: res = getMin(arr);
if(res == INT_MAX)
cout<<"Priority Queue empty\n";
else
cout<<res;
break;
case 3: res = extractMin(arr);
if(res == INT_MAX)
cout<<"Priority Queue empty\n";
else
cout<<res;
break;
case 4: cout<<"Enter index and new key\n";
cin>>index>>data;
decreaseKey(arr,index,data);
break;
case 5: traverse(arr);
break;
}
if(ch == 6)
break;
}
return 0;
}