From 5c8409330640ffccd5ab6f893118d7c948aa7f73 Mon Sep 17 00:00:00 2001 From: bhuynh-grc Date: Fri, 10 Mar 2023 21:25:16 -0800 Subject: [PATCH 1/2] Added name to Queue and MathSet --- src/edu/greenriver/sdev333/MathSet.java | 1 + src/edu/greenriver/sdev333/Queue.java | 1 + 2 files changed, 2 insertions(+) diff --git a/src/edu/greenriver/sdev333/MathSet.java b/src/edu/greenriver/sdev333/MathSet.java index 4273aba..98f6905 100644 --- a/src/edu/greenriver/sdev333/MathSet.java +++ b/src/edu/greenriver/sdev333/MathSet.java @@ -1,6 +1,7 @@ package edu.greenriver.sdev333; /** + * Bao Huynh * A MathSet represents a finite mathematical set. * Sets have a collection of unique elements (keys) - no duplicate keys allowed. * Set operations include contains, size, union, intersection, and difference. diff --git a/src/edu/greenriver/sdev333/Queue.java b/src/edu/greenriver/sdev333/Queue.java index 638d71c..9e00c57 100644 --- a/src/edu/greenriver/sdev333/Queue.java +++ b/src/edu/greenriver/sdev333/Queue.java @@ -3,6 +3,7 @@ import java.util.Iterator; /** + * Bao Huynh * FIFO queue, page 151 of the red book */ public class Queue implements Iterable { From 6cb88eab7806f42f43ee2422c03184e052594c5a Mon Sep 17 00:00:00 2001 From: bhuynh-grc Date: Wed, 15 Mar 2023 22:12:49 -0700 Subject: [PATCH 2/2] Final Project --- .idea/uiDesigner.xml | 124 +++++++++++ .idea/vcs.xml | 6 + src/FlightRoutesGraph.java | 73 +++++++ src/Main.java | 149 ++++++++++++- src/edu/greenriver/sdev333/BSTSet.java | 199 ++++++++++++++++++ src/edu/greenriver/sdev333/HashSet.java | 14 ++ src/edu/greenriver/sdev333/Queue.java | 6 +- .../sdev333/SeparateChainingHashTable.java | 159 ++++++++++++++ 8 files changed, 727 insertions(+), 3 deletions(-) create mode 100644 .idea/uiDesigner.xml create mode 100644 .idea/vcs.xml create mode 100644 src/FlightRoutesGraph.java create mode 100644 src/edu/greenriver/sdev333/BSTSet.java create mode 100644 src/edu/greenriver/sdev333/HashSet.java create mode 100644 src/edu/greenriver/sdev333/SeparateChainingHashTable.java diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/FlightRoutesGraph.java b/src/FlightRoutesGraph.java new file mode 100644 index 0000000..17c4bf1 --- /dev/null +++ b/src/FlightRoutesGraph.java @@ -0,0 +1,73 @@ +import edu.greenriver.sdev333.BSTSet; +import edu.greenriver.sdev333.MathSet; + +import java.util.HashMap; +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 + + private class Edge{ + private String node1; + private String node2; + + public Edge(String from, String to){ + node1 = from; + node2 = to; + } + } + private MathSet nodes; + private MathSet edges; + + public FlightRoutesGraph(){ + nodes = new BSTSet<>(); + // edges = new HashSet<>(); + } + + 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 getNeighbors(String city){ + MathSet 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", "ALT"); + + //look for direct flights from JFK + MathSet directFromJFK = g.getNeighbors("JFK"); + MathSet directFromATL = g.getNeighbors("ATL"); + + } +} diff --git a/src/Main.java b/src/Main.java index 3e59c38..1b43b0a 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,152 @@ +import edu.greenriver.sdev333.*; + +/** + * @author Bao Huynh + * Date: 03/15/2023 + * Class: SDEV333 + * FINAL PROJECT + */ public class Main { public static void main(String[] args) { - 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 different + + MathSet set1 = new BSTSet<>(); + MathSet set2 = new BSTSet<>(); + + //add elements to the sets + set1.add("1"); + set1.add("2"); + set1.add("3"); + set1.add("4"); + set1.add("5"); + + set2.add("1"); + set2.add("6"); + set2.add("7"); + + System.out.print("SET A: "); + for(String num : set1.keys()){ + System.out.print(num + " "); + } + + + System.out.print("\nSET B: "); + for(String num : set2.keys()){ + System.out.print(num + " "); + } + + //Test contains + System.out.println("\nSET A contains number 1? " + set1.contains("1")); + System.out.println("SET B contains number 1? " + set2.contains("1")); + + //Test size + System.out.println("SET A size: " + set1.size()); + System.out.println("SET B size: " + set2.size()); + + //Test isEmpty + System.out.println("SET A empty? " + set1.isEmpty()); + System.out.println("SET B empty? " + set2.isEmpty()); + + //Test Union + //union set a and set b, save into result1 + MathSet result1 = set1.union(set2); + System.out.print("UNION SET A and SET B: "); + for (String num : result1.keys()){ + System.out.print(num + " "); + } + + //Test Intersection + MathSet result2 = set1.intersection(set2); + System.out.print("\nINTERSECTION SET A and SET B: "); + for (String num : result2.keys()){ + System.out.print(num + " "); + } + + //Test Different + MathSet result3 = set1.difference(set2); + System.out.print("\nDIFFERENCE SET A and SET B: "); + for (String num : result3.keys()){ + System.out.print(num + " "); + } + + MathSet result4 = set2.difference(set1); + System.out.print("\nDIFFERENCE SET B and SET A: "); + for (String num : result4.keys()){ + System.out.print(num + " "); + } + + System.out.println("\n------------------------------------------"); + MathSet set3 = new SeparateChainingHashTable<>(20); + MathSet set4 = new BSTSet<>(); + + set3.add("Dog"); + set3.add("Cat"); + set3.add("Chicken"); + + set4.add("Fish"); + set4.add("Pig"); + set4.add("Wolf"); + set4.add("Chicken"); + set4.add("Bird"); + + System.out.print("SET C: "); + for(String num : set3.keys()){ + System.out.print(num + " "); + } + + + System.out.print("\nSET D: "); + for(String num : set4.keys()){ + System.out.print(num + " "); + } + + //Test contains + System.out.println("\nSET C contains Fish? " + set3.contains("Fish")); + System.out.println("SET D contains Fish? " + set4.contains("Fish")); + + //Test size + System.out.println("SET C size: " + set3.size()); + System.out.println("SET D size: " + set4.size()); + + //Test isEmpty + System.out.println("SET C empty? " + set3.isEmpty()); + System.out.println("SET D empty? " + set4.isEmpty()); + + //Test Union + //union set c and set d, save into result5 + MathSet result5 = set3.union(set4); + System.out.print("UNION SET C and SET D: "); + for (String num : result5.keys()){ + System.out.print(num + " "); + } + + //Test Intersection + MathSet result6 = set3.intersection(set4); + System.out.print("\nINTERSECTION SET C and SET D: "); + for (String num : result6.keys()){ + System.out.print(num + " "); + } + + //Test Different + MathSet result7 = set3.difference(set4); + System.out.print("\nDIFFERENCE SET C and SET D: "); + for (String num : result7.keys()){ + System.out.print(num + " "); + } + + MathSet result8 = set4.difference(set3); + System.out.print("\nDIFFERENCE SET D and SET C: "); + for (String num : result8.keys()){ + System.out.print(num + " "); + } + + + + } } \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/BSTSet.java b/src/edu/greenriver/sdev333/BSTSet.java new file mode 100644 index 0000000..af67c43 --- /dev/null +++ b/src/edu/greenriver/sdev333/BSTSet.java @@ -0,0 +1,199 @@ +package edu.greenriver.sdev333; + +import java.util.Iterator; + +import javax.swing.plaf.PanelUI; +import java.security.Key; + +/** + * @author Bao Huynh + * Date: 03/15/2023 + * Class: SDEV333 + * FINAL PROJECT + * @param + */ + +public class BSTSet> implements MathSet { + + //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) { + root = add(root, key); + } + private Node add(Node current, KeyType key){ + //if the current node is null + if (current == null){ + return new Node(key,1); + } + //compare the key to the current nodes key + int cmp = key.compareTo(current.key); + + //less than the current->add to the left + if(cmp < 0){ + current.left = add(current.left, key); + } else if (cmp > 0) { //greater than current->add to the right + current.right = add(current.right, key) +; }else{ + return current; + } + + //update + 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) { + Node current = root; + while (current != null){ + int cmp = key.compareTo(current.key); + if(cmp < 0){ + current = current.left; + } else if (cmp > 0) { + current = current.left; + }else{ + return true; //found with the same key + } + } + //not found + return false; + } + + /** + * Is the set empty? + * + * @return true if the set is empty, false otherwise + */ + @Override + public boolean isEmpty() { + return root == null; + } + + /** + * 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; + } + 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 union(MathSet other) { + MathSet result = new BSTSet<>(); + for(KeyType currentKey : this.keys()){ + result.add(currentKey); + } + for(KeyType key : other.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 intersection(MathSet other){ + MathSet result = new BSTSet<>(); + for(KeyType currentKey : this.keys()){ + if(other.contains(currentKey)){ + result.add(currentKey); + } + } + 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 difference(MathSet other) { + //create an empty set that will hold the result + MathSet result = new BSTSet(); + + 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 keys() { + Queue queue = new Queue<>(); + + inOrder(root, queue); + return queue; + } + + private void inOrder(Node current, Queue q) { + if(current == null){ + return; + } + inOrder(current.left,q); + q.enqueue(current.key); + inOrder(current.right, q); + } +} diff --git a/src/edu/greenriver/sdev333/HashSet.java b/src/edu/greenriver/sdev333/HashSet.java new file mode 100644 index 0000000..97585d6 --- /dev/null +++ b/src/edu/greenriver/sdev333/HashSet.java @@ -0,0 +1,14 @@ +package edu.greenriver.sdev333; + +public class HashSet { + //Used SeparateChainingHashST as model/example + //^^^^ CODE manages the "buckets" of the hash table + + //vvvvv code manages the insides of the buckets of the hash table + // a linked list is inside each bucket + //SeparateChainingHashST depends on having SequentialSearchST + // will need to either bring in SequentialSearchST + //or write your own linked list + + +} diff --git a/src/edu/greenriver/sdev333/Queue.java b/src/edu/greenriver/sdev333/Queue.java index 9e00c57..a5002b0 100644 --- a/src/edu/greenriver/sdev333/Queue.java +++ b/src/edu/greenriver/sdev333/Queue.java @@ -3,8 +3,10 @@ import java.util.Iterator; /** - * Bao Huynh - * FIFO queue, page 151 of the red book + * @author Bao Huynh + * Date: 03/15/2023 + * Class: SDEV333 + * FINAL PROJECT */ public class Queue implements Iterable { // private helper node class: diff --git a/src/edu/greenriver/sdev333/SeparateChainingHashTable.java b/src/edu/greenriver/sdev333/SeparateChainingHashTable.java new file mode 100644 index 0000000..0786d25 --- /dev/null +++ b/src/edu/greenriver/sdev333/SeparateChainingHashTable.java @@ -0,0 +1,159 @@ +package edu.greenriver.sdev333; +import java.security.Key; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.Queue; + +public class SeparateChainingHashTable> implements MathSet { + private LinkedList[] st; + private int M; //number of buckets + + public SeparateChainingHashTable(int M){ + this.M = M; + //make array + st = new LinkedList[M]; + for(int i = 0; i < M; i++){ + st[i] = new LinkedList<>(); + } + } + + private int hash(KeyType key){ + return (keys().hashCode() & 0x7fffffff) % M; + } + + /** + * Puts the specified key into the set. + * + * @param key key to be added into the set + */ + @Override + public void add(KeyType key) { + int i = hash(key); + //check if the key exist + if(st[i].contains(key)){ + return; + } + //add the key + st[i].add(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) { + int i = hash(key); + LinkedList l = st[i]; + for(KeyType element : l){ + if(element.equals(key)){ + return true; + } + } + return false; + } + + /** + * 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() { + int count = 0; + for(int i = 0; i < M; i++){ + count += st[i].size(); + } + return count; + } + + /** + * 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 union(MathSet other) { + MathSet result = new BSTSet(); + for(KeyType currentKey : this.keys()){ + result.add(currentKey); + } + for(KeyType currentKey : other.keys()){ + result.add(currentKey); + } + 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 intersection(MathSet other) { + MathSet result = new BSTSet<>(); + for(KeyType currentKey : keys()){ + if(other.contains(currentKey)); + result.add(currentKey); + } + 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 difference(MathSet other) { + MathSet result = new BSTSet<>(); + for(KeyType currentKey : 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 keys() { + LinkedList l = new LinkedList<>(); + for(int i = 0; i < M; i++){ + for(KeyType k : st[i]){ + l.add(k); + } + } + return l; + } + } +