<s>
A	O
binary	B-Application
heap	I-Application
is	O
a	O
heap	B-Application
data	I-Application
structure	I-Application
that	O
takes	O
the	O
form	O
of	O
a	O
binary	O
tree	O
.	O
</s>
<s>
Binary	B-Application
heaps	I-Application
are	O
a	O
common	O
way	O
of	O
implementing	O
priority	B-Application
queues	I-Application
.	O
</s>
<s>
The	O
binary	B-Application
heap	I-Application
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
heapsort	B-Application
.	O
</s>
<s>
A	O
binary	B-Application
heap	I-Application
is	O
defined	O
as	O
a	O
binary	O
tree	O
with	O
two	O
additional	O
constraints	O
:	O
</s>
<s>
Shape	O
property	O
:	O
a	O
binary	B-Application
heap	I-Application
is	O
a	O
complete	O
binary	O
tree	O
;	O
that	O
is	O
,	O
all	O
levels	O
of	O
the	O
tree	O
,	O
except	O
possibly	O
the	O
last	O
one	O
(	O
deepest	O
)	O
are	O
fully	O
filled	O
,	O
and	O
,	O
if	O
the	O
last	O
level	O
of	O
the	O
tree	O
is	O
not	O
complete	O
,	O
the	O
nodes	O
of	O
that	O
level	O
are	O
filled	O
from	O
left	O
to	O
right	O
.	O
</s>
<s>
Heap	B-Application
property	I-Application
:	O
the	O
key	O
stored	O
in	O
each	O
node	O
is	O
either	O
greater	O
than	O
or	O
equal	O
to	O
( ≥	O
)	O
or	O
less	O
than	O
or	O
equal	O
to	O
( ≤	O
)	O
the	O
keys	O
in	O
the	O
node	O
's	O
children	O
,	O
according	O
to	O
some	O
total	O
order	O
.	O
</s>
<s>
Heaps	B-Application
where	O
the	O
parent	O
key	O
is	O
greater	O
than	O
or	O
equal	O
to	O
( ≥	O
)	O
the	O
child	O
keys	O
are	O
called	O
max-heaps	B-Application
;	O
those	O
where	O
it	O
is	O
less	O
than	O
or	O
equal	O
to	O
( ≤	O
)	O
are	O
called	O
min-heaps	B-Application
.	O
</s>
<s>
Efficient	O
(	O
logarithmic	O
time	O
)	O
algorithms	O
are	O
known	O
for	O
the	O
two	O
operations	O
needed	O
to	O
implement	O
a	O
priority	B-Application
queue	I-Application
on	O
a	O
binary	B-Application
heap	I-Application
:	O
inserting	O
an	O
element	O
,	O
and	O
removing	O
the	O
smallest	O
or	O
largest	O
element	O
from	O
a	O
min-heap	B-Application
or	O
max-heap	B-Application
,	O
respectively	O
.	O
</s>
<s>
Binary	B-Application
heaps	I-Application
are	O
also	O
commonly	O
employed	O
in	O
the	O
heapsort	B-Application
sorting	B-Algorithm
algorithm	I-Algorithm
,	O
which	O
is	O
an	O
in-place	B-Algorithm
algorithm	I-Algorithm
because	O
binary	B-Application
heaps	I-Application
can	O
be	O
implemented	O
as	O
an	O
implicit	B-Data_Structure
data	I-Data_Structure
structure	I-Data_Structure
,	O
storing	O
keys	O
in	O
an	O
array	B-Data_Structure
and	O
using	O
their	O
relative	O
positions	O
within	O
that	O
array	B-Data_Structure
to	O
represent	O
child	O
–	O
parent	O
relationships	O
.	O
</s>
<s>
Both	O
the	O
insert	O
and	O
remove	O
operations	O
modify	O
the	O
heap	B-Application
to	O
conform	O
to	O
the	O
shape	O
property	O
first	O
,	O
by	O
adding	O
or	O
removing	O
from	O
the	O
end	O
of	O
the	O
heap	B-Application
.	O
</s>
<s>
Then	O
the	O
heap	B-Application
property	I-Application
is	O
restored	O
by	O
traversing	O
up	O
or	O
down	O
the	O
heap	B-Application
.	O
</s>
<s>
To	O
add	O
an	O
element	O
to	O
a	O
heap	B-Application
,	O
we	O
can	O
perform	O
this	O
algorithm	O
:	O
</s>
<s>
Add	O
the	O
element	O
to	O
the	O
bottom	O
level	O
of	O
the	O
heap	B-Application
at	O
the	O
leftmost	O
open	O
space	O
.	O
</s>
<s>
Steps	O
2	O
and	O
3	O
,	O
which	O
restore	O
the	O
heap	B-Application
property	I-Application
by	O
comparing	O
and	O
possibly	O
swapping	O
a	O
node	O
with	O
its	O
parent	O
,	O
are	O
called	O
the	O
up-heap	O
operation	O
(	O
also	O
known	O
as	O
bubble-up	O
,	O
percolate-up	O
,	O
sift-up	O
,	O
trickle-up	O
,	O
swim-up	O
,	O
heapify-up	O
,	O
or	O
cascade-up	O
)	O
.	O
</s>
<s>
The	O
number	O
of	O
operations	O
required	O
depends	O
only	O
on	O
the	O
number	O
of	O
levels	O
the	O
new	O
element	O
must	O
rise	O
to	O
satisfy	O
the	O
heap	B-Application
property	I-Application
.	O
</s>
<s>
Thus	O
,	O
the	O
insertion	O
operation	O
has	O
a	O
worst-case	B-General_Concept
time	O
complexity	O
of	O
.	O
</s>
<s>
For	O
a	O
random	O
heap	B-Application
,	O
and	O
for	O
repeated	O
insertions	O
,	O
the	O
insertion	O
operation	O
has	O
an	O
average-case	B-General_Concept
complexity	O
of	O
O(1 )	O
.	O
</s>
<s>
and	O
we	O
want	O
to	O
add	O
the	O
number	O
15	O
to	O
the	O
heap	B-Application
.	O
</s>
<s>
However	O
,	O
the	O
heap	B-Application
property	I-Application
is	O
violated	O
since	O
,	O
so	O
we	O
need	O
to	O
swap	O
the	O
15	O
and	O
the	O
8	O
.	O
</s>
<s>
So	O
,	O
we	O
have	O
the	O
heap	B-Application
looking	O
as	O
follows	O
after	O
the	O
first	O
swap	O
:	O
</s>
<s>
However	O
the	O
heap	B-Application
property	I-Application
is	O
still	O
violated	O
since	O
,	O
so	O
we	O
need	O
to	O
swap	O
again	O
:	O
</s>
<s>
which	O
is	O
a	O
valid	O
max-heap	B-Application
.	O
</s>
<s>
There	O
is	O
no	O
need	O
to	O
check	O
the	O
left	O
child	O
after	O
this	O
final	O
step	O
:	O
at	O
the	O
start	O
,	O
the	O
max-heap	B-Application
was	O
valid	O
,	O
meaning	O
the	O
root	O
was	O
already	O
greater	O
than	O
its	O
left	O
child	O
,	O
so	O
replacing	O
the	O
root	O
with	O
an	O
even	O
greater	O
value	O
will	O
maintain	O
the	O
property	O
that	O
each	O
node	O
is	O
greater	O
than	O
its	O
children	O
(	O
;	O
if	O
,	O
and	O
,	O
then	O
,	O
because	O
of	O
the	O
transitive	O
relation	O
)	O
.	O
</s>
<s>
The	O
procedure	O
for	O
deleting	O
the	O
root	O
from	O
the	O
heap	B-Application
(	O
effectively	O
extracting	O
the	O
maximum	O
element	O
in	O
a	O
max-heap	B-Application
or	O
the	O
minimum	O
element	O
in	O
a	O
min-heap	B-Application
)	O
while	O
retaining	O
the	O
heap	B-Application
property	I-Application
is	O
as	O
follows	O
:	O
</s>
<s>
Replace	O
the	O
root	O
of	O
the	O
heap	B-Application
with	O
the	O
last	O
element	O
on	O
the	O
last	O
level	O
.	O
</s>
<s>
(	O
Swap	O
with	O
its	O
smaller	O
child	O
in	O
a	O
min-heap	B-Application
and	O
its	O
larger	O
child	O
in	O
a	O
max-heap	B-Application
.	O
)	O
</s>
<s>
Steps	O
2	O
and	O
3	O
,	O
which	O
restore	O
the	O
heap	B-Application
property	I-Application
by	O
comparing	O
and	O
possibly	O
swapping	O
a	O
node	O
with	O
one	O
of	O
its	O
children	O
,	O
are	O
called	O
the	O
down-heap	O
(	O
also	O
known	O
as	O
bubble-down	O
,	O
percolate-down	O
,	O
sift-down	O
,	O
sink-down	O
,	O
trickle	O
down	O
,	O
heapify-down	O
,	O
cascade-down	O
,	O
extract-min	O
or	O
extract-max	O
,	O
or	O
simply	O
heapify	B-Application
)	O
operation	O
.	O
</s>
<s>
Now	O
the	O
heap	B-Application
property	I-Application
is	O
violated	O
since	O
8	O
is	O
greater	O
than	O
4	O
.	O
</s>
<s>
In	O
this	O
case	O
,	O
swapping	O
the	O
two	O
elements	O
,	O
4	O
and	O
8	O
,	O
is	O
enough	O
to	O
restore	O
the	O
heap	B-Application
property	I-Application
and	O
we	O
need	O
not	O
swap	O
elements	O
further	O
:	O
</s>
<s>
The	O
downward-moving	O
node	O
is	O
swapped	O
with	O
the	O
larger	O
of	O
its	O
children	O
in	O
a	O
max-heap	B-Application
(	O
in	O
a	O
min-heap	B-Application
it	O
would	O
be	O
swapped	O
with	O
its	O
smaller	O
child	O
)	O
,	O
until	O
it	O
satisfies	O
the	O
heap	B-Application
property	I-Application
in	O
its	O
new	O
position	O
.	O
</s>
<s>
This	O
functionality	O
is	O
achieved	O
by	O
the	O
Max-Heapify	O
function	O
as	O
defined	O
below	O
in	O
pseudocode	B-Language
for	O
an	O
array-backed	O
heap	B-Application
A	O
of	O
length	O
length(A )	O
.	O
</s>
<s>
Max-Heapify(A, i )	O
:	O
</s>
<s>
For	O
the	O
above	O
algorithm	O
to	O
correctly	O
re-heapify	O
the	O
array	B-Data_Structure
,	O
no	O
nodes	O
besides	O
the	O
node	O
at	O
index	O
i	O
and	O
its	O
two	O
direct	O
children	O
can	O
violate	O
the	O
heap	B-Application
property	I-Application
.	O
</s>
<s>
The	O
down-heap	O
operation	O
(	O
without	O
the	O
preceding	O
swap	O
)	O
can	O
also	O
be	O
used	O
to	O
modify	O
the	O
value	O
of	O
the	O
root	O
,	O
even	O
when	O
an	O
element	O
is	O
not	O
being	O
deleted	O
.	O
</s>
<s>
In	B-General_Concept
the	I-General_Concept
worst	I-General_Concept
case	I-General_Concept
,	O
the	O
new	O
root	O
has	O
to	O
be	O
swapped	O
with	O
its	O
child	O
on	O
each	O
level	O
until	O
it	O
reaches	O
the	O
bottom	O
level	O
of	O
the	O
heap	B-Application
,	O
meaning	O
that	O
the	O
delete	O
operation	O
has	O
a	O
time	O
complexity	O
relative	O
to	O
the	O
height	O
of	O
the	O
tree	O
,	O
or	O
O(log n )	O
.	O
</s>
<s>
Inserting	O
an	O
element	O
then	O
extracting	O
from	O
the	O
heap	B-Application
can	O
be	O
done	O
more	O
efficiently	O
than	O
simply	O
calling	O
the	O
insert	O
and	O
extract	O
functions	O
defined	O
above	O
,	O
which	O
would	O
involve	O
both	O
an	O
upheap	O
and	O
downheap	O
operation	O
.	O
</s>
<s>
If	O
the	O
root	O
of	O
the	O
heap	B-Application
is	O
greater	O
:	O
</s>
<s>
Python	B-Language
provides	O
such	O
a	O
function	O
for	O
insertion	O
then	O
extraction	O
called	O
"	O
heappushpop	O
"	O
,	O
which	O
is	O
paraphrased	O
below	O
.	O
</s>
<s>
The	O
heap	B-Application
array	B-Data_Structure
is	O
assumed	O
to	O
have	O
its	O
first	O
element	O
at	O
index	O
1	O
.	O
</s>
<s>
//	O
Push	O
a	O
new	O
item	O
to	O
a	O
(	O
max	O
)	O
heap	B-Application
and	O
then	O
extract	O
the	O
root	O
of	O
the	O
resulting	O
heap	B-Application
.	O
</s>
<s>
//	O
Returns	O
the	O
greater	O
of	O
the	O
two	O
between	O
item	O
and	O
the	O
root	O
of	O
heap	B-Application
.	O
</s>
<s>
</s>
<s>
A	O
similar	O
function	O
can	O
be	O
defined	O
for	O
popping	O
and	O
then	O
inserting	O
,	O
which	O
in	O
Python	B-Language
is	O
called	O
"	O
heapreplace	O
"	O
:	O
</s>
<s>
Replace( heap	O
:	O
Listxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx1	O
T	O
:	O
</s>
<s>
Down-heapify	O
or	O
up-heapify	O
to	O
restore	O
the	O
heap	B-Application
property	I-Application
.	O
</s>
<s>
In	O
a	O
max-heap	B-Application
(	O
min-heap	B-Application
)	O
,	O
up-heapify	O
is	O
only	O
required	O
when	O
the	O
new	O
key	O
of	O
element	O
is	O
greater	O
(	O
smaller	O
)	O
than	O
the	O
previous	O
one	O
because	O
only	O
the	O
heap-property	O
of	O
the	O
parent	O
element	O
might	O
be	O
violated	O
.	O
</s>
<s>
Assuming	O
that	O
the	O
heap-property	O
was	O
valid	O
between	O
element	O
and	O
its	O
children	O
before	O
the	O
element	O
swap	O
,	O
it	O
ca	O
n't	O
be	O
violated	O
by	O
a	O
now	O
larger	O
(	O
smaller	O
)	O
key	O
value	O
.	O
</s>
<s>
When	O
the	O
new	O
key	O
is	O
less	O
(	O
greater	O
)	O
than	O
the	O
previous	O
one	O
then	O
only	O
a	O
down-heapify	O
is	O
required	O
because	O
the	O
heap-property	O
might	O
only	O
be	O
violated	O
in	O
the	O
child	O
elements	O
.	O
</s>
<s>
This	O
involves	O
finding	O
the	O
node	O
with	O
the	O
given	O
value	O
,	O
changing	O
the	O
value	O
,	O
and	O
then	O
down-heapifying	O
or	O
up-heapifying	O
to	O
restore	O
the	O
heap	B-Application
property	I-Application
.	O
</s>
<s>
Building	O
a	O
heap	B-Application
from	O
an	O
array	B-Data_Structure
of	O
input	O
elements	O
can	O
be	O
done	O
by	O
starting	O
with	O
an	O
empty	O
heap	B-Application
,	O
then	O
successively	O
inserting	O
each	O
element	O
.	O
</s>
<s>
This	O
approach	O
,	O
called	O
Williams	O
 '	O
method	O
after	O
the	O
inventor	O
of	O
binary	B-Application
heaps	I-Application
,	O
is	O
easily	O
seen	O
to	O
run	O
in	O
time	O
:	O
it	O
performs	O
insertions	O
at	O
cost	O
each	O
.	O
</s>
<s>
A	O
faster	O
method	O
(	O
due	O
to	O
Floyd	O
)	O
starts	O
by	O
arbitrarily	O
putting	O
the	O
elements	O
on	O
a	O
binary	O
tree	O
,	O
respecting	O
the	O
shape	O
property	O
(	O
the	O
tree	O
could	O
be	O
represented	O
by	O
an	O
array	B-Data_Structure
,	O
see	O
below	O
)	O
.	O
</s>
<s>
Then	O
starting	O
from	O
the	O
lowest	O
level	O
and	O
moving	O
upwards	O
,	O
sift	O
the	O
root	O
of	O
each	O
subtree	O
downward	O
as	O
in	O
the	O
deletion	O
algorithm	O
until	O
the	O
heap	B-Application
property	I-Application
is	O
restored	O
.	O
</s>
<s>
More	O
specifically	O
if	O
all	O
the	O
subtrees	O
starting	O
at	O
some	O
height	O
have	O
already	O
been	O
"	O
heapified	O
"	O
(	O
the	O
bottommost	O
level	O
corresponding	O
to	O
)	O
,	O
the	O
trees	O
at	O
height	O
can	O
be	O
heapified	O
by	O
sending	O
their	O
root	O
down	O
along	O
the	O
path	O
of	O
maximum	O
valued	O
children	O
when	O
building	O
a	O
max-heap	B-Application
,	O
or	O
minimum	O
valued	O
children	O
when	O
building	O
a	O
min-heap	B-Application
.	O
</s>
<s>
Since	O
the	O
height	O
of	O
the	O
heap	B-Application
is	O
,	O
the	O
number	O
of	O
nodes	O
at	O
height	O
is	O
.	O
</s>
<s>
This	O
uses	O
the	O
fact	O
that	O
the	O
given	O
infinite	O
series	O
converges	B-Algorithm
.	O
</s>
<s>
The	O
exact	O
value	O
of	O
the	O
above	O
(	O
the	O
worst-case	B-General_Concept
number	O
of	O
comparisons	O
during	O
the	O
heap	B-Application
construction	O
)	O
is	O
known	O
to	O
be	O
equal	O
to	O
:	O
</s>
<s>
The	O
average	B-General_Concept
case	I-General_Concept
is	O
more	O
complex	O
to	O
analyze	O
,	O
but	O
it	O
can	O
be	O
shown	O
to	O
asymptotically	O
approach	O
comparisons	O
.	O
</s>
<s>
binary	O
tree	O
with	O
n	O
nodes	O
to	O
a	O
max-heap	B-Application
by	O
repeatedly	O
using	O
Max-Heapify	O
(	O
down-heapify	O
for	O
a	O
max-heap	B-Application
)	O
in	O
a	O
bottom-up	O
manner	O
.	O
</s>
<s>
are	O
all	O
leaves	O
for	O
the	O
tree	O
(	O
assuming	O
that	O
indices	O
start	O
at	O
1	O
)	O
—	O
thus	O
each	O
is	O
a	O
one-element	O
heap	B-Application
,	O
and	O
does	O
not	O
need	O
to	O
be	O
down-heapified	O
.	O
</s>
<s>
Max-Heapify	O
on	O
each	O
of	O
the	O
remaining	O
tree	O
nodes	O
.	O
</s>
<s>
Build-Max-Heap	O
(	O
A	O
)	O
:	O
</s>
<s>
Heaps	B-Application
are	O
commonly	O
implemented	O
with	O
an	O
array	B-Data_Structure
.	O
</s>
<s>
Any	O
binary	O
tree	O
can	O
be	O
stored	O
in	O
an	O
array	B-Data_Structure
,	O
but	O
because	O
a	O
binary	B-Application
heap	I-Application
is	O
always	O
a	O
complete	O
binary	O
tree	O
,	O
it	O
can	O
be	O
stored	O
compactly	O
.	O
</s>
<s>
No	O
space	O
is	O
required	O
for	O
pointers	O
;	O
instead	O
,	O
the	O
parent	O
and	O
children	O
of	O
each	O
node	O
can	O
be	O
found	O
by	O
arithmetic	O
on	O
array	B-Data_Structure
indices	O
.	O
</s>
<s>
These	O
properties	O
make	O
this	O
heap	B-Application
implementation	O
a	O
simple	O
example	O
of	O
an	O
implicit	B-Data_Structure
data	I-Data_Structure
structure	I-Data_Structure
or	O
Ahnentafel	O
list	O
.	O
</s>
<s>
Let	O
n	O
be	O
the	O
number	O
of	O
elements	O
in	O
the	O
heap	B-Application
and	O
i	O
be	O
an	O
arbitrary	O
valid	O
index	O
of	O
the	O
array	B-Data_Structure
storing	O
the	O
heap	B-Application
.	O
</s>
<s>
This	O
implementation	O
is	O
used	O
in	O
the	O
heapsort	B-Application
algorithm	O
which	O
reuses	O
the	O
space	O
allocated	O
to	O
the	O
input	O
array	B-Data_Structure
to	O
store	O
the	O
heap	B-Application
(	O
i.e.	O
</s>
<s>
the	O
algorithm	O
is	O
done	O
in-place	B-Algorithm
)	O
.	O
</s>
<s>
This	O
implementation	O
is	O
also	O
useful	O
as	O
a	O
Priority	B-Application
queue	I-Application
.	O
</s>
<s>
When	O
a	O
dynamic	B-Data_Structure
array	I-Data_Structure
is	O
used	O
,	O
insertion	O
of	O
an	O
unbounded	O
number	O
of	O
items	O
is	O
possible	O
.	O
</s>
<s>
The	O
upheap	O
or	O
downheap	O
operations	O
can	O
then	O
be	O
stated	O
in	O
terms	O
of	O
an	O
array	B-Data_Structure
as	O
follows	O
:	O
suppose	O
that	O
the	O
heap	B-Application
property	I-Application
holds	O
for	O
the	O
indices	O
b	O
,	O
b+1	O
,	O
...	O
,	O
e	O
.	O
The	O
sift-down	O
function	O
extends	O
the	O
heap	B-Application
property	I-Application
to	O
b−1	O
,	O
b	O
,	O
b+1	O
,	O
...	O
,	O
e	O
.	O
</s>
<s>
Only	O
index	O
i	O
=	O
b−1	O
can	O
violate	O
the	O
heap	B-Application
property	I-Application
.	O
</s>
<s>
Let	O
j	O
be	O
the	O
index	O
of	O
the	O
largest	O
child	O
of	O
a[i]	O
(	O
for	O
a	O
max-heap	B-Application
,	O
or	O
the	O
smallest	O
child	O
for	O
a	O
min-heap	B-Application
)	O
within	O
the	O
range	O
b	O
,	O
...	O
,	O
e	O
.	O
</s>
<s>
(	O
If	O
no	O
such	O
index	O
exists	O
because	O
then	O
the	O
heap	B-Application
property	I-Application
holds	O
for	O
the	O
newly	O
extended	O
range	O
and	O
nothing	O
needs	O
to	O
be	O
done	O
.	O
)	O
</s>
<s>
By	O
swapping	O
the	O
values	O
a[i]	O
and	O
a[j]	O
the	O
heap	B-Application
property	I-Application
for	O
position	O
i	O
is	O
established	O
.	O
</s>
<s>
At	O
this	O
point	O
,	O
the	O
only	O
problem	O
is	O
that	O
the	O
heap	B-Application
property	I-Application
might	O
not	O
hold	O
for	O
index	O
j	O
.	O
</s>
<s>
The	O
sift-down	O
function	O
is	O
applied	O
tail-recursively	B-Language
to	O
index	O
j	O
until	O
the	O
heap	B-Application
property	I-Application
is	O
established	O
for	O
all	O
elements	O
.	O
</s>
<s>
For	O
big	O
heaps	B-Application
and	O
using	O
virtual	B-Architecture
memory	I-Architecture
,	O
storing	O
elements	O
in	O
an	O
array	B-Data_Structure
according	O
to	O
the	O
above	O
scheme	O
is	O
inefficient	O
:	O
(	O
almost	O
)	O
every	O
level	O
is	O
in	O
a	O
different	O
page	B-General_Concept
.	O
</s>
<s>
B-heaps	B-Application
are	O
binary	B-Application
heaps	I-Application
that	O
keep	O
subtrees	O
in	O
a	O
single	O
page	B-General_Concept
,	O
reducing	O
the	O
number	O
of	O
pages	O
accessed	O
by	O
up	O
to	O
a	O
factor	O
of	O
ten	O
.	O
</s>
<s>
The	O
operation	O
of	O
merging	O
two	O
binary	B-Application
heaps	I-Application
takes	O
Θ(n )	O
for	O
equal-sized	O
heaps	B-Application
.	O
</s>
<s>
The	O
best	O
you	O
can	O
do	O
is	O
(	O
in	O
case	O
of	O
array	B-Data_Structure
implementation	O
)	O
simply	O
concatenating	O
the	O
two	O
heap	B-Application
arrays	O
and	O
build	O
a	O
heap	B-Application
of	O
the	O
result	O
.	O
</s>
<s>
A	O
heap	B-Application
on	O
n	O
elements	O
can	O
be	O
merged	O
with	O
a	O
heap	B-Application
on	O
k	O
elements	O
using	O
O(log n log k )	O
key	O
comparisons	O
,	O
or	O
,	O
in	O
case	O
of	O
a	O
pointer-based	O
implementation	O
,	O
in	O
O(log n log k )	O
time	O
.	O
</s>
<s>
of	O
heaps	B-Application
as	O
an	O
ordered	O
collections	O
of	O
subheaps	O
was	O
presented	O
in	O
.	O
</s>
<s>
The	O
view	O
also	O
presents	O
a	O
new	O
and	O
conceptually	O
simple	O
algorithm	O
for	O
merging	O
heaps	B-Application
.	O
</s>
<s>
When	O
merging	O
is	O
a	O
common	O
task	O
,	O
a	O
different	O
heap	B-Application
implementation	O
is	O
recommended	O
,	O
such	O
as	O
binomial	B-Application
heaps	I-Application
,	O
which	O
can	O
be	O
merged	O
in	O
O(log n )	O
.	O
</s>
<s>
Additionally	O
,	O
a	O
binary	B-Application
heap	I-Application
can	O
be	O
implemented	O
with	O
a	O
traditional	O
binary	O
tree	O
data	B-General_Concept
structure	I-General_Concept
,	O
but	O
there	O
is	O
an	O
issue	O
with	O
finding	O
the	O
adjacent	O
element	O
on	O
the	O
last	O
level	O
on	O
the	O
binary	B-Application
heap	I-Application
when	O
adding	O
an	O
element	O
.	O
</s>
<s>
This	O
element	O
can	O
be	O
determined	O
algorithmically	O
or	O
by	O
adding	O
extra	O
data	O
to	O
the	O
nodes	O
,	O
called	O
"	O
threading	O
"	O
the	O
tree	O
—	O
instead	O
of	O
merely	O
storing	O
references	O
to	O
the	O
children	O
,	O
we	O
store	O
the	O
inorder	B-Algorithm
successor	O
of	O
the	O
node	O
as	O
well	O
.	O
</s>
<s>
It	O
is	O
possible	O
to	O
modify	O
the	O
heap	B-Application
structure	I-Application
to	O
make	O
the	O
extraction	O
of	O
both	O
the	O
smallest	O
and	O
largest	O
element	O
possible	O
in	O
time	O
.	O
</s>
<s>
To	O
do	O
this	O
,	O
the	O
rows	O
alternate	O
between	O
min	B-Application
heap	I-Application
and	O
max-heap	B-Application
.	O
</s>
<s>
The	O
performance	O
is	O
roughly	O
the	O
same	O
as	O
a	O
normal	O
single	O
direction	O
heap	B-Application
.	O
</s>
<s>
This	O
idea	O
can	O
be	O
generalized	O
to	O
a	O
min-max-median	O
heap	B-Application
.	O
</s>
<s>
In	O
an	O
array-based	O
heap	B-Application
,	O
the	O
children	O
and	O
parent	O
of	O
a	O
node	O
can	O
be	O
located	O
via	O
simple	O
arithmetic	O
on	O
the	O
node	O
's	O
index	O
.	O
</s>
<s>
This	O
section	O
derives	O
the	O
relevant	O
equations	O
for	O
heaps	B-Application
with	O
their	O
root	O
at	O
index	O
0	O
,	O
with	O
additional	O
notes	O
on	O
heaps	B-Application
with	O
their	O
root	O
at	O
index	O
1	O
.	O
</s>
<s>
Using	O
this	O
throughout	O
yields	O
and	O
for	O
heaps	B-Application
with	O
their	O
root	O
at	O
1	O
.	O
</s>
<s>
Since	O
the	O
ordering	O
of	O
siblings	O
in	O
a	O
heap	B-Application
is	O
not	O
specified	O
by	O
the	O
heap	B-Application
property	I-Application
,	O
a	O
single	O
node	O
's	O
two	O
children	O
can	O
be	O
freely	O
interchanged	O
unless	O
doing	O
so	O
violates	O
the	O
shape	O
property	O
(	O
compare	O
with	O
treap	B-Application
)	O
.	O
</s>
<s>
Note	O
,	O
however	O
,	O
that	O
in	O
the	O
common	O
array-based	O
heap	B-Application
,	O
simply	O
swapping	O
the	O
children	O
might	O
also	O
necessitate	O
moving	O
the	O
children	O
's	O
sub-tree	O
nodes	O
to	O
retain	O
the	O
heap	B-Application
property	I-Application
.	O
</s>
<s>
The	O
binary	B-Application
heap	I-Application
is	O
a	O
special	O
case	O
of	O
the	O
d-ary	B-Application
heap	I-Application
in	O
which	O
d	O
=	O
2	O
.	O
</s>
