Skip to content
Snippets Groups Projects
Commit 5f9d50a0 authored by thib's avatar thib
Browse files

done

parent 7b2a7645
Branches
No related tags found
No related merge requests found
...@@ -3,18 +3,31 @@ int main(){ ...@@ -3,18 +3,31 @@ int main(){
node_t *tree = bst_create(); node_t *tree = bst_create();
tree = bst_insert(tree, 10); tree = bst_insert(tree, 10);
// count_child(tree);
tree = bst_insert(tree, 5); tree = bst_insert(tree, 5);
// count_child(tree);
tree = bst_insert(tree, 15); tree = bst_insert(tree, 15);
tree = bst_insert(tree, 2); tree = bst_insert(tree, 2);
tree = bst_insert(tree, 6); tree = bst_insert(tree, 6);
bst_print(tree,0);
if(bst_is_present(tree, 3)){ if(bst_is_bst(tree)){
printf("found\n"); printf("i'm a bst !\n");
}
if(NULL!=bst_search(tree, 2)){
printf("found it\n");
} }
printf("%d\n",bst_find_min(tree)); bst_print(tree, 0);
printf("%d\n",bst_find_max(tree)); tree = bst_delete(tree, 5);
printf("\n\n");
bst_print(tree, 0);
// 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));
// count_child(tree);
bst_destroy(tree); bst_destroy(tree);
} }
\ No newline at end of file
...@@ -16,7 +16,7 @@ MunitResult test_insert() //f de test ...@@ -16,7 +16,7 @@ MunitResult test_insert() //f de test
bst_insert(tree,15); bst_insert(tree,15);
bst_insert(tree,2); bst_insert(tree,2);
bst_insert(tree,6); bst_insert(tree,6);
bst_print(tree); bst_print(tree,0);
return MUNIT_OK; return MUNIT_OK;
......
...@@ -72,11 +72,11 @@ node_t *bst_insert(node_t *tree, int val) { ...@@ -72,11 +72,11 @@ node_t *bst_insert(node_t *tree, int val) {
return tree; return tree;
} }
// la valeur val est-elle présente dans l'arbre? // la valeur val est-elle présente dans l'arbre?
bool bst_is_present(node_t *tree, int val) { bool bst_is_present(node_t *tree, int val) {
return bst_search(tree, val) != NULL; return bst_search(tree, val) != NULL;
} }
// retourne le noeud où la valeur val se trouve (NULL si absent) // retourne le noeud où la valeur val se trouve (NULL si absent)
node_t *bst_search(node_t *tree, int val) { node_t *bst_search(node_t *tree, int val) {
...@@ -95,7 +95,20 @@ node_t *bst_search(node_t *tree, int val) { ...@@ -95,7 +95,20 @@ node_t *bst_search(node_t *tree, int val) {
} }
// l'arbre est-il un arbre binaire de recherche? // l'arbre est-il un arbre binaire de recherche?
bool bst_is_bst(node_t *tree); bool bst_is_bst(node_t *tree) {
if(tree==NULL){
return true;
}
if (tree->left != NULL && tree->left->val > tree->val) {
return false;
}
if (tree->right != NULL && tree->right->val < tree->val) {
return false;
}
return bst_is_bst(tree->left) && bst_is_bst(tree->right);
}
node_t *bst_find_min_node(node_t *tree) { node_t *bst_find_min_node(node_t *tree) {
if (tree->left != NULL) { if (tree->left != NULL) {
...@@ -115,14 +128,41 @@ node_t *bst_find_max_node(node_t *tree) { ...@@ -115,14 +128,41 @@ node_t *bst_find_max_node(node_t *tree) {
} }
} }
// int bst_find_max(node_t *tree) { return bst_find_max_node(tree)->val; } 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 int count_child(node_t *node) {
// // et retourne l'arbre (ne fait rien si val est absente) int nb = (node->left != NULL) + (node->right != NULL);
// node_t *bst_delete(node_t *tree, int val){ // printf("%d child(s)\n", nb);
// node_t *tmp=bst_search(tree, val); return nb;
// if(tmp->left==NULL&&tmp->right==NULL){ }
// tmp=NULL;
// free(tmp); node_t *bst_delete(node_t *tree, int val) {
// }else if() if (!bst_is_empty(tree)) {
// }
\ No newline at end of file if (val > tree->val) {
tree->right = bst_delete(tree->right, val); // go right
} else if (val < tree->val) {
tree->left = bst_delete(tree->left, val); // go left
// found node
} else if (count_child(tree) == 2) {
node_t *next = bst_find_min_node(tree->right);
tree->val = next->val;
tree->right = bst_delete(next, next->val);
} else { // 1 or 0 childs
node_t *tmp = tree;
if (tmp->left != NULL) { // left
tree = tree->left;
} else if (tmp->right != NULL) { // right
tree = tree->right;
} else {
tree = NULL;
}
free(tmp);
}
}
return tree;
}
...@@ -38,4 +38,5 @@ node_t *bst_find_max_node(node_t *tree); ...@@ -38,4 +38,5 @@ node_t *bst_find_max_node(node_t *tree);
// retourne la valeur la plus grande stockée dans l'arbre (ou MAX_INT) // retourne la valeur la plus grande stockée dans l'arbre (ou MAX_INT)
int bst_find_max(node_t *tree); int bst_find_max(node_t *tree);
int count_child(node_t *node);
#endif #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