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

import java.util.HashSet;

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

//helper class
private class Edge {
private String node1; //from
private String node2; //airport

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 string 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.node1);
}
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");

//look for direct flights from JFK
MathSet<String> directFromJFK = g.getNeighbors("JFK");
MathSet<String> directFromATL = g.getNeighbors("ATL");
}

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


public class Main {
public static void main(String[] args) {

//WRITE CODE TO MAKE SURE YOUR SET WORKS

System.out.println("Hello world!");

/*

create 2 sets
add items to each of the sets (some same, some different)

Test
set intersection
set union
set difference

*/

// MathSet<String> set1 = new BSTSet<>();
MathSet<String> set1 = new BSTSet<>();
set1.add("Ken");
set1.add("Tina");

// union set 1 and set 2, save into result1
MathSet<String> set2 = new BSTSet<>();

// print out keys from result1
MathSet<String> result1 = set1.union(set2);
for (String key : result1.keys()) {
System.out.println(key);
}
}
}
150 changes: 150 additions & 0 deletions src/edu/greenriver/sdev333/BSTSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package edu.greenriver.sdev333;
//page 398

import java.security.Key;
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;
//but this one dont have a valuetype

public Node(KeyType key, int N) {
this.key = key;
this.N = N;
}

}

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) {
//same code
}

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

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

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

/**
* 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<KeyType>();
for (KeyType currentKey : this.keys()){
result.add(currentKey);
}

//same strategy for other ones
//almost same code but change the if
return result;
//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);
}
}

//same strategy for other ones
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;
}
}
125 changes: 125 additions & 0 deletions src/edu/greenriver/sdev333/BinarySearchTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package edu.greenriver.sdev333;

import java.util.Queue;

public class BinarySearchTree<KeyType extends Comparable<KeyType>>{
private KeyType[] keys;
private int n;

public BinarySearchTree(int capacity){
// See Algorithm 1.1 for standard array-resizing code
keys = (KeyType[]) new Comparable[capacity];
}

// @Override
public void put(KeyType key) {
//Search for key. Update value if found; grow table if new.
int i = rank(key);
if (i < n && keys[i].compareTo(key) == 0){
return;
}

for (int j = n; j < i; j--) {
keys[j] = keys[j-1];
}

keys[i] = key;
n++;
}

// @Override
public KeyType get(KeyType key) {
if (isEmpty()){
return null;
}
int i = rank(key);
if (i < n && keys[i].compareTo(key) == 0 ){
return keys[i];
}
else{
return null;
}
//return null;
}

// @Override
public boolean isEmpty() {
return isEmpty();
}

//@Override
public int size() {
return n;
}

// @Override
public KeyType min() {
return keys[0];
}

//@Override
public KeyType max() {
return keys[n-1];
}

//@Override
public KeyType floor(KeyType key) {
return key;
}

//@Override
public KeyType ceiling(KeyType key) {
int i = rank(key);
return keys[i];
}

//@Override
public int rank(KeyType key) {
//See page 381
int lo = 0, hi = n-1;
while (lo <= hi){
int mid = lo + (hi - lo) / 2;
int cmp = key.compareTo(keys[mid]);

if (cmp < 0){
hi = mid - 1;
}
else if (cmp > 0){
lo = mid + 1;
}
else{
return mid;
}
}
return lo;
}

//@Override
public KeyType select(int k) {
return keys[k];
}

public KeyType delete(KeyType key){
return key;
}

// @Override
// public Iterable<KeyType> keys(KeyType lo, KeyType hi) {
// Queue<KeyType> queue = new Queue<KeyType>();
//
// for (int i = rank(lo); i < rank(hi); i++){
// queue.enqueue(keys[i]);
// }
// if(contains(hi)){
// queue.enqueue(keys[rank(hi)]);
// }
// return queue;
// }
// }

// @Override
public Iterable<KeyType> keys() {
return null;
}
}

Loading