void shuffle(DynamicArray* array) { for (int i = 0; i < dySize(array); i++) { int j = rand() % dySize(array); dySwap(array, i, j); } }
/** * Builds a valid heap from an arbitrary array. * @param heap array with elements in any order. * @param compare pointer to compare function. */ void buildHeap(DynamicArray* heap, compareFunction compare) { int last = (dySize(heap) / 2) - 1; int x; for (x = last; x >= 0; x--) { adjustHeap(heap, heap->size, x, compare); } }
void testDyHeapAdd(CuTest* test) { const int n = 100; Task* tasks = createTasks(n); for (int i = 0; i < n; i++) { DynamicArray* heap = dyNew(1); for (int j = 0; j < n; j++) { dyHeapAdd(heap, &tasks[rand() % n], taskCompare); CuAssertIntEquals(test, j + 1, dySize(heap)); assertHeapProperty(test, heap); } dyDelete(heap); } free(tasks); }
void testBuildHeap(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++) { shuffle(heap); buildHeap(heap, taskCompare); CuAssertIntEquals(test, n, dySize(heap)); assertHeapProperty(test, heap); } dyDelete(heap); 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); }
/** * 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; } }
void testAdjustHeap(CuTest* test) { const int n = 100; Task* tasks = createTasks(n); for (int j = 0; j < n; j++) { DynamicArray* heap = dyNew(1); for (int i = 0; i < n; i++) { dyAdd(heap, &tasks[i]); } for (int i = 0; i < n; i++) { dyPut(heap, &tasks[rand() % n], 0); adjustHeap(heap, dySize(heap) - 1, 0, taskCompare); assertHeapProperty(test, heap); } dyDelete(heap); } free(tasks); }
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); } } }