34 template<
typename U = T >
35 typename std::enable_if<std::is_pointer<U>::value,
int >::type
smallerChild(
int index);
36 template<
typename U = T >
37 typename std::enable_if<!std::is_pointer<U>::value,
int >::type
smallerChild(
int index);
44 template<
typename U = T >
45 typename std::enable_if<std::is_pointer<U>::value,
bool > ::type
isValidParent(
int index);
46 template<
typename U = T >
47 typename std::enable_if<!std::is_pointer<U>::value,
bool > ::type
isValidParent(
int index);
52 template<
typename U = T>
53 typename std::enable_if<std::is_pointer<U>::value,
void >::type
bubbleUp();
54 template<
typename U = T>
55 typename std::enable_if<!std::is_pointer<U>::value,
void >::type
bubbleUp();
109 return getSize() == 0;
119 return (index - 1) / 2;
124 return (index * 2) + 1;
129 return (index * 2) + 2;
134 T temp = items[first];
135 items[first] = items[second];
136 items[second] = temp;
142 std::cout <<
"ERROR: MinHeap Underflow" << std::endl;
150 items.push_back(value);
154 template<
typename T>
template<
typename U>
156 int index = getSize() - 1;
157 while (index > 0 && *items[index] < *items[parent(index)]) {
158 swap(index, parent(index));
159 index = parent(index);
163 template<
typename T>
template<
typename U>
165 int index = getSize() - 1;
166 while (index > 0 && items[index] < items[parent(index)]) {
167 swap(index, parent(index));
168 index = parent(index);
175 std::cout <<
"ERROR: MinHeap Underflow" << std::endl;
180 items[0] = items[getSize() - 1];
190 while (index < getSize() && !isValidParent(index)) {
191 int smallerIndex = smallerChild(index);
192 swap(index, smallerIndex);
193 index = smallerIndex;
199 return leftChild(index) < getSize();
204 return rightChild(index) < getSize();
207 template<
typename T>
template<
typename U>
209 if (!hasLeftChild(index))
211 if (!hasRightChild(index))
212 return *items[index] <= *items[leftChild(index)];
214 return *items[index] <= *items[leftChild(index)] && *items[index] <= *items[rightChild(index)];
217 template<
typename T>
template<
typename U>
219 if (!hasLeftChild(index))
221 if (!hasRightChild(index))
222 return items[index] <= items[leftChild(index)];
224 return items[index] <= items[leftChild(index)] && items[index] <= items[rightChild(index)];
227 template<
typename T>
template<
typename U>
229 if (!hasLeftChild(index))
231 if (!hasRightChild(index))
232 return leftChild(index);
234 return (*items[leftChild(index)] < *items[rightChild(index)]) ?
235 leftChild(index) : rightChild(index);
238 template<
typename T>
template<
typename U>
240 if (!hasLeftChild(index))
242 if (!hasRightChild(index))
243 return leftChild(index);
245 return (items[leftChild(index)] < items[rightChild(index)]) ?
246 leftChild(index) : rightChild(index);
252 for (
int i = 0; i < getSize(); i++)
254 std::cout << items[i] <<
", ";
256 std::cout <<
"\b\b ]\n";
This file exports the constants used in throughout the project.
constexpr auto RESERVE_SIZE
Constants for MinHeap.
This class models a structure called a MinHeap in which value in the root node is minimum.
int parent(int)
Vector of items of MinHeap.
std::enable_if< std::is_pointer< U >::value, int >::type smallerChild(int index)
std::enable_if< std::is_pointer< U >::value, void >::type bubbleUp()
Bubbles up the value to meet the MinHeap property.
std::enable_if< std::is_pointer< U >::value, bool >::type isValidParent(int index)
void swap(int, int)
swap two values in the items vector
std::enable_if<!std::is_pointer< U >::value, bool >::type isValidParent(int index)
void insert(const T &)
Adds value of type T to the MinHeap.
std::enable_if<!std::is_pointer< U >::value, void >::type bubbleUp()
void display()
Prints the values of the MinHeap.
void bubbleDown()
Bubbles down the value to meet the MinHeap property.
T remove()
Removes and returns the minimum value.
std::enable_if<!std::is_pointer< U >::value, int >::type smallerChild(int index)