fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct node{
  4. int data;
  5. struct node *left;
  6. struct node *right;
  7. }stick;
  8. stick *createnode(int data)
  9. {
  10. stick *newNode;
  11. newNode=(stick *)calloc(1,sizeof(stick));
  12. newNode->data=data;
  13. newNode->left=NULL;
  14. newNode->right=NULL;
  15. return newNode;
  16. }
  17. stick *binarysearchAndentry(int value,stick *node,int operation)
  18. {
  19. if(node==NULL)
  20. {
  21. if(operation==0)
  22. {printf("%d is not in the tree",value);}
  23. else
  24. {
  25. printf("%d is entered in appropiate position\n",value);
  26. node=createnode(value);
  27. return node;
  28. }
  29. }
  30. else
  31. {
  32. if(node->left==NULL && node->data>value)
  33. {
  34. stick *lol;
  35. lol=createnode(value);
  36. node->left=lol;
  37. printf("%d is entered in appropiate position\n",value);
  38. }
  39. else
  40. {
  41. if(node->right==NULL && node->data<value)
  42. {
  43. stick *lol;
  44. lol=createnode(value);
  45. node->right=lol;
  46. printf("%d is entered in appropiate position\n",value);
  47. }
  48. else
  49. {
  50. if(value==node->data)
  51. {
  52. printf("%d is found in the tree",value);
  53. }
  54. else
  55. {
  56. if(node->data>value)
  57. {
  58. binarysearchAndentry(value,node->left,operation);
  59. }
  60. else
  61. {
  62. binarysearchAndentry(value,node->right,operation);
  63. }
  64. }
  65. }
  66. }
  67. }
  68. }
  69.  
  70. int main(void) {
  71. stick *root;
  72. root=binarysearchAndentry(20,NULL,1);
  73. binarysearchAndentry(30,root,1);
  74. binarysearchAndentry(10,root,1);
  75. binarysearchAndentry(50,root,1);
  76. binarysearchAndentry(25,root,1);
  77. printf("%d\n",root->data);
  78. printf("%d\n",root->left->data);
  79. printf("%d\n",root->right->data);
  80. printf("%d\n",root->right->right->data);
  81. printf("%d\n",root->right->left->data);
  82.  
  83. return 0;
  84. }
  85.  
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
20 is entered in appropiate position
30 is entered in appropiate position
10 is entered in appropiate position
50 is entered in appropiate position
25 is entered in appropiate position
20
10
30
50
25