250 lines
6.4 KiB
C
250 lines
6.4 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include "stack.h"
|
|
#include "bintree.h"
|
|
|
|
|
|
//TODO: binären Suchbaum implementieren
|
|
/* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv), Done
|
|
* `clearTree`: gibt den gesamten Baum frei (rekursiv), Done
|
|
* `treeSize`: zählt die Knoten im Baum (rekursiv), Done
|
|
* `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. Done */
|
|
|
|
|
|
static StackNode *stackRoot = NULL;
|
|
|
|
|
|
TreeNode *addToTree (TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate);
|
|
void *nextTreeData (TreeNode *root);
|
|
void clearTree (TreeNode *root);
|
|
unsigned int treeSize (const TreeNode *root);
|
|
|
|
// self declared functions
|
|
TreeNode *addToTreeRec (TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate, const int root);
|
|
void clearTreeRec (TreeNode *currentNode);
|
|
void clearNode (TreeNode *node);
|
|
int treeSizeRec (const TreeNode *currentNode);
|
|
void *nextTreeDataRec (TreeNode *node, StackNode *stack);
|
|
|
|
|
|
// 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).
|
|
|
|
// returned Value is new root
|
|
TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate)
|
|
{
|
|
// create a node
|
|
TreeNode *newNode;
|
|
|
|
|
|
newNode = calloc(1, sizeof(TreeNode));
|
|
newNode->data = calloc(1, dataSize);
|
|
newNode->left = NULL;
|
|
newNode->right = NULL;
|
|
memcpy(newNode->data, data, dataSize);
|
|
|
|
|
|
return addToTreeRec(root, newNode, compareFct, isDuplicate, 1);
|
|
}
|
|
|
|
/*
|
|
TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate, const int root)
|
|
{
|
|
/*if ((currentNode == NULL))
|
|
{
|
|
if ((isDuplicate == NULL) || root)
|
|
{
|
|
return newNode;
|
|
}
|
|
else
|
|
{
|
|
return currentNode;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Mögliche Ergänzung --------------------------
|
|
if (currentNode == NULL)
|
|
{
|
|
if (isDuplicate != NULL)
|
|
*isDuplicate = 0;
|
|
|
|
return newNode;
|
|
}
|
|
//--------------------------------
|
|
|
|
else if ((compareFct(currentNode->data, newNode->data) < 0))
|
|
{
|
|
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate, 0);
|
|
}
|
|
else if ((compareFct(currentNode->data, newNode->data) > 0))
|
|
{
|
|
currentNode->right = addToTreeRec(currentNode->right, newNode, compareFct, isDuplicate, 0);
|
|
}
|
|
else if ((compareFct(currentNode->data, newNode->data) == 0))
|
|
{
|
|
if (isDuplicate == NULL)
|
|
{
|
|
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate, 0);
|
|
}
|
|
else
|
|
{
|
|
*isDuplicate = 1;
|
|
}
|
|
}
|
|
|
|
|
|
return currentNode;
|
|
} */
|
|
|
|
TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate, const int root)
|
|
{
|
|
if ((currentNode == NULL))
|
|
{
|
|
if ((isDuplicate == NULL) || root)
|
|
{
|
|
if (isDuplicate != NULL)
|
|
{
|
|
*isDuplicate = 0;
|
|
}
|
|
|
|
return newNode;
|
|
}
|
|
else
|
|
{
|
|
*isDuplicate = 0;
|
|
return currentNode;
|
|
}
|
|
}
|
|
else if ((compareFct(currentNode->data, newNode->data) < 0))
|
|
{
|
|
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate, 0);
|
|
}
|
|
else if ((compareFct(currentNode->data, newNode->data) > 0))
|
|
{
|
|
currentNode->right = addToTreeRec(currentNode->right, newNode, compareFct, isDuplicate, 0);
|
|
}
|
|
else if ((compareFct(currentNode->data, newNode->data) == 0))
|
|
{
|
|
if (isDuplicate == NULL)
|
|
{
|
|
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate, 0);
|
|
}
|
|
else
|
|
{
|
|
*isDuplicate = 1;
|
|
}
|
|
}
|
|
|
|
if (isDuplicate != NULL)
|
|
{
|
|
*isDuplicate = 0;
|
|
}
|
|
return currentNode;
|
|
}
|
|
|
|
|
|
// 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.
|
|
|
|
// Needs stack!!
|
|
// Stack functions: push(), pop(), top(), clearStack()
|
|
void *nextTreeData(TreeNode *root)
|
|
{
|
|
void *pointerToReturn = NULL;
|
|
|
|
if (root != NULL)
|
|
{
|
|
// Add tree to stack so that bigest entry is ontop
|
|
stackRoot = nextTreeDataRec(root, stackRoot);
|
|
pointerToReturn = stackRoot;
|
|
}
|
|
else
|
|
{
|
|
// return current top entry and then pop that top entry
|
|
pointerToReturn = top(stackRoot);
|
|
stackRoot = pop(stackRoot);
|
|
}
|
|
|
|
|
|
return pointerToReturn;
|
|
}
|
|
|
|
|
|
void *nextTreeDataRec(TreeNode *tree, StackNode *stack)
|
|
{
|
|
if (tree != NULL)
|
|
{
|
|
stack = nextTreeDataRec (tree->left, stack);
|
|
stack = push (stack, tree->data);
|
|
stack = nextTreeDataRec (tree->right, stack);
|
|
}
|
|
|
|
|
|
return stack;
|
|
}
|
|
|
|
|
|
// Releases all memory resources (including data copies).
|
|
void clearTree(TreeNode *root)
|
|
{
|
|
clearTreeRec(root);
|
|
}
|
|
|
|
|
|
void clearTreeRec(TreeNode *currentNode)
|
|
{
|
|
if (currentNode != NULL)
|
|
{
|
|
clearTree(currentNode->left);
|
|
clearTree(currentNode->right);
|
|
clearNode(currentNode);
|
|
}
|
|
}
|
|
|
|
|
|
void clearNode(TreeNode *node)
|
|
{
|
|
free(node->data);
|
|
node->data = NULL;
|
|
|
|
node->left = NULL;
|
|
node->right = NULL;
|
|
|
|
free(node);
|
|
node = NULL;
|
|
}
|
|
|
|
|
|
// Returns the number of entries in the tree given by root.
|
|
unsigned int treeSize(const TreeNode *root)
|
|
{
|
|
unsigned int amountOfNodes = 0;
|
|
|
|
|
|
amountOfNodes = treeSizeRec(root);
|
|
|
|
|
|
return amountOfNodes;
|
|
}
|
|
|
|
|
|
int treeSizeRec(const TreeNode *currentNode)
|
|
{
|
|
int nodeCount = 0;
|
|
|
|
|
|
if (currentNode != NULL)
|
|
{
|
|
nodeCount += treeSizeRec(currentNode->left);
|
|
nodeCount += treeSizeRec(currentNode->right);
|
|
return nodeCount + 1;
|
|
}
|
|
|
|
return nodeCount;
|
|
}
|
|
|