-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap.cpp
More file actions
218 lines (194 loc) · 4.51 KB
/
heap.cpp
File metadata and controls
218 lines (194 loc) · 4.51 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
#include <bits/stdc++.h>
using namespace std;
void swap(int &x, int &y)
{
int temp = y;
y = x;
x = temp;
}
class Minheap
{
public:
int heapsize;
int capacity;
int *harr;
Minheap(int cap)
{
heapsize = 0;
capacity = cap;
harr = new int[cap];
}
int parent(int i)
{
return (i - 1) / 2;
}
int right(int i)
{
return (2 * i + 2);
}
int left(int i)
{
return (2 * i + 1);
}
void insert(int key)
{
if (heapsize == capacity)
{
cout << "Overflow" << endl;
return;
}
heapsize++;
int i = heapsize - 1;
harr[i] = key;
while (i != 0 && (harr[parent(i)] < harr[i]))
{
swap(harr[i], harr[parent(i)]);
i = parent(i);
}
}
void linearSearch(int val)
{
for (int i = 0; i < heapsize; i++)
{
if (harr[i] == val)
{
cout << "Value Found!";
return;
}
}
cout << "Value NOT Found!";
}
void printArray()
{
for (int i = 0; i < heapsize; i++)
cout << harr[i] << " ";
cout << endl;
}
int height()
{
return ceil(log2(heapsize + 1)) - 1;
}
void MinHeapify(int i)
{
int l = left(i);
int r = right(i);
int largest = i;
if (l < heapsize && harr[l] > harr[i])
{
largest = l;
}
else if (r < heapsize && harr[r] > harr[largest])
{
largest = r;
}
if (largest != i)
{
swap(harr[i], harr[largest]);
MinHeapify(largest);
}
}
int extractMin()
{
if (heapsize <= 0)
{
return INT_MAX;
}
else if (heapsize == 1)
{
heapsize--;
return harr[0];
}
else if (heapsize > 1)
{
int root = harr[0];
harr[0] = harr[heapsize - 1];
heapsize--;
MinHeapify(0);
return root;
}
}
void 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);
}
}
void deleteKey(int i)
{
decreaseKey(i, INT_MIN);
extractMin();
}
};
int main()
{
int s;
cout << "Enter size of min heap" << endl;
Minheap obj(s);
cout << "Minheap is created" << endl;
int option, val;
do
{
cout << "What operation do you want to perform? "
<< " Select Option number. Enter 0 to exit." << endl;
cout << "1. Insert Key/Node" << endl;
cout << "2. Search Key/Node" << endl;
cout << "3. Delete Key/Node" << endl;
cout << "4. Get Min" << endl;
cout << "5. Extract Min" << endl;
cout << "6. Height of Heap" << endl;
cout << "7. Print/Traversal Heap values" << endl;
cout << "8. Clear Screen" << endl;
cout << "0. Exit Program" << endl;
cin >> option;
switch (option)
{
case 0:
break;
case 1:
cout << "INSERT Operation -" << endl;
cout << "Enter VALUE to INSERT in HEAP: ";
cin >> val;
obj.insert(val);
cout << endl;
break;
case 2:
cout << "SEARCH Operation -" << endl;
cout << "Enter VALUE to SEARCH in HEAP: ";
cin >> val;
obj.linearSearch(val);
break;
case 3:
cout << "DELETE Operation -" << endl;
cout << "Enter INDEX of Heap Array to DELETE: ";
cin >> val;
obj.deleteKey(val);
break;
case 4:
cout << "GET Min value : ";
//<< obj.getMin();
cout << endl;
break;
case 5:
cout << "EXTRACT Min value : " << obj.extractMin();
cout << endl;
break;
case 6:
cout << "HEAP TREE HEIGHT : " << obj.height() << endl;
break;
case 7:
cout << "PRINT Heap Array : " << endl;
obj.printArray();
cout << endl;
break;
case 8:
system("cls");
break;
default:
cout << "Enter Proper Option number " << endl;
}
} while (option != 0);
return 0;
}