1
- // Starter code for LP5
2
-
3
- // Change to your netid
1
+ /**
2
+ * @author Axat Chaudhari (akc170000)
3
+ * @author Jaiminee Kataria (jxk172330)
4
+ * @author Param Parikh (psp170230)
5
+ * @author Tej Patel (txp172630)
6
+ */
4
7
package akc170000 ;
5
8
6
9
import java .util .Arrays ;
7
- import java .util .Comparator ;
8
10
import java .util .NoSuchElementException ;
9
- import java .util .PriorityQueue ;
10
11
11
12
public class BinaryHeap <T extends Comparable <? super T >> {
12
13
Comparable [] pq ;
@@ -19,21 +20,34 @@ public BinaryHeap(int maxCapacity) {
19
20
this .maxCapacity = maxCapacity ;
20
21
}
21
22
22
- // add method: resize pq if needed
23
+ /**
24
+ * add x to PriorityQueue
25
+ * @param x element to add
26
+ * @return true if queue reached max capacity else false
27
+ */
23
28
public boolean add (T x ) {
24
- if (size == maxCapacity ){
29
+ if (size () == maxCapacity ){
25
30
return false ;
26
31
}
27
32
pq [size ++] = x ;
28
33
percolateUp (size -1 );
29
34
return true ;
30
35
}
31
36
37
+ /**
38
+ * same as add(x)
39
+ * @param x
40
+ * @return true if reached to max capacity else false
41
+ */
32
42
public boolean offer (T x ) {
33
43
return add (x );
34
44
}
35
45
36
- // throw exception if pq is empty
46
+ /**
47
+ * extract min from queue
48
+ * @return min element from queue
49
+ * @throws NoSuchElementException when queue is empty
50
+ */
37
51
public T remove () throws NoSuchElementException {
38
52
T result = poll ();
39
53
if (result == null ) {
@@ -43,9 +57,12 @@ public T remove() throws NoSuchElementException {
43
57
}
44
58
}
45
59
46
- // return null if pq is empty
60
+ /**
61
+ * same as remove()
62
+ * @return null if queue is empty
63
+ */
47
64
public T poll () {
48
- if (size == 0 ) {
65
+ if (isEmpty () ) {
49
66
return null ;
50
67
}
51
68
T temp = (T ) pq [0 ];
@@ -54,19 +71,36 @@ public T poll() {
54
71
return temp ;
55
72
}
56
73
74
+ /**
75
+ * get min element
76
+ * @return element with max priority
77
+ */
57
78
public T min () {
58
79
return peek ();
59
80
}
60
81
61
- // return null if pq is empty
82
+ /**
83
+ * same as min()
84
+ * @return element with max priority
85
+ */
62
86
public T peek () {
63
- return size == 0 ? null : (T ) pq [0 ];
87
+ return isEmpty () ? null : (T ) pq [0 ];
64
88
}
65
89
90
+ /**
91
+ * parent of i
92
+ * @param i index
93
+ * @return (i-1)*2
94
+ */
66
95
int parent (int i ) {
67
96
return (i -1 )>>1 ;
68
97
}
69
98
99
+ /**
100
+ * left child of i
101
+ * @param i index
102
+ * @return i*2 + 1
103
+ */
70
104
int leftChild (int i ) {
71
105
return (i <<1 ) + 1 ;
72
106
}
@@ -77,8 +111,7 @@ void percolateUp(int index) {
77
111
78
112
// go up and bring down elements while they are greater than 'temp'
79
113
while (index > 0 && compare (temp , pq [parent (index )]) < 0 ){
80
- // pq[index] = pq[parent(index)]; // bringing down parent to one level below
81
- move (index , pq [parent (index )]);
114
+ move (index , pq [parent (index )]); // bringing down parent to one level below
82
115
index = parent (index );
83
116
}
84
117
move (index , temp ); // move saved element to empty position of heap
@@ -95,14 +128,18 @@ void percolateDown(int index) {
95
128
}
96
129
// if current element is less than child then we have found the place for current element
97
130
if (compare (temp , pq [c ]) <= 0 ) break ;
98
- // pq[index] = pq[c];
99
131
move (index , pq [c ]);
100
132
index = c ;
101
133
c = leftChild (index ); // update to next left child
102
134
}
103
135
move (index , temp ); // move current element to its place
104
136
}
105
137
138
+ /**
139
+ * move x to dest
140
+ * @param dest position to store x
141
+ * @param x element to store at position dest
142
+ */
106
143
void move (int dest , Comparable x ) {
107
144
pq [dest ] = x ;
108
145
}
@@ -118,19 +155,32 @@ void buildHeap() {
118
155
}
119
156
}
120
157
158
+ /**
159
+ * is queue empty or not
160
+ * @return true if empty otherwise false
161
+ */
121
162
public boolean isEmpty () {
122
163
return size () == 0 ;
123
164
}
124
165
166
+ /**
167
+ * size of queue
168
+ * @return #elements in queue
169
+ */
125
170
public int size () {
126
171
return size ;
127
172
}
128
173
129
- // Resize array to double the current size
174
+ /**
175
+ * resize q to double the size
176
+ */
130
177
void resize () {
131
178
pq = Arrays .copyOf (pq , pq .length <<1 );
132
179
}
133
180
181
+ /**
182
+ * Interface for IndexedPriorityQueue
183
+ */
134
184
public interface Index {
135
185
public void putIndex (int index );
136
186
public int getIndex ();
0 commit comments