C Program to Create a Mirror Copy of a Tree

This C Program Creates Mirror Copy of a Tree.

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

$ cc tree16.c
$ a.out
Enter the elements of the tree(enter 0 to exit)
40
20
60
10
30
80
90
0
elements in a tree in inorder are
10      20      30      40      60      80      90
mirror image of the elements are
90      80      60      40      30      20      10

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.