27 Commits
Author SHA1 Message Date
hofmannjo99187 7c3df62d86 further debugging 2025-12-09 08:46:56 +01:00
hofmannjo99187 f7549910eb freeing node->data works, freeing node itself does not 2025-12-09 08:09:59 +01:00
hofmannjo99187 14fa122f20 added runbintreeTests.exe to git 2025-12-09 07:22:34 +01:00
hofmannjo99187 1c10994396 clear does not yet work, also TEST_ASSERT_EQUAL datatype is off 2025-12-09 01:40:43 +01:00
hofmannjo99187 402604cb45 removed no longer needed code 2025-12-09 01:07:39 +01:00
hofmannjo99187 5dc45f8590 uncommented not needed code from test_addToTreeExpandsTreeCorrectly and addToTree 2025-12-09 01:03:58 +01:00
hofmannjo99187 6b30677698 addToTreeExpandsTreeCorrectly 2025-12-09 01:00:42 +01:00
hofmannjo99187 d56a2f7a03 adding double working, double not Permitted not working 2025-12-09 00:44:04 +01:00
hofmannjo99187 36ac0d6d9d normal Nodes working in addToTree() 2025-12-09 00:36:27 +01:00
hofmannjo99187 517d363f3e debugging 2025-12-08 22:41:49 +01:00
hofmannjo99187 f91ab68a72 added bintree.o to git 2025-12-08 20:49:01 +01:00
hofmannjo99187 574ed71bae added bintree.o to makefile 2025-12-08 20:48:02 +01:00
hofmannjo99187 c49a8d687c reset highscore.txt 2025-12-08 13:01:23 +01:00
hofmannjo99187 42b83afec0 Merge branch 'branchjens' into branchjonas 2025-12-08 12:58:13 +01:00
hofmannjo99187 b138a8f251 added bintreeTests to makefile as Target 2025-12-08 08:36:00 +01:00
hofmannjo99187 ffde7270db wrote main() in bintreeTests.c 2025-12-08 08:30:10 +01:00
hofmannjo99187 409afc165b wrote test_treeSizeWorkingLikeExpected(void) 2025-12-08 08:24:30 +01:00
hofmannjo99187 4042a7d979 wrote test_clearTreeworksLikeExpected() 2025-12-08 08:19:12 +01:00
hofmannjo99187 1ead9595ce wrote test_addToTreeExpandsTreeCorrectly() 2025-12-08 08:03:16 +01:00
hofmannjo99187 e523b5462d started writing tests but ran into problems 2025-12-07 23:23:32 +01:00
hofmannjo99187 b50bd58dac created bintreeTests.c 2025-12-07 14:34:37 +01:00
hofmannjo99187 2176383f1e wrote treeSize(), created treeSizeRec() 2025-12-06 23:44:40 +01:00
hofmannjo99187 e52da066fa wrote clearTree(), created clearTreeRec() and clearNode() 2025-12-06 23:18:07 +01:00
hofmannjo99187 494fa2237c fixed logic problem in addToTreeRec() 2025-12-06 22:27:57 +01:00
hofmannjo99187 7dca38c37f wrote first version of addToTree() and wrote new recursive function addToTreeRec() 2025-12-06 16:40:57 +01:00
hofmannjo99187 03495bddf0 einlesen und grobe logik in kommentaren etablieren 2025-12-05 23:50:23 +01:00
hofmannjo99187 1763ac1f6d made doble_initial 2025-12-02 19:19:02 +01:00
8 changed files with 400 additions and 55 deletions
+141 -5
View File
@@ -1,36 +1,172 @@
#include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h>
#include "stack.h" #include "stack.h"
#include "bintree.h" #include "bintree.h"
//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), Done
* `clearTree`: gibt den gesamten Baum frei (rekursiv), * `clearTree`: gibt den gesamten Baum frei (rekursiv), Done
* `treeSize`: zählt die Knoten im Baum (rekursiv), * `treeSize`: zählt die Knoten im Baum (rekursiv), Done
* `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. */ * `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. */
static TreeNode *root = NULL;
TreeNode *addToTree (TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate);
void *nextTreeData (TreeNode *root);
void clearTree (TreeNode *root);
unsigned int treeSize (const TreeNode *root);
// self declared functions
TreeNode *addToTreeRec (TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate);
void clearTreeRec (TreeNode *currentNode);
void clearNode (TreeNode *node);
void treeSizeRec (const TreeNode *currentNode, unsigned int *nodeCount);
// Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates // 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). // if isDuplicate is NULL, otherwise ignores duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added).
// returned Value is new root
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)
{ {
// create a node
TreeNode *newNode;
newNode = calloc(1, sizeof(TreeNode));
newNode->data = calloc(1, dataSize);
newNode->left = NULL;
newNode->right = NULL;
memcpy(&newNode->data, data, dataSize);
return addToTreeRec(root, newNode, compareFct, isDuplicate);
} }
TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate)
{
if ((currentNode == NULL))
{
if (isDuplicate == NULL)
{
return newNode;
}
else
{
return currentNode;
}
}
else if ((compareFct(&currentNode->data, &newNode->data) < 0))
{
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate);
}
else if ((compareFct(&currentNode->data, &newNode->data) > 0))
{
currentNode->right = addToTreeRec(currentNode->right, newNode, compareFct, isDuplicate);
}
else if ((compareFct(&currentNode->data, &newNode->data) == 0))
{
if (isDuplicate == NULL)
{
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate);
}
else
{
*isDuplicate = 1;
}
}
return currentNode;
}
// 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.
// Use your implementation of a stack to organize the iterator. Push the root node and all left nodes first. On returning the next element, // Use your implementation of a stack to organize the iterator. Push the root node and all left nodes first. On returning the next element,
// push the top node and push all its left nodes. // push the top node and push all its left nodes.
// Needs stack!!
void *nextTreeData(TreeNode *root) void *nextTreeData(TreeNode *root)
{ {
} }
// Releases all memory resources (including data copies). // Releases all memory resources (including data copies).
void clearTree(TreeNode *root) void clearTree(TreeNode *root)
{ {
clearTreeRec(root);
} }
void clearTreeRec(TreeNode *currentNode)
{
if (currentNode != NULL)
{
clearTree(currentNode->left);
clearTree(currentNode->right);
clearNode(currentNode);
/*
printf("1\n");
free(&currentNode->data);
currentNode->data = NULL;
printf("2\n");
// free(currentNode);
// currentNode = NULL;
printf("3\n");
// */
}
}
void clearNode(TreeNode *node)
{
// printf("in clearNode\n");
// printf("node-> data = %u\n", node->data);
// printf("node-> data = %u\n", _ADDRESSOF(node->data));
free(&node->data);
node->data = NULL;
// printf("node-> data = %u\n", &node->data);
// printf("data freed \n");
node->left = NULL;
node->right = NULL;
// printf("left & right = Null\n");
printf("node = %u\n", &node);
// printf("node = %u\n", _ADDRESSOF(node));
// free(node);
// printf("node = %d\n", node);
node = NULL;
printf("node = %u\n", &node);
printf("freed node\n");
}
// 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)
{
unsigned int amountOfNodes = 0;
treeSizeRec(root, &amountOfNodes);
return amountOfNodes;
}
void treeSizeRec(const TreeNode *currentNode, unsigned int *nodeCount)
{ {
if (currentNode != NULL)
{
treeSizeRec(currentNode->left, nodeCount);
*nodeCount++;
treeSizeRec(currentNode->right, nodeCount);
}
}
}
BIN
View File
Binary file not shown.
+208
View File
@@ -0,0 +1,208 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "unity.h"
#include "bintree.h"
#define MAX_TEST_NAME_LEN 10
void setUp(void) {
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
}
void tearDown(void) {
// Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
}
static int compareIntEntries(const void *arg1, const void *arg2)
{
const int *entry1 = (const void *)arg1;
const int *entry2 = (const void *)arg2;
int result = *entry2 - *entry1;
return result;
}
// test if addToTree expands tree correctly
// by going down the path where the given pice of data is expected
// and checking if the pice of data is found there
void test_addToTreeExpandsTreeCorrectly(void)
{
TreeNode *testRoot = NULL;
int testIsDouble = 0;
int score1 = 12;
int score2 = 6;
int score3 = 18;
int score4 = 3;
int score5 = 9;
int score6 = 15;
int score7 = 21;
testRoot = addToTree(testRoot, &score1, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score2, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score3, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score4, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score5, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score6, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score7, sizeof(int), compareIntEntries, NULL);
// Checking the Tree without Doubles
TEST_ASSERT_NOT_NULL(testRoot);
TEST_ASSERT_EQUAL_UINT16(score1, testRoot->data);
TEST_ASSERT_NOT_NULL(testRoot->left);
TEST_ASSERT_EQUAL_UINT16(score2, testRoot->left->data);
TEST_ASSERT_NOT_NULL(testRoot->right);
TEST_ASSERT_EQUAL_UINT16(score3, testRoot->right->data);
TEST_ASSERT_NOT_NULL(testRoot->left->left);
TEST_ASSERT_EQUAL_UINT16(score4, testRoot->left->left->data);
TEST_ASSERT_NOT_NULL(testRoot->left->right);
TEST_ASSERT_EQUAL_UINT16(score5, testRoot->left->right->data);
TEST_ASSERT_NOT_NULL(testRoot->right->left);
TEST_ASSERT_EQUAL_UINT16(score6, testRoot->right->left->data);
TEST_ASSERT_NOT_NULL(testRoot->right->right);
TEST_ASSERT_EQUAL_UINT16(score7, testRoot->right->right->data);
// Adding Double
testRoot = addToTree(testRoot, &score4, sizeof(int), compareIntEntries, NULL);
TEST_ASSERT_NOT_NULL(testRoot->left->left->left);
TEST_ASSERT_EQUAL_UINT16(score4, testRoot->left->left->left->data);
// Trying to add Double while Doubles not Permitted
testRoot = addToTree(testRoot, &score7, sizeof(int), compareIntEntries, &testIsDouble);
TEST_ASSERT_NULL(testRoot->right->right->left);
TEST_ASSERT_EQUAL_UINT16(1, testIsDouble);
// clearTree(testRoot);
}
// test if nextTreeData returns the next pice of data correctly
// needs Stack!!!
void test_nextTreeDataReturnsNextDataCorrectly(void)
{
}
// test if clear Tree frees all node.name and node memory AND sets them to zero
// aditionally tests if the memoryspaces have been cleared
void test_clearTreeworksLikeExpected(void)
{
TreeNode *testRoot = NULL;
int score1 = 12;
int score2 = 6;
int score3 = 18;
int score4 = 3;
int score5 = 9;
int score6 = 15;
int score7 = 21;
// Fill Tree
testRoot = addToTree(testRoot, &score1, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score2, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score3, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score4, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score5, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score6, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score7, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score4, sizeof(int), compareIntEntries, NULL);
printf("Tree Filled\n");
// Save all Adresses
TreeNode *node1 = testRoot->left;
TreeNode *node2 = testRoot->left->left;
TreeNode *node3 = testRoot->left->right;
TreeNode *node4 = testRoot->right;
TreeNode *node5 = testRoot->right->left;
TreeNode *node6 = testRoot->right->right;
TreeNode *node7 = testRoot->left->left->left;
printf("Adresses Saved\n");
clearTree(testRoot);
printf("Tree Cleared\n");
// Check if everything has been set to NULL
TEST_ASSERT_NULL(testRoot->data);
TEST_ASSERT_NULL(testRoot);
TEST_ASSERT_NULL(node1->data);
// TEST_ASSERT_NULL(node1);
TEST_ASSERT_NULL(node2->data);
// TEST_ASSERT_NULL(node2);
TEST_ASSERT_NULL(node3->data);
// TEST_ASSERT_NULL(node3);
TEST_ASSERT_NULL(node4->data);
// TEST_ASSERT_NULL(node4);
TEST_ASSERT_NULL(node5->data);
// TEST_ASSERT_NULL(node5);
TEST_ASSERT_NULL(node6->data);
// TEST_ASSERT_NULL(node6);
TEST_ASSERT_NULL(node7->data);
// TEST_ASSERT_NULL(node7);
}
// tests if treeSize returns correct amount of nodes in Tree
// by using addToTree a given number of times and testing to see if
// the treeSize matches the number of nodes added
void test_treeSizeWorkingLikeExpected(void)
{
TreeNode *testRoot = NULL;
int nodeCount = 7;
unsigned int testTreeSize = 0;
int score1 = 12;
int score2 = 6;
int score3 = 18;
int score4 = 3;
int score5 = 9;
int score6 = 15;
int score7 = 21;
// Fill Tree
testRoot = addToTree(testRoot, &score1, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score2, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score3, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score4, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score5, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score6, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score7, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score4, sizeof(int), compareIntEntries, NULL);
testTreeSize = treeSize(testRoot);
TEST_ASSERT_EQUAL(nodeCount, testTreeSize);
clearTree(testRoot);
}
// main, strings together all tests
int main()
{
UNITY_BEGIN();
printf("\n============================\nBinary Tree tests\n============================\n");
// RUN_TEST(test_addToTreeExpandsTreeCorrectly);
// RUN_TEST(test_nextTreeDataReturnsNextDataCorrectly);
RUN_TEST(test_clearTreeworksLikeExpected);
// RUN_TEST(test_treeSizeWorkingLikeExpected);
return UNITY_END();
}
BIN
View File
Binary file not shown.
-1
View File
@@ -1,2 +1 @@
player_name;9943
player1;3999 player1;3999
+7 -6
View File
@@ -35,14 +35,15 @@ $(program_obj_filesobj_files): %.o: %.c
# -------------------------- # --------------------------
# Unit Tests # Unit Tests
# -------------------------- # --------------------------
unitTests: # unitTests:
echo "needs to be implemented" # echo "needs to be implemented"
stack: stack.c bintree: bintree.c
$(CC) $(FLAGS) -c stack stack.c $(CC) $(FLAGS) -c bintree bintree.c
bintreeTests: bintree.o bintreeTests.c $(unityfolder)/unity.c
$(CC) $(FLAGS) -o runbintreeTests bintreeTests.c bintree.o $(unityfolder)/unity.c
test_stack: stack.o test_stack.c $(unityfolder)/unity.c
$(CC) $(FLAGS) -o runstackTests test_stack.c stack.o $(unityfolder)/unity.c
# -------------------------- # --------------------------
# Clean # Clean
# -------------------------- # --------------------------
Binary file not shown.
+44 -43
View File
@@ -1,30 +1,24 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "unity.h"
#include "stack.h" #include "stack.h"
void setUp(void) {
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
}
void tearDown(void) {
// Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
}
void testeStackBeschreiben() void testeStackBeschreiben()
{ {
//printf("=== Test: push() ===\n"); printf("=== Test: push() ===\n");
StackNode *stack = NULL; StackNode *stack = NULL;
int wert1 = 42; int wert1 = 42;
stack = push(stack, &wert1); stack = push(stack, &wert1);
int *topValue = (int *)(stack->data); int *topValue = (int *)(stack->data);
if(topValue != NULL && *topValue == 42)
TEST_ASSERT_NOT_NULL(topValue); {
TEST_ASSERT_EQUAL_INT(42, *topValue); printf("Test 1: Erstes Element erfolgreich gepusht!\n");
}
else
{
printf("Test 1: FEHLGESCHLAGEN!\n");
}
int wert2 = 12; int wert2 = 12;
@@ -33,19 +27,23 @@ void testeStackBeschreiben()
topValue = (int *)(stack->data); topValue = (int *)(stack->data);
int *secondValue = (int *)((stack->next)->data); int *secondValue = (int *)((stack->next)->data);
TEST_ASSERT_NOT_NULL(topValue); if(topValue != NULL && *topValue == 12.25 && secondValue != NULL && *secondValue == 42)
TEST_ASSERT_EQUAL_INT(12, *topValue); {
TEST_ASSERT_NOT_NULL(secondValue); printf("Test 2: Zweites Element erfolgreich gepusht!\n");
TEST_ASSERT_EQUAL_INT(42, *secondValue); }
else
{
printf("Test 2: FEHLGESCHLAGEN!\n");
}
//printf("=== Ende Test: push() ===\n\n"); printf("=== Ende Test: push() ===\n\n");
return; return;
} }
void testepop() void testepop()
{ {
//printf("=== Test: pop() ===\n"); printf("=== Test: pop() ===\n");
StackNode *stack = NULL; StackNode *stack = NULL;
int wert1 = 20; int wert1 = 20;
@@ -58,15 +56,21 @@ void testepop()
int *topValue = (int *)(stack->data); int *topValue = (int *)(stack->data);
TEST_ASSERT_NOT_NULL(topValue); if(topValue != NULL && *topValue == 20)
TEST_ASSERT_EQUAL_INT(20, *topValue); {
printf("Test: Erstes Element erfolgreich gelöscht!\n");
}
else
{
printf("Test 1: FEHLGESCHLAGEN!\n");
}
//printf("=== Ende Test: pop() ===\n\n"); printf("=== Ende Test: pop() ===\n\n");
} }
void testetop() void testetop()
{ {
//printf("=== Test: top() ===\n"); printf("=== Test: top() ===\n");
StackNode *stack = NULL; StackNode *stack = NULL;
int wert1 = 20; int wert1 = 20;
@@ -77,15 +81,21 @@ void testetop()
int *topValue = top(stack); int *topValue = top(stack);
TEST_ASSERT_NOT_NULL(topValue); if(topValue != NULL && *topValue == 74)
TEST_ASSERT_EQUAL_INT(74, *topValue); {
printf("Test: top() gibt korrektes Element zurück!\n");
}
else
{
printf("Test: FEHLGESCHLAGEN!\n");
}
//printf("=== Ende Test: top() ===\n\n"); printf("=== Ende Test: top() ===\n\n");
} }
void testeclearStack() void testeclearStack()
{ {
//printf("=== Test: clearStack() ===\n"); printf("=== Test: clearStack() ===\n");
StackNode *stack = NULL; StackNode *stack = NULL;
int wert1 = 20; int wert1 = 20;
@@ -96,25 +106,16 @@ void testeclearStack()
clearStack(stack); clearStack(stack);
//printf("Test: clearStack() aufgerufen. Speicher freigegeben.\n"); printf("Test: clearStack() aufgerufen. Speicher freigegeben.\n");
//printf("=== Ende Test: clearStack() ===\n\n"); printf("=== Ende Test: clearStack() ===\n\n");
} }
int main() int main()
{ {
UNITY_BEGIN(); testeStackBeschreiben();
//printf("...");
RUN_TEST(testeStackBeschreiben);
RUN_TEST(testepop);
RUN_TEST(testetop);
RUN_TEST(testeclearStack);
/*testeStackBeschreiben();
testepop(); testepop();
testetop(); testetop();
testeclearStack();*/ testeclearStack();
return UNITY_END(); return 0;
} }