1 00:00:06,290 --> 00:00:08,750 So now we're going to talk about a binary heap. 2 00:00:08,990 --> 00:00:13,130 A binary heap is a collection whose elements are kept loosely organized. 3 00:00:13,280 --> 00:00:16,430 The greatest value always bubbles up to the front. 4 00:00:16,610 --> 00:00:21,950 There are three methods commonly used with the binary heap and they are push, pop and peak. 5 00:00:21,950 --> 00:00:24,320 And those are going to be the three methods that we look at. 6 00:00:25,250 --> 00:00:34,280 So let's start by bringing in a binary heap into our scope by calling standard collections binary heap. 7 00:00:35,930 --> 00:00:42,290 So now we can create a mutable B heap by saying binary heap new. 8 00:00:42,770 --> 00:00:46,070 And that's going to instantiate us a new binary heap. 9 00:00:46,670 --> 00:00:52,430 And now, like what we did with a vector, we can push values onto here. 10 00:00:52,430 --> 00:01:00,200 So let's push in multiple values just to make sure that our largest value is always bubbling up to the 11 00:01:00,200 --> 00:01:00,740 front. 12 00:01:03,650 --> 00:01:09,380 So now let's print out the binary heap and just make sure that 20 13 00:01:12,620 --> 00:01:14,990 is at the front. 14 00:01:18,110 --> 00:01:19,250 That's exactly what we got. 15 00:01:19,280 --> 00:01:19,700 Awesome. 16 00:01:19,700 --> 00:01:22,880 So the behavior is working as expected. 17 00:01:24,110 --> 00:01:28,310 So the first thing you know, we saw that in vectors. 18 00:01:28,580 --> 00:01:31,100 But we can also do pop. 19 00:01:31,640 --> 00:01:36,620 And what this is going to do is it's it's going to take off the value at the front. 20 00:01:36,620 --> 00:01:44,000 So if we run it again, we expect 20 to be gone, but now we expect 18 to be at the front, which is 21 00:01:44,000 --> 00:01:46,520 great because that's exactly what we got after doing. 22 00:01:47,410 --> 00:01:51,850 But one method that we can use is called peak. 23 00:01:52,450 --> 00:01:57,700 And what peak is going to do is very similar to pop. 24 00:01:58,240 --> 00:02:12,580 But instead of us getting a returned value and the excuse me, instead of the value being removed from 25 00:02:12,580 --> 00:02:13,640 the binary heap. 26 00:02:13,660 --> 00:02:20,400 Peak is going to leave the value there for us, but it's still going to return that value to us. 27 00:02:20,410 --> 00:02:27,550 So when I run this now, I expect 18 some 18 to be returned. 28 00:02:27,550 --> 00:02:41,140 So peak is going to return an option T and then obviously return none if empty or some T otherwise, 29 00:02:41,680 --> 00:02:43,660 which is great because that's exactly what we got. 30 00:02:43,660 --> 00:02:50,230 But just to make sure that the value wasn't removed, let's print out our B heap again. 31 00:02:51,070 --> 00:02:53,920 And great, we still see that 18 is at the front. 32 00:02:53,920 --> 00:03:01,540 So again, the difference between peak and pop is that peak is going to leave the the value inside the 33 00:03:01,540 --> 00:03:07,810 binary heap, whereas pop is going to actually remove the value and return it to us. 34 00:03:08,710 --> 00:03:15,130 So again, a binary heap is not too difficult, but this collection is useful in cases where we care 35 00:03:15,130 --> 00:03:16,330 about priority. 36 00:03:16,330 --> 00:03:24,640 So as we add values in the higher the priority, obviously it will be at the front of the binary heap, 37 00:03:24,820 --> 00:03:32,260 which is going to allow us to take care of whatever has the higher priority first.