generated from freudenreichan/info2Praktikum-DobleSpiel
Compare commits
No commits in common. "jakob" and "main" have entirely different histories.
104
bintree.c
104
bintree.c
@ -2,66 +2,17 @@
|
||||
#include "stack.h"
|
||||
#include "bintree.h"
|
||||
|
||||
// TODO: binären Suchbaum implementieren
|
||||
//TODO: binären Suchbaum implementieren
|
||||
/* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv),
|
||||
* `clearTree`: gibt den gesamten Baum frei (rekursiv),
|
||||
* `treeSize`: zählt die Knoten im Baum (rekursiv),
|
||||
* `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. */
|
||||
* `clearTree`: gibt den gesamten Baum frei (rekursiv),
|
||||
* `treeSize`: zählt die Knoten im Baum (rekursiv),
|
||||
* `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. */
|
||||
|
||||
// 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 *newtreenode = malloc(sizeof(TreeNode));
|
||||
if (newtreenode != NULL)
|
||||
{
|
||||
|
||||
newtreenode->data = malloc(dataSize);
|
||||
if (newtreenode->data == NULL)
|
||||
{
|
||||
free(newtreenode);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memcpy(newtreenode->data, data, dataSize);
|
||||
|
||||
newtreenode->left = NULL;
|
||||
newtreenode->right = NULL;
|
||||
|
||||
if (isDuplicate != NULL)
|
||||
{
|
||||
*isDuplicate = 0;
|
||||
}
|
||||
}
|
||||
return newtreenode;
|
||||
}
|
||||
|
||||
int compare_value = compareFct(data, root->data); // wie funktioniert comparefcttype funktion? bzw wo steht diese?
|
||||
|
||||
if (compare_value == 0 && isDuplicate != NULL)
|
||||
{
|
||||
*isDuplicate = 1;
|
||||
return root;
|
||||
}
|
||||
|
||||
if (compare_value < 0)
|
||||
{
|
||||
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
|
||||
}
|
||||
else
|
||||
{
|
||||
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.
|
||||
@ -69,60 +20,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)
|
||||
{
|
||||
static StackNode *stack = NULL;
|
||||
TreeNode *currentElement = root;
|
||||
|
||||
if (currentElement != NULL)
|
||||
{
|
||||
clearStack(stack);
|
||||
stack = NULL;
|
||||
|
||||
while (currentElement != NULL)
|
||||
{
|
||||
stack = push(stack, currentElement);
|
||||
currentElement = currentElement->left;
|
||||
}
|
||||
}
|
||||
|
||||
if (stack == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
TreeNode *node = (TreeNode *)top(stack);
|
||||
stack = pop(stack);
|
||||
|
||||
currentElement = node->right;
|
||||
while (currentElement != NULL)
|
||||
{
|
||||
stack = push(stack, currentElement);
|
||||
currentElement = currentElement->left;
|
||||
}
|
||||
return node->data;
|
||||
}
|
||||
|
||||
// Releases all memory resources (including data copies).
|
||||
void clearTree(TreeNode *root)
|
||||
{
|
||||
if (root != NULL)
|
||||
{
|
||||
clearTree(root->left);
|
||||
clearTree(root->right);
|
||||
free(root->data);
|
||||
free(root);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Returns the number of entries in the tree given by root.
|
||||
unsigned int treeSize(const TreeNode *root)
|
||||
{
|
||||
unsigned int count = 0;
|
||||
|
||||
if (root != NULL)
|
||||
{
|
||||
count = 1;
|
||||
count += treeSize(root->right);
|
||||
count += treeSize(root->left);
|
||||
}
|
||||
return count;
|
||||
|
||||
}
|
||||
@ -22,6 +22,7 @@ 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
|
||||
|
||||
@ -1,3 +1,2 @@
|
||||
jakob;11860
|
||||
Jakob;4974
|
||||
player1;3999
|
||||
|
||||
3
makefile
3
makefile
@ -40,9 +40,6 @@ 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
|
||||
|
||||
@ -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