In The Name of Allah
LOGO
Discrete Structures
Instructor: Dr. Ali Movaghar
Sharif University of Technology
LOGO
Fall 1389
Fall 1389
LOGO
Trees:
Discrete Structures
In these slides:
• Introduction to trees
• Applications of trees
• Tree traversal
2
Discrete Structures
Fall 1389
LOGO
3
Discrete Structures
Fall 1389
Introduction to Trees
LOGO
A tree is a connected undirected graph that
contains no circuits.
Theorem: There is a unique simple path between any
two of its nodes.
4
Fall 1389
Introduction to Trees
LOGO
A tree is a connected undirected graph that
contains no circuits.
Theorem: There is a unique simple path between any
two of its nodes.
A (not-necessarily-connected) undirected graph
without simple circuits is called a forest.
Discrete Structures
You can think of it as a set of trees having disjoint sets
of nodes.
5
Fall 1389
Introduction to Trees
LOGO
A tree is a connected undirected graph that
contains no circuits.
Theorem: There is a unique simple path between any
two of its nodes.
A (not-necessarily-connected) undirected graph
without simple circuits is called a forest.
Discrete Structures
You can think of it as a set of trees having disjoint sets
of nodes.
A leaf node in a tree or forest is any pendant or
isolated vertex. An internal node is any non-leaf
vertex (thus it has degree ≥ ? ).
6
Fall 1389
Introduction to Trees
LOGO
A tree is a connected undirected graph that
contains no circuits.
Theorem: There is a unique simple path between any
two of its nodes.
A (not-necessarily-connected) undirected graph
without simple circuits is called a forest.
Discrete Structures
You can think of it as a set of trees having disjoint sets
of nodes.
A leaf node in a tree or forest is any pendant or
isolated vertex. An internal node is any non-leaf
vertex (thus it has degree ≥ 2 ).
7
Fall 1389
Tree and Forest Examples
A Tree:
LOGO
A Forest:
Discrete Structures
Leaves in green, internal nodes in brown.
8
Fall 1389
Rooted Trees
LOGO
A rooted tree is a tree in which one node has
been designated the root.
Discrete Structures
Every edge is (implicitly or explicitly) directed
away from the root.
9
Fall 1389
Rooted Trees
LOGO
A rooted tree is a tree in which one node has
been designated the root.
Every edge is (implicitly or explicitly) directed
away from the root.
Discrete Structures
Concepts related to rooted trees:
Parent, child, siblings, ancestors, descendents, leaf,
internal node, subtree.
10
LOGO
Fall 1389
Rooted Tree Examples
Note that a given unrooted tree with n nodes
yields n different rooted trees.
Discrete Structures
root
Same tree
except
for choice
of root
root
11
LOGO
Fall 1389
Rooted-Tree Terminology Exercise
Find the parent,
children, siblings,
ancestors, &
descendants
of node f.
o
r
d
m
b
root
a
e
Discrete Structures
n
h
c
g
q
i
f
p
j
k
l
12
Fall 1389
n-ary trees
LOGO
A rooted tree is called n-ary if every vertex has
no more than n children.
Discrete Structures
It is called full if every internal (non-leaf) vertex
has exactly n children.
13
Fall 1389
n-ary trees
LOGO
A rooted tree is called n-ary if every vertex has
no more than n children.
It is called full if every internal (non-leaf) vertex
has exactly n children.
Discrete Structures
A 2-ary tree is called a binary tree.
These are handy for describing sequences of yesno decisions.
• Example: Comparisons in binary search algorithm.
14
Discrete Structures
Fall 1389
Which Tree is Binary?
LOGO
Theorem: A given rooted tree is a binary tree iff
every node other than the root has degree ≤ ___, and
the root has degree ≤ ___.
15
Discrete Structures
Fall 1389
Ordered Rooted Tree
LOGO
This is just a rooted tree in which the children
of each internal node are ordered.
16
Fall 1389
Ordered Rooted Tree
LOGO
This is just a rooted tree in which the children
of each internal node are ordered.
In ordered binary trees, we can define:
Discrete Structures
left child, right child
left subtree, right subtree
17
Fall 1389
Ordered Rooted Tree
LOGO
This is just a rooted tree in which the children
of each internal node are ordered.
In ordered binary trees, we can define:
Discrete Structures
left child, right child
left subtree, right subtree
For n-ary trees with n>2, can use terms like
“leftmost”, “rightmost,” etc.
18
Fall 1389
Trees as Models
LOGO
Can use trees to model the following:
Saturated hydrocarbons
Organizational structures
Computer file systems
Discrete Structures
In each case, would you use a rooted or a nonrooted tree?
19
Discrete Structures
Fall 1389
Some Tree Theorems
LOGO
Any tree with n nodes has e = n−1 edges.
Proof: Consider removing leaves.
20
Fall 1389
Some Tree Theorems
LOGO
Any tree with n nodes has e = n−1 edges.
Proof: Consider removing leaves.
A full m-ary tree with i internal nodes has n=mi+1
nodes, and =(m−1)i+1 leaves.
Discrete Structures
Proof: There are mi children of internal nodes, plus the
root. And, = n−i = (m−1)i+1. □
21
Fall 1389
Some Tree Theorems
LOGO
Any tree with n nodes has e = n−1 edges.
Proof: Consider removing leaves.
A full m-ary tree with i internal nodes has n=mi+1
nodes, and =(m−1)i+1 leaves.
Discrete Structures
Proof: There are mi children of internal nodes, plus the
root. And, = n−i = (m−1)i+1. □
Thus, when m is known and the tree is full, we can
compute all four of the values e, i, n, and , given any
one of them.
22
Fall 1389
Some More Tree Theorems
LOGO
Definition: The level of a node is the length of the
simple path from the root to the node.
Discrete Structures
The height of a tree is maximum node level.
A rooted m-ary tree with height h is called balanced if all
leaves are at levels h or h−1.
23
Fall 1389
Some More Tree Theorems
LOGO
Definition: The level of a node is the length of the
simple path from the root to the node.
The height of a tree is maximum node level.
A rooted m-ary tree with height h is called balanced if all
leaves are at levels h or h−1.
Discrete Structures
Theorem: There are at most mh leaves in an m-ary
tree of height h.
Corollary: An m-ary tree with leaves has height
h≥logm . If m is full and balanced then h=logm.
24
Discrete Structures
Fall 1389
LOGO
25
Discrete Structures
Fall 1389
Applications of Trees
LOGO
Binary search trees
A simple data structure for sorted lists
26
Fall 1389
Applications of Trees
LOGO
Binary search trees
A simple data structure for sorted lists
Decision trees
Discrete Structures
Minimum comparisons in sorting algorithms
27
Fall 1389
Applications of Trees
LOGO
Binary search trees
A simple data structure for sorted lists
Decision trees
Minimum comparisons in sorting algorithms
Prefix codes
Discrete Structures
Huffman coding
28
Fall 1389
Applications of Trees
LOGO
Binary search trees
A simple data structure for sorted lists
Decision trees
Minimum comparisons in sorting algorithms
Prefix codes
Discrete Structures
Huffman coding
Game trees
29
Fall 1389
Binary Search Trees
LOGO
A representation for sorted sets of items.
Supports the following operations in Θ(log n)
average-case time:
• Searching for an existing item.
• Inserting a new item, if not already present.
Discrete Structures
Supports printing out all items in Θ(n) time.
Note that inserting into a plain sequence ai would instead take Θ(n) worstcase time.
30
LOGO
Fall 1389
Binary Search Tree Format
Items are stored at
individual tree nodes.
We arrange for the
tree to always obey
this invariant:
Example:
7
3
12
Discrete Structures
For every item x,
• Every node in x’s left
subtree is less than x.
• Every node in x’s right
subtree is greater than x.
1
0
5
2
9
8
15
11
31
Discrete Structures
Fall 1389
Recursive Binary Tree Insert
LOGO
procedure insert(T: binary tree, x: item)
v := root[T]
if v = null then begin
root[T] := x; return “Done” end
else if v = x return “Already present”
else if x < v then
return insert(leftSubtree[T], x)
else {must be x > v}
return insert(rightSubtree[T], x)
32
Discrete Structures
Fall 1389
Decision Trees
LOGO
A decision tree represents a decision-making process.
Each possible “decision point” or situation is represented
by a node.
Each possible choice that could be made at that decision
point is represented by an edge to a child node.
33
Fall 1389
Decision Trees
LOGO
A decision tree represents a decision-making process.
Each possible “decision point” or situation is represented
by a node.
Each possible choice that could be made at that decision
point is represented by an edge to a child node.
Discrete Structures
In the extended decision trees used in decision
analysis, we also include nodes that represent random
events and their outcomes.
34
LOGO
Fall 1389
Coin-Weighing Problem
Imagine you have 8 coins, one
of which is a lighter counterfeit,
and a free-beam balance.
Discrete Structures
No scale of weight markings
is required for this problem!
How many weighings are
needed to guarantee that the
counterfeit coin will be found?
?
35
Fall 1389
As a Decision-Tree Problem
In each situation, we pick two disjoint and equalsize subsets of coins to put on the scale.
The balance then
“decides” whether
to tip left, tip right,
or stay balanced.
Discrete Structures
LOGO
A given sequence of
weighings thus yields
a decision tree with
branching factor 3.
36
Fall 1389
Applying the Tree Height Theorem
LOGO
The decision tree must have at least 8 leaf
nodes, since there are 8 possible outcomes.
In terms of which coin is the counterfeit one.
Recall the tree-height theorem, h≥logm.
Thus the decision tree must have height
h ≥ log38 = 1.893… = 2.
Discrete Structures
Let’s see if we solve the problem with only 2
weighings…
37
Fall 1389
General Solution Strategy
LOGO
The problem is an example of searching for 1 unique particular
item, from among a list of n otherwise identical items.
Somewhat analogous to the adage of “searching for a needle in haystack.”
Armed with our balance, we can attack the problem using a divideand-conquer strategy, like what’s done in binary search.
We want to narrow down the set of possible locations where the desired item
(coin) could be found down from n to just 1, in a logarithmic fashion.
Discrete Structures
Each weighing has 3 possible outcomes.
Thus, we should use it to partition the search space into 3 pieces that are as
close to equal-sized as possible.
This strategy will lead to the minimum possible worst-case number
of weighings required.
38
Fall 1389
General Balance Strategy
LOGO
On each step, put n/3 of the n coins to be
searched on each side of the scale.
If the scale tips to the left, then:
• The lightweight fake is in the right set of n/3 ≈ n/3 coins.
If the scale tips to the right, then:
• The lightweight fake is in the left set of n/3 ≈ n/3 coins.
If the scale stays balanced, then:
Discrete Structures
• The fake is in the remaining set of n − 2n/3 ≈ n/3 coins that
were not weighed!
Except if n mod 3 = 1 then we can do a little better
by weighing n/3 of the coins on each side.
You can prove that this strategy always leads to a balanced 3-ary tree.
39
Fall 1389
Coin Balancing Decision Tree
Here’s what the tree looks like in our case:
left:
123
123 vs 456
balanced:
78
right:
456
4 vs. 5
1 vs. 2
Discrete Structures
LOGO
L:1
R:2
B:3
L:4 R:5
7 vs. 8
B:6
L:7
R:8
40
Discrete Structures
Fall 1389
LOGO
41
Discrete Structures
Fall 1389
Universal Address Systems
LOGO
To totally order the vertices of on ordered rooted
tree:
1. Label the root with the integer 0.
2. Label its k children (at level 1) from left to right
with 1, 2, 3, …, k.
3. For each vertex v at level n with label A, label
its kv children, from left to right, with A.1, A.2,
A.3, …, A.kv.
42
Discrete Structures
Fall 1389
Universal Address Systems
LOGO
To totally order the vertices of on ordered rooted
tree:
1. Label the root with the integer 0.
2. Label its k children (at level 1) from left to right
with 1, 2, 3, …, k.
3. For each vertex v at level n with label A, label
its kv children, from left to right, with A.1, A.2,
A.3, …, A.kv.
This labeling is called the universal address system
of the ordered rooted tree.
43
Discrete Structures
Fall 1389
Universal Address Systems
LOGO
44
Fall 1389
Traversal Algorithms
LOGO
A traversal algorithm is a procedure for
systematically visiting every vertex of an
ordered rooted tree
Discrete Structures
An ordered rooted tree is a rooted tree where
the children of each internal vertex are ordered
45
Fall 1389
Traversal Algorithms
LOGO
A traversal algorithm is a procedure for
systematically visiting every vertex of an
ordered rooted tree
An ordered rooted tree is a rooted tree where
the children of each internal vertex are ordered
Discrete Structures
The three common traversals are:
Preorder traversal
Inorder traversal
Postorder traversal
46
LOGO
Fall 1389
Traversal
Let T be an ordered rooted tree with root r.
Suppose T1, T2, …,Tn are the subtrees at r
from left to right in T.
Discrete Structures
r
T1
T2
Tn
47
LOGO
Discrete Structures
Fall 1389
Preorder Traversal
Step 1: Visit r
Step 2: Visit T1 in preorder
Step 3: Visit T2 in preorder
.
.
.
Step n+1: Visit Tn in preorder
r
T1
T2
Tn
48
LOGO
Discrete Structures
Fall 1389
Preorder Traversal
Step 1: Visit r
Step 2: Visit T1 in preorder
Step 3: Visit T2 in preorder
.
.
.
Step n+1: Visit Tn in preorder
r
T1
T2
Tn
49
LOGO
Discrete Structures
Fall 1389
Preorder Traversal
Step 1: Visit r
Step 2: Visit T1 in preorder
Step 3: Visit T2 in preorder
.
.
.
Step n+1: Visit Tn in preorder
r
T1
T2
Tn
50
LOGO
Discrete Structures
Fall 1389
Preorder Traversal
Step 1: Visit r
Step 2: Visit T1 in preorder
Step 3: Visit T2 in preorder
.
.
.
Step n+1: Visit Tn in preorder
r
T1
T2
Tn
51
LOGO
Discrete Structures
Fall 1389
Preorder Traversal
Step 1: Visit r
Step 2: Visit T1 in preorder
Step 3: Visit T2 in preorder
.
.
.
Step n+1: Visit Tn in preorder
r
T1
T2
Tn
52
LOGO
Fall 1389
Example
Tree:
M
A
Discrete Structures
J
Y
R
E
H
P
Q
T
Visiting sequence:
M
53
LOGO
Fall 1389
Example
Tree:
M
A
Discrete Structures
J
Y
R
E
H
P
Q
T
Visiting sequence:
M
A
54
LOGO
Fall 1389
Example
Tree:
M
A
Discrete Structures
J
Y
R
E
H
P
Q
T
Visiting sequence:
M
A
J
55
LOGO
Fall 1389
Example
Tree:
M
A
Y
Discrete Structures
J
R
E
H
P
Q
T
Visiting sequence:
M
A
J
Y
56
LOGO
Fall 1389
Example
Tree:
M
A
Y
Discrete Structures
J
R
E
H
P
Q
T
Visiting sequence:
M
A
J
Y
R
57
LOGO
Fall 1389
Example
Tree:
M
A
Y
Discrete Structures
J
R
E
H
P
Q
T
Visiting sequence:
M
A
J
Y
R
H
58
LOGO
Fall 1389
Example
Tree:
M
A
Y
Discrete Structures
J
R
E
H
P
Q
T
Visiting sequence:
M
A
J
Y
R
H
P
59
LOGO
Fall 1389
Example
Tree:
M
A
Y
Discrete Structures
J
R
E
H
P
Q
T
Visiting sequence:
M
A
J
Y
R
H
P
Q
60
LOGO
Fall 1389
Example
Tree:
M
A
Y
Discrete Structures
J
R
E
H
P
Q
T
Visiting sequence:
M
A
J
Y
R
H
P
Q
T
61
LOGO
Fall 1389
Example
Tree:
M
A
Y
Discrete Structures
J
R
E
H
P
Q
T
Visiting sequence:
M
A
J
Y
R
H
P
Q
T
E
62
Discrete Structures
Fall 1389
The Preorder Traversal of T
LOGO
In which order
does a preorder
traversal visit
the vertices in
the ordered
rooted tree T
shown to the
left?
Remember:
Visit root, then
visit subtrees
left to right.
63
Discrete Structures
Fall 1389
The Preorder Traversal of T
LOGO
64
Discrete Structures
Fall 1389
The Preorder Traversal of T
LOGO
65
Discrete Structures
Fall 1389
The Preorder Traversal of T
LOGO
66
LOGO
Discrete Structures
Fall 1389
The Preorder Traversal of T
67
LOGO
Discrete Structures
Fall 1389
Inorder Traversal
Step 1: Visit T1 in inorder
Step 2: Visit r
Step 3: Visit T2 in inorder
.
.
.
Step n+1: Visit Tn in inorder
r
T1
T2
Tn
68
LOGO
Discrete Structures
Fall 1389
Inorder Traversal
Step 1: Visit T1 in inorder
Step 2: Visit r
Step 3: Visit T2 in inorder
.
.
.
Step n+1: Visit Tn in inorder
r
T1
T2
Tn
69
LOGO
Discrete Structures
Fall 1389
Inorder Traversal
Step 1: Visit T1 in inorder
Step 2: Visit r
Step 3: Visit T2 in inorder
.
.
.
Step n+1: Visit Tn in inorder
r
T1
T2
Tn
70
LOGO
Discrete Structures
Fall 1389
Inorder Traversal
Step 1: Visit T1 in inorder
Step 2: Visit r
Step 3: Visit T2 in inorder
.
.
.
Step n+1: Visit Tn in inorder
r
T1
T2
Tn
71
LOGO
Discrete Structures
Fall 1389
Inorder Traversal
Step 1: Visit T1 in inorder
Step 2: Visit r
Step 3: Visit T2 in inorder
.
.
.
Step n+1: Visit Tn in inorder
r
T1
T2
Tn
72
LOGO
Fall 1389
Example
Tree:
M
A
Discrete Structures
J
Y
R
E
H
P
Q
T
Visiting sequence:
J
73
LOGO
Fall 1389
Example
Tree:
M
A
Discrete Structures
J
Y
R
E
H
P
Q
T
Visiting sequence:
J
A
74
LOGO
Fall 1389
Example
Tree:
M
A
Discrete Structures
J
Y
R
E
H
P
Q
T
Visiting sequence:
J
A
M
75
LOGO
Fall 1389
Example
Tree:
M
A
Y
Discrete Structures
J
R
E
H
P
Q
T
Visiting sequence:
J
A
M
R
76
LOGO
Fall 1389
Example
Tree:
M
A
Y
Discrete Structures
J
R
E
H
P
Q
T
Visiting sequence:
J
A
M
R
Y
77
LOGO
Fall 1389
Example
Tree:
M
A
Y
Discrete Structures
J
R
E
H
P
Q
T
Visiting sequence:
J
A
M
R
Y
P
78
LOGO
Fall 1389
Example
Tree:
M
A
Y
Discrete Structures
J
R
E
H
P
Q
T
Visiting sequence:
J
A
M
R
Y
P
H
79
LOGO
Fall 1389
Example
Tree:
M
A
Y
Discrete Structures
J
R
E
H
P
Q
T
Visiting sequence:
J
A
M
R
Y
P
H
Q
80
LOGO
Fall 1389
Example
Tree:
M
A
Y
Discrete Structures
J
R
E
H
P
Q
T
Visiting sequence:
J
A
M
R
Y
P
H
Q
T
81
LOGO
Fall 1389
Example
Tree:
M
A
Y
Discrete Structures
J
R
E
H
P
Q
T
Visiting sequence:
J
A
M
R
Y
P
H
Q
T
E
82
Discrete Structures
Fall 1389
The Inorder Traversal of T
LOGO
In which order
does an inorder
traversal visit the
vertices in the
ordered rooted tree
T shown to the
left?
Remember:
Visit leftmost tree,
visit root, visit
other subtrees left
to right.
83
LOGO
Discrete Structures
Fall 1389
The Inorder Traversal of T
84
Discrete Structures
Fall 1389
The Inorder Traversal of T
LOGO
85
Discrete Structures
Fall 1389
The Inorder Traversal of T
LOGO
86
Discrete Structures
Fall 1389
The Inorder Traversal of T
LOGO
87
LOGO
Discrete Structures
Fall 1389
Postorder Traversal
Step 1: Visit T1 in postorder
Step 2: Visit T2 in postorder
.
.
.
Step n: Visit Tn in postorder
Step n+1: Visit r
r
T1
T2
Tn
88
LOGO
Discrete Structures
Fall 1389
Postorder Traversal
Step 1: Visit T1 in postorder
Step 2: Visit T2 in postorder
.
.
.
Step n: Visit Tn in postorder
Step n+1: Visit r
r
T1
T2
Tn
89
LOGO
Discrete Structures
Fall 1389
Postorder Traversal
Step 1: Visit T1 in postorder
Step 2: Visit T2 in postorder
.
.
.
Step n: Visit Tn in postorder
Step n+1: Visit r
r
T1
T2
Tn
90
LOGO
Discrete Structures
Fall 1389
Postorder Traversal
Step 1: Visit T1 in postorder
Step 2: Visit T2 in postorder
.
.
.
Step n: Visit Tn in postorder
Step n+1: Visit r
r
T1
T2
Tn
91
LOGO
Discrete Structures
Fall 1389
Postorder Traversal
Step 1: Visit T1 in postorder
Step 2: Visit T2 in postorder
.
.
.
Step n: Visit Tn in postorder
Step n+1: Visit r
r
T1
T2
Tn
92
LOGO
Fall 1389
Example
Tree:
M
A
Discrete Structures
J
Y
R
E
H
P
Q
T
Visiting sequence:
J
93
LOGO
Fall 1389
Example
Tree:
M
A
Discrete Structures
J
Y
R
E
H
P
Q
T
Visiting sequence:
J
A
94
LOGO
Fall 1389
Example
Tree:
M
A
Discrete Structures
J
Y
R
E
H
P
Q
T
Visiting sequence:
J
A
R
95
LOGO
Fall 1389
Example
Tree:
M
A
Y
Discrete Structures
J
R
E
H
P
Q
T
Visiting sequence:
J
A
R
P
96
LOGO
Fall 1389
Example
Tree:
M
A
Y
Discrete Structures
J
R
E
H
P
Q
T
Visiting sequence:
J
A
R
P
Q
97
LOGO
Fall 1389
Example
Tree:
M
A
Y
Discrete Structures
J
R
E
H
P
Q
T
Visiting sequence:
J
A
R
P
Q
T
98
LOGO
Fall 1389
Example
Tree:
M
A
Y
Discrete Structures
J
R
E
H
P
Q
T
Visiting sequence:
J
A
R
P
Q
T
H
99
LOGO
Fall 1389
Example
Tree:
M
A
Y
Discrete Structures
J
R
E
H
P
Q
T
Visiting sequence:
J
A
R
P
Q
T
H
Y
100
LOGO
Fall 1389
Example
Tree:
M
A
Y
Discrete Structures
J
R
E
H
P
Q
T
Visiting sequence:
J
A
R
P
Q
T
H
Y
E
101
LOGO
Fall 1389
Example
Tree:
M
A
Y
Discrete Structures
J
R
E
H
P
Q
T
Visiting sequence:
J
A
R
P
Q
T
H
Y
E
M
102
Discrete Structures
Fall 1389
The Postorder Traversal of T
LOGO
In which order
does a postorder
traversal visit
the vertices in
the ordered
rooted tree T
shown to the
left?
Remember:
Visit subtrees
left to right, then
visit root.
103
Discrete Structures
Fall 1389
The Postorder Traversal of T
LOGO
104
Discrete Structures
Fall 1389
The Postorder Traversal of T
LOGO
105
Discrete Structures
Fall 1389
The Postorder Traversal of T
LOGO
106
Discrete Structures
Fall 1389
The Postorder Traversal of T
LOGO
107
Fall 1389
Representing Arithmetic Expressions
LOGO
Complicated arithmetic expressions can be
represented by an ordered rooted tree
Discrete Structures
Internal vertices represent operators
Leaves represent operands
108
Fall 1389
Representing Arithmetic Expressions
LOGO
Complicated arithmetic expressions can be
represented by an ordered rooted tree
Internal vertices represent operators
Leaves represent operands
Discrete Structures
Build the tree bottom-up
Construct smaller subtrees
Incorporate the smaller subtrees as part of
larger subtrees
109
Discrete Structures
Fall 1389
Example
LOGO
(x+y)2 + (x-3)/(y+2)
110
Fall 1389
Example
LOGO
(x+y)2 + (x-3)/(y+2)
Discrete Structures
+
x
y
111
Fall 1389
Example
LOGO
(x+y)2 + (x-3)/(y+2)
2
Discrete Structures
+
x
y
112
Fall 1389
Example
LOGO
(x+y)2 + (x-3)/(y+2)
2
Discrete Structures
+
x
y
–
x 3
113
LOGO
Fall 1389
Example
(x+y)2 + (x-3)/(y+2)
2
Discrete Structures
+
x
y
–
+
x 3 y 2
114
LOGO
Fall 1389
Example
(x+y)2 + (x-3)/(y+2)
2
Discrete Structures
+
x
/
y
–
+
x 3 y 2
115
LOGO
Fall 1389
Example
(x+y)2 + (x-3)/(y+2)
+
2
Discrete Structures
+
x
/
y
–
+
x 3 y 2
116
Fall 1389
LOGO
Evaluating Prefix Notation
In an prefix expression, a binary operator
precedes its two operands
The expression is evaluated right-left
Discrete Structures
Look for the first operator from the right
Evaluate the operator with the two operands
immediately to its right
117
Discrete Structures
Fall 1389
Example
LOGO
+ / + 2 2 2 / – 3 2 + 1 0
118
Discrete Structures
Fall 1389
Example
LOGO
+ / + 2 2 2 / – 3 2 + 1 0
119
Fall 1389
Example
LOGO
+ / + 2 2 2 / – 3 2 + 1 0
Discrete Structures
+ / + 2 2 2 / – 3 2 1
120
Fall 1389
Example
LOGO
+ / + 2 2 2 / – 3 2 + 1 0
Discrete Structures
+ / + 2 2 2 / – 3 2 1
121
Fall 1389
Example
LOGO
+ / + 2 2 2 / – 3 2 + 1 0
+ / + 2 2 2 / – 3 2 1
Discrete Structures
+ / + 2 2 2 / 1 1
122
Fall 1389
Example
LOGO
+ / + 2 2 2 / – 3 2 + 1 0
+ / + 2 2 2 / – 3 2 1
Discrete Structures
+ / + 2 2 2 / 1 1
123
Fall 1389
Example
LOGO
+ / + 2 2 2 / – 3 2 + 1 0
+ / + 2 2 2 / – 3 2 1
+ / + 2 2 2 / 1 1
Discrete Structures
+ / + 2 2 2 1
124
Fall 1389
Example
LOGO
+ / + 2 2 2 / – 3 2 + 1 0
+ / + 2 2 2 / – 3 2 1
+ / + 2 2 2 / 1 1
Discrete Structures
+ / + 2 2 2 1
125
Fall 1389
Example
LOGO
+ / + 2 2 2 / – 3 2 + 1 0
+ / + 2 2 2 / – 3 2 1
+ / + 2 2 2 / 1 1
Discrete Structures
+ / + 2 2 2 1
+ / 4 2 1
126
Fall 1389
Example
LOGO
+ / + 2 2 2 / – 3 2 + 1 0
+ / + 2 2 2 / – 3 2 1
+ / + 2 2 2 / 1 1
Discrete Structures
+ / + 2 2 2 1
+ / 4 2 1
127
Fall 1389
Example
LOGO
+ / + 2 2 2 / – 3 2 + 1 0
+ / + 2 2 2 / – 3 2 1
+ / + 2 2 2 / 1 1
Discrete Structures
+ / + 2 2 2 1
+ / 4 2 1
+ 2 1
128
Fall 1389
Example
LOGO
+ / + 2 2 2 / – 3 2 + 1 0
+ / + 2 2 2 / – 3 2 1
+ / + 2 2 2 / 1 1
Discrete Structures
+ / + 2 2 2 1
+ / 4 2 1
+ 2 1
129
Fall 1389
Example
LOGO
+ / + 2 2 2 / – 3 2 + 1 0
+ / + 2 2 2 / – 3 2 1
+ / + 2 2 2 / 1 1
Discrete Structures
+ / + 2 2 2 1
+ / 4 2 1
+ 2 1
3
130
Fall 1389
Postfix Notation (Reverse Polish)
LOGO
• Traverse in postorder
+
Discrete Structures
+
x
/
2
y
–
+
x 3 y 2
131
Fall 1389
Postfix Notation (Reverse Polish)
LOGO
• Traverse in postorder
+
Discrete Structures
+
x
/
2
y
–
+
x 3 y 2
x
132
Fall 1389
Postfix Notation (Reverse Polish)
LOGO
• Traverse in postorder
+
Discrete Structures
+
x
/
2
y
–
+
x 3 y 2
x y
133
Fall 1389
Postfix Notation (Reverse Polish)
LOGO
• Traverse in postorder
+
Discrete Structures
+
x
/
2
y
–
+
x 3 y 2
x y +
134
Fall 1389
Postfix Notation (Reverse Polish)
LOGO
• Traverse in postorder
+
Discrete Structures
+
x
/
2
y
–
+
x 3 y 2
x y + 2
135
Fall 1389
Postfix Notation (Reverse Polish)
LOGO
• Traverse in postorder
+
Discrete Structures
+
x
/
2
y
–
+
x 3 y 2
x y + 2
136
Fall 1389
Postfix Notation (Reverse Polish)
LOGO
• Traverse in postorder
+
Discrete Structures
+
x
/
2
y
–
+
x 3 y 2
x y + 2 x
137
Fall 1389
Postfix Notation (Reverse Polish)
LOGO
• Traverse in postorder
+
Discrete Structures
+
x
/
2
y
–
+
x 3 y 2
x y + 2 x 3
138
Fall 1389
Postfix Notation (Reverse Polish)
LOGO
• Traverse in postorder
+
Discrete Structures
+
x
/
2
y
–
+
x 3 y 2
x y + 2 x 3 –
139
Fall 1389
Postfix Notation (Reverse Polish)
LOGO
• Traverse in postorder
+
Discrete Structures
+
x
/
2
y
–
+
x 3 y 2
x y + 2 x 3 – y
140
Fall 1389
Postfix Notation (Reverse Polish)
LOGO
• Traverse in postorder
+
Discrete Structures
+
x
/
2
y
–
+
x 3 y 2
x y + 2 x 3 – y 2
141
Fall 1389
Postfix Notation (Reverse Polish)
LOGO
• Traverse in postorder
+
Discrete Structures
+
x
/
2
y
–
+
x 3 y 2
x y + 2 x 3 – y 2 +
142
Fall 1389
Postfix Notation (Reverse Polish)
LOGO
• Traverse in postorder
+
Discrete Structures
+
x
/
2
y
–
+
x 3 y 2
x y + 2 x 3 – y 2 + /
143
Fall 1389
Postfix Notation (Reverse Polish)
LOGO
• Traverse in postorder
+
Discrete Structures
+
x
/
2
y
–
+
x 3 y 2
x y + 2 x 3 – y 2 + /+
144
Fall 1389
Evaluating Postfix Notation
In an postfix expression, a binary
operator follows its two operands
LOGO
The expression is evaluated left-right
Discrete Structures
Look for the first operator from the left
Evaluate the operator with the two
operands immediately to its left
145
Fall 1389
Example
LOGO
Discrete Structures
2 2 + 2 / 3 2 – 1 0 + / +
146
Fall 1389
Example
LOGO
Discrete Structures
2 2 + 2 / 3 2 – 1 0 + / +
147
Fall 1389
Example
LOGO
2 2 + 2 / 3 2 – 1 0 + / +
Discrete Structures
4 2 / 3 2 – 1 0 + / +
148
Fall 1389
Example
LOGO
2 2 + 2 / 3 2 – 1 0 + / +
Discrete Structures
4 2 / 3 2 – 1 0 + / +
149
Fall 1389
Example
LOGO
2 2 + 2 / 3 2 – 1 0 + / +
4 2 / 3 2 – 1 0 + / +
Discrete Structures
2 3 2 – 1 0 + / +
150
Fall 1389
Example
LOGO
2 2 + 2 / 3 2 – 1 0 + / +
4 2 / 3 2 – 1 0 + / +
Discrete Structures
2 3 2 – 1 0 + / +
151
Fall 1389
Example
LOGO
2 2 + 2 / 3 2 – 1 0 + / +
4 2 / 3 2 – 1 0 + / +
2 3 2 – 1 0 + / +
Discrete Structures
2 1 1 0 + / +
152
Fall 1389
Example
LOGO
2 2 + 2 / 3 2 – 1 0 + / +
4 2 / 3 2 – 1 0 + / +
2 3 2 – 1 0 + / +
Discrete Structures
2 1 1 0 + / +
153
Fall 1389
Example
LOGO
2 2 + 2 / 3 2 – 1 0 + / +
4 2 / 3 2 – 1 0 + / +
2 3 2 – 1 0 + / +
Discrete Structures
2 1 1 0 + / +
2 1 1 / +
154
Fall 1389
Example
LOGO
2 2 + 2 / 3 2 – 1 0 + / +
4 2 / 3 2 – 1 0 + / +
2 3 2 – 1 0 + / +
Discrete Structures
2 1 1 0 + / +
2 1 1 / +
155
LOGO
Fall 1389
Example
2 2 + 2 / 3 2 – 1 0 + / +
4 2 / 3 2 – 1 0 + / +
2 3 2 – 1 0 + / +
Discrete Structures
2 1 1 0 + / +
2 1 1 / +
2 1 +
156
LOGO
Fall 1389
Example
2 2 + 2 / 3 2 – 1 0 + / +
4 2 / 3 2 – 1 0 + / +
2 3 2 – 1 0 + / +
Discrete Structures
2 1 1 0 + / +
2 1 1 / +
2 1 +
157
LOGO
Fall 1389
Example
2 2 + 2 / 3 2 – 1 0 + / +
4 2 / 3 2 – 1 0 + / +
2 3 2 – 1 0 + / +
Discrete Structures
2 1 1 0 + / +
2 1 1 / +
2 1 +
3
158
Fall 1389
Spanning Trees
LOGO
You will learn about the following topics in
tree later in other courses:
Discrete Structures
Spanning Trees
Minimum Spanning Trees
159
LOGO
Good Luck
LOGO
© Copyright 2026 Paperzz