diff --git a/Startcode/bintree.c b/Startcode/bintree.c index 5cf82a9..3185307 100644 --- a/Startcode/bintree.c +++ b/Startcode/bintree.c @@ -7,11 +7,23 @@ * `clearTree`: gibt den gesamten Baum frei (rekursiv), * `treeSize`: zählt die Knoten im Baum (rekursiv), * `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. */ - // Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates // if isDuplicate is NULL, otherwise ignores duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added). TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate) { + // create new node + TreeNode* newNode = (TreeNode *)calloc(1, sizeof(TreeNode)); + newNode->data = calloc(1, dataSize); + memcpy(newNode->data, data, dataSize); + + if (root == NULL) return newNode; + if (compareFct(data, root->data)) { + root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate); + } else if (compareFct(root->data, data)) { + root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate); + } + return root; + } @@ -26,11 +38,24 @@ void *nextTreeData(TreeNode *root) // Releases all memory resources (including data copies). void clearTree(TreeNode *root) { + if(root->left != NULL){ + clearTree(root->left); + free(root->left->data); + free(root->left); + root->left = NULL; + } + if(root->right != NULL){ + clearTree(root->right); + free(root->right->data); + free(root->right); + root->right; + } } // Returns the number of entries in the tree given by root. unsigned int treeSize(const TreeNode *root) { - + if(root == NULL) return 0; + return 1 + treeSize(root->left) + treeSize(root->right); } \ No newline at end of file diff --git a/Startcode/numbers.c b/Startcode/numbers.c index f59d9a2..e734b9e 100644 --- a/Startcode/numbers.c +++ b/Startcode/numbers.c @@ -14,11 +14,67 @@ // Returns len random numbers between 1 and 2x len in random order which are all different, except for two entries. // Returns NULL on errors. Use your implementation of the binary search tree to check for possible duplicates while // creating random numbers. + + +static unsigned int randNumExists(unsigned int randNum, TreeNode *node){ + unsigned int randExists = 0; + while(node != NULL || node->left != NULL || node->right != NULL){ + if(node->data == randNum){ + randExists = 1; + break; + } + if(randNum < node->data){ + node = node->left; + } + else{ + node = node->right; + } + } + return 0; +} + + unsigned int *createNumbers(unsigned int len) { + srand(time(NULL)); + int randomNumber = rand() % len + len; + TreeNode randRoot; + randRoot.data = randomNumber; + randRoot.left = NULL; + randRoot.right = NULL; + unsigned int randomNumbers[len]; + randomNumbers[0] = randRoot.data; + int i = 1; + int pairExists = 0; + while(i < len-1){ + randomNumber = rand() % len + len; + if(!randNumExists(randomNumber, &randRoot)){ + randomNumbers[i++] = randomNumber; + } + else{ + if(!pairExists){ + randomNumbers[i++] = randomNumber; + pairExists = 1; + } + else{ + { + randomNumber = rand() % len + len; + }while(randNumExists(randomNumber)); + randomNumbers[i++] = randomNumber; + } + } + } + if(!pairExists){ + randomNumbers[len-1] = randomNumbers[rand() % (len-1)]; + } + else{ + randomNumbers[len-1] = rand() % len + len; + } + return randomNumbers; } + // Returns only the only number in numbers which is present twice. Returns zero on errors. unsigned int getDuplicate(const unsigned int numbers[], unsigned int len) { diff --git a/Startcode/stack.c b/Startcode/stack.c index 6477ba4..a09b8df 100644 --- a/Startcode/stack.c +++ b/Startcode/stack.c @@ -12,7 +12,9 @@ StackNode *push(StackNode *stack, void *data) { StackNode *newItem = (StackNode*)calloc(1, sizeof(StackNode)); if (newItem == NULL) return NULL; - stack->left = newItem; + if (stack != NULL) { + stack->left = newItem; + } newItem->right = stack; newItem->left = NULL; newItem->data = data; diff --git a/Startcode/test_stack.c b/Startcode/test_stack.c new file mode 100644 index 0000000..3c939c5 --- /dev/null +++ b/Startcode/test_stack.c @@ -0,0 +1,77 @@ +#include "unity.h" +#include "stack.h" + +void test_stackCreate() { + int* firstData = calloc(1, sizeof(int)); + *firstData = 1; + StackNode* initial = push(NULL, firstData); + + TEST_ASSERT_EQUAL_INT(1, *((int *)initial->data)); + TEST_ASSERT_NULL(initial->left); + TEST_ASSERT_NULL(initial->right); +} + +void test_stackPush() { + int* firstData = calloc(1, sizeof(int)); + *firstData = 1; + int* secondData = calloc(1, sizeof(int)); + *secondData = 2; + + StackNode* tree = push(NULL, firstData); + tree = push(tree, secondData); + + TEST_ASSERT_EQUAL_INT(2, *((int *)tree->data)); + TEST_ASSERT_EQUAL_INT(2, *((int*)top(tree))); + +} + +void test_stackPop() { + int* firstData = calloc(1, sizeof(int)); + *firstData = 1; + int* secondData = calloc(1, sizeof(int)); + *secondData = 2; + + StackNode* tree = push(NULL, firstData); + tree = push(tree, secondData); + + free(tree->data); + tree = pop(tree); + TEST_ASSERT_EQUAL_INT(1, *((int*)top(tree))); + +} + +// void test_stackClear() { +// int* firstData = calloc(1, sizeof(int)); +// *firstData = 1; +// int* secondData = calloc(1, sizeof(int)); +// *secondData = 2; + +// StackNode* stack = push(NULL, firstData); +// stack = push(stack, secondData); + +// clearStack(stack); +// TEST_ASSERT_NULL(stack); + + +// } + +void setUp(void) { + // Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden +} + +void tearDown(void) { + // Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden +} + +int main() { + + UNITY_BEGIN(); + + printf("\n============================\nStack tests\n============================\n"); + RUN_TEST(test_stackCreate); + RUN_TEST(test_stackPush); + RUN_TEST(test_stackPop); + // RUN_TEST(test_stackClear); + + return UNITY_END(); +}