wrote clearTree(), created clearTreeRec() and clearNode()

This commit is contained in:
Jonas Hofmann 2025-12-06 23:18:07 +01:00
parent 494fa2237c
commit e52da066fa

View File

@ -18,14 +18,16 @@
void clearTree (TreeNode *root);
unsigned int treeSize (const TreeNode *root);
// selfdeclaredfunctions
// self declared functions
TreeNode *addToTreeRec (TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate);
TreeNode *clearTreeRec (TreeNode *currentNode);
void clearNode (TreeNode *node);
// 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?
// returned Value is new root
TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate)
{
// create a node
@ -79,6 +81,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.
// 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!!
void *nextTreeData(TreeNode *root)
{
@ -88,7 +92,31 @@ void *nextTreeData(TreeNode *root)
// Releases all memory resources (including data copies).
void clearTree(TreeNode *root)
{
clearTreeRec(root);
}
TreeNode *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;
}