normal Nodes working in addToTree()

This commit is contained in:
Jonas Hofmann 2025-12-09 00:36:27 +01:00
parent 517d363f3e
commit 36ac0d6d9d
3 changed files with 20 additions and 11 deletions

View File

@ -50,9 +50,7 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate) TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate)
{ {
// printf("entered addRec\n"); printf("entered addRec\n");
//printf("curdat = %d\n", currentNode->data);
//printf("newdat = %d\n", newNode->data);
if (currentNode == NULL) if (currentNode == NULL)
{ {
printf("currentNode == NULL\n"); printf("currentNode == NULL\n");
@ -63,17 +61,17 @@ TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType
return newNode; return newNode;
} }
else if (compareFct(currentNode->data, newNode->data) < 0) else if (compareFct(&currentNode->data, &newNode->data) < 0)
{ {
printf("compareFct(currentNode->data, newNode->data) < 0\n"); printf("compareFct(currentNode->data, newNode->data) < 0\n");
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate); currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate);
} }
else if (compareFct(currentNode->data, newNode->data) > 0) else if (compareFct(&currentNode->data, &newNode->data) > 0)
{ {
printf("compareFct(currentNode->data, newNode->data) > 0\n"); printf("compareFct(currentNode->data, newNode->data) > 0\n");
currentNode->right = addToTreeRec(currentNode->right, newNode, compareFct, isDuplicate); currentNode->right = addToTreeRec(currentNode->right, newNode, compareFct, isDuplicate);
} }
else if (compareFct(currentNode->data, newNode->data) == 0) else if (compareFct(&currentNode->data, &newNode->data) == 0)
{ {
//duplicate //duplicate
printf("duplicate\n"); printf("duplicate\n");

BIN
bintree.o

Binary file not shown.

View File

@ -1,4 +1,5 @@
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include "unity.h" #include "unity.h"
#include "bintree.h" #include "bintree.h"
@ -17,10 +18,13 @@ void tearDown(void) {
static int compareIntEntries(const void *arg1, const void *arg2) static int compareIntEntries(const void *arg1, const void *arg2)
{ {
// printf("in comp\n"); // printf("in comp\n");
const int *entry1 = (const int *)arg1; const int *entry1 = (const void *)arg1;
const int *entry2 = (const int *)arg2; const int *entry2 = (const void *)arg2;
// printf("const set\n"); // printf("const set\n");
int result = entry2 - entry1; int result = *entry2 - *entry1;
// int result = entry2 - entry1;
// printf("exit comp\n"); // printf("exit comp\n");
return result; return result;
@ -45,18 +49,25 @@ void test_addToTreeExpandsTreeCorrectly(void)
testRoot = addToTree(testRoot, &score1, sizeof(int), compareIntEntries, NULL); testRoot = addToTree(testRoot, &score1, sizeof(int), compareIntEntries, NULL);
printf("add1passed\n"); printf("add1passed\n");
testRoot = addToTree(testRoot, &score2, sizeof(int), compareIntEntries, NULL); testRoot = addToTree(testRoot, &score2, sizeof(int), compareIntEntries, NULL);
printf("add2passed\n"); printf("add2passed\n");
testRoot = addToTree(testRoot, &score3, sizeof(int), compareIntEntries, NULL); testRoot = addToTree(testRoot, &score3, sizeof(int), compareIntEntries, NULL);
printf("add3passed\n"); printf("add3passed\n");
testRoot = addToTree(testRoot, &score4, sizeof(int), compareIntEntries, NULL); testRoot = addToTree(testRoot, &score4, sizeof(int), compareIntEntries, NULL);
printf("add4passed\n"); printf("add4passed\n");
testRoot = addToTree(testRoot, &score5, sizeof(int), compareIntEntries, NULL); testRoot = addToTree(testRoot, &score5, sizeof(int), compareIntEntries, NULL);
printf("add5passed\n"); printf("add5passed\n");
testRoot = addToTree(testRoot, &score6, sizeof(int), compareIntEntries, NULL); testRoot = addToTree(testRoot, &score6, sizeof(int), compareIntEntries, NULL);
printf("add6passed\n"); printf("add6passed\n");
/*
testRoot = addToTree(testRoot, &score7, sizeof(int), compareIntEntries, NULL); testRoot = addToTree(testRoot, &score7, sizeof(int), compareIntEntries, NULL);
printf("add7passed\n"); printf("add7passed\n");
*/
// Checking the Tree without Doubles // Checking the Tree without Doubles
TEST_ASSERT_NOT_NULL(testRoot); TEST_ASSERT_NOT_NULL(testRoot);
@ -78,7 +89,7 @@ void test_addToTreeExpandsTreeCorrectly(void)
TEST_ASSERT_NOT_NULL(testRoot->left->right); TEST_ASSERT_NOT_NULL(testRoot->left->right);
TEST_ASSERT_EQUAL_UINT16(score5, testRoot->left->right->data); TEST_ASSERT_EQUAL_UINT16(score5, testRoot->left->right->data);
printf("node5passed\n"); printf("node5passed\n");
/*
TEST_ASSERT_NOT_NULL(testRoot->right->left); TEST_ASSERT_NOT_NULL(testRoot->right->left);
TEST_ASSERT_EQUAL_UINT16(score6, testRoot->right->left->data); TEST_ASSERT_EQUAL_UINT16(score6, testRoot->right->left->data);
printf("node6passed\n"); printf("node6passed\n");