Deletion in Heap
Given a Binary Heap and an element present in the given Heap. The task is to delete an element from this Heap.
The standard deletion operation on Heap is to delete the element present at the root node of the Heap. That is if it is a Max Heap, the standard deletion operation will delete the maximum element and if it is a Min heap, it will delete the minimum element.
Process of Deletion:
Since deleting an element at any intermediary position in the heap can be costly, so we can simply replace the element to be deleted by the last element and delete the last element of the Heap.
- Replace the root or element to be deleted by the last element.
- Delete the last element from the Heap.
- Since, the last element is now placed at the position of the root node. So, it may not follow the heap property. Therefore, heapify the last node placed at the position of root.
Suppose the Heap is a Max-Heap as: 10 / \ 5 3 / \ 2 4 The element to be deleted is root, i.e. 10. Process: The last element is 4. Step 1: Replace the last element with root, and delete it. 4 / \ 5 3 / 2 Step 2: Heapify root. Final Heap: 5 / \ 4 3 / 2
5 4 3 2
Insertion in Heaps
The insertion operation is also similar to that of the deletion process.
Given a Binary Heap and a new element to be added to this Heap. The task is to insert the new element to the Heap maintaining the properties of Heap.
Process of Insertion: Elements can be isnerted to the heap following a similar approach as discussed above for deletion. The idea is to:
- First increase the heap size by 1, so that it can store the new element.
- Insert the new element at the end of the Heap.
- This newly inserted element may distort the properties of Heap for its parents. So, in order to keep the properties of Heap, heapify this newly inserted element following a bottom-up approach.
Suppose the Heap is a Max-Heap as: 10 / \ 5 3 / \ 2 4 The new element to be inserted is 15. Process: Step 1: Insert the new element at the end. 10 / \ 5 3 / \ / 2 4 15 Step 2: Heapify the new element following bottom-up approach. -> 15 is less than its parent 3, swap them. 10 / \ 5 15 / \ / 2 4 3 -> 15 is again less than its parent 10, swap them. 15 / \ 5 10 / \ / 2 4 3 Therefore, the final heap after insertion is: -> 15 is less than its parent 3, swap them. 15 / \ 5 10 / \ / 2 4 3
15 5 10 2 4 3
- Merge two binary Max Heaps
- Ternary Search Tree (Deletion)
- Fibonacci Heap - Deletion, Extract min and Decrease key
- Kth smallest element after every insertion
- Fibonacci Heap - Insertion and Union
- Reduce the array to a single integer with the given operation
- Divide the given linked list in two lists of size ratio p:q
- Generate two BSTs from the given array such that maximum height among them is minimum
- Delete all even elements from a stack
- Sum of the mirror image nodes of a complete binary tree in an inorder way
- Kth largest node among all directly connected nodes to the given node in an undirected graph
- Index of kth set bit in a binary array with update queries
- Iterative Postorder traversal | Set 3
- Sum of all odd frequency nodes of the Linked List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
Improved By : AnkitRai01