From 26f2fa167cbbbd1e6bb1a36687dc6212073a9ca6 Mon Sep 17 00:00:00 2001 From: poulpe <poulpe@localhost.localdomain> Date: Wed, 24 Mar 2021 15:54:58 +0100 Subject: [PATCH] [Update] Add find_max + test --- tests/test.c | 30 +++++++++++++++++++++++++++++- tree.c | 16 ++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/tests/test.c b/tests/test.c index 8ecab52..8678185 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 7491828..24c20bc 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;} -- GitLab