Author SHA1 Message Date
Simon May 804294b96e aufräumen 2025-12-02 14:44:04 +01:00
11 changed files with 39 additions and 145 deletions
-3
View File
@@ -6,6 +6,3 @@ runNumbersTest.exe
numbers.o numbers.o
.vscode/launch.json .vscode/launch.json
.vscode/settings.json .vscode/settings.json
*.o
*.exe
runBintreeTest
+15 -26
View File
@@ -2,7 +2,6 @@
#include "stack.h" #include "stack.h"
#include "bintree.h" #include "bintree.h"
static StackNode *stack;
// TODO: binären Suchbaum implementieren // TODO: binären Suchbaum implementieren
/* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv), /* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv),
* `clearTree`: gibt den gesamten Baum frei (rekursiv), * `clearTree`: gibt den gesamten Baum frei (rekursiv),
@@ -13,24 +12,8 @@ static StackNode *stack;
// if isDuplicate is NULL, otherwise ignores duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added). // 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) TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate)
{ {
if (root == NULL)
{
TreeNode *newNode = malloc(sizeof(TreeNode));
newNode->data = malloc(dataSize);
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
if (compareFct(root->data, data) < 0)
{
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
}
else if (compareFct(root->data, data) > 0)
{
root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
}
return root;
} }
// Iterates over the tree given by root. Follows the usage of strtok. If tree is NULL, the next entry of the last tree given is returned in ordering direction. // Iterates over the tree given by root. Follows the usage of strtok. If tree is NULL, the next entry of the last tree given is returned in ordering direction.
@@ -38,11 +21,9 @@ 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)
{ {
return NULL;
} }
// Releases all memory resources (including data copies). // Releases all memory resources (including data copies).
void clearTree(TreeNode *root) void clearTree(TreeNode *root)
{ {
@@ -55,19 +36,27 @@ void clearTree(TreeNode *root)
{ {
clearTree(root->left); clearTree(root->left);
free(root->left); free(root->left);
root->left = NULL;
} }
if (root->right != NULL) else if (root->right != NULL)
{ {
clearTree(root->right); clearTree(root->right);
free(root->right); free(root->right);
root->right = NULL;
} }
root = NULL; root->data = NULL;
} }
// 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)
{ {
return root == NULL ? 0 : treeSize(root->left) + treeSize(root->right) + 1; int counterL, counterR = 0;
if (root->left != NULL)
{
counterL = treeSize(root->left) + 1;
}
else if (root->right != NULL)
{
counterR = treeSize(root->right) + 1;
}
return counterL + counterR;
} }
+1
View File
@@ -0,0 +1 @@
player1;3999
-6
View File
@@ -44,12 +44,6 @@ unitTests: stack.o test_stack.c $(unityfolder)/unity.c
numbersTests: numbers.o test_numbers.c $(unityfolder)/unity.c numbersTests: numbers.o test_numbers.c $(unityfolder)/unity.c
$(CC) $(FLAGS) -I$(unityfolder) -o runNumbersTest test_numbers.c numbers.o $(unityfolder)/unity.c $(CC) $(FLAGS) -I$(unityfolder) -o runNumbersTest test_numbers.c numbers.o $(unityfolder)/unity.c
# --------------------------
# bintree.c Tests
# --------------------------
bintreeTests: bintree.o test_bintree.c $(unityfolder)/unity.c
$(CC) $(FLAGS) -I$(unityfolder) -o runBintreeTest test_bintree.c bintree.o $(unityfolder)/unity.c
# -------------------------- # --------------------------
# Clean # Clean
# -------------------------- # --------------------------
+14 -21
View File
@@ -23,8 +23,6 @@ unsigned int checkArray(unsigned int *array, unsigned int len, unsigned int numb
if (array[i] == number) if (array[i] == number)
{ {
free = 0; free = 0;
break;
} }
} }
@@ -35,37 +33,32 @@ unsigned int *createNumbers(unsigned int len)
{ {
srand(time(NULL)); srand(time(NULL));
unsigned int *array = (unsigned int*)malloc(len * sizeof(unsigned int)); unsigned int *array = (unsigned int*)malloc(len * sizeof(unsigned int));
int randomNr, randomPos, filler; int randomNr, counter;
if(array == NULL) if(array == NULL)
{ {
return NULL; // Fehler return NULL;
}
for (int i = 0; i < len; i++)
{
array[i] = 0;
} }
for (int i = 0; i < len; i++) for (int i = 0; i < len; i++)
{ {
counter = 0;
do do
{ {
array[i] = (rand() % (2 * len))+ 1; if (counter == 9)
} while (!checkArray(array, i, array[i]));
}
randomPos = rand() % len;
randomNr = array[randomPos];
filler = randomPos;
while(filler == randomPos)
{ {
filler = rand() % len; return NULL;
} }
array[filler] = randomNr;
randomNr = rand() % (2 * len + 1);
counter++;
} while (!checkArray(array, i, randomNr));
array[i] = randomNr;
printf("%u ", array[i]);
}
printf("\n");
return array; return array;
} }
BIN
View File
Binary file not shown.
Binary file not shown.
+1 -1
View File
@@ -13,7 +13,7 @@ StackNode *push(StackNode *stack, void *data)
StackNode *tempNode, *newNode; StackNode *tempNode, *newNode;
newNode = malloc(sizeof(StackNode)); newNode = malloc(sizeof(StackNode));
newNode->value = data; newNode->value = *(int *)data;
newNode->next = NULL; newNode->next = NULL;
if (stack == NULL) if (stack == NULL)
+1 -1
View File
@@ -9,7 +9,7 @@ The latest element is taken from the stack. */
//TODO: passenden Datentyp als struct anlegen //TODO: passenden Datentyp als struct anlegen
typedef struct Node { typedef struct Node {
void *value; int value;
struct Node* next; struct Node* next;
} StackNode; } StackNode;
-80
View File
@@ -1,80 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include "bintree.h"
#include "unity.h"
void sizeTest()
{
TreeNode *root = (TreeNode *)malloc(sizeof(TreeNode));
TreeNode *node1 = (TreeNode *)malloc(sizeof(TreeNode));
TreeNode *node2 = (TreeNode *)malloc(sizeof(TreeNode));
int dataRoot = 2;
int dataNode1 = 1;
int dataNode2 = 3;
root->data = &dataRoot;
root->left = (TreeNode *)node1;
root->right = (TreeNode *)node2;
node1->data = &dataNode1;
node1->left = NULL;
node1->right = NULL;
node2->data = &dataNode2;
node2->left = NULL;
node2->right = NULL;
TEST_ASSERT_EQUAL_INT(3,treeSize(root));
}
void clearTest()
{
TreeNode *root = (TreeNode *)malloc(sizeof(TreeNode));
TreeNode *node1 = (TreeNode *)malloc(sizeof(TreeNode));
TreeNode *node2 = (TreeNode *)malloc(sizeof(TreeNode));
int dataRoot = 2;
int dataNode1 = 1;
int dataNode2 = 3;
root->data = &dataRoot;
root->left = (TreeNode *)node1;
root->right = (TreeNode *)node2;
node1->data = &dataNode1;
node1->left = NULL;
node1->right = NULL;
node2->data = &dataNode2;
node2->left = NULL;
node2->right = NULL;
TreeNode *ptr = root;
clearTree(ptr);
TEST_ASSERT_EQUAL_INT(0,treeSize(root));
}
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("============================\nNumbers tests\n============================\n");
RUN_TEST(sizeTest);
RUN_TEST(clearTest);
return UNITY_END();
}
+1 -1
View File
@@ -25,7 +25,7 @@ void test_push(void)
TEST_ASSERT_EQUAL_INT(2, testNode->next->value); TEST_ASSERT_EQUAL_INT(2, testNode->next->value);
} }
StackNode* setup(void *value, StackNode* next) { StackNode* setup(int value, StackNode* next) {
StackNode* node = malloc(sizeof(StackNode)); // allocate memory on heap StackNode* node = malloc(sizeof(StackNode)); // allocate memory on heap
if (node == NULL) { if (node == NULL) {
perror("malloc failed"); perror("malloc failed");