Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

I have a working array class that I am trying to base a (Binary tree) heap class on. I would like to know the code for (or at least get some direction on) the following code for the heap class: the copy constructor, the upheap, and the downheap. I have everything else I need. Let me mention that although this is for homework, it is more for self study.

Thank you very much.

Thank you very much.

What I mean is a class for implementing a heap using a binary tree. The binary tree is represented using a dynamic array. If you like, I can give you the code for the array class.

I think my instructor wanted to use this excerise to show us how dynamic arrays and trees can be related.

The only thing that I can think of for the copy constructor is that it will allocate memory for a heap that will be the same size as the source heap. After the memory has been allocated the data would be copied into the newly allocated array, probably with a small loop. The destructor (I guess) would release that dynamic memory.

Like I said, I can give you the .h and .cpp files that I have, but if this is getting too sticky I would understand because believe me I¹m more confused than you are.

char *StrPtr1 = new char[100]; // this is useful.

new char[100]; // This is useless. I can't access the data created!

Are you sure you want a copy constructor? My intuition would be that the class should declare a private copy constructor and not implement it. This technique prevents the class from being copied. This makes sense to me. It yould allow you to declare the heap class objects, but could not pass them by value.

I'd be happy to look at the code, but I'm going away today. I'll be back next tuesday. hopefully you'll get more help before then.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

I am having 2 solutions for creating HEAP with different definitions.

I SOLUTION :

------------

HEAP : A heap is a complete binary tree with the property that the value at each node is at least

as large as its children (if they exist).

#include <iostream.h>

const int MAX = 80;

/* If you want you can include this in Class object also */

void heap(int heap_array[],int n);

void main(void)

{

int arr[MAX],i,j,n;

/* First you store all the elements in the ARRAY

whatever order it is */

cout << "Enter the no. of elements in the array : ";

cin >> n;

for (i=0;i<n;i++)

{

cout<<"Enter the element "<<i<<" : ";

cin >> arr[i];

}

/* Assuming that 1st element in the array is heaped

and from 2 element onwards this will place in a proper position */

if (n > 0) // If there is more than one record then only heap is required

for (i=1; i<n; i++)

heap(arr,i);

}

void heap(heap_array[],n) /* Heap Creation by inserting one item at a time

Inserts the value in heap_array[n] into the heap which

is stored at heap_array[1] to heap_array[n-1] */

{

int i,j,k;

j = n;

i = n/2;

item = heap_array[n];

while (i>=0 && heap_array[i] < item)

{

heap_array[j] = heap_array[i]; /* Move the parent down */

j = i;

i /= 2; /* Parent of heap i is at i/2 */

}

heap_array[j] = item; /* A place for heap_array[n] is found */

}

II SOLUTION

This is another algorithm for creating a heap which has the nice property that its worst

case time is an order of magnitude faster than n-1 calls of above program.

#include <iostream.h>

const int MAX = 80;

/* If you want you can include this in Class object also */

void adjust(int heap_array[],int i,int n);

void heapify(int heap_array[],int n);

void main(void)

{

int arr[MAX],i,j,n;

/* First you store all the elements in the ARRAY

whatever order it is */

cout << "Enter the no. of elements in the array : ";

cin >> n;

for (i=0;i<n;i++)

{

cout<<"Enter the element "<<i<<" : ";

cin >> arr[i];

}

heapify(arr,n);

}

void heapify(int heap_array[],int n)

{

/* Readjust the elements in heap_array(0:n-1) to form a heap */

int i,j;

j = n/2;

for (i=j;i>=0;i--)

adjust(heap_array,i,n);

}

void adjust(int heap_array[],int i, int n)

{

/* The complete binary trees with roots heap(2*i) and heap(2*i+1) are

combined with heap(i) to form a single heap, 0<= i < n

No node has an address >= n or less than 0 */

int j,item,k;

j = 2*i;

item = heap_array[i];

while ( j < n)

{

if ( j<(n-1) && heap_array[j] < heap_array[j+1] ) /* Compare left and right childs */

j++;

if (item >= heap_array[j])

return; /* item has been already in heap order */

else

{

k = j/2;

heap_array[k] = item;

}

}

k = j/2;

heap_array[k] = item;

return;

}

I hope these solutions are more than sufficient. If you have any problem, you are

welcome to contact me either EE or through my E-Mail sganta@ch.oracle.com

JESUS LOVES YOU - sganta