C Program to Create a Mirror Copy of a Tree and Display using BFS Traversal

This C Program creates a mirror copy of a tree and display using BFS traversal.

Here is source code of the C Program to create a mirror copy of a tree and display using BFS traversal. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /* 
  2.  * C Program to Create a Mirror Copy of a Tree and Display using 
  3.  * BFS Traversal
  4.  *                    40
  5.  *                    /\
  6.  *                   20 60
  7.  *                   /\  \
  8.  *                  10 30  80
  9.  *                          \
  10.  *                           90
  11.  */
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14.  
  15. struct btnode
  16. { 
  17.     int value; 
  18.     struct btnode *left, *right; 
  19. }; 
  20. typedef struct btnode node;
  21.  
  22. /* function prototypes */
  23. void insert(node *, node *);
  24. void mirror(node *);
  25.  
  26. /* global variables */
  27. node *root = NULL;
  28. int val, front = 0, rear = -1, i;
  29. int queue[20];
  30.  
  31. void main() 
  32. { 
  33.     node *new = NULL ; 
  34.     int num = 1; 
  35.     printf("Enter the elements of the tree(enter 0 to exit)\n"); 
  36.     while (1) 
  37.     {     
  38.         scanf("%d",  &num); 
  39.         if (num == 0) 
  40.             break; 
  41.         new = malloc(sizeof(node)); 
  42.         new->left = new->right = NULL; 
  43.         new->value = num; 
  44.         if (root == NULL) 
  45.             root = new; 
  46.         else 
  47.         { 
  48.             insert(new, root); 
  49.         } 
  50.     }
  51.     printf("mirror image of tree is\n"); 
  52.     queue[++rear] = root->value;
  53.     mirror(root);
  54.     for (i = 0;i <= rear;i++)
  55.         printf("%d -> ", queue[i]);
  56.     printf("%d\n", root->right->right->right->value);
  57. }
  58.  
  59. /* inserting nodes into the tree */
  60. void insert(node * new , node *root) 
  61. { 
  62.     if (new->value > root->value) 
  63.     {     
  64.         if (root->right == NULL) 
  65.             root->right = new; 
  66.         else 
  67.             insert (new, root->right); 
  68.     } 
  69.     if (new->value < root->value) 
  70.     {     
  71.         if (root->left == NULL) 
  72.             root->left = new; 
  73.         else 
  74.             insert (new, root->left); 
  75.     }     
  76. }
  77.  
  78. /* mirror image of nodes */
  79. void mirror(node *root)
  80. {
  81.     val = root->value;
  82.     if ((front <= rear) && (root->value == queue[front])) 
  83.     {
  84.         if (root->right != NULL || root->right == NULL)
  85.             queue[++rear] = root->right->value;
  86.         if (root->left != NULL)
  87.             queue[++rear] = root->left->value;
  88.         front++;
  89.     }
  90.     if (root->right != NULL)
  91.     {
  92.         mirror(root->right);
  93.     }
  94.     if (root->left != NULL)
  95.     {
  96.         mirror(root->left);
  97.     }
  98. }

$ cc tree16.c
$ a.out
Enter the elements of the tree(enter 0 to exit)
40
20
10
30
60
70
80
0
mirror image of tree is
40 -> 60 -> 20 -> 70 -> 30 -> 10 -> 80

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

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

If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.