diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file 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/src/FlightRoutesGraph.java b/src/FlightRoutesGraph.java new file mode 100644 index 0000000..c4342db --- /dev/null +++ b/src/FlightRoutesGraph.java @@ -0,0 +1,79 @@ +import edu.greenriver.sdev333.BSTSet; +import edu.greenriver.sdev333.HashSet; +import edu.greenriver.sdev333.MathSet; + +public class FlightRoutesGraph { + // two sets needed to model a graph (network) + // 1. a set of vertices (points,nodes) - airports + // 2. a set of edges (connections, lines, relationship) - route between airports + + /** + * private helper class for within MathSet + */ + 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 HashSet<>(); // must use HashSet here b/c edges are not comparable + } + + public void addNode(String 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 neigbors = 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)) { + neigbors.add(e.node2); + } else if (e.node2.equals(city)) { + neigbors.add(e.node1); + } + } + + return neigbors; + } + + public static void main(String[] args) { + FlightRoutesGraph graph = new FlightRoutesGraph(); + + // add all teh cites first (nodes) + graph.addNode("JFK"); + graph.addNode("ORD"); + graph.addNode("ATL"); + graph.addNode("MCO"); + graph.addNode("DEN"); + + // add connections between cities (edges, route) + graph.addEdge("JFK","MCO"); + graph.addEdge("ALT","MCO"); + graph.addEdge("DEN","ORD"); + graph.addEdge("JFK","MCO"); + graph.addEdge("JFK","MCO"); + + // loo for direct flights from JFK + MathSet directJFK = graph.getNeighbors("JFK"); + MathSet directFromATL = graph.getNeighbors("ATL"); + + } +} diff --git a/src/Main.java b/src/Main.java index 3e59c38..3c6eda0 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,31 @@ +import edu.greenriver.sdev333.BSTSet; +import edu.greenriver.sdev333.MathSet; + public class Main { public static void main(String[] args) { + System.out.println("Hello world!"); + + // CREATE 2 SETS + // ADD ITEMS TO EACH OF THE SETS (SOME SAME, SOME DIFFERENT) + + // TEST + // SET INTERSECTION + // SET UNION + // SET DIFFERENCE + + MathSet set1 = new BSTSet<>(); + set1.add("Ken"); + set1.add("Tina"); + + MathSet set2 = new BSTSet<>(); + + // union set 1 and set 2, save into result1 + MathSet result1 = set1.union(set2); + + //print out keys from result 1 + for (String key : result1.keys()) { + System.out.println(key); + } } } \ 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..b68d458 --- /dev/null +++ b/src/edu/greenriver/sdev333/BSTSet.java @@ -0,0 +1,167 @@ +package edu.greenriver.sdev333; + +public class BSTSet> implements MathSet { + + // private 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) { + 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); + if(cmp < 0) { + current.left = add(current.left, key); + } + if(cmp > 0) { + current.right = add(current.right,key); + } + // increment 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) { + while(root!=null) { + /// not sure what to put here + } + 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() { + return size(root); + } + + // helper class for size + private int size(Node current) { + if(current == null) { + return 0; + } + 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 union = new BSTSet(); + + for(KeyType key : this.keys()) { + union.add(key); + } + for(KeyType key : other.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) { + 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 the 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/HashSet.java b/src/edu/greenriver/sdev333/HashSet.java new file mode 100644 index 0000000..e04cdf9 --- /dev/null +++ b/src/edu/greenriver/sdev333/HashSet.java @@ -0,0 +1,149 @@ +package edu.greenriver.sdev333; + +public class HashSet implements MathSet { + // Used SeparateChainingHashST as model/example + // ^^^ code manges the "buckets" of the hash table + + // array of linked list + private HashSet[] set; + private int M; // M is the number of buckets + // VVV code manages the insides of the buckets of the hash table + // a linked list is the inside each bucket + // SeperateChainingHashST depends on having SequentialSearchST + // will need to either bring in SequentialSearchST + // or write your own linked list + + public HashSet() { + } + public HashSet(int M) { + // take the number of buckets and save it to fields + this.M = M; + + // create a array + set = new HashSet[M]; + + // for each position in the array (for each bucket) + // create linked list in each bucket + for(int i = 0; i < M; i++) { + set[i] = new HashSet<>(); + } + } + + private int hash(Key key) { + // hash function = they give me a key, I return an int + // return an int (bucket #, array index) + 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(Key key) { + int i = hash(key); + set[i].get(key); + } + + public Key get(Key key) { + int i = hash(key); // find the bucket number + // go into that bucket and see if it's there + return set[i].get(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(Key key) { + // not sure what to put here + 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 count = 0; + for(int i = 0; i < M; i++) { + count+= set[i].size(); + } + return count; + } + + /** + * 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 HashSet(M); + + for(Key key : this.keys()) { + union.add(key); + } + for(Key key : other.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) { + 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; + } +}