Professional Documents
Culture Documents
(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.
3
Nearly balanced Binary Search Tree
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
11
The algorithm
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 …
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
34
Next step
35
How to handle parentheses ?
37
How to handle parentheses ?
¿
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 ?
¿
$
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
41