Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 108 additions & 1 deletion src/Main.java
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> 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<String> 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<String> 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<String> 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<String> 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<String> 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<String> 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<String> 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<String> SeparateChainingHashIntersection;
SeparateChainingHashIntersection = hashSet1.intersection(hashSet2);
System.out.print("\nHashSet 1 Intersect HashSet 2: ");
for (String s : SeparateChainingHashIntersection.keys()){
System.out.print(s + " ");
}
}
}
229 changes: 229 additions & 0 deletions src/edu/greenriver/sdev333/BSTSet.java
Original file line number Diff line number Diff line change
@@ -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<KeyType extends Comparable<KeyType>> implements MathSet<KeyType> {
//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<KeyType> union(MathSet<KeyType> other) {
// Create an empty set that hold the result
MathSet<KeyType> result = new BSTSet<KeyType>();

//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<KeyType> intersection(MathSet<KeyType> other) {
// Create an empty set that hold the result
MathSet<KeyType> result = new BSTSet<KeyType>();

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<KeyType> difference(MathSet<KeyType> other) {
// Create an empty set that hold the result
MathSet<KeyType> result = new BSTSet<KeyType>();

// iterate (walk through all items in this)
// Iterator<KeyType> itr = (Iterator<KeyType>) 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<KeyType> keys() {
//create a new empty queue to hold my results
Queue<KeyType> 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<KeyType> q){
if (current == null){
//do nothing - intentional
return;
}

inorder(current.left, q);
q.enqueue(current.key);
inorder(current.right, q);
}
}
7 changes: 6 additions & 1 deletion src/edu/greenriver/sdev333/Queue.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<ItemType> implements Iterable<ItemType> {
// private helper node class:
Expand Down
Loading