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/out/production/FinalProject/Main.class b/out/production/FinalProject/Main.class new file mode 100644 index 0000000..e2ac606 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..dfd2ae0 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..e539f75 Binary files /dev/null and b/out/production/FinalProject/edu/greenriver/sdev333/BSTset.class differ diff --git a/out/production/FinalProject/edu/greenriver/sdev333/FlightRoutesGraph$Edge.class b/out/production/FinalProject/edu/greenriver/sdev333/FlightRoutesGraph$Edge.class new file mode 100644 index 0000000..f1a199e Binary files /dev/null and b/out/production/FinalProject/edu/greenriver/sdev333/FlightRoutesGraph$Edge.class differ diff --git a/out/production/FinalProject/edu/greenriver/sdev333/FlightRoutesGraph.class b/out/production/FinalProject/edu/greenriver/sdev333/FlightRoutesGraph.class new file mode 100644 index 0000000..79a37fe Binary files /dev/null and b/out/production/FinalProject/edu/greenriver/sdev333/FlightRoutesGraph.class differ diff --git a/out/production/FinalProject/edu/greenriver/sdev333/HashSet.class b/out/production/FinalProject/edu/greenriver/sdev333/HashSet.class new file mode 100644 index 0000000..e412039 Binary files /dev/null and b/out/production/FinalProject/edu/greenriver/sdev333/HashSet.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/out/production/FinalProject/edu/greenriver/sdev333/SeparateChainingHashTable.class b/out/production/FinalProject/edu/greenriver/sdev333/SeparateChainingHashTable.class new file mode 100644 index 0000000..4bb5fbd Binary files /dev/null and b/out/production/FinalProject/edu/greenriver/sdev333/SeparateChainingHashTable.class differ diff --git a/out/production/FinalProject/edu/greenriver/sdev333/SequentialSearchST$Node.class b/out/production/FinalProject/edu/greenriver/sdev333/SequentialSearchST$Node.class new file mode 100644 index 0000000..e3a976a Binary files /dev/null and b/out/production/FinalProject/edu/greenriver/sdev333/SequentialSearchST$Node.class differ diff --git a/out/production/FinalProject/edu/greenriver/sdev333/SequentialSearchST.class b/out/production/FinalProject/edu/greenriver/sdev333/SequentialSearchST.class new file mode 100644 index 0000000..4d0880d Binary files /dev/null and b/out/production/FinalProject/edu/greenriver/sdev333/SequentialSearchST.class differ diff --git a/src/Main.java b/src/Main.java index 3e59c38..9f272cb 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,112 @@ +import java.util.Scanner; + +import edu.greenriver.sdev333.MathSet; +import edu.greenriver.sdev333.SeparateChainingHashTables; +import edu.greenriver.sdev333.BSTset; + + public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + //System.out.println("Hello world!"); + //string created to put in set + String testString = "I L O V E K U M A"; //9 + + MathSet testOne = new SeparateChainingHashTables<>(); + + + //scanner to add string to BSTSet + Scanner input = new Scanner(testString); + + //testing isEmpty (true/false) + // System.out.println(testOne.isEmpty()); + + //loop to add the strings as keys to my testOne + while(input.hasNext()){ + String key = input.next(); + testOne.add(key); + } + + //Test for MathSet + MathSet testTwo = new SeparateChainingHashTables<>(); + testTwo.add("I"); + testTwo.add("t"); + testTwo.add("i"); + testTwo.add("S"); + testTwo.add("W"); + testTwo.add("h"); + testTwo.add("a"); + testTwo.add("T"); + testTwo.add(("i")); + testTwo.add(("t")); + testTwo.add(("i")); + testTwo.add(("s")); + + + //testing all methods in mathSet: return int + System.out.println(testOne.size()); + + //returns boolean + System.out.println(testOne.contains("B")); + System.out.println(testOne.contains("R")); + System.out.println(testOne.contains("o")); + // +// System.out.println(testOne.isEmpty()); +// System.out.println(); +// + System.out.println("__________________________________"); +// +// System.out.println(); +// // and again on testTwo + System.out.println(testTwo.size()); + System.out.println(testTwo.contains("A")); + System.out.println(testTwo.contains("K")); + System.out.println(testTwo.contains("a")); + System.out.println(testTwo.contains("T")); + System.out.println(testTwo.contains("z")); + + System.out.println("__________________________________"); +// +// System.out.println(); +// // this is the testOne for union/intersection/different methods + MathSet unionTester = testOne.union(testTwo); + System.out.println("union: testOne and testTwo"); + for (String element: unionTester.keys()){ + System.out.println(element); + } + System.out.println("__________________________________"); + System.out.println("intersection: testOne and testTwo"); + MathSet intersectionTester = testOne.intersection((testTwo)); + for (String element: intersectionTester.keys()){ + System.out.println(element); + } + System.out.println("__________________________________"); + + System.out.println("difference: testOne and testTwo"); + MathSet differenceTester = testOne.difference((testTwo)); + for (String element: differenceTester.keys()){ + System.out.println(element); + } } -} \ No newline at end of file +} +//IN CLASS +// +//// +//// //Create 2 sets +//// MathSet set1 = new BSTset<>(); +//// // add items to each of the sets (some same, some different) +//// set1.add("Ken"); +//// set1.add("Tina"); +//// +// MathSet set2 = new BSTset<>(); +// +// +// MathSet result1 = set1.union(set2); +// //test +// for (String key : result1.keys()) { +// System.out.println(key); +// } + + //set intersection + //set union + //set difference + diff --git a/src/edu/greenriver/sdev333/BSTset.java b/src/edu/greenriver/sdev333/BSTset.java new file mode 100644 index 0000000..d43bc6e --- /dev/null +++ b/src/edu/greenriver/sdev333/BSTset.java @@ -0,0 +1,208 @@ +package edu.greenriver.sdev333; + +public class BSTset> implements MathSet { + //fields + private Node root; + + //Node helper class + private class Node{ + private KeyType key; + private Node left; + private Node right; + private int N; // number of nodes in the subtree rooted here + + 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){ + // current is the root of the subtree wer are looking at + + //we are at where we are supposed to be + if (current == null){ + //create new node + return new Node(key, 1); + } + + int cmp = key.compareTo(current.key); + // left + if (cmp < 0){ + current.left = add(current.left, key); + } + //right + else if (cmp > 0){ + current.right = add(current.right, key); + } + 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) { + Node current = root; + + while (current != null) { + int cmp = key.compareTo(current.key); + + if (cmp == 0) { + return true; + } else if (cmp < 0) { + current = current.left; + } else { + current = current.right; + } + } + + return false; //no matching in tree + } + + /** + * Is the set empty? + * + * @return true if the set is empty, false otherwise + */ + @Override + public boolean isEmpty() { + return root == null; + } + + /** + * 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) { + // Create an empty set that hold the result + MathSet result = new BSTset(); + + //add all element from this set + for (KeyType currentKey : this.keys()){ + result.add(currentKey); + } + //add all element from the other set + for (KeyType currentKey : other.keys()){ + result.add(currentKey); + } + 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 currentKey : this.keys()){ + // if the key contains in the other set, add to result set + if (other.contains(currentKey)){ + result.add(currentKey); + } + } + 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 an empty set that hold the result + MathSet result = new BSTset(); + for (KeyType currentKey : this.keys()){ + if (!other.contains(currentKey)){ + result.add(currentKey); + } + } + return result; + } + // 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); +// } +// } + + /** + * Retrieves a collection of all the keys in this set. + * + * @return a collection of all keys in this set + */ + @Override + public Iterable keys() { + //create a new empty queue to hold my results + Queue queue = new Queue<>(); + + //start recursion, collecting results in the queue + inorder(root, queue); + return queue; + } + + private void inorder(Node current, Queue q){ + if (current == null){ + //do nothing - intentional + return; + } + + inorder(current.left, q); + q.enqueue(current.key); + inorder(current.right, q); + } +} \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/FlightRoutesGraph.java b/src/edu/greenriver/sdev333/FlightRoutesGraph.java new file mode 100644 index 0000000..51fed91 --- /dev/null +++ b/src/edu/greenriver/sdev333/FlightRoutesGraph.java @@ -0,0 +1,108 @@ +package edu.greenriver.sdev333; + +/** + * This class represents a basic graph of airports connected + * by flight paths. + * It is an introductory class which only contains a way to build + * the graph and find the nodes directly connected. We will be + * going into greater depth (or breadth, lol) on this concept next quarter + */ +public class FlightRoutesGraph { + //two sets need to model a graph (network) + //1. a set of vertices (points, nodes) --airports + //2. a set of edges (connections, lines, routes, relationships)--route between airports + + //helper class + private class Edge{ + private String node1; + private String node2; + + public Edge(String from, String to){ + node1 = from; + node2 = to; + } + } + //airports + private MathSet nodes; + //fight paths + private MathSet edges; + + /** + * default constructor + */ + public FlightRoutesGraph(){ + nodes = new BSTset<>(); //BST okay because strings are comparable + edges = (MathSet) new HashSet();//must use hash here because edges are not comparable + } + + /** + * add a node + * @param city airport node + */ + public void addNode(String city){ + nodes.add(city); + } + + /** + * add a route from one city to another + * @param city1 one of the connecting cities + * @param city2 the other connecting city + */ + public void addEdge(String city1, String city2){ + Edge connection = new Edge(city1, city2); + edges.add(connection); + } + + /** + * find all cities that are connected to given city via an edge (flight path) + * @param city airport to check + * @return airports directly connected to airport to check + */ + public MathSet getNeighbors(String city){ + //create an empty set to hold the results + MathSet neighbors = new BSTset<>(); + //loop through edges and check + //is city 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; + } + + //explore graph with BFS--breadth first search + //or with DFS--depth first search (use recursion or stack) + //shortest path algorithm + + /** + * Tester method (to keep Main class clean for testing BSTSet and HashSet only) + * @param args + */ + public static void main(String[] args){ + FlightRoutesGraph g = new FlightRoutesGraph(); + + //add all cities first + g.addNode("JFK"); + g.addNode("ORD"); + g.addNode("ATL"); + g.addNode("MCO"); + g.addNode("SEA"); + g.addNode("DEN"); + + //add connections between cities + g.addEdge("JFK","MCO"); + g.addEdge("ATL","MCO"); + g.addEdge("DEN","ORD"); + g.addEdge("ORD","ATL"); + g.addEdge("SEA","DEN"); + + //look for direct flights from MCO + MathSet directFromMCO = g.getNeighbors("MCO"); + MathSet directFromATL = g.getNeighbors(("ATL")); + System.out.println("Direct flights from MCO: " + directFromMCO); + System.out.println("Direct flights from ATL: " + directFromATL); + } +} \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/HashSet.java b/src/edu/greenriver/sdev333/HashSet.java new file mode 100644 index 0000000..e0479d4 --- /dev/null +++ b/src/edu/greenriver/sdev333/HashSet.java @@ -0,0 +1,11 @@ +package edu.greenriver.sdev333; + +public class HashSet { + //Used SeparateChainingHashST as model/example + //^^^ code manage the "buckets" of hash table + + //SeparateChainingHashST depends on having SequentialSearchST + //^^^ code manage the insides of the buckets of the has table + // a linked list is inside each bucket + //will need to either bring in SequentialSearchST or write your own linked list +} diff --git a/src/edu/greenriver/sdev333/SeparatingChainHashTables.java b/src/edu/greenriver/sdev333/SeparatingChainHashTables.java new file mode 100644 index 0000000..c8caa59 --- /dev/null +++ b/src/edu/greenriver/sdev333/SeparatingChainHashTables.java @@ -0,0 +1,189 @@ +package edu.greenriver.sdev333; +/** + * This class is to implement the MathSet interface. The + * SequentialSearchST class (created in-class with instructor + * Ken Hang) is used in this implementation. + */ +public class SeparateChainingHashTables implements MathSet { + // fields: + // array of linked lists - but we wrote a linked list in SequentialSearchSt + private SequentialSearchST[] st; + private int M; // M is the number of buckets + + // default constructor that calls our parameterized constructor + public SeparateChainingHashTables(){ + // default constructor + // calls the other constructor with a value + this(997); + } + + // constructor + public SeparateChainingHashTables(int M) { + // take their number of buckets and save them into the field + this.M = M; + + // create the array + st = new SequentialSearchST[M]; + + // for each position in the array, create a linked list (SequentialSearchSt) + for (int i = 0; i < M; i++) { + st[i] = new SequentialSearchST<>(); + } + } + + + /** + * Puts the specified key into the set. + * + * @param key key to be added into the set + */ + @Override + public void add(KeyType key) { + // use hash function to determine which index to add to + int indexToAddTo = hash(key); + // add here + st[indexToAddTo].put(key); + } + + // private helper method created in class to return an int + private int hash(KeyType key){ + // hash function = give me a key, I return an int (bucket#, array index) + return (key.hashCode() & 0x7fffffff ) % M; + } + + /** + * 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; + } + + // use method created in class (SequentialSearchST) + // return null if not found + public KeyType get(KeyType key) { + int index = hash(key); // find the bucket number + // go into the bucket and see if it's there + return st[index].get(key); + } + + /** + * 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() { + // iterate through each "bucket"'s linkedList + // for each of these lists, call the size method used in + // SequentialSearchST ( <- every time we "put" a key - size is incremented ) + int size = 0; + for (int i = 0; i < M; i++) { + size += st[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 result = new SeparateChainingHashTables(M); + + // add all current keys to resultSet + for (KeyType currentKey: this.keys()) { + result.add(currentKey); + } + // add all "other" keys to resultSet + for(KeyType currentKey : other.keys()){ + result.add(currentKey); + } + 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 SeparateChainingHashTables(M); + + // if our current key is also in our "other" keys, add to resultSet + for (KeyType currentKey: this.keys()) { + if(other.contains(currentKey)){ + result.add(currentKey); + } + } + 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) { + + MathSet result = new SeparateChainingHashTables(M); + + // if our current key is not in our "other" keys, add to resultSet + 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() { + Queue collector = new Queue<>(); + // for every bucket + for (int i = 0; i < M; i++) { + // take each key in that bucket and add it into the collector + for (KeyType key : st[i].keys()) { + collector.enqueue(key); + } + } + return collector; + } +} \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/SequentialSearchST.java b/src/edu/greenriver/sdev333/SequentialSearchST.java new file mode 100644 index 0000000..e0e48c5 --- /dev/null +++ b/src/edu/greenriver/sdev333/SequentialSearchST.java @@ -0,0 +1,87 @@ +package edu.greenriver.sdev333; + +import java.util.Iterator; + +/** + * Sequential search (unordered linked list implementation) of Symbol Table + * Refer to p. 374-377 in Sedgewick and Wayne, Algorithms, 4th edition + * + * Note: size method returns number of elements, but there is currently no method + * to remove elements, only add via put(). + * + * @param +> + */ +public class SequentialSearchST { + + private Node first; + private int size; + + private class Node { + KeyType key; + Node next; + public Node(KeyType key, Node next) { + this.key = key; + this.next = next; + } + } + + /** + * Puts the key/value into the tree + * @param key + */ + + public void put(KeyType key) { + // cycle through linked list looking for value + for (Node x = first; x != null; x = x.next) { + if (key.equals(x.key)) { + return; + } + } + + // value not found in list, create a new node and assign value + first = new Node(key, first); + ++size; + } + + /** + * Gets the given key/value from the tree + * @param key + * @return + */ + public KeyType get(KeyType key) { + // cycle through linked list looking for matching key + for (Node x = first; x != null; x = x.next) { + if (key.equals(x.key)) { + return x.key; + } + } + + // no matching key found, return null + return null; + } + /** + * returns the size of the tree as an int value + * @return + */ + + public int size() { + return size; + } + + /** + * Returns an iterable list of keys for this list + * @return + */ + public Iterable keys() { + Queue queue = new Queue<>(); + + Node current = first; + while (current != null) { + queue.enqueue(current.key); + current = current.next; + } + + return queue; + } +} \ No newline at end of file