Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
98
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 N;
  4. struct node
  5. {
  6.     int data;
  7.     N *left ,*right;
  8. };
  9. N *create()
  10. {
  11.     int item;
  12.     N *new_node;
  13.     new_node=(N*)malloc(sizeof(N));
  14.     printf("Enter the data:\n");
  15.     scanf("%d",&item);
  16.     if(item==-1)
  17.         return 0;
  18.    new_node->data=item;
  19.    printf("Enter the left data of %d:\n",item);
  20.    new_node->left=create();
  21.    printf("Enter the right data of %d:\n",item);
  22.    new_node->right=create();
  23.    return new_node;
  24. }
  25. void pre_order(N *root)
  26. {
  27.     if(root==-1)
  28.     {
  29.         return ;
  30.     }
  31.     printf("%d\t",root->data);
  32.     pre_order(root->left);
  33.     pre_order(root->right);
  34. }
  35. void in_order(N *root)
  36. {
  37.     if(root==-1)
  38.     {
  39.         return ;
  40.     }
  41.     in_order(root->left);
  42.     printf("%d\t",root->data);
  43.     in_order(root->right);
  44. }
  45. void post_order(N *root)
  46. {
  47.     if(root==-1)
  48.         return ;
  49.     post_order(root->left);
  50.     post_order(root->right);
  51.     printf("%d\t",root->data);
  52. }
  53. int main()
  54. {
  55.     N *root=NULL;
  56.     root=create();
  57.     printf("The pre_order is:\n");
  58.     pre_order(root);
  59.     printf("The post_order is :\n");
  60.     post_order(root);
  61.     printf("The in_order is :\n");
  62.     in_order(root);
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement