peak wrote:
I have this code that I am using to create a heap
template <class t>
void BinaryHeap<t>::insert( const T &x)
{
int hole = ++ currentSize;
for ( ; hole > 1 && x < array[hole/2];hole /= 2)
arrsy[hole] = arrsy[hole/2];
array[hole] =x;
}
I am not allowed to change this code. I want to use the binary heap as
a queue to store Huffman nodes(trees).
How would I do it so that the comparison in the for loop is correct?
Aside from the spelling errors that John pointed out it's not clear to
me what your problem is. Your code appears to represent a binary heap
as a tree in array form and appears to do the right things required when
inserting an object into the heap.
Of course it's required that operator< be defined for items of type T.
You can define these yourself as needed in one of two ways:
// non-member function
bool operator< (const T& lhs, const T& rhs) { ... }
// member function
class T
{
...
bool operator< (const T& rhs) const { ... }
...
};