Skip to content
Snippets Groups Projects
Commit 7b2a7645 authored by thib's avatar thib
Browse files

create print insert min max search

parent 2bac22c5
No related branches found
No related tags found
No related merge requests found
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
#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
munit @ fbbdf146
Subproject commit fbbdf1467eb0d04a6ee465def2e529e4c87f2118
#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
#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
#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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment