From 51c01e991ae7117f33ecd9a4316dd378ec4cb64a Mon Sep 17 00:00:00 2001 From: AdamZWinter Date: Wed, 8 Mar 2023 14:16:29 -0800 Subject: [PATCH 1/6] First commit --- src/Main.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index 3e59c38..21321ab 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,9 @@ +/** + * @author Adam Winter + */ public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + + System.out.println("Hello world! "); } } \ No newline at end of file From b93acdd2ff63401a75d546a944558b10d28fbf63 Mon Sep 17 00:00:00 2001 From: AdamZWinter Date: Thu, 9 Mar 2023 10:56:28 -0800 Subject: [PATCH 2/6] All methods for BSTSet written, needs testing --- src/edu/greenriver/sdev333/BSTSet.java | 197 +++++++++++++++++++++++++ 1 file changed, 197 insertions(+) create mode 100644 src/edu/greenriver/sdev333/BSTSet.java diff --git a/src/edu/greenriver/sdev333/BSTSet.java b/src/edu/greenriver/sdev333/BSTSet.java new file mode 100644 index 0000000..07973d1 --- /dev/null +++ b/src/edu/greenriver/sdev333/BSTSet.java @@ -0,0 +1,197 @@ +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 hodl the result + MathSet result = new BSTSet(); + + //iterate through all items in this + Iterator itr = (Iterator) this.keys(); + 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); + } +} From 0d1419d49c6b06e5aea95bc5754d86bd503ca2ff Mon Sep 17 00:00:00 2001 From: AdamZWinter Date: Thu, 9 Mar 2023 12:27:37 -0800 Subject: [PATCH 3/6] BSTSET passing tests --- FinalProject.iml | 58 ++++++++ src/Main.java | 84 +++++++++++- src/edu/greenriver/sdev333/BSTSet.java | 7 +- tests/edu/greenriver/sdev333/BSTSetTest.java | 131 +++++++++++++++++++ 4 files changed, 276 insertions(+), 4 deletions(-) create mode 100644 tests/edu/greenriver/sdev333/BSTSetTest.java 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/Main.java b/src/Main.java index 21321ab..6419879 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,9 +1,91 @@ +import edu.greenriver.sdev333.BSTSet; +import edu.greenriver.sdev333.MathSet; + +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 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<>(); + 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 index 07973d1..45db6b5 100644 --- a/src/edu/greenriver/sdev333/BSTSet.java +++ b/src/edu/greenriver/sdev333/BSTSet.java @@ -51,7 +51,7 @@ private Node put(Node x, KeyType key){ //already exists //else x.key = key; //already exists - //x.n = size(x.left) + size(x.right) + 1; + x.n = size(x.left) + size(x.right) + 1; return x; } @@ -157,11 +157,12 @@ public MathSet intersection(MathSet other) { */ @Override public MathSet difference(MathSet other) { - // create an empty set that will hodl the result + // create an empty set that will hold the result MathSet result = new BSTSet(); //iterate through all items in this - Iterator itr = (Iterator) this.keys(); + Iterable keys = this.keys(); + Iterator itr = keys.iterator(); while (itr.hasNext()){ KeyType currentKey = itr.next(); if(!other.contains(currentKey)){ 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 From 84beefa81ccf445e327b32e8a4a278eb55368380 Mon Sep 17 00:00:00 2001 From: AdamZWinter Date: Thu, 9 Mar 2023 20:06:10 -0800 Subject: [PATCH 4/6] SeparateChainingHashSet implemented and passing tests --- .idea/deployment.xml | 14 ++ .idea/uiDesigner.xml | 124 ++++++++++++ .idea/vcs.xml | 6 + src/Main.java | 26 +++ .../sdev333/SeparateChainingHashSet.java | 180 ++++++++++++++++++ .../greenriver/sdev333/SequentialSearch.java | 55 ++++++ 6 files changed, 405 insertions(+) create mode 100644 .idea/deployment.xml create mode 100644 .idea/uiDesigner.xml create mode 100644 .idea/vcs.xml create mode 100644 src/edu/greenriver/sdev333/SeparateChainingHashSet.java create mode 100644 src/edu/greenriver/sdev333/SequentialSearch.java 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/src/Main.java b/src/Main.java index 6419879..a15922f 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,6 @@ import edu.greenriver.sdev333.BSTSet; import edu.greenriver.sdev333.MathSet; +import edu.greenriver.sdev333.SeparateChainingHashSet; import java.util.Iterator; @@ -11,6 +12,31 @@ public class Main { public static void main(String[] args) { + 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"); diff --git a/src/edu/greenriver/sdev333/SeparateChainingHashSet.java b/src/edu/greenriver/sdev333/SeparateChainingHashSet.java new file mode 100644 index 0000000..c1bf5bf --- /dev/null +++ b/src/edu/greenriver/sdev333/SeparateChainingHashSet.java @@ -0,0 +1,180 @@ +package edu.greenriver.sdev333; + +import java.util.Arrays; +import java.util.Iterator; + +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 + 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 + 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. + * + * @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. + * + * @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; + } +} From c4f8c0ff8243d7072aa35eb4907210534c1193c1 Mon Sep 17 00:00:00 2001 From: AdamZWinter Date: Thu, 9 Mar 2023 20:14:04 -0800 Subject: [PATCH 5/6] SeparateChainingHashSet implemented and passing tests --- src/Main.java | 3 ++- .../sdev333/SeparateChainingHashSet.java | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Main.java b/src/Main.java index a15922f..b3b7611 100644 --- a/src/Main.java +++ b/src/Main.java @@ -95,7 +95,7 @@ public static void main(String[] args) { assertEquals("three", itr3.next()); - MathSet set4 = new BSTSet<>(); + MathSet set4 = new BSTSet<>(); //integrity check set4.add("one"); set4.add("two"); set4.add("three"); @@ -113,5 +113,6 @@ public static void main(String[] args) { + } } \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/SeparateChainingHashSet.java b/src/edu/greenriver/sdev333/SeparateChainingHashSet.java index c1bf5bf..0f84dff 100644 --- a/src/edu/greenriver/sdev333/SeparateChainingHashSet.java +++ b/src/edu/greenriver/sdev333/SeparateChainingHashSet.java @@ -3,6 +3,14 @@ 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; @@ -13,7 +21,7 @@ public SeparateChainingHashSet(int M){ // take their number of buckets, save it into my field this.M = M; - // create the array + // create the array with integrity st = new SequentialSearch[M]; for (int i = 0; i < M; i++){ @@ -26,7 +34,7 @@ public SeparateChainingHashSet(){ } private int hash(KeyType key){ - // has function = they give me a key, I return an int + // has function = they give me a key, I return an int, is what was done in class return ( key.hashCode() & 0x7fffffff ) % M; } @@ -90,6 +98,7 @@ public int size() { * 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 @@ -133,6 +142,7 @@ public MathSet intersection(MathSet other) { * 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 From e680edac6dc775f23da04f18d7d8a5479bad34ac Mon Sep 17 00:00:00 2001 From: AdamZWinter Date: Mon, 13 Mar 2023 14:24:53 -0700 Subject: [PATCH 6/6] HashSet does not implement comparable --- src/FlightRoutesGraph.java | 82 +++++++++++++++++++ .../sdev333/SeparateChainingHashSet.java | 2 +- 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 src/FlightRoutesGraph.java 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/edu/greenriver/sdev333/SeparateChainingHashSet.java b/src/edu/greenriver/sdev333/SeparateChainingHashSet.java index 0f84dff..5a86050 100644 --- a/src/edu/greenriver/sdev333/SeparateChainingHashSet.java +++ b/src/edu/greenriver/sdev333/SeparateChainingHashSet.java @@ -11,7 +11,7 @@ * @param */ -public class SeparateChainingHashSet> implements MathSet { +public class SeparateChainingHashSet implements MathSet { private SequentialSearch[] st;