In-Pre-Post Fix Expressions
In-Pre-Post Fix Expressions
In-Pre-Post Fix Expressions
a a
b b
• (a + b) * (c + d) + e – f/g*h + 3.25
• Expressions comprise three kinds of entities.
Operators (+, -, /, *).
Operands (a, b, c, d, e, f, g, h, 3.25, (a + b), (c + d),
etc.).
Delimiters ((, )).
Operator Degree
• Infix = a * b + c
Postfix = a b * c +
• Infix = (a + b) * (c – d) / (e + f)
Postfix = a b + c d - * e f + /
Unary Operators
A/B-C+D*E-A*C
•Fully Paranthecize the expression
In order of precedence of operators
((((A/B)-C)+(D*E))-
(A*C))
((((AB/C-(DE*+(AC*-
Post fix: AB/C-DE*+AC*-
Example
• Move Operators
replacing the
corresponding right
Parantheces
((((AB/C-(DE*(AC*-
•Remove all parantheses
• Post Fix : AB/C-DE*AC*-+
INFIX to PREFIX conversion
1. Fully Paranthecize the expression
according to the order of precedence of
operators
2. Move all the operators so that they replace
the corresponding left parantheses
3. Replace unary operator with any symbol
@,? etc. to differentiate it from binary
operators
4. Delete all the parantheces
Example
A/B-C+D*E-A*C
•Fully Paranthecize the expression
In order of precedence of operators
((((A/B)-C)+(D*E))-(A*C))
-+-/ABC)*DE))*AC))
PREFIX: -
+-/ABC*DE*AC
Example
• Move Operators
replacing the
crresponding left
Parantheces
-+-/AB)C)*DE))*AC))
•Remove all Parantheses
Pre Fix expression: +-/ABC-*DE*AC
Examples
1. A*B*C
2. -A+B-C+D
3. A*-B+C
4. (A+B)*D+E/(F+A*D)+C
5. A&&B||C||!(E>F)
6. !(A&&!((B<C)||(C>D)))||(C<E)
Steps for Converting infix expression to
postfix form
Assume array ‘a’ contains the infix
expression and array ‘b’ is for the postfix
expression and initially stack is empty
•Scan the infix array ‘a’ , for each char a[i]
•If a[i] is ‘(‘ push it on to the stack
•If a[i] is operand , write it into b[j]
•If a[i] is operator , compare its priority with
the priority of operator on stack top.
oIf priority of a[i] is greater push a[i] on to
the stack
Steps for Converting infix expression to
postfix form…
o Otherwise delete each operator from the stack and
write in b[j] until we get an operator whose
priority is less than or equal to that of a[i],
then Push a[i] onto stack
• If a[i] is ‘)’ delete each operator from stack and
write in b[j] until we encounter a
‘(‘ on stack top
Delete ‘(‘ from stack but don’t write in b[j]
• If a[i]=‘\0’(end of array ‘a’) then delete each
operator fromstack and write in b[j] until stack
becomes empty
• Append a ‘\0’ at end of array b
Steps for Converting infix expression to
prefix form
Assume array ‘a’ contains the REVERSED
infix expression and array ‘b’ is for the prefix
expression and initially stack is empty
•Scan the infix array ‘a’ , for each char a[i]
•If a[i] is ‘)‘ push it on to the stack
•If a[i] is operand , write it into b[j]
•If a[i] is operator , compare its priority with
the priority of operator on stack top.
oIf priority of a[i] is greater or equal to the
operator on the top of the stack push a[i] on to
the stack
Steps for Converting infix expression to
prefix form…
o Otherwise delete each operator from the
stack and write in b[j] until we get an
operator on stack top whose priority is less
than that of a[i]
• If a[i] is ‘(’ delete each operator from stack
and write in b[j] until we encounter a
‘)‘ on stack top
Delete ‘)‘ from stack but don’t write in b[j]
• If a[i]=‘\0’(end of array ‘a’) then delete
each operator fromstack and write in b[j]
until stack becomes empty
Steps for Converting infix expression to
prefix form…
• Append a ‘\0’ at end of array b
• Finally REVERSE array ‘b’ to get the
prefix expression
• (a + b) * (c – d) / (e + f)
• ab+cd-*ef+/
• ab+cd-*ef+/
• ab+cd-*ef+/
• ab+cd-*ef+/
b
a
stack
Postfix Evaluation
• (a + b) * (c – d) / (e + f)
• ab+cd-*ef+/
• ab+cd-*ef+/
• ab+cd-*ef+/
• ab+cd-*ef+/ d
• ab+cd-*ef+/ c
• ab+cd-*ef+/ (a + b)
• ab+cd-*ef+/ stack
Postfix Evaluation
• (a + b) * (c – d) / (e + f)
• ab+cd-*ef+/
• ab+cd-*ef+/
(c – d)
(a + b)
stack
Postfix Evaluation
• (a + b) * (c – d) / (e + f)
• ab+cd-*ef+/
• ab+cd-*ef+/
• ab+cd-*ef+/
• ab+cd-*ef+/ f
• ab+cd-*ef+/ e
(a + b)*(c – d)
stack
Postfix Evaluation
• (a + b) * (c – d) / (e + f)
• ab+cd-*ef+/
• ab+cd-*ef+/
• ab+cd-*ef+/
• ab+cd-*ef+/
• ab+cd-*ef+/ (e + f)
• ab+cd-*ef+/ (a + b)*(c – d)
stack
Prefix Form
• The prefix form of a variable or constant is
the same as its infix form.
a, b, 3.25
• The relative order of operands is the same in
infix and prefix forms.
• Operators come immediately before the
prefix form of their operands.
Infix = a + b
Postfix = ab+
Prefix = +ab