101 lines
2.9 KiB
C
101 lines
2.9 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 *root = 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);
|
|
|
|
// selfdeclaredfunctions
|
|
TreeNode *addToTreeRec (TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate);
|
|
|
|
|
|
// 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);
|
|
}
|
|
|
|
|
|
TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate)
|
|
{
|
|
if (currentNode == NULL)
|
|
{
|
|
isDuplicate = 0;
|
|
return newNode;
|
|
}
|
|
else if (compareFct(newNode->data, currentNode->data) < 0)
|
|
{
|
|
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate);
|
|
}
|
|
else if (compareFct(newNode->data, currentNode->data) > 0)
|
|
{
|
|
currentNode->right = addToTreeRec(currentNode->right, newNode, compareFct, isDuplicate);
|
|
}
|
|
else
|
|
{
|
|
//duplicate
|
|
if (isDuplicate == NULL)
|
|
{
|
|
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate);
|
|
}
|
|
else
|
|
{
|
|
isDuplicate = 1;
|
|
}
|
|
}
|
|
|
|
|
|
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.
|
|
void *nextTreeData(TreeNode *root)
|
|
{
|
|
|
|
}
|
|
|
|
|
|
// Releases all memory resources (including data copies).
|
|
void clearTree(TreeNode *root)
|
|
{
|
|
|
|
}
|
|
|
|
|
|
// Returns the number of entries in the tree given by root.
|
|
unsigned int treeSize(const TreeNode *root)
|
|
{
|
|
|
|
}
|
|
|