-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1705069_online.cpp
199 lines (148 loc) · 3.16 KB
/
1705069_online.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include<iostream>
using namespace std;
#define INFINTITY 999999;
template<class T>
class PriorityQueue{
T *arr;
int size, length;
void MaxHeapify(int i){
int l = lchild(i);
int r = rchild(i);
int largest = i;
if(l<length && arr[l]>arr[largest])
largest = l;
else
largest = i;
if(r<length && arr[r]>arr[largest])
largest = r;
if(largest != i){
swap(arr[i],arr[largest]);
MaxHeapify(largest);
}
}
void BuildMaxHeap(){
for(int i = length/2 - 1; i>=0 ; i--){
MaxHeapify(i);
}
}
int parent(int i){return (i-1)/2;}
int lchild(int i){return 2*i + 1;}
int rchild(int i){return 2*i + 2;}
void resize(){
}
public:
PriorityQueue(int size){
arr = new T[size];
this->size = size;
this->length = 0;
}
void Insert(T &elem){
arr[length++] = -INFINTITY;
IncreaseKey(length-1,elem);
}
T FindMax(){
return arr[0];
}
T ExtractMax(){
if (this->length <= 0 ){
cout<<endl<<"Heap Underflow"<<endl;
return -INFINTITY;
}
T max = arr[0];
arr[0] = arr[length-1];
length--;
MaxHeapify(0);
return max;
}
void IncreaseKey(int i,T &newKey){
if (newKey < arr[i]){
cout<<"New key is smaller than current key"<<endl;
return;
}
arr[i] = newKey;
while (i>0 && arr[parent(i)] < arr[i]){
swap(arr[i],arr[parent(i)]);
i = parent(i);
}
}
void DecreaseKey(int i,T newKey){
if (newKey > arr[i]){
cout<<"New key is larger than current key"<<endl;
return;
}
arr[i] = newKey;
MaxHeapify(i);
}
void Print(){
cout<<"Capacity: "<<this->size<<endl<<"Current length: "<<this->length<<endl;
cout<<"Elements: ";
for(int i = 0 ; i<length; i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}
int count (T val, int i = 0){
if (arr[i] <= val && i < length)
return 0;
else if (i >= length/2)
return 1;
else
return 1 + count(val,lchild(i)) + count (val, rchild(i));
}
};
int main(){
int ch;
PriorityQueue<int> pq(100);
bool quit = false;
while(!quit){
cout<<"1. Insert"<<endl;
cout<<"2. FindMax"<<endl;
cout<<"3. ExtractMax"<<endl;
cout<<"4. IncreaseKey"<<endl;
cout<<"5. DecreaseKey"<<endl;
cout<<"6. Print"<<endl;
cout<<"7. Number of elements larger"<<endl;
cout<<"8. Exit"<<endl<<endl;
cout<<"Enter your choice: ";
cin>>ch;
switch(ch){
case 1:
cout<<"Enter your element: ";
int num;
cin>>num;
pq.Insert(num);
break;
case 2:
cout<<"Max element: "<<pq.FindMax()<<endl;
break;
case 3:
cout<<"Max extracted: "<<pq.ExtractMax()<<endl;\
break;
case 4:
cout<<"Enter the index & new key: ";
int index, newKey;
cin>>index>>newKey;
pq.IncreaseKey(index,newKey);
break;
case 5:
cout<<"Enter the index & new key: ";
cin>>index>>newKey;
pq.DecreaseKey(index,newKey);
break;
case 6:
pq.Print();
break;
case 8:
quit = true;
break;
case 7:
int number;
cout<<"Enter a number: ";
cin>>number;
cout<<"Number of elements larger: "<<pq.count(number)<<endl;;
break;
}
}
return 0;
}
//1 12 1 6 1 7 1 3 1 5 1 4 1 3 1 2 1 1 1 0 1 -1