forked from GreenRiverCollege-SDEV333/FinalProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/zhenhuaizeng/FinalProject
- Loading branch information
Showing
4 changed files
with
181 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import edu.greenriver.sdev333.MathSet; | ||
import edu.greenriver.sdev333.BSTSet; | ||
|
||
import java.util.HashSet; | ||
|
||
|
||
public class FlightRoutesGraph | ||
{ | ||
// two sets need to model a graph (network) | ||
// 1. a set of vertices (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 HashSet<>(); // 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 flgihts from JFK | ||
MathSet<String> directJFK = g.getNeighbors("JFK"); | ||
MathSet<String> directFromATL = g.getNeighbors("ATL"); | ||
|
||
|
||
|
||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package edu.greenriver.sdev333; | ||
|
||
public class HashSet | ||
{ | ||
|
||
|
||
private Node first; | ||
private int size = 0; | ||
private class Node <KeyType, ValueType> implements SymbolTable<KeyType, ValueType> | ||
{ //Linked-list node | ||
private KeyType key; | ||
private ValueType val; | ||
private Node next; | ||
public Node(KeyType key, ValueType val, Node next) | ||
{ | ||
this.key = key; | ||
this.val = val; | ||
this.next = next; | ||
} | ||
|
||
} | ||
@Override | ||
public void put(KeyType key, ValueType value) { | ||
//Search for key, Update value if found; grow table if new | ||
for(Node x = first; x != null; x = x.next) | ||
{ | ||
if(key.equals(x.key)) | ||
{ | ||
x.val = value; // Search hit: update val. | ||
return; | ||
} | ||
} | ||
first = new Node(key,value,first); //Search miss: add new node. | ||
size++; | ||
} | ||
|
||
@Override | ||
public ValueType get(KeyType key) { | ||
//Search for key, return associated value. | ||
for(Node x = first; x != null; x = x.next) | ||
{ | ||
if(key.equals(x.key)) | ||
{ | ||
return x.val; // search hit | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return size; | ||
} | ||
|
||
@Override | ||
public Iterable<KeyType> keys() { | ||
Queue<KeyType> queue = new Queue<>(); | ||
Node current = first; | ||
while(current != null) | ||
{ | ||
queue.enqueue(current.key); | ||
current = current.next; | ||
} | ||
|
||
return queue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters