Home »
Interview coding problems/challenges
Convert Ternary Expression to Binary Tree
In this article, we are going to see how to convert the ternary expression to a binary tree? This problem has been featured in Facebook interview.
Submitted by Radib Kar, on March 02, 2019 [Last updated : March 20, 2023]
Problem Description
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.
Example
Input:
a?b:c
Output:
a
/ \
b c
Input:
a?b?c:d:e
Output:
a
/ \
b e
/ \
c d
Solution of Convert Ternary Expression to Binary Tree
Ternary expression: In C, we are acquainted with the ternary expression. Ternary expressions are equivalent to the if-else statement in C.
if(a)
b
else
c
a,b,c=statements/expressions
This if else statement is equivalent to a?b: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:
Pre-requisite:
- Ternary expression str
- Node* newNode(string str, index i) : Creates a new node with data value str[i]
Algorithm
//recursive function to build the tree
FUNCTION convertExpression(string str, int& i)
1. Create root referring to the current character(str[i]);
root =newNode(str,i); //create a node with element str[i]
2. Increment i (index that point to current character);
//if i<string length and current token is '?' increment i
3. IF(i<str.length() && str[i]=='?'){
//need to build left child recursively increment i
root->left=convertExpression(str,i);
//need to build right child recursively
root->right=convertExpression(str,i);
4. return root;
Algorithm with example
Tree nodes represented by their values only
Input string (ternary expression)
a?b:c
-------------------------------------------------
In main function we call convertExpression(str,0)
convertExpression(str,0):
root=newNode(str, 0); //root=a
i=1; //incremented
i<str.length() && str[i]=='?'
i=2;//incremented
root->left=convertExpression(str,2);
-------------------------------------------------
convertExpression(str,2):
root=newNode(str, 2); //root=b
i=3; //incremented
i<str.length()&& str[i]!='?'
return root //b
at convertExpression(str,0)
now root->left=b
i=4; //i++ step evaluated
root->right=convertExpression(str,4)
-------------------------------------------------
convertExpression(str,4):
root=newNode(str, 4); //root=c
i=5; //incremented
I is not <str.length()
return root //c
at convertExpression(str,0)
now root->right=c
return root;
returns to main
built tree:
a
/ \
b c
C++ implementation of Convert Ternary Expression to Binary Tree
#include <bits/stdc++.h>
using namespace std;
struct Node {
char data;
Node *left, *right;
};
//function to create node
Node* newNode(char Data)
{
Node* new_node = new Node;
new_node->data = Data;
new_node->left = new_node->right = NULL;
return new_node;
}
//function to traverse preorder
void preorder(Node* root)
{
if (root == NULL)
return;
cout << root->data << " ";
preorder(root->left);
preorder(root->right);
}
//recursive function to build the tree
Node* convertExpression(string str, int& i)
{
Node* root = newNode(str[i]);
i++;
if (i < str.length() && str[i] == '?') {
i++;
root->left = convertExpression(str, i);
i++; //skipping ':' character
root->right = convertExpression(str, i);
}
return root;
}
int main()
{
string str;
cout << "Enter your expression\n";
cin >> str;
int i = 0;
Node* root = convertExpression(str, i);
cout << "Printing pre-order traversal of the tree...\n";
//pre-order traversal of the tree,
//should be same with expressionthe
preorder(root);
cout << endl;
return 0;
}
Output
First run:
Enter your expression
a?b:c
Printing pre-order traversal of the tree...
a b c
Second run:
Enter your expression
a?b?c:e:d
Printing pre-order traversal of the tree...
a b c e d