Snippet Maximum Binary Heap

Nestharus

o-o
Reaction score
84
Good Tutorial on Binary Heaps and Priority Queues

JASS:

library MABH /* v1.0.0.1
*************************************************************************************
*
*   Maximum Binary Heap
*
************************************************************************************
*
*   static readonly thistype root
*       -   The node with the largest value
*   readonly thistype node
*       -   Node stored within heap position
*   readonly thistype heap
*       -   Heap position of node
*   static readonly integer size
*       -   Size of binary heap
*   readonly integer value
*       -   Sorted value (the value of the node)
*
*   static method insert takes integer sortValue returns thistype
*       -   Inserts a new node into the heap and returns it
*   static method delete takes thistype node returns nothing
*       -   Deletes node from heap
*
************************************************************************************/
    module MABH
        readonly static integer size=0
        readonly thistype node              //node
        private static thistype nc=0        //node instance count
        private static thistype array r     //node recycler
        readonly integer value
        readonly thistype heap
        static method operator root takes nothing returns thistype
            return thistype(1).node
        endmethod
        static method allocate takes integer v returns thistype
            local thistype i
            if (0==r[0]) then
                set i=nc+1
                set nc=i
            else
                set i=r[0]
                set r[0]=r<i>
            endif
            set i.value=v
            return i
        endmethod
        method deallocate takes nothing returns nothing
            set r[this]=r[0]
            set r[0]=this
        endmethod
        static method insert takes integer v returns thistype
            local thistype i=size+1         //heap node
            local thistype p=i/2            //parent
            local thistype m
            set size=i
            
            //allocate node
            if (0==r[0]) then
                set m=nc+1
                set nc=m
            else
                set m=r[0]
                set r[0]=r[m]
            endif
            set m.value=v
            
            loop
                exitwhen 0==p or v&lt;p.node.value
                set i.node=p.node
                set i.node.heap=i
                set i=p
                set p=p/2
            endloop
            set i.node=m
            set m.heap=i
            
            return m
        endmethod
        static method delete takes thistype i returns nothing
            local thistype m
            local thistype h
            local thistype o
            if (0&lt;size) then
                set m=i.node
                set r[m]=r[0]
                set r[0]=m
                loop
                    set m=i*2       //left
                    set o=m+1       //right
                    exitwhen 0==m.node and 0==o.node
                    if (0==o.node.value or m.node.value&gt;o.node.value) then
                        set i.node=m.node
                        set i.node.heap=i
                        set i=m
                    else
                        set i.node=o.node
                        set i.node.heap=i
                        set i=o
                    endif
                    set i.node=0
                endloop
                set h=thistype(size).node       //last node
                set thistype(size).node=0
                set size=size-1                 //deallocate heap node
                if (0!=h) then
                    set m=i/2       //parent
                    set o=h.value   //value of last node
                    loop
                        exitwhen 0==m or integer(o)&lt;m.node.value
                        set i.node=m.node
                        set i.node.heap=i
                        set i=m
                        set m=m/2
                    endloop
                    set i.node=h
                    set h.heap=i
                endif
            endif
        endmethod
    endmodule
endlibrary
</i>


Demo
JASS:

struct Heap extends array
    implement MABH

    private static method onInit takes nothing returns nothing
        call insert(5)
        call insert(9)
        call insert(3)
        call insert(2)
        call insert(-1)
        call insert(16)
        loop
            exitwhen 0==size
            call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,60,I2S(root.value))
            call delete(1) //or call delete(root.heap), but delete 1 is faster
        endloop
        //16,9,5,3,2,-1
    endmethod
endstruct
 
I'm sorry Nest, but I really don't will follow links posted by you at all your threads explaining what is it.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    News portal has been retired. Main page of site goes to Headline News forum now
  • The Helper The Helper:
    I am working on getting access to the old news portal under a different URL for those that would rather use that for news before we get a different news view.
  • Ghan Ghan:
    Easily done
    +1
  • The Helper The Helper:
    https://www.thehelper.net/pages/news/ is a link to the old news portal - i will integrate it into the interface somewhere when i figure it out
  • Ghan Ghan:
    Need to try something
  • Ghan Ghan:
    Hopefully this won't cause problems.
  • Ghan Ghan:
    Hmm
  • Ghan Ghan:
    I have converted the Headline News forum to an Article type forum. It will now show the top 20 threads with more detail of each thread.
  • Ghan Ghan:
    See how we like that.
  • The Helper The Helper:
    I do not see a way to go past the 1st page of posts on the forum though
  • The Helper The Helper:
    It is OK though for the main page to open up on the forum in the view it was before. As long as the portal has its own URL so it can be viewed that way I do want to try it as a regular forum view for a while
  • Ghan Ghan:
    Yeah I'm not sure what the deal is with the pagination.
  • Ghan Ghan:
    It SHOULD be there so I think it might just be an artifact of having an older style.
  • Ghan Ghan:
    I switched it to a "Standard" article forum. This will show the thread list like normal, but the threads themselves will have the first post set up above the rest of the "comments"
  • The Helper The Helper:
    I don't really get that article forum but I think it is because I have never really seen it used on a multi post thread
  • Ghan Ghan:
    RpNation makes more use of it right now as an example: https://www.rpnation.com/news/
  • The Helper The Helper:
  • The Helper The Helper:
    What do you think Tom?
  • tom_mai78101 tom_mai78101:
    I will have to get used to this.
  • tom_mai78101 tom_mai78101:
    The latest news feed looks good

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top