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
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

140 changes: 139 additions & 1 deletion src/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,143 @@
import edu.greenriver.sdev333.*;

import java.sql.SQLOutput;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");

// TEST CODE FOR BSTSET //

//create two sets
//add items to each of the sets (some same. some different)
MathSet<String> bstSet1 = new BSTSet<>();
bstSet1.add("Ken");
bstSet1.add("Tina");
bstSet1.add("Josh");
bstSet1.add("Tyler");
bstSet1.add("Monroe");
bstSet1.add("Snoop");

MathSet<String> bstSet2 = new BSTSet<>();
bstSet2.add("Tina");
bstSet2.add("Tyler");
bstSet2.add("Ken");
bstSet2.add("Kelly");
bstSet2.add("Zoe");

//empty bstSet to test isEmpty()
MathSet<String> emptyBSTSet = new BSTSet<>();

System.out.println("BSTSet: ");

System.out.println("bstSet1 Empty: " + bstSet1.isEmpty());
System.out.println("bstSet2 Empty: " + bstSet2.isEmpty());
System.out.println("emptyBSTSet Empty: " + emptyBSTSet.isEmpty());
System.out.println("");

System.out.println("bstSet1 size: " + bstSet1.size());
System.out.println("bstSet2 size: " + bstSet2.size());
System.out.println("");

//tests
//set intersection
// intersect set 1 and set 2, save into intersResult
MathSet<String> intersResult1 = bstSet1.intersection(bstSet2);

//print out keys from intersResult
System.out.println("Intersection results: ");
for(String key : intersResult1.keys()) {
System.out.println(key);
}
System.out.println("");

//set union
// union set 1 and set 2, save into unionResult
MathSet<String> unionResult1 = bstSet1.union(bstSet2);

//print out keys from unionResult
System.out.println("Union results: ");
for(String key : unionResult1.keys()) {
System.out.println(key);
}
System.out.println("");

//set difference
// union set 1 and set 2, save into diffResult
MathSet<String> diffResult1 = bstSet1.difference(bstSet2);

//print out keys from diffResult
System.out.println("Difference results: ");
for(String key : diffResult1.keys()) {
System.out.println(key);
}
System.out.println("");

// TEST CODE FOR SEPARATECHAININGHASHSET //

//create two sets
//add items to each of the sets (some same. some different)
MathSet<String> schSet1 = new SeparateChainingHashSet<>();
schSet1.add("Ken");
schSet1.add("Tina");
schSet1.add("Josh");
schSet1.add("Tyler");
schSet1.add("Monroe");
schSet1.add("Snoop");

MathSet<String> schSet2 = new SeparateChainingHashSet<>();
schSet2.add("Tina");
schSet2.add("Tyler");
schSet2.add("Ken");
schSet2.add("Kelly");
schSet2.add("Zoe");

//empty schSet to test isEmpty()
MathSet<String> emptySCHSet = new SeparateChainingHashSet<>();

System.out.println("SeparateChainingHashSet: ");

System.out.println("schSet1 Empty: " + schSet1.isEmpty());
System.out.println("schSet2 Empty: " + schSet2.isEmpty());
System.out.println("emptySCHSet Empty: " + emptySCHSet.isEmpty());
System.out.println("");

System.out.println("schSet1 size: " + schSet1.size());
System.out.println("schSet2 size: " + schSet2.size());
System.out.println("");

//tests
//set intersection
// intersect set 1 and set 2, save into intersResult
MathSet<String> intersResult2 = schSet1.intersection(schSet2);

//print out keys from intersResult
System.out.println("Intersection results: ");
for(String key : intersResult2.keys()) {
System.out.println(key);
}
System.out.println("");

//set union
// union set 1 and set 2, save into unionResult
MathSet<String> unionResult2 = schSet1.union(schSet2);

//print out keys from unionResult
System.out.println("Union results: ");
for(String key : unionResult2.keys()) {
System.out.println(key);
}
System.out.println("");

//set difference
// union set 1 and set 2, save into diffResult
MathSet<String> diffResult2 = schSet1.difference(schSet2);

//print out keys from diffResult
System.out.println("Difference results: ");
for(String key : diffResult2.keys()) {
System.out.println(key);
}
System.out.println("");

}
}
209 changes: 209 additions & 0 deletions src/edu/greenriver/sdev333/BSTSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
package edu.greenriver.sdev333;

import java.util.Iterator;

public class BSTSet<KeyType extends Comparable<KeyType>> implements MathSet<KeyType> {

//node helper class
private class Node {

private KeyType key;
private Node left;
private Node right;
private int N;

public Node(KeyType key, int N) {
this.key = key;
this.N = N;
}
}

//field
private Node root;

/**
* 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);
}

//helper method
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);
}
x.N = size(x.left) + size(x.right) + 1;
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 get(key) != null;
}

public KeyType get(KeyType key) {
return get(root, key);
}

private KeyType get(Node x, KeyType key) {
if(x == null) {
return null;
}
int cmp = key.compareTo(x.key);
if(cmp < 0) {
//go left
return get(x.left, key);
} else if (cmp > 0) {
return get(x.right, key);
} else {
return x.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() {
return size(root);
}
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<KeyType> union(MathSet<KeyType> other) {
//create an empty set that will hold the result
MathSet<KeyType> result = new BSTSet<KeyType>();

for (KeyType currentKey : this.keys()) {
result.add(currentKey);
}

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 will hold the result
MathSet<KeyType> result = new BSTSet<KeyType>();

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.
* 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 will 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);
}
}
*/

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() {
Queue<KeyType> queue = new Queue<>();

inOrder(root, queue);

return queue;
}
private void inOrder(Node x, Queue<KeyType> queue) {
if (x == null) {
return;
}
inOrder(x.left, queue);
queue.enqueue(x.key);
inOrder(x.right, queue);
}
}
1 change: 1 addition & 0 deletions src/edu/greenriver/sdev333/Queue.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Iterator;

/**
* Zoe Fortin
* FIFO queue, page 151 of the red book
*/
public class Queue<ItemType> implements Iterable<ItemType> {
Expand Down
Loading