From 1b9be91851f463e388ca3e1ab47808d8450aa895 Mon Sep 17 00:00:00 2001 From: Jrome Date: Wed, 8 Mar 2023 14:17:18 -0800 Subject: [PATCH 1/5] first commit to test --- src/edu/greenriver/sdev333/MathSet.java | 6 ++++++ 1 file changed, 6 insertions(+) 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. From 1c9998e3d3c22bcd56a2690617f6372473d296ea Mon Sep 17 00:00:00 2001 From: Jrome Date: Wed, 15 Mar 2023 21:02:53 -0700 Subject: [PATCH 2/5] hashmathset --- .idea/vcs.xml | 6 + src/Main.java | 36 +++- src/edu/greenriver/sdev333/BSTSet.java | 179 ++++++++++++++++++++ src/edu/greenriver/sdev333/HashMathSet.java | 70 ++++++++ 4 files changed, 290 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/HashMathSet.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..b1ddc8e 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,39 @@ +import edu.greenriver.sdev333.BSTSet; +import edu.greenriver.sdev333.MathSet; + 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")); } -} \ 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..92c764c --- /dev/null +++ b/src/edu/greenriver/sdev333/BSTSet.java @@ -0,0 +1,179 @@ +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 (KeyType key : keys()) { + result.add(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 + */ + @Override + public MathSet intersection(MathSet other) { + return null; + } + + /** + * 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/HashMathSet.java b/src/edu/greenriver/sdev333/HashMathSet.java new file mode 100644 index 0000000..ad88cab --- /dev/null +++ b/src/edu/greenriver/sdev333/HashMathSet.java @@ -0,0 +1,70 @@ +package edu.greenriver.sdev333; + +public class HashMathSet implements MathSet { + + private SeparateChainingHashST hashTable; + + public HashMathSet() { + hashTable = new SeparateChainingHashST(); + } + + @Override + public void add(KeyType key) { + hashTable.put(key, true); + } + + @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 HashMathSet(); + 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 HashMathSet(); + for (KeyType key : keys()) { + if (other.contains(key)) { + result.add(key); + } + } + return result; + } + + @Override + public MathSet difference(MathSet other) { + MathSet result = new HashMathSet(); + for (KeyType key : keys()) { + if (!other.contains(key)) { + result.add(key); + } + } + return result; + } + + @Override + public Iterable keys() { + return hashTable.keys(); + } +} + From 8f4252474871e2eba25a49f36677145c77e51065 Mon Sep 17 00:00:00 2001 From: Jrome Date: Thu, 16 Mar 2023 16:13:48 -0700 Subject: [PATCH 3/5] updated final --- src/Main.java | 2 ++ src/edu/greenriver/sdev333/BSTSet.java | 12 +++++++++--- ...{HashMathSet.java => SeparateChainingHashST.java} | 10 +++++----- 3 files changed, 16 insertions(+), 8 deletions(-) rename src/edu/greenriver/sdev333/{HashMathSet.java => SeparateChainingHashST.java} (80%) diff --git a/src/Main.java b/src/Main.java index b1ddc8e..9f2eaf6 100644 --- a/src/Main.java +++ b/src/Main.java @@ -36,4 +36,6 @@ public static void main(String[] args) { assert(!result3.contains("dog")); assert(!result3.contains("mouse")); } + + } diff --git a/src/edu/greenriver/sdev333/BSTSet.java b/src/edu/greenriver/sdev333/BSTSet.java index 92c764c..ff5cada 100644 --- a/src/edu/greenriver/sdev333/BSTSet.java +++ b/src/edu/greenriver/sdev333/BSTSet.java @@ -125,9 +125,15 @@ public MathSet union(MathSet other) { * @param other specified set to intersect * @return the intersection of this set with other */ - @Override - public MathSet intersection(MathSet other) { - return null; + + public MathSet intersection(MathSet other) { + MathSet result = new BSTSet<>(); + for (KeyType key : this.keys()) { + if (other.contains(key)) { + result.add(key); + } + } + return result; } /** diff --git a/src/edu/greenriver/sdev333/HashMathSet.java b/src/edu/greenriver/sdev333/SeparateChainingHashST.java similarity index 80% rename from src/edu/greenriver/sdev333/HashMathSet.java rename to src/edu/greenriver/sdev333/SeparateChainingHashST.java index ad88cab..7018585 100644 --- a/src/edu/greenriver/sdev333/HashMathSet.java +++ b/src/edu/greenriver/sdev333/SeparateChainingHashST.java @@ -1,10 +1,10 @@ package edu.greenriver.sdev333; -public class HashMathSet implements MathSet { +public class SeparateChainingHashST implements MathSet { private SeparateChainingHashST hashTable; - public HashMathSet() { + public SeparateChainingHashST() { hashTable = new SeparateChainingHashST(); } @@ -30,7 +30,7 @@ public int size() { @Override public MathSet union(MathSet other) { - MathSet result = new HashMathSet(); + MathSet result = new SeparateChainingHashST(); for (KeyType key : keys()) { result.add(key); } @@ -42,7 +42,7 @@ public MathSet union(MathSet other) { @Override public MathSet intersection(MathSet other) { - MathSet result = new HashMathSet(); + MathSet result = new SeparateChainingHashST(); for (KeyType key : keys()) { if (other.contains(key)) { result.add(key); @@ -53,7 +53,7 @@ public MathSet intersection(MathSet other) { @Override public MathSet difference(MathSet other) { - MathSet result = new HashMathSet(); + MathSet result = new SeparateChainingHashST(); for (KeyType key : keys()) { if (!other.contains(key)) { result.add(key); From 6bf31774d3256c05232fabd80fe24a0b81bed9bb Mon Sep 17 00:00:00 2001 From: Jrome Date: Mon, 20 Mar 2023 14:05:22 -0700 Subject: [PATCH 4/5] updated final --- .idea/uiDesigner.xml | 124 ++++++++++++++++++ src/edu/greenriver/sdev333/BSTSet.java | 10 +- .../sdev333/SeparateChainingHashST.java | 9 +- 3 files changed, 135 insertions(+), 8 deletions(-) create mode 100644 .idea/uiDesigner.xml 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/src/edu/greenriver/sdev333/BSTSet.java b/src/edu/greenriver/sdev333/BSTSet.java index ff5cada..937c829 100644 --- a/src/edu/greenriver/sdev333/BSTSet.java +++ b/src/edu/greenriver/sdev333/BSTSet.java @@ -104,8 +104,8 @@ private int size(Node x) { @Override public MathSet union(MathSet other) { MathSet result = new BSTSet(); - for (KeyType key : keys()) { - result.add(key); + for (Object key : keys()) { + result.add((KeyType) key); } for (KeyType key : other.keys()) { if (!contains(key)) { @@ -128,9 +128,9 @@ public MathSet union(MathSet other) { public MathSet intersection(MathSet other) { MathSet result = new BSTSet<>(); - for (KeyType key : this.keys()) { - if (other.contains(key)) { - result.add(key); + for (Object key : this.keys()) { + if (other.contains((KeyType) key)) { + result.add((KeyType) key); } } return result; diff --git a/src/edu/greenriver/sdev333/SeparateChainingHashST.java b/src/edu/greenriver/sdev333/SeparateChainingHashST.java index 7018585..0bbe9ec 100644 --- a/src/edu/greenriver/sdev333/SeparateChainingHashST.java +++ b/src/edu/greenriver/sdev333/SeparateChainingHashST.java @@ -2,15 +2,18 @@ public class SeparateChainingHashST implements MathSet { - private SeparateChainingHashST hashTable; + private SeparateChainingHashST hashTable; public SeparateChainingHashST() { - hashTable = new SeparateChainingHashST(); + hashTable = new SeparateChainingHashST(); } @Override public void add(KeyType key) { - hashTable.put(key, true); + hashTable.put(key); + } + + private void put(KeyType key) { } @Override From 57b20a82b661e21c814c305be43e32110f15392a Mon Sep 17 00:00:00 2001 From: Jrome Date: Mon, 20 Mar 2023 20:40:11 -0700 Subject: [PATCH 5/5] tried testing seperate chaining hashst --- src/Main.java | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/Main.java b/src/Main.java index 9f2eaf6..8aaba4d 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.SeparateChainingHashST; public class Main { public static void main(String[] args) { @@ -35,7 +36,44 @@ public static void main(String[] args) { 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)); + } + } + + }