treeSize + Test

This commit is contained in:
Thomas Rauh Desktop 2025-11-23 01:05:43 +01:00
parent 11b25b75d2
commit 50079dbfca
4 changed files with 44 additions and 10 deletions

View File

@ -114,5 +114,8 @@ void clearTree(TreeNode *root)
// Returns the number of entries in the tree given by root.
unsigned int treeSize(const TreeNode *root)
{
if(root==NULL){
return 0;
}
return 1 + treeSize(root->left) + treeSize(root->right);
}

Binary file not shown.

View File

@ -73,6 +73,7 @@ static void testNextTreeDataInorderTraversal(){
}
static void testClearTree() {
TreeNode *root = NULL;
int values[] = {10,5,15};
@ -90,6 +91,33 @@ static void testClearTree() {
TEST_ASSERT_NULL(root);
}
static void testTreeSizeEmpty() {
TreeNode *root = NULL;
TEST_ASSERT_EQUAL_INT(0,treeSize(root));
}
static void testTreeSizeSingleNode() {
TreeNode *root = NULL;
int value = 42;
root = addToTree(root,&value,sizeof(int),compareInt,NULL);
TEST_ASSERT_EQUAL_INT(1,treeSize(root));
clearTree(root);
}
static void testTreeSizeMultipleNodes() {
TreeNode *root = NULL;
int values[] = {10,5,15,3,7,12,18};
unsigned int expectedSize = sizeof(values) / sizeof(values[0]); //Auf groeße von data achten
for (int i=0;i<expectedSize;i++) {
root = addToTree(root, &values[i], sizeof(int), compareInt, NULL);
}
TEST_ASSERT_EQUAL_INT(expectedSize, treeSize(root));
clearTree(root);
}
void setUp(void){
}
@ -108,6 +136,9 @@ int main(){
RUN_TEST(testAddToTreeNoData);
RUN_TEST(testNextTreeDataInorderTraversal);
RUN_TEST(testClearTree);
RUN_TEST(testTreeSizeEmpty);
RUN_TEST(testTreeSizeSingleNode);
RUN_TEST(testTreeSizeMultipleNodes);
return UNITY_END();
}

Binary file not shown.