00001
00005
00006 class minheap;
00007 class maxHeap;
00008
00009 #include "elements.h"
00010 #include "ckheap.h"
00011
00012
00013
00014 minHeap::minHeap(int nsize)
00015 {
00016 size = nsize;
00017 h = new heapRecord[size];
00018 count = 0;
00019 }
00020
00021 minHeap::~minHeap()
00022 {
00023 delete [] h;
00024 }
00025
00026 int minHeap::insert(InfoRecord *x)
00027 {
00028 int current;
00029
00030 if (count < size) {
00031 h[count].info = x;
00032 h[count].deleted = 0;
00033 current = count;
00034 count++;
00035 } else {
00036 printf("minHeap overflow. \n") ;
00037 return -1;
00038 }
00039
00040 int parent = (current - 1)/2;
00041 while (current != 0)
00042 {
00043 if (h[current].info->load < h[parent].info->load)
00044 {
00045 swap(current, parent);
00046 current = parent;
00047 parent = (current-1)/2;
00048 }
00049 else
00050 break;
00051 }
00052 return 0;
00053 }
00054
00055 InfoRecord *minHeap::deleteMin()
00056 {
00057 if (count == 0) return 0;
00058
00059 InfoRecord *tmp = h[0].info;
00060 int best;
00061
00062 h[0] = h[count-1];
00063 count--;
00064
00065 int current = 0;
00066 int c1 = 1;
00067 int c2 = 2;
00068 while (c1 < count)
00069 {
00070 if (c2 >= count)
00071 best = c1;
00072 else
00073 {
00074 if (h[c1].info->load < h[c2].info->load)
00075 best = c1;
00076 else
00077 best = c2;
00078 }
00079 if (h[best].info->load < h[current].info->load)
00080 {
00081 swap(best, current);
00082 current = best;
00083 c1 = 2*current + 1;
00084 c2 = c1 + 1;
00085 }
00086 else
00087 break;
00088 }
00089 return tmp;
00090 }
00091
00092 InfoRecord *minHeap::iterator(heapIterator *iter){
00093 iter->next = 1;
00094 if (count == 0)
00095 return 0;
00096 return h[0].info;
00097 }
00098 InfoRecord *minHeap::next(heapIterator *iter){
00099 if (iter->next >= count)
00100 return 0;
00101 iter->next += 1;
00102 return h[iter->next - 1].info;
00103 }
00104
00105 int minHeap::least(int a, int b, int c){
00106 int smaller;
00107
00108 if(h[a].info->load < h[b].info->load)
00109 smaller=a;
00110 else
00111 smaller=b;
00112
00113 if(h[smaller].info->load < h[c].info->load)
00114 return smaller;
00115 else
00116 return c;
00117 }
00118
00119 void minHeap::update(InfoRecord *x) {
00120
00121
00122 int index;
00123 for (index=0; index<numElements(); index++)
00124 if (x == h[index].info) break;
00125 if (index == numElements()) {
00126 CmiAbort("minHeap: update a non-existent element!\n");
00127 }
00128 update(index);
00129 }
00130
00131 void minHeap::update(int index) {
00132 int parent = (index-1)/2;
00133
00134 if((index != 0) && h[index].info->load < h[parent].info->load) {
00135 swap(parent,index);
00136 update(parent);
00137 }
00138
00139 int c1 = 2*index+1;
00140 int c2 = 2*index+2;
00141
00142 if(c2<numElements()){
00143 int smaller = least(index,c1,c2);
00144 if(smaller != index){
00145 swap(smaller,index);
00146 update(smaller);
00147 return;
00148 }
00149 }
00150 if(c1<numElements() && h[c1].info->load < h[index].info->load) {
00151 swap(c1,index);
00152 update(c1);
00153 return;
00154 }
00155 }
00156
00157
00158
00159
00160 maxHeap::maxHeap(int nsize)
00161 {
00162 size = nsize;
00163 h = new heapRecord[size];
00164 count = 0;
00165 }
00166
00167 maxHeap::~maxHeap()
00168 {
00169 delete [] h;
00170 }
00171
00172 int maxHeap::numElements()
00173 {
00174 return count;
00175 }
00176
00177 int maxHeap::insert(InfoRecord *x)
00178 {
00179 int current;
00180
00181 if (count < size) {
00182 h[count].info = x;
00183 h[count].deleted = 0;
00184 current = count;
00185 count++;
00186 } else {
00187 printf("maxHeap overflow. \n");
00188 return -1;
00189 }
00190
00191 int parent = (current - 1)/2;
00192 while (current != 0)
00193 {
00194 if (h[current].info->load > h[parent].info->load)
00195 {
00196 swap(current, parent);
00197 current = parent;
00198 parent = (current-1)/2;
00199 }
00200 else
00201 break;
00202 }
00203 return 0;
00204 }
00205
00206 InfoRecord *maxHeap::deleteMax()
00207 {
00208 if (count == 0) return 0;
00209 InfoRecord *tmp = h[0].info;
00210 int best;
00211
00212 h[0] = h[count-1];
00213 count--;
00214
00215 int current = 0;
00216 int c1 = 1;
00217 int c2 = 2;
00218 while (c1 < count)
00219 {
00220 if (c2 >= count)
00221 best = c1;
00222 else
00223 {
00224 if (h[c1].info->load > h[c2].info->load)
00225 best = c1;
00226 else
00227 best = c2;
00228 }
00229 if (h[best].info->load > h[current].info->load)
00230 {
00231 swap(best, current);
00232 current = best;
00233 c1 = 2*current + 1;
00234 c2 = c1 + 1;
00235 }
00236 else
00237 break;
00238 }
00239 return tmp;
00240 }
00241
00242
00243 InfoRecord *maxHeap::iterator(heapIterator *iter){
00244 iter->next = 1;
00245 if (count == 0)
00246 return 0;
00247 return h[0].info;
00248 }
00249
00250 InfoRecord *maxHeap::next(heapIterator *iter){
00251 if (iter->next >= count)
00252 return 0;
00253 iter->next += 1;
00254 return h[iter->next - 1].info;
00255 }
00256