24 #define LCHILD(x) (2 * x + 1)
25 #define RCHILD(x) (2 * x + 2)
26 #define PARENT(x) ((x - 1) / 2)
47 if(
NULL != this->heap ) {
52 this->heap =
new Node[capacity];
53 if(
NULL == this->heap ) {
56 this->capacity = capacity;
77 while(index && maxCount < maxIter) {
86 if(value <= this->heap[parent].value) {
90 this->heap[index] = this->heap[parent];
96 FW_ASSERT(maxCount < maxIter, maxCount, maxIter);
100 this->heap[index].value = value;
101 this->heap[index].order = order;
102 this->heap[index].id = id;
118 value = this->heap[0].value;
119 id = this->heap[0].id;
127 this->heap[0]= this->heap[index];
139 return (this->size == this->capacity);
144 return (this->size == 0);
156 void MaxHeap::heapify() {
166 while(index <= this->size && maxCount < maxIter) {
176 if (left >= this->size) {
186 largest = this->max(left, largest);
189 if (right < this->size) {
192 largest = this->max(right, largest);
197 if (largest == index)
206 this->swap(index, largest);
213 FW_ASSERT(maxCount < maxIter, maxCount, maxIter);
220 FW_ASSERT(a < this->size, a, this->size);
221 FW_ASSERT(b < this->size, b, this->size);
233 if(aValue == bValue) {
243 if( aValue > bValue ) {
252 FW_ASSERT(a < this->size, a, this->size);
253 FW_ASSERT(b < this->size, b, this->size);
254 Node temp = this->heap[a];
255 this->heap[a] = this->heap[b];
256 this->heap[b] = temp;
265 while(index < this->size) {
269 if( left >= size && index == 0) {
271 index, this->heap[index].value, this->heap[index].
id);
273 else if( right >= size && left < size ) {
275 index, this->heap[index].value, this->heap[index].
id,
276 left, this->heap[left].value, this->heap[left].
id);
278 else if( right < size && left < size ) {
280 index, this->heap[index].value, this->heap[index].
id,
281 left, this->heap[left].value,this->heap[left].
id,
282 right, this->heap[right].value, this->heap[right].
id);