Heap Maintenance

Code Id 21
Date Updated 3/7/2010
Title Heap maintenance  
Description
This is a program illustrating heap maintenance. It supports insertion and deletion in heap. 
                                  
Codes Snippet
# include 
int arr[100],n;
main( )
{
int choice,num;
n=0;
/*Represents number of nodes in the heap*/
 while( 1)
{
printf("l.Insertn");
printf("2.Deleten"); 
printf("3.Displayn"); 
printf("4.Quitn");
printf("Enter your choice: ");
scanf("%d" ,&choice); 
switch( choice)
{
case 1:
printf("Enter the number to be inserted: ");
scanf("%d" ,&num);
insert(num,n); .
n=n+ 1;
break;
case 2:
printf("Enter the number to be deleted: ");
scanf("%d" ,&num);
del(num);
break;
case 3:
display( );
break;
        case 4:
        exit( );
        default:
        printf("Wrong choicen");
        } /*End of switch * /
        }/*End of while */
}/*End of main( )*/
display( ) 
{ 
int i;
        if(n==0)
        {
printf("Heap is emptyn"); 
return;
}
for(i=0;i0) 
{
par=(1oc-l )/2; 
if(num<=arr[par ]) 
{
arr[loc ]=num;
return;
}
arr[loc ]=arr[par];
loc=par;
}/*End of while*/
arr[0]=num; /*assign null to the root node */ 
}/*End of insert( )*/
del(int num) 
{
int left,right,i,temp,par; 
for(i=0;i arr[par])
{
insert( arr[i],i); 
return;
}
left=2 *i+ 1; /*left child of i * /
right=2*i+2; /* right child of i*/
while(right < n)
{
if( arr[i]>=arr[left] && arr[i]>=arr[right] )
                        return;
if( arr[right]<=arr[left] )
{
temp=arr[i];
arr[i]=arr[left];
arr[left ]=temp;
i=left;
} 
else
{
temp=arr[i]; 
arr[i]=arr[ right];
arr[ right]=temp; 
i=right;
}
left=2*i+ 1;
right=2*i+2;
}/*End ofwhile*/
if( left==n-l && arr[i]

Comments are closed.