i need help building heap class - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

i need help building heap class

babymanbabyman Posts: 2Member
I have not used c++ for long time as i am just minoring in computer science.
I had an assignment in which i was required to do the following:
You are asked to provide a C++ class to implement a MAX heap of integers.
Your class should have the appropriate constructors (including a copy-constructor)
and destructor. In addition, your class should provide the following operators and
(a) operator+ to allow for adding of two heaps (e.g., heap3 = heap1 +
heap2 results in heap3 contacting the element of heap1 and heap2).
(b) operator+ to allow for adding a single integer to a heap (e.g., heap2 =
heap1 + 5 results in heap2 containing the elements of heap and the value
(c) operator[] to allow for accessing the individual elements of the heap as
they were stored in a sorted array (e.g., heap[5] accesses the 5th largest
element in the heap).
(d) operator= to allow heap assignments (e.g. heap1 = heap2 = heap3)
(e) operator+= to allow heap assignments (e.g. heap1 += heap2 and
heap3 += 5)
(f) operator<< to allow for printing heaps on cout (e.g. cout << heap1
prints heap1 on cout).

Note: for an example of a basic MAX heap class see:

I built this code and need somebody to refer me 2 my mistakes. i really appreciate ur help.

header file:
#ifndef HEAP_H
#define HEAP_H

class heap
friend ostream &operator<<( ostream&, const heap & );

Type *array;
int MaxSize, Nel;
// Max. size of the heap, no. of elements
void Adjust(Type a[], int i, int n);
heap(int MSize):
heap (const heap &);
heap &operator+ (const heap &);
heap &operator+=(const heap &);
heap &operator+ (int );
operator=( const heap &);
operator[]( int);
bool Insert(Type item);
// Insert item.
bool DelMax(Type& item);
// Delete the maximum.

cpp file:
#include <iostream>
#include "heap.h"
using namespace std;

heap:: heap(int size)
array = new Type[MaxSize+1];

delete []array;

heap::heap (const heap &a)
array=new Type [MaxSize+1];
for (int i=0;i=MaxSize;i++)
friend ostream &operator<<( ostream &output, const heap &a)
int i;

// output private ptr-based array
for ( i = 0; i = a.MaxSize; i++ ) {
output << setw( 12 ) << a.array[ i ];

if ( ( i + 1 ) % 4 == 0 ) // 4 numbers per row of output
output << endl;

} // end for

if ( i % 4 != 0 ) // end last line of output
output << endl;

return output; // enables cout << x << y;

} // end function operator<<

const heap::heap &operator+ (const heap &heap1)
Type *array1;
int x=sizeof(heap1)/4;
for (int i=0;i=MaxSize;i++)
for (i=Maxsize;i=x+MaxSize;i++)
delete []array1;

const heap::heap &operator+=(const heap &right)
Type *array1;
array=new Type[MaxSize+1];
for (int i=0;i=MaxSize;i++)
delete []array;
Type *array;
array=new Type[MaxSize+1];
for (i=0;i=MaxSize1;i++)
for (i=MaxSize1+1;i=MaxSize;i++)

const heap::heap &operator+ (int x)

const heap &heap::operator=( const heap &right )
if ( &right != this ) // check for self-assignment
// for arrays of different sizes, deallocate original
// left-side array, then allocate new left-side array
if ( MaxSize != right.size )
delete [] array; // reclaim space
MaxSize = right.MaxSize; // resize this object
array = new int[ MaxSize ]; // create space for array copy

} // end inner if

for ( int i = 0; i < MaxSize; i++ )
array[ i ] = right.array[ i ]; // copy array into object

} // end outer if
return *this; // enables x = y = z, for example

} // end function operator=

int &heap::operator[]( int subscript )
// check for subscript out of range error
if ( subscript < 0 || subscript >= MaxSize ) {
cout << "
Error: Subscript " << subscript
<< " out of range" << endl;

exit( 1 ); // terminate program; subscript out of range

} // end if

return array[ subscript ]; // reference return

} // end function operator[]

bool heap::Insert(Type item)
{ // Inserts item.
int i = ++Nel;
if (i==MaxSize)
cout << "heap size exceeded"
<< endl;
return false;
while ((i>1) && (array[i/2]= a[j])
// A position for item is found.
a[j/2] = a[j];
j *= 2;
a[j/2] = item;

bool heap::DelMax(Type& item)
if (!Nel)
{ cout << "heap is empty"<< endl;
return false;
Adjust(array, 1, Nel);
return true;
Sign In or Register to comment.