A Min Heap Binary Tree is a Binary Tree where the root node has the minimum key in the tree.
The above definition holds true for all sub-trees in the tree. This is called the Min Heap property.
Almost every node other than the last two layers must have two children. That is, this is almost a complete binary tree, with the exception of the last 2 layers.
The below tree is an example of a min heap binary tree since the above two properties hold.
Now that we’ve covered what a min heap tree is, let’s look at how we can represent it.
A Min Heap Binary Tree is commonly represented as an array, which is indexed according to the below format:
Current Node | arr[i] |
Parent Node | arr[(i-1)/2] |
Left Child | arr[(2*i) + 1] |
Right Child | arr[(2*i )+ 2] |
The root of the whole tree is at arr[0]
.
We will use the indexing as shown in the below figure. It’s not very hard to find the pattern here, which will match with the above table.
This indexing follows a Level Order Traversal of the Binary Tree, so a Binary Heap array is a Binary Tree using a level order traversal.
The above figure shows the array representation of the Min Heap Tree.
Now that we’ve covered the concepts, let’s move onto implementing this in C!
We will use the array representation to build the tree. Let’s start writing the structure for the Min Heap.
We’ll have an array of elements, and a size, which gets updated as elements are being inserted or deleted.
The array also has a capacity, which indicates the maximum size of the array.
There are a few functions that we need to write to indicate that we are representing a Min Heap Tree, like finding the parent, and the children.
We’ll write functions to initialize and free the heap.
With that covered, let’s now move on to how we can insert elements!
The insertion algorithm is simple. This inserts an element into the tree.
Breaking down the algorithm:
To understand this procedure, let’s take an example.
Consider the tree below, having only one element.
Let’s insert the element 40. Since there is only one element, it inserts to the bottom, and we observe that the min-heap property is satisfies, since 10 < 40. So there is no need to swap.
Next, we’ll insert 50. A similar procedure follows.
Next, we’ll insert 5. So now, we first insert to the bottom of the tree, at index 3.
The min heap property is violated for the sub-tree 1-3, and therefore, for the whole tree. So, we must keep swapping with the parent until we reach the root.
So, we need one more swap, since again, the min-heap property is violated for the sub-tree rooted at node 0.
Alright. Now that we have visualized it, let’s write it down!
We’ll now implement the deletion method.
Before we look at deleting an element any index, since the min-heap is very closely associated with the root, we will look at deleting the root first.
To delete the minimum element (i.e the root), we will do the following:
heapify()
to do this for us.So, we know that the deletion method will be complete after we do the heapify()
method as well.
This function takes in an element index index
, and maintains the min heap property, by swapping with the smallest element of its immediate sub-tree.
The resulting tree will satisfy the min-heap property.
This involves finding the minimum element of the sub-tree and performing a swap with the current element.
After this, we still need to make sure the entire tree satisfies this. So, we need to recursively call the procedure on the smallest element, until we reach the root!
We can now extend this delete_minimum()
function, to delete any element.
This involves only setting the desired element to the minimum possible value, that will be get_min() - 1
, since it must be lesser than the current minimum.
We will now keep swapping until we update the position so that the new root is this element.
Now, we’re back at our old delete_minimum()
function! We can simply delete the new root!
With this, our entire deletion procedure will look like this:
Phew! We’re finally done. I’ll now show you the entire code until now, along with the print()
function, to visualize the tree.
Output
The time complexities of the above procedures are mentioned below:
Function | Time Complexity |
get_min() |
O(1) |
insert_minheap() |
O(logN) |
delete_minimum() |
Same as insert - O(logN) |
heapify() |
O(logN) |
delete_element() |
O(logN) |
You can download the complete code as a Github Gist that I have uploaded. If you have any queries regarding this, do ask them in the comment section below!
In this article, we learned how we can represent a Min Heap Binary Tree, and also look at an implementation in C.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.