generated from freudenreichan/info2Praktikum-DobleSpiel
Änderung von bintree.c
This commit is contained in:
parent
888b7f1c29
commit
510edf8585
76
bintree.c
76
bintree.c
@ -12,7 +12,43 @@
|
|||||||
// 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).
|
||||||
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)
|
||||||
{
|
{
|
||||||
|
// 1. Leerer Baum -> neuen Knoten anlegen
|
||||||
|
if (root == NULL)
|
||||||
|
{
|
||||||
|
TreeNode *node = malloc(sizeof(TreeNode)); //Speicherreservierung
|
||||||
|
if (!node) return NULL; //Abbruch, wenn kein Speicher verfügbar
|
||||||
|
|
||||||
|
node->data = malloc(dataSize); //Speicher für Daten reservieren
|
||||||
|
if(!node->data) //Bei Fehler -> freigeben des Knotens und abbrechen
|
||||||
|
{
|
||||||
|
free(node);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
memcpy(node->data, data, dataSize); //Daten kopieren (nicht Zeiger)
|
||||||
|
|
||||||
|
node->left = node->right = NULL; //Neuer Knoten hat keine Kinder
|
||||||
|
|
||||||
|
if (isDuplicate) *isDuplicate = 0; //Rückmeldung (kein Duplikat)
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
//2. Vergleich, um Einfügerichtung zu bestimmen
|
||||||
|
int cmp = compareFct(data, root->data);
|
||||||
|
|
||||||
|
if (cmp < 0) //kleiner nach links weitergeben (rekursiv)
|
||||||
|
{
|
||||||
|
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
|
||||||
|
}
|
||||||
|
else if (cmp > 0) //größer nach rechts weitergeben
|
||||||
|
{
|
||||||
|
root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
|
||||||
|
}
|
||||||
|
else //Duplikat -> wird nicht eingefügt (keine Änderung am Baum)
|
||||||
|
{
|
||||||
|
if (isDuplicate) *isDuplicate = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 +56,57 @@ 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 *iterStack = NULL; //behält den Wert zwischen Funktionsaufrufen
|
||||||
|
|
||||||
|
if (root != NULL) //Wird ein neuer Baum übergeben?
|
||||||
|
{
|
||||||
|
clearStack(iterStack); // alten Stack löschen
|
||||||
|
iterStack = NULL;
|
||||||
|
|
||||||
|
TreeNode *cur = root; //Root + alle linken Knoten auf Stack pushen
|
||||||
|
while (cur != NULL)
|
||||||
|
{
|
||||||
|
iterStack = push(iterStack, cur);
|
||||||
|
cur = cur->left;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (iterStack == NULL) //Kein Baum mehr -> besuchen aller Knoten beendet
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
//Top-Knoten (als nächtes Reihe)
|
||||||
|
TreeNode *node = (TreeNode *)top(iterStack); //Top-Knoten als nächste Reihe
|
||||||
|
iterStack = pop(iterStack); //entfernen des Kontens
|
||||||
|
|
||||||
|
//Rechtsknoten
|
||||||
|
TreeNode *right = node->right; //nach Besuch bei rechten Knoten in rechten Teilbaum
|
||||||
|
while (right != NULL)
|
||||||
|
{
|
||||||
|
iterStack = push(iterStack, right);
|
||||||
|
right = right->left; //von dort ganz nach links und alles pushen
|
||||||
|
}
|
||||||
|
|
||||||
|
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) //Abbruchbedienung
|
||||||
|
return;
|
||||||
|
|
||||||
|
clearTree(root->left); //links und rechts löschen
|
||||||
|
clearTree(root->right);
|
||||||
|
|
||||||
|
free(root->data); //Daten freigeben
|
||||||
|
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)
|
||||||
{
|
{
|
||||||
|
if (root == NULL) //leerer Baum
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return 1 + treeSize(root->left) + treeSize(root->right); //Größe = 1 + Größe links + Größe rechts
|
||||||
}
|
}
|
||||||
BIN
doble_initial.exe
Normal file
BIN
doble_initial.exe
Normal file
Binary file not shown.
@ -1 +1,3 @@
|
|||||||
|
player_name;7977
|
||||||
|
player_name;7952
|
||||||
player1;3999
|
player1;3999
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user