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.

Binary file not shown.
Binary file not shown.
Binary file added out/production/FinalProject/Main.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
89 changes: 89 additions & 0 deletions src/FlightRoutesGraph.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import edu.greenriver.sdev333.BSTSet;
import edu.greenriver.sdev333.HashingSet;
import edu.greenriver.sdev333.MathSet;

public class FlightRoutesGraph {
//two sets needed to model graph/network
//1. set of vertices (points, nodes, etc) -airports
//2. set of edges (where the points connect with each other) -routes

private class Edge{
private String city1;
private String city2;

public Edge(String one, String two){
this.city1 = one;
this.city2 = two;
}
}

private MathSet<String> nodes;
private MathSet<Edge> connections;

public FlightRoutesGraph(){
nodes = new BSTSet<>();
connections = new HashingSet<>();
}

public void addNode(String city){
nodes.add(city);
}
public void addEdge(String city1, String city2){
connections.add(new Edge(city1, city2));
}
public MathSet<String> getNeighbors(String city){
//empty set to hold results
MathSet<String> neighbors = new BSTSet<>();
//loop through edges looking for node value
for (Edge e: connections.keys()) {
if(e.city1.equals(city)){
neighbors.add(e.city2);
} else if (e.city2.equals(city)) {
neighbors.add(e.city1);
}
}
return neighbors;
}


public static void main(String[] args) {
FlightRoutesGraph g = new FlightRoutesGraph();
//add cities
g.addNode("SEA");
g.addNode("JFK");
g.addNode("ORD");
g.addNode("ATL");
g.addNode("MCO");
g.addNode("DEN");
g.addNode("NED");
g.addNode("ASE");
g.addNode("PLD");
//duplicate
g.addNode("PLD");

//add connections
g.addEdge("JFK", "MCO");
g.addEdge("ATL", "MCO");
g.addEdge("DEN", "ORD");
g.addEdge("ORD", "ATL");
g.addEdge("SEA", "MCO");
g.addEdge("SEA", "JFK");
g.addEdge("JFK", "ATL");

g.addEdge("JFK", "PLD");
g.addEdge("ASE", "MCO");
g.addEdge("DEN", "NED");
g.addEdge("JFK", "NED");
g.addEdge("SEA", "ASE");
g.addEdge("SEA", "DEN");
//duplicate
g.addEdge("JFK", "ATL");

//look for direct flights from JFK
MathSet<String> directJFK = g.getNeighbors("JFK");
for (String a: directJFK.keys()) {
System.out.println(a);
}

}
}
58 changes: 57 additions & 1 deletion src/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,61 @@
import edu.greenriver.sdev333.BSTSet;
import edu.greenriver.sdev333.HashingSet;
import edu.greenriver.sdev333.MathSet;


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

System.out.println("ISEMPTY SET ONE ------------");
MathSet<String> one = new HashingSet<>();
System.out.println(one.isEmpty());
one.add("first");
one.add("second");
one.add("third");
one.add("fourth");
one.add("first");
one.add("tenth");
one.add("ninth");
one.add("eleventh");
System.out.println(one.isEmpty());

MathSet<String> two = new HashingSet<>();
two.add("second");
two.add("fourth");
two.add("sixth");
two.add("eighth");
two.add("sixth");
two.add("tenth");

//test add
System.out.println("TEST ADD SET TWO -----------");
for (String a: two.keys()) {
System.out.println(a);
}
//test size
System.out.println("SIZE SET ONE ---------------");
System.out.println(one.size());
//test contains
System.out.println("CONTAINS SET ONE -----------");
System.out.println(one.contains("first"));
System.out.println(one.contains("sixth"));
//test union
System.out.println("UNION --------------");
MathSet<String> union = one.union(two);
for (String a: union.keys()) {
System.out.println(a);
}
System.out.println("INTERSECTION--------");
//test intersection
MathSet<String> intersection = one.intersection(two);
for (String a: intersection.keys()) {
System.out.println(a);
}
//test difference
System.out.println("DIFFERENCE----------");
MathSet<String> difference = one.difference(two);
for (String a: difference.keys()) {
System.out.println(a);
}
}
}
188 changes: 188 additions & 0 deletions src/edu/greenriver/sdev333/BSTSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
package edu.greenriver.sdev333;

import java.util.Iterator;

/**
* @author Katherine Watkins
* SDEV 333
* Final Project
* @param <KeyType>
*/

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

/**
* 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){
if(current == null){
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);
}
else{
current.key = key;
}
//update N
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) {
for(KeyType currentKey: this.keys()){
if(currentKey.equals(key)){
return true;
}
}
return false;
}

/**
* Is the set empty?
*
* @return true if the set is empty, false otherwise
*/
@Override
public boolean isEmpty() {
return this.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 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) {
MathSet<KeyType> result = new BSTSet<>();
for (KeyType key : other.keys()) {
result.add(key);
}
for (KeyType key : this.keys()) {
result.add(key);
}
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<>();
for (KeyType key : other.keys()) {
if(this.contains(key)){
result.add(key);
}
}
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 empty set to hold result
MathSet<KeyType> result = new BSTSet<KeyType>();

for (KeyType a: this.keys()) {
if(!other.contains(a)){
result.add(a);
}
}
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() {
//empty queue to hold my result

Queue<KeyType> queue = new Queue<>();
//start recursion
inOrder(root, queue);
//when done return the queue
return queue;
}
private void inOrder(Node current, Queue<KeyType> q){
if(current == null){
//do nothing - intentionally blank
return;
}

inOrder(current.left, q);
q.enqueue(current.key);
inOrder(current.right, q);
}
}
Loading