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. // Returns the number of entries in the tree given by root.
unsigned int treeSize(const TreeNode *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

@ -53,19 +53,19 @@ static void testAddToTreeNoData(){
static void testNextTreeDataInorderTraversal(){ static void testNextTreeDataInorderTraversal(){
TreeNode *root = NULL; TreeNode *root = NULL;
int values[] = {5, 3, 8, 1, 4}; int values[] = {5,3,8,1,4};
int expectedInorder[] = {1, 3, 4, 5, 8}; int expectedInorder[] = {1,3,4,5,8};
for(int i = 0; i < 5; i++) { for(int i=0;i<5;i++) {
root = addToTree(root, &values[i], sizeof(int), compareInt, NULL); //Tree befüllen root = addToTree(root,&values[i],sizeof(int),compareInt,NULL); //Tree befüllen
} }
void *data = nextTreeData(root); void *data = nextTreeData(root);
TEST_ASSERT_NOT_NULL(data); TEST_ASSERT_NOT_NULL(data);
for(int i = 0; i < 5; i++){ for(int i=0;i<5;i++){
TEST_ASSERT_NOT_NULL(data); TEST_ASSERT_NOT_NULL(data);
TEST_ASSERT_EQUAL_INT(expectedInorder[i], *(int*)data); TEST_ASSERT_EQUAL_INT(expectedInorder[i],*(int*)data);
data = nextTreeData(NULL); // Nächstes Element data = nextTreeData(NULL); // Nächstes Element
} }
@ -73,13 +73,14 @@ static void testNextTreeDataInorderTraversal(){
} }
static void testClearTree() { static void testClearTree() {
TreeNode *root = NULL; TreeNode *root = NULL;
int values[] = {10, 5, 15}; int values[] = {10,5,15};
int i; int i;
for(i = 0; i < 3; i++) { for(i=0;i<3;i++) {
root = addToTree(root, &values[i], sizeof(int), compareInt, NULL); root = addToTree(root,&values[i],sizeof(int),compareInt,NULL);
} }
TEST_ASSERT_NOT_NULL(root); TEST_ASSERT_NOT_NULL(root);
@ -90,6 +91,33 @@ static void testClearTree() {
TEST_ASSERT_NULL(root); 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){ void setUp(void){
} }
@ -108,6 +136,9 @@ int main(){
RUN_TEST(testAddToTreeNoData); RUN_TEST(testAddToTreeNoData);
RUN_TEST(testNextTreeDataInorderTraversal); RUN_TEST(testNextTreeDataInorderTraversal);
RUN_TEST(testClearTree); RUN_TEST(testClearTree);
RUN_TEST(testTreeSizeEmpty);
RUN_TEST(testTreeSizeSingleNode);
RUN_TEST(testTreeSizeMultipleNodes);
return UNITY_END(); return UNITY_END();
} }

Binary file not shown.