0% found this document useful (0 votes)
17 views23 pages

Infix, Postfix Expressions

The document explains arithmetic expressions and their notations, specifically focusing on infix, prefix, and postfix notations. It details how each notation is structured and provides examples, along with an algorithm for converting infix expressions to postfix. Additionally, it outlines operator precedence and includes exercises for evaluating expressions in both infix and postfix forms.

Uploaded by

madhulikathrasu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views23 pages

Infix, Postfix Expressions

The document explains arithmetic expressions and their notations, specifically focusing on infix, prefix, and postfix notations. It details how each notation is structured and provides examples, along with an algorithm for converting infix expressions to postfix. Additionally, it outlines operator precedence and includes exercises for evaluating expressions in both infix and postfix forms.

Uploaded by

madhulikathrasu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

• An expression that only contains arithmetic

operands and operators is called an arithmetic


expression
• The way to write arithmetic expression is
known as a notation. An arithmetic
expression can be written in three
different but equivalent notations, i.e.,
without changing the essence or output
of an expression. These notations are −
Infix Notation
Prefix (Polish) Notation
Postfix (Reverse-Polish) Notation
Infix Notation

• We write expression in infix notation,


e.g. a - b + c, where operators are
used in-between operands. It is easy
for us humans to read, write, and
speak in infix notation but the same
does not go well with computing
devices. An algorithm to process infix
notation could be difficult and costly
in terms of time and space
consumption
Prefix Notation

• In this notation, operator is prefixed


to operands, i.e. operator is written
ahead of operands. For
example, +ab. This is equivalent to
its infix notation a + b. Prefix
notation is also known as Polish
Notation.
Postfix Notation

• This notation style is known


as Reversed Polish Notation. In
this notation style, the operator
is postfixed to the operands i.e., the
operator is written after the
operands. For example, ab+. This is
equivalent to its infix notation a + b
The following table briefly tries to show
the difference in all three notations −
Sr.No. Infix Notation Prefix Postfix
Notation Notation
1 a+b +ab ab+
2 (a + b) ∗ c ∗+abc ab+c∗
3 a ∗ (b + c) ∗a+bc abc+∗
4 a/b+c/d +/ab/cd ab/cd/+
5 (a + b) ∗ (c + d) ∗+ab+cd ab+cd+∗

6 ((a + b) ∗ c) - d -∗+abcd ab+c∗d-


There are 3 levels of precedence for 5
binary operators as given below:

Highest: Exponentiation (^)


Next highest: Multiplication (*)
and division(/)
Lowest: Addition (+) and
Subtraction (-)

• Infix notation: (A-B)*[C/(D+E)+F]


• Post-fix notation: AB- CDE +/F +*
Infix notation:
(2+4) * (4+6)

Post-fix notation:
2 4 + 4 6 + *

Result: 60
Infix vs Postfix Expression
Infix expression
into
postfix expression
Algorithm
 Scan the infix notation from left to right one character at a time.
 If the next symbol scanned as an operand, append it to the postfix string.
 If the next symbol scanned as an operator, the:
 Pop and append to the postfix string every operator on the stack that:
 Is above the most recently scanned left parenthesis, and
 Has precedence higher than or is a right-associative operator of equal precedence to
that of the new operator symbol.
 Push the new operator onto the stack
 If a left parenthesis is scanned, push it into the stack.
 If a right parenthesis is scanned, all operators down to the most recently
scanned left parenthesis must be popped and appended to the postfix
string. Furthermore, the pair of parentheses must be discarded.
 When the infix string is fully scanned, the stack may still contain some
operators. All the remaining operators should be popped and appended
to the postfix string.
To evaluate 3+4–5
Assignment
Evaluate 3+4*5
Evaluate 3*(4+5)
Translating A+B–C into Postfix
Infix to Postfix Conversion Example
Convert the (X - Y / (Z + U) * V) infix expression into postfix expression.
Postfix
to
Infix Expression
Algorithm

• Read the postfix expression from left to right one


character at a time.
• If it is operand push into operand stack.
• If it is an operator, then:
– Pop two operand from the stack.
– From infix expression and push into the operand stack
• If the expression is not ended go to the first step.
• Pop operand stack and display.
• Exit
Postfix Expression : abc-+de-+
Assignment
Evaluate the Postfix expression using stack
752+*415-/-

Convert Infix to postfix


• 3+4*5/6
• (3 + 6) * (2 - 4) + 7.
• (4 + 5) * 3 - 7
3+4*5/6

• Postfix Expression : 3 4 5 * 6 / +
752+*415-/-
Infix Expression : A+(B*C+D)/E

You might also like