diff --git a/tests/test.c b/tests/test.c index 8ecab52e743fa072dcac2409f22e282ee1f02310..8678185109c21940312f58df858097241c17e49c 100644 --- a/tests/test.c +++ b/tests/test.c @@ -109,7 +109,27 @@ static MunitResult test_tree_find_min() nd = bst_insert(nd,4); nd = bst_insert(nd,5); nd = bst_insert(nd,10); - munit_assert_int32(bst_find_min(nd),==,2); + nd = bst_insert(nd,1); + munit_assert_int32(bst_find_min(nd),==,1); + bst_destroy(nd); + return MUNIT_OK; +} + +static MunitResult test_tree_find_max() +{ + 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_int32(bst_find_max(nd),==,50); bst_destroy(nd); return MUNIT_OK; } @@ -194,6 +214,14 @@ static MunitTest test_suite_tests[] = { MUNIT_TEST_OPTION_NONE, NULL }, + { + (char*) "/test_tree_find_max", + test_tree_find_max, + NULL, + NULL, + MUNIT_TEST_OPTION_NONE, + NULL + }, // { // (char*) "/test_tree_is_BST", // test_tree_is_BST, diff --git a/tree.c b/tree.c index 749182895fa8d01ec46a2b39f01e773e2a285fd8..24c20bc0e4321ab580d98f8699413807d51b12fc 100644 --- a/tree.c +++ b/tree.c @@ -50,6 +50,22 @@ int bst_find_min(node_t *tree) } } +int bst_find_max(node_t *tree) +{ + if(tree == NULL) + { + return INT_MIN; + } + if(tree->right != NULL) + { + return bst_find_max(tree->right); + } + else + { + return tree->val; + } +} + void bst_destroy(node_t *tree) { if(tree == NULL){return;}