Isn't there a problem with the second if-statement? Consider the situation where right is indeed the last position, meaning there must also be a left child. If the right position is greater than parent, you always swap it, regardless of whether the left child is greater than right. In the case that the right child is last position, you should consider it as an internal node. Also, using greater than or equal to in the third if-statement is redundant, as you have already checked for equality, simply using strictly greater than is more accurate (and you will not need to check right, if a node doesn't have a left child, it wont have a right child, if it only has a left child, this is handled in the first if statement, and if it has both, it's an internal node). I would consider removing the second if statement and updating the condition of the third one to if (left > lastPosition)
Tell me, what about the statement where the parent is bigger than his childs. Where is statement for that case? // i answear that question for myself xD ( there won't be such a case )
@@potatorun3541 if right is last position, it might cause a heap violation, but that is only because of the second if-statement (consider adding a 17 as the last node in the video before trickleDown, the second if-statement will swap 17 and 11, causing 17 to be a parent to 22 and ending the recursion)
way better than my current DS class... thx professor Edward ! !
Clear explanation but you should have used just an int[] array to avoid littering the code with generics comparable and hide the essential topic.
Isn't there a problem with the second if-statement? Consider the situation where right is indeed the last position, meaning there must also be a left child. If the right position is greater than parent, you always swap it, regardless of whether the left child is greater than right. In the case that the right child is last position, you should consider it as an internal node. Also, using greater than or equal to in the third if-statement is redundant, as you have already checked for equality, simply using strictly greater than is more accurate (and you will not need to check right, if a node doesn't have a left child, it wont have a right child, if it only has a left child, this is handled in the first if statement, and if it has both, it's an internal node). I would consider removing the second if statement and updating the condition of the third one to if (left > lastPosition)
Yep. I had the same comments when was watching it :)
Glad I found this comment. I was thinking the same while watching the video
Please make a playlist for algorithm design and analysis.
We always remove root, then trickle down, but what if the value we want to remove is not in the root, is some other node?
In the third case in TrickDown function, there should be no equal because the first two cases had discussed about it
wouldn't the second if statement be better like this:
if(right == lastPosititon && array[parent] < array[right] && array[left] < array[right]){
swap(parent, right);
return;
}else if(right == lastPosititon && array[left] > array[parent]){
swap(parent, left);
return;
}
?
it's same
the remove is complicated function
Tell me, what about the statement where the parent is bigger than his childs. Where is statement for that case? // i answear that question for myself xD ( there won't be such a case )
Algorithm doesn't work, the if conditions make it exit loop early
I'm thinking the same, would the recursive call happen inside of the if loop?
the return statement in if conditions is only reached when left or right as already at last position.
@@potatorun3541 if right is last position, it might cause a heap violation, but that is only because of the second if-statement (consider adding a 17 as the last node in the video before trickleDown, the second if-statement will swap 17 and 11, causing 17 to be a parent to 22 and ending the recursion)