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 3e59c38..8aaba4d 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,79 @@ +import edu.greenriver.sdev333.BSTSet; +import edu.greenriver.sdev333.MathSet; +import edu.greenriver.sdev333.SeparateChainingHashST; + public class Main { public static void main(String[] args) { System.out.println("Hello world!"); + + // Test add method + MathSet set1 = new BSTSet<>(); + set1.add("cat"); + set1.add("dog"); + assert(set1.contains("cat")); + assert(set1.contains("dog")); + + MathSet set2 = new BSTSet<>(); + set2.add("dog"); + set2.add("mouse"); + assert(set2.contains("dog")); + assert(set2.contains("mouse")); + + // Test union method + MathSet result1 = set1.union(set2); + assert(result1.contains("cat")); + assert(result1.contains("dog")); + assert(result1.contains("mouse")); + + // Test intersection method + MathSet result2 = set1.intersection(set2); + assert(result2.contains("dog")); + assert(!result2.contains("cat")); + assert(!result2.contains("mouse")); + + // Test difference method + MathSet result3 = set1.difference(set2); + assert(result3.contains("cat")); + assert(!result3.contains("dog")); + assert(!result3.contains("mouse")); + + //test number 2 + + SeparateChainingHashST st1 = new SeparateChainingHashST<>(); + SeparateChainingHashST st2 = new SeparateChainingHashST<>(); + + st1.put("a", 1); + st1.put("b", 2); + st1.put("c", 3); + + st2.put("c", 4); + st2.put("d", 5); + st2.put("e", 6); + + // Testing union + SeparateChainingHashST union = st1.union(st2); + System.out.println("Union:"); + for (String key : union.keys()) { + System.out.println(key + ": " + union.get(key)); + } + + // Testing intersection + SeparateChainingHashST intersection = st1.intersection(st2); + System.out.println("Intersection:"); + for (String key : intersection.keys()) { + System.out.println(key + ": " + intersection.get(key)); + } + + // Testing difference + SeparateChainingHashST difference = st1.difference(st2); + System.out.println("Difference:"); + for (String key : difference.keys()) { + System.out.println(key + ": " + difference.get(key)); + } + } -} \ 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..937c829 --- /dev/null +++ b/src/edu/greenriver/sdev333/BSTSet.java @@ -0,0 +1,185 @@ +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; + + //field + public Node(KeyType key, int N) { + this.key = key; + this.N = N; + } + } + + private Node root; // root of BST + + /** + * 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); + } + + /** + * Adds key to subtree rooted at x; returns subtree with key added + */ + 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); + else x.key = key; + x.N = 1 + size(x.left) + size(x.right); + 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 contains(root, key); + } + + /** + * Does the subtree rooted at x contain key? + */ + private boolean contains(Node x, KeyType key) { + if (x == null) return false; + int cmp = key.compareTo(x.key); + if (cmp < 0) return contains(x.left, key); + else if (cmp > 0) return contains(x.right, key); + else return true; + } + + /** + * 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); + } + + /** + * Number of nodes in subtree rooted at x; 0 if x is null + */ + 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) { + MathSet result = new BSTSet(); + for (Object key : keys()) { + result.add((KeyType) key); + } + for (KeyType key : other.keys()) { + if (!contains(key)) { + result.add(key); + } + } + return result; + } + + /** + * Determine the intersection of this set with another specified set. + * Returns A intersect B, + + * 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 + */ + + public MathSet intersection(MathSet other) { + MathSet result = new BSTSet<>(); + for (Object key : this.keys()) { + if (other.contains((KeyType) key)) { + result.add((KeyType) key); + } + } + 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 set + Iterator itr = (Iterator) this.keys(); + while (itr.hasNext()) { + KeyType currentKey = itr.next(); + if (!other.contains(currentKey)) { + result.add(currentKey); + } + } + return result; + } + //using for each loop + /* + public MathSet difference(MathSet other) { + MathSet result = new BSTSet(); + 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() { + return null; + } +} diff --git a/src/edu/greenriver/sdev333/MathSet.java b/src/edu/greenriver/sdev333/MathSet.java index 4273aba..2b3c231 100644 --- a/src/edu/greenriver/sdev333/MathSet.java +++ b/src/edu/greenriver/sdev333/MathSet.java @@ -7,6 +7,12 @@ * Set complement and element (key) deletion is not supported by this API. * @param */ + +/** + * Jerome Shadkam + *3/8/23 + */ + public interface MathSet { /** * Puts the specified key into the set. diff --git a/src/edu/greenriver/sdev333/SeparateChainingHashST.java b/src/edu/greenriver/sdev333/SeparateChainingHashST.java new file mode 100644 index 0000000..0bbe9ec --- /dev/null +++ b/src/edu/greenriver/sdev333/SeparateChainingHashST.java @@ -0,0 +1,73 @@ +package edu.greenriver.sdev333; + +public class SeparateChainingHashST implements MathSet { + + private SeparateChainingHashST hashTable; + + public SeparateChainingHashST() { + hashTable = new SeparateChainingHashST(); + } + + @Override + public void add(KeyType key) { + hashTable.put(key); + } + + private void put(KeyType key) { + } + + @Override + public boolean contains(KeyType key) { + return hashTable.contains(key); + } + + @Override + public boolean isEmpty() { + return hashTable.isEmpty(); + } + + @Override + public int size() { + return hashTable.size(); + } + + @Override + public MathSet union(MathSet other) { + MathSet result = new SeparateChainingHashST(); + for (KeyType key : keys()) { + result.add(key); + } + for (KeyType key : other.keys()) { + result.add(key); + } + return result; + } + + @Override + public MathSet intersection(MathSet other) { + MathSet result = new SeparateChainingHashST(); + for (KeyType key : keys()) { + if (other.contains(key)) { + result.add(key); + } + } + return result; + } + + @Override + public MathSet difference(MathSet other) { + MathSet result = new SeparateChainingHashST(); + for (KeyType key : keys()) { + if (!other.contains(key)) { + result.add(key); + } + } + return result; + } + + @Override + public Iterable keys() { + return hashTable.keys(); + } +} +