-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinPriorityQueue.java
More file actions
176 lines (159 loc) · 5.19 KB
/
MinPriorityQueue.java
File metadata and controls
176 lines (159 loc) · 5.19 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
import java.util.ArrayList;
class MinPriorityQueue
{
// Keep the nodes in an ArrayList. Two advantages
// (1) We can store objects that store both value and priority
// (2) We don't have to worry about expanding
// PriQueueNode is defined at the bottom of this file.
ArrayList<PriQueueNode> items;
/**
* Creates an empty priority queue
*/
public MinPriorityQueue()
{
items = new ArrayList<PriQueueNode>();
}
/**
* @return the item with the minimum priority
*/
public Object peek()
{
return items.get(0).data;
}
/**
* Removes and returns the item with the minimum priority
* @return the item associated with the minimum priority
*/
public Object dequeue()
{
if (items.size() > 1){
Object removedval = items.get(0);
items.set(0, items.remove(items.size()-1));
bubbleDown(0);
return removedval;
} else {
return items.remove(0);
}
}
/**
* Moves an item at the given index down the tree
* As long as at least one of it's children is smaller
* or we reach a leaf position.
* @param idx the index of the item to move
*/
protected void bubbleDown(int idx)
{
if (getMinChild(idx) > -1){
if (items.get(idx).priority > items.get(getMinChild(idx)).priority){
PriQueueNode temp = items.get(idx);
items.set(idx, items.get(getMinChild(idx)));
items.set(getMinChild(idx), temp);
bubbleDown(getMinChild(idx));
}
}
}
/**
* Returns index position of the child node
* whose priority is smallest.
* A given index may only have 1 or no children.
*/
protected Integer getMinChild(int idx)
{
int leftChildIdx = (idx*2)+1;
int rightChildIdx = (idx*2)+2;
if (leftChildIdx >= items.size()){
return -1;
}
if (rightChildIdx >= items.size()){
return leftChildIdx;
}
if (items.get(leftChildIdx).priority < items.get(rightChildIdx).priority){
return leftChildIdx;
}
return rightChildIdx;
}
/**
* Inserts an item into the queue with the given priority
* @param item the item to insert
* @param priority the item's priority value
*/
public void enqueue(double priority, Object val)
{
//The last thing in the list, is the last thing in the bottom layer
PriQueueNode newItem = new PriQueueNode(priority, val);
items.add(newItem);
bubbleUp(items.size() - 1);
}
/**
* Moves the item at the given index "up" the "tree" until its parent is smaller
* @param idx the index of the item to move
*/
protected void bubbleUp(int idx)
{
int parentIDX = (idx-1)/2; //if idx == 0, parent == 0, so priorities are the same!
double parentPriority = items.get(parentIDX).priority;
double currentPriority = items.get(idx).priority;
if(currentPriority < parentPriority) {
//Swap items
PriQueueNode parent = items.get(parentIDX);
PriQueueNode current = items.get(idx);
items.set(parentIDX, current);
items.set(idx, parent);
bubbleUp(parentIDX); // Recursive implementation
}
}
/**
* @return a string representation of the heap
*/
public String toString()
{
return items.toString();
}
public static void main(String [] args)
{
// Write tests here based on question two from previous in the lab
// You can use arbitrary data (or perhaps null) for the "value" of each node
// Make sure you use the same priorities, in the same order as in question two
MinPriorityQueue queue = new MinPriorityQueue();
queue.enqueue(12.2, 1);
queue.enqueue(23.0, 2);
queue.enqueue(9.0, 3);
queue.enqueue(8.0, 4);
queue.enqueue(12.0, 5);
queue.enqueue(15.5, 6);
for (int i = 0; i < queue.items.size(); i++){
System.out.println(queue.dequeue());
}
System.out.println(queue.peek());
System.out.println(queue.dequeue());
System.out.println(queue.peek());
System.out.println(queue.dequeue());
System.out.println(queue.peek());
System.out.println(queue.dequeue());
System.out.println(queue.peek());
System.out.println(queue.dequeue());
System.out.println(queue.peek());
System.out.println(queue.dequeue());
System.out.println(queue.peek());
System.out.println(queue.dequeue());
}
}
class PriQueueNode{
// This object allows us to store the
// data and the priority for this data
// together in "package" together
//
// +-PriQueueNode----+
// | data: "hello!" |
// | pri: 2.4 |
// +-----------------+
public Object data;
public double priority;
public PriQueueNode(double pri, Object newVal){
priority = pri;
data = newVal;
}
public String toString(){
return "(" + data + " {pri: " + priority + "})";
}
}