forked from freudenreichan/info2Praktikum-DobleSpiel
113 lines
3.0 KiB
C
113 lines
3.0 KiB
C
#include <string.h>
|
|
#include "stack.h"
|
|
#include "bintree.h"
|
|
|
|
// 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 *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 *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 *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 *const root)
|
|
{
|
|
if(root == NULL)
|
|
return 0;
|
|
|
|
return treeSize(root->left) + treeSize(root->right) + 1;
|
|
}
|