diff --git a/tests/test.c b/tests/test.c index e2b00f43e293f91a4364ca033660269aacd6796b..07893ec61a8c0be1a4e73b8bf1df616d7036ec4d 100644 --- a/tests/test.c +++ b/tests/test.c @@ -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 } }; diff --git a/tree.c b/tree.c index 01d12bd7be38401f078b3005607edc5d123f4cd4..bb4535b93d7df1903905ade3ca3ccc443eed2ca0 100644 --- a/tree.c +++ b/tree.c @@ -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; diff --git a/tree.h b/tree.h index 2255ce725b5222651de2c53acf7fe1afd228b887..aa475e8707fea5317982d816dce0a99f9e2bf058 100644 --- a/tree.h +++ b/tree.h @@ -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);