diff --git a/.idea/deployment.xml b/.idea/deployment.xml
new file mode 100644
index 0000000..d303460
--- /dev/null
+++ b/.idea/deployment.xml
@@ -0,0 +1,14 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
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/FinalProject.iml b/FinalProject.iml
index c90834f..da485a9 100644
--- a/FinalProject.iml
+++ b/FinalProject.iml
@@ -4,8 +4,66 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/FlightRoutesGraph.java b/src/FlightRoutesGraph.java
new file mode 100644
index 0000000..baa9087
--- /dev/null
+++ b/src/FlightRoutesGraph.java
@@ -0,0 +1,82 @@
+import edu.greenriver.sdev333.BSTSet;
+import edu.greenriver.sdev333.MathSet;
+import edu.greenriver.sdev333.SeparateChainingHashSet;
+
+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 , relationships) - routes between airports
+
+ private class Edge{
+ private String node1;
+ private String node2;
+ private Edge(String from, String to){
+ node1 = from;
+ node2 = to;
+ }
+ }
+
+ private MathSet nodes;
+ private MathSet edges;
+
+ public FlightRoutesGraph(){
+ nodes = new BSTSet<>(); // BST ok here because strings are comparable
+ edges = new SeparateChainingHashSet<>(); // must use HashSet here because 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);
+ }
+
+ public MathSet getNeighbors(String city){
+ 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 graph = new FlightRoutesGraph();
+
+ //add all the cities first (nodes)
+ graph.addNode("JFK");
+ graph.addNode("ORD");
+ graph.addNode("ATL");
+ graph.addNode("MCO");
+ graph.addNode("DEN");
+ graph.addNode("LAS");
+ graph.addNode("PHX");
+
+ //add connections between nodes
+ graph.addEdge("JFK", "MCO");
+ graph.addEdge("ATL", "MCO");
+ graph.addEdge("DEN", "ORD");
+ graph.addEdge("ORD", "ATL");
+ graph.addEdge("ORD", "JFK");
+ graph.addEdge("LAS", "DEN");
+ graph.addEdge("PHX", "DEN");
+ graph.addEdge("JFK", "ATL");
+
+ MathSet directFromJFK = graph.getNeighbors("JFK");
+ MathSet directFromATL = graph.getNeighbors("ATL");
+
+ for(String n : directFromJFK.keys()){
+ System.out.println(n);
+ }
+
+ }
+}
diff --git a/src/Main.java b/src/Main.java
index 3e59c38..b3b7611 100644
--- a/src/Main.java
+++ b/src/Main.java
@@ -1,5 +1,118 @@
+import edu.greenriver.sdev333.BSTSet;
+import edu.greenriver.sdev333.MathSet;
+import edu.greenriver.sdev333.SeparateChainingHashSet;
+
+import java.util.Iterator;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+/**
+ * @author Adam Winter
+ */
public class Main {
public static void main(String[] args) {
- System.out.println("Hello world!");
+
+ MathSet hs = new SeparateChainingHashSet<>();
+ hs.add("one");
+ hs.add("two");
+ hs.add("three");
+ hs.add("four");
+ hs.add("five");
+ hs.add("six");
+ hs.add("seven");
+ System.out.println("Set 1: " + hs);
+ MathSet hs2 = new SeparateChainingHashSet<>();
+ hs2.add("five");
+ hs2.add("six");
+ hs2.add("seven");
+ hs2.add("eight");
+ hs2.add("nine");
+ hs2.add("ten");
+ hs2.add("eleven");
+ System.out.println("Set 2: " + hs2);
+ System.out.println("Union: " + hs.union(hs2));
+ System.out.println("1 diff 2: " + hs.difference(hs2));
+ System.out.println("Intersection: " + hs.intersection(hs2));
+
+ //other methods are required to work for the Set methods, tested above, to work
+
+
+ MathSet set = new BSTSet<>();
+ set.add("one");
+ set.add("two");
+ Iterable keys = set.keys();
+ Iterator itr = keys.iterator();
+ assertEquals("one", itr.next()); //o before t
+ assertEquals("two", itr.next());
+
+ set.add("one");
+ set.add("two");
+ set.add("three");
+ set.add("four");
+ assertEquals(true, set.contains("three"));
+ assertEquals(false, set.contains("five"));
+
+
+ assertEquals(false, set.isEmpty());
+
+ set.add("one");
+ set.add("two");
+ set.add("three");
+ set.add("four");
+ assertEquals(4, set.size());
+
+ MathSet other = new BSTSet<>();
+ other.add("five");
+ other.add("six");
+ other.add("seven");
+ other.add("eight");
+ MathSet union = set.union(other);
+ Iterable keys2 = union.keys();
+ Iterator itr2 = keys2.iterator();
+ assertEquals("eight", itr2.next()); // eight five four one seven six three two
+ assertEquals("five", itr2.next());
+ itr2.next();
+ itr2.next();
+ itr2.next();
+ itr2.next();
+ itr2.next();
+ assertEquals("two", itr2.next());
+
+ MathSet set3 = new BSTSet<>();
+ set3.add("one");
+ set3.add("two");
+ set3.add("three");
+ set3.add("four");
+ MathSet other3 = new BSTSet<>();
+ other3.add("three");
+ other3.add("four");
+ other3.add("seven");
+ other3.add("eight");
+ MathSet intersection = set3.intersection(other3);
+ Iterable keys3 = intersection.keys();
+ Iterator itr3 = keys3.iterator();
+ assertEquals("four", itr3.next());
+ assertEquals("three", itr3.next());
+
+
+ MathSet set4 = new BSTSet<>(); //integrity check
+ set4.add("one");
+ set4.add("two");
+ set4.add("three");
+ set4.add("four");
+ MathSet other4 = new BSTSet<>();
+ other4.add("three");
+ other4.add("four");
+ other4.add("seven");
+ other4.add("eight");
+ MathSet diff = set4.difference(other4);
+ Iterable keys4 = diff.keys();
+ Iterator itr4 = keys4.iterator();
+ assertEquals("one", itr4.next());
+ assertEquals("two", itr4.next());
+
+
+
+
}
}
\ 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..45db6b5
--- /dev/null
+++ b/src/edu/greenriver/sdev333/BSTSet.java
@@ -0,0 +1,198 @@
+package edu.greenriver.sdev333;
+
+import java.util.Iterator;
+
+public class BSTSet> implements MathSet{
+
+ private Node root;
+
+ //this does not have value / ValueType. Just the Key. That is the difference from other BST we already did.
+ 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) {
+ put(key);
+ }
+
+ private void put(KeyType key) {
+ root = put(root, key);
+ }
+
+ // helper method for put for recursion
+ private Node put(Node x, KeyType key){
+ // current is the root of the subtree we are looking at
+
+
+ if(x == null){
+ return new Node(key, 1);
+ }
+ int cmp = key.compareTo(x.key);
+ // go left
+ if(cmp < 0) x.left = put(x.left, key);
+
+ //go right
+ else if (cmp > 0) x.right = put(x.right, key);
+
+ //already exists
+ //else x.key = key; //already exists
+ x.n = size(x.left) + size(x.right) + 1;
+ return x;
+ }
+
+ /**
+ * 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;
+ }
+
+ private KeyType get(KeyType key) {
+ return get(root, key);
+ }
+
+ private KeyType get(Node x, KeyType key){
+ if(x == null){return null;}
+ int cmp = key.compareTo(x.key);
+ if(cmp < 0) return get(x.left, key);
+ else if (cmp > 0) return get(x.right, key);
+ else return x.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() {
+ return size(root);
+ }
+
+ private int size(Node x){
+ if(x == null){ return 0;}
+ else{return x.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 will hold the result
+ MathSet result = new BSTSet();
+ for(KeyType currentKey : this.keys()){
+ result.add(currentKey);
+ }
+ 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) {
+ // create an empty set that will hold the result
+ MathSet result = new BSTSet();
+ 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) {
+ // create an empty set that will hold the result
+ MathSet result = new BSTSet();
+
+ //iterate through all items in this
+ Iterable keys = this.keys();
+ Iterator itr = keys.iterator();
+ while (itr.hasNext()){
+ KeyType currentKey = itr.next();
+ 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 queue = new Queue<>();
+ //start the recursion, collecting results in the queue
+ 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/SeparateChainingHashSet.java b/src/edu/greenriver/sdev333/SeparateChainingHashSet.java
new file mode 100644
index 0000000..5a86050
--- /dev/null
+++ b/src/edu/greenriver/sdev333/SeparateChainingHashSet.java
@@ -0,0 +1,190 @@
+package edu.greenriver.sdev333;
+
+import java.util.Arrays;
+import java.util.Iterator;
+
+/** Separate Chaining Hash Set
+ * Uses Sequential Search linked list for side chains
+ * Size of array is hardcoded for constructor without this parameter passed
+ *
+ * @author Adam Winter
+ * @param
+ */
+
+public class SeparateChainingHashSet implements MathSet {
+
+ private SequentialSearch[] st;
+
+ private int M; // M is the number of buckets
+
+ public SeparateChainingHashSet(int M){
+ // take their number of buckets, save it into my field
+ this.M = M;
+
+ // create the array with integrity
+ st = new SequentialSearch[M];
+
+ for (int i = 0; i < M; i++){
+ st[i] = new SequentialSearch<>();
+ }
+ }
+
+ public SeparateChainingHashSet(){
+ this(997);
+ }
+
+ private int hash(KeyType key){
+ // has function = they give me a key, I return an int, is what was done in class
+ 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); //find the bucket number
+ //put the key into that bucket
+ st[index].put(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 hash = hash(key);
+ for (Object other : st[hash].keys()){
+ if(key.equals(other)){
+ 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() {
+ // go through each bucket and add up each individual size
+ int sum = 0;
+ for (int i = 0; i < M; i++){
+ sum += st[i].size();
+ }
+ return sum;
+ }
+
+ /**
+ * 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.
+ * 7a43c5948aa8
+ *
+ * @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 will hold the result
+ MathSet result = new SeparateChainingHashSet<>();
+ for(KeyType currentKey : this.keys()){
+ result.add(currentKey);
+ }
+ 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) {
+ // create an empty set that will hold the result
+ MathSet result = new SeparateChainingHashSet<>();
+ 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.
+ * 7a43c5948aa8
+ *
+ * @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 SeparateChainingHashSet<>();
+ 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 (int i = 0; i < M; i++){
+ for (Object key : st[i].keys()){
+ collector.enqueue((KeyType) key);
+ }
+ }
+ return collector;
+ }
+
+
+ public String toString(){
+ int size = size();
+ Iterable keys = keys();
+ Iterator itr = keys.iterator();
+ String[] stringArray = new String[size];
+ for(int i = 0; i < size; i++){
+ stringArray[i] = itr.next().toString();
+ }
+ return Arrays.toString(stringArray);
+ }
+
+}
diff --git a/src/edu/greenriver/sdev333/SequentialSearch.java b/src/edu/greenriver/sdev333/SequentialSearch.java
new file mode 100644
index 0000000..7513917
--- /dev/null
+++ b/src/edu/greenriver/sdev333/SequentialSearch.java
@@ -0,0 +1,55 @@
+package edu.greenriver.sdev333;
+
+
+import java.util.ArrayList;
+
+/**
+ * Sequential search (unordered linked list implementation)
+ * Refer to p. 374-377 in Sedgewick and Wayne, Algorithms, 4th edition
+ * @param
+ */
+public class SequentialSearch {
+ private Node first;
+ private int size = 0;
+ private class Node{
+ KeyType key;
+ Node next;
+ public Node(KeyType key, Node next){
+ this.key = key;
+ this.next = next;
+ }
+ }
+
+ public void put(KeyType key) {
+ for(Node x = first; x != null; x = x.next){
+ if(key.equals(x.key)){
+ return;
+ }
+ }
+ first = new Node(key, first);
+ size++;
+ }
+
+// @Override
+// public ValueType get(KeyType key) {
+// for(Node x = first; x != null; x = x.next){
+// if(key.equals(x.key)){
+// return x.val;
+// }
+// }
+// return null;
+// }
+
+
+ public int size() {
+ return size;
+ }
+
+ public Iterable keys() {
+ ArrayList keyList = new ArrayList<>();
+ for(Node x = first; x != null; x = x.next){
+ keyList.add(x.key);
+ }
+ return keyList;
+ }
+}
diff --git a/tests/edu/greenriver/sdev333/BSTSetTest.java b/tests/edu/greenriver/sdev333/BSTSetTest.java
new file mode 100644
index 0000000..694cd6b
--- /dev/null
+++ b/tests/edu/greenriver/sdev333/BSTSetTest.java
@@ -0,0 +1,131 @@
+package edu.greenriver.sdev333;
+
+import org.junit.jupiter.params.shadow.com.univocity.parsers.common.IterableResult;
+
+import java.util.Iterator;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+class BSTSetTest {
+
+ @org.junit.jupiter.api.Test
+ void add() {
+ MathSet set = new BSTSet<>();
+ set.add("one");
+ set.add("two");
+ Iterable keys = set.keys();
+ Iterator itr = keys.iterator();
+ assertEquals("one", itr.next()); //o before t
+ assertEquals("two", itr.next());
+ }
+
+ @org.junit.jupiter.api.Test
+ void contains() {
+ MathSet set = new BSTSet<>();
+ set.add("one");
+ set.add("two");
+ set.add("three");
+ set.add("four");
+ assertEquals(true, set.contains("three"));
+ assertEquals(false, set.contains("five"));
+ }
+
+ @org.junit.jupiter.api.Test
+ void isEmpty() {
+ MathSet set = new BSTSet<>();
+ assertEquals(true, set.isEmpty());
+ set.add("one");
+ set.add("two");
+ set.add("three");
+ set.add("four");
+ assertEquals(false, set.isEmpty());
+ }
+
+ @org.junit.jupiter.api.Test
+ void size() {
+ MathSet set = new BSTSet<>();
+ assertEquals(0, set.size());
+ set.add("one");
+ set.add("two");
+ set.add("three");
+ set.add("four");
+ assertEquals(4, set.size());
+ set.add("one");
+ set.add("two");
+ set.add("three");
+ set.add("four");
+ assertEquals(4, set.size());
+ set.add("five");
+ set.add("six");
+ set.add("seven");
+ set.add("eight");
+ assertEquals(8, set.size());
+ }
+
+ @org.junit.jupiter.api.Test
+ void union() {
+ MathSet set = new BSTSet<>();
+ set.add("one");
+ set.add("two");
+ set.add("three");
+ set.add("four");
+ MathSet other = new BSTSet<>();
+ other.add("five");
+ other.add("six");
+ other.add("seven");
+ other.add("eight");
+ MathSet union = set.union(other);
+ Iterable keys = union.keys();
+ Iterator itr = keys.iterator();
+ assertEquals("eight", itr.next()); // eight five four one seven six three two
+ assertEquals("five", itr.next());
+ itr.next();
+ itr.next();
+ itr.next();
+ itr.next();
+ itr.next();
+ assertEquals("two", itr.next());
+ }
+
+ @org.junit.jupiter.api.Test
+ void intersection() {
+ MathSet set = new BSTSet<>();
+ set.add("one");
+ set.add("two");
+ set.add("three");
+ set.add("four");
+ MathSet other = new BSTSet<>();
+ other.add("three");
+ other.add("four");
+ other.add("seven");
+ other.add("eight");
+ MathSet intersection = set.intersection(other);
+ Iterable keys = intersection.keys();
+ Iterator itr = keys.iterator();
+ assertEquals("four", itr.next());
+ assertEquals("three", itr.next());
+ }
+
+ @org.junit.jupiter.api.Test
+ void difference() {
+ MathSet set = new BSTSet<>();
+ set.add("one");
+ set.add("two");
+ set.add("three");
+ set.add("four");
+ MathSet other = new BSTSet<>();
+ other.add("three");
+ other.add("four");
+ other.add("seven");
+ other.add("eight");
+ MathSet diff = set.difference(other);
+ Iterable keys = diff.keys();
+ Iterator itr = keys.iterator();
+ assertEquals("one", itr.next());
+ assertEquals("two", itr.next());
+ }
+
+ @org.junit.jupiter.api.Test
+ void keys() {
+ }
+}
\ No newline at end of file