<s>
In	O
computing	O
,	O
a	O
threaded	B-Data_Structure
binary	I-Data_Structure
tree	I-Data_Structure
is	O
a	O
binary	O
tree	O
variant	O
that	O
facilitates	O
traversal	B-Algorithm
in	O
a	O
particular	O
order	O
(	O
often	O
the	O
same	O
order	O
already	O
defined	O
for	O
the	O
tree	O
)	O
.	O
</s>
<s>
An	O
entire	O
binary	B-Language
search	I-Language
tree	I-Language
can	O
be	O
easily	O
traversed	O
in	O
order	O
of	O
the	O
main	O
key	B-Application
,	O
but	O
given	O
only	O
a	O
pointer	O
to	O
a	O
node	B-Data_Structure
,	O
finding	O
the	O
node	B-Data_Structure
which	O
comes	O
next	O
may	O
be	O
slow	O
or	O
impossible	O
.	O
</s>
<s>
For	O
example	O
,	O
leaf	O
nodes	O
by	O
definition	O
have	O
no	O
descendants	O
,	O
so	O
given	O
only	O
a	O
pointer	O
to	O
a	O
leaf	O
node	B-Data_Structure
no	O
other	O
node	B-Data_Structure
can	O
be	O
reached	O
.	O
</s>
<s>
A	O
threaded	O
tree	O
adds	O
extra	O
information	O
in	O
some	O
or	O
all	O
nodes	O
,	O
so	O
that	O
for	O
any	O
given	O
single	O
node	B-Data_Structure
the	O
"	O
next	O
"	O
node	B-Data_Structure
can	O
be	O
found	O
quickly	O
,	O
allowing	O
tree	B-Algorithm
traversal	I-Algorithm
without	O
recursion	O
and	O
the	O
extra	O
storage	O
(	O
proportional	O
to	O
the	O
tree	O
's	O
depth	O
)	O
that	O
recursion	O
requires	O
.	O
</s>
<s>
"	O
A	O
binary	O
tree	O
is	O
threaded	O
by	O
making	O
all	O
right	O
child	O
pointers	O
that	O
would	O
normally	O
be	O
null	O
point	O
to	O
the	O
in-order	B-Algorithm
successor	O
of	O
the	O
node	B-Data_Structure
(	O
if	O
it	O
exists	O
)	O
,	O
and	O
all	O
left	O
child	O
pointers	O
that	O
would	O
normally	O
be	O
null	O
point	O
to	O
the	O
in-order	B-Algorithm
predecessor	O
of	O
the	O
node	B-Data_Structure
.	O
</s>
<s>
This	O
assumes	O
the	O
traversal	B-Algorithm
order	O
is	O
the	O
same	O
as	O
in-order	B-Algorithm
traversal	B-Algorithm
of	O
the	O
tree	O
.	O
</s>
<s>
Linked	B-Data_Structure
lists	I-Data_Structure
thus	O
defined	O
are	O
also	O
commonly	O
called	O
"	O
threads	O
"	O
,	O
and	O
can	O
be	O
used	O
to	O
enable	O
traversal	B-Algorithm
in	O
any	O
order(s )	O
desired	O
.	O
</s>
<s>
For	O
example	O
,	O
a	O
tree	O
whose	O
nodes	O
represent	O
information	O
about	O
people	O
might	O
be	O
sorted	O
by	O
name	O
,	O
but	O
have	O
extra	O
threads	O
allowing	O
quick	O
traversal	B-Algorithm
in	O
order	O
of	O
birth	O
date	O
,	O
weight	O
,	O
or	O
any	O
other	O
known	O
characteristic	O
.	O
</s>
<s>
Trees	O
,	O
including	O
(	O
but	O
not	O
limited	O
to	O
)	O
binary	B-Language
search	I-Language
trees	I-Language
,	O
can	O
be	O
used	O
to	O
store	O
items	O
in	O
a	O
particular	O
order	O
,	O
such	O
as	O
the	O
value	O
of	O
some	O
property	O
stored	O
in	O
each	O
node	B-Data_Structure
,	O
often	O
called	O
a	O
key	B-Application
.	O
</s>
<s>
One	O
useful	O
operation	O
on	O
such	O
a	O
tree	O
is	O
traversal	B-Algorithm
:	O
visiting	O
all	O
the	O
items	O
in	O
order	O
of	O
the	O
key	B-Application
.	O
</s>
<s>
A	O
simple	O
recursive	O
traversal	B-Algorithm
algorithm	O
that	O
visits	O
each	O
node	B-Data_Structure
of	O
a	O
binary	B-Language
search	I-Language
tree	I-Language
is	O
the	O
following	O
.	O
</s>
<s>
Assume	O
is	O
a	O
pointer	O
to	O
a	O
node	B-Data_Structure
,	O
or	O
.	O
</s>
<s>
"	O
Visiting	O
"	O
can	O
mean	O
performing	O
any	O
action	O
on	O
the	O
node	B-Data_Structure
or	O
its	O
contents	O
.	O
</s>
<s>
It	O
is	O
common	O
to	O
have	O
a	O
pointer	O
to	O
a	O
particular	O
node	B-Data_Structure
,	O
but	O
that	O
is	O
not	O
sufficient	O
to	O
get	O
back	O
to	O
the	O
rest	O
of	O
the	O
tree	O
unless	O
extra	O
information	O
is	O
added	O
,	O
such	O
as	O
thread	O
pointers	O
.	O
</s>
<s>
In	O
this	O
approach	O
,	O
it	O
may	O
not	O
be	O
possible	O
to	O
tell	O
whether	O
the	O
left	O
and/or	O
right	O
pointers	O
in	O
a	O
given	O
node	B-Data_Structure
actually	O
point	O
to	O
children	O
,	O
or	O
are	O
a	O
consequence	O
of	O
threading	O
.	O
</s>
<s>
If	O
the	O
distinction	O
is	O
necessary	O
,	O
adding	O
a	O
single	O
bit	O
to	O
each	O
node	B-Data_Structure
is	O
enough	O
to	O
record	O
it	O
.	O
</s>
<s>
In	O
a	O
1968	O
textbook	O
,	O
Donald	O
Knuth	O
asked	O
whether	O
a	O
non-recursive	O
algorithm	O
for	O
in-order	B-Algorithm
traversal	B-Algorithm
exists	O
,	O
that	O
uses	O
no	O
stack	O
and	O
leaves	O
the	O
tree	O
unmodified	O
.	O
</s>
<s>
Another	O
way	O
to	O
achieve	O
similar	O
goals	O
is	O
to	O
include	O
a	O
pointer	O
in	O
every	O
node	B-Data_Structure
,	O
to	O
that	O
node	B-Data_Structure
's	O
parent	O
node	B-Data_Structure
.	O
</s>
<s>
Given	O
that	O
,	O
the	O
"	O
next	O
"	O
node	B-Data_Structure
can	O
always	O
be	O
reached	O
.	O
</s>
<s>
To	O
find	O
the	O
"	O
next	O
"	O
node	B-Data_Structure
from	O
a	O
node	B-Data_Structure
whose	O
right	O
pointer	O
is	O
null	O
,	O
walk	O
up	O
through	O
"	O
parent	O
"	O
pointers	O
until	O
reaching	O
a	O
node	B-Data_Structure
whose	O
right	O
pointer	O
is	O
not	O
null	O
,	O
and	O
is	O
not	O
the	O
child	O
you	O
just	O
came	O
up	O
from	O
.	O
</s>
<s>
That	O
node	B-Data_Structure
is	O
the	O
"	O
next	O
"	O
node	B-Data_Structure
,	O
and	O
after	O
it	O
come	O
its	O
descendants	O
on	O
the	O
right	O
.	O
</s>
<s>
It	O
is	O
also	O
possible	O
to	O
discover	O
the	O
parent	O
of	O
a	O
node	B-Data_Structure
from	O
a	O
threaded	B-Data_Structure
binary	I-Data_Structure
tree	I-Data_Structure
,	O
without	O
explicit	O
use	O
of	O
parent	O
pointers	O
or	O
a	O
stack	O
,	O
although	O
it	O
is	O
slower	O
.	O
</s>
<s>
To	O
see	O
this	O
,	O
consider	O
a	O
node	B-Data_Structure
k	O
with	O
right	O
child	O
r	O
.	O
Then	O
the	O
left	O
pointer	O
of	O
r	O
must	O
be	O
either	O
a	O
child	O
or	O
a	O
thread	O
back	O
to	O
k	O
.	O
In	O
the	O
case	O
that	O
r	O
has	O
a	O
left	O
child	O
,	O
that	O
left	O
child	O
must	O
in	O
turn	O
have	O
either	O
a	O
left	O
child	O
of	O
its	O
own	O
or	O
a	O
thread	O
back	O
to	O
k	O
,	O
and	O
so	O
on	O
for	O
all	O
successive	O
left	O
children	O
.	O
</s>
<s>
In	O
Python:def	O
parent(node )	O
:	O
</s>
<s>
if	O
node	B-Data_Structure
is	O
node.tree.root	O
:	O
</s>
<s>
if	O
p	O
is	O
None	O
or	O
p.left	O
is	O
not	O
node	B-Data_Structure
:	O
</s>
<s>
if	O
p	O
is	O
None	O
or	O
p.right	O
is	O
not	O
node	B-Data_Structure
:	O
</s>
<s>
Single	O
threaded	O
:	O
each	O
node	B-Data_Structure
is	O
threaded	O
towards	O
either	O
the	O
in-order	B-Algorithm
predecessor	O
or	O
successor	O
(	O
left	O
or	O
right	O
)	O
.	O
</s>
<s>
Double	O
threaded	O
:	O
each	O
node	B-Data_Structure
is	O
threaded	O
towards	O
both	O
the	O
in-order	B-Algorithm
predecessor	O
and	O
successor	O
(	O
left	O
and	O
right	O
)	O
.	O
</s>
<s>
Threads	O
are	O
reference	O
to	O
the	O
predecessors	O
and	O
successors	O
of	O
the	O
node	B-Data_Structure
according	O
to	O
an	O
inorder	B-Algorithm
traversal	B-Algorithm
.	O
</s>
<s>
In-order	B-Algorithm
traversal	B-Algorithm
of	O
the	O
threaded	O
tree	O
is	O
A	O
,	O
B	O
,	O
C	O
,	O
D	O
,	O
E	O
,	O
F	O
,	O
G	O
,	O
H	O
,	O
I	O
,	O
the	O
predecessor	O
of	O
E	O
is	O
D	O
,	O
the	O
successor	O
of	O
E	O
is	O
F	O
.	O
</s>
<s>
Let	O
's	O
make	O
the	O
Threaded	B-Data_Structure
Binary	I-Data_Structure
tree	I-Data_Structure
out	O
of	O
a	O
normal	O
binary	O
tree	O
:	O
</s>
<s>
In	O
an	O
m-way	O
threaded	B-Data_Structure
binary	I-Data_Structure
tree	I-Data_Structure
with	O
n	O
nodes	O
,	O
there	O
are	O
n×m	O
−	O
(	O
n−1	O
)	O
'	O
void	O
links	O
.	O
</s>
