Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenhuaizeng committed Mar 15, 2023
2 parents 8ea6d73 + cbc0028 commit e82d5c6
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 24 deletions.
109 changes: 109 additions & 0 deletions src/FlightRoutesGraph.java
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");



}


}
4 changes: 2 additions & 2 deletions src/edu/greenriver/sdev333/BSTSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.Iterator;

// page 398
public class BSTSet<KeyType> implements MathSet<KeyType>
public class BSTSet<KeyType extends Comparable<KeyType>> implements MathSet<KeyType>
{
private class Node{
private KeyType key;
Expand Down Expand Up @@ -31,7 +31,7 @@ public Node(KeyType key, int N)
@Override
public void add(KeyType key)
{

//TODO
}

/**
Expand Down
67 changes: 67 additions & 0 deletions src/edu/greenriver/sdev333/HashSet.java
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;
}
}
25 changes: 3 additions & 22 deletions src/edu/greenriver/sdev333/Queue.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.Iterator;

/**
* ZhenHuai Zeng
* FIFO queue, page 151 of the red book
*/
public class Queue<ItemType> implements Iterable<ItemType> {
Expand All @@ -13,40 +12,25 @@ private class Node {
private Node next;
}

// fields
// fields:
private Node first;
private Node last;
private int size;

/**
* Constructor. Creates an empty queue.
*/
public Queue() {
first = null;
last = null;
size = 0;
}

/**
* Check if the queue is empty.
* @return true if the queue is empty, false otherwise
*/
public boolean isEmpty() {
return first == null;
}

/**
* Number of items in the queue.
* @return the number of items in the queue
*/
public int size() {
return size;
}

/**
* Adds a specified item to the rear of the queue.
* @param item item to be added
*/
public void enqueue(ItemType item) {
Node oldlast = last;
last = new Node();
Expand All @@ -63,10 +47,6 @@ public void enqueue(ItemType item) {
size++;
}

/**
* Removes and returns the item at the front of the queue.
* @return the item that was at the front of the queue before it was removed
*/
public ItemType dequeue() {
ItemType item = first.data;
first = first.next;
Expand All @@ -79,6 +59,7 @@ public ItemType dequeue() {

/**
* Returns an iterator over elements of type {@code T}.
*
* @return an Iterator.
*/
@Override
Expand All @@ -105,4 +86,4 @@ public ItemType next() {
return item;
}
}
}
}

0 comments on commit e82d5c6

Please sign in to comment.