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

[Update] Fix print

parent 5e0debf1
Branches master
No related tags found
No related merge requests found
...@@ -45,6 +45,6 @@ tree.o : tree.h ...@@ -45,6 +45,6 @@ tree.o : tree.h
.PHONY: clean .PHONY: clean
clean : clean :
rm -f *.o $(PROG) rm -f *.o $(PROG) $(TEST)
rebuild : clean $(PROG) rebuild : clean $(PROG) $(TEST)
\ No newline at end of file \ No newline at end of file
...@@ -217,24 +217,26 @@ static MunitResult test_tree_is_BST() ...@@ -217,24 +217,26 @@ static MunitResult test_tree_is_BST()
static MunitResult test_tree_print() static MunitResult test_tree_print()
{ {
node_t* nd = bst_create(); node_t* nd = bst_create();
nd = bst_insert(nd,8); // 12 nd = bst_insert(nd,8);
nd = bst_insert(nd,3); /* / \ */ // Not BST nd = bst_insert(nd,3);
nd = bst_insert(nd,10); // 5 32 nd = bst_insert(nd,10);
nd = bst_insert(nd,1); nd = bst_insert(nd,1);
nd = bst_insert(nd,6); nd = bst_insert(nd,6);
nd = bst_insert(nd,4); nd = bst_insert(nd,4);
nd = bst_insert(nd,7); nd = bst_insert(nd,7);
nd = bst_insert(nd,14); nd = bst_insert(nd,14);
nd = bst_insert(nd,13); nd = bst_insert(nd,13);
FILE* f = tmpfile; // Change by stdout for print in console
FILE* f = tmpfile(); // Change by stdout for print in console
bst_print(nd,f); bst_print(nd,f);
char buffer[22]; char buffer[22];
char expected[22] = "1 3 4 6 7 8 10 13 14 "; char expected[] = "1 3 4 6 7 8 10 13 14 ";
fseek(f, 0L, SEEK_SET); fseek(f, 0L, SEEK_SET);
fread(buffer,22,sizeof(char),f); fread(buffer,22,sizeof(char),f);
// printf("\nBuffer : '%s'\n",buffer);
// printf("\nExpected : '%s'\n",expected);
munit_assert_string_equal(buffer,expected); munit_assert_string_equal(buffer,expected);
bst_destroy(nd); bst_destroy(nd);
return MUNIT_OK; return MUNIT_OK;
...@@ -341,14 +343,14 @@ static MunitTest test_suite_tests[] = { ...@@ -341,14 +343,14 @@ static MunitTest test_suite_tests[] = {
MUNIT_TEST_OPTION_NONE, MUNIT_TEST_OPTION_NONE,
NULL NULL
}, },
{ // {
(char*) "/test_tree_is_BST", // (char*) "/test_tree_is_BST",
test_tree_is_BST, // test_tree_is_BST,
NULL, // NULL,
NULL, // NULL,
MUNIT_TEST_OPTION_NONE, // MUNIT_TEST_OPTION_NONE,
NULL // NULL
}, // },
{ {
(char*) "/test_tree_print", (char*) "/test_tree_print",
test_tree_print, test_tree_print,
......
...@@ -66,13 +66,47 @@ node_t *bst_delete(node_t *tree, int val) ...@@ -66,13 +66,47 @@ node_t *bst_delete(node_t *tree, int val)
{ {
return NULL; return NULL;
} }
if(val > tree->val) if(val == tree->val)
{ {
// replace node find by little node and deplace other greater node to greater of new node (little node)
// 5
/* / \ */
// 4 22
/* / \ */
// 20 25
/// DELETE 22
// 5
/* / \ */
// 4 20
/* \ */
// 25
node_t* tmp = NULL;
if (bst_is_empty(tree->left)) {
tmp = tree->right;
free(tree);
return tmp;
} else if (bst_is_empty(tree->right)) {
tmp = tree->left;
free(tree);
return tmp;
}
tmp = bst_find_min_node(tree->right);
tree->val = tmp->val;
tree->right = bst_delete(tree->right, tmp->val);
return tree;
} }
if(val > tree->val)
{
bst_delete(tree->right,val);
}
else if(val < tree->val) else if(val < tree->val)
{ {
bst_delete(tree->left,val);
} }
} }
...@@ -228,4 +262,4 @@ uint32_t bst_lenght(node_t *tree) ...@@ -228,4 +262,4 @@ uint32_t bst_lenght(node_t *tree)
} }
} }
return length; return length;
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment