# C Program to Create Mirror Image of Binary Tree

This is a C Program for creating a mirror image of a Binary Tree using recursion.

Problem Description

We will be given a Tree and we have to create its mirror image and perform level order traversal on the tree before and after creating its mirror image.

Expected Input and Output

Case 1. If the input tree is a balanced tree. For example:

```                    25                   |                      25
/    \                 |                    /     \
27     19               |                  19      27
/ \     / \              |                 /  \    /  \
17  91   13 55             |                55  13  91   17

Input Tree             Mirror               Output Tree```

Case 2. If the tree has only right children at all the levels (Right Skewed Tree). A right skewed tree is one in which all the nodes just have a right child at all the levels. For example:

```         1                                   |                                  1
\                                  |                                 /
2                                 |                                2
\                                |                               /
3                               |                              3
\                              |                             /
4                             |                            4
\                            |                           /
5                           |                          5

Input Tree                   Mirror                    Output Tree```

Case 3. Tree having just one node. For example:

```
15                |              15
Input Tree       Mirror        Output Tree```
Problem Solution

1. For creating a mirror image of a tree, we need to traverse the subtrees.
2. While traversing the subtrees we have to swap the left and the right child of all the nodes.
3. After swapping the left and the right child of all the nodes the tree which we will obtain, will be the mirror image of the original tree which was taken as an input.

Program/Source Code

Here is source code of the C Program for creating a Mirror Image of a given Tree using Recursion. The program is successfully compiled and tested using Codeblocks gnu/GCC compiler on windows 10. The program output is also shown below.

1. `/* C Program for creating the mirror image of a given tree.*/`
2. `#include <stdio.h>`
3. `#include <stdlib.h>`
4. ` `
5. `struct node`
6. `{`
7. `    int info;`
8. `    struct node* left, *right;`
9. `};`
10. ` `
11. `/*`
12. ` * Function to create new nodes.`
13. ` */`
14. ` `
15. `struct node* createnode(int key)`
16. `{`
17. `    struct node* newnode = (struct node*)malloc(sizeof(struct node));`
18. `    newnode->info = key;`
19. `    newnode->left = NULL;`
20. `    newnode->right = NULL;`
21. ` `
22. `    return(newnode);`
23. `}`
24. ` `
25. `/*`
26. ` *Function to swap left and right child of  a node for creating mirror image.`
27. ` */`
28. ` `
29. `void mirrorimage(struct node* root)`
30. `{`
31. `  if (root != NULL)`
32. `    {`
33. `        struct node* temp;`
34. `        /*first traversing the left subtree */`
35. `        mirrorimage(root->left);      `
36. `        /* Traversing the right subtree. */`
37. `        mirrorimage(root->right);     `
38. ` `
39. `        /* swap the left and right child of all the nodes to create`
40. `         * a mirror image of a tree`
41. `         */`
42. ` `
43. `        temp = root->left;`
44. `        root->left  = root->right;   `
45. `        root->right = temp;`
46. ` `
47. `    }`
48. `}`
49. ` `
50. `/*`
51. ` * Function to find the height of a tree.`
52. ` */`
53. ` `
54. `int heightoftree(struct node* root)`
55. `{`
56. `    int max;`
57. ` `
58. `    if (root!=NULL)`
59. `    {`
60. `        /*Finding the height of left subtree.*/`
61. `        int leftsubtree = heightoftree(root->left);`
62. ` `
63. `        /*Finding the height of right subtree.*/`
64. `        int rightsubtree = heightoftree(root->right);  `
65. ` `
66. ` `
67. `        if (leftsubtree > rightsubtree)`
68. `        {`
69. `            max = leftsubtree + 1;`
70. `            return max;`
71. `        }`
72. `        else`
73. `        {`
74. `            max = rightsubtree + 1;`
75. `            return max;`
76. `        }`
77. `    }`
78. `}`
79. ` `
80. `/*`
81. ` * Function to print all the nodes left to right of the current level`
82. ` */`
83. ` `
84. `void currentlevel(struct node* root, int level)`
85. `{`
86. `    if (root != NULL)`
87. `    {`
88. `        if (level == 1)`
89. `        {`
90. `            printf("%d ", root->info);`
91. `        }`
92. ` `
93. `        else if (level > 1)`
94. `        {`
95. `            currentlevel(root->left, level-1);`
96. `            currentlevel(root->right, level-1);`
97. `        }`
98. `    }`
99. ` `
100. `}`
101. ` `
102. `int main()`
103. `{`
104. `   /* Creating first Tree.*/`
105. ` `
106. `    struct node *newnode = createnode(25);`
107. `    newnode->left = createnode(27);`
108. `    newnode->right = createnode(19);`
109. `    newnode->left->left = createnode(17);`
110. `    newnode->left->right = createnode(91);`
111. `    newnode->right->left = createnode(13);`
112. `    newnode->right->right = createnode(55);`
113. ` `
114. `    /* Sample Tree 1- Balanced Tree.`
115. ` `
116. ` `
117. `                    25                   |                      25`
118. `                  /    \                 |                    /     \`
119. `                 27     19               |                  19      27`
120. `                / \     / \              |                 /  \    /  \`
121. `              17  91   13 55             |                55  13  91   17`
122. ` `
123. `             Input Tree                 Mirror           Output Tree`
124. `    */`
125. ` `
126. `    printf("Level Order Traversal of Tree 1 "`
127. `           "before creating its mirror image is \n");`
128. ` `
129. `    int i;`
130. `    int height = heightoftree(newnode);`
131. ` `
132. `    /* calling current level function, by passing levels one by one `
133. `     * in an increasing order.`
134. `     */`
135. ` `
136. `    for(i = 1; i <= height; i++)      `
137. `    {`
138. `        currentlevel(newnode,i);`
139. `    }`
140. `    printf("\n\nLevel Order Traversal of Tree 1 "`
141. `               "after creating its mirror image is \n");`
142. ` `
143. ` `
144. `    height = heightoftree(newnode);`
145. `    mirrorimage(newnode);`
146. ` `
147. `   /* calling current level function, by passing levels one by one `
148. `    * in an increasing order.`
149. `    */`
150. ` `
151. `    for(i = 1; i <= height; i++)   `
152. `    {`
153. `        currentlevel(newnode,i);`
154. `    }`
155. ` `
156. `    /*Creating second Tree.*/`
157. ` `
158. `    struct node *node = createnode(1);`
159. `    node->right = createnode(2);`
160. `    node->right->right = createnode(3);`
161. `    node->right->right->right = createnode(4);`
162. `    node->right->right->right->right = createnode(5);`
163. ` `
164. `    /* Sample Tree 2-   Right Skewed Tree (Unbalanced).`
165. ` `
166. `      1                                   |                                  1`
167. `       \                                  |                                 /`
168. `        2                                 |                                2`
169. `         \                                |                               /`
170. `          3                               |                              3`
171. `           \                              |                             /`
172. `            4                             |                            4`
173. `             \                            |                           /`
174. `              5                           |                          5`
175. ` `
176. `           Input Tree                   Mirror                    Output Tree`
177. `    */  `
178. ` `
179. `    printf("\n\nLevel Order Traversal of Tree 2 "`
180. `               "before creating its mirror image is \n");`
181. ` `
182. `    height = heightoftree(node);`
183. ` `
184. `    /* calling current level function, by passing levels one by one`
185. `     * in an increasing order.`
186. `     */`
187. ` `
188. `    for(i = 1; i <= height; i++)`
189. `    {`
190. `        currentlevel(node,i);`
191. `    }`
192. ` `
193. `    printf("\n\nLevel Order Traversal of Tree 2 "`
194. `               "after creating its mirror image is \n");`
195. ` `
196. `    height = heightoftree(node);`
197. `    mirrorimage(node);`
198. ` `
199. `    /* calling current level function, by passing levels one by one`
200. `     * in an increasing order.`
201. `     */`
202. ` `
203. `    for(i = 1; i <= height; i++)      `
204. `    {`
205. `        currentlevel(node,i);`
206. `    }`
207. ` `
208. `    /* Creating  third tree having just one root node */`
209. `    struct node *root = createnode(15);`
210. ` `
211. ` `
212. ` `
213. `    /* Sample Tree 3 -   Tree having just one root node.`
214. ` `
215. `                   15           |              15`
216. `             Input Tree                      Output Tree`
217. `                              Mirror`
218. `    */`
219. ` `
220. `    printf("\n\nLevel Order Traversal of Tree 3 "`
221. `               "before creating its mirror image is \n");`
222. `    height = heightoftree(root);`
223. ` `
224. `    /* calling current level function, by passing levels one by one`
225. `     * in an increasing order.`
226. `     */`
227. ` `
228. `    for(i = 1; i <= height; i++)  `
229. `    {`
230. `        currentlevel(root,i);`
231. `    }`
232. ` `
233. `    printf("\n\nLevel Order Traversal of Tree 3 "`
234. `               "after creating its mirror image is \n");`
235. `    height = heightoftree(root);`
236. `    mirrorimage(root);`
237. ` `
238. `    /* calling current level function, by passing levels one by one`
239. `     * in an increasing order.`
240. `     */`
241. ` `
242. `    for(i = 1; i <= height; i++)`
243. `    {`
244. `        currentlevel(root,i);`
245. `    }`
246. ` `
247. `    return 0;`
248. `}`
Program Explanation

1. Here in this program we have created a function called mirrorimage(struct node* root).
2. The idea behind creating a mirror image is swapping the left and the right child of all the nodes from top to bottom.
3. In order to do that we need to traverse the nodes. So, we have used the post order traversal i.e. first we will visit all the nodes left to the root node then we will visit all the nodes right to the root node, and will swap both the children of a node one by one.

Runtime Test Cases
```
Level Order Traversal of Tree 1 before creating its mirror image is
25 27 19 17 91 13 55

Level Order Traversal of Tree 1 after creating its mirror image is
25 19 27 55 13 91 17

Level Order Traversal of Tree 2 before creating its mirror image is
1 2 3 4 5

Level Order Traversal of Tree 2 after creating its mirror image is
1 2 3 4 5

Level Order Traversal of Tree 3 before creating its mirror image is
15

Level Order Traversal of Tree 3 after creating its mirror image is
15```

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms