/* 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); } } }
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 */ 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); } } }
/* Get the index of the smaller node between two nodes in a heap param: heap pointer to the heap param: i index of one node param: j index of other node pre: i < size and j < size ret: the index of the smaller node */ int _smallerIndexHeap(DynArr *heap, int i, int j, comparator compare) { if(compare(getDynArr(heap, i),getDynArr(heap, j))== -1){ return i; } return j; }
/* 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; } } }
/* Get the index of the smaller node between two nodes in a heap param: heap pointer to the heap param: i index of one node param: j index of other node pre: i < size and j < size ret: the index of the smaller node */ int _smallerIndexHeap(DynArr *heap, int i, int j) { /* FIXME */ if (compare(getDynArr(heap, i), getDynArr(heap, j)) == -1){ return i; } return j; }
/* Get the index of the smaller node between two nodes in a heap param: heap pointer to the heap param: i index of one node param: j index of other node pre: i < size and j < size ret: the index of the smaller node */ int _smallerIndexHeap(DynArr *heap, int i, int j) { if(compare(getDynArr(heap, i), getDynArr(heap, j)) <= 0) return i; else return j; }
/* Get the index of the smaller node between two nodes in a heap param: heap pointer to the heap param: i index of one node param: j index of other node pre: i < size and j < size ret: the index of the smaller node */ int _smallerIndexHeap(DynArr *heap, int i, int j) { assert(i < sizeDynArr(heap)); assert(j < sizeDynArr(heap)); if (compare(getDynArr(heap, i), getDynArr(heap, j)) == -1) return i; else return j; }
/* Get the index of the smaller node between two nodes in a heap param: heap pointer to the heap param: i index of one node param: j index of other node pre: i < size and j < size ret: the index of the smaller node */ int _smallerIndexHeap(DynArr *heap, int i, int j) { /* FIXME */ if (getDynArr(heap, i) < getDynArr(heap, j)) return i; else return j; }
/* Get the index of the smaller node between two nodes in a heap param: heap pointer to the heap param: i index of one node param: j index of other node pre: i < size and j < size ret: the index of the smaller node */ int _smallerIndexHeap(DynArr *heap, int i, int j) { /* FIXME */ assert(i < heap->size && j < heap->size); if(compare(getDynArr(heap, i), getDynArr(heap, j)) == -1) //if i < j { return i; } else return j; }
/* Get the index of the smaller node between two nodes in a heap param: heap pointer to the heap param: i index of one node param: j index of other node pre: i < size and j < size ret: the index of the smaller node */ int _smallerIndexHeap(DynArr *heap, int i, int j) { /* FIXME */ assert(i < heap->size); assert(j < heap->size); if (compare(getDynArr(heap, i), getDynArr(heap, j) ) == -1) // i is less than j return i; else // they are equal or j is less return j; }
/* 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; } }
/* 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); }
/* 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); }
/* 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); }
/* Returns the element at the top of the stack param: v pointer to the dynamic array pre: v is not null pre: v is not empty post: no changes to the stack */ TYPE topDynArr(DynArr *v) { assert(v != 0); assert(v->size > 0); return getDynArr(v, sizeDynArr(v) - 1); }
/* 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; } } }
/* Get the index of the smaller node between two nodes in a heap param: heap pointer to the heap param: i index of one node param: j index of other node pre: i < size and j < size ret: the index of the smaller node */ int _smallerIndexHeap(DynArr *heap, int i, int j) { assert( heap != NULL ); assert( i < sizeDynArr( heap ) ); assert( j < sizeDynArr( heap ) ); int sml_idx = -1; if( compare( getDynArr( heap, i ), getDynArr( heap, j ) ) == -1 ) { sml_idx = i; } else { sml_idx = j; } return( sml_idx ); }
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); }
/* Get the first node, which has the min priority, from the heap param: heap pointer to the heap pre: heap is not empty ret: value of first node */ TYPE getMinHeap(DynArr *heap) { /* FIXME */ /* Temporary returning NULL */ // return NULL; assert(!isEmptyDynArr(heap)); return getDynArr(heap,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; } }
/* 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 }
/* 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; } }
/* 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 } }
/* Save the list to a file param: heap pointer to the list param: filePtr pointer to the file to which the list is saved pre: The list is not empty post: The list is saved to the file in tab-delimited format. Each line in the file stores a task, starting with the task priority, followed by a tab character (\t), and the task description. The tasks are not necessarily stored in the file in priority order. */ void saveList(DynArr *heap, FILE *filePtr) { int i; Task* task; assert(sizeDynArr(heap) > 0); for(i = 0; i < sizeDynArr(heap); i++) { task = getDynArr(heap, i); fprintf(filePtr, "%d\t%s\n", task->priority, task->description); } }
/* 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); } } }
/* Print the list param: heap pointer to the list pre: the list is not empty post: The tasks from the list are printed out in priority order. The tasks are not removed from the list. */ void printList(DynArr *heap) { /* FIXME: Write this */ struct DynArr *toPrint = createDynArr(sizeDynArr(heap)); copyDynArr(heap, toPrint); int i, max = sizeDynArr(toPrint); sortHeap(toPrint); // call print_type() on each node for(i = 0; i < max; i++) print_type(getDynArr(toPrint, i)); deleteList(toPrint); }
/* Print the list param: heap pointer to the list pre: the list is not empty post: The tasks from the list are printed out in priority order. The tasks are not removed from the list. */ void printList(DynArr *heap) { /* DONE: Write this */ for (int i = 0; i < sizeDynArr(heap); i++) { struct Task* t = (struct Task*)getDynArr(heap, i); //strip off timestamp from upper 16 bits //int pri = t->priority; int pri = t->priority & 0x0000FFFF; printf("%d: %s\n", pri, t->description); } }
/* 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. */ }