From 87a7e9bb45bb0025f877bdfef232e895c1c66c25 Mon Sep 17 00:00:00 2001 From: j Date: Mon, 13 Mar 2023 20:36:15 -0700 Subject: [PATCH 1/4] working on bstset --- .idea/vcs.xml | 6 ++ src/edu/greenriver/sdev333/BSTSet.java | 98 ++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 .idea/vcs.xml create mode 100644 src/edu/greenriver/sdev333/BSTSet.java 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/edu/greenriver/sdev333/BSTSet.java b/src/edu/greenriver/sdev333/BSTSet.java new file mode 100644 index 0000000..06443d0 --- /dev/null +++ b/src/edu/greenriver/sdev333/BSTSet.java @@ -0,0 +1,98 @@ +package edu.greenriver.sdev333; + +import java.util.Iterator; + +public class BSTSet implements MathSet { + 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; + } + } + + private Node root; + + /** + * @param key key to be added into the set + */ + @Override + public void add(Object key) { + + } + + /** + * @param key key to check + * @return true if key is in the set, false otherwise + */ + @Override + public boolean contains(Object key) { + return false; + } + + /** + * @return true if the set is empty, false otherwise + */ + @Override + public boolean isEmpty() { + return false; + } + + /** + * @return number of keys in the set. + */ + @Override + public int size() { + return 0; + } + + /** + * @param other specified set to union + * @return the union of this set with other + */ + @Override + public MathSet union(MathSet other) { + return null; + } + + /** + * @param other specified set to intersect + * @return the intersection of this set with other + */ + @Override + public MathSet intersection(MathSet other) { + return null; + } + + /** + * @param other specified set to difference + * @return the difference of this set with other + */ + @Override + public MathSet difference(MathSet other) { + MathSet result = new BSTSet(); + + Iterator itr = (Iterator) this.keys(); + + while (itr.hasNext()) { + KeyType currentKey = itr.next(); + if (!other.contains(currentKey)) { + result.add(currentKey); + } + } + + return result; + } + + /** + * @return a collection of all keys in this set + */ + @Override + public Iterable keys() { + return null; + } +} \ No newline at end of file From 3f3cba630b922550efbf212265e93456f71ad751 Mon Sep 17 00:00:00 2001 From: j Date: Wed, 15 Mar 2023 01:45:42 -0700 Subject: [PATCH 2/4] added code from class to file --- src/FlightRoutesGraph.java | 74 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/FlightRoutesGraph.java diff --git a/src/FlightRoutesGraph.java b/src/FlightRoutesGraph.java new file mode 100644 index 0000000..8d89a0b --- /dev/null +++ b/src/FlightRoutesGraph.java @@ -0,0 +1,74 @@ +import edu.greenriver.sdev333.BSTSet; +import edu.greenriver.sdev333.MathSet; + +import java.util.HashSet; + +public class FlightRoutesGraph { + + + + 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); + } + + + + /** + * + * @param city + */ + public MathSet getNeighbors(String city) { + MathSet neighbors = new BSTSet<>(); + + 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(); + g.addNode("JFK"); + g.addNode("ORD"); + g.addNode("ATL"); + g.addNode("MCO"); + g.addNode("DEN"); + g.addEdge("ATL", "MCO"); + g.addEdge("JFK", "MCO"); + g.addEdge("DEN", "ORD"); + g.addEdge("ORD", "ATL"); + MathSet directFromJFK = g.getNeighbors("JFK"); + MathSet directFromATL = g.getNeighbors("ATL"); + + } +} From dc566a34ba519ada84efb21cc0d8521a321f84c2 Mon Sep 17 00:00:00 2001 From: j Date: Wed, 15 Mar 2023 02:46:45 -0700 Subject: [PATCH 3/4] did some work on BSTSet.java --- src/edu/greenriver/sdev333/BSTSet.java | 47 ++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/src/edu/greenriver/sdev333/BSTSet.java b/src/edu/greenriver/sdev333/BSTSet.java index 06443d0..3ae438d 100644 --- a/src/edu/greenriver/sdev333/BSTSet.java +++ b/src/edu/greenriver/sdev333/BSTSet.java @@ -22,6 +22,28 @@ public Node(KeyType key, int N) { */ @Override public void add(Object key) { + root = add(root,key); + + } + + private Node add( Node current, KeyType key) { + if (current == null) { + return new Node(key, 1); + } + + int x = key.compareTo(current.key); + + if (x < 0) { + current.left = add(current.left, key); + } else if (x > 0) { + current.right = add(current.right, key); + } else { + current.key = key; + } + + current.N = size(current.left) + size(current.right) + 1; + + return current; } @@ -31,15 +53,28 @@ public void add(Object key) { */ @Override public boolean contains(Object key) { + Node current = root; + while(current != null){ + int x = key.compareTo(current.key); + if(x < 0){ + current = current.left; + }else if(x > 0){ + current = current.right; + }else { + return true; + } + } + return false; } + /** * @return true if the set is empty, false otherwise */ @Override public boolean isEmpty() { - return false; + return root == null || root.N == 0; } /** @@ -47,7 +82,15 @@ public boolean isEmpty() { */ @Override public int size() { - return 0; + return size(root); + } + + private int size(Node current){ + if(current == null){ + return 0; + } else { + return size(current.left) + size(current.right) + 1; + } } /** From f7745752eb7f7dc651b9438e99da4c45e803fb7a Mon Sep 17 00:00:00 2001 From: j Date: Wed, 15 Mar 2023 03:46:53 -0700 Subject: [PATCH 4/4] final versions of bst and test code in main... --- src/Main.java | 44 +++++++++++++++++++++++++- src/edu/greenriver/sdev333/BSTSet.java | 27 +++++++++++++--- 2 files changed, 66 insertions(+), 5 deletions(-) diff --git a/src/Main.java b/src/Main.java index 3e59c38..5e464fe 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,47 @@ +import edu.greenriver.sdev333.BSTSet; +import edu.greenriver.sdev333.MathSet; +import edu.greenriver.sdev333.SeparateChainingHashTable; +import java.security.Key; + public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + + String testChar = "P O O P I E"; + + MathSet testset = new SeparateChainingHashTableSet<>(); + + Scanner input = new Scanner(testChar); + + System.out.println(testset.isEmpty()); + + while(input.hasNext()){ + String key = input.next(); + testset.add(key); + } + + System.out.println(testset.size()); + System.out.println(testset.contains("P")); + System.out.println(testset.contains("O")); + System.out.println(testset.contains("O")); + System.out.println(testset.contains("P")); + System.out.println(testset.isEmpty()); + System.out.println(); + } + + MathSet testset2 = new SeparateChainingHashTable<>(); + + testset2.add("P"); + testset2.add("e"); + testset2.add("e"); + testset2.add("P"); + testset2.add("I"); + testset2.add("E"); + + System.out.println("testset.union(testset2): " + testset.union(testset2)); + System.out.println("testset.intersection(testset2): " + testset.intersection(testset2)); + System.out.println("testset.difference(testset2): " + testset.difference(testset2)); + + + } \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/BSTSet.java b/src/edu/greenriver/sdev333/BSTSet.java index 3ae438d..5204273 100644 --- a/src/edu/greenriver/sdev333/BSTSet.java +++ b/src/edu/greenriver/sdev333/BSTSet.java @@ -64,7 +64,6 @@ public boolean contains(Object key) { return true; } } - return false; } @@ -99,7 +98,18 @@ private int size(Node current){ */ @Override public MathSet union(MathSet other) { - return null; + MathSet result = new BSTSet(); + + for(KeyType currentKey : this.keys()){ + result.add(currentKey); + } + + for (KeyType currentKey : other.keys()) { + if (!result.contains(currentKey)) { + result.add(currentKey); + } + } + return result; } /** @@ -108,7 +118,14 @@ public MathSet union(MathSet other) { */ @Override public MathSet intersection(MathSet other) { - return null; + MathSet result = new BSTSet(); + + for(KeyType currentKey : this.keys()){ + if(other.contains(currentKey)) { + result.add(currentKey); + } + } + return result; } /** @@ -136,6 +153,8 @@ public MathSet difference(MathSet other) { */ @Override public Iterable keys() { - return null; + Queue queue = new Queue<>(); + inorder(root,queue); + return queue; } } \ No newline at end of file