8/9/2019 Infix to postfix.pptx
1/27
Stack ApplicationsLecture #9
Infix Notation , Prefix Notation
& Postfix Notation
Data Structure & AlgorithmsSE-203
8/9/2019 Infix to postfix.pptx
2/27
Infix, Postfix and Prefix Notations
The usual way of expressing the sum of two numbers
A and B is :A+B
The operator ‘+’ is placed between the two operands Aand B.
This is called the “ Infix Notation” Consider a bit more complex example:
(13 – 5) / (3 + 1) When the parentheses are removed the situation becomes ambiguous.
13 – 5 / 3 + 1
is it (13 – 5) / (3 + 1)
or 13 – (5 / 3) + 1 To cater for such ambiguity, you must have operator
precedence rules to follow (as in C++).
8/9/2019 Infix to postfix.pptx
3/27
Infix, Postfix and Prefix Notations
In the absence of parentheses
13 – 5 / 3 + 1
Will be evaluated as 13 – (5 / 3) + 1.
Operator precedence isby-passed with thehelp of parentheses as in (13 – 5) / (3 + 1).
The infix notation is therefore cumbersome
due to Operator Precedence rules and
Evaluation of Parentheses
8/9/2019 Infix to postfix.pptx
4/27
Postfix Notation
It is a notation for writing arithmetic expressions
in which operands appear before the operator.
E.g. A + B is written as A B + in postfix notation.
There are no precedence rules to be learnt in it.
Parentheses are neer needed..
!ue to its simplicit", some calculators use postfix
notation. This is also called the #$eerse Polish Notation or
$PN%.
8/9/2019 Infix to postfix.pptx
5/27
Postfix Notation & 'ome examples
8/9/2019 Infix to postfix.pptx
6/27
(onersion from Infix to Postfix
Notation
We have to accommodate the presence of operator precedence rules andParentheses while converting from infix to postfix.
Data objects required for the conversion are
◦ An operator / parentheses stack.
◦ A Postfix expression string to store the resultant.
◦ An infix expression string read one item at a time.
8/9/2019 Infix to postfix.pptx
7/27
(onersion from Infix to Postfix
The Algorithm
◦ What are possible items in an input Infix expression◦ Read an item from input infix expression
◦ If item is an operand append it to postfix string
◦ If item is “(“ push it on the stack
◦ If the item is an operator If the operator has higher precedence than the one already ontop of the stack then push it onto the operator stack
If the operator has lower precedence than the one already ontop of the stack then
pop the operator on top of the operator stack and append itto postfix string, and
push lower precedence operator onto the stack
◦ If item is “)” pop all operators from top of the stack one-by-one,
until a “(“ is encountered on stack and removed◦ If end of infix strin o the stack one-b-one and a end to
8/9/2019 Infix to postfix.pptx
8/27
8/9/2019 Infix to postfix.pptx
9/27
Tr" it "ourself
'how a trace of algorithm that conerts the infix expression
a + ) b * c & d e
8/9/2019 Infix to postfix.pptx
10/27
Exercise
8/9/2019 Infix to postfix.pptx
11/27
'olution
8/9/2019 Infix to postfix.pptx
12/27
Ealuation of Postfix Expression
After an infix expression is conerted to postfix, its ealuation is a simple
affair.
'tac- comes in hand", AAIN
The Algorithm
◦ $ead the postfix expression one item at/a/time
◦
If item is an operand push it on to the stac-.◦ If item is an operator pop the top two operands from stac- and appl" the
operator.
◦ Push the result bac- on top of the stac-, which will become an operand for nextoperation.
◦ 0inal result will be the onl" item left on top of the stac-.
8/9/2019 Infix to postfix.pptx
13/27
top
Stack in Action ….
5 7 +
6 2 - *
Postfix Expression
8/9/2019 Infix to postfix.pptx
14/27
5top
Stack in Action ….
5 7 +
6 2 - *
Postfix Expression
8/9/2019 Infix to postfix.pptx
15/27
5
top
Stack in Action ….
5 7 + 6 2 - *
Postfix Expression7
8/9/2019 Infix to postfix.pptx
16/27
5
top
Stack in Action ….
5 7 6 2 - *
Postfix Expression7
+
$esult 1 Pop) #+% Pop) Push )$esult
top
8/9/2019 Infix to postfix.pptx
17/27
12top
Stack in Action ….
5 7 + 6 2-
*
Postfix Expression
8/9/2019 Infix to postfix.pptx
18/27
12
top
Stack in Action ….
5 7 + 6 2 - *
Postfix Expression 6
8/9/2019 Infix to postfix.pptx
19/27
12
top
Stack in Action ….
5 7 + 6 2 - *
Postfix Expression 6
2
8/9/2019 Infix to postfix.pptx
20/27
12
top
Stack in Action ….
5 7 + 6 2 - *
Postfix Expression 6
2
$esult 1 Pop) #/% Pop) Push )$esult
12
top4
8/9/2019 Infix to postfix.pptx
21/27
Stack in Action ….
5 7 + 6 2 - *
Postfix Expression
12
top4
8/9/2019 Infix to postfix.pptx
22/27
12
top4
$esult 1 Pop) # * % Pop) Push )$esult
top
485 7 + 6 2 - *
Postfix Expression
8/9/2019 Infix to postfix.pptx
23/27
$esult 1 Pop)
top48
5 7 + 6 2 - *
Postfix Expression
top
$esult 1 23
8/9/2019 Infix to postfix.pptx
24/27
Exercise
(onert the following infix expression into postfix
A + B * ( + ) ! * E + 0 *
Now assume the gien alues for the postfix expression
A14, B15, (16, !17, E12, 013, 18
And ealuate the postfix expression
8/9/2019 Infix to postfix.pptx
25/27
'olution
Postfix expression is
A B C * + D E * F + G * +
And eva!ation o" e#$%e&&ion !&in' 'iven va!e& i&
1(7
8/9/2019 Infix to postfix.pptx
26/27
Ealuation of Postfix Expression
Conversion from infix to postfix is difficult because :◦ Rules governing the precedence of operators are to becatered for
◦ Many possibilities for incoming characters
◦ To cater for parentheses
◦ To cater for error conditions / checks
Evaluation of postfix expression is very simple to
implement because operators appear in precisely theorder in which they are to be executed.
8/9/2019 Infix to postfix.pptx
27/27
9otiation for the conersion
Motivation for this conversion is the need to have theoperators in the precise order for execution.
While using paper and pencil to do the conversion wecan “foresee” the expression string and the depth of allthe scopes (if the expressions are not very long andcomplicated).
When a program is required to evaluate an expression,it must be accurate.
At any time during scanning of an expression we cannot be sure that we have reached the inner most scope.
Encountering an operator or parentheses may requirefrequent “backtracking”.Rather than backtracking, we use the stack to“remember” the operators encountered previously.