-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaxHeap.cpp
260 lines (222 loc) Β· 9.17 KB
/
MaxHeap.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include <iostream>
#include <climits>
using namespace std;
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
// Prototype of a utility function to swap two integers
void Swap(int* x, int* y);
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
// A class for Max Heap
class MaxHeap{
int* harr; // pointer to array of elements in heap
int capacity; // maximum possible size of max heap
int HeapSize; // Current number of elements in ma heap
public:
// Constructor
MaxHeap(int capacity);
// to heapify a subtree with the root at given index
void MaxHeapify(int);
int parent(int i){
return (i-1)/2;
}
// to get index of left child of node at index i
int left(int i){
return (2*i + 1);
}
// to get index of right child of node at index i
int right(int i){
return (2*i + 2);
}
// to extract the root which is the maximum element
int ExtractMax();
// Increases key value of key at index i to new_val
void IncreaseKey(int i, int NewVal);
// Returns the maximum key (key at root) from max heap
int GetMax(){
return harr[0];
}
// Deletes a key stored at index i
void DeleteKey(int i);
// Inserts a new key 'k'
void InsertKey(int k);
void HeapSort();
void NthMax(int n);
void Display();
};
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
// Constructor: Builds a heap from a given array a[] of given size
MaxHeap::MaxHeap(int cap){
HeapSize = 0;
capacity = cap;
harr = new int[cap];
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
// Inserts a new key 'k'
void MaxHeap::InsertKey(int k){
if (HeapSize == capacity){
cout << "\nOverflow : Could not Insert the Key\n";
return;
}
// First insert the new key at the end
HeapSize++;
int i = HeapSize - 1;
harr[i] = k;
// Fix the max heap property if it is violated
while (i != 0 && harr[parent(i)] > harr[i]){
Swap(&harr[i], &harr[parent(i)]);
i = parent(i);
}
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
// Increases value of key at index 'i' to new_val. It is assumed that new_val is larger than harr[i].
void MaxHeap::IncreaseKey(int i, int NewVal){
harr[i] = NewVal;
while (i != 0 && harr[parent(i)] > harr[i]){
Swap(&harr[i], &harr[parent(i)]);
i = parent(i);
}
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
// Method to remove maximum element (or root) from max heap
int MaxHeap::ExtractMax(){
if (HeapSize <= 0)
return INT_MIN;
if (HeapSize == 1){
HeapSize--;
return harr[0];
}
// Store the maximum value, and remove it from heap
int root = harr[0];
harr[0] = harr[HeapSize-1];
HeapSize--;
MaxHeapify(0);
return root;
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
// This function deletes key at index i. It first reduced value to infinite, then calls extractMax()
void MaxHeap::DeleteKey(int i){
IncreaseKey(i, INT_MAX);
ExtractMax();
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
// A recursive method to heapify a subtree with the root at given index
// This method assumes that the subtrees are already heapified
void MaxHeap::MaxHeapify(int i){
int l = left(i);
int r = right(i);
int largest = i;
if (l < HeapSize && harr[l] > harr[i])
largest = l;
if (r < HeapSize && harr[r] > harr[largest])
largest = r;
if (largest != i){
Swap(&harr[i], &harr[largest]);
MaxHeapify(largest);
}
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
void MaxHeap::HeapSort(){
int x = HeapSize;
for (int i = HeapSize - 1; i >= 0; i--) {
// Move current root to end
cout << harr[0] << " ";
Swap(&harr[0], &harr[i]);
HeapSize--;
// call max heapify on the reduced heap
MaxHeapify(0);
}
HeapSize = x;
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
void MaxHeap::NthMax(int n){
int x = HeapSize;
for (int i = n - 1; i >= 0; i--) {
// Move current root to end
cout << harr[0] << " ";
Swap(&harr[0], &harr[i]);
HeapSize--;
// call max heapify on the reduced heap
MaxHeapify(0);
}
cout << n;
DeleteKey(0);
HeapSize = x;
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
void MaxHeap::Display(){
for (int i = 0; i < HeapSize; ++i)
cout << harr[i] << " ";
cout << "\n";
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
// A utility function to swap two elements
void Swap(int *x, int *y){
int temp = *x;
*x = *y;
*y = temp;
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
// Driver program to test above functions
int main(){
int size;
cout << "Enter the Maximum Capacity of the Heap : ";
cin >> size;
MaxHeap h(size);
int ch, a, b, c, d, n;
cout << "\n1 βββββ> Insert Key \n";
cout << "2 βββββ> Delete Key \n";
cout << "3 βββββ> Increase Key \n";
cout << "4 βββββ> Extract Max \n";
cout << "5 βββββ> Get Max \n";
cout << "6 βββββ> Heap Sort \n";
cout << "7 βββββ> Delete the nth Maximum Element \n";
cout << "8 βββββ> Display \n";
cout << "9 βββββ> Exit \n";
do{
cout << "\nEnter your choice : ";
cin >> ch;
switch(ch){
case (1):
cout << "Enter the Key to be Inserted to the Max Heap : ";
cin >> a;
h.InsertKey(a);
break;
case (2):
cout << "Enter the Index to be Deleted from the Max Heap : ";
cin >> b;
h.DeleteKey(b);
break;
case (3):
cout << "Which Index do you want to Increase : ";
cin >> c;
cout << "Inrease to : ";
cin >> d;
h.IncreaseKey(c, d);
break;
case (4):
cout << "Extract Max : " << h.ExtractMax() << endl;
break;
case (5):
cout << "Get Max : " << h.GetMax() << endl;
break;
case (6):
cout << "Heap Sort : ";
h.HeapSort();
cout << endl;
break;
case (7):
cout << "Enter the value of n : ";
cin >> n;
h.NthMax(n);
cout << endl;
break;
case (8):
h.Display();
break;
case (9):
break;
default:
cout << "Enter a value between 1 and 9";
}
}while(ch != 9);
return 0;
}