HOG2
Heap.cpp
Go to the documentation of this file.
1 /*
2  * $Id: Heap.cpp
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 11/29/06.
6  * Modified by Nathan Sturtevant on 02/29/20.
7  *
8  * This file is part of HOG2. See https://github.com/nathansttt/hog2 for licensing information.
9  *
10  */
11 
12 // HOG File
13 
14 #include <iostream>
15 #include "FPUtil.h"
16 #include "Heap.h"
17 
18 Heap::Heap(int s)
19 {
20  count = 0;
21  _elts.reserve(s);
22 }
23 
25 {
26 }
27 
28 unsigned int Heap::size()
29 {
30  return _elts.size();
31 }
32 
37 {
38  val->key = count;
39  _elts.push_back(val);
40  count++;
41  HeapifyUp(val->key);
42 }
43 
48 {
49  HeapifyUp(val->key);
50 }
51 
56 {
57  if (val->key < _elts.size() &&
58  (_elts[val->key] == val))
59  return true;
60  return false;
61 }
62 
67 {
68  if (Empty())
69  return 0;
70  count--;
71  graph_object *ans = _elts[0];
72  _elts[0] = _elts[count];
73  _elts[0]->key = 0;
74  _elts.pop_back();
75  HeapifyDown(0);
76 
77  return ans;
78 }
79 
84 {
85  return count == 0;
86 }
87 
88 void Heap::HeapifyUp(int index)
89 {
90  if (index == 0) return;
91  int parent = (index-1)/2;
92 
93  if (fgreater(_elts[parent]->GetKey(), _elts[index]->GetKey()))
94  {
95  graph_object *tmp = _elts[parent];
96  _elts[parent] = _elts[index];
97  _elts[index] = tmp;
98  _elts[parent]->key = parent;
99  _elts[index]->key = index;
100  HeapifyUp(parent);
101  }
102 }
103 
104 void Heap::HeapifyDown(int index)
105 {
106  int child1 = index*2+1;
107  int child2 = index*2+2;
108  int which;
109 
110  // find smallest child
111  if (child1 >= count)
112  return;
113  else if (child2 >= count)
114  which = child1;
115  else if (fless(_elts[child1]->GetKey(), _elts[child2]->GetKey()))
116  which = child1;
117  else
118  which = child2;
119 
120  if (fless(_elts[which]->GetKey(), _elts[index]->GetKey()))
121  {
122  graph_object *tmp = _elts[which];
123  _elts[which] = _elts[index];
124  _elts[index] = tmp;
125  _elts[which]->key = which;
126  _elts[index]->key = index;
127  HeapifyDown(which);
128  }
129 }
Heap::size
unsigned int size()
Definition: Heap.cpp:28
Heap::~Heap
~Heap()
Definition: Heap.cpp:24
graph_object::key
unsigned int key
Definition: Graph.h:54
Heap.h
Heap::HeapifyUp
void HeapifyUp(int index)
Definition: Heap.cpp:88
Heap::_elts
std::vector< graph_object * > _elts
Definition: Heap.h:40
FPUtil.h
Heap::DecreaseKey
void DecreaseKey(graph_object *val)
Indicate that the key for a particular object has decreased.
Definition: Heap.cpp:47
graph_object
Parent class for nodes and edges allowing them to be stored in a Heap or manipulated with other data ...
Definition: Graph.h:47
Heap::Empty
bool Empty()
Returns true if no items are in the Heap.
Definition: Heap.cpp:83
fless
bool fless(double a, double b)
Definition: FPUtil.h:28
Heap::Remove
graph_object * Remove()
Remove the item with the lowest key from the Heap & re-heapify.
Definition: Heap.cpp:66
Heap::HeapifyDown
void HeapifyDown(int index)
Definition: Heap.cpp:104
fgreater
bool fgreater(double a, double b)
Definition: FPUtil.h:29
Heap::Heap
Heap(int s=DEFAULT_SIZE)
Definition: Heap.cpp:18
Heap::Add
void Add(graph_object *val)
Add object into Heap.
Definition: Heap.cpp:36
Heap::count
int count
Definition: Heap.h:41
Heap::IsIn
bool IsIn(graph_object *val)
Returns true if the object is in the Heap.
Definition: Heap.cpp:55