Python Algorithm – Evaluation of Postfix Expression:

The Postfix notation is used to represent algebraic expressions. The expressions written in postfix form are evaluated faster compared to infix notation as parenthesis are not required in postfix. We have discussed infix to postfix conversion. In this post, evaluation of postfix expressions is discussed.

Following is algorithm for evaluation postfix expressions:

Step 1: Create a stack to store operands (or values).
Step 2: Scan the given expression and do following for every scanned element.
…..a) If the element is a number, push it into the stack
…..b) If the element is a operator, pop operands for the operator from stack. Evaluate the operator and push the result back to the stack
Step 3: When the expression is ended, the number in the stack is the final answer

Example:

Let the given expression be “2 3 1 * + 9 –“. We scan all elements one by one.
1) Scan ‘2’, it’s a number, so push it to stack. Stack contains ‘2’
2) Scan ‘3’, again a number, push it to stack, stack now contains ‘2 3’ (from bottom to top)
3) Scan ‘1’, again a number, push it to stack, stack now contains ‘2 3 1’
4) Scan ‘*’, it’s an operator, pop two operands from stack, apply the * operator on operands, we get 3*1 which results in 3. We push the result ‘3’ to stack. Stack now becomes ‘2 3’.
5) Scan ‘+’, it’s an operator, pop two operands from stack, apply the + operator on operands, we get 3 + 2 which results in 5. We push the result ‘5’ to stack. Stack now becomes ‘5’.
6) Scan ‘9’, it’s a number, we push it to the stack. Stack now becomes ‘5 9’.
7) Scan ‘-‘, it’s an operator, pop two operands from stack, apply the – operator on operands, we get 5 – 9 which results in -4. We push the result ‘-4’ to stack. Stack now becomes ‘-4’.
8) There are no more elements to scan, we return the top element from stack (which is the only element left in stack).

[ad type=”banner”]

Following is Python implementation of above algorithm.

Python Programming:

[pastacode lang=”python” manual=”%23%20Python%20program%20to%20evaluate%20value%20of%20a%20postfix%20expression%0A%20%0A%23%20Class%20to%20convert%20the%20expression%0Aclass%20Evaluate%3A%0A%20%20%20%20%20%0A%20%20%20%20%23%20Constructor%20to%20initialize%20the%20class%20variables%0A%20%20%20%20def%20__init__(self%2C%20capacity)%3A%0A%20%20%20%20%20%20%20%20self.top%20%3D%20-1%0A%20%20%20%20%20%20%20%20self.capacity%20%3D%20capacity%0A%20%20%20%20%20%20%20%20%23%20This%20array%20is%20used%20a%20stack%20%0A%20%20%20%20%20%20%20%20self.array%20%3D%20%5B%5D%0A%20%20%20%20%20%0A%20%20%20%20%23%20check%20if%20the%20stack%20is%20empty%0A%20%20%20%20def%20isEmpty(self)%3A%0A%20%20%20%20%20%20%20%20return%20True%20if%20self.top%20%3D%3D%20-1%20else%20False%0A%20%20%20%20%20%0A%20%20%20%20%23%20Return%20the%20value%20of%20the%20top%20of%20the%20stack%0A%20%20%20%20def%20peek(self)%3A%0A%20%20%20%20%20%20%20%20return%20self.array%5B-1%5D%0A%20%20%20%20%20%0A%20%20%20%20%23%20Pop%20the%20element%20from%20the%20stack%0A%20%20%20%20def%20pop(self)%3A%0A%20%20%20%20%20%20%20%20if%20not%20self.isEmpty()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self.top%20-%3D%201%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20self.array.pop()%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20%22%24%22%0A%20%20%20%20%20%0A%20%20%20%20%23%20Push%20the%20element%20to%20the%20stack%0A%20%20%20%20def%20push(self%2C%20op)%3A%0A%20%20%20%20%20%20%20%20self.top%20%2B%3D%201%0A%20%20%20%20%20%20%20%20self.array.append(op)%20%0A%20%0A%20%0A%20%20%20%20%23%20The%20main%20function%20that%20converts%20given%20infix%20expression%0A%20%20%20%20%23%20to%20postfix%20expression%0A%20%20%20%20def%20evaluatePostfix(self%2C%20exp)%3A%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%23%20Iterate%20over%20the%20expression%20for%20conversion%0A%20%20%20%20%20%20%20%20for%20i%20in%20exp%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20If%20the%20scanned%20character%20is%20an%20operand%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20(number%20here)%20push%20it%20to%20the%20stack%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20i.isdigit()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20self.push(i)%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20If%20the%20scanned%20character%20is%20an%20operator%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20pop%20two%20elements%20from%20stack%20and%20apply%20it.%0A%20%20%20%20%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20val1%20%3D%20self.pop()%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20val2%20%3D%20self.pop()%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20self.push(str(eval(val2%20%2B%20i%20%2B%20val1)))%0A%20%0A%20%20%20%20%20%20%20%20return%20int(self.pop())%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%23%20Driver%20program%20to%20test%20above%20function%0Aexp%20%3D%20%22231*%2B9-%22%0Aobj%20%3D%20Evaluate(len(exp))%0Aprint%20%22Value%20of%20%25s%20is%20%25d%22%20%25(exp%2C%20obj.evaluatePostfix(exp))%0A%20%0A%23%20This%20code%20is%20contributed%20by%20Nikhil%20Kumar%20Singh(nickzuck_007)” message=”” highlight=”” provider=”manual”/]

Output:

Value of 231*+9- is -4

Time complexity of evaluation algorithm is O(n) where n is number of characters in input expression.

There are following limitations of above implementation:

1) It supports only 4 binary operators+’, ‘*’, ‘-‘ and ‘/’. It can be extended for more operators by adding more switch cases.
2) The allowed operands are only single digit operands. The program can be extended for multiple digits by adding a separator like space between all elements (operators and operands) of given expression.

[ad type=”banner”]