Build Binary Expression Tree From Infix Expression

Posted: 27 Mar, 2021
Difficulty: Moderate

PROBLEM STATEMENT

Try Problem

You are given a string ‘S’, an infix expression that has operands, operators and parentheses ‘(‘ and ‘)’.

You need to return the binary expression tree, whose inorder traversal is the same as ‘S’.

Note:

Infix expression: The expression of the form ‘a operator b’. When an operator is in-between every pair of operands.
The expression tree is a binary tree in which each internal node corresponds to the operator and each leaf node corresponds to the operand so for example expression tree for 5 * ( 6 - 3 ) / 2 - 8 would be:

Input Format:

The first line of input contains an integer 'T' representing the number of test cases.

The first line of each test case contains a string S representing infix expression.
Output Format :
For each test case, return the binary expression tree, whose inorder traversal is the same as 'S'. 

Return -1 instead of NULL.

The output for each test case is printed in a separate line.

Note:

You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 5
1 <= N <= 5000
Operands are only numbers between 0 and 9 (included).
It is guaranteed that ‘S’ is a valid infix expression.

Time limit: 1 sec
Approach 1

The idea is that we will use two stacks, one for operators and the other for nodes but before all this, we need to first set the priority of each operator. We can use a map to set priority.

After setting the priority we will iterate through all characters of the given string ‘STR’ and we will have 4 cases.

 

  • Case 1: When we encounter an open parenthesis ‘(‘. We just push this character in the operators stack and move on.
  • Case 2: When a digit is encountered then we create a new node and push that in the nodes stack.
  • Case 3: When a close parenthesis is encountered ‘)’ then we will start popping the elements from operators stack till we get an open parenthesis and on each pop, we will make a new node such that the right and left child of the node are top 2 nodes from node stack and push the new node back in the stack.
  • Case 4: When an operator(*, +, -, / ) is encountered. We will again form a new node as we did in the previous case till either operator is empty or the priority of the top element of operators is less than the priority of the current operator.

 

After this if stack size is still greater than 1 then we will make new nodes using operator and node stack till get size = 1 in the node stack.

 

Algorithm:

             map<char,int> priority;

             Set priority of ‘(‘ = 1, ‘-’ = 2, ‘+’ = 2,  ’*’ =3 , ‘/’ = 3     

             stack<char> operators;

             stack<binarytreenode> tree

             foreach(char c of  s){

                 Check for all above cases one by one.

             }

             while(node.size > 1)

             {

                 makeNode(operator, tree).

             }

            return tree.top().

Try Problem