void testDyHeapGetMin(CuTest* test) { const int n = 10; DynamicArray* heap = dyNew(1); Task* tasks = createTasks(n); for (int i = 0; i < n; i++) { dyAdd(heap, &tasks[i]); } CuAssertPtrNotNull(test, dyHeapGetMin(heap)); CuAssertTrue(test, dyGet(heap, 0) == dyHeapGetMin(heap)); shuffle(heap); CuAssertPtrNotNull(test, dyHeapGetMin(heap)); CuAssertTrue(test, dyGet(heap, 0) == dyHeapGetMin(heap)); free(tasks); dyDelete(heap); }
/** * Adds an element to the heap. * @param heap * @param value value to be added to heap. * @param compare pointer to compare function. */ void dyHeapAdd(DynamicArray* heap, TYPE value, compareFunction compare) { //int pos = dySize(heap) - 1; dyAdd(heap, value); int pos = dySize(heap) - 1; int parent; while (pos > 0) { parent = (pos - 1) / 2; if(compare(dyGet(heap, pos), dyGet(heap, parent)) == -1){ dySwap(heap, pos, parent); pos = parent; } else break; } }
/** * Adjusts heap to maintain the heap property. * @param heap * @param last index to adjust up to. * @param position index where adjustment starts. * @param compare pointer to compare function. */ void adjustHeap(DynamicArray* heap, int last, int position, compareFunction compare) { int leftChild = 2 * position + 1; int rightChild = 2 * position + 2; int smallestChild; /* we have two children */ if (rightChild < last) { if (compare(dyGet(heap, leftChild), dyGet(heap,rightChild)) == -1) smallestChild = leftChild; else smallestChild = rightChild; //get index of smallest child if (compare(dyGet(heap, position), dyGet(heap, smallestChild)) == 1) { dySwap(heap, position, smallestChild); adjustHeap(heap, last, smallestChild, compare); } } else if (leftChild < last) { if (compare(dyGet(heap, position), dyGet(heap, leftChild)) == 1) { dySwap(heap, position, leftChild); adjustHeap(heap, last, leftChild, compare); } } }
/** * Just a skeleton code function test. * @param test */ void testDyOrderedAdd(CuTest* test) { const int n = 100; Task* tasks = createTasks(n); DynamicArray* array = dyNew(1); for (int i = 0; i < n; i++) { dyOrderedAdd(array, &tasks[rand() % n], taskCompare); } int maxPriority = ((Task*)dyGet(array, 0))->priority; for (int i = 0; i < n; i++) { Task* task = (Task*)dyGet(array, i); CuAssertTrue(test, task->priority >= maxPriority); if (task->priority > maxPriority) { maxPriority = task->priority; } } dyDelete(array); free(tasks); }
void testDyHeapSort(CuTest* test) { const int n = 100; struct Task* tasks = createTasks(n); DynamicArray* heap = dyNew(1); for (int i = 0; i < n; i++) { dyAdd(heap, &tasks[i]); } shuffle(heap); dyHeapSort(heap, taskCompare); CuAssertIntEquals(test, n, dySize(heap)); for (int i = 0; i < n; i++) { CuAssertTrue(test, dyGet(heap, i) == &tasks[n - i - 1]); } free(tasks); dyDelete(heap); }
int dyOrderedContains(DynamicArray* bag, TYPE value, compareFunction compare) { int position = binarySearch(bag, value, compare); return compare(value, dyGet(bag, position)) == 0; }
TYPE dyStackTop(DynamicArray* stack) { return dyGet(stack, stack->size - 1); }
void testDyHeapRemoveMin(CuTest* test) { const int n = 100; Task* tasks = createTasks(n); DynamicArray* heap = dyNew(1); for (int i = 0; i < n; i++) { dyAdd(heap, &tasks[i]); } for (int i = 0; i < n; i++) { CuAssertIntEquals(test, n - i, dySize(heap)); CuAssertTrue(test, dyGet(heap, 0) == &tasks[i]); dyHeapRemoveMin(heap, taskCompare); assertHeapProperty(test, heap); CuAssertIntEquals(test, n - i - 1, dySize(heap)); } dyDelete(heap); free(tasks); }
void assertHeapProperty(CuTest* test, DynamicArray* heap) { for (int i = 0; i < dySize(heap); i++) { int priority = ((Task*)dyGet(heap, i))->priority; int left = 2 * i + 1; int right = 2 * i + 2; if (left < dySize(heap)) { int leftPriority = ((Task*)dyGet(heap, left))->priority; CuAssertTrue(test, priority <= leftPriority); } if (right < dySize(heap)) { int rightPriority = ((Task*)dyGet(heap, right))->priority; CuAssertTrue(test, priority <= rightPriority); } } }