Huffman Zipper  v-1.0
Data Compression and Decompression using Greedy Huffman Algorithm
MinHeap.h
Go to the documentation of this file.
1 #pragma once
2 #include <iostream>
3 #include <vector>
4 #include <type_traits>
5 
6 #include "BinNode.h"
7 #include "Constants.h"
8 
9 
17 template <class T>
18 class MinHeap {
19 private:
20  std::vector<T> items;
22 private:
23 
25  int parent(int);
26 
28  int leftChild(int);
29 
31  int rightChild(int);
32 
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);
38 
40  bool hasLeftChild(int);
41 
43  bool hasRightChild(int);
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);
48 
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();
56 
60  void bubbleDown();
61 
66  void swap(int, int);
67 
68 public:
69  MinHeap();
70  ~MinHeap();
71 
73  bool isEmpty();
74 
76  int getSize();
77 
79  T min();
80 
82  void insert(const T&);
83 
87  T remove();
88 
90  void display();
91 };
92 
93 /************************************************************************/
94 /* implementation */
95 /************************************************************************/
96 
97 template <class T>
99  items.reserve(RESERVE_SIZE);
100 }
101 
102 template <class T>
104  items.clear();
105 }
106 
107 template <class T>
109  return getSize() == 0;
110 }
111 
112 template <class T>
114  return items.size();
115 }
116 
117 template <class T>
118 int MinHeap<T>::parent(int index) {
119  return (index - 1) / 2;
120 }
121 
122 template <class T>
123 int MinHeap<T>::leftChild(int index) {
124  return (index * 2) + 1;
125 }
126 
127 template <class T>
128 int MinHeap<T>::rightChild(int index) {
129  return (index * 2) + 2;
130 }
131 
132 template <class T>
133 void MinHeap<T>::swap(int first, int second) {
134  T temp = items[first];
135  items[first] = items[second];
136  items[second] = temp;
137 }
138 
139 template <class T>
141  if (isEmpty()) {
142  std::cout << "ERROR: MinHeap Underflow" << std::endl;
143  exit(1);
144  }
145  return items[0];
146 }
147 
148 template <class T>
149 void MinHeap<T>::insert(const T& value) {
150  items.push_back(value);
151  bubbleUp();
152 }
153 
154 template<typename T> template<typename U>
155 typename std::enable_if<std::is_pointer<U>::value, void >::type MinHeap<T>::bubbleUp() {
156  int index = getSize() - 1;
157  while (index > 0 && *items[index] < *items[parent(index)]) {
158  swap(index, parent(index));
159  index = parent(index);
160  }
161 }
162 
163 template<typename T> template<typename U>
164 typename std::enable_if<!std::is_pointer<U>::value, void >::type MinHeap<T>::bubbleUp() {
165  int index = getSize() - 1;
166  while (index > 0 && items[index] < items[parent(index)]) {
167  swap(index, parent(index));
168  index = parent(index);
169  }
170 }
171 
172 template <class T>
174  if (isEmpty()) {
175  std::cout << "ERROR: MinHeap Underflow" << std::endl;
176  exit(1);
177  }
178 
179  T root = items[0];
180  items[0] = items[getSize() - 1];
181  bubbleDown();
182 
183  items.pop_back();
184  return root;
185 }
186 
187 template <class T>
189  int index = 0;
190  while (index < getSize() && !isValidParent(index)) {
191  int smallerIndex = smallerChild(index);
192  swap(index, smallerIndex);
193  index = smallerIndex;
194  }
195 }
196 
197 template <class T>
198 bool MinHeap<T>::hasLeftChild(int index) {
199  return leftChild(index) < getSize();
200 }
201 
202 template <class T>
203 bool MinHeap<T>::hasRightChild(int index) {
204  return rightChild(index) < getSize();
205 }
206 
207 template<typename T> template<typename U>
208 typename std::enable_if<std::is_pointer<U>::value, bool > ::type MinHeap<T>::isValidParent(int index) {
209  if (!hasLeftChild(index))
210  return true;
211  if (!hasRightChild(index))
212  return *items[index] <= *items[leftChild(index)];
213 
214  return *items[index] <= *items[leftChild(index)] && *items[index] <= *items[rightChild(index)];
215 }
216 
217 template<typename T> template<typename U>
218 typename std::enable_if<!std::is_pointer<U>::value, bool >::type MinHeap<T>::isValidParent(int index) {
219  if (!hasLeftChild(index))
220  return true;
221  if (!hasRightChild(index))
222  return items[index] <= items[leftChild(index)];
223 
224  return items[index] <= items[leftChild(index)] && items[index] <= items[rightChild(index)];
225 }
226 
227 template<typename T> template<typename U>
228 typename std::enable_if<std::is_pointer<U>::value, int >::type MinHeap<T>::smallerChild(int index) {
229  if (!hasLeftChild(index))
230  return index;
231  if (!hasRightChild(index))
232  return leftChild(index);
233 
234  return (*items[leftChild(index)] < *items[rightChild(index)]) ?
235  leftChild(index) : rightChild(index);
236 }
237 
238 template<typename T> template<typename U>
239 typename std::enable_if<!std::is_pointer<U>::value, int >::type MinHeap<T>::smallerChild(int index) {
240  if (!hasLeftChild(index))
241  return index;
242  if (!hasRightChild(index))
243  return leftChild(index);
244 
245  return (items[leftChild(index)] < items[rightChild(index)]) ?
246  leftChild(index) : rightChild(index);
247 }
248 
249 template <class T>
251  std::cout << "[ ";
252  for (int i = 0; i < getSize(); i++)
253  {
254  std::cout << items[i] << ", ";
255  }
256  std::cout << "\b\b ]\n";
257 }
This file exports the constants used in throughout the project.
constexpr auto RESERVE_SIZE
Constants for MinHeap.
Definition: Constants.h:14
This class models a structure called a MinHeap in which value in the root node is minimum.
Definition: MinHeap.h:18
int leftChild(int)
Definition: MinHeap.h:123
int parent(int)
Vector of items of MinHeap.
Definition: MinHeap.h:118
bool hasRightChild(int)
Definition: MinHeap.h:203
std::enable_if< std::is_pointer< U >::value, int >::type smallerChild(int index)
Definition: MinHeap.h:228
std::enable_if< std::is_pointer< U >::value, void >::type bubbleUp()
Bubbles up the value to meet the MinHeap property.
Definition: MinHeap.h:155
std::enable_if< std::is_pointer< U >::value, bool >::type isValidParent(int index)
Definition: MinHeap.h:208
bool hasLeftChild(int)
Definition: MinHeap.h:198
void swap(int, int)
swap two values in the items vector
Definition: MinHeap.h:133
int rightChild(int)
Definition: MinHeap.h:128
std::enable_if<!std::is_pointer< U >::value, bool >::type isValidParent(int index)
std::vector< T > items
Definition: MinHeap.h:20
bool isEmpty()
Definition: MinHeap.h:108
~MinHeap()
Definition: MinHeap.h:103
void insert(const T &)
Adds value of type T to the MinHeap.
Definition: MinHeap.h:149
MinHeap()
Definition: MinHeap.h:98
std::enable_if<!std::is_pointer< U >::value, void >::type bubbleUp()
void display()
Prints the values of the MinHeap.
Definition: MinHeap.h:250
int getSize()
Definition: MinHeap.h:113
void bubbleDown()
Bubbles down the value to meet the MinHeap property.
Definition: MinHeap.h:188
T min()
Definition: MinHeap.h:140
T remove()
Removes and returns the minimum value.
Definition: MinHeap.h:173
std::enable_if<!std::is_pointer< U >::value, int >::type smallerChild(int index)