C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Data Structures in C

Binary Search Tree — Mirror Image Creation

 Concept Overview

A mirror image of a binary tree is another tree where the left and right children of all nodes are interchanged.

Example:

Original BST

        50

       /  \

     30    70

    / \    / \

  20  40  60  80

Mirror Image

        50

       /  \

     70    30

    / \    / \

  80  60  40  20

 Logic Summary

To create a mirror image:

  1. Traverse each node recursively.
  2. For each node, swap its left and right child.
  3. Continue recursively for both subtrees.

Recursive Function:

void mirror(struct Node* root) {

    if (root == NULL)

        return;

    mirror(root->left);

    mirror(root->right);

 

    // Swap left and right child

    struct Node* temp = root->left;

    root->left = root->right;

    root->right = temp;

}

 

C Program: Binary Search Tree - Mirror Image Creation

Mirror Image of a BST

C

#include <stdio.h>

#include <stdlib.h>

 

// Structure for BST Node

struct Node {

    int data;

    struct Node *left, *right;

};

 

// Function to create a new node

struct Node* createNode(int value) {

    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

    newNode->data = value;

    newNode->left = newNode->right = NULL;

    return newNode;

}

 

// Function to insert node in BST

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

    if (root == NULL)

        return createNode(value);

 

    if (value < root->data)

        root->left = insert(root->left, value);

    else if (value > root->data)

        root->right = insert(root->right, value);

 

    return root;

}

 

// Inorder traversal

void inorder(struct Node* root) {

    if (root != NULL) {

        inorder(root->left);

        printf("%d ", root->data);

        inorder(root->right);

    }

}

 

// Function to create mirror image of the tree

void mirror(struct Node* root) {

    if (root == NULL)

        return;

 

    // Recursively mirror left and right subtrees

    mirror(root->left);

    mirror(root->right);

 

    // Swap left and right pointers

    struct Node* temp = root->left;

    root->left = root->right;

    root->right = temp;

}

 

int main() {

    struct Node* root = NULL;

 

    // Construct BST

    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 Original BST: ");

    inorder(root);

    printf("\n");

 

    // Create mirror image

    mirror(root);

 

    printf("Inorder Traversal of Mirror Image BST: ");

    inorder(root);

    printf("\n");

 

    return 0;

}

Output

 
OUTPUT :
Inorder Traversal of Original BST: 20 30 40 50 60 70 80
Inorder Traversal of Mirror Image BST: 80 70 60 50 40 30 20

Visualization

Before Mirroring

        50

       /  \

     30    70

    / \    / \

  20  40  60  80

After Mirroring

        50

       /  \

     70    30

    / \    / \

  80  60  40  20

Notice how the left and right subtrees are swapped at every level.

Complexity Analysis

Operation

Time Complexity

Space Complexity

Mirror Tree

O(n)

O(h)

Here,

  • n = number of nodes
  • h = height of the tree (for recursion stack)

Key Takeaways

  • The mirror of a BST may not be a valid BST (since ordering changes).
  • Mirroring is mainly a structural transformation, not a search-oriented one.
  • It’s a great example of postorder traversal logic — process left, right, then root.
  • Used in tree visualization, symmetry testing, and image processing algorithms.