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

[Update] Add bst_search + test

parent 26f2fa16
No related branches found
No related tags found
No related merge requests found
...@@ -134,6 +134,26 @@ static MunitResult test_tree_find_max() ...@@ -134,6 +134,26 @@ static MunitResult test_tree_find_max()
return MUNIT_OK; return MUNIT_OK;
} }
static MunitResult test_tree_search()
{
node_t* nd = bst_create();
munit_assert_true(bst_is_empty(nd));
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);
nd = bst_insert(nd,50);
munit_assert_ptr_not_null(bst_search(nd,50));
munit_assert_ptr_null(bst_search(nd,5239487));
bst_destroy(nd);
return MUNIT_OK;
}
static MunitResult test_tree_is_BST() static MunitResult test_tree_is_BST()
{ {
node_t* nd = bst_create(); node_t* nd = bst_create();
...@@ -222,6 +242,14 @@ static MunitTest test_suite_tests[] = { ...@@ -222,6 +242,14 @@ static MunitTest test_suite_tests[] = {
MUNIT_TEST_OPTION_NONE, MUNIT_TEST_OPTION_NONE,
NULL NULL
}, },
{
(char*) "/test_tree_search",
test_tree_search,
NULL,
NULL,
MUNIT_TEST_OPTION_NONE,
NULL
},
// { // {
// (char*) "/test_tree_is_BST", // (char*) "/test_tree_is_BST",
// test_tree_is_BST, // test_tree_is_BST,
......
...@@ -66,6 +66,27 @@ int bst_find_max(node_t *tree) ...@@ -66,6 +66,27 @@ int bst_find_max(node_t *tree)
} }
} }
node_t *bst_search(node_t *tree, int val)
{
if(tree == NULL)
{
return NULL;
}
if(val == tree->val)
{
return tree;
}
if(val > tree->val) // Right
{
return bst_search(tree->right,val);
}
if(val < tree->val) // Left
{
return bst_search(tree->left,val);
}
}
void bst_destroy(node_t *tree) void bst_destroy(node_t *tree)
{ {
if(tree == NULL){return;} if(tree == NULL){return;}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment