-
Notifications
You must be signed in to change notification settings - Fork 13.3k
BinaryHeap example should show how to make a min-heap. #58174
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
Comments
Do you think you would be able to make a PR based on e.g. https://stackoverflow.com/questions/54489368/how-do-i-create-a-binaryheap-that-pops-the-smallest-value-not-the-largest ? |
Added an example in #60451 |
BinaryHeap: add min-heap example Fixes #58174.
I would like to point out that the current tutorial of making min-heap introduces unnecessary Is there any alternative I could approach to? or is it possible to introduce a compare function to binary heap? |
Not sure what you mean by unnecessary here. Can you explain?
Using a newtype is the way to use a custom compare function. By using |
As the tutorial shows, if I would like a min-heap based on binary heap, i need to unwrap the let Some(Reverse(n))=heap.pop() else {return ()};
// in compare with
let Some(n)=heap.pop() else {return ()}; It feels like an unnecessary gap for the differance of min or max heap. In my point of view, i would expect identical interface for the getter/setter since both are using the heap. How the heap is orderd should only be considered on the builder of that heap. i.e. //not working, just to show the concept of decide the order at building a heap
let mut rev_heap = BinaryHeap::<Reverse>::new();
// user, setting
rev_heap.push(1);
rev_heap.push(5);
rev_heap.push(2);
// user, getting
// If we pop these scores now, they should come back in the reverse order.
assert_eq!(rev_heap.pop(), Some(1));
let Some(n)=rev_heap.pop() else {return ()};
assert_eq!(n,2); To conclude, i think the general use case is to set the order when defining the heap, rather than when the first time using it. ps. I could fully understand that we would like to provide a minimal and stable interface for such a basic data structure. And let people decide what they actually want. But we could discuss if there's a better way, couldn't we? |
Alright, I get your point. Let me try to clarify some things. It seems to me that you're new to Rust. If you questions feel free to ask them on ULRO
They are!
That's a possible approach, but not the one taken by the Rust language. Here the struct that is put in the Heap says how ordering should be done by using the
The correct one would be:
That's not correct. As said, the struct inside the heap (in your case I hope this makes it clearer to you. If not, feel free to open a new topic on the users forum |
std::collections::BinaryHeap is a max-heap. A very common use case for users will be to make a min-heap. The basic example should include a min-heap.
The text was updated successfully, but these errors were encountered: