#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct NODE
{
  int val;
  struct NODE *left;
  struct NODE *right;
};
struct NODE *root = NULL;

void insert(int val)
{
  struct NODE *new_node;
  struct NODE *p;

  new_node = malloc(sizeof(struct NODE));
  new_node->val = val;
  new_node->left = new_node->right = NULL;

  if(root == NULL){
    root = new_node;
  }
  else{     
    p = root;
    while(p != NULL){
      if(val < p->val){
        if(p->left == NULL){
          p->left = new_node;
          break;
        }
        p = p->left;
      }
      else{
        if(p->right == NULL){
          p->right = new_node;
          break;
        }
        p = p->right;
      }
    }
  }
}

void print(struct NODE *n)
{
  if(n == NULL){
    return;
  }
  print(n->left);
  printf("%d ", n->val);   
  print(n->right);
}

void insertValues(FILE *f)
{
  char c[10];

  while (fgets(c, 10, f) != NULL) {
    insert(atoi(c));
  }
}

int main(void)
{
  FILE *f;  

  f = fopen("numbers.txt", "r");
  insertValues(f);
  fclose(f);
  
  print(root);
  printf("\n");
  return 0;
}
