fork download
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #define FIN "algsort.in"
  4. #define FOUT "algsort.out"
  5.  
  6. typedef struct bst {
  7. int data;
  8. struct bst *left;
  9. struct bst *right;
  10. } tree;
  11.  
  12.  
  13. tree *insert(int value, tree *t) {
  14.  
  15. if(t == NULL) {
  16.  
  17. t = (tree*)malloc(sizeof(tree));
  18. t->data = value;
  19. t->left = NULL;
  20. t->right = NULL;
  21.  
  22. } else if(value < t->data) {
  23.  
  24. t->left = insert(value, t->left);
  25.  
  26. } else if(value >= t->data) {
  27.  
  28. t->right = insert(value, t->right);
  29. }
  30.  
  31. return t;
  32. }
  33.  
  34. void inorder(tree *t) {
  35.  
  36. if(t != NULL)
  37. inorder(t->left),
  38. printf("%d ", t->data),
  39. inorder(t->right);
  40. }
  41.  
  42. int main(int argc, char const *argv[]) {
  43.  
  44. //freopen(FIN, "r", stdin);
  45.  
  46. //freopen(FOUT, "w", stdout);
  47.  
  48. tree *root = NULL;
  49.  
  50. int n, x;
  51.  
  52. scanf("%d", &n);
  53.  
  54. for(int i = 0; i < n; ++i)
  55.  
  56. scanf("%d", &x),
  57.  
  58. root = insert( x, root );
  59.  
  60. inorder( root );
  61.  
  62. return 0;
  63. }
Success #stdin #stdout 0s 5348KB
stdin
10
9 8 7 6 5 4 3 2 1 0
stdout
0 1 2 3 4 5 6 7 8 9