In the last post, we discussed Insertion and Union of Fibonacci Heaps. In this post, we will discuss Extract_min(), Decrease_key() and Deletion() operations on Fibonacci heap.
Extract_min(): We create a function for deleting the minimum node and setting the min pointer to the minimum value in the remaining heap. The following algorithm is followed:
- Delete the min node.
- Set head to the next min node and add all the tree of the deleted node in root list.
- Create an array of degree pointers of the size of the deleted node.
- Set degree pointer to current node.
- Move to the next node.
- If degrees are different then set degree pointer to next node.
- If degrees are same then join the Fibonacci trees by union operation.
- Repeat steps 4 and 5 until the heap is completed.
Decrease_key(): To decrease the value of any element in the heap, we follow the following algorithm:
- Update min pointer if necessary.
- Cut off the link between ‘x’ and its parent.
- Mark the parent of ‘x’.
- Add tree rooted at ‘x’ to the root list and update min pointer if necessary.
- Cut off the link between ‘x’ and its parent p[x].
- Add ‘x’ to the root list, updating min pointer if necessary.
- Cut off link between p[x] and p[p[x]].
- Add p[x] to the root list, updating min pointer if necessary.
- If p[p[x]] is unmarked, mark it.
- Else, cut off p[p[x]] and repeat steps 4.2 to 4.5, taking p[p[x]] as ‘x’.
Deletion(): To delete any element in a Fibonacci heap, the following algorithm is followed:
- Decrease the value of the node to be deleted ‘x’ to minimum by Decrease_key() function.
- By using min heap property, heapify the heap containing ‘x’, bringing ‘x’ to the root list.
- Apply Extract_min() algorithm to the Fibonacci heap.
Following is a program to demonstrate Extract min(), Deletion() and Decrease key() operations on a Fibonacci Heap:
Creating an initial heap The root nodes of Heap are: 2-->5-->8 The heap has 3 nodes Extracting min The root nodes of Heap are: 5 The heap has 2 nodes Decrease value of 8 to 7 The root nodes of Heap are: 5 The heap has 2 nodes Delete the node 7 Key Deleted The root nodes of Heap are: 5 The heap has 1 nodes
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Convert min Heap to max Heap
- Heap Sort for decreasing order using min heap
- Fibonacci Heap - Insertion and Union
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Convert BST to Min Heap
- Merge k sorted linked lists | Set 2 (Using Min Heap)
- Print all nodes less than a value x in a Min Heap.
- Maximum element in min heap
- K’th Least Element in a Min-Heap
- Maximise the number of toys that can be purchased with amount K using min Heap
- Merge two sorted arrays in constant space using Min Heap
- Min Heap in Python
- Complexity analysis of various operations of Binary Min Heap
- k largest(or smallest) elements in an array | added Min Heap method
- Fibonacci Heap | Set 1 (Introduction)
- Difference between Primary key and Unique key
- Check if a M-th fibonacci number divides N-th fibonacci number
- Check if sum of Fibonacci elements in an Array is a Fibonacci number or not
- Insertion and Deletion in Heaps
- m-Way Search Tree | Set-2 | Insertion and Deletion
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.