Inserting an item is very simple in itself. We add the new element to the end of the list (which we understand to be the bottom of the tree). Then we increment the size of the heap by one.
But after each insert, we need to float the new element up if needed. Bear in mind that the lowest element in the min heap needs to be the root element. We first create a helper method called float that takes care of this. Let us look at how it is meant to behave. Imagine that we have the following heap and want to insert the value 2:
The new element has occupied the last slot in the third row or level. Its index value is 7. Now we compare that ...