Compare commits

...
4 Commits
10 changed files with 58 additions and 19 deletions

No files matched your search

+15 -13
View File
@@ -22,18 +22,18 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
newNode->data = malloc(dataSize); newNode->data = malloc(dataSize);
newNode->left = NULL; newNode->left = NULL;
newNode->right = NULL; newNode->right = NULL;
strcpy(newNode->data, data); memcpy(newNode->data, data, dataSize);
if(isDuplicate) if(isDuplicate)
*isDuplicate = 0; *isDuplicate = 0;
return newNode; return newNode;
} }
if(compareFct(data, root->data) > 0) if(compareFct(data, root->data) > 0)
{ {
root->right = addToTree((root->right), data, dataSize, compareFct, isDuplicate); root->left = addToTree((root->left), data, dataSize, compareFct, isDuplicate);
} }
else if(compareFct(data, root->data) < 0) else if(compareFct(data, root->data) < 0)
{ {
root->left = addToTree((root->left), data, dataSize, compareFct, isDuplicate); root->right = addToTree((root->right), data, dataSize, compareFct, isDuplicate);
} }
else else
{ {
@@ -52,28 +52,30 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
// push the top node and push all its left nodes. // push the top node and push all its left nodes.
void *nextTreeData(TreeNode *root) void *nextTreeData(TreeNode *root)
{ {
printf("nextTreeData wurde gecallt!\n");
if(root) if(root)
{ {
addTreeToStack(stackRoot, root); stackRoot = addTreeToStack(stackRoot, root);
printf("Baum wurde geladen!\n");
} }
void *buffer = pop(stackRoot); void *stackBuffer = top(stackRoot);
printf("neuer Eintrag wurde geholt: %x\n", buffer); stackRoot = pop(stackRoot);
return buffer; //void *buffer = (stackBuffer->stackData);
//printf("neuer Eintrag wurde geholt: %x\n", buffer);
//return buffer;
return stackBuffer;
} }
// Adds all Treenode-Datas of a root into a stack ordered from left to right // Adds all Treenode-Datas of a root into a stack ordered from left to right
void addTreeToStack(StackNode *start, TreeNode *root) StackNode *addTreeToStack(StackNode *start, TreeNode *root)
{ {
if(root->left) if(root->left)
{ {
addTreeToStack(start, root->left); start = addTreeToStack(start, root->left);
} }
push(start, root->data); start = push(start, root->data);
if(root->right) if(root->right)
{ {
addTreeToStack(start, root->right); start = addTreeToStack(start, root->right);
} }
return start;
} }
// Releases all memory resources (including data copies). // Releases all memory resources (including data copies).
+1 -1
View File
@@ -26,5 +26,5 @@ void clearTree(TreeNode *root);
// Returns the number of entries in the tree given by root. // Returns the number of entries in the tree given by root.
unsigned int treeSize(const TreeNode *root); unsigned int treeSize(const TreeNode *root);
// Adds all Treenode-Datas of a root into a stack ordered from left to right // Adds all Treenode-Datas of a root into a stack ordered from left to right
void addTreeToStack(StackNode *start, TreeNode *root); StackNode *addTreeToStack(StackNode *start, TreeNode *root);
#endif #endif
+3
View File
@@ -0,0 +1,3 @@
Player1;2996
Player2;2988
Player3;2939
-1
View File
@@ -84,6 +84,5 @@ int main(int argc, char *argv[])
exitCode = EXIT_SUCCESS; exitCode = EXIT_SUCCESS;
} }
return exitCode; return exitCode;
} }
+2 -2
View File
@@ -39,8 +39,8 @@ stackTests: stack.o test_stack.c $(unityfolder)/unity.c
$(CC) $(CFLAGS) -I$(unityfolder) -o runStackTests test_stack.c stack.o $(unityfolder)/unity.c $(CC) $(CFLAGS) -I$(unityfolder) -o runStackTests test_stack.c stack.o $(unityfolder)/unity.c
echo "needs to be implemented" echo "needs to be implemented"
numbersTests: numbers.o numbersTests.c $(unityfolder)/unity.c numbersTests: numbers.o numbersTests.c bintree.c bintree.h stack.o $(unityfolder)/unity.c
$(CC) $(CFLAGS) -I$(unityfolder) -o runNumbersTests numbersTests.c numbers.o $(unityfolder)/unity.c $(CC) $(CFLAGS) -I$(unityfolder) -o runNumbersTests numbersTests.c numbers.o bintree.o stack.o $(unityfolder)/unity.c
# -------------------------- # --------------------------
# Clean # Clean
+28 -1
View File
@@ -51,7 +51,7 @@ unsigned int *createNumbers(unsigned int len){
return numbers; return numbers;
} }
/*
// Returns only the only number in numbers which is present twice. Returns zero on errors. // 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) unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
{ {
@@ -102,4 +102,31 @@ unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
return duplicate; return duplicate;
}
*/
unsigned int getDuplicate(const unsigned int *numbers, unsigned int len)
{
TreeNode *root = NULL;
int buffer = 0;
int counter = 0;
for(int i = 0; i < len; i++)
{
int isDuplicat = 0;
root = addToTree(root, numbers + i, sizeof(unsigned int), checkIfBigger, &isDuplicat);
if(isDuplicat)
{
buffer = *(numbers + i);
counter += 1;
}
}
clearTree(root);
if(counter == 1)
return buffer;
return 0;
}
//Just a compare function needed for getDuplicate
int checkIfBigger(const void *char1, const void *char2)
{
return *((int *)char1) - *((int *)char2);
} }
+4
View File
@@ -1,6 +1,8 @@
#ifndef NUMBERS_H #ifndef NUMBERS_H
#define NUMBERS_H #define NUMBERS_H
#include "bintree.h"
// Returns len random numbers between 1 and 2x len in random order which are all different, except for two entries. // 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 // Returns NULL on errors. Use your implementation of the binary search tree to check for possible duplicates while
// creating random numbers. // creating random numbers.
@@ -9,4 +11,6 @@ unsigned int *createNumbers(unsigned int len);
// Returns only the only number in numbers which is present twice. Returns zero on errors. // 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); unsigned int getDuplicate(const unsigned int *numbers, unsigned int len);
//Just a compare function needed for getDuplicate
int checkIfBigger(const void *char1, const void *char2);
#endif #endif
+1
View File
@@ -42,6 +42,7 @@ void test_getDouplicateReturnsZeroWhenThereAreMultipleDuplicates(void){
void test_CreateNumbersCreatesDuplicatein2LenArray(void){ void test_CreateNumbersCreatesDuplicatein2LenArray(void){
unsigned int *testArray = createNumbers(2); unsigned int *testArray = createNumbers(2);
unsigned int testValue = getDuplicate(testArray, 2); unsigned int testValue = getDuplicate(testArray, 2);
printf("\nTestvalue: %d\n", testValue);
if(!testValue) if(!testValue)
testValue = 0; testValue = 0;
Binary file not shown.
+4 -1
View File
@@ -1,4 +1,5 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include "stack.h" #include "stack.h"
//TODO: grundlegende Stackfunktionen implementieren: //TODO: grundlegende Stackfunktionen implementieren:
@@ -40,7 +41,9 @@ StackNode *pop(StackNode *stack)
free(temp); free(temp);
temp = NULL; temp = NULL;
return stack; return stack;
} else { }
else
{
return NULL; return NULL;
} }
} }