generated from freudenreichan/info2Praktikum-DobleSpiel
Compare commits
8
Commits
8a29c029ff
...
jakob
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ed4edda352 | ||
|
|
36333491f9 | ||
|
|
f75a452daf | ||
|
|
5c646e3317 | ||
|
|
e0b80e8068 | ||
|
|
7a0585d892 | ||
|
|
600c414daa | ||
|
|
75a227eae0 |
No files matched your search
Vendored
+6
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"stdio.h": "c",
|
||||||
|
"unity.h": "c"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,17 +2,66 @@
|
|||||||
#include "stack.h"
|
#include "stack.h"
|
||||||
#include "bintree.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),
|
/* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv),
|
||||||
* `clearTree`: gibt den gesamten Baum frei (rekursiv),
|
* `clearTree`: gibt den gesamten Baum frei (rekursiv),
|
||||||
* `treeSize`: zählt die Knoten im Baum (rekursiv),
|
* `treeSize`: zählt die Knoten im Baum (rekursiv),
|
||||||
* `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. */
|
* `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
|
// 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 *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.
|
// 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.
|
// push the top node and push all its left nodes.
|
||||||
void *nextTreeData(TreeNode *root)
|
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).
|
// Releases all memory resources (including data copies).
|
||||||
void clearTree(TreeNode *root)
|
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.
|
// Returns the number of entries in the tree given by root.
|
||||||
unsigned int treeSize(const TreeNode *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;
|
||||||
}
|
}
|
||||||
@@ -17,11 +17,19 @@ typedef struct node
|
|||||||
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);
|
||||||
// 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.
|
||||||
// Use your implementation of a stack to organize the iterator. Push the root node and all left nodes first. On returning the next element,
|
// 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);
|
void *nextTreeData(TreeNode *root);
|
||||||
// Releases all memory resources (including data copies).
|
// 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);
|
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);
|
||||||
|
//rekursiv die Anzahl der gesetzten Knoten ermitteln
|
||||||
#endif
|
#endif
|
||||||
@@ -1,2 +1,7 @@
|
|||||||
|
NAME;21730
|
||||||
|
jakob;11918
|
||||||
|
jakob;11860
|
||||||
Jakob;4974
|
Jakob;4974
|
||||||
player1;3999
|
player1;3999
|
||||||
|
NAME;2997
|
||||||
|
NAME;2995
|
||||||
@@ -36,8 +36,14 @@ $(program_obj_filesobj_files): %.o: %.c
|
|||||||
# Unit Tests
|
# Unit Tests
|
||||||
# --------------------------
|
# --------------------------
|
||||||
test_numbers:
|
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
|
# Clean
|
||||||
# --------------------------
|
# --------------------------
|
||||||
|
|||||||
@@ -27,33 +27,29 @@ unsigned int setSeed(unsigned int seed_test)
|
|||||||
|
|
||||||
void create_unique_random_numbers(unsigned int len, unsigned int numbers[])
|
void create_unique_random_numbers(unsigned int len, unsigned int numbers[])
|
||||||
{
|
{
|
||||||
for (unsigned int i = 0; i < len; i++)
|
TreeNode *root = NULL;
|
||||||
{
|
int isDuplicate;
|
||||||
unsigned int same = 0;
|
|
||||||
|
|
||||||
do
|
for (unsigned int i = 0; i < len; i++)
|
||||||
{
|
{
|
||||||
unsigned int value = 0;
|
unsigned int same = 0;
|
||||||
value = (rand() % (len * 2) + 1);
|
do
|
||||||
|
|
||||||
same = 0;
|
|
||||||
|
|
||||||
for (unsigned int j = 0; j < i; j++)
|
|
||||||
{
|
|
||||||
if (numbers[j] == value)
|
|
||||||
{
|
{
|
||||||
same = 1;
|
unsigned int value = rand() % (len * 2) + 1;
|
||||||
break;
|
root = addToTree(root, &value, sizeof(unsigned int), compare, &isDuplicate);
|
||||||
}
|
if (isDuplicate)
|
||||||
}
|
{
|
||||||
|
same = 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
numbers[i] = value;
|
||||||
|
same = 0;
|
||||||
|
}
|
||||||
|
} while (same);
|
||||||
|
}
|
||||||
|
|
||||||
if (same == 0)
|
clearTree(root);
|
||||||
{
|
|
||||||
numbers[i] = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
} while (same == 1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int *createNumbers(unsigned int len)
|
unsigned int *createNumbers(unsigned int len)
|
||||||
|
|||||||
@@ -1,39 +1,65 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "stack.h"
|
#include "stack.h"
|
||||||
|
|
||||||
//TODO: grundlegende Stackfunktionen implementieren:
|
// TODO: grundlegende Stackfunktionen implementieren:
|
||||||
/* * `push`: legt ein Element oben auf den Stack,
|
/* * `push`: legt ein Element oben auf den Stack,
|
||||||
* `pop`: entfernt das oberste Element,
|
* `pop`: entfernt das oberste Element,
|
||||||
* `top`: liefert das oberste Element zurück,
|
* `top`: liefert das oberste Element zurück,
|
||||||
* `clearStack`: gibt den gesamten Speicher frei. */
|
* `clearStack`: gibt den gesamten Speicher frei. */
|
||||||
|
|
||||||
|
static StackNode *createNewElement()
|
||||||
|
{
|
||||||
|
return malloc(sizeof(StackNode));
|
||||||
|
}
|
||||||
|
|
||||||
// Pushes data as pointer onto the stack.
|
// Pushes data as pointer onto the stack.
|
||||||
typedef struct stackNode
|
|
||||||
{
|
|
||||||
void *data;
|
|
||||||
struct stackNode *next;
|
|
||||||
} StackNode;
|
|
||||||
|
|
||||||
StackNode *push(StackNode *stack, void *data)
|
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
|
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
|
||||||
// freed by caller.)
|
// freed by caller.)
|
||||||
StackNode *pop(StackNode *stack)
|
StackNode *pop(StackNode *stack)
|
||||||
{
|
{
|
||||||
|
if (stack != NULL)
|
||||||
|
{
|
||||||
|
StackNode *currentElement = stack;
|
||||||
|
stack = stack->next;
|
||||||
|
|
||||||
|
free(currentElement);
|
||||||
|
}
|
||||||
|
return stack;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the data of the top element.
|
// Returns the data of the top element.
|
||||||
void *top(StackNode *stack)
|
void *top(StackNode *stack)
|
||||||
{
|
{
|
||||||
|
if (stack != NULL)
|
||||||
|
{
|
||||||
|
return stack->data;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clears stack and releases all memory.
|
// Clears stack and releases all memory.
|
||||||
void clearStack(StackNode *stack)
|
void clearStack(StackNode *stack)
|
||||||
{
|
{
|
||||||
|
StackNode *nextElement = stack;
|
||||||
|
|
||||||
}
|
while (stack != NULL)
|
||||||
|
{
|
||||||
|
nextElement = stack->next;
|
||||||
|
|
||||||
|
free(stack);
|
||||||
|
|
||||||
|
stack = nextElement;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,6 +8,11 @@ The latest element is taken from the stack. */
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
//TODO: passenden Datentyp als struct anlegen
|
//TODO: passenden Datentyp als struct anlegen
|
||||||
|
typedef struct stackNode
|
||||||
|
{
|
||||||
|
void *data;
|
||||||
|
struct stackNode *next;
|
||||||
|
} StackNode;
|
||||||
|
|
||||||
// Pushes data as pointer onto the stack.
|
// Pushes data as pointer onto the stack.
|
||||||
StackNode *push(StackNode *stack, void *data);
|
StackNode *push(StackNode *stack, void *data);
|
||||||
|
|||||||
@@ -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();
|
||||||
|
}
|
||||||
File renamed without changes.
@@ -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();
|
||||||
|
}
|
||||||
Reference in new issue
Block a user