Posted 09 August 2017 - 12:48 PM
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.
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.
Adds a new element into the heap and sorts it
Returns top element and removes it from the heap
Same as before but doesn't remove the top element from the heap
Takes an array and creates a heap out of it
Returns the internal array of the heap
These should be self-explaining
For the sake of it I also implemented a heap sort function:
array is an unsorted array and CompareFunction is the same as in new().
Returns a sorted array
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