• 1 day ago
Transcript
00:00Now, we will learn about the heap data structure. So, as many of the priorities we have learned, they will
00:05implement all of this heap data structure. Now, the first question is, did you create trees? Where did the trees
00:15come from? Now, we were talking about the priority queues. You should have to use the queues. But actually, the
00:21heap data structure is a binary tree. And we implement the array with binary tree.
00:32So we are going to use three data structures. And this is not difficult. In fact, HEAPS is one of
00:39the most simple concepts in Java. Everything is fixed. Everything is fixed. We are going to be able to understand
00:47that what is HEAP data structure. This is not a difficult data structure. You can only see two different data
00:52structures in the beginning. Why are you reading so many?
00:59Heap Data Structure, as I have told you about it, like the data structure. What do you remember from this
01:04type of structure? I remember my binary tree. If you don't remember before, I don't remember a beginner who has
01:12read it. Probably remember it. I have two types of heaps in Java. One is my max heap and one
01:19is my min heap. What happens in max heap? If I implement this in priority queue, which is my maximum
01:27element,
01:28is the highest priority. And within the min heap, which is the lowest or the smallest minimum element, is the
01:37highest priority. So, can I say that by default, why the priority in Java becomes the min heap? Because as
01:44much as the value of the element is smaller, the priority is higher. So, there are two types of max
01:49heap and min heap. And we visualize these heaps as a tree. So, there are two things. One is to
01:57visualize it.
01:59How do we visualize in Java? We visualize heap as a binary tree. And how do we implement? We can
02:10implement as an array.
02:13This is very similar to saying that when we made a stack of two stacks, when we made a queue,
02:18then why did the visualization of the queue? I mean, when I made a queue, I had seen it. But
02:23internally, the implementation of the stack was happening in that question. When we had a single queue in two stacks,
02:28we had to implement a single queue. Something like that, the visualization of the queue is like a tree. But
02:34what would you write in code? With the code of the array of the help. Or I would say with
02:37the array list of the help. So let's understand that the heap actually
02:42What happens in the heap? The heap which is a data structure, there are three properties in it. And after
02:46these three properties, we will make notes, because it is important to remember the three properties and remember them.
02:52The first property of heap is always a binary tree, or at most two children. For example, I have given
03:00you a min-heap example. This is a binary tree with at most two children. There will be no money
03:06in which there will be three children. It can happen if it doesn't have one children. For example, it will
03:10be a leaf node. Or it will be one child or two children. But it will not be more than
03:15two. The second property says that heap is a complete binary tree. A complete binary tree has probably a new
03:22we will see in the first time, there was a binary tree. What is a complete binary tree? CBT is
03:29a binary tree in which all levels are completely filled. That means, the above levels are completely filled, then we
03:37have to fill the above levels. Except possibly the last one. That means, every node on the above levels will
03:43be given two children. After that, the above levels will be given the children's nodes.
03:48And in the last level, the nodes will be given to the left and to the right. This means that
03:54if I tried to create a binary tree structure, then the child node will be given first. Now if I
04:01want to add a new node in this tree, then where will I do it? Or I can do it
04:05here, but this is wrong. Because my upper level is empty. So what will I do? I will add my
04:11new node here. So this will be my...
04:15CBT. If I want to add one new node, I can add it here.
04:20Or I can add here or here or here. But how do I always add? I always add left to
04:27right. First I add left and then I add right. So my new node will come here. So first I
04:34will add this level above and then I will add it to the lower level. Then when I add this
04:39level, I will add it to the lower level. Then when I add this level, I will add it to
04:41the lower level. Then when I add this level, I will add it to the children's nodes. And how do
04:45I add? In the left to right order.
04:49know what my complete binary tree is. This definition is important. Complete binary tree are those binary trees in which
04:56all levels are completely filled. Except the last one. That means that the last one is completely filled. Like here,
05:04if there are two nodes here, what will happen? Completely filled. But here, because
05:10If there is no one node, this is also my CBT. The last level is not filled in it. But
05:14the above is complete and the above is complete. If you want to add it, then I will add it
05:20here. Directly right child cannot be made. First, you should have left child. For more context, let's see some examples
05:26of CBT. This tree is a CBT.
05:29First we can see the last field, the upper level is filled, then this is the last level, which is
05:35empty. First we have left node and right node, so this is my CBT, so this will be yes answer.
05:42If I talk about this tree, then this level is completely filled and this level is completely empty. First we
05:48have left node, then we should come here, the other node is not here, so this is not my CBT.
05:55If I talk about this tree, this level is completely filled here and this is completely filled. This level is
05:59completely filled here, but left to right, my nodes are added. And the last part is empty, so this is
06:05my CBT. The next thing I talk about is this level is completely filled and this level is completely filled
06:10here, but we have missed out of this node. So this is not a CBT.
06:16If I talk about this tree, this level is completely filled, this is completely filled, this is also completely filled
06:21and left to right is automatically filled. So this is my successfully CBT. If I talk about this level, this
06:28level is completely filled, but its left node is not. But we have started adding more in the lower level.
06:33If it was there, we should put it here. Therefore, this is not my proper CBT. So from this example,
06:39I hope we will clear that this complete binary tree is how it looks and how it looks.
06:45Third, I have a heap order property. Heap order property says that children nodes' value is always higher than the
06:51parent node's value in a min heap. And in max heap, children nodes' value is always lower than the parent
06:59node's value or equal. For this example, I know that this is a binary tree. This is a complete binary
07:06tree. Let me see if this is a heap order property or not. Here the parent is one.
07:121 की value 2 से भी छोटी है और 1 की value 4 से भी छोटी है तो यह node
07:16satisfy करती है फिर 2 2 की value 5 से भी छोटी है 2 की value 10 से भी छोटी
07:20है और कोई तो node है नहीं यह सारी leaf nodes है इनके कोई children है नहीं तो यह दोनों
07:24nodes तो heap order को satisfy करती है क्योंकि यह children node की value parent node से बढ़ी है तो
07:29यह मेरा एक min heap का example है
07:32But if I see my previous examples, this is max heap. In the max heap, the parent node of this
07:38child is also bigger than this node. This 4 is also bigger than 1 and 2. So what is my
07:43max heap? We usually try to use min heap in our codes. And the max heap is the same. But
07:52the comparison is a little reverse. The other logic is the same. Let's try to solve some examples and see
07:57if it is a heap or not. My first example is this.
08:01What is the value of 10? The value of 10 is 6 and the value of 10 is 5. So,
08:06this is the Heap Order. Which is the Heap? The Max Heap. The value of 6 is 1 and the
08:11value of 6 is 2. So, what is this successfully?
08:15max heap becomes a max heap. Here if I see 10's value is 5 and 8's, this is the property
08:21satisfying. But 5's value is 4 and 6's. So here the property is failed, that's why it's not a max
08:28heap. So which single max heap is my CBT?