-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinAndMaxHeap.cpp
More file actions
175 lines (153 loc) · 4.21 KB
/
MinAndMaxHeap.cpp
File metadata and controls
175 lines (153 loc) · 4.21 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
//
// Implementation of min and max heap in C++
// In heaps, there are a couple of things to remember:
// a) While adding a new value, you always start at the lowest level and left-most side
// and you keep swapping up till heap-order property is maintained
// b) While removing an element, it is always at the top/root. You keep swapping down
// till heap-order property is maintained
//
// Created by Madhumitha Raghu on 1/18/21.
//
#include <iostream>
#include <vector>
template<typename T>
class Heap {
private:
std::vector<int> data;
T comparator;
public:
Heap()
: comparator(T()) {
}
void push(int value)
{
if (data.empty())
{
data.push_back(value);
} else {
data.push_back(value);
heapify_up(data.size() - 1);
}
}
void pop()
{
if (data.empty())
throw;
data[0] = data.back();
data.pop_back();
heapify_down(0);
}
int top()
{
if (data.empty())
throw;
return data[0];
}
size_t size()
{
return data.size();
}
void print()
{
std::cout << "Printing queue : ";
for (auto iter : data)
{
std::cout << iter << " ";
}
std::cout << std::endl;
}
private:
size_t parent(size_t child_indx)
{
return (child_indx - 1) / 2;
}
size_t left(size_t indx)
{
return 2*indx + 1;
}
size_t right(size_t indx)
{
return 2*indx + 2;
}
void heapify_up(size_t indx)
{
// The parent must either be smaller than
// or larger then the children
if (indx != 0) { // if we have reached the root, then the heap order is maintained
size_t parent_indx = parent(indx);
bool result = comparator(data[indx], data[parent_indx]);
if (result)
{
std::swap(data[indx], data[parent_indx]);
heapify_up(parent_indx);
}
}
}
void heapify_down(size_t indx)
{
size_t maxIndx = indx;
size_t leftindx = left(indx);
size_t rightindx = right(indx);
if (leftindx >= 0 && comparator(data[leftindx], data[maxIndx]))
maxIndx = leftindx;
if (rightindx < data.size() && comparator(data[rightindx], data[maxIndx]))
maxIndx = rightindx;
if (maxIndx != indx)
{
// continue process because we made changes
// to keep the headp-order
std::swap(data[indx], data[maxIndx]);
heapify_down(maxIndx);
}
}
};
int main(int argc, const char * argv[]) {
/* Max heap testing */
std::cout << "Testing max heap" << std::endl;
Heap<std::greater<int>> MaxHeap;
MaxHeap.push(3);
MaxHeap.push(2);
MaxHeap.push(15);
std::cout << "Size is " << MaxHeap.size() << std::endl;
std::cout << MaxHeap.top() << " ";
MaxHeap.pop();
std::cout << MaxHeap.top() << " ";
MaxHeap.pop();
MaxHeap.push(5);
MaxHeap.push(4);
MaxHeap.push(45);
std::cout << std::endl << "Size is " << MaxHeap.size() << std::endl;
std::cout << MaxHeap.top() << " ";
MaxHeap.pop();
std::cout << MaxHeap.top() << " ";
MaxHeap.pop();
std::cout << MaxHeap.top() << " ";
MaxHeap.pop();
std::cout << MaxHeap.top() << " ";
MaxHeap.pop();
std::cout << std::endl;
/* Min heap testing */
std::cout << "Testing min heap" << std::endl;
Heap<std::less<int>> MinHeap;
MinHeap.push(3);
MinHeap.push(2);
MinHeap.push(15);
std::cout << "Size is " << MinHeap.size() << std::endl;
std::cout << MinHeap.top() << " ";
MinHeap.pop();
std::cout << MinHeap.top() << " ";
MinHeap.pop();
MinHeap.push(5);
MinHeap.push(4);
MinHeap.push(45);
std::cout << std::endl << "Size is " << MinHeap.size() << std::endl;
std::cout << MinHeap.top() << " ";
MinHeap.pop();
std::cout << MinHeap.top() << " ";
MinHeap.pop();
std::cout << MinHeap.top() << " ";
MinHeap.pop();
std::cout << MinHeap.top() << " ";
MinHeap.pop();
return 0;
}