import
java.util.*;
public
class
Main
{
static
class
Node {
public
char
data;
public
Node left, right;
public
Node(
char
data)
{
this
.data = data;
left = right =
null
;
}
}
static
Node createNewNode(
char
data)
{
Node node =
new
Node(data);
return
node;
}
static
void
preorder(Node root)
{
if
(root ==
null
)
return
;
System.out.print(root.data +
" "
);
preorder(root.left);
preorder(root.right);
}
static
Node convertExpression(String str)
{
Stack<Node> s =
new
Stack<Node>();
for
(
int
i = str.length() -
1
; i >=
0
😉 {
if
((i == str.length() -
1
)
|| (i !=
0
&& ((str.charAt(i -
1
) ==
':'
&& str.charAt(i +
1
) ==
':'
)
|| (str.charAt(i -
1
) ==
'?'
&& str.charAt(i +
1
) ==
':'
)))) {
s.push(createNewNode(str.charAt(i)));
}
else
{
Node lnode = (Node)s.peek();
s.pop();
Node rnode = (Node)s.peek();
s.pop();
Node node = createNewNode(str.charAt(i));
node.left = lnode;
node.right = rnode;
s.push(node);
}
i -=
2
;
}
return
(Node)s.peek();
}
public
static
void
main(String[] args)
{
String str =
"a?b?c:d:e"
;
Node root = convertExpression(str);
preorder(root);
}
}