In previous post i.e. Set 1 we have discussed that implements these below functions:
- insert(H, k): Inserts a key ‘k’ to Binomial Heap ‘H’. This operation first creates a Binomial Heap with single key ‘k’, then calls union on H and the new Binomial heap.
- getMin(H): A simple way to getMin() is to traverse the list of root of Binomial Trees and return the minimum key. This implementation requires O(Logn) time. It can be optimized to O(1) by maintaining a pointer to minimum key root.
- extractMin(H): This operation also uses union(). We first call getMin() to find the minimum key Binomial Tree, then we remove the node and create a new Binomial Heap by connecting all subtrees of the removed minimum node. Finally we call union() on H and the newly created Binomial Heap. This operation requires O(Logn) time.
12------------10--------------------20 / \ / | \ 15 50 70 50 40 | / | | 30 80 85 65 | 100 A Binomial Heap with 13 nodes. It is a collection of 3 Binomial Trees of orders 0, 2 and 3 from left to right. 10--------------------20 / \ / | \ 15 50 70 50 40 | / | | 30 80 85 65 | 100
In this post, below functions are implemented.
- delete(H): Like Binary Heap, delete operation first reduces the key to minus infinite, then calls extractMin().
- decreaseKey(H): decreaseKey() is also similar to Binary Heap. We compare the decreases key with it parent and if parent’s key is more, we swap keys and recur for parent. We stop when we either reach a node whose parent has smaller key or we hit the root node. Time complexity of decreaseKey() is O(Logn)
The heap is: 50 10 30 40 20 After deleing 10, the heap is: 20 30 40 50
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Implementation of Binomial Heap
- Binomial Heap
- Memory representation of Binomial Heap
- Treap | Set 2 (Implementation of Search, Insert and Delete)
- Heap Sort for decreasing order using min heap
- Convert min Heap to max Heap
- K-ary Heap
- Convert BST to Max Heap
- Convert BST to Min Heap
- Min Heap in Python
- Max Heap in Python
- K’th Least Element in a Min-Heap
- Pairing Heap
- Weak Heap
- Max Heap in Java