static void siftdown(cp_heap *h, unsigned int p) { unsigned int c; #ifdef _HEAP_TRACE DEBUGMSG("siftdown start\n"); heap_info(h); fflush(stdout); #endif for (c = LEFTCHILD(p); c < h->heap_end; p = c, c = LEFTCHILD(p)) { #ifdef _HEAP_TRACE DEBUGMSG(" c = %i, h->heap_end = %i\n", c, h->heap_end); #endif if (h->comp(h->heap[c], h->heap[c + 1]) <= 0) c++; /* c points to the largest among the children of p */ if (h->comp(h->heap[p], h->heap[c]) <= 0) swap(&(h->heap[p]), &(h->heap[c])); else return; } if (c == h->heap_end && h->comp(h->heap[p], h->heap[c]) <= 0) swap(&(h->heap[p]), &(h->heap[c])); }
static void siftdown( Bh *h, unsigned int p ) { unsigned int c; _( printf( "siftdown start\n" ) ); for( c = LEFTCHILD( p ); c < h->heap_end; p = c, c = LEFTCHILD( p ) ) { _( printf( " c = %i, h->heap_end = %i\n", c, h->heap_end ) ); if( h->comp( h->heap[c], h->heap[c + 1] ) <= 0 ) { c++; } /* c points to the largest among the children of p */ if( h->comp( h->heap[p], h->heap[c] ) <= 0 ) { swap( &( h->heap[p] ), &( h->heap[c] ) ); } else { return; } } if( c == h->heap_end && h->comp( h->heap[p], h->heap[c] ) <= 0 ) { swap( &( h->heap[p] ), &( h->heap[c] ) ); } }
int check_heap(int *heap, int size) { int left, right, i; for (i = 0; i < size/2; i++) { left = LEFTCHILD(i); right = RIGHTCHILD(i); if (left < size && heap[i] > heap[left]) return 0; if (right < size && heap[i] > heap[right]) return 0; } return 1; }
void bubble_down(int *heap, int size, int index) { int left, right, tmp, smallest; smallest = index; left = LEFTCHILD(index); right = RIGHTCHILD(index); if (left < size && heap[left] < heap[smallest]) smallest = left; if (right < size && heap[right] < heap[smallest]) smallest = right; if (smallest != index) { SWAP(heap[smallest], heap[index], tmp); bubble_down(heap, size, smallest); } }