Exemple #1
0
//
// Enforce heap property on a path in the tree
//
static void heapify(PQueue* pq, unsigned int root) {

  unsigned int min_index = root;
  unsigned int lc = heap_lchild(root);
  unsigned int rc = heap_rchild(root);

  assert(pq && pq->elements);
  if ((lc < pq->cursize) && 
      (getPriority(pq->elements[lc]) < getPriority(pq->elements[min_index]))) {
    min_index = lc;
  }
  if ((rc < pq->cursize) && 
      (getPriority(pq->elements[rc]) < getPriority(pq->elements[min_index]))) {
    min_index = rc;
  }
  
  if (min_index != root) {
    PQ_elemType tmp_q = pq->elements[min_index];
    
    pq->elements[min_index] = pq->elements[root];
    pq->elements[root] = tmp_q;

    // This is triangle specific: Need to update triangle's pointer to
    // itself in the PQ
    pq->elements[min_index]->pqIndex = min_index;
    pq->elements[root]->pqIndex = root;
    
    heapify(pq, min_index);
  }
}   
Exemple #2
0
static void heapify(PQueue* pq, unsigned int root) {

  unsigned int min_index = root;
  unsigned int lc = heap_lchild(root);
  unsigned int rc = heap_rchild(root);

  assert(pq && pq->elements);
  if ((lc < pq->cursize) &&
      (compare_element(pq->elements[lc], pq->elements[min_index]) < 0)) {
    min_index = lc;
  }
  if ((rc < pq->cursize) && 
      (compare_element(pq->elements[rc], pq->elements[min_index]) < 0)) {
    min_index = rc;
  }
  
  if (min_index != root) {
    elemType tmp_q = pq->elements[min_index];
    
    pq->elements[min_index] = pq->elements[root];
    pq->elements[root] = tmp_q;
    
    heapify(pq, min_index);
  }
}