From df3750b4a2ac53f671d8d03fd0b99c56f8b5760a Mon Sep 17 00:00:00 2001 From: Zoe F Date: Wed, 8 Mar 2023 14:16:31 -0800 Subject: [PATCH 1/2] added name to Queue --- src/edu/greenriver/sdev333/Queue.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/edu/greenriver/sdev333/Queue.java b/src/edu/greenriver/sdev333/Queue.java index 638d71c..3227774 100644 --- a/src/edu/greenriver/sdev333/Queue.java +++ b/src/edu/greenriver/sdev333/Queue.java @@ -3,6 +3,7 @@ import java.util.Iterator; /** + * Zoe Fortin * FIFO queue, page 151 of the red book */ public class Queue implements Iterable { From bb6c539050b0d27dc154db6bc88096e3520ce5e1 Mon Sep 17 00:00:00 2001 From: Zoe F Date: Wed, 15 Mar 2023 22:08:55 -0700 Subject: [PATCH 2/2] implemented BSTSet and SeparateChainingHashSet -- put test code in main for both classes --- .idea/vcs.xml | 6 + src/Main.java | 140 +++++++++++- src/edu/greenriver/sdev333/BSTSet.java | 209 ++++++++++++++++++ .../sdev333/SeparateChainingHashSet.java | 162 ++++++++++++++ 4 files changed, 516 insertions(+), 1 deletion(-) create mode 100644 .idea/vcs.xml create mode 100644 src/edu/greenriver/sdev333/BSTSet.java create mode 100644 src/edu/greenriver/sdev333/SeparateChainingHashSet.java 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 3e59c38..d8c37b0 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,143 @@ +import edu.greenriver.sdev333.*; + +import java.sql.SQLOutput; + public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + + // TEST CODE FOR BSTSET // + + //create two sets + //add items to each of the sets (some same. some different) + MathSet bstSet1 = new BSTSet<>(); + bstSet1.add("Ken"); + bstSet1.add("Tina"); + bstSet1.add("Josh"); + bstSet1.add("Tyler"); + bstSet1.add("Monroe"); + bstSet1.add("Snoop"); + + MathSet bstSet2 = new BSTSet<>(); + bstSet2.add("Tina"); + bstSet2.add("Tyler"); + bstSet2.add("Ken"); + bstSet2.add("Kelly"); + bstSet2.add("Zoe"); + + //empty bstSet to test isEmpty() + MathSet emptyBSTSet = new BSTSet<>(); + + System.out.println("BSTSet: "); + + System.out.println("bstSet1 Empty: " + bstSet1.isEmpty()); + System.out.println("bstSet2 Empty: " + bstSet2.isEmpty()); + System.out.println("emptyBSTSet Empty: " + emptyBSTSet.isEmpty()); + System.out.println(""); + + System.out.println("bstSet1 size: " + bstSet1.size()); + System.out.println("bstSet2 size: " + bstSet2.size()); + System.out.println(""); + + //tests + //set intersection + // intersect set 1 and set 2, save into intersResult + MathSet intersResult1 = bstSet1.intersection(bstSet2); + + //print out keys from intersResult + System.out.println("Intersection results: "); + for(String key : intersResult1.keys()) { + System.out.println(key); + } + System.out.println(""); + + //set union + // union set 1 and set 2, save into unionResult + MathSet unionResult1 = bstSet1.union(bstSet2); + + //print out keys from unionResult + System.out.println("Union results: "); + for(String key : unionResult1.keys()) { + System.out.println(key); + } + System.out.println(""); + + //set difference + // union set 1 and set 2, save into diffResult + MathSet diffResult1 = bstSet1.difference(bstSet2); + + //print out keys from diffResult + System.out.println("Difference results: "); + for(String key : diffResult1.keys()) { + System.out.println(key); + } + System.out.println(""); + + // TEST CODE FOR SEPARATECHAININGHASHSET // + + //create two sets + //add items to each of the sets (some same. some different) + MathSet schSet1 = new SeparateChainingHashSet<>(); + schSet1.add("Ken"); + schSet1.add("Tina"); + schSet1.add("Josh"); + schSet1.add("Tyler"); + schSet1.add("Monroe"); + schSet1.add("Snoop"); + + MathSet schSet2 = new SeparateChainingHashSet<>(); + schSet2.add("Tina"); + schSet2.add("Tyler"); + schSet2.add("Ken"); + schSet2.add("Kelly"); + schSet2.add("Zoe"); + + //empty schSet to test isEmpty() + MathSet emptySCHSet = new SeparateChainingHashSet<>(); + + System.out.println("SeparateChainingHashSet: "); + + System.out.println("schSet1 Empty: " + schSet1.isEmpty()); + System.out.println("schSet2 Empty: " + schSet2.isEmpty()); + System.out.println("emptySCHSet Empty: " + emptySCHSet.isEmpty()); + System.out.println(""); + + System.out.println("schSet1 size: " + schSet1.size()); + System.out.println("schSet2 size: " + schSet2.size()); + System.out.println(""); + + //tests + //set intersection + // intersect set 1 and set 2, save into intersResult + MathSet intersResult2 = schSet1.intersection(schSet2); + + //print out keys from intersResult + System.out.println("Intersection results: "); + for(String key : intersResult2.keys()) { + System.out.println(key); + } + System.out.println(""); + + //set union + // union set 1 and set 2, save into unionResult + MathSet unionResult2 = schSet1.union(schSet2); + + //print out keys from unionResult + System.out.println("Union results: "); + for(String key : unionResult2.keys()) { + System.out.println(key); + } + System.out.println(""); + + //set difference + // union set 1 and set 2, save into diffResult + MathSet diffResult2 = schSet1.difference(schSet2); + + //print out keys from diffResult + System.out.println("Difference results: "); + for(String key : diffResult2.keys()) { + System.out.println(key); + } + System.out.println(""); + } } \ 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..9dde129 --- /dev/null +++ b/src/edu/greenriver/sdev333/BSTSet.java @@ -0,0 +1,209 @@ +package edu.greenriver.sdev333; + +import java.util.Iterator; + +public class BSTSet> implements MathSet { + + //node 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); + } + + //helper method + private Node add(Node x, KeyType key) { + if(x==null) { + return new Node(key, 1); + } + int cmp = key.compareTo(x.key); + if(cmp < 0) { + x.left = add(x.left, key); + } else if (cmp > 0) { + x.right = add(x.right, key); + } + 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; + } + + public 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) { + //go left + 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 (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); + } + } + */ + + 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 queue = new Queue<>(); + + inOrder(root, queue); + + return queue; + } + private void inOrder(Node x, Queue queue) { + if (x == null) { + return; + } + inOrder(x.left, queue); + queue.enqueue(x.key); + inOrder(x.right, queue); + } +} diff --git a/src/edu/greenriver/sdev333/SeparateChainingHashSet.java b/src/edu/greenriver/sdev333/SeparateChainingHashSet.java new file mode 100644 index 0000000..d6f32af --- /dev/null +++ b/src/edu/greenriver/sdev333/SeparateChainingHashSet.java @@ -0,0 +1,162 @@ +package edu.greenriver.sdev333; + +import java.security.Key; +import java.util.LinkedList; + +public class SeparateChainingHashSet implements MathSet { + private int size; + private LinkedList[] lists; //array of LinkedLists + + public SeparateChainingHashSet() { + this(997); + } + public SeparateChainingHashSet(int size) { + this.size = size; + lists = new LinkedList[size]; + for (int i = 0; i < size; i++) { + lists[i] = new LinkedList<>(); + } + } + private int hash(KeyType key) { + return (key.hashCode() & 0x7fffffff) % size; + } + /** + * 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 bucket number + + lists[index].add(key); //add key into bucket + } + + /** + * 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 index = hash(key); + + return lists[index].contains(key); + } + + /** + * Is the set empty? + * + * @return true if the set is empty, false otherwise + */ + @Override + public boolean isEmpty() { + //returns using size() method since default constructor puts + //in 997 for size + return this.size() == 0; + } + + /** + * Number of keys in the set + * + * @return number of keys in the set. + */ + @Override + public int size() { + int sum = 0; + for (int i = 0; i < size; i++) { + sum += lists[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 empty set that will hold the result + MathSet result = new SeparateChainingHashSet(); + + for(KeyType currentKey : this.keys()) { + result.add(currentKey); + } + + for(KeyType currentKey : other.keys()) { + if(!result.contains(currentKey)) { + 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 empty set that will hold the result + MathSet result = new SeparateChainingHashSet(); + + //checks other for currentKey & adds to result if other contains currentKey + 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 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 every bucket + for (int i = 0; i < size; i++) { + // take every key in that bucket and add it to the collector + for (KeyType key : lists[i]) { + collector.enqueue(key); + } + } + return collector; + } +}