| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent bb670a2 commit 8b92c3f
9 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,16 +1,15 @@ | |||
| 1 | 1 | /** | |
| 2 | - * | ||
| 2 | + * | ||
| 3 | 3 | */ | |
| 4 | - package heaps; | ||
| 4 | + package Heaps; | ||
| 5 | 5 | ||
| 6 | 6 | /** | |
| 7 | 7 | * @author Nicolas Renard | |
| 8 | 8 | * Exception to be thrown if the getElement method is used on an empty heap. | |
| 9 | - * | ||
| 10 | 9 | */ | |
| 11 | 10 | @SuppressWarnings("serial") | |
| 12 | 11 | public class EmptyHeapException extends Exception { | |
| 13 | - | ||
| 12 | + | ||
| 14 | 13 | public EmptyHeapException(String message) { | |
| 15 | 14 | super(message); | |
| 16 | 15 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,4 @@ | |||
| 1 | - package heaps; | ||
| 1 | + package Heaps; | ||
| 2 | 2 | ||
| 3 | 3 | /** | |
| 4 | 4 | * Interface common to heap data structures.<br> | |
@@ -10,32 +10,31 @@ | |||
| 10 | 10 | * max-heap).</p> | |
| 11 | 11 | * <p>All heap-related operations (inserting or deleting an element, extracting the min or max) are performed in | |
| 12 | 12 | * O(log n) time.</p> | |
| 13 | + * | ||
| 13 | 14 | * @author Nicolas Renard | |
| 14 | - * | ||
| 15 | - * | ||
| 16 | 15 | */ | |
| 17 | 16 | public interface Heap { | |
| 18 | - | ||
| 17 | + | ||
| 19 | 18 | /** | |
| 20 | - * | ||
| 21 | 19 | * @return the top element in the heap, the one with lowest key for min-heap or with | |
| 22 | 20 | * the highest key for max-heap | |
| 23 | - * @throws Exception if heap is empty | ||
| 21 | + * @throws EmptyHeapException if heap is empty | ||
| 24 | 22 | */ | |
| 25 | - public abstract HeapElement getElement() throws EmptyHeapException; | ||
| 23 | + HeapElement getElement() throws EmptyHeapException; | ||
| 24 | + | ||
| 26 | 25 | /** | |
| 27 | 26 | * Inserts an element in the heap. Adds it to then end and toggle it until it finds its | |
| 28 | 27 | * right position. | |
| 29 | - * | ||
| 28 | + * | ||
| 30 | 29 | * @param element an instance of the HeapElement class. | |
| 31 | 30 | */ | |
| 32 | - public abstract void insertElement(HeapElement element); | ||
| 33 | - | ||
| 31 | + void insertElement(HeapElement element); | ||
| 32 | + | ||
| 34 | 33 | /** | |
| 35 | 34 | * Delete an element in the heap. | |
| 36 | - * | ||
| 35 | + * | ||
| 37 | 36 | * @param elementIndex int containing the position in the heap of the element to be deleted. | |
| 38 | 37 | */ | |
| 39 | - public abstract void deleteElement(int elementIndex); | ||
| 38 | + void deleteElement(int elementIndex); | ||
| 40 | 39 | ||
| 41 | - } | ||
| 40 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,7 +1,7 @@ | |||
| 1 | 1 | /** | |
| 2 | - * | ||
| 2 | + * | ||
| 3 | 3 | */ | |
| 4 | - package heaps; | ||
| 4 | + package Heaps; | ||
| 5 | 5 | ||
| 6 | 6 | import java.lang.Double; | |
| 7 | 7 | import java.lang.Object; | |
@@ -12,116 +12,110 @@ | |||
| 12 | 12 | * or double, either primitive type or object) and any kind of IMMUTABLE object the user sees fit | |
| 13 | 13 | * to carry any information he/she likes. Be aware that the use of a mutable object might | |
| 14 | 14 | * jeopardize the integrity of this information. </p> | |
| 15 | - * @author Nicolas Renard | ||
| 16 | 15 | * | |
| 16 | + * @author Nicolas Renard | ||
| 17 | 17 | */ | |
| 18 | 18 | public class HeapElement { | |
| 19 | 19 | private final double key; | |
| 20 | 20 | private final Object additionalInfo; | |
| 21 | - | ||
| 21 | + | ||
| 22 | 22 | // Constructors | |
| 23 | 23 | ||
| 24 | 24 | /** | |
| 25 | - * | ||
| 26 | - * @param key : a number of primitive type 'double' | ||
| 25 | + * @param key : a number of primitive type 'double' | ||
| 27 | 26 | * @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 | ||
| 29 | 28 | */ | |
| 30 | 29 | public HeapElement(double key, Object info) { | |
| 31 | 30 | this.key = key; | |
| 32 | 31 | this.additionalInfo = info; | |
| 33 | 32 | } | |
| 34 | - | ||
| 33 | + | ||
| 35 | 34 | /** | |
| 36 | - * | ||
| 37 | - * @param key : a number of primitive type 'int' | ||
| 35 | + * @param key : a number of primitive type 'int' | ||
| 38 | 36 | * @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 | ||
| 40 | 38 | */ | |
| 41 | 39 | public HeapElement(int key, Object info) { | |
| 42 | 40 | this.key = key; | |
| 43 | 41 | this.additionalInfo = info; | |
| 44 | 42 | } | |
| 45 | - | ||
| 43 | + | ||
| 46 | 44 | /** | |
| 47 | - * | ||
| 48 | - * @param key : a number of object type 'Integer' | ||
| 45 | + * @param key : a number of object type 'Integer' | ||
| 49 | 46 | * @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 | ||
| 51 | 48 | */ | |
| 52 | 49 | public HeapElement(Integer key, Object info) { | |
| 53 | 50 | this.key = key; | |
| 54 | 51 | this.additionalInfo = info; | |
| 55 | 52 | } | |
| 56 | - | ||
| 53 | + | ||
| 57 | 54 | /** | |
| 58 | - * | ||
| 59 | - * @param key : a number of object type 'Double' | ||
| 55 | + * @param key : a number of object type 'Double' | ||
| 60 | 56 | * @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 | ||
| 62 | 58 | */ | |
| 63 | 59 | public HeapElement(Double key, Object info) { | |
| 64 | 60 | this.key = key; | |
| 65 | 61 | this.additionalInfo = info; | |
| 66 | 62 | } | |
| 67 | - | ||
| 63 | + | ||
| 68 | 64 | /** | |
| 69 | - * | ||
| 70 | 65 | * @param key : a number of primitive type 'double' | |
| 71 | 66 | */ | |
| 72 | 67 | public HeapElement(double key) { | |
| 73 | 68 | this.key = key; | |
| 74 | 69 | this.additionalInfo = null; | |
| 75 | 70 | } | |
| 76 | - | ||
| 71 | + | ||
| 77 | 72 | /** | |
| 78 | - * | ||
| 79 | 73 | * @param key : a number of primitive type 'int' | |
| 80 | 74 | */ | |
| 81 | 75 | public HeapElement(int key) { | |
| 82 | 76 | this.key = key; | |
| 83 | 77 | this.additionalInfo = null; | |
| 84 | 78 | } | |
| 85 | - | ||
| 79 | + | ||
| 86 | 80 | /** | |
| 87 | - * | ||
| 88 | 81 | * @param key : a number of object type 'Integer' | |
| 89 | 82 | */ | |
| 90 | 83 | public HeapElement(Integer key) { | |
| 91 | 84 | this.key = key; | |
| 92 | 85 | this.additionalInfo = null; | |
| 93 | 86 | } | |
| 94 | - | ||
| 87 | + | ||
| 95 | 88 | /** | |
| 96 | - * | ||
| 97 | 89 | * @param key : a number of object type 'Double' | |
| 98 | 90 | */ | |
| 99 | 91 | public HeapElement(Double key) { | |
| 100 | 92 | this.key = key; | |
| 101 | 93 | this.additionalInfo = null; | |
| 102 | 94 | } | |
| 103 | - | ||
| 95 | + | ||
| 104 | 96 | // Getters | |
| 97 | + | ||
| 105 | 98 | /** | |
| 106 | 99 | * @return the object containing the additional info provided by the user. | |
| 107 | 100 | */ | |
| 108 | 101 | public Object getInfo() { | |
| 109 | 102 | return additionalInfo; | |
| 110 | 103 | } | |
| 104 | + | ||
| 111 | 105 | /** | |
| 112 | 106 | * @return the key value of the element | |
| 113 | 107 | */ | |
| 114 | 108 | public double getKey() { | |
| 115 | 109 | return key; | |
| 116 | 110 | } | |
| 117 | - | ||
| 111 | + | ||
| 118 | 112 | // Overridden object methods | |
| 119 | - | ||
| 113 | + | ||
| 120 | 114 | public String toString() { | |
| 121 | - return "Key: " + key + " - " +additionalInfo.toString(); | ||
| 115 | + return "Key: " + key + " - " + additionalInfo.toString(); | ||
| 122 | 116 | } | |
| 117 | + | ||
| 123 | 118 | /** | |
| 124 | - * | ||
| 125 | 119 | * @param otherHeapElement | |
| 126 | 120 | * @return true if the keys on both elements are identical and the additional info objects | |
| 127 | 121 | * are identical. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,71 +1,76 @@ | |||
| 1 | - package heaps; | ||
| 1 | + package Heaps; | ||
| 2 | 2 | ||
| 3 | 3 | import java.util.ArrayList; | |
| 4 | 4 | import java.util.List; | |
| 5 | 5 | ||
| 6 | 6 | /** | |
| 7 | 7 | * Heap tree where a node's key is higher than or equal to its parent's and lower than or equal | |
| 8 | 8 | * to its children's. | |
| 9 | - * @author Nicolas Renard | ||
| 10 | 9 | * | |
| 10 | + * @author Nicolas Renard | ||
| 11 | 11 | */ | |
| 12 | 12 | public class MaxHeap implements Heap { | |
| 13 | - | ||
| 13 | + | ||
| 14 | 14 | 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<>(); | ||
| 18 | 18 | for (HeapElement heapElement : listElements) { | |
| 19 | 19 | if (heapElement != null) insertElement(heapElement); | |
| 20 | 20 | else System.out.println("Null element. Not added to heap"); | |
| 21 | 21 | } | |
| 22 | 22 | 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 | + */ | ||
| 26 | 31 | 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"); | ||
| 28 | 34 | return maxHeap.get(elementIndex - 1); | |
| 29 | 35 | } | |
| 30 | - | ||
| 36 | + | ||
| 31 | 37 | // Get the key of the element at a given index | |
| 32 | 38 | private double getElementKey(int elementIndex) { | |
| 33 | 39 | return maxHeap.get(elementIndex - 1).getKey(); | |
| 34 | 40 | } | |
| 35 | - | ||
| 41 | + | ||
| 36 | 42 | // Swaps two elements in the heap | |
| 37 | 43 | private void swap(int index1, int index2) { | |
| 38 | 44 | HeapElement temporaryElement = maxHeap.get(index1 - 1); | |
| 39 | 45 | maxHeap.set(index1 - 1, maxHeap.get(index2 - 1)); | |
| 40 | 46 | maxHeap.set(index2 - 1, temporaryElement); | |
| 41 | 47 | } | |
| 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 | ||
| 44 | 50 | private void toggleUp(int elementIndex) { | |
| 45 | 51 | 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); | ||
| 49 | 55 | } | |
| 50 | 56 | } | |
| 51 | - | ||
| 57 | + | ||
| 52 | 58 | // 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 | ||
| 54 | 60 | private void toggleDown(int elementIndex) { | |
| 55 | 61 | 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) { | ||
| 58 | 64 | // 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; | ||
| 62 | 71 | } | |
| 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 | + | ||
| 69 | 74 | } | |
| 70 | 75 | } | |
| 71 | 76 | ||
@@ -84,21 +89,23 @@ public void insertElement(HeapElement element) { | |||
| 84 | 89 | ||
| 85 | 90 | @Override | |
| 86 | 91 | 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"); | ||
| 94 | 100 | // The last element in heap replaces the one to be deleted | |
| 95 | 101 | maxHeap.set(elementIndex - 1, getElement(maxHeap.size())); | |
| 96 | 102 | maxHeap.remove(maxHeap.size()); | |
| 97 | 103 | // 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); | ||
| 102 | 109 | } | |
| 103 | 110 | ||
| 104 | 111 | @Override | |
@@ -109,7 +116,4 @@ public HeapElement getElement() throws EmptyHeapException { | |||
| 109 | 116 | throw new EmptyHeapException("Heap is empty. Error retrieving element"); | |
| 110 | 117 | } | |
| 111 | 118 | } | |
| 112 | - | ||
| 113 | - } | ||
| 114 | - | ||
| 115 | - | ||
| 119 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments