forked from freudenreichan/info2Praktikum-DobleSpiel
Implement bintree.c
This commit is contained in:
3 files changed
+128
-17
No files matched your search
+87
-11
@@ -2,35 +2,111 @@
|
||||
#include "stack.h"
|
||||
#include "bintree.h"
|
||||
|
||||
//TODO: binären Suchbaum implementieren
|
||||
/* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv),
|
||||
* `clearTree`: gibt den gesamten Baum frei (rekursiv),
|
||||
* `treeSize`: zählt die Knoten im Baum (rekursiv),
|
||||
* `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. */
|
||||
|
||||
// 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).
|
||||
TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate)
|
||||
TreeNode *addToTree(TreeNode *root, const void *const data, const size_t dataSize, const CompareFctType compareFct, int *isDuplicate)
|
||||
{
|
||||
if(data == NULL || dataSize == 0 || compareFct == NULL)
|
||||
return root;
|
||||
|
||||
if(root == NULL)
|
||||
{
|
||||
if(isDuplicate != NULL)
|
||||
*isDuplicate = 0;
|
||||
|
||||
root = malloc(sizeof(TreeNode));
|
||||
if(root == NULL)
|
||||
return NULL;
|
||||
|
||||
*root = (TreeNode){
|
||||
.data = malloc(dataSize),
|
||||
.left = NULL,
|
||||
.right = NULL,
|
||||
};
|
||||
|
||||
if(root->data == NULL)
|
||||
{
|
||||
free(root);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memcpy(root->data, data, dataSize);
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
const int comparisonValue = compareFct(data, root->data);
|
||||
|
||||
if(comparisonValue > 0)
|
||||
root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
|
||||
else if(comparisonValue < 0 || isDuplicate == NULL)
|
||||
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
|
||||
|
||||
if(isDuplicate != NULL && comparisonValue == 0)
|
||||
*isDuplicate = 1;
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
static StackNode *treeNodeHistory = NULL;
|
||||
|
||||
static TreeNode *getSmallest(TreeNode *const root)
|
||||
{
|
||||
TreeNode *smallest = root;
|
||||
while(smallest != NULL)
|
||||
{
|
||||
treeNodeHistory = push(treeNodeHistory, smallest);
|
||||
smallest = smallest->left;
|
||||
}
|
||||
|
||||
return top(treeNodeHistory);
|
||||
}
|
||||
|
||||
// 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,
|
||||
// push the top node and push all its left nodes.
|
||||
void *nextTreeData(TreeNode *root)
|
||||
void *nextTreeData(TreeNode *const root)
|
||||
{
|
||||
if(root != NULL)
|
||||
{
|
||||
clearStack(treeNodeHistory);
|
||||
treeNodeHistory = NULL;
|
||||
return getSmallest(root)->data;
|
||||
}
|
||||
|
||||
if(treeNodeHistory == NULL)
|
||||
return NULL;
|
||||
|
||||
TreeNode *last = top(treeNodeHistory);
|
||||
treeNodeHistory = pop(treeNodeHistory);
|
||||
|
||||
if(last->right == NULL)
|
||||
{
|
||||
TreeNode *const current = top(treeNodeHistory);
|
||||
return current == NULL ? NULL : current->data;
|
||||
}
|
||||
|
||||
return getSmallest(last->right)->data;
|
||||
}
|
||||
|
||||
// Releases all memory resources (including data copies).
|
||||
void clearTree(TreeNode *root)
|
||||
void clearTree(TreeNode *const root)
|
||||
{
|
||||
if(root == NULL)
|
||||
return;
|
||||
|
||||
clearTree(root->left);
|
||||
clearTree(root->right);
|
||||
|
||||
free(root->data);
|
||||
free(root);
|
||||
}
|
||||
|
||||
// Returns the number of entries in the tree given by root.
|
||||
unsigned int treeSize(const TreeNode *root)
|
||||
unsigned int treeSize(const TreeNode *const root)
|
||||
{
|
||||
if(root == NULL)
|
||||
return 0;
|
||||
|
||||
}
|
||||
return treeSize(root->left) + treeSize(root->right) + 1;
|
||||
}
|
||||
Reference in new issue
Block a user