diff --git a/java/datastructures/lib/src/main/java/datastructures/graph/Graph.java b/java/datastructures/lib/src/main/java/datastructures/graph/Graph.java index 4ea634b..86a62f3 100644 --- a/java/datastructures/lib/src/main/java/datastructures/graph/Graph.java +++ b/java/datastructures/lib/src/main/java/datastructures/graph/Graph.java @@ -1,8 +1,13 @@ package datastructures.graph; -import java.net.CookieManager; +import datastructures.linkedlist.Node; + +import datastructures.graph.Edge; +import datastructures.graph.Vertex; +import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; +import java.util.List; public class Graph> implements Comparable> { @@ -15,8 +20,12 @@ public Graph(int numberOfVertices) { public Vertex addVertex(T value) // addNode() { - // TODO: implement me - return null; + Vertex newVertex = new Vertex<>(value); + if(adjacencyLists.get(newVertex) == null){ + adjacencyLists.put(newVertex, null); + numberOfVertices ++; + } + return newVertex; } public void addEdge(Vertex start, Vertex destination) @@ -26,20 +35,37 @@ public void addEdge(Vertex start, Vertex destination) public void addEdge(Vertex start, Vertex destination, int weight) { - // TODO: implement me + Edge newEdge = new Edge<>(destination, weight); + if(adjacencyLists.get(start) == null){ + LinkedList> newLinkedList= new LinkedList<>(); + newLinkedList.add(newEdge); + adjacencyLists.put(start, newLinkedList); + }else { + LinkedList> existingLinkedList = adjacencyLists.get(start); + existingLinkedList.add(newEdge); + adjacencyLists.put(start, existingLinkedList); + } } public LinkedList> getVertices() // getNodes() { - // TODO: implement me - return null; + LinkedList> list = new LinkedList<>(); + for (Vertex vertex : adjacencyLists.keySet()){ + list.add(vertex); + } + return list; } - public LinkedList> getNeighbors(Vertex vertex) - { - // TODO: implement me - return null; - } +// public LinkedList> getNeighbors(Vertex vertex) +// { +// LinkedList> list = new LinkedList<>(); +// LinkedList> edgeLinkedList = adjacencyLists.get(vertex); +// if(edgeLinkedList !=null){ +// Node current; +// current = edgeLinkedList. +// } +// return list; +// } diff --git a/java/datastructures/lib/src/main/java/datastructures/trees/FizzBuzzTree.java b/java/datastructures/lib/src/main/java/datastructures/trees/FizzBuzzTree.java new file mode 100644 index 0000000..d6d25d5 --- /dev/null +++ b/java/datastructures/lib/src/main/java/datastructures/trees/FizzBuzzTree.java @@ -0,0 +1,5 @@ +package datastructures.trees; + +public class FizzBuzzTree { + +} diff --git a/java/datastructures/lib/src/main/java/datastructures/trees/Node.java b/java/datastructures/lib/src/main/java/datastructures/trees/Node.java index 9c57440..cf47fdf 100644 --- a/java/datastructures/lib/src/main/java/datastructures/trees/Node.java +++ b/java/datastructures/lib/src/main/java/datastructures/trees/Node.java @@ -16,7 +16,7 @@ public Node (T value) { this.right = null; } - // allows for constructing the trees easier in tests, + // allows for constructing the trees easier in test, // since this allows for adding in the right and the left node. public Node(T value, Node left, Node right) { this.value = value;