Skip to content
Snippets Groups Projects
Commit 0fecde1b authored by florent.didion's avatar florent.didion
Browse files

ended i guess

parent ce25c712
No related branches found
No related tags found
No related merge requests found
...@@ -9,7 +9,9 @@ binary_tree: $(OBJECTS) ...@@ -9,7 +9,9 @@ binary_tree: $(OBJECTS)
binary_tree_lib.o: binary_tree_lib.h binary_tree_lib.o: binary_tree_lib.h
test: test.o
test.o: test.c
.PHONY :clean .PHONY :clean
......
File added
#include "binary_tree_lib.h" #include "binary_tree_lib.h"
#include <stdlib.h>
// Fonctions de création, destruction et affichage
// création d'un arbre vide (retourne NULL)
node_t *bst_create(){
node_t* bst =NULL;
return bst;
}
node_t *bst_create_node(int val){
node_t *new_node=malloc(sizeof(node_t));
new_node->val=val;
new_node->left=NULL;
new_node->right=NULL;
return new_node;
}
// détruit l'arbre et vide la mémoire
void bst_destroy(node_t *tree){
if(tree->left==NULL&&tree->right==NULL){
free(tree);
}
else if(tree->left!=NULL){
bst_destroy(tree->left);
}
else if(tree->right!=NULL){
bst_destroy(tree->right);
}
}
// affiche l'arbre (voir plus bas)
void bst_print(node_t *tree,int prof){
if (tree == NULL) {
return;
}
bst_print(tree->left, prof + 1);
for (int i = 0; i < prof; i++) {
printf("-------");
}
printf(" %d \n\n", tree->val);
bst_print(tree->right, prof + 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(tree==NULL){
tree=bst_create_node(val);
}
else if(val>tree->val){
if(tree->right==NULL){
tree->right=bst_create_node(val);
}
else{
bst_insert(tree->right, val);
}
}
else if(val<=tree->val){
if(tree->left==NULL){
tree->left=bst_create_node(val);
}
else{
bst_insert(tree->left, val);
}
}
return tree;
}
// 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){
if(val<tree->val){
bst_delete(tree->left, val);
}
else if(val>tree->val){
bst_delete(tree->right, val);
}
if(val==tree->val){
bst_destroy(tree);
}
return tree;
}
// la valeur val est-elle présente dans l'arbre?
bool bst_is_present(node_t *tree, int val){
if(val<tree->val){
bst_is_present(tree->left, val);
}
else if(val>tree->val){
bst_is_present(tree->right, val);
}
if(val==tree->val){
return true;
}
return false;
}
// retourne le noeud où la valeur val se trouve (NULL si absent)
node_t *bst_search(node_t *tree, int val){
if(val<tree->val){
bst_search(tree->left,val);
}
if(val<tree->val){
bst_search(tree->right,val);
}
if(val==tree->val){
return tree;
}
return NULL;
}
// l'arbre est-il un arbre binaire de recherche?
bool bst_is_bst(node_t *tree){
if(!(tree->val>tree->left->val)&&tree->left!=NULL){
return false;
}
else if(!(tree->val<tree->right->val)&&tree->right!=NULL){
return false;
}
else if(tree->left!=NULL){
bst_is_bst(tree->left);
}
else if(tree->right!=NULL){
bst_is_bst(tree->right);
}
return true;
}
// 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){
if(tree->left!=NULL){
bst_find_min_node(tree->left);
}
return tree;
}
// retourne la valeur la plus petite stockée dans l'arbre (ou MIN_INT)
int bst_find_min(node_t *tree){
return bst_find_min_node(tree)->val;
}
// 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){
if(tree->right!=NULL){
bst_find_min_node(tree->right);
}
return tree;
}
// retourne la valeur la plus grande stockée dans l'arbre (ou MAX_INT)
int bst_find_max(node_t *tree){
return bst_find_max_node(tree)->val;
}
\ No newline at end of file
...@@ -3,10 +3,24 @@ ...@@ -3,10 +3,24 @@
#include <stdbool.h> #include <stdbool.h>
typedef struct _node_t { typedef struct _node_t {
int val; int val;
struct _node_t *left; struct _node_t *left;
struct _node_t *right; struct _node_t *right;
} node_t; } node_t;
// Fonctions de création, destruction et affichage
node_t *bst_create();
node_t *bst_create_node(int val);
void bst_destroy(node_t *tree);
void bst_print(node_t *tree, int prof);
node_t *bst_insert(node_t *tree, int val);
node_t *bst_delete(node_t *tree, int val);
bool bst_is_present(node_t *tree, int val);
node_t *bst_search(node_t *tree, int val);
bool bst_is_bst(node_t *tree);
node_t *bst_find_min_node(node_t *tree);
int bst_find_min(node_t *tree);
node_t *bst_find_max_node(node_t *tree);
int bst_find_max(node_t *tree);
int main(){ #include "binary_tree_lib.h"
#include <stdlib.h>
#include <time.h>
int main(){
srand(time(NULL));
int test[10];
for(int i =0;i<10;i++){
test[i]=rand()/(RAND_MAX/100);
printf("%d\n",test[i]);
}
node_t* tree=bst_create();
for (int i =0;i<10;i++){
tree=bst_insert(tree, test[i]);
}
bst_print(tree, 0);
printf("printed \n\n");
bst_destroy(tree);
......
...@@ -5,8 +5,3 @@ ...@@ -5,8 +5,3 @@
int main(){
return 0;
}
\ 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