Link
Tree and Graph Traversals
Tree traversals: a data structure-specific operation, and how they generalize to the Graph data type.
Kevin Lin, with thanks to many others.
1
Ask questions anonymously on Piazza. Look for the pinned Lecture Questions thread.

This lecture is much shorter than usual due to the midterm survey.

File System Tree
Sometimes, we want to iterate over a tree.
Formally, this is called a tree traversal. There are several different types of tree traversals.
2
words.txt
data
tiles
cities.txt
d1_x0_y0.jpg
d0_x1_y0.jpg
d0_x0_y0.jpg

Tree Traversal Orderings
Level-Order Traversal. Visit top-to-bottom, left-to-right (like reading in English): DBFACEG

Depth-First Traversals.
Traverse deep nodes (A, C, E, G) before shallow ones (D, B, F).
Note: “Traversing” a node is different than “visiting” a node.
3 types: Preorder, Inorder, Postorder.
3
A
C
B
D
E
F
G

Depth-First Traversals
Preorder Traversal.“Visit” a node, then traverse its children.
4
A
C
B
D
E
F
G
preOrder(BSTNode x) {
    if (x == null) return;
    print(x.key)
    preOrder(x.left)
    preOrder(x.right)
}
D
B
A
C
F
E
G

Depth-First Traversals
Inorder Traversal.Traverse left child, “visit”, then traverse right child.
5
A
C
B
D
E
F
G
inOrder(BSTNode x) {
    if (x == null) return;
    inOrder(x.left)
    print(x.key)
    inOrder(x.right)
}
A
B
C
D
E
F
G

Depth-First Traversals
Postorder Traversal.Traverse left, traverse right, then “visit.”
6
A
C
B
D
E
F
G
postOrder(BSTNode x) {
    if (x == null) return;
    postOrder(x.left)
    postOrder(x.right)
    print(x.key)
}
Q
Q1: Give the postorder traversal for the graph.

Which of the following is the postorder traversal of the graph?
7

Depth-First Traversals
Postorder Traversal.Traverse left, traverse right, then “visit.”
8
A
C
B
D
E
F
G
postOrder(BSTNode x) {
    if (x == null) return;
    postOrder(x.left)
    postOrder(x.right)
    print(x.key)
}
A C B E G F D
A

Depth-First Traversals: Visual Trick (for humans)
First, trace a path around the graph from the top going counter-clockwise.
Preorder. “Visit” when passing the left.
Inorder. “Visit” when passing the bottom.
Postorder. “Visit” when passing the right.
9
A
C
B
D
E
F
G
A C B E G F D

File System Tree
Preorder traversal can print the files in a directory. Postorder can summarize file sizes.
10
words.txt
data
tiles
cities.txt
d1_x0_y0.jpg
d0_x1_y0.jpg
d0_x0_y0.jpg
data/
  tiles/
    d0_x0_y0.jpg
    d0_x1_y0.jpg
    d1_x0_y0.jpg
    ...
  cities.txt
  words.txt
13 K
587 K
3502 K
16 K
11 K

Graphs
11

Beyond Trees
Trees are fantastic for representing ordered data or hierarchical data.
Real-world example: Paris Metro map.

This is not a tree as it contains cycles! More than one way to get from A to B.
12

Alternate Tree Definition
Tree. Consists of a set of nodes and a set of edges that connect those nodes.
Invariant. There is exactly one path between any two nodes.
13

Graph Definition
Graph. Consists of a set of nodes and a set of zero or more edges.
Each edge connects any two nodes. Not all nodes need to be connected.
14

Simple Graph Definition
Simple Graph. A graph with no self-loops and no parallel edges.
Unless otherwise stated, all graphs in this course are simple graphs.
15
Self-loop
Parallel

16
a
b
d
c
a
b
d
c
e
a
b
d
c
a
b
d
c
b
d
c
e
a
1
2
3
1
Acyclic
Cyclic
Directed
Undirected
Edge Labels
Types of Graphs

Beyond Trees
17
This Paris Metro map is a simple graph.

Undirected.
Connected.
Cyclic. Not a tree!
Vertex-labeled. Each node has a color.

Graph Connectivity Problem
18

s-t Connectivity
Let’s solve a classic graph problem called the s-t connectivity problem.
Given source vertex s and a target vertex t, does there exist a path between s and t?

Try to come up with an algorithm for connected(s, t).
19
1
2
3
4
5
6
7
8
0
s
t
Q1: Try to come up with an algorithm for connected(s, t).

Applying Tree Traversal
One possible recursive algorithm for connected(s, t).
Does s == t? If so, return true.
Otherwise, if connected(v, t) for any neighbor v of s, return true.
Return false.
What is problematic about this algorithm?
20
Q
1
2
3
4
5
6
7
8
0
s
t
Q1: What is wrong with this algorithm?

Applying Tree Traversal
One possible recursive algorithm for connected(s, t).
Does s == t? If so, return true.
Otherwise, if connected(v, t) for any neighbor v of s, return true.
Return false.
What is problematic about this algorithm?
Does 0 == 7? No, so…
if (connected(1, 7)) return true;
connected(1, 7):
Does 1 == 7? No, so…
if (connected(0, 7)) …
21
A
1
2
3
4
5
6
7
8
0
s
t

Depth-First Search
One possible recursive algorithm for connected(s, t).
Mark s as visited.
Does s == t? If so, return true.
Otherwise, if connected(v, t) for any unmarked neighbor v of s, return true.
Return false.
Each vertex visited at most once.
Depth-First Search.
22
1
2
3
4
5
6
7
8
0
s
t
Demo

Summary
Graphs are a more general idea than a tree. A tree is a graph where there are no cycles and every vertex is connected.
Trees have level-order traversals, and 3 depth-first traversals: pre-, in-, and post-order.

Depth-First Search is a powerful algorithm that can be used to solve many graph problems by performing actions or setting instance variables during a graph (or tree) traversal.
On graphs, we have DFS Preorder and DFS Postorder.
23
Demo

24
xkcd
DFS (Randall Munroe/xkcd)