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.

74 changes: 74 additions & 0 deletions src/FlightRoutesGraph.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import edu.greenriver.sdev333.BSTSet;
import edu.greenriver.sdev333.MathSet;

import java.util.HashSet;

public class FlightRoutesGraph {



private class Edge {
private String node1;
private String node2;

public Edge(String from, String to) {
node1 = from;
node2 = to;
}
}

private MathSet<String> nodes;

private MathSet<Edge> edges;

public FlightRoutesGraph() {
nodes = new BSTSet<>();
edges = new HashSet<>();
}

public void addNode(String city) {
nodes.add(city);
}

public void addEdge(String city1, String city2) {
Edge connection = new Edge(city1, city2);
edges.add(connection);
}



/**
*
* @param city
*/
public MathSet<String> getNeighbors(String city) {
MathSet<String> neighbors = new BSTSet<>();

for (Edge e: edges.keys()) {
if (e.node1.equals(city)) {
neighbors.add(e.node2);
}
else if (e.node2.equals(city)) {
neighbors.add(e.node1);
}
}

return neighbors;
}

public static void main(String[] args) {
FlightRoutesGraph g = new FlightRoutesGraph();
g.addNode("JFK");
g.addNode("ORD");
g.addNode("ATL");
g.addNode("MCO");
g.addNode("DEN");
g.addEdge("ATL", "MCO");
g.addEdge("JFK", "MCO");
g.addEdge("DEN", "ORD");
g.addEdge("ORD", "ATL");
MathSet<String> directFromJFK = g.getNeighbors("JFK");
MathSet<String> directFromATL = g.getNeighbors("ATL");

}
}
44 changes: 43 additions & 1 deletion src/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,47 @@
import edu.greenriver.sdev333.BSTSet;
import edu.greenriver.sdev333.MathSet;
import edu.greenriver.sdev333.SeparateChainingHashTable;
import java.security.Key;

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

String testChar = "P O O P I E";

MathSet<String> testset = new SeparateChainingHashTableSet<>();

Scanner input = new Scanner(testChar);

System.out.println(testset.isEmpty());

while(input.hasNext()){
String key = input.next();
testset.add(key);
}

System.out.println(testset.size());
System.out.println(testset.contains("P"));
System.out.println(testset.contains("O"));
System.out.println(testset.contains("O"));
System.out.println(testset.contains("P"));
System.out.println(testset.isEmpty());
System.out.println();

}

MathSet<String> testset2 = new SeparateChainingHashTable<>();

testset2.add("P");
testset2.add("e");
testset2.add("e");
testset2.add("P");
testset2.add("I");
testset2.add("E");

System.out.println("testset.union(testset2): " + testset.union(testset2));
System.out.println("testset.intersection(testset2): " + testset.intersection(testset2));
System.out.println("testset.difference(testset2): " + testset.difference(testset2));



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

import java.util.Iterator;

public class BSTSet<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;

/**
* @param key key to be added into the set
*/
@Override
public void add(Object key) {
root = add(root,key);

}

private Node add( Node current, KeyType key) {
if (current == null) {
return new Node(key, 1);
}

int x = key.compareTo(current.key);

if (x < 0) {
current.left = add(current.left, key);
} else if (x > 0) {
current.right = add(current.right, key);
} else {
current.key = key;
}

current.N = size(current.left) + size(current.right) + 1;

return current;

}

/**
* @param key key to check
* @return true if key is in the set, false otherwise
*/
@Override
public boolean contains(Object key) {
Node current = root;
while(current != null){
int x = key.compareTo(current.key);
if(x < 0){
current = current.left;
}else if(x > 0){
current = current.right;
}else {
return true;
}
}
return false;
}


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

/**
* @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 size(current.left) + size(current.right) + 1;
}
}

/**
* @param other specified set to union
* @return the union of this set with other
*/
@Override
public MathSet union(MathSet other) {
MathSet<KeyType> result = new BSTSet<KeyType>();

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

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

/**
* @param other specified set to intersect
* @return the intersection of this set with other
*/
@Override
public MathSet intersection(MathSet other) {
MathSet<KeyType> result = new BSTSet<KeyType>();

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

/**
* @param other specified set to difference
* @return the difference of this set with other
*/
@Override
public MathSet difference(MathSet other) {
MathSet<KeyType> result = new BSTSet<KeyType>();

Iterator<KeyType> itr = (Iterator<KeyType>) this.keys();

while (itr.hasNext()) {
KeyType currentKey = itr.next();
if (!other.contains(currentKey)) {
result.add(currentKey);
}
}

return result;
}

/**
* @return a collection of all keys in this set
*/
@Override
public Iterable keys() {
Queue<KeyType> queue = new Queue<>();
inorder(root,queue);
return queue;
}
}