<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
recursion	O
is	O
a	O
method	O
of	O
solving	O
a	O
computational	O
problem	O
where	O
the	O
solution	O
depends	O
on	O
solutions	O
to	O
smaller	O
instances	O
of	O
the	O
same	O
problem	O
.	O
</s>
<s>
The	O
approach	O
can	O
be	O
applied	O
to	O
many	O
types	O
of	O
problems	O
,	O
and	O
recursion	O
is	O
one	O
of	O
the	O
central	O
ideas	O
of	O
computer	B-General_Concept
science	I-General_Concept
.	O
</s>
<s>
Some	O
functional	B-Language
programming	I-Language
languages	I-Language
(	O
for	O
instance	O
,	O
Clojure	B-Language
)	O
do	O
not	O
define	O
any	O
looping	O
constructs	O
but	O
rely	O
solely	O
on	O
recursion	O
to	O
repeatedly	O
call	O
code	O
.	O
</s>
<s>
It	O
is	O
proved	O
in	O
computability	O
theory	O
that	O
these	O
recursive-only	O
languages	O
are	O
Turing	B-Algorithm
complete	I-Algorithm
;	O
this	O
means	O
that	O
they	O
are	O
as	O
powerful	O
(	O
they	O
can	O
be	O
used	O
to	O
solve	O
the	O
same	O
problems	O
)	O
as	O
imperative	B-Application
languages	I-Application
based	O
on	O
control	O
structures	O
such	O
as	O
and	O
.	O
</s>
<s>
Repeatedly	O
calling	O
a	O
function	O
from	O
within	O
itself	O
may	O
cause	O
the	O
call	B-General_Concept
stack	I-General_Concept
to	O
have	O
a	O
size	O
equal	O
to	O
the	O
sum	O
of	O
the	O
input	O
sizes	O
of	O
all	O
involved	O
calls	O
.	O
</s>
<s>
It	O
follows	O
that	O
,	O
for	O
problems	O
that	O
can	O
be	O
solved	O
easily	O
by	O
iteration	B-Algorithm
,	O
recursion	O
is	O
generally	O
less	O
efficient	B-General_Concept
,	O
and	O
,	O
for	O
large	O
problems	O
,	O
it	O
is	O
fundamental	O
to	O
use	O
optimization	O
techniques	O
such	O
as	O
tail	B-Language
call	I-Language
optimization	I-Language
.	O
</s>
<s>
This	O
is	O
often	O
referred	O
to	O
as	O
the	O
divide-and-conquer	B-Algorithm
method	I-Algorithm
;	O
when	O
combined	O
with	O
a	O
lookup	B-Data_Structure
table	I-Data_Structure
that	O
stores	O
the	O
results	O
of	O
previously	O
solved	O
sub-problems	O
(	O
to	O
avoid	O
solving	O
them	O
repeatedly	O
and	O
incurring	O
extra	O
computation	O
time	O
)	O
,	O
it	O
can	O
be	O
referred	O
to	O
as	O
dynamic	B-Algorithm
programming	I-Algorithm
or	O
memoization	O
.	O
</s>
<s>
(	O
Functions	O
that	O
are	O
not	O
intended	O
to	O
terminate	O
under	O
normal	O
circumstances	O
—	O
for	O
example	O
,	O
some	O
system	B-Operating_System
and	I-Operating_System
server	I-Operating_System
processes	I-Operating_System
—	O
are	O
an	O
exception	O
to	O
this	O
.	O
)	O
</s>
<s>
Neglecting	O
to	O
write	O
a	O
base	O
case	O
,	O
or	O
testing	O
for	O
it	O
incorrectly	O
,	O
can	O
cause	O
an	O
infinite	B-Algorithm
loop	I-Algorithm
.	O
</s>
<s>
Such	O
an	O
example	O
is	O
more	O
naturally	O
treated	O
by	O
corecursion	B-Application
,	O
where	O
successive	O
terms	O
in	O
the	O
output	O
are	O
the	O
partial	O
sums	O
;	O
this	O
can	O
be	O
converted	O
to	O
a	O
recursion	O
by	O
using	O
the	O
indexing	O
parameter	O
to	O
say	O
"	O
compute	O
the	O
nth	O
term	O
(	O
nth	O
partial	O
sum	O
)	O
"	O
.	O
</s>
<s>
Many	O
computer	B-Application
programs	I-Application
must	O
process	B-Operating_System
or	O
generate	O
an	O
arbitrarily	O
large	O
quantity	O
of	O
data	O
.	O
</s>
<s>
Recursion	O
is	O
a	O
technique	O
for	O
representing	O
data	O
whose	O
exact	O
size	O
is	O
unknown	O
to	O
the	O
programmer	B-Application
:	O
the	O
programmer	B-Application
can	O
specify	O
this	O
data	O
with	O
a	O
self-referential	O
definition	O
.	O
</s>
<s>
There	O
are	O
two	O
types	O
of	O
self-referential	O
definitions	O
:	O
inductive	O
and	O
coinductive	B-Application
definitions	O
.	O
</s>
<s>
For	O
example	O
,	O
linked	B-Data_Structure
lists	I-Data_Structure
can	O
be	O
defined	O
inductively	O
(	O
here	O
,	O
using	O
Haskell	B-Language
syntax	O
)	O
:	O
</s>
<s>
A	O
coinductive	B-Application
data	O
definition	O
is	O
one	O
that	O
specifies	O
the	O
operations	O
that	O
may	O
be	O
performed	O
on	O
a	O
piece	O
of	O
data	O
;	O
typically	O
,	O
self-referential	O
coinductive	B-Application
definitions	O
are	O
used	O
for	O
data	O
structures	O
of	O
infinite	O
size	O
.	O
</s>
<s>
A	O
coinductive	B-Application
definition	O
of	O
infinite	O
streams	B-Architecture
of	O
strings	O
,	O
given	O
informally	O
,	O
might	O
look	O
like	O
this	O
:	O
</s>
<s>
Corecursion	B-Application
is	O
related	O
to	O
coinduction	B-Application
,	O
and	O
can	O
be	O
used	O
to	O
compute	O
particular	O
instances	O
of	O
(	O
possibly	O
)	O
infinite	O
objects	O
.	O
</s>
<s>
The	O
problem	O
of	O
computing	O
the	O
first	O
n	O
prime	O
numbers	O
is	O
one	O
that	O
can	O
be	O
solved	O
with	O
a	O
corecursive	B-Application
program	O
(	O
e.g.	O
</s>
<s>
Standard	O
examples	O
of	O
single	B-Algorithm
recursion	I-Algorithm
include	O
list	O
traversal	O
,	O
such	O
as	O
in	O
a	O
linear	O
search	O
,	O
or	O
computing	O
the	O
factorial	O
function	O
,	O
while	O
standard	O
examples	O
of	O
multiple	O
recursion	O
include	O
tree	B-Algorithm
traversal	I-Algorithm
,	O
such	O
as	O
in	O
a	O
depth-first	B-Algorithm
search	I-Algorithm
.	O
</s>
<s>
Single	B-Algorithm
recursion	I-Algorithm
is	O
often	O
much	O
more	O
efficient	B-General_Concept
than	O
multiple	O
recursion	O
,	O
and	O
can	O
generally	O
be	O
replaced	O
by	O
an	O
iterative	B-Algorithm
computation	O
,	O
running	O
in	O
linear	O
time	O
and	O
requiring	O
constant	O
space	O
.	O
</s>
<s>
Multiple	O
recursion	O
,	O
by	O
contrast	O
,	O
may	O
require	O
exponential	O
time	O
and	O
space	O
,	O
and	O
is	O
more	O
fundamentally	O
recursive	O
,	O
not	O
being	O
able	O
to	O
be	O
replaced	O
by	O
iteration	B-Algorithm
without	O
an	O
explicit	O
stack	B-Application
.	O
</s>
<s>
Multiple	O
recursion	O
can	O
sometimes	O
be	O
converted	O
to	O
single	B-Algorithm
recursion	I-Algorithm
(	O
and	O
,	O
if	O
desired	O
,	O
thence	O
to	O
iteration	B-Algorithm
)	O
.	O
</s>
<s>
For	O
example	O
,	O
while	O
computing	O
the	O
Fibonacci	O
sequence	O
naively	O
entails	O
multiple	O
iteration	B-Algorithm
,	O
as	O
each	O
value	O
requires	O
two	O
previous	O
values	O
,	O
it	O
can	O
be	O
computed	O
by	O
single	B-Algorithm
recursion	I-Algorithm
by	O
passing	O
two	O
successive	O
values	O
as	O
parameters	O
.	O
</s>
<s>
This	O
is	O
more	O
naturally	O
framed	O
as	O
corecursion	B-Application
,	O
building	O
up	O
from	O
the	O
initial	O
values	O
,	O
while	O
tracking	O
two	O
successive	O
values	O
at	O
each	O
step	O
–	O
see	O
corecursion	B-Application
:	O
examples	O
.	O
</s>
<s>
A	O
more	O
sophisticated	O
example	O
involves	O
using	O
a	O
threaded	B-Data_Structure
binary	I-Data_Structure
tree	I-Data_Structure
,	O
which	O
allows	O
iterative	B-Algorithm
tree	B-Algorithm
traversal	I-Algorithm
,	O
rather	O
than	O
multiple	O
recursion	O
.	O
</s>
<s>
Indirect	O
recursion	O
is	O
also	O
called	O
mutual	B-Algorithm
recursion	I-Algorithm
,	O
which	O
is	O
a	O
more	O
symmetric	O
term	O
,	O
though	O
this	O
is	O
simply	O
a	O
difference	O
of	O
emphasis	O
,	O
not	O
a	O
different	O
notion	O
.	O
</s>
<s>
Similarly	O
a	O
set	O
of	O
three	O
or	O
more	O
functions	O
that	O
call	O
each	O
other	O
can	O
be	O
called	O
a	O
set	O
of	O
mutually	B-Algorithm
recursive	I-Algorithm
functions	O
.	O
</s>
<s>
However	O
,	O
recursion	O
can	O
also	O
be	O
done	O
via	O
implicitly	O
calling	O
a	O
function	O
based	O
on	O
the	O
current	O
context	O
,	O
which	O
is	O
particularly	O
useful	O
for	O
anonymous	B-General_Concept
functions	I-General_Concept
,	O
and	O
is	O
known	O
as	O
anonymous	B-Language
recursion	I-Language
.	O
</s>
<s>
Structural	B-Algorithm
recursion	I-Algorithm
includes	O
nearly	O
all	O
tree	B-Algorithm
traversals	I-Algorithm
,	O
including	O
XML	O
processing	O
,	O
binary	O
tree	O
creation	O
and	O
search	O
,	O
etc	O
.	O
</s>
<s>
By	O
considering	O
the	O
algebraic	O
structure	O
of	O
the	O
natural	O
numbers	O
(	O
that	O
is	O
,	O
a	O
natural	O
number	O
is	O
either	O
zero	O
or	O
the	O
successor	O
of	O
a	O
natural	O
number	O
)	O
,	O
functions	O
such	O
as	O
factorial	O
may	O
also	O
be	O
regarded	O
as	O
structural	B-Algorithm
recursion	I-Algorithm
.	O
</s>
<s>
All	O
structurally	O
recursive	O
functions	O
on	O
finite	O
(	O
inductively	O
defined	O
)	O
data	O
structures	O
can	O
easily	O
be	O
shown	O
to	O
terminate	O
,	O
via	O
structural	B-Algorithm
induction	I-Algorithm
:	O
intuitively	O
,	O
each	O
recursive	O
call	O
receives	O
a	O
smaller	O
piece	O
of	O
input	O
data	O
,	O
until	O
a	O
base	O
case	O
is	O
reached	O
.	O
</s>
<s>
Generatively	O
recursive	O
functions	O
,	O
in	O
contrast	O
,	O
do	O
not	O
necessarily	O
feed	O
smaller	O
input	O
to	O
their	O
recursive	O
calls	O
,	O
so	O
proof	O
of	O
their	O
termination	O
is	O
not	O
necessarily	O
as	O
simple	O
,	O
and	O
avoiding	O
infinite	B-Algorithm
loops	I-Algorithm
requires	O
greater	O
care	O
.	O
</s>
<s>
These	O
generatively	O
recursive	O
functions	O
can	O
often	O
be	O
interpreted	O
as	O
corecursive	B-Application
functions	O
–	O
each	O
step	O
generates	O
the	O
new	O
data	O
,	O
such	O
as	O
successive	O
approximation	O
in	O
Newton	O
's	O
method	O
–	O
and	O
terminating	O
this	O
corecursion	B-Application
requires	O
that	O
the	O
data	O
eventually	O
satisfy	O
some	O
condition	O
,	O
which	O
is	O
not	O
necessarily	O
guaranteed	O
.	O
</s>
<s>
In	O
terms	O
of	O
loop	B-Application
variants	I-Application
,	O
structural	B-Algorithm
recursion	I-Algorithm
is	O
when	O
there	O
is	O
an	O
obvious	O
loop	B-Application
variant	I-Application
,	O
namely	O
size	O
or	O
complexity	O
,	O
which	O
starts	O
off	O
finite	O
and	O
decreases	O
at	O
each	O
recursive	O
step	O
.	O
</s>
<s>
By	O
contrast	O
,	O
generative	O
recursion	O
is	O
when	O
there	O
is	O
not	O
such	O
an	O
obvious	O
loop	B-Application
variant	I-Application
,	O
and	O
termination	O
depends	O
on	O
a	O
function	O
,	O
such	O
as	O
"	O
error	O
of	O
approximation	O
"	O
that	O
does	O
not	O
necessarily	O
decrease	O
to	O
zero	O
,	O
and	O
thus	O
termination	O
is	O
not	O
guaranteed	O
without	O
further	O
analysis	O
.	O
</s>
<s>
The	O
box	O
shows	O
C	B-Language
code	O
to	O
shortcut	O
factorial	O
cases	O
0	O
and	O
1	O
.	O
</s>
<s>
Short-circuiting	O
is	O
primarily	O
a	O
concern	O
when	O
many	O
base	O
cases	O
are	O
encountered	O
,	O
such	O
as	O
Null	O
pointers	O
in	O
a	O
tree	O
,	O
which	O
can	O
be	O
linear	O
in	O
the	O
number	O
of	O
function	O
calls	O
,	O
hence	O
significant	O
savings	O
for	O
algorithms	O
;	O
this	O
is	O
illustrated	O
below	O
for	O
a	O
depth-first	B-Algorithm
search	I-Algorithm
.	O
</s>
<s>
Conceptually	O
,	O
short-circuiting	O
can	O
be	O
considered	O
to	O
either	O
have	O
the	O
same	O
base	O
case	O
and	O
recursive	O
step	O
,	O
checking	O
the	O
base	O
case	O
only	O
before	O
the	O
recursion	O
,	O
or	O
it	O
can	O
be	O
considered	O
to	O
have	O
a	O
different	O
base	O
case	O
(	O
one	O
step	O
removed	O
from	O
standard	O
base	O
case	O
)	O
and	O
a	O
more	O
complex	O
recursive	O
step	O
,	O
namely	O
"	O
check	O
valid	O
then	O
recurse	O
"	O
,	O
as	O
in	O
considering	O
leaf	B-Application
nodes	I-Application
rather	O
than	O
Null	O
nodes	O
as	O
base	O
cases	O
in	O
a	O
tree	O
.	O
</s>
<s>
A	O
basic	O
example	O
of	O
short-circuiting	O
is	O
given	O
in	O
depth-first	B-Algorithm
search	I-Algorithm
(	O
DFS	O
)	O
of	O
a	O
binary	O
tree	O
;	O
see	O
binary	O
trees	B-Application
section	O
for	O
standard	O
recursive	O
discussion	O
.	O
</s>
<s>
Note	O
that	O
this	O
requires	O
a	O
wrapper	O
function	O
to	O
handle	O
the	O
case	O
when	O
the	O
tree	O
itself	O
is	O
empty	O
(	O
root	B-Application
node	I-Application
is	O
Null	O
)	O
.	O
</s>
<s>
In	O
C	B-Language
,	O
the	O
standard	O
recursive	O
algorithm	O
may	O
be	O
implemented	O
as	O
:	O
</s>
<s>
For	O
this	O
reason	O
efficient	B-General_Concept
implementations	O
of	O
recursive	O
algorithms	O
often	O
start	O
with	O
the	O
recursive	O
algorithm	O
,	O
but	O
then	O
switch	O
to	O
a	O
different	O
algorithm	O
when	O
the	O
input	O
becomes	O
small	O
.	O
</s>
<s>
An	O
important	O
example	O
is	O
merge	B-Algorithm
sort	I-Algorithm
,	O
which	O
is	O
often	O
implemented	O
by	O
switching	O
to	O
the	O
non-recursive	O
insertion	B-Algorithm
sort	I-Algorithm
when	O
the	O
data	O
is	O
sufficiently	O
small	O
,	O
as	O
in	O
the	O
tiled	O
merge	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
Hybrid	O
recursive	O
algorithms	O
can	O
often	O
be	O
further	O
refined	O
,	O
as	O
in	O
Timsort	B-Algorithm
,	O
derived	O
from	O
a	O
hybrid	O
merge	O
sort/insertion	O
sort	O
.	O
</s>
<s>
Recursion	O
and	O
iteration	B-Algorithm
are	O
equally	O
expressive	O
:	O
recursion	O
can	O
be	O
replaced	O
by	O
iteration	B-Algorithm
with	O
an	O
explicit	O
call	B-General_Concept
stack	I-General_Concept
,	O
while	O
iteration	B-Algorithm
can	O
be	O
replaced	O
with	O
tail	B-Language
recursion	I-Language
.	O
</s>
<s>
In	O
imperative	B-Application
programming	I-Application
,	O
iteration	B-Algorithm
is	O
preferred	O
,	O
particularly	O
for	O
simple	O
recursion	O
,	O
as	O
it	O
avoids	O
the	O
overhead	O
of	O
function	O
calls	O
and	O
call	B-General_Concept
stack	I-General_Concept
management	O
,	O
but	O
recursion	O
is	O
generally	O
used	O
for	O
multiple	O
recursion	O
.	O
</s>
<s>
By	O
contrast	O
,	O
in	O
functional	B-Language
languages	I-Language
recursion	O
is	O
preferred	O
,	O
with	O
tail	B-Language
recursion	I-Language
optimization	I-Language
leading	O
to	O
little	O
overhead	O
.	O
</s>
<s>
Implementing	O
an	O
algorithm	O
using	O
iteration	B-Algorithm
may	O
not	O
be	O
easily	O
achievable	O
.	O
</s>
<s>
For	O
an	O
imperative	B-Application
language	I-Application
the	O
overhead	O
is	O
to	O
define	O
the	O
function	O
,	O
and	O
for	O
a	O
functional	B-Language
language	I-Language
the	O
overhead	O
is	O
to	O
define	O
the	O
accumulator	O
variable	O
x	O
.	O
</s>
<s>
For	O
example	O
,	O
a	O
factorial	O
function	O
may	O
be	O
implemented	O
iteratively	O
in	O
C	B-Language
by	O
assigning	O
to	O
a	O
loop	O
index	O
variable	O
and	O
accumulator	O
variable	O
,	O
rather	O
than	O
by	O
passing	O
arguments	O
and	O
returning	O
values	O
by	O
recursion	O
:	O
</s>
<s>
When	O
such	O
a	O
function	O
is	O
called	O
,	O
the	O
program	O
's	O
runtime	B-Device
environment	I-Device
keeps	O
track	O
of	O
the	O
various	O
instances	O
of	O
the	O
function	O
(	O
often	O
using	O
a	O
call	B-General_Concept
stack	I-General_Concept
,	O
although	O
other	O
methods	O
may	O
be	O
used	O
)	O
.	O
</s>
<s>
Every	O
recursive	O
function	O
can	O
be	O
transformed	O
into	O
an	O
iterative	B-Algorithm
function	O
by	O
replacing	O
recursive	O
calls	O
with	O
iterative	B-Algorithm
control	O
constructs	O
and	O
simulating	O
the	O
call	B-General_Concept
stack	I-General_Concept
with	O
a	O
stack	B-Application
explicitly	O
managed	O
by	O
the	O
program	O
.	O
</s>
<s>
Conversely	O
,	O
all	O
iterative	B-Algorithm
functions	O
and	O
procedures	O
that	O
can	O
be	O
evaluated	O
by	O
a	O
computer	O
(	O
see	O
Turing	B-Algorithm
completeness	I-Algorithm
)	O
can	O
be	O
expressed	O
in	O
terms	O
of	O
recursive	O
functions	O
;	O
iterative	B-Algorithm
control	O
constructs	O
such	O
as	O
while	O
loops	O
and	O
for	B-Language
loops	I-Language
are	O
routinely	O
rewritten	O
in	O
recursive	O
form	O
in	O
functional	B-Language
languages	I-Language
.	O
</s>
<s>
However	O
,	O
in	O
practice	O
this	O
rewriting	O
depends	O
on	O
tail	B-Language
call	I-Language
elimination	I-Language
,	O
which	O
is	O
not	O
a	O
feature	O
of	O
all	O
languages	O
.	O
</s>
<s>
C	B-Language
,	O
Java	B-Language
,	O
and	O
Python	B-Language
are	O
notable	O
mainstream	O
languages	O
in	O
which	O
all	O
function	O
calls	O
,	O
including	O
tail	B-Language
calls	I-Language
,	O
may	O
cause	O
stack	B-General_Concept
allocation	I-General_Concept
that	O
would	O
not	O
occur	O
with	O
the	O
use	O
of	O
looping	O
constructs	O
;	O
in	O
these	O
languages	O
,	O
a	O
working	O
iterative	B-Algorithm
program	O
rewritten	O
in	O
recursive	O
form	O
may	O
overflow	B-Error_Name
the	I-Error_Name
call	I-Error_Name
stack	I-Error_Name
,	O
although	O
tail	B-Language
call	I-Language
elimination	I-Language
may	O
be	O
a	O
feature	O
that	O
is	O
not	O
covered	O
by	O
a	O
language	O
's	O
specification	O
,	O
and	O
different	O
implementations	O
of	O
the	O
same	O
language	O
may	O
differ	O
in	O
tail	B-Language
call	I-Language
elimination	I-Language
capabilities	O
.	O
</s>
<s>
In	O
languages	O
(	O
such	O
as	O
C	B-Language
and	O
Java	B-Language
)	O
that	O
favor	O
iterative	B-Algorithm
looping	O
constructs	O
,	O
there	O
is	O
usually	O
significant	O
time	O
and	O
space	O
cost	O
associated	O
with	B-Language
recursive	I-Language
programs	O
,	O
due	O
to	O
the	O
overhead	O
required	O
to	O
manage	O
the	O
stack	B-Application
and	O
the	O
relative	O
slowness	O
of	O
function	O
calls	O
;	O
in	O
functional	B-Language
languages	I-Language
,	O
a	O
function	O
call	O
(	O
particularly	O
a	O
tail	B-Language
call	I-Language
)	O
is	O
typically	O
a	O
very	O
fast	O
operation	O
,	O
and	O
the	O
difference	O
is	O
usually	O
less	O
noticeable	O
.	O
</s>
<s>
As	O
a	O
concrete	O
example	O
,	O
the	O
difference	O
in	O
performance	O
between	O
recursive	O
and	O
iterative	B-Algorithm
implementations	O
of	O
the	O
"	O
factorial	O
"	O
example	O
above	O
depends	O
highly	O
on	O
the	O
compiler	B-Language
used	O
.	O
</s>
<s>
In	O
languages	O
where	O
looping	O
constructs	O
are	O
preferred	O
,	O
the	O
iterative	B-Algorithm
version	O
may	O
be	O
as	O
much	O
as	O
several	O
orders	O
of	O
magnitude	O
faster	O
than	O
the	O
recursive	O
one	O
.	O
</s>
<s>
In	O
functional	B-Language
languages	I-Language
,	O
the	O
overall	O
time	O
difference	O
of	O
the	O
two	O
implementations	O
may	O
be	O
negligible	O
;	O
in	O
fact	O
,	O
the	O
cost	O
of	O
multiplying	O
the	O
larger	O
numbers	O
first	O
rather	O
than	O
the	O
smaller	O
numbers	O
(	O
which	O
the	O
iterative	B-Algorithm
version	O
given	O
here	O
happens	O
to	O
do	O
)	O
may	O
overwhelm	O
any	O
time	O
saved	O
by	O
choosing	O
iteration	B-Algorithm
.	O
</s>
<s>
In	O
some	O
programming	O
languages	O
,	O
the	O
maximum	O
size	O
of	O
the	O
call	B-General_Concept
stack	I-General_Concept
is	O
much	O
less	O
than	O
the	O
space	O
available	O
in	O
the	O
heap	O
,	O
and	O
recursive	O
algorithms	O
tend	O
to	O
require	O
more	O
stack	B-Application
space	O
than	O
iterative	B-Algorithm
algorithms	O
.	O
</s>
<s>
Consequently	O
,	O
these	O
languages	O
sometimes	O
place	O
a	O
limit	O
on	O
the	O
depth	O
of	O
recursion	O
to	O
avoid	O
stack	B-Error_Name
overflows	I-Error_Name
;	O
Python	B-Language
is	O
one	O
such	O
language	O
.	O
</s>
<s>
Note	O
the	O
caveat	O
below	O
regarding	O
the	O
special	O
case	O
of	O
tail	B-Language
recursion	I-Language
.	O
</s>
<s>
Because	O
recursive	O
algorithms	O
can	O
be	O
subject	O
to	O
stack	B-Error_Name
overflows	I-Error_Name
,	O
they	O
may	O
be	O
vulnerable	O
to	O
pathological	O
or	O
malicious	O
input	O
.	O
</s>
<s>
Some	O
malware	O
specifically	O
targets	O
a	O
program	O
's	O
call	B-General_Concept
stack	I-General_Concept
and	O
takes	O
advantage	O
of	O
the	O
stack	B-Application
's	O
inherently	O
recursive	O
nature	O
.	O
</s>
<s>
Even	O
in	O
the	O
absence	O
of	O
malware	O
,	O
a	O
stack	B-General_Concept
overflow	I-General_Concept
caused	O
by	O
unbounded	O
recursion	O
can	O
be	O
fatal	O
to	O
the	O
program	O
,	O
and	O
exception	B-General_Concept
handling	I-General_Concept
logic	O
may	O
not	O
prevent	O
the	O
corresponding	O
process	B-Operating_System
from	O
being	O
terminated	O
.	O
</s>
<s>
One	O
example	O
is	O
tree	B-Algorithm
traversal	I-Algorithm
as	O
in	O
depth-first	B-Algorithm
search	I-Algorithm
;	O
though	O
both	O
recursive	O
and	O
iterative	B-Algorithm
methods	O
are	O
used	O
,	O
they	O
contrast	O
with	O
list	O
traversal	O
and	O
linear	O
search	O
in	O
a	O
list	O
,	O
which	O
is	O
a	O
singly	O
recursive	O
and	O
thus	O
naturally	O
iterative	B-Algorithm
method	O
.	O
</s>
<s>
Other	O
examples	O
include	O
divide-and-conquer	B-Algorithm
algorithms	I-Algorithm
such	O
as	O
Quicksort	B-Algorithm
,	O
and	O
functions	O
such	O
as	O
the	O
Ackermann	O
function	O
.	O
</s>
<s>
All	O
of	O
these	O
algorithms	O
can	O
be	O
implemented	O
iteratively	O
with	O
the	O
help	O
of	O
an	O
explicit	O
stack	B-Application
,	O
but	O
the	O
programmer	B-Application
effort	O
involved	O
in	O
managing	O
the	O
stack	B-Application
,	O
and	O
the	O
complexity	O
of	O
the	O
resulting	O
program	O
,	O
arguably	O
outweigh	O
any	O
advantages	O
of	O
the	O
iterative	B-Algorithm
solution	O
.	O
</s>
<s>
One	O
method	O
for	O
replacing	O
recursive	O
algorithms	O
is	O
to	O
simulate	O
them	O
using	O
heap	B-General_Concept
memory	I-General_Concept
in	O
place	O
of	O
stack	B-General_Concept
memory	I-General_Concept
.	O
</s>
<s>
For	O
example	O
,	O
recursive	O
algorithms	O
for	O
matching	B-Algorithm
wildcards	I-Algorithm
,	O
such	O
as	O
Rich	B-Application
Salz	I-Application
 '	O
wildmat	B-Algorithm
algorithm	O
,	O
were	O
once	O
typical	O
.	O
</s>
<s>
Non-recursive	O
algorithms	O
for	O
the	O
same	O
purpose	O
,	O
such	O
as	O
the	O
Krauss	B-Algorithm
matching	I-Algorithm
wildcards	I-Algorithm
algorithm	I-Algorithm
,	O
have	O
been	O
developed	O
to	O
avoid	O
the	O
drawbacks	O
of	O
recursion	O
and	O
have	O
improved	O
only	O
gradually	O
based	O
on	O
techniques	O
such	O
as	O
collecting	O
tests	O
and	O
profiling	O
performance	O
.	O
</s>
<s>
Tail-recursive	B-Language
functions	I-Language
are	O
functions	O
in	O
which	O
all	O
recursive	O
calls	O
are	O
tail	B-Language
calls	I-Language
and	O
hence	O
do	O
not	O
build	O
up	O
any	O
deferred	O
operations	O
.	O
</s>
<s>
For	O
example	O
,	O
the	O
gcd	O
function	O
(	O
shown	O
again	O
below	O
)	O
is	O
tail-recursive	B-Language
.	O
</s>
<s>
In	O
contrast	O
,	O
the	O
factorial	O
function	O
(	O
also	O
below	O
)	O
is	O
not	O
tail-recursive	B-Language
;	O
because	O
its	O
recursive	O
call	O
is	O
not	O
in	O
tail	O
position	O
,	O
it	O
builds	O
up	O
deferred	O
multiplication	O
operations	O
that	O
must	O
be	O
performed	O
after	O
the	O
final	O
recursive	O
call	O
completes	O
.	O
</s>
<s>
With	O
a	O
compiler	B-Language
or	O
interpreter	B-Application
that	O
treats	O
tail-recursive	B-Language
calls	O
as	O
jumps	B-Application
rather	O
than	O
function	O
calls	O
,	O
a	O
tail-recursive	B-Language
function	I-Language
such	O
as	O
gcd	O
will	O
execute	O
using	O
constant	O
space	O
.	O
</s>
<s>
Thus	O
the	O
program	O
is	O
essentially	O
iterative	B-Algorithm
,	O
equivalent	O
to	O
using	O
imperative	B-Application
language	I-Application
control	O
structures	O
like	O
the	O
"	O
for	O
"	O
and	O
"	O
while	O
"	O
loops	O
.	O
</s>
<s>
The	O
significance	O
of	O
tail	B-Language
recursion	I-Language
is	O
that	O
when	O
making	O
a	O
tail-recursive	B-Language
call	O
(	O
or	O
any	O
tail	B-Language
call	I-Language
)	O
,	O
the	O
caller	O
's	O
return	O
position	O
need	O
not	O
be	O
saved	O
on	O
the	O
call	B-General_Concept
stack	I-General_Concept
;	O
when	O
the	O
recursive	O
call	O
returns	O
,	O
it	O
will	O
branch	O
directly	O
on	O
the	O
previously	O
saved	O
return	O
position	O
.	O
</s>
<s>
Therefore	O
,	O
in	O
languages	O
that	O
recognize	O
this	O
property	O
of	O
tail	B-Language
calls	I-Language
,	O
tail	B-Language
recursion	I-Language
saves	O
both	O
space	O
and	O
time	O
.	O
</s>
<s>
Also	O
note	O
that	O
the	O
order	O
of	O
the	O
print	O
statements	O
is	O
reversed	O
,	O
which	O
is	O
due	O
to	O
the	O
way	O
the	O
functions	O
and	O
statements	O
are	O
stored	O
on	O
the	O
call	B-General_Concept
stack	I-General_Concept
.	O
</s>
<s>
Pseudocode	B-Language
(	O
recursive	O
)	O
:	O
function	O
factorial	O
is	O
:	O
</s>
<s>
This	O
evaluation	O
of	O
the	O
recurrence	O
relation	O
demonstrates	O
the	O
computation	O
that	O
would	O
be	O
performed	O
in	O
evaluating	O
the	O
pseudocode	B-Language
above	O
:	O
</s>
<s>
This	O
factorial	O
function	O
can	O
also	O
be	O
described	O
without	O
using	O
recursion	O
by	O
making	O
use	O
of	O
the	O
typical	O
looping	O
constructs	O
found	O
in	O
imperative	B-Application
programming	I-Application
languages	I-Application
:	O
</s>
<s>
Pseudocode	B-Language
(	O
iterative	B-Algorithm
)	O
:	O
function	O
factorial	O
is	O
:	O
</s>
<s>
The	O
imperative	B-Application
code	O
above	O
is	O
equivalent	O
to	O
this	O
mathematical	O
definition	O
using	O
an	O
accumulator	O
variable	O
:	O
</s>
<s>
The	O
definition	O
above	O
translates	O
straightforwardly	O
to	O
functional	B-Language
programming	I-Language
languages	I-Language
such	O
as	O
Scheme	B-Language
;	O
this	O
is	O
an	O
example	O
of	O
iteration	B-Algorithm
implemented	O
recursively	O
.	O
</s>
<s>
Pseudocode	B-Language
(	O
recursive	O
)	O
:	O
function	O
gcd	O
is	O
:	O
</s>
<s>
The	O
recursive	O
program	O
above	O
is	O
tail-recursive	B-Language
;	O
it	O
is	O
equivalent	O
to	O
an	O
iterative	B-Algorithm
algorithm	O
,	O
and	O
the	O
computation	O
shown	O
above	O
shows	O
the	O
steps	O
of	O
evaluation	O
that	O
would	O
be	O
performed	O
by	O
a	O
language	O
that	O
eliminates	O
tail	B-Language
calls	I-Language
.	O
</s>
<s>
Below	O
is	O
a	O
version	O
of	O
the	O
same	O
algorithm	O
using	O
explicit	O
iteration	B-Algorithm
,	O
suitable	O
for	O
a	O
language	O
that	O
does	O
not	O
eliminate	O
tail	B-Language
calls	I-Language
.	O
</s>
<s>
By	O
maintaining	O
its	O
state	O
entirely	O
in	O
the	O
variables	O
x	O
and	O
y	O
and	O
using	O
a	O
looping	O
construct	O
,	O
the	O
program	O
avoids	O
making	O
recursive	O
calls	O
and	O
growing	O
the	O
call	B-General_Concept
stack	I-General_Concept
.	O
</s>
<s>
Pseudocode	B-Language
(	O
iterative	B-Algorithm
)	O
:	O
function	O
gcd	O
is	O
:	O
</s>
<s>
The	O
iterative	B-Algorithm
algorithm	O
requires	O
a	O
temporary	O
variable	O
,	O
and	O
even	O
given	O
knowledge	O
of	O
the	O
Euclidean	O
algorithm	O
it	O
is	O
more	O
difficult	O
to	O
understand	O
the	O
process	B-Operating_System
by	O
simple	O
inspection	O
,	O
although	O
the	O
two	O
algorithms	O
are	O
very	O
similar	O
in	O
their	O
steps	O
.	O
</s>
<s>
There	O
are	O
three	O
pegs	O
which	O
can	O
hold	O
stacks	B-Application
of	O
disks	O
of	O
different	O
diameters	O
.	O
</s>
<s>
What	O
is	O
the	O
smallest	O
number	O
of	O
steps	O
to	O
move	O
the	O
stack	B-Application
?	O
</s>
<s>
Pseudocode	B-Language
(	O
recursive	O
)	O
:	O
function	O
hanoi	O
is	O
:	O
</s>
<s>
The	O
binary	O
search	O
algorithm	O
is	O
a	O
method	O
of	O
searching	O
a	O
sorted	B-Data_Structure
array	I-Data_Structure
for	O
a	O
single	O
element	O
by	O
cutting	O
the	O
array	O
in	O
half	O
with	O
each	O
recursive	O
pass	O
.	O
</s>
<s>
Example	O
implementation	O
of	O
binary	O
search	O
in	O
C	B-Language
:	O
</s>
<s>
An	O
important	O
application	O
of	O
recursion	O
in	O
computer	B-General_Concept
science	I-General_Concept
is	O
in	O
defining	O
dynamic	O
data	O
structures	O
such	O
as	O
lists	O
and	O
trees	B-Application
.	O
</s>
<s>
Recursive	O
data	O
structures	O
can	O
dynamically	O
grow	O
to	O
a	O
theoretically	O
infinite	O
size	O
in	O
response	O
to	O
runtime	O
requirements	O
;	O
in	O
contrast	O
,	O
the	O
size	O
of	O
a	O
static	O
array	O
must	O
be	O
set	O
at	O
compile	B-Language
time	O
.	O
</s>
<s>
The	O
examples	O
in	O
this	O
section	O
illustrate	O
what	O
is	O
known	O
as	O
"	O
structural	B-Algorithm
recursion	I-Algorithm
"	O
.	O
</s>
<s>
As	O
long	O
as	O
a	O
programmer	B-Application
derives	O
the	O
template	O
from	O
a	O
data	O
definition	O
,	O
functions	O
employ	O
structural	B-Algorithm
recursion	I-Algorithm
.	O
</s>
<s>
Below	O
is	O
a	O
C	B-Language
definition	O
of	O
a	O
linked	B-Data_Structure
list	I-Data_Structure
node	O
structure	O
.	O
</s>
<s>
In	O
the	O
C	B-Language
implementation	O
,	O
the	O
list	O
remains	O
unchanged	O
by	O
the	O
list_print	O
procedure	O
.	O
</s>
<s>
Like	O
the	O
node	O
for	O
linked	B-Data_Structure
lists	I-Data_Structure
,	O
it	O
is	O
defined	O
in	O
terms	O
of	O
itself	O
,	O
recursively	O
.	O
</s>
<s>
The	O
above	O
example	O
illustrates	O
an	O
in-order	B-Algorithm
traversal	I-Algorithm
of	O
the	O
binary	O
tree	O
.	O
</s>
<s>
A	O
Binary	B-Language
search	I-Language
tree	I-Language
is	O
a	O
special	O
case	O
of	O
the	O
binary	O
tree	O
where	O
the	O
data	O
elements	O
of	O
each	O
node	O
are	O
in	O
order	O
.	O
</s>
<s>
Since	O
the	O
number	O
of	O
files	O
in	O
a	O
filesystem	B-Application
may	O
vary	O
,	O
recursion	O
is	O
the	O
only	O
practical	O
way	O
to	O
traverse	O
and	O
thus	O
enumerate	O
its	O
contents	O
.	O
</s>
<s>
Traversing	O
a	O
filesystem	B-Application
is	O
very	O
similar	O
to	O
that	O
of	O
tree	B-Algorithm
traversal	I-Algorithm
,	O
therefore	O
the	O
concepts	O
behind	O
tree	B-Algorithm
traversal	I-Algorithm
are	O
applicable	O
to	O
traversing	O
a	O
filesystem	B-Application
.	O
</s>
<s>
More	O
specifically	O
,	O
the	O
code	O
below	O
would	O
be	O
an	O
example	O
of	O
a	O
preorder	B-Algorithm
traversal	I-Algorithm
of	O
a	O
filesystem	B-Application
.	O
</s>
<s>
This	O
code	O
is	O
both	O
recursion	O
and	O
iteration	B-Algorithm
-	O
the	O
files	O
and	O
directories	O
are	O
iterated	B-Algorithm
,	O
and	O
each	O
directory	O
is	O
opened	O
recursively	O
.	O
</s>
<s>
The	O
"	O
base	O
case	O
"	O
scenario	O
is	O
that	O
there	O
will	O
always	O
be	O
a	O
fixed	O
number	O
of	O
files	O
and/or	O
directories	O
in	O
a	O
given	O
filesystem	B-Application
.	O
</s>
