I'm trying to implement the binary heap. I've successfully implemented display()
and insert()
functions.
Problem : Inside deleteHeap()
function
Approach 1.Copy the heap's root into a variable
2.Now copy the heap's last element into the root.Decrement the heap node count n
3.Now check if the property of heap is violated.
4.Start from top.Mark root node as parent node. Check if any of the children are greater than the parent node.
5.Swap the largest child with parent and mark this child as new parent.
6.If parent_index<=n/2 return;
7.Else goto step 5.
Note The program is behaving anomalously while running. I recommend you to run it once.!
**Code**
#include<stdio.h>
#include<stdlib.h>
int a[100],n=0;
void display()
{
int i=1;
printf("\nThe heap contains - ");
while(i<=n)
printf("%d ",a[i++]);
}
void fixheap1()
{
int j=n,temp;//Child's index
int i=n/2;//Parent's index
while(a[j]>a[i] && i!=0)
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
i=i/2;
j=j/2;
}
}
void insert(int key)
{
a[++n]=key;
fixheap1();
}
int findGreatest(int j,int k)
{
if(a[j]>a[k])
return j;
else
return k;
}
void fixheap2()
{
int i=1,j,k,temp,max;
while(i<=n/2)
{
j=2i;
k=2i+1;
if(k>n && j<=n)
max=j;
else
if(j>n)
break;
else
max=findGreatest(j,k);
temp=a[max];
a[max]=a[i];
a[i]=temp;
i=max;
}
}
void deleteHeap()
{
int key=a[1];
a[1]=a[n];
n--;
fixheap2();
printf("\nElement successfully deleted %d",key);
}
int main()
{
int i,choice=0,key;
while(choice!=5)
{
system("clear");
printf("\n\t\tHeaps\n1.Insert into heap\n2.Display heap\n3.Heap sort\n4.Delete from heap\n5.Exit\n\nChoice - ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\nEnter the number of elements to be inserted - ");
scanf("%d",&i);
printf("\nEnter the elements - ");
while(i--)
{
scanf("%d",&key);
insert(key);
}
printf("\nElements successfully inserted");
break;
case 2: display();
break;
case 3:
break;
case 4: deleteHeap();
break;
}
getchar();
getchar();
}
printf("\n\nThank You\n\n");
}
Edit 1: After someone pointed out the mistake I've updated my function
void fixheap2()
{
int i=1,j,k,temp,max;
while(i<=n/2)
{
j=2i;
k=2i+1;
if(k>n && j<=n) //k does not exists
max=j;
else
if(j>n) //j also doesn't exist
break;
else
max=findGreatest(j,k); //find the index of greater
if(a[max]>a[i]) //if the child is greater than root
{
temp=a[max];
a[max]=a[i];
a[i]=temp;
i=max;
}
else
break;
}
}
Sample Input and Output :
Heaps
1.Insert into heap
2.Display heap
3.Heap sort
4.Delete from heap
5.Exit
Choice - 1
Enter the number of elements to be inserted - 6
Enter the elements - 6 5 4 3 2 1
Elements successfully inserted
Heaps
1.Insert into heap
2.Display heap
3.Heap sort
4.Delete from heap
5.Exit
Choice - 2
The heap contains - 6 5 4 3 2 1
Heaps
1.Insert into heap
2.Display heap
3.Heap sort
4.Delete from heap
5.Exit
Choice - 4
Element successfully deleted 6
Heaps
1.Insert into heap
2.Display heap
3.Heap sort
4.Delete from heap
5.Exit
Choice - 2
The heap contains - 1 5 4 3 2
Heaps
1.Insert into heap
2.Display heap
3.Heap sort
4.Delete from heap
5.Exit
Choice - 4
Element successfully deleted 1