-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriorityQueue2.java
More file actions
95 lines (83 loc) · 3.04 KB
/
PriorityQueue2.java
File metadata and controls
95 lines (83 loc) · 3.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
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
/**
* @author Xuan Duong
* This Queue class holds priority customer objects in line
*/
public class PriorityQueue2
{
private PriorityCustomer[] queue;
private int size;
// constructor creste a queue of 60 customers, stored in an array of 60 elements
public PriorityQueue2(){
queue = new PriorityCustomer[60];
size = 0;
}
public void addCustomer(PriorityCustomer c){
int index = size + 1;
queue[index] = c;
while(index > 1){
int parent = index / 2;
PriorityCustomer rootValue = queue[1];
if(queue[parent].getPriority() < c.getPriority()){
queue[index] = queue[parent];
queue[parent] = c;
index = parent;
}
else if(rootValue.getPriority() < c.getPriority()){
PriorityCustomer temp = queue[index];
for(int i = (index - 1); i >= 2; i--){
queue[i + 1] = queue[i];
}
queue[2] = temp;
}
else{
break;
}
}
size++;
}
public PriorityCustomer removeCustomer(){
PriorityCustomer rootValue = queue[1]; //store root value to return at the end
PriorityCustomer x = queue[size]; //store last value in heap in x
queue[1] = x; //take v and move to root
queue[size] = null; //delete node at last position (b/c we moved it to the root)
int index = 1;
while(index * 2 < size){ //is there at least one child at index
int leftIndex = (index * 2) - 1;
int rightIndex = leftIndex + 1;
PriorityCustomer leftValue = queue[leftIndex];
PriorityCustomer rightValue = null;
if(rightIndex < size){ //there is a right child
rightValue = queue[rightIndex];
}
PriorityCustomer maxChild;
int maxIndex; //find index of and value of larger child
if(leftValue.getPriority() >= rightValue.getPriority()){ //swap with left child if values are the same
maxChild = leftValue;
maxIndex = leftIndex;
}
else{
maxChild = rightValue;
maxIndex = rightIndex;
}
if(x.getPriority() < maxChild.getPriority()){ //swap if value is less than the larger child
queue[index] = maxChild; //perform swap with larger of two children
queue[maxIndex] = x;
index = maxIndex;
}
else{
break; //stop if value is in a valid position
}
}
size --; //update size
//return old root
return rootValue;
}
//return size of queue
public int getSize(){
return size;
}
// return first customer in line
public PriorityCustomer getFirst(){
return queue[1];
}
}