Compare commits

...

3 Commits

View File

@ -4,8 +4,8 @@
//TODO: binären Suchbaum implementieren //TODO: binären Suchbaum implementieren
/* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv), /* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv), Done
* `clearTree`: gibt den gesamten Baum frei (rekursiv), * `clearTree`: gibt den gesamten Baum frei (rekursiv), Done
* `treeSize`: zählt die Knoten im Baum (rekursiv), * `treeSize`: zählt die Knoten im Baum (rekursiv),
* `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. */ * `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. */
@ -18,14 +18,17 @@
void clearTree (TreeNode *root); void clearTree (TreeNode *root);
unsigned int treeSize (const TreeNode *root); unsigned int treeSize (const TreeNode *root);
// selfdeclaredfunctions // self declared functions
TreeNode *addToTreeRec (TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate); TreeNode *addToTreeRec (TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate);
void clearTreeRec (TreeNode *currentNode);
void clearNode (TreeNode *node);
void treeSizeRec (const TreeNode *currentNode, unsigned int *nodeCount);
// Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates // 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). // 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? // returned Value is new root
TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate) TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate)
{ {
// create a node // create a node
@ -63,11 +66,11 @@ TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType
//duplicate //duplicate
if (isDuplicate == NULL) if (isDuplicate == NULL)
{ {
return newNode; currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate);
} }
else else
{ {
isDuplicate = 1; *isDuplicate = 1;
} }
} }
@ -79,6 +82,8 @@ TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType
// 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. // 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, // 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. // push the top node and push all its left nodes.
// Needs stack!!
void *nextTreeData(TreeNode *root) void *nextTreeData(TreeNode *root)
{ {
@ -88,13 +93,53 @@ void *nextTreeData(TreeNode *root)
// Releases all memory resources (including data copies). // Releases all memory resources (including data copies).
void clearTree(TreeNode *root) void clearTree(TreeNode *root)
{ {
clearTreeRec(root);
}
void clearTreeRec(TreeNode *currentNode)
{
if (currentNode != NULL)
{
clearTree(currentNode->left);
clearNode(currentNode);
clearTree(currentNode->right);
}
}
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. // Returns the number of entries in the tree given by root.
unsigned int treeSize(const TreeNode *root) unsigned int treeSize(const TreeNode *root)
{ {
unsigned int amountOfNodes = 0;
treeSizeRec(root, &amountOfNodes);
return amountOfNodes;
}
void treeSizeRec(const TreeNode *currentNode, unsigned int *nodeCount)
{
if (currentNode != NULL)
{
treeSizeRec(currentNode->left, nodeCount);
*nodeCount++;
treeSizeRec(currentNode->right, nodeCount);
}
} }