Exemplo n.º 1
0
void BuildHeap(ElementType* Numbers, int Size)
{
  int i;

  for (i = Size / 2; i >= 1; --i)
    FixDown(Numbers, i, Size);
}
Exemplo n.º 2
0
void AdjustRandItemInHeap(ElementType* Heap, int k, int Size)
{
  if (k > 1 && Heap[k] -> Value > Heap[k/2] -> Value)
    FixUp(Heap, k, Size);
  else 
    FixDown(Heap, k, Size);
}
Exemplo n.º 3
0
int PQdelmin(PrioQ* PQ) {
    int *heap = PQ->heap;
    int *index = PQ->index;
    int aux;
    int N;

    N = PQ->N;

    if(N == 0)
        return -1;


    /* switch places in table, and update index table */
    aux = heap[N - 1];

    heap[N - 1] = heap[0];
    index[ heap[0] ] = N - 1;

    heap[0] = aux;
    index[ aux ] = 0;

    PQ->N--;
    FixDown(PQ, 0);


    return heap[N - 1];
}
Exemplo n.º 4
0
void PQupdateNodeHighPrio(PrioQ *PQ, int node) {
    int *heap = PQ->heap;
    int *wt = PQ->wt;
    int heapIndex = PQ->index[node];
    /* verify if index corresponds to heap's highest priority */
    if(heapIndex == 0)
        FixDown(PQ, 0);
    /* try to FixUp first */
    else if(wt[ heap[heapIndex] ] < wt[ heap[(heapIndex - 1)/2] ]) {
        FixUp(PQ, heapIndex);
        return;
    }
    /* if we dont FixUp we can FixDown */
    FixDown(PQ, heapIndex);
    return;
}
Exemplo n.º 5
0
void PQupdate(PrioQ *PQ) {
    int i = 0;

    for(i = PQ->N/2 -1; i >= 0; i--) {
        FixDown(PQ, i);
    }
}
Exemplo n.º 6
0
/*
 * Retira e devolve o primeiro elemento da fila.
 */
void* RetiraDaFila( pq fila ) {
    void* top;

    top = fila -> table[ 1 ];
    fila -> table[ 1 ] = fila -> table[ fila -> n_items ];
    fila -> n_items --;
    FixDown( 1, fila );

    return top;
}
Exemplo n.º 7
0
int RemoveMax(Heap * h, int *node_distance, int**heap_place, int *node_hops)
{
  int t;

  if ((h)->n_elements > 0) {

    t = ((h)->heapdata)[0];
    ((h)->heapdata)[0] = ((h)->heapdata)[(h)->n_elements - 1];
    //((h)->heapdata)[(h)->n_elements - 1] = t;
    (*heap_place)[((h)->heapdata)[(h)->n_elements - 1]-1]=0;
    (*heap_place)[t-1]=-1;
    (h)->n_elements--;
    FixDown(h, 0, node_distance, &(*heap_place), node_hops);
	 //(*heap_place)[t-1]=-1;
  /*  for(i=0; i<(h)->n_elements; i++)printf("%d\t", ((h)->heapdata)[i]);
    printf("\n\n");*/
    return t;
  }

  return -1;
}