Inaccuracy in output of deletion function in binary search tree

I am making a binary search tree code, in that 'am facing an issue in the deletion function. After executing the function, the code is showing inorder display incorrectly. Basically, it is incorrect, and I'am not able to understand where the problem is.

In the delete function, I have used a search function that will find the node which the user has to delete. And I have used inorder successor method for deletion so the minimum function will find the successor. All other functions like insertion, minimum, and inorder are working fine.

    #include <stdio.h>
    #include <stdlib.h>
    struct node
    {
        int data;
        struct node *left;
        struct node *right;
    } node;
    struct node *insert(struct node *tnode, int data)
    {
    if (tnode == NULL)
    {
        struct node *ptr;
        ptr = (struct node *)malloc(sizeof(struct node));
        ptr->data = data;
        ptr->left = NULL;
        ptr->right = NULL;
        return ptr;
    }
    if (data > (tnode->data))
    {
        tnode->right = insert(tnode->right, data);
        return tnode;
    }
    if (data < (tnode->data))
    {
        tnode->left = insert(tnode->left, data);
        return tnode;
    }
    }
    void in_order(struct node *tnode)
    {
    if (tnode == NULL)
        return;
    else
    {
        in_order(tnode->left);
        printf("%d\t", tnode->data);
        in_order(tnode->right);
    }
    }
    struct node *search(struct node *tnode, int data)
    {
    if (tnode == NULL)
    {
        return NULL;
    }
    else
    {
        if (data == tnode->data)
        {
            return tnode;
        }
        else
        {
            if (data > tnode->data)
            {
                search(tnode->right, data);
            }
            else
            {
                search(tnode->left, data);
            }
        }
    }
    }
    struct node *minimum(struct node *tnode)
    {
    if (tnode == NULL)
    {
        printf("No node present\n");
        return NULL;
    }
    else
    {
        if (tnode->left == NULL)
            return tnode;
        else
            minimum(tnode->left);
    }
    }
    struct node *deletenode(struct node *tnode, int data)
    {
    struct node *s;
    s = search(tnode, data);
    if (s == NULL)
    {
        printf("Value not found\n");
        return NULL;
    }
    else
    {
        if (s->left == NULL && s->right == NULL) // No child case
        {
            free(s);
            return NULL;
        }
        else
        {
            if (s->left == NULL && s->right != NULL) // single child case(right child)
            {
                struct node *temp = s->right;
                free(s);
                return temp;
            }
            else if (s->right == NULL && s->left != NULL) // single child case(left child)
            {
                struct node *temp = s->left;
                free(s);
                return temp;
            }
            else
            {
                struct node *temp;
                temp = minimum(s->right);
                s->data = temp->data;
                s->right = deletenode(s->right, temp->data);
            }
        }
    }

    return s;
    }
    int main()
    {
    struct node *root = NULL, *del;
    int n, i, data, key;
    printf("Enter how many nodes you have to enter in a tree\n");
    scanf("%d", &n);
    for (i = 0; i < n; i++)
    {
        printf("Enter data\n");
        scanf("%d", &data);
        root = insert(root, data);
    }
    printf("\nIN-Order display\n");
    in_order(root);
    int d;
    printf("\nEnter data to be delete\n");
    scanf("%d", &d);
    del=deletenode(root, d);
    printf("\nIN-Order display\n");
    in_order(root);
    return 0;
    }


Comments