-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryHeap.java
More file actions
167 lines (149 loc) · 3.65 KB
/
BinaryHeap.java
File metadata and controls
167 lines (149 loc) · 3.65 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
package txp172630;
import java.util.Comparator;
import java.util.Scanner;
public class BinaryHeap<T extends Comparable<? super T>> {
T[] pq;
Comparator<T> comp;
int size;
int len;
// Constructor for building an empty priority queue using natural ordering
// of T
public BinaryHeap(T[] q) {
// Use a lambda expression to create comparator from compareTo
this(q, (T a, T b) -> a.compareTo(b));
}
// Constructor for building an empty priority queue with custom comparator
public BinaryHeap(T[] q, Comparator<T> c) {
size = -1;
len = q.length;
pq = q;
comp = c;
}
// Add element in priority queue (throws exception if queue is full)
public void add(T x) throws Exception {
if (isFull()) {
throw new Exception("Priority Queue is Full");
}
size++;
pq[size] = x;
percolateUp(size);
}
// Add element in priority queue (does not throw exception if queue is full,
// returns false)
public boolean offer(T x) throws Exception {
if (isFull()) {
return false;
}
add(x);
return true;
}
// Remove element in priority queue (throws exception if queue is empty)
public T remove() throws Exception {
if (isEmpty()) {
throw new Exception("Priority Queue is empty");
}
T value = pq[0];
pq[0] = pq[size];
size--;
percolateDown(0);
return value;
}
// Remove element in priority queue (does not throw exception if queue is
// empty, returns null)
public T poll() throws Exception {
if (isEmpty()) {
return null;
}
return remove();
}
public T peek() {
if (isEmpty()) {
return null;
}
return pq[0];
}
// pq[i] may violate heap order with parent
void percolateUp(int i) {
T curVal = pq[i];
while (i > 0 && comp.compare(curVal, pq[parent(i)]) < 0) {
move(i, pq[parent(i)]);
i = parent(i);
}
move(i, curVal);
}
// pq[i] may violate heap order with children
void percolateDown(int i) {
T curVal = pq[i];
int dominant = leftChild(i);
while (dominant <= size) {
if ((dominant + 1) <= size && comp.compare(pq[dominant], pq[dominant + 1]) > 0) {
dominant++;
}
if (comp.compare(curVal, pq[dominant]) < 0) {
break;
}
move(i, pq[dominant]);
i = dominant;
dominant = leftChild(i);
}
move(i, curVal);
}
boolean isEmpty() {
return size == -1;
}
boolean isFull() {
return size == len - 1;
}
// Assign x to pq[i]. Indexed heap will override this method
void move(int i, T x) {
pq[i] = x;
}
int parent(int i) {
return (i - 1) / 2;
}
int leftChild(int i) {
return 2 * i + 1;
}
public void printPriorityQueue() {
System.out.print((size + 1) + ": ");
for (int i = 0; i <= size; i++) {
System.out.print(pq[i] + " ");
}
System.out.println();
}
public static void main(String[] cd) throws Exception {
Integer[] pq = new Integer[5];
BinaryHeap<Integer> bh = new BinaryHeap<>(pq);// , (Integer a, Integer
// b) ->
// b.compareTo(a));
Scanner sc = new Scanner(System.in);
whileloop: while (sc.hasNext()) {
int insertVal;
switch (sc.nextInt()) {
case 1: // Add
insertVal = sc.nextInt();
bh.add(insertVal);
bh.printPriorityQueue();
break;
case 2: // Offer
insertVal = sc.nextInt();
bh.offer(insertVal);
bh.printPriorityQueue();
break;
case 3: // Remove
System.out.println(bh.remove());
bh.printPriorityQueue();
break;
case 4: // Poll
System.out.println(bh.poll());
bh.printPriorityQueue();
break;
case 5: // Peek
System.out.println(bh.peek());
break;
default:
break whileloop;
}
}
}
}