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.

118 changes: 117 additions & 1 deletion src/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,121 @@
import edu.greenriver.sdev333.BSTSet;
import edu.greenriver.sdev333.MathSet;
import edu.greenriver.sdev333.SCHSet;

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

//Test set this
MathSet<Integer> testSetThis = new BSTSet<Integer>();
testSetThis.add(5);
testSetThis.add(8);
testSetThis.add(34);
testSetThis.add(45);
testSetThis.add(13);
testSetThis.add(76);
testSetThis.add(23);
testSetThis.add(2);
testSetThis.add(55);
testSetThis.add(31);

//Test set other
MathSet<Integer> testSetOther = new BSTSet<Integer>();
testSetOther.add(5);
testSetOther.add(8);
testSetOther.add(34);
testSetOther.add(89);
testSetOther.add(13);
testSetOther.add(76);
testSetOther.add(29);
testSetOther.add(2);
testSetOther.add(25);
testSetOther.add(39);

//Test Union
MathSet<Integer> testSetUnion = testSetThis.union(testSetOther);

for (Integer currentKey: testSetUnion.keys()
) {
System.out.print("BST Union: " + currentKey);
System.out.print("\n");
}

//Test Difference
MathSet<Integer> testSetDifference = testSetThis.difference(testSetOther);

for (Integer currentKey: testSetDifference.keys()
) {
System.out.print("BST Difference: " + currentKey);
System.out.print("\n");
}

//Test Intersection
MathSet<Integer> testSetIntersection = testSetThis.intersection(testSetOther);

for (Integer currentKey: testSetIntersection.keys()
) {
System.out.print("BST Intersection: " + currentKey);
System.out.print("\n");
}

// SCH TEST

//Test set this
MathSet<Integer> testSetThisChain = new SCHSet<Integer>();
testSetThisChain.add(5);
testSetThisChain.add(8);
testSetThisChain.add(34);
testSetThisChain.add(45);
testSetThisChain.add(13);
testSetThisChain.add(76);
testSetThisChain.add(23);
testSetThisChain.add(2);
testSetThisChain.add(55);
testSetThisChain.add(31);

//Test set other
MathSet<Integer> testSetOtherChain = new SCHSet<Integer>();
testSetOtherChain.add(5);
testSetOtherChain.add(8);
testSetOtherChain.add(34);
testSetOtherChain.add(89);
testSetOtherChain.add(13);
testSetOtherChain.add(76);
testSetOtherChain.add(29);
testSetOtherChain.add(2);
testSetOtherChain.add(25);
testSetOtherChain.add(39);

//Test Union
MathSet<Integer> testSetUnionChain = testSetThisChain.union(testSetOtherChain);

for (Integer currentKey: testSetUnionChain.keys()
) {
System.out.print("SCH Union: " + currentKey);
System.out.print("\n");
}

//Test Difference
MathSet<Integer> testSetDifferenceChain = testSetThisChain.difference(testSetOtherChain);

for (Integer currentKey: testSetDifferenceChain.keys()
) {
System.out.print("SCH Difference: " + currentKey);
System.out.print("\n");
}

//Test Inersection
MathSet<Integer> testSetIntersectionChain = testSetThisChain.intersection(testSetOtherChain);

for (Integer currentKey: testSetIntersectionChain.keys()
) {
System.out.print("SCH Intersection: " + currentKey);
System.out.print("\n");
}

System.out.println(testSetThis.isEmpty());
System.out.println(testSetThisChain.isEmpty());
System.out.println(testSetThis.contains(13));
System.out.println(testSetOtherChain.contains(98));
}
}
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 @@
package edu.greenriver.sdev333;

import java.util.Iterator;

public class BSTSet<KeyType extends Comparable<KeyType>> implements MathSet<KeyType>{
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;
}
}

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) {
//
// }
@Override
public void add(KeyType key) {
root = put(root, key);
}

public Node put(Node current, KeyType key){
//current is null create a new node n = 1
if(current == null){
return new Node(key,1);
}
//compare key past in to current key
int cmp = key.compareTo(current.key);

//if cmp less than 0 then put on left, if greater than 0 put on right else change current val
if (cmp < 0 ) {
current.left = put(current.left, key);
} else if (cmp > 0 ){
current.right = put(current.right, key);
}
//increment n
current.N = size(current.left) + size(current.right) + 1;
return current;
}

/**
* Returns the value associated with the given key in the set.
*
* @param key the key to look up
* @return the value associated with the given key, or null if the key is not found in the set
*/
public KeyType get(KeyType key) {
Node node = root;
while (node != null) {
int cmp = key.compareTo(node.key);
if (cmp < 0) {
node = node.left;
} else if (cmp > 0) {
node = node.right;
} else {
return node.key;
}
}
return null;
}

/**
* 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) {

for (KeyType currentKey : this.keys()) {
if(currentKey == key){
return true;
}
}
return false;
}

/**
* Is the set empty?
*
* @return true if the set is empty, false otherwise
*/
@Override
public boolean isEmpty() {
if(root.N == 0){
return true;
}
return false;
}

/**
* Number of keys in the set
*
* @return number of keys in the set.
*/
@Override
public int size() {
if (root == null){
return 0;
} else {
return root.N;
}
}

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


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

for (KeyType currentKey: other.keys()) {
if(!result.contains(currentKey)) {
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) {
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>();


for (KeyType currentKey: this.keys()) {
if(!other.contains(currentKey)){
result.add(currentKey);
}
}
return result;
//Iterate through all of this
// Iterator<KeyType> itr = (Iterator<KeyType>) this.keys();
// while (itr.hasNext()){
// KeyType currentKey = itr.next();
// if (!other.contains(currentKey)) {
// result.add(currentKey);
// }
// }
}

/**
* 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<KeyType>();
inorder(root, queue);
return new Iterable<KeyType>() {
public Iterator<KeyType> iterator() {
return queue.iterator();
}
};
}

private void inorder(Node current, Queue<KeyType> queue) {
if (current == null) {
return;
}
inorder(current.left, queue);
queue.enqueue(current.key);
inorder(current.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;

/**
* @author Thomas Loudon
* FIFO queue, page 151 of the red book
*/
public class Queue<ItemType> implements Iterable<ItemType> {
Expand Down
Loading