diff --git a/The-Nook-GamesTM/Fundamentals/basic_tree.c b/The-Nook-GamesTM/Fundamentals/basic_tree.c index 3d0c583..8e53f1d 100644 --- a/The-Nook-GamesTM/Fundamentals/basic_tree.c +++ b/The-Nook-GamesTM/Fundamentals/basic_tree.c @@ -1,6 +1,8 @@ #include "basic_tree.h" #include #include +#include +#include // FCTs perso struct queue *enqueue(struct queue *queue, struct node *node){ @@ -52,6 +54,15 @@ void queue_destroy(struct queue *queue){ } } +void delete_tree(struct node *root){ + + if(!root) return; + + delete_tree(root->left); + delete_tree(root->right); + free(root); +} + int find(struct node *root, int value){ // -1 : erreur, 0 : pas de leaf/node, x>0 : nombre trouvé @@ -79,9 +90,6 @@ int find(struct node *root, int value){ free (queue); return found; - - - } @@ -166,17 +174,127 @@ void delete_leaf(struct node *root, int value){ } -struct node *delete_node(struct node *root, int value); +struct node *delete_node(struct node *root, int value){ -void pretty_print(struct node *root); + if(!root) return NULL; + // 3 cas : 1 si le node a un child, 2 si le node a deux nodes, 3 si il en a pas + + // Si il a pas de child + if(!root->left && !root->right){ + if(root->value == value){ + free(root); + return NULL; + } + return root; + } + + struct node *targ = NULL; + struct node *prec_child = NULL; + struct node *prec_dad = NULL; + + struct queue *queue = enqueue(NULL, root); + + while(queue != NULL){ + + struct node *tmp = dequeue(&queue); + + + if (tmp->value == value && !targ) targ = tmp; + + if(tmp->left){ + prec_dad = tmp; + queue = enqueue(queue, tmp->left); + prec_child = tmp->left; + } + + if(tmp->right){ + prec_dad = tmp; + queue = enqueue(queue, tmp->right); + prec_child = tmp->right; + } + } + if(targ){ + + targ->value = prec_child->value; + + if(prec_dad->right == prec_child) prec_dad->right = NULL; + else prec_dad->left = NULL; + free(prec_child); + } + + return root; +} + +void pretty_print(struct node *root){ + + + if(!root) return; + struct queue *queue = enqueue(NULL, root); + int index_l = 1; + int index_n = 0; + + while(queue){ + + struct node *tmp = dequeue(&queue); + index_l--; + + printf("%d", tmp->value); + + if(tmp->left){ + queue = enqueue(queue, tmp->left); + index_n++; + } + + if(tmp->right){ + queue = enqueue(queue, tmp->right); + index_n++; + } + if(index_l > 0) printf("\t"); + else{ + printf("\n"); + index_l = index_n; + index_n = 0; + } + } +} + +int main(int argc, char *argv[]) { + if (argc == 1) + return 1; + + struct node *root = NULL; + for (int i = 1; i < argc; i++) { + root = insert(root, atoi(argv[i])); + } + + pretty_print(root); + + delete_tree(root); + return 0; +} + + + +/* +int main() { + struct node *root = insert(NULL, 0); + root = insert(root, 1); + root = insert(root, 2); + root = delete_node(root, 0); + printf("%d, %d\n", root->value, root->left->value); // 2, 1 +} +*/ + +/* int main() { struct node *root = insert(NULL, 0); root = insert(root, 1); root = insert(root, 2); delete_leaf(root, 2); printf("%d, %d\n", root->value, root->left->value); // 0, 1 -} + +}i*/ /* diff --git a/The-Nook-GamesTM/Fundamentals/expansion.c b/The-Nook-GamesTM/Fundamentals/expansion.c index 5931a94..7be3a68 100644 --- a/The-Nook-GamesTM/Fundamentals/expansion.c +++ b/The-Nook-GamesTM/Fundamentals/expansion.c @@ -1,3 +1,5 @@ #include "expansion.h" -char *weird_copy(const char *input); \ No newline at end of file +char *weird_copy(const char *input){ + return NULL; +} diff --git a/The-Nook-GamesTM/Fundamentals/parsing.c b/The-Nook-GamesTM/Fundamentals/parsing.c index f4ef08b..2072d2d 100644 --- a/The-Nook-GamesTM/Fundamentals/parsing.c +++ b/The-Nook-GamesTM/Fundamentals/parsing.c @@ -4,10 +4,18 @@ #include #include -struct node *create_node(const char *line); +struct node *create_node(const char *line){ -struct node *parse_file(const char *filename); + return NULL; +} -void execute_tree(struct node *node, int value); +struct node *parse_file(const char *filename){ -int main(int argc, char *argv[]); \ No newline at end of file + return NULL; +} + +void execute_tree(struct node *node, int value){ + return NULL; +} + +int main(int argc, char *argv[]); diff --git a/The-Nook-GamesTM/Fundamentals/vector_tree.c b/The-Nook-GamesTM/Fundamentals/vector_tree.c index 5e5ac7e..8ddd919 100644 --- a/The-Nook-GamesTM/Fundamentals/vector_tree.c +++ b/The-Nook-GamesTM/Fundamentals/vector_tree.c @@ -1,7 +1,12 @@ #include "vector_tree.h" -struct vector *insert(struct vector *tree, int value); +struct vector *insert(struct vector *tree, int value){ -void delete_node(struct vector *tree, int value); + return NULL; +} -void pretty_print(struct vector *tree); \ No newline at end of file +void delete_node(struct vector *tree, int value){ + return; +} + +void pretty_print(struct vector *tree); diff --git a/The-Nook-GamesTM/Proficiencies/evaluate_rpn.c b/The-Nook-GamesTM/Proficiencies/evaluate_rpn.c index 97cec10..5bbc632 100644 --- a/The-Nook-GamesTM/Proficiencies/evaluate_rpn.c +++ b/The-Nook-GamesTM/Proficiencies/evaluate_rpn.c @@ -1,3 +1,3 @@ #include "evaluate_rpn.h" -int evaluate_rpn(struct queue *queue); \ No newline at end of file +int evaluate_rpn(struct queue *queue){return 0 ;} diff --git a/The-Nook-GamesTM/Proficiencies/main.c b/The-Nook-GamesTM/Proficiencies/main.c index d746ac7..56b2e88 100644 --- a/The-Nook-GamesTM/Proficiencies/main.c +++ b/The-Nook-GamesTM/Proficiencies/main.c @@ -1 +1 @@ -int main(int argc, char** argv); \ No newline at end of file +int main(int argc, char** argv){return 0;} diff --git a/The-Nook-GamesTM/Proficiencies/parse_exp.c b/The-Nook-GamesTM/Proficiencies/parse_exp.c index 7e005ee..5161cf9 100644 --- a/The-Nook-GamesTM/Proficiencies/parse_exp.c +++ b/The-Nook-GamesTM/Proficiencies/parse_exp.c @@ -1,3 +1,3 @@ #include "parse_exp.h" -struct queue *parse_expression(const char *str); \ No newline at end of file +struct queue *parse_expression(const char *str){return NULL;}