Lecture 21
Lecture 21
1
Tree Traversal
B C
D E F
Preorder: ABDECFG
Postorder: DEBGFCA G
In Order: DBEAFGC
Tree Traversals: An Example
A
B C
D E F
Preorder: A
(visit each node as your reach it) G
Tree Traversals: An Example
A
B C
D E F
Preorder: AB
(visit each node as your reach it) G
Tree Traversals: An Example
A
B C
D E F
Preorder: ABD
(visit each node as your reach it) G
Tree Traversals: An Example
A
B C
D E F
Preorder: ABDE
(visit each node as your reach it) G
Tree Traversals: An Example
A
B C
D E F
Preorder: ABDEC
(visit each node as your reach it) G
Tree Traversals: An Example
A
B C
D E F
Preorder: ABDECF
(visit each node as your reach it) G
Tree Traversals: An Example
A
B C
D E F
Preorder: ABDECFG
(visit each node as your reach it) G
Tree Traversals: An Example
A
B C
D E F
Postorder:
G
Tree Traversals: An Example
A
B C
D E F
Postorder:
G
Tree Traversals: An Example
A
B C
D E F
Postorder: D
G
Tree Traversals: An Example
A
B C
D E F
Postorder: DE
G
Tree Traversals: An Example
A
B C
D E F
Postorder: DEB
G
Tree Traversals: An Example
A
B C
D E F
Postorder: DEB
G
Tree Traversals: An Example
A
B C
D E F
Postorder: DEB
G
Tree Traversals: An Example
A
B C
D E F
Postorder: DEBG
G
Tree Traversals: An Example
A
B C
D E F
Postorder: DEBGF
G
Tree Traversals: An Example
A
B C
D E F
Postorder: DEBGFC
G
Tree Traversals: An Example
A
B C
D E F
Postorder: DEBGFCA
G
Tree Traversals: An Example
A
B C
D E F
G
In Order:
Tree Traversals: An Example
A
B C
D E F
G
In Order:
Tree Traversals: An Example
A
B C
D E F
G
In Order: D
Tree Traversals: An Example
A
B C
D E F
G
In Order: DB
Tree Traversals: An Example
A
B C
D E F
G
In Order: DBE
Tree Traversals: An Example
A
B C
D E F
G
In Order: DBEA
Tree Traversals: An Example
A
B C
D E F
G
In Order: DBEA
Tree Traversals: An Example
A
B C
D E F
G
In Order: DBEAF
Tree Traversals: An Example
A
B C
D E F
G
In Order: DBEAFG
Tree Traversals: An Example
A
B C
D E F
G
In Order: DBEAFGC
Tree Traversals: An Example
A
B C
D E F
Preorder: ABDECFG
Postorder: DEBGFCA G
In Order: DBEAFGC
Tree Traversals: Another Example
A
B C
D E
G
F
Preorder: ABDFHIECG
Postorder: HIFDEBGCA
H I In Order: DHFIBEACG
Tree Traversals: Another Example
A
B C
D E
G
F
Preorder: A
H I
Tree Traversals: Another Example
A
B C
D E
G
F
Preorder: AB
H I
Tree Traversals: Another Example
A
B C
D E
G
F
Preorder: ABD
H I
Tree Traversals: Another Example
A
B C
D E
G
F
Preorder: ABDF
H I
Tree Traversals: Another Example
A
B C
D E
G
F
Preorder: ABDFH
H I
Tree Traversals: Another Example
A
B C
D E
G
F
Preorder: ABDFHI
H I
Tree Traversals: Another Example
A
B C
D E
G
F
Preorder: ABDFHIE
H I
Tree Traversals: Another Example
A
B C
D E
G
F
Preorder: ABDFHIEC
H I
Tree Traversals: Another Example
A
B C
D E
G
F
Preorder: ABDFHIECG
H I
Tree Traversals: Another Example
A
B C
D E
G
Postorder:
H I
Tree Traversals: Another Example
A
B C
D E
G
Postorder:
H I
Tree Traversals: Another Example
A
B C
D E
G
Postorder:
H I
Tree Traversals: Another Example
A
B C
D E
G
Postorder:
H I
Tree Traversals: Another Example
A
B C
D E
G
Postorder: H
H I
Tree Traversals: Another Example
A
B C
D E
G
Postorder: HI
H I
Tree Traversals: Another Example
A
B C
D E
G
Postorder: HIF
H I
Tree Traversals: Another Example
A
B C
D E
G
Postorder: HIFD
H I
Tree Traversals: Another Example
A
B C
D E
G
Postorder: HIFDE
H I
Tree Traversals: Another Example
A
B C
D E
G
Postorder: HIFDEB
H I
Tree Traversals: Another Example
A
B C
D E
G
Postorder: HIFDEB
H I
Tree Traversals: Another Example
A
B C
D E
G
Postorder: HIFDEBG
H I
Tree Traversals: Another Example
A
B C
D E
G
Postorder: HIFDEBGC
H I
Tree Traversals: Another Example
A
B C
D E
G
Postorder: HIFDEBGCA
H I
Tree Traversals: Another Example
A
B C
D E
G
H I In Order:
Tree Traversals: Another Example
A
B C
D E
G
H I In Order:
Tree Traversals: Another Example
A
B C
D E
G
H I In Order: D
Tree Traversals: Another Example
A
B C
D E
G
H I In Order: D
Tree Traversals: Another Example
A
B C
D E
G
H I In Order: DH
Tree Traversals: Another Example
A
B C
D E
G
H I In Order: DHF
Tree Traversals: Another Example
A
B C
D E
G
H I In Order: DHFI
Tree Traversals: Another Example
A
B C
D E
G
H I In Order: DHFIB
Tree Traversals: Another Example
A
B C
D E
G
H I In Order: DHFIBE
Tree Traversals: Another Example
A
B C
D E
G
H I In Order: DHFIBEA
Tree Traversals: Another Example
A
B C
D E
G
H I In Order: DHFIBEAC
Tree Traversals: Another Example
A
B C
D E
G
H I In Order: DHFIBEACG
Tree Traversals: Another Example
A
B C
D E
G
F
Preorder: ABDFHIECG
Postorder: HIFDEBGCA
H I In Order: DHFIBEACG
Tree Anatomy
The children of a node are, themselves, trees, called subtrees.
Root
Level 0 R
Level 2 X U V W
Leaf
Level 3 Y Z
Child of X
Subtree
Parent of Z and Y
70
Tree Traversals
• One of the most common operations
performed on trees, are a tree traversals
• A traversal starts at the root of the tree and
visits every node in the tree exactly once
– visit means to process the data in the node
• Traversals are either depth-first or breadth-
first
71
Breadth First Traversals
• All the nodes in one 7
level are visited
• Followed by the nodes 6 10
the at next level
• Beginning at the root 4 8 13
• For the sample tree 3 5
– 7, 6, 10, 4, 8, 13, 3, 5
72
Queue and stack
• A queue is a sequence of elements
such that each new element is added
(enqueued) to one end, called the
back of the queue, and an element is
removed (dequeued) from the other
end, called the front
Step Output Queue
74
Depth-First Traversals
75
Pre-order Traversal: VLR
• Visit the node 7
• Do a pre-order
traversal of the left 6 10
subtree
• Finish with a pre-order 4 8 13
traversal of the right 3 5
subtree
• For the sample tree
– 7, 6, 4, 3, 5, 10, 8, 13
76
Pre-order tree traversal with a stack
• Push root onto the stack
• While stack is not empty
– Pop a vertex off stack, and write it to the output list
– Push its children right-to-left onto stack
Step Output Stack
77
Representing 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
subtrees
78
Example
(x+y)2 + (x-3)/(y+2)
+
/
+ 2 – +
x y x 3 y 2
79
Infix Notation
• Traverse in inorder (LVR) adding parentheses for
each operation
+
/
+ 2 – +
x y x 3 y 2
( ((x + y ) 2 ) +( ( x – 3 ) / ( y + 2 ) ) )
80
Prefix Notation
(Polish Notation)
• Traverse in preorder (VLR)
+
/
+ 2 – +
x y x 3 y 2
+ + x y 2 / – x 3 + y 2
81
Evaluating Prefix Notation
82
Example
+ / + 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
3 83
Postfix Notation
(Reverse Polish)
• Traverse in postorder (LRV)
+
/
+ 2 – +
x y x 3 y 2
x y + 2 x 3 – y 2 + /+
84
Evaluating Postfix Notation
85
Example
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 +
3
86