So now we're going to talk about a binary heap.
A binary heap is a collection whose elements are kept loosely organized.
The greatest value always bubbles up to the front.
There are three methods commonly used with the binary heap and they are push, pop and peak.
And those are going to be the three methods that we look at.
So let's start by bringing in a binary heap into our scope by calling standard collections binary heap.
So now we can create a mutable B heap by saying binary heap new.
And that's going to instantiate us a new binary heap.
And now, like what we did with a vector, we can push values onto here.
So let's push in multiple values just to make sure that our largest value is always bubbling up to the
front.
So now let's print out the binary heap and just make sure that 20
is at the front.
That's exactly what we got.
Awesome.
So the behavior is working as expected.
So the first thing you know, we saw that in vectors.
But we can also do pop.
And what this is going to do is it's it's going to take off the value at the front.
So if we run it again, we expect 20 to be gone, but now we expect 18 to be at the front, which is
great because that's exactly what we got after doing.
But one method that we can use is called peak.
And what peak is going to do is very similar to pop.
But instead of us getting a returned value and the excuse me, instead of the value being removed from
the binary heap.
Peak is going to leave the value there for us, but it's still going to return that value to us.
So when I run this now, I expect 18 some 18 to be returned.
So peak is going to return an option T and then obviously return none if empty or some T otherwise,
which is great because that's exactly what we got.
But just to make sure that the value wasn't removed, let's print out our B heap again.
And great, we still see that 18 is at the front.
So again, the difference between peak and pop is that peak is going to leave the the value inside the
binary heap, whereas pop is going to actually remove the value and return it to us.
So again, a binary heap is not too difficult, but this collection is useful in cases where we care
about priority.
So as we add values in the higher the priority, obviously it will be at the front of the binary heap,
which is going to allow us to take care of whatever has the higher priority first.