diff --git a/README.md b/README.md new file mode 100644 index 0000000..a79c45a --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# FinalProject diff --git a/src/FlightRoutesGraph.java b/src/FlightRoutesGraph.java new file mode 100644 index 0000000..ea3cbc8 --- /dev/null +++ b/src/FlightRoutesGraph.java @@ -0,0 +1,75 @@ +import edu.greenriver.sdev333.BSTSet; +import edu.greenriver.sdev333.MathSet; +import edu.greenriver.sdev333.SCHashSet; + +public class FlightRoutesGraph { + // two sets needed to modal a graph (network) + // 1. a set of verticals (points, nodes) - airports + // 2. a set of edges (connections, lines, relationships) - route between airports + + private class Edge { + private String node1; + private String node2; + + public Edge(String from, String to) { + node1 = from; + node2 = to; + } + } + + private MathSet nodes; + private MathSet edges; + + public FlightRoutesGraph() { + nodes = new BSTSet<>(); // BST ok here b/c strings are comparable + edges = new SCHashSet<>(); // must use HashSet here b/c edges are not comparable + } + + public void addNode(String city){ + nodes.add(city); + } + + public void addEdge(String city1, String city2) { + Edge connection = new Edge(city1,city2); + edges.add(connection); + } + + MathSet getNeighbors(String city) { + // create an empty set to hold the results + MathSet neighbors = new BSTSet<>(); + + // loop through the edges and check + // if the city is either in node1 or node2 + for (Edge e : edges.keys()) { + if (e.node1.equals(city)) { + neighbors.add(e.node2); + } + else if (e.node2.equals(city)) { + neighbors.add(e.node1); + } + } + return neighbors; + } + + public static void main(String[] args) { + FlightRoutesGraph g = new FlightRoutesGraph(); + + // add all the cities first (nodes) + g.addNode("JFK"); + g.addNode("ORD"); + g.addNode("ATL"); + g.addNode("MCO"); + g.addNode("DEN"); + + // add connections between cities (edges, routes) + g.addEdge("JFK","MCO"); + g.addEdge("ATL","MCO"); + g.addEdge("DEN","ORD"); + g.addEdge("ORD","ATL"); + // more to go if you want... + + //look for direct flights from JFK + MathSet directJFK = g.getNeighbors("JFK"); + MathSet directFromATL = g.getNeighbors("ATL"); + } +} diff --git a/src/Main.java b/src/Main.java index 3e59c38..be3eabc 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,24 @@ +import edu.greenriver.sdev333.BSTSet; +import edu.greenriver.sdev333.MathSet; + +import java.util.Scanner; + +/** + * @Junny Noriega-Arenas + * Final Project Part 1 + * SDEV 333 + * Description: Create two classes that implements the MathSet interface, which represents a mathematical set. + */ + public class Main { public static void main(String[] args) { System.out.println("Hello world!"); + + String inputString = "S E A R C H E X A M P L E"; + + Scanner input = new Scanner(inputString); + + MathSet st = new BSTSet<>(); + } } \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/BSTSet.java b/src/edu/greenriver/sdev333/BSTSet.java new file mode 100644 index 0000000..f2548f7 --- /dev/null +++ b/src/edu/greenriver/sdev333/BSTSet.java @@ -0,0 +1,162 @@ +package edu.greenriver.sdev333; + +import static javax.swing.UIManager.get; + +public class BSTSet> implements MathSet { + // node helper class + private class Node { + private KeyType key; + private Node left; + private Node right; + private int N; + + public Node (KeyType key, int N) { + this.key = key; + this.N = N; + } + } + + // field + private Node root; + + /** + * Puts the specified key into the set. + * + * @param key key to be added into the set + */ + @Override + public void add(KeyType key) { + // starts the recursion + root = add(root, key); + + } + public Node add(Node current, KeyType key) { + // current is the root of the subtree we are looking at + + // we are at where we are supposed to be + if (current == null) { + return new Node(key,1); + } + + int cmp = key.compareTo(current.key); + // cmp will be -1 (negative) if key < current.key + // cmp will be 0 (zero) if key == current.key + // cmp will be +1 (positive) if key > current.key + + if (cmp < 0) { + // go left + current.left = add(current.left,key); + } + else if (cmp > 0) { + // go right + current.right = add(current.right,key); + } + else { + // key already exists, replace the data(val) + current.key = key; + } + + return current; + } + + /** + * Is the key in the set? + * + * @param key key to check + * @return true if key is in the set, false otherwise + */ + @Override + public boolean contains(KeyType key) { + return get(key) != null; + } + + /** + * Is the set empty? + * + * @return true if the set is empty, false otherwise + */ + @Override + public boolean isEmpty() { + return size() == 0; + } + + /** + * Number of keys in the set + * + * @return number of keys in the set. + */ + @Override + public int size() { + return 0; + } + + /** + * Determine the union of this set with another specified set. + * Returns A union B, where A is this set, B is other set. + * A union B = {key | A.contains(key) OR B.contains(key)}. + * Does not change the contents of this set or the other set. + * + * @param other specified set to union + * @return the union of this set with other + */ + @Override + public MathSet union(MathSet other) { + return null; + } + + /** + * Determine the intersection of this set with another specified set. + * Returns A intersect B, where A is this set, B is other set. + * A intersect B = {key | A.contains(key) AND B.contains(key)}. + * Does not change the contents of this set or the other set. + * + * @param other specified set to intersect + * @return the intersection of this set with other + */ + @Override + public MathSet intersection(MathSet other) { + return null; + } + + /** + * Determine the difference of this set with another specified set. + * Returns A difference B, where A is this set, B is other set. + * A difference B = {key | A.contains(key) AND !B.contains(key)}. + * Does not change the contents of this set or the other set. + * + * @param other specified set to difference + * @return the difference of this set with other + */ + @Override + public MathSet difference(MathSet other) { + // create an empty set that will hold the result + MathSet result = new BSTSet(); + + // iterate (walk) through all items in this +// Iterator itr = (Iterator) this.keys(); +// while (itr.hasNext()) { +// KeyType currentKey = itr.next(); +// if (other.contains(currentKey)) { +// result.add(currentKey); +// } +// } + + for (KeyType currentKey: this.keys()) { + if (!other.contains(currentKey)) { + result.add(currentKey); + } + } + + return result; + } + + /** + * Retrieves a collection of all the keys in this set. + * + * @return a collection of all keys in this set + */ + @Override + public Iterable keys() { + return null; + } +} diff --git a/src/edu/greenriver/sdev333/Queue.java b/src/edu/greenriver/sdev333/Queue.java index 638d71c..b9e4c45 100644 --- a/src/edu/greenriver/sdev333/Queue.java +++ b/src/edu/greenriver/sdev333/Queue.java @@ -3,6 +3,7 @@ import java.util.Iterator; /** + * @author junnynoriega-arenas * FIFO queue, page 151 of the red book */ public class Queue implements Iterable { diff --git a/src/edu/greenriver/sdev333/SCHashSet.java b/src/edu/greenriver/sdev333/SCHashSet.java new file mode 100644 index 0000000..3e0d9a3 --- /dev/null +++ b/src/edu/greenriver/sdev333/SCHashSet.java @@ -0,0 +1,99 @@ +package edu.greenriver.sdev333; + +public class SCHashSet> implements MathSet { + // field + private int M; + + /** + * Puts the specified key into the set. + * + * @param key key to be added into the set + */ + @Override + public void add(KeyType key) { + + } + + /** + * Is the key in the set? + * + * @param key key to check + * @return true if key is in the set, false otherwise + */ + @Override + public boolean contains(KeyType key) { + return false; + } + + /** + * Is the set empty? + * + * @return true if the set is empty, false otherwise + */ + @Override + public boolean isEmpty() { + return false; + } + + /** + * Number of keys in the set + * + * @return number of keys in the set. + */ + @Override + public int size() { + return 0; + } + + /** + * Determine the union of this set with another specified set. + * Returns A union B, where A is this set, B is other set. + * A union B = {key | A.contains(key) OR B.contains(key)}. + * Does not change the contents of this set or the other set. + * + * @param other specified set to union + * @return the union of this set with other + */ + @Override + public MathSet union(MathSet other) { + return null; + } + + /** + * Determine the intersection of this set with another specified set. + * Returns A intersect B, where A is this set, B is other set. + * A intersect B = {key | A.contains(key) AND B.contains(key)}. + * Does not change the contents of this set or the other set. + * + * @param other specified set to intersect + * @return the intersection of this set with other + */ + @Override + public MathSet intersection(MathSet other) { + return null; + } + + /** + * Determine the difference of this set with another specified set. + * Returns A difference B, where A is this set, B is other set. + * A difference B = {key | A.contains(key) AND !B.contains(key)}. + * Does not change the contents of this set or the other set. + * + * @param other specified set to difference + * @return the difference of this set with other + */ + @Override + public MathSet difference(MathSet other) { + return null; + } + + /** + * Retrieves a collection of all the keys in this set. + * + * @return a collection of all keys in this set + */ + @Override + public Iterable keys() { + return null; + } +}