Compare commits

..

No commits in common. "bc923cca9fff1ff89de4914cfce73ab538deb1da" and "5ed4a367c861bfd1fc7e575aaaff5ff4b7345d0c" have entirely different histories.

12 changed files with 29 additions and 109 deletions

View File

@ -3,20 +3,18 @@
#include "bintree.h" #include "bintree.h"
#include "stack.h" #include "stack.h"
/* Fügt eine Kopie der Daten in den Baum ein, geordnet nach compareFct. Akzeptiert Duplikate, /* Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates
wenn isDuplicate NULL ist, andernfalls ignoriert Duplikate und setzt isDuplicate auf 1 (oder auf 0 bei neuem Eintrag). */ 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) TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate)
{ {
// Überprüfe ungültige Eingabeparameter
if (compareFct == NULL || data == NULL || dataSize == 0) if (compareFct == NULL || data == NULL || dataSize == 0)
return root; // ungültige Eingabe: nichts tun return root; // invalid input: do nothing
// Wenn der Baum leer ist, erstelle einen neuen Wurzelknoten
if (root == NULL) if (root == NULL)
{ {
TreeNode *node = (TreeNode *)malloc(sizeof(TreeNode)); TreeNode *node = (TreeNode *)malloc(sizeof(TreeNode));
if (node == NULL) if (node == NULL)
return NULL; // Speicherallokation fehlgeschlagen return NULL; // allocation failed
node->data = malloc(dataSize); node->data = malloc(dataSize);
if (node->data == NULL) if (node->data == NULL)
@ -34,29 +32,25 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
return node; return node;
} }
// Vergleiche neue Daten mit aktueller Wurzel
int cmp = compareFct(data, root->data); int cmp = compareFct(data, root->data);
// Wenn neue Daten kleiner sind, füge in linken Unterbaum ein
if (cmp < 0) if (cmp < 0)
{ {
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate); root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
} }
// Wenn neue Daten größer sind, füge in rechten Unterbaum ein
else if (cmp > 0) else if (cmp > 0)
{ {
root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate); root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
} }
// Wenn gleich (Duplikat) else // cmp == 0 -> duplicate
else
{ {
// Wenn Duplikate erkannt werden sollen, setze Flag und ignoriere
if (isDuplicate != NULL) if (isDuplicate != NULL)
{ {
*isDuplicate = 1; *isDuplicate = 1;
// ignore duplicate insertion
} }
// Andernfalls erlaube Duplikate durch Einfügen in rechten Unterbaum
else else
{ {
// duplicates allowed: insert to right subtree for stability
root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate); root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
} }
} }
@ -64,23 +58,23 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
return root; return root;
} }
/* Iteriert über den Baum in aufsteigender Reihenfolge (in-order). /* Iterates over the tree given by root in-order (ascending order).
Verwendet die Logik von strtok: Wenn root != NULL, initialisiere/reset Iterator für diesen Baum. Follows the usage of strtok: If root != NULL then create/reset iterator for that tree.
Wenn root == NULL, setze Iteration von letzter Position fort. If root == NULL, continue iteration from last position.
Verwendet Stack zur Verwaltung des Traversierungs-Zustands. */ Uses stack to manage traversal state. */
void *nextTreeData(TreeNode *root) void *nextTreeData(TreeNode *root)
{ {
// Statischer Stack zur Aufrechterhaltung des Iterator-Zustands zwischen Aufrufen // static iterator state
static StackNode *iterStack = NULL; static StackNode *iterStack = NULL;
// Wenn ein neuer Baum bereitgestellt wird, initialisiere den Iterator // initialize iterator for a new tree
if (root != NULL) if (root != NULL)
{ {
// Lösche vorherigen Iterator-Zustand // clear any previous iterator state
clearStack(iterStack); clearStack(iterStack);
iterStack = NULL; iterStack = NULL;
// Pushe die Wurzel und alle linken Nachfahren auf den Stack // push root and all its left descendants
TreeNode *cur = root; TreeNode *cur = root;
while (cur != NULL) while (cur != NULL)
{ {
@ -90,20 +84,20 @@ void *nextTreeData(TreeNode *root)
} }
else else
{ {
// Wenn Iteration fortgesetzt wird, aber kein Stack initialisiert, gib NULL zurück // if user asks to continue but iterator not initialized, nothing to return
if (iterStack == NULL) if (iterStack == NULL)
return NULL; return NULL;
} }
// Wenn Stack leer ist, keine weiteren Elemente // get next node
if (iterStack == NULL) if (iterStack == NULL)
return NULL; return NULL;
// Poppe den nächsten Knoten vom Stack (in-order-Traversierung) // pop the top node
TreeNode *node = (TreeNode *)top(iterStack); TreeNode *node = (TreeNode *)top(iterStack);
iterStack = pop(iterStack); iterStack = pop(iterStack);
// Pushe den rechten Unterbaum des aktuellen Knotens und seine linken Nachfahren // after popping node, push its right child and all left descendants of that right child
TreeNode *r = node->right; TreeNode *r = node->right;
while (r != NULL) while (r != NULL)
{ {
@ -114,31 +108,26 @@ void *nextTreeData(TreeNode *root)
return node->data; return node->data;
} }
/* Gibt alle Speicherressourcen frei (einschließlich Datenkopien). */ /* Releases all memory resources (including data copies). */
void clearTree(TreeNode *root) void clearTree(TreeNode *root)
{ {
// Basisfall: wenn Baum leer, nichts tun
if (root == NULL) if (root == NULL)
return; return;
// Rekursiv linken und rechten Unterbaum löschen
if (root->left != NULL) if (root->left != NULL)
clearTree(root->left); clearTree(root->left);
if (root->right != NULL) if (root->right != NULL)
clearTree(root->right); clearTree(root->right);
// Daten und Knoten selbst freigeben
free(root->data); free(root->data);
root->data = NULL; root->data = NULL;
free(root); free(root);
} }
/* Gibt die Anzahl der Einträge im Baum zurück. */ /* Returns the number of entries in the tree given by root. */
unsigned int treeSize(const TreeNode *root) unsigned int treeSize(const TreeNode *root)
{ {
// Basisfall: leerer Baum hat Größe 0
if (root == NULL) if (root == NULL)
return 0; return 0;
// Größe ist 1 (aktueller Knoten) plus Größen der Unterbäume
return 1 + treeSize(root->left) + treeSize(root->right); return 1 + treeSize(root->left) + treeSize(root->right);
} }

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,3 +1,2 @@
Nicolas;9984
Test;4988 Test;4988
player1;3999 player1;3999

Binary file not shown.

View File

@ -16,32 +16,27 @@ static int compareUInt(const void *a, const void *b)
return 0; return 0;
} }
// Gibt ein Array mit len zufälligen Zahlen zwischen 1 und 2*len zurück, die alle unterschiedlich sind, // Returns len random numbers between 1 and 2x len in random order which are all different,
// außer zwei Einträgen (ein Duplikat). Verwendet den Binärbaum, um Duplikate zu vermeiden. // except for two entries. Uses the binary search tree to avoid duplicates.
unsigned int *createNumbers(unsigned int len) unsigned int *createNumbers(unsigned int len)
{ {
// Überprüfe ungültige Länge
if (len < 2) if (len < 2)
return NULL; return NULL;
// Allokiere Speicher für das Array
unsigned int *arr = malloc(sizeof(unsigned int) * len); unsigned int *arr = malloc(sizeof(unsigned int) * len);
if (!arr) if (!arr)
return NULL; return NULL;
// Initialisiere Zufallszahlengenerator
srand((unsigned int)time(NULL)); srand((unsigned int)time(NULL));
TreeNode *root = NULL; TreeNode *root = NULL;
unsigned int count = 0; unsigned int count = 0;
// Generiere len-1 eindeutige Zahlen while (count < len - 1) // generate len-1 UNIQUE numbers
while (count < len - 1)
{ {
unsigned int val = (rand() % (2 * len)) + 1; unsigned int val = (rand() % (2 * len)) + 1;
int isDup = 0; int isDup = 0;
// Füge in Baum ein und prüfe auf Duplikat
root = addToTree(root, &val, sizeof(unsigned int), compareUInt, &isDup); root = addToTree(root, &val, sizeof(unsigned int), compareUInt, &isDup);
if (!isDup) if (!isDup)
@ -50,30 +45,28 @@ unsigned int *createNumbers(unsigned int len)
} }
} }
// Wähle einen zufälligen bestehenden Wert als Duplikat // pick a random existing value to duplicate
unsigned int duplicateIndex = rand() % (len - 1); unsigned int duplicateIndex = rand() % (len - 1);
arr[len - 1] = arr[duplicateIndex]; arr[len - 1] = arr[duplicateIndex];
// Baum freigeben
clearTree(root); clearTree(root);
return arr; return arr;
} }
// Gibt die einzige Zahl im Array zurück, die zweimal vorkommt. // Returns the only number in the array that occurs twice.
unsigned int getDuplicate(const unsigned int numbers[], unsigned int len) unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
{ {
// Überprüfe ungültige Eingaben
if (!numbers || len < 2) if (!numbers || len < 2)
return 0; return 0;
// Kopiere Array // copy array
unsigned int *copy = malloc(sizeof(unsigned int) * len); unsigned int *copy = malloc(sizeof(unsigned int) * len);
if (!copy) if (!copy)
return 0; return 0;
memcpy(copy, numbers, sizeof(unsigned int) * len); memcpy(copy, numbers, sizeof(unsigned int) * len);
// Sortiere das Array (einfache Bubble-Sort) // sort
for (unsigned int i = 0; i < len - 1; i++) for (unsigned int i = 0; i < len - 1; i++)
{ {
for (unsigned int j = i + 1; j < len; j++) for (unsigned int j = i + 1; j < len; j++)
@ -87,7 +80,7 @@ unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
} }
} }
// Finde angrenzendes Duplikat // find adjacent duplicate
unsigned int duplicate = 0; unsigned int duplicate = 0;
for (unsigned int i = 0; i < len - 1; i++) for (unsigned int i = 0; i < len - 1; i++)
{ {
@ -98,7 +91,6 @@ unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
} }
} }
// Speicher freigeben
free(copy); free(copy);
return duplicate; return duplicate;
} }

Binary file not shown.

Binary file not shown.

View File

@ -1,60 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include "numbers.h"
// Einfache Funktion, um zu zählen, wie oft eine Zahl im Array vorkommt
int countOccurrences(const unsigned int *arr, unsigned int len, unsigned int value) {
int count = 0;
for (unsigned int i = 0; i < len; i++) {
if (arr[i] == value) count++;
}
return count;
}
// Testfunktion für createNumbers und getDuplicate
void testNumbers(unsigned int len) {
printf("Teste mit Laenge %u:\n", len);
// Erstelle Zahlenarray
unsigned int *numbers = createNumbers(len);
if (numbers == NULL) {
printf("Fehler: Konnte Array nicht erstellen.\n");
return;
}
// Gib Array aus
printf("Generierte Zahlen: ");
for (unsigned int i = 0; i < len; i++) {
printf("%u ", numbers[i]);
}
printf("\n");
// Finde Duplikat
unsigned int duplicate = getDuplicate(numbers, len);
printf("Gefundenes Duplikat: %u\n", duplicate);
// Überprüfe, ob es genau zweimal vorkommt
int occ = countOccurrences(numbers, len, duplicate);
if (occ == 2) {
printf("Korrekte Überprüfung: %u kommt genau zweimal vor.\n", duplicate);
} else {
printf("Fehler: %u kommt %d mal vor (sollte 2 sein).\n", duplicate, occ);
}
// Speicher freigeben
free(numbers);
printf("\n");
}
int main() {
printf("Testprogramm für numbers.c\n");
printf("=========================\n\n");
// Teste mit verschiedenen Längen
testNumbers(5);
testNumbers(10);
testNumbers(20);
printf("Tests abgeschlossen.\n");
return 0;
}

Binary file not shown.

Binary file not shown.