Reference
Binary Heap
A Binary Heap is a Binary Tree with following properties.
- It’s a complete tree. This property makes them suitable to be stored in an array
- A Binary Heap is either Min Heap or Max Heap. (Recursively)
How is Binary Heap represent?
A Binary Heap is a Complete Binary Tree. A binary heap is typically represented as an array.
The root element will be at Arr[0]
Arr[(i-1)/2] : the parent node
Arr[(2*i)+1] : the left child
Arr[(2*i)+2] : the right child
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()
andextractmax()
,decreaseKey()
operation in O(logn) time. - Graph Algorithms: The priority queues are especially used in
Graph Algorithms
likeDijkstra's Shortest Path
andPrim's Minimum Spanning Tree
- Many problems can be efficiently solved using Heaps. See following. for example.
- K’th Largest Element in an array
- Sort an almost sorted array
- Merge K sorted Arrays
Operation on Min Heap
- getMini()
- extractMin()
- decreaseKey()
- insert()
- delete()