From e52da066fa2239621706d1766235cf8b60f88599 Mon Sep 17 00:00:00 2001 From: Hofmann Jonas Date: Sat, 6 Dec 2025 23:18:07 +0100 Subject: [PATCH] wrote clearTree(), created clearTreeRec() and clearNode() --- bintree.c | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/bintree.c b/bintree.c index 1943006..60daca8 100644 --- a/bintree.c +++ b/bintree.c @@ -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; }