Visualisierung2
myMaxheap.cpp
Go to the documentation of this file.
1 #include "myMaxheap.h"
2 
3 
5 {
6  mytuple *newtuple;
7  heaplimit = 1; // first free location is A[1]
8  arraysize = heapmin+1; //
9  isempty = true; // heap is initially empty
10  A = new hnode [arraysize]; // allocate array for heap
11  for (int i=0; i<arraysize; i++) { // initialize heap values
12  newtuple = new mytuple; //
13  A[i].d = newtuple; //
14  A[i].d->m = -4294967296.0; // a very negative value; unlikely to be a valid dQ
15  A[i].d->i = 0; //
16  A[i].d->j = 0; //
17  A[i].d->k = i; //
18  }
19 }
21  mytuple *newtuple;
22  heaplimit = 1; // first free location is A[1]
23  arraysize = size+1; //
24  isempty = true; // heap is initially empty
25  A = new hnode [arraysize]; // allocate array for heap
26  for (int i=0; i<arraysize; i++) { // initialize heap values
27  newtuple = new mytuple; //
28  A[i].d = newtuple; //
29  A[i].d->m = -4294967296.0; // a very negative value; unlikely to be a valid dQ
30  A[i].d->i = 0; //
31  A[i].d->j = 0; //
32  A[i].d->k = i; //
33  }
34 }
35 
37 {
38  for (int i=0; i<arraysize; i++) { delete A[i].d; } // first delete the containers pointed to
39  delete [] A;
40 }
41 
42 // Pop Maximum ------------------------------------------------------------------------
46 mytuple myMaxheap::popMaximum() { // O(log k) time
47  mytuple temp = returnMaximum();
48  deleteItem(A[1].d);
49  return temp;
50 }
51 
52 // Return Maximum --------------------------------------------------------------------
57  mytuple temp;
58  temp.m = A[1].d->m; //
59  temp.i = A[1].d->i; //
60  temp.j = A[1].d->j; //
61  temp.k = A[1].d->k; // grab A's data
62  return temp;
63 }
72 
73 
74 // Heapification functions ------------------------------------------------------------
78 int myMaxheap::downsift(int index) { // O(log k) time
79  bool stopFlag = false;
80  int L = left(index);
81  int R = right(index);
82  int swap;
83  mytuple* temp;
84 
85  while (!stopFlag) {
86  // check that both children are within the array boundaries
87  if ((L < heaplimit) && (R < heaplimit)) {
88  if (A[L].d->m > A[R].d->m) { swap = L; } else { swap = R; } // first choose larger of the children
89  } else { if (L < heaplimit) { swap = L; } else { break; } } // only one child to consider
90 
91  // now decide if need to exchange A[index] with A[swap]
92  if (A[index].d->m < A[swap].d->m) {
93  temp = A[index].d; // exchange pointers A[index] and A[swap]
94  A[index].d = A[swap].d; //
95  A[index].d->k = index; // note A[index].d's change of array location
96  A[swap].d = temp; //
97  A[swap].d->k = swap; // note A[swap].d's change in array location
98 
99  index = swap; // update indices for next pass
100  L = left(index); //
101  R = right(index); //
102  } else { stopFlag = true; }
103  }
104  return index; // return the new index location of downsifted element
105 }
109 int myMaxheap::upsift(int index) { // O(log k) time
110  bool stopFlag = false;
111  int P = parent(index);
112  mytuple* temp;
113  while (!stopFlag) {
114  // decide if A[index] needs to move up in tree
115  if ((P > 0) && (A[index].d->m > A[P].d->m)) {
116  temp = A[index].d; // exchange A[index] and A[P]
117  A[index].d = A[P].d; //
118  A[index].d->k = index; // note A[index].d's change of array location
119  A[P].d = temp; //
120  A[P].d->k = P; // note A[P].d's change of array location
121 
122  index = P; // update indices for next pass
123  P = parent(index); //
124  } else { stopFlag = true; }
125  }
126  return index;
127 }
128 
132 int myMaxheap::left (int index) { return 2*index; }
136 int myMaxheap::right (int index) { return 2*index+1; }
140 int myMaxheap::parent(int index) { return (int)index/2; }
141 
145 void myMaxheap::grow() { // O(k) time
146  mytuple *newtuple;
147  hnode *B; // scratch space for expansion of A
148  B = new hnode [arraysize]; //
149  for (int i=0; i<arraysize; i++) { B[i].d = A[i].d; } // copy A into B
150  delete [] A; // delete old array of addresses
151  A = new hnode [2*arraysize]; // grow A by factor of 2
152  for (int i=0; i<arraysize; i++) { A[i].d = B[i].d; } // copy B into first half of A
153  for (int i=arraysize; i<(2*arraysize); i++) { // initialize new heap values
154  newtuple = new mytuple; //
155  A[i].d = newtuple; //
156  A[i].d->m = -4294967296.0; //
157  A[i].d->i = 0; //
158  A[i].d->j = 0; //
159  A[i].d->k = i; //
160  }
161  delete [] B; // delete scratch space B
162  arraysize = 2*arraysize; // update size of array
163  return;
164 }
168 void myMaxheap::shrink() { // O(k) time
169  mytuple *newtuple;
170  hnode *B; // scratch space for contraction of A
171  B = new hnode [heaplimit]; //
172  for (int i=0; i<heaplimit; i++) { B[i].d = A[i].d; } // copy A into B
173  delete [] A; // delete old array of addresses
174  A = new hnode [arraysize/2]; // shrink A by factor of 2
175  for (int i=0; i<heaplimit; i++) { A[i].d = B[i].d; } // copy B into A
176  for (int i=heaplimit; i<(arraysize/2); i++) { // initialize new heap values
177  newtuple = new mytuple; //
178  A[i].d = newtuple; //
179  A[i].d->m = -4294967296.0; //
180  A[i].d->i = 0; //
181  A[i].d->j = 0; //
182  A[i].d->k = i; //
183  }
184  delete [] B; // delete scratch space B
185  arraysize = arraysize/2; // update size of array
186  return;
187 }
188 
189 // Insert + Update Functions --------------------------------------------------------------
193 mytuple* myMaxheap::insertItem(const mytuple newData) { // O(log k) time
194  int index;
195  mytuple *pointer;
196  if (heaplimit >= (arraysize-1)) { grow(); } // if heap is full, grow by factor of 2
197 
198  index = heaplimit; //
199  A[index].d->m = newData.m; // copy newData onto the bottom of the heap
200  A[index].d->i = newData.i; //
201  A[index].d->j = newData.j; //
202  A[index].d->k = index; //
203  pointer = A[index].d; // store pointer to container
204  heaplimit++; // note the larger heap
205  upsift(index); // upsift new item up to proper spot
206  if (heaplimit > 1) { isempty = false; }
207 
208  return pointer;
209 }
210 
214 void myMaxheap::updateItem(mytuple *address, mytuple newData) { // O(log k) time
215  double oldm = address->m;
216  address->m = newData.m; // udpate the dQ stored
217  address->j = newData.j; // udpate the community to which to join
218  if (oldm > newData.m) { downsift(address->k); } // downsift if old value was larger
219  else { upsift(address->k); } // upsift otherwise
220  return;
221 }
225 void myMaxheap::updateItem(mytuple *address, double newStored) { // O(log k) time
226  double oldm = address->m;
227  address->m = newStored; // udpate the dQ stored
228  if (oldm > newStored) { downsift(address->k); } // downsift if old value was larger
229  else { upsift(address->k); } // upsift otherwise
230  return;
231 }
236  mytuple *swap;
237  int smal = heaplimit-1;
238  int index = address->k;
239 
240  if (heaplimit==2) { // check if deleting last item in heap
241  A[1].d->m = -4294967296.0; // zero out root data
242  A[1].d->i = 0; //
243  A[1].d->j = 0; //
244  A[1].d->k = 1; //
245  isempty = true; // note the heap's emptiness
246  heaplimit--; // decrement size of heap to empty
247  } else {
248  A[index].d->m = -4294967296.0; // zero out the deleted item's data
249  A[index].d->i = 0; //
250  A[index].d->j = 0; //
251  swap = A[index].d; // swap this element with item at end of heap
252  A[index].d = A[smal].d; //
253  A[smal].d = swap; //
254  A[index].d->k = index; // note the change in locations
255  A[smal].d->k = smal; //
256  heaplimit--; // note change in heap size
257  downsift(index); // downsift moved element to new location; O(log k)
258  if ((heaplimit/2 > heapmin) && (heaplimit == (arraysize/2))) { shrink(); } // shrink by factor of 2 if necessary
259  }
260  return;
261 }
265 bool myMaxheap::heapIsEmpty() { return isempty; }
269 int myMaxheap::heapSize() { return heaplimit-1; }
270 
271 // Display Functions --------------------------------------------------------------------
276  for (int i=1; i<heaplimit; i++) {
277  std::cout << A[i].d <<"\t["<<A[i].d->k<<"]\tdQ = "<<A[i].d->m<<"\t"<<A[i].d->i<<" -> "<<A[i].d->j<<"\n"; }
278  return;
279 }
284  int limit;
285  if (heaplimit>10) { limit = 11; } else { limit = heaplimit; }
286  for (int i=1; i<limit; i++) {
287  std::cout << A[i].d <<"\t["<<A[i].d->k<<"]\tdQ = "<<A[i].d->m<<"\t"<<A[i].d->i<<" -> "<<A[i].d->j<<"\n"; }
288  return;
289 }
290 
291 // ------------------------------------------------------------------------------------
int upsift(int i)
Funktion um das Element von A[i] richtig in den Heap zu ordnen.
Definition: myMaxheap.cpp:109
int downsift(int i)
Funktion um das Element von A[i] richtig in den Heap zu ordnen.
Definition: myMaxheap.cpp:78
void updateItem(mytuple *address, mytuple newData)
Updated das Element an der angegeben Adresse mit dem uebergeben Wert.
Definition: myMaxheap.cpp:214
void printHeap()
Gibt die Elemente im Heap aus.
Definition: myMaxheap.cpp:275
mytuple returnMaximum()
Gibt das maximale Element im Heap zurueck. Keine Aenderungen im Heap.
Definition: myMaxheap.cpp:56
int returnArraysize()
Gibt die Groesse des Wertearrays zurueck.
Definition: myMaxheap.cpp:67
int parent(int i)
Gibt den Index des Elternknotens zurueck.
Definition: myMaxheap.cpp:140
~myMaxheap(void)
Definition: myMaxheap.cpp:36
int k
Index des Elements im Heap.
Definition: myElement.h:11
int left(int i)
Gibt den Index des Linken Kindex zurueck.
Definition: myMaxheap.cpp:132
int heapSize()
gibt die Groesse des heaps zurueck (maxheap-1).
Definition: myMaxheap.cpp:269
hnode * A
Arrayspeicher des Maxheaps.
Definition: myMaxheap.h:41
const int heapmin
Definition: myMaxheap.h:30
int returnHeaplimit()
Gibt den Index vom ersten ungenutzen Element zurueck.
Definition: myMaxheap.cpp:71
mytuple * insertItem(const mytuple newData)
Fuegt newData in den Heap ein und gibt die Adresse davon zurueck.
Definition: myMaxheap.cpp:193
bool heapIsEmpty()
True wenn der Heap leer ist, andersfalls false.
Definition: myMaxheap.cpp:265
mytuple popMaximum()
Gibt das maximale Element im Heap zurueck und entfernt es aus dem Heap. Ordnet dannach den Heap neu...
Definition: myMaxheap.cpp:46
void grow()
Erhoeht die Groesse des A Arrays.
Definition: myMaxheap.cpp:145
bool isempty
Gibt an ob der Heap leer ist. True..ist leer, False..nicht leer.
Definition: myMaxheap.h:44
int right(int i)
Gibt den Index des rechten Kindes zurueck.
Definition: myMaxheap.cpp:136
void deleteItem(mytuple *address)
Entfernt das Element an der angegeben Adresse.
Definition: myMaxheap.cpp:235
myMaxheap(void)
Definition: myMaxheap.cpp:4
void shrink()
Verringert die Groesse des A Arrays.
Definition: myMaxheap.cpp:168
int i
Zeilenindex des Elements.
Definition: myElement.h:9
double m
Abgelegter Wert/Parameter.
Definition: myElement.h:8
int arraysize
Groesse des Arrays.
Definition: myMaxheap.h:43
void printHeapTop10()
Gibt die groessten 10 Elemente im Heap aus.
Definition: myMaxheap.cpp:283
int j
Spaltenindex des Elements.
Definition: myElement.h:10
int heaplimit
Erstes unbenutzes Element im Heap.
Definition: myMaxheap.h:42
mytuple * d
Definition: myMaxheap.h:28