Advertisement
Arnab_Manna

mirror image

Jan 20th, 2023
845
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.12 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int count=0,max=0,min=0;
  5. typedef struct node
  6. {
  7.     int info;
  8.     struct node *lc,*rc;
  9. }bin_tere;
  10.  
  11. typedef struct q
  12. {
  13.     bin_tere *arr[200];
  14.     int front;
  15.     int rear;
  16. }Q;
  17.  
  18. void create(bin_tere *root)
  19. {
  20.     char ans;
  21.     bin_tere *ptr;
  22.     printf("\nDo you want to create the left child of %d? ",root->info);
  23.     fflush(stdin);
  24.     scanf("%c",&ans);
  25.     if(ans=='Y' || ans=='y')
  26.     {
  27.         ptr=(bin_tere*)malloc(sizeof(bin_tere));
  28.         printf("\nenter the information for the left child: ");
  29.         scanf("%d",&ptr->info);
  30.         ptr->lc=ptr->rc=NULL;
  31.         root->lc=ptr;
  32.         create(root->lc);
  33.     }
  34.     else
  35.         root->lc=NULL;
  36.     printf("\nDo you want to create the right child of %d? ",root->info);
  37.     fflush(stdin);
  38.     scanf("%c",&ans);
  39.     if(ans=='Y' || ans=='y')
  40.     {
  41.         ptr=(bin_tere*)malloc(sizeof(bin_tere));
  42.         printf("\nenter the information for the right child: ");
  43.         scanf("%d",&ptr->info);
  44.         ptr->lc=ptr->rc=NULL;
  45.         root->rc=ptr;
  46.         create(root->rc);
  47.     }
  48.     else
  49.         root->rc=NULL;
  50. }
  51.  
  52. void enqueue(Q *q, bin_tere *node) {
  53.     q->arr[q->rear] = node;
  54.     q->rear++;
  55. }
  56.  
  57. bin_tere *dequeue(Q *q) {
  58.    bin_tere *temp = q->arr[q->front];
  59.     q->front++;
  60.     return temp;
  61. }
  62.  
  63. void levelOrder(bin_tere *root) {
  64.     Q *q = (Q*)malloc(sizeof(Q));
  65.     q->front = 0;
  66.     q->rear = 0;
  67.     enqueue(q, root);
  68.     while (q->front != q->rear) {
  69.         bin_tere *temp = dequeue(q);
  70.         printf("%d ", temp->info);
  71.         if (temp->lc)
  72.             enqueue(q, temp->lc);
  73.         if (temp->rc)
  74.             enqueue(q, temp->rc);
  75.     }
  76. }
  77.  
  78. void mirror(bin_tere *root)
  79. {
  80.     if(root==NULL)
  81.     {
  82.         return;
  83.     }
  84.     else
  85.     {
  86.         bin_tere *temp=root->lc;
  87.         root->lc=root->rc;
  88.         root->rc=temp;
  89.         mirror( root->lc);
  90.         mirror( root->rc);
  91.        
  92.     }
  93. }
  94.  
  95. int main()
  96. {
  97.     bin_tere *root=NULL,*ptr;
  98.     int ch,n;
  99.     printf("\nenter the information for the root: ");
  100.     ptr=(bin_tere*)malloc(sizeof(bin_tere));
  101.     scanf("%d",&ptr->info);
  102.     ptr->lc=ptr->rc=NULL;
  103.     root=ptr;
  104.     create(root);
  105.    
  106.     printf("the level wise traversal :");
  107.     levelOrder(root);
  108.     mirror(root);
  109.     printf("the level wise traversal after mirror image :");
  110.     levelOrder(root);
  111.     return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement