public SimpleVertex <T> DepthFirstTraversal(SimpleVertex <T> root, T searchVal) { item = null; visited = new List <SimpleVertex <T> >(); DepthFirstTraversalHelper(root, searchVal); return(item); }
public SimpleVertex <T> BreadthFirstTraversal(SimpleVertex <T> root, T searchVal) { visited = new List <SimpleVertex <T> >(); q = new Queue <SimpleVertex <T> >(); q.Enqueue(root); while (q.Count != 0) { var n = q.Peek(); if (n.val.CompareTo(searchVal) == 0) { return(n); } q.Dequeue(); foreach (var i in n.neighbors) { if (!visited.Contains(i)) { q.Enqueue(i); } } visited.Add(n); } Console.WriteLine("hi!"); return(null); }
public void AddEdge(SimpleVertex <T> a, SimpleVertex <T> b) { SimpleVertex <T> aVertex = vertices.Find(i => i == a); SimpleVertex <T> bVertex = vertices.Find(i => i == b); aVertex.neighbors.Add(bVertex); bVertex.neighbors.Add(aVertex); }
public void RemoveVertex(SimpleVertex <T> vertex) { foreach (SimpleVertex <T> possibleNeighbor in vertices) //any better way to do this? Is there a List.Map? Can I use List.ConvertAll? { if (possibleNeighbor.neighbors.Contains(vertex)) { possibleNeighbor.neighbors.Remove(vertex); } } vertices.Remove(vertex); }
public SimpleVertex <T> AddVertex(SimpleVertex <T> newVertex) { vertices.Add(newVertex); foreach (SimpleVertex <T> i in vertices) { if (i == newVertex) { return(i); } } throw new Exception("Can't find the newly added Vertex!"); }
void DepthFirstTraversalHelper(SimpleVertex <T> thisNode, T searchVal) { if (item != null) { return; } foreach (SimpleVertex <T> n in thisNode.neighbors) { if (visited.Contains(n)) { continue; } if (n.val.CompareTo(searchVal) == 0) { item = n; return; } visited.Add(n); DepthFirstTraversalHelper(n, searchVal); } }
public bool HasVertex(SimpleVertex <T> vertex) => vertices.Contains(vertex);