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"); } }
/* 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 } } }
/* 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) { /* 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) { 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); } } }
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; }
/* 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; } }
/* 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); } } }
/* 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) { 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. */ }
/* 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); } } }