LOGO. Trees: In these slides: Introduction to trees Applications of trees Tree traversal 2.

160
LOGO LOGO Discrete Structures Instructor: Dr. Ali Movaghar Sharif University of Technology Fall 1389 In The Name of Allah

Transcript of LOGO. Trees: In these slides: Introduction to trees Applications of trees Tree traversal 2.

Page 1: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

LOGO

D i s c r e t e S t r u c t u r e s

Instructor: Dr. Ali Movaghar

Sharif University of Technology Fall 1389

In The Name of Allah

Page 2: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

2

Trees:

In these slides: • Introduction to trees• Applications of trees• Tree traversal

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 3: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

3

Introduction to

Trees

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 4: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

4

Introduction to TreesA tree is a connected undirected graph that

contains no circuits. Theorem: There is a unique simple path between any

two of its nodes.

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 5: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

5

Introduction to TreesA 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. You can think of it as a set of trees having disjoint sets

of nodes.

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 6: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

6

Introduction to TreesA 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. 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 ≥ ? ).

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 7: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

7

Introduction to TreesA 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. 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 ).

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 8: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

8

Tree and Forest Examples

A Tree:A Forest:

Leaves in green, internal nodes in brown.

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 9: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

9

Rooted Trees

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.

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 10: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

10

Rooted Trees

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.

Concepts related to rooted trees: Parent, child, siblings, ancestors, descendents, leaf,

internal node, subtree.

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 11: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

11

Rooted Tree Examples

Note that a given unrooted tree with n nodes yields n different rooted trees.

root

root

Same tree except

for choiceof root

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 12: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

12

Rooted-Tree Terminology Exercise

Find the parent,children, siblings,ancestors, & descendants of node f.

a

b

c

d

e

f

g

h

i

j k l

m

no

p

q

r

root

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 13: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

13

n-ary trees

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.

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 14: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

14

n-ary trees

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.

A 2-ary tree is called a binary tree. These are handy for describing sequences of yes-

no decisions.• Example: Comparisons in binary search algorithm.

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 15: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

15

Which Tree is Binary?Theorem: A given rooted tree is a binary tree iff

every node other than the root has degree ≤ ___, and the root has degree ≤ ___.

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 16: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

16

Ordered Rooted TreeThis is just a rooted tree in which the children

of each internal node are ordered.

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 17: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

17

Ordered Rooted TreeThis is just a rooted tree in which the children

of each internal node are ordered.

In ordered binary trees, we can define: left child, right child left subtree, right subtree

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 18: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

18

Ordered Rooted TreeThis is just a rooted tree in which the children

of each internal node are ordered.

In ordered binary trees, we can define: left child, right child left subtree, right subtree

For n-ary trees with n>2, can use terms like “leftmost”, “rightmost,” etc.

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 19: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

19

Trees as Models

Can use trees to model the following: Saturated hydrocarbons Organizational structures Computer file systems

In each case, would you use a rooted or a non-rooted tree?

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 20: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

20

Some Tree TheoremsAny tree with n nodes has e = n−1 edges.

Proof: Consider removing leaves.

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 21: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

21

Some Tree TheoremsAny 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. Proof: There are mi children of internal nodes, plus the

root. And, = n−i = (m−1)i+1. □

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 22: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

22

Some Tree TheoremsAny 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. 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.

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 23: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

23

Some More Tree TheoremsDefinition: 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.

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 24: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

24

Some More Tree TheoremsDefinition: 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.

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.

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 25: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

25

Applications of

Trees

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 26: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

26

Applications of Trees

Binary search trees A simple data structure for sorted lists

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 27: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

27

Applications of Trees

Binary search trees A simple data structure for sorted lists

Decision trees Minimum comparisons in sorting algorithms

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 28: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

28

Applications of Trees

Binary search trees A simple data structure for sorted lists

Decision trees Minimum comparisons in sorting algorithms

Prefix codes Huffman coding

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 29: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

29

Applications of Trees

Binary search trees A simple data structure for sorted lists

Decision trees Minimum comparisons in sorting algorithms

Prefix codes Huffman coding

Game trees

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 30: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

30

Binary Search Trees

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.

Supports printing out all items in Θ(n) time.

Note that inserting into a plain sequence ai would instead take Θ(n) worst-case time.

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 31: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

31

Binary Search Tree Format

Items are stored at individual tree nodes.

We arrange for the tree to always obey this invariant: 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.

7

3 12

1 5 9 15

0 2 8 11

Example:

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 32: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

32

Recursive Binary Tree Insert

procedure insert(T: binary tree, x: item)v := root[T]if v = null then begin

root[T] := x; return “Done” endelse 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)

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 33: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

33

Decision TreesA 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.

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 34: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

34

Decision TreesA 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.

In the extended decision trees used in decision analysis, we also include nodes that represent random events and their outcomes.

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 35: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

35

Coin-Weighing Problem

Imagine you have 8 coins, oneof which is a lighter counterfeit, and a free-beam balance. No scale of weight markings

is required for this problem!

How many weighings are needed to guarantee that the counterfeit coin will be found?

?

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 36: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

36

As a Decision-Tree Problem

In each situation, we pick two disjoint and equal-size subsets of coins to put on the scale.

The balance then“decides” whether to tip left, tip right, or stay balanced.

A given sequence ofweighings thus yieldsa decision tree withbranching factor 3.

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 37: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

37

Applying the Tree Height TheoremThe 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.Let’s see if we solve the problem with only 2

weighings…

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 38: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

38

General Solution Strategy

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 divide-and-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. 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.

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 39: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

39

General Balance StrategyOn 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:

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

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 40: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

40

Coin Balancing Decision Tree

Here’s what the tree looks like in our case:

123 vs 456

1 vs. 2

left:123 balanced:

78right:456

7 vs. 84 vs. 5

L:1 R:2 B:3 L:4 R:5 B:6 L:7 R:8

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 41: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

41

Tree Traversal

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 42: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOUniversal Address Systems

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

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 43: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOUniversal Address Systems

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

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 44: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOUniversal Address Systems

44

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 45: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOTraversal Algorithms

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

45

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 46: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOTraversal Algorithms

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 orderedThe three common traversals are:

Preorder traversal Inorder traversal Postorder traversal

46

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 47: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOTraversal

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.

r

T1 T2 Tn

47

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 48: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOPreorder Traversal

r

T1 T2 Tn

Step 1: Visit rStep 2: Visit T1 in preorderStep 3: Visit T2 in preorder

.

.

.Step n+1: Visit Tn in preorder

48

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 49: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOPreorder Traversal

r

T1 T2 Tn

Step 1: Visit rStep 2: Visit T1 in preorderStep 3: Visit T2 in preorder

.

.

.Step n+1: Visit Tn in preorder

49

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 50: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOPreorder Traversal

r

T1 T2 Tn

Step 1: Visit rStep 2: Visit T1 in preorderStep 3: Visit T2 in preorder

.

.

.Step n+1: Visit Tn in preorder

50

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 51: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOPreorder Traversal

r

T1 T2 Tn

Step 1: Visit rStep 2: Visit T1 in preorderStep 3: Visit T2 in preorder

.

.

.Step n+1: Visit Tn in preorder

51

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 52: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOPreorder Traversal

r

T1 T2 Tn

Step 1: Visit rStep 2: Visit T1 in preorderStep 3: Visit T2 in preorder

.

.

.Step n+1: Visit Tn in preorder

52

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 53: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

M

A

R

EY

P

M

HJ

Q T

Tree:

Visiting sequence:

53

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 54: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

AM

A

R

EY

P

M

HJ

Q T

Tree:

Visiting sequence:

54

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 55: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

AM J

A

R

EY

P

M

HJ

Q T

Tree:

Visiting sequence:

55

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 56: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

A YM J

A

R

EY

P

M

HJ

Q T

Tree:

Visiting sequence:

56

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 57: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

A RYM J

A

R

EY

P

M

HJ

Q T

Tree:

Visiting sequence:

57

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 58: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

A RYM HJ

A

R

EY

P

M

HJ

Q T

Tree:

Visiting sequence:

58

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 59: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

A RY PM HJ

A

R

EY

P

M

HJ

Q T

Tree:

Visiting sequence:

59

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 60: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

A RY PM HJ Q

A

R

EY

P

M

HJ

Q T

Tree:

Visiting sequence:

60

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 61: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

A RY PM HJ Q T

A

R

EY

P

M

HJ

Q T

Tree:

Visiting sequence:

61

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 62: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

A R EY PM HJ Q T

A

R

EY

P

M

HJ

Q T

Tree:

Visiting sequence:

62

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 63: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOThe Preorder Traversal of T

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

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 64: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOThe Preorder Traversal of T

64

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 65: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOThe Preorder Traversal of T

65

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 66: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOThe Preorder Traversal of T

66

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 67: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOThe Preorder Traversal of T

67

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 68: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOInorder Traversal

Step 1: Visit T1 in inorderStep 2: Visit rStep 3: Visit T2 in inorder

.

.

.Step n+1: Visit Tn in inorder

r

T1 T2 Tn

68

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 69: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOInorder Traversal

Step 1: Visit T1 in inorderStep 2: Visit rStep 3: Visit T2 in inorder

.

.

.Step n+1: Visit Tn in inorder

r

T1 T2 Tn

69

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 70: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOInorder Traversal

Step 1: Visit T1 in inorderStep 2: Visit rStep 3: Visit T2 in inorder

.

.

.Step n+1: Visit Tn in inorder

r

T1 T2 Tn

70

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 71: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOInorder Traversal

Step 1: Visit T1 in inorderStep 2: Visit rStep 3: Visit T2 in inorder

.

.

.Step n+1: Visit Tn in inorder

r

T1 T2 Tn

71

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 72: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOInorder Traversal

Step 1: Visit T1 in inorderStep 2: Visit rStep 3: Visit T2 in inorder

.

.

.Step n+1: Visit Tn in inorder

r

T1 T2 Tn

72

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 73: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

J

A

R

EY

P

M

HJ

Q T

Tree:

Visiting sequence:

73

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 74: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

AJ

A

R

EY

P

M

HJ

Q T

Tree:

Visiting sequence:

74

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 75: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

A MJ

A

R

EY

P

M

HJ

Q T

Tree:

Visiting sequence:

75

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 76: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

A RMJ

A

R

EY

P

M

HJ

Q T

Tree:

Visiting sequence:

76

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 77: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

A R YMJ

A

R

EY

P

M

HJ

Q T

Tree:

Visiting sequence:

77

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 78: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

A R Y PMJ

A

R

EY

P

M

HJ

Q T

Tree:

Visiting sequence:

78

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 79: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

A R Y PM HJ

A

R

EY

P

M

HJ

Q T

Tree:

Visiting sequence:

79

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 80: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

A R Y PM HJ Q

A

R

EY

P

M

HJ

Q T

Tree:

Visiting sequence:

80

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 81: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

A R Y PM HJ Q T

A

R

EY

P

M

HJ

Q T

Tree:

Visiting sequence:

81

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 82: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

A R EY PM HJ Q T

A

R

EY

P

M

HJ

Q T

Tree:

Visiting sequence:

82

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 83: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOThe Inorder Traversal of T

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

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 84: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOThe Inorder Traversal of T

84

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 85: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOThe Inorder Traversal of T

85

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 86: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOThe Inorder Traversal of T

86

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 87: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOThe Inorder Traversal of T

87

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 88: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOPostorder Traversal

Step 1: Visit T1 in postorderStep 2: Visit T2 in postorder

.

.

.Step n: Visit Tn in postorderStep n+1: Visit r

r

T1 T2 Tn

88

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 89: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOPostorder Traversal

Step 1: Visit T1 in postorderStep 2: Visit T2 in postorder

.

.

.Step n: Visit Tn in postorderStep n+1: Visit r

r

T1 T2 Tn

89

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 90: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOPostorder Traversal

Step 1: Visit T1 in postorderStep 2: Visit T2 in postorder

.

.

.Step n: Visit Tn in postorderStep n+1: Visit r

r

T1 T2 Tn

90

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 91: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOPostorder Traversal

Step 1: Visit T1 in postorderStep 2: Visit T2 in postorder

.

.

.Step n: Visit Tn in postorderStep n+1: Visit r

r

T1 T2 Tn

91

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 92: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOPostorder Traversal

Step 1: Visit T1 in postorderStep 2: Visit T2 in postorder

.

.

.Step n: Visit Tn in postorderStep n+1: Visit r

r

T1 T2 Tn

92

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 93: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

J

A

R

EY

P

M

HJ

Q T

Tree:

Visiting sequence:

93

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 94: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

AJ

A

R

EY

P

M

HJ

Q T

Tree:

Visiting sequence:

94

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 95: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

A RJ

A

R

EY

P

M

HJ

Q T

Tree:

Visiting sequence:

95

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 96: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

A R PJ

A

R

EY

P

M

HJ

Q T

Tree:

Visiting sequence:

96

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 97: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

A R PJ Q

A

R

EY

P

M

HJ

Q T

Tree:

Visiting sequence:

97

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 98: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

A R PJ Q T

A

R

EY

P

M

HJ

Q T

Tree:

Visiting sequence:

98

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 99: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

A R P HJ Q T

A

R

EY

P

M

HJ

Q T

Tree:

Visiting sequence:

99

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 100: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

A R YP HJ Q T

A

R

EY

P

M

HJ

Q T

Tree:

Visiting sequence:

100

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 101: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

A R EYP HJ Q T

A

R

EY

P

M

HJ

Q T

Tree:

Visiting sequence:

101

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 102: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

A R EYP MHJ Q T

A

R

EY

P

M

HJ

Q T

Tree:

Visiting sequence:

102

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 103: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOThe Postorder Traversal of T

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

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 104: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOThe Postorder Traversal of T

104

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 105: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOThe Postorder Traversal of T

105

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 106: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOThe Postorder Traversal of T

106

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 107: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOThe Postorder Traversal of T

107

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 108: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGORepresenting Arithmetic Expressions

Complicated arithmetic expressions can be represented by an ordered rooted tree Internal vertices represent operators Leaves represent operands

108

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 109: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGORepresenting Arithmetic Expressions

Complicated arithmetic expressions can be represented by an ordered rooted tree Internal vertices represent operators Leaves represent operands

Build the tree bottom-up Construct smaller subtrees Incorporate the smaller subtrees as part of

larger subtrees109

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 110: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

(x+y)2 + (x-3)/(y+2)

110

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 111: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

(x+y)2 + (x-3)/(y+2)

+

x y

111

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 112: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

(x+y)2 + (x-3)/(y+2)

+

x y

2

112

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 113: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

(x+y)2 + (x-3)/(y+2)

+

x y

2

x 3

113

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 114: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

(x+y)2 + (x-3)/(y+2)

+

x y

2

x 3

+

y 2

114

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 115: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

(x+y)2 + (x-3)/(y+2)

+

x y

2

x 3

+

y 2

/

115

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 116: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

(x+y)2 + (x-3)/(y+2)

+

x y

2

x 3

+

y 2

/

+

116

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 117: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOEvaluating Prefix NotationIn an prefix expression, a binary operator

precedes its two operands

The expression is evaluated right-left

Look for the first operator from the right

Evaluate the operator with the two operands immediately to its right

117

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 118: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

+ / + 2 2 2 / – 3 2 + 1 0

118

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 119: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

+ / + 2 2 2 / – 3 2 + 1 0

119

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 120: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

+ / + 2 2 2 / – 3 2 + 1 0+ / + 2 2 2 / – 3 2 1

120

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 121: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

+ / + 2 2 2 / – 3 2 + 1 0+ / + 2 2 2 / – 3 2 1

121

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 122: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

+ / + 2 2 2 / – 3 2 + 1 0+ / + 2 2 2 / – 3 2 1+ / + 2 2 2 / 1 1

122

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 123: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

+ / + 2 2 2 / – 3 2 + 1 0+ / + 2 2 2 / – 3 2 1+ / + 2 2 2 / 1 1

123

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 124: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

+ / + 2 2 2 / – 3 2 + 1 0+ / + 2 2 2 / – 3 2 1+ / + 2 2 2 / 1 1+ / + 2 2 2 1

124

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 125: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

+ / + 2 2 2 / – 3 2 + 1 0+ / + 2 2 2 / – 3 2 1+ / + 2 2 2 / 1 1+ / + 2 2 2 1

125

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 126: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

+ / + 2 2 2 / – 3 2 + 1 0+ / + 2 2 2 / – 3 2 1+ / + 2 2 2 / 1 1+ / + 2 2 2 1+ / 4 2 1

126

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 127: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

+ / + 2 2 2 / – 3 2 + 1 0+ / + 2 2 2 / – 3 2 1+ / + 2 2 2 / 1 1+ / + 2 2 2 1+ / 4 2 1

127

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 128: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

+ / + 2 2 2 / – 3 2 + 1 0+ / + 2 2 2 / – 3 2 1+ / + 2 2 2 / 1 1+ / + 2 2 2 1+ / 4 2 1

+ 2 1

128

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 129: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

+ / + 2 2 2 / – 3 2 + 1 0+ / + 2 2 2 / – 3 2 1+ / + 2 2 2 / 1 1+ / + 2 2 2 1+ / 4 2 1

+ 2 1

129

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 130: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

+ / + 2 2 2 / – 3 2 + 1 0+ / + 2 2 2 / – 3 2 1+ / + 2 2 2 / 1 1+ / + 2 2 2 1+ / 4 2 1

+ 2 1

3130

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 131: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOPostfix Notation (Reverse Polish)

• Traverse in postorder

+

– +

/

+ 2

x y x 3 y 2

131

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 132: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOPostfix Notation (Reverse Polish)

• Traverse in postorder

x

+

– +

/

+ 2

x y x 3 y 2

132

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 133: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOPostfix Notation (Reverse Polish)

• Traverse in postorder

x y

+

– +

/

+ 2

x y x 3 y 2

133

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 134: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOPostfix Notation (Reverse Polish)

• Traverse in postorder

x +y

+

– +

/

+ 2

x y x 3 y 2

134

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 135: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOPostfix Notation (Reverse Polish)

• Traverse in postorder

x +y 2

+

– +

/

+ 2

x y x 3 y 2

135

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 136: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOPostfix Notation (Reverse Polish)

• Traverse in postorder

x +y 2

+

– +

/

+ 2

x y x 3 y 2

136

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 137: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOPostfix Notation (Reverse Polish)

• Traverse in postorder

x +y 2 x

+

– +

/

+ 2

x y x 3 y 2

137

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 138: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOPostfix Notation (Reverse Polish)

• Traverse in postorder

x +y 2 x 3

+

– +

/

+ 2

x y x 3 y 2

138

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 139: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOPostfix Notation (Reverse Polish)

• Traverse in postorder

x +y 2 x –3

+

– +

/

+ 2

x y x 3 y 2

139

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 140: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOPostfix Notation (Reverse Polish)

• Traverse in postorder

x +y 2 x –3 y

+

– +

/

+ 2

x y x 3 y 2

140

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 141: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOPostfix Notation (Reverse Polish)

• Traverse in postorder

x +y 2 x –3 y 2

+

– +

/

+ 2

x y x 3 y 2

141

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 142: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOPostfix Notation (Reverse Polish)

• Traverse in postorder

x +y 2 x –3 y +2

+

– +

/

+ 2

x y x 3 y 2

142

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 143: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOPostfix Notation (Reverse Polish)

• Traverse in postorder

x +y 2 x –3 /y +2

+

– +

/

+ 2

x y x 3 y 2

143

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 144: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOPostfix Notation (Reverse Polish)

• Traverse in postorder

x +y 2 +x –3 /y +2

+

– +

/

+ 2

x y x 3 y 2

144

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 145: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

In an postfix expression, a binary operator follows its two operands

The expression is evaluated left-right

Look for the first operator from the left

Evaluate the operator with the two operands immediately to its left

Evaluating Postfix Notation

145

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 146: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

2 2 + 2 / 3 2 – 1 0 + / +

146

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 147: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

2 2 + 2 / 3 2 – 1 0 + / +

147

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 148: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

2 2 + 2 / 3 2 – 1 0 + / +

4 2 / 3 2 – 1 0 + / +

148

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 149: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

2 2 + 2 / 3 2 – 1 0 + / +

4 2 / 3 2 – 1 0 + / +

149

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 150: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

2 2 + 2 / 3 2 – 1 0 + / +

4 2 / 3 2 – 1 0 + / +

2 3 2 – 1 0 + / +

150

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 151: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

2 2 + 2 / 3 2 – 1 0 + / +

4 2 / 3 2 – 1 0 + / +

2 3 2 – 1 0 + / +

151

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 152: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

2 2 + 2 / 3 2 – 1 0 + / +

4 2 / 3 2 – 1 0 + / +

2 3 2 – 1 0 + / +

2 1 1 0 + / +

152

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 153: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

2 2 + 2 / 3 2 – 1 0 + / +

4 2 / 3 2 – 1 0 + / +

2 3 2 – 1 0 + / +

2 1 1 0 + / +

153

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 154: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

2 2 + 2 / 3 2 – 1 0 + / +

4 2 / 3 2 – 1 0 + / +

2 3 2 – 1 0 + / +

2 1 1 0 + / +

2 1 1 / +

154

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 155: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

2 2 + 2 / 3 2 – 1 0 + / +

4 2 / 3 2 – 1 0 + / +

2 3 2 – 1 0 + / +

2 1 1 0 + / +

2 1 1 / +

155

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 156: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

2 2 + 2 / 3 2 – 1 0 + / +

4 2 / 3 2 – 1 0 + / +

2 3 2 – 1 0 + / +

2 1 1 0 + / +

2 1 1 / +

2 1 +

156

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 157: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

2 2 + 2 / 3 2 – 1 0 + / +

4 2 / 3 2 – 1 0 + / +

2 3 2 – 1 0 + / +

2 1 1 0 + / +

2 1 1 / +

2 1 +

157

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 158: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGOExample

3

2 2 + 2 / 3 2 – 1 0 + / +

4 2 / 3 2 – 1 0 + / +

2 3 2 – 1 0 + / +

2 1 1 0 + / +

2 1 1 / +

2 1 +

158

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 159: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

159

Spanning Trees

You will learn about the following topics in tree later in other courses: Spanning Trees Minimum Spanning Trees

Dis

cret

e S

truc

ture

s

Fal

l 138

9

Page 160: LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

LOGO

LOGO

Any Question?Good Luck