Check if two Trees are Mirror Images or not
Write a C Program to Check if two Trees are Mirror Images or not. Here’s simple Program to Check if two binary trees are mirror image of each other or not in C Programming Language.
What is Tree ?
In linear data structure, data is organized in sequential order and in non-linear data structure, data is organized in random order. Tree is a very popular data structure used in wide range of applications.
A tree data structure can be defined as follows…
- Tree is a non-linear data structure which organizes data in hierarchical structure and this is a recursive definition.
A tree data structure can also be defined as follows…
- Tree data structure is a collection of data (Node) which is organized in hierarchical structure and this is a recursive definition.
Every individual element is called as Node. Node in a tree data structure, stores the actual data of that particular element and link to next element in hierarchical structure.
Below is the source code for C Program to Check if two Trees are Mirror Images or not which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
/* C Program to Check if two Trees are Mirror Images or not */ #include<stdio.h> #include<stdlib.h> struct node { struct node *lchild; int info; struct node *rchild; }; struct node *insert(struct node *ptr, int ikey); void display(struct node *ptr,int level); void SwapChildren(struct node* ptr); int isIdentical( struct node *p1, struct node *p2); int isMirrorImage(struct node *root1, struct node *root2); int main() { struct node *root=NULL,*root1=NULL,*ptr; int choice,k,item; while(1) { printf("\n"); printf("1.Insert Tree 1\n"); printf("2.Display Tree 1\n"); printf("3.Insert Tree 2\n"); printf("4.Display Tree 2\n"); printf("5.Check for MirrorImage\n"); printf("6.Quit\n"); printf("\nEnter your choice : "); scanf("%d",&choice); switch(choice) { case 1: printf("\nEnter the key to be inserted : "); scanf("%d",&k); root = insert(root, k); break; case 2: printf("\n"); display(root,0); printf("\n"); break; case 3: printf("\nEnter the key to be inserted : "); scanf("%d",&k); root1 = insert(root1, k); break; case 4: printf("\n"); SwapChildren(root1); display(root1,0); printf("\n"); break; case 5: printf("\n"); if( isMirrorImage(root, root1)) printf("Tree 1 is mirror image of tree 2\n"); else printf("Tree 1 is not mirror image of tree 2\n"); break; case 6: exit(1); default: printf("\nWrong choice\n"); }/*End of switch */ }/*End of while */ return 0; }/*End of main( )*/ struct node *insert(struct node *ptr, int ikey ) { if(ptr==NULL) { ptr = (struct node *) malloc(sizeof(struct node)); ptr->info = ikey; ptr->lchild = NULL; ptr->rchild = NULL; } else if(ikey < ptr->info) /*Insertion in left subtree*/ ptr->lchild = insert(ptr->lchild, ikey); else if(ikey > ptr->info) /*Insertion in right subtree */ ptr->rchild = insert(ptr->rchild, ikey); else printf("Duplicate key\n"); return(ptr); }/*End of insert( )*/ void display(struct node *ptr,int level) { int i; if(ptr == NULL )/*Base Case*/ return; else { display(ptr->rchild, level+1); printf("\n"); for (i=0; i<level; i++) printf(" "); printf("%d", ptr->info); display(ptr->lchild, level+1); } }/*End of display()*/ void SwapChildren(struct node* ptr) { struct node* tmp; if (ptr==NULL) return; SwapChildren(ptr->lchild); SwapChildren(ptr->rchild); tmp = ptr->lchild; ptr->lchild = ptr->rchild; ptr->rchild = tmp; } int isIdentical( struct node *p1, struct node *p2) { if(p1==NULL && p2==NULL) return 1; if(p1!=NULL && p2!=NULL && p1->info == p2->info) if(isIdentical(p1->lchild, p2->lchild) && isIdentical(p1->rchild, p2->rchild)) return 1; return 0; } int isMirrorImage(struct node *root1, struct node *root2) { int flag; SwapChildren(root1); if( isIdentical(root1, root2)) flag=1; else flag=0; SwapChildren(root1); return flag; }
OUTPUT : :
/* C Program to Check if two Trees are Mirror Images or not */ 1.Insert Tree 1 2.Display Tree 1 3.Insert Tree 2 4.Display Tree 2 5.Check for MirrorImage 6.Quit Enter your choice : 1 Enter the key to be inserted : 5 1.Insert Tree 1 2.Display Tree 1 3.Insert Tree 2 4.Display Tree 2 5.Check for MirrorImage 6.Quit Enter your choice : 1 Enter the key to be inserted : 3 1.Insert Tree 1 2.Display Tree 1 3.Insert Tree 2 4.Display Tree 2 5.Check for MirrorImage 6.Quit Enter your choice : 1 Enter the key to be inserted : 4 1.Insert Tree 1 2.Display Tree 1 3.Insert Tree 2 4.Display Tree 2 5.Check for MirrorImage 6.Quit Enter your choice : 1 Enter the key to be inserted : 2 1.Insert Tree 1 2.Display Tree 1 3.Insert Tree 2 4.Display Tree 2 5.Check for MirrorImage 6.Quit Enter your choice : 1 Enter the key to be inserted : 6 1.Insert Tree 1 2.Display Tree 1 3.Insert Tree 2 4.Display Tree 2 5.Check for MirrorImage 6.Quit Enter your choice : 1 Enter the key to be inserted : 7 1.Insert Tree 1 2.Display Tree 1 3.Insert Tree 2 4.Display Tree 2 5.Check for MirrorImage 6.Quit Enter your choice : 3 Enter the key to be inserted : 5 1.Insert Tree 1 2.Display Tree 1 3.Insert Tree 2 4.Display Tree 2 5.Check for MirrorImage 6.Quit Enter your choice : 3 Enter the key to be inserted : 3 1.Insert Tree 1 2.Display Tree 1 3.Insert Tree 2 4.Display Tree 2 5.Check for MirrorImage 6.Quit Enter your choice : 3 Enter the key to be inserted : 4 1.Insert Tree 1 2.Display Tree 1 3.Insert Tree 2 4.Display Tree 2 5.Check for MirrorImage 6.Quit Enter your choice : 3 Enter the key to be inserted : 2 1.Insert Tree 1 2.Display Tree 1 3.Insert Tree 2 4.Display Tree 2 5.Check for MirrorImage 6.Quit Enter your choice : 3 Enter the key to be inserted : 6 1.Insert Tree 1 2.Display Tree 1 3.Insert Tree 2 4.Display Tree 2 5.Check for MirrorImage 6.Quit Enter your choice : 3 Enter the key to be inserted : 7 1.Insert Tree 1 2.Display Tree 1 3.Insert Tree 2 4.Display Tree 2 5.Check for MirrorImage 6.Quit Enter your choice : 2 7 6 5 4 3 2 1.Insert Tree 1 2.Display Tree 1 3.Insert Tree 2 4.Display Tree 2 5.Check for MirrorImage 6.Quit Enter your choice : 4 2 3 4 5 6 7 1.Insert Tree 1 2.Display Tree 1 3.Insert Tree 2 4.Display Tree 2 5.Check for MirrorImage 6.Quit Enter your choice : 5 Tree 1 is mirror image of tree 2 1.Insert Tree 1 2.Display Tree 1 3.Insert Tree 2 4.Display Tree 2 5.Check for MirrorImage 6.Quit Enter your choice : 6 Process returned 1
If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.
Thanks for reading the post…