diff --git a/Makefile b/Makefile index ac6c1b272f25255af1755e500dfd7a370436478f..5da2e8aca7ed3a31728fbd6158cc8c3525dea1b6 100644 --- a/Makefile +++ b/Makefile @@ -45,6 +45,6 @@ tree.o : tree.h .PHONY: clean clean : - rm -f *.o $(PROG) + rm -f *.o $(PROG) $(TEST) -rebuild : clean $(PROG) \ No newline at end of file +rebuild : clean $(PROG) $(TEST) \ No newline at end of file diff --git a/tests/test.c b/tests/test.c index 4c95fd1feb4f463c8d199a1ea80daafaa20fea89..25596150c742ad829b51651ea04b0244c1308a35 100644 --- a/tests/test.c +++ b/tests/test.c @@ -217,24 +217,26 @@ static MunitResult test_tree_is_BST() 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,8); + nd = bst_insert(nd,3); + nd = bst_insert(nd,10); 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; // Change by stdout for print in console + + FILE* f = tmpfile(); // Change by stdout for print in console bst_print(nd,f); 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); fread(buffer,22,sizeof(char),f); - + // printf("\nBuffer : '%s'\n",buffer); + // printf("\nExpected : '%s'\n",expected); munit_assert_string_equal(buffer,expected); bst_destroy(nd); return MUNIT_OK; @@ -341,14 +343,14 @@ 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, diff --git a/tree.c b/tree.c index bb4535b93d7df1903905ade3ca3ccc443eed2ca0..1c527647703cf67e287d3efbf0838b6709840ba3 100644 --- a/tree.c +++ b/tree.c @@ -66,13 +66,47 @@ node_t *bst_delete(node_t *tree, int val) { 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) { - + bst_delete(tree->left,val); } } @@ -228,4 +262,4 @@ uint32_t bst_lenght(node_t *tree) } } return length; -} \ No newline at end of file +}