void update(int a[], int pos, int info) { if(pos>numm || pos<1 || a[pos]==info) return; if(info < a[pos]) { a[pos]=info; chkUp(a, pos); } else { a[pos]=info; minHeapify(a,numm,pos); } }
//Function to perform min heapify to implement min priority queue. void MinPriority::minHeapify(int i,int heapsize, vector<nodeT *> & pqueue) { int l; int r; int smallest; l = 2*i+1; r = 2*i+2; //Comparing parent with left child if ((l<heapsize) && (pqueue[l]->nodevalue<=pqueue[i]->nodevalue)) //if ((l<heapsize) && (pqueue[l]->nodevalue < pqueue[i]->nodevalue)) { if(pqueue[l]->nodevalue==pqueue[i]->nodevalue) { if(pqueue[l]->nodename<pqueue[i]->nodename) smallest=l; else smallest=i; } else smallest=l; } else smallest = i; int t=smallest; //if ((r<heapsize) && (pqueue[r]->nodevalue < pqueue[smallest]->nodevalue)) if ((r<heapsize) && (pqueue[r]->nodevalue<=pqueue[smallest]->nodevalue)) if(pqueue[r]->nodevalue==pqueue[smallest]->nodevalue) { if(pqueue[r]->nodename<pqueue[smallest]->nodename) smallest=r; else smallest=t; } else smallest=r; if (smallest!=i) { swap(pqueue[smallest],pqueue[i]); minHeapify(smallest, heapsize, pqueue); } }
void minHeapify(int a[], int size, int i) { int l = 2*i; int r - 2*i + 1; int smallest = i; if(l < size && a[l] < a[smallest]) smallest = l; if(r < size && a[r] < a[smallest]) smallest = r; if(smallest != i) { swap(&a[i], &a[smallest]); minHeapify(a, size, smallest); } }
int MinHeap::extractMin() { if(heapSize <= 0) { return INT_MIN; } if(heapSize-- == 1) { return arr[0]; } int root = arr[0]; std::swap(0, heapSize); minHeapify(0); return root; }
void minHeapify (MinHeap* minHeap, int idx) //for the first call, idx is always 0 { int smallest, left, right; smallest = idx; //reset left = 2 * idx + 1; //left node index has this relation right = 2 * idx + 2; //right node index has this relation if (left < minHeap->size && minHeap->array[left]->freq < minHeap->array[smallest]->freq) smallest=left; //this is done, as it was not following the rule otherwise if (right < minHeap->size && minHeap->array[right]->freq < minHeap->array[smallest]->freq) smallest=right; //this is done, as it was not following the rule otherwise //the rule: children nodes have values larger than parent node if (smallest != idx) //meaning nothing needs to be changed! { swapMinHeapNode ( &minHeap->array[smallest], &minHeap->array[idx] ); //sending addresses of nodes minHeapify (minHeap, smallest); //recursive call of the function, till smallest = idx } }
void MinHeap::minHeapify(int index) { int left = arr[index * 2 + 1]; int right = arr[index * 2 + 2]; int smallest = index; if(left < heapSize && arr[index] > arr[left]) smallest = left; if(right < heapSize && arr[left] > arr[right]) smallest = right; if(smallest != index) { std::swap(arr[index], arr[smallest]); minHeapify(smallest); } }
/** Inserts a num into the data structure. left of median=maxheap right of median=minheap*/ void addNum(struct MedianFinder* mf, int num) { if(!mf) return; int i = compare(mf->maxheap->count, mf->minheap->count); printf("\n median=%lf, num=%d",mf->median,num); switch(i) { case -1: if(num<mf->median) insertMaxHeap(mf->maxheap, num); else{ int e = mf->minheap->arr[0]; insertMaxHeap(mf->maxheap,e); mf->minheap->arr[0]=num; minHeapify(mf->minheap, 0); } mf->median=(double)((mf->maxheap->arr[0]+mf->minheap->arr[0])/2); break; case 0: if(num<mf->median) { insertMaxHeap(mf->maxheap, num); mf->median= mf->maxheap->arr[0]; } else{ insertMinHeap(mf->minheap, num); mf->median= mf->minheap->arr[0]; } break; case 1: if(num<mf->median){ int e = mf->maxheap->arr[0]; insertMinHeap(mf->minheap,e); mf->maxheap->arr[0]=num; maxHeapify(mf->maxheap, 0); } else{ insertMinHeap(mf->minheap, num); } mf->median=(mf->maxheap->arr[0]+mf->minheap->arr[0])/2; break; default: break; } }
void minHeapify(struct MinHeap* minHeap, int idx) { int smallest = idx; int left = 2 * idx + 1; int right = 2 * idx + 2; if (left < minHeap->size && minHeap->array[left]->freq < minHeap->array[smallest]->freq) smallest = left; if (right < minHeap->size && minHeap->array[right]->freq < minHeap->array[smallest]->freq) smallest = right; if (smallest != idx) { swapMinHeapNode(&minHeap->array[smallest], &minHeap->array[idx]); minHeapify(minHeap, smallest); } }
void minHeapify(Heap *h, int idx) { if(!h) return; int left = 2*idx+1; int right = 2*idx+2; int smallest =idx; if(left<h->count && h->arr[idx]>h->arr[left]) smallest=left; if(right < h->count && h->arr[smallest] > h->arr[right]) smallest=right; if(smallest!=idx) { swap(&h->arr[smallest], &h->arr[idx]); minHeapify(h,smallest); } }
// Applies the minHeapify algorithm starting from the given index void MinHeap::minHeapify(int index) { // TODO int smallest = index; int size = heapSize; if (left(index) < size && heapNodes[left(index)]->frequency < heapNodes[smallest]->frequency) { smallest = left(index); } if (right(index) < size && heapNodes[right(index)]->frequency < heapNodes[smallest]->frequency) { smallest = right(index); } if (smallest != index) { exchange(smallest, index); minHeapify(smallest); } }
void minHeapify(int index) { assert(index < this->size()); int left_index = get_left_index(index); int right_index = get_right_index(index); int min_index = index; if (left_index < size() && heapArray[left_index] < heapArray[min_index]) { min_index = left_index; } if (right_index < size() && heapArray[right_index] < heapArray[min_index]) { min_index = right_index; } if (min_index != index) { std::swap(heapArray[min_index], heapArray[index]); minHeapify(min_index); } }
/* * @brief Through key comparisons reorders the heap to keep the * Elements in proper order * * @param i an interger containing the starting point of the * comparisons * * @return nothing * * Swaps Elements as needed to have the smallest key at the root, * and then key values ascending from there. */ void MinPriorityQ::minHeapify(int i) { unsigned int l = left(i); unsigned int r = right(i); int smallest; if(l < minHeap.size() && minHeap[l]->key < minHeap[i]->key) { smallest = l; } else { smallest = i; } if(r < minHeap.size() && minHeap[r]->key < minHeap[smallest]->key) { smallest = r; } if(smallest != i) { Element* temp = minHeap[i]; minHeap[i] = minHeap[smallest]; minHeap[smallest] = temp; minHeapify(smallest); } }
void minHeapify(minHeap *h, int i) { int smallest; int l = leftChild(i); int r = rightChild(i); if (l <= h->size && dist[h->array[l]] < dist[h->array[i]]) { smallest = l; } else { smallest = i; } if (r <= h->size && dist[h->array[r]] < dist[h->array[smallest]]) { smallest = r; } if (smallest != i) { swap(&h->array[i], &h->array[smallest]); minHeapify(h, smallest); } }
void minHeapify(int i,int length) { int lc=left(i); int rc=right(i); int sl; if((lc<length)&&(array[lc]<array[i])) sl=lc; else sl=i; if((rc<length)&&(array[rc]<array[sl])) sl=rc; if(sl!=i) { int temp=array[sl]; array[sl]=array[i]; array[i]=temp; minHeapify(sl,length); } }
void buildHeap(int A[], int p, int q, int flag) { int i; if(flag) { for(i = p+(q-p+2)/2; i >= p; i--) { minHeapify(A, p, q, i); } } else { for(i = p+(q-p+2)/2; i >= p; i--) { maxHeapify(A, p, q, i); } } return; }
void minHeapify (int A[], int p, int q, int i) { int l, r; int temp; l = left(i, p); r = right(i, p); int smallest = i; if((l <= q) && (A[l] < A[i])) smallest = l; if((r <= q) && (A[r] < A[smallest])) smallest = r; if(smallest != i) { temp = A[smallest]; A[smallest] = A[i]; A[i] = temp; minHeapify(A, p, q, smallest); } }
struct MinHeapNode * extractMin ( struct MinHeap * heap ) { // fetch the root node struct MinHeapNode * root = heap -> array [ 0 ]; // fetch the last node struct MinHeapNode * lastNode = heap -> array [ heap -> current_size - 1 ]; // set root to lastNode heap -> array [0] = lastNode; // update the position of last node heap -> current_position [ lastNode -> element ] = 0; // update the position of root node. it's discarded now heap -> current_position [root->element] = heap -> current_size - 1; // reduce the size by 1 heap -> current_size -= 1; // heapify root minHeapify(heap, 0); return root; }
void heapSort(int* A, int n){ /* * * * * * * * * * * * * * * * * * * * * */ /* Inputs: */ /* A : int array array of indices */ /* n : int number of element in array */ /* * * * * * * * * * * * * * * * * * * * * */ buildMaxHeap(A, n); int i; for(i = n - 1; i > 0; i--) { int temp = A[heapSize]; A[heapSize] = A[0]; A[0] = temp; heapSize--; minHeapify(A, 0); } }
// Return minimum element from all linked lists ListNode* extractMin( MinHeap* minHeap ) { if( isEmpty( minHeap ) ) return NULL; // The root of heap will have minimum value MinHeapNode temp = minHeap->array[0]; // Replace root either with next node of the same list. if( temp.head->next ) minHeap->array[0].head = temp.head->next; else // If list empty, then reduce heap size { minHeap->array[0] = minHeap->array[ minHeap->count - 1 ]; --minHeap->count; } minHeapify( minHeap, 0 ); return temp.head; }
void MinHeap::minHeapify(int idx) { int left = 2 * idx; int right = (2 * idx) + 1; int smallest = idx; if(left < count && nodes[left].frequency < nodes[idx].frequency) { smallest = left; } if(right < count && nodes[right].frequency < nodes[idx].frequency) { smallest = right; } if(smallest != idx) { nodes[smallest].root->min_heap_index = idx; nodes[idx].root->min_heap_index = smallest; swap(nodes[smallest], nodes[idx]); minHeapify(smallest); } }
int heapExtractMin(struct heap* H){ if(H->flag!=0){ printf("ExtractMin function is only supported in a min heap\n"); return -1; } int min; // Input: A: an array representing a heap // Output: The maximum element of A and A as a heap with this element removed // Running Time: O(log n) where n =heap-size[A] min=H->A[0]; int *t1,*t2; t1=&(H->A[0]); t2=&(H->A[H->heapSize-1]); //replacing with last index swap(t1,t2); H->heapSize=H->heapSize-1; minHeapify(H,0); return min; }//end of heapExtractMax()
//维护最小堆性质子程序 void PriorityQueue::minHeapify(int i) { int l = 2 * (i + 1) - 1; int r = 2 * (i + 1); int smallest; Node temp; if (l < heapsize && (PQpoint + l)->getKey() < (PQpoint + i)->getKey()) smallest = l; else smallest = i; if (r < heapsize && (PQpoint + r)->getKey() < (PQpoint + smallest)->getKey()) smallest = r; if (smallest != i) { temp = *(PQpoint + i); *(PQpoint + i) = *(PQpoint + smallest); *(PQpoint + smallest) = temp; minHeapify(smallest); } }
int kthLargest(int a[], int size, int k) { int minHeap[k]; int i; for(i = 0; i < k; i++) minHeap[i] = a[i]; buildMinHeap(minHeap, k); for(i = k; i < size; i++) { if(a[i] > minHeap[0]) { minHeap[0] = a[i]; minHeapify(minHeap, k, 0); } } return minHeap[0]; }
void minHeapify(int i) { int l = left(i); int r = right(i); int smallest; if(l<heap.length && heap.Arr[l] < heap.Arr[i]) { smallest = l; } else smallest = i; if(r<heap.length && heap.Arr[r] < heap.Arr[smallest]) smallest = r; if (smallest !=i) { int temp = heap.Arr[i]; heap.Arr[i] = heap.Arr[smallest]; heap.Arr[smallest] = temp; minHeapify(smallest); } }
void minHeapify(struct heap *H, int i){ int l=left(i); int r=right(i); int smallest; int *t1,*t2; if(l<=H->heapSize-1 && (H->A[l]< H->A[i])) smallest=l; else smallest=i; if(r<=H->heapSize-1 && (H->A[r]< H->A[smallest])) smallest=r; if(smallest!=i){ t1=&(H->A[i]); t2=&(H->A[smallest]); swap(t1,t2); minHeapify(H,smallest); } }//end of minHeapify
//Funcao que realiza a troca de posicoes entre pais e filhos para manter a prioridade do Heap de mínimo constante void minHeapify(HEAP * h, int i, short BlocoAtual){ int esq = FilhoEsquerda(i); int dir = FilhoDireita(i); int temp, temp2; int menor = i; if ((h->VETOR[i].flag == h->VETOR[esq].flag) && (h->VETOR[dir].flag == h->VETOR[esq].flag )) { if ((esq <= h->tamanhoAtual) && (h->VETOR[esq].valor < h->VETOR[menor].valor)) menor = esq; if ((dir <= h->tamanhoAtual) && (h->VETOR[dir].valor < h->VETOR[menor].valor)) menor = dir; }else if((h->VETOR[i].flag == BlocoAtual) && (h->VETOR[dir].flag == BlocoAtual) && (h->VETOR[esq].flag != BlocoAtual)) { if ((dir <= h->tamanhoAtual) && (h->VETOR[dir].valor < h->VETOR[menor].valor)) menor = dir; }else if((h->VETOR[i].flag == BlocoAtual) && (h->VETOR[dir].flag != BlocoAtual) && (h->VETOR[esq].flag == BlocoAtual)) { if ((esq <= h->tamanhoAtual) && (h->VETOR[esq].valor < h->VETOR[menor].valor)) menor = esq; }else if(((h->VETOR[i].flag != BlocoAtual) && (h->VETOR[dir].flag == BlocoAtual) && (h->VETOR[esq].flag == BlocoAtual))) { if ((esq <= h->tamanhoAtual) && (h->VETOR[esq].valor <= h->VETOR[dir].valor)) menor = esq; if ((dir <= h->tamanhoAtual) && (h->VETOR[dir].valor < h->VETOR[esq].valor)) menor = dir; }else if(((h->VETOR[i].flag != BlocoAtual) && (h->VETOR[dir].flag == BlocoAtual) && (h->VETOR[esq].flag != BlocoAtual))) { menor = dir; }else if(((h->VETOR[i].flag != BlocoAtual) && (h->VETOR[dir].flag != BlocoAtual) && (h->VETOR[esq].flag == BlocoAtual))) { menor = esq; } if ((menor != i) && (menor <= h->tamanhoAtual)) { //printf("HFY: %d sobe, %d desce\n",h->VETOR[menor].valor,h->VETOR[i].valor); temp = h->VETOR[i].valor; temp2 = h->VETOR[i].flag; h->VETOR[i].valor = h->VETOR[menor].valor; h->VETOR[i].flag = h->VETOR[menor].flag; h->VETOR[menor].valor = temp; h->VETOR[menor].flag = temp2; minHeapify(h,menor,BlocoAtual); } }
// heapify the minheap void VertexHeap::minHeapify(int i) { int l = left(i); int r = right(i); int smallest; if (l <= heapSize && Vertices[l].getKey() < Vertices[i].getKey()) smallest = l; else smallest = i; if (r <= heapSize && Vertices[r].getKey() < Vertices[smallest].getKey()) smallest = r; if (smallest != i && smallest <= heapSize) { Vertex temp = Vertices[i]; Vertices[i] = Vertices[smallest]; Vertices[smallest] = temp; minHeapify(smallest); } }
/* * The function to build a heap from a given array of integers * Note ~ The parameter size refers to the total number of elements in the array */ void buildHeap (struct minHeap* x, int arr[], int size) { int i; for (i = 0; i < size; i++) { if (x->size == 0) { x->elements = (struct node*)malloc(sizeof(struct node)); if (x->elements == NULL) { printf("Error in memory allocation\n"); exit(1); } } else { x->elements = realloc(x->elements, sizeof(struct node) * (x->size + 1)); if (x->elements == NULL) { printf("Error in memory allocation\n"); exit(1); } } struct node temp; temp.data = arr[i]; x->elements[x->size] = temp; x->size++; /* OR: x->elements[x->size].data = arr[i]; x->size++;*/ } for (i = (x->size - 1) / 2; i >= 0; i--) { minHeapify(x, i); } }
// Standard function to extract minimum node from heap struct MinHeapNode* extractMin(struct MinHeap* minHeap) { if (isEmpty(minHeap)) return NULL; // Store the root node struct MinHeapNode* root = minHeap->array[0]; // Replace root node with last node struct MinHeapNode* lastNode = minHeap->array[minHeap->size - 1]; minHeap->array[0] = lastNode; // Update position of last node minHeap->pos[root->v] = minHeap->size-1; minHeap->pos[lastNode->v] = 0; // Reduce heap size and heapify root --minHeap->size; minHeapify(minHeap, 0); return root; }
//makes sure value at i is correct, assumes rest of heap correct void minHeapify(double *A, int *B, int i, int size) { int l = left(i); int r = right(i); int smallest; if ((l < size) && (A[l] < A[i])) { smallest = l; } else { smallest = i; } if ((r < size) && (A[r] < A[smallest])) { smallest = r; } if (smallest != i) { SwitchAlphas(A, B, i, smallest); minHeapify(A, B, smallest, size); } }