Kunal Shantaram Bhoi
Div – C ( CSE-AIML )
Batch – A UID : 2022600007
Program Statement :
Take binary tree as input from user then mirror that binary tree and print.
PROGRAM :
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* left, * right;
};
void printInorder(struct node* node) {
if (node == NULL)
return;
printInorder(node->left);
printf("%d ", node->data);
printInorder(node->right);
}
struct node* create() {
struct node* temp;
int data, choice;
temp = (struct node*)malloc(sizeof(struct node));
printf("\n Press 0 to exit");
printf("\nPress 1 for new node");
printf("\n Enter your choice : ");
scanf("%d", &choice);
if (choice == 0) {
return NULL;
}
else {
printf("Enter the data:");
scanf("%d", &data);
temp->data = data;
printf("\n Enter the left child of %d", data);
temp->left = create();
printf("\n Enter the right child of %d", data);
temp->right = create();
return temp;
}
}
void mirror(struct node* root) {
if (root == NULL) {
return;
}
struct node* temp = root->left;
root->left = root->right;
root->right = temp;
mirror(root->left);
mirror(root->right);
}
int main() {
struct node* root = NULL;
printf("Creating tree:\n");
root = create();
printf("Inorder traversal of the original binary tree:\n");
printInorder(root);
mirror(root);
printf("\nInorder traversal of the mirrored binary tree:\n");
printInorder(root);
return 0;
}
OUTPUT :