2078 2

CSC211 ยท 2078 ยท 10 marks

What is binary search tree? Write a program to implement insertion and deletion algorithms in binary search tree.

What is binary search tree? Write a program to implement insertion and deletion algorithms in binary search tree.[10]

Data Structures and AlgorithmsChapter 8: Trees and GraphsBinary Search Tree, Insertion, Deletion, Traversals, Search

Quick answer

A Binary Search Tree (BST) is a binary tree that is either empty or in which every node contains a key (value) and satisfies the following conditions: All keys in the left sub-tree of the root are smaller than the key in the root node; All keys in the right sub-tree of the root are greater than the key in the root...

Binary Search Tree: Definition, Insertion and Deletion


Definition of Binary Search Tree (BST)

A Binary Search Tree (BST) is a binary tree that is either empty or in which every node contains a key (value) and satisfies the following conditions:

  • All keys in the left sub-tree of the root are smaller than the key in the root node.
  • All keys in the right sub-tree of the root are greater than the key in the root node.
  • The left and right sub-trees of the root are again binary search trees (recursive definition).

Example:

        50
       /  \
     30    70
    /  \  /  \
   20  40 60  80

Here, every left child < parent < right child.


Node Structure

Each node in a BST contains:

  • A data/key field
  • A pointer to the left child
  • A pointer to the right child

C Program: BST Insertion and Deletion

#include <stdio.h>
#include <stdlib.h>

/* โ”€โ”€ Node structure โ”€โ”€ */
struct Node {
    int data;
    struct Node *left;
    struct Node *right;
};

/* โ”€โ”€ Helper: create a new node โ”€โ”€ */
struct Node* createNode(int value) {
    struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data  = value;
    newNode->left  = NULL;
    newNode->right = NULL;
    return newNode;
}

/* โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
   INSERTION
   โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
   Algorithm:
   1. If tree is empty, new node becomes root.
   2. If value < current node data, go LEFT.
   3. If value > current node data, go RIGHT.
   4. Repeat until NULL position found; insert there.
*/
struct Node* insert(struct Node *root, int value) {
    /* Base case: empty tree or reached insertion point */
    if (root == NULL) {
        return createNode(value);
    }

    if (value < root->data) {
        /* Insert in left sub-tree */
        root->left = insert(root->left, value);
    }
    else if (value > root->data) {
        /* Insert in right sub-tree */
        root->right = insert(root->right, value);
    }
    else {
        /* Duplicate keys are not inserted */
        printf("Value %d already exists in BST.\n", value);
    }

    return root;
}

/* โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
   FIND MINIMUM NODE
   (Used during deletion)
   Returns the node with minimum key
   in a given sub-tree (leftmost node).
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ• */
struct Node* findMin(struct Node *root) {
    while (root->left != NULL) {
        root = root->left;
    }
    return root;
}

/* โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
   DELETION
   โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
   Three cases:
   Case 1: Node has NO child (leaf)
           --> Simply delete the node.

   Case 2: Node has ONE child
           --> Replace node with its child.

   Case 3: Node has TWO children
           --> Find in-order successor
               (smallest in right sub-tree).
           --> Copy successor's value to node.
           --> Delete the in-order successor.
*/
struct Node* deleteNode(struct Node *root, int value) {
    /* Base case: value not found */
    if (root == NULL) {
        printf("Value %d not found in BST.\n", value);
        return NULL;
    }

    if (value < root->data) {
        /* Value lies in left sub-tree */
        root->left = deleteNode(root->left, value);
    }
    else if (value > root->data) {
        /* Value lies in right sub-tree */
        root->right = deleteNode(root->right, value);
    }
    else {
        /* Node to be deleted is found */

        /* Case 1: Leaf node (no children) */
        if (root->left == NULL && root->right == NULL) {
            free(root);
            return NULL;
        }

        /* Case 2a: Only right child exists */
        else if (root->left == NULL) {
            struct Node *temp = root->right;
            free(root);
            return temp;
        }

        /* Case 2b: Only left child exists */
        else if (root->right == NULL) {
            struct Node *temp = root->left;
            free(root);
            return temp;
        }

        /* Case 3: Node has two children */
        else {
            /* Find in-order successor (min of right sub-tree) */
            struct Node *successor = findMin(root->right);

            /* Copy successor's value to current node */
            root->data = successor->data;

            /* Delete the in-order successor from right sub-tree */
            root->right = deleteNode(root->right, successor->data);
        }
    }

    return root;
}

/* โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
   IN-ORDER TRAVERSAL (Left-Root-Right)
   Prints BST in sorted order
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ• */
void inorder(struct Node *root) {
    if (root != NULL) {
        inorder(root->left);
        printf("%d ", root->data);
        inorder(root->right);
    }
}

/* โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
   MAIN FUNCTION
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ• */
int main() {
    struct Node *root = NULL;

    /* Inserting nodes */
    root = insert(root, 50);
    root = insert(root, 30);
    root = insert(root, 70);
    root = insert(root, 20);
    root = insert(root, 40);
    root = insert(root, 60);
    root = insert(root, 80);

    printf("Inorder traversal of BST: ");
    inorder(root);
    printf("\n");

    /* Delete a leaf node */
    root = deleteNode(root, 20);
    printf("After deleting 20: ");
    inorder(root);
    printf("\n");

    /* Delete a node with one child */
    root = deleteNode(root, 30);
    printf("After deleting 30: ");
    inorder(root);
    printf("\n");

    /* Delete a node with two children */
    root = deleteNode(root, 50);
    printf("After deleting 50: ");
    inorder(root);
    printf("\n");

    return 0;
}

Sample Output:

Inorder traversal of BST: 20 30 40 50 60 70 80
After deleting 20: 30 40 50 60 70 80
After deleting 30: 40 50 60 70 80
After deleting 50: 40 60 70 80

Explanation

Deleting 20 removes a leaf node directly. Deleting 30 removes a node with only one child (40), so 40 takes its place. Deleting 50 (the root, which now has two children 40 and 70) is handled by Case 3: the in-order successor, 60 (the smallest value in the right sub-tree rooted at 70), replaces 50's value, and the original node holding 60 is then removed from the right sub-tree. The in-order traversal after each deletion confirms that the BST property (left < parent < right at every node) is preserved throughout, which is why the printed sequence always stays sorted.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.