diff --git a/src/Main.java b/src/Main.java index 3e59c38..15527ad 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,112 @@ +/** + * @Author Ron Nguyen + * Date: March 15, 2023 + * SDEV 333 + * Professor: Ken Hang + * File name: Main.java + * File Description: This file contains test code for MathSet of both implement of + * SeparateChainingHashST and BST(Binary Search Tree) + */ + +import edu.greenriver.sdev333.BSTSet; +import edu.greenriver.sdev333.MathSet; +import edu.greenriver.sdev333.SeparateChainingHashSet; + public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + MathSet bstSet1 = new BSTSet<>(); + bstSet1.add("A"); + bstSet1.add("B"); + bstSet1.add("C"); + bstSet1.add("D"); + System.out.print("BSTSet 1: "); + for (String s : bstSet1.keys()){ + System.out.print(s + " "); + } + MathSet bstSet2 = new BSTSet<>(); + bstSet2.add("C"); + bstSet2.add("D"); + bstSet2.add("E"); + bstSet2.add("F"); + System.out.print("\nBSTSet 2: "); + for (String s : bstSet2.keys()){ + System.out.print(s + " "); + } + System.out.println("\nBSTSet 1 size: " + bstSet1.size()); + System.out.println("BSTSet 1 is empty? "+ bstSet1.isEmpty()); + System.out.println("BSTSet 1 contain letter A?: " + bstSet1.contains("A")); + System.out.println("BSTSet 2 contain letter A?: " + bstSet2.contains("A")); + + // Test Different method + MathSet BSTDifferent = new BSTSet<>(); + BSTDifferent = bstSet1.difference(bstSet2); + System.out.print("Different of BSTSet 1 to BSTSet 2: "); + for (String s : BSTDifferent.keys()){ + System.out.print(s + " "); + } + + // Test Union method + MathSet BSTUnion= new BSTSet<>(); + BSTUnion = bstSet1.union(bstSet2); + System.out.print("\nBSTSet 1 Union BSTSet 2: "); + for (String s : BSTUnion.keys()){ + System.out.print(s + " "); + } + + //Test Intersection method + MathSet BSTIntersection= new BSTSet<>(); + BSTIntersection = bstSet1.intersection(bstSet2); + System.out.print("\nBSTSet 1 Intersect BSTSet 2: "); + for (String s : BSTIntersection.keys()){ + System.out.print(s + " "); + } + //HASHSET + System.out.println("\n=========================================================="); + MathSet hashSet1 = new SeparateChainingHashSet<>(10); + hashSet1.add("Green"); + hashSet1.add("Red"); + hashSet1.add("Orange"); + hashSet1.add("Yellow"); + System.out.print("HashSet 1: "); + for (String s : hashSet1.keys()){ + System.out.print(s + " "); + } + MathSet hashSet2 = new BSTSet<>(); + hashSet2.add("Red"); + hashSet2.add("Yellow"); + hashSet2.add("Blue"); + hashSet2.add("Cyan"); + System.out.print("\nHashSet 2: "); + for (String s : hashSet2.keys()){ + System.out.print(s + " "); + } + System.out.println("\nHashSet 1 size is: "+ hashSet1.size()); + System.out.println("Is HashSet 1 empty? " + hashSet1.isEmpty()); + System.out.println("Does HashSet 1 contain color Blue? " + hashSet1.contains("Blue")); + System.out.println("Does HashSet 2 contain color Blue? " + hashSet2.contains("Blue")); + + // Test Different method + MathSet SeparateChainingHashDifferent; + SeparateChainingHashDifferent = hashSet1.difference(hashSet2); + System.out.print("Different of HashSet 1 to HashSet 2: "); + for (String s : SeparateChainingHashDifferent.keys()){ + System.out.print(s + " "); + } + + // Test Union method + MathSet SeparateChainingHashUnion; + SeparateChainingHashUnion = hashSet1.union(hashSet2); + System.out.print("\nHashSet 1 Union HashSet 2: "); + for (String s : SeparateChainingHashUnion.keys()){ + System.out.print(s + " "); + } + + //Test Intersection method + MathSet SeparateChainingHashIntersection; + SeparateChainingHashIntersection = hashSet1.intersection(hashSet2); + System.out.print("\nHashSet 1 Intersect HashSet 2: "); + for (String s : SeparateChainingHashIntersection.keys()){ + System.out.print(s + " "); + } } } \ 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..ae7fabf --- /dev/null +++ b/src/edu/greenriver/sdev333/BSTSet.java @@ -0,0 +1,229 @@ +/** + * @Author Ron Nguyen + * Date: March 15, 2023 + * SDEV 333 + * Professor: Ken Hang + * File name: BSTSet.java + * File Description: This file is an implement of Math set using Binary Search Tree + */ + +package edu.greenriver.sdev333; + +public class BSTSet> implements MathSet { + //fields + private Node root; + + //Node 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) { + root = add(root,key); + } + + private Node add(Node current, KeyType key){ + // current is the root of the subtree wer are looking at + + //we are at where we are supposed to be + if (current == null){ + //create new node + return new Node(key, 1); + } + + int cmp = key.compareTo(current.key); + //go left + if (cmp < 0){ + current.left = add(current.left, key); + } + //go right + else if (cmp > 0){ + current.right = add(current.right, key); + } + // update the node's N - number of nodes in the subtree + current.N = size(current.left) + size(current.right) + 1; + return current; + } + + + + /** + * 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) { + Node current = root; + + while (current != null) { + int cmp = key.compareTo(current.key); + + if (cmp == 0) { + // We found an exact match for the key, so return true + return true; + } else if (cmp < 0) { + // The key is less than the current node's key, so move left + current = current.left; + } else { + // The key is greater than the current node's key, so move right + current = current.right; + } + } + + // If we get here, we didn't find a matching key in the tree + return false; + } + + /** + * Is the set empty? + * + * @return true if the set is empty, false otherwise + */ + @Override + public boolean isEmpty() { + return root == null; + } + + /** + * Number of keys in the set + * + * @return number of keys in the set. + */ + @Override + public int size() { + return size(root); + } + + private int size(Node current){ + if (current == null){ + return 0; + } + else { + return current.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 hold the result + MathSet result = new BSTSet(); + + //add all element from this set + for (KeyType currentKey : this.keys()){ + result.add(currentKey); + } + //add all element from the other set + 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 hold the result + MathSet result = new BSTSet(); + + for (KeyType currentKey : this.keys()){ + // if the key contains in the other set, add to result set + 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 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); +// } +// } + + //foreach loop + 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() { + //create a new empty queue to hold my results + 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 - intentional + return; + } + + inorder(current.left, q); + q.enqueue(current.key); + inorder(current.right, q); + } +} diff --git a/src/edu/greenriver/sdev333/Queue.java b/src/edu/greenriver/sdev333/Queue.java index 638d71c..de1d7c4 100644 --- a/src/edu/greenriver/sdev333/Queue.java +++ b/src/edu/greenriver/sdev333/Queue.java @@ -3,7 +3,12 @@ import java.util.Iterator; /** - * FIFO queue, page 151 of the red book + * @Author Ron Nguyen + * Date: March 15, 2023 + * SDEV 333 + * Professor: Ken Hang + * File name: Queue.java + * File Description: This file is an implement of Queue */ public class Queue implements Iterable { // private helper node class: diff --git a/src/edu/greenriver/sdev333/SeparateChainingHashSet.java b/src/edu/greenriver/sdev333/SeparateChainingHashSet.java new file mode 100644 index 0000000..92cc84b --- /dev/null +++ b/src/edu/greenriver/sdev333/SeparateChainingHashSet.java @@ -0,0 +1,179 @@ +/** + * @Author Ron Nguyen + * Date: March 15, 2023 + * SDEV 333 + * Professor: Ken Hang + * File name: SeparateChainingHashSet.java + * File Description: This file is an implement of MathSet using SeparateChainingHashST + */ + +package edu.greenriver.sdev333; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; + +public class SeparateChainingHashSet> implements MathSet { + private int M; // M is the number of buckets (or pile) + private LinkedList[] st; // array of ST objects + + public SeparateChainingHashSet(int M){ + // Take their number of buckets, save it into my filed + this.M = M; + st = new LinkedList[M]; + for (int i = 0; i < M; i++) { + st[i] = new LinkedList<>(); + } + } + + private int hash(KeyType key){ + // hash function = they give me a key, I return an int (bucket #, array index) + 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); + // put the key and value into that bucket + st[index].add(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 index = hash(key); + return st[index].contains(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() { + 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 hold the result + MathSet result = new BSTSet(); + + //add all element from this set + for (KeyType currentKey : this.keys()){ + result.add(currentKey); + } + //add all element from the other set + 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 hold the result + MathSet result = new BSTSet(); + + for (KeyType currentKey : this.keys()){ + // if the key contains in the other set, add to result set + 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 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); +// } +// } + + //foreach loop + 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() { + LinkedList list = new LinkedList<>(); + for (int i = 0; i < M; i++) { + for (KeyType key : st[i]) { + list.add(key); + } + } + return list; + } +}