Skip to content

Commit 058d5c2

Browse files
committed
done commenting
1 parent fb6512e commit 058d5c2

File tree

1 file changed

+66
-16
lines changed

1 file changed

+66
-16
lines changed

src/akc170000/BinaryHeap.java

Lines changed: 66 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
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+
*/
47
package akc170000;
58

69
import java.util.Arrays;
7-
import java.util.Comparator;
810
import java.util.NoSuchElementException;
9-
import java.util.PriorityQueue;
1011

1112
public class BinaryHeap<T extends Comparable<? super T>> {
1213
Comparable[] pq;
@@ -19,21 +20,34 @@ public BinaryHeap(int maxCapacity) {
1920
this.maxCapacity = maxCapacity;
2021
}
2122

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+
*/
2328
public boolean add(T x) {
24-
if (size == maxCapacity){
29+
if (size() == maxCapacity){
2530
return false;
2631
}
2732
pq[size++] = x;
2833
percolateUp(size-1);
2934
return true;
3035
}
3136

37+
/**
38+
* same as add(x)
39+
* @param x
40+
* @return true if reached to max capacity else false
41+
*/
3242
public boolean offer(T x) {
3343
return add(x);
3444
}
3545

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+
*/
3751
public T remove() throws NoSuchElementException {
3852
T result = poll();
3953
if(result == null) {
@@ -43,9 +57,12 @@ public T remove() throws NoSuchElementException {
4357
}
4458
}
4559

46-
// return null if pq is empty
60+
/**
61+
* same as remove()
62+
* @return null if queue is empty
63+
*/
4764
public T poll() {
48-
if (size == 0) {
65+
if (isEmpty()) {
4966
return null;
5067
}
5168
T temp = (T) pq[0];
@@ -54,19 +71,36 @@ public T poll() {
5471
return temp;
5572
}
5673

74+
/**
75+
* get min element
76+
* @return element with max priority
77+
*/
5778
public T min() {
5879
return peek();
5980
}
6081

61-
// return null if pq is empty
82+
/**
83+
* same as min()
84+
* @return element with max priority
85+
*/
6286
public T peek() {
63-
return size == 0 ? null : (T) pq[0];
87+
return isEmpty() ? null : (T) pq[0];
6488
}
6589

90+
/**
91+
* parent of i
92+
* @param i index
93+
* @return (i-1)*2
94+
*/
6695
int parent(int i) {
6796
return (i-1)>>1;
6897
}
6998

99+
/**
100+
* left child of i
101+
* @param i index
102+
* @return i*2 + 1
103+
*/
70104
int leftChild(int i) {
71105
return (i<<1) + 1;
72106
}
@@ -77,8 +111,7 @@ void percolateUp(int index) {
77111

78112
// go up and bring down elements while they are greater than 'temp'
79113
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
82115
index = parent(index);
83116
}
84117
move(index, temp); // move saved element to empty position of heap
@@ -95,14 +128,18 @@ void percolateDown(int index) {
95128
}
96129
// if current element is less than child then we have found the place for current element
97130
if (compare(temp, pq[c]) <= 0) break;
98-
// pq[index] = pq[c];
99131
move(index, pq[c]);
100132
index = c;
101133
c = leftChild(index); // update to next left child
102134
}
103135
move(index, temp); // move current element to its place
104136
}
105137

138+
/**
139+
* move x to dest
140+
* @param dest position to store x
141+
* @param x element to store at position dest
142+
*/
106143
void move(int dest, Comparable x) {
107144
pq[dest] = x;
108145
}
@@ -118,19 +155,32 @@ void buildHeap() {
118155
}
119156
}
120157

158+
/**
159+
* is queue empty or not
160+
* @return true if empty otherwise false
161+
*/
121162
public boolean isEmpty() {
122163
return size() == 0;
123164
}
124165

166+
/**
167+
* size of queue
168+
* @return #elements in queue
169+
*/
125170
public int size() {
126171
return size;
127172
}
128173

129-
// Resize array to double the current size
174+
/**
175+
* resize q to double the size
176+
*/
130177
void resize() {
131178
pq = Arrays.copyOf(pq, pq.length<<1);
132179
}
133180

181+
/**
182+
* Interface for IndexedPriorityQueue
183+
*/
134184
public interface Index {
135185
public void putIndex(int index);
136186
public int getIndex();

0 commit comments

Comments
 (0)