Language: C++

#include<iostream>
#include<vector>
#include<string>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)
#define pb         push_back

class Tree{
public:
  Tree *l,*r;
  Tree *rsib,*lsib;
  char c;
  int x,y;
  bool isright;
  bool vis;
  Tree():l(NULL),r(NULL),rsib(NULL),lsib(NULL),vis(false){;}
};

//parsig
Tree *parse(string &in,int &p,bool isleft);
void makepos(Tree *in,int depth);

//make left tree
void dfs(Tree *now,vector<Tree*> & isvis,int depth);
void moveleft(int height,int x);
void moveleft_check(Tree *now,int depth);

//debug
void travarse(Tree *in);
void output();


const int R = 200,C=3000;
char m[R][C];
Tree* data[R][C];
vector<Tree*> node[R];


 
 void find_rl_sib(){
  rep(i,R){
    Tree *prev;
    prev=NULL;
    rep(j,C){
      if(data[i][j] != NULL){
	if (prev != NULL){
	  prev->rsib=data[i][j];
	  data[i][j]->lsib=prev;
	}
	prev = data[i][j];
      }
    }
  }
}


void moveright(int height,int x){
  bool eraseflag=false;
  if (x == 0)x =1,eraseflag=true;;
  for(int j=C-1;j>=x;j--){
    m[height+0][j]=m[height][j-1];
    if (height)m[height-1][j]=m[height-1][j-1];
  }
  
  if (eraseflag){
    m[height+0][0]=' ';
    if (height)m[height-1][0]=' ';
  }
}

void moveright_check(Tree *now,int depth){
  int xfrom=now->x;
  vector<Tree*> isvis;
  Tree *tmp,*prev;
  tmp=now->lsib;
  prev=now;
  while(tmp != NULL){
    if (tmp->l != NULL)break;
    if (tmp->isright){
      ;
    }else {
      xfrom = tmp->x;
    }  
    prev=tmp;
    tmp=tmp->lsib;
    if (tmp == NULL)break;
  }

  tmp = prev;
  while(tmp != NULL){
    isvis.pb(tmp);
    tmp=tmp->rsib;
  }

  if (isvis.size() == 0)isvis.pb(now);
  
  moveright(depth,xfrom);

  rep(i,isvis.size()){
    isvis[i]->x++;
  }
}

void solve(){
  for(int i=R-1;i>=0;i--){
    rep(j,node[i].size()){
      Tree *now;
      now =node[i][j];
      //output();
      if (now->l == NULL){//leaf
	while(m[i+1][now->x] == '-'){
	  moveleft_check(now,i);
	  //output();
	}
      }else if(now->r == NULL){//unary
	if (now->l->x == now->x);//nomove
	else if (now->l->x < now->x){//child left move
	  while(now->l->x < now->x){
	    moveleft_check(now,i);
	  }
	}else if (now->l->x > now->x){//now right move
	  while(now->l->x   > now->x){
	    moveright_check(now,i);
	    //output();
	  }	  
	}
      }else {//binary
	int mypos = now->r->x - now->l->x +1;
	if (mypos%2 == 0)mypos=now->l->x + mypos/2-1;
	else mypos=now->l->x + mypos/2;

	
	if (mypos == now->x){//nomove
	  ;
	}else if (mypos < now->x){//child left move
	  while(mypos < now->x)moveleft_check(now,i);
	}else if (mypos > now->x){//now right move;
	  while(mypos   > now->x){
	    moveright_check(now,i);
	    //output();
	  }
	}
      }
    }
  }
}

void output_tree(){
  for(int i=R-1;i>=0;i--){
    int r = 0;
    rep(j,C){
      if (m[i][j] != ' ')r=j+1;
    }
    if (r ==0)continue;

    rep(j,r){
      cout << m[i][j];
    }
    cout << endl;
  }
}

void destoroy(Tree *in){
  if (in->l != NULL)destoroy(in->l);
  if (in->r != NULL)destoroy(in->r);
  free(in);
}

main(){
  string in;
  int tc = 1;
  while(getline(cin,in) && in != "."){
    fill(&m[0][0],&m[R][0],' ');
    rep(i,R)rep(j,C)data[i][j]=NULL;
    rep(i,R)node[i].clear();
    int p = 0;
    Tree *root = parse(in,p,false);
    
    makepos(root,0);

    find_rl_sib();

    //travarse(root);
    


    //cout <<"Before"<<endl<<endl;
    //    output();
    solve();
    //cout <<"after" << endl<<endl;
    
    cout << tc++<<":"<<endl;
    output_tree();
    destoroy(root);
    //cout << "end" << endl<<endl;

  }
}


//parsing
Tree* parse(string &in,int &p,bool isright){
  Tree *now;
  now = new Tree();
  now->c = in[p];
  now->isright=isright;

  p++;
  if (p < in.size() && in[p] != '('){//leaf
    ;
  }else{//
    p++;
    now->l = parse(in,p,false);
    if (in[p] == ')'){
    }else if (in[p] == ','){
      p++;
      int tmp = 0;
      now->r=parse(in,p,true);
    }
    p++;
  }
  return now;
}

//parsing
void makepos(Tree *in,int depth){
  node[depth].push_back(in);
  int fpos=-2;
  rep(i,C)if (m[depth][i] != ' ')fpos=i;
  in->x=fpos+2;
  in->y=depth;
  m[depth][fpos+2]=in->c;
  data[depth][fpos+2]=in;
  if (depth!=0)m[depth-1][fpos+2]='-';
  if (in->isright){
    m[depth-1][fpos+1]='-';
  }
  if (in->l != NULL)makepos(in->l,depth+2);
  if (in->r != NULL)makepos(in->r,depth+2);
}


//moveleft
int movepos[R];
void dfs(Tree *now,vector<Tree*> & isvis,int depth){
  if (now->vis)return;
  now->vis=true;
  isvis.pb(now);
  movepos[depth]=min(now->x,movepos[depth]);
  if (now->l    != NULL)dfs(now->l,isvis,depth+2);
  if (now->r    != NULL)dfs(now->r,isvis,depth+2);
  if (now->rsib != NULL)dfs(now->rsib,isvis,depth);
}
//moveleft
void moveleft(int height,int x){
  for(int j=C-1;j>x;j--){
    m[height+0][j]=m[height][j-1];
    if (height)m[height-1][j]=m[height-1][j-1];
  }
  m[height][x]  =' ';
  if (height)m[height-1][x]=' ';
}
//moveleft
void moveleft_check(Tree *now,int depth){
  rep(i,R)movepos[i]=C;
  vector<Tree*> isvis;
  dfs(now,isvis,depth);

  rep(i,R){
    if (movepos[i] == C||i == depth)continue;
    moveleft(i,movepos[i]);
  }
  rep(i,isvis.size()){
    isvis[i]->vis=false;
    if (isvis[i]->y != depth)isvis[i]->x++;
  }
}


//debug
void travarse(Tree *in){
  cout << in->c <<" ";
  if (in->l != NULL)cout << " l: "<<in->l->c;
  if (in->r != NULL)cout << " r: "<<in->r->c;
  if (in->rsib != NULL)cout << " rsib: "<< in->rsib->c;
  if (in->lsib != NULL)cout << " lsib: "<< in->lsib->c;
  cout << endl;
  if (in->l != NULL)travarse(in->l);
  if (in->r != NULL)travarse(in->r);
}

//debug
void output(){
  static int H = 15;
    rep(i,H){
      rep(j,60){
	cout << m[H-1-i][j];
      }
      cout << endl;
    }
}

Compiler output:
a.cc: In function 'void find_rl_sib()':
a.cc:46:10: error: reference to 'data' is ambiguous
   46 |       if(data[i][j] != NULL){
      |          ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:36:7: note:                 'Tree* data [200][3000]'
   36 | Tree* data[R][C];
      |       ^~~~
a.cc:48:22: error: reference to 'data' is ambiguous
   48 |           prev->rsib=data[i][j];
      |                      ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:36:7: note:                 'Tree* data [200][3000]'
   36 | Tree* data[R][C];
      |       ^~~~
a.cc:49:11: error: reference to 'data' is ambiguous
   49 |           data[i][j]->lsib=prev;
      |           ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:36:7: note:                 'Tree* data [200][3000]'
   36 | Tree* data[R][C];
      |       ^~~~
a.cc:51:16: error: reference to 'data' is ambiguous
   51 |         prev = data[i][j];
      |                ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:36:7: note:                 'Tree* data [200][3000]'
   36 | Tree* data[R][C];
      |       ^~~~
a.cc: At global scope:
a.cc:170:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  170 | main(){
      | ^~~~
a.cc: In function 'int main()':
a.cc:175:21: error: reference to 'data' is ambiguous
  175 |     rep(i,R)rep(j,C)data[i][j]=NULL;
      |                     ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:36:7: note:                 'Tree* data [200][3000]'
   36 | Tree* data[R][C];
      |       ^~~~
a.cc: In function 'void makepos(Tree*, int)':
a.cc:234:3: error: reference to 'data' is ambiguous
  234 |   data[depth][fpos+2]=in;
      |   ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:36:7: note:                 'Tree* data [200][3000]'
   36 | Tree* data[R][C];
      |       ^~~~