Uploaded by Tanisha Ramani

DS PTT

advertisement
MADHUBEN & BHANUBHAI PATEL
INSTITUTE OF TECHNOLOGY
Topic :- Infix to Postfix and Infix to Prefix Conversion
Prepared By:
Darshan Moradiya (12202080701021)
Patel Bhavy (12202080701017)
Patel Deep (12202080701025)
Infix Notation
Infix notation is the standard way of writing mathematical expressions, where operators are placed
between operands. For example, "2 + 3".
Postfix Notation
Postfix notation, also known as Reverse Polish Notation (RPN), is an alternative way of representing
mathematical expressions. Operators are placed after operands. For example, "2 3 +".
Conversion from Infix to
Postfix
Converting an infix expression to postfix involves rearranging the
operators and operands based on certain rules. The result is a postfix
expression that can be easily evaluated.
Algorithm for Infix to Postfix Conversion
Step 1
Step 2
Step 3
Initialize an empty stack.
Scan the infix expression
If the scanned character is
from left to right.
an operand, output it.
Step 4
Step 5
Step 6
If the scanned character is
If the scanned character is
If the scanned character is
an operator, pop operators
an opening parenthesis,
a closing parenthesis, pop
from the stack and output
push it onto the stack.
operators from the stack
them until an operator with
and output them until an
lower precedence is
opening parenthesis is
encountered. Push the
encountered. Pop and
scanned operator onto the
discard the opening
stack.
parenthesis.
Step 7
Step 8
Step 9
Repeat steps 2-6 until all
Pop any remaining
The output is the postfix
characters in the infix
operators from the stack
expression.
expression have been
and output them.
scanned.
Prefix Notation
Prefix expression notation requires that all operators precede the two operands that they work on.
For example, “+ 2 3”
Conversion from Infix to
Prefix
Sometimes, converting an infix expression to prefix notation can be more
useful or necessary. It involves rearranging the operators and operands,
placing operators before operands.
Algorithm for Infix to Prefix Conversion
Step 1
Step 2
Step 3
Reverse the infix expression.
Use the algorithm for infix to
Reverse the resulting postfix
postfix conversion on the
expression to obtain the prefix
reversed expression.
notation.
Thank You
Download