129 lines
3.2 KiB
C
129 lines
3.2 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 StackNode *stack = NULL;
|
|
|
|
TreeNode* createNode(const void *data, size_t dataSize) {
|
|
TreeNode* newNode = (TreeNode*)malloc(sizeof(TreeNode));
|
|
if (newNode == NULL) {
|
|
// Fehlerbehandlung: Speicher konnte nicht alloziiert werden
|
|
return NULL;
|
|
}
|
|
|
|
// Speicher für die Daten allozieren und kopieren
|
|
newNode->data = malloc(dataSize);
|
|
if (newNode->data == NULL) {
|
|
free(newNode); // newNode freigeben, wenn Datenallokation fehlschlägt
|
|
return NULL;
|
|
}
|
|
memcpy(newNode->data, data, dataSize);
|
|
|
|
newNode->left = NULL;
|
|
newNode->right = NULL;
|
|
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).
|
|
TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate)
|
|
{
|
|
|
|
|
|
if (root == NULL)
|
|
{
|
|
TreeNode *newNode = createNode(data,dataSize);
|
|
if (isDuplicate != NULL)
|
|
*isDuplicate = 0;
|
|
|
|
return newNode;
|
|
}
|
|
if (compareFct(root->data, data)==0)
|
|
{
|
|
if (isDuplicate != NULL) {
|
|
*isDuplicate=1;
|
|
return root;
|
|
}
|
|
TreeNode *newNode = createNode(data,dataSize);
|
|
return newNode;
|
|
}
|
|
else if (compareFct(root->data, data)>0)
|
|
{
|
|
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
|
|
}
|
|
else if (compareFct(root->data, data)<0)
|
|
{
|
|
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.
|
|
// 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)
|
|
{
|
|
if (root != NULL) {
|
|
clearStack(stack);
|
|
stack = NULL;
|
|
TreeNode *current = root;
|
|
while (current != NULL) {
|
|
stack = push(stack,current);
|
|
current = current->left;
|
|
}
|
|
}
|
|
if (stack == NULL) {
|
|
return NULL;
|
|
}
|
|
TreeNode *node = (TreeNode*) top(stack);
|
|
stack = pop(stack);
|
|
|
|
TreeNode *right = node->right;
|
|
while (right != NULL) {
|
|
stack = push(stack,right);
|
|
right = right->left;
|
|
}
|
|
return node->data;
|
|
}
|
|
|
|
// Releases all memory resources (including data copies).
|
|
void clearTree(TreeNode *root)
|
|
{
|
|
if (root != NULL) {
|
|
if (root->left != NULL) {
|
|
clearTree(root->left);
|
|
}
|
|
if (root->right != NULL) {
|
|
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 size = 0;
|
|
if (root == NULL) {
|
|
return 0;
|
|
}
|
|
|
|
if (root->left != NULL)
|
|
{
|
|
size += treeSize(root->left);
|
|
}
|
|
if (root->right != NULL)
|
|
{
|
|
size += treeSize(root->right);
|
|
}
|
|
return size+1;
|
|
|
|
} |