Skip to content
Snippets Groups Projects
Commit 9bdc66c9 authored by poulpe's avatar poulpe
Browse files

[Update] Add test + fix somes function

parent 4172d801
No related branches found
No related tags found
No related merge requests found
...@@ -29,11 +29,47 @@ static MunitResult test_tree_insert() ...@@ -29,11 +29,47 @@ static MunitResult test_tree_insert()
static MunitResult test_tree_destroy() static MunitResult test_tree_destroy()
{ {
node_t* nd = bst_create(); 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); munit_assert_ptr(nd,!=,NULL);
bst_destroy(nd); bst_destroy(nd);
return MUNIT_OK; 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[] = { static MunitTest test_suite_tests[] = {
{ {
...@@ -52,6 +88,30 @@ static MunitTest test_suite_tests[] = { ...@@ -52,6 +88,30 @@ static MunitTest test_suite_tests[] = {
MUNIT_TEST_OPTION_NONE, MUNIT_TEST_OPTION_NONE,
NULL 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 } { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
}; };
......
...@@ -21,6 +21,7 @@ void bst_destroy(node_t *tree) ...@@ -21,6 +21,7 @@ void bst_destroy(node_t *tree)
bst_destroy(tree->right); bst_destroy(tree->right);
tree->right = NULL; tree->right = NULL;
} }
if(tree->right == NULL && tree->left == NULL) if(tree->right == NULL && tree->left == NULL)
{ {
free(tree); free(tree);
...@@ -28,6 +29,11 @@ void bst_destroy(node_t *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) node_t *bst_insert(node_t *tree, int val)
{ {
if(tree == NULL) if(tree == NULL)
...@@ -41,25 +47,30 @@ node_t *bst_insert(node_t *tree, int val) ...@@ -41,25 +47,30 @@ node_t *bst_insert(node_t *tree, int val)
if(val > tree->val) // Right if(val > tree->val) // Right
{ {
// if(tree->right != NULL) tree->right = bst_insert(tree->right,val);
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;
// }
} }
else if(val < tree->val) // Left else if(val < tree->val) // Left
{ {
// if(tree->left != NULL) tree->left = bst_insert(tree->left,val);
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;
// }
} }
return tree; 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
...@@ -38,7 +38,7 @@ node_t *bst_find_max_node(node_t *tree); ...@@ -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) // retourne la valeur la plus grande stockée dans l'arbre (ou MAX_INT)
int bst_find_max(node_t *tree); int bst_find_max(node_t *tree);
int bst_lenght(node_t *tree); // Retourne la longeur de l'arbre uint32_t 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 bool bst_is_empty(node_t *tree); // Retourne true si l'arbre est vide
#endif #endif
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment