#include <stdio.h>

#include <malloc.h>

 

typedef struct nodetype *nodeptr;

 

typedef struct nodetype {

  int val;

  nodeptr lft, rgt;

} nodetype;

 

void insertNode(nodeptr curr, int val);

void print(nodeptr curr);

 

main()

{

  nodeptr node = NULL;

  int val;

 

  do {

    printf("Input a number: ");

    if (scanf("%d", &val) != 1) break;

    if (node == NULL) {

      node = (nodeptr)malloc(sizeof(nodetype));

      node->lft = node->rgt = NULL;

      node->val = val;

    } else {

      insertNode(node, val);

    }

  } while (1);

  print(node);

}

 

void insertNode(nodeptr curr, int val)

{

  nodeptr node;

 

  if (val > curr->val) {

    if (curr->rgt == NULL) {

      curr->rgt = node = (nodeptr)malloc(sizeof(nodetype));

      node->lft = node->rgt = NULL;

      node->val = val;

      return;

    }

    insertNode(curr->rgt, val);

  } else {

    if (curr->lft == NULL) {

      curr->lft = node = (nodeptr)malloc(sizeof(nodetype));

      node->lft = node->rgt = NULL;

      node->val = val;

      return;

    }

    insertNode(curr->lft, val);

  }

}

 

void print(nodeptr curr)

{

  if (curr->lft) print(curr->lft);

  printf("%d\n", curr->val);

  if (curr->rgt) print(curr->rgt);

}