generated from freudenreichan/info2Praktikum-DobleSpiel
Compare commits
13 Commits
a1debfe55a
...
63e7c18db6
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
63e7c18db6 | ||
|
|
2c35bafba9 | ||
|
|
bc24ca7b46 | ||
|
|
b3c9fec7ba | ||
|
|
c860a197ee | ||
|
|
bfcb9ed426 | ||
|
|
5859268475 | ||
|
|
be5c1754f9 | ||
|
|
7a20acd8f6 | ||
|
|
09db456716 | ||
|
|
fc5f249554 | ||
|
|
1536413888 | ||
|
|
c06f7c2b61 |
144
binTreeTest.c
Normal file
144
binTreeTest.c
Normal file
@ -0,0 +1,144 @@
|
||||
#include "unity.h"
|
||||
#include <stdlib.h>
|
||||
#include "bintree.h"
|
||||
|
||||
static int compare(const void *a, const void *b)
|
||||
{
|
||||
return (*(int *)a > *(int *)b) - (*(int *)a < *(int *)b); // a und b werden in int konvertiert und deren Werte miteinander verglichen
|
||||
// returns 1 for a>b or -1 for a<b
|
||||
// in bintree.c wird ueberprueft, ob compare eine positive oder eine negative Zahl zurueckgibt,
|
||||
// wenn a groeßer b, positiv und dann wird links nach Teilbauemen gesucht
|
||||
}
|
||||
|
||||
void setUp() {}
|
||||
void tearDown() {}
|
||||
|
||||
//Adds a single element to the tree
|
||||
void test_add_single_element_to_Tree()
|
||||
{
|
||||
TreeNode *root = NULL;
|
||||
int value = 5;
|
||||
int duplicate = -1;
|
||||
|
||||
root = addToTree(root, &value, sizeof(int), compare, &duplicate);
|
||||
|
||||
TEST_ASSERT_NOT_NULL(root); //uberprueft, ob root dem Tree hinzugefuegt werden konnte
|
||||
TEST_ASSERT_EQUAL_INT(5, *(int*)root->data); //ueberprueft, ob der Wert fuer data richtig uebernommen wurde
|
||||
TEST_ASSERT_EQUAL_INT(0, duplicate); //ueberprueft, ob isDuplicate 0 gesetzt wurde (neue Knoten -> isDuplicate sollte 0 sein)
|
||||
|
||||
clearTree(root);
|
||||
}
|
||||
|
||||
//Adds multiplie elements to a tree
|
||||
void test_add_multiple_elements_to_Tree()
|
||||
{
|
||||
TreeNode *root = NULL;
|
||||
int value[] = {2, 5, 7, 9};
|
||||
int duplicate = -1;
|
||||
|
||||
for(int j = 0; j < 4; ++j)
|
||||
{
|
||||
root = addToTree(root, &value[j], sizeof(int), compare, &duplicate);
|
||||
}
|
||||
|
||||
TEST_ASSERT_EQUAL_INT(4, treeSize(root));
|
||||
|
||||
clearTree(root);
|
||||
}
|
||||
|
||||
//Detects the size of a tree
|
||||
void test_detect_empty_size()
|
||||
{
|
||||
TEST_ASSERT_EQUAL_INT(0, treeSize(NULL));
|
||||
}
|
||||
|
||||
//checks, wether size of tree is correctly determined and wether clearTree() works
|
||||
// Test: Duplikate nicht erlaubt (isDuplicate != NULL)
|
||||
void test_detect_size() {
|
||||
TreeNode *root = NULL;
|
||||
int values[] = {1, 3, 1, 4, 5, 6, 7, 5, 9, 10};
|
||||
int duplicate = 0; // wird pro Einfügen gesetzt
|
||||
|
||||
for (int j = 0; j < 10; ++j) {
|
||||
root = addToTree(root, &values[j], sizeof(int), compare, &duplicate);
|
||||
if (duplicate) {
|
||||
// Optional: prüfen, dass ein Duplikat erkannt wurde
|
||||
TEST_ASSERT_TRUE(duplicate == 1);
|
||||
}
|
||||
duplicate = 0; // zurücksetzen für nächstes Einfügen
|
||||
}
|
||||
|
||||
// Prüfen der Baumgröße ohne Duplikate
|
||||
TEST_ASSERT_EQUAL_INT(8, treeSize(root));
|
||||
|
||||
clearTree(root);
|
||||
}
|
||||
|
||||
|
||||
// Test: Duplikate erlaubt (isDuplicate == NULL)
|
||||
void test_add_multiplie_elements_one_dup() {
|
||||
TreeNode *root = NULL;
|
||||
int values[] = {1, 3, 1, 4, 5, 6, 7, 5, 9, 10};
|
||||
|
||||
for (int j = 0; j < 10; ++j) {
|
||||
root = addToTree(root, &values[j], sizeof(int), compare, NULL);
|
||||
}
|
||||
|
||||
// Alle Werte inklusive Duplikate
|
||||
TEST_ASSERT_EQUAL_INT(10, treeSize(root));
|
||||
|
||||
clearTree(root);
|
||||
}
|
||||
|
||||
|
||||
//Traverses the tree inorder to check wether nextTreeData works
|
||||
// Hilfsfunktion: rekursive Inorder-Prüfung
|
||||
void inorderCheck(TreeNode *node, int expected[], int *idx) {
|
||||
if (node == NULL) return;
|
||||
|
||||
// Linken Teilbaum prüfen
|
||||
inorderCheck(node->left, expected, idx);
|
||||
|
||||
// Aktuelles Element prüfen
|
||||
TEST_ASSERT_EQUAL_INT(expected[*idx], *(int*)node->data);
|
||||
(*idx)++;
|
||||
|
||||
// Rechten Teilbaum prüfen
|
||||
inorderCheck(node->right, expected, idx);
|
||||
}
|
||||
|
||||
void test_inorder() {
|
||||
TreeNode *root = NULL;
|
||||
int values[] = {5, 3, 7, 2, 4, 6, 8};
|
||||
|
||||
// Baum füllen
|
||||
for (int i = 0; i < 7; i++) {
|
||||
root = addToTree(root, &values[i], sizeof(int), compare, NULL);
|
||||
}
|
||||
|
||||
// Erwartete Inorder-Reihenfolge
|
||||
int expected[] = {2,3,4,5,6,7,8};
|
||||
|
||||
int idx = 0;
|
||||
inorderCheck(root, expected, &idx);
|
||||
|
||||
// Alle Einträge geprüft?
|
||||
TEST_ASSERT_EQUAL_INT(7, idx);
|
||||
|
||||
clearTree(root);
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
UNITY_BEGIN();
|
||||
|
||||
RUN_TEST(test_add_single_element_to_Tree);
|
||||
RUN_TEST(test_add_multiple_elements_to_Tree);
|
||||
RUN_TEST(test_add_multiplie_elements_one_dup);
|
||||
RUN_TEST(test_detect_empty_size);
|
||||
RUN_TEST(test_detect_size);
|
||||
RUN_TEST(test_inorder);
|
||||
|
||||
return UNITY_END();
|
||||
}
|
||||
97
bintree.c
97
bintree.c
@ -12,7 +12,53 @@
|
||||
// if isDuplicate is NULL, otherwise ignores duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added).
|
||||
TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate)
|
||||
{
|
||||
if(data!= NULL && dataSize > 0)
|
||||
{
|
||||
if(root == NULL) //Abbruchbedingung: Keine Wurzel vorhanden, deshalb fügen wir hier einen neuen Knote ein
|
||||
{
|
||||
TreeNode *newNode = (TreeNode *)malloc(sizeof(TreeNode));
|
||||
if(newNode == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
newNode->data = malloc(dataSize);
|
||||
if(newNode->data == NULL)
|
||||
{
|
||||
free(newNode);
|
||||
return NULL;
|
||||
}
|
||||
memcpy(newNode->data, data, dataSize);
|
||||
newNode->left = NULL;
|
||||
newNode->right = NULL;
|
||||
|
||||
if(isDuplicate!= NULL) //wenn Zeiger isDUplicate auf einen Wert zeigt, wird isDuplicate auf 0 gesetzt
|
||||
{
|
||||
*isDuplicate = 0;
|
||||
}
|
||||
|
||||
return newNode;
|
||||
}
|
||||
int cmp = compareFct(root->data, data);
|
||||
if(cmp > 0)
|
||||
{
|
||||
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
|
||||
}
|
||||
else if(cmp < 0){
|
||||
root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (isDuplicate) {
|
||||
*isDuplicate = 1;
|
||||
}
|
||||
else {
|
||||
// isDuplicate == NULL → trotzdem ein Duplikat einfügen (z.B. rechts)
|
||||
root->right = addToTree(root->right, data, dataSize, compareFct, NULL);
|
||||
}
|
||||
}
|
||||
return root;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// 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 +66,68 @@ 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;
|
||||
|
||||
// 1) Falls neuer Baum übergeben wurde → Initialisieren
|
||||
if (root != NULL)
|
||||
{
|
||||
// alten Stack leeren
|
||||
while (stack != NULL)
|
||||
stack = pop(stack);
|
||||
|
||||
// alle linken Knoten pushen
|
||||
while (root != NULL) {
|
||||
stack = push(stack, root);
|
||||
root = root->left;
|
||||
}
|
||||
}
|
||||
|
||||
// 2) Wenn Stack leer → fertig
|
||||
if (stack == NULL)
|
||||
return NULL;
|
||||
|
||||
// 3) Top-Knoten holen
|
||||
TreeNode *node = (TreeNode *)top(stack);
|
||||
stack = pop(stack);
|
||||
|
||||
// 4) Wenn rechter Teilbaum existiert → alle linken Knoten pushen
|
||||
TreeNode *right = node->right;
|
||||
while (right != NULL) {
|
||||
stack = push(stack, right);
|
||||
right = right->left;
|
||||
}
|
||||
|
||||
// 5) Daten zurückgeben
|
||||
return node->data;
|
||||
}
|
||||
|
||||
|
||||
// Releases all memory resources (including data copies).
|
||||
void clearTree(TreeNode *root)
|
||||
{
|
||||
if (root == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
// Erst linken Knoten löschen
|
||||
clearTree(root->left);
|
||||
|
||||
// Dann rechten Knoten löschen
|
||||
clearTree(root->right);
|
||||
|
||||
// Dann eigenen Speicher freigeben
|
||||
free(root->data);
|
||||
free(root);
|
||||
}
|
||||
|
||||
|
||||
// Returns the number of entries in the tree given by root.
|
||||
unsigned int treeSize(const TreeNode *root)
|
||||
{
|
||||
if(root == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return 1 + treeSize(root->left) + treeSize(root->right); //1, weil eine Wurzel gefunden wurde und dann immer plus eins fuer einen Teilbaum
|
||||
|
||||
}
|
||||
BIN
doble_initial.exe
Normal file
BIN
doble_initial.exe
Normal file
Binary file not shown.
@ -1 +1,3 @@
|
||||
Silvana;9944
|
||||
hannes;9910
|
||||
player1;3999
|
||||
|
||||
21
makefile
21
makefile
@ -35,8 +35,25 @@ $(program_obj_filesobj_files): %.o: %.c
|
||||
# --------------------------
|
||||
# Unit Tests
|
||||
# --------------------------
|
||||
unitTests:
|
||||
echo "needs to be implemented"
|
||||
unity_src = $(unityfolder)/unity.c
|
||||
|
||||
unitTests: numbersTest stackTest bintreeTest
|
||||
# ./runNumbersTest
|
||||
# ./runStackTest
|
||||
./runBintreeTest
|
||||
|
||||
numbersTest: numbers.o bintree.o stack.o numbersTest.c $(unity_src)
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) -I$(unityfolder) $^ -o runNumbersTest
|
||||
|
||||
stackTest: stack.o stackTest.c $(unity_src)
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) -I$(unityfolder) $^ -o runStackTests
|
||||
|
||||
binTreeTest: bintree.o binTreeTest.c $(unity_src) stack.o
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) -I$(unityfolder) $^ -o runBinTreeTest
|
||||
|
||||
%.o: %.c
|
||||
$(CC) -c $(CFLAGS) $< -o $@
|
||||
|
||||
|
||||
# --------------------------
|
||||
# Clean
|
||||
|
||||
76
numbers.c
76
numbers.c
@ -14,13 +14,87 @@
|
||||
// Returns len random numbers between 1 and 2x len in random order which are all different, except for two entries.
|
||||
// Returns NULL on errors. Use your implementation of the binary search tree to check for possible duplicates while
|
||||
// creating random numbers.
|
||||
// Returns len random numbers between 1 and 2*len in random order,
|
||||
// all different, except for exactly one duplicate (two entries the same).
|
||||
// Uses your binary search tree implementation to check for duplicates while generating numbers.
|
||||
unsigned int *createNumbers(unsigned int len)
|
||||
{
|
||||
if (len < 2)
|
||||
return NULL;
|
||||
|
||||
srand(time(NULL));
|
||||
|
||||
|
||||
unsigned int *numbers = malloc(len * sizeof(unsigned int));
|
||||
if (!numbers)
|
||||
return NULL;
|
||||
|
||||
TreeNode *root = NULL; // Baum anfänglich leer
|
||||
unsigned int count = 0;
|
||||
|
||||
// Zufallszahlen generieren, bis das Array voll ist
|
||||
while (count < len) {
|
||||
unsigned int random = (rand() % (2 * len)) + 1;
|
||||
|
||||
int duplicate = 0; // Anfangswert für Duplikat-Check
|
||||
root = addToTree(root, &random, sizeof(random), compareFct, &duplicate);
|
||||
|
||||
if (root == NULL) {
|
||||
free(numbers);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!duplicate) { // Zahl war neu → ins Array einfügen
|
||||
numbers[count++] = random;
|
||||
}
|
||||
// duplicate == 1 → Zahl existiert schon, neue Zahl generieren
|
||||
}
|
||||
|
||||
// Jetzt len eindeutige Zahlen erzeugt → ein Duplikat erzwingen
|
||||
unsigned int idx1 = rand() % len;
|
||||
unsigned int idx2 = rand() % len;
|
||||
while (idx2 == idx1) // sicherstellen, dass es eine andere Position ist
|
||||
idx2 = rand() % len;
|
||||
|
||||
numbers[idx2] = numbers[idx1];
|
||||
|
||||
// Baum wieder freigeben
|
||||
clearTree(root);
|
||||
|
||||
return numbers;
|
||||
}
|
||||
|
||||
// Jetzt len eindeutige Zahlen erzeugt ⇒ wir müssen ein Duplikat erzwingen
|
||||
unsigned int idx1 = rand() % len;
|
||||
unsigned int idx2 = rand() % len;
|
||||
while (idx2 == idx1)
|
||||
idx2 = rand() % len;
|
||||
|
||||
numbers[idx2] = numbers[idx1]; // zweites Exemplar
|
||||
|
||||
clearTree(root);
|
||||
return numbers;
|
||||
}
|
||||
|
||||
|
||||
// Returns only the only number in numbers which is present twice. Returns zero on errors.
|
||||
unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
|
||||
{
|
||||
|
||||
if(len>0)
|
||||
{
|
||||
unsigned int duplicate = 0;
|
||||
for(unsigned int i=0;i<len;i++)
|
||||
{
|
||||
unsigned int v1 = numbers[i];
|
||||
for(unsigned int j=i+1;j<len;j++)
|
||||
{
|
||||
unsigned int v2 = numbers[j];
|
||||
if(v1==v2)
|
||||
{
|
||||
return v1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
BIN
runBinTreeTest.exe
Normal file
BIN
runBinTreeTest.exe
Normal file
Binary file not shown.
33
stack.c
33
stack.c
@ -10,24 +10,49 @@
|
||||
// Pushes data as pointer onto the stack.
|
||||
StackNode *push(StackNode *stack, void *data)
|
||||
{
|
||||
|
||||
if(stack && data){
|
||||
StackNode *t = (StackNode *)malloc(sizeof(StackNode));
|
||||
if(!t)
|
||||
{
|
||||
return NULL; //Speicherfehler
|
||||
}
|
||||
t->next = stack;
|
||||
t->data = data;
|
||||
return t; //Gibt den ersten StackNode des Stacks zurueck
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// 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)
|
||||
{
|
||||
StackNode *t = stack->next; //Naechstes Element im Stack wird erstes Element
|
||||
free(stack);
|
||||
return t;
|
||||
}
|
||||
}
|
||||
|
||||
// Returns the data of the top element.
|
||||
void *top(StackNode *stack)
|
||||
{
|
||||
|
||||
if(stack)
|
||||
{
|
||||
return stack->data;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Clears stack and releases all memory.
|
||||
void clearStack(StackNode *stack)
|
||||
{
|
||||
|
||||
while(stack)
|
||||
{
|
||||
StackNode *tmp = stack;
|
||||
stack = stack->next;
|
||||
free(tmp->data);
|
||||
free(tmp);
|
||||
}
|
||||
}
|
||||
5
stack.h
5
stack.h
@ -8,6 +8,11 @@ The latest element is taken from the stack. */
|
||||
#include <stdlib.h>
|
||||
|
||||
//TODO: passenden Datentyp als struct anlegen
|
||||
typedef struct Node{
|
||||
void* data;
|
||||
struct Node *next;
|
||||
}StackNode;
|
||||
|
||||
|
||||
// Pushes data as pointer onto the stack.
|
||||
StackNode *push(StackNode *stack, void *data);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user