Link to home
Start Free TrialLog in
Avatar of jdixon
jdixon

asked on

Heap help!

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.
Avatar of nietod
nietod

I'm not familar with this terminology.  By binary tree heap class, do you mean a class for implementing a heap that uses a binary tree?  Or do you mean a binary tree class that uses the heap to store the nodes?
Avatar of jdixon

ASKER

I apologize.

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'm afraid I'm more curious than helpful in this area.  It seems strange to me that you want to impliment a heap as a class.  I can't image what the advantage would be.  For example, you want code for a copy constructor.  What should the copy constructor do?  It shouldn't copy the data in the heap, there would be no points to the data so it would never get freed.  I gues the copy constructor could create a new heap of the same size as the source heap, but with no objects allocated in it?  Is that what you were planning?
Avatar of jdixon

ASKER

Sorry Nietod, I guess this query is more dicey than I thought.

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.
The copy constructor could copy the data from the source heap.  But what would be the point?  The data in the source heap is being "used" in the sense that whatever created the data (requested it from the heap) has pointers to the data and can use the data.  but if you create a copy of the data, no one has pointers to the copies so no one can use the data.  Its kinda like

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.
Avatar of jdixon

ASKER

Okay, thanks alot nietod.
ASKER CERTIFIED SOLUTION
Avatar of sganta
sganta

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jdixon

ASKER

Yes, yes, yes. You hit it on the head. Thanks a lot.