Advertisement
Arnab_Manna

NonRectarversals

Jan 20th, 2023 (edited)
780
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.09 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. bin_tere *stack[500];
  12. int top=-1;
  13.  
  14. void push(bin_tere *root)
  15. {
  16.     stack[++top]=root;
  17. }
  18.  
  19. bin_tere *pop()
  20. {
  21.     bin_tere *temp;
  22.     temp=stack[top--];
  23.     return temp;    
  24. }
  25.  
  26. void create(bin_tere *root)
  27. {
  28.     char ans;
  29.     bin_tere *ptr;
  30.     printf("\nDo you want to create the left child of %d? ",root->info);
  31.     fflush(stdin);
  32.     scanf("%c",&ans);
  33.     if(ans=='Y' || ans=='y')
  34.     {
  35.         ptr=(bin_tere*)malloc(sizeof(bin_tere));
  36.         printf("\nenter the information for the left child: ");
  37.         scanf("%d",&ptr->info);
  38.         ptr->lc=ptr->rc=NULL;
  39.         root->lc=ptr;
  40.         create(root->lc);
  41.     }
  42.     else
  43.         root->lc=NULL;
  44.     printf("\nDo you want to create the right child of %d? ",root->info);
  45.     fflush(stdin);
  46.     scanf("%c",&ans);
  47.     if(ans=='Y' || ans=='y')
  48.     {
  49.         ptr=(bin_tere*)malloc(sizeof(bin_tere));
  50.         printf("\nenter the information for the right child: ");
  51.         scanf("%d",&ptr->info);
  52.         ptr->lc=ptr->rc=NULL;
  53.         root->rc=ptr;
  54.         create(root->rc);
  55.     }
  56.     else
  57.         root->rc=NULL;
  58. }
  59.  
  60. void inorder(bin_tere *root)
  61. {
  62.     bin_tere *curr,*temp;
  63.     curr=root;
  64.     //printf("im here %d",curr->info);
  65.     while(1)
  66.     {
  67.         while(curr!=NULL)
  68.         {
  69.             push(curr);
  70.             curr=curr->lc;
  71.         }
  72.         if(curr==NULL && top>-1)
  73.         {
  74.             temp=pop();
  75.             printf("%d ",temp->info);
  76.             curr=temp->rc;
  77.         }
  78.         if(curr==NULL & top==-1)
  79.             break;
  80.     }
  81. }
  82.    
  83.  
  84. void preorder(bin_tere *root)
  85. {
  86.     struct node *stack[100];
  87.     int top = -1;
  88.     struct node *current = root;
  89.  
  90.     while (1)
  91.     {
  92.         while (current)
  93.         {
  94.             printf("%d ", current->info);
  95.             stack[++top] = current;
  96.             current = current->lc;
  97.         }
  98.  
  99.         if (top < 0)
  100.         {
  101.             break;
  102.         }
  103.  
  104.         current = stack[top--];
  105.         current = current->rc;
  106.     }
  107. }
  108.  
  109. void postorder(bin_tere *root)
  110. {
  111.     do
  112.     {
  113.         while (root)
  114.         {
  115.             if (root->rc)
  116.                 push(root->rc);
  117.             push(root);
  118.  
  119.             root = root->lc;
  120.         }
  121.  
  122.         root = pop();
  123.  
  124.         if (root->rc && stack[top] == root->rc)
  125.         {
  126.             pop();
  127.             push(root);
  128.             root = root->rc;
  129.         }
  130.         else
  131.         {
  132.             printf("%d ", root->info);
  133.             root = NULL;
  134.         }
  135.     } while (top != -1);
  136.     top = -1;
  137. }
  138.  
  139. int main()
  140. {
  141.     bin_tere *root=NULL,*ptr;
  142.     int ch,n;
  143.     printf("\nenter the information for the root: ");
  144.     ptr=(bin_tere*)malloc(sizeof(bin_tere));
  145.     scanf("%d",&ptr->info);
  146.     ptr->lc=ptr->rc=NULL;
  147.     root=ptr;
  148.     create(root);
  149.    
  150.     printf("The in-order traversal is :");
  151.     inorder(root);
  152.     printf("\nThe Pre-order traversal is :");
  153.     preorder(root);
  154.     printf("\nThe Post-order traversal is :");
  155.     postorder(root);
  156.     return 0;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement