2. PRESENTATION TOPIC :Write a program to construct a Heap and perform deletion, insertion,
search, update. Also find the Smallest and Largest Elements in the Heap and Find the Common Ancestor
and Print the Path.
Presented to:
Dr. Sheak Rashed Haider Noori
Assistant Professor
Department Of CSE
Daffodil Internal University
• Presented by:
• Anirudha Dhar Parash (152-15-5742)
• S.M. Zahidul Islam (152-15-5925)
• Samyamay Howlader (152-15-5913)
• Muzahidul Islam (152-15-5585)
• Sharuk Ahmed (152-15-5796)
6. DELETE FUNCTION
void delete(int array[],int size )
{
int i=size-1;
array[0]=array[i];
i--;
heap_sort(array,i);
}
void heap_sort(int array[],int size)
{
int i,c,root,temp;
7. for (i = 1; i <=size; i++)
{
c = i;
do
{
root = (c - 1) / 2;
if (array[root] < array[c])
{
temp = array[root];
array[root] = array[c];
array[c] = temp;
}
c = root;
}
while (c != 0);
}
}
8. DISPLAY FUNCTION
void display(int array[],int n)
{
int i;
if (n == 0)
{
printf("Heap is empty n");
return;
}
for (i = 0; i < n; i++)
printf("%d ", array[i]);
printf("n");
}
10. SEARCH FUNCTION
void search(int array[],int n)
{
int i,key;
printf("Enter the number that you search:");
scanf("%d",&key);
if (n == 0)
{
printf("Heap is empty n");
return;
}
for (i = 0; i < n; i++)
{
if(array[i]==key)
{
printf("The number is found");
}
}
}
11. (Max & Min Num Function)
Muzahidul Islam
ID: 152-15-5585
12. MAX NUM FUNCTION
void max_num(int array[],int n)
{
int i,max;
max=array[0];
for (i = 0; i < n; i++)
{
if(array[i]>max)
{
max=array[i];
}
}
printf("The maximum number in heap: %d",max);
}
13. MIN NUM FUNCTION
void min_num(int array[],int n)
{
int i,min;
min=array[0];
for (i = 0; i < n; i++)
{
if(array[i]<min)
{
min=array[i];
}
}
printf("The minimum number in heap: %d",min);
}
15. COMMON ANCESTOR FUNCTION
void ancestor(int array[],int n,int num)
{
int i,a[100];
if (n == 0)
{
printf("Heap is empty n");
return;
}
for (i = 0; i < n; i++)
{
a[i]=array[i];
if(array[i]==num)
{
break;
}
}
17. UPDATE FUNCTION
void update(int array[],int n )
{
int i,key,num;
printf("enter the number that is to be delete : ");
scanf("%d",&key);
printf("enter the number that you update : ");
scanf("%d",&num);
for(i=0; i<n; i++)
{
if(array[i]==key)
{
array[i]=num;
}
}
heap_sort(array,n);
printf("the number is updated : ");
}
18. MAIN FUNCTION
int main()
{
int choice, num;
n = 0;
while(1)
{
printf("1.Insert the element n");
printf("2.Delete the element n");
printf("3.Display all elements n");
printf("4.Search element: n");
printf("5.Search max number: n");
printf("6.Search min number: n");
printf("7.Find the Common Ancestor: n");
printf("8.Update an element: n");
printf("9.Quit n");
printf("Enter your choice : ");