Skip to content

Commit 8b92c3f

Browse files
committed
fix: remove unnecesary throw to fix TheAlgorithms#704
- Fix TheAlgorithms#704 - Thanks @lprone
1 parent bb670a2 commit 8b92c3f

File tree

9 files changed

+369
-364
lines changed

9 files changed

+369
-364
lines changed

DataStructures/Heaps/EmptyHeapException.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
/**
2-
*
2+
*
33
*/
4-
package heaps;
4+
package Heaps;
55

66
/**
77
* @author Nicolas Renard
88
* Exception to be thrown if the getElement method is used on an empty heap.
9-
*
109
*/
1110
@SuppressWarnings("serial")
1211
public class EmptyHeapException extends Exception {
13-
12+
1413
public EmptyHeapException(String message) {
1514
super(message);
1615
}

DataStructures/Heaps/Heap.java

+12-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package heaps;
1+
package Heaps;
22

33
/**
44
* Interface common to heap data structures.<br>
@@ -10,32 +10,31 @@
1010
* max-heap).</p>
1111
* <p>All heap-related operations (inserting or deleting an element, extracting the min or max) are performed in
1212
* O(log n) time.</p>
13+
*
1314
* @author Nicolas Renard
14-
*
15-
*
1615
*/
1716
public interface Heap {
18-
17+
1918
/**
20-
*
2119
* @return the top element in the heap, the one with lowest key for min-heap or with
2220
* the highest key for max-heap
23-
* @throws Exception if heap is empty
21+
* @throws EmptyHeapException if heap is empty
2422
*/
25-
public abstract HeapElement getElement() throws EmptyHeapException;
23+
HeapElement getElement() throws EmptyHeapException;
24+
2625
/**
2726
* Inserts an element in the heap. Adds it to then end and toggle it until it finds its
2827
* right position.
29-
*
28+
*
3029
* @param element an instance of the HeapElement class.
3130
*/
32-
public abstract void insertElement(HeapElement element);
33-
31+
void insertElement(HeapElement element);
32+
3433
/**
3534
* Delete an element in the heap.
36-
*
35+
*
3736
* @param elementIndex int containing the position in the heap of the element to be deleted.
3837
*/
39-
public abstract void deleteElement(int elementIndex);
38+
void deleteElement(int elementIndex);
4039

41-
}
40+
}

DataStructures/Heaps/HeapElement.java

+26-32
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
2-
*
2+
*
33
*/
4-
package heaps;
4+
package Heaps;
55

66
import java.lang.Double;
77
import java.lang.Object;
@@ -12,116 +12,110 @@
1212
* or double, either primitive type or object) and any kind of IMMUTABLE object the user sees fit
1313
* to carry any information he/she likes. Be aware that the use of a mutable object might
1414
* jeopardize the integrity of this information. </p>
15-
* @author Nicolas Renard
1615
*
16+
* @author Nicolas Renard
1717
*/
1818
public class HeapElement {
1919
private final double key;
2020
private final Object additionalInfo;
21-
21+
2222
// Constructors
2323

2424
/**
25-
*
26-
* @param key : a number of primitive type 'double'
25+
* @param key : a number of primitive type 'double'
2726
* @param info : any kind of IMMUTABLE object. May be null, since the purpose is only to carry
28-
* additional information of use for the user
27+
* additional information of use for the user
2928
*/
3029
public HeapElement(double key, Object info) {
3130
this.key = key;
3231
this.additionalInfo = info;
3332
}
34-
33+
3534
/**
36-
*
37-
* @param key : a number of primitive type 'int'
35+
* @param key : a number of primitive type 'int'
3836
* @param info : any kind of IMMUTABLE object. May be null, since the purpose is only to carry
39-
* additional information of use for the user
37+
* additional information of use for the user
4038
*/
4139
public HeapElement(int key, Object info) {
4240
this.key = key;
4341
this.additionalInfo = info;
4442
}
45-
43+
4644
/**
47-
*
48-
* @param key : a number of object type 'Integer'
45+
* @param key : a number of object type 'Integer'
4946
* @param info : any kind of IMMUTABLE object. May be null, since the purpose is only to carry
50-
* additional information of use for the user
47+
* additional information of use for the user
5148
*/
5249
public HeapElement(Integer key, Object info) {
5350
this.key = key;
5451
this.additionalInfo = info;
5552
}
56-
53+
5754
/**
58-
*
59-
* @param key : a number of object type 'Double'
55+
* @param key : a number of object type 'Double'
6056
* @param info : any kind of IMMUTABLE object. May be null, since the purpose is only to carry
61-
* additional information of use for the user
57+
* additional information of use for the user
6258
*/
6359
public HeapElement(Double key, Object info) {
6460
this.key = key;
6561
this.additionalInfo = info;
6662
}
67-
63+
6864
/**
69-
*
7065
* @param key : a number of primitive type 'double'
7166
*/
7267
public HeapElement(double key) {
7368
this.key = key;
7469
this.additionalInfo = null;
7570
}
76-
71+
7772
/**
78-
*
7973
* @param key : a number of primitive type 'int'
8074
*/
8175
public HeapElement(int key) {
8276
this.key = key;
8377
this.additionalInfo = null;
8478
}
85-
79+
8680
/**
87-
*
8881
* @param key : a number of object type 'Integer'
8982
*/
9083
public HeapElement(Integer key) {
9184
this.key = key;
9285
this.additionalInfo = null;
9386
}
94-
87+
9588
/**
96-
*
9789
* @param key : a number of object type 'Double'
9890
*/
9991
public HeapElement(Double key) {
10092
this.key = key;
10193
this.additionalInfo = null;
10294
}
103-
95+
10496
// Getters
97+
10598
/**
10699
* @return the object containing the additional info provided by the user.
107100
*/
108101
public Object getInfo() {
109102
return additionalInfo;
110103
}
104+
111105
/**
112106
* @return the key value of the element
113107
*/
114108
public double getKey() {
115109
return key;
116110
}
117-
111+
118112
// Overridden object methods
119-
113+
120114
public String toString() {
121-
return "Key: " + key + " - " +additionalInfo.toString();
115+
return "Key: " + key + " - " + additionalInfo.toString();
122116
}
117+
123118
/**
124-
*
125119
* @param otherHeapElement
126120
* @return true if the keys on both elements are identical and the additional info objects
127121
* are identical.

DataStructures/Heaps/MaxHeap.java

+49-45
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,76 @@
1-
package heaps;
1+
package Heaps;
22

33
import java.util.ArrayList;
44
import java.util.List;
55

66
/**
77
* Heap tree where a node's key is higher than or equal to its parent's and lower than or equal
88
* to its children's.
9-
* @author Nicolas Renard
109
*
10+
* @author Nicolas Renard
1111
*/
1212
public class MaxHeap implements Heap {
13-
13+
1414
private final List<HeapElement> maxHeap;
15-
16-
public MaxHeap(List<HeapElement> listElements) throws Exception {
17-
maxHeap = new ArrayList<HeapElement>();
15+
16+
public MaxHeap(List<HeapElement> listElements) {
17+
maxHeap = new ArrayList<>();
1818
for (HeapElement heapElement : listElements) {
1919
if (heapElement != null) insertElement(heapElement);
2020
else System.out.println("Null element. Not added to heap");
2121
}
2222
if (maxHeap.size() == 0) System.out.println("No element has been added, empty heap.");
23-
}
24-
25-
// Get the element at a given index. The key for the list is equal to index value - 1
23+
}
24+
25+
/**
26+
* Get the element at a given index. The key for the list is equal to index value - 1
27+
*
28+
* @param elementIndex index
29+
* @return heapElement
30+
*/
2631
public HeapElement getElement(int elementIndex) {
27-
if ((elementIndex <= 0) && (elementIndex > maxHeap.size())) throw new IndexOutOfBoundsException("Index out of heap range");
32+
if ((elementIndex <= 0) || (elementIndex > maxHeap.size()))
33+
throw new IndexOutOfBoundsException("Index out of heap range");
2834
return maxHeap.get(elementIndex - 1);
2935
}
30-
36+
3137
// Get the key of the element at a given index
3238
private double getElementKey(int elementIndex) {
3339
return maxHeap.get(elementIndex - 1).getKey();
3440
}
35-
41+
3642
// Swaps two elements in the heap
3743
private void swap(int index1, int index2) {
3844
HeapElement temporaryElement = maxHeap.get(index1 - 1);
3945
maxHeap.set(index1 - 1, maxHeap.get(index2 - 1));
4046
maxHeap.set(index2 - 1, temporaryElement);
4147
}
42-
43-
// Toggle an element up to its right place as long as its key is lower than its parent's
48+
49+
// Toggle an element up to its right place as long as its key is lower than its parent's
4450
private void toggleUp(int elementIndex) {
4551
double key = maxHeap.get(elementIndex - 1).getKey();
46-
while (getElementKey((int) Math.floor(elementIndex/2)) < key) {
47-
swap(elementIndex, (int) Math.floor(elementIndex/2));
48-
elementIndex = (int) Math.floor(elementIndex/2);
52+
while (getElementKey((int) Math.floor(elementIndex / 2)) < key) {
53+
swap(elementIndex, (int) Math.floor(elementIndex / 2));
54+
elementIndex = (int) Math.floor(elementIndex / 2);
4955
}
5056
}
51-
57+
5258
// Toggle an element down to its right place as long as its key is higher
53-
// than any of its children's
59+
// than any of its children's
5460
private void toggleDown(int elementIndex) {
5561
double key = maxHeap.get(elementIndex - 1).getKey();
56-
boolean wrongOrder = (key < getElementKey(elementIndex*2)) || (key < getElementKey(Math.min(elementIndex*2, maxHeap.size())));
57-
while ((2*elementIndex <= maxHeap.size()) && wrongOrder) {
62+
boolean wrongOrder = (key < getElementKey(elementIndex * 2)) || (key < getElementKey(Math.min(elementIndex * 2, maxHeap.size())));
63+
while ((2 * elementIndex <= maxHeap.size()) && wrongOrder) {
5864
// Check whether it shall swap the element with its left child or its right one if any.
59-
if ((2*elementIndex < maxHeap.size()) && (getElementKey(elementIndex*2 + 1) > getElementKey(elementIndex*2))) {
60-
swap(elementIndex, 2*elementIndex + 1);
61-
elementIndex = 2*elementIndex + 1;
65+
if ((2 * elementIndex < maxHeap.size()) && (getElementKey(elementIndex * 2 + 1) > getElementKey(elementIndex * 2))) {
66+
swap(elementIndex, 2 * elementIndex + 1);
67+
elementIndex = 2 * elementIndex + 1;
68+
} else {
69+
swap(elementIndex, 2 * elementIndex);
70+
elementIndex = 2 * elementIndex;
6271
}
63-
else {
64-
swap(elementIndex, 2*elementIndex);
65-
elementIndex = 2*elementIndex;
66-
}
67-
wrongOrder = (key < getElementKey(elementIndex*2)) || (key < getElementKey(Math.min(elementIndex*2, maxHeap.size())));
68-
72+
wrongOrder = (key < getElementKey(elementIndex * 2)) || (key < getElementKey(Math.min(elementIndex * 2, maxHeap.size())));
73+
6974
}
7075
}
7176

@@ -84,21 +89,23 @@ public void insertElement(HeapElement element) {
8489

8590
@Override
8691
public void deleteElement(int elementIndex) {
87-
if (maxHeap.isEmpty())
88-
try {
89-
throw new EmptyHeapException("Attempt to delete an element from an empty heap");
90-
} catch (EmptyHeapException e) {
91-
e.printStackTrace();
92-
}
93-
if ((elementIndex > maxHeap.size()) && (elementIndex <= 0)) throw new IndexOutOfBoundsException("Index out of heap range");
92+
if (maxHeap.isEmpty())
93+
try {
94+
throw new EmptyHeapException("Attempt to delete an element from an empty heap");
95+
} catch (EmptyHeapException e) {
96+
e.printStackTrace();
97+
}
98+
if ((elementIndex > maxHeap.size()) || (elementIndex <= 0))
99+
throw new IndexOutOfBoundsException("Index out of heap range");
94100
// The last element in heap replaces the one to be deleted
95101
maxHeap.set(elementIndex - 1, getElement(maxHeap.size()));
96102
maxHeap.remove(maxHeap.size());
97103
// Shall the new element be moved up...
98-
if (getElementKey(elementIndex) > getElementKey((int) Math.floor(elementIndex/2))) toggleUp(elementIndex);
99-
// ... or down ?
100-
else if (((2*elementIndex <= maxHeap.size()) && (getElementKey(elementIndex) < getElementKey(elementIndex*2))) ||
101-
((2*elementIndex < maxHeap.size()) && (getElementKey(elementIndex) < getElementKey(elementIndex*2)))) toggleDown(elementIndex);
104+
if (getElementKey(elementIndex) > getElementKey((int) Math.floor(elementIndex / 2))) toggleUp(elementIndex);
105+
// ... or down ?
106+
else if (((2 * elementIndex <= maxHeap.size()) && (getElementKey(elementIndex) < getElementKey(elementIndex * 2))) ||
107+
((2 * elementIndex < maxHeap.size()) && (getElementKey(elementIndex) < getElementKey(elementIndex * 2))))
108+
toggleDown(elementIndex);
102109
}
103110

104111
@Override
@@ -109,7 +116,4 @@ public HeapElement getElement() throws EmptyHeapException {
109116
throw new EmptyHeapException("Heap is empty. Error retrieving element");
110117
}
111118
}
112-
113-
}
114-
115-
119+
}

0 commit comments

Comments
 (0)