Advertisement
afrinahoque

Ekhon koi shomossha? :e

Dec 10th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.16 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct node
  4. {
  5.     int a;
  6.     struct node *left, *right;
  7. }node;
  8.  
  9. node *root=NULL, *temp=NULL;
  10.  
  11. void insert(node *temp, node *N)
  12. {
  13.  
  14.         if(N->a<temp->a)
  15.         {
  16.             if(temp->left==NULL)
  17.             {
  18.                 temp->left=N;
  19.             }
  20.             else
  21.             {
  22.                 insert(temp->left, N);
  23.             }
  24.         }
  25.             else
  26.             {
  27.                 if(temp->right==NULL)
  28.                 {
  29.                     temp->right=N;
  30.                 }
  31.                 else
  32.                 {
  33.                     insert(temp->right, N);
  34.                 }
  35.             }
  36.         }
  37.  
  38. void preorder()
  39. {
  40.     node *temp=root;
  41.     while(temp!=NULL)
  42.     {
  43.         printf("Preorder: %d ", temp->a);
  44.         preorder(temp->left);
  45.         preorder(temp->right);
  46.     }
  47. }
  48.  
  49. int main()
  50. {
  51.     int i;
  52.     for(i=0; i<5; i++)
  53.     {
  54.         node *N=(node*)malloc(sizeof(node));
  55.     scanf("%d", &N->a);
  56.     N->left=NULL;
  57.     N->right=NULL;
  58.  
  59.     if(root==NULL)
  60.     {
  61.         root=N;
  62.     }
  63.     else
  64.     {
  65.         insert(root, N);
  66.     }
  67.     }
  68.     preorder();
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement