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

[Update] Add find_min + test

parent 4f4fae8b
No related branches found
No related tags found
No related merge requests found
......@@ -96,6 +96,46 @@ static MunitResult test_tree_is_present()
return MUNIT_OK;
}
static MunitResult test_tree_find_min()
{
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);
munit_assert_int32(bst_find_min(nd),==,2);
bst_destroy(nd);
return MUNIT_OK;
}
static MunitResult test_tree_is_BST()
{
node_t* nd = bst_create();
nd = bst_insert(nd,12); // 12
nd = bst_insert(nd,5); /* / \ */ // Not BST
nd = bst_insert(nd,32); // 5 32
munit_assert_false(bst_is_bst(nd));
bst_destroy(nd);
nd = bst_create();
nd = bst_insert(nd,12); // 12
nd = bst_insert(nd,5); /* / */
nd = bst_insert(nd,10); // 5
/* / \ */
// 10
munit_assert_true(bst_is_bst(nd));
bst_destroy(nd);
return MUNIT_OK;
}
static MunitTest test_suite_tests[] = {
{
......@@ -146,6 +186,22 @@ static MunitTest test_suite_tests[] = {
MUNIT_TEST_OPTION_NONE,
NULL
},
{
(char*) "/test_tree_find_min",
test_tree_find_min,
NULL,
NULL,
MUNIT_TEST_OPTION_NONE,
NULL
},
// {
// (char*) "/test_tree_is_BST",
// test_tree_is_BST,
// NULL,
// NULL,
// MUNIT_TEST_OPTION_NONE,
// NULL
// },
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
};
......
......@@ -9,8 +9,51 @@ node_t *bst_create()
return NULL;
}
bool bst_is_bst(node_t *tree)
{
if(tree == NULL) // Default is true
{
return true;
}
if(tree->left != NULL)
{
if(tree->val < tree->left->val) // Is not BST
{
return false;
}
return bst_is_bst(tree->left);
}
if(tree->left != NULL)
{
if(tree->val > tree->right->val) // Is not BST
{
return false;
}
return bst_is_bst(tree->right);
}
}
int bst_find_min(node_t *tree)
{
if(tree == NULL)
{
return INT_MIN;
}
if(tree->left != NULL)
{
return bst_find_min(tree->left);
}
else
{
return tree->val;
}
}
void bst_destroy(node_t *tree)
{
if(tree == NULL){return;}
if(tree->left != NULL)
{
bst_destroy(tree->left);
......
......@@ -5,6 +5,7 @@
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <limits.h>
typedef struct _node_t {
int val;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment