tags:

views:

98

answers:

2

I need to create a minheap template which includes nodes in it.

The problem I have is that I don't know if I need to create a node template class as well, or should it be included inside the heap template class as a struct?

+4  A: 

Min heaps aren’t usually (never?) implemented using explicit nodes – since a heap is always left-filled (“complete”) and thus has a well-defined structure, that would be unnecessarily inefficient since the handling of nodes and node links introduces quite a bit of overhead, not to mention destroying locality of reference, leading to cache misses and poor performance.

(The same goes for max heaps of course.)

Instead, they are implemented using arrays. In fact, the C++ standard library already includes the functions make_heap, push_heap and pop_heap to work on iterator Ranges. Use them in conjunction with a vector and you got your heap.

Konrad Rudolph
the thing is that i do need nodes ... i need to have the spot of the node inside the heap for extracting him in log(n) run time , i need to build a data structure which contains a map and a min heap and those two structures should be able to communicate with each other so i will be able to extract / update /remove in log(n) run time i thought about having nodes inside both of those structures and letting them communicate with eachother with having the spot of the node inside the heap .
Nadav Stern
No, as both containers will be trying to manage the memory for the nodes. Use the std:: heap implementation with pointers to your other data and an appropriate comparison functor. You get O(lnN) anyway without having a pointer to the node inside the heap.
Pete Kirkham
@Navad: no, that’s not how a heap works. A heap is a well-defined data structure with well-defined algorithms for its efficient operations. What you’re describing is not a heap – it’s a different data structure that mimics a heap. Have a look at your trusted algorithms text book or the NIST directory for algorithms and data structures or your lecture notes to see how a heap is supposed to work.
Konrad Rudolph
+2  A: 

Basically the nodes aren't needed to be implemented with the nodes as template class. The implementation might be something like this declaration:

template <class T>
class MinHeap {
private:
    std::vector<T> _heap;
    int _maxSize;
    int _size;

public:
    MinHeap(int maxSize);
    ~MinHeap();
    void Insert(T elem);
     T RemoveMin();

private:
    int LeftChild(int pos);
    int RightChild(int pos);
    int Parent(int pos);
    boolean IsLeaf(int pos);
    void Swap(int pos1, int pos2);
    void PushDown(int position);
};
rursw1
thanks for your answer but i didnt understand ,where do the nodes should be ? should i implement a node class that should be a template 1 and the minheap will get this node as an input (class T)?
Nadav Stern
@Nadav, this means that the type of the nodes is depended on your decision in the place of T. T can be an integer, a character, or any class which implements the comparison operators. For example: MinHeap<int> intMeanHeap;
rursw1