generated from freudenreichan/info2Praktikum-DobleSpiel
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c860a197ee | ||
|
|
bfcb9ed426 | ||
|
|
5859268475 | ||
|
|
be5c1754f9 | ||
|
|
7a20acd8f6 | ||
|
|
09db456716 | ||
|
|
fc5f249554 | ||
|
|
1536413888 | ||
|
|
c06f7c2b61 |
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)
|
||||
{
|
||||
*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 numNodes;
|
||||
}
|
||||
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
|
||||
|
||||
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;
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user