And the Edges represents the bus names that travels through that edge/road.
A Tree that can have at-most two children.
A Heap or a Binary Heap is a Binary Tree, where all the elements in the child Node should be greater than its parent. That's a Min Heap(Max Heap should be just the opposite).
So, by definition, a Heap or a Binary Heap is a Binary Tree that has priority elements in its child Nodes. It can also be called as a Min Heap.
We can consider, 'priority elements' as the elements that are greater than the lower priority elements.
Say '7' has a higher priority, when compared to '5'.
Let us simplify it with the below example :
As mentioned above, A Heap or a Binary Heap is a Binary Tree that has priority elements in its child Nodes.
i.e. All the elements in the child Node should be greater than its parent.
Let us take the parent element in Level 1 (i.e. 2). It has two elements in its child Nodes(i.e. 4 and 9). And both 4 and 9 are greater than its parent element i.e. 2.
Similarly, if we take the the elements of Level 2 and Level 3. 10 and 14 are Children of 4. And 17 and 16 are children of 9. Both 10 and 14 are greater than 4 and 17 and 16 are greaterthan 9.
Same applies for Level 3 and Level 4.
The next property of a Heap is that all the levels should be full except the last level that has to be left filled.
Let's make it simple.
Level 1 can have a maximum of one element. Which it has (i.e. 2)
Similarly, Level 2 can have a maximum of two elements. Which it has (i.e. 4 and 9).
Also, Level 3 can have a maximum of four elements. Which it has (i.e. 10, 14, 17 and 16).
Now, Level 4 can have a maximum of eight elements. But there are just five. And all the five elements are left aligned.
And that's the second property. All the levels must be filled, except the last level that should be left aligned.
The above tree is not a heap because it doesn't satisfy the second condition. i.e. The last level has to be left filled. As in level 4, 25 should be the left child of 17. But, instead it is the right child of 17 and the left child is empty.
Look closely, the above tree is not a Heap. Why?
Take a look at Level 2 and Level 3. '4' from Level 2 has a left child '3'. But that shouldn't be. As the child should be greater than parent.
Which violates property 1. i.e. All the elements in the child Node should be greater than its parent.
The minimum element of the Heap will always be the root element or the element at Level 1.
Because if the minimum element would be anywhere else in the Heap. It would violate the property, "All the elements in the child Node should be greater than its parent".
Finding the minimum element would take O(1) time. As we now where it lies.
If we look at the above Heap, we can see that the height of the Heap is 4 (Since it has 4 levels).
And it has 12 elements in it. And the above Heap can have a max of 16 elements.
Now, if we try 'log 16', we get 4. Which is the height of the Heap.
So, if there are 'n' elements in a Heap. The height of a Heap would be,
h = log n
To implement a Heap using Arrays, we will start filling the array starting from the root element of the Heap. Or the elements from Level 1.
Then we will start moving one level down and fill elements from left to right.
Let me clear it.
At first we will take the root element(i.e. 2) from Level 1 and place it in the 1st position of the array.
Similarly, we take the elements from level 2. And place the elements starting from the leftmost element of level 2. i.e. We take 4 and place it in the second location of the array.
And take 9 and place at the 3rd position in the array.
Same way, we will take the elements of Level 3. And place the elements starting from the leftmost element of level 3. i.e. We will start from 10, then 14 and so on.
And we do the same for level 4.
Now, since the Array is filled. Let us try to see the logic to find the elements.
The logic is
Let us pick a random element from the array. Say 14.
Now, 14 is in location 4 of the array.
Now,
So the parent of 14 is in location 1 in the array, i.e. 4.
And if we look at the Heap. 4 is the parent of 14.
The logic is
Let us pick a random element from the array. Say 9.
Now, 9 is in location 2 of the array.
Now,
So the left child of 9 is in location 5 in the array, i.e. 17.
And if we look at the Heap. The left child of 9 is 17.
The logic is
Let us pick a random element from the array. Say 14.
Now, 14 is in location 4 of the array.
Now,
So the right child of 14 is in location 10 in the array, i.e. 19.
And if we look at the Heap. The right child of 14 is 19.
Let us take the below Heap :
Now, we will insert a new element 5 in the Heap.
And as per the second property of Heap, all the elements in the last level should be left filled.
So, we can insert 5 as the right child of 17.
Now, according to the first property, the new element i.e. 5 should be greater than its parent i.e. 17.
But we can see 5 is less than its parent 17. S0, we swap the positions of 5 and 17.
Now, we can see that the parent of 5 is 9. Which is violating the 1st property.
So, we swap 5 and 9.
Now, if we compare 5 with its parent i.e. 2. We can see 5 is greater than 2.
So, 5 is in the right place.
We have seen in the above scenario that in the worst case, the element needs to travel all the way up.
Just note, in the above case the Heap has 4 levels and the new element had to travel till Level 2. If the element would have been 1, it would have to travel to level 1.
So, in the worst case the element had to travel till the top. Which is the height of the Heap.
And we have seen, it takes O(log n) time to find the height of the Heap.
So, the insertion of an element in a Heap takes O(log n) time.
As the name suggests, Heapify is a method that is used to convert a non-heap to a heap. The only condition should be, both the children of the node where Heapify needs to be applied, should be a Heap.
Let me clarify with an example,
As we can see in the above case, the root element at level 1 is 18. That is greater than both of its children i.e. 4 and 5. So, it violates the heap property.
In this case, we can apply Heapify on 18. But the only condition should be, both the children of 18 should be a Heap.
Now, if we check the children of 4. They are 10 and 14. Both are greater than 4. Similarly, the children of 10 are 21 and 20. And the children of 14 are 48 and 19. Both satisfy the Heap structural property.
Again, if we check the children of 5. Its 9 and 16. Both are greater than 5. And if we check the children of 9, they are 25 ad 17. Both are greater than 9.
So, we can conclude saying the children of 18 are Heaps and we can apply Heapify on 18.
Now, the Heapify method works in the following way :
We have seen the running time to insert an element in the Heap is O(log n). As it travels up the heap .
And in case of Heapify , the element travels down the Heap . That should be equivalent to the height of the Heap.
Running time of Heapify : O(log n)
We have seen that the minimum element or the smallest element always stays on top of the Heap.
Now, if we remove the root element and rearrange the Heap, we are done.
Let us see the steps with the below example :
To remove the root element from Level 1 and place the rightmost element from Level 4 takes constant time.
And Heapify takes O(log n).
Time taken to remove minmm element from Heap : O(log n)
Say, we are given a few numbers. And we need to create a Heap out of it.
What we will do is, create a tree out of the given numbers and then use Heapify to create a Heap out of it.
Let's us explain with the below example :
We are given a list of 6 numbers,
Now, we will follow the below steps to create a Heap out of it :
Running time to build a heap is : O(n)