  
  [1X3 [33X[0;0YHeaps[133X[101X
  
  
  [1X3.1 [33X[0;0YIntroduction[133X[101X
  
  [33X[0;0YA  [13Xheap[113X  is  a  tree  datastructure such that for any child [23XC[123X of a node [23XN[123X it
  holds that [23XC \leq N[123X, according to some ordering relation [23X\leq[123X.[133X
  
  [33X[0;0YThe fundamental operations for heaps are Construction, [10XPush[110Xing data onto the
  heap,  [10XPeek[110Xing  at the topmost item, and [10XPop[110Xping the topmost item off of the
  heap.[133X
  
  [33X[0;0YFor  a  good  heap  implementation  these basic operations should not exceed
  [23XO(\log n)[123X in runtime where [23Xn[123X is the number of items on the heap.[133X
  
  [33X[0;0YWe  currently provide two types of heaps: Binary Heaps [14X3.3[114X and Pairing Heaps
  [14X3.4[114X.[133X
  
  [33X[0;0YThe following code shows how to use a binary heap.[133X
  
  [4X[32X  Example  [32X[104X
    [4X[25Xgap>[125X [27Xh := BinaryHeap();[127X[104X
    [4X[28X<binary heap with 0 entries>[128X[104X
    [4X[25Xgap>[125X [27XPush(h, 5);[127X[104X
    [4X[25Xgap>[125X [27XPush(h, -10);[127X[104X
    [4X[25Xgap>[125X [27XPeek(h);[127X[104X
    [4X[28X5[128X[104X
    [4X[25Xgap>[125X [27XPop(h);[127X[104X
    [4X[28X5[128X[104X
    [4X[25Xgap>[125X [27XPeek(h);[127X[104X
    [4X[28X-10[128X[104X
  [4X[32X[104X
  
  [33X[0;0YThe following code shows how to use a pairing heap.[133X
  
  [4X[32X  Example  [32X[104X
    [4X[25Xgap>[125X [27Xh := PairingHeap( {x,y} -> x.rank > y.rank );[127X[104X
    [4X[28X<pairing heap with 0 entries>[128X[104X
    [4X[25Xgap>[125X [27XPush(h, rec( rank  := 5 ));[127X[104X
    [4X[25Xgap>[125X [27XPush(h, rec( rank  := 7 ));[127X[104X
    [4X[25Xgap>[125X [27XPush(h, rec( rank  := -15 ));[127X[104X
    [4X[25Xgap>[125X [27Xh;[127X[104X
    [4X[28X<pairing heap with 3 entries>[128X[104X
    [4X[25Xgap>[125X [27XPeek(h);[127X[104X
    [4X[28Xrec( rank := -15 )[128X[104X
    [4X[25Xgap>[125X [27XPop(h);[127X[104X
    [4X[28Xrec( rank := -15 )[128X[104X
  [4X[32X[104X
  
  
  [1X3.2 [33X[0;0YAPI[133X[101X
  
  [33X[0;0YFor  the  purposes  of  the  [5Xdatastructures[105X,  we  provide  a category [2XIsHeap[102X
  ([14X???IsHeap???[114X)  .  Every  implementation  of  a  heap in the category [2XIsHeap[102X
  ([14X???IsHeap???[114X) must follow the API described in this section.[133X
  
  [1X3.2-1 IsHeap[101X
  
  [33X[1;0Y[29X[2XIsHeap[102X( [3Xarg[103X ) [32X filter[133X
  [6XReturns:[106X  [33X[0;10Y[10Xtrue[110X or [10Xfalse[110X[133X
  
  [33X[0;0YThe category of heaps. Every object in this category promises to support the
  API described in this section.[133X
  
  [1X3.2-2 Heap[101X
  
  [33X[1;0Y[29X[2XHeap[102X( [3Xarg[103X ) [32X function[133X
  
  [33X[0;0YWrapper function around constructors[133X
  
  [1X3.2-3 NewHeap[101X
  
  [33X[1;0Y[29X[2XNewHeap[102X( [[3Xfilter[103X, [3Xfunc[103X, [3Xdata[103X] ) [32X constructor[133X
  [6XReturns:[106X  [33X[0;10Ya heap[133X
  
  [33X[0;0YConstruct a new heap[133X
  
  [1X3.2-4 Push[101X
  
  [33X[1;0Y[29X[2XPush[102X( [3Xheap[103X, [3Xobject[103X ) [32X operation[133X
  
  [33X[0;0YPuts the object [3Xobject[103X a new object onto [3Xheap[103X.[133X
  
  [1X3.2-5 Peek[101X
  
  [33X[1;0Y[29X[2XPeek[102X( [3Xheap[103X ) [32X operation[133X
  
  [33X[0;0YInspect the item at the top of [3Xheap[103X.[133X
  
  [1X3.2-6 Pop[101X
  
  [33X[1;0Y[29X[2XPop[102X( [3Xheap[103X ) [32X operation[133X
  [6XReturns:[106X  [33X[0;10Yan object[133X
  
  [33X[0;0YRemove the top item from [3Xheap[103X and return it.[133X
  
  [1X3.2-7 Merge[101X
  
  [33X[1;0Y[29X[2XMerge[102X( [3Xheap1[103X, [3Xheap2[103X ) [32X operation[133X
  
  [33X[0;0YMerge two heaps (of the same type)[133X
  
  [33X[0;0YHeaps also support [2XIsEmpty[102X ([14XReference: IsEmpty[114X) and [2XSize[102X ([14XReference: Size[114X)[133X
  
  
  [1X3.3 [33X[0;0YBinary Heaps[133X[101X
  
  [33X[0;0YA  binary  heap  employs a binary tree as its underlying tree datastructure.
  The  implemenataion  of binary heaps in [5Xdatastructures[105X 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.[133X
  
  [33X[0;0YFor some reference see [7Xhttp://stackoverflow.com/questions/6531543[107X[133X
  
  [1X3.3-1 BinaryHeap[101X
  
  [33X[1;0Y[29X[2XBinaryHeap[102X( [[3XisLess[103X[, [3Xdata[103X]] ) [32X function[133X
  [6XReturns:[106X  [33X[0;10YA binary heap[133X
  
  [33X[0;0YConstructor  for  binary heaps. The optinal argument [3XisLess[103X must be a binary
  function  that  performs  comparison  between  two elements on the heap, and
  returns  [9Xtrue[109X  if  the  first  argument  is  less than the second, and [9Xfalse[109X
  otherwise.  Using  the optional argument [3Xdata[103X the user can give a collection
  of initial values that are pushed on the stack after construction.[133X
  
  
  [1X3.4 [33X[0;0YPairing Heaps[133X[101X
  
  [33X[0;0YA  pairing heap is a heap datastructure with a very simple implementation in
  terms  of  [5XGAP[105X  lists.  [10XPush[110X  and  [10XPeek[110X have [22XO(1)[122X complexity, and [10XPop[110X has an
  amortized amortised O(log n), where [22Xn[122X is the number of items on the heap.[133X
  
  [33X[0;0YFor a reference see [FSST86].[133X
  
  [1X3.4-1 PairingHeap[101X
  
  [33X[1;0Y[29X[2XPairingHeap[102X( [[3XisLess[103X[, [3Xdata[103X]] ) [32X function[133X
  [6XReturns:[106X  [33X[0;10YA pairing heap[133X
  
  [33X[0;0YConstructor for pairing heaps. The optional argument [3XisLess[103X must be a binary
  function  that  performs  comparison  between  two elements on the heap, and
  returns  [9Xtrue[109X  if  the  first  argument  is  less than the second, and [9Xfalse[109X
  otherwise.  Using  the optional argument [3Xdata[103X the user can give a collection
  of initial values that are pushed on the stack after construction.[133X
  
  
  [1X3.5 [33X[0;0YDeclarations[133X[101X
  
  [1X3.5-1 IsBinaryHeapFlatRep[101X
  
  [33X[1;0Y[29X[2XIsBinaryHeapFlatRep[102X( [3Xarg[103X ) [32X filter[133X
  [6XReturns:[106X  [33X[0;10Y[10Xtrue[110X or [10Xfalse[110X[133X
  
  
  [1X3.6 [33X[0;0YImplementation[133X[101X
  
  [1X3.6-1 IsPairingHeapFlatRep[101X
  
  [33X[1;0Y[29X[2XIsPairingHeapFlatRep[102X( [3Xarg[103X ) [32X filter[133X
  [6XReturns:[106X  [33X[0;10Y[10Xtrue[110X or [10Xfalse[110X[133X
  
