Exemplo n.º 1
0
static int
check_values(Heap *h,int idx,int depth)
{
    HeapElement *he_l = NULL;
    HeapElement *he_r = NULL;
    HeapElement *he_p = NULL;
    HeapInternCmp cmp_func;

    if (idx < 0 || idx >= HSIZE(h))
	return 0;

    if (h->hpMode == HEAP_MINIMIZE)
	cmp_func = heap_larger;
    else 
	cmp_func = heap_smaller;

    he_p = HARRAY(h,idx);

    if (HLEFT(idx) >= HSIZE(h))	/* No left child */
    	return 0;

    if (HRIGHT(idx) < HSIZE(h))	/* Has right child */
	he_r = HARRAY(h,HRIGHT(idx));
	
    he_l = HARRAY(h,HLEFT(idx));

    if ( cmp_func(h,he_p,he_l))
    {
	printf("*** Heap violates parent-lchild property.\n");
	printf("*** Left child (%d) is %s than parent (%d)\n",
		HLEFT(idx),
		h->hpMode == HEAP_MINIMIZE ? "smaller" : "larger",
		idx);
	printf("*** Depth %d\n",depth);
	printf("%.8f - %.8f = %.8f\n",Key2Double(he_l),Key2Double(he_p),
		Key2Double(he_l) - Key2Double(he_p));

	return -1;
    }

    if (he_r &&  cmp_func(h,he_p,he_r))
    {
	printf("*** Heap violates parent-rchild property.\n");
	printf("*** Right child (%d) is %s than parent (%d)\n",
		HRIGHT(idx),h->hpMode == HEAP_MINIMIZE ? "smaller" : "larger",
		idx);
	printf("*** Depth %d\n",depth);
	printf("%.8f - %.8f = %.8f\n",Key2Double(he_r),Key2Double(he_p),
		Key2Double(he_r) - Key2Double(he_p));

	return -1;
    }

    if (check_values(h,HLEFT(idx),depth+1))
	return -1;

    if (he_r)
	return check_values(h,HRIGHT(idx),depth+1);
    return 0;
}
Exemplo n.º 2
0
static int
heap_heapify(Heap *h,int idx)
{
    int 		l,r,largest;
    HeapInternCmp 	cmp_func;
    DBG(debug("heap_heapify(h=%p,idx=%d)\n",h,idx));

    l = HLEFT(idx);
    r = HRIGHT(idx);

    LLOG(l); LLOG(r);

    if (h->hpMode == HEAP_MAXIMIZE)
	cmp_func = heap_larger;
    else 
    	cmp_func = heap_smaller;

    if (l <= HSIZE(h) && cmp_func(h,HARRAY(h,l),HARRAY(h,idx)))
	largest = l; 
    else 
	largest = idx;

    if (r <= HSIZE(h) && cmp_func(h,HARRAY(h,r),HARRAY(h,largest)))
	largest = r;

    if (largest != idx)
    {
	heap_swap(h,idx,largest);
	return heap_heapify(h,largest);
    }

    return 0;
}
Exemplo n.º 3
0
/**Function*************************************************************

  Synopsis    [Moves the entry down.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Msat_HeapPercolateDown( Msat_Order_t * p, int i )
{
    int x = HHEAP(p, i);
    int Child;
    while ( HLEFT(i) < HSIZE(p) )
    {
        if ( HRIGHT(i) < HSIZE(p) && HCOMPARE(p, HHEAP(p, HRIGHT(i)), HHEAP(p, HLEFT(i))) )
            Child = HRIGHT(i);
        else
            Child = HLEFT(i);
        if ( !HCOMPARE(p, HHEAP(p, Child), x) )
            break;
        p->vHeap->pArray[i]            = HHEAP(p, Child);
        p->vIndex->pArray[HHEAP(p, i)] = i;
        i                              = Child;
    }
    p->vHeap->pArray[i]  = x;
    p->vIndex->pArray[x] = i;
}
Exemplo n.º 4
0
static void
heap_walk(Heap *h,int idx,int depth,HEAPWALKFUNC func)
{
    HeapElement *he;

    if (idx < 0 || idx >= HSIZE(h))
	return;

    he = HARRAY(h,idx);

    func(depth,he->heKey,he->heData);

    heap_walk(h,HLEFT(idx),depth + 1,func);
    heap_walk(h,HRIGHT(idx),depth + 1,func);
}
Exemplo n.º 5
0
static void
print_tree(Heap * h,int idx,int depth,
	   HEAPPRINTFUNC func)
{
    HeapElement *he;

    DBG(debug("print_tree(h=%p,idx=%d,depth=%d,func=%p)\n",
    	h,idx,depth,func));

    if (idx < 0 || idx >= HSIZE(h))
	return;
    he = HARRAY(h,idx);

    func(depth,he->heKey,he->heData);

    print_tree(h,HLEFT(idx),depth+1,func);
    print_tree(h,HRIGHT(idx),depth+1,func);
}
Exemplo n.º 6
0
/**Function*************************************************************

  Synopsis    [Checks the heap property recursively.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Msat_HeapCheck_rec( Msat_Order_t * p, int i )
{
    return i >= HSIZE(p) ||
        ( HPARENT(i) == 0 || !HCOMPARE(p, HHEAP(p, i), HHEAP(p, HPARENT(i))) ) &&
        Msat_HeapCheck_rec( p, HLEFT(i) ) && Msat_HeapCheck_rec( p, HRIGHT(i) );
}