From 40ab0c340594cb0b47026691859031bea08a2da7 Mon Sep 17 00:00:00 2001 From: adamwise Date: Wed, 8 Mar 2023 14:16:29 -0800 Subject: [PATCH 1/3] added BSTSet class --- .idea/vcs.xml | 6 ++ src/edu/greenriver/sdev333/BSTSet.java | 113 ++++++++++++++++++++++++ src/edu/greenriver/sdev333/MathSet.java | 2 + 3 files changed, 121 insertions(+) create mode 100644 .idea/vcs.xml create mode 100644 src/edu/greenriver/sdev333/BSTSet.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/edu/greenriver/sdev333/BSTSet.java b/src/edu/greenriver/sdev333/BSTSet.java new file mode 100644 index 0000000..ac44299 --- /dev/null +++ b/src/edu/greenriver/sdev333/BSTSet.java @@ -0,0 +1,113 @@ +package edu.greenriver.sdev333; + +public class BSTSet implements MathSet{ + + private Node root; + //helper class + private class Node { + private KeyType key; + + private Node left; + private Node right; + private int N; // Number of Nodes in the subtree rooted here + + 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) { + + } + + /** + * 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 false; + } + + /** + * Is the set empty? + * + * @return true if the set is empty, false otherwise + */ + @Override + public boolean isEmpty() { + return false; + } + + /** + * Number of keys in the set + * + * @return number of keys in the set. + */ + @Override + public int size() { + return 0; + } + + /** + * 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) { + return null; + } + + /** + * 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) { + 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) { + return null; + } + + /** + * 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..070f96d 100644 --- a/src/edu/greenriver/sdev333/MathSet.java +++ b/src/edu/greenriver/sdev333/MathSet.java @@ -1,6 +1,8 @@ package edu.greenriver.sdev333; /** + * @author Adam Wise + * * A MathSet represents a finite mathematical set. * Sets have a collection of unique elements (keys) - no duplicate keys allowed. * Set operations include contains, size, union, intersection, and difference. From ba742f9b4a5d1898f653a10c2d2948556af7b184 Mon Sep 17 00:00:00 2001 From: adamwise Date: Mon, 13 Mar 2023 00:20:16 -0700 Subject: [PATCH 2/3] finished the BSTSet class implmeneted all methods, moving onto speratechaining hash table set --- src/Main.java | 85 ++++++++++ src/edu/greenriver/sdev333/BSTSet.java | 151 +++++++++++++++--- .../sdev333/SeperateChainingHashTableSet.java | 97 +++++++++++ 3 files changed, 314 insertions(+), 19 deletions(-) create mode 100644 src/edu/greenriver/sdev333/SeperateChainingHashTableSet.java diff --git a/src/Main.java b/src/Main.java index 3e59c38..29ff0aa 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,90 @@ +import edu.greenriver.sdev333.BSTSet; +import edu.greenriver.sdev333.MathSet; + +import java.util.Scanner; + public class Main { public static void main(String[] args) { System.out.println("Hello world!"); + + //string created to put in set + String testString = "A E S T H E T I C "; + + //tester method + BSTSet tester = new BSTSet(); + + //scanner to add string to BSTSet + Scanner input = new Scanner(testString); + + //testing is empty before anything is added + System.out.println(tester.isEmpty()); + + //loop to add the strings as keys to my tester + while(input.hasNext()){ + String key = input.next(); + tester.add(key); + } + + BSTSet otherTester = new BSTSet(); + otherTester.add("A"); + otherTester.add("a"); + otherTester.add("e"); + otherTester.add("E"); + otherTester.add("P"); + otherTester.add("T"); + otherTester.add("O"); + otherTester.add("P"); + otherTester.add(("i")); + + //testing all my methods + System.out.println(tester.size()); + System.out.println(tester.contains("A")); + System.out.println(tester.contains("T")); + System.out.println(tester.contains("t")); + System.out.println(tester.contains("p")); + System.out.println(tester.isEmpty()); + System.out.println(); + + System.out.println("//////////////////////////////////////////////"); + + System.out.println(); + // and again on otherTester + System.out.println(otherTester.size()); + System.out.println(otherTester.contains("A")); + System.out.println(otherTester.contains("T")); + System.out.println(otherTester.contains("t")); + System.out.println(otherTester.contains("p")); + System.out.println(otherTester.isEmpty()); + System.out.println(); + + System.out.println("//////////////////////////////////////////////"); + + System.out.println(); + // this is the tester for the 3 set specific methods + MathSet unionTester = tester.union(otherTester); + System.out.println("union of tester and otherTester"); + for (String element: unionTester.keys()){ + System.out.println(element); + } + System.out.println(); + System.out.println("//////////////////////////////////////////////"); + System.out.println(); + + System.out.println("intersection of tester and otherTester"); + MathSet intersectionTester = tester.intersection((otherTester)); + for (String element: intersectionTester.keys()){ + System.out.println(element); + } + + System.out.println(); + System.out.println("//////////////////////////////////////////////"); + System.out.println(); + + System.out.println("difference of tester and otherTester"); + MathSet differenceTester = tester.difference((otherTester)); + for (String element: differenceTester.keys()){ + System.out.println(element); + } + } } \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/BSTSet.java b/src/edu/greenriver/sdev333/BSTSet.java index ac44299..365adb7 100644 --- a/src/edu/greenriver/sdev333/BSTSet.java +++ b/src/edu/greenriver/sdev333/BSTSet.java @@ -1,8 +1,10 @@ package edu.greenriver.sdev333; - -public class BSTSet implements MathSet{ +import java.util.*; +public class BSTSet> implements MathSet{ private Node root; + + //helper class private class Node { private KeyType key; @@ -25,9 +27,53 @@ public Node(KeyType key, int N){ */ @Override public void add(KeyType key) { + root = add(root,key); + + } + + private Node add( Node current, KeyType key) { + //current is the root of the subtree we are looking at + + //where we are supposed to be + if (current == null) { + return new Node(key, 1); + } + + int cmp = key.compareTo(current.key); + // cmp will be -1(negative) if key < current.key + //cmp will be 0(zero) if the key == current.key + // cmp will be +1(positive) if key > current.key + if (cmp < 0) { + //go left + current.left = add(current.left, key); + + } else if (cmp > 0) { + //go right\ + current.right = add(current.right, key); + } else { + //key already exists + current.key = key; + } + + current.N = size(current.left) + size(current.right) + 1; + + return current; } + @Override + public int size() { + return size(root); + } + + private int size(Node current){ + if(current == null){ + return 0; + } else { + return size(current.left) + size(current.right) + 1; + } + } + /** * Is the key in the set? * @@ -36,8 +82,26 @@ public void add(KeyType key) { */ @Override public boolean contains(KeyType key) { - return false; - } + + + //if someone gives me a key, i want to find the value for that key + Node current = root; + while(current != null){ + int cmp = key.compareTo(current.key); + //compareTo returns neg, zero, pos + + if(cmp < 0){ + current = current.left; + }else if(cmp > 0){ + current = current.right; + }else { + return true; + } + }// end of the while loop + + return false; + } + /** * Is the set empty? @@ -46,18 +110,10 @@ public boolean contains(KeyType key) { */ @Override public boolean isEmpty() { - return false; + return root == null || root.N == 0; } - /** - * Number of keys in the set - * - * @return number of keys in the set. - */ - @Override - public int size() { - return 0; - } + /** * Determine the union of this set with another specified set. @@ -70,7 +126,20 @@ public int size() { */ @Override public MathSet union(MathSet other) { - return null; + MathSet result = new BSTSet(); + + //add all the elements of this se to the result set + for(KeyType currentKey : this.keys()){ + result.add(currentKey); + } + + // add elements of the other set to the result set if they are not already in the result set + for (KeyType currentKey : other.keys()) { + if (!result.contains(currentKey)) { + result.add(currentKey); + } + } + return result; } /** @@ -84,7 +153,14 @@ public MathSet union(MathSet other) { */ @Override public MathSet intersection(MathSet other) { - return null; + MathSet result = new BSTSet(); + + for(KeyType currentKey : this.keys()){ + if(other.contains(currentKey)) { + result.add(currentKey); + } + } + return result; } /** @@ -98,7 +174,30 @@ public MathSet intersection(MathSet other) { */ @Override public MathSet difference(MathSet other) { - return null; + + //create an empty set that will hold a result + MathSet result = new BSTSet(); + + for(KeyType currentKey : this.keys()){ + if(!other.contains(currentKey)){ + result.add(currentKey); + } + } + + return result; + + + + //iterator version +// //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); +// } +// } +// } /** @@ -106,8 +205,22 @@ public MathSet difference(MathSet other) { * * @return a collection of all keys in this set */ - @Override public Iterable keys() { - return null; + Queue queue = new Queue<>(); + //start the recursion, collecting the 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/SeperateChainingHashTableSet.java b/src/edu/greenriver/sdev333/SeperateChainingHashTableSet.java new file mode 100644 index 0000000..b614d78 --- /dev/null +++ b/src/edu/greenriver/sdev333/SeperateChainingHashTableSet.java @@ -0,0 +1,97 @@ +package edu.greenriver.sdev333; + +public class SeperateChainingHashTableSet implements MathSet { + + /** + * Puts the specified key into the set. + * + * @param key key to be added into the set + */ + @Override + public void add(KeyType 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) { + return false; + } + + /** + * Is the set empty? + * + * @return true if the set is empty, false otherwise + */ + @Override + public boolean isEmpty() { + return false; + } + + /** + * Number of keys in the set + * + * @return number of keys in the set. + */ + @Override + public int size() { + return 0; + } + + /** + * 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) { + return null; + } + + /** + * 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) { + 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) { + return null; + } + + /** + * 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; + } +} From 3b82617d9c9f60da90a04c7786d32c8a39324ada Mon Sep 17 00:00:00 2001 From: adamwise Date: Mon, 13 Mar 2023 00:50:21 -0700 Subject: [PATCH 3/3] okay finished the other class for SeperateChainingHashTableSet, 100% completed --- src/Main.java | 14 +- .../sdev333/SeperateChainingHashTableSet.java | 125 ++++++++++++++++-- 2 files changed, 128 insertions(+), 11 deletions(-) diff --git a/src/Main.java b/src/Main.java index 29ff0aa..41f7f20 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,17 +1,22 @@ import edu.greenriver.sdev333.BSTSet; import edu.greenriver.sdev333.MathSet; +import edu.greenriver.sdev333.SeperateChainingHashTableSet; import java.util.Scanner; public class Main { public static void main(String[] args) { + + /// okay both implementations work ken, just uncommen the one you want to use System.out.println("Hello world!"); //string created to put in set String testString = "A E S T H E T I C "; //tester method - BSTSet tester = new BSTSet(); + //MathSet tester = new BSTSet(); + MathSet tester = new SeperateChainingHashTableSet<>(); + //scanner to add string to BSTSet Scanner input = new Scanner(testString); @@ -25,7 +30,8 @@ public static void main(String[] args) { tester.add(key); } - BSTSet otherTester = new BSTSet(); + //MathSet otherTester = new BSTSet(); + MathSet otherTester = new SeperateChainingHashTableSet<>(); otherTester.add("A"); otherTester.add("a"); otherTester.add("e"); @@ -86,5 +92,9 @@ public static void main(String[] args) { System.out.println(element); } + + + + } } \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/SeperateChainingHashTableSet.java b/src/edu/greenriver/sdev333/SeperateChainingHashTableSet.java index b614d78..d310559 100644 --- a/src/edu/greenriver/sdev333/SeperateChainingHashTableSet.java +++ b/src/edu/greenriver/sdev333/SeperateChainingHashTableSet.java @@ -1,7 +1,30 @@ package edu.greenriver.sdev333; +import java.util.Iterator; +import java.util.NoSuchElementException; + public class SeperateChainingHashTableSet implements MathSet { + + private Node head; // first node in the linked list + private int size; // size variable for tracking size + + private class Node { + KeyType key; + Node next; + + /** + * Node class used in lunkedlist symbol table implentation + * @param key + * @param next + */ + public Node(KeyType key, Node next) { + this.key = key; + this.next = next; + } + + } + /** * Puts the specified key into the set. * @@ -10,7 +33,15 @@ public class SeperateChainingHashTableSet implements MathSet @Override public void add(KeyType key) { - } + for (Node current = head; current != null; current = current.next) { + if (key.equals(current.key)) { + return; + } + } + head = new Node(key, head); + + } + /** * Is the key in the set? @@ -20,8 +51,15 @@ public void add(KeyType key) { */ @Override public boolean contains(KeyType key) { - return false; - } + + for (Node current = head; current != null; current = current.next) { + if (key.equals(current.key)) { + return true; + } + } + return false; + } + /** * Is the set empty? @@ -30,7 +68,9 @@ public boolean contains(KeyType key) { */ @Override public boolean isEmpty() { - return false; + + + return size() == 0; } /** @@ -40,7 +80,10 @@ public boolean isEmpty() { */ @Override public int size() { - return 0; + for (KeyType key : this.keys()) { + size++; + } + return size; } /** @@ -54,7 +97,20 @@ public int size() { */ @Override public MathSet union(MathSet other) { - return null; + MathSet result = new SeperateChainingHashTableSet(); + + //add all the elements of this se to the result set + for(KeyType currentKey : this.keys()){ + result.add(currentKey); + } + + // add elements of the other set to the result set if they are not already in the result set + for (KeyType currentKey : other.keys()) { + if (!result.contains(currentKey)) { + result.add(currentKey); + } + } + return result; } /** @@ -68,9 +124,17 @@ public MathSet union(MathSet other) { */ @Override public MathSet intersection(MathSet other) { - return null; + MathSet result = new SeperateChainingHashTableSet(); + + 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. @@ -82,7 +146,16 @@ public MathSet intersection(MathSet other) { */ @Override public MathSet difference(MathSet other) { - return null; + //create an empty set that will hold a result + MathSet result = new SeperateChainingHashTableSet(); + + for(KeyType currentKey : this.keys()){ + if(!other.contains(currentKey)){ + result.add(currentKey); + } + } + + return result; } /** @@ -92,6 +165,40 @@ public MathSet difference(MathSet other) { */ @Override public Iterable keys() { - return null; + return new Iterable<>() { + @Override + public Iterator iterator() { + return new SSIterator(head); + } + }; + } + + + /** + * helper iterator class used in for each loops + */ + public class SSIterator implements Iterator { + + private Node current; + + public SSIterator(Node head) { + current = head; + } + + @Override + public boolean hasNext() { + return current != null; + } + + @Override + public KeyType next() { + + if (!hasNext()) { + throw new NoSuchElementException(); + } + KeyType key = current.key; + current = current.next; + return key; + } } }