In this video, I show you how the Build Max Heap algorithm works.
Пікірлер: 90
@garic84235 ай бұрын
10 years later this is still really helpful!
@ozzyfromspace Жыл бұрын
Watched at x2 and it immediately brought the algorithm back. Clearly you're amazing at presenting information. Thanks, mate!
@brecoldyls6 жыл бұрын
Great, simple, and concise explanation. Thank you
@chrise2022 жыл бұрын
at 5:10, after i=1, your algorithm stops according to the code (for i=1 to 1). You have not explained how did you found out that the LEFT child of index i=1 does not align with the max_heap rules. You did it via visual inspection instead :(
@williamshaoul80885 ай бұрын
The way we'd know if a node did not follow max_heap rules is by having the maxHeapify function being recursive. There is a conditional at the top of the algorithm that checks if A) the node we are at is a leaf or B) either child of the node has a value that is greater than the current node's value that is being passed through. If the current node is a leaf we can't heapifyDown anymore, but if either child is greater we check which one has a greater value and do a regular swap. Then we recursively call the function again. This process allows for a natural "trickle" down the heap, and for the recursion to be killed if the base case conditional is true (the current node is a leaf OR both children are less than the current value)..
@colecook8344 ай бұрын
Thank you
@Fanaz102 ай бұрын
yea that was wack
@gamefreak16756 жыл бұрын
Yo! best video for explained how to structure maxheaps! Good job!
@nurulathirah38078 жыл бұрын
This video is such a great help..thank you so much😊
@theone6853 Жыл бұрын
Your explanation is very easy to understand
@varunsingh64803 жыл бұрын
Hi the algo starts from half of the length to 1, but when we reached at index 1, we destroyed the max heap property at index 2, but algo stops at 1. In the video we revisit the 2 index but algo is already stopped at index 1. Its unclear to me
@shreypatel93799 ай бұрын
Kinda late but the rearranging of index 2 is taken care of by the Max-Heapify(A,1) call which is done during the last iteration of for loop. *As you might know that Max-Heapify called at a particular node takes care of the subtree at that node i.e. the subtree at that node follows max heap property when Max-Heapify function is done executing. * So Max-Heapify(A,1) is called which swaps 5 from index 1 and places it at index 2. * Then it swaps 5 with 37 * So on and so forth This way 5 "floats" down to the place it is supposed to be. Then the max-heap property is satisfied for index at root node i.e. node index 1.
@DannyStopMotions8 жыл бұрын
Thank you, Tony Hawk :)
@mrchedda7 жыл бұрын
Thanks. Good video but just unclear on the last step once the root was swapped, how, programatically does it know to check the left subtree randomly? Also. What presentation software are you using? Thanks!
@Z00CE7 жыл бұрын
Assuming we also take care of array boundaries correctly, the MaxHeapify( A, i ) algorithm goes something like this: 1. largest = indexOf( max{ A[i], A[2*i], A[2*i+1] } ) 2. if largest != i, then swap( A[i], A[largest] ) and MaxHeapify( A, largest ) Also, I used Pixelmator to draw during the video. Thanks for watching!
@mrchedda7 жыл бұрын
+zooce thanks for the reply! Appreciate it!
@adityaprasad1574 Жыл бұрын
Wowwwwwwwwwwwwwwww
@nurulfadillah12484 ай бұрын
this is really helpful, thank you!
@tobiasgehtumdiewelt34557 жыл бұрын
you explain it so easy (y) thx
@patelji...91232 жыл бұрын
Awesome explanation..... Thx ❤️
@maxkellerer19237 ай бұрын
wish that someone explained like this in the lecture
@user-yd9xy3rb4x2 ай бұрын
You’re out of this world
@CantBeTamed538 жыл бұрын
THANKS FOR TEACHING ME THIS! YOU'RE A HOMIE!
@nandkishorenangre82447 жыл бұрын
Thanks it really helped me bro.....
@HostVisible6 жыл бұрын
Great video!
@chasemedia78707 жыл бұрын
Thank you - super clear and easy to follow. What software are you using to present your examples? I noticed you were able to erase a specific color and leave the rest of the board untouched.
@Z00CE6 жыл бұрын
I used Pixelmator, which has layers (the effect you saw of me erasing certain colors).
@TF2Shows6 жыл бұрын
when i = 1 why did u max heapify again? its exit loop
@logan21164 ай бұрын
great explanation
@husseinmohammed40209 жыл бұрын
great video
@nicklasnilsson82179 жыл бұрын
Nice I got it. Just wonder at the last step after the pointer i is at the root, do you then just check the whole heap a for broken properties or is there a more sophisticated approach to it? Thanks
@Z00CE9 жыл бұрын
Nicklas Nilsson, the last step (when i is at the root) is to simply call Max-Heapify, which fixes the rest of the issues in the Heap. So, to re-cap, first you fill up the heap, top to bottom, left to right (just for visuals - its actually just an array). Next you begin calling Max-Heapify from the bottom (middle of the array) to the top. Max-Heapify will correct any issues within the "sub-tree" that it is working on. As it moves upward, it continually fixes the Heap (all the way to the bottom from where it is currently being called). Hope that helps clear things up! Thanks for watching!
@nicklasnilsson82179 жыл бұрын
+zooce Ah okey, so everytime you call maxHeapify you have to check its child subtrees if it broke any properties and if so, swap until it is bigger than its children further down the tree?
@rajantamang8 жыл бұрын
I too had problem understanding the last one .. thanks for the answer
@souravkalal38314 жыл бұрын
Same here 😀😀
@sahidulll7 жыл бұрын
How do you go back and forth to check whether parent is greater than children? It's easy to swap with a pencil but how do you do in terms of coding? Thanks.
@omerkatz95095 жыл бұрын
You check that the array index i is greater than the items in index 2i and index 2i+1. So the first thing you do in terms of code is find out how long your array. You divide that number by 2 and round down (if the size was odd) and that would give you the first parent. And then as you go down from the last parent to the first parent you always check i against 2i and 2i + 1 (which are the indices of the array that represents the tree).
@nathanemiliano62093 жыл бұрын
you prolly dont care but if you guys are bored like me atm then you can watch pretty much all of the new movies on instaflixxer. Have been streaming with my brother these days =)
@amoszahir73463 жыл бұрын
@Nathan Emiliano Definitely, been watching on instaflixxer for since november myself :)
@jubinsoni46944 жыл бұрын
Nice Explanation Bro
@mohammadyahya782 жыл бұрын
Very clear explanation. Thank you very much.
@darshitakumar79135 жыл бұрын
Thanks! This helps :)
@japananh15 жыл бұрын
Thank you so much. Easy enouh for me! :)
@MartinThePooh7 жыл бұрын
Hey i have a question on you... I understand why we needed to heapify node 2 after decrementing "i", but how do you explain it from algorithm point of view? is there something like post-for-loop check of rules or how?
@blueknight4684 Жыл бұрын
If you had to swap, you should heapify on the index that got swapped
@Shobu7 жыл бұрын
Thank you !!
@claexlife3 жыл бұрын
thanks homie love you
@nekokun43616 жыл бұрын
Thank you :)
@apelmahmudorkut64967 жыл бұрын
Very helpful tut
@prashanttgs5 жыл бұрын
*heapful ;)
@nicopostigo1235 жыл бұрын
Thank you!
@hiyamghannam19398 жыл бұрын
Thank you zooce, This was fantastic, just wanna ask, I have a hmework were I must implement the class heap, should a build real nodes or all of the work is on an array ?
@chaitanyabalanagu626 Жыл бұрын
For those wondering how this algorithm works as it stops in the middle, you need to write a condition to check if maxHeap property is applied for the whole list everytime and repeat the process of heapify if it fails, here is an example import java.util.Arrays; public class MyClass { static boolean maxHeapFormed = false; public static void main(String args[]) { int[] nums = {5,12,64,1,37,90,91,97}; while(maxHeapFormed == false){ maxHeapify(nums); checkMaxHeap(nums); } System.out.println(Arrays.toString(nums)); } public static void checkMaxHeap(int[] nums){ int leftChild; int rightChild; for(int i = nums.length/2;i>=0;i--){ leftChild = 2*i+1; rightChild = 2*i+2; if(leftChild < nums.length && nums[leftChild] > nums[i]) return; else if(rightChild < nums.length && nums[rightChild] > nums[i]) return; } maxHeapFormed = true; } public static void maxHeapify(int[] nums){ int length = nums.length; for(int i = nums.length/2;i>=0;i--){ heapify(nums,i); } } public static void heapify(int[] nums, int i){ int maxValue = i; int leftChild = 2*i+1; int rightChild = 2*i+2; if(leftChild < nums.length && nums[leftChild] > nums[maxValue]) maxValue = leftChild; if(rightChild < nums.length && nums[rightChild] > nums[maxValue]) maxValue = rightChild; if(maxValue != i) swap(nums,maxValue,i); } public static void swap(int[] nums,int highest, int i){ int temp = nums[highest]; nums[highest] = nums[i]; nums[i] = temp; } }
@hamzajammy7 жыл бұрын
good job
@hamzajammy7 жыл бұрын
thank God
@combofriend44612 жыл бұрын
Can someone explain to me how this is different than what's being described in the first video? The first video is explaining max reality but it's also describing calling it from the last node with a child back to the root to build a heap just like this one is right? Am I missing something? The steps being walked through on both are the same aren't they?
@wangbiqing288828 күн бұрын
Visually I see the last swap between 5 and 37. but programmatically how to detect the disorder after looping all node index?
@rohitchauhan55874 жыл бұрын
Nice explanation .. Love from India sir
@liveyourlife36598 жыл бұрын
what happens if we have 9 elements instead of 8. 9 divided by 2 ? 4 or 5...does that technique applies in that situation?
@Z00CE8 жыл бұрын
We `floor` the division in the equation, so if there were 9 elements, then that part of the algorithm would evaluate to 4 because the `floor` of 9 / 2 is 4. Then at index 4, we call MaxHeapify, to `heapify` the subtree at with node 4 as the root. I hope this answers your question. Thanks for watching!
@Vlad-rk5go2 ай бұрын
3:45 why do we swap 97 and 1 instead of 97 and 12? Even overall I can't see any benefits over using a binary tree.
@derdev65747 жыл бұрын
thanks
@muhammadzeeshan00135 жыл бұрын
good i got the key now ;)
@ankeshpandey36306 жыл бұрын
Nice video sir but i have a doubt. What did you draw at the start of the video. I mean was it a min heap aur something else. In our college we are first asked to draw heap and then sort it into max or min. So what did you draw at first, please answer Thank you :)
@chloetang2113 жыл бұрын
its good explain!
@richardatkins36237 жыл бұрын
In all these videos no one explains how to actually enter the numbers from the array into the tree in that format (left to right)
@Z00CE7 жыл бұрын
The tree is only visual; there's no actual tree data structure. The way in which you access the array is representative of a tree (e.g. left = 2i, right = 2i + 1).
@raist1er6 жыл бұрын
The floor of 7 and 6 divided by 2 give 3 twice, did you skip to 2 on purpose?
@mjjply5 жыл бұрын
it doesnt matter, because it will only switch based on value.
@ERol-du3rd6 жыл бұрын
Does this run in O(n) time?
@kalvikaraiyila96936 жыл бұрын
what can we do if given elements in an array are same for e.g. A ={2,2,2,2,2,2,2,2,2,2}
@Z00CE6 жыл бұрын
Basically, this algorithm would simply iterate through the indexes ( i ) and the calls to Max-Heapify( A, i ), wouldn't have any work to do. Here's a quick run-through of the Max-Heapify algorithm: kzbin.info/www/bejne/a5qlhoeDjKynf7M. Thanks for watching, BTW!
@kalvikaraiyila96936 жыл бұрын
zooce ..., ya but if a ques arises that the given array A={2,2,...,2} belongs to max heap or min heap what would be the ans...,
@Z00CE6 жыл бұрын
That question, as you've stated it, would be ridiculous (it's a trick question, really), and doesn't test one's knowledge of min/max heaps. The answer is, both.
@kalvikaraiyila96936 жыл бұрын
ok tq fr ur response...,
@swastikagarwal86055 жыл бұрын
what is max heapify exactly... what does it do...
@abdulwahab1824 жыл бұрын
Which gadgets you are using for screen recording
@gpugpugpu4 жыл бұрын
Couldn't you have chosen a better example where the recursion of the downHeap algorithm comes in earlier? You don't even mention it until the end of the video and you kind of make it seem like it's a special case, even though the recursion is happening every time (only it is being exited earlier). It's kind of a fundamental function for heaps.
@Herotruth7 жыл бұрын
I dont see the difference between MaxHeap(a) and MaxHeapify(a, i).... It seems the only reason we have MaxHeap is so we can pass one perimeter.
@Z00CE7 жыл бұрын
This is only one implementation of this, which is a static implementation. This means that the algorithm MaxHeapify, doesn't know anything about the array, so you have to give it an array and the index of the subtree you want it to heapify. There are obviously other ways to do this.
@payalsagar18085 жыл бұрын
thankyou! INDIA..
@pawelloozik7 жыл бұрын
I have i =3 whats that mean ?
@colestecyk99575 жыл бұрын
rip my ears
@Lordycal Жыл бұрын
ngl this made no sense. How did you have that graph already made? shouldnt you be making it as you go?
@masterjif95062 ай бұрын
Well it doesn’t matter now cause you passed your exam anyway
@LLee04 жыл бұрын
I find that it is easier to implement by constructing a new array, instead of modifying an existing one: medium.com/@randerson112358/lets-build-a-max-heap-161d676394e See the section: # Williams Algorithm: top down while not end of array, if heap is empty, place item at root; else, place item at bottom of heap; while (child > parent) swap(parent, child); go to next array element; end Finish the program in one hour.
@prasanjeetdas35555 жыл бұрын
that's a wrong way.
@abhishekgautam10635 жыл бұрын
Wrong Process of Max Heapify, you are not doing recursion!