0% found this document useful (0 votes)
33 views3 pages

P5 BinaryTree

The document describes a C program that takes a binary tree as input, mirrors the tree by swapping the left and right children of each node, and prints the original and mirrored trees. The program creates the tree by recursively prompting the user for nodes and children. It contains functions to print the tree inorder, create the tree by user input, mirror the tree by swapping children, and the main function that calls the other functions.

Uploaded by

kunal.bhoi.st
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views3 pages

P5 BinaryTree

The document describes a C program that takes a binary tree as input, mirrors the tree by swapping the left and right children of each node, and prints the original and mirrored trees. The program creates the tree by recursively prompting the user for nodes and children. It contains functions to print the tree inorder, create the tree by user input, mirror the tree by swapping children, and the main function that calls the other functions.

Uploaded by

kunal.bhoi.st
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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 :

You might also like