Evaluate an expression represented by a String. The expression can contain parentheses, you can assume parentheses are well-matched. For simplicity, you can assume only binary operations allowed are +, -, *, and /. Arithmetic Expressions can be written in one of three forms:
Infix Notation: Operators are written between the operands they operate on, e.g. 3 + 4.
Prefix Notation: Operators are written before the operands, e.g + 3 4
Postfix Notation: Operators are written after operands.
Infix Expressions are harder for Computers to evaluate because of the additional work needed to decide precedence. Infix notation is how expressions are written and recognized by humans and, generally, input to programs. Given that they are harder to evaluate, they are generally converted to one of the two remaining forms. A very well known algorithm for converting an infix notation to a postfix notation is Shunting Yard Algorithm by Edgar Dijkstra. This algorithm takes as input an Infix Expression and produces a queue that has this expression converted to postfix notation. The same algorithm can be modified so that it outputs the result of the evaluation of expression instead of a queue. The trick is using two stacks instead of one, one for operands, and one for operators. The algorithm was described succinctly on http://www.cis.upenn.edu/ matuszek/cit594-2002/Assignments/5-expressions.htm, and is reproduced here. (Note that credit for succinctness goes to the author of said page)
1. While there are still tokens to be read in, 1.1 Get the next token. 1.2 If the token is: 1.2.1 A number: push it onto the value stack. 1.2.2 A variable: get its value, and push onto the value stack. 1.2.3 A left parenthesis: push it onto the operator stack. 1.2.4 A right parenthesis: 1 While the thing on top of the operator stack is not a left parenthesis, 1 Pop the operator from the operator stack. 2 Pop the value stack twice, getting two operands. 3 Apply the operator to the operands, in the correct order. 4 Push the result onto the value stack. 2 Pop the left parenthesis from the operator stack, and discard it. 1.2.5 An operator (call it thisOp): 1 While the operator stack is not empty, and the top thing on the operator stack has the same or greater precedence as thisOp, 1 Pop the operator from the operator stack. 2 Pop the value stack twice, getting two operands. 3 Apply the operator to the operands, in the correct order. 4 Push the result onto the value stack. 2 Push thisOp onto the operator stack. 2. While the operator stack is not empty, 1 Pop the operator from the operator stack. 2 Pop the value stack twice, getting two operands. 3 Apply the operator to the operands, in the correct order. 4 Push the result onto the value stack. 3. At this point the operator stack should be empty, and the value stack should have only one value in it, which is the final result.
It should be clear that this algorithm runs in linear time – each number or operator is pushed onto and popped from Stack only once. Also see http://www2.lawrence.edu/fast/GREGGJ/CMSC270/Infix.html,
Following is the implementation of above algorithm:
22 212 1400 100
Time Complexity: O(n)
Space Complexity: O(n)
See this for a sample run with more test cases.
This article is compiled by Ciphe. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Stack | Set 4 (Evaluation of Postfix Expression)
- Horner's Method for Polynomial Evaluation
- Evaluation of Prefix Expressions
- Evaluation of Risk in Investments
- Find all possible outcomes of a given expression
- Minimum number of bracket reversals needed to make an expression balanced
- Find if an expression has duplicate parenthesis or not
- Find index of closing bracket for a given opening bracket in an expression
- Identify and mark unmatched parenthesis in an expression
- Expression contains redundant bracket or not
- Arithmetic Expression Evalution
- Maximum and Minimum Values of an Algebraic Expression
- Balanced expression with replacement
- Check for balanced parentheses in an expression | O(1) space | O(N^2) time complexity
- Find the missing digit x from the given expression
- Deriving the expression of Fibonacci Numbers in terms of golden ratio
- Solve the Logical Expression given by string
- Program to evaluate the expression (√X+1)^6 + (√X-1)^6
- Find a permutation of 2N numbers such that the result of given expression is exactly 2K
- Print the balanced bracket expression using given brackets