Professional Documents
Culture Documents
Data Structure Chapter 5
Data Structure Chapter 5
Arithmetic Expression:
The arithmetic expression consists of operands and operators. An operand may be a variable or
constant. The operator is a symbol that performs some operation on operands. So the combination of operands
and operators is called an arithmetic expression.
A+B/C
ABC +/
Operands Operators
2+7 %6-8*9
27689 +%-*
Operands Operators
Infix notation
Postfix notation
Prefix notation
Infix, postfix and prefix notations are three different but equivalent ways of writing expressions.
Infix Notation:
A way of writing expressions in which operator comes between their operands is called infix notation. This
is the usual way we write expression. Infix notation need extra information to make the order of evaluation
of operators clear: rules are built to evaluate the operators such as BDMAS rule.
If one ignores that rules then the expressions will not be evaluated properly.
Postfix Notation:
Postfix notation is also known as Reverse Polish or suffix notation. A way of writing expressions in which
operator comes after their operands is called postfix (reverse Polish or suffix) notation.
Examples of postfix notation:
AB+
abc*+
39*
Prefix Notation:
Prefix notation is also known as Polish notation. A way of writing expressions in which operator comes
before their operands is called prefix (Polish) notation.
Examples of prefix notation:
+AB
*56
+*abc
Note: Polish notations are named after the Polish mathematician Jan Lukasiewicz, who introduced this
notation.
Priority Operators
level Symbol Name
Highest or ^ Power
Next highest *, / , % Multiply , Divide , Modulus
Lowest +, - Addition , Subtraction
Steps:
= A (B*C) +
= (A+ (B*C))
Note: Although many brackets will be appearing in expression but they will appear in such a way that it doesn’t
affect the expression result. In above example the original expression is:
A+B*C
But if we convert them into postfix then they will give the same answer. So they are equal.
Sol: AB+C*
= (A+B) C*
= ((A+B)*C)
Required infix notation.
Steps:
= + A (B*C)
= (A+ (B*C))
Required infix notation.
= * (A+B) C
= ((A+B)*C)
Required infix notation.
Stack Method:
The stack method is used to convert an infix notation into postfix notation. The stack is used to store
operands and then passed to postfix expressions according to their precedence. The infix notation is
converted into postfix notation according to the following rules:
a) The infix expression is scanned from left to right until end of the expression.
b) The operands are added to the output.
c) While operators and parenthesis (left parenthesis) are put into stack. If an operator of high priority
(as compared to one which is in stack) is put into stack, then no popping would be done. But if an
operator of low or same priority (as compared to one which is in stack) put in stack than then the
high priority or equal priority operator is popped and added to output.
d) If an operator is before left parenthesis ‘(’mean while right parenthesis ‘)’encounter then the operator
before left parenthesis is popped and added to the output. The left and right parenthesis will be
disappeared.
Example #1: Convert into postfix notation using stack method: A+B*C
Sol: A+B*C
Example #2: Convert into postfix notation using stack method: ((A-(B+C))*D)*(E+F)
Sol: ((A-(B+C))*D)*(E+F)
+
( +
- - (
( ( * * *
( ( ( ( (
Example #3: Convert into postfix notation using stack method: A+ (B*C- (D / E F) *G) * H
/ /
( ( *
* - - - *
( ( ( ( (
+ + + + +
* +
* ( ( *
+ + + +
Assignment: Convert the following into postfix notation using stack method:
5.8 : (A-B) * (D / E)
5.9 : (A+B D) / (E-F) +G
5.10 12 / (7-3) +2 * (1+5)
Answers:
5.8 : AB-DE/*
5.9 : ABD+EF-/G+
5.10 12, 7, 3, -, /, 2, 1, 5, +, *, +,
Prepared By: Muhammad Fayaz
M.Sc Computer Science, SBBU.
Contact#: 0342-5850786
E-mail: fayazsbbu786@gmail.com