Mirror tree is obtained by interchanging the left subtree with the right subtree
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* left;
struct node* right;
};
struct node* newNodeCreate(int value)
{
struct node* newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=value;
newnode->left=NULL;
newnode->right=NULL;
return newnode;
}
void inOrderTraversal(struct node* head)
{
if(head == NULL)
return;
inOrderTraversal(head->left);
printf("%d\n",head->data);
inOrderTraversal(head->right);
}
void mirrorTree(struct node *ptr)
{
if(ptr==NULL)
return ;
struct node* temp;
temp=ptr->right;
ptr->right=ptr->left;
ptr->left=temp;
mirrorTree(ptr->left);
mirrorTree(ptr->right);
}
int main()
{
struct node* root=(struct node*)malloc(sizeof(struct node));
root = newNodeCreate(1);
root->left=newNodeCreate(2);
root->right=newNodeCreate(3);
root->left->left=newNodeCreate(4);
root->left->right=newNodeCreate(5);
printf("\n Printing the values in tree before creating mirror image ");
inOrderTraversal(root);
mirrorTree(root);
printf("\n Printing the values in tree after creating mirror image");
inOrderTraversal(root);
getchar();
return 0;
}
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* left;
struct node* right;
};
struct node* newNodeCreate(int value)
{
struct node* newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=value;
newnode->left=NULL;
newnode->right=NULL;
return newnode;
}
void inOrderTraversal(struct node* head)
{
if(head == NULL)
return;
inOrderTraversal(head->left);
printf("%d\n",head->data);
inOrderTraversal(head->right);
}
void mirrorTree(struct node *ptr)
{
if(ptr==NULL)
return ;
struct node* temp;
temp=ptr->right;
ptr->right=ptr->left;
ptr->left=temp;
mirrorTree(ptr->left);
mirrorTree(ptr->right);
}
int main()
{
struct node* root=(struct node*)malloc(sizeof(struct node));
root = newNodeCreate(1);
root->left=newNodeCreate(2);
root->right=newNodeCreate(3);
root->left->left=newNodeCreate(4);
root->left->right=newNodeCreate(5);
printf("\n Printing the values in tree before creating mirror image ");
inOrderTraversal(root);
mirrorTree(root);
printf("\n Printing the values in tree after creating mirror image");
inOrderTraversal(root);
getchar();
return 0;
}
No comments:
Post a Comment