2016-04-28 111 views
0

我想在文本文件上实现深度优先搜索和宽度优先搜索。代码应该跳过文本文件中的单词,但是在单词之后的字母上执行深度优先搜索和广度优先搜索。我收到一个不兼容的类型错误。以下是我的代码和文本文件。深度优先搜索和图形上的宽度优先搜索

PROG

  Scanner myScanner = new Scanner(new File(args[0])); 
      Graph obj = new Graph(); 
      while(myScanner.hasNextLine()) 
      { 
       if(myScanner.hasNext("add")) 
       { 
        Node[] n1=new Node[1]; 
        Node[] n2=new Node[2]; 
        line 23 **obj.add(n1);** 
        obj.add(n2); 
        obj.connectNode(n1,n2); 
       } 

       if(myScanner.hasNext("breadth")) 
       { 
        obj.bfs(); 
       } 

       if(myScanner.hasNext("depth")) 
       { 
        obj.dfs(); 
       } 

       if(myScanner.hasNext("remove")) 
       { 
        Node[] n1=new Node[1]; 
        Node[] n2=new Node[2]; 
        obj.remove(); 
        obj.remove(); 
        obj.connectNode(n1,n2); 
       } 

      } 
     } 
} 

Graph.java

public class Graph 
{ 
    public Node rootNode; 
    public ArrayList nodes=new ArrayList(); 
    public int[][] adjMatrix;//Edges will be represented as adjacency Matrix 
    int size; 
    public void setRootNode(Node n) 
    { 
     this.rootNode=n; 
    } 

    public Node getRootNode() 
    { 
     return this.rootNode; 
    } 

    public void add(Node n) 
    { 
     nodes.add(n); 
    } 


    //This method will be called to make connect two nodes 
    public void connectNode(Node start,Node end) 
    { 
     if(adjMatrix==null) 
     { 
      size=nodes.size(); 
      adjMatrix=new int[size][size]; 
     } 

     int startIndex=nodes.indexOf(start); 
     int endIndex=nodes.indexOf(end); 
     adjMatrix[startIndex][endIndex]=1; 
     adjMatrix[endIndex][startIndex]=1; 
    } 

    private Node getUnvisitedChildNode(Node n) 
    { 

     int index=nodes.indexOf(n); 
     int j=0; 
     while(j<size) 
     { 
      if(adjMatrix[index][j]==1 &&  ((Node)nodes.get(j)).visited==false) 
      { 
       return (Node)nodes.get(j); 
      } 
      j++; 
     } 
     return null; 
    } 

    //BFS traversal of a tree is performed by the bfs() function 
    public void bfs() 
    { 
     //BFS uses Queue data structure 
     Queue q=new LinkedList(); 
     q.add(this.rootNode); 
     printNode(this.rootNode); 
     rootNode.visited=true; 
     while(!q.isEmpty()) 
     { 
      Node n=(Node)q.remove(); 
      Node child=null; 
      while((child=getUnvisitedChildNode(n))!=null) 
      { 
       child.visited=true; 
       printNode(child); 
       q.add(child); 
      } 
     } 
     //Clear visited property of nodes 
     remove(); 
    } 

    //DFS traversal of a tree is performed by the dfs() function 
    public void dfs() 
    { 
     //DFS uses Stack data structure 
     Stack s=new Stack(); 
     s.push(this.rootNode); 
     rootNode.visited=true; 
     printNode(rootNode); 
     while(!s.isEmpty()) 
     { 
      Node n=(Node)s.peek(); 
      Node child=getUnvisitedChildNode(n); 
      if(child!=null) 
      { 
       child.visited=true; 
       printNode(child); 
       s.push(child); 
      } 
      else 
      { 
       s.pop(); 
      } 
     } 
     //Clear visited property of nodes 
     remove(); 
    } 


    //Utility methods for clearing visited property of node 
    void remove() 
    { 
     int i=0; 
     while(i<size) 
     { 
      Node n=(Node)nodes.get(i); 
      n.visited=false; 
      i++; 
     } 
    } 

    //Utility methods for printing the node's label 
    private void printNode(Node n) 
    { 
     System.out.print(n.label+" "); 
    } 

} 

Node.java

public class Node 
{ 
    public char label; 
    public boolean visited=false; 
    public Node(char l) 
    { 
     this.label=l; 
    } 
} 

input.txt中

add A B 
add A C 
add B D 
add D E 
add E A 
add E B 
breadth A 
depth A 
remove A B 
add B A 
breadth B 
depth B 
+0

向我们显示实际的错误。 –

+0

堆栈跟踪和线路发生错误?考虑格式化代码并清理额外的空间。这使其他人更容易阅读。 –

+0

运行: 线程“main”中的异常java.lang.RuntimeException:不可编译的源代码 - 错误的sym类型:prog2.Graph.add \t at prog2.Prog2.main(Prog2.java:23) – pinkcouture

回答

0

add(Node node)connectNode(Node start,Node end)接收类Node不是数组节点的[]你路过这这里的对象:

    Node[] n1=new Node[1]; 
        Node[] n2=new Node[2]; 
        obj.add(n1); 
        obj.add(n2); 
        obj.connectNode(n1,n2); 
+0

我会如何传递这个类的对象? – pinkcouture

+0

你想做什么?为什么你首先在那里实例化一个数组? – raven

0

你在你的23行做.add(Node[] nodes),instaed所需.add(Node node)的。

您应该实例新节点,如:

Node node1 = new Node(char1); //some character that you need to pass in according to your code 
Node node2 = new Node(char2); 
obj.add(node1); 
obj.add(node2); 
obj.connectNode(node1, node2); 

同去的删除,不初始化节点的数组。