From 61fa6e4275ad73a94f24a116fb64bfeb74ea7b66 Mon Sep 17 00:00:00 2001 From: Ron Nguyen Date: Wed, 8 Mar 2023 17:17:21 -0500 Subject: [PATCH 1/3] test --- 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..3684856 100644 --- a/src/edu/greenriver/sdev333/Queue.java +++ b/src/edu/greenriver/sdev333/Queue.java @@ -3,6 +3,7 @@ import java.util.Iterator; /** + * @author Ron Nguyen * FIFO queue, page 151 of the red book */ public class Queue implements Iterable { From 4b1f8096f49946249ffc3412510bec512407ec18 Mon Sep 17 00:00:00 2001 From: Ron Nguyen Date: Tue, 14 Mar 2023 21:57:19 -0400 Subject: [PATCH 2/3] Implement MathSet using SeparateChanningHashST and BST --- src/Main.java | 109 ++++++++- src/edu/greenriver/sdev333/BSTSet.java | 229 ++++++++++++++++++ src/edu/greenriver/sdev333/Queue.java | 8 +- .../sdev333/SeparateChainingHashSet.java | 179 ++++++++++++++ .../sdev333/SequentialSearchST.java | 71 ++++++ 5 files changed, 593 insertions(+), 3 deletions(-) create mode 100644 src/edu/greenriver/sdev333/BSTSet.java create mode 100644 src/edu/greenriver/sdev333/SeparateChainingHashSet.java create mode 100644 src/edu/greenriver/sdev333/SequentialSearchST.java 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 3684856..de1d7c4 100644 --- a/src/edu/greenriver/sdev333/Queue.java +++ b/src/edu/greenriver/sdev333/Queue.java @@ -3,8 +3,12 @@ import java.util.Iterator; /** - * @author Ron Nguyen - * 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; + } +} diff --git a/src/edu/greenriver/sdev333/SequentialSearchST.java b/src/edu/greenriver/sdev333/SequentialSearchST.java new file mode 100644 index 0000000..d53771b --- /dev/null +++ b/src/edu/greenriver/sdev333/SequentialSearchST.java @@ -0,0 +1,71 @@ +package edu.greenriver.sdev333; + +/** + * Sequential search (unordered linked list implementation) of Symbol Table + * Refer to p. 374-377 in Sedgewick and Wayne, Algorithms, 4th edition + * @param + * @param + */ +public class SequentialSearchST implements SymbolTable { + //fields + private Node first; + private int size; + + private class Node{//linked-list Node + KeyType key; + ValueType val; + Node next; + public Node(KeyType key, ValueType val, Node next){ + this.key = key; + this.val = val; + this.next = next; + } + } + + @Override + public void put(KeyType key, ValueType value) { + //search for key, return associated value using for loop + for (Node current = first; current != null;current = current.next){ + //search if key equal to current.key + if (key.equals(current.key)){ + current.val = value; //update matched key to value + return; + } + } + //if search found no same key, add new node + first = new Node(key, value, first); + size++; + } + + @Override + public ValueType get(KeyType key) { + //search for key, return associated value using for loop + for (Node current = first; current != null;current = current.next){ + //if search found + if (key.equals(current.key)){ + return current.val; + } + } + //if not found + return null; + } + + @Override + public int size() { + return size; + } + + @Override + public Iterable keys() { + // Create a new queue to hold the keys + Queue queue = new Queue<>(); + + // Iterate over the queue and add each key to the queue + for (Node current = first; current != null; current = current.next) { + queue.enqueue(current.key); + } + + // Return the keys in the queue as an iterable + return queue; + } +} From 393284ab71498f1039b32ba5d1112ea070325291 Mon Sep 17 00:00:00 2001 From: ronnguyen1011 <66894026+ronnguyen1011@users.noreply.github.com> Date: Tue, 14 Mar 2023 18:59:17 -0700 Subject: [PATCH 3/3] Delete SequentialSearchST.java --- .../sdev333/SequentialSearchST.java | 71 ------------------- 1 file changed, 71 deletions(-) delete mode 100644 src/edu/greenriver/sdev333/SequentialSearchST.java diff --git a/src/edu/greenriver/sdev333/SequentialSearchST.java b/src/edu/greenriver/sdev333/SequentialSearchST.java deleted file mode 100644 index d53771b..0000000 --- a/src/edu/greenriver/sdev333/SequentialSearchST.java +++ /dev/null @@ -1,71 +0,0 @@ -package edu.greenriver.sdev333; - -/** - * Sequential search (unordered linked list implementation) of Symbol Table - * Refer to p. 374-377 in Sedgewick and Wayne, Algorithms, 4th edition - * @param - * @param - */ -public class SequentialSearchST implements SymbolTable { - //fields - private Node first; - private int size; - - private class Node{//linked-list Node - KeyType key; - ValueType val; - Node next; - public Node(KeyType key, ValueType val, Node next){ - this.key = key; - this.val = val; - this.next = next; - } - } - - @Override - public void put(KeyType key, ValueType value) { - //search for key, return associated value using for loop - for (Node current = first; current != null;current = current.next){ - //search if key equal to current.key - if (key.equals(current.key)){ - current.val = value; //update matched key to value - return; - } - } - //if search found no same key, add new node - first = new Node(key, value, first); - size++; - } - - @Override - public ValueType get(KeyType key) { - //search for key, return associated value using for loop - for (Node current = first; current != null;current = current.next){ - //if search found - if (key.equals(current.key)){ - return current.val; - } - } - //if not found - return null; - } - - @Override - public int size() { - return size; - } - - @Override - public Iterable keys() { - // Create a new queue to hold the keys - Queue queue = new Queue<>(); - - // Iterate over the queue and add each key to the queue - for (Node current = first; current != null; current = current.next) { - queue.enqueue(current.key); - } - - // Return the keys in the queue as an iterable - return queue; - } -}