clearTree + Test

This commit is contained in:
Thomas Rauh Desktop 2025-11-23 00:45:24 +01:00
parent f4cf06ff53
commit 11b25b75d2
4 changed files with 31 additions and 0 deletions

View File

@ -96,7 +96,19 @@ 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)
{ {
if (root == NULL) {
return;
}
clearTree(root->left);
clearTree(root->right);
if (root->data != NULL) {
free(root->data);
root->data = NULL;
}
free(root);
} }
// Returns the number of entries in the tree given by root. // Returns the number of entries in the tree given by root.

Binary file not shown.

View File

@ -73,6 +73,23 @@ static void testNextTreeDataInorderTraversal(){
} }
static void testClearTree() {
TreeNode *root = NULL;
int values[] = {10, 5, 15};
int i;
for(i = 0; i < 3; i++) {
root = addToTree(root, &values[i], sizeof(int), compareInt, NULL);
}
TEST_ASSERT_NOT_NULL(root);
clearTree(root);
root = NULL;
TEST_ASSERT_NULL(root);
}
void setUp(void){ void setUp(void){
} }
@ -89,6 +106,8 @@ int main(){
RUN_TEST(testAddToTreeNewRoot); RUN_TEST(testAddToTreeNewRoot);
RUN_TEST(testAddToTreeToExistingRoot); RUN_TEST(testAddToTreeToExistingRoot);
RUN_TEST(testAddToTreeNoData); RUN_TEST(testAddToTreeNoData);
RUN_TEST(testNextTreeDataInorderTraversal);
RUN_TEST(testClearTree);
return UNITY_END(); return UNITY_END();
} }

Binary file not shown.