-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinHeap.cpp
245 lines (222 loc) · 6.26 KB
/
MinHeap.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
#include<stdio.h>
#define MAX_HEAP_SIZE 100000
#define MAXREAL 999999.0
class HeapItem
{
public:
int data; //actual data that is stored
float key; //key value of the data, heap is constructed based on key
};
//MinHeap class, minimum item stored at the root of heap tree
class MinHeap
{
public:
HeapItem * A; //stores heap items, e.g., nodes
int heapLength;
int * map;
MinHeap() //constructor
{
A = new HeapItem[MAX_HEAP_SIZE];
map = new int[MAX_HEAP_SIZE];
heapLength=0;
}
~MinHeap() //destructor
{
if(map) delete [] map;
if(A) delete [] A;
map = 0; //set to NULL after deletion
A = 0; //set to NULL after deletion
}
//Fills the heap with an array of integers
//key values do not maintain heap property
//May be used in some algorithms such as dijkstra's shortest path
void initialize(int v[], int n)
{
heapLength = n;
for(int i=0; i<n; i++) //nodes are stored from index 1 instead of 0 in the heap
{
A[i+1].data = v[i];
A[i+1].key = MAXREAL;
map[v[i]] = i+1; //map tracks which vertex is stored at which heap node
}
}
//this function inserts a new (data,key) pair in the heap
//call to buheapify is required
void insertItem(int data, float key)
{
//heap can store MAX_HEAP_SIZE-1 (0 index unused)
//overflow checking
if(heapLength>=MAX_HEAP_SIZE-1)
{
printf("Storage limit exceeded\n");
return;
}
A[++heapLength].data = data;
A[heapLength].key = key;
map[data] = heapLength;
buHeapify(heapLength);
}
//this function removes (and returns) the node which contains the minimum key value
HeapItem removeMin()
{
HeapItem t;
t=A[1];
A[1]=A[heapLength];
map[A[1].data]=1;
A[heapLength]=t;
map[A[heapLength].data]=0;
heapLength--;
heapify(1);
return t;
}
//The function updates the key value of an existing data
//stored in the heap
//Note that updates can result in an increase or decrease of key value
//Call to heapify or buheapify is required
void updateKey(int data, float key)
{
//Write your codes here.
int idx = map[data];
if(idx<1 || idx>heapLength)
{
printf("Not found\n");
return;
}
if(A[idx].key > key)
{
A[idx].key = key;
buHeapify(idx);
}
else if(A[idx].key < key)
{
A[idx].key = key;
heapify(idx);
}
}
//This function returns the key value of a data stored in heap
float getKey(int data)
{
int i = map[data];
return A[i].key;
}
//This function heapifies the heap
//When a key value of ith node is increased (because of update), then calling
//this function will restore heap property
void heapify(int i)
{
int l,r,smallest;
while(1)
{
l=2*i; //left child index
r=2*i+1; //right child index
smallest=i;
if(l>heapLength && r>heapLength)
break; //nothing to do, we are at bottom
else if(r>heapLength)
smallest = l;
else if(l>heapLength)
smallest = r;
else if( A[l].key < A[r].key )
smallest = l;
else
smallest = r;
if(A[i].key <= A[smallest].key)
break; //we are done heapifying
else
{
//swap nodes with smallest child, adjust map array accordingly
HeapItem t;
t=A[i];
A[i]=A[smallest];
map[A[i].data]=i;
A[smallest]=t;
map[A[smallest].data]=smallest;
i=smallest;
}
}
}
//This function heapifies the heap form bottom to up
//When a key value of ith node is decreased (because of update), then calling
//this function will restore heap property
//In addition, when a new item is inserted at the end of the heap, then
//calling this function restores heap property
void buHeapify(int i)
{
int parent = i/2;
if(A[parent].key<=A[i].key)
{
return;
}
HeapItem t;
t = A[i];
A[i] = A[parent];
map[A[i].data] = i;
A[parent] = t;
map[A[parent].data] = parent;
i = parent;
buHeapify(i);
}
void printHeap()
{
printf("Heap length: %d\n", heapLength);
for(int i=1; i<=heapLength; i++)
{
printf("(%d,%.2f) ", A[i].data, A[i].key);
}
printf("\n");
}
bool Empty()
{
if(heapLength==0)return true;
else return false;
}
};
int main()
{
int choice;
int data;
float key;
MinHeap heap;
bool exit = false;
while(!exit)
{
printf("1. Insert 2. RemoveMin 3.Update 4. Print 5. Exit. 6.Get key\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
scanf("%d%f",&data,&key);
heap.insertItem(data, key);
heap.printHeap();
break;
case 2:
if(!heap.Empty())
{
HeapItem item;
item = heap.removeMin();
printf("Removed: (%d,%.2f)\n", item.data, item.key);
}
else{
printf("No element\n");
}
heap.printHeap();
break;
case 3:
scanf("%d%f",&data,&key);
heap.updateKey(data,key);
heap.printHeap();
break;
case 4:
heap.printHeap();
break;
case 5:
exit = true;
break;
case 6:
int d;
scanf("%d", &d);
printf("Key : %f\n", heap.getKey(d));
}
}
return 0;
}