Write a C program to insert and delete elements in the Binary search tree and display the tree in inorder traversal.


Q.) Write a C program to insert and delete elements in the Binary search tree and display the tree in inorder traversal.

Subject: data structures
#include 
#include 

// Node of the binary search tree
struct Node {
    int data;
    struct Node *left;
    struct Node *right;
};

// Function to insert a node into the binary search tree
struct Node *insert(struct Node *root, int data) {
    if (root == NULL) {
        struct Node *new_node = (struct Node *)malloc(sizeof(struct Node));
        new_node->data = data;
        new_node->left = NULL;
        new_node->right = NULL;
        return new_node;
    } else if (data < root->data) {
        root->left = insert(root->left, data);
    } else {
        root->right = insert(root->right, data);
    }
    return root;
}

// Function to delete a node from the binary search tree
struct Node *delete(struct Node *root, int data) {
    if (root == NULL) {
        return NULL;
    } else if (data < root->data) {
        root->left = delete(root->left, data);
    } else if (data > root->data) {
        root->right = delete(root->right, data);
    } else {
        if (root->left == NULL) {
            struct Node *temp = root->right;
            free(root);
            return temp;
        } else if (root->right == NULL) {
            struct Node *temp = root->left;
            free(root);
            return temp;
        } else {
            struct Node *temp = root->right;
            while (temp->left != NULL) {
                temp = temp->left;
            }
            root->data = temp->data;
            root->right = delete(root->right, temp->data);
        }
    }
    return root;
}

// Function to display the binary search tree in inorder traversal
void inorder(struct Node *root) {
    if (root != NULL) {
        inorder(root->left);
        printf("%d ", root->data);
        inorder(root->right);
    }
}

int main() {
    struct Node *root = NULL;
    root = insert(root, 50);
    root = insert(root, 30);
    root = insert(root, 20);
    root = insert(root, 40);
    root = insert(root, 70);
    root = insert(root, 60);
    root = insert(root, 80);

    printf("Inorder traversal of the binary search tree before deletion: ");
    inorder(root);
    printf("\n");

    root = delete(root, 20);
    root = delete(root, 70);

    printf("Inorder traversal of the binary search tree after deletion: ");
    inorder(root);
    printf("\n");

    return 0;
}

In this program, we first create a function called insert() to insert a node into the binary search tree. The insert() function takes two arguments: the root of the binary search tree and the data to be inserted. The function first checks if the root is NULL. If it is, it creates a new node with the given data and sets its left and right children to NULL. Otherwise, it checks if the data is less than the root's data. If it is, it recursively calls the insert() function on the left child of the root. Otherwise, it recursively calls the insert() function on the right child of the root.

Next, we create a function called delete() to delete a node from the binary search tree. The delete() function takes two arguments: the root of the binary search tree and the data to be deleted. The function first checks if the root is NULL. If it is, it returns NULL. Otherwise, it checks if the data is less than the root's data. If it is, it recursively calls the delete() function on the left child of the root. Otherwise, it checks if the data is greater than the root's data. If it is, it recursively calls the delete() function on the right child of the root.

If the data is equal to the root's data, the function checks if the root has any children. If it does not, it frees the root and returns NULL. If the root has one child, it sets the root's child to the root and frees the root. If the root has two children, it finds the smallest node in the right subtree of the root and replaces the root's data with the data from the smallest node. Then, it recursively calls the delete() function on the right subtree of the root to delete the smallest node.

Finally, we create a function called inorder() to display the binary search tree in inorder traversal. The inorder() function takes one argument: the root of the binary search tree. The function recursively calls itself on the left child of the root, then prints the root's data, and then recursively calls itself on the right child of the root.

In the main() function, we create a root node for the binary search tree and insert some data into the tree using the insert() function. Then, we display the binary search tree in inorder traversal using the inorder() function. Next, we delete some data from the tree using the delete() function and display the tree again.