fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct node{
  4. int data;
  5. struct node* left;
  6. struct node* right;
  7. };
  8.  
  9. struct node* createnode(int val)
  10. {
  11. struct node* p=(struct node*)malloc(sizeof(struct node));
  12. p->data=val;
  13. p->left=NULL;
  14. p->right=NULL;
  15. return p;
  16. }
  17. struct node* insertnode(struct node* root,int num)
  18. {
  19. if(root==NULL)
  20. {
  21. root=createnode(num);
  22. return root;
  23. }
  24. if(num<root->data)
  25. root->left=insertnode(root->left,num);
  26. else
  27. root->right=insertnode(root->right,num);
  28. return root;
  29. }
  30. void printPreorder(struct node* root)
  31. {
  32. if(root==NULL)
  33. return;
  34. printf("%d ",root->data);
  35. printPreorder(root->left);
  36. printPreorder(root->right);
  37. }
  38. void printInorder(struct node* root)
  39. {
  40. if(root==NULL)
  41. return;
  42. printInorder(root->left);
  43. printf("%d ",root->data);
  44. printInorder(root->right);
  45. }
  46.  
  47. void printPostorder(struct node* root)
  48. {
  49. if(root==NULL)
  50. return;
  51. printPostorder(root->left);
  52. printPostorder(root->right);
  53. printf("%d ",root->data);
  54. }
  55.  
  56. int main() {
  57. struct node* root=createnode(20);
  58. root->left=createnode(15);
  59. root->right=createnode(25);
  60. root->left->left=createnode(13);
  61. root->left->right=createnode(17);
  62. root->left->left->left=createnode(9);
  63. root->right->left=createnode(22);
  64. root->right->right=createnode(26);
  65. root->right->right->right=createnode(30);
  66. printPreorder(root);
  67. printf("\n");
  68. printInorder(root);
  69. printf("\n");
  70. printPostorder(root);
  71. return 0;
  72. }
  73.  
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
20 15 13 9 17 25 22 26 30 
9 13 15 17 20 22 25 26 30 
9 13 17 15 22 30 26 25 20