• 1 day ago
Transcript
00:00Now when we have understood logic, let's discuss why I don't implement heaps in a node class. The first question
00:08was when I said that the implementation is in the form of arrays. It could be that they can form
00:12normal trees like normal trees, so I can create a static class with node name.
00:17and in that node I store all the information and then like today we have binary search trees. I will
00:25define all the logic in heaps. So why don't we follow that way? Why do we implement the array? After
00:32that there is a logic. It comes from time complexity. Let's say I have to insert this node 1 or
00:39add it inside this already existing heap.
00:43I know that I have a min heap. Min heap means that the parent node is small from the child
00:49node. So if I insert one into this heap and I have called a normal class or binary trees, then
00:59I will know what I will do. I will go to each level and see where the place is left.
01:05So the amount of nodes I have in my tree,
01:09I have to go to those nodes and see where I can insert it. First of all, I will go
01:14to this node. If there are two childs, we can insert it. Then I will go to this node. Then
01:18I will go to this node. Then I will tell you that we can insert it here. So I will
01:23go to each node and see what I can insert it here. Finally, I will tell you that in this
01:28tree, I can insert this node here. So I have to insert this node here. How much time was there?
01:37It was big of end time.
01:40If I already have n nodes exist in my tree, then I have time complexity to n nodes. Worst case
01:47is to figure out where my 1 node will add. After that, there will be more time. Now, I have
01:53added 1 in the heap, but you will see that the property is bad. Min heap says that there should
01:58be minimum value parent.
02:00How much minimum for 4 and 1? 1 minimum. So, from 4, we should have 4 here and here and
02:061. Then we will compare again. How much minimum for 1 and 2? 1 minimum. So, from 4, we should
02:11have 1 here and 2 here. So, my final heap structure will look like this. In which 1 will come
02:16up, then 3 will come up, then 5 will come up, then 10 will come up. On the right side,
02:21I will have 2 and then 4 will come up. So, the heap...
02:25After inserting it, I also have to correct it. And I also have to correct it in time. How can
02:32I say it? Level-wise, level-level compare. We will compare all the notes with our parents. We are discussing
02:38this logic in detail. I am just telling you for understanding that after inserting it, we have to fix it
02:44in our heap. So what do I think? Extra time.
02:49This is a lot of time. And I have already discussed the priority use of my add. How much time
02:54should I have to have my add? I should have to have login time. Now, with login time, there is
02:59a lot of extra time. Therefore, because in the insert operations, I have to find a proper place in my
03:06tree, so we do not implement our heaps in a class. This is the only reason that we implement the
03:13heaps using arrays and array lists.
03:18Now, why does heaps visualize in the tree? Heaps is visualized in the tree because we have to show a
03:25parent-child relationship. In the parent-child relationship, I have to show that in the min heap,
03:32The parent's value is less than or equal to from your child's notes. This relationship is very special and because
03:39it is a parent-child relationship, it can show the best tree in the form. But in the form of
03:45the area, it exists to show it in the form of the area. For these trees, complete binary trees.
03:52If I want to visualize this heap in an array list, then how can I do this? I already know
03:58this is a min heap which is small 2 is 3 and 4 is small 3 is 5 and 10
04:04is small. How can I visualize this in an array? Let's give some numbers of the heap. This is my
04:121 node, this is my 2 node, this is my 3 node, this is my 4 node, this is my
04:165 node.
04:16In fact, if there is an element, then you know that it is 6. If there is an element, then
04:20you know that it is 6. If there is an element, then you know that it is 7. So, in
04:22this way, we give our elements a numbering. And I can also give my arrays a index. So, I can
04:29give my arrays a index. So, I can give my arrays a index. So, I can give my arrays a
04:32index. So, if I give my arrays a index.
04:36CBT or this heap to write in our array in the form. So what do I do? Which I had
04:41given 1, which I had stored in index 0. Which I had given 2 to index 1, which I had
04:47given 3 to index 2, which I had given 4 to index 3, and which I had given 5 to
04:52index 4. Which I have given the number of my node, which I have taken minus 1 to index. And
04:57I have stored in there. So this node 2 will be stored here.
05:013 node store, then 4 node store, here 5 node store, here 10 node store, here. Now, when we have
05:08complete binary tree or a heap, visualize a array list or array form, there is a special property. And this
05:15special property is defined before. This property says that if there is a node that the index is equal to
05:22i, then I can say that its left child's index is always
05:282i plus 1 will be 2i plus 1 will be 2i plus 1 will be 2i plus 2 will be
05:342i plus 1 will be 2i plus 2 will be 2i plus 2 will be 2i plus 2 will be
05:352i plus 2 will be 2i plus 2 will be 2i plus 2 will be 2i plus 2 will be
05:382i plus 1 will be 2i plus 1 will be 2i plus 1 will be 2i plus 1 will be
05:422i plus 1 will be 2i plus 1 will be 2i plus 1 will be 2i plus 1 will be
05:442i plus 1 will be 2i plus 1 will be 2i plus 1 will be 2i plus 1 will be
05:472i plus 1 will be 2i plus 1 will be 2i plus 1 will be 2i plus 1 will be
05:482i plus 1 will be 2i plus 1 will be 2i plus 1 will be 2i plus 1 will be
05:492i plus 1 will be 2i plus 1 will be 2i plus 1 will be 2i plus 1 will be
05:552i plus 1 will be 2i
05:571 left child, 2 right child. If 2 is index 0, then 3 is left child which is 1 and
06:044 is right child which is 2. If this element 2 is talking about 2, then left child is left
06:11and right child is left. Let's take an example. If I talk about index 3, then index i is equal
06:21to 1.
06:22in which element is 3, so left is 2 into 1 plus 1 which is 3 and right is 2
06:29into 1 plus 2 which is 4. So index 3 is my node 5, index 4 is my node 100.
06:37This is 5, this is 10, so left child is 5 and right child is 10. So this is a
06:44fixed property.
06:47If my parent node comes, I can remove left child's index like this or right child's index like this. So,
06:55using this property, we can use our parent-child relationship with the heaps. We can also implement it in the
07:03form of trees. We can replicate this relationship with each node. We can find out the left child's index and
07:13the right child's index.
07:15Now I know that whenever I have to insert any element, if I had to insert a tree in a
07:20tree, if I had to add or insert, then what would I have to do in the tree? Level wise,
07:28I would have to find out the last position in which I would have
07:32linear time, but here, the last position in the array is the easiest way to find out the last position.
07:37I don't know how many of the array is in the last position. In fact, I will talk about the
07:42array list. If this is my array list, what is the last position? We will create it. Simply, I can
07:49write ARR.add and add your value or your data.
07:54I can add it. What will happen? This data is by default in the last position. And how much time
07:59does it look? I think time is big of 1. I mean constant time. So I was doing more in
08:05the work of time complexity. I will do the work of insertion and addition in constant time using my ArrayList.
08:12This is why my heaps are in code in the form of ArrayList. Because in ArrayList we can simulate this
08:18relationship with parent-child. We can replicate it using this.
08:23property. This property is very important. I want to remember this too. In fact, this is not just heaps or
08:29heaps. There are also many logical questions that are used in which I have to visualize the tree in the
08:33form of an array. So, this is very important. In fact, many students do what do they do? Many students
08:40make an array list or an array. There is another parallel implementation in which the index of zero, we always
08:46keep minus one. That means we keep empty. And what else do we do? One, two, three.
08:524, we take index from this way. So, when we give heap number 1, 2, 3, 4, 5, we start
08:58with number 1, 2, 3, 4, 5. If you want to implement this, what will happen in this case? No
09:04change. If index is i, then left child will be 2 multiplied by i, and right child will be 2i
09:12plus 1.
09:13So there is a little change in your relationship. If you want, you can also implement it in this way.
09:18But in class, because we will follow a little simplicity, we are going to start with 0th index and we
09:24are going to numbering from 0th index. So this same relationship will replicate and we will use these formulas in
09:30class.