Hey everyone, just wanted to point out a small mistake in the code demonstrated in the tutorial. In the heapify function, when checking the conditions for left and right children, it should be leftchild
I think there will be one more condition in deletion for checking that if left is also greater than right or if right is greater than left in else if. One more thing when we are checking the bound in that it should be less than equal to. The correct code for deletion will be void deletefromHeap() { if(size==0) { cout
You men, nailed it, i have started and left multiple time DSA just because i i did not find the way i was looking to be taught but you men made it exactly what i was looking empowering to write code yourself just by the concept explanation, the barrier of language does not comes into picture once the concept is rock solid. Hats off.
25:02 Deletion logic me you swapped with whichever of the two children are greater. 28:30 Code: compares with either left or, later, right. If left one is greater (even if not greater than right) then that case will run. Suppose tree was 52 53 54 50, then code would make it 53 52 54 50 and not 54 53 52 50. Koi baat nhi hume logic samajh aa gya👍
for the second one , dono me if statements use hue hai, so agar pehla wala true hota hai, to pehla wala largest hoga, uske baad wo dusre wale k saath pehla compare hoga because wo already largest ho chuka hai,
ye jo tree hai 52 53 54 50,, ---> ye kisi heap property ko follow hae nahi krrha nither minheap nor maxheap toh isko pehle ek heap property ka tree banao, scondly haan there is error in thr code for deletion. we need to compare leftchild and rightchild before saying who is the greatest of them
I think #LoveBabbar did a slight mistake while writing the deletion method. We should do some slight modification in that code... void deleteRoot(){ if(size==0){ cout
it feels good to continue watching the whole series and completed this many lectures thank you bhaiya for providing us this amazing series free of cost.
Maza aagya bhaiya... Thanks to this course... Ab contest me bhi questions solve ho rhe hai... 3 months pehle he coding seekhni start ki thi aapke saath... Thanks again !!!!!❤❤❤❤
bhaiya... 1 saal baad is video dekhne aaya hoon.. kuki bhul gya hoon heapify function But ye wala look me aapko firse dekh ke maza aagya sachi bata rha hoon 😅😍😍
I can't thank you enough bhaiya for this amazing series.. this series is helping me a lot to polish my skills. You are a great teacher and blessing to us ⭐
your content and explanation just awesome bhaiya. Some guys just notice one mistake and keep barking abt that without knowing how much effort you put for one lecture
Babbar bhai thanks a lot. Mujhe kabhi nahi laga tha mai array k alawa kuch samajh bhi sakunga but after watching your videos I can't believe I have started solving Hard level DSA questions on leetcode. Thank you so much brother.
Ek no. bhiya we dont want animation but the quality content.....my concept getting stronger day by day....i will watch more 2 videos of heap question and please upload hash in upcoming week so that i can study that topic.
Correct Deletion Logic: int index = 1; while(index < size){ int leftIndex = 2 * index; int rightIndex = 2 * index + 1; int swapIndex = index; if(leftIndex
In deletion , we are not firstly comparing left and right child to find which is max ,we are simply uisng if else, if left greater than a[i] , we are swaping a[i] with left and due to if else , we are not comparing a[i] with right child , so little confusion in deletion code
@@KazHachiOreki You are wrong bro there is problem in deletion : After deleting first element if parent is 30 leftchild 40 rightchild 50 and according to code we are swapping with leftchild then parent 40 leftchild 30 rightchild still 50 (here rightchild is greater than parent which is not a heap) so we have to take max of left and right child before swapping ; please clarify if i am wrong ;
yes to my understanding the parent node should swap the largest value among left and right child. Thus a comparison b/w left and right child value is necessary
I learned all topic from start this dsa course very help full beginner level to high level because small concet point to point understand by love babbar bhaiya so again thanks bhaiya for free course . this best explanation for heap topc in c++.
It seems there is an issue in the deletion function. It is only checking either the left or right child, whereas it should check both the left and right children, similar to how it's done in the heapify function.
45:02 // H/W - How function - buidHeap takes O(N) why not O(Log N) as it is n/2 Lets Dive into code , as during each Iteration the , heapify function takes O(Log N) time , so when we call n/2 each element takes O(Log N) time till it reaches 1 as O(Log N) + O(Log N) + O(Log N) ------- 1 which boils Time complexity as O(N) for buidHeap() Function Hope you got it
in insertion short if you add provided code in last of while loop then it will give correct answer , right now if you try sir's code with insertion of - 50 then 52 then 53 then 54 then 55 then you definately get wrong answer , here is something i want to add if(index%2 ==0){ if(array[index] > array[index+1]){ swap(array[index] , array[index+1]); index++; } } if you add this at the place as told then your code will correct answer iin any case
Bhaiya deletion thoda wrong lag raha hai. Let's say right child bada ho left child se to uss case mai ham leftchild index se swap kar rahe hai and index to aage nikalke ja raha hai so max heap ki property satisfy nahi karega na . So pahle ye check karle ki left and right mai se konsa bada hai and then swap kar le to ho jayega. E.g. int largeChild = arr[leftChild]>arr[rightChild] ? leftChild:rightChild Then only 1 condition if ye largeChild index se bada hai to swap else return Amazing content bhaiya. Maja aa raha hai...
I think there is an error in the deletion part. We are first checking the left then right. But what if right part is maximum? Try making a heap this way and you'll understand: 50 55 54 53 53 upon deletion, we get : 53 52 54 50: 53 becomes root and 54 comes later...
there is no problem in sir code it is 100% correct listen if there is no need to swap in left then we swap in right side then this means we have to forget the left subtree for further sorting because there is no need and vice versa and this continues until all conditions fulfilled and then return if you dry run you will notice this:) believe me just dry run
At 25:30 the deletion code may not work for other input values (like : 50,60,70,80,90,100). Another if condition is required to find the larger value between arr[leftIndex] and arr[rightIndex], for this use : void Delete(){ if(size < 1){ cout
28:32 there is small correction in the code that is if(leftIndex < size && arr[i] < arr[leftIndex] && arr[leftIndex] > arr[rightIndex]) in line 57 The reason : we need to compare the left and right children before swapping that's why we added another conditional " arr[leftIndex] > arr[rightIndex]" and there is no need to add "arr[leftIndex] < arr[rightIndex]" in else if part.
00:03 Heaps in C++ and Heap Sort 03:27 Understanding the structure and composition of a heap 07:14 Heap property in C++ 09:27 Understanding insertion and deletion in a heap 15:44 Understanding and implementing the Inverter function 17:50 MS Word printing and setting pins 22:17 Deletion in Heap 24:20 Explanation of heap sorting and insertion/deletion in heap 29:46 Lecture 74 discusses Heaps in C++ including Heap Sort, Insertion/Deletion in Heap, and Priority Queue STL. 32:23 Understanding the concept of heaps in C++ 36:45 Explanation of shifting inside a heap and its significance 38:44 Heaps in C++ and Heap Sort 43:14 Explanation of the heap sort algorithm 45:37 Heap Sort in C++ 50:11 Heap Sort is a sorting algorithm that utilizes a heap data structure. 52:22 Heaps in C++ 57:23 Heaps in C++ 1:00:09 Understanding priority queues and heap sort in C++
For the deletion code mistake, being discussed in the comments: Simply replace the if -- else if -- check with two ifs -- and check if "i" was modified or not. (0-based implementation) while( i < vec.size()) { int original_index = i; int left = 2*i + 1; int right = 2*i + 2;
// The first check is important since we might encounter an (internal) leaf node if( left < vec.size() && vec[left] > vec[i]) { std::swap(vec[i], vec[left]); i = left; } if(( right < vec.size() && vec[right] > vec[i])) { std::swap(vec[i], vec[right]); i = right; } if(i == original_index) // Everything is stable return;
attendance ++ aandd consistency ++,aur bahot mazha araha hai,completed 160 questions on leetcode since january 1
Hey bhagwan jii babbar bhaiya jesa dedication aur consistency dedo apne goal ke liye 👌🙏🔥
Self motivated rkho then all goals you achieve 🤞
Ekdum bro
Milegii bhi😀
Ye bhai tumne dil ki baat chhin li
nazar lagadi tum logo ne ab video hi ni dalpate bhaia😂
in deletion there should one condition should also mention arr[leftIndex]
yepp,this code is right bro
thanks a lot you too bhaiya
exactly.... !
if you use if statements only instead of if and else if then you will get the correct answer
Thank you buddy :)
Hey everyone, just wanted to point out a small mistake in the code demonstrated in the tutorial.
In the heapify function, when checking the conditions for left and right children, it should be leftchild
I have started advanced dsa so many times from different sources and channels but this time the understanding is of next level thanks a lot bhaiya
I think there will be one more condition in deletion for checking that if left is also greater than right or if right is greater than left in else if.
One more thing when we are checking the bound in that it should be less than equal to.
The correct code for deletion will be
void deletefromHeap()
{
if(size==0)
{
cout
leftIndex
I do agree 👍👍
Bcoz without equal sign we will miss the last child bcoz we have already decremented the size above in the code
@@HridoyHasan-j5b
One more correction when we are checking leftindex
Even this is not going to work in case of example :- 10 7 6 5 2
I never enjoyed coding ...but after following your course ..now icoding becomes my habit .thanks a lot bhaia
Eagerly waiting for this!!! Thank you so much Bhaiya for covering this topic ♥️💯💯
la jawab! simply magnificent! boht hi nazakat se ek jattil vishya ko suljhaya!
You men, nailed it, i have started and left multiple time DSA just because i i did not find the way i was looking to be taught but you men made it exactly what i was looking empowering to write code yourself just by the concept explanation, the barrier of language does not comes into picture once the concept is rock solid. Hats off.
25:02 Deletion logic me you swapped with whichever of the two children are greater.
28:30 Code: compares with either left or, later, right. If left one is greater (even if not greater than right) then that case will run.
Suppose tree was 52 53 54 50, then code would make it 53 52 54 50 and not 54 53 52 50.
Koi baat nhi hume logic samajh aa gya👍
Yes
yes
for the second one , dono me if statements use hue hai, so agar pehla wala true hota hai, to pehla wala largest hoga, uske baad wo dusre wale k saath pehla compare hoga because wo already largest ho chuka hai,
*DELETION CORRECT CODE WITH EACH EDGE CASE
void dlt ()
{
if ( size == -1 )
{
cout
ye jo tree hai 52 53 54 50,, ---> ye kisi heap property ko follow hae nahi krrha nither minheap nor maxheap toh isko pehle ek heap property ka tree banao, scondly haan there is error in thr code for deletion. we need to compare leftchild and rightchild before saying who is the greatest of them
correction in the deletion method - compare the leftIndex and rightIndex, then swap the greater one if it is greater than parent.
correct bro
can u pls send me the correct code
@@suratnu4609 void deleteFromHeap(){
if(size==0){
cout
@@rajeevkumaryadav6948
// Simpler one
void deleteh()
{
if (size == 0)
{
cout
@@ashhariqbalshanu thanks for help bro
Best DSA playlist ever, thank you so much for helping us.😍😍❤❤
Itna achy sy Heap ko sari youtube py kisi ny nahi samjhaya...You are great Sir. Thanks for this amazing series.
Love you babbar bhai....... Ham Garibo ke liye IT MEANS ALOT
I think there's a mistake in Heapify Algorithm, the right condition should be right
index is start from 0 so given ans is correct.
yes. I just discovered it myself also. Glad to see someone also been practicing and finding errors also.
@@AtulSharma-sr9wi starting index is taken 1 from the beginning of the video, were u on weeds while taking the lecture ?
Thanks for pointing out bro
//For those who are struggling with deletion code, this is the correct one
//The problem is with the condition (index
great job bruh!
if left==size then right==size+1, so arr[right] should not be compared with arr[left]
still wrong
it is wrong.
heap wala code bhi galat hai kya bhai ?
You guys are the ones, for whom we cse students can survive.
I think #LoveBabbar did a slight mistake while writing the deletion method. We should do some slight modification in that code...
void deleteRoot(){
if(size==0){
cout
i was also stuck at this
Thanks u r the saviour
thank you later . uwuw
still wrong, in modification 1's if condition you are accessing arr[rightInd] without checking rightIndex
Literally you save my time, thanks❤❤
while(codehelp==babbar){
learning ++;
cout
it feels good to continue watching the whole series and completed this many lectures thank you bhaiya for providing us this amazing series free of cost.
Are you able to solve leetcode questions?
consistency again high hai bhaiya dono ki.........thanks.
The best explanation of heaps out here in C++. Such great content !!!
Maza aagya bhaiya... Thanks to this course... Ab contest me bhi questions solve ho rhe hai... 3 months pehle he coding seekhni start ki thi aapke saath... Thanks again !!!!!❤❤❤❤
bhaiya... 1 saal baad is video dekhne aaya hoon.. kuki bhul gya hoon heapify function
But ye wala look me aapko firse dekh ke maza aagya sachi bata rha hoon
😅😍😍
I can't thank you enough bhaiya for this amazing series.. this series is helping me a lot to polish my skills. You are a great teacher and blessing to us ⭐
Literally sometimes I feel u r god behind this face.... Thnkuu so much for all your teaching 🙏🙏🙏🙏
Thanks brothers , Our bangladeshi Coders really depends on the guys like you
I never enjoyed coding ...but after following your course ..now icoding becomes my habit .thanks a lot bhaia #love babbar
Laal phool pila phool Love bhaiya beautiful. Jokes apart tbh I really think he is such a cool person. Amazing explanation. Easy to understand.
amazing video bhayya
Learnt:
1.insertion
2.deletion
3.heapify or build heap
4.sort heap
5.using stl priority queue >>max heap and min heap ;
your content and explanation just awesome bhaiya.
Some guys just notice one mistake and keep barking abt that without knowing how much effort you put for one lecture
I don't have words to express about ur dedication & hardworking. Tusi great ho great babar bhaiya
Babbar bhai thanks a lot. Mujhe kabhi nahi laga tha mai array k alawa kuch samajh bhi sakunga but after watching your videos I can't believe I have started solving Hard level DSA questions on leetcode. Thank you so much brother.
29:16 it's better to keep leftIndex
yes, actually. I don't understand why he's isn't checking those cases. He wrote it wrong?
Exactly
wahh bhiyaa mza aa gya ekdum jabardast...subkuch samaj aya mereko......thanks bhiya love u
the way you explain things is great because you cover all the details in your lecture.
THANKYOU SO MUCH BHAIYA FOR THE AWESOME VIDEOS,SAB SMZH AA GYA Attendance ++ and Consistency ++
I love you babbar bhaiyya... doing heap revision and still everything is crystal clear...
Ek no. bhiya we dont want animation but the quality content.....my concept getting stronger day by day....i will watch more 2 videos of heap question and please upload hash in upcoming week so that i can study that topic.
Welcome back bhaiya with great consistency
Right now I am watching your 74th lecture , Tq for this course Bhaiya 🤘
The BEST explanation of heaps on the internet, Great video
really , i have watched almost all the available videos on heap but you explained everything about heap and in great way , thankyou for this effort
is heap structure is created only using arrays ? no need to create nodes like in tree ?
Correct Deletion Logic:
int index = 1;
while(index < size){
int leftIndex = 2 * index;
int rightIndex = 2 * index + 1;
int swapIndex = index;
if(leftIndex
can anyone help me to get his github code after lecture no.65 plz?
AND I CAME TO CORRECT IT , BUT FOUND U HERE🙃🙂
@@130anirudh4
i am also 😶😶
delete wala portion galat he]
ur code is also wrong
In deletion , we are not firstly comparing left and right child to find which is max ,we are simply uisng if else, if left greater than a[i] , we are swaping a[i] with left and due to if else , we are not comparing a[i] with right child , so little confusion in deletion code
yes its not right you run for other test case. use heapify function instead to put new root at right position'
@@KazHachiOreki You are wrong bro there is problem in deletion : After deleting first element if parent is 30 leftchild 40 rightchild 50 and according to code we are swapping with leftchild then parent 40 leftchild 30 rightchild still 50 (here rightchild is greater than parent which is not a heap) so we have to take max of left and right child before swapping ;
please clarify if i am wrong ;
yes to my understanding the parent node should swap the largest value among left and right child. Thus a comparison b/w left and right child value is necessary
you are the best teacher Bhaiya ,Really Your video is awesome
A like is not enough justify the efforts and quality of this video. Thanks.
The way you teach is unstoppable. you are amazing teacher💯💯❤🔥❤🔥❤🔥
present marked sir ,getting motivation from your consistency level, aag laga diye bhaiya aap..
I learned all topic from start this dsa course very help full beginner level to high level because small concet point to point understand by love babbar bhaiya so again thanks bhaiya for free course .
this best explanation for heap topc in c++.
Awesome Explanation sir ji.. No one has covered these advanced topics so bautifully.😇
thanks luv babbar 💛 couldnt find any better way to understand heaps than this , respect++
One of the Best lecture on Heap Topic .ThankYou Bhaiya 🙂
best dsa course on yt, no comparison bro 🔥🔥
Thanks for explaining such typicall topic in such a easy way...🤩🤩
29:00 deletion logic a little change
just to check in left or right which one is greater
void delHeapEle() {
if(size == 0) {
cout
currently watching lecture 40.... Amazing series bhaiya!! 😍😍🙏🙏
now where re u?
why here??
Best DSA course on youtube !!👍
probably the best course for dsa in youtube thanks a lot bhaiya
tagda video ,
everything explainded very clearly with clear logic and code .
One of the finest explanations. May God bless you.
this course is the best dsa course in the country
Thank sir kl lab me que solve hai or aaj aapse pura heap padh liya❤❤❤
Great explanation and the best part is your approach towards the question, it really helped me, thanks a lot bhaiya.
It seems there is an issue in the deletion function. It is only checking either the left or right child, whereas it should check both the left and right children, similar to how it's done in the heapify function.
yess!
else if ko else krde bhai
thank you for being so great with us, you have clarified the searching technique of parent, left and right node so easily.
Bhaiya aap boring ko bhi interesting bana dete ho, your teaching is very good
First time commenting best channel for dsa
even after 6 months no had made Data Structure course like yours Babbar bhaiya.
Mja hi aa gya bhaiya aap se heap topic pdke... Thanks, bhaiya for making such an amazing course
45:02 // H/W - How function - buidHeap takes O(N) why not O(Log N) as it is n/2
Lets Dive into code , as during each Iteration the , heapify function takes O(Log N) time , so when we call n/2 each element takes O(Log N) time till it reaches 1 as O(Log N) + O(Log N) + O(Log N) ------- 1
which boils Time complexity as O(N) for buidHeap() Function
Hope you got it
Mja a gya ......such an elaborative and simple explanation
Thank you Bhaiya 🙏 aisa course dene ke liye, ab lagta hai kuch kar sakta hai aapn log.
Lecture was superb dear Thanks a lot for such amazing content!
in insertion short if you add provided code in last of while loop then it will give correct answer , right now if you try sir's code with insertion of - 50 then 52 then 53 then 54 then 55 then you definately get wrong answer , here is something i want to add
if(index%2 ==0){
if(array[index] > array[index+1]){
swap(array[index] , array[index+1]);
index++;
}
}
if you add this at the place as told then your code will correct answer iin any case
Bhaiya deletion thoda wrong lag raha hai. Let's say right child bada ho left child se to uss case mai ham leftchild index se swap kar rahe hai and index to aage nikalke ja raha hai so max heap ki property satisfy nahi karega na .
So pahle ye check karle ki left and right mai se konsa bada hai and then swap kar le to ho jayega.
E.g. int largeChild = arr[leftChild]>arr[rightChild] ? leftChild:rightChild
Then only 1 condition if ye largeChild index se bada hai to swap else return
Amazing content bhaiya. Maja aa raha hai...
Best DSA Teacher
thanq thanq thanq bhaiyaa .. majja agya isme toh .. kamaal krte ho aap..
World number 1 DSA course.
Best video for understanding heaps. Thank you so much!
Really great consistency hai sir aapki I'm not able to match till now even🌚
Ek hi to dill he kitnaa jitoge bhaiya 👌😂😂
heapify function line 88 and 91 mein correction(left
I think there is an error in the deletion part.
We are first checking the left then right.
But what if right part is maximum?
Try making a heap this way and you'll understand:
50 55 54 53 53
upon deletion, we get :
53 52 54 50: 53 becomes root and 54 comes later...
there is no problem in sir code it is 100% correct
listen if there is no need to swap in left then we swap in right side
then this means we have to forget the left subtree for further sorting because there is no need and vice versa and this continues until all conditions fulfilled and then return
if you dry run you will notice this:)
believe me just dry run
there is a problem,take example as 52 53 54 50 you will get wrong answer as:53 52 54 50@@ashutosh7944
ya you are right its wrong and incomplete
Attendance marked ...
Thank you so much for this course
your videos are really helpful... Thank you.
At 25:30 the deletion code may not work for other input values (like : 50,60,70,80,90,100).
Another if condition is required to find the larger value between arr[leftIndex] and arr[rightIndex],
for this use :
void Delete(){
if(size < 1){
cout
i think it wont work for -1,120,100,110,80,70 this heap
Attendence Marked Bhaiya...Up to dated m, And enjoying every lectures till now,,Thanks A Lot....
Corrected Code dealing all edge case for deletion in max heap:
Hope Sir corrects it in future and add it in description !
void deletefromHeap(){
if(size < 1) return;
swap(arr[1],arr[size]);
size--;
int index = 1;
while(true)
{
int leftIndex = index*2;
int rightIndex = index*2+1;
if(leftIndex arr[rightIndex]) break;
else
{
if(arr[leftIndex] < arr[rightIndex]) {swap(arr[rightIndex],arr[index]);index = rightIndex;}
else {swap(arr[leftIndex],arr[index]);index=leftIndex;}
}
}
else if(leftIndex > size && rightIndex arr[rightIndex]) break;
else
{
swap(arr[rightIndex],arr[index]);
index = rightIndex;
}
}
else if(leftIndex size)
{
if(arr[index]>arr[leftIndex]) break;
else
{
swap(arr[leftIndex],arr[index]);
index = leftIndex;
}
}
else break;
}
}
28:32 there is small correction in the code that is
if(leftIndex < size && arr[i] < arr[leftIndex] && arr[leftIndex] > arr[rightIndex]) in line 57
The reason :
we need to compare the left and right children before swapping that's why we added another conditional " arr[leftIndex] > arr[rightIndex]" and there is no need to add "arr[leftIndex] < arr[rightIndex]" in else if part.
You know you are beauty with brains!!
Hats off to dedication level of bhaiya for us.
Thanks love bhaiya😀 Placement lagne ke baad apko mere taraaf se party pakki😁
Best course for DSA
Thanku bhaiya
Sab samaj gaya sir 🔥🔥🔥
//// Modified and correct Code of deleion() function :
void delroot(){
if(size==0){
cout
00:03 Heaps in C++ and Heap Sort
03:27 Understanding the structure and composition of a heap
07:14 Heap property in C++
09:27 Understanding insertion and deletion in a heap
15:44 Understanding and implementing the Inverter function
17:50 MS Word printing and setting pins
22:17 Deletion in Heap
24:20 Explanation of heap sorting and insertion/deletion in heap
29:46 Lecture 74 discusses Heaps in C++ including Heap Sort, Insertion/Deletion in Heap, and Priority Queue STL.
32:23 Understanding the concept of heaps in C++
36:45 Explanation of shifting inside a heap and its significance
38:44 Heaps in C++ and Heap Sort
43:14 Explanation of the heap sort algorithm
45:37 Heap Sort in C++
50:11 Heap Sort is a sorting algorithm that utilizes a heap data structure.
52:22 Heaps in C++
57:23 Heaps in C++
1:00:09 Understanding priority queues and heap sort in C++
For the deletion code mistake, being discussed in the comments:
Simply replace the if -- else if -- check with two ifs -- and check if "i" was modified or not.
(0-based implementation)
while( i < vec.size()) {
int original_index = i;
int left = 2*i + 1;
int right = 2*i + 2;
// The first check is important since we might encounter an (internal) leaf node
if( left < vec.size() && vec[left] > vec[i])
{
std::swap(vec[i], vec[left]);
i = left;
}
if(( right < vec.size() && vec[right] > vec[i]))
{
std::swap(vec[i], vec[right]);
i = right;
}
if(i == original_index) // Everything is stable
return;
}