void MinHeapify(MinHeap* heap, int root) { if (root > heap->size - 1) { return; } if (heap->values[root] > heap->values[LeftChild(root)]) { int tmp = heap->values[root]; heap->values[root] = heap->values[LeftChild(root)]; heap->values[LeftChild(root)] = tmp; MinHeapify(heap, LeftChild(root)); } if (heap->values[root] > heap->values[RightChild(root)]) { int tmp = heap->values[root]; heap->values[root] = heap->values[RightChild(root)]; heap->values[RightChild(root)] = tmp; MinHeapify(heap, RightChild(root)); } }
void BuildMinHeap(int* pBuffer, uint32_t nSize) { for (int i = nSize/2; i >= 0; --i) { MinHeapify(pBuffer, i, nSize); } }
int Merge (int arrays[][4], int k, int n, int result[]) { int i=0, j=0, r=0; HeapNode *node = NULL, *minnode = NULL; Heap *h = NULL; init_heap (&h, k); for (i=0; i<k; i++) { node = newnode(); node->data = arrays[i][0]; node->array = i; node->index = 1; HeapAdd (h, node, i); } MinHeap (h); r = 0; for (i=0; i<n*k; i++) { minnode = getMin (h); result[r++] = minnode->data; if (minnode->index < n) { minnode->data = arrays[minnode->array][minnode->index]; minnode->index++; } else { minnode->data = MAX; } MinHeapify (h, 0); } return 0; }
int RemoveMin(MinHeap* heap) { int min = heap->values[0]; heap->values[0] = heap->values[heap->size - 1]; heap->size--; MinHeapify(heap, 0); return min; }
void MinHeapify(int *H,int n,int k) // min-heapify function for setting the ordering property of min heap { int min=k; if((-n+2*k-1)>=0){if(H[-n+2*k-1]<H[min])min=(-n+2*k-1);} if((-n+2*k)>=0){if(H[-n+2*k]<H[min])min=(-n+2*k);} if(min==k)return; else {swap(H[k],H[min]);MinHeapify(H,n,min);} }
void ReverseHeapSort(int* pBuffer, unsigned int nLen) { BuildMinHeap(pBuffer, nLen); for (int i = nLen - 1; i > 0; --i) { std::swap(pBuffer[i], pBuffer[0]); MinHeapify(pBuffer, 0, i); } }
void MinHeap (Heap *h) { if (!h) return; int i = (h->size/2) + 1; for (; i>=0; i--) { MinHeapify (h, i); } return; }
// FOLLOWING ARE IMPLEMENTATIONS OF STANDARD MIN HEAP METHODS // FROM CORMEN BOOK // Constructor: Builds a heap from a given array a[] of given size MinHeap::MinHeap(MinHeapNode a[], int size) { heap_size = size; harr = a; // store address of array int i = (heap_size - 1)/2; while (i >= 0) { MinHeapify(i); i--; } }
void insert(heap *h, int val) { assert(h); h->data[h->size++] = val; //#define MINHEAP #ifdef MINHEAP /*min-heap proprity queue*/ MinHeapify(h); #else MaxHeapify(h); #endif }
long int extractMinHeap(MinHeap *A) { /* We assume that we never call the function when the heap is empty */ long int min = A->arr[1]; A->arr[1] = A->arr[A->heapSize]; (A->heapSize)--; MinHeapify(A, 1); return min; }
void MinHeap (Heap* h) { int i =0 ; i = (h->size - 1)/2; while (i>=0) { MinHeapify (h, i); i--; } return; }
// A recursive method to heapify a subtree with root at given index // This method assumes that the subtrees are already heapified void MinHeap::MinHeapify(int i) { int l = left(i); int r = right(i); int smallest = i; if (l < heap_size && harr[l].element < harr[i].element) smallest = l; if (r < heap_size && harr[r].element < harr[smallest].element) smallest = r; if (smallest != i) { swap(&harr[i], &harr[smallest]); MinHeapify(smallest); } }
void PartialMinHeapSort(int* pBuffer, uint32_t nLen, uint32_t nPartialSize) { BuildMinHeap(pBuffer, nLen); for (int i = nLen - 1; i > nLen - nPartialSize - 1; --i) { std::swap(pBuffer[i], pBuffer[0]); MinHeapify(pBuffer, 0, i); } for (int i = 0; i < nPartialSize + 1; ++i) { std::swap(pBuffer[i], pBuffer[nLen - 1 - i]); } }
void MinHeapify (Heap *h, int i) { int left=2*i+1, right=2*i+2, min=0; HeapNode *tmp; min = i; if (left < h->size && h->harr[left]->data < h->harr[i]->data) { min = left; } if (right < h->size && h->harr[right]->data < h->harr[min]->data) { min = right; } if (min != i) { tmp = h->harr[i]; h->harr[i] = h->harr[min]; h->harr[min] = tmp; MinHeapify (h, min); } return; }
void MinHeapify (Heap *h, int index) { int l, r, tmp, min; if (!h || index >= h->size) return; l = 2*index + 1; r = 2*index + 2; min = index; if (l < h->size && h->harr[l] < h->harr[index]) { min = l; } if (r < h->size && h->harr[r] < h->harr[min]) { min = r; } if (min != index) { tmp = h->harr[min]; h->harr[min] = h->harr[index]; h->harr[index] = tmp; MinHeapify (h, min); } return; }
void klargest (int arr[], int n, int k) { if (n <= 0 || k <= 0 || n < k ) return; int i = 0, min = 0; Heap *h = NULL; init_heap (&h, k); for (i=0; i<k; i++) { AddHeap (h, arr[i], i); } MinHeap (h); for (; i<n; i++) { Heapmin (h, &min); if (arr[i] > min) { ReplaceMin (h, arr[i]); MinHeapify (h, 0); } } printHeap (h); return; }
void deleteMin(int *H,int& n) // deletes the minimum element in the min heap { H[n-1]=H[0];n--;H=H+1; MinHeapify(H,n,n-1); }