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

[Update] Add print function + test

parent af965ba7
No related branches found
No related tags found
No related merge requests found
......@@ -214,6 +214,43 @@ static MunitResult test_tree_is_BST()
return MUNIT_OK;
}
static MunitResult test_tree_print()
{
node_t* nd = bst_create();
nd = bst_insert(nd,8); // 12
nd = bst_insert(nd,3); /* / \ */ // Not BST
nd = bst_insert(nd,10); // 5 32
nd = bst_insert(nd,1);
nd = bst_insert(nd,6);
nd = bst_insert(nd,4);
nd = bst_insert(nd,7);
nd = bst_insert(nd,14);
nd = bst_insert(nd,13);
FILE* f = tmpfile();
bst_print(nd,f);
char buffer[22];
char expected[22] = "1 3 4 6 7 8 10 13 14 ";
fseek(f, 0L, SEEK_SET);
fread(buffer,22,sizeof(char),f);
munit_assert_string_equal(buffer,expected);
bst_destroy(nd);
return MUNIT_OK;
}
static MunitResult test_tree_delete()
{
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_int32(bst_delete(nd,32)->val,==,32);
bst_destroy(nd);
return MUNIT_OK;
}
static MunitTest test_suite_tests[] = {
{
......@@ -304,14 +341,30 @@ static MunitTest test_suite_tests[] = {
MUNIT_TEST_OPTION_NONE,
NULL
},
// {
// (char*) "/test_tree_is_BST",
// test_tree_is_BST,
// NULL,
// NULL,
// MUNIT_TEST_OPTION_NONE,
// NULL
// },
{
(char*) "/test_tree_is_BST",
test_tree_is_BST,
NULL,
NULL,
MUNIT_TEST_OPTION_NONE,
NULL
},
{
(char*) "/test_tree_print",
test_tree_print,
NULL,
NULL,
MUNIT_TEST_OPTION_NONE,
NULL
},
{
(char*) "/test_tree_delete",
test_tree_delete,
NULL,
NULL,
MUNIT_TEST_OPTION_NONE,
NULL
},
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
};
......
......@@ -9,6 +9,8 @@ node_t *bst_create()
return NULL;
}
// Marche po T_T
bool bst_is_bst(node_t *tree)
{
if(tree == NULL) // Default is true
......@@ -31,22 +33,63 @@ bool bst_is_bst(node_t *tree)
}
return bst_is_bst(tree->right);
}
return NULL;
}
void bst_print(node_t *tree,FILE* out)
{
if(tree == NULL){return;}
if(tree->left != NULL)
{
bst_print(tree->left,out);
fprintf(out,"%d ",tree->val);
}
if(tree->left == NULL && tree->right != NULL)
{
fprintf(out,"%d ",tree->val);
}
if(tree->right != NULL)
{
bst_print(tree->right,out);
}
if(tree->left == NULL && tree->right == NULL)
{
fprintf(out,"%d ",tree->val);
}
}
node_t *bst_delete(node_t *tree, int val)
{
if(tree == NULL)
{
return NULL;
}
if(val > tree->val)
{
}
else if(val < tree->val)
{
}
}
node_t *bst_find_min_node(node_t *tree)
{
node_t* tmp = tree;
if(tmp == NULL)
if(tree == NULL)
{
return NULL;
}
if(tmp->left != NULL)
if(tree->left != NULL)
{
return bst_find_min_node(tmp->left);
return bst_find_min_node(tree->left);
}
else
{
return tmp;
return tree;
}
}
......@@ -64,18 +107,17 @@ int bst_find_max(node_t *tree)
node_t* bst_find_max_node(node_t *tree)
{
node_t* tmp = tree;
if(tmp == NULL)
if(tree == NULL)
{
return NULL;
}
if(tmp->right != NULL)
if(tree->right != NULL)
{
return bst_find_max_node(tmp->right);
return bst_find_max_node(tree->right);
}
else
{
return tmp;
return tree;
}
}
......@@ -122,6 +164,7 @@ void bst_destroy(node_t *tree)
}
}
bool bst_is_empty(node_t *tree)
{
return (tree == NULL)?true:false;
......
......@@ -15,7 +15,7 @@ typedef struct _node_t {
node_t *bst_create(); // création d'un arbre vide (retourne NULL)
void bst_destroy(node_t *tree); // détruit l'arbre et vide la mémoire
void bst_print(node_t *tree); // affiche l'arbre (voir plus bas)
void bst_print(node_t *tree,FILE* out); // affiche l'arbre (voir plus bas)
// insertion de val dans l'arbre et retourne l'arbre (ou NULL si problème)
node_t *bst_insert(node_t *tree, int val);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment