Convert Ternary Expression to Binary Tree
Given a string that contains ternary expressions. The expressions may be nested. You need to convert the given ternary expression to a binary Tree and return the root.
Input: a?b:c Output: a / \ b c Input: a?b?c:d:e Output: a / \ b e / \ c d
Ternary expression: In C, we are acquainted with the ternary expression. Ternary expressions are equivalent to the if-else statement in C.
Similarly, a ternary expression can be converted to a binary tree, where a will be the root, b will be the left child and c will be the right one. This small miniature can be expanded (followed) for nesting ternary expression.
The algorithm for constructing the binary tree from the ternary expression is:
Algorithm with example: