-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeap.cpp
More file actions
271 lines (226 loc) · 5.48 KB
/
Heap.cpp
File metadata and controls
271 lines (226 loc) · 5.48 KB
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
261
262
263
264
265
266
267
268
269
270
271
// A C++ program to demonstrate common Binary Heap Operations
#include<iostream>
#include<climits>
using namespace std;
struct heaps{
int *vetor;
int tamMax;
int tamAtual;
};
void inicializaHeap(int cap, heaps* heap){
heap->tamAtual = 0;
heap->tamMax = cap;
heap->vetor = new int[cap];
}
int pai(int i){
return (i-1)/2;
}
int filhoEsq(int i){
return (2*i + 1);
}
int filhoDir(int i){
return (2*i + 2);
}
int getMin(heaps* heap){
return heap->vetor[0];
}
void troca(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
void inserir(heaps* heap, int chave){
if(heap->tamAtual == heap->tamMax){
cout<<"\nTamanho maximo atingido, chave nao pode ser inserida\n";
return;
}
heap->tamAtual++;
int i = heap->tamAtual - 1;
heap->vetor[i] = chave;
while(i != 0 && heap->vetor[pai(i)] > heap->vetor[i]){
troca(&heap->vetor[i], &heap->vetor[pai(i)]);
i = pai(i);
}
}
void decreaseKey(int i, int valor, heaps* heap){
heap->vetor[i] = valor;
while(i != 0 && heap->vetor[pai(i)] > heap->vetor[i]){
troca(&heap->vetor[i], &heap->vetor[pai(i)]);
i = pai(i);
}
}
void minHeapify(int i, heaps* heap){
int esq = filhoEsq(i);
int dir = filhoDir(i);
int menor = i;
if(esq < heap->tamAtual && heap->vetor[esq] < heap->vetor[i])
menor = i;
if(dir < heap->tamAtual && heap->vetor[dir] < heap->vetor[menor])
menor = dir;
if(menor != i){
troca(&heap->vetor[i], &heap->vetor[menor]);
minHeapify(menor, heap);
}
}
int extractMin(heaps* heap)
{
if(heap->tamAtual <= 0)
return INT_MAX;
if(heap->tamAtual == 1){
heap->tamAtual --;
return heap->vetor[0];
}
int raiz = heap->vetor[0];
heap->vetor[0] = heap->vetor[heap->tamAtual-1];
heap->tamAtual --;
minHeapify(0, heap);
return raiz;
}
void deletar(int i, heaps* heap){
decreaseKey(i, INT_MIN, heap);
extractMin(heap);
}
/* A class for Min Heap
class MinHeap
{
int *harr; // pointer to array of elements in heap
int capacity; // maximum possible size of min heap
int heap_size; // Current number of elements in min heap
public:
// Constructor
MinHeap(int capacity);
// to heapify a subtree with the root at given index
void MinHeapify(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 minimum element
int extractMin();
// Decreases key value of key at index i to new_val
void decreaseKey(int i, int new_val);
// Returns the minimum key (key at root) from min heap
int getMin() { return harr[0]; }
// Deletes a key stored at index i
void deleteKey(int i);
// Inserts a new key 'k'
void insertKey(int k);
};
// Constructor: Builds a heap from a given array a[] of given size
MinHeap::MinHeap(int cap)
{
heap_size = 0;
capacity = cap;
harr = new int[cap];
}
// Inserts a new key 'k'
void MinHeap::insertKey(int k)
{
if (heap_size == capacity)
{
cout << "\nOverflow: Could not insertKey\n";
return;
}
// First insert the new key at the end
heap_size++;
int i = heap_size - 1;
harr[i] = k;
// Fix the min heap property if it is violated
while (i != 0 && harr[parent(i)] > harr[i])
{
swap(&harr[i], &harr[parent(i)]);
i = parent(i);
}
}
// Decreases value of key at index 'i' to new_val. It is assumed that
// new_val is smaller than harr[i].
void MinHeap::decreaseKey(int i, int new_val)
{
harr[i] = new_val;
while (i != 0 && harr[parent(i)] > harr[i])
{
swap(&harr[i], &harr[parent(i)]);
i = parent(i);
}
}
// Method to remove minimum element (or root) from min heap
int MinHeap::extractMin()
{
if (heap_size <= 0)
return INT_MAX;
if (heap_size == 1)
{
heap_size--;
return harr[0];
}
// Store the minimum value, and remove it from heap
int root = harr[0];
harr[0] = harr[heap_size-1];
heap_size--;
MinHeapify(0);
return root;
}
// This function deletes key at index i. It first reduced value to minus
// infinite, then calls extractMin()
void MinHeap::deleteKey(int i)
{
decreaseKey(i, INT_MIN);
extractMin();
}
// A recursive method to heapify a subtree with the root at given index
// This method assumes that the subtrees are already heapified
void MinHeap::MinHeapify(int i)
{
int l = left(i);
int r = right(i);
int smallest = i;
if (l < heap_size && harr[l] < harr[i])
smallest = l;
if (r < heap_size && harr[r] < harr[smallest])
smallest = r;
if (smallest != i)
{
swap(&harr[i], &harr[smallest]);
MinHeapify(smallest);
}
}
// 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()
{
MinHeap h(11);
h.insertKey(3);
h.insertKey(2);
h.deleteKey(1);
h.insertKey(15);
h.insertKey(5);
h.insertKey(4);
h.insertKey(45);
cout << h.extractMin() << " ";
cout << h.getMin() << " ";
h.decreaseKey(2, 1);
cout << h.getMin();
return 0;
}*/
int main(void){
heaps *h;
inicializaHeap(11, h);
inserir(h, 3);
inserir(h, 2);
inserir(h, 1);
inserir(h, 15);
inserir(h, 5);
inserir(h, 4);
inserir(h, 45);
for(int i=0; i<11; i++){
cout<<h->vetor[i]<<" ";
}
}