A Binary Heap is a complete Binary Tree which is used to store data efficiently to get the max or min element based on its structure.
A Binary Heap is either Min Heap or Max Heap. In a Min Binary Heap, the key at the root must be minimum among all keys present in Binary Heap. The same property must be recursively true for all nodes in Binary Tree. Max Binary Heap is similar to MinHeap.
Examples of Min Heap:
/ \ / \
20 100 15 30
/ / \ / \
30 40 50 100 40
How is Binary Heap represented?
A Binary Heap is a Complete Binary Tree. A binary heap is typically represented as an array.
- The root element will be at Arr.
- The below table shows indices of other nodes for the ith node, i.e., Arr[i]:
|Arr[(i-1)/2]||Returns the parent node|
|Arr[(2*i)+1]||Returns the left child node|
|Arr[(2*i)+2]||Returns the right child node|
The traversal method use to achieve Array representation is Level Order Traversal. Please refer to Array Representation Of Binary Heap for details.
Operations on Heap:
Below are some standard operations on min heap:
- getMin(): It returns the root element of Min Heap. The time Complexity of this operation is O(1). In case of a maxheap it would be getMax().
- extractMin(): Removes the minimum element from MinHeap. The time Complexity of this Operation is O(log N) as this operation needs to maintain the heap property (by calling heapify()) after removing the root.
- decreaseKey(): Decreases the value of the key. The time complexity of this operation is O(log N). If the decreased key value of a node is greater than the parent of the node, then we don’t need to do anything. Otherwise, we need to traverse up to fix the violated heap property.
- insert(): Inserting a new key takes O(log N) time. We add a new key at the end of the tree. If the new key is greater than its parent, then we don’t need to do anything. Otherwise, we need to traverse up to fix the violated heap property.
- delete(): Deleting a key also takes O(log N) time. We replace the key to be deleted with the minimum infinite by calling decreaseKey(). After decreaseKey(), the minus infinite value must reach root, so we call extractMin() to remove the key.
Below is the implementation of basic heap operations.
2 4 1
Applications of Heaps:
- Heap Sort: Heap Sort uses Binary Heap to sort an array in O(nLogn) time.
- Priority Queue: Priority queues can be efficiently implemented using Binary Heap because it supports insert(), delete() and extractmax(), decreaseKey() operations in O(log N) time. Binomial Heap and Fibonacci Heap are variations of Binary Heap. These variations perform union also efficiently.
- Graph Algorithms: The priority queues are especially used in Graph Algorithms like Dijkstra’s Shortest Path and Prim’s Minimum Spanning Tree.
- Many problems can be efficiently solved using Heaps. See following for example. a) K’th Largest Element in an array. b) Sort an almost sorted array/ c) Merge K Sorted Arrays.
- Coding Practice on Heap
- All Articles on Heap
- Quiz on Heap
- PriorityQueue : Binary Heap Implementation in Java Library
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Please Login to comment...