fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct tree{int data;
  4. struct tree*right,*left;
  5. };
  6. void insert(struct tree**,int);
  7. void inorder(struct tree*);
  8. int main()
  9. { struct tree *head=NULL;
  10. insert(&head,5);
  11. insert(&head,9);
  12. insert(&head,8);
  13. insert(&head,6);
  14. insert(&head,2);
  15. insert(&head,15);
  16. inorder(head);
  17. return 0;
  18. }
  19.  
  20. void insert(struct tree **head,int i)
  21. {struct tree*temp=*head;
  22. if(temp==NULL)
  23. {temp=malloc(sizeof(struct tree));
  24. temp->right=NULL;
  25. temp->left=NULL;
  26. *head=temp;
  27. }
  28. else
  29. { if(temp->data>i)
  30. insert(&(temp->left),i);
  31. else
  32. insert(&(temp->right),i);
  33. }
  34. }
  35.  
  36. void inorder(struct tree*head)
  37. {if(head)
  38. {inorder(head->left);
  39. printf("%d",head->data);
  40. inorder(head->right);
  41. }
  42. }
Success #stdin #stdout 0.01s 1852KB
stdin
Standard input is empty
stdout
000000