<s>
In	O
discrete	O
mathematics	O
,	O
tree	B-Data_Structure
rotation	I-Data_Structure
is	O
an	O
operation	O
on	O
a	O
binary	O
tree	O
that	O
changes	O
the	O
structure	O
without	O
interfering	O
with	O
the	O
order	O
of	O
the	O
elements	O
.	O
</s>
<s>
A	O
tree	B-Data_Structure
rotation	I-Data_Structure
moves	O
one	O
node	O
up	O
in	O
the	O
tree	O
and	O
one	O
node	O
down	O
.	O
</s>
<s>
It	O
is	O
used	O
to	O
change	O
the	O
shape	O
of	O
the	O
tree	O
,	O
and	O
in	O
particular	O
to	O
decrease	O
its	O
height	O
by	O
moving	O
smaller	O
subtrees	B-Application
down	O
and	O
larger	O
subtrees	B-Application
up	O
,	O
resulting	O
in	O
improved	O
performance	O
of	O
many	O
tree	O
operations	O
.	O
</s>
<s>
Some	O
say	O
that	O
the	O
direction	O
of	O
rotation	O
reflects	O
the	O
direction	O
that	O
a	O
node	O
is	O
moving	O
upon	O
rotation	O
(	O
a	O
left	O
child	O
rotating	O
into	O
its	O
parent	O
's	O
location	O
is	O
a	O
right	O
rotation	O
)	O
while	O
others	O
say	O
that	O
the	O
direction	O
of	O
rotation	O
reflects	O
which	O
subtree	B-Application
is	O
rotating	O
(	O
a	O
left	O
subtree	B-Application
rotating	O
into	O
its	O
parent	O
's	O
location	O
is	O
a	O
left	O
rotation	O
,	O
the	O
opposite	O
of	O
the	O
former	O
)	O
.	O
</s>
<s>
Another	O
constraint	O
is	O
the	O
main	O
property	O
of	O
a	O
binary	B-Language
search	I-Language
tree	I-Language
,	O
namely	O
that	O
the	O
right	O
child	O
is	O
greater	O
than	O
the	O
parent	O
and	O
the	O
left	O
child	O
is	O
less	O
than	O
the	O
parent	B-Application
.	I-Application
</s>
<s>
Assuming	O
this	O
is	O
a	O
binary	B-Language
search	I-Language
tree	I-Language
,	O
as	O
stated	O
above	O
,	O
the	O
elements	O
must	O
be	O
interpreted	O
as	O
variables	O
that	O
can	O
be	O
compared	O
to	O
each	O
other	O
.	O
</s>
<s>
The	O
circles	O
represent	O
individual	O
nodes	O
and	O
the	O
triangles	O
represent	O
subtrees	B-Application
.	O
</s>
<s>
Each	O
subtree	B-Application
could	O
be	O
empty	O
,	O
consist	O
of	O
a	O
single	O
node	O
,	O
or	O
consist	O
of	O
any	O
number	O
of	O
nodes	O
.	O
</s>
<s>
When	O
a	O
subtree	B-Application
is	O
rotated	O
,	O
the	O
subtree	B-Application
side	O
upon	O
which	O
it	O
is	O
rotated	O
increases	O
its	O
height	O
by	O
one	O
node	O
while	O
the	O
other	O
subtree	B-Application
decreases	O
its	O
height	O
.	O
</s>
<s>
This	O
makes	O
tree	B-Data_Structure
rotations	I-Data_Structure
useful	O
for	O
rebalancing	O
a	O
tree	O
.	O
</s>
<s>
Consider	O
the	O
terminology	O
of	O
Root	O
for	O
the	O
parent	B-Application
node	I-Application
of	O
the	O
subtrees	B-Application
to	O
rotate	O
,	O
Pivot	O
for	O
the	O
node	O
which	O
will	O
become	O
the	O
new	O
parent	B-Application
node	I-Application
,	O
RS	O
for	O
the	O
side	O
of	O
rotation	O
and	O
OS	O
for	O
the	O
opposite	O
side	O
of	O
rotation	O
.	O
</s>
<s>
The	O
tree	B-Data_Structure
rotation	I-Data_Structure
renders	O
the	O
inorder	O
traversal	O
of	O
the	O
binary	O
tree	O
invariant	B-Application
.	O
</s>
<s>
The	O
following	O
is	O
example	O
Python	B-Language
code	I-Language
that	O
performs	O
that	O
computation	O
:	O
</s>
<s>
Tree	B-Data_Structure
rotations	I-Data_Structure
are	O
used	O
in	O
a	O
number	O
of	O
tree	B-Application
data	I-Application
structures	I-Application
such	O
as	O
AVL	O
trees	O
,	O
red	O
–	O
black	O
trees	O
,	O
WAVL	B-Data_Structure
trees	I-Data_Structure
,	O
splay	B-Data_Structure
trees	I-Data_Structure
,	O
and	O
treaps	B-Application
.	O
</s>
<s>
Self-balancing	O
binary	B-Language
search	I-Language
trees	I-Language
apply	O
this	O
operation	O
automatically	O
.	O
</s>
