/* tree syntax parser and display program */
/*
        tree := digit digits ( tree ) ( tree ) | * | <null>
        digit := 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
        digits := digit digits | <null>
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct tree tree;
typedef struct tree {
    unsigned int id;
    tree *left, *right;
} tree;
typedef struct {
    unsigned int width, height, pos;
    char grid[1];
} ascii_t;
int match( const char **s, char c ) {
    if ( s == 0 || *s == 0 || **s != c ) return 0;
    return ++*s, 1;
}
int digit( const char **s ) {
    return match( s, '0' ) || match( s, '1' ) || match( s, '2' ) ||
        match( s, '3' ) || match( s, '4' ) || match( s, '5' ) ||
        match( s, '6' ) || match( s, '7' ) || match( s, '8' ) ||
        match( s, '9' );
}
tree * create( const char **s ) {
    char c;
    unsigned int value;
    tree *node, *left, *right;
    if ( s == 0 || *s == 0 ) return 0;
    if ( match( s, '*' ) ) return 0;
    if ( c= **s, !digit( s ) ) return 0;
    for ( value= c - '0'; c= **s, digit( s ); ) value= 10*value + c - '0';
    if ( !match( s, '(' ) ) return 0;
    left= create( s );
    if ( !match( s, ')' ) || !match( s, '(' ) ) return 0;
    right= create( s );
    if ( !match( s, ')' ) ) return 0;
    node= (tree *) malloc( sizeof( tree ) );
    node->id= value;
    node->left= left;
    node->right= right;
    return node;
}
void destroy( tree * t ) {
    if ( t == 0 ) return;
    destroy( t->left );
    destroy( t->right );
    free( t );
    return;
}
void display( ascii_t *r ) {
    unsigned int i, j;
        for ( i= 0; i < r->height; ++i ) {
            for ( j= 0; j < r->width; ++j )
                putchar( r->grid[ i*r->width+j ] );
            putchar( '\n' );
        }
    return;
}
void sidedisplay( ascii_t *r ) {
    unsigned int i, j;
        for ( j= 0; j < r->width; ++j ) {
            for ( i= 0; i < r->height; ++i )
                putchar( r->grid[ i*r->width+j ] );
            putchar( '\n' );
        }
    return;
}
ascii_t * ascii( tree *t ) {
    char digits[10];
    ascii_t *result, *left, *right;
    unsigned int i, j, lh, rh, lw, rw, h, w, id, count;
    if ( t == 0 ) {
        result= (ascii_t *) malloc( sizeof( ascii_t ) );
        result->height= result->width= 1;
        result->pos= 0;
        result->grid[0]= '*';
        return result;
    }
    id= t->id;
    count= 0;
    do {
        digits[count++]= '0' + (id % 10);
        id /= 10;
    } while ( id > 0 );
    left= ascii( t->left );
    right= ascii( t->right );
    if ( left->width < 2 )
        lh= lw= 2;
    else {
        lh= (left->width + 1) >> 1;
        lh= left->width - left->pos;
        lw= left->width;
    }
    if ( right->width < 2 )
        rh= rw= 2;
    else {
        rh= (right->width + 1) >> 1;
        rh= right->pos + 1;
        rw= right->width;
    }
    w= 1 + lw + rw;
    h= lh + left->height >= rh + right->height? lh + left->height : rh + right->height;
    if ( count > h ) h= count;
    result= (ascii_t *) malloc( sizeof( ascii_t ) + w*h - 1 );
    result->height= h;
    result->width= w;
    result->pos= lw;
    for ( i= 0; i < w*h; ++i )
        result->grid[i]= ' ';
    for ( i= 0; i < count; ++i )
        result->grid[ i*w + lw ]= digits[count-i-1];
    for ( i= 0; i < left->height; ++i )
        for ( j= 0; j < left->width; ++j )
            result->grid[ (lh+i)*w+j ]= left->grid[ i*left->width+j ];
    for ( i= 0; i < right->height; ++i )
        for ( j= 0; j < right->width; ++j )
            result->grid[ (rh+i)*w+lw+1+j+(right->width < 2) ]= right->grid[ i*right->width+j ];
    for ( i= 1; i < lh; ++i )
        result->grid[ i*w+lw-i ]= '/';
    for ( i= 1; i < rh; ++i )
        result->grid[ i*w+lw+i ]= '\\';
    free( left );
    free( right );
    return result;
}
void print( tree * t, int tab ) {
    if ( t == 0 ) { printf( "%*s(%c)\n", tab, "", '*' ); return; }
    printf( "%*s%d\n", tab, "", t->id );
    print( t->left, tab+4 );
    print( t->right, tab+4 );
    return;
}
int main( int argc, char *argv[] ) {
    tree *mytree;
    char b[200], *p;
    p= fgets( b, sizeof(b), stdin );
    if ( p == 0 ) return 0;
    mytree= create( &p );
    if ( *p == '\0' || *p == '\n' ) {
        ascii_t *result= ascii( mytree );
        printf( "Downward tree form:\n" );
        display( result );
        printf( "\nSideways tree form:\n" );
        sidedisplay( result );
        printf( "\nTabbed tree form:\n" );
        print( mytree, 0 );
        free( result );
    } else
        printf( "Failed to parse correctly.\n" );
    destroy( mytree );
    return 0;
}