-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.cpp
223 lines (171 loc) · 4.93 KB
/
heap.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
//min heap data structure implementation in C++
#include<iostream>
#include<vector>
#include<utility>
class Heap
{
public:
//default constructor creates an empty heap with a default operational size, which is 100
Heap();
//this constructor creates an empty heap whose operational size is equal to the argument of this constructor
explicit Heap(const int);
virtual ~Heap();
Heap(const Heap&) = delete;
Heap& operator=(const Heap&) = delete;
Heap(Heap&&) noexcept;
Heap& operator=(Heap&&) noexcept;
//this method inserts a single element into a heap
void insert(const int);
//this method inserts the elements of the vector taken as a parameter
void insert(const std::vector<int>&);
//this method returns the root element of a heap
int getRoot() const;
//this method deletes the root element of a heap
void deleteRoot();
int getOperationalSize() const;
int getCurrentSize() const;
//just temporary method
void print()
{
for(int i = 0; i<currentSize; i++)
std::cout << heapPointer[i] << " ";
std::cout << std::endl;
}
private:
//the total operational size of a heap
int size;
//the current number the elements in a heap
int currentSize;
//pointer pointing to a heap
int* heapPointer;
//helper method for swaping heap's elements
void heapify(int);
//helper function for freeing up memory
void clean (int*);
//helper method used for moving an object of a heap
void moveFrom (Heap&) noexcept;
};
int main ()
{
Heap h1;
h1.insert(10);
h1.insert(9);
h1.insert(8);
h1.insert(25);
h1.insert(4);
h1.insert(4);
std::vector<int> vec1 {1, 3, 5, 19};
h1.insert(vec1);
std::cout << "the root element of h1 is " << h1.getRoot() << std::endl;
h1.deleteRoot();
std::cout << "the root element of h1 is " << h1.getRoot() << std::endl;
h1.insert(1);
std::cout << "the total operational size of h1 is " << h1.getOperationalSize() << std::endl;
std::cout << "the current size h1 is " << h1.getCurrentSize() << std::endl;
std::cout << std::endl;
Heap h2(200);
std::cout << "the total operational size of h2 is " << h2.getOperationalSize() << std::endl;
std::cout << "the current size h2 is " << h2.getCurrentSize() << std::endl;
h2.insert(33);
h2.insert(333);
std::cout << "now, the total operational size of h2 is " << h2.getOperationalSize() << std::endl;
std::cout << "now, the current size h2 is " << h2.getCurrentSize() << std::endl;
std::cout << std::endl;
Heap h3(std::move(h2));
std::cout << "the total operational size of h3 is " << h3.getOperationalSize() << std::endl;
std::cout << "the current size h3 is " << h3.getCurrentSize() << std::endl;
std::cout << std::endl;
Heap h4;
h4 = std::move(h3);
std::cout << "the total operational size of h4 is " << h4.getOperationalSize() << std::endl;
std::cout << "the current size h4 is " << h4.getCurrentSize() << std::endl;
return 0;
}
Heap::Heap(): size{100}, currentSize{0}, heapPointer{new int [100]} {}
Heap::Heap(int s): size{s}, currentSize{0}, heapPointer{new int [size]} {}
Heap::Heap(Heap&& src) noexcept
{
moveFrom(src);
}
Heap& Heap::operator=(Heap&& src) noexcept
{
if(this == &src) return *this;
clean(heapPointer);
moveFrom(src);
return *this;
}
Heap::~Heap()
{
clean(heapPointer);
}
void Heap::insert(const int val)
{
if (currentSize >= size) return;
int i = currentSize;
heapPointer[i] = val;
int parent = (i - 1)/2;
while (parent >= 0 && i>0)
{
if(heapPointer[parent] > heapPointer[i])
{
int temp = heapPointer[parent];
heapPointer[parent] = heapPointer[i];
heapPointer[i] = temp;
}
i = parent;
parent = (i - 1)/2;
}
currentSize++;
}
void Heap::insert (const std::vector<int>& vec)
{
if (vec.size() > (size - currentSize)) return;
for (int el : vec) insert(el);
}
int Heap::getRoot() const
{
return heapPointer[0];
}
void Heap::heapify(int index)
{
int temp, left, right;
left = 2 * index + 1;
right = 2 * index + 2;
if (left < currentSize)
{
if (heapPointer[index] > heapPointer[left])
{
std::swap(heapPointer[index], heapPointer[left]);
heapify(right);
}
}
}
void Heap::deleteRoot()
{
if(currentSize == 0) return;
heapPointer[0] = heapPointer[currentSize - 1];
currentSize--;
heapify(0);
}
int Heap::getCurrentSize() const
{
return currentSize;
}
int Heap::getOperationalSize() const
{
return size;
}
void Heap::clean(int* ptr)
{
delete [] ptr;
ptr = nullptr;
}
void Heap::moveFrom(Heap& src) noexcept
{
heapPointer = src.heapPointer;
size = src.size;
currentSize = src.currentSize;
src.heapPointer = nullptr;
src.size = 0;
src.currentSize = 0;
}