Exemplo n.º 1
0
void heapSort(Heap **heap) {
	int i, n = (*heap)->size, tmp;

	for(i = (n - 2) / 2; i >= 0; i--) {
		descer(heap, i);
	}
	for(i = n - 1; i > 0; i--) {
		tmp = (*heap)->h[0];
		(*heap)->h[0] = (*heap)->h[i];
		(*heap)->h[i] = tmp;
		(*heap)->size -= 1;
		descer(heap, 0);
	}
	(*heap)->size = n;
}
Exemplo n.º 2
0
/* recebe um vetor e corrige ele de forma a transforma-lo em um min-heap */
void corrigirMinHeap(Heap **heap) {
	if(*heap == NULL) {
		return;
	}

	int i;
	for(i = ((*heap)->size - 2) / 2; i >= 0; i--) {
		descer(heap, i);
	}
}
Exemplo n.º 3
0
/*ESTA FUNÇÃO NÃO RETORNA OS ELEMENTOS MIM. ELA SUMPÕE QUE OS MESMOS FORAM TOMANDOS IMEDIATAMENTE ANTES DA CHAMADA DA FUNÇÃO*/
bool  Heap::extract (){ /*pega o último elemento e coloca no lugar do primeiro, depois desce ele.*/
	if (size>0){
		heap[1] = heap[size];
		//int id = auxHeap[1];
		auxHeap[1] = auxHeap[size];
		descer(1);
		size--;
		//auxID[id] = 0;
		return true;
	} else return false;
}
Exemplo n.º 4
0
void  Heap::descer(int i){
		int filho = i*2;
		if (filho<=size){
			if (filho!=size){ 
				if (maiorIgualQuefloat(heap[filho+1],heap[filho])==false) filho++;
			}
			if (maiorQuefloat(heap[i],heap[filho])==true){
				float aux1 = heap[i];
				heap[i] = heap[filho];
				heap[filho] = aux1;
				
				int aux = auxHeap[i];
				auxHeap[i] = auxHeap[filho];
				auxHeap[filho] = aux;
			
				descer(filho);
			}
		}
		//if (auxHeap[i]>=0)auxID[auxHeap[i]] = i;
	}
Exemplo n.º 5
0
/*
    Remove o elemento de menor prioridade do heap min-max.
 */
int removerMenorPrioridade( HeapMinMax *heapMinMax ) {

    if ( estaVazio( heapMinMax ) ) {

        printf( "Heap min-max vazio - Underflow!" );
        exit( 1 );

    } else {

        int menor = heapMinMax->valores[1];
        heapMinMax->valores[1] = heapMinMax->valores[heapMinMax->tamanho];
        heapMinMax->tamanho--;

        descer( heapMinMax, 1 );

        return menor;

    }

}
Exemplo n.º 6
0
/*
    Remove o elemento de maior prioridade do heap min-max.
 */
int removerMaiorPrioridade( HeapMinMax *heapMinMax ) {

    if ( estaVazio( heapMinMax ) ) {

        printf( "Heap min-max vazio - Underflow!" );
        exit( 1 );

    } else {

        int indiceMaior = indiceMaiorPrioridade( heapMinMax );
        int maior = heapMinMax->valores[indiceMaior];
        heapMinMax->valores[indiceMaior] = heapMinMax->valores[heapMinMax->tamanho];
        heapMinMax->tamanho--;

        descer( heapMinMax, indiceMaior );

        return maior;

    }

}
Exemplo n.º 7
0
void removerHeap(Heap **heap) {
	(*heap)->h[0] = (*heap)->h[(*heap)->size - 1];
	(*heap)->size -= 1;
	(*heap)->h = realocarVetor((*heap)->h, (*heap)->size);
	descer(heap, 0);
}