Evaluation of postfix expression
-
Upload
akhil-ahuja -
Category
Education
-
view
1.054 -
download
1
description
Transcript of Evaluation of postfix expression
![Page 1: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/1.jpg)
DATA STRUCTURES USING
‘C’
![Page 2: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/2.jpg)
Evaluation
Of
Postfix
![Page 3: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/3.jpg)
Evaluation
Of
Postfix
-BY AKHIL AHUJA
![Page 4: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/4.jpg)
What is Postfix expression?
Postfix expression is also known as Reverse Polish Notation(RPN). In RPN the operators follow their operands.
IF THE OPERATOR SYMBOLS ARE PLACED AFTER ITS OPERANDS , THEN THE EXPRESSION IS IN POSTFIX NOTATION.
Despite the name , Reverse Polish Notation is not exactly the reverse of polish notation , for the operands they are still written in conventional manner.
For eg : “*63”in polish notation and “63*” in reverse polish notation. In this , expression is different but the answer is same (i.e 18)
![Page 5: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/5.jpg)
Precedence of operators:
1. ‘$’ or ‘^’ Highest Precedence
2. ‘*’ and ‘/’
High Precedence
3. ’+’ and ‘-’
Low Precedence
4. ‘=‘ Lower Precedence
5. ‘(‘ and ‘)’
Lowest Precedence
![Page 6: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/6.jpg)
Fundamental principles followed while evaluating the postfix expression :1.Read the expression from left to right.
2.If there comes an operand , push it into the stack.3.If there comes an operator , pop operand 1 and operand 2 and then :
A . Push ‘(‘B . Push ‘operand 2’C . Push ‘operator’D . Push ‘operand 1’E . Push ‘)’
![Page 7: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/7.jpg)
(A). Postfix notation is easier to work with. In a postfix expression operators operands appear before the operators, there is no need of operator precedence and other rules. In postfix expression the topmost operands are popped off and operator is applied and the result is again pushed to the stack and thus finally the stack contains a single value at the end of the process.
Advantages of Postfix Expression:
(B). In postfix expression, there are no parentheses and therefore the order of evaluation will be determined by the positions of the operators and related operands in the expression.
![Page 8: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/8.jpg)
Algorithm for Postfix Expression :1.Read the element.
2.If element is an operand then:Push the element into the stack.
3.If element is an operator then :Pop two operands from the stack.Evaluate expression formed by two
operand and the operator.Push the result of expression in the stack
end.4.If no more elements then:Pop the resultElseGoto step 1.
![Page 9: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/9.jpg)
How to evaluate postfix (manually)
Going from left to right, if you see an operator, apply it to the previous two operands (numbers)
Example:
Equivalent infix: A+ B * C / D– (E– F)
A B C * D / + E F - -
(B*C)
((B*C)/D)
(A+(B*C)/D)
(E – F)
((A+(B*C)/D)-(E-F))
![Page 10: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/10.jpg)
Here’s an another example
![Page 11: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/11.jpg)
4 3 * 6 7 + 5 - + )
![Page 12: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/12.jpg)
4 3 * 6 7 + 5 - + )
STACK
Push the opening bracket ‘(‘ into the stack .
(
![Page 13: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/13.jpg)
4 3 * 6 7 + 5 - + )
STACK
( Operand ‘4’ push it into the stack
4
![Page 14: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/14.jpg)
4 3 * 6 7 + 5 - + )
STACK
(4 Operand ‘3’ push it into
the stack.
3
![Page 15: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/15.jpg)
4 3 * 6 7 + 5 - + )
STACK
(43
*
Now comes Operator ‘*’
![Page 16: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/16.jpg)
4 3 * 6 7 + 5 - + )
STACK
(
43
*
Pop operand ‘3’ and ‘4’
![Page 17: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/17.jpg)
4 3 * 6 7 + 5 - + )
STACK
(
4
3*
Evaluate the expression4*3 =12Pus it into the stack
![Page 18: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/18.jpg)
4 3 * 6 7 + 5 - + )
STACK
(
4
3*
Evaluate the expression4*3 =12
![Page 19: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/19.jpg)
4 3 * 6 7 + 5 - + )
STACK
(
4
3*
Push it into the stack
12
![Page 20: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/20.jpg)
4 3 * 6 7 + 5 - + )
STACK
(
12
Operand ‘6’ push it into the stack
6
![Page 21: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/21.jpg)
4 3 * 6 7 + 5 - + )
STACK
(
12Operand ‘7’ push it into the stack
6
7
![Page 22: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/22.jpg)
4 3 * 6 7 + 5 - + )
STACK
(
12Operand ‘+’ push it into the stack
67
+
Operand ‘7’ and ‘6’ Are popped
![Page 23: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/23.jpg)
4 3 * 6 7 + 5 - + )
STACK
(
12
6 7+
Evaluate ‘6+7’
=13
![Page 24: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/24.jpg)
4 3 * 6 7 + 5 - + )
STACK
(
12
13
Push it into the stack
![Page 25: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/25.jpg)
4 3 * 6 7 + 5 - + )
STACK
(
1213
5
Operand ‘5’ push it into the stack
![Page 26: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/26.jpg)
4 3 * 6 7 + 5 - + )
STACK
(
12135
Now operator ‘-’ occurs
-
Operands ‘5’ and ‘13’ are popped
![Page 27: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/27.jpg)
4 3 * 6 7 + 5 - + )
STACK
(
12
Evaluating “13-5”
We get ‘8’
![Page 28: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/28.jpg)
4 3 * 6 7 + 5 - + )
STACK
(
12Now, push the resultant value i.e ‘8’ in the stack
8
![Page 29: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/29.jpg)
4 3 * 6 7 + 5 - + )
STACK
(
128
Operator ‘+’
+
![Page 30: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/30.jpg)
4 3 * 6 7 + 5 - + )
STACK
(
12 8+
By Evaluating , we get 20
![Page 31: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/31.jpg)
4 3 * 6 7 + 5 - + )
STACK
(
So push ‘20’ in stack
20
![Page 32: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/32.jpg)
4 3 * 6 7 + 5 - + )
STACK
(
20Now atlast ‘)’ occurs
)
Now , pop the element till the opening bracket
![Page 33: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/33.jpg)
4 3 * 6 7 + 5 - + )
STACK(
20
)
![Page 34: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/34.jpg)
4 3 * 6 7 + 5 - + )
STACK
20
Now push the element
()AND THE FINAL ANSWRER IS ’20’
![Page 35: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/35.jpg)
THANK
YOU
![Page 36: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/36.jpg)
THANK
YOU
![Page 37: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/37.jpg)
THANK
YOU
![Page 38: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/38.jpg)
THANK
YOU
![Page 39: Evaluation of postfix expression](https://reader036.fdocuments.net/reader036/viewer/2022081416/55839452d8b42a282c8b5677/html5/thumbnails/39.jpg)
THANK
YOU