You are on page 1of 40

Data Structures and Algorithms

(ESO207A)

Lecture 12:
• Arithmetic expression evaluation: Complete algorithm using stack

1
Quick Recap of last lecture

2
Nearly balanced Binary Search Tree
Terminology:
size of a binary tree is the number of nodes present in it.

Definition: A binary search tree T is said to be nearly balanced at node v, if


size(left(v)) size(v)
and
size(right(v)) size(v)

Definition: A binary search tree T is said to be nearly balanced if


it is nearly balanced at each node.

3
Nearly balanced Binary Search Tree

very efficient for searching. But …

Question: How to maintain it ?

Rome was not built in a day…


As first step, try to address the following easier problem as homework:
Design an efficient algorithm to transform
a nearly balanced BST to a perfectly balanced BST.

4
Stack: a new data structure
A special kind of list
where all operations (insertion, deletion, query) take place at one end only,
called the top.

top 𝑎1
𝑎2

𝑎𝑛

5
Evaluating
an arithmetic operation

8+3 ∗ 52 – 9∗ 67

6
Evaluating
an arithmetic operation

8+3 ∗ 52 – 9∗ 67

7
Evaluating
an arithmetic operation

25 ∗ 2+ 6 ∗ ¿

8
Evaluating
an arithmetic operation
Challenges

• Precedence of operators

• Associativity of operators (A
A ● B)
B ●C or A ● (B ● C)

• Parentheses
9
Incorporating precedence of operators

Operator Priority

10
Insight into the problem

2
No 8+3 ∗ 5 – 9∗ 67 Yes

Can we execute this Can we execute this


multiplication immediately ? multiplication immediately ?
Let : the operator at position in the expression.
Question: Can we execute operator immediately?
Answer: if
• priority() ?? > priority() Can you use this insight to
• priority() ?? priority() design an algorithm ?
≥ Try for some time…

11
The algorithm

Make a scan of the expression from left to right, and …

… execute an operation as soon as it is possible ...

12
Expression:

13
Priority no. 𝑜1

Expression:

14
Priority no. 𝑜1 𝑜2

Expression:

15
Priority no. 𝑜1 𝑜2 𝑜3

Expression:

16
Priority no. 𝑜1 𝑜2 𝑜3 𝑜4

Expression:

17
Priority no. 𝑜1 𝑜2 𝑜3 𝑜4 𝑜5

Expression:

18
Priority no. 𝑜1 𝑜2 𝑜3 𝑜4 𝑜5 𝑜6

Expression: 𝑛′

19
Priority no. 𝑜1 𝑜2 𝑜3 𝑜4 𝑜6

Expression: 𝑛′ 𝑛
′ ′ 𝑜 6 𝑛7 𝑜 7 …

20
Priority no. 𝑜1 𝑜2 𝑜3 𝑜6

Expression: 𝑛𝑜
′𝑛
′6 𝑛
′ 7 𝑜7 …

21
I am sure now you
know how to handle
now. Isn’t it ?

Priority no. 𝑜1 𝑜2 𝑜3 𝑜6 𝑜7

Expression: 𝑛𝑜
′𝑛
′6 𝑛
′ 7 𝑜7 …

Can you come up with the


implementation of the algorithm ?
Try for some time…

22
Can you see the role of stack ?

23
Expression: 𝑛1𝑜 1𝑛2𝑜2𝑛3𝑜3𝑛4𝑜4𝑛5𝑜 5𝑛6𝑜6𝑛7𝑜7𝑛8

N-stack O-stack 24
for operands for operators
Priority no. 𝑜1

Expression: 𝑜1𝑛2𝑜2𝑛3𝑜3𝑛4𝑜4𝑛5𝑜5𝑛6𝑜6𝑛7𝑜7𝑛8

𝑛1
N-stack O-stack 25
for operands for operators
Priority no. 𝑜1 𝑜2

Expression: 𝑜2𝑛3𝑜3𝑛4𝑜4𝑛5𝑜5𝑛6𝑜6𝑛7𝑜7𝑛8

𝑛2
𝑛1 𝑜1
N-stack O-stack 26
for operands for operators
Priority no. 𝑜1 𝑜2 𝑜3

Expression: 𝑜3𝑛4𝑜4𝑛5𝑜5𝑛6𝑜6𝑛7𝑜7𝑛8

𝑛3
𝑛2 𝑜2
𝑛1 𝑜1
N-stack O-stack 27
for operands for operators
Priority no. 𝑜1 𝑜2 𝑜3 𝑜4

Expression: 𝑜4𝑛5𝑜5𝑛6𝑜6𝑛7𝑜7𝑛8

𝑛4
𝑛3 𝑜3
𝑛2 𝑜2
𝑛1 𝑜1
N-stack O-stack 28
for operands for operators
Priority no. 𝑜1 𝑜2 𝑜3 𝑜4 𝑜5

Expression: 𝑜5𝑛6𝑜6𝑛7𝑜7𝑛8

𝑛5
𝑛4 𝑜4
𝑛3 𝑜3
𝑛2 𝑜2
𝑛1 𝑜1
N-stack O-stack 29
for operands for operators
Priority no. 𝑜1 𝑜2 𝑜3 𝑜4 𝑜5 𝑜6

Expression: 𝑜6𝑛7𝑜7𝑛8
𝑛′¿𝑛5𝑜5𝑛6

𝑛6
𝑛5 𝑜5
𝑛4 𝑜4
𝑛3 𝑜3
𝑛2 𝑜2
𝑛1 𝑜1
N-stack O-stack 30
for operands for operators
Priority no. 𝑜1 𝑜2 𝑜3 𝑜4 𝑜6

Expression: 𝑜6𝑛7𝑜7𝑛8
𝑛′′ ¿ 𝑛4 𝑜4𝑛′

𝑛′ 𝑜4
𝑛4
𝑛3 𝑜3
𝑛2 𝑜2
𝑛1 𝑜1
N-stack O-stack 31
for operands for operators
Priority no. 𝑜1 𝑜2 𝑜3 𝑜6

Expression: 𝑜6𝑛7𝑜7𝑛8

𝑛′′ 𝑜3
𝑛3
𝑛2 𝑜2
𝑛1 𝑜1
N-stack O-stack 32
for operands for operators
Priority no. 𝑜1 𝑜2 𝑜3 𝑜6 𝑜7

Expression: 𝑜7𝑛8

I am sure now you


know how to handle
𝑛7 now. Isn’t it ?
𝑜6
𝑛′′ 𝑜3
𝑛3
𝑛2 𝑜2
𝑛1 𝑜1
N-stack O-stack 33
for operands for operators
A simple algorithm
Priority of $ : Least
push($,O-stack);
While ( ? ) do
{ x  next_token();
Two cases:
x is number : push(x,N-stack);
x is operator :
while( PRIORITY(TOP(O-stack)
? >= ?
PRIORITY(x) )
{ o  POP(O-stack);
• POP two numbers from N-stack
Execute(o); • apply operator o on them
} • place the result back into N-stack
push(x,O-stack);
}

34
Next step

Transforming the solution to Solve


the most general case

35
How to handle parentheses ?

What should we do whenever


we encounter ( in the
expression ?
¿
Evaluate the expression enclosed by the parenthesis
$ before any operator currently present in O-stack.
 should be allowed to enter the stack.
O-stack
So ( must have higher priority than every other operator in the stack.

37
How to handle parentheses ?

What needs to be done when


( is at the top of the O-stack ?

¿
The ( should act as an artificial bottom of the O-stack .
$ every operator that follows (
should be allowed to sit on the top of ( in the stack .
O-stack
So ( must have higher priority than every other operator in the stack.
So ( must have less priority than every other operator in the stack.

A CONTRADICTION !! 38
Take a pause for a few minutes to look closely into the contradiction
and ..
realize that we are talking about priority of ( in 2 different situations.

39
How to handle parentheses ?

When ( is out of the stack When ( is inside the stack

¿
$
O-stack
So ( must have higher priority than every other operator in the stack.
So ( must have less priority than every other operator in the stack.

A CONTRADICTION !! 40
Homework
• Try to handle the apparent contradiction using 2 different
priorities for each operator
– Priority inside the stack
– Priority outside the stack

• Try to handle right associativity of certain operators using


different priorities.

Make sincere attempts to do this homework 

41

You might also like