134 lines
3.4 KiB
C
134 lines
3.4 KiB
C
#include <string.h>
|
|
#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. */
|
|
|
|
static TreeNode *createNode(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 0;
|
|
}
|
|
newNode->data = data;
|
|
|
|
return newNode;
|
|
}
|
|
// 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).
|
|
// 1. Fall: isDuplicate = NULL -> accepts
|
|
// 2. Fall: other -> ignores duplicates and
|
|
TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate)
|
|
{
|
|
if (isDuplicate != NULL)
|
|
{
|
|
*isDuplicate = 0;
|
|
}
|
|
if (!root)
|
|
{ // wurzerl == 0;
|
|
TreeNode *newNode = createNode(data, dataSize);
|
|
return newNode;
|
|
}
|
|
// wenn duplicate NUll -> doppelt hinzufügen
|
|
//
|
|
|
|
if (compareFct(data, root->data) < 0)
|
|
{
|
|
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
|
|
}
|
|
else if (compareFct(data, root->data) > 0)
|
|
{
|
|
root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
|
|
}
|
|
else if (compareFct(data, root->data) == 0)
|
|
{
|
|
if (isDuplicate != NULL)
|
|
{
|
|
*isDuplicate = 1;
|
|
}
|
|
else
|
|
{
|
|
root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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)
|
|
{
|
|
static StackNode *stack = NULL;
|
|
if (root != NULL)
|
|
{
|
|
clearStack(stack);
|
|
stack = push(NULL, root);
|
|
while (root->left != NULL)
|
|
{
|
|
root = root->left;
|
|
stack = push(stack, root);
|
|
}
|
|
}
|
|
|
|
if (stack == NULL)
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
TreeNode *result = (TreeNode *)top(stack);
|
|
stack = pop(stack);
|
|
root = result;
|
|
|
|
if (root->right != NULL)
|
|
{
|
|
root = root->right;
|
|
stack = push(stack, root);
|
|
while (root->left != NULL)
|
|
{
|
|
root = root->left;
|
|
stack = push(stack, root);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
// Releases all memory resources (including data copies).
|
|
void clearTree(TreeNode *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 = 0;
|
|
TreeNode* data = nextTreeData(root);
|
|
while (data != NULL)
|
|
{
|
|
treeSize++;
|
|
data = nextTreeData(NULL);
|
|
}
|
|
return treeSize;
|
|
} |