I'm trying to write a function to maintain order in my priority queue however it doesn't seem to be working. The program compiles fine but i dont get the output in the correct order. I implemented the priority queue with a dynamically allocated array. I have a feeling its the way i passed in top in the enqueue function to the reheapUp. Any suggestions would be very helpful!
you seem to be quite confused about index and element. reheapUp(*heap, length);
*heap is equivalent to `head[0]', that is, the first element.
So you pass the first element and the size of the queue
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
void PQueue::reheapUp(int top, int bottom) //the names suggest that they both refer to index
{
int child = bottom; //child is clearly an index
int parent;
while(child > top) //so it should be comparing an index. However, ¿wouldn't top always be 0?
{
parent = (child - 1)/2;
if(heap[child] > heap[parent])
{
int newChild = parent; //capturing the index...
int newParent = child;
heap[child] = newChild; //and then overwriting the element ¿?
heap[parent] = newParent;
//¿want to swap? use std::swap()
}
child = parent;
}
}
yeah! After looking over my notes again i was confusing the element and index. Thank you for clarifying the array part! As well as explaining it. It was more helpful than my notes!