<s>
The	O
-ary	O
heap	O
or	O
-heap	O
is	O
a	O
priority	B-Application
queue	I-Application
data	B-General_Concept
structure	I-General_Concept
,	O
a	O
generalization	O
of	O
the	O
binary	B-Application
heap	I-Application
in	O
which	O
the	O
nodes	O
have	O
children	O
instead	O
of	O
2	O
.	O
</s>
<s>
Thus	O
,	O
a	O
binary	B-Application
heap	I-Application
is	O
a	O
2-heap	O
,	O
and	O
a	O
ternary	B-Application
heap	I-Application
is	O
a	O
3-heap	O
.	O
</s>
<s>
This	O
data	B-General_Concept
structure	I-General_Concept
allows	O
decrease	O
priority	O
operations	O
to	O
be	O
performed	O
more	O
quickly	O
than	O
binary	B-Application
heaps	I-Application
,	O
at	O
the	O
expense	O
of	O
slower	O
delete	O
minimum	O
operations	O
.	O
</s>
<s>
This	O
tradeoff	O
leads	O
to	O
better	O
running	O
times	O
for	O
algorithms	O
such	O
as	O
Dijkstra	B-Algorithm
's	I-Algorithm
algorithm	I-Algorithm
in	O
which	O
decrease	O
priority	O
operations	O
are	O
more	O
common	O
than	O
delete	O
min	O
operations	O
.	O
</s>
<s>
Additionally	O
,	O
-ary	O
heaps	O
have	O
better	O
memory	B-General_Concept
cache	I-General_Concept
behavior	O
than	O
binary	B-Application
heaps	I-Application
,	O
allowing	O
them	O
to	O
run	O
more	O
quickly	O
in	O
practice	O
despite	O
having	O
a	O
theoretically	O
larger	O
worst-case	O
running	O
time	O
.	O
</s>
<s>
Like	O
binary	B-Application
heaps	I-Application
,	O
-ary	O
heaps	O
are	O
an	O
in-place	B-Algorithm
data	I-Algorithm
structure	I-Algorithm
that	O
uses	O
no	O
additional	O
storage	O
beyond	O
that	O
needed	O
to	O
store	O
the	O
array	B-Data_Structure
of	O
items	O
in	O
the	O
heap	O
.	O
</s>
<s>
The	O
-ary	O
heap	O
consists	O
of	O
an	O
array	B-Data_Structure
of	O
items	O
,	O
each	O
of	O
which	O
has	O
a	O
priority	O
associated	O
with	O
it	O
.	O
</s>
<s>
These	O
items	O
may	O
be	O
viewed	O
as	O
the	O
nodes	O
in	O
a	O
complete	O
-ary	O
tree	O
,	O
listed	O
in	O
breadth	B-Algorithm
first	I-Algorithm
traversal	I-Algorithm
order	I-Algorithm
:	O
the	O
item	O
at	O
position	O
0	O
of	O
the	O
array	B-Data_Structure
(	O
using	O
zero-based	O
numbering	O
)	O
forms	O
the	O
root	O
of	O
the	O
tree	O
,	O
the	O
items	O
at	O
positions	O
1	O
through	O
are	O
its	O
children	O
,	O
the	O
next	O
items	O
are	O
its	O
grandchildren	O
,	O
etc	O
.	O
</s>
<s>
According	O
to	O
the	O
heap	B-Application
property	I-Application
,	O
in	O
a	O
min-heap	O
,	O
each	O
item	O
has	O
a	O
priority	O
that	O
is	O
at	O
least	O
as	O
large	O
as	O
its	O
parent	O
;	O
in	O
a	O
max-heap	O
,	O
each	O
item	O
has	O
a	O
priority	O
that	O
is	O
no	O
larger	O
than	O
its	O
parent	O
.	O
</s>
<s>
The	O
minimum	O
priority	O
item	O
in	O
a	O
min-heap	O
(	O
or	O
the	O
maximum	O
priority	O
item	O
in	O
a	O
max-heap	O
)	O
may	O
always	O
be	O
found	O
at	O
position	O
0	O
of	O
the	O
array	B-Data_Structure
.	O
</s>
<s>
To	O
remove	O
this	O
item	O
from	O
the	O
priority	B-Application
queue	I-Application
,	O
the	O
last	O
item	O
x	O
in	O
the	O
array	B-Data_Structure
is	O
moved	O
into	O
its	O
place	O
,	O
and	O
the	O
length	O
of	O
the	O
array	B-Data_Structure
is	O
decreased	O
by	O
one	O
.	O
</s>
<s>
Then	O
,	O
while	O
item	O
x	O
and	O
its	O
children	O
do	O
not	O
satisfy	O
the	O
heap	B-Application
property	I-Application
,	O
item	O
x	O
is	O
swapped	O
with	O
one	O
of	O
its	O
children	O
(	O
the	O
one	O
with	O
the	O
smallest	O
priority	O
in	O
a	O
min-heap	O
,	O
or	O
the	O
one	O
with	O
the	O
largest	O
priority	O
in	O
a	O
max-heap	O
)	O
,	O
moving	O
it	O
downward	O
in	O
the	O
tree	O
and	O
later	O
in	O
the	O
array	B-Data_Structure
,	O
until	O
eventually	O
the	O
heap	B-Application
property	I-Application
is	O
satisfied	O
.	O
</s>
<s>
To	O
insert	O
a	O
new	O
item	O
into	O
the	O
heap	O
,	O
the	O
item	O
is	O
appended	O
to	O
the	O
end	O
of	O
the	O
array	B-Data_Structure
,	O
and	O
then	O
while	O
the	O
heap	B-Application
property	I-Application
is	O
violated	O
it	O
is	O
swapped	O
with	O
its	O
parent	O
,	O
moving	O
it	O
upward	O
in	O
the	O
tree	O
and	O
earlier	O
in	O
the	O
array	B-Data_Structure
,	O
until	O
eventually	O
the	O
heap	B-Application
property	I-Application
is	O
satisfied	O
.	O
</s>
<s>
To	O
create	O
a	O
new	O
heap	O
from	O
an	O
array	B-Data_Structure
of	O
items	O
,	O
one	O
may	O
loop	O
over	O
the	O
items	O
in	O
reverse	O
order	O
,	O
starting	O
from	O
the	O
item	O
at	O
position	O
and	O
ending	O
at	O
the	O
item	O
at	O
position	O
0	O
,	O
applying	O
the	O
downward-swapping	O
procedure	O
for	O
each	O
item	O
.	O
</s>
<s>
The	O
exact	O
value	O
of	O
the	O
above	O
(	O
the	O
worst-case	O
number	O
of	O
comparisons	O
during	O
the	O
construction	O
of	O
d-ary	B-Application
heap	I-Application
)	O
is	O
known	O
to	O
be	O
equal	O
to	O
:	O
</s>
<s>
The	O
space	O
usage	O
of	O
the	O
heap	O
,	O
with	O
insert	O
and	O
delete-min	O
operations	O
,	O
is	O
linear	O
,	O
as	O
it	O
uses	O
no	O
extra	O
storage	O
other	O
than	O
an	O
array	B-Data_Structure
containing	O
a	O
list	O
of	O
the	O
items	O
in	O
the	O
heap	O
.	O
</s>
<s>
When	O
operating	O
on	O
a	O
graph	O
with	O
edges	O
and	O
vertices	O
,	O
both	O
Dijkstra	B-Algorithm
's	I-Algorithm
algorithm	I-Algorithm
for	O
shortest	O
paths	O
and	O
Prim	B-Algorithm
's	I-Algorithm
algorithm	I-Algorithm
for	O
minimum	O
spanning	O
trees	O
use	O
a	O
min-heap	O
in	O
which	O
there	O
are	O
delete-min	O
operations	O
and	O
as	O
many	O
as	O
decrease-priority	O
operations	O
.	O
</s>
<s>
By	O
using	O
a	O
-ary	O
heap	O
with	O
,	O
the	O
total	O
times	O
for	O
these	O
two	O
types	O
of	O
operations	O
may	O
be	O
balanced	O
against	O
each	O
other	O
,	O
leading	O
to	O
a	O
total	O
time	O
of	O
for	O
the	O
algorithm	O
,	O
an	O
improvement	O
over	O
the	O
running	O
time	O
of	O
binary	B-Application
heap	I-Application
versions	O
of	O
these	O
algorithms	O
whenever	O
the	O
number	O
of	O
edges	O
is	O
significantly	O
larger	O
than	O
the	O
number	O
of	O
vertices	O
.	O
</s>
<s>
An	O
alternative	O
priority	B-Application
queue	I-Application
data	B-General_Concept
structure	I-General_Concept
,	O
the	O
Fibonacci	B-Application
heap	I-Application
,	O
gives	O
an	O
even	O
better	O
theoretical	O
running	O
time	O
of	O
,	O
but	O
in	O
practice	O
-ary	O
heaps	O
are	O
generally	O
at	O
least	O
as	O
fast	O
,	O
and	O
often	O
faster	O
,	O
than	O
Fibonacci	B-Application
heaps	I-Application
for	O
this	O
application	O
.	O
</s>
<s>
4-heaps	O
may	O
perform	O
better	O
than	O
binary	B-Application
heaps	I-Application
in	O
practice	O
,	O
even	O
for	O
delete-min	O
operations	O
.	O
</s>
<s>
a	O
-ary	O
heap	O
typically	O
runs	O
much	O
faster	O
than	O
a	O
binary	B-Application
heap	I-Application
for	O
heap	O
sizes	O
that	O
exceed	O
the	O
size	O
of	O
the	O
computer	O
's	O
cache	B-General_Concept
memory	I-General_Concept
:	O
</s>
<s>
A	O
binary	B-Application
heap	I-Application
typically	O
requires	O
more	O
cache	B-General_Concept
misses	O
and	O
virtual	B-Architecture
memory	I-Architecture
page	B-General_Concept
faults	I-General_Concept
than	O
a	O
-ary	O
heap	O
,	O
each	O
one	O
taking	O
far	O
more	O
time	O
than	O
the	O
extra	O
work	O
incurred	O
by	O
the	O
additional	O
comparisons	O
a	O
-ary	O
heap	O
makes	O
compared	O
to	O
a	O
binary	B-Application
heap	I-Application
.	O
</s>
