Title: Tree Traversal
1Tree Traversal
- Section 9.3
- Longin Jan Latecki
- Temple University
- Based on slides by
- Paul Tymann, Andrew Watkins,
- and J. van Helden
2Tree Anatomy
The children of a node are, themselves, trees,
called subtrees.
Root
R
Level 0
Level 1
S
T
Internal Node
X
U
V
W
Level 2
Leaf
Y
Z
Level 3
Child of X
Subtree
Parent of Z and Y
3Tree 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
4Breadth First Traversals
- All the nodes in one level are visited
- Followed by the nodes the at next level
- Beginning at the root
- For the sample tree
- 7, 6, 10, 4, 8, 13, 3, 5
7
6
10
4
8
13
3
5
5Queue 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 - A stack is a sequence of elements such that each
new element is added (or pushed) onto one end,
called the top, and an element is removed
(popped) from the same end
6Breadth first tree traversal with a queue
- Enqueue root
- While queue is not empty
- Dequeue a vertex and write it to the output list
- Enqueue its children left-to-right
Step Output Queue 0 a 1 a e,d 2 e d,i,b 3 d i,b,k
,l 4 i b,k,l 5 b k,l,f 6 k l,f 7 l f 8 f g 9 g j,h
10 j h,m 11 h m 12 m c 13 c
7Depth-First Traversals
- There are 8 different depth-first traversals
- VLR (pre-order traversal)
- VRL
- LVR (in-order traversal)
- RVL
- RLV
- LRV (post-order traversal)
8Pre-order Traversal VLR
- Visit the node
- Do a pre-order traversal of the left subtree
- Finish with a pre-order traversal of the right
subtree - For the sample tree
- 7, 6, 4, 3, 5, 10, 8, 13
7
6
10
4
8
13
3
5
9Pre-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 0 a 1 a d,e 2 e d,b,i 3 i d,b 4
b d,f 5 f d,g 6 g d,h,j 7 j d,h,m 8 m d,h,c 9 c d
,h 10 h d 11 d l,k 12 k l 13 l
10Preorder Traversal
Step 1 Visit r
Step 2 Visit T1 in preorder
Step 3 Visit T2 in preorder
Step n1 Visit Tn in preorder
11Example
A
R
E
Y
P
M
H
J
Q
T
12Ordering of the preorder traversal is the same a
the Universal Address System with lexicographic
ordering.
0
1
2
3
2.2
2.1
1.1
2.2.1 2.2.2 2.2.3
A
R
E
Y
P
M
H
J
Q
T
13In-order Traversal LVR
- Do an in-order traversal of the left subtree
- Visit the node
- Finish with an in-order traversal of the right
subtree - For the sample tree
- 3, 4, 5, 6, 7, 8, 10, 13
7
6
10
4
8
13
3
5
14Inorder Traversal
Step 1 Visit T1 in inorder
Step 2 Visit r
Step 3 Visit T2 in inorder
Step n1 Visit Tn in inorder
15Example
A
R
E
Y
P
M
H
J
Q
T
16inorder (t) if t ! NIL inorder
(leftt) write (labelt) inorder (rightt)
Inorder Traversal on a binary search tree.
17Post-order Traversal LRV
- Do a post-order traversal of the left subtree
- Followed by a post-order traversal of the right
subtree - Visit the node
- For the sample tree
- 3, 5, 4, 6, 8, 13, 10, 7
7
6
10
4
8
13
3
5
18Postorder Traversal
Step 1 Visit T1 in postorder
Step 2 Visit T2 in postorder
Step n Visit Tn in postorder
Step n1 Visit r
19Example
A
R
E
Y
P
M
H
J
Q
T
20Representing 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
21Example
22Infix Notation
- Traverse in inorder (LVR) adding parentheses for
each operation
x
y
2
x
3
y
2
?
/
23Prefix Notation(Polish Notation)
- Traverse in preorder (VLR)
x
y
2
x
3
y
2
?
/
24Evaluating Prefix Notation
- In 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
25Example
/ 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
26Postfix Notation(Reverse Polish)
- Traverse in postorder (LRV)
x
y
2
x
3
y
2
?
/
27Evaluating Postfix Notation
- 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
28Example
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