Hey guys.. thanks in advance for whosoever helps and gives their views..

So I am facing some problems with my CS program... which is in c++ and is about heaps.. its pretty basic stuff..

Here's the Heap.cpp (implementation file)

/** @file Heap.cpp */

#include "Heap.h"  // header file for class Heap


Heap::Heap() : size(0)
{
}  // end default constructor

Heap::~Heap()
{
}  // end destructor

bool Heap::heapIsEmpty() const
{
   return bool(size == 0);
}  // end heapIsEmpty

void Heap::heapInsert(const HeapItemType& newItem)
// Method: Inserts the new item after the last item in the
// heap and trickles it up to its proper position. The
// heap is full when it contains MAX_HEAP items.
{
	int numCom = 0;
   // place the new item at the end of the heap
   items[size] = newItem;

   // trickle new item up to its proper position
   int place = size;
   int parent = (place - 1)/2;
   while (numCom++, (parent >= 0) &&
           (items[place].getKey() > items[parent].getKey()) )
   {  // swap items[place] and items[parent]
      HeapItemType temp = items[parent];
      items[parent] = items[place];
      items[place] = temp;

      place = parent;
      parent = (place - 1)/2;
   }  // end while

   ++size;
}  // end heapInsert

void Heap::heapDelete(HeapItemType& rootItem)
// Method: Swaps the last item in the heap with the root
// and trickles it down to its proper position.
{
   rootItem = items[0];
   items[0] = items[--size];
   heapRebuild(0);
}  // end heapDelete

void Heap::heapRebuild(int root)
{
   // if the root is not a leaf and the root's search key
   // is less than the larger of the search keys in the
   // root's children
   int child = 2 * root + 1;  // index of root's left
                              // child, if any
   if ( child < size )
   {  // root is not a leaf, so it has a left child at child
      int rightChild = child + 1;  // index of right child,
                                   // if any

      // if root has a right child, find larger child
      if ( (rightChild < size) &&
           (items[rightChild].getKey() > items[child].getKey()) )
         child = rightChild;  // index of larger child

      // if the root's value is smaller than the
      // value in the larger child, swap values
      if ( items[root].getKey() < items[child].getKey() )
      {  HeapItemType temp = items[root];
         items[root] = items[child];
         items[child] = temp;

         // transform the new subtree into a heap
         heapRebuild(child);
      }  // end if
   }  // end if

   // if root is a leaf, do nothing
}  // end heapRebuild
// End of implementation file.

So when I call "heapDelete(HeapItemType& rootItem)" function in the driver file..
I don't know what to pass as argument.

Please let me know how I can explain more to help you guys help me.. and another thing.. I'll keep updating the thread for further problems I have with the program..

Thank you!!

When you post C++ code like this, also post the class header. Without it, we are flying blind, so to speak. Also, explain what your Heap class is supposed to be doing - what it's purpose is, as clearly as possible. If we know your intention, we can better advise you.

So when I call "heapDelete(HeapItemType& rootItem)" function in the driver file..
I don't know what to pass as argument.

It looks like you're just saving the top of the heap before removing it. rootItem would be any temporary variable you're using in your code:

while (!h.heapIsEmpty()) {
    HeapItemType top;

    h.heapDelete(top);
    std::cout<<"Removing '"<< top <<"'\n";
}

Some more information on HeapItemType would be useful though. Did you write this Heap class?

Edited 5 Years Ago by Narue: n/a

Thanks guys!! That answers my question.. really appreciate it!!

This question has already been answered. Start a new discussion instead.