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.

95 changes: 95 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,100 @@
import edu.greenriver.sdev333.*;

public class Main {
public static void main(String[] args) {

System.out.println("Hello world!");

// BSTSET:
System.out.println("\nBSTSet Testing: ");

// Create 2 BSTSets
// add items to each of the sets (some same, some different)
MathSet<String> set1 = new BSTSet<>();
set1.add("Ken");
set1.add("Ken");
set1.add("Tina");
set1.add("Susan");

MathSet<String> set2 = new BSTSet<>();
set2.add("Josh");
set2.add("Ken");
set2.add("Tyler");

// Test

// intersection set 1 and set 2
MathSet<String> result1 = set1.intersection(set2);
System.out.println("\nBSTSet intersection: ");
for (String key : result1.keys()) {
System.out.println(key);
}

// union set 1 and set 2
MathSet<String> result2 = set1.union(set2);
System.out.println("\nBSTSet union: ");
for (String key : result2.keys()) {
System.out.println(key);
}

// difference set 1 -> set 2
MathSet<String> result3 = set1.difference(set2);
System.out.println("\nBSTSet difference: ");
for (String key : result3.keys()) {
System.out.println(key);
}

// difference set 2 -> set 1
MathSet<String> result4 = set2.difference(set1);
System.out.println("\nBSTSet reverse difference: ");
for (String key : result4.keys()) {
System.out.println(key);
}

// HASHSET:
System.out.println("\nHashSet Testing: ");

// Create 2 HashSets
// add items to each of the sets (some same, some different)
MathSet<String> set3 = new HashSet<>(1);
set3.add("Ken");
set3.add("Ken");
set3.add("Tina");
set3.add("Susan");

MathSet<String> set4 = new HashSet<>(10);
set4.add("Josh");
set4.add("Ken");
set4.add("Tyler");

// Test

// intersection set 3 and set 4
MathSet<String> result5 = set3.intersection(set4);
System.out.println("\nHashSet intersection: ");
for (String key : result5.keys()) {
System.out.println(key);
}

// union set 3 and set 4
MathSet<String> result6 = set3.union(set4);
System.out.println("\nHashSet union: ");
for (String key : result6.keys()) {
System.out.println(key);
}

// difference set 3 -> set 4
MathSet<String> result7 = set3.difference(set4);
System.out.println("\nHashSet difference: ");
for (String key : result7.keys()) {
System.out.println(key);
}

// difference set 4 -> set 3
MathSet<String> result8 = set4.difference(set3);
System.out.println("\nHashSet reverse difference: ");
for (String key : result8.keys()) {
System.out.println(key);
}
}
}
215 changes: 215 additions & 0 deletions src/edu/greenriver/sdev333/BSTSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
package edu.greenriver.sdev333;

import java.util.Iterator;
import java.util.NoSuchElementException;

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

private Node root;

public 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);
}

public 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 contains(root, key);
}

public boolean contains(Node x, KeyType key) {
if (x == null) {
return false;
}
int cmp = key.compareTo(x.key);
if (cmp < 0) return contains(x.left, key);
else if (cmp > 0) return contains(x.right, key);
else return true;
}

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

// iterate through all items in this
for (KeyType currentKey : this.keys()) {
// add items that are not in other
if (!other.contains(currentKey)) {
result.add(currentKey);
}
}
// add all items from other
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>();

// iterate through all items in this
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 through all items in this
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() {
return keys(min(), max());
}

public Iterable<KeyType> keys(KeyType lo, KeyType hi) {
Queue<KeyType> queue = new Queue<KeyType>();
keys(root, queue, lo, hi);
return queue;
}

private void keys(Node x, Queue<KeyType> queue, KeyType lo, KeyType hi) {
if (x == null) return;
int cmplo = lo.compareTo(x.key);
int cmphi = hi.compareTo(x.key);
if (cmplo < 0) keys(x.left, queue, lo, hi);
if (cmplo <= 0 && cmphi >= 0) queue.enqueue(x.key);
if (cmphi > 0) keys(x.right, queue, lo, hi);
}

// pulled in min() and max() from BST to help keys()
public KeyType min() {
if (isEmpty()) throw new NoSuchElementException();
Node x = min(root);
return x.key;
}

private Node min(Node x) {
if (x.left == null) return x;
return min(x.left);
}

public KeyType max() {
if (isEmpty()) throw new NoSuchElementException();
Node x = max(root);
return x.key;
}

private Node max(Node x) {
if (x.right == null) return x;
return max(x.right);
}
}
Loading