0% found this document useful (0 votes)
21 views1 page

CS301P Lab 11 Solution Max Heap

Uploaded by

hejaz589
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views1 page

CS301P Lab 11 Solution Max Heap

Uploaded by

hejaz589
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

// CS301P Lab 11 Problem KST Learning

#include<iostream>
#include<conio.h>
using namespace std;

class Heap
{
private:
int *array, current;

public:
Heap(int size)
{
array = new int[size++];
current = 0;
}

void insert(int value)


{
int index = ++current;
for(; index > 1 && value > array[index/2]; index /= 2)
{
array[index] = array[index/2];
}
array[index] = value;
}

void traverse()
{
for(int i=1; i<=current; i++)
{
cout << array[i] << " ";
}
}
};

main()
{
int size = 16;
int array[size] = {18, 31, 82, 85, 37, 20, 23, 79, 47, 51, 96, 97, 42, 94,
57, 29};
Heap obj(size);
for(int i=0; i<size; i++)
{
obj.insert(array[i]);
}
cout << "*** Max Heap Using Insert Method ***\n\n";
obj.traverse();
getch();
return 0;
}

You might also like