<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
tree	B-Algorithm
traversal	I-Algorithm
(	O
also	O
known	O
as	O
tree	B-Algorithm
search	I-Algorithm
and	O
walking	B-Algorithm
the	I-Algorithm
tree	I-Algorithm
)	O
is	O
a	O
form	O
of	O
graph	B-Algorithm
traversal	I-Algorithm
and	O
refers	O
to	O
the	O
process	O
of	O
visiting	O
(	O
e.g.	O
</s>
<s>
retrieving	O
,	O
updating	O
,	O
or	O
deleting	O
)	O
each	O
node	O
in	O
a	O
tree	B-Application
data	I-Application
structure	I-Application
,	O
exactly	O
once	O
.	O
</s>
<s>
Unlike	O
linked	B-Data_Structure
lists	I-Data_Structure
,	O
one-dimensional	O
arrays	O
and	O
other	O
linear	O
data	O
structures	O
,	O
which	O
are	O
canonically	O
traversed	O
in	O
linear	O
order	O
,	O
trees	O
may	O
be	O
traversed	O
in	O
multiple	O
ways	O
.	O
</s>
<s>
They	O
may	O
be	O
traversed	O
in	O
depth-first	B-Algorithm
or	O
breadth-first	B-Algorithm
order	O
.	O
</s>
<s>
There	O
are	O
three	O
common	O
ways	O
to	O
traverse	O
them	O
in	O
depth-first	B-Algorithm
order	O
:	O
in-order	O
,	O
pre-order	O
and	O
post-order	O
.	O
</s>
<s>
Beyond	O
these	O
basic	O
traversals	O
,	O
various	O
more	O
complex	O
or	O
hybrid	O
schemes	O
are	O
possible	O
,	O
such	O
as	O
depth-limited	B-Algorithm
searches	I-Algorithm
like	O
iterative	B-Algorithm
deepening	I-Algorithm
depth-first	I-Algorithm
search	I-Algorithm
.	O
</s>
<s>
The	O
latter	O
,	O
as	O
well	O
as	O
breadth-first	B-Algorithm
search	I-Algorithm
,	O
can	O
also	O
be	O
used	O
to	O
traverse	O
infinite	O
trees	O
,	O
see	O
below	O
.	O
</s>
<s>
This	O
is	O
often	O
done	O
via	O
a	O
stack	B-Application
(	O
LIFO	B-Application
)	O
or	O
queue	B-Application
(	O
FIFO	O
)	O
.	O
</s>
<s>
As	O
a	O
tree	O
is	O
a	O
self-referential	O
(	O
recursively	O
defined	O
)	O
data	O
structure	O
,	O
traversal	O
can	O
be	O
defined	O
by	O
recursion	O
or	O
,	O
more	O
subtly	O
,	O
corecursion	B-Application
,	O
in	O
a	O
natural	O
and	O
clear	O
fashion	O
;	O
in	O
these	O
cases	O
the	O
deferred	O
nodes	O
are	O
stored	O
implicitly	O
in	O
the	O
call	B-General_Concept
stack	I-General_Concept
.	O
</s>
<s>
Depth-first	B-Algorithm
search	I-Algorithm
is	O
easily	O
implemented	O
via	O
a	O
stack	B-Application
,	O
including	O
recursively	O
(	O
via	O
the	O
call	B-General_Concept
stack	I-General_Concept
)	O
,	O
while	O
breadth-first	B-Algorithm
search	I-Algorithm
is	O
easily	O
implemented	O
via	O
a	O
queue	B-Application
,	O
including	O
corecursively	O
.	O
</s>
<s>
In	O
depth-first	B-Algorithm
search	I-Algorithm
(	O
DFS	O
)	O
,	O
the	O
search	B-Data_Structure
tree	I-Data_Structure
is	O
deepened	O
as	O
much	O
as	O
possible	O
before	O
going	O
to	O
the	O
next	O
sibling	O
.	O
</s>
<s>
To	O
traverse	O
binary	O
trees	O
with	O
depth-first	B-Algorithm
search	I-Algorithm
,	O
perform	O
the	O
following	O
operations	O
at	O
each	O
node	O
:	O
</s>
<s>
L	O
:	O
Recursively	O
traverse	O
the	O
current	O
node	O
's	O
left	O
subtree	B-Application
.	O
</s>
<s>
R	O
:	O
Recursively	O
traverse	O
the	O
current	O
node	O
's	O
right	O
subtree	B-Application
.	O
</s>
<s>
Recursively	O
traverse	O
the	O
current	O
node	O
's	O
left	O
subtree	B-Application
.	O
</s>
<s>
Recursively	O
traverse	O
the	O
current	O
node	O
's	O
right	O
subtree	B-Application
.	O
</s>
<s>
The	O
pre-order	O
traversal	O
is	O
a	O
topologically	B-Algorithm
sorted	I-Algorithm
one	O
,	O
because	O
a	O
parent	B-Application
node	I-Application
is	O
processed	O
before	O
any	O
of	O
its	O
child	O
nodes	O
is	O
done	O
.	O
</s>
<s>
Recursively	O
traverse	O
the	O
current	O
node	O
's	O
left	O
subtree	B-Application
.	O
</s>
<s>
Recursively	O
traverse	O
the	O
current	O
node	O
's	O
right	O
subtree	B-Application
.	O
</s>
<s>
Post-order	O
traversal	O
can	O
be	O
useful	O
to	O
get	O
postfix	O
expression	O
of	O
a	O
binary	B-Algorithm
expression	I-Algorithm
tree	I-Algorithm
.	O
</s>
<s>
Recursively	O
traverse	O
the	O
current	O
node	O
's	O
left	O
subtree	B-Application
.	O
</s>
<s>
Recursively	O
traverse	O
the	O
current	O
node	O
's	O
right	O
subtree	B-Application
.	O
</s>
<s>
In	O
a	O
binary	B-Language
search	I-Language
tree	I-Language
ordered	O
such	O
that	O
in	O
each	O
node	O
the	O
key	O
is	O
greater	O
than	O
all	O
keys	O
in	O
its	O
left	O
subtree	B-Application
and	O
less	O
than	O
all	O
keys	O
in	O
its	O
right	O
subtree	B-Application
,	O
in-order	O
traversal	O
retrieves	O
the	O
keys	O
in	O
ascending	O
sorted	O
order	O
.	O
</s>
<s>
Recursively	O
traverse	O
the	O
current	O
node	O
's	O
right	O
subtree	B-Application
.	O
</s>
<s>
Recursively	O
traverse	O
the	O
current	O
node	O
's	O
left	O
subtree	B-Application
.	O
</s>
<s>
Recursively	O
traverse	O
the	O
current	O
node	O
's	O
right	O
subtree	B-Application
.	O
</s>
<s>
Recursively	O
traverse	O
the	O
current	O
node	O
's	O
left	O
subtree	B-Application
.	O
</s>
<s>
Recursively	O
traverse	O
the	O
current	O
node	O
's	O
right	O
subtree	B-Application
.	O
</s>
<s>
Recursively	O
traverse	O
the	O
current	O
node	O
's	O
left	O
subtree	B-Application
.	O
</s>
<s>
In	O
a	O
binary	B-Language
search	I-Language
tree	I-Language
ordered	O
such	O
that	O
in	O
each	O
node	O
the	O
key	O
is	O
greater	O
than	O
all	O
keys	O
in	O
its	O
left	O
subtree	B-Application
and	O
less	O
than	O
all	O
keys	O
in	O
its	O
right	O
subtree	B-Application
,	O
reverse	O
in-order	O
traversal	O
retrieves	O
the	O
keys	O
in	O
descending	O
sorted	O
order	O
.	O
</s>
<s>
To	O
traverse	O
arbitrary	O
trees	O
(	O
not	O
necessarily	O
binary	O
trees	O
)	O
with	O
depth-first	B-Algorithm
search	I-Algorithm
,	O
perform	O
the	O
following	O
operations	O
at	O
each	O
node	O
:	O
</s>
<s>
For	O
each	O
i	O
from	O
1	O
to	O
the	O
current	O
node	O
's	O
number	O
of	O
subtrees	B-Application
−	O
1	O
,	O
or	O
from	O
the	O
latter	O
to	O
the	O
former	O
for	O
reverse	O
traversal	O
,	O
do	O
:	O
</s>
<s>
Recursively	O
traverse	O
the	O
current	O
node	O
's	O
i-th	O
subtree	B-Application
.	O
</s>
<s>
Recursively	O
traverse	O
the	O
current	O
node	O
's	O
last	O
subtree	B-Application
.	O
</s>
<s>
Depending	O
on	O
the	O
problem	O
at	O
hand	O
,	O
pre-order	O
,	O
post-order	O
,	O
and	O
especially	O
one	O
of	O
the	O
number	O
of	O
subtrees	B-Application
−	O
1	O
in-order	O
operations	O
may	O
be	O
optional	O
.	O
</s>
<s>
In	O
breadth-first	B-Algorithm
search	I-Algorithm
(	O
BFS	O
)	O
or	O
level-order	O
search	O
,	O
the	O
search	B-Data_Structure
tree	I-Data_Structure
is	O
broadened	O
as	O
much	O
as	O
possible	O
before	O
going	O
to	O
the	O
next	O
depth	O
.	O
</s>
<s>
There	O
are	O
also	O
tree	B-Algorithm
traversal	I-Algorithm
algorithms	O
that	O
classify	O
as	O
neither	O
depth-first	B-Algorithm
search	I-Algorithm
nor	O
breadth-first	B-Algorithm
search	I-Algorithm
.	O
</s>
<s>
One	O
such	O
algorithm	O
is	O
Monte	B-Application
Carlo	I-Application
tree	I-Application
search	I-Application
,	O
which	O
concentrates	O
on	O
analyzing	O
the	O
most	O
promising	O
moves	O
,	O
basing	O
the	O
expansion	O
of	O
the	O
search	B-Data_Structure
tree	I-Data_Structure
on	O
random	B-Algorithm
sampling	I-Algorithm
of	O
the	O
search	O
space	O
.	O
</s>
<s>
Pre-order	O
traversal	O
can	O
be	O
used	O
to	O
make	O
a	O
prefix	O
expression	O
(	O
Polish	O
notation	O
)	O
from	O
expression	B-Algorithm
trees	I-Algorithm
:	O
traverse	O
the	O
expression	B-Algorithm
tree	I-Algorithm
pre-orderly	O
.	O
</s>
<s>
Preorder	B-Algorithm
traversal	I-Algorithm
is	O
also	O
used	O
to	O
create	O
a	O
copy	O
of	O
the	O
tree	O
.	O
</s>
<s>
Traversing	O
the	O
depicted	O
arithmetic	O
expression	O
in	O
post-order	O
yields	O
"	O
A	O
B	O
C	O
−	O
*	O
D	O
E	O
+	O
+	O
"	O
;	O
the	O
latter	O
can	O
easily	O
be	O
transformed	O
into	O
machine	B-Language
code	I-Language
to	O
evaluate	O
the	O
expression	O
by	O
a	O
stack	B-Application
machine	I-Application
.	O
</s>
<s>
Postorder	B-Algorithm
traversal	I-Algorithm
is	O
also	O
used	O
to	O
delete	O
the	O
tree	O
.	O
</s>
<s>
In-order	O
traversal	O
is	O
very	O
commonly	O
used	O
on	O
binary	B-Language
search	I-Language
trees	I-Language
because	O
it	O
returns	O
values	O
from	O
the	O
underlying	O
set	O
in	O
order	O
,	O
according	O
to	O
the	O
comparator	O
that	O
set	O
up	O
the	O
binary	B-Language
search	I-Language
tree	I-Language
.	O
</s>
<s>
The	O
node	O
to	O
be	O
started	O
with	O
may	O
have	O
been	O
found	O
in	O
the	O
binary	B-Language
search	I-Language
tree	I-Language
bst	O
by	O
means	O
of	O
a	O
standard	O
search	O
function	O
,	O
which	O
is	O
shown	O
here	O
in	O
an	O
implementation	O
without	O
parent	O
pointers	O
,	O
i.e.	O
</s>
<s>
it	O
uses	O
a	O
stack	B-Application
for	O
holding	O
the	O
ancestor	O
pointers	O
.	O
</s>
<s>
The	O
function	O
inorderNext	O
returns	O
an	O
in-order-neighbor	O
of	O
node	O
,	O
either	O
the	O
(	O
for	O
dir	O
=	O
1	O
)	O
or	O
the	O
(	O
for	O
dir	O
=	O
0	O
)	O
,	O
and	O
the	O
updated	O
stack	B-Application
,	O
so	O
that	O
the	O
binary	B-Language
search	I-Language
tree	I-Language
may	O
be	O
sequentially	O
in-order-traversed	O
and	O
searched	O
in	O
the	O
given	O
direction	O
dir	O
further	O
on	O
.	O
</s>
<s>
Note	O
that	O
the	O
function	O
does	O
not	O
use	O
keys	O
,	O
which	O
means	O
that	O
the	O
sequential	O
structure	O
is	O
completely	O
recorded	O
by	O
the	O
binary	B-Language
search	I-Language
tree	I-Language
’s	O
edges	O
.	O
</s>
<s>
For	O
traversals	O
without	O
change	O
of	O
direction	O
,	O
the	O
(	O
amortised	B-General_Concept
)	O
average	O
complexity	O
is	O
because	O
a	O
full	O
traversal	O
takes	O
steps	O
for	O
a	O
BST	O
of	O
size	O
1	O
step	O
for	O
edge	O
up	O
and	O
1	O
for	O
edge	O
down	O
.	O
</s>
<s>
All	O
the	O
above	O
implementations	O
require	O
stack	B-Application
space	O
proportional	O
to	O
the	O
height	O
of	O
the	O
tree	O
which	O
is	O
a	O
call	B-General_Concept
stack	I-General_Concept
for	O
the	O
recursive	O
and	O
a	O
parent	O
(	O
ancestor	O
)	O
stack	B-Application
for	O
the	O
iterative	O
ones	O
.	O
</s>
<s>
With	O
the	O
iterative	O
implementations	O
we	O
can	O
remove	O
the	O
stack	B-Application
requirement	O
by	O
maintaining	O
parent	O
pointers	O
in	O
each	O
node	O
,	O
or	O
by	O
threading	O
the	O
tree	O
(	O
next	O
section	O
)	O
.	O
</s>
<s>
Avoids	O
recursion	O
,	O
which	O
uses	O
a	O
call	B-General_Concept
stack	I-General_Concept
and	O
consumes	O
memory	O
and	O
time	O
.	O
</s>
<s>
Also	O
,	O
listed	O
below	O
is	O
pseudocode	O
for	O
a	O
simple	O
queue	B-Application
based	O
level-order	O
traversal	O
,	O
and	O
will	O
require	O
space	O
proportional	O
to	O
the	O
maximum	O
number	O
of	O
nodes	O
at	O
a	O
given	O
depth	O
.	O
</s>
<s>
A	O
more	O
space-efficient	O
approach	O
for	O
this	O
type	O
of	O
traversal	O
can	O
be	O
implemented	O
using	O
an	O
iterative	B-Algorithm
deepening	I-Algorithm
depth-first	I-Algorithm
search	I-Algorithm
.	O
</s>
<s>
While	O
traversal	O
is	O
usually	O
done	O
for	O
trees	O
with	O
a	O
finite	O
number	O
of	O
nodes	O
(	O
and	O
hence	O
finite	O
depth	O
and	O
finite	O
branching	B-Data_Structure
factor	I-Data_Structure
)	O
it	O
can	O
also	O
be	O
done	O
for	O
infinite	O
trees	O
.	O
</s>
<s>
This	O
is	O
of	O
particular	O
interest	O
in	O
functional	B-Language
programming	I-Language
(	O
particularly	O
with	O
lazy	O
evaluation	O
)	O
,	O
as	O
infinite	O
data	O
structures	O
can	O
often	O
be	O
easily	O
defined	O
and	O
worked	O
with	O
,	O
though	O
they	O
are	O
not	O
(	O
strictly	O
)	O
evaluated	O
,	O
as	O
this	O
would	O
take	O
infinite	O
time	O
.	O
</s>
<s>
Some	O
finite	O
trees	O
are	O
too	O
large	O
to	O
represent	O
explicitly	O
,	O
such	O
as	O
the	O
game	O
tree	O
for	O
chess	B-Application
or	O
go	O
,	O
and	O
so	O
it	O
is	O
useful	O
to	O
analyze	O
them	O
as	O
if	O
they	O
were	O
infinite	O
.	O
</s>
<s>
For	O
example	O
,	O
given	O
a	O
binary	O
tree	O
of	O
infinite	O
depth	O
,	O
a	O
depth-first	B-Algorithm
search	I-Algorithm
will	O
go	O
down	O
one	O
side	O
(	O
by	O
convention	O
the	O
left	O
side	O
)	O
of	O
the	O
tree	O
,	O
never	O
visiting	O
the	O
rest	O
,	O
and	O
indeed	O
an	O
in-order	O
or	O
post-order	O
traversal	O
will	O
never	O
visit	O
any	O
nodes	O
,	O
as	O
it	O
has	O
not	O
reached	O
a	O
leaf	O
(	O
and	O
in	O
fact	O
never	O
will	O
)	O
.	O
</s>
<s>
By	O
contrast	O
,	O
a	O
breadth-first	B-Algorithm
(	O
level-order	O
)	O
traversal	O
will	O
traverse	O
a	O
binary	O
tree	O
of	O
infinite	O
depth	O
without	O
problem	O
,	O
and	O
indeed	O
will	O
traverse	O
any	O
tree	O
with	O
bounded	O
branching	B-Data_Structure
factor	I-Data_Structure
.	O
</s>
<s>
On	O
the	O
other	O
hand	O
,	O
given	O
a	O
tree	O
of	O
depth	O
2	O
,	O
where	O
the	O
root	O
has	O
infinitely	O
many	O
children	O
,	O
and	O
each	O
of	O
these	O
children	O
has	O
two	O
children	O
,	O
a	O
depth-first	B-Algorithm
search	I-Algorithm
will	O
visit	O
all	O
nodes	O
,	O
as	O
once	O
it	O
exhausts	O
the	O
grandchildren	O
(	O
children	O
of	O
children	O
of	O
one	O
node	O
)	O
,	O
it	O
will	O
move	O
on	O
to	O
the	O
next	O
(	O
assuming	O
it	O
is	O
not	O
post-order	O
,	O
in	O
which	O
case	O
it	O
never	O
reaches	O
the	O
root	O
)	O
.	O
</s>
<s>
By	O
contrast	O
,	O
a	O
breadth-first	B-Algorithm
search	I-Algorithm
will	O
never	O
reach	O
the	O
grandchildren	O
,	O
as	O
it	O
seeks	O
to	O
exhaust	O
the	O
children	O
first	O
.	O
</s>
<s>
A	O
more	O
sophisticated	O
analysis	O
of	O
running	O
time	O
can	O
be	O
given	O
via	O
infinite	O
ordinal	B-Language
numbers	I-Language
;	O
for	O
example	O
,	O
the	O
breadth-first	B-Algorithm
search	I-Algorithm
of	O
the	O
depth	O
2	O
tree	O
above	O
will	O
take	O
ω·2	O
steps	O
:	O
ω	B-Language
for	O
the	O
first	O
level	O
,	O
and	O
then	O
another	O
ω	B-Language
for	O
the	O
second	O
level	O
.	O
</s>
<s>
Thus	O
,	O
simple	O
depth-first	B-Algorithm
or	O
breadth-first	B-Algorithm
searches	I-Algorithm
do	O
not	O
traverse	O
every	O
infinite	O
tree	O
,	O
and	O
are	O
not	O
efficient	O
on	O
very	O
large	O
trees	O
.	O
</s>
<s>
The	O
nodes	O
are	O
thus	O
in	O
a	O
one-to-one	B-Algorithm
correspondence	I-Algorithm
with	O
finite	O
(	O
possibly	O
empty	O
)	O
sequences	O
of	O
positive	O
numbers	O
,	O
which	O
are	O
countable	O
and	O
can	O
be	O
placed	O
in	O
order	O
first	O
by	O
sum	O
of	O
entries	O
,	O
and	O
then	O
by	O
lexicographic	O
order	O
within	O
a	O
given	O
sum	O
(	O
only	O
finitely	O
many	O
sequences	O
sum	O
to	O
a	O
given	O
value	O
,	O
so	O
all	O
entries	O
are	O
reached	O
—	O
formally	O
there	O
are	O
a	O
finite	O
number	O
of	O
compositions	O
of	O
a	O
given	O
natural	O
number	O
,	O
specifically	O
2n−1	O
compositions	O
of	O
)	O
,	O
which	O
gives	O
a	O
traversal	O
.	O
</s>
<s>
This	O
can	O
be	O
interpreted	O
as	O
mapping	O
the	O
infinite	O
depth	O
binary	O
tree	O
onto	O
this	O
tree	O
and	O
then	O
applying	O
breadth-first	B-Algorithm
search	I-Algorithm
:	O
replace	O
the	O
"	O
down	O
"	O
edges	O
connecting	O
a	O
parent	B-Application
node	I-Application
to	O
its	O
second	O
and	O
later	O
children	O
with	O
"	O
right	O
"	O
edges	O
from	O
the	O
first	O
child	O
to	O
the	O
second	O
child	O
,	O
from	O
the	O
second	O
child	O
to	O
the	O
third	O
child	O
,	O
etc	O
.	O
</s>
