Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Add a binary_heap module #459

wants to merge 1 commit into from

Conversation

dr-m
Copy link

@dr-m dr-m commented Feb 23, 2025

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

Copy link
Contributor

@s-hadinger s-hadinger left a 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?

# https://en.wikipedia.org/wiki/Binary_heap
# similar to C++11 std::make_heap

def heapify(array, cmp, i)
Copy link
Contributor

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

Copy link
Author

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants