I know queue but with priority queue i am really amateur.
can you help me implement some functions in priority queue. thanks in advance!
1 2 3 4 5 6 7 8 9 10 11 12 13 14
template <class T> class PRIORITY_QUEUE {
private:
T *items; // array of queue items
int rear;
int maxSize; // maximum size of queue
void heapify(int i); // adjust heap at position “i”
public:
PRIORITY_QUEUE(int size); // create queue with ‘size’ items
PRIORITY_QUEUE(const PRIORITY_QUEUE &aQueue);
~PRIORITY_QUEUE(); // destructor
bool isEmpty();
void insert(T newItem);
T deleteMin();
};
Try googling "heapify". That will give you a number of pages that help explain it. Essentially, a heap is a binary tree in which all child nodes of are less than the parent node, and all nodes in the tree. Every sub-tree is a heap. The wikipedia page (http://en.wikipedia.org/wiki/Heapsort) shows to implement this as an array.
The pseudo-code on the wikipedia page is actually pretty clear. It is more complete than something I would write up. Read the Overview section (to understand how the tree is represented in the array) and the beginning of the Pseudocode section (for a description of heapsort(), heapify() and siftDown() ). At the end of the Pseudocode section is an alternative method of heapify() using siftUp(). You can use whichever one you want to use. The variable 'count' is the size of the array being heapified. Your PRIORITY_QUEUE class should have that variable available somehow (maybe that's what maxSize is supposed to be?).
BTW, Sorry about the bad link in the previous post. The closing ')' got mixed into the link somehow.