From 8b92c3fdbeedb7ae0d3b5b6d79871af91cf7c2ea Mon Sep 17 00:00:00 2001
From: yanglbme
@@ -10,32 +10,31 @@ package heaps;
* max-heap).
All heap-related operations (inserting or deleting an element, extracting the min or max) are performed in * O(log n) time.
+ * * @author Nicolas Renard - * - * */ public interface Heap { - + /** - * * @return the top element in the heap, the one with lowest key for min-heap or with * the highest key for max-heap - * @throws Exception if heap is empty + * @throws EmptyHeapException if heap is empty */ - public abstract HeapElement getElement() throws EmptyHeapException; + HeapElement getElement() throws EmptyHeapException; + /** * Inserts an element in the heap. Adds it to then end and toggle it until it finds its * right position. - * + * * @param element an instance of the HeapElement class. */ - public abstract void insertElement(HeapElement element); - + void insertElement(HeapElement element); + /** * Delete an element in the heap. - * + * * @param elementIndex int containing the position in the heap of the element to be deleted. */ - public abstract void deleteElement(int elementIndex); + void deleteElement(int elementIndex); -} +} \ No newline at end of file diff --git a/DataStructures/Heaps/HeapElement.java b/DataStructures/Heaps/HeapElement.java index e0cc93ccb..60640346b 100644 --- a/DataStructures/Heaps/HeapElement.java +++ b/DataStructures/Heaps/HeapElement.java @@ -1,7 +1,7 @@ /** - * + * */ -package heaps; +package Heaps; import java.lang.Double; import java.lang.Object; @@ -12,116 +12,110 @@ import java.lang.Object; * or double, either primitive type or object) and any kind of IMMUTABLE object the user sees fit * to carry any information he/she likes. Be aware that the use of a mutable object might * jeopardize the integrity of this information. - * @author Nicolas Renard * + * @author Nicolas Renard */ public class HeapElement { private final double key; private final Object additionalInfo; - + // Constructors /** - * - * @param key : a number of primitive type 'double' + * @param key : a number of primitive type 'double' * @param info : any kind of IMMUTABLE object. May be null, since the purpose is only to carry - * additional information of use for the user + * additional information of use for the user */ public HeapElement(double key, Object info) { this.key = key; this.additionalInfo = info; } - + /** - * - * @param key : a number of primitive type 'int' + * @param key : a number of primitive type 'int' * @param info : any kind of IMMUTABLE object. May be null, since the purpose is only to carry - * additional information of use for the user + * additional information of use for the user */ public HeapElement(int key, Object info) { this.key = key; this.additionalInfo = info; } - + /** - * - * @param key : a number of object type 'Integer' + * @param key : a number of object type 'Integer' * @param info : any kind of IMMUTABLE object. May be null, since the purpose is only to carry - * additional information of use for the user + * additional information of use for the user */ public HeapElement(Integer key, Object info) { this.key = key; this.additionalInfo = info; } - + /** - * - * @param key : a number of object type 'Double' + * @param key : a number of object type 'Double' * @param info : any kind of IMMUTABLE object. May be null, since the purpose is only to carry - * additional information of use for the user + * additional information of use for the user */ public HeapElement(Double key, Object info) { this.key = key; this.additionalInfo = info; } - + /** - * * @param key : a number of primitive type 'double' */ public HeapElement(double key) { this.key = key; this.additionalInfo = null; } - + /** - * * @param key : a number of primitive type 'int' */ public HeapElement(int key) { this.key = key; this.additionalInfo = null; } - + /** - * * @param key : a number of object type 'Integer' */ public HeapElement(Integer key) { this.key = key; this.additionalInfo = null; } - + /** - * * @param key : a number of object type 'Double' */ public HeapElement(Double key) { this.key = key; this.additionalInfo = null; } - + // Getters + /** * @return the object containing the additional info provided by the user. */ public Object getInfo() { return additionalInfo; } + /** * @return the key value of the element */ public double getKey() { return key; } - + // Overridden object methods - + public String toString() { - return "Key: " + key + " - " +additionalInfo.toString(); + return "Key: " + key + " - " + additionalInfo.toString(); } + /** - * * @param otherHeapElement * @return true if the keys on both elements are identical and the additional info objects * are identical. diff --git a/DataStructures/Heaps/MaxHeap.java b/DataStructures/Heaps/MaxHeap.java index 5f774e353..302840c14 100644 --- a/DataStructures/Heaps/MaxHeap.java +++ b/DataStructures/Heaps/MaxHeap.java @@ -1,4 +1,4 @@ -package heaps; +package Heaps; import java.util.ArrayList; import java.util.List; @@ -6,66 +6,71 @@ import java.util.List; /** * Heap tree where a node's key is higher than or equal to its parent's and lower than or equal * to its children's. - * @author Nicolas Renard * + * @author Nicolas Renard */ public class MaxHeap implements Heap { - + private final List* NOTE: The inputs to Dijkstra's algorithm are a directed and weighted graph consisting * of 2 or more nodes, generally represented by an adjacency matrix or list, and a start node. - * + *
* Original source of code: https://rosettacode.org/wiki/Dijkstra%27s_algorithm#Java
* Also most of the comments are from RosettaCode.
- *
*/
-//import java.io.*;
-import java.util.*;
-public class Dijkstra {
- private static final Graph.Edge[] GRAPH = {
- new Graph.Edge("a", "b", 7), //Distance from node "a" to node "b" is 7. In the current Graph there is no way to move the other way (e,g, from "b" to "a"), a new edge would be needed for that
- new Graph.Edge("a", "c", 9),
- new Graph.Edge("a", "f", 14),
- new Graph.Edge("b", "c", 10),
- new Graph.Edge("b", "d", 15),
- new Graph.Edge("c", "d", 11),
- new Graph.Edge("c", "f", 2),
- new Graph.Edge("d", "e", 6),
- new Graph.Edge("e", "f", 9),
- };
- private static final String START = "a";
- private static final String END = "e";
- /**
- * main function
- * Will run the code with "GRAPH" that was defined above.
- */
- public static void main(String[] args) {
- Graph g = new Graph(GRAPH);
- g.dijkstra(START);
- g.printPath(END);
- //g.printAllPaths();
- }
+import java.util.*;
+
+public class Dijkstra {
+ private static final Graph.Edge[] GRAPH = {
+ // Distance from node "a" to node "b" is 7.
+ // In the current Graph there is no way to move the other way (e,g, from "b" to "a"),
+ // a new edge would be needed for that
+ new Graph.Edge("a", "b", 7),
+ new Graph.Edge("a", "c", 9),
+ new Graph.Edge("a", "f", 14),
+ new Graph.Edge("b", "c", 10),
+ new Graph.Edge("b", "d", 15),
+ new Graph.Edge("c", "d", 11),
+ new Graph.Edge("c", "f", 2),
+ new Graph.Edge("d", "e", 6),
+ new Graph.Edge("e", "f", 9),
+ };
+ private static final String START = "a";
+ private static final String END = "e";
+
+ /**
+ * main function
+ * Will run the code with "GRAPH" that was defined above.
+ */
+ public static void main(String[] args) {
+ Graph g = new Graph(GRAPH);
+ g.dijkstra(START);
+ g.printPath(END);
+ //g.printAllPaths();
+ }
}
-
+
class Graph {
- private final Map