Compare commits

...

2 Commits

5 changed files with 64 additions and 33 deletions

View File

@ -10,41 +10,15 @@
// 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).
/*
memcpy
dest - pointer to the memory location to copy to
src - pointer to the memory location to copy from
count - number of bytes to copy
*/
TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate)
{
if (root = NULL)
{
TreeNode *node = malloc(sizeof(data));
strcpy(root, data);
node->left = NULL;
node->right = NULL;
if (isDuplicate != NULL)
{
isDuplicate = 0;
}
}
return node;
int compare = compareFct(data, root->data);
if (compare < 0) // linker Teilbaum
{
root->left = addToTree();
}
else if (compare > 0) // rechter Teilbaum
{
root->right = addToTree();
}
else
{
if (isDuplicate != NULL)
{
isDuplicate = 1;
}
return root;
}
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.
@ -52,14 +26,17 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
// push the top node and push all its left nodes.
void *nextTreeData(TreeNode *root)
{
}
// Releases all memory resources (including data copies).
void clearTree(TreeNode *root)
{
}
// Returns the number of entries in the tree given by root.
unsigned int treeSize(const TreeNode *root)
{
}

View File

@ -22,7 +22,6 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
// wenn neuer Baum dann pushallleft auf wurzel
// immer aufräumen!!!
// kein vorsortiertes array, sonst entarteter Baum
void *nextTreeData(TreeNode *root);
// Releases all memory resources (including data copies).
// sortierte Ausgabe

View File

@ -1,2 +1,3 @@
jakob;11860
Jakob;4974
player1;3999

View File

@ -40,6 +40,9 @@ test_numbers:
test_stack:
$(CC) -o test_stack test_stack.c stack.c $(unityfolder)/unity.c $(FLAGS)
test_bintree:
$(CC) -o test_bintree test_bintree.c bintree.c stack.c $(unityfolder)/unity.c $(FLAGS)
# --------------------------
# Clean

View File

@ -0,0 +1,51 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "unity/unity.h"
#include "bintree.h"
#include "stack.h"
int cmpInt(const void *a, const void *b) {
return *(int *)a - *(int *)b;
}
void test_binary_tree_functions(void)
{
TreeNode *root = NULL;
int dup;
int values[] = {5, 3, 8, 2, 6};
for (int i = 0; i < 5; i++) {
root = addToTree(root, &values[i], sizeof(int), cmpInt, &dup);
TEST_ASSERT_EQUAL_INT(0, dup);
}
int testDup = 5;
root = addToTree(root, &testDup, sizeof(int), cmpInt, &dup);
TEST_ASSERT_EQUAL_INT(1, dup);
TEST_ASSERT_EQUAL_UINT(5, treeSize(root));
int inorderExpected[] = {2, 3, 5, 6, 8};
int idx = 0;
void *data;
for (data = nextTreeData(root); data != NULL; data = nextTreeData(NULL)) {
TEST_ASSERT_EQUAL_INT(inorderExpected[idx], *(int *)data);
idx++;
}
TEST_ASSERT_EQUAL_INT(5, idx);
clearTree(root);
root = NULL;
TEST_ASSERT_NULL(root);
}
void setUp(void) {}
void tearDown(void) {}
int main(void)
{
UNITY_BEGIN();
RUN_TEST(test_binary_tree_functions);
return UNITY_END();
}