-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxPriorityQueue.cpp
More file actions
46 lines (33 loc) · 1.04 KB
/
MaxPriorityQueue.cpp
File metadata and controls
46 lines (33 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include "MaxHeap.h"
#include "MaxPriorityQueue.h"
using namespace std;
MaxPriorityQueue * initMaxPQ (int A[], int numElements) {
MaxPriorityQueue *q=new MaxPriorityQueue;
q->heap=initMaxHeapFromArray(A,numElements);
buildMaxHeap(q->heap);
return q;
}
int maximumPQ (MaxPriorityQueue * maxPQ) {
if(isEmptyMaxHeap(maxPQ->heap)) return -1;
return maxPQ->heap->A[1];
}
int extractMaximumPQ (MaxPriorityQueue * maxPQ) {
if(isEmptyMaxHeap(maxPQ->heap)) return -1;
return deleteMaxHeap(maxPQ->heap,1);
}
void insertMaxPQ (MaxPriorityQueue * maxPQ, int priority) {
insertMaxHeap(maxPQ->heap,priority);
}
void increasePriority (MaxPriorityQueue * maxPQ, int i, int newPriority) {
if(i>maxPQ->heap->size || maxPQ->heap->A[i]>=newPriority) return;
maxPQ->heap->A[i]=newPriority;
while(i>1&&maxPQ->heap->A[i/2]<maxPQ->heap->A[i]){
int temp=maxPQ->heap->A[i/2];
maxPQ->heap->A[i]=temp;
i/=2;
}
}
void displayMaxPQ (MaxPriorityQueue * maxPQ) {
displayMaxHeap (maxPQ->heap);
}