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..8d89a0b --- /dev/null +++ b/src/FlightRoutesGraph.java @@ -0,0 +1,74 @@ +import edu.greenriver.sdev333.BSTSet; +import edu.greenriver.sdev333.MathSet; + +import java.util.HashSet; + +public class FlightRoutesGraph { + + + + 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<>(); + edges = new HashSet<>(); + } + + public void addNode(String city) { + nodes.add(city); + } + + public void addEdge(String city1, String city2) { + Edge connection = new Edge(city1, city2); + edges.add(connection); + } + + + + /** + * + * @param city + */ + public MathSet getNeighbors(String city) { + MathSet neighbors = new BSTSet<>(); + + 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(); + g.addNode("JFK"); + g.addNode("ORD"); + g.addNode("ATL"); + g.addNode("MCO"); + g.addNode("DEN"); + g.addEdge("ATL", "MCO"); + g.addEdge("JFK", "MCO"); + g.addEdge("DEN", "ORD"); + g.addEdge("ORD", "ATL"); + MathSet directFromJFK = g.getNeighbors("JFK"); + MathSet directFromATL = g.getNeighbors("ATL"); + + } +} diff --git a/src/Main.java b/src/Main.java index 3e59c38..5e464fe 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,47 @@ +import edu.greenriver.sdev333.BSTSet; +import edu.greenriver.sdev333.MathSet; +import edu.greenriver.sdev333.SeparateChainingHashTable; +import java.security.Key; + public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + + String testChar = "P O O P I E"; + + MathSet testset = new SeparateChainingHashTableSet<>(); + + Scanner input = new Scanner(testChar); + + System.out.println(testset.isEmpty()); + + while(input.hasNext()){ + String key = input.next(); + testset.add(key); + } + + System.out.println(testset.size()); + System.out.println(testset.contains("P")); + System.out.println(testset.contains("O")); + System.out.println(testset.contains("O")); + System.out.println(testset.contains("P")); + System.out.println(testset.isEmpty()); + System.out.println(); + } + + MathSet testset2 = new SeparateChainingHashTable<>(); + + testset2.add("P"); + testset2.add("e"); + testset2.add("e"); + testset2.add("P"); + testset2.add("I"); + testset2.add("E"); + + System.out.println("testset.union(testset2): " + testset.union(testset2)); + System.out.println("testset.intersection(testset2): " + testset.intersection(testset2)); + System.out.println("testset.difference(testset2): " + testset.difference(testset2)); + + + } \ 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..5204273 --- /dev/null +++ b/src/edu/greenriver/sdev333/BSTSet.java @@ -0,0 +1,160 @@ +package edu.greenriver.sdev333; + +import java.util.Iterator; + +public class BSTSet implements MathSet { + 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; + } + } + + private Node root; + + /** + * @param key key to be added into the set + */ + @Override + public void add(Object key) { + root = add(root,key); + + } + + private Node add( Node current, KeyType key) { + if (current == null) { + return new Node(key, 1); + } + + int x = key.compareTo(current.key); + + if (x < 0) { + current.left = add(current.left, key); + } else if (x > 0) { + current.right = add(current.right, key); + } else { + current.key = key; + } + + current.N = size(current.left) + size(current.right) + 1; + + return current; + + } + + /** + * @param key key to check + * @return true if key is in the set, false otherwise + */ + @Override + public boolean contains(Object key) { + Node current = root; + while(current != null){ + int x = key.compareTo(current.key); + if(x < 0){ + current = current.left; + }else if(x > 0){ + current = current.right; + }else { + return true; + } + } + return false; + } + + + /** + * @return true if the set is empty, false otherwise + */ + @Override + public boolean isEmpty() { + return root == null || root.N == 0; + } + + /** + * @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 size(current.left) + size(current.right) + 1; + } + } + + /** + * @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 currentKey : this.keys()){ + result.add(currentKey); + } + + for (KeyType currentKey : other.keys()) { + if (!result.contains(currentKey)) { + result.add(currentKey); + } + } + return result; + } + + /** + * @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 currentKey : this.keys()){ + if(other.contains(currentKey)) { + result.add(currentKey); + } + } + return result; + } + + /** + * @param other specified set to difference + * @return the difference of this set with other + */ + @Override + public MathSet difference(MathSet other) { + MathSet result = new BSTSet(); + + Iterator itr = (Iterator) this.keys(); + + while (itr.hasNext()) { + KeyType currentKey = itr.next(); + if (!other.contains(currentKey)) { + result.add(currentKey); + } + } + + return result; + } + + /** + * @return a collection of all keys in this set + */ + @Override + public Iterable keys() { + Queue queue = new Queue<>(); + inorder(root,queue); + return queue; + } +} \ No newline at end of file