CamScanner 05-12-2020 12.38.29

You might also like

Download as pdf
Download as pdf
You are on page 1of 6
Postfix Expression Evaluation A postfix expression is a collection of operators and operands in which the operator is placed after the operands. That means, in a postfix expression the operator follows the operands. Postfix Expression has following general structure... Example Operand1 Wy Operator ‘Seanneé wih CamScanet Postfix ression Evaluation using Stack Data Structure A postfix expression can be evaluated using the Stack data structure. To evaluate a postfix expression using Stack data structure we can use the following steps... 1. Read all the symbols one by one from left to right in the given Postfix Expression 2. If the reading symbol Is operand, then push it on to the Stack. 3. If the reading symbol Is operator (+ ,-, * , / etc.,), then perform TWO pop operations and store the two popped oparands in two different variables (operand1 and operand2). Then perform reading symbol operation using operandi and operand2 and push result back on to the Stack. 4. Finally! perform a pop operation and display the popped value as final result. Example Consider the following Expression... Ad covered content ‘Ad was inappropriate Not interested in this ad ‘Scanned wth CamScanet Infix Expression (5 + 3) * (8-2) Postfix Expression 5 3 + 82 - * ‘Above Postfix Expression can be evaluated by using Stack Data Structure as follows Evaluated of Expression H i Nothing 5, push(5) H Nothing 3 push(3) F Nothing aluel = pop) man tones jalue2 = pop() | ramult 5 68 + result = value2 + value!| i Post 8) push(result) i (5 + 3) 8 —_push(@) 643) 2 i push(2) = i (5 + 3) Ls] i valuel = pop() _ | value2 = pop() i result = value2 = valued push(result) | (8-2) 6 +3), (8-2) ‘Scanned wth CamScanet 2 push(2) i valuel = pop() : value2 = pop( ; result = value2 - valuel : push(result) i valuel = pop() * : value2 = pop() ; result = value2 * valuel push(result) result = pop() End of Expression i : Push 6) | (8 - 2) (5 + 3), (8-2) valuel = popO: // 6 5 value? = popd: //8 result = 8+ 6 fam ; Push( 48) | (6*8) (5 +3)* (8-2) Display (resuit) 48 As final result Infix Expression (5 + 3) * (8-2) = 48 Postfix Expression § 3 + 8 2-* value is 48 ‘Seanneé wih CamScanet Stack | Set 4 (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. 1) Create a stack to store operands (or values). 2) Scan the given expression and do following for every scanned element. sate a) If the element is a number, push it into the stack sana 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 3) When the expression is ended, the number in the stack is the final answer ‘Seanneé wih CamScanet Postfix notation is when we use operator after the operands. Let's discuss the advantages are- 1. Any formula can be expressed without parenthesis. N It is very convenient for evaluating formulas on computer with stacks. wo Postfix expression doesn't has the operator precedence. 4. Postfix is slightly easier to evaluate. a . It reflects the order in which operations are performed. a . You need to worry about the left and right associativity. ‘Seanneé wih CamScanet

You might also like