diff --git a/tests/test.c b/tests/test.c index 7d3f9fd528dd9670c6731642ada3be4b01f677e3..92cb42c6bda76394b9f963ee5c8437826bc57ed6 100644 --- a/tests/test.c +++ b/tests/test.c @@ -29,11 +29,47 @@ static MunitResult test_tree_insert() static MunitResult test_tree_destroy() { node_t* nd = bst_create(); + munit_assert_ptr(nd,==,NULL); + nd = bst_insert(nd,12); + nd = bst_insert(nd,22); + nd = bst_insert(nd,32); + nd = bst_insert(nd,42); + nd = bst_insert(nd,52); + nd = bst_insert(nd,62); + nd = bst_insert(nd,72); + nd = bst_insert(nd,82); munit_assert_ptr(nd,!=,NULL); bst_destroy(nd); return MUNIT_OK; } +static MunitResult test_tree_length() +{ + node_t* nd = bst_create(); + munit_assert_ptr(nd,==,NULL); + nd = bst_insert(nd,12); + nd = bst_insert(nd,22); + nd = bst_insert(nd,32); + nd = bst_insert(nd,42); + nd = bst_insert(nd,52); + nd = bst_insert(nd,62); + nd = bst_insert(nd,72); + nd = bst_insert(nd,82); + munit_assert_ptr(nd,!=,NULL); + bst_destroy(nd); + return MUNIT_OK; +} + +static MunitResult test_tree_is_empty() +{ + node_t* nd = bst_create(); + munit_assert_true(bst_is_empty(nd)); + nd = bst_insert(nd,12); + munit_assert_false(bst_is_empty(nd)); + bst_destroy(nd); + return MUNIT_OK; +} + static MunitTest test_suite_tests[] = { { @@ -52,6 +88,30 @@ static MunitTest test_suite_tests[] = { MUNIT_TEST_OPTION_NONE, NULL }, + { + (char*) "/test_tree_destroy", + test_tree_destroy, + NULL, + NULL, + MUNIT_TEST_OPTION_NONE, + NULL + }, + { + (char*) "/test_tree_length", + test_tree_length, + NULL, + NULL, + MUNIT_TEST_OPTION_NONE, + NULL + }, + { + (char*) "/test_tree_empty", + test_tree_is_empty, + NULL, + NULL, + MUNIT_TEST_OPTION_NONE, + NULL + }, { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } }; diff --git a/tree.c b/tree.c index b8e12bb59dc647a122968311f5e9a69421c1053a..44bf7ac2e0c21a187b06ba3f79849513eed34e17 100644 --- a/tree.c +++ b/tree.c @@ -21,6 +21,7 @@ void bst_destroy(node_t *tree) bst_destroy(tree->right); tree->right = NULL; } + if(tree->right == NULL && tree->left == NULL) { free(tree); @@ -28,6 +29,11 @@ void bst_destroy(node_t *tree) } } +bool bst_is_empty(node_t *tree) +{ + return (tree == NULL)?true:false; +} + node_t *bst_insert(node_t *tree, int val) { if(tree == NULL) @@ -41,25 +47,30 @@ node_t *bst_insert(node_t *tree, int val) if(val > tree->val) // Right { - // if(tree->right != NULL) - tree = bst_insert(tree->right,val); - // else if(tree->right == NULL) - // { - // tree->right = malloc(1 * sizeof(node_t)); - // tree->right->val = val; - // tree->left = NULL; - // } + tree->right = bst_insert(tree->right,val); } else if(val < tree->val) // Left { - // if(tree->left != NULL) - tree = bst_insert(tree->left,val); - // else if(tree->left == NULL) - // { - // tree->left = malloc(1 * sizeof(node_t)); - // tree->left->val = val; - // tree->right = NULL; - // } + tree->left = bst_insert(tree->left,val); } return tree; +} + +uint32_t bst_lenght(node_t *tree) +{ + uint32_t length = 0; + if(tree->left == NULL && tree->right == NULL && tree != NULL) + { + return 1; + } + else + { + if (tree->left != NULL) { + length += bst_lenght(tree->left); + } + if(tree->right != NULL){ + length += bst_lenght(tree->right); + } + } + return length; } \ No newline at end of file diff --git a/tree.h b/tree.h index 36d60039eef19580a5d2062229ed1237a0f69433..fa9d80d7cd84c02cdc70ac99754868257d286fb6 100644 --- a/tree.h +++ b/tree.h @@ -38,7 +38,7 @@ node_t *bst_find_max_node(node_t *tree); // retourne la valeur la plus grande stockée dans l'arbre (ou MAX_INT) int bst_find_max(node_t *tree); -int bst_lenght(node_t *tree); // Retourne la longeur de l'arbre -int bst_is_empty(node_t *tree); // Retourne true si l'arbre est vide +uint32_t bst_lenght(node_t *tree); // Retourne la longeur de l'arbre +bool bst_is_empty(node_t *tree); // Retourne true si l'arbre est vide #endif \ No newline at end of file