You are on page 1of 80

Stack Applications

Infix, Prefix, Postfix

Aman Kumar
UpGrad
Campus
Agenda

1. Infix, Prefix and Postfix Conversions using Stack


2. Infix, Prefix and Postfix Evaluations using Stack
3. Balanced Parenthesis
Evaluating Expressions

The most common way of writing any expression


▪ 2+3
▪ A+B
▪ (P * 2)
▪ <operand> <operator> <operand>
Operand : the object on which the operation is performed.
It can be constant, variable or expression itself.
Rules to evaluate expressions

▪ Single operator : just apply


▪ Multiple operators : if no parenthesis, order should be decided.
Ex:

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

Here, we have 4 operators: *,/,-,+

* and / have same precedence and will be done left to right.

▪ So, what is the result???


Solution: 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)

▪ What is the result now???


Solution:

▪ Result = - 4

▪ Infix Notation : the most common way of writing expressions using a


lot of parenthesis
▪ But not the only way as it is difficult to parse and evaluate an infix
expression.
2 Other ways
▪ Mathematicians and Logicians

▪ studied this problem and came up with 2 other ways of writing


expressions that are parenthesis free and can be parsed without
ambiguity, no precedence rules.
Prefix and Postfix

1+2 can be written in different notations:


▪ Infix notation is 1+2 // good for human
readability
▪ Prefix notation is
+12
– Also known as Polish Notation
– Operator (+) is to the left of its operands (1,2)

▪ Postfix notation is 12+


– Also known as Reverse Polish Notation
– Operator (+) is to the right of its operands (1,2) // good for machine readability

11
Conversion of Infix to Postfix Expressions

Example: Convert the following expression from infix to postfix


(A + B) * C

Solution: (A + B) * C
AB+ * C
AB + C *
Question 1: Infix to Postfix

What is the correct postfix output for following infix expression


(A-B) * (C-D)

a) ABCD-- *
b) AB-*CD-
c) AB-*-CD
d) AB-CD- *
Question 2: Infix to Postfix

What is the correct postfix output for following infix expression


(A+B) / (C+D) – (D*E)

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

What is the correct prefix output for following infix expression


(A-B) * (C-D)

a) - AB *+CD
b) * - AB- CD
c) * - AB+CD
d) - * AB+CD
Question 2: Infix to Prefix

What is the correct prefix output for following infix expression


( A+B) / (C+D) – (D*E)

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 1: Start at left and look at expression until you come to


an operator

Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.

Step 3: If the formula now consists of only one number, that


is the result;
if not continue to look for an operator and continue with step
2.
Evaluating Inverse Notation Expressions
19

8 2 5 * + 1 3 2 * + 4- /

2 * 5 = 10

Step 1: Start at left and look at expression until you come


to an operator

Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.

Step 3: If the formula now consists of only one number that


is
the result; if not continue to look for an operator and
continue with step 2.
Evaluating Inverse Notation Expressions
20

8 10 + 1 3 2 * + 4 - /

2 * 5 = 10

Step 1: Start at left and look at expression until you come


to an operator

Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.

Step 3: If the formula now consists of only one number that


is
the result; if not continue to look for an operator and
continue with step 2.
Evaluating Inverse Notation Expressions
21

8 10 + 1 3 2 * + 4 - /

Step 1: Start at left and look at expression until you come


to an operator

Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.

Step 3: If the formula now consists of only one number that


is
the result; if not continue to look for an operator and
continue with step 2.
Evaluating Inverse Notation Expressions
22

8 10 + 1 3 2 * + 4 - /

8 + 10 = 18

Step 1: Start at left and look at expression until you come


to an operator

Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.

Step 3: If the formula now consists of only one number that


is
the result; if not continue to look for an operator and
continue with step 2.
Evaluating Inverse Notation Expressions
23

18 1 3 2 * +4 - /

8 + 10 = 18

Step 1: Start at left and look at expression until you come


to an operator

Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.

Step 3: If the formula now consists of only one number that


is
the result; if not continue to look for an operator and
continue with step 2.
Evaluating Inverse Notation Expressions
24

18 1 3 2 * + 4 - /

Step 1: Start at left and look at expression until you come


to an operator

Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.

Step 3: If the formula now consists of only one number that


is
the result; if not continue to look for an operator and
continue with step 2.
Evaluating Inverse Notation Expressions
25

18 1 3 2 * + 4 - /

3*2=6

Step 1: Start at left and look at expression until you come


to an operator

Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.

Step 3: If the formula now consists of only one number that


is
the result; if not continue to look for an operator and
continue with step 2.
Evaluating Inverse Notation Expressions
26

18 1 6 + 4- /

3*2=6

Step 1: Start at left and look at expression until you come


to an operator

Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.

Step 3: If the formula now consists of only one number that


is
the result; if not continue to look for an operator and
continue with step 2.
Evaluating Inverse Notation Expressions
27

18 1 6 + 4 - /

Step 1: Start at left and look at expression until you come


to an operator

Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.

Step 3: If the formula now consists of only one number that


is
the result; if not continue to look for an operator and
continue with step 2.
Evaluating Inverse Notation Expressions
28

18 1 6 + 4- /

1+6=7

Step 1: Start at left and look at expression until you come


to an operator

Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.

Step 3: If the formula now consists of only one number that


is
the result; if not continue to look for an operator and
continue with step 2.
Evaluating Inverse Notation Expressions
29

18 7 4 - /

1+6=7

Step 1: Start at left and look at expression until you come


to an operator

Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.

Step 3: If the formula now consists of only one number that


is
the result; if not continue to look for an operator and
continue with step 2.
Evaluating Inverse Notation Expressions
30

18 7 4 - /

Step 1: Start at left and look at expression until you come


to an operator

Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.

Step 3: If the formula now consists of only one number that


is
the result; if not continue to look for an operator and
continue with step 2.
Evaluating Inverse Notation Expressions
31

18 7 4 - /

7-4=3

Step 1: Start at left and look at expression until you come


to an operator

Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.

Step 3: If the formula now consists of only one number that


is
the result; if not continue to look for an operator and
continue with step 2.
Evaluating Inverse Notation Expressions
32

18 3 /

7-4=3

Step 1: Start at left and look at expression until you come


to an operator

Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.

Step 3: If the formula now consists of only one number that


is
the result; if not continue to look for an operator and
continue with step 2.
Evaluating Inverse Notation Expressions
33

18 3 /

Step 1: Start at left and look at expression until you come


to an operator

Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.

Step 3: If the formula now consists of only one number that


is
the result; if not continue to look for an operator and
continue with step 2.
Evaluating Inverse Notation Expressions
34

18 3 /

18 / 3 = 6

Step 1: Start at left and look at expression until you come


to an operator

Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.

Step 3: If the formula now consists of only one number that


is
the result; if not continue to look for an operator and
continue with step 2.
Evaluating Inverse Notation Expressions
35

18 / 3 = 6

Step 1: Start at left and look at expression until you come


to an operator

Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.

Step 3: If the formula now consists of only one number that


is
the result; if not continue to look for an operator and
continue with step 2.
Evaluating Inverse Notation Expressions
36

6 Result

Step 1: Start at left and look at expression until you come


to an operator

Step 2: Replace the two numbers to the left of the operator with
the result of performing the operator on the two numbers.

Step 3: If the formula now consists of only one number that


is
the result; if not continue to look for an operator and
continue with step 2.
37
Evaluating Postfix Expressions Using a STACK

Step 1: Assume that formula consists of n symbols, and that


we use variable k to count number of steps. Set k to 1

Step 2: Examine the kth symbol. If it is an operand (number),


push it onto the stack. If it is an operator, POP two items
from the stack, perform the operation and push result
back onto the stack.

Step 3: If k=n the algorithm terminates and the answer is on


the stack; otherwise add 1 to k and go to step 2.
38
8 2 5 * + 1 3 2 * + 4 - /
Step 1: Set k to 1

Step 2: Examine the kth symbol. If it is a operand


(number), push it onto the stack. If it is an
operator, POP two items from the stack,
perform the operation and push result
back onto the stack.

Step 3: If k=n the algorithm terminates and the


answer is on the stack; otherwise add 1
to k and go to step 2.
39
8 2 5 * + 1 3 2 * + 4 - /
Step 1: Set k to PUSH 8
1
Step 2: Examine the kth symbol. If it is an operand 8
(number), push it onto the stack. If it is an
operator, POP two items from the stack,
perform the operation and push result
back onto the stack.

Step 3: If k=n the algorithm terminates and the


answer is on the stack; otherwise add 1 Stack
to k and go to step 2.
40
8 2 5 * + 1 3 2 * + 4 - /
Step 1: Set k to
1
Step 2: Examine the kth symbol. If it is an operand 8
(number), push it onto the stack. If it is an
operator, POP two items from the stack,
perform the operation and push result back
onto the stack.

Step 3: If k=n the algorithm terminates and the


answer is on the stack; otherwise add 1 Stack
to k and go to step 2.
41
8 2 5 * + 1 3 2 * + 4 - /
Step 1: Set k to PUSH 2
1
Step 2: Examine the kth symbol. If it is an operand 2
(number), push it onto the stack. If it is an 8
operator, POP two items from the stack,
perform the operation and push result back
onto the stack.

Step 3: If k=n the algorithm terminates and the


answer is on the stack; otherwise add 1 Stack
to k and go to step 2.
42
8 2 5 * + 1 3 2 * + 4 - /
Step 1: Set k to
1
Step 2: Examine the kth symbol. If it is an operand 2
(number), push it onto the stack. If it is an 8
operator, POP two items from the stack,
perform the operation and push result back
onto the stack.

Step 3: If k=n the algorithm terminates and the


answer is on the stack; otherwise add 1 Stack
to k and go to step 2.
43
8 2 5 * + 1 3 2 * + 4 - /
Step 1: Set k to PUSH 5
1
Step 2: Examine the kth symbol. If it is an operand 5
(number), push it onto the stack. If it is an 2
operator, POP two items from the stack,
perform the operation and push result 8
back onto the stack.

Step 3: If k=n the algorithm terminates and the


answer is on the stack; otherwise add 1 Stack
to k and go to step 2.
44
8 2 5 * + 1 3 2 * + 4 - /
Step 1: Set k to
1
Step 2: Examine the kth symbol. If it is an operand 5
(number), push it onto the stack. If it is an 2
operator, POP two items from the stack,
perform the operation and push result back 8
onto the stack.

Step 3: If k=n the algorithm terminates and the


answer is on the stack; otherwise add 1 Stack
to k and go to step 2.
45
8 2 5 * + 1 3 2 * + 4 - /
Step 1: Set k to POP 5
1
Step 2: Examine the kth symbol. If it is an operand 5
(number), push it onto the stack. If it is an 2
operator, POP two items from the stack,
perform the operation and push result back 8
onto the stack.

Step 3: If k=n the algorithm terminates and the


answer is on the stack; otherwise add 1 Stack
to k and go to step 2.
46
8 2 5 * + 1 3 2 * + 4 - /
POP 5
Step 1: Set k to POP 2
1
Step 2: Examine the kth symbol. If it is an operand 2
(number), push it onto the stack. If it is an 8
operator, POP two items from the stack,
perform the operation and push result
back onto the stack.

Step 3: If k=n the algorithm terminates and the


answer is on the stack; otherwise add 1 Stack
to k and go to step 2.
47
8 2 5 * + 1 3 2 * + 4 -POP/5
POP 2
Step 1: Set k to 2 * 5 =10
1
Step 2: Examine the kth symbol. If it is an operand 8
(number), push it onto the stack. If it is an
operator, POP two items from the stack,
perform the operation and push result back
onto the stack.

Step 3: If k=n the algorithm terminates and the


answer is on the stack; otherwise add 1 Stack
to k and go to step 2.
8 2 5 * + 1 3 2 * + 4 - / 48
POP 5
POP 2
2 * 5 =10
Step 1: Set k to PUSH 10
1
Step 2: Examine the kth symbol. If it is an operand 10
(number), push it onto the stack. If it is an 8
operator, POP two items from the stack,
perform the operation and push result back
onto the stack.

Step 3: If k=n the algorithm terminates and the


answer is on the stack; otherwise add 1 Stack
to k and go to step 2.
49
8 2 5 * + 1 3 2 * + 4 - /
Step 1: Set k to
1
Step 2: Examine the kth symbol. If it is an operand 10
(number), push it onto the stack. If it is an 8
operator, POP two items from the stack,
perform the operation and push result back
onto the stack.

Step 3: If k=n the algorithm terminates and the


answer is on the stack; otherwise add 1 Stack
to k and go to step 2.
8 2 5 * + 1 3 2 * + 4 - / 50
POP 10
POP 8
8+10=18
Step 1: Set k to PUSH 18
1
Step 2: Examine the kth symbol. If it is an operand 18
(number), push it onto the stack. If it is an
operator, POP two items from the stack,
perform the operation and push result back
onto the stack.

Step 3: If k=n the algorithm terminates and the


answer is on the stack; otherwise add 1 Stack
to k and go to step 2.
51
8 2 5 * + 1 3 2 * + 4 - /
Step 1: Set k to
1
Step 2: Examine the kth symbol. If it is an operand 18
(number), push it onto the stack. If it is an
operator, POP two items from the stack,
perform the operation and push result back
onto the stack.

Step 3: If k=n the algorithm terminates and the


answer is on the stack; otherwise add 1 Stack
to k and go to step 2.
52
8 2 5 * + 1 3 2 * + 4 - /
Step 1: Set k to PUSH 1
1
Step 2: Examine the kth symbol. If it is an operand 1
(number), push it onto the stack. If it is an 18
operator, POP two items from the stack,
perform the operation and push result back
onto the stack.

Step 3: If k=n the algorithm terminates and the


answer is on the stack; otherwise add 1 Stack
to k and go to step 2.
53
8 2 5 * + 1 3 2 * + 4 - /
Step 1: Set k to
1
Step 2: Examine the kth symbol. If it is an operand 1
(number), push it onto the stack. If it is an 18
operator, POP two items from the stack,
perform the operation and push result back
onto the stack.

Step 3: If k=n the algorithm terminates and the


answer is on the stack; otherwise add 1 Stack
to k and go to step 2.
54
8 2 5 * + 1 3 2 * + 4 - /
Step 1: Set k to PUSH 3
1
Step 2: Examine the kth symbol. If it is an operand 3
(number), push it onto the stack. If it is an 1
operator, POP two items from the stack,
perform the operation and push result 18
back onto the stack.

Step 3: If k=n the algorithm terminates and the


answer is on the stack; otherwise add 1 Stack
to k and go to step 2.
55
8 2 5 * + 1 3 2 * + 4 - /
Step 1: Set k to
1
Step 2: Examine the kth symbol. If it is an operand 3
(number), push it onto the stack. If it is an 1
operator, POP two items from the stack,
perform the operation and push result back 18
onto the stack.

Step 3: If k=n the algorithm terminates and the


answer is on the stack; otherwise add 1 Stack
to k and go to step 2.
56
8 2 5 * + 1 3 2 * + 4 - /
Step 1: Set k to PUSH 2
1
Step 2: Examine the kth symbol. If it is an operand 2
(number), push it onto the stack. If it is an 3
operator, POP two items from the stack,
perform the operation and push result back 1
onto the stack. 18
Step 3: If k=n the algorithm terminates and the
answer is on the stack; otherwise add 1 Stack
to k and go to step 2.
57
8 2 5 * + 1 3 2 * + 4 - /
Step 1: Set k to
1
Step 2: Examine the kth symbol. If it is an operand 2
(number), push it onto the stack. If it is an 3
operator, POP two items from the stack,
perform the operation and push result 1
back onto the stack. 18
Step 3: If k=n the algorithm terminates and the
answer is on the stack; otherwise add 1 Stack
to k and go to step 2.
8 2 5 * + 1 3 2 * + 4 - / 58
POP 2
POP 3
3*2= 6
Step 1: Set k to PUSH 6
1
Step 2: Examine the kth symbol. If it is an operand 6
(number), push it onto the stack. If it is an 1
operator, POP two items from the stack,
perform the operation and push result back 18
onto the stack.

Step 3: If k=n the algorithm terminates and the


answer is on the stack; otherwise add 1 Stack
to k and go to step 2.
59
8 2 5 * + 1 3 2 * + 4 - /
Step 1: Set k to
1
Step 2: Examine the kth symbol. If it is an operand 6
(number), push it onto the stack. If it is an 1
operator, POP two items from the stack,
perform the operation and push result 18
back onto the stack.

Step 3: If k=n the algorithm terminates and the


answer is on the stack; otherwise add 1 Stack
to k and go to step 2.
8 2 5 * + 1 3 2 * + 4 - / 60
POP 6
POP 1
1+6=7
Step 1: Set k to PUSH 7
1
Step 2: Examine the kth symbol. If it is an operand 7
(number), push it onto the stack. If it is an 18
operator, POP two items from the stack,
perform the operation and push result back
onto the stack.

Step 3: If k=n the algorithm terminates and the


answer is on the stack; otherwise add 1 Stack
to k and go to step 2.
61
8 2 5 * + 1 3 2 * + 4 - /
Step 1: Set k to
1
Step 2: Examine the kth symbol. If it is an operand 7
(number), push it onto the stack. If it is an 18
operator, POP two items from the stack,
perform the operation and push result
back onto the stack.

Step 3: If k=n the algorithm terminates and the


answer is on the stack; otherwise add 1 Stack
to k and go to step 2.
62
8 2 5 * + 1 3 2 * + 4 - /
Step 1: Set k to PUSH 4
1
Step 2: Examine the kth symbol. If it is an operand 4
(number), push it onto the stack. If it is an 7
operator, POP two items from the stack,
perform the operation and push result back 18
onto the stack.

Step 3: If k=n the algorithm terminates and the


answer is on the stack; otherwise add 1 Stack
to k and go to step 2.
8 2 5 * + 1 3 2 * + 4 - /
Step 1: Set k to
1
Step 2: Examine the kth symbol. If it is an operand 4
(number), push it onto the stack. If it is an 7
operator, POP two items from the stack,
perform the operation and push result back 18
onto the stack.

Step 3: If k=n the algorithm terminates and the


answer is on the stack; otherwise add 1 Stack
to k and go to step 2.
8 2 5 * + 1 3 2 * + 4 - / 64
POP 4
POP 7
7–4=3
Step 1: Set k to PUSH 3
1
Step 2: Examine the kth symbol. If it is an operand 3
(number), push it onto the stack. If it is an 18
operator, POP two items from the stack,
perform the operation and push result back
onto the stack.

Step 3: If k=n the algorithm terminates and the


answer is on the stack; otherwise add 1 Stack
to k and go to step 2.
65
8 2 5 * + 1 3 2 * + 4 - /
Step 1: Set k to
1
Step 2: Examine the kth symbol. If it is an operand 3
(number), push it onto the stack. If it is an 18
operator, POP two items from the stack,
perform the operation and push result
back onto the stack.

Step 3: If k=n the algorithm terminates and the


answer is on the stack; otherwise add 1 Stack
to k and go to step 2.
8 2 5 * + 1 3 2 * + 4 - / 66
POP 3
POP 18
18 / 3 = 6
Step 1: Set k to PUSH 6
1
Step 2: Examine the kth symbol. If it is an operand 6
(number), push it onto the stack. If it is an
operator, POP two items from the stack,
perform the operation and push result back
onto the stack.

Step 3: If k=n the algorithm terminates and the


answer is on the stack; otherwise add 1 Stack
to k and go to step 2.
67
8 2 5 * + 1 3 2 * + 4 - /
Step 1: Set k to
1
Step 2: Examine the kth symbol. If it is an operand 6
(number), push it onto the stack. If it is an
operator, POP two items from the stack,
perform the operation and push result back
onto the stack.

Step 3: If k=n the algorithm terminates and the


answer is on the stack; otherwise add 1 Stack
to k and go to step 2.
Using Stack
Exercise

▪ Evaluate the following Infix to Postfix notations by showing each stage in stack

Take [Top-1] operator [Top]

▪ 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.

Rule : We use a stack

▪ When an operand is read, output it in a list.

▪ When an operator is read


-Pop until the top of the stack has an element of lower precedence
–Then push it

▪ When ) is found, pop until we find the matching (

▪ ( 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.

Rule : We use a stack

▪ When an operand is read, output it in a list.

▪ When an operator is read


-Pop until the top of the stack has an element of lower precedence
–Then push it

▪ When ) is found, pop until we find the matching (

▪ ( 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.

You might also like