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.
Illustration:
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
Implementation:
C++
#include <iostream>
using namespace std;
void heapify( int arr[], int n, int i)
{
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if (l < n && arr[l] > arr[largest])
largest = l;
if (r < n && arr[r] > arr[largest])
largest = r;
if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}
void deleteRoot( int arr[], int & n)
{
int lastElement = arr[n - 1];
arr[0] = lastElement;
n = n - 1;
heapify(arr, n, 0);
}
void printArray( int arr[], int n)
{
for ( int i = 0; i < n; ++i)
cout << arr[i] << " " ;
cout << "\n" ;
}
int main()
{
int arr[] = { 10, 5, 3, 2, 4 };
int n = sizeof (arr) / sizeof (arr[0]);
deleteRoot(arr, n);
printArray(arr, n);
return 0;
}
|
Java
public class deletionHeap {
static void heapify( int arr[], int n, int i)
{
int largest = i;
int l = 2 * i + 1 ;
int r = 2 * i + 2 ;
if (l < n && arr[l] > arr[largest])
largest = l;
if (r < n && arr[r] > arr[largest])
largest = r;
if (largest != i) {
int swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;
heapify(arr, n, largest);
}
}
static int deleteRoot( int arr[], int n)
{
int lastElement = arr[n - 1 ];
arr[ 0 ] = lastElement;
n = n - 1 ;
heapify(arr, n, 0 );
return n;
}
static void printArray( int arr[], int n)
{
for ( int i = 0 ; i < n; ++i)
System.out.print(arr[i] + " " );
System.out.println();
}
public static void main(String args[])
{
int arr[] = { 10 , 5 , 3 , 2 , 4 };
int n = arr.length;
n = deleteRoot(arr, n);
printArray(arr, n);
}
}
|
Python3
def heapify(arr, n, i):
largest = i
l = 2 * i + 1
r = 2 * i + 2
if (l < n and arr[l] > arr[largest]):
largest = l
if (r < n and arr[r] > arr[largest]):
largest = r
if (largest ! = i):
arr[i],arr[largest] = arr[largest],arr[i]
heapify(arr, n, largest)
def deleteRoot(arr):
global n
lastElement = arr[n - 1 ]
arr[ 0 ] = lastElement
n = n - 1
heapify(arr, n, 0 )
def printArray(arr, n):
for i in range (n):
print (arr[i],end = " " )
print ()
if __name__ = = '__main__' :
arr = [ 10 , 5 , 3 , 2 , 4 ]
n = len (arr)
deleteRoot(arr)
printArray(arr, n)
|
C#
using System;
public class deletionHeap
{
static void heapify( int []arr, int n, int i)
{
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if (l < n && arr[l] > arr[largest])
largest = l;
if (r < n && arr[r] > arr[largest])
largest = r;
if (largest != i)
{
int swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;
heapify(arr, n, largest);
}
}
static int deleteRoot( int []arr, int n)
{
int lastElement = arr[n - 1];
arr[0] = lastElement;
n = n - 1;
heapify(arr, n, 0);
return n;
}
static void printArray( int []arr, int n)
{
for ( int i = 0; i < n; ++i)
Console.Write(arr[i] + " " );
Console.WriteLine();
}
public static void Main()
{
int []arr = { 10, 5, 3, 2, 4 };
int n = arr.Length;
n = deleteRoot(arr, n);
printArray(arr, n);
}
}
|
Javascript
<script>
function heapify(arr, n, i)
{
let largest = i;
let l = 2 * i + 1;
let r = 2 * i + 2;
if (l < n && arr[l] > arr[largest])
largest = l;
if (r < n && arr[r] > arr[largest])
largest = r;
if (largest != i)
{
let swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;
heapify(arr, n, largest);
}
}
function deleteRoot(arr, n)
{
let lastElement = arr[n - 1];
arr[0] = lastElement;
n = n - 1;
heapify(arr, n, 0);
return n;
}
function printArray(arr, n)
{
for (let i = 0; i < n; ++i)
document.write(arr[i] + " " );
document.write( "</br>" );
}
let arr = [ 10, 5, 3, 2, 4 ];
let n = arr.length;
n = deleteRoot(arr, n);
printArray(arr, n);
</script>
|
Time complexity: O(logn) where n is no of elements in the heap
Auxiliary Space: O(n)
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 inserted 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.
Illustration:
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 more than its parent 3, swap them.
10
/ \
5 15
/ \ /
2 4 3
-> 15 is again more than its parent 10, swap them.
15
/ \
5 10
/ \ /
2 4 3
Therefore, the final heap after insertion is:
15
/ \
5 10
/ \ /
2 4 3
Implementation:
C++
#include <iostream>
using namespace std;
#define MAX 1000 // Max size of Heap
void heapify( int arr[], int n, int i) {
int parent = (i - 1) / 2;
if (parent >= 0) {
if (arr[i] > arr[parent]) {
swap(arr[i], arr[parent]);
heapify(arr, n, parent);
}
}
}
void insertNode( int arr[], int & n, int Key)
{
n = n + 1;
arr[n - 1] = Key;
heapify(arr, n, n - 1);
}
void printArray( int arr[], int n)
{
for ( int i = 0; i < n; ++i)
cout << arr[i] << " " ;
cout << "\n" ;
}
int main()
{
int arr[MAX] = { 10, 5, 3, 2, 4 };
int n = 5;
int key = 15;
insertNode(arr, n, key);
printArray(arr, n);
return 0;
}
|
Java
public class insertionHeap {
static void heapify( int [] arr, int n, int i)
{
int parent = (i - 1 ) / 2 ;
if (parent >= 0 ) {
if (arr[i] > arr[parent]) {
int temp = arr[i];
arr[i] = arr[parent];
arr[parent] = temp;
heapify(arr, n, parent);
}
}
}
static int insertNode( int [] arr, int n, int Key)
{
n = n + 1 ;
arr[n - 1 ] = Key;
heapify(arr, n, n - 1 );
return n;
}
static void printArray( int [] arr, int n)
{
for ( int i = 0 ; i < n; ++i)
System.out.println(arr[i] + " " );
System.out.println();
}
public static void main(String args[])
{
int MAX = 1000 ;
int [] arr = new int [MAX];
arr[ 0 ] = 10 ;
arr[ 1 ] = 5 ;
arr[ 2 ] = 3 ;
arr[ 3 ] = 2 ;
arr[ 4 ] = 4 ;
int n = 5 ;
int Key = 15 ;
n = insertNode(arr, n, Key);
printArray(arr, n);
}
}
|
Python3
def heapify(arr, n, i):
parent = int (((i - 1 ) / 2 ))
if parent > = 0 :
if arr[i] > arr[parent]:
arr[i], arr[parent] = arr[parent], arr[i]
heapify(arr, n, parent)
def insertNode(arr, key):
global n
n + = 1
arr.append(key)
heapify(arr, n, n - 1 )
def printArr(arr, n):
for i in range (n):
print (arr[i], end = " " )
arr = [ 10 , 5 , 3 , 2 , 4 , 1 , 7 ]
n = 7
key = 15
insertNode(arr, key)
printArr(arr, n)
|
C#
using System;
public class insertionHeap {
static void heapify( int [] arr, int n, int i) {
int parent = (i - 1) / 2;
if (parent >= 0) {
if (arr[i] > arr[parent]) {
int temp = arr[i];
arr[i] = arr[parent];
arr[parent] = temp;
heapify(arr, n, parent);
}
}
}
static int insertNode( int [] arr, int n, int Key) {
n = n + 1;
arr[n - 1] = Key;
heapify(arr, n, n - 1);
return n;
}
static void printArray( int [] arr, int n) {
for ( int i = 0; i < n; ++i)
Console.WriteLine(arr[i] + " " );
Console.WriteLine( "" );
}
public static void Main( string [] args) {
int MAX = 1000;
int [] arr = new int [MAX];
arr[0] = 10;
arr[1] = 5;
arr[2] = 3;
arr[3] = 2;
arr[4] = 4;
int n = 5;
int Key = 15;
n = insertNode(arr, n, Key);
printArray(arr, n);
}
}
|
Javascript
let MAX = 1000;
function heapify(arr, n, i)
{
let parent = Math.floor((i-1)/2);
if (parent >= 0) {
if (arr[i] > arr[parent]) {
let temp = arr[i];
arr[i] = arr[parent];
arr[parent] = temp;
heapify(arr, n, parent);
}
}
}
function insertNode(arr, n, Key)
{
n = n + 1;
arr[n - 1] = Key;
heapify(arr, n, n - 1);
return n;
}
function printArray(arr, n)
{
for (let i = 0; i < n; ++i)
console.log(arr[i] + " " );
console.log( "</br>" );
}
let arr = [ 10, 5, 3, 2, 4 ];
let n = arr.length;
let key = 15;
n = insertNode(arr, n, key);
printArray(arr, n);
|
Time Complexity: O(log(n)) (where n is no of elements in the heap)
Auxiliary Space: O(n)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!