Advertisement
Niloy007

Assignment of DS

May 28th, 2020
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.99 KB | None | 0 0
  1.  
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4.  
  5. struct node
  6. {
  7.     int data;
  8.     struct node* left;
  9.     struct node* right;
  10. };
  11.  
  12. struct node* createNode(int value)
  13. {
  14.     struct node* newNode = (struct node*) malloc (sizeof(struct node));
  15.     newNode->data = value;
  16.     newNode->left = NULL;
  17.     newNode->right = NULL;
  18.  
  19.     return newNode;
  20. }
  21.  
  22.  
  23. struct node* insert(struct node* root, int data)
  24. {
  25.     if (root == NULL)
  26.         return createNode(data);
  27.  
  28.     if (data < root->data)
  29.         root->left  = insert(root->left, data);
  30.     else if (data > root->data)
  31.         root->right = insert(root->right, data);
  32.  
  33.     return root;
  34. }
  35.  
  36. void inorder(struct node* root)
  37. {
  38.     if(root == NULL)
  39.         return;
  40.     inorder(root->left);
  41.     printf("%d ->", root->data);
  42.     inorder(root->right);
  43. }
  44.  
  45.  
  46. int main()
  47. {
  48.     struct node *root = NULL;
  49.     int n, k, value;
  50.     scanf("%d %d", &n, &k);
  51.  
  52.     while(n--) {
  53.         scanf("%d", &value);
  54.         root = insert(root, value);
  55.     }
  56.  
  57.     inorder(root);
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement