Upload
others
View
3
Download
0
Embed Size (px)
Citation preview
FAST- National University of
Computer and Emerging Sciences
CFD Campus
Data Structures and Algorithm
Application of Stack
Asma Sattar
Lecture No. 7
Today’s Lecture
• Algebraic Expression
• Infix to Postfix
• Postfix Evaluation
• Infix to Prefix
• Prefix Evaluation
Algebraic Expression
• An algebraic expression is a legal combination of operands and the operators.
– Operand is the quantity on which a mathematical operation is performed.
– Operator is a symbol which signifies a mathematical operation
Infix, Postfix and Prefix Expressions
• INFIX: expressions in which operands surround the operator.
• POSTFIX: operator comes after the operands, also Known as Reverse Polish Notation (RPN).
• PREFIX: operator comes before the operands, also Known as Polish notation.
• Example
– Infix: A+B-C Postfix: AB+C- Prefix: +A-BC
Why do we need PREFIX/POSTFIX?
• Appearance may be misleading, INFIX notations are not as simple as they seem
• To evaluate an infix expression we need to consider
– Operators’ Priority – Associative property
Why do we need PREFIX/POSTFIX?
• Infix Expression Is Hard To Parse and difficult to evaluate.
• Postfix and prefix do not rely on operator priority and are easier to parse.
Why do we need PREFIX/POSTFIX?
• An expression in infix form is thus converted into prefix or postfix form and then evaluated without considering the operators priority and delimiters.
Examples of infix to prefix and post
fix
Infix PostFix Prefix
A+B AB+ +AB
(A+B) * (C + D) AB+CD+* *+AB+CD
Prefix example:
2+3*4-5/6^(2-1*3)
Prefix= -+2*34/5^6-2*13 (solve on board)
Prefix Task:
2*5^6/3
Prefix=/*2^563
Postfix example:
2+3*4-5/6^(2-1*3)
Postfix= 234*+56231*-^/-
Postfix Task:
f^(g-h)+(j-k)
Postfix= F g h - ^ j k -+
Algorithm Infix To Postfix
Assumptions:
1. Space delimited list of tokens represents a
infix expression
2. Operands are single characters.
3. Operators +,-,*,/ , ^
Algorithm for Infix to postfix
conversionAlgorithm: Q is the given infix expression & we want P.
1. Push “(“ on STACK and add “)” at the end of Q.
2. Scan Q from left to right and repeat steps 3 & 6 for each element of Q until the STACK is empty.
3. If an operand is encountered, add it to P
4. If a left parenthesis is encountered, push it onto STACK.
5. If an operator X is encountered, then:a. Repeatedly pop from STACK and add to P each operator which has same or higher
precedence than X
b. Push X on STACK
6. If a right parenthesis is encountered, then:a. Repeatedly pop from STACK and add to P each operator until a left parenthesis is
encountered
b. Remove the left parenthesis. [Do not add it to P]
7. Exit
Infix to Postfix
( ( A + B ) * ( C - E ) ) / ( F + G )
stack: output: []
Infix to Postfix
( ( A + B ) * ( C - E ) ) / ( F + G ) )
stack: (output: []
Infix to Postfix
( A + B ) * ( C - E ) ) / ( F + G ) )
stack: ( (output: []
Infix to Postfix
A + B ) * ( C - E ) ) / ( F + G ) )
stack: ( ( (output: []
Infix to Postfix
+ B ) * ( C - E ) ) / ( F + G ) )
stack: ( ( (output: [A]
Infix to Postfix
B ) * ( C - E ) ) / ( F + G ) )
stack: ( ( ( +output: [A]
Infix to Postfix
) * ( C - E ) ) / ( F + G ) )
stack: ( ( ( +output: [A B]
Infix to Postfix
* ( C - E ) ) / ( F + G ) )
stack: ( ( output: [A B + ]
Infix to Postfix
( C - E ) ) / ( F + G ) )
stack: ( ( * output: [A B + ]
Infix to Postfix
C - E ) ) / ( F + G ) )
stack: ( ( * (output: [A B + ]
Infix to Postfix
- E ) ) / ( F + G ) )
stack: ( ( * (output: [A B + C ]
Infix to Postfix
E ) ) / ( F + G ) )
stack: ( ( * ( -output: [A B + C ]
Infix to Postfix
) ) / ( F + G ) )
stack: ( ( * ( -output: [A B + C E ]
Infix to Postfix
) / ( F + G ) )
stack: ( ( *output: [A B + C E - ]
Infix to Postfix
/ ( F + G ) )
stack: ( output: [A B + C E - * ]
Infix to Postfix
( F + G ) )
stack: ( /output: [A B + C E - * ]
Infix to Postfix
F + G ) )
stack: ( / (output: [A B + C E - * ]
Infix to Postfix
+ G ) )
stack: ( / (output: [A B + C E - * F ]
Infix to Postfix
G ) )
stack: ( / ( +output: [A B + C E - * F ]
Infix to Postfix
) )
stack: ( / ( +output: [A B + C E - * F G ]
Infix to Postfix
)
stack: ( /output: [A B + C E - * F G + ]
Infix to Postfix
stack: output: [A B + C E - * F G + / ]
Task
infix
postfixt
( a + b - c ) * d – ( e + f )
Evaluating a postfix expression
Algorithm: P is the given postfix expression.
1. Add a right parenthesis “)” at the end of P. [acts as a sentinel]
2. Scan P from left to right and repeat steps 3 & 4 for each element of P until “)” is encountered.
3. If an operand is encountered, push it on STACK
4. If an operator is encountered, then:a. Pop two operands from STACK: A & B
b. Evaluate: B operator A
c. Push result on STACK
5. Set value equal to the top element on STACK
6. Exit
From Postfix to Answer
• The reason to convert infix to postfixexpression is that we can compute theanswer of postfix expression easier byusing a stack.
From
Ex: 10 2 8 *
Postfix
+ 3 -
to Answer
• First, push(10) into thestack
10
From
Ex: 10
2 8 *
Postfix
+ 3 -
to Answer
• Then, push(2) into thestack
2
10
From Postfix to
Ex: 10 2 8 * + 3 -
Answer
• Push(8) into the stack
8
2
10
From
Ex: 10
2 8 *
Postfix to
+ 3 -
Answer
• Now we see an operator *,that means we can get annew number by calculation
8
2
10
From Postfix to
+ 3 -
Answer
Ex: 10 2 8 *
• Now we see an operator *,that means we can get annew number by calculation
Pop the first two numbers•
* = 1682
8
2
10
From Postfix to
+ 3 -
Answer
Ex: 10 2 8 *
• Now we see an operator *,that means we can get annew number by calculation
Push the new number back•
* = 168216
10
From Postfix
+ 3 -
to Answer
Ex: 10 2 8 *
• Then we see the nextoperator + and performthe calculation
+ = 26161016
10
From Postfix
+ 3 -
to Answer
Ex: 10 2 8 *
• Then we see the nextoperator + and performthe calculation
Push the new number back•
+ = 261610
26
From
Ex: 10
2 8 *
Postfix
+ 3 -
to Answer
•
•
We see the next number 3
Push (3) into the stack
3
26
Compute the Answer
Ex: 10 2 8 * + 3 -
• The last operation
- = 23326
From Postfix
+ 3 -
to Answer
Ex: 10 2 8 *
• The last operation
- = 23
answe
r!
326
23
Task
623+-382/+*2^3+
Task
Quiz Time
Reading Material
• Nell Dale – Chapter#4
• Schaum’s Outlines – Chapter#6
• D. S. Malik – Chapter#7
• http://www.cs.man.ac.uk/~pjj/cs2121/fix.html
• Examples
– animations\in2post\infixtopostfix.exe
– animations\evaluation\evaluation.exe
– animations\tower\tower.exe
CS 201 - Fall 2013
http://www.cs.man.ac.uk/~pjj/cs2121/fix.htmlanimations/in2post/infixtopostfix.exeanimations/evaluation/evaluation.exeanimations/tower/tower.exe