Hi all,

I need to utilize a Graph Data structure for an assignment. I've understood the following code (posted below) until I came to the search function. The depthFirstSearch and breadthFirstSearch both require a Visitor<T> which is an interface. I'm not much used to interfaces, so I'm not sure where to start in utilizing these functions.

If someone could help me understand how I can build an interface that implements Visitor, it would be much appreciated.

depthFirstSearch function

   public <E extends Exception> void depthFirstSearch(Vertex<T> v, VisitorEX<T, E> visitor) throws E{
       if( visitor != null ){
         visitor.visit(this, v);   
     }
  v.visit();
  for (int i = 0; i < v.getOutgoingEdgeCount(); i++) {
     Edge<T> e = v.getOutgoingEdge(i);
     if (!e.getTo().visited()){
        depthFirstSearch(e.getTo(), visitor);
     }
  }
 }

Visitor Interface

interface Visitor<T> {

  public void visit(Graph<T> g, Vertex<T> v);
  }


 interface VisitorEX<T, E extends Exception> {

    public void visit(Graph<T> g, Vertex<T> v) throws E;
  }

Thanks in advance

Recommended Answers

All 2 Replies

For some examples look at the API doc for java.util.List (an interface) and then at a class that implements it: ArrayList

Great thank you very much :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.