Skip to content
Snippets Groups Projects
Commit 4f4fae8b authored by poulpe's avatar poulpe
Browse files

[Update] Add is_present

parent 9bdc66c9
No related branches found
No related tags found
No related merge requests found
......@@ -55,6 +55,14 @@ static MunitResult test_tree_length()
nd = bst_insert(nd,62);
nd = bst_insert(nd,72);
nd = bst_insert(nd,82);
nd = bst_insert(nd,12);
nd = bst_insert(nd,22);
nd = bst_insert(nd,32);
nd = bst_insert(nd,42);
nd = bst_insert(nd,2);
nd = bst_insert(nd,3);
nd = bst_insert(nd,4);
nd = bst_insert(nd,5);
munit_assert_ptr(nd,!=,NULL);
bst_destroy(nd);
return MUNIT_OK;
......@@ -70,6 +78,24 @@ static MunitResult test_tree_is_empty()
return MUNIT_OK;
}
static MunitResult test_tree_is_present()
{
node_t* nd = bst_create();
munit_assert_false(bst_is_present(nd,10));
nd = bst_insert(nd,12);
nd = bst_insert(nd,22);
nd = bst_insert(nd,32);
nd = bst_insert(nd,42);
nd = bst_insert(nd,2);
nd = bst_insert(nd,3);
nd = bst_insert(nd,4);
nd = bst_insert(nd,5);
nd = bst_insert(nd,10);
munit_assert_true(bst_is_present(nd,10));
bst_destroy(nd);
return MUNIT_OK;
}
static MunitTest test_suite_tests[] = {
{
......@@ -112,6 +138,14 @@ static MunitTest test_suite_tests[] = {
MUNIT_TEST_OPTION_NONE,
NULL
},
{
(char*) "/test_tree_is_present",
test_tree_is_present,
NULL,
NULL,
MUNIT_TEST_OPTION_NONE,
NULL
},
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
};
......
......@@ -34,6 +34,25 @@ bool bst_is_empty(node_t *tree)
return (tree == NULL)?true:false;
}
bool bst_is_present(node_t *tree, int val)
{
if(tree == NULL){return false;}
if(tree->val == val)
{
return true;
}
else
{
if(val > tree->val)
{
return bst_is_present(tree->right,val);
}else
{
return bst_is_present(tree->left,val);
}
}
}
node_t *bst_insert(node_t *tree, int val)
{
if(tree == NULL)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment