Evaluation of postfix expression

16,092 views 39 slides Feb 05, 2014
Slide 1
Slide 1 of 39
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39

About This Presentation

this is just to explain how expression is converted from postfix to infix in data structure


Slide Content

DATA STRUCTURES USING ‘C’

Evaluation Of Postfix

Evaluation Of Postfix -BY AKHIL AHUJA

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)

Precedence of operators: 1. ‘$’ or ‘^’ Highest Precedence 2. ‘*’ and ‘/’ High Precedence 3. ’+’ and ‘-’ Low Precedence 4. ‘=‘ Lower Precedence 5. ‘(‘ and ‘)’ Lowest Precedence

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 ‘)’

  (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.

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. E valuate expression formed by two operand and the operator. Push the result of expression in the stack end . 4.If n o more elements then: Pop the result Else Goto step 1.

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))

Here’s an another example

4 3 * 6 7 + 5 - + )

4 3 * 6 7 + 5 - + ) STACK Push the opening bracket ‘(‘ into the stack . (

4 3 * 6 7 + 5 - + ) STACK ( Operand ‘4’ push it into the stack 4

4 3 * 6 7 + 5 - + ) STACK ( 4 Operand ‘3’ push it into the stack. 3

4 3 * 6 7 + 5 - + ) STACK ( 4 3 * Now comes Operator ‘*’

4 3 * 6 7 + 5 - + ) STACK ( 4 3 * Pop operand ‘3’ and ‘4’

4 3 * 6 7 + 5 - + ) STACK ( 4 3 * Evaluate the expression 4*3 =12 Pus it into the stack

4 3 * 6 7 + 5 - + ) STACK ( 4 3 * Evaluate the expression 4*3 =12 12

4 3 * 6 7 + 5 - + ) STACK ( 4 3 * Push it into the stack 12

4 3 * 6 7 + 5 - + ) STACK ( 12 Operand ‘6’ push it into the stack 6

4 3 * 6 7 + 5 - + ) STACK ( 12 Operand ‘7’ push it into the stack 6 7

4 3 * 6 7 + 5 - + ) STACK ( 12 Operand ‘+’ push it into the stack 6 7 + Operand ‘7’ and ‘6’ Are popped

4 3 * 6 7 + 5 - + ) STACK ( 12 6 7 + Evaluate ‘6+7’ 13 =13

4 3 * 6 7 + 5 - + ) STACK ( 12 13 Push it into the stack

4 3 * 6 7 + 5 - + ) STACK ( 12 13 5 Operand ‘5’ push it into the stack

4 3 * 6 7 + 5 - + ) STACK ( 12 13 5 Now operator ‘-’ occurs - Operands ‘5’ and ‘13’ are popped

4 3 * 6 7 + 5 - + ) STACK ( 12 Evaluating “13-5” We get ‘8’ 8

4 3 * 6 7 + 5 - + ) STACK ( 12 Now, push the resultant value i.e ‘8’ in the stack 8

4 3 * 6 7 + 5 - + ) STACK ( 12 8 Operator ‘+’ +

4 3 * 6 7 + 5 - + ) STACK ( 12 8 + By Evaluating , we get 20 20

4 3 * 6 7 + 5 - + ) STACK ( So push ‘20’ in stack 20

4 3 * 6 7 + 5 - + ) STACK ( 20 Now atlast ‘)’ occurs ) Now , pop the element till the opening bracket

4 3 * 6 7 + 5 - + ) STACK ( 20 )

4 3 * 6 7 + 5 - + ) STACK 20 Now push the element () AND THE FINAL ANSWRER IS ’20’

THANK YOU

THANK YOU

THANK YOU

THANK YOU

THANK YOU
Tags