bintree fehlerbehebung und bintreetest erstellt

This commit is contained in:
Ben Skuppin 2025-12-10 15:43:52 +01:00
parent df7018f8f6
commit 33b370f0d2
2 changed files with 62 additions and 9 deletions

View File

@ -34,14 +34,16 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
int cmp = compareFct(data, root->data); int cmp = compareFct(data, root->data);
if (cmp == 0){ if (cmp == 0){
if (isDuplicate != NULL) {
*isDuplicate = 1;
}
/* /*
if (isDuplicate == NULL){ if (isDuplicate == NULL){
root->right = addToTree(root->right, data, dataSize, compareFct, NULL); root->right = addToTree(root->right, data, dataSize, compareFct, NULL);
return root; return root;
}*/ }*/
*isDuplicate = 1;
return root; return root;
} }
@ -60,15 +62,41 @@ 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)
{ {
if (root != NULL){ static StackNode *stack = NULL;
static TreeNode* currentNode = NULL;
void* result;
if(stack==NULL && root != NULL){
currentNode = root;
while(currentNode != NULL){
stack = push(stack,currentNode);
currentNode = currentNode->left;
} }
} }
if (stack == NULL)return NULL;
currentNode = top(stack);
stack = pop(stack);
result = currentNode->data;
if(currentNode->right != NULL){
currentNode = currentNode->right;
while(currentNode != NULL){
stack = push(stack,currentNode);
currentNode = currentNode->left;
}
}
return result;
}
// Releases all memory resources (including data copies). // Releases all memory resources (including data copies).
void clearTree(TreeNode *root) void clearTree(TreeNode *root)
{ {
@ -103,8 +131,8 @@ int compareFct(const void *a, const void *b){
unsigned int A = *(unsigned int *)a; unsigned int A = *(unsigned int *)a;
unsigned int B = *(unsigned int *)b; unsigned int B = *(unsigned int *)b;
int result= 0; int result= 0;
if(A>B)result = -1; //logik vertauscht? if(A>B)result = 1;
if(B>A)result = 1; //logik vertauscht? if(B>A)result = -1;
if(A==B)result = 0; if(A==B)result = 0;
return result; return result;

25
bintreeTests.c Normal file
View File

@ -0,0 +1,25 @@
#include <stdio.h>
#include "unity/unity.h"
#include "bintree.h"
void setUp(void){}
void tearDown(void){}
void TEST_BINTREE{
}
int main(){
UNITY_BEGIN();
printf("\n============================\nbintree tests\n============================\n");
//RUN_TEST();
//RUN_TEST();
//RUN_TEST();
return UNITY_END();
}