{
    "mode": "man",
    "parameter": "Heap",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/man/Heap/json",
    "generated": "2026-11-11T07:42:02Z",
    "synopsis": "use Heap;\nmy $heap = Heap->new;\nmy $elem;\nuse Heap::Elem::Num(NumElem);\nforeach $i ( 1..100 ) {\n$elem = NumElem( $i );\n$heap->add( $elem );\n}\nwhile( defined( $elem = $heap->extracttop ) ) {\nprint \"Smallest is \", $elem->val, \"\\n\";\n}",
    "sections": {
        "NAME": {
            "content": "Heap - Perl extensions for keeping data partially sorted\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use Heap;\n\nmy $heap = Heap->new;\nmy $elem;\n\nuse Heap::Elem::Num(NumElem);\n\nforeach $i ( 1..100 ) {\n$elem = NumElem( $i );\n$heap->add( $elem );\n}\n\nwhile( defined( $elem = $heap->extracttop ) ) {\nprint \"Smallest is \", $elem->val, \"\\n\";\n}\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "The Heap collection of modules provide routines that manage a heap of elements.  A heap is a\npartially sorted structure that is always able to easily extract the smallest of the elements\nin the structure (or the largest if a reversed compare routine is provided).\n\nIf the collection of elements is changing dynamically, the heap has less overhead than\nkeeping the collection fully sorted.\n\nThe elements must be objects as described in \"Heap::Elem\" and all elements inserted into one\nheap must be mutually compatible - either the same class exactly or else classes that differ\nonly in ways unrelated to the Heap::Elem interface.\n",
            "subsections": []
        },
        "METHODS": {
            "content": "$heap = HeapClass::new(); $heap2 = $heap1->new();\nReturns  a  new  heap  object  of  the  specified  (sub-)class.   This is often used as a\nsubroutine instead of a method, of course.\n\n$heap->DESTROY\nEnsures that no internal circular data references remain.  Some variants of  Heap  ignore\nthis  (they  have  no  such  references).   Heap  users normally need not worry about it,\nDESTROY is automatically invoked when the heap reference goes out of scope.\n\n$heap->add($elem)\nAdd an element to the heap.\n\n$elem = $heap->top\nReturn the top element on the heap.  It is not removed from the heap but will  remain  at\nthe  top.  It will be the smallest element on the heap (unless a reversed cmp function is\nbeing used, in which case it will be the largest).  Returns undef if the heap is empty.\n\nThis method used to be called  \"minimum\"  instead  of  \"top\".   The  old  name  is  still\nsupported  but  is  deprecated.  (It was confusing to use the method \"minimum\" to get the\nmaximum value on the heap when a reversed cmp function was used for ordering elements.)\n\n$elem = $heap->extracttop\nDelete the top element from the heap and return it.  Returns undef if the heap was empty.\n\nThis method used to be called \"extractminimum\" instead of \"extracttop\".  The  old  name\nis   still   supported   but  is  deprecated.   (It  was  confusing  to  use  the  method\n\"extractminimum\" to get the maximum value on the heap when a reversed cmp  function  was\nused for ordering elements.)\n\n$heap1->absorb($heap2)\nMerge all of the elements from $heap2 into $heap1.  This will leave $heap2 empty.\n\n$heap1->decreasekey($elem)\nThe  element  will  be  moved closed to the top of the heap if it is now smaller than any\nhigher  parent  elements.   The  user  must  have  changed  the  value  of  $elem  before\ndecreasekey  is called.  Only a decrease is permitted.  (This is a decrease according to\nthe cmp function - if it is a reversed order comparison, then you are only  permitted  to\nincrease  the  value  of  the  element.  To be pedantic, you may only use decreasekey if\n$elem-cmp($elemoriginal) <= 0> if $elemoriginal were an elem with the value that  $elem\nhad before it was decreased.)\n\n$elem = $heap->delete($elem)\nThe element is removed from the heap (whether it is at the top or not).\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "John Macdonald, john@perlwolf.com\n",
            "subsections": []
        },
        "COPYRIGHT": {
            "content": "Copyright 1998-2007, O'Reilly & Associates.\n\nThis code is distributed under the same copyright terms as perl itself.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "Heap::Elem(3), Heap::Binary(3), Heap::Binomial(3), Heap::Fibonacci(3).\n\nperl v5.36.0                                 2022-10-22                                    Heap(3pm)",
            "subsections": []
        }
    },
    "summary": "Heap - Perl extensions for keeping data partially sorted",
    "flags": [],
    "examples": [],
    "see_also": [
        {
            "name": "Elem",
            "section": "3",
            "url": "https://www.chedong.com/phpMan.php/man/Elem/3/json"
        },
        {
            "name": "Binary",
            "section": "3",
            "url": "https://www.chedong.com/phpMan.php/man/Binary/3/json"
        },
        {
            "name": "Binomial",
            "section": "3",
            "url": "https://www.chedong.com/phpMan.php/man/Binomial/3/json"
        },
        {
            "name": "Fibonacci",
            "section": "3",
            "url": "https://www.chedong.com/phpMan.php/man/Fibonacci/3/json"
        }
    ]
}