Author SHA1 Message Date
D2A62006 7652d3ea7d Create bintreeTest.c file 2025-12-07 18:47:50 +01:00
D2A62006 9f15c0c01f implement clearTree() and fix addToTree() 2025-12-07 18:47:33 +01:00
D2A62006 2d79193d6c implement *addToTree() 2025-12-07 17:34:08 +01:00
9 changed files with 87 additions and 139 deletions

No files matched your search

-6
View File
@@ -1,6 +0,0 @@
{
"files.associations": {
"stdio.h": "c",
"stdlib.h": "c"
}
}
+58 -1
View File
@@ -12,7 +12,55 @@
// 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)
{
if(root == NULL){
//Allocate Memory for Node
TreeNode *newNode = calloc(1, sizeof(TreeNode));
if(newNode == NULL){
return NULL; //Memory allocation failed
}
//Allocate Memory for data
newNode->data = malloc(dataSize);
if(newNode->data == NULL){
free(newNode); //Free unused Memory
return NULL;; //Memory allocation failed
}
memcpy(newNode->data, data, dataSize); //Copy Data
newNode->left = NULL;
newNode->right = NULL;
if(isDuplicate != NULL){
*isDuplicate = 0;
}
return newNode;
}
int cmp = compareFct(root->data, data);
if(cmp == 0){
//Duplicate
if(isDuplicate != NULL){
//Ignore duplicate
*isDuplicate = 1;
return root;
} else{
//Accept duplicate
root->right = addToTree(root->right, data, dataSize, compareFct, NULL);
}
}
else if (cmp > 0)
{
//Data is smaller -> left
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
}else{
//Data is bigger -> right
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.
@@ -26,7 +74,16 @@ void *nextTreeData(TreeNode *root)
// Releases all memory resources (including data copies).
void clearTree(TreeNode *root)
{
if(root == NULL){
return;
}
clearTree(root->right);
clearTree(root->left);
free(root->data);
free(root);
}
// Returns the number of entries in the tree given by root.
+19
View File
@@ -0,0 +1,19 @@
#include <stdio.h>
#include <stdlib.h>
#include "unity.h"
#include "bintree.h"
void setUp(void){
//Use if needed
}
void tearDown(void){
//Use if needed
}
int main(){
UNITY_BEGIN();
printf("\n============================\nBintree tests\n============================\n");
}
-2
View File
@@ -37,8 +37,6 @@ doble_initial:
# --------------------------
# Unit Tests
# --------------------------
stackTests: stack.o test_stack.c $(unityfolder)/unity.c
$(CC) $(CFLAGS) -I$(unityfolder) -o stackTests test_stack.c stack.o $(unityfolder)/unity.c ${LDFLAGS}
unitTests:
echo "needs to be implemented"
+1 -1
View File
@@ -14,7 +14,7 @@
// 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.
unsigned int* createNumbers(unsigned int len)
unsigned int *createNumbers(unsigned int len)
{
}
+5 -32
View File
@@ -8,53 +8,26 @@
* `clearStack`: gibt den gesamten Speicher frei. */
// Pushes data as pointer onto the stack.
StackNode* push(StackNode* stack, void* data)
StackNode *push(StackNode *stack, void *data)
{
//pointer onto new struct
StackNode* newNode = malloc(sizeof(StackNode));
newNode->data = data;
//*stack := first node of the stack; is now the second to first node
newNode->nextNode = stack;
return newNode;
}
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
// freed by caller.)
StackNode* pop(StackNode* stack)
StackNode *pop(StackNode *stack)
{
//getting the second to first node
StackNode* tmp = stack->nextNode;
// data should be freed by the caller
// free(stack->data);
// stack->data = NULL;
free(stack);
stack = NULL;
return tmp;
}
// Returns the data of the top element.
void* top(StackNode* stack)
void *top(StackNode *stack)
{
//No stack --> No data-pointer
if (stack == NULL) return NULL;
return stack->data;
}
// Clears stack and releases all memory.
void clearStack(StackNode* stack)
void clearStack(StackNode *stack)
{
if (stack == NULL) return;
if (stack->nextNode != NULL) clearStack(stack->nextNode);
// date should be freed by the caller.
// free(stack->data);
// stack->data = NULL;
free(stack);
// technically not needed (dangling pointer)
stack = NULL;
}
+4 -7
View File
@@ -7,20 +7,17 @@ The latest element is taken from the stack. */
#include <stdlib.h>
typedef struct StackNode {
void* data;
struct StackNode* nextNode;
} StackNode;
//TODO: passenden Datentyp als struct anlegen
// Pushes data as pointer onto the stack.
StackNode* push(StackNode *stack, void *data);
StackNode *push(StackNode *stack, void *data);
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
// freed by caller.)
StackNode* pop(StackNode *stack);
StackNode *pop(StackNode *stack);
// Returns the data of the top element.
void* top(StackNode *stack);
void *top(StackNode *stack);
// Clears stack and releases all memory.
void clearStack(StackNode *stack);
BIN
View File
Binary file not shown.
-90
View File
@@ -1,90 +0,0 @@
#include <stdlib.h>
#include <stdio.h>
#include "unity.h"
#include "stack.h"
void test_topReturnsCorrectValue(void) {
// 1. Arrange
int* value1;
int* value2;
int* outVal1;
int* outVal2;
value1 = malloc(sizeof(int));
value2 = malloc(sizeof(int));
StackNode* startNode = NULL;
// 2. Act
*value1 = 1234;
*value2 = 5678;
startNode = push(startNode, value1);
// new top node is node 2
startNode = push(startNode, value2);
outVal2 = top(startNode);
// node 1 should now be the top node
startNode = pop(startNode);
outVal1 = top(startNode);
// 3. Assert
//Also tests for the functionality of 'push'
TEST_ASSERT_EQUAL_INT32(*value1, *outVal1);
TEST_ASSERT_EQUAL_INT32(*value2, *outVal2);
free(value1);
value1 = NULL;
free(value2);
value2 = NULL;
clearStack(startNode);
}
void test_topReturnsNullOnNotExistingStack(void) {
// 1. Arrange
StackNode* startNode = NULL;
int* data;
// 2. Act
data = top(startNode);
//3. Assert
TEST_ASSERT_NULL(data);
}
void test_popRemovesElements(void) {
// 1. Arrange
StackNode* startNode = NULL;
// 2. Act
// Entering some Elements into Stack (could be added manually for testing)
startNode = push(startNode, NULL);
startNode = push(startNode, NULL);
startNode = push(startNode, NULL);
startNode = push(startNode, NULL);
// Now removing created Elements
startNode = pop(startNode);
startNode = pop(startNode);
startNode = pop(startNode);
startNode = pop(startNode);
// 3. Assert
TEST_ASSERT_NULL(startNode);
}
void setUp(void){
}
void tearDown(void){
}
int main(void) {
UNITY_BEGIN();
printf("\n============================\nStack tests\n============================\n");
RUN_TEST(test_topReturnsNullOnNotExistingStack);
RUN_TEST(test_topReturnsCorrectValue);
RUN_TEST(test_popRemovesElements);
return UNITY_END();
}