<P/>
A <E>heap</E > is a tree datastructure such that for any child <Math>C</Math> of a node <Math>N</Math>
it holds that <Math>C \leq N</Math>, according to some ordering relation <Math>\leq</Math>.
<P/>
The fundamental operations for heaps are Construction, <C>Push</C>ing data
onto the heap, <C>Peek</C>ing at the topmost item, and <C>Pop</C>ping the
topmost item off of the heap.
<P/>
For a good heap implementation these basic operations should not exceed
<Math>O(\log n)</Math> in runtime where <Math>n</Math> is the number of items on the heap.
<P/>
We currently provide two types of heaps: Binary Heaps <Ref Sect='Section_BinaryHeap'/> and
Pairing Heaps <Ref Sect="Section_PairingHeap" />.<P/>
<P/>
The following code shows how to use a binary heap.
<Example><![CDATA[
gap> h := BinaryHeap();
<binary heap with 0 entries>
gap> Push(h, 5);
gap> Push(h, -10);
gap> Peek(h);
5
gap> Pop(h);
5
gap> Peek(h);
-10
]]></Example>
<P/>
The following code shows how to use a pairing heap.
<Example><![CDATA[
gap> h := PairingHeap( {x,y} -> x.rank > y.rank );
<pairing heap with 0 entries>
gap> Push(h, rec( rank := 5 ));
gap> Push(h, rec( rank := 7 ));
gap> Push(h, rec( rank := -15 ));
gap> h;
<pairing heap with 3 entries>
gap> Peek(h);
rec( rank := -15 )
gap> Pop(h);
rec( rank := -15 )
]]></Example>
<P/>
For the purposes of the <Package>datastructures</Package>, we provide
a category <Ref Filt="IsHeap" Label="for IsObject"/> . Every
implementation of a heap in the category <Ref Filt="IsHeap" Label="for IsObject"/>
must follow the API described in this section.
<P/>
<ManSection>
<Filt Arg="arg" Name="IsHeap" Label="for IsObject"/>
<Returns><K>true</K> or <K>false</K>
</Returns>
<Description>
The category of heaps. Every object in this category promises to
support the API described in this section.
</Description>
</ManSection>
<ManSection>
<Func Arg="arg" Name="Heap" />
<Description>
Wrapper function around constructors
</Description>
</ManSection>
<ManSection>
<Oper Arg="heap, object" Name="Push" Label="for IsHeap, IsObject"/>
<Description>
Puts the object <A>object</A> a new object onto <A>heap</A>.
<P/>
</Description>
</ManSection>
<ManSection>
<Oper Arg="heap" Name="Peek" Label="for IsHeap"/>
<Description>
Inspect the item at the top of <A>heap</A>.
<P/>
</Description>
</ManSection>
<ManSection>
<Oper Arg="heap" Name="Pop" Label="for IsHeap"/>
<Returns>an object
</Returns>
<Description>
Remove the top item from <A>heap</A> and return it.
</Description>
</ManSection>
<ManSection>
<Oper Arg="heap1, heap2" Name="Merge" Label="for IsHeap, IsHeap"/>
<Description>
Merge two heaps (of the same type)
</Description>
</ManSection>
Heaps also support <Ref Filt="IsEmpty" BookName="ref"/> and
<Ref Oper="Size" BookName="ref"/>
</Section>
<P/>
A binary heap employs a binary tree as its underlying tree datastructure.
The implemenataion of binary heaps in <Package>datastructures</Package> stores
this tree in a flat array which makes it a very good and fast default choice for
general purpose use. In particular, even though other heap implementations have
better theoretical runtime bounds, well-tuned binary heaps outperform them
in many applications.
<P/>
For some reference see <URL>http://stackoverflow.com/questions/6531543</URL>
<ManSection>
<Func Arg="[isLess, [data]]" Name="BinaryHeap" />
<Returns>A binary heap
</Returns>
<Description>
Constructor for binary heaps. The optional argument <A>isLess</A> must be a binary function
that performs comparison between two elements on the heap, and returns <K>true</K> if the first
argument is less than the second, and <K>false</K> otherwise.
Using the optional argument <A>data</A> the user can give a collection of initial values that
are pushed on the stack after construction.
</Description>
</ManSection>
<P/>
A pairing heap is a heap datastructure with a very simple implementation in
terms of &GAP; lists.
<C>Push</C> and <C>Peek</C> have <M>O(1)</M> complexity, and <C>Pop</C> has an amortized
amortised O(log n), where <M>n</M> is the number of items on the heap.
<P/>
For a reference see <Cite Key="Fredman1986"/>.
<P/>
<ManSection>
<Func Arg="[isLess, [data]]" Name="PairingHeap" />
<Returns>A pairing heap
</Returns>
<Description>
Constructor for pairing heaps. The optional argument <A>isLess</A> must be a binary function
that performs comparison between two elements on the heap, and returns <K>true</K> if the first
argument is less than the second, and <K>false</K> otherwise.
Using the optional argument <A>data</A> the user can give a collection of initial values that
are pushed on the stack after construction.
</Description>
</ManSection>
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.