diff --git a/tree/Makefile b/tree/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..b560aa8f4c1326ab414b69f93a6f3474f1294729 --- /dev/null +++ b/tree/Makefile @@ -0,0 +1,21 @@ +cc=gcc +CFLAGS=-Wextra -Wall -g -fsanitize=address -fsanitize=leak +LFLAGS=-fsanitize=address -fsanitize=leak -lm + +test :test.x + ./test.x + +test.x: tree.o test.o munit.o + $(cc) -o $@ $^ $(CFLAGS) $(LFLAGS) + +munit.o: munit/munit.c munit/munit.h + $(CC) $^ -c + +main: main.x + ./main.x + +main.x: tree.o main.o + $(cc) -o $@ $^ $(CFLAGS) $(LFLAGS) + +clean: + rm -f *.o *.x diff --git a/tree/main.c b/tree/main.c new file mode 100644 index 0000000000000000000000000000000000000000..c45187229afd024b196bc5a0b37a5d94c241bfcb --- /dev/null +++ b/tree/main.c @@ -0,0 +1,20 @@ +#include "tree.h" +int main(){ + + node_t *tree=bst_create(); + tree=bst_insert(tree,10); + tree=bst_insert(tree,5); + tree=bst_insert(tree,15); + tree=bst_insert(tree,2); + tree=bst_insert(tree,6); + bst_print(tree,0); + if(bst_is_present(tree, 3)){ + printf("found\n"); + } + if(NULL!=bst_search(tree, 2)){ + printf("found it\n"); + } + printf("%d\n",bst_find_min(tree)); + printf("%d\n",bst_find_max(tree)); + bst_destroy(tree); +} \ No newline at end of file diff --git a/tree/munit b/tree/munit new file mode 160000 index 0000000000000000000000000000000000000000..fbbdf1467eb0d04a6ee465def2e529e4c87f2118 --- /dev/null +++ b/tree/munit @@ -0,0 +1 @@ +Subproject commit fbbdf1467eb0d04a6ee465def2e529e4c87f2118 diff --git a/tree/test.c b/tree/test.c new file mode 100644 index 0000000000000000000000000000000000000000..c811ac50ba3180b6813c318b83c10d2b4a15a716 --- /dev/null +++ b/tree/test.c @@ -0,0 +1,55 @@ +#include "munit/munit.h" +#include "tree.h" + +MunitResult test_init() //f de test +{ + node_t *tree=bst_create(); + munit_assert_ptr_not_equal(tree,NULL); + return MUNIT_OK; +} + +MunitResult test_insert() //f de test +{ + node_t (*tree)=bst_create(); + bst_insert(tree,10); + bst_insert(tree,5); + bst_insert(tree,15); + bst_insert(tree,2); + bst_insert(tree,6); + bst_print(tree); + + return MUNIT_OK; + +} + +MunitTest tests[] = {// liste de tests + { + "/init",/* name */ + test_init,/* test */ + NULL,/* setup */ + NULL,/* tear_down */ + MUNIT_TEST_OPTION_NONE,/* options */ + NULL/* parameters */ + },{ + "/insert",/* name */ + test_insert,/* test */ + NULL,/* setup */ + NULL,/* tear_down */ + MUNIT_TEST_OPTION_NONE,/* options */ + NULL/* parameters */ + }, + /* La fin de la liste de tests */ + { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, +}; + +const MunitSuite test_suite={ + (char*)"les supers tests youpi", + tests, + NULL, + 1, + MUNIT_SUITE_OPTION_NONE, +}; + +int main(int argc, char** argv){ + return munit_suite_main(&test_suite, NULL, argc,argv); +} \ No newline at end of file diff --git a/tree/tree.c b/tree/tree.c new file mode 100644 index 0000000000000000000000000000000000000000..74357897527ba45a269a577aa136d430ff15f308 --- /dev/null +++ b/tree/tree.c @@ -0,0 +1,128 @@ +#include "tree.h" + +// typedef struct _node_t { +// int val; +// struct _node_t *left; +// struct _node_t *right; +// } node_t; + +// peite gauche et plus grand droite +bool bst_is_empty(node_t *tree) { return tree == NULL; } + +static node_t *create_node(int val) { + node_t *node = malloc(sizeof(node_t)); + if (node == NULL) { // memory error + return NULL; + } + node->left = NULL; + node->right = NULL; + node->val = val; + return node; +} + +node_t *bst_create() { return NULL; } + +void bst_destroy(node_t *tree) { + if (!bst_is_empty(tree)) { + if (tree->left != NULL) { + bst_destroy(tree->left); + } + if (tree->right != NULL) { + bst_destroy(tree->right); + } + free(tree); + } +} + +void bst_print(node_t *tree, int depth) { // affiche l'arbre ordre croissant + + if (tree == NULL) { + return; + } + bst_print(tree->left, depth + 1); + for (int i = 0; i < depth; i++) { + printf("-------"); + } + printf(" %d \n\n", tree->val); + bst_print(tree->right, depth + 1); +} + +// insertion de val dans l'arbre et retourne l'arbre (ou NULL si problème) +node_t *bst_insert(node_t *tree, int val) { + + if (bst_is_empty(tree)) { // root + tree = create_node(val); + } else { + + if (val > tree->val) { // right + if (tree->right == NULL) { + tree->right = create_node(val); + } else { + tree->right = bst_insert(tree->right, val); + } + + } else { // left + if (tree->left == NULL) { + tree->left = create_node(val); + } else { + tree->left = bst_insert(tree->left, val); + } + } + } + return tree; +} + + +// la valeur val est-elle présente dans l'arbre? +bool bst_is_present(node_t *tree, int val) { + return bst_search(tree, val) != NULL; +} +// retourne le noeud où la valeur val se trouve (NULL si absent) +node_t *bst_search(node_t *tree, int val) { + + if (!bst_is_empty(tree)) { + + if (val == tree->val) { // found it + return tree; + } else if (val > tree->val) { // right + return bst_search(tree->right, val); + } else { // left + return bst_search(tree->left, val); + } + } + + return NULL; +} + +// l'arbre est-il un arbre binaire de recherche? +bool bst_is_bst(node_t *tree); + +node_t *bst_find_min_node(node_t *tree) { + if (tree->left != NULL) { + return bst_find_min_node(tree->left); + } else { + return tree; + } +} + +int bst_find_min(node_t *tree) { return bst_find_min_node(tree)->val; } + +node_t *bst_find_max_node(node_t *tree) { + if (tree->right != NULL) { + return bst_find_min_node(tree->right); + } else { + return tree; + } +} + +// int bst_find_max(node_t *tree) { return bst_find_max_node(tree)->val; } + +// // efface le premier élément contenant la valeur val dans l'arbre +// // et retourne l'arbre (ne fait rien si val est absente) +// node_t *bst_delete(node_t *tree, int val){ +// node_t *tmp=bst_search(tree, val); +// if(tmp->left==NULL&&tmp->right==NULL){ +// tmp=NULL; +// free(tmp); +// }else if() +// } \ No newline at end of file diff --git a/tree/tree.h b/tree/tree.h new file mode 100644 index 0000000000000000000000000000000000000000..3fa62a64f4e94b59bdf202150252a45f7cd134b5 --- /dev/null +++ b/tree/tree.h @@ -0,0 +1,41 @@ +#ifndef _STACK_H_ +#define _STACK_H_ + +#include <stdio.h> +#include <stdbool.h> +#include <malloc.h> + +typedef struct _node_t { +int val; +struct _node_t *left; +struct _node_t *right; +} node_t; + +//BINARY SEARCH TREE (BST) + +// Fonctions de création, destruction et affichage +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,int depth); // 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); +// efface le premier élément contenant la valeur val dans l'arbre +// et retourne l'arbre (ne fait rien si val est absente) +node_t *bst_delete(node_t *tree, int val); +// la valeur val est-elle présente dans l'arbre? +bool bst_is_present(node_t *tree, int val); +// retourne le noeud où la valeur val se trouve (NULL si absent) +node_t *bst_search(node_t *tree, int val); +// l'arbre est-il un arbre binaire de recherche? +bool bst_is_bst(node_t *tree); +// retourne le noeud avec la valeur minimale de l'arbre (NULL s'il y a pas) +node_t *bst_find_min_node(node_t *tree); +// retourne la valeur la plus petite stockée dans l'arbre (ou MIN_INT) +int bst_find_min(node_t *tree); +// retourne le noeud avec la valeur maximale de l'arbre (NULL s'il y a pas) +node_t *bst_find_max_node(node_t *tree); +// retourne la valeur la plus grande stockée dans l'arbre (ou MAX_INT) +int bst_find_max(node_t *tree); + +#endif \ No newline at end of file