Skip to content

Commit 23b16c6

Browse files
committed
style: make heap code less verbose
1 parent 514e2a7 commit 23b16c6

File tree

1 file changed

+37
-29
lines changed

1 file changed

+37
-29
lines changed

src/main/java/dataStructures/heap/MaxHeap.java

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public MaxHeap() {
3535
}
3636

3737
public int size() {
38-
return this.heap.size();
38+
return heap.size();
3939
}
4040

4141
/**
@@ -70,7 +70,10 @@ public T poll() {
7070
* @param item item to be inserted
7171
*/
7272
public void offer(T item) {
73-
assert !indexOf.containsKey(item) : "Duplicate objects found!";
73+
// shouldn't happen as mentioned in README; do nothing, though should customize behaviour in practice
74+
if (indexOf.containsKey(item)) {
75+
76+
}
7477

7578
heap.add(item); // add to the end of the arraylist
7679
indexOf.put(item, size() - 1); // add item into index map; here becomes problematic if there are duplicates
@@ -83,11 +86,10 @@ public void offer(T item) {
8386
* @param obj object to be removed
8487
*/
8588
public void remove(T obj) {
86-
if (!indexOf.containsKey(obj)) {
87-
System.out.printf("%s does not exist!%n", obj);
88-
return;
89+
if (!indexOf.containsKey(obj)) { // do nothing
90+
8991
}
90-
this.remove(indexOf.get(obj));
92+
remove(indexOf.get(obj));
9193
}
9294

9395
/**
@@ -97,9 +99,9 @@ public void remove(T obj) {
9799
* @return deleted element
98100
*/
99101
private T remove(int i) {
100-
T item = this.get(i); // remember element to be removed
101-
swap(i, this.size() - 1); // O(1) swap with last element in the heap
102-
heap.remove(this.size() - 1); // O(1)
102+
T item = get(i); // remember element to be removed
103+
swap(i, size() - 1); // O(1) swap with last element in the heap
104+
heap.remove(size() - 1); // O(1)
103105
indexOf.remove(item); // remove from index map
104106
bubbleDown(i); // O(log n)
105107
return item;
@@ -112,7 +114,10 @@ private T remove(int i) {
112114
* @param updatedObj updated object
113115
*/
114116
public void decreaseKey(T obj, T updatedObj) {
115-
assert updatedObj.compareTo(obj) <= 0 : "Value should reduce or remain the same";
117+
// shouldn't happen; do nothing, though should customize behaviour in practice
118+
if (updatedObj.compareTo(obj) > 0) {
119+
120+
}
116121

117122
int idx = indexOf.get(obj); // get the index of the object in the array implementation
118123
heap.set(idx, updatedObj); // simply replace
@@ -128,7 +133,10 @@ public void decreaseKey(T obj, T updatedObj) {
128133
* @param updatedObj updated object
129134
*/
130135
public void increaseKey(T obj, T updatedObj) {
131-
assert updatedObj.compareTo(obj) >= 0 : "Value should reduce or remain the same";
136+
// shouldn't happen; do nothing, though should customize behaviour in practice
137+
if (updatedObj.compareTo(obj) < 0) {
138+
return;
139+
}
132140

133141
int idx = indexOf.get(obj); // get the index of the object in the array implementation
134142
heap.set(idx, updatedObj); // simply replace
@@ -144,10 +152,10 @@ public void increaseKey(T obj, T updatedObj) {
144152
*/
145153
public void heapify(List<T> lst) {
146154
heap = new ArrayList<>(lst);
147-
for (int i = 0; i < this.size(); i++) {
148-
indexOf.put(this.get(i), i);
155+
for (int i = 0; i < size(); i++) {
156+
indexOf.put(get(i), i);
149157
}
150-
for (int i = this.size() - 1; i >= 0; i--) {
158+
for (int i = size() - 1; i >= 0; i--) {
151159
bubbleDown(i);
152160
}
153161
}
@@ -165,7 +173,7 @@ public void heapify(T... seq) {
165173
indexOf.put(obj, j);
166174
j++;
167175
}
168-
for (int i = this.size() - 1; i >= 0; i--) {
176+
for (int i = size() - 1; i >= 0; i--) {
169177
bubbleDown(i);
170178
}
171179
}
@@ -176,7 +184,7 @@ public void heapify(T... seq) {
176184
@Override
177185
public String toString() {
178186
StringBuilder ret = new StringBuilder("[");
179-
for (int i = 0; i < this.size(); i++) {
187+
for (int i = 0; i < size(); i++) {
180188
ret.append(heap.get(i));
181189
ret.append(", ");
182190
}
@@ -199,11 +207,11 @@ private T get(int i) {
199207
*/
200208
private void swap(int idx1, int idx2) {
201209
// update the index of each value in the map
202-
indexOf.put(this.get(idx1), idx2);
203-
indexOf.put(this.get(idx2), idx1);
210+
indexOf.put(get(idx1), idx2);
211+
indexOf.put(get(idx2), idx1);
204212

205213
T tmp = get(idx1); // Recall internally implemented with an ArrayList
206-
heap.set(idx1, this.get(idx2));
214+
heap.set(idx1, get(idx2));
207215
heap.set(idx2, tmp);
208216
}
209217

@@ -220,7 +228,7 @@ private int getParentIndex(int i) {
220228
* @return parent of element at index i
221229
*/
222230
private T getParent(int i) {
223-
return this.get(getParentIndex(i));
231+
return get(getParentIndex(i));
224232
}
225233

226234
/**
@@ -236,7 +244,7 @@ private int getLeftIndex(int i) {
236244
* @return left child of element at index i
237245
*/
238246
private T getLeft(int i) {
239-
return this.get(getLeftIndex(i));
247+
return get(getLeftIndex(i));
240248
}
241249

242250
/**
@@ -252,7 +260,7 @@ private int getRightIndex(int i) {
252260
* @return right child of element at index i
253261
*/
254262
private T getRight(int i) {
255-
return this.get(getRightIndex(i));
263+
return get(getRightIndex(i));
256264
}
257265

258266
/**
@@ -262,9 +270,9 @@ private T getRight(int i) {
262270
* @param i given index
263271
*/
264272
private void bubbleUp(int i) {
265-
while (i > 0 && this.get(i).compareTo(getParent(i)) > 0) { // the furthest up you can go is the root
273+
while (i > 0 && get(i).compareTo(getParent(i)) > 0) { // the furthest up you can go is the root
266274
int parentIdx = getParentIndex(i);
267-
this.swap(i, parentIdx);
275+
swap(i, parentIdx);
268276
i = parentIdx;
269277
}
270278
}
@@ -277,7 +285,7 @@ private void bubbleUp(int i) {
277285
*/
278286
private boolean isLeaf(int i) {
279287
// actually, suffice to compare index of left child of a node and size of heap
280-
return this.getRightIndex(i) >= this.size() && this.getLeftIndex(i) >= this.size();
288+
return getRightIndex(i) >= size() && getLeftIndex(i) >= size();
281289
}
282290

283291
/**
@@ -287,17 +295,17 @@ private boolean isLeaf(int i) {
287295
* @param i given index
288296
*/
289297
private void bubbleDown(int i) {
290-
while (!this.isLeaf(i)) {
291-
T maxItem = this.get(i);
298+
while (!isLeaf(i)) {
299+
T maxItem = get(i);
292300
int maxIndex = i; // index of max item
293301

294302
// check if left child is greater in priority, if left exists
295-
if (getLeftIndex(i) < this.size() && maxItem.compareTo(getLeft(i)) < 0) {
303+
if (getLeftIndex(i) < size() && maxItem.compareTo(getLeft(i)) < 0) {
296304
maxItem = getLeft(i);
297305
maxIndex = getLeftIndex(i);
298306
}
299307
// check if right child is greater in priority, if right exists
300-
if (getRightIndex(i) < this.size() && maxItem.compareTo(getRight(i)) < 0) {
308+
if (getRightIndex(i) < size() && maxItem.compareTo(getRight(i)) < 0) {
301309
maxIndex = getRightIndex(i);
302310
}
303311

0 commit comments

Comments
 (0)