fork download
  1. #include <stdio.h>
  2. #include <stdlib.h> //without this calloc can not be used
  3. typedef struct node //tree structure definition
  4. {
  5. int data;
  6. struct node *left;
  7. struct node *right;
  8. }branch; // here we have set it as branch
  9.  
  10. branch *createnode(int value) //createnode is the function to create a node of the tree and return it
  11. {
  12. branch *stick;
  13. stick=(branch *)calloc(1,sizeof(branch));
  14. stick->data=value;
  15. stick->left=NULL;
  16. stick->right=NULL;
  17. return stick;
  18. }
  19. void pre_order(branch *root)
  20. {
  21. if(root==NULL)
  22. {return;}
  23. printf("%d ",root->data);
  24. pre_order(root->left);
  25. pre_order(root->right);
  26.  
  27. }
  28. void post_order(branch *root)
  29. {
  30. if(root==NULL)
  31. {return;}
  32. post_order(root->left);
  33. post_order(root->right);
  34. printf("%d ",root->data);
  35. }
  36. void in_order(branch *root)
  37. {
  38. if(root==NULL)
  39. {return;}
  40. in_order(root->left);
  41. printf("%d ",root->data);
  42. in_order(root->right);
  43.  
  44. }
  45. //driver program
  46. int main(void)
  47. {
  48. branch *root=createnode(2);
  49. root->left=createnode(5);
  50. root->right=createnode(1);
  51. root->left->left=createnode(4);
  52. root->left->right=createnode(9);
  53. root->right->left=createnode(0);
  54. root->right->right=createnode(6);
  55. printf("the preorder representation is:");
  56. pre_order(root);
  57. printf("\n the in-order representation is:");
  58. in_order(root);
  59. printf("\n the post-order representation is:");
  60. post_order(root);
  61. return 0;
  62. }
  63.  
  64.  
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
the preorder representation is:2 5 4 9 1 0 6 
 the in-order representation is:4 5 9 2 0 1 6 
 the post-order representation is:4 9 5 0 6 1 2