#include "basic_tree.h"
#include <stdlib.h>
#include <stdio.h>
// FCTs perso

struct queue *enqueue(struct queue *queue, struct node *node){

    //if (!queue || !node) return NULL;

    //struct queue *tmp = queue;
    // ajout en fin
    //while(tmp){
     //   tmp = tmp->next;
    //}
    
    struct queue *new_queue = malloc(sizeof(struct queue));
    if(!new_queue)return NULL;

    new_queue->node = node;
    new_queue->next = NULL;
        
    if(!queue) return new_queue;

    struct queue *tmp = queue;

    while(tmp->next) tmp = tmp->next;

    tmp->next = new_queue;

    return queue;
}

struct node *dequeue(struct queue **queue){

    if(!queue || !*queue) return NULL;

    // On suppr la head
    struct queue *tmp = *queue;
    struct node *node = tmp->node;
    *queue = tmp->next;
    free(tmp);

    return node;
}

void queue_destroy(struct queue *queue){

    while(queue){
    struct queue *tmp = queue;
    queue = queue->next;
    free(tmp);
    }
}

int find(struct node *root, int value){

    // -1 : erreur, 0 : pas de leaf/node, x>0 : nombre trouvé
    if(!root)return -1;

    int found = 0;
    struct queue *queue = enqueue(NULL, root);
    
    while(queue != NULL){

        struct node *tmp = dequeue(&queue);

        //si tmp est une leaf
        if(!tmp->left && !tmp->right) {
           if(tmp->value == value) found ++;
        } 
        else {

            if(tmp->value == value) found ++;
            queue = enqueue(queue, tmp->left);
            queue = enqueue(queue, tmp->right);
        }
    }

    free (queue);
    return found;




}


//#################################################################
// FCTs sujet

struct node *insert(struct node *root, int value){

    // Parcours largeur

    struct node *node = malloc(sizeof(struct node));
    if(!node) return NULL;
    
    node->value = value;
    node->left = NULL;
    node->right = NULL;

    if (!root) return node;
    else{

        struct queue *queue = enqueue(NULL ,root);
        
        while(queue != NULL){
            
            struct node *tmp = dequeue(&queue);

            if(tmp->left == NULL) {
                tmp->left = node;
                queue_destroy(queue);
                break;
            }
            else if (tmp->right == NULL) {
                tmp->right = node;
                queue_destroy(queue);
                break;
            }
            else{

                queue = enqueue(queue, tmp->left);
                queue = enqueue(queue, tmp->right);
            }

        }
        
    }
    return root;
}

void delete_leaf(struct node *root, int value){

    if(!root || !value) return;


    if(root->left){

        // On regarde si c'est un noeud
        if(root->left || root->right){

            // On regarde si c'est une leaf
            if(!root->left->left && !root->left->right && root->left->value == value){
                free(root->left);
                root->left = NULL;
            }
            else delete_leaf(root->left, value);
        }
    }

    if(root->right){
        
    // On regarde si c'est un noeud
        
        if(root->left || root->right){

            // On regarde si c'est une leaf 
            if(!root->right->left && !root->right->right && root->right->value == value){
                free(root->right);
                root->right = NULL;
            }
            else delete_leaf(root->right, value);
        }    
    }
     
}
    
struct node *delete_node(struct node *root, int value);

void pretty_print(struct node *root);

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
}


/*
int main() {
  struct node *root = insert(NULL, 0);
  root = insert(root, 1);
  root = insert(root, 2);
  printf("%d, %d, %d\n", root->value, root->left->value, root->right->value); // 0, 1, 2
    
}
*/
