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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# FinalProject
75 changes: 75 additions & 0 deletions src/FlightRoutesGraph.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import edu.greenriver.sdev333.BSTSet;
import edu.greenriver.sdev333.MathSet;
import edu.greenriver.sdev333.SCHashSet;

public class FlightRoutesGraph {
// two sets needed to modal a graph (network)
// 1. a set of verticals (points, nodes) - airports
// 2. a set of edges (connections, lines, relationships) - route between airports

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<>(); // BST ok here b/c strings are comparable
edges = new SCHashSet<>(); // must use HashSet here b/c edges are not comparable
}

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

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

MathSet<String> getNeighbors(String city) {
// create an empty set to hold the results
MathSet<String> neighbors = new BSTSet<>();

// loop through the edges and check
// if the city is either in node1 or node2
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();

// add all the cities first (nodes)
g.addNode("JFK");
g.addNode("ORD");
g.addNode("ATL");
g.addNode("MCO");
g.addNode("DEN");

// add connections between cities (edges, routes)
g.addEdge("JFK","MCO");
g.addEdge("ATL","MCO");
g.addEdge("DEN","ORD");
g.addEdge("ORD","ATL");
// more to go if you want...

//look for direct flights from JFK
MathSet<String> directJFK = g.getNeighbors("JFK");
MathSet<String> directFromATL = g.getNeighbors("ATL");
}
}
19 changes: 19 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import edu.greenriver.sdev333.BSTSet;
import edu.greenriver.sdev333.MathSet;

import java.util.Scanner;

/**
* @Junny Noriega-Arenas
* Final Project Part 1
* SDEV 333
* Description: Create two classes that implements the MathSet interface, which represents a mathematical set.
*/

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

String inputString = "S E A R C H E X A M P L E";

Scanner input = new Scanner(inputString);

MathSet<String> st = new BSTSet<>();

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

import static javax.swing.UIManager.get;

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) {
// starts the recursion
root = add(root, key);

}
public Node add(Node current, KeyType key) {
// current is the root of the subtree we are looking at

// we are at where we are supposed to be
if (current == null) {
return new Node(key,1);
}

int cmp = key.compareTo(current.key);
// cmp will be -1 (negative) if key < current.key
// cmp will be 0 (zero) if key == current.key
// cmp will be +1 (positive) if key > current.key

if (cmp < 0) {
// go left
current.left = add(current.left,key);
}
else if (cmp > 0) {
// go right
current.right = add(current.right,key);
}
else {
// key already exists, replace the data(val)
current.key = key;
}

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) {
return get(key) != null;
}

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

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

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

/**
* 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() {
return null;
}
}
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 junnynoriega-arenas
* FIFO queue, page 151 of the red book
*/
public class Queue<ItemType> implements Iterable<ItemType> {
Expand Down
99 changes: 99 additions & 0 deletions src/edu/greenriver/sdev333/SCHashSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package edu.greenriver.sdev333;

public class SCHashSet<KeyType extends Comparable<KeyType>> implements MathSet<KeyType> {
// field
private int M;

/**
* Puts the specified key into the set.
*
* @param key key to be added into the set
*/
@Override
public void add(KeyType key) {

}

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

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

/**
* Number of keys in the set
*
* @return number of keys in the set.
*/
@Override
public int size() {
return 0;
}

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

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

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

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