/* 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); } } }
/* 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); } } }
/* 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); } } }
/* 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 } } }
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"); } }
/* 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 */ heap->data[0] = getDynArr(heap, (sizeDynArr(heap) - 1)); heap->size--; _adjustHeap(heap, (sizeDynArr(heap) - 1), 0); }
void _buildHeap(DynArr *heap) { int max = sizeDynArr(heap); int i; for (i = max/2-1; i >= 0; i--) _adjustHeap(heap, max, i); }
void _buildHeap(DynArr *heap) { /* FIXME */ int index = sizeDynArr(heap); for (int i = index / 2 - 1; i>=0; i--) _adjustHeap(heap, index, i); }
/* 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) { int last = sizeDynArr(heap) - 1; putDynArr(heap, 0, getDynArr(heap, last)); removeAtDynArr(heap, last); _adjustHeap(heap, last, 0); }
void _buildHeap(DynArr *heap, comparator compare) { int max = sizeDynArr(heap); int i; for(i=(max/2)-1; i>=0; i--) _adjustHeap(heap, max, i, compare); }
/* 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) { int last = heap->size - 1; heap->data[0] = heap->data[last]; removeAtDynArr(heap, last); _adjustHeap(heap, heap->size, 0); }
void _buildHeap(DynArr *heap) { int firstLeaf = (heap->size / 2) - 1; for (int i = firstLeaf; i >= 0; i--) _adjustHeap(heap, heap->size, i); }
/* 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, comparator compare) { addDynArr(heap, val); int i = heap->size; int itterator = i-1; _adjustHeap(heap, i, (itterator-1)/2, compare); }
/* 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) { /* heap->data should be replaced with getmin function*/ heap->data[0] = getDynArr(heap, (sizeDynArr(heap) - 1)); heap->size--; _adjustHeap(heap, (sizeDynArr(heap) - 1), 0); }
void sortHeap(DynArr *heap) { /* FIXME */ for (int i = sizeDynArr(heap)-1; i > 0; i--){ swapDynArr(heap, 0, i); _adjustHeap(heap, i, 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; }
/* 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, comparator compare) { int last = sizeDynArr(heap)-1; //assert(last !=0); /*at least one element!*/ putDynArr(heap,0,getDynArr(heap,last)); removeAtDynArr(heap,last); /*remove last element*/ _adjustHeap(heap,last,0, compare); }
void _buildHeap(DynArr *heap) { int maxidx = heap->size; int i; for (i = maxidx/2-1; i>=0; --i) { _adjustHeap(heap, maxidx, i); } }
void _buildHeap(DynArr *heap) { int maxpos = heap->size - 1; int i; for(i = (maxpos - 1) / 2; i >= 0; i--){ _adjustHeap(heap, maxpos, i); } }
/* 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) { int last = sizeDynArr(heap) -1; if (last != 0) { heap->data[0] = heap->data[last]; } removeAtDynArr(heap, last); _adjustHeap(heap, last, 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); } }
void _buildHeap(DynArr *heap) { /* FIXME */ int max = sizeDynArr(heap); for(int i = max/2-1; i>=0; i--) { _adjustHeap(heap, max, i);} }
/* 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(heap->size != 0); int max = sizeDynArr(heap); //last element of array putDynArr(heap, 0, getDynArr(heap, (max - 1))); //set array index 0 to last element removeDynArr(heap, (max - 1)); //remove last element of array _adjustHeap(heap, max, 0); //adjust heap from root to last }
void sortHeap(DynArr *heap) { _buildHeap(heap); int i; for(i=sizeDynArr(heap)-1; i>0; --i) { swapDynArr(heap, i, 0); _adjustHeap(heap, i, 0); } }
void _buildHeap(DynArr *heap) { assert( heap != NULL ); assert( sizeDynArr( heap ) > 0 ); /* Start at the last non-leaf node */ for( int i = ( sizeDynArr( heap ) / 2 ) - 1; i >= 0; --i ) { _adjustHeap( heap, sizeDynArr( heap ), i ); } }
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); } }
/* 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); } } }
/* 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); } } }
void _buildHeap(DynArr *heap) { /* FIXME */ assert(!isEmptyDynArr(heap)); int max = heap->size - 1; for(int i = max/ 2; i >= 0; i--) _adjustHeap(heap, max, i); }
/* 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. */ }