Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

BinaryHeap.h
Go to the documentation of this file.
1 
33 #pragma once
34 
36 
37 namespace ogdf {
38 
46 template<
47  typename T,
48  typename C=std::less<T>
49 >
50 class BinaryHeap : public HeapBase<BinaryHeap<T, C>, int, T, C>
51 {
52 
53  using base_type = HeapBase<BinaryHeap<T, C>, int, T, C>;
54 
55 public:
56 
63  explicit BinaryHeap(const C &comp = C(), int initialSize = 128);
64 
65  virtual ~BinaryHeap() {
66  if (m_heapArray) {
67  for(int i = 1; i <= m_size; i++) {
68  delete m_heapArray[i].handle;
69  }
70 
71  delete[] m_heapArray;
72  }
73  }
74 
80  const T &top() const override;
81 
88  int *push(const T &value) override;
89 
93  void pop() override;
94 
101  void decrease(int *handle, const T &value) override;
102 
109  const T &value(int *handle) const override;
110 
112  int capacity() const { return m_arraySize; }
113 
115  int size() const { return m_size; }
116 
118  bool empty() const { return m_size == 0; }
119 
121 
125  void clear();
126 
127 private:
128 
130  void siftUp(int pos);
131 
133  void siftDown(int pos);
134 
136  int parentIndex(int num)
137  {
138  OGDF_ASSERT(num > 0);
139  return num / 2;
140  }
141 
143  int leftChildIndex(int num)
144  {
145  OGDF_ASSERT(num > 0);
146  return 2 * num;
147  }
148 
150  int rightChildIndex(int num)
151  {
152  OGDF_ASSERT(num > 0);
153  return 2*num + 1;
154  }
155 
157  bool hasLeft(int num)
158  {
159  OGDF_ASSERT(num > 0);
160  return leftChildIndex(num) <= m_size;
161  }
162 
164  bool hasRight(int num)
165  {
166  OGDF_ASSERT(num > 0);
167  return rightChildIndex(num) <= m_size;
168  }
169 
170  // helper functions for internal maintenance
171 
172  int arrayBound(int arraySize) { return arraySize + 1; }
173  int higherArrayBound(int arraySize) { return 2*arraySize + 1; }
174  int higherArraySize(int arraySize) { return 2 * arraySize; }
175  int lowerArrayBound(int arraySize) { return arraySize/2 + 1; }
176  int lowerArraySize(int arraySize) { return arraySize / 2; }
177  void init(int initialSize);
178 
179  struct HeapEntry {
180  T value;
181  int *handle = nullptr;
182  };
183 
184  // dynamically maintained array of entries
186 
187  // number of elements stored in heap
188  int m_size;
189 
190  // current capacity of the heap
192 
193  // used to check reallocation bound
195 };
196 
197 
198 template <typename T, typename C>
199 const T &BinaryHeap<T, C>::top() const {
200  int firstIndex = 1;
201  return value(&firstIndex);
202 }
203 
204 template <typename T, typename C>
207  m_size++;
208 
209  // does the array size have to be adjusted ?
210  if (m_size == m_arraySize) {
212 
213  // last one is not occupied yet
214  for (int i = 1; i <= m_arraySize; i++) {
215  tempHeap[i] = m_heapArray[i];
216  }
217 
218  delete[] m_heapArray;
219  m_heapArray = tempHeap;
221  }
222 
223  // actually insert object and reestablish heap property
224  HeapEntry &entry = m_heapArray[m_size];
225  entry.value = value;
226  entry.handle = new int;
227  *(entry.handle) = m_size;
228 
229  int *result = entry.handle;
230 
231  OGDF_ASSERT(*result == *(m_heapArray[*result].handle));
232 
233  siftUp(m_size);
234 
235  OGDF_ASSERT(*result == *(m_heapArray[*result].handle));
236 
237  return result;
238 }
239 
240 template <typename T, typename C>
242  OGDF_ASSERT(!empty());
243  delete m_heapArray[1].handle;
244  m_size--;
245 
246  if (m_size > 0) {
247  // former last leaf
249 
250  // check if reallocation is possible
251  // TODO make reallocation threshold configurable?
252  if ((m_size < (m_arraySize/3)) && (m_arraySize > 2*m_initialSize-1)) {
254 
255  for (int i = 1; i <= m_size ; i++) {
256  tempHeap[i] = m_heapArray[i];
257  }
258 
259  delete[] m_heapArray;
260  m_heapArray = tempHeap;
262  }
263 
264  siftDown(1);
265  }
266 }
267 
268 template <typename T, typename C>
270  HeapEntry& he = m_heapArray[*handle];
271  OGDF_ASSERT(this->comparator()(value, he.value));
272 
273  he.value = value;
274  siftUp(*handle);
275 }
276 
277 template <typename T, typename C>
278 const T &BinaryHeap<T, C>::value(int *handle) const {
279  OGDF_ASSERT(handle != nullptr);
280  OGDF_ASSERT(*handle > 0);
281  OGDF_ASSERT(*handle <= m_size);
282 
283  return m_heapArray[*handle].value;
284 }
285 
286 template <typename T, typename C>
287 BinaryHeap<T, C>::BinaryHeap(const C &comp, int initialSize)
288 : base_type(comp)
289 {
290  init(initialSize);
291 }
292 
293 template <typename T, typename C>
294 void BinaryHeap<T, C>::init(int initialSize)
295 {
296  m_arraySize = initialSize;
297  m_initialSize = initialSize;
298  m_size = 0;
299 
300  // create an array of HeapEntry Elements
301  // start at 1
303 }
304 
305 template <typename T, typename C>
307  for(int i = 1; i <= m_size; i++) {
308  delete m_heapArray[i].handle;
309  }
310 
311  delete[] m_heapArray;
313 }
314 
315 template <typename T, typename C>
317 {
318  OGDF_ASSERT(pos > 0);
319  OGDF_ASSERT(pos <= m_size);
320 
321  if (pos == 1) {
322  *(m_heapArray[1].handle) = 1;
323  } else {
324  HeapEntry tempEntry = m_heapArray[pos];
325  int run = pos;
326 
327  while ((parentIndex(run) >= 1) &&
328  this->comparator()(tempEntry.value, m_heapArray[parentIndex(run)].value)) {
329  m_heapArray[run] = m_heapArray[parentIndex(run)];
330  *(m_heapArray[run].handle) = run;
331 
332  run = parentIndex(run);
333  }
334 
335  m_heapArray[run] = tempEntry;
336  *(m_heapArray[run].handle) = run;
337  }
338 }
339 
340 template <typename T, typename C>
342 {
343  OGDF_ASSERT(pos > 0);
344  OGDF_ASSERT(pos <= m_size);
345 
346  // is leaf?
347  if (pos >= m_size/2 + 1) {
348  *(m_heapArray[pos].handle) = pos;
349  // leafs cant move further down
350  } else {
351  T value = m_heapArray[pos].value;
352  int sIndex = pos;
353  C const &compare = this->comparator();
354 
355  // left child is smallest child?
356  if (hasLeft(pos) && compare(m_heapArray[leftChildIndex(pos)].value, value)
357  && compare(m_heapArray[leftChildIndex(pos)].value, m_heapArray[rightChildIndex(pos)].value)) {
358  sIndex = leftChildIndex(pos);
359  // or is right child smaller?
360  } else if (hasRight(pos) && compare(m_heapArray[rightChildIndex(pos)].value, value)) {
361  sIndex = rightChildIndex(pos);
362  }
363 
364  // need recursive sift?
365  if (sIndex != pos) {
366  HeapEntry tempEntry = m_heapArray[pos];
367  m_heapArray[pos] = m_heapArray[sIndex];
368  m_heapArray[sIndex] = tempEntry;
369 
370  // update both index entries
371  *(m_heapArray[pos].handle) = pos;
372  *(m_heapArray[sIndex].handle) = sIndex;
373 
374  siftDown(sIndex);
375  } else {
376  *(m_heapArray[pos].handle) = pos;
377  }
378  }
379 }
380 
381 } // end namespace ogdf
int parentIndex(int num)
Array index of parent node.
Definition: BinaryHeap.h:136
const T & top() const override
Returns the topmost value in the heap.
Definition: BinaryHeap.h:199
void siftUp(int pos)
Establishes heap property by moving element up in heap if necessary.
Definition: BinaryHeap.h:316
bool empty() const
Returns true iff the heap is empty.
Definition: BinaryHeap.h:118
Interface for heap implementations.
Definition: BinaryHeap.h:179
int * push(const T &value) override
Inserts a value into the heap.
Definition: BinaryHeap.h:205
HeapEntry * m_heapArray
Definition: BinaryHeap.h:185
virtual ~BinaryHeap()
Definition: BinaryHeap.h:65
int lowerArraySize(int arraySize)
Definition: BinaryHeap.h:176
int higherArrayBound(int arraySize)
Definition: BinaryHeap.h:173
int size() const
Returns the number of stored elements.
Definition: BinaryHeap.h:115
int leftChildIndex(int num)
Array index of left child.
Definition: BinaryHeap.h:143
void decrease(int *handle, const T &value) override
Decreases a single value.
Definition: BinaryHeap.h:269
bool hasRight(int num)
Returns true if right child exists.
Definition: BinaryHeap.h:164
virtual const C & comparator() const
Returns the comparator used to sort the values in the heap.
Definition: HeapBase.h:75
int higherArraySize(int arraySize)
Definition: BinaryHeap.h:174
void init(int initialSize)
Definition: BinaryHeap.h:294
void clear()
Reinitializes the data structure.
Definition: BinaryHeap.h:306
T value
Definition: BinaryHeap.h:180
Heap realized by a data array.
Definition: BinaryHeap.h:50
bool hasLeft(int num)
Returns true if left child exists.
Definition: BinaryHeap.h:157
Common interface for all heap classes.
Definition: HeapBase.h:55
int arrayBound(int arraySize)
Definition: BinaryHeap.h:172
void siftDown(int pos)
Establishes heap property by moving element down in heap if necessary.
Definition: BinaryHeap.h:341
int * handle
Definition: BinaryHeap.h:181
The namespace for all OGDF objects.
int rightChildIndex(int num)
Array index of right child.
Definition: BinaryHeap.h:150
const T & value(int *handle) const override
Returns the value of that handle.
Definition: BinaryHeap.h:278
int capacity() const
Returns the current size.
Definition: BinaryHeap.h:112
BinaryHeap(const C &comp=C(), int initialSize=128)
Initializes an empty binary heap.
Definition: BinaryHeap.h:287
int lowerArrayBound(int arraySize)
Definition: BinaryHeap.h:175
void pop() override
Removes the topmost value from the heap.
Definition: BinaryHeap.h:241
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41