Ejemplo n.º 1
0
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);
	}

}
Ejemplo n.º 2
0
//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);
    }

	
}
Ejemplo n.º 3
0
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);
	}
}
Ejemplo n.º 4
0
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
	}
}
Ejemplo n.º 6
0
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);
	}
}	
Ejemplo n.º 7
0
/** 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;
  }
}
Ejemplo n.º 8
0
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);
    }
}
Ejemplo n.º 9
0
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);
  }

}
Ejemplo n.º 10
0
// 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);
	}
}
Ejemplo n.º 11
0
    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);
    }
}
Ejemplo n.º 13
0
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);
	}
}
Ejemplo n.º 14
0
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);
  }
}
Ejemplo n.º 15
0
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;
}
Ejemplo n.º 16
0
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);
    }
}
Ejemplo n.º 17
0
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;
}
Ejemplo n.º 18
0
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);
    }
    
}
Ejemplo n.º 19
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;
}
Ejemplo n.º 20
0
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);
    }
}
Ejemplo n.º 21
0
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()
Ejemplo n.º 22
0
//维护最小堆性质子程序
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);
	}
}
Ejemplo n.º 23
0
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];
		
}
Ejemplo n.º 24
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);
  }
}
Ejemplo n.º 25
0
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
Ejemplo n.º 26
0
//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);
  }
}
Ejemplo n.º 27
0
// 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);
	}
}
Ejemplo n.º 29
0
// 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);
    }
  }