Ejemplo n.º 1
0
/*	Adjust heap to maintain heap property

	param: 	heap	pointer to the heap
	param: 	max		index to adjust up to  (but not included)
	param: 	pos		position index where the adjustment starts
	pre:	max <= size
	post:	heap property is maintained for nodes from index pos to index max-1  (ie. up to, but not including max)
 */
void _adjustHeap(DynArr *heap, int max, int pos)
{
    assert(max <= sizeDynArr(heap));
    int leftidx = (pos*2)+1;
    int rightidx = (pos*2)+2;
    int smallidx;
    // TYPE temp;
    if(rightidx < max) { //2 Children
        smallidx = _smallerIndexHeap(heap, leftidx, rightidx);
        if(compare(heap->data[smallidx], heap->data[pos]) == -1) {
            /*temp = heap->data[smallidx];
            heap->data[smallidx] = heap->data[pos];
            heap->data[pos] = temp;*/
            swapDynArr(heap, pos, smallidx);
            _adjustHeap(heap, max, smallidx);
        }
    }
    else if(leftidx < max) { // 1 Child
        if(compare(heap->data[pos], heap->data[leftidx]) == -1) {
            //heap->data[pos] = heap->data[leftidx];
            swapDynArr(heap, pos, leftidx);
            _adjustHeap(heap, max, leftidx);
        }

    }
}
Ejemplo n.º 2
0
void _adjustHeap(DynArr *heap, int max, int pos, comparator compare)
{
	//printf("\n\n********new fucntion start \n Adjusting with max: %d, position: %d \n", max, pos);
	int leftChild = (2*pos)+1;
	int rightChild = (2*pos)+2;
	int smallest;


	if(rightChild < max){	/*two children*/
		smallest = _smallerIndexHeap(heap, leftChild, rightChild, compare);
		//get index of smallest child
		if(compare(getDynArr(heap, pos), getDynArr(heap, smallest))== 1){
			swapDynArr(heap, pos, smallest);
			_adjustHeap(heap, max, smallest, compare);
		}
	}
	else if(leftChild < max){	/*one child*/
		if(compare(getDynArr(heap, pos), getDynArr(heap, leftChild)) == 1){
			swapDynArr(heap, pos, leftChild);
			_adjustHeap(heap, max, leftChild , compare);
	
		}
	
	}
	else{ 					/*no children*/
		//printf("No children, exiting...\n");
	}
	
}
Ejemplo n.º 3
0
/*	Adjust heap to maintain heap property

	param: 	heap	pointer to the heap
	param: 	max		index to adjust up to  (but not included)
	param: 	pos		position index where the adjustment starts
	pre:	max <= size
	post:	heap property is maintained for nodes from index pos to index max-1  (ie. up to, but not including max)
*/
void _adjustHeap(DynArr *heap, int max, int pos)
{
   /* FIXME */
  assert(max <= sizeDynArr(heap));

  int left = (2 * pos) + 1;
  int right = (2 * pos) + 2;
  int small;
  
  if(right < max)
    {
      small = _smallerIndexHeap(heap, left, right);
      // can't alter type.h, but this would have been cleaner
      // if(heap->data[small].priority < heap->data[pos].priority)
      if (compare(getDynArr(heap,small),getDynArr(heap,pos)) == -1)
	{
	  swapDynArr(heap, small, pos); 
	  _adjustHeap(heap, max, small);
	}
    }
  else if(left < max)
    {
      // here is the type.h problem again
      // if(heap->data[left].priority < heap->data[pos].priority)
      if (compare(getDynArr(heap,left),getDynArr(heap,pos)) == -1)
	{
	  swapDynArr(heap, left, pos);
	  _adjustHeap(heap, max, left);
	}
    }
}
Ejemplo n.º 4
0
/*	Adjust heap to maintain heap property

	param: 	heap	pointer to the heap
	param: 	max		index to adjust up to  (but not included)
	param: 	pos		position index where the adjustment starts
	pre:	max <= size
	post:	heap property is maintained for nodes from index pos to index max-1  (ie. up to, but not including max)
*/
void _adjustHeap(DynArr *heap, int max, int pos)
{
   /* FIXME */
   assert(max <= heap->size);
   int rghtIdx = pos * 2 + 2;
   int leftIdx = pos * 2 + 1;

   if(rghtIdx < max)        //if 2 children nodes exist
   {
        int minIdx = _smallerIndexHeap(heap, rghtIdx, leftIdx); //smaller of children
        if(compare(pos, minIdx) == -1)           //if parent < child
        {
            swapDynArr(heap, pos, minIdx);      //swap parent with child
            _adjustHeap(heap, (max - 1), minIdx);     //call function with new current node
        }
   }
   else if(leftIdx < max)   //if 1 child node exists it must be the left
   {
       if(compare(pos, leftIdx) == -1)          //if parent < child
       {
            swapDynArr(heap, pos, leftIdx);     //swap parent with child
            _adjustHeap(heap, (max - 1), leftIdx);    //call function with new current node
       }
   }
}
Ejemplo n.º 5
0
/*	Adjust heap to maintain heap property

	param: 	heap	pointer to the heap
	param: 	max		index to adjust up to  (but not included)
	param: 	pos		position index where the adjustment starts
	pre:	max <= size
	post:	heap property is maintained for nodes from index pos to index max-1  (ie. up to, but not including max)
*/
void _adjustHeap(DynArr *heap, int max, int pos)
{
   /* FIXME */
	
	int left, right, small;
	
	assert(max <= sizeDynArr(heap)); 
	
	left = 2 * pos +1;
	right = 2 * pos +2; 

	if(right < max)
	{	
		small = _smallerIndexHeap(heap, left, right);
		
		if(compare(getDynArr(heap, small), getDynArr(heap, pos)) == -1)
		{	
			swapDynArr(heap, pos, small);
		
			_adjustHeap(heap, max, small);
		}
	}
	else if(left <  max)
	{ 	
		if(compare(getDynArr(heap, left), getDynArr(heap, pos)) == -1)
		{	
			swapDynArr(heap, pos, left);
		
			_adjustHeap(heap, max, left);
		}
	}

}
Ejemplo n.º 6
0
/*	Add a node to the heap

	param: 	heap	pointer to the heap
	param: 	node	node to be added to the heap
	pre:	heap is not null
	post:	node is added to the heap
*/
void addHeap(DynArr *heap, TYPE val)
{
    /* FIXME */

    assert(heap != 0);

    /* iterators for heap */
    int parentIt;

    //int posIt = sizeDynArr(heap); //same result
    int posIt = heap->size;

    /* add val to end of array, then adjust position */
    addDynArr(heap, val);

    /* percolate from leaves to root and swap if/where appropriate */
    while (posIt != 0)
    {
        parentIt = (posIt-1)/2;
        if (compare(getDynArr(heap, posIt), getDynArr(heap, parentIt) ) == -1) //posIt is < parent, so swap
        {
            swapDynArr(heap, posIt, parentIt);
            posIt = parentIt;                                                  //onward towards the root
        }
        else                                                                   //posIt is > or = to parent, NO swap
        {
            return;
        }
    }

}
Ejemplo n.º 7
0
void sortHeap(DynArr *heap)
{
   /* FIXME */
    for (int i = sizeDynArr(heap)-1; i > 0; i--){
        swapDynArr(heap, 0, i);
        _adjustHeap(heap, i, 0);
    }
}
Ejemplo n.º 8
0
void _adjustHeap(DynArr *heap, int max, int pos)
{
    /* FIXME */
    /* Help w/ this function from tutor Dustin Castor: I was overusing compare() */

    assert (max <= sizeDynArr(heap));
    int leftChild = 2 * pos + 1;
    int rightChild = 2 * pos + 2;
    int iSmall = 0;

    /* if we have two children */

    if (rightChild < max)  //if (compare(getDynArr(heap, rightChild), getDynArr(heap, max) ) == -1)
    {
        /* get index of smallest child */
        iSmall = _smallerIndexHeap (heap, leftChild, rightChild);

            /* if value at pos > value of smallest child */
            //if (pos > iSmall)
            if(compare(getDynArr(heap, pos), getDynArr(heap, iSmall) ) == 1)
            {
                /* swap with smallest child */
                swapDynArr(heap, iSmall, pos);

                /* call adjustHeap w/ max and index of smallest child */
                _adjustHeap(heap, max, iSmall);
            }
    }
    /* if we have one child-- it'll be on L bc "complete tree" */
    else if (leftChild < max)   //else if(compare(getDynArr(heap, leftChild), getDynArr(heap, max) ) == -1)
    {
        /* if value at pos > value of child */
        //if (pos > leftChild)
        if(compare(getDynArr(heap, pos), getDynArr(heap, leftChild) ) == 1)
        {
            /* swap with smallest child */
            swapDynArr(heap, leftChild, pos);

            /* call adjustHeap w/ max and index of LEFT child (the only option) */
            _adjustHeap(heap, max, leftChild);
        }
    }
    else /* else: no children, done (BASE CASE)*/
        return;
}
Ejemplo n.º 9
0
void sortHeap(DynArr *heap, comparator compare)
{
	int i;
	_buildHeap(heap, compare);
	for(i= sizeDynArr(heap)-1; i>0; i--){
		swapDynArr(heap,0,i);
		_adjustHeap(heap,i,0,compare);
	}
}
Ejemplo n.º 10
0
void sortHeap(DynArr *heap)
{
    _buildHeap(heap);
    int i;
    for(i=sizeDynArr(heap)-1; i>0; --i) {
        swapDynArr(heap, i, 0);
        _adjustHeap(heap, i, 0);
    }

}
Ejemplo n.º 11
0
/*	Adjust heap to maintain heap property

	param: 	heap	pointer to the heap
	param: 	max		index to adjust up to  (but not included)
	param: 	pos		position index where the adjustment starts
	pre:	max <= size
	post:	heap property is maintained for nodes from index pos to index max-1  (ie. up to, but not including max)
*/
void _adjustHeap(DynArr *heap, int max, int pos)
{
	int left = pos*2 + 1;
	int right = pos*2 + 2;

	if(right < max){
		int child = _smallerIndexHeap(heap, left, right);
		if (getDynArr(heap, child) < getDynArr(heap, pos)){
			swapDynArr(heap, child, pos);
			_adjustHeap(heap, max, child);
		}
	}
	else if(left < max){
		int child = left;
		if(getDynArr(heap, child) < getDynArr(heap, pos)){
			swapDynArr(heap, child, pos);
			_adjustHeap(heap, max, child);
		}
	}
}
Ejemplo n.º 12
0
void sortHeap(DynArr *heap)
{
    int maxpos;
    _buildHeap(heap);

    for(maxpos = heap->size - 1; maxpos >= 0; maxpos--){
        swapDynArr(heap, maxpos, 0);
        _adjustHeap(heap, maxpos - 1, 0);
    }

}
Ejemplo n.º 13
0
/*	Adjust heap to maintain heap property

	param: 	heap	pointer to the heap
	param: 	max		max index of the heap nodes in the dynamic array
	param: 	pos		position index where the adjustment starts
	pre:	none
	post:	heap property is maintained for nodes from index pos to index max
*/
void _adjustHeap(DynArr *heap, int max, int pos)
{
    int left = ((2 * pos) + 1);
    int right = ((2 * pos) + 2);
    int small;

    if(right <= max){
        small = _smallerIndexHeap(heap, left, right);
        if(heap->data[small].priority < heap->data[pos].priority){
            swapDynArr(heap, small, pos);
            _adjustHeap(heap, max, small);
        }
    }
    else if(left <= max){
        if(heap->data[left].priority < heap->data[pos].priority){
            swapDynArr(heap, left, pos);
            _adjustHeap(heap, max, left);
        }
    }
}
Ejemplo n.º 14
0
/*	Adjust heap to maintain heap property

    param:  heap	pointer to the heap
    param:  max		index to adjust up to  (but not included)
    param:  pos		position index where the adjustment starts
    pre:	max <= size
    post:	heap property is maintained for nodes from index pos to index max-1  (ie. up to, but not including max)
 */
void _adjustHeap(DynArr *heap, int max, int pos)
{
    assert( heap != NULL );
    assert( max <= sizeDynArr( heap ) );
    int left_child = pos * 2 + 1;
    int right_child = pos * 2 + 2;
    int sml_child = -1;

    if( right_child < max )
        {
        /* Check for a second child */
        if( left_child < max )
            {
            /* Determine the smallest child */
            sml_child = _smallerIndexHeap( heap, left_child, right_child );
            }
        else
            {
            /* Only one child, so smallest by default */
            sml_child = right_child;
            }
        if( compare( getDynArr( heap, sml_child ), getDynArr( heap, pos ) ) == -1 )
            {
            swapDynArr( heap, pos, sml_child );
            }
        _adjustHeap( heap, max, sml_child );
        }

    /* Only one child */
    else if( left_child < max )
        {
        if( compare( getDynArr( heap, left_child ), getDynArr( heap, pos ) ) == -1 )
            {
            swapDynArr( heap, pos, left_child );
            }
        _adjustHeap( heap, max, left_child );
        }

    /* No children, so done. */
}
Ejemplo n.º 15
0
void sortHeap(DynArr *heap)
{
	_buildHeap(heap);

	int last = heap->size - 1;

	while (last > 0)
	{
		swapDynArr(heap, 0, last);
		_adjustHeap(heap, last, 0);
		last--;
	}
}
Ejemplo n.º 16
0
void sortHeap(DynArr *heap)
{
    assert( heap != NULL );
    assert( sizeDynArr( heap ) > 0 );

    _buildHeap( heap );
    for( int i = ( sizeDynArr( heap ) - 1 ); i > 0; --i )
        {
        /* Swap the outer two elements in the heap, then adjust it */
        swapDynArr( heap, i, 0 );
        _adjustHeap( heap, i, 0 );
        }
}
Ejemplo n.º 17
0
void sortHeap(DynArr *heap)
{
   /* FIXME */
    assert(heap->size != 0);
    _buildHeap(heap);                           //build heap data structure

    int last = sizeDynArr(heap) - 1;            //last element of array
    while(last != 0)
    {
        swapDynArr(heap, last, 0);              //swap elements between 1st and last element
        _adjustHeap(heap, last, 0);
        last--;
    }
}
Ejemplo n.º 18
0
/*	Add a node to the heap

	param: 	heap	pointer to the heap
	param: 	node	node to be added to the heap
	pre:	heap is not null
	post:	node is added to the heap
*/
void addHeap(DynArr *heap, TYPE val)
{
    /* FIXME */
    int parent;
    int pos = sizeDynArr(heap);
    addDynArr(heap, val);
    while (pos != 0){
        parent = (pos - 1) / 2;
        if (compare(getDynArr(heap, pos), getDynArr(heap, parent)) == -1){
            swapDynArr(heap, parent, pos);
            pos = parent;
        }
        else return;
    }
}
Ejemplo n.º 19
0
void sortHeap(DynArr *heap)
{
   /* FIXME */
  assert(!isEmptyDynArr(heap));
  
  //_buildHeap(heap);
  int max = heap->size - 1;
  
  for(int i = max; i > 0; i--)
    {
      swapDynArr(heap, i, 0);
      _adjustHeap(heap, i, 0);
    }

}
Ejemplo n.º 20
0
/*	Add a node to the heap

	param: 	heap	pointer to the heap
	param: 	node	node to be added to the heap
	pre:	heap is not null
	post:	node is added to the heap
 */
void addHeap(DynArr *heap, TYPE val)
{
    int parent;

    addDynArr(heap, val);
    int pos = sizeDynArr(heap)-1;
    while(pos != 0) {
        parent = (pos-1)/2;
        if (compare(getDynArr(heap,pos),getDynArr(heap,parent)) == -1) {
            swapDynArr(heap, pos, parent);
            pos = parent;
        }
        else {
            return;
        }
    }
}
Ejemplo n.º 21
0
/*	Remove the first node, which has the min priority, from the heap

	param: 	heap	pointer to the heap
	pre:	heap is not empty
	post:	the first node is removed from the heap
*/
void removeMinHeap(DynArr *heap)
{
    /* FIXME */

    assert (sizeDynArr(heap) > 0);

    int fin = (sizeDynArr(heap) - 1);   //last element

    /* swap the last element for the first, then remove the last position */
    swapDynArr(heap, fin, 0);

    removeAtDynArr(heap, fin);          //shifts any elements past last element to left and reduces size

    /* rebuild the heap */
    _adjustHeap(heap, fin, 0);

}
Ejemplo n.º 22
0
/*	Add a node to the heap

	param: 	heap	pointer to the heap
	param: 	node	node to be added to the heap
	pre:	heap is not null
	post:	node is added to the heap
*/
void addHeap(DynArr *heap, TYPE val)
{
    /* FIXME */
    int parent;
int pos = sizeDynArr(heap);
addDynArr(heap, val); /* adds to end – now need to adjust position */
	while(pos != 0){
		parent = (pos-1) / 2;
		if ( heap->data[pos] < heap->data[parent]){
			swapDynArr(heap, pos, parent);
			pos = parent;
		}
		else return;
	}


}
Ejemplo n.º 23
0
/*	Add a node to the heap

	param: 	heap	pointer to the heap
	param: 	node	node to be added to the heap
	pre:	heap is not null
	post:	node is added to the heap
*/
void addHeap(DynArr *heap, TYPE val)
{
    addDynArr(heap, val);

	int curPos = sizeDynArr(heap) - 1;

	while(curPos > 0)
	{
		int parent = (curPos - 1) / 2;
		if(compare(getDynArr(heap, curPos), getDynArr(heap, parent)) == -1)
		{
			swapDynArr(heap, curPos, parent);
			curPos = parent;
		}
		else
			break;
	}

}
Ejemplo n.º 24
0
/*	Add a node to the heap

	param: 	heap	pointer to the heap
	param: 	node	node to be added to the heap
	pre:	heap is not null
	post:	node is added to the heap
*/
void addHeap(DynArr *heap, TYPE node)
{
    int currIdx;
    int parIdx;
    TYPE parVal;

    addDynArr(heap, node);
    currIdx = sizeDynArr(heap) - 1;
    parIdx = (currIdx - 1) / 2;
    parVal = heap->data[parIdx];

    while(currIdx > 0 && compare(node, parVal) == -1 ){
        swapDynArr(heap, currIdx, parIdx);
        currIdx = parIdx;
        parIdx = (currIdx - 1) / 2;
        parVal = heap->data[parIdx];
    }

}
Ejemplo n.º 25
0
void sortHeap(DynArr *heap)
{
    /* FIXME */

    assert (sizeDynArr(heap) > 0);

    int i;
    int last = sizeDynArr(heap) - 1;

    /* build heap, then sort it (swap and adjust) */
    _buildHeap(heap);

    for (i = last; i > 0; i-- )
    {
        swapDynArr(heap, i, 0);
        _adjustHeap(heap, i, 0);
    }

}
Ejemplo n.º 26
0
/*	Add a node to the heap

	param: 	heap	pointer to the heap
	param: 	node	node to be added to the heap
	pre:	heap is not null
	post:	node is added to the heap
*/
void addHeap(DynArr *heap, TYPE val)
{
	int pos = sizeDynArr(heap);

	addDynArr(heap, val);

	while(pos){
		int parent = (pos-1)/2;
		int left = pos*2 + 1;
		int right = pos*2 + 2;
		int child = _smallerIndexHeap(heap, left, right);
		if(getDynArr(heap, child) < getDynArr(heap, pos)){
			swapDynArr(heap, parent, pos);
			pos = parent;
		}
		else
			return;
	}
}
Ejemplo n.º 27
0
/*	Add a node to the heap

	param: 	heap	pointer to the heap
	param: 	node	node to be added to the heap
	pre:	heap is not null
	post:	node is added to the heap
*/
void addHeap(DynArr *heap, TYPE val)
{
    /* FIXME */
    assert(heap != 0);
    int parent;

    int pos = sizeDynArr(heap);     //set entry point to last element of array
    addDynArr(heap, val);           //add value to array. Resize array if needed

    while(pos != 0)                //while not at root element
    {
        parent = (pos - 1) / 2;     //parent of current node
        if(compare(getDynArr(heap, pos), getDynArr(heap, parent)) == -1)    //if pos < parent
        {
            swapDynArr(heap, parent, pos);  //swap nodes at pos and parent index
            pos = parent;           //current position is the new parent
        }
        else return;                //if pos >= parent
    }
}
Ejemplo n.º 28
0
// this main function contains some
int main(int argc, char* argv[]){

	DynArr *dyn;
	dyn = createDynArr(2);
	
	printf("\n\nTesting addDynArr...\n");
	addDynArr(dyn, 3);
	addDynArr(dyn, 4);
	addDynArr(dyn, 10);
	addDynArr(dyn, 5);
	addDynArr(dyn, 6);
	
	
	printf("The array's content: [3,4,10,5,6]\n");
	assertTrue(EQ(getDynArr(dyn, 0), 3), "Test 1st element == 3");
	assertTrue(EQ(getDynArr(dyn, 1), 4), "Test 2nd element == 4");
	assertTrue(EQ(getDynArr(dyn, 2), 10), "Test 3rd element == 10");
	assertTrue(EQ(getDynArr(dyn, 3), 5), "Test 4th element == 5");
	assertTrue(EQ(getDynArr(dyn, 4), 6), "Test 5th element == 6");
	assertTrue(sizeDynArr(dyn) == 5, "Test size = 5");
	
	printf("\n\nTesting putDynArr...\nCalling putDynArr(dyn, 2, 7)\n");
	putDynArr(dyn, 2, 7); 
	printf("The array's content: [3,4,7,5,6]\n");
	assertTrue(EQ(getDynArr(dyn, 2), 7), "Test 3rd element == 7");
	assertTrue(sizeDynArr(dyn) == 5, "Test size = 5");
	
	printf("\n\nTesting swapDynArr...\nCalling swapDynArr(dyn, 2, 4)\n");
	swapDynArr(dyn, 2, 4);
	printf("The array's content: [3,4,6,5,7]\n");
	assertTrue(EQ(getDynArr(dyn, 2), 6), "Test 3rd element == 6");
	assertTrue(EQ(getDynArr(dyn, 4), 7), "Test 5th element == 7");
	
	printf("\n\nTesting removeAtDynArr...\nCalling removeAtDynArr(dyn, 1)\n");
	removeAtDynArr(dyn, 1);
	printf("The array's content: [3,6,5,7]\n");
	assertTrue(EQ(getDynArr(dyn, 0), 3), "Test 1st element == 3");
	assertTrue(EQ(getDynArr(dyn, 3), 7), "Test 4th element == 7");
	assertTrue(sizeDynArr(dyn) == 4, "Test size = 4");
	
	printf("\n\nTesting stack interface...\n");
	printf("The stack's content: [3,6,5,7] <- top\n");
	assertTrue(!isEmptyDynArr(dyn), "Testing isEmptyDynArr");
	assertTrue(EQ(topDynArr(dyn), 7), "Test topDynArr == 7");
	
	popDynArr(dyn);
	printf("Poping...\nThe stack's content: [3,6,5] <- top\n");
	assertTrue(EQ(topDynArr(dyn), 5), "Test topDynArr == 5");
	
	pushDynArr(dyn, 9);
	printf("Pushing 9...\nThe stack's content: [3,6,5,9] <- top\n");
	assertTrue(EQ(topDynArr(dyn), 9), "Test topDynArr == 9");
	
	printf("\n\nTesting bag interface...\n");
	printf("The bag's content: [3,6,5,9]\n");
	assertTrue(containsDynArr(dyn, 3), "Test containing 3");
	assertTrue(containsDynArr(dyn, 6), "Test containing 6");
	assertTrue(containsDynArr(dyn, 5), "Test containing 5");
	assertTrue(containsDynArr(dyn, 9), "Test containing 9");
	assertTrue(!containsDynArr(dyn, 7), "Test not containing 7");
	
	removeDynArr(dyn, 3);
	printf("Removing 3...\nThe stack's content: [6,5,9]\n");
	assertTrue(!containsDynArr(dyn, 3), "Test not containing 3");
	
	printf("Executing test functions...\n");
	printf("\nTesting addDynArr()\n");
	addDynArr_TEST(dyn);  //Testing the add functionality under 3 conditions
	printf("\nTesting popDynArr()\n");
	popDynArr_TEST(dyn);  //Testing the pop function under 2 conditions
	printf("\nTesting topDynArr()\n");
	topDynArr_TEST(dyn);  //Testing the top function under 2 conditions
	printf("\nTesting pushDynArr()\n");
	pushDynArr_TEST(dyn); //Testing the push function under 3 conditions
	printf("\nTesting containsDynArr()\n");
	containsDynArr_TEST(dyn); //Testing the contains function under 2 conditions
	printf("\nTesting removeDynArr()\n"); 
	removeDynArr_TEST(dyn); //Testing the remove function under 2 conditions
	return 0;
}
Ejemplo n.º 29
0
/*	Adjust heap to maintain heap property

	param: 	heap	pointer to the heap
	param: 	max		index to adjust up to  (but not included)
	param: 	pos		position index where the adjustment starts
	pre:	max <= size
	post:	heap property is maintained for nodes from index pos to index max-1  (ie. up to, but not including max)
*/
void _adjustHeap(DynArr *heap, int max, int pos)
{
	int leftChild = (pos * 2) + 1;
	int rightChild = (pos * 2) + 2;
	int leftChildExists = 0;
	int rightChildExists = 0;

	if (leftChild < max)
		leftChildExists = 1;
	if (rightChild < max)
		rightChildExists = 1;

	if (leftChildExists && rightChildExists)
	{
		if (_smallerIndexHeap(heap, leftChild, rightChild) == leftChild)
		{

			if (_smallerIndexHeap(heap, pos, leftChild) == leftChild)
			{
				swapDynArr(heap, pos, leftChild);
				_adjustHeap(heap, max, leftChild);
			}
			if (_smallerIndexHeap(heap, pos, rightChild) == rightChild)
			{
				swapDynArr(heap, pos, rightChild);
				_adjustHeap(heap, max, rightChild);
			}
		}
		else
		{
			if (_smallerIndexHeap(heap, pos, rightChild) == rightChild)
			{
				swapDynArr(heap, pos, rightChild);
				_adjustHeap(heap, max, rightChild);
			}
			if (_smallerIndexHeap(heap, pos, leftChild) == leftChild)
			{
				swapDynArr(heap, pos, leftChild);
				_adjustHeap(heap, max, leftChild);
			}
		}

	}
	else if (leftChildExists)
	{
		if (_smallerIndexHeap(heap, pos, leftChild) == leftChild)
		{
			swapDynArr(heap, pos, leftChild);
			_adjustHeap(heap, max, leftChild);
		}
	}
	else if (rightChildExists)
	{
		if (_smallerIndexHeap(heap, pos, rightChild) == rightChild)
		{
			swapDynArr(heap, pos, rightChild);
			_adjustHeap(heap, max, rightChild);
		}
	}

}
Ejemplo n.º 30
0
// this main function contains some
int main(int argc, char* argv[]){

	DynArr *dyn;
	dyn = newDynArr(2);
        int i; 

	printf("\n\nTesting addDynArr...\n");
	addDynArr(dyn, 3);
	addDynArr(dyn, 4);
	addDynArr(dyn, 10);
	addDynArr(dyn, 5);
	addDynArr(dyn, 6);
	
	printf("The array's content: [3,4,10,5,6]\n");
	assertTrue(EQ(getDynArr(dyn, 0), 3), "Test 1st element == 3");
	assertTrue(EQ(getDynArr(dyn, 1), 4), "Test 2nd element == 4");
	assertTrue(EQ(getDynArr(dyn, 2), 10), "Test 3rd element == 10");
	assertTrue(EQ(getDynArr(dyn, 3), 5), "Test 4th element == 5");
	assertTrue(EQ(getDynArr(dyn, 4), 6), "Test 5th element == 6");
	assertTrue(sizeDynArr(dyn) == 5, "Test size = 5");
	
	printf("\n\nTesting putDynArr...\nCalling putDynArr(dyn, 2, 7)\n");
	putDynArr(dyn, 2, 7); 
	printf("The array's content: [3,4,7,5,6]\n");
	assertTrue(EQ(getDynArr(dyn, 2), 7), "Test 3rd element == 7");
	assertTrue(sizeDynArr(dyn) == 5, "Test size = 5");
	
	printf("\n\nTesting swapDynArr...\nCalling swapDynArr(dyn, 2, 4)\n");
	swapDynArr(dyn, 2, 4);
	printf("The array's content: [3,4,6,5,7]\n");
	assertTrue(EQ(getDynArr(dyn, 2), 6), "Test 3rd element == 6");
	assertTrue(EQ(getDynArr(dyn, 4), 7), "Test 5th element == 7");
	
	printf("\n\nTesting removeAtDynArr...\nCalling removeAtDynArr(dyn, 1)\n");
	removeAtDynArr(dyn, 1);
	printf("The array's content: [3,6,5,7]\n");
	assertTrue(EQ(getDynArr(dyn, 0), 3), "Test 1st element == 3");
	assertTrue(EQ(getDynArr(dyn, 3), 7), "Test 4th element == 7");
	assertTrue(sizeDynArr(dyn) == 4, "Test size = 4");
	
	printf("\n\nTesting stack interface...\n");
	printf("The stack's content: [3,6,5,7] <- top\n");
	assertTrue(!isEmptyDynArr(dyn), "Testing isEmptyDynArr");
	assertTrue(EQ(topDynArr(dyn), 7), "Test topDynArr == 7");
	
	popDynArr(dyn);
	printf("Poping...\nThe stack's content: [3,6,5] <- top\n");
	assertTrue(EQ(topDynArr(dyn), 5), "Test topDynArr == 5");
	
	pushDynArr(dyn, 9);
	printf("Pushing 9...\nThe stack's content: [3,6,5,9] <- top\n");
	assertTrue(EQ(topDynArr(dyn), 9), "Test topDynArr == 9");
	
	printf("\n\nTesting bag interface...\n");
	printf("The bag's content: [3,6,5,9]\n");
	assertTrue(containsDynArr(dyn, 3), "Test containing 3");
	assertTrue(containsDynArr(dyn, 6), "Test containing 6");
	assertTrue(containsDynArr(dyn, 5), "Test containing 5");
	assertTrue(containsDynArr(dyn, 9), "Test containing 9");
	assertTrue(!containsDynArr(dyn, 7), "Test not containing 7");
	
	removeDynArr(dyn, 3);
	printf("Removing 3...\nThe stack's content: [6,5,9]\n");
	assertTrue(!containsDynArr(dyn, 3), "Test not containing 3");
	
	return 0;
}