-
Notifications
You must be signed in to change notification settings - Fork 99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a binary_heap module #459
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. I'm not sure to understand what use the heap serves? There are no heap traversal functions, so it looks like it's only used to keep a sorted list of items?
modules/binary_heap.be
Outdated
# https://en.wikipedia.org/wiki/Binary_heap | ||
# similar to C++11 std::make_heap | ||
|
||
def heapify(array, cmp, i) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome. One last thing, can you include heapify
in the module scope? Here it is polluting the global namespace more than necessary.
If you want to keep it private, you could also use binary_heap._heapify
using the _
prefix as a marker for a private function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My intention was to keep it private, so I will use binary_heap._heapify
.
I had not realized that the function could end up being defined in the global namespace. I just double-checked in the Berry scripting console of my Tasmota environment that includes arendst/Tasmota#22529, and even after I execute import binary_heap
it would not recognize the symbol heapify
. I also failed to find that symbol via import global
. For my understanding, how could I access this kind of a symbol that is defined outside the module scope?
binary_heap.make_heap(array,cmp) transforms a Berry list into a binary heap where the heap property is defined by a comparison cmp. binary_heap.remove_heap(array,cmp) removes and returns the first element of a binary heap and maintains the heap property for the remaining elements. By repeatedly invoking it, one can obtain the first few elements of a Berry list in a sorted order, without having to sort all elements. binary_heap.sort(array,cmp) implements a heap sort of a Berry list. The example program can be run as follows: berry -m modules modules/examples/binary_heap_sort.be
binary_heap.make_heap(array,cmp)
transforms a Berrylist
into a binary heap where the heap property is defined by a comparisoncmp
.binary_heap.remove_heap(array,cmp)
removes and returns the first element of a binary heap and maintains the heap property for the remaining elements. By repeatedly invoking it, one can obtain the first few elements of a Berry list in a sorted order, without having to sort all elements.binary_heap.sort(array,cmp)
implements a heap sort of a Berrylist
.The example program can be run as follows: