Lecture5 Basic Traversal and Search Techniques
Lecture5 Basic Traversal and Search Techniques
SEARCH TECHNIQUES
Outlines
Introduction
Binary Tree Traversal Methods
Preorder
Inorder
Postorder
Graph Search & Traversal Methods
Breadth First
Depth First
Introduction
In a traversal of a binary tree, each element of the
binary tree is visited exactly once.
b c
a b c
Preorder Example (visit = print)
b c
f
d e
g h i j
abdghei cf j
Preorder Of Expression Tree
* +
e f
+ -
a b c d
/ * +ab- c d+e f
b c
b a c
Inorder Example (visit = print)
b c
f
d e
g h i j
gdhbei af j c
Inorder By Projection (Squishing)
b c
f
d e
g h i j
g d h b e i a f j c
Inorder Of Expression Tree
/
* +
e f
+ -
a b c d
a + b * c - d/ e + f
b c
bca
Postorder Example (visit = print)
b c
f
d e
g h i j
ghdi ebj f ca
Postorder Of Expression Tree
/
* +
e f
+ -
a b c d
ab+c d- * ef + /
gdhbei fjc
Inorder And Preorder
a
gdhbei fjc
preorder = a b d g h e i c f j
b is the next root; gdh are in a
the left subtree; ei are in the
right subtree.
b fjc
gdh ei
Inorder And Preorder
a
b fjc
gdh ei
preorder = d g h e i c f j a
d is the next root; g is in the b fjc
left subtree; h is in the right
subtree. d ei
g h
Inorder And Preorder
a
preorder = e i c f j b fjc
e is the next root; i is in
the right subtree. d e
g h i
a
preorder = c f j
b
c is the next root; fj is in the c
left subtree. d e
fj
g h i
Inorder And Preorder
preorder = f j
f is the next root; j is in the
right subtree.
a
b
c
d e
f
g h i j
Inorder And Postorder
4
5
9
10
6
7 11
Graph Search Methods
Many graph problems solved using a search
method.
Path from one vertex to another.
Is the graph connected?
Find a spanning tree.
Etc.
Commonly used search methods:
Breadth-first search.
Depth-first search.
Breadth-First Search
4
5
9
10
6
7 11
2 FIFO Queue
3 1
8
1
4
5
9
10
6
7 11
2 FIFO Queue
3 1
8
1
4
5
9
10
6
7 11
2 FIFO Queue
3
2 4
8
1
4
5
9
10
6
7 11
2 FIFO Queue
3
2 4
8
1
4
5
9
10
6
7 11
2 FIFO Queue
3
4 5 3 6
8
1
4
5
9
10
6
7 11
2 FIFO Queue
3
4 5 3 6
8
1
4
5
9
10
6
7 11
2 FIFO Queue
3
5 3 6
8
1
4
5
9
10
6
7 11
2 FIFO Queue
3
5 3 6
8
1
4
5
9
10
6
7 11
2 FIFO Queue
3
3 6 9 7
8
1
4
5
9
10
6
7 11
2 FIFO Queue
3
3 6 9 7
8
1
4
5
9
1
0
6
7 11
2 FIFO Queue
3
6 9 7
8
1
4
5
9
10
6
7 11
2 FIFO Queue
3
6 9 7
8
1
4
5
9
1
0
6
7 11
2 FIFO Queue
3
9 7
8
1
4
5
9
10
6
7 11
2 FIFO Queue
3
9 7
8
1
4
5
9
10
6
7 11
2 FIFO Queue
3
7 8
8
1
4
5
9
10
6
7 11
put in Q.
Breadth-First Search Example
2 FIFO Queue
3
7 8
8
1
4
5
9
10
6
7 11
2 FIFO Queue
3
8
8
1
4
5
9
10
6
7 11
2 FIFO Queue
3
8
8
1
4
5
9
10
6
7 11
2 FIFO Queue
3
8
1
4
5
9
10
6
7 11
2
3
8
1
4
5
9
1
0
6
7 11
Spanning Tree
Start a breadth-first search at any vertex of
the graph.
If graph is connected, the n-1 edges used to
get to unvisited vertices define a spanning tree
(breadth-first spanning tree).
Time
O(n2) when adjacency matrix used
O(n+e) when adjacency lists used (e is
number of edges)
Depth-First Search
4
5
9
1
0
6
7 11
Start search at vertex 1.
Label vertex 1 and do a depth first search from either 2 or 4.
4
5
9
10
6
7 11
4
5
9
1
0
6
7 11
2
3
8
1
4
5
9
10
6
7 11
4
5
9
1
0
6
7 11
2
3
8
1
4
5
9
10
6
7 11
4
5
9
1
0
6
7 11
4
5
9
10
6
7 11
4
5
9
1
0
6
7 11
Return to 5.
Depth-First Search Example
2
3
8
1
4
5
9
1
0
6
7 11
Do a DFS(3).
Depth-First Search Example
2
3
8
1
4
5
9
1
0
6
7 11
Return to 2.
Depth-First Search Example
2
3
8
1
4
5
9
10
6
7 11
Return to 1.
Depth-First Search Example
2
3
8
1
4
5
9
10
6
7 11
Q&A