<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
a	O
heap	B-Application
is	O
a	O
specialized	O
tree-based	O
data	B-General_Concept
structure	I-General_Concept
which	O
is	O
essentially	O
an	O
almost	O
complete	O
binary	O
tree	B-Application
that	O
satisfies	O
the	O
heap	B-Application
property	I-Application
:	O
in	O
a	O
max	B-Application
heap	I-Application
,	O
for	O
any	O
given	O
node	B-Data_Structure
C	B-Language
,	O
if	O
P	O
is	O
a	O
parent	B-Application
node	I-Application
of	O
C	B-Language
,	O
then	O
the	O
key	O
(	O
the	O
value	O
)	O
of	O
P	O
is	O
greater	O
than	O
or	O
equal	O
to	O
the	O
key	O
of	O
C	B-Language
.	O
In	O
a	O
min	B-Application
heap	I-Application
,	O
the	O
key	O
of	O
P	O
is	O
less	O
than	O
or	O
equal	O
to	O
the	O
key	O
of	O
C	B-Language
.	O
The	O
node	B-Data_Structure
at	O
the	O
"	O
top	O
"	O
of	O
the	O
heap	B-Application
(	O
with	O
no	O
parents	O
)	O
is	O
called	O
the	O
root	B-Application
node	I-Application
.	O
</s>
<s>
The	O
heap	B-Application
is	O
one	O
maximally	O
efficient	O
implementation	O
of	O
an	O
abstract	O
data	O
type	O
called	O
a	O
priority	B-Application
queue	I-Application
,	O
and	O
in	O
fact	O
,	O
priority	B-Application
queues	I-Application
are	O
often	O
referred	O
to	O
as	O
"	O
heaps	B-Application
"	O
,	O
regardless	O
of	O
how	O
they	O
may	O
be	O
implemented	O
.	O
</s>
<s>
In	O
a	O
heap	B-Application
,	O
the	O
highest	O
(	O
or	O
lowest	O
)	O
priority	O
element	O
is	O
always	O
stored	O
at	O
the	O
root	O
.	O
</s>
<s>
However	O
,	O
a	O
heap	B-Application
is	O
not	O
a	O
sorted	O
structure	O
;	O
it	O
can	O
be	O
regarded	O
as	O
being	O
partially	O
ordered	O
.	O
</s>
<s>
A	O
heap	B-Application
is	O
a	O
useful	O
data	B-General_Concept
structure	I-General_Concept
when	O
it	O
is	O
necessary	O
to	O
repeatedly	O
remove	O
the	O
object	O
with	O
the	O
highest	O
(	O
or	O
lowest	O
)	O
priority	O
,	O
or	O
when	O
insertions	O
need	O
to	O
be	O
interspersed	O
with	O
removals	O
of	O
the	O
root	B-Application
node	I-Application
.	O
</s>
<s>
A	O
common	O
implementation	O
of	O
a	O
heap	B-Application
is	O
the	O
binary	B-Application
heap	I-Application
,	O
in	O
which	O
the	O
tree	B-Application
is	O
a	O
binary	O
tree	B-Application
(	O
see	O
figure	O
)	O
.	O
</s>
<s>
The	O
heap	B-Application
data	I-Application
structure	I-Application
,	O
specifically	O
the	O
binary	B-Application
heap	I-Application
,	O
was	O
introduced	O
by	O
J	O
.	O
W	O
.	O
J	O
.	O
Williams	O
in	O
1964	O
,	O
as	O
a	O
data	B-General_Concept
structure	I-General_Concept
for	O
the	O
heapsort	B-Application
sorting	B-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
Heaps	B-Application
are	O
also	O
crucial	O
in	O
several	O
efficient	O
graph	O
algorithms	O
such	O
as	O
Dijkstra	B-Algorithm
's	I-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
When	O
a	O
heap	B-Application
is	O
a	O
complete	O
binary	O
tree	B-Application
,	O
it	O
has	O
a	O
smallest	O
possible	O
height	O
—	O
a	O
heap	B-Application
with	O
N	O
nodes	O
and	O
a	O
branches	O
for	O
each	O
node	B-Data_Structure
always	O
has	O
loga	O
N	O
height	O
.	O
</s>
<s>
Note	O
that	O
,	O
as	O
shown	O
in	O
the	O
graphic	O
,	O
there	O
is	O
no	O
implied	O
ordering	O
between	O
siblings	O
or	O
cousins	O
and	O
no	O
implied	O
sequence	O
for	O
an	O
in-order	O
traversal	O
(	O
as	O
there	O
would	O
be	O
in	O
,	O
e.g.	O
,	O
a	O
binary	B-Language
search	I-Language
tree	I-Language
)	O
.	O
</s>
<s>
The	O
heap	B-Application
relation	O
mentioned	O
above	O
applies	O
only	O
between	O
nodes	O
and	O
their	O
parents	O
,	O
grandparents	O
,	O
etc	O
.	O
</s>
<s>
The	O
maximum	O
number	O
of	O
children	O
each	O
node	B-Data_Structure
can	O
have	O
depends	O
on	O
the	O
type	O
of	O
heap	B-Application
.	O
</s>
<s>
The	O
common	O
operations	O
involving	O
heaps	B-Application
are	O
:	O
</s>
<s>
find-max	O
(	O
or	O
find-min	O
)	O
:	O
find	O
a	O
maximum	O
item	O
of	O
a	O
max-heap	B-Application
,	O
or	O
a	O
minimum	O
item	O
of	O
a	O
min-heap	B-Application
,	O
respectively	O
(	O
a.k.a.	O
</s>
<s>
More	O
efficient	O
than	O
pop	O
followed	O
by	O
push	O
,	O
since	O
only	O
need	O
to	O
balance	O
once	O
,	O
not	O
twice	O
,	O
and	O
appropriate	O
for	O
fixed-size	O
heaps	B-Application
.	O
</s>
<s>
merge	O
(	O
union	O
)	O
:	O
joining	O
two	O
heaps	B-Application
to	O
form	O
a	O
valid	O
new	O
heap	B-Application
containing	O
all	O
the	O
elements	O
of	O
both	O
,	O
preserving	O
the	O
original	O
heaps	B-Application
.	O
</s>
<s>
meld	O
:	O
joining	O
two	O
heaps	B-Application
to	O
form	O
a	O
valid	O
new	O
heap	B-Application
containing	O
all	O
the	O
elements	O
of	O
both	O
,	O
destroying	O
the	O
original	O
heaps	B-Application
.	O
</s>
<s>
size	O
:	O
return	O
the	O
number	O
of	O
items	O
in	O
the	O
heap	B-Application
.	O
</s>
<s>
is-empty	O
:	O
return	O
true	O
if	O
the	O
heap	B-Application
is	O
empty	O
,	O
false	O
otherwise	O
.	O
</s>
<s>
sift-up	O
:	O
move	O
a	O
node	B-Data_Structure
up	O
in	O
the	O
tree	B-Application
,	O
as	O
long	O
as	O
needed	O
;	O
used	O
to	O
restore	O
heap	B-Application
condition	O
after	O
insertion	O
.	O
</s>
<s>
Called	O
"	O
sift	O
"	O
because	O
node	B-Data_Structure
moves	O
up	O
the	O
tree	B-Application
until	O
it	O
reaches	O
the	O
correct	O
level	O
,	O
as	O
in	O
a	O
sieve	O
.	O
</s>
<s>
sift-down	O
:	O
move	O
a	O
node	B-Data_Structure
down	O
in	O
the	O
tree	B-Application
,	O
similar	O
to	O
sift-up	O
;	O
used	O
to	O
restore	O
heap	B-Application
condition	O
after	O
deletion	O
or	O
replacement	O
.	O
</s>
<s>
Heaps	B-Application
are	O
usually	O
implemented	O
with	O
an	O
array	B-Data_Structure
,	O
as	O
follows	O
:	O
</s>
<s>
The	O
parent	O
/	O
child	O
relationship	O
is	O
defined	B-Data_Structure
implicitly	I-Data_Structure
by	O
the	O
elements	O
 '	O
indices	O
in	O
the	O
array	B-Data_Structure
.	O
</s>
<s>
For	O
a	O
binary	B-Application
heap	I-Application
,	O
in	O
the	O
array	B-Data_Structure
,	O
the	O
first	O
index	O
contains	O
the	O
root	O
element	O
.	O
</s>
<s>
The	O
next	O
two	O
indices	O
of	O
the	O
array	B-Data_Structure
contain	O
the	O
root	O
's	O
children	O
.	O
</s>
<s>
Therefore	O
,	O
given	O
a	O
node	B-Data_Structure
at	O
index	O
,	O
its	O
children	O
are	O
at	O
indices	O
and	O
,	O
and	O
its	O
parent	O
is	O
at	O
index	O
.	O
</s>
<s>
This	O
simple	O
indexing	O
scheme	O
makes	O
it	O
efficient	O
to	O
move	O
"	O
up	O
"	O
or	O
"	O
down	O
"	O
the	O
tree	B-Application
.	O
</s>
<s>
Balancing	O
a	O
heap	B-Application
is	O
done	O
by	O
sift-up	O
or	O
sift-down	O
operations	O
(	O
swapping	O
elements	O
which	O
are	O
out	O
of	O
order	O
)	O
.	O
</s>
<s>
As	O
we	O
can	O
build	O
a	O
heap	B-Application
from	O
an	O
array	B-Data_Structure
without	O
requiring	O
extra	O
memory	O
(	O
for	O
the	O
nodes	O
,	O
for	O
example	O
)	O
,	O
heapsort	B-Application
can	O
be	O
used	O
to	O
sort	O
an	O
array	B-Data_Structure
in-place	O
.	O
</s>
<s>
After	O
an	O
element	O
is	O
inserted	O
into	O
or	O
deleted	O
from	O
a	O
heap	B-Application
,	O
the	O
heap	B-Application
property	I-Application
may	O
be	O
violated	O
,	O
and	O
the	O
heap	B-Application
must	O
be	O
re-balanced	O
by	O
swapping	O
elements	O
within	O
the	O
array	B-Data_Structure
.	O
</s>
<s>
Although	O
different	O
type	O
of	O
heaps	B-Application
implement	O
the	O
operations	O
differently	O
,	O
the	O
most	O
common	O
way	O
is	O
as	O
follows	O
:	O
</s>
<s>
Insertion	O
:	O
Add	O
the	O
new	O
element	O
at	O
the	O
end	O
of	O
the	O
heap	B-Application
,	O
in	O
the	O
first	O
available	O
free	O
space	O
.	O
</s>
<s>
If	O
this	O
will	O
violate	O
the	O
heap	B-Application
property	I-Application
,	O
sift	O
up	O
the	O
new	O
element	O
(	O
swim	O
operation	O
)	O
until	O
the	O
heap	B-Application
property	I-Application
has	O
been	O
reestablished	O
.	O
</s>
<s>
Extraction	O
:	O
Remove	O
the	O
root	O
and	O
insert	O
the	O
last	O
element	O
of	O
the	O
heap	B-Application
in	O
the	O
root	O
.	O
</s>
<s>
If	O
this	O
will	O
violate	O
the	O
heap	B-Application
property	I-Application
,	O
sift	O
down	O
the	O
new	O
root	O
(	O
sink	O
operation	O
)	O
to	O
reestablish	O
the	O
heap	B-Application
property	I-Application
.	O
</s>
<s>
Construction	O
of	O
a	O
binary	O
(	O
or	O
d-ary	O
)	O
heap	B-Application
out	O
of	O
a	O
given	O
array	B-Data_Structure
of	O
elements	O
may	O
be	O
performed	O
in	O
linear	O
time	O
using	O
the	O
classic	O
Floyd	O
algorithm	O
,	O
with	O
the	O
worst-case	O
number	O
of	O
comparisons	O
equal	O
to	O
2N	O
−	O
2s2(N )	O
−	O
e2(N )	O
(	O
for	O
a	O
binary	B-Application
heap	I-Application
)	O
,	O
where	O
s2(N )	O
is	O
the	O
sum	O
of	O
all	O
digits	O
of	O
the	O
binary	O
representation	O
of	O
N	O
and	O
e2(N )	O
is	O
the	O
exponent	O
of	O
2	O
in	O
the	O
prime	O
factorization	O
of	O
N	O
.	O
This	O
is	O
faster	O
than	O
a	O
sequence	O
of	O
consecutive	O
insertions	O
into	O
an	O
originally	O
empty	O
heap	B-Application
,	O
which	O
is	O
log-linear	O
.	O
</s>
<s>
The	O
heap	B-Application
data	I-Application
structure	I-Application
has	O
many	O
applications	O
.	O
</s>
<s>
Heapsort	B-Application
:	O
One	O
of	O
the	O
best	O
sorting	B-Algorithm
methods	O
being	O
in-place	O
and	O
with	O
no	O
quadratic	O
worst-case	O
scenarios	O
.	O
</s>
<s>
Selection	B-Algorithm
algorithms	I-Algorithm
:	O
A	O
heap	B-Application
allows	O
access	O
to	O
the	O
min	O
or	O
max	O
element	O
in	O
constant	O
time	O
,	O
and	O
other	O
selections	O
(	O
such	O
as	O
median	O
or	O
kth-element	O
)	O
can	O
be	O
done	O
in	O
sub-linear	O
time	O
on	O
data	O
that	O
is	O
in	O
a	O
heap	B-Application
.	O
</s>
<s>
Graph	O
algorithms	O
:	O
By	O
using	O
heaps	B-Application
as	O
internal	O
traversal	O
data	B-General_Concept
structures	I-General_Concept
,	O
run	O
time	O
will	O
be	O
reduced	O
by	O
polynomial	O
order	O
.	O
</s>
<s>
Examples	O
of	O
such	O
problems	O
are	O
Prim	B-Algorithm
's	I-Algorithm
minimal-spanning-tree	I-Algorithm
algorithm	I-Algorithm
and	O
Dijkstra	B-Algorithm
's	I-Algorithm
shortest-path	I-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
Priority	B-Application
queue	I-Application
:	O
A	O
priority	B-Application
queue	I-Application
is	O
an	O
abstract	O
concept	O
like	O
"	O
a	O
list	O
"	O
or	O
"	O
a	O
map	O
"	O
;	O
just	O
as	O
a	O
list	O
can	O
be	O
implemented	O
with	O
a	O
linked	O
list	O
or	O
an	O
array	B-Data_Structure
,	O
a	O
priority	B-Application
queue	I-Application
can	O
be	O
implemented	O
with	O
a	O
heap	B-Application
or	O
a	O
variety	O
of	O
other	O
methods	O
.	O
</s>
<s>
K-way	B-Algorithm
merge	I-Algorithm
:	O
A	O
heap	B-Application
data	I-Application
structure	I-Application
is	O
useful	O
to	O
merge	O
many	O
already-sorted	O
input	O
streams	O
into	O
a	O
single	O
sorted	O
output	O
stream	O
.	O
</s>
<s>
Examples	O
of	O
the	O
need	O
for	O
merging	O
include	O
external	O
sorting	B-Algorithm
and	O
streaming	O
results	O
from	O
distributed	O
data	O
such	O
as	O
a	O
log	O
structured	O
merge	O
tree	B-Application
.	O
</s>
<s>
The	O
inner	O
loop	O
is	O
obtaining	O
the	O
min	O
element	O
,	O
replacing	O
with	O
the	O
next	O
element	O
for	O
the	O
corresponding	O
input	O
stream	O
,	O
then	O
doing	O
a	O
sift-down	O
heap	B-Application
operation	O
.	O
</s>
<s>
(	O
Using	O
extract-max	O
and	O
insert	O
functions	O
of	O
a	O
priority	B-Application
queue	I-Application
are	O
much	O
less	O
efficient	O
.	O
)	O
</s>
<s>
Order	B-General_Concept
statistics	I-General_Concept
:	O
The	O
Heap	B-Application
data	I-Application
structure	I-Application
can	O
be	O
used	O
to	O
efficiently	O
find	O
the	O
kth	O
smallest	O
(	O
or	O
largest	O
)	O
element	O
in	O
an	O
array	B-Data_Structure
.	O
</s>
<s>
The	O
C++	B-Language
Standard	I-Language
Library	I-Language
provides	O
the	O
,	O
and	O
algorithms	O
for	O
heaps	B-Application
(	O
usually	O
implemented	O
as	O
binary	B-Application
heaps	I-Application
)	O
,	O
which	O
operate	O
on	O
arbitrary	O
random	O
access	O
iterators	O
.	O
</s>
<s>
It	O
treats	O
the	O
iterators	O
as	O
a	O
reference	O
to	O
an	O
array	B-Data_Structure
,	O
and	O
uses	O
the	O
array-to-heap	O
conversion	O
.	O
</s>
<s>
The	O
Boost	B-Language
C++	I-Language
libraries	I-Language
include	O
a	O
heaps	B-Application
library	O
.	O
</s>
<s>
Unlike	O
the	O
STL	O
,	O
it	O
supports	O
decrease	O
and	O
increase	O
operations	O
,	O
and	O
supports	O
additional	O
types	O
of	O
heap	B-Application
:	O
specifically	O
,	O
it	O
supports	O
d-ary	O
,	O
binomial	O
,	O
Fibonacci	O
,	O
pairing	O
and	O
skew	B-Application
heaps	I-Application
.	O
</s>
<s>
There	O
is	O
a	O
for	O
C	B-Language
and	O
C++	B-Language
with	O
D-ary	B-Application
heap	I-Application
and	O
B-heap	B-Application
support	O
.	O
</s>
<s>
The	O
standard	O
library	O
of	O
the	O
D	B-Application
programming	I-Application
language	I-Application
includes	O
,	O
which	O
is	O
implemented	O
in	O
terms	O
of	O
D	B-Application
's	O
.	O
</s>
<s>
exposes	O
an	O
that	O
allows	O
iteration	O
with	O
D	B-Application
's	O
built-in	O
statements	O
and	O
integration	O
with	O
the	O
range-based	O
API	O
of	O
the	O
.	O
</s>
<s>
For	O
Haskell	B-Language
there	O
is	O
the	O
module	O
.	O
</s>
<s>
The	O
Java	B-Language
platform	O
(	O
since	O
version	O
1.5	O
)	O
provides	O
a	O
binary	B-Application
heap	I-Application
implementation	O
with	O
the	O
class	O
in	O
the	O
Java	B-Language
Collections	I-Language
Framework	I-Language
.	O
</s>
<s>
This	O
class	O
implements	O
by	O
default	O
a	O
min-heap	B-Application
;	O
to	O
implement	O
a	O
max-heap	B-Application
,	O
programmer	O
should	O
write	O
a	O
custom	O
comparator	O
.	O
</s>
<s>
Python	B-Language
has	O
a	O
module	O
that	O
implements	O
a	O
priority	B-Application
queue	I-Application
using	O
a	O
binary	B-Application
heap	I-Application
.	O
</s>
<s>
PHP	B-Application
has	O
both	O
max-heap	B-Application
(	O
)	O
and	O
min-heap	B-Application
(	O
)	O
as	O
of	O
version	O
5.3	O
in	O
the	O
Standard	O
PHP	B-Application
Library	O
.	O
</s>
<s>
Perl	B-Language
has	O
implementations	O
of	O
binary	O
,	O
binomial	O
,	O
and	O
Fibonacci	B-Application
heaps	I-Application
in	O
the	O
distribution	O
available	O
on	O
CPAN	B-Language
.	O
</s>
<s>
The	O
Go	B-Application
language	I-Application
contains	O
a	O
package	O
with	O
heap	B-Application
algorithms	O
that	O
operate	O
on	O
an	O
arbitrary	O
type	O
that	O
satisfies	O
a	O
given	O
interface	O
.	O
</s>
<s>
Apple	O
's	O
Core	B-Operating_System
Foundation	I-Operating_System
library	O
contains	O
a	O
structure	O
.	O
</s>
<s>
Pharo	B-Language
has	O
an	O
implementation	O
of	O
a	O
heap	B-Application
in	O
the	O
Collections-Sequenceable	O
package	O
along	O
with	O
a	O
set	O
of	O
test	O
cases	O
.	O
</s>
<s>
A	O
heap	B-Application
is	O
used	O
in	O
the	O
implementation	O
of	O
the	O
timer	O
event	O
loop	O
.	O
</s>
<s>
The	O
Rust	B-Application
programming	I-Application
language	I-Application
has	O
a	O
binary	O
max-heap	B-Application
implementation	O
,	O
,	O
in	O
the	O
module	O
of	O
its	O
standard	O
library	O
.	O
</s>
<s>
.NET	B-Application
has	O
class	O
which	O
uses	O
quaternary	O
(	O
d-ary	O
)	O
min-heap	B-Application
implementation	O
.	O
</s>
<s>
It	O
is	O
available	O
from	O
.NET	B-Application
6	I-Application
.	O
</s>
