Data Structure - Lec05
Data Structure - Lec05
Infix to Postfix
Conversion
Infix to Postfix Example.
Postfix Evaluation
Postfix Evaluation
Example
What is an Expression?
In any programming language, if we want to perform any calculation
or to frame a condition etc., we use a set of symbols to perform the
task.
6
7
Expression evaluations is one of the major application
that illustrates the different types of stacks.
Consider the sum of A and B
A+B
where A and B are called operands and “+” is called
operator
9
We have five binary operations: addition, subtraction,
multiplication, division, and exponentiation.
The first four are available in C++ and are denoted by
the usual operators + , - , *, and / .
The fifth exponentiation is represented by the operator
^
A^B A is raised to the power B
In order to get the value of any expression (infix,
postfix, prefix).
You have to know the priority or precedence of each
operator in the expression.
Then the highest Priority will be executed first then
the less priority and the less and …..
10
We can also convert one type of expression to
another type of expression like Infix to Postfix,
Infix to Prefix, Postfix to Prefix.
To convert any Infix expression into Postfix or Prefix
expression we can use the following procedure:
1) Find all the operators in the given Infix
Expression.
2) Find the order of operators evaluated according
to their Operator precedence.
3) Convert each operator into required type of
expression (Postfix or Prefix) in the same order.
12
Consider the following Infix Expression to be
converted into Postfix Expression.
D=A+B*C
Step 1: The Operators in the given Infix Expression :
= ,+, *
Step 2: The Order of Operators according to their
preference : * ,+,=
Step 3: Now, convert the first operator * -----
D=A+BC*
Step 4: Convert the next operator + ----- D = A BC* +
Step 5: Convert the next operator = ----- D ABC*+ =
13
The rules to be remembered during infix to postfix
conversion are:
1.Parenthesize the expression starting from left to
light.
2. During parenthesizing the expression, the
operands associated with operator having higher
precedence are first parenthesized.
3. The sub-expression (part of expression), which has
been converted into postfix, is to be treated as single
operand.
4. Once the expression is converted to postfix form,
remove the parenthesis.
14
Example: Evaluate the postfix expression of the
infix exp.
(A + B) * C / D + E ^ A / B
15
16
Consider the following Infix Expression...
(A+B)*(C-D)
17
The expression A+B*C is evaluated as A+(B*C)
because multiplication takes precedence over
addition.
To rewrite A+B*C in postfix:
18
(A+B*C)
((A+B)*C)
19
infix postfix
A+B AB+
(A+B)*(C-D) AB+CD-*
A^B*C-D+E/F/(G+H) AB^C*D-EF/GH+/+
infix prefix
A+B +AB
(A+B)*(C-D) *+AB-CD
A^B*C-D+E/F/(G+H) +-*^ABCD//EF+GH
20
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 operands in two different variables (operand1
and operand2). Then perform reading symbol
operation using operand1 and operand2 and push
result back on to the Stack.
4. Finally! perform a pop operation and display the
popped value as final result.
21