-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmax_heap.cpp
More file actions
175 lines (139 loc) · 3.24 KB
/
max_heap.cpp
File metadata and controls
175 lines (139 loc) · 3.24 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
// Max-Heap example program
#include <iostream>
using namespace std;
// Class for Max-Heap
class MaxHeap
{
int *arr;
int size;
int n;
int MIN_VAL;
int getParent(const int x)
{
return (x % 2 == 0) ? (x - 1) / 2 : (x - 2) / 2;
}
void heapify()
{
int curr = n-1;
int parent = getParent(curr);
// While newly inserted element is lesser than its parent
while(arr[curr] > arr[parent])
{
// Swap parent and that child
int temp = arr[curr];
arr[curr] = arr[parent];
arr[parent] = temp;
curr = getParent(curr);
parent = getParent(curr);
}
}
void downHeapify()
{
int curr = 0;
int child1 = (2*curr) + 1;
int child2 = (2*curr) + 2;
// While curr is less than (for max-heap) its children
while((arr[curr] < arr[child1] || arr[curr] < arr[child2]) && curr < n)
{
if (arr[child1] > arr[child2])
{
int temp = arr[curr];
arr[curr] = arr[child1];
arr[child1] = temp;
curr = child1;
child1 = (2*curr) + 1;
child2 = (2*curr) + 2;
}
else
{
int temp = arr[curr];
arr[curr] = arr[child2];
arr[child2] = temp;
curr = child2;
child1 = (2*curr) + 1;
child2 = (2*curr) + 2;
}
}
}
public:
/**
* @param size Size of heap to create
*/
MaxHeap(int size)
{
this->size = size;
arr = new int[this->size];
n = 0;
MIN_VAL = -9999999;
}
int getMaxSize()
{
return this->size;
}
int getNumElements()
{
return this->n;
}
void displayHeap()
{
cout << "Heap is ";
for(int i=0; i<n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
/**
* @param element Integer to insert into heap
*/
void insert(int element)
{
if (n>=size)
{
cout << "Heap Overflow!" << endl;
return;
}
arr[n] = element;
n += 1;
heapify();
cout << element << " inserted successfully." << endl;
}
// Function to remove top element from max-heap
int remove()
{
if (n <= 0)
{
cout << "Heap Underflow!" << endl;
return -9999;
}
int temp = arr[0];
arr[0] = arr[n-1];
arr[n-1] = temp;
n -= 1;
int result = arr[n];
arr[n] = MIN_VAL;
downHeapify();
return result;
}
};
// Main Function
int main()
{
const int size = 20;
MaxHeap obj(50);
obj.displayHeap();
for (int i=0; i<=11; i++)
{
obj.insert(i);
obj.displayHeap();
}
cout << "Delete#1: " << obj.remove() << endl;
obj.displayHeap();
cout << "Delete#2: " << obj.remove() << endl;
obj.displayHeap();
cout << "Delete#3: " << obj.remove() << endl;
obj.displayHeap();
cout << "Delete#4: " << obj.remove() << endl;
obj.displayHeap();
return 0;
}