<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
a	O
weak	B-Application
heap	I-Application
is	O
a	O
data	B-General_Concept
structure	I-General_Concept
for	O
priority	B-Application
queues	I-Application
,	O
combining	O
features	O
of	O
the	O
binary	B-Application
heap	I-Application
and	O
binomial	B-Application
heap	I-Application
.	O
</s>
<s>
It	O
can	O
be	O
stored	O
in	O
an	O
array	B-Data_Structure
as	O
an	O
implicit	B-Data_Structure
binary	O
tree	O
like	O
a	O
binary	B-Application
heap	I-Application
,	O
and	O
has	O
the	O
efficiency	O
guarantees	O
of	O
binomial	B-Application
heaps	I-Application
.	O
</s>
<s>
A	O
sorting	B-Algorithm
algorithm	I-Algorithm
using	O
weak	B-Application
heaps	I-Application
,	O
weak-heapsort	O
,	O
uses	O
a	O
number	O
of	O
comparisons	O
that	O
is	O
close	O
to	O
the	O
theoretical	O
lower	O
bound	O
on	O
the	O
number	O
of	O
comparisons	O
required	O
to	O
sort	O
a	O
list	O
,	O
so	O
is	O
particularly	O
useful	O
when	O
comparison	O
is	O
expensive	O
,	O
such	O
as	O
when	O
comparing	O
strings	O
using	O
the	O
full	O
Unicode	O
collation	O
algorithm	O
.	O
</s>
<s>
A	O
weak	B-Application
heap	I-Application
is	O
most	O
easily	O
understood	O
as	O
a	O
heap-ordered	O
multi-way	B-Data_Structure
tree	I-Data_Structure
stored	O
as	O
a	O
binary	O
tree	O
using	O
the	O
"	O
right-child	O
left-sibling	O
"	O
convention	O
.	O
</s>
<s>
In	O
the	O
multi-way	B-Data_Structure
tree	I-Data_Structure
,	O
and	O
assuming	O
a	O
max-heap	O
,	O
each	O
parent	O
's	O
key	O
is	O
greater	O
than	O
or	O
equal	O
to	O
(	O
)	O
all	O
the	O
child	O
keys	O
(	O
and	O
thus	O
,	O
by	O
induction	O
,	O
all	O
members	O
of	O
the	O
subtree	O
)	O
.	O
</s>
<s>
The	O
last	O
condition	O
is	O
a	O
consequence	O
of	O
the	O
fact	O
that	O
an	O
implicit	B-Data_Structure
binary	O
tree	O
is	O
a	O
complete	O
binary	O
tree	O
.	O
</s>
<s>
The	O
structure	O
of	O
this	O
tree	O
maps	O
very	O
neatly	O
onto	O
the	O
traditional	O
-based	O
(	O
Ahnentafel	O
)	O
implicit	B-Data_Structure
binary	O
tree	O
arrangement	O
,	O
where	O
node	O
has	O
a	O
next	O
sibling	O
(	O
left	O
child	O
)	O
numbered	O
and	O
a	O
first	O
child	O
(	O
right	O
child	O
)	O
numbered	O
,	O
by	O
adding	O
an	O
additional	O
root	O
numbered	O
.	O
</s>
<s>
This	O
structure	O
is	O
very	O
similar	O
to	O
that	O
of	O
a	O
binomial	B-Application
heap	I-Application
,	O
with	O
a	O
tree	O
of	O
height	O
being	O
composed	O
of	O
a	O
root	O
plus	O
trees	O
of	O
heights	O
,	O
,	O
...	O
,	O
.	O
</s>
<s>
A	O
perfect	O
(	O
no	O
missing	O
leaves	O
)	O
weak	B-Application
heap	I-Application
with	O
elements	O
is	O
exactly	O
isomorphic	O
to	O
a	O
binomial	B-Application
heap	I-Application
of	O
the	O
same	O
size	O
,	O
but	O
the	O
two	O
algorithms	O
handle	O
sizes	O
which	O
are	O
not	O
a	O
power	O
of	O
differently	O
:	O
a	O
binomial	B-Application
heap	I-Application
uses	O
multiple	O
perfect	O
trees	O
,	O
while	O
a	O
weak	B-Application
heap	I-Application
uses	O
a	O
single	O
imperfect	O
tree	O
.	O
</s>
<s>
Weak	B-Application
heaps	I-Application
require	O
the	O
ability	O
to	O
exchange	O
the	O
left	O
and	O
right	O
children	O
(	O
and	O
associated	O
subtrees	O
)	O
of	O
a	O
node	O
.	O
</s>
<s>
In	O
an	O
implicit	B-Data_Structure
(	O
array	B-Data_Structure
)	O
representation	O
,	O
this	O
requires	O
one	O
"	O
reverse	O
bit	O
"	O
per	O
internal	O
node	O
to	O
indicate	O
which	O
child	O
is	O
considered	O
the	O
left	O
child	O
.	O
</s>
<s>
A	O
weak	B-Application
heap	I-Application
is	O
thus	O
not	O
a	O
strictly	O
implicit	B-Data_Structure
data	I-Data_Structure
structure	I-Data_Structure
since	O
it	O
requires	O
additional	O
space	O
(	O
bit	O
per	O
node	O
)	O
.	O
</s>
<s>
In	O
the	O
implicit	B-Data_Structure
binary	O
tree	O
,	O
node	O
with	O
reverse	O
bit	O
has	O
parent	O
,	O
left	O
child	O
,	O
and	O
right	O
child	O
.	O
</s>
<s>
Viewed	O
as	O
a	O
multi-way	B-Data_Structure
tree	I-Data_Structure
,	O
each	O
node	O
in	O
a	O
weak	B-Application
heap	I-Application
is	O
linked	O
to	O
two	O
others	O
:	O
a	O
"	O
next	O
sibling	O
"	O
and	O
a	O
"	O
first	O
child	O
"	O
.	O
</s>
<s>
In	O
the	O
implicit	B-Data_Structure
tree	O
,	O
the	O
links	O
are	O
fixed	O
,	O
so	O
which	O
of	O
the	O
two	O
links	O
is	O
the	O
sibling	O
and	O
which	O
the	O
first	O
child	O
is	O
indicated	O
by	O
the	O
reverse	O
bit	O
.	O
</s>
<s>
Note	O
that	O
every	O
node	O
in	O
a	O
weak	B-Application
heap	I-Application
can	O
be	O
considered	O
the	O
root	O
of	O
a	O
smaller	O
weak	B-Application
heap	I-Application
by	O
ignoring	O
its	O
next	O
sibling	O
.	O
</s>
<s>
Nodes	O
with	O
no	O
first	O
child	O
are	O
automatically	O
valid	O
weak	B-Application
heaps	I-Application
.	O
</s>
<s>
A	O
node	O
's	O
parent	O
in	O
the	O
multi-way	B-Data_Structure
tree	I-Data_Structure
is	O
called	O
its	O
"	O
distinguished	O
ancestor	O
"	O
.	O
</s>
<s>
In	O
the	O
implicit	B-Data_Structure
tree	O
,	O
finding	O
the	O
binary	O
parent	O
is	O
easy	O
,	O
but	O
its	O
reverse	O
bit	O
must	O
be	O
consulted	O
to	O
determine	O
which	O
type	O
of	O
child	O
the	O
node	O
is	O
.	O
</s>
<s>
Like	O
binomial	B-Application
heaps	I-Application
,	O
the	O
fundamental	O
operation	O
on	O
weak	B-Application
heaps	I-Application
is	O
merging	O
two	O
heaps	O
of	O
equal	O
height	O
,	O
to	O
make	O
a	O
weak	B-Application
heap	I-Application
of	O
height	O
.	O
</s>
<s>
This	O
operation	O
can	O
be	O
performed	O
on	O
the	O
implicit	B-Data_Structure
tree	O
structure	O
because	O
the	O
heaps	O
being	O
merged	O
are	O
never	O
arbitrary	O
.	O
</s>
<s>
Rather	O
,	O
the	O
two	O
heaps	O
are	O
formed	O
as	O
part	O
of	O
sifting	O
a	O
node	O
up	O
the	O
multi-way	B-Data_Structure
tree	I-Data_Structure
:	O
</s>
<s>
The	O
first	O
is	O
a	O
normal	O
weak	B-Application
heap	I-Application
(	O
whose	O
next	O
sibling	O
link	O
exists	O
,	O
but	O
is	O
ignored	O
)	O
.	O
</s>
<s>
The	O
second	O
case	O
works	O
because	O
,	O
in	O
the	O
multi-way	B-Data_Structure
tree	I-Data_Structure
,	O
each	O
node	O
keeps	O
its	O
children	O
with	O
it	O
.	O
</s>
<s>
Weak	B-Application
heaps	I-Application
may	O
be	O
used	O
to	O
sort	O
an	O
array	B-Data_Structure
,	O
in	O
essentially	O
the	O
same	O
way	O
as	O
a	O
conventional	O
heapsort	B-Application
.	O
</s>
<s>
First	O
,	O
a	O
weak	B-Application
heap	I-Application
is	O
built	O
out	O
of	O
all	O
of	O
the	O
elements	O
of	O
the	O
array	B-Data_Structure
,	O
and	O
then	O
the	O
root	O
is	O
repeatedly	O
exchanged	O
with	O
the	O
last	O
element	O
,	O
which	O
is	O
sifted	O
down	O
to	O
its	O
proper	O
place	O
.	O
</s>
<s>
A	O
weak	B-Application
heap	I-Application
of	O
elements	O
can	O
be	O
formed	O
in	O
merges	O
.	O
</s>
<s>
It	O
can	O
be	O
done	O
on	O
various	O
orders	O
,	O
but	O
a	O
simple	O
bottom-up	O
implementation	O
works	O
from	O
the	O
end	O
of	O
the	O
array	B-Data_Structure
to	O
the	O
beginning	O
,	O
merging	O
each	O
node	O
with	O
its	O
distinguished	O
ancestor	O
.	O
</s>
<s>
As	O
with	O
heapsort	B-Application
,	O
if	O
the	O
array	B-Data_Structure
to	O
be	O
sorted	O
is	O
larger	O
than	O
the	O
CPU	B-General_Concept
cache	I-General_Concept
,	O
performance	O
is	O
improved	O
if	O
subtrees	O
are	O
merged	O
as	O
soon	O
as	O
two	O
of	O
the	O
same	O
size	O
become	O
available	O
,	O
rather	O
than	O
merging	O
all	O
subtrees	O
on	O
one	O
level	O
before	O
proceeding	O
to	O
the	O
next	O
.	O
</s>
<s>
Sifting	O
down	O
in	O
a	O
weak	B-Application
heap	I-Application
can	O
be	O
done	O
in	O
comparisons	O
,	O
as	O
opposed	O
to	O
for	O
a	O
binary	B-Application
heap	I-Application
,	O
or	O
for	O
the	O
"	O
bottom-up	O
heapsort	B-Application
"	O
variant	O
.	O
</s>
<s>
As	O
with	O
binary	B-Application
heaps	I-Application
,	O
weak	B-Application
heaps	I-Application
can	O
support	O
the	O
typical	O
operations	O
of	O
a	O
priority	B-Application
queue	I-Application
data	B-General_Concept
structure	I-General_Concept
:	O
insert	O
,	O
delete-min	O
,	O
delete	O
,	O
or	O
decrease-key	O
,	O
in	O
logarithmic	O
time	O
per	O
operation	O
.	O
</s>
<s>
Sifting	O
up	O
is	O
done	O
using	O
the	O
same	O
process	O
as	O
in	O
binary	B-Application
heaps	I-Application
.	O
</s>
<s>
Variants	O
of	O
the	O
weak	B-Application
heap	I-Application
structure	O
allow	O
constant	O
amortized	B-General_Concept
time	I-General_Concept
insertions	O
and	O
decrease-keys	O
,	O
matching	O
the	O
time	O
for	O
Fibonacci	B-Application
heaps	I-Application
.	O
</s>
<s>
Weak	B-Application
heaps	I-Application
were	O
introduced	O
by	O
,	O
as	O
part	O
of	O
a	O
variant	O
heap	B-Application
sort	I-Application
algorithm	O
that	O
(	O
unlike	O
the	O
standard	O
heap	B-Application
sort	I-Application
using	O
binary	B-Application
heaps	I-Application
)	O
could	O
be	O
used	O
to	O
sort	O
items	O
using	O
only	O
comparisons	O
.	O
</s>
<s>
They	O
were	O
later	O
investigated	O
as	O
a	O
more	O
generally	O
applicable	O
priority	B-Application
queue	I-Application
data	B-General_Concept
structure	I-General_Concept
.	O
</s>
