-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.cpp
executable file
·225 lines (191 loc) · 4.85 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
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
// C++ code to depict
// the implementation of a max heap.
#include <bits/stdc++.h>
using namespace std;
// A class for Max Heap.
class MaxHeap
{
// A pointer pointing to the elements
// in the array in the heap.
int *arr;
// Maximum possible size of
// the Max Heap.
int maxSize;
// Number of elements in the
// Max heap currently.
int heapSize;
public:
// Constructor function.
MaxHeap(int maxSize);
// Heapifies a sub-tree taking the
// given index as the root.
void MaxHeapify(int);
// Returns the index of the parent
// of the element at ith index.
int parent(int i)
{
return (i - 1) / 2;
}
// Returns the index of the left child.
int lChild(int i)
{
return (2 * i + 1);
}
// Returns the index of the
// right child.
int rChild(int i)
{
return (2 * i + 2);
}
// Removes the root which in this
// case contains the maximum element.
int removeMax();
// Increases the value of the key
// given by index i to some new value.
void increaseKey(int i, int newVal);
// Returns the maximum key
// (key at root) from max heap.
int getMax()
{
return arr[0];
}
int curSize()
{
return heapSize;
}
// Deletes a key at given index i.
void deleteKey(int i);
// Inserts a new key 'x' in the Max Heap.
void insertKey(int x);
};
// Constructor function builds a heap
// from a given array a[]
// of the specified size.
MaxHeap::MaxHeap(int totSize)
{
heapSize = 0;
maxSize = totSize;
arr = new int[totSize];
}
// Inserting a new key 'x'.
void MaxHeap::insertKey(int x)
{
// To check whether the key
// can be inserted or not.
if (heapSize == maxSize)
{
cout << "\nOverflow: Could not insertKey\n";
return;
}
// The new key is initially
// inserted at the end.
heapSize++;
int i = heapSize - 1;
arr[i] = x;
// The max heap property is checked
// and if violation occurs,
// it is restored.
while (i != 0 && arr[parent(i)] < arr[i])
{
swap(arr[i], arr[parent(i)]);
i = parent(i);
}
}
// Increases value of key at
// index 'i' to new_val.
void MaxHeap::increaseKey(int i, int newVal)
{
arr[i] = newVal;
while (i != 0 && arr[parent(i)] < arr[i])
{
swap(arr[i], arr[parent(i)]);
i = parent(i);
}
}
// To remove the root node which contains
// the maximum element of the Max Heap.
int MaxHeap::removeMax()
{
// Checking whether the heap array
// is empty or not.
if (heapSize <= 0)
return INT_MIN;
if (heapSize == 1)
{
heapSize--;
return arr[0];
}
// Storing the maximum element
// to remove it.
int root = arr[0];
arr[0] = arr[heapSize - 1];
heapSize--;
// To restore the property
// of the Max heap.
MaxHeapify(0);
return root;
}
// In order to delete a key
// at a given index i.
void MaxHeap::deleteKey(int i)
{
// It increases the value of the key
// to infinity and then removes
// the maximum value.
increaseKey(i, INT_MAX);
removeMax();
}
// To heapify the subtree this method
// is called recursively
void MaxHeap::MaxHeapify(int i)
{
int l = lChild(i);
int r = rChild(i);
int largest = i;
if (l < heapSize && arr[l] > arr[i])
largest = l;
if (r < heapSize && arr[r] > arr[largest])
largest = r;
if (largest != i)
{
swap(arr[i], arr[largest]);
MaxHeapify(largest);
}
}
// Driver program to test above functions.
int main()
{
// Assuming the maximum size of the heap to be 15.
MaxHeap h(15);
// Asking the user to input the keys:
int k, i, n = 6, arr[10];
cout << "Entered 6 keys:- 3, 10, 12, 8, 2, 14 \n";
h.insertKey(3);
h.insertKey(10);
h.insertKey(12);
h.insertKey(8);
h.insertKey(2);
h.insertKey(14);
// Printing the current size
// of the heap.
cout << "The current size of the heap is "
<< h.curSize() << "\n";
// Printing the root element which is
// actually the maximum element.
cout << "The current maximum element is " << h.getMax()
<< "\n";
// Deleting key at index 2.
h.deleteKey(2);
// Printing the size of the heap
// after deletion.
cout << "The current size of the heap is "
<< h.curSize() << "\n";
// Inserting 2 new keys into the heap.
h.insertKey(15);
h.insertKey(5);
cout << "The current size of the heap is "
<< h.curSize() << "\n";
cout << "The current maximum element is " << h.getMax()
<< "\n";
return 0;
}