A Binary Heap is a Binary Tree with following properties.
1) It’s a complete tree (All levels are completely filled except possibly the last level and the last level has all keys as left as possible). This property of Binary Heap makes them suitable to be stored in an array.
2) A Binary Heap is either Min Heap or Max Heap. In a Min Binary Heap, the key at 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:
10 10 / \ / \ 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.
- Below table shows indexes 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
Please refer Array Representation Of Binary Heap for details.
Applications of Heaps:
1) Heap Sort: Heap Sort uses Binary Heap to sort an array in O(nLogn) time.
2) Priority Queue: Priority queues can be efficiently implemented using Binary Heap because it supports insert(), delete() and extractmax(), decreaseKey() operations in O(logn) time. Binomoial Heap and Fibonacci Heap are variations of Binary Heap. These variations perform union also efficiently.
Operations on Min Heap:
1) getMini(): It returns the root element of Min Heap. Time Complexity of this operation is O(1).
2) extractMin(): Removes the minimum element from MinHeap. Time Complexity of this Operation is O(Logn) as this operation needs to maintain the heap property (by calling heapify()) after removing root.
3) decreaseKey(): Decreases value of key. The time complexity of this operation is O(Logn). If the decreases 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.
4) insert(): Inserting a new key takes O(Logn) time. We add a new key at the end of the tree. IF 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.
5) delete(): Deleting a key also takes O(Logn) time. We replace the key to be deleted with minum 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
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Check if a given Binary Tree is Heap
- Array Representation Of Binary Heap
- Why is Binary Heap Preferred over BST for Priority Queue?
- How to check if a given array represents a Binary Heap?
- Height of a complete binary tree (or Heap) with N nodes
- Leaf starting point in a Binary Heap data structure
- Overview of Data Structures | Set 2 (Binary Tree, BST, Heap and Hash)
- Heap Sort for decreasing order using min heap
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Tournament Tree (Winner Tree) and Binary Heap
- Convert min Heap to max Heap
- K-ary Heap
- Convert BST to Max Heap
- Skew Heap
- How to implement Min Heap using STL?