1
COMP 250
Lecture 20
tree traversal
- Oct. 25/26, 2017
tree traversal Oct. 25/26, 2017 1 2 Tree Traversal How to visit - - PowerPoint PPT Presentation
COMP 250 Lecture 20 tree traversal Oct. 25/26, 2017 1 2 Tree Traversal How to visit (enumerate, iterate through, traverse ) all the nodes of a tree ? 3 depthfirst (root){ // preorder if (root is not empty){ visit root for each
1
2
3
4
1
2 3
5
1
2 3 4 5
6
2 7 8 11 3 4 10 9 5 6
7
8
Documents (directory) Music (directory) Eminem (directory) Lose Yourself (file) Raffi (directory) Shake My Sillies Out (file) Baby Beluga (file) Videos (directory) : (file) Work (directory) COMP250 (directory) : Research (directory) :
My Documents
Music Videos Work Raffi Eminem
COMP 250 Lose Yourself Baby Beluga Shake my sillies out
Research
9
10
11
12
13
14
4
2 1 3 1 1 2 1
15
16
17
18
a
b g h k c d j i e f
19
a
b g h k c d j i e f
20
a
b g h k c d j i e f
21
a
b g h k c d j i e f
22
23
24
25
26
27
a
b g h k c d j i e f
28
a
b g h k c d j i e f
29
a
b g h k c d j i e f
30
a
b g h k c d j i e f
31
a
b g h k c d j i e f
32
a
b g h k c d j i e f
33
a
b g h k c d j i e f
34
a
b g h k c d j i e f
35
a
b g h k c d j i e f
36
a
b g h k c d j i e f
37
a
b g h k c d j i e f
38
39
40
a
b c d i e f h g j k
Queue state at start of the while loop
41
a
b c d i e f h g j k
Queue state at start of the while loop
42
a
b c d i e f h g j k
Queue state at start of the while loop
43
a
b c d i e f h g j k
Queue state at start of the while loop
44
a
b c d i e f h g j k
Queue state at start of the while loop
45
a
b c d i e f h g j k
46
a
b c d i e f h g j k
47
48
49