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

#define VERBOSE
#define INDAMT 2

/*
   sample tree building and printing 
   by recursion.

   Burt Rosenberg (c) 23 Oct. 1994
*/

struct tree {
   char data ;
   struct tree * left, * right, * parent ; } ;

void print_tree_aux( struct tree * t, int indent )
{
   int i ;
   for (i=0;i<indent;i++) printf(" ") ;
   if (t==NULL) printf("-\n") ;
   else {
       printf("%c\n", t->data ) ;
       print_tree_aux( t->left, indent+INDAMT ) ;
       print_tree_aux( t->right,indent+INDAMT ) ;
   }
}

void print_tree( struct tree * t ) 
{
   print_tree_aux( t, 0 ) ;
}

static char * s_global ;

struct tree * build_tree_aux( void )
{
   struct tree * t ;
#ifdef VERBOSE
   printf("build_tree_aux: entered, looking at %c\n",*s_global) ;
#endif
   if (*s_global=='.') {
      s_global++ ;
      return NULL ;
   }
   else {
#ifdef VERBOSE
      int ctemp = *s_global ;
      printf("build_tree_aux: building %c\n",ctemp) ;
#endif
      t = (struct tree *) malloc(sizeof(struct tree)) ;
      t->data = *(s_global++) ;
#ifdef VERBOSE
      printf("build_tree_aux: going left\n") ;
#endif
      t->left = build_tree_aux() ;
#ifdef VERBOSE
      printf("build_tree_aux: return to node %c\n",ctemp) ;
      printf("build_tree_aux: going right\n") ;
#endif
      t->right = build_tree_aux() ;
#ifdef VERBOSE
      printf("build_tree_aux: returned to node %c\n",ctemp) ;
#endif
      return t ;
   }
}

struct tree * build_tree( char * s )
{
   s_global = s ;
   return build_tree_aux() ;
}

main(){
   struct tree * root ;
   char * s ;

   s = "@EI...T.." ;
   root = build_tree(s) ;
   print_tree( root ) ;

}

