Jump to content




Lua binary heap implementation

api lua

  • You cannot reply to this topic
No replies to this topic

#1 Henrik

  • Members
  • 3 posts

Posted 09 August 2017 - 11:48 AM

Hello fellows,
I created a lua module that implements a binary heap. A binary heap is a structure that allows to access the top element very fast.
Code: https://gitlab.com/H.../LuaBinary_Heap
Raw: https://gitlab.com/H...binary_heap.lua
I will now briefly descripe what each function does.

binary_heap.new(CompareFunction)

Returns a new heap object. By default it will create a max heap, but with "<" as input it will create a min heap. You can also pass a special compare function in case you sort more complex values.

binary_heap:push(value)

Adds a new element into the heap and sorts it

binary_heap:pop()

Returns top element and removes it from the heap

binary_heap:top()

Same as before but doesn't remove the top element from the heap

binary_heap:make(array)

Takes an array and creates a heap out of it

binary_heap:array()

Returns the internal array of the heap

binary_heap:size()
binary_heap:empty()

These should be self-explaining

For the sake of it I also implemented a heap sort function:

binary_heap.sort(array, CompareFunction)

array is an unsorted array and CompareFunction is the same as in new().
Returns a sorted array





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users