<commit_before>#include <stdio.h>
#include <stdlib.h>
#include "BST_actions.h"


struct leaf *create_leaf(void) {

    struct leaf *fresh_leaf = malloc ( sizeof *fresh_leaf );
    fresh_leaf->parent = NULL;
    fresh_leaf->left = NULL;
    fresh_leaf->right = NULL;
    return fresh_leaf;
}

struct leaf *min_tree(struct leaf **tree) { 

    if ((*tree)->left) {
        min_tree(&(*tree)->left);
    }

    return *tree; 
}

void transplant(struct leaf **rm_branch, struct leaf **branch) {

    if ((!(*rm_branch)->parent)) {
        (*branch)->parent = NULL;
        (*branch)->left = (*rm_branch)->left;
        if ((*branch)->left) {
            (*branch)->left->parent = *branch;
        }
        *rm_branch = *branch; 
    }                         

    else if (*rm_branch == (*rm_branch)->parent->left) {
        (*rm_branch)->parent->left = *branch;
    }

    else {
        (*rm_branch)->parent->right = *branch;
    }

    if (*branch) { 
        (*branch)->parent = (*rm_branch)->parent;  
    }                                             
}

void delete(struct leaf **tree, long value) {

    if (!(*tree)) {
        printf ("Value %ld not in tree!\n", value);
    }

    else if ((*tree)->value == value) {

        if ((!(*tree)->left) && (!(*tree)->right) && (!(*tree)->parent)) {
            free (*tree);
            *tree = NULL;
        }

        else if ((!(*tree)->left)) {          
            transplant(tree, &(*tree)->right); 
        }

        else if ((!(*tree)->right)) {
            transplant(tree, &(*tree)->left); 
        }

        else {

            struct leaf *_branch = malloc ( sizeof *_branch ); 
            _branch = min_tree(&(*tree)->right);

            if (_branch->parent != *tree) { 
                transplant(&_branch, &(_branch)->right);   
                _branch->right = (*tree)->right;          
                _branch->right->parent = _branch;        
            }

            transplant(&(*tree), &_branch); 
            _branch->left = (*tree)->left;     
            _branch->left->parent = _branch;  
        }
    }

    else if ((*tree)->value < value) {
        delete(&(*tree)->right, value);
    }

    else {
        delete(&(*tree)->left, value);
    }
}

void insert(struct leaf **tree, long value) {

    if ((*tree)->value < value) {

        if (!((*tree)->right)) {

            struct leaf *new_leaf = create_leaf();
            new_leaf->parent = *tree;
            new_leaf->value = value;

            (*tree)->right = new_leaf;
        }

        else {
            insert(&(*tree)->right, value);
        }
    }

    else if ((*tree)->value >= value) {

        if (!((*tree)->left)) {

            struct leaf *new_leaf = create_leaf();
            new_leaf->parent = *tree;
            new_leaf->value = value;

            (*tree)->left = new_leaf;
        }

        else {
            insert(&(*tree)->left, value);
        }
    }           
}

void search(struct leaf *tree, long value) {

    if (tree && tree->value != value) {

        if (tree->value < value) {
            search(tree->right, value);
        }

        else {
            search(tree->left, value);
        }
    }

    else if (tree && tree->value == value) {
        printf ("Value %ld is in tree!\n", tree->value);
    }

    else {
        printf ("Value %ld is not in tree!\n", value);
    }
}

void dump_tree(struct leaf *tree) {

    if (tree) {

        dump_tree(tree->left);
        printf ("Leaf: %ld\n", tree->value);
        dump_tree(tree->right);
    }
}
<commit_msg>fix null ptr ref<commit_after>#include <stdio.h>
#include <stdlib.h>
#include "BST_actions.h"


struct leaf *create_leaf(void) {

    struct leaf *fresh_leaf = malloc(sizeof *fresh_leaf);
    fresh_leaf->parent = NULL;
    fresh_leaf->left = NULL;
    fresh_leaf->right = NULL;
    return fresh_leaf;
}

struct leaf *min_tree(struct leaf **tree) { 

    if ((*tree)->left) {
        min_tree(&(*tree)->left);
    }

    return *tree; 
}

void transplant(struct leaf **rm_branch, struct leaf **branch) {

    if ((!(*rm_branch)->parent)) {
        (*branch)->parent = NULL;
        (*branch)->left = (*rm_branch)->left;
        if ((*branch)->left) {
            (*branch)->left->parent = *branch;
        }
        *rm_branch = *branch; 
    }                         

    else if (*rm_branch == (*rm_branch)->parent->left) {
        (*rm_branch)->parent->left = *branch;
    }

    else {
        (*rm_branch)->parent->right = *branch;
    }

    if (*branch) { 
        (*branch)->parent = (*rm_branch)->parent;  
    }                                             
}

void delete(struct leaf **tree, long value) {

    if (!(*tree)) {
        printf ("Value %ld not in tree!\n", value);
    }

    else if ((*tree)->value == value) {

        if ((!(*tree)->left) && (!(*tree)->right) && (!(*tree)->parent)) {
            free (*tree);
            *tree = NULL;
        }

        else if ((!(*tree)->left)) {          
            transplant(tree, &(*tree)->right); 
        }

        else if ((!(*tree)->right)) {
            transplant(tree, &(*tree)->left); 
        }

        else {

            struct leaf *_branch = malloc(sizeof *_branch); 
            _branch = min_tree(&(*tree)->right);

            if (_branch->parent != *tree) { 
                transplant(&_branch, &(_branch)->right);   
                _branch->right = (*tree)->right;          
                if (_branch->right) {
                    _branch->right->parent = _branch;
                }
            }

            transplant(&(*tree), &_branch); 
            _branch->left = (*tree)->left;     
            _branch->left->parent = _branch;  
        }
    }

    else if ((*tree)->value < value) {
        delete(&(*tree)->right, value);
    }

    else {
        delete(&(*tree)->left, value);
    }
}

void insert(struct leaf **tree, long value) {

    if ((*tree)->value < value) {

        if (!((*tree)->right)) {

            struct leaf *new_leaf = create_leaf();
            new_leaf->parent = *tree;
            new_leaf->value = value;

            (*tree)->right = new_leaf;
        }

        else {
            insert(&(*tree)->right, value);
        }
    }

    else if ((*tree)->value >= value) {

        if (!((*tree)->left)) {

            struct leaf *new_leaf = create_leaf();
            new_leaf->parent = *tree;
            new_leaf->value = value;

            (*tree)->left = new_leaf;
        }

        else {
            insert(&(*tree)->left, value);
        }
    }           
}

void search(struct leaf *tree, long value) {

    if (tree && tree->value != value) {

        if (tree->value < value) {
            search(tree->right, value);
        }

        else {
            search(tree->left, value);
        }
    }

    else if (tree && tree->value == value) {
        printf ("Value %ld is in tree!\n", tree->value);
    }

    else {
        printf ("Value %ld is not in tree!\n", value);
    }
}

void dump_tree(struct leaf *tree) {

    if (tree) {

        dump_tree(tree->left);
        printf ("Leaf: %ld\n", tree->value);
        dump_tree(tree->right);
    }
}
{'fraction_non_alphanumeric': 0.1756152125279642, 'fraction_numerical': 0.0, 'mean_word_length': 1.6029839883551673, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 136, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1966800', 'n_tokens_mistral': 2232, 'n_tokens_neox': 2115, 'n_words': 461}