<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
a	O
2	B-Data_Structure
–	I-Data_Structure
3	I-Data_Structure
–	I-Data_Structure
4	I-Data_Structure
tree	I-Data_Structure
(	O
also	O
called	O
a	O
2	O
–	O
4	O
tree	B-Application
)	O
is	O
a	O
self-balancing	O
data	B-General_Concept
structure	I-General_Concept
that	O
can	O
be	O
used	O
to	O
implement	O
dictionaries	B-Application
.	O
</s>
<s>
The	O
numbers	O
mean	O
a	O
tree	B-Application
where	O
every	O
node	B-Data_Structure
with	O
children	O
(	O
internal	O
node	B-Data_Structure
)	O
has	O
either	O
two	O
,	O
three	O
,	O
or	O
four	O
child	O
nodes	O
:	O
</s>
<s>
a	O
2-node	O
has	O
one	O
data	O
element	O
,	O
and	O
if	O
internal	O
has	O
two	O
child	O
nodes	O
;	O
</s>
<s>
a	O
3-node	O
has	O
two	O
data	O
elements	O
,	O
and	O
if	O
internal	O
has	O
three	O
child	O
nodes	O
;	O
</s>
<s>
a	O
4-node	O
has	O
three	O
data	O
elements	O
,	O
and	O
if	O
internal	O
has	O
four	O
child	O
nodes	O
;	O
</s>
<s>
2	B-Data_Structure
–	I-Data_Structure
3	I-Data_Structure
–	I-Data_Structure
4	I-Data_Structure
trees	I-Data_Structure
are	O
B-trees	B-Architecture
of	O
order	O
4	O
;	O
like	O
B-trees	B-Architecture
in	O
general	O
,	O
they	O
can	O
search	O
,	O
insert	O
and	O
delete	O
in	O
O(log n )	O
time	O
.	O
</s>
<s>
One	O
property	O
of	O
a	O
2	B-Data_Structure
–	I-Data_Structure
3	I-Data_Structure
–	I-Data_Structure
4	I-Data_Structure
tree	I-Data_Structure
is	O
that	O
all	O
external	B-Application
nodes	I-Application
are	O
at	O
the	O
same	O
depth	O
.	O
</s>
<s>
2	B-Data_Structure
–	I-Data_Structure
3	I-Data_Structure
–	I-Data_Structure
4	I-Data_Structure
trees	I-Data_Structure
are	O
isomorphic	O
to	O
red	O
–	O
black	O
trees	O
,	O
meaning	O
that	O
they	O
are	O
equivalent	O
data	B-General_Concept
structures	I-General_Concept
.	O
</s>
<s>
In	O
other	O
words	O
,	O
for	O
every	O
2	B-Data_Structure
–	I-Data_Structure
3	I-Data_Structure
–	I-Data_Structure
4	I-Data_Structure
tree	I-Data_Structure
,	O
there	O
exists	O
at	O
least	O
one	O
and	O
at	O
most	O
one	O
red	O
–	O
black	O
tree	B-Application
with	O
data	O
elements	O
in	O
the	O
same	O
order	O
.	O
</s>
<s>
Moreover	O
,	O
insertion	O
and	O
deletion	O
operations	O
on	O
2	B-Data_Structure
–	I-Data_Structure
3	I-Data_Structure
–	I-Data_Structure
4	I-Data_Structure
trees	I-Data_Structure
that	O
cause	O
node	B-Data_Structure
expansions	O
,	O
splits	O
and	O
merges	O
are	O
equivalent	O
to	O
the	O
color-flipping	O
and	O
rotations	O
in	O
red	O
–	O
black	O
trees	O
.	O
</s>
<s>
Introductions	O
to	O
red	O
–	O
black	O
trees	O
usually	O
introduce	O
2	B-Data_Structure
–	I-Data_Structure
3	I-Data_Structure
–	I-Data_Structure
4	I-Data_Structure
trees	I-Data_Structure
first	O
,	O
because	O
they	O
are	O
conceptually	O
simpler	O
.	O
</s>
<s>
2	B-Data_Structure
–	I-Data_Structure
3	I-Data_Structure
–	I-Data_Structure
4	I-Data_Structure
trees	I-Data_Structure
,	O
however	O
,	O
can	O
be	O
difficult	O
to	O
implement	O
in	O
most	O
programming	O
languages	O
because	O
of	O
the	O
large	O
number	O
of	O
special	O
cases	O
involved	O
in	O
operations	O
on	O
the	O
tree	B-Application
.	O
</s>
<s>
Every	O
node	B-Data_Structure
(	O
leaf	O
or	O
internal	O
)	O
is	O
a	O
2-node	O
,	O
3-node	O
or	O
a	O
4-node	O
,	O
and	O
holds	O
one	O
,	O
two	O
,	O
or	O
three	O
data	O
elements	O
,	O
respectively	O
.	O
</s>
<s>
To	O
insert	O
a	O
value	O
,	O
we	O
start	O
at	O
the	O
root	O
of	O
the	O
2	B-Data_Structure
–	I-Data_Structure
3	I-Data_Structure
–	I-Data_Structure
4	I-Data_Structure
tree	I-Data_Structure
:	O
</s>
<s>
If	O
the	O
current	O
node	B-Data_Structure
is	O
a	O
4-node	O
:	O
</s>
<s>
Remove	O
and	O
save	O
the	O
middle	O
value	O
to	O
get	O
a	O
3-node	O
.	O
</s>
<s>
Split	O
the	O
remaining	O
3-node	O
up	O
into	O
a	O
pair	O
of	O
2-nodes	O
(	O
the	O
now	O
missing	O
middle	O
value	O
is	O
handled	O
in	O
the	O
next	O
step	O
)	O
.	O
</s>
<s>
If	O
this	O
is	O
the	O
root	B-Application
node	I-Application
(	O
which	O
thus	O
has	O
no	O
parent	O
)	O
:	O
</s>
<s>
the	O
middle	O
value	O
becomes	O
the	O
new	O
root	O
2-node	O
and	O
the	O
tree	B-Application
height	O
increases	O
by	O
1	O
.	O
</s>
<s>
Otherwise	O
,	O
push	O
the	O
middle	O
value	O
up	O
into	O
the	O
parent	B-Application
node	I-Application
.	O
</s>
<s>
Ascend	O
into	O
the	O
parent	B-Application
node	I-Application
.	O
</s>
<s>
If	O
that	O
child	O
is	O
a	O
leaf	O
,	O
insert	O
the	O
value	O
into	O
the	O
child	O
node	B-Data_Structure
and	O
finish	O
.	O
</s>
<s>
To	O
insert	O
the	O
value	O
"	O
25	O
"	O
into	O
this	O
2	B-Data_Structure
–	I-Data_Structure
3	I-Data_Structure
–	I-Data_Structure
4	I-Data_Structure
tree	I-Data_Structure
:	O
</s>
<s>
Node	B-Data_Structure
(	O
22	O
,	O
24	O
,	O
29	O
)	O
is	O
a	O
4-node	O
,	O
so	O
its	O
middle	O
element	O
24	O
is	O
pushed	O
up	O
into	O
the	O
parent	B-Application
node	I-Application
.	O
</s>
<s>
The	O
remaining	O
3-node	O
(	O
22	O
,	O
29	O
)	O
is	O
split	O
into	O
a	O
pair	O
of	O
2-nodes	O
(	O
22	O
)	O
and	O
(	O
29	O
)	O
.	O
</s>
<s>
Node	B-Data_Structure
(	O
29	O
)	O
has	O
no	O
leftmost	O
child	O
.	O
</s>
<s>
Stop	O
here	O
and	O
insert	O
value	O
25	O
into	O
this	O
node	B-Data_Structure
.	O
</s>
<s>
However	O
,	O
the	O
drawback	O
of	O
this	O
method	O
is	O
that	O
the	O
size	O
of	O
the	O
tree	B-Application
does	O
not	O
decrease	O
.	O
</s>
<s>
If	O
a	O
large	O
proportion	O
of	O
the	O
elements	O
of	O
the	O
tree	B-Application
are	O
deleted	O
,	O
then	O
the	O
tree	B-Application
will	O
become	O
much	O
larger	O
than	O
the	O
current	O
size	O
of	O
the	O
stored	O
elements	O
,	O
and	O
the	O
performance	O
of	O
other	O
operations	O
will	O
be	O
adversely	O
affected	O
by	O
the	O
deleted	O
elements	O
.	O
</s>
<s>
When	O
this	O
is	O
undesirable	O
,	O
the	O
following	O
algorithm	O
can	O
be	O
followed	O
to	O
remove	O
a	O
value	O
from	O
the	O
2	B-Data_Structure
–	I-Data_Structure
3	I-Data_Structure
–	I-Data_Structure
4	I-Data_Structure
tree	I-Data_Structure
:	O
</s>
<s>
If	O
the	O
element	O
is	O
not	O
in	O
a	O
leaf	O
node	B-Data_Structure
,	O
remember	O
its	O
location	O
and	O
continue	O
searching	O
until	O
a	O
leaf	O
,	O
which	O
will	O
contain	O
the	O
element	O
's	O
successor	O
,	O
is	O
reached	O
.	O
</s>
<s>
It	O
is	O
simplest	O
to	O
make	O
adjustments	O
to	O
the	O
tree	B-Application
from	O
the	O
top	O
down	O
such	O
that	O
the	O
leaf	O
node	B-Data_Structure
found	O
is	O
not	O
a	O
2-node	O
.	O
</s>
<s>
That	O
way	O
,	O
after	O
the	O
swap	O
,	O
there	O
will	O
not	O
be	O
an	O
empty	O
leaf	O
node	B-Data_Structure
.	O
</s>
<s>
If	O
the	O
element	O
is	O
in	O
a	O
2-node	O
leaf	O
,	O
just	O
make	O
the	O
adjustments	O
below	O
.	O
</s>
<s>
Make	O
the	O
following	O
adjustments	O
when	O
a	O
2-node	O
–	O
except	O
the	O
root	B-Application
node	I-Application
–	O
is	O
encountered	O
on	O
the	O
way	O
to	O
the	O
leaf	O
we	O
want	O
to	O
remove	O
:	O
</s>
<s>
If	O
a	O
sibling	O
on	O
either	O
side	O
of	O
this	O
node	B-Data_Structure
is	O
a	O
3-node	O
or	O
a	O
4-node	O
(	O
thus	O
having	O
more	O
than	O
1	O
key	O
)	O
,	O
perform	O
a	O
rotation	O
with	O
that	O
sibling	O
:	O
</s>
<s>
The	O
key	O
from	O
the	O
other	O
sibling	O
closest	O
to	O
this	O
node	B-Data_Structure
moves	O
up	O
to	O
the	O
parent	O
key	O
that	O
overlooks	O
the	O
two	O
nodes	O
.	O
</s>
<s>
The	O
parent	O
key	O
moves	O
down	O
to	O
this	O
node	B-Data_Structure
to	O
form	O
a	O
3-node	O
.	O
</s>
<s>
The	O
child	O
that	O
was	O
originally	O
with	O
the	O
rotated	O
sibling	O
key	O
is	O
now	O
this	O
node	B-Data_Structure
's	O
additional	O
child	O
.	O
</s>
<s>
If	O
the	O
parent	O
is	O
a	O
2-node	O
and	O
the	O
sibling	O
is	O
also	O
a	O
2-node	O
,	O
combine	O
all	O
three	O
elements	O
to	O
form	O
a	O
new	O
4-node	O
and	O
shorten	O
the	O
tree	B-Application
.	O
</s>
<s>
(	O
This	O
rule	O
can	O
only	O
trigger	O
if	O
the	O
parent	O
2-node	O
is	O
the	O
root	O
,	O
since	O
all	O
other	O
2-nodes	O
along	O
the	O
way	O
will	O
have	O
been	O
modified	O
to	O
not	O
be	O
2-nodes	O
.	O
</s>
<s>
This	O
is	O
why	O
"	O
shorten	O
the	O
tree	B-Application
"	O
here	O
preserves	O
balance	O
;	O
this	O
is	O
also	O
an	O
important	O
assumption	O
for	O
the	O
fusion	O
operation	O
.	O
)	O
</s>
<s>
If	O
the	O
parent	O
is	O
a	O
3-node	O
or	O
a	O
4-node	O
and	O
all	O
adjacent	O
siblings	O
are	O
2-nodes	O
,	O
do	O
a	O
fusion	O
operation	O
with	O
the	O
parent	O
and	O
an	O
adjacent	O
sibling	O
:	O
</s>
<s>
The	O
adjacent	O
sibling	O
and	O
the	O
parent	O
key	O
overlooking	O
the	O
two	O
sibling	O
nodes	O
come	O
together	O
to	O
form	O
a	O
4-node	O
.	O
</s>
<s>
Transfer	O
the	O
sibling	O
's	O
children	O
to	O
this	O
node	B-Data_Structure
.	O
</s>
<s>
Once	O
the	O
sought	O
value	O
is	O
reached	O
,	O
it	O
can	O
now	O
be	O
placed	O
at	O
the	O
removed	O
entry	O
's	O
location	O
without	O
a	O
problem	O
because	O
we	O
have	O
ensured	O
that	O
the	O
leaf	O
node	B-Data_Structure
has	O
more	O
than	O
1	O
key	O
.	O
</s>
<s>
Deletion	O
in	O
a	O
2	B-Data_Structure
–	I-Data_Structure
3	I-Data_Structure
–	I-Data_Structure
4	I-Data_Structure
tree	I-Data_Structure
is	O
O(log n )	O
,	O
assuming	O
transfer	O
and	O
fusion	O
run	O
in	O
constant	O
time	O
(O(1 )	O
)	O
.	O
</s>
<s>
Since	O
2	B-Data_Structure
–	I-Data_Structure
3	I-Data_Structure
–	I-Data_Structure
4	I-Data_Structure
trees	I-Data_Structure
are	O
similar	O
in	O
structure	O
to	O
red	O
–	O
black	O
trees	O
,	O
parallel	O
algorithms	O
for	O
red	O
–	O
black	O
trees	O
can	O
be	O
applied	O
to	O
2	B-Data_Structure
–	I-Data_Structure
3	I-Data_Structure
–	I-Data_Structure
4	I-Data_Structure
trees	I-Data_Structure
as	O
well	O
.	O
</s>
