generated from freudenreichan/info2Praktikum-DobleSpiel
Compare commits
No commits in common. "f75a452dafb7d0183d46ae9bcd05981a82677fe3" and "e0b80e8068220679a68059e121705d38b3326103" have entirely different histories.
f75a452daf
...
e0b80e8068
41
bintree.c
41
bintree.c
@ -10,15 +10,41 @@
|
|||||||
|
|
||||||
// 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).
|
||||||
/*
|
|
||||||
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)
|
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.
|
// 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,17 +52,14 @@ 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)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Releases all memory resources (including data copies).
|
// Releases all memory resources (including data copies).
|
||||||
void clearTree(TreeNode *root)
|
void clearTree(TreeNode *root)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -22,6 +22,7 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
|
|||||||
// wenn neuer Baum dann pushallleft auf wurzel
|
// wenn neuer Baum dann pushallleft auf wurzel
|
||||||
// immer aufräumen!!!
|
// immer aufräumen!!!
|
||||||
// kein vorsortiertes array, sonst entarteter Baum
|
// kein vorsortiertes array, sonst entarteter Baum
|
||||||
|
|
||||||
void *nextTreeData(TreeNode *root);
|
void *nextTreeData(TreeNode *root);
|
||||||
// Releases all memory resources (including data copies).
|
// Releases all memory resources (including data copies).
|
||||||
// sortierte Ausgabe
|
// sortierte Ausgabe
|
||||||
|
|||||||
@ -1,3 +1,2 @@
|
|||||||
jakob;11860
|
|
||||||
Jakob;4974
|
Jakob;4974
|
||||||
player1;3999
|
player1;3999
|
||||||
|
|||||||
3
makefile
3
makefile
@ -40,9 +40,6 @@ test_numbers:
|
|||||||
|
|
||||||
test_stack:
|
test_stack:
|
||||||
$(CC) -o test_stack test_stack.c stack.c $(unityfolder)/unity.c $(FLAGS)
|
$(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
|
# Clean
|
||||||
|
|||||||
@ -1,51 +0,0 @@
|
|||||||
#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();
|
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user