Question

c++ *construct (draw) a Max Heap when the following keys are inserted in the given order:...

c++

*construct (draw) a Max Heap when the following keys are inserted in the given order:

5, 10, 4, 3, 1, 15, 11, 12, 20, 19, 2

*What is the index of the right child of key 20?

*Draw the max Heap after performing removeMax()

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code -

#include <iostream>
#include <conio.h>
using namespace std;
void max_heapify(int *a, int i, int n)
{
int j, temp;
temp = a[i];
j = 2 * i;
while (j <= n)
{
if (j < n && a[j+1] > a[j])
j = j + 1;
if (temp > a[j])
break;
else if (temp <= a[j])
{
a[j / 2] = a[j];
j = 2 * j;
}
}
a[j/2] = temp;
return;
}
void build_maxheap(int *a,int n)
{
int i;
for(i = n/2; i >= 1; i--)
{
max_heapify(a,i,n);
}
}
int main()
{
int n, i, x;
int a[] ={5, 10, 4, 3, 1, 15, 11, 12, 20, 19, 2};
n =(sizeof(a)/sizeof(*a));
build_maxheap(a,11);
cout<<"Max Heap\n";
for (i = 1; i <= n; i++)
{
cout<<a[i]<<endl;
}
getch();
}

Screenshots -

Add a comment
Know the answer?
Add Answer to:
c++ *construct (draw) a Max Heap when the following keys are inserted in the given order:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT