#include<stdio.h>
void buildHeap(int [],int);
void maxHeapify(int [],int,int);
int main()
{
printf("\nThe number of the node are “);
int n;
scanf(”%d",&n);
int arr[n];
printf("\nEnter the nodes value :");
for(int i=1;i<=n;i++)
scanf("%d",&arr[i]);
int i=arr[1];
buildHeap(arr,n);
for(i=1;i<=n;i++)
printf("%d “,arr[i]);
int dell=heapExtractMax(arr,n);
printf(”\nElement which got deleted is %d",dell);
printf("\nHeap after extracting the max “);
for(i=1;i<=n;i++)
printf(”%d ",arr[i]);
return 0;
}
void buildHeap(int arr[],int n)
{
int i;
for(i=n/2;i>=1;i–)
maxHeapify(arr,i,n);
}
void maxHeapify(int arr[],int i,int n)
{
int largest;
int left=(2i)+1;
int right=(2i)+2;
if((left<=n) && arr>arr[i])
largest=left;
else
largest=i;
if((right<=n) && arr[right]>arr[largest])
largest=right;
if(largest!=i)
{
int temp=arr[i];
arr[i]=arr[largest];
arr[largest]=temp;
maxHeapify(arr,largest,n);
}
}
int heapExtractMax(int arr[],int n)
{
int max =arr[1];
arr[1]=arr[n];
n=n-1;
maxHeapify(arr,1,n);
return max;
}