generated from freudenreichan/info2Praktikum-DobleSpiel
Fast fertig bis auf nextTreeData
This commit is contained in:
parent
e81380b531
commit
8b09fec7b2
82
bintree.c
82
bintree.c
@ -8,12 +8,61 @@
|
|||||||
* `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. */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//Hilfsfunktion für addToTree. Erstellt eine treenode.
|
||||||
|
static TreeNode* createTreeNode(const void *data, size_t dataSize)
|
||||||
|
{
|
||||||
|
TreeNode* newNode = calloc(1, sizeof(TreeNode));
|
||||||
|
if(!newNode)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
newNode ->data = malloc(dataSize);
|
||||||
|
if(!newNode->data)
|
||||||
|
{
|
||||||
|
free(newNode);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
memcpy(newNode -> data, data, dataSize);
|
||||||
|
return newNode;
|
||||||
|
}
|
||||||
|
|
||||||
// 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). (auf 1 wenn duplikat geaddet)
|
||||||
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)
|
||||||
|
{
|
||||||
|
TreeNode *newNode = createTreeNode(data, dataSize);
|
||||||
|
if(isDuplicate != NULL)
|
||||||
|
{
|
||||||
|
*isDuplicate = 0;
|
||||||
}
|
}
|
||||||
|
return newNode;
|
||||||
|
}
|
||||||
|
int compare = compareFct(data, root-> data);
|
||||||
|
if(compare < 0)
|
||||||
|
{
|
||||||
|
root -> left = addToTree(root -> left, data, dataSize, compareFct, isDuplicate);
|
||||||
|
}
|
||||||
|
else if(compare > 0)
|
||||||
|
{
|
||||||
|
root -> right = addToTree(root -> right, data, dataSize, compareFct, isDuplicate);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if(isDuplicate != NULL)
|
||||||
|
{
|
||||||
|
*isDuplicate = 1;
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
//Konvention: rechts ist >= also das Duplikat wird nach rechts verfrachtet.
|
||||||
|
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.
|
||||||
// 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,
|
||||||
@ -24,13 +73,40 @@ void *nextTreeData(TreeNode *root)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Releases all memory resources (including data copies).
|
// Releases all memory resources (including data copies).
|
||||||
|
|
||||||
|
// Gibt den gesamten Speicher (Knoten + Daten) frei
|
||||||
void clearTree(TreeNode *root)
|
void clearTree(TreeNode *root)
|
||||||
{
|
{
|
||||||
|
// 1. Abbruchbedingung: Wenn der Knoten existiert, müssen wir was tun.
|
||||||
|
// Wenn er NULL ist, machen wir einfach gar nichts (return void).
|
||||||
|
if (root)
|
||||||
|
{
|
||||||
|
// 2. Rekursion: Erst tief in den Baum absteigen (Post-Order)
|
||||||
|
clearTree(root->left);
|
||||||
|
clearTree(root->right);
|
||||||
|
|
||||||
|
// 3. Jetzt sind die Kinder weg. Wir kümmern uns um den aktuellen Knoten.
|
||||||
|
|
||||||
|
// Erst den Inhalt (die Datenkopie) löschen!
|
||||||
|
// (free(NULL) ist in C erlaubt, daher müssen wir nicht zwingend auf NULL prüfen,
|
||||||
|
// aber es schadet auch nicht).
|
||||||
|
free(root->data);
|
||||||
|
|
||||||
|
// 4. Dann den Container (den Knoten selbst) löschen
|
||||||
|
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)
|
||||||
{
|
{
|
||||||
|
// Abbruchbedingung: Wenn kein Knoten da ist, ist die Größe 0
|
||||||
|
if (root == NULL)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rekursionsschritt:
|
||||||
|
// 1 (für den aktuellen Knoten) + alles im linken Baum + alles im rechten Baum
|
||||||
|
return 1 + treeSize(root->left) + treeSize(root->right);
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user