Professional Documents
Culture Documents
Aman Kumar
UpGrad
Campus
Agenda
4+6*2 4+6*2
10 * 2 4 + 12
20 16
Order of Operations
▪ Parenthesis ( ), { }, [ ]
▪Exponents ^ : right to
left eg : 2 ^ 3 ^ 2
2^ 9
512
▪ Multiplication and
Division : left to right
▪ Addition and
Subtraction : left to
right
Example : 2*6 /2–3+7
▪ 12 / 2 – 3 + 7
▪ 6–3+7 // - and + same precedence.
▪ 3+7
▪ 10 // Final result
Use of Parenthesis
▪ Improves readability
▪ Calculate { (2 * 6 ) / 2 } – (3
+ 7)
▪ Result = - 4
11
Conversion of Infix to Postfix Expressions
Solution: (A + B) * C
AB+ * C
AB + C *
Question 1: Infix to Postfix
a) ABCD-- *
b) AB-*CD-
c) AB-*-CD
d) AB-CD- *
Question 2: Infix to Postfix
a) AB+/CD+DE*
b) AB+CD+/DE*-
c) AB+CD+/-DE*
d) AB+CD/+DE*-
Conversion of Infix to Prefix
Expressions
▪ Example: (A+B) *
C
▪ Solution : (A+B) * C
+AB *
C
* +AB C
Question 1: Infix to Prefix
a) - AB *+CD
b) * - AB- CD
c) * - AB+CD
d) - * AB+CD
Question 2: Infix to Prefix
a) - / AB++CD *DE
b) - / +AB+CD *DE
c) - + /AB+CD *DE
d) - / +AB+ *CD DE
Evaluating Inverse Notation Expressions
18
8 2 5 * + 1 3 2 * + 4- /
Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.
8 2 5 * + 1 3 2 * + 4- /
2 * 5 = 10
Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.
8 10 + 1 3 2 * + 4 - /
2 * 5 = 10
Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.
8 10 + 1 3 2 * + 4 - /
Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.
8 10 + 1 3 2 * + 4 - /
8 + 10 = 18
Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.
18 1 3 2 * +4 - /
8 + 10 = 18
Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.
18 1 3 2 * + 4 - /
Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.
18 1 3 2 * + 4 - /
3*2=6
Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.
18 1 6 + 4- /
3*2=6
Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.
18 1 6 + 4 - /
Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.
18 1 6 + 4- /
1+6=7
Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.
18 7 4 - /
1+6=7
Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.
18 7 4 - /
Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.
18 7 4 - /
7-4=3
Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.
18 3 /
7-4=3
Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.
18 3 /
Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.
18 3 /
18 / 3 = 6
Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.
18 / 3 = 6
Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.
6 Result
Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.
▪ Evaluate the following Infix to Postfix notations by showing each stage in stack
▪ 456*+
▪ 78+32+/
▪ 10 5 + 5 / 16 4 - /
Evaluation of Infix to Prefix
2*3+5*4–9
▪ Insert Parenthesis according to priorities
▪ { (2 * 3) + (5 * 4)} – 9
▪ { (2 * 3) + (5 * 4)} – 9
▪ { *23 +*54}–9
▪ { *23 +*54}–9
▪ {+ *23*54} -9
▪ -+ *23*54 9 // Result Get rid of all the parenthesis
Using Stack
Scan Right to Left
- + *2 3*5 4 9
- + *2 3*5 4 9
- + *2 3*5 4 9
5 :Top
▪ The Difference b/w Postfix and Prefix
4 :Top-1
▪ [Top] operator [Top-1]
▪ Result is 17
Conversions using Stack : Infix
to Postfix
▪ Ex : A + B * C – D * E
▪Operands are appended in a list. Operators are pushed into the stack.
▪ ( has the lowest precedence when in the stack but has the highest precedence when in
the input
▪ When we reach the end of input, pop until the stack is empty
Important:
Dealing with brackets
▪ Operands are appended in a list. Operators are pushed into the stack.
▪ ( has the lowest precedence when in the stack but has the highest precedence when in the
input
▪ When we reach the end of input, pop until the stack is empty
Examples
a) 3 + 4 * 5 / 6
Result : 3 4 5 * 6 / +
b) (300+23)*(43 – 21)/(84+7)
Result : 300 23 + 43 21 -* 84 7
+/
Balanced Parenthesis
▪ Used by Compiler.
▪ For every starting braces, there should be counter closing braces.
▪ What’s inside the brackets, doesn’t matter here
Better Version
Solution
▪ Whenever you come across an opening braces, PUSH it onto the
stack.
▪ However, whenever you come across a closing parenthesis, POP a
parenthesis from the stack.
▪ At last, if the stack becomes empty, that means braces are
balanced.