8 Commits
11 changed files with 259 additions and 26 deletions

No files matched your search

+6
View File
@@ -0,0 +1,6 @@
{
"files.associations": {
"stdio.h": "c",
"unity.h": "c"
}
}
+93 -1
View File
@@ -10,9 +10,58 @@
// 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.
@@ -20,17 +69,60 @@ 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;
}
+10 -2
View File
@@ -17,11 +17,19 @@ typedef struct node
TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate);
// 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.
// Use your implementation of a stack to organize the iterator. Push the root node and all left nodes first. On returning the next element,
// push the top node and push all its left nodes.
// push the right top node and push all its left nodes.
// Hilfsfunktion pushAllLeft (node) auf rechten nachfolger
// 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
// aufrufen wie mit strtok
// welcher Baum durchlaufen
// immer null aufrufen bis fertig durchlaufen weil null zurückgegeben wird
void clearTree(TreeNode *root);
// Returns the number of entries in the tree given by root.
unsigned int treeSize(const TreeNode *root);
//rekursiv die Anzahl der gesetzten Knoten ermitteln
#endif
+5
View File
@@ -1,2 +1,7 @@
NAME;21730
jakob;11918
jakob;11860
Jakob;4974
player1;3999
NAME;2997
NAME;2995
+7 -1
View File
@@ -36,7 +36,13 @@ $(program_obj_filesobj_files): %.o: %.c
# Unit Tests
# --------------------------
test_numbers:
$(CC) -o test_numbers test__numbers.c numbers.c $(unityfolder)/unity.c $(FLAGS)
$(CC) -o test_numbers test_numbers.c numbers.c $(unityfolder)/unity.c $(FLAGS)
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
+11 -15
View File
@@ -27,33 +27,29 @@ unsigned int setSeed(unsigned int seed_test)
void create_unique_random_numbers(unsigned int len, unsigned int numbers[])
{
TreeNode *root = NULL;
int isDuplicate;
for (unsigned int i = 0; i < len; i++)
{
unsigned int same = 0;
do
{
unsigned int value = 0;
value = (rand() % (len * 2) + 1);
same = 0;
for (unsigned int j = 0; j < i; j++)
{
if (numbers[j] == value)
unsigned int value = rand() % (len * 2) + 1;
root = addToTree(root, &value, sizeof(unsigned int), compare, &isDuplicate);
if (isDuplicate)
{
same = 1;
break;
}
}
if (same == 0)
else
{
numbers[i] = value;
same = 0;
}
} while (same);
}
} while (same == 1);
}
clearTree(root);
}
unsigned int *createNumbers(unsigned int len)
+33 -7
View File
@@ -7,33 +7,59 @@
* `top`: liefert das oberste Element zurück,
* `clearStack`: gibt den gesamten Speicher frei. */
// Pushes data as pointer onto the stack.
typedef struct stackNode
static StackNode *createNewElement()
{
void *data;
struct stackNode *next;
} StackNode;
return malloc(sizeof(StackNode));
}
// Pushes data as pointer onto the stack.
StackNode *push(StackNode *stack, void *data)
{
StackNode *node = createNewElement();
if (node != NULL)
{
node->data = data;
node->next = stack;
return node;
}
return stack;
}
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
// freed by caller.)
StackNode *pop(StackNode *stack)
{
if (stack != NULL)
{
StackNode *currentElement = stack;
stack = stack->next;
free(currentElement);
}
return stack;
}
// Returns the data of the top element.
void *top(StackNode *stack)
{
if (stack != NULL)
{
return stack->data;
}
return NULL;
}
// Clears stack and releases all memory.
void clearStack(StackNode *stack)
{
StackNode *nextElement = stack;
while (stack != NULL)
{
nextElement = stack->next;
free(stack);
stack = nextElement;
}
}
+5
View File
@@ -8,6 +8,11 @@ The latest element is taken from the stack. */
#include <stdlib.h>
//TODO: passenden Datentyp als struct anlegen
typedef struct stackNode
{
void *data;
struct stackNode *next;
} StackNode;
// Pushes data as pointer onto the stack.
StackNode *push(StackNode *stack, void *data);
+51
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();
}
View File
File renamed without changes.
+38
View File
@@ -0,0 +1,38 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "unity/unity.h"
#include "stack.h"
void test_stackOrderIsCorrect()
{
int expectedValues[] = {1, 2, 3, 4};
const unsigned int exprectedLen = sizeof(expectedValues) / sizeof(expectedValues[0]);
unsigned int observedLen = 0;
StackNode *stack = NULL;
for(int i = 0; i < exprectedLen; i++)
{
stack = push(stack, &expectedValues[i]);
}
for(int i = exprectedLen-1; i >= 0 && stack != NULL; i--)
{
TEST_ASSERT_EQUAL(expectedValues[i], *(int *)top(stack));
observedLen++;
stack = pop(stack);
}
TEST_ASSERT_EQUAL_UINT32(exprectedLen, observedLen);
clearStack(stack);
}
void setUp(void) {}
void tearDown(void) {}
int main(void)
{
UNITY_BEGIN();
RUN_TEST(test_stackOrderIsCorrect);
return UNITY_END();
}