package model;

import java.util.HashSet;

//a tree contains a set of edges
public class Tree {
	private HashSet<Edge> edges = new HashSet<Edge>();
	
	public void add(Edge e) {
		edges.add(e);
	}
	
	public void remove(Edge e) {
		edges.remove(e);
	}
	
	public HashSet<Edge> getEdges() {
		return edges;
	}
	
	// contains edge or reverse-edge
	public boolean contains(Edge edge) {
		return edges.contains(edge);
	}
	
	// does the edge touch the tree
	public boolean connects(Edge edge) {
		for (Edge e : edges) {
			if (edge.getV1() == e.getV1() || edge.getV1() == e.getV2()) return true;
			if (edge.getV2() == e.getV1() || edge.getV2() == e.getV2()) return true;
		}
		return false;
	}
	
	// do both edges touch the tree
	public boolean connectsBoth(Edge edge) {
		int c = 0;
		for (Edge e : edges) {
			if (edge.getV1() == e.getV1() || edge.getV1() == e.getV2()) c++;
			if (edge.getV2() == e.getV1() || edge.getV2() == e.getV2()) c++;
			
			if (c >= 2) {
				// both edges touch the tree
				return true;
			}
		}
		return false;
	}
	
	// number of edges
	public int size() {
		return edges.size();
	}
}

@@ -720,60 +720,154 @@
 ee%0A%09
-public boolean connectsBoth(Edge edge) %7B%0A%09%09int c = 0
+// use a hashset to ensure vertices aren't checked twice%0A%09public boolean connectsBoth(Edge edge) %7B%0A%09%09HashSet%3CVertex%3E match = new HashSet%3CVertex%3E()
 ;%0A%09%09
@@ -948,27 +948,47 @@
  e.getV2()) 
-c++
+match.add(edge.getV1())
 ;%0A%09%09%09if (edg
@@ -1044,11 +1044,31 @@
 ()) 
-c++
+match.add(edge.getV2())
 ;%0A%09%09
@@ -1076,17 +1076,28 @@
 %0A%09%09%09if (
-c
+match.size()
  %3E= 2) %7B

fix connectsBoth in certain situations