#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<vector>
#include<string.h>
#include<map>
#include<cmath>
#include<sstream>
#include<stack>
#include<queue>
#define ll long long
#define maxn 200005
#define ull unsigned long long
using namespace std;
typedef char ElemType;
typedef struct BiThrNode
{
ElemType data;
BiThrNode *LChild, *RChild;
int LTag,RTag;
}BiThrNode,*BiThrTree;
BiThrTree pre;
void InThreading_Thrt(BiThrTree T)
{
if(T)
{
InThreading_Thrt(T->LChild);
if(!T->LChild)
{
T->LTag=1;
T->LChild=pre;
}
if(!pre->RChild)
{
pre->RTag=1;
pre->RChild=T;
}
pre=T;
InThreading_Thrt(T->RChild);
}
}
int InThreading(BiThrTree *thrt,BiThrTree T)
{
if(!(*thrt=(BiThrTree)malloc(sizeof(BiThrNode)))) exit(-2);
(*thrt)->LTag=0;
(*thrt)->RTag=1;
(*thrt)->RChild=(*thrt);
if(!T) (*thrt)->LChild=(*thrt);
else {
(*thrt)->LChild=T;
pre=(*thrt);
InThreading_Thrt(T);
pre->RChild=(*thrt);
pre->RTag=1;
(*thrt)->RChild=pre;
}
return 1;
}
int InThreading_Traverse(BiThrTree T)
{
BiThrNode *p=T->LChild;
while(p!=T)
{
while(p->LTag==0) p=p->LChild;
cout<<p->data<<"("<<p->LTag<<","<<p->RTag<<")"<<" ";
while(p->RTag==1&&p->RChild!=T)
{
p=p->RChild;
cout<<p->data<<"("<<p->LTag<<","<<p->RTag<<")"<<" ";
}
p=p->RChild;
}
return 1;
}
int CreateBinaryTree(BiThrTree *B,ElemType empty){
ElemType ch;
cin>>ch;
if (ch ==empty ) *B = NULL;
else
{
if(!((*B)=(BiThrTree)malloc(sizeof(BiThrNode)))) exit(-2);
(*B)->data=ch;
(*B)->LTag=0;
(*B)->RTag=0;
CreateBinaryTree(&(*B)->LChild,empty);
CreateBinaryTree(&(*B)->RChild,empty);
}
return 1;
}
int main()
{
char str;
cin>>str;
BiThrTree root,ro;
CreateBinaryTree(&root,str);
InThreading(&ro,root);
InThreading_Traverse(ro);
return 0;
}