• 10 hours ago
Transcript
00:00Next just function ko muchhe implement karna hai ye hai to delete in a heap. Mera agar heap kuch aisa
00:05dekhta hai, ye hai mera eek min heap. Uska array list ka visualization kuch aisa dekhta hooga. Jis meh yahan
00:13par mein index dhe deeti ho, this is zero, this is one, this is two, this is three, this is
00:17four, this is five. Aur ye ho gai mera array list mein zero, one, two, three, four and five. To
00:23meera joh delete wala step hai, wo eek three step process hota hai. Pehla step is process ke anndar hota
00:29hai ki mein aapne first,
00:30And I will swap my last node. I will swap my last node.
00:39This means that the tree of this node will swap these nodes. So, here it will be 6 and here
00:45it will be 2. I will remove this value and I will remove this value. In the last index, the
00:53complexity of the last index is constant. So, I can remove my last index. I can remove my last index.
01:00If I delete the minimum element in the heap, I will delete the minimum element in the heap.
01:08simply what do I do? I have swapping after my minimum element is last index. In the last index, the
01:15second step will be delete last index. Which method we use? This is my remove method. So, I simply do
01:28ARR.remove which I pass to ARR.size
01:34So my last index will remove the element in the array list. Remove means that my element completely removed from
01:41here. So this element is deleted and my array list now looks like this. Here from tree, my last node
01:49was completely deleted from here.
02:18My third step is to fix my heap.
02:23And for this heap, we use a special type of function which we call heapify. This heapify function we are
02:30going to write ourselves, but its logic is fixed. We can fix the heaping function from the help of the
02:35heap. We can bring it back into the original or in the right form. How does heapify work? Let's take
02:43an example. Let's say that I have
02:46This is a heap which I have to fix. This heap is broken and I have to fix it. So
02:52we call for heap 5. We call for root. Heap 5 calls and we call for root. The root index
02:59is i. So here my root index will be i. This will be 0, this will be 1, this will
03:05be 2. So how do I see my array list? My array list looks like this. In which 0 index
03:09is 5, 1 index is 3, 2 index is 4. Now what will I do? I will remove root first.
03:14Which will be 2i plus 1.
03:17If i is my root, then it will be 2 into 0 plus 1, 1. And the right child will
03:24be 2i plus 2, which is equal to 2. So what can I say? In the array, this is left
03:31child and 5 is right child. Now, heapify is such a work that heapify root is left and right. I
03:39will compare the values on these three indexes. And I will remove the minimum from them.
03:46Minimum of my root. Minimum of i, 2i plus 1 and 2i plus 2. Root left and right, which will
03:57be my minimum. I will create my parent.
04:02What will I do? I will take a minimum variable in which I will put the root index. What is
04:07the minimum? It is 5. First, 5 is 3 compared with 3. Which minimum is 3? Minimum. Then, the index
04:13index will be 1. Then, I will compare this index with 4. When I compare this index, then minimum is
04:193. Minimum is 3. Minimum is 3. Minimum is 3. Minimum is 3. Minimum is 3. Minimum is 3.
04:29So, I took the parent in 3 and made the child 5. So, whatever the minimum will come, we will
04:34swap the root with that. So, what happened? This heap fixed here. Swapping means here my 5 went away and
04:42here my 3 went away. Now, my 5 went here and 3 went here. So, 3 left child became 5
04:47and 3 right child became 4. So, now my heap fixed here. How it fixed? The minimum is up here,
04:533. Now, the parent became 5 left child and 4 right child.
04:58So this is my heapify process. What is the heapify process? We compare the left and right child to your
05:04root and remove the root. Whatever the minimum comes, the value of the root will swap. Last step is to
05:10swap with root.
05:14So, my heapify is a recursive function which will call every time in the beginning, for the root, for the
05:19child and for the child, so that we can fix all the tree from above. So, I will summarize it
05:26first. First, I will delete the first node with the last node. Then I will remove the last node. Then
05:33I will call the heapify for my root. Then I will write code and then we will dry run the
05:41whole process. I already have a heap class in my heap
05:44Add function has been created. Peek function has been created. Now, I have created the element. This is the 0th
05:50index element. I am going to delete it. Public. I will return it. I will give it the name of
05:57remove. Now, what will happen in remove? First, I will remove the data which I have to remove. Which is
06:04my array.get 0. Now, what was my step 1? Swap first and last.
06:13So I will take a temporary variable which will store first. Array.get0. Then what will we do? Array.set
06:200th index. We will add last value. Array.size-1. Array.set Array.size-1. I will store my temporary
06:34value on last index. So my step 1 is complete. Step 2 is complete. Delete last.
06:44Last but now my minimum element is gone, so I will delete it. So, ARR.remove, which I will pass
06:50in which I will pass in ARR.size-1. So, I have passed the last index, so the array list
06:55in the last index will be deleted. Now, I have to perform step 3. Step 3 is heapify.
07:04Let's write a code for Heapify. Heapify is a private function within our heap. No one could use it outside.
07:10But yes, my heap class will be able to call it. So I will create it private. Private. Void. Heapify.
07:18Heapify's work is to fix your heap. So Heapify takes index to fix the heap. So it has index IDX.
07:27I can call it IB. For my index I will be left left. 2 into I plus 1.
07:33And my right child will be 2 into i plus 2. I take another index. Minimum index. Minimum now is
07:41what? Minimum is my root. In minimum heap, we always assume in min heap that my root is my minimum
07:49value. That's why I store min index. First, I will compare this min index to left child. Then, min index
07:56to right child. Whatever it is, I store min index to min index.
08:03is my left less than my array size? Sometimes what can happen is that this is my i, my leaf
08:11node. Leaf node is not left child or right child. So leaf node, if you consider me, I have called
08:18for the last index. So left will be such a value that exists in the array list. Right will be
08:24such a value that exists in the array list.
08:28If I take left and right to this node 10, then it will be left, its index 2 into 4
08:35plus 1, this value is equal to 9, and right will be 2 into 4 plus 2, this value is
08:41equal to 10. Now, what do my array list in 4 after index 9 and index 10 exist? No.
08:49there will not be left child or right child. So the left value will come. I have to check whether
08:54this value exists in array list or not. Or I am trying to access a leaf node of left child
08:58or right child. Therefore, I have written a line for comparison here. Left is less than array.size and if
09:07my array.get minimum index value greater than array.get my left. If left value is less than minimum index,
09:18then the minimum index will be left.
09:21We will check this same thing for right. If my right is less than array.size and my array.get
09:29minimum index value, if greater than, array.get my right. So what will happen in that case? If the right
09:36value is less than minimum index, then we will store the right. Minimum index is equal to right. So in
09:45this way, the maximum maximum of my i
09:48from my left and my right. Now it is stored in my minimum index. Now I will check if my
09:55minimum index value is i or equal. If we think that anything we have removed, we have removed a new
10:01node and our heap is not bad. We don't need to fix it. If the heap is right, then the
10:07root will be minimum. If the root is minimum, then there is no problem. If the root is minimum, then
10:12we will fix it. We will do swap. First of all, we will swap values.
10:18int temporary is equal to arr.get i array.set ith index per set kardienge hum array.get minimum index
10:29hi value and array.set minimum index per set kardienge hum temporary ki value.
10:37So, which I have swapped? I have swapped the root with the index, which is now the minimum index. So,
10:43this is my swapping. Now, it can be possible that in the heap, I have fixed this node, but because
10:48of this, there is no one wrong node. So, I will call the heapify for my minimum index. I have
10:56changed the value to the index. Now, it will be another call for the index. Let's do this save. And
11:03here, I will call the heapify.
11:06For my root and last, I will return my data. I will call 0 at value. 0 will be 0
11:14at my root.
11:16I will summarize this a little bit. If I want to remove any data, first step was swap first and
11:22last. Then delete last. Third step was call heapify for root. Heapify's work is to fix your heap and fix
11:31your right heap. And last, which I removed, I will return. Heapify's work is to remove any root I. First,
11:39I will remove left and right child. Then I will assign minimum index value to I.
11:45Let's see if the leaf node is bigger than the left area. Then I will check that if the minimum
11:51index value is greater than my left index, then the minimum index will be left. If the minimum index value
11:57is greater than the right, then the minimum index will be right. If the minimum index value changes, then swap
12:03things to the right. Then we will call minimum index to the minimum index. We will try to dry run
12:10this in a heap.
12:12What I have done is that I have taken the same heap in which I have deleted my last node.
12:17After I have deleted my last node, my heap looks like this. Now I have to fix this heap. First
12:23of all, I will call the heapify for my root node, i is equal to 0, i is equal to
12:300 here. To fix this, I will remove left, left will be 2i plus 1, right will be 2i plus
12:372,
12:38Now I will compare these three. Which one will be minimum value? Minimum value will be 6, 3 and 4
12:46will be 1. What will I do? Minimum index will be 1. Minimum index will be 1. Minimum index will
12:52be 3. Minimum index will swap this value with root. Minimum index will be 1 and will swap this value
13:02with 6.
13:05Now 3 will go here and 6 will go here. This means that 6 will go here and 3 will
13:11go here. Now 3 will go here and 6 will go here. Now who will compare it to 6? I
13:16will compare it to minimum index. Minimum index was 1.
13:22Now here is the value of 6. Now I need to call this 6 for heapify. What will I pass?
13:29I will have 6 index. 6 index is 1. Left will have 2 into 1 plus 1 and right will
13:37have 2 into 1 plus 2 is 4. Left will have 3 and index will have 5 and right will
13:44have 4 value is 10.
13:46This is in the diagram that the left child will be 5 and the right child will be 10. Now
13:53I have to compare these three, which will be minimum index. Finally, after comparison, which index will be 5. That
14:03means index 3 will come. So what do I have to do? I have to swap these 3 with root.
14:10So here I will take 6 quotes and here I will take 5 quotes and here I will take 5
14:14quotes and here I will take 6 quotes and here I will take 5 quotes and here I will take
14:215 quotes and here I will take 6 quotes and here I will take 6 quotes and here I will
14:26take 6 quotes and here I will take 6 quotes and here I will take 6 quotes and here I
14:28will take 6 quotes and here I will take 6 quotes
14:34Now the value of i will be 3. What will be left? 2 into i plus 1, meaning 7. What
14:40will be right? 2 into i plus 2, meaning 8. Now if I see 7 and 8,
14:44So where are 7 or 8 in this area list? It doesn't exist in this area list, it doesn't exist
14:48in this area list. This means that my root was in index 3, meaning that 6 is a node, this
14:54is my leaf node, which left and right child is null, doesn't exist in this case. So in this case,
14:58I don't need to compare in this case. In this case, my minimum index will be this value. So now
15:05my final tree, what is fixed? So what did Heepify? What did Heepify? The whole tree?
15:11from top down approach to the top down approach. And here the minimum element has become the top of the
15:19heap. So the zero hit index is the minimum element of the heap data structure. So this is how the
15:26heapify works. What do we do? Call for root, then there is swapping, then call for the next node, then
15:33call for the next node, then call for the next node. This is how the heapify works.
15:39How many levels have we traveled? Worst case, we will travel all all levels. If there are n nodes in
15:47this tree, we will travel all the levels.
15:52Heapify, the time complexity has become big of login. In the delete function, there are three steps. The first step
15:59is to exchange the first and last. The second step is to remove the last element in the array list.
16:05That is constant time. The third step is to call the heapify. And heapify takes login time in itself. That's
16:11why my delete complexity has heaps. That is my big of login time complexity.
16:19Now, let's execute the code for this. What I have done in my heap? Already add function has been created,
16:25peak function has been created for heapify private function and has been created for remove. I have made another function
16:31that checks whether my heap is empty. So, public boolean is empty. I will return ar.size equal to 0.
16:44If the array list size is equal to 0, it means that my heap is empty, then it will return
16:48true, then it will return false. In the remove function, we are going to change a little bit. I have
16:55swapped the first and last index values. I have written the last index. I have not stored the last index.
17:01I have to store the value here. I will write array.get. The value of the last index will be
17:10stored in 0, and the last index will be stored in 0.
17:14This is a small change and a small change in the add function. In the add function, I am adding
17:20in my last index. Then, I am taking the child and parent pointer. Then, I am swap values. After swap,
17:26I have to update those pointers because there is a while loop. Now, the parent and child will change. The
17:33new value of the child will be equal to the parent index. And the parent index will be equal to
17:37x-1 divided by 2 is equal. So, my loop will continue. Let's do this.
17:43What will I do in the main function? I will create a heap. h is equal to new heap. What
17:51should I do in h? I will add some values in h.add , h.add , h.add ,
17:58h.add , h.add , h.add , h.add , h.add , h.add , h.add
18:04, h.add , h.add , h.add , h.add , h.add , h.add , h
18:05.add , h.add , h.add , h.add , h.add , h.add , h.add ,
18:05h.add , h.add , h.add , h.add , h.add , h.add , h.add
18:06, h.add , h.add , h.add , h.add , h.add , h.add , h
18:06.add , h.add , h.add , h.add , h.add , h.add , h.add ,
18:07h.add , h.add , h.add , h.add , h
18:18Let's save it and run it. So, the elements that have come in ascending order are sorted. How? When I
18:29added 3, 4, 1, 5, I added it first, it will come out of min heap. Min heap came out
18:34of minimum, then 1 came out. After that, 3 was minimum, then 3 came out. 4 was minimum, then 4
18:38came out. 5 was minimum, then 5 came out. Now, if I simply change my heap name, I'll PQ everywhere.
18:45I've also PQed it. I've also PQed it.
18:50I have also put PQ here and I will write PQ and everywhere we will know that what we are
19:00doing. We are doing this.
19:05I want to give it a heap or a PQ. What did it do? What did it do? It did
19:10a priority queue. I can say that my priority queue is already built in the Java collection framework. I had
19:18used it directly in the first video. How does it implement? It is implemented in the heap data structure. In
19:25which all the functions are the same. Output is the same. Input is the same. These functions are implemented in
19:33a heap.
19:34Now here, if I have to convert the same heap in max heap, then I have to change the logic.
19:40Where do I change the logic? In the add, the logic will be greater than. In the heapify, I had
19:49less than what I had done, this will be greater than. So in this way, this will be greater than.
19:56In this way, this will be greater than. This is simple.
20:00I will reverse my code in three places. I will save and run this once again. Once I have my
20:09output, I have my elements in descending order. The largest max heap was 5, 4, 3 and 1. All the
20:18elements have come. Now you can change a little bit of max heap. This is the minimum index. We have
20:24to name the maximum index.
20:27So we have to do a small change. We will give it the maximum index name. And I will change
20:313 parts. 2 parts are Heapify and 1 parts are Add function. And I will convert MinHeap into MaxHeap. So
20:39we implement Heap Data Structure using Java. Now here we will notice that the priority queue or heap is sorted
20:49order.
20:52The process is called Heapsort. Heapsort is an algorithm in the form of Heapsort in the form of Heapsort. So
21:01when we use Heapsort in the form of Heapsort, when we get out of the data, then heaps in sorted
21:06order. Heapsort's time complexity is equal to n log in.