Hello ,
i am working on a question and the task i need to do is implement a priority queue using min heap and max heap . i was able to get my max heap to work and thought changing the signs would do the opposite (min heap ) but it does not seem to work . Any inputs ?
The percolate up and down is a lil jumbled , but for insert i use percolate up , thinking percolate down will do the opposite , it would still do the same .
The code snippet :
Code:
template <class T>void MinHeapTest<T>::PercolateDown(int head,int tail)
{
int maxChild;
int right;
int left;
MinHeap<T> temp;
left = head * 2 + 1;
right = head * 2 + 2;
if(left <= tail)
{
if(left == tail)
{
maxChild = left;
}
else
{
if(i1[left].getpriority() <= i1[right].getpriority())
maxChild = right; //right
else
maxChild = left; //left
}
if(i1[head].getpriority() > i1[maxChild].getpriority())
{
temp = i1[head];
i1[head] = i1[maxChild];
i1[maxChild] = temp;
PercolateDown(maxChild,tail);//tail
}
}
}
template <class T>
void MinHeapTest<T>::PercolateUP(int head,int tail)
{
int temp1;
MinHeap<T> temp2;
if(tail > head)
{
temp1 = (tail -1) / 2;
if(i1[temp1].getpriority() < i1[tail].getpriority())
{
temp2 = i1[temp1];
i1[temp1] =i1[tail];
i1[tail] = temp2;
PercolateUP(head, temp1);
}
}
}
i1 is the array which holds the values each with individual priorities .