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 Min Heap.

How is Binary Heap represented?
A Binary Heap is a Complete Binary Tree. A binary heap is typically represented as array. Please refer below article for details.
Array Representation Of Binary Heap.

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.

3) Graph Algorithms: The priority queues are especially used in Graph Algorithms like Dijkstra’s Shortest Path and Prim’s Minimum Spanning Tree.

4) 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.

[ad type=”banner”]

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 Min Heap. 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. Time complexity of this operation is O(Logn). If the decreases key value of a node is greater than 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 key.

python programming:

[pastacode lang=”python” manual=”%23%20A%20Python%20program%20to%20demonstrate%20common%20binary%20heap%20operations%0A%20%0A%23%20Import%20the%20heap%20functions%20from%20python%20library%0Afrom%20heapq%20import%20heappush%2C%20heappop%2C%20heapify%20%0A%20%0A%23%20heappop%20-%20pop%20and%20return%20the%20smallest%20element%20from%20heap%0A%23%20heappush%20-%20push%20the%20value%20item%20onto%20the%20heap%2C%20maintaining%0A%23%20%20%20%20%20%20%20%20%20%20%20%20%20heap%20invarient%0A%23%20heapify%20-%20transform%20list%20into%20heap%2C%20in%20place%2C%20in%20linear%20time%0A%20%0A%23%20A%20class%20for%20Min%20Heap%0Aclass%20MinHeap%3A%0A%20%20%20%20%20%0A%20%20%20%20%23%20Constructor%20to%20initialize%20a%20heap%0A%20%20%20%20def%20__init__(self)%3A%0A%20%20%20%20%20%20%20%20self.heap%20%3D%20%5B%5D%20%0A%20%0A%20%20%20%20def%20parent(self%2C%20i)%3A%0A%20%20%20%20%20%20%20%20return%20(i-1)%2F2%0A%20%20%20%20%20%0A%20%20%20%20%23%20Inserts%20a%20new%20key%20’k’%0A%20%20%20%20def%20insertKey(self%2C%20k)%3A%0A%20%20%20%20%20%20%20%20heappush(self.heap%2C%20k)%20%20%20%20%20%20%20%20%20%20%20%0A%20%0A%20%20%20%20%23%20Decrease%20value%20of%20key%20at%20index%20’i’%20to%20new_val%0A%20%20%20%20%23%20It%20is%20assumed%20that%20new_val%20is%20smaller%20than%20heap%5Bi%5D%0A%20%20%20%20def%20decreaseKey(self%2C%20i%2C%20new_val)%3A%0A%20%20%20%20%20%20%20%20self.heap%5Bi%5D%20%20%3D%20new_val%20%0A%20%20%20%20%20%20%20%20while(i%20!%3D%200%20and%20self.heap%5Bself.parent(i)%5D%20%3E%20self.heap%5Bi%5D)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Swap%20heap%5Bi%5D%20with%20heap%5Bparent(i)%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20self.heap%5Bi%5D%20%2C%20self.heap%5Bself.parent(i)%5D%20%3D%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20self.heap%5Bself.parent(i)%5D%2C%20self.heap%5Bi%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%23%20Method%20to%20remove%20minium%20element%20from%20min%20heap%0A%20%20%20%20def%20extractMin(self)%3A%0A%20%20%20%20%20%20%20%20return%20heappop(self.heap)%0A%20%0A%20%20%20%20%23%20This%20functon%20deletes%20key%20at%20index%20i.%20It%20first%20reduces%0A%20%20%20%20%23%20value%20to%20minus%20infinite%20and%20then%20calls%20extractMin()%0A%20%20%20%20def%20deleteKey(self%2C%20i)%3A%0A%20%20%20%20%20%20%20%20self.decreaseKey(i%2C%20float(%22-inf%22))%0A%20%20%20%20%20%20%20%20self.extractMin()%0A%20%0A%20%20%20%20%23%20Get%20the%20minimum%20element%20from%20the%20heap%0A%20%20%20%20def%20getMin(self)%3A%0A%20%20%20%20%20%20%20%20return%20self.heap%5B0%5D%0A%20%0A%23%20Driver%20pgoratm%20to%20test%20above%20function%0AheapObj%20%3D%20MinHeap()%0AheapObj.insertKey(3)%0AheapObj.insertKey(2)%0AheapObj.deleteKey(1)%0AheapObj.insertKey(15)%0AheapObj.insertKey(5)%0AheapObj.insertKey(4)%0AheapObj.insertKey(45)%0A%20%0Aprint%20heapObj.extractMin()%2C%0Aprint%20heapObj.getMin()%2C%0AheapObj.decreaseKey(2%2C%201)%0Aprint%20heapObj.getMin()” message=”” highlight=”” provider=”manual”/]

Output:

2 4 1
[ad type=”banner”]

Categorized in: