int * min_heapify(int *pHeap, int i) { int iLeft; int iRight; int iSmallest; int iTemp; int SIZE = pHeap[0]; iLeft = left(i); iRight = right(i); if ( (iLeft <= SIZE) && (pHeap[iLeft] < pHeap[i]) ) { iSmallest = iLeft; } else { iSmallest = i; } if ( (iRight <= SIZE) && (pHeap[iRight] < pHeap[iSmallest]) ) { iSmallest = iRight; } if (iSmallest != i) { iTemp = pHeap[i]; pHeap[i] = pHeap[iSmallest]; pHeap[iSmallest] = iTemp; pHeap = min_heapify(pHeap, iSmallest); } return pHeap; }
/* max_heapify(s, largest, lastLeafIndex); } } */ void min_heapify(symbol s[], int semiRootIndex, int lastLeafIndex) { //printf("...semiRootIndex: %d, lastLeafIndex: %d\n", semiRootIndex, lastLeafIndex); int left = 2*semiRootIndex + 1; //left leaf int right = 2*semiRootIndex + 2; //right leaf int smallest = semiRootIndex; // index containing the smallest value // initialized to semiRootIndex //printf("...left: %d, right: %d, smallest: %d\n", left, right, smallest); if(left <= lastLeafIndex && s[left].freq < s[semiRootIndex].freq) smallest = left; if(right <= lastLeafIndex && s[right].freq < s[smallest].freq) smallest = right; /* if value of given index's value isn't smaller than one of its leaves', swap the smaller-valued leaf with the given node. */ if(smallest != semiRootIndex) { symbol temp = s[smallest]; s[smallest] = s[semiRootIndex]; s[semiRootIndex] = temp; /* percolating down (smallest is now the leaf index and it's parent now actually has a larger value, so the name 'smallest' has become temporarily misleading here. On the other hand, this leaf at index 'smallest' is now the semi-heap's root index to be heapified.*/ min_heapify(s, smallest, lastLeafIndex); } }
//Deletion in Min - Heap int delete_min(Heap H,int n) { int del = H.a[n-1]; H.a[n-1] = H.a[H.size_min]; min_heapify(H,n,H.size_min+1,n-1); return del; }
prq * min_heapify(prq *a,int i) { int smallest; int l = left(i); int r = right(i); if(r <= heapsize && ((a+r)->pri) < ((a+i)->pri)) smallest = r; else smallest = i; if(l <= heapsize && ((a+l)->pri) < ((a+smallest)->pri)) smallest = l; if(smallest != i) { prq *temp; temp = malloc(1*sizeof(prq)); temp->data = (a+i)->data; temp->pri = (a+i)->pri; (a+i)->data = (a+smallest)->data; (a+i)->pri = (a+smallest)->pri; (a+smallest)->data = temp->data; (a+smallest)->pri = temp->pri; min_heapify(a,smallest); } return a; }
/* rec way */ void min_heapify(HeapT* h, size_t index) { assert(h != NULL); HeapImplT* heap = (HeapImplT*) h; size_t left_index = LEFT_CHILD(index); size_t right_index = RIGHT_CHILD(index); size_t lowest_index = index; if (heap->size > left_index && heap->data[left_index] < heap->data[lowest_index] ) { lowest_index = left_index; } if (heap->size > right_index && heap->data[right_index] < heap->data[lowest_index] ) { lowest_index = right_index; } if (lowest_index != index) { swap(&heap->data[lowest_index], &heap->data[index]); min_heapify(h, lowest_index); } }
/***************** min_heapify() **************/ void min_heapify(minheapPTR hh , int i) { int smallest = i; int left = 2*i + 1; int right = 2*i + 2; if( left < hh->size && hh->array[left]->freq < hh->array[smallest]->freq ) smallest = left; if( left < hh->size && ( hh->array[left]->freq == hh->array[smallest]->freq)) { if( hh->array[left]->entry_time < hh->array[smallest]->entry_time) smallest = left; } if( right < hh->size && hh->array[right]->freq < hh->array[smallest]->freq ) smallest = right; if( right < hh->size && ( hh->array[right]->freq == hh->array[smallest]->freq)) { if( hh->array[right]->entry_time < hh->array[smallest]->entry_time) smallest = right; } if( left < hh->size && right < hh->size && smallest < hh->size) { printf("%c | %d\n",hh->array[left]->data,hh->array[left]->freq); printf("%c | %d\n",hh->array[smallest]->data , hh->array[smallest]->freq); printf("%c | %d\n",hh->array[right]->data , hh->array[right]->freq); } printf("inside min_heapify %d %d\n\n",smallest,hh->array[smallest]->freq); if( smallest != i ) { swapNode(&(hh->array[smallest]) , &(hh->array[i])); min_heapify(hh , smallest); } }
void build_heap() { int i; for(i=heap_size>>1; i>=0; i--) min_heapify(i); }
void put_median(int num) { int bal=check(); if(bal>=1) { if(num>=median) { insert_min(num); } else { insert_min(maxheap[1]); maxheap[1]=maxheap[maxheap[0]]; maxheap[0]--; max_heapify(1); insert_max(num); } } else { if(num>median) { insert_min(num); insert_max(minheap[1]); minheap[1]=minheap[minheap[0]]; minheap[0]--; min_heapify(1); } else { insert_max(num); } } return; }
void min_heapify(Node * min_heap_array, int i){ int smallest_node = 0; int left_child_index = 2 * i; int right_child_index = 2 * i + 1; // Verify we don't go outside our array size // See if the left child is smaller than parent node if( left_child_index <= MIN_HEAP_SIZE && min_heap_array[left_child_index].value < min_heap_array[i].value){ smallest_node = left_child_index; } else{ smallest_node = i; } // Verify we don't go outside our array size // See if the right child is smaller than smallest node (either parent or left child) if( right_child_index <= MIN_HEAP_SIZE && min_heap_array[right_child_index].value < min_heap_array[smallest_node].value){ smallest_node = right_child_index; } if( smallest_node != i){ int temp_val = min_heap_array[i].value; int temp_index = min_heap_array[i].index; min_heap_array[i].value = min_heap_array[smallest_node].value; min_heap_array[i].index = min_heap_array[smallest_node].index; min_heap_array[smallest_node].value = temp_val; min_heap_array[smallest_node].index = temp_index; min_heapify(min_heap_array, smallest_node); } }
void Build_min_heap(int adj_matrix[],int track_index[],int length) { int i; for(i=length/2;i>=0;i--) min_heapify(adj_matrix,track_index,i,length); array1=adj_matrix,array2=track_index; }
void build_min_heap(MinQueue *q) { int i; for(i=q->size/2;i>0;i--) { min_heapify(q,i); } }
void build_min_heap(long long *heap, int length) { int i; heap_size = length; for(i=(heap_size>>1)-1; i>=0; i--) min_heapify(heap, i); }
void get_k_min(int array[], int k, int len) { for (int i = 0; i < k; i++) { int min = array[0]; printf("%d\n",min); swap(&array[0], &array[len - i - 1]); min_heapify(array, 0, len - i - 1); } }
minheapNodePTR extractMin (minheapPTR hh ) { minheapNodePTR nn = hh->array[0]; /************* here is the problem i guess *************/ hh->array[0] = hh->array[hh->size - 1]; hh->size -= 1; min_heapify(hh,0); return nn; }
node_dist min_heap_pull(min_heap *target) { /* I have no idea if this is supposed to work like this, but whatever */ node_dist ret = *(target->cont); /*int i;*/ target->cont++; min_heapify(target); target->size--; return ret; }
static void row_sort(int *row_len, int *reorder_map, int rows) { int i; // build the initial heap first for (i = (rows - 2) >> 1; i >= 0; i--) { min_heapify(row_len, reorder_map, rows, i); } // move heap top(minimum) to the end and reheapify for (i = rows - 1; i > 0; i--) { swap_pos(row_len, 0, i); swap_pos(reorder_map, 0, i); min_heapify(row_len, reorder_map, i, 0); } }
void build_heap(int a[MAX]){ int size = MAX; int i, j; for(i=size/2; i>=0; --i){ min_heapify(a, i, MAX); } }
int * build_min_heap(int *pHeap) { int i; int SIZE = pHeap[0]; for(i=(SIZE/2); i>0; i--) { pHeap = min_heapify(pHeap,i); } return pHeap; }
struct EACH pop() { struct EACH rt = queue[0]; swap(0, count-1); --count; min_heapify(count, 0); return rt; }
//------------------------------------------------------------------------------ int heap_extract_min(int *heap,int &n) { if(n==0) return -1; int min=heap[1]; heap[1]=heap[n]; n--; min_heapify(heap,1,n); return min; }
void build_min_heap(HeapT* h) { assert(h != NULL); HeapImplT* heap = (HeapImplT*) h; int index = PARENT(heap->size - 1); for (; index >= 0; --index) { min_heapify(h, index); } }
void build_min_heap(heap* A) { A->heap_size = A->length; int i, n = A->length; for(i=n/2; i>=1; i--) { min_heapify(A,i); } return; }
//Extract minimum element from min heap HuffmanNode* HuffmanMinHeap::extractmin(){ HuffmanNode *smallestNode; HuffmanNode *lastNode; smallestNode=heapnodearray[0]; lastNode=heapnodearray[heapsize-1]; swap(0,heapsize-1); heapsize--; min_heapify(0); return smallestNode; }
void buildminHeap( minheapPTR hh ) { int n = hh->size - 1; int i; for(i = (n-1)/2; i >= 0 ; i--) { printf("BUILDING i = %d\n",i); min_heapify(hh,i); } }
int extract_min(MinQueue *q) { int min; assert(q->size > 0); min = q->heap[1]; q->heap[1] = q->heap[q->size]; q->size -= 1; min_heapify(q,1); return min; }
int min_heap_extract(struct heap *heap) { int min = heap->data[0]; heap->data[0] = heap->data[heap->size-1]; heap->size--; min_heapify(heap, 0); return min; }
void heap_sort(int a[MAX]){ int i; int temp; for(i=MAX-1; i>=0; i--){ temp = a[0]; a[0] = a[i]; a[i] = temp; min_heapify(a,0,i); } }
void *heap_extract_min(heap_t h) { if (unlikely(h->size < 1)) fatal_trace("heap underflow"); void *min = USER(h, 1); NODE(h, 1) = NODE(h, h->size); --(h->size); min_heapify(h, 1); return min; }
static int build_min_heap(pqueue *pq) { int i; int n = pq->size; for(i=n/2;i>0;i--){ min_heapify(pq, i); } return 0; }
static int extract_min(pqueue *pq) { int size = pq->size; int k = pq->a[1]; swap(pq->a, 1, size); pq->size--; min_heapify(pq, 1); return k; }