SlideShare a Scribd company logo
Can you give an example of a binary heap program
Can you give an example of a binary heap program
Solution
/*
* C Program to Implement a Heap & provide Insertion & Deletion Operation
*/
#include
int array[100], n;
main()
{
int choice, num;
n = 0;/*Represents number of nodes in the heap*/
while(1)
{
printf("1.Insert the element  ");
printf("2.Delete the element  ");
printf("3.Display all elements  ");
printf("4.Quit  ");
printf("Enter your choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter the element to be inserted to the list : ");
scanf("%d", &num);
insert(num, n);
n = n + 1;
break;
case 2:
printf("Enter the elements to be deleted from the list: ");
scanf("%d", &num);
delete(num);
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Invalid choice  ");
}/*End of switch */
}/*End of while */
}/*End of main()*/
display()
{
int i;
if (n == 0)
{
printf("Heap is empty  ");
return;
}
for (i = 0; i < n; i++)
printf("%d ", array[i]);
printf(" ");
}/*End of display()*/
insert(int num, int location)
{
int parentnode;
while (location > 0)
{
parentnode =(location - 1)/2;
if (num <= array[parentnode])
{
array[location] = num;
return;
}
array[location] = array[parentnode];
location = parentnode;
}/*End of while*/
array[0] = num; /*assign number to the root node */
}/*End of insert()*/
delete(int num)
{
int left, right, i, temp, parentnode;
for (i = 0; i < num; i++) {
if (num == array[i])
break;
}
if (num != array[i])
{
printf("%d not found in heap list ", num);
return;
}
array[i] = array[n - 1];
n = n - 1;
parentnode =(i - 1) / 2; /*find parentnode of node i */
if (array[i] > array[parentnode])
{
insert(array[i], i);
return;
}
left = 2 * i + 1; /*left child of i*/
right = 2 * i + 2; /* right child of i*/
while (right < n)
{
if (array[i] >= array[left] && array[i] >= array[right])
return;
if (array[right] <= array[left])
{
temp = array[i];
array[i] = array[left];
array[left] = temp;
i = left;
}
else
{
temp = array[i];
array[i] = array[right];
array[right] = temp;
i = right;
}
left = 2 * i + 1;
right = 2 * i + 2;
}/*End of while*/
if (left == n - 1 && array[i]) {
temp = array[i];
array[i] = array[left];
array[left] = temp;
}
}
Output:

More Related Content

DOCX
1- please please please don't send me an incomplete program this code.docx
PPT
Complete binary tree and heap
PPT
Heapsortokkay
PPTX
Data structures and algorithms lab10
PPTX
Data Structures and Agorithm: DS 18 Heap.pptx
PPT
Heap tree
PPT
Heaps & priority queues
1- please please please don't send me an incomplete program this code.docx
Complete binary tree and heap
Heapsortokkay
Data structures and algorithms lab10
Data Structures and Agorithm: DS 18 Heap.pptx
Heap tree
Heaps & priority queues

Similar to Can you give an example of a binary heap programCan you give an .pdf (20)

PDF
Find attached the code to implement the following two functions- 1- In.pdf
DOCX
1 Part I written exercises.1. Using the Red-Black Tre.docx
PPT
Algorithm
PDF
program on string in java Lab file 2 (3-year)
DOCX
Data Structures Using C Practical File
PPTX
05 heap 20161110_jintaeks
PDF
Heap DS.pdf.dew dehde. djcejfefcvikefv.fc d efvjefrufifeufwwef ev
PDF
1. Create the binary tree with 9 nodes (Figure 1) by using pointer-b.pdf
PPT
lecture 5
PPT
3.7 heap sort
PDF
I have code written for this problem but I am struggling finishing i.pdf
PDF
Heap Tree.pdf
PPT
Heapsort
PPTX
Heaps and tries power point this is an educational material
PDF
4 heapsort pq
PPT
chapt11.ppt
PDF
CS-102 Course_ Binary Tree Lectures .pdf
PPTX
Hash table and heaps
PDF
Binary Tree
Find attached the code to implement the following two functions- 1- In.pdf
1 Part I written exercises.1. Using the Red-Black Tre.docx
Algorithm
program on string in java Lab file 2 (3-year)
Data Structures Using C Practical File
05 heap 20161110_jintaeks
Heap DS.pdf.dew dehde. djcejfefcvikefv.fc d efvjefrufifeufwwef ev
1. Create the binary tree with 9 nodes (Figure 1) by using pointer-b.pdf
lecture 5
3.7 heap sort
I have code written for this problem but I am struggling finishing i.pdf
Heap Tree.pdf
Heapsort
Heaps and tries power point this is an educational material
4 heapsort pq
chapt11.ppt
CS-102 Course_ Binary Tree Lectures .pdf
Hash table and heaps
Binary Tree
Ad

More from arorasales234 (20)

PDF
Our best evidence to data suggests that the Earth formed 4.6 billion .pdf
PDF
one human resource manager recently got a thank you note on her iPho.pdf
PDF
Mutations rII1 and rII2 are known to fall into the rIIA and rIIB cis.pdf
PDF
Money laundering has become a mechanism for financing terrorist acti.pdf
PDF
Let X and Y be continuous r.v. with joint density functionPlease g.pdf
PDF
Independent auditors must provide dual opinions. What are these two .pdf
PDF
Imperialism and Nationalism are often incongruous concepts that exis.pdf
PDF
Imagine that you plant the massive seed from inside an avocado. You p.pdf
PDF
Identify three traits each that distinguish channels from carriers. .pdf
PDF
i am trying to get this library file income.js using this, to be loa.pdf
PDF
Explain why it can be problematic to base morality and ethics only o.pdf
PDF
Discuss the reasons why the Human Microbiome Project is a step forwa.pdf
PDF
Describe what is happening in Italy (or you may substitute Argentina.pdf
PDF
Consider an extension of the queue abstract data type, called a .pdf
PDF
Cordia Corporation is planning a 15 year project with an initial inv.pdf
PDF
Case study on Queensland Health Payroll Debacle & Queensland Governm.pdf
PDF
All businesses, small or large, depend on data centers. Answer the f.pdf
PDF
A) The linearity condition is not satisfied. B) The linearity cond.pdf
PDF
4. Records of the missions of Baja California in the form of baptism.pdf
PDF
4. Cafazzo et al. studied a group of stray dogs in Rome, and ranked t.pdf
Our best evidence to data suggests that the Earth formed 4.6 billion .pdf
one human resource manager recently got a thank you note on her iPho.pdf
Mutations rII1 and rII2 are known to fall into the rIIA and rIIB cis.pdf
Money laundering has become a mechanism for financing terrorist acti.pdf
Let X and Y be continuous r.v. with joint density functionPlease g.pdf
Independent auditors must provide dual opinions. What are these two .pdf
Imperialism and Nationalism are often incongruous concepts that exis.pdf
Imagine that you plant the massive seed from inside an avocado. You p.pdf
Identify three traits each that distinguish channels from carriers. .pdf
i am trying to get this library file income.js using this, to be loa.pdf
Explain why it can be problematic to base morality and ethics only o.pdf
Discuss the reasons why the Human Microbiome Project is a step forwa.pdf
Describe what is happening in Italy (or you may substitute Argentina.pdf
Consider an extension of the queue abstract data type, called a .pdf
Cordia Corporation is planning a 15 year project with an initial inv.pdf
Case study on Queensland Health Payroll Debacle & Queensland Governm.pdf
All businesses, small or large, depend on data centers. Answer the f.pdf
A) The linearity condition is not satisfied. B) The linearity cond.pdf
4. Records of the missions of Baja California in the form of baptism.pdf
4. Cafazzo et al. studied a group of stray dogs in Rome, and ranked t.pdf
Ad

Recently uploaded (20)

PDF
A systematic review of self-coping strategies used by university students to ...
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Cell Types and Its function , kingdom of life
PDF
01-Introduction-to-Information-Management.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Lesson notes of climatology university.
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
GDM (1) (1).pptx small presentation for students
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Computing-Curriculum for Schools in Ghana
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
A systematic review of self-coping strategies used by university students to ...
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Complications of Minimal Access Surgery at WLH
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Final Presentation General Medicine 03-08-2024.pptx
Cell Types and Its function , kingdom of life
01-Introduction-to-Information-Management.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Lesson notes of climatology university.
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Microbial disease of the cardiovascular and lymphatic systems
Abdominal Access Techniques with Prof. Dr. R K Mishra
GDM (1) (1).pptx small presentation for students
STATICS OF THE RIGID BODIES Hibbelers.pdf
human mycosis Human fungal infections are called human mycosis..pptx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Computing-Curriculum for Schools in Ghana
Chapter 2 Heredity, Prenatal Development, and Birth.pdf

Can you give an example of a binary heap programCan you give an .pdf

  • 1. Can you give an example of a binary heap program Can you give an example of a binary heap program Solution /* * C Program to Implement a Heap & provide Insertion & Deletion Operation */ #include int array[100], n; main() { int choice, num; n = 0;/*Represents number of nodes in the heap*/ while(1) { printf("1.Insert the element "); printf("2.Delete the element "); printf("3.Display all elements "); printf("4.Quit "); printf("Enter your choice : "); scanf("%d", &choice); switch(choice) { case 1: printf("Enter the element to be inserted to the list : "); scanf("%d", &num); insert(num, n); n = n + 1; break; case 2: printf("Enter the elements to be deleted from the list: "); scanf("%d", &num); delete(num);
  • 2. break; case 3: display(); break; case 4: exit(0); default: printf("Invalid choice "); }/*End of switch */ }/*End of while */ }/*End of main()*/ display() { int i; if (n == 0) { printf("Heap is empty "); return; } for (i = 0; i < n; i++) printf("%d ", array[i]); printf(" "); }/*End of display()*/ insert(int num, int location) { int parentnode; while (location > 0) { parentnode =(location - 1)/2; if (num <= array[parentnode]) { array[location] = num; return; } array[location] = array[parentnode]; location = parentnode;
  • 3. }/*End of while*/ array[0] = num; /*assign number to the root node */ }/*End of insert()*/ delete(int num) { int left, right, i, temp, parentnode; for (i = 0; i < num; i++) { if (num == array[i]) break; } if (num != array[i]) { printf("%d not found in heap list ", num); return; } array[i] = array[n - 1]; n = n - 1; parentnode =(i - 1) / 2; /*find parentnode of node i */ if (array[i] > array[parentnode]) { insert(array[i], i); return; } left = 2 * i + 1; /*left child of i*/ right = 2 * i + 2; /* right child of i*/ while (right < n) { if (array[i] >= array[left] && array[i] >= array[right]) return; if (array[right] <= array[left]) { temp = array[i]; array[i] = array[left]; array[left] = temp; i = left; }
  • 4. else { temp = array[i]; array[i] = array[right]; array[right] = temp; i = right; } left = 2 * i + 1; right = 2 * i + 2; }/*End of while*/ if (left == n - 1 && array[i]) { temp = array[i]; array[i] = array[left]; array[left] = temp; } } Output: