-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriorityQ.java
More file actions
192 lines (161 loc) · 4.44 KB
/
PriorityQ.java
File metadata and controls
192 lines (161 loc) · 4.44 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
* A priority queue implementation using max heap
* @author Saveri Pal
*
*/
public class PriorityQ {
/**
* Array of type Task that stores (jobs,priority)
*/
public Task[] maxHeap;
/**
* size of heap
*/
private int heapSize;
/**
* Constructs a PriorityQ
*/
public PriorityQ()
{
maxHeap = new Task[100];
for(int i = 0; i < maxHeap.length; i++)
{
maxHeap[i] = new Task(" ", 0);
}
heapSize = 0;
}
/**
* Adds string s with priority p to the queue
* @param s a job
* @param p priority of the job
*/
public void add(String s, int p)
{
heapSize++;
maxHeap[heapSize-1]= new Task(s,p);
sort();
}
/**
* Sorts a heap containg jobs with priorities
*/
public void sort()
{
int n = heapSize;
for (int i = n / 2 - 1; i >= 0; i--)
heapify(n, i);
}
/**
* Max heapifies
* @param n size of heap
* @param i largest priority
*/
public void heapify(int n, int i)
{
int largest = i; // parent
int left = 2*i + 1; // left child
int right = 2*i + 2; // right child
// If left child is larger than largest so far
if (left < n && maxHeap[left].p > maxHeap[largest].p)
largest = left;
// If right child is larger than largest so far
if (right < n && maxHeap[right].p > maxHeap[largest].p)
largest = right;
// If largest is not parent
if (largest != i)
{
Task swap = new Task(" ",0);
swap.p = maxHeap[i].p;
swap.s = maxHeap[i].s;
maxHeap[i].p = maxHeap[largest].p;
maxHeap[i].s = maxHeap[largest].s;
maxHeap[largest].p = swap.p;
maxHeap[largest].s = swap.s;
// Recursively heapify the affected sub-tree
heapify(n, largest);
}
}
/**
* Returns a string whose priority is maximum
* @return a string/job with max priority
*/
public String returnMax()
{
return maxHeap[0].s;
}
/**
* Extracts the highest priority string from the priority queue
* And returns it
* And removes it from the priority queue
* @return highest priority string/job from queue
*/
public String extractMax()
{
String highestPriorityString = maxHeap[0].s;
remove(0);
return highestPriorityString;
}
/**
* Removes the i-th Array index element from the priority queue
* @param i index of element to be removed
*/
public void remove(int i)
{
maxHeap[i].p = maxHeap[heapSize-1].p;
maxHeap[i].s = maxHeap[heapSize-1].s;
heapSize = heapSize-1;
sort();
}
/**
* Decrements the priority of i-th element by k
* @param i element whose priority needs to be reduced
* @param k amount by which the priority needs to be removed
*/
public void decrementPriority(int i, int k)
{
maxHeap[i].p -= k;
sort();
}
/**
* Returns a priority array, B containing the priorities of all elements in A
* B[i] = Key(A[i])
* @return
*/
public int[] priorityArray()
{
int B[]= new int[heapSize];
for(int i = 0;i<heapSize;i++)
{
B[i] = maxHeap[i].p;
//System.out.println(maxHeap[i].p);
}
return B;
}
/**
* Returns key(A[i]), where A is the priority queue
* @param i element
* @return priority of the element
*/
public int getKey(int i)
{
return maxHeap[i].p;
}
/**
* Returns value(A[i]), where A is the priority queue
* @param i
* @return string s
*/
public String getValue(int i)
{
return maxHeap[i].s;
}
/**
* Check if heap is empty of not
* @return true or false depending on empty or not
*/
public boolean isEmpty()
{
boolean temp;
temp = (heapSize==0)?true:false;
return temp;
}
}