diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/out/production/FinalProject/FlightRoutesGraph$Edge.class b/out/production/FinalProject/FlightRoutesGraph$Edge.class new file mode 100644 index 0000000..245bf79 Binary files /dev/null and b/out/production/FinalProject/FlightRoutesGraph$Edge.class differ diff --git a/out/production/FinalProject/FlightRoutesGraph.class b/out/production/FinalProject/FlightRoutesGraph.class new file mode 100644 index 0000000..67892eb Binary files /dev/null and b/out/production/FinalProject/FlightRoutesGraph.class differ diff --git a/out/production/FinalProject/Main.class b/out/production/FinalProject/Main.class new file mode 100644 index 0000000..54b8b19 Binary files /dev/null and b/out/production/FinalProject/Main.class differ diff --git a/out/production/FinalProject/edu/greenriver/sdev333/BSTSet$Node.class b/out/production/FinalProject/edu/greenriver/sdev333/BSTSet$Node.class new file mode 100644 index 0000000..86f4613 Binary files /dev/null and b/out/production/FinalProject/edu/greenriver/sdev333/BSTSet$Node.class differ diff --git a/out/production/FinalProject/edu/greenriver/sdev333/BSTSet.class b/out/production/FinalProject/edu/greenriver/sdev333/BSTSet.class new file mode 100644 index 0000000..ed720d8 Binary files /dev/null and b/out/production/FinalProject/edu/greenriver/sdev333/BSTSet.class differ diff --git a/out/production/FinalProject/edu/greenriver/sdev333/HashingSet.class b/out/production/FinalProject/edu/greenriver/sdev333/HashingSet.class new file mode 100644 index 0000000..e067855 Binary files /dev/null and b/out/production/FinalProject/edu/greenriver/sdev333/HashingSet.class differ diff --git a/out/production/FinalProject/edu/greenriver/sdev333/MathSet.class b/out/production/FinalProject/edu/greenriver/sdev333/MathSet.class new file mode 100644 index 0000000..b6d6603 Binary files /dev/null and b/out/production/FinalProject/edu/greenriver/sdev333/MathSet.class differ diff --git a/out/production/FinalProject/edu/greenriver/sdev333/Queue$LinkedListIterator.class b/out/production/FinalProject/edu/greenriver/sdev333/Queue$LinkedListIterator.class new file mode 100644 index 0000000..e6bebde Binary files /dev/null and b/out/production/FinalProject/edu/greenriver/sdev333/Queue$LinkedListIterator.class differ diff --git a/out/production/FinalProject/edu/greenriver/sdev333/Queue$Node.class b/out/production/FinalProject/edu/greenriver/sdev333/Queue$Node.class new file mode 100644 index 0000000..8e49b37 Binary files /dev/null and b/out/production/FinalProject/edu/greenriver/sdev333/Queue$Node.class differ diff --git a/out/production/FinalProject/edu/greenriver/sdev333/Queue.class b/out/production/FinalProject/edu/greenriver/sdev333/Queue.class new file mode 100644 index 0000000..4ee4e02 Binary files /dev/null and b/out/production/FinalProject/edu/greenriver/sdev333/Queue.class differ diff --git a/src/FlightRoutesGraph.java b/src/FlightRoutesGraph.java new file mode 100644 index 0000000..73eb33e --- /dev/null +++ b/src/FlightRoutesGraph.java @@ -0,0 +1,89 @@ +import edu.greenriver.sdev333.BSTSet; +import edu.greenriver.sdev333.HashingSet; +import edu.greenriver.sdev333.MathSet; + +public class FlightRoutesGraph { + //two sets needed to model graph/network + //1. set of vertices (points, nodes, etc) -airports + //2. set of edges (where the points connect with each other) -routes + + private class Edge{ + private String city1; + private String city2; + + public Edge(String one, String two){ + this.city1 = one; + this.city2 = two; + } + } + + private MathSet nodes; + private MathSet connections; + + public FlightRoutesGraph(){ + nodes = new BSTSet<>(); + connections = new HashingSet<>(); + } + + public void addNode(String city){ + nodes.add(city); + } + public void addEdge(String city1, String city2){ + connections.add(new Edge(city1, city2)); + } + public MathSet getNeighbors(String city){ + //empty set to hold results + MathSet neighbors = new BSTSet<>(); + //loop through edges looking for node value + for (Edge e: connections.keys()) { + if(e.city1.equals(city)){ + neighbors.add(e.city2); + } else if (e.city2.equals(city)) { + neighbors.add(e.city1); + } + } + return neighbors; + } + + + public static void main(String[] args) { + FlightRoutesGraph g = new FlightRoutesGraph(); + //add cities + g.addNode("SEA"); + g.addNode("JFK"); + g.addNode("ORD"); + g.addNode("ATL"); + g.addNode("MCO"); + g.addNode("DEN"); + g.addNode("NED"); + g.addNode("ASE"); + g.addNode("PLD"); + //duplicate + g.addNode("PLD"); + + //add connections + g.addEdge("JFK", "MCO"); + g.addEdge("ATL", "MCO"); + g.addEdge("DEN", "ORD"); + g.addEdge("ORD", "ATL"); + g.addEdge("SEA", "MCO"); + g.addEdge("SEA", "JFK"); + g.addEdge("JFK", "ATL"); + + g.addEdge("JFK", "PLD"); + g.addEdge("ASE", "MCO"); + g.addEdge("DEN", "NED"); + g.addEdge("JFK", "NED"); + g.addEdge("SEA", "ASE"); + g.addEdge("SEA", "DEN"); + //duplicate + g.addEdge("JFK", "ATL"); + + //look for direct flights from JFK + MathSet directJFK = g.getNeighbors("JFK"); + for (String a: directJFK.keys()) { + System.out.println(a); + } + + } +} diff --git a/src/Main.java b/src/Main.java index 3e59c38..5f56f0a 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,61 @@ +import edu.greenriver.sdev333.BSTSet; +import edu.greenriver.sdev333.HashingSet; +import edu.greenriver.sdev333.MathSet; + + public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + + System.out.println("ISEMPTY SET ONE ------------"); + MathSet one = new HashingSet<>(); + System.out.println(one.isEmpty()); + one.add("first"); + one.add("second"); + one.add("third"); + one.add("fourth"); + one.add("first"); + one.add("tenth"); + one.add("ninth"); + one.add("eleventh"); + System.out.println(one.isEmpty()); + + MathSet two = new HashingSet<>(); + two.add("second"); + two.add("fourth"); + two.add("sixth"); + two.add("eighth"); + two.add("sixth"); + two.add("tenth"); + + //test add + System.out.println("TEST ADD SET TWO -----------"); + for (String a: two.keys()) { + System.out.println(a); + } + //test size + System.out.println("SIZE SET ONE ---------------"); + System.out.println(one.size()); + //test contains + System.out.println("CONTAINS SET ONE -----------"); + System.out.println(one.contains("first")); + System.out.println(one.contains("sixth")); + //test union + System.out.println("UNION --------------"); + MathSet union = one.union(two); + for (String a: union.keys()) { + System.out.println(a); + } + System.out.println("INTERSECTION--------"); + //test intersection + MathSet intersection = one.intersection(two); + for (String a: intersection.keys()) { + System.out.println(a); + } + //test difference + System.out.println("DIFFERENCE----------"); + MathSet difference = one.difference(two); + for (String a: difference.keys()) { + System.out.println(a); + } } } \ 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..b233297 --- /dev/null +++ b/src/edu/greenriver/sdev333/BSTSet.java @@ -0,0 +1,188 @@ +package edu.greenriver.sdev333; + +import java.util.Iterator; + +/** + * @author Katherine Watkins + * SDEV 333 + * Final Project + * @param + */ + +public class BSTSet > implements MathSet{ + private Node root; + 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; + } + } + + /** + * Puts the specified key into the set. + * + * @param key key to be added into the set + */ + @Override + public void add(KeyType key) { + root = add(root, key); + } + + private Node add(Node current, KeyType key){ + if(current == null){ + return new Node (key, 1); + } + int cmp = key.compareTo(current.key); + //go left + if(cmp < 0){ + current.left = add(current.left, key); + } + //go right + else if(cmp > 0){ + current.right = add(current.right, key); + } + else{ + current.key = key; + } + //update N + current.N = size(current.left) + size(current.right) + 1; + 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) { + for(KeyType currentKey: this.keys()){ + if(currentKey.equals(key)){ + return true; + } + } + return false; + } + + /** + * Is the set empty? + * + * @return true if the set is empty, false otherwise + */ + @Override + public boolean isEmpty() { + return this.size() == 0; + } + + /** + * Number of keys in the set + * + * @return number of keys in the set. + */ + @Override + public int size() { + return size(root); + } + private int size(Node current){ + if(current == null){ + return 0; + } + else{ + return current.N; + } + } + /** + * 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) { + MathSet result = new BSTSet<>(); + for (KeyType key : other.keys()) { + result.add(key); + } + for (KeyType key : this.keys()) { + result.add(key); + } + return result; + } + + /** + * 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) { + MathSet result = new BSTSet<>(); + for (KeyType key : other.keys()) { + if(this.contains(key)){ + result.add(key); + } + } + return result; + } + + /** + * 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 empty set to hold result + MathSet result = new BSTSet(); + + for (KeyType a: this.keys()) { + if(!other.contains(a)){ + result.add(a); + } + } + 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() { + //empty queue to hold my result + + Queue queue = new Queue<>(); + //start recursion + inOrder(root, queue); + //when done return the queue + return queue; + } + private void inOrder(Node current, Queue q){ + if(current == null){ + //do nothing - intentionally blank + return; + } + + inOrder(current.left, q); + q.enqueue(current.key); + inOrder(current.right, q); + } +} diff --git a/src/edu/greenriver/sdev333/HashingSet.java b/src/edu/greenriver/sdev333/HashingSet.java new file mode 100644 index 0000000..58e9a27 --- /dev/null +++ b/src/edu/greenriver/sdev333/HashingSet.java @@ -0,0 +1,158 @@ +package edu.greenriver.sdev333; +import java.util.LinkedList; + +/** + * @author Katherine Watkins + * SDEV 333 + * Final Project + * @param + */ + +public class HashingSet implements MathSet{ + private LinkedList [] hs; + private int M; + + public HashingSet(){ + this(997); + } + public HashingSet(int M){ + this.M = M; + hs = new LinkedList[M]; + for(int i= 0; i(); + } + } + private int hash(KeyType key){ + return (key.hashCode() & 0x7fffffff) % M; + } + + + /** + * Puts the specified key into the set. + * + * @param key key to be added into the set + */ + @Override + public void add(KeyType key) { + int index = hash(key); + if(!hs[index].contains(key)) { + hs[index].add(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) { + int index = hash(key); + if(hs[index].contains(key)){ + return true; + } + return false; + } + + /** + * 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() { + int size = 0; + for(int i=0; i < M; i++){ + size += hs[i].size(); + } + return size; + } + + /** + * 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) { + MathSet union = new HashingSet<>(); + for (KeyType key: other.keys()) { + union.add(key); + } + for(KeyType key : this.keys()){ + union.add(key); + } + return union; + } + + /** + * 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) { + MathSet intersection = new HashingSet<>(); + for(KeyType key : other.keys()){ + if(this.contains(key)){ + intersection.add(key); + } + } + return intersection; + } + + /** + * 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) { + MathSet difference = new HashingSet<>(); + for (KeyType key: this.keys()) { + if(!other.contains(key)){ + difference.add(key); + } + } + return difference; + } + + /** + * Retrieves a collection of all the keys in this set. + * + * @return a collection of all keys in this set + */ + @Override + public Iterable keys() { + Queue queue = new Queue<>(); + for(int i =0; i