英文:
methodes for linking nodes
问题
以下是您提供的内容的翻译:
我是一个Java初学者,我应该为我的课程做一个练习,涉及创建一个表示图中节点的泛型类。我实现的方法traverse或connect(或两者)都不能正常工作。该图应该是一个无向图,这意味着两个点之间的每个连接都是双向的。
这是我用于Node类的代码:
import java.util.LinkedList;
public class Node <T>{
    private T value;
    private String name;
    private LinkedList<Node <T>> adjacent;
    
    public Node(String name, T value){
        this.value = value;
        this.name = name;
        this.adjacent = new LinkedList<Node <T>>();
    }
    
    public String getName(){
        return name;
    }
    
    public T getValue(){
        return value;
    }
   
    public void setValue(T newValue){
        this.value = newValue;
    } 
    
    public void connect(Node<T> connectableNode){
        this.adjacent.add(connectableNode);
        connectableNode.adjacent.add(this);
    }
    
    public void disconnect(Node<T> removableNode){
        if (this.adjacent.contains(removableNode)){
            this.adjacent.remove(removableNode);
            removableNode.adjacent.remove(this);
        }
    }
    
    public LinkedList<Node<T>> traverse(){
        LinkedList<Node<T>> graph = new LinkedList<Node<T>>();
        LinkedList<Node<T>> queue = new LinkedList<Node<T>>();
        Node<T> current;
        Node<T> next;
        int i;
        
        graph.add(this);
        queue.add(this);
        while (!queue.isEmpty()){
            current = queue.removeFirst();
            for (i = 0; i < current.adjacent.size(); i++){
                next = current.adjacent.removeFirst();
                if (!graph.contains(next)){
                    graph.add(next);
                    queue.add(next);
                }
            }
        }
        return graph;
    }
}
我应该创建一个包含7个节点(命名为A到G)的图,但我只得到4个连接的节点作为输出:
(A,3)
(G,9)
(B,8)
(F,17)
此外,节点应该使用广度优先搜索算法进行遍历,所以我有的这4个节点不在正确的顺序中。
这是用于测试Node类的代码:
import java.util.*;
class TestNodeClass{   
    public static void main(String[] args){
        LinkedList<Node<Integer>> course;
        Iterator<Node<Integer>> iter;
        Node<Integer> a = new Node<Integer>("A", 3);
        Node<Integer> b = new Node<Integer>("B", 8);
        Node<Integer> c = new Node<Integer>("C", 5);
        Node<Integer> d = new Node<Integer>("D", 1);
        Node<Integer> e = new Node<Integer>("E", 8);
        Node<Integer> f = new Node<Integer>("F", 17);
        Node<Integer> g = new Node<Integer>("G", 9);
        a.connect(g);
        a.connect(f);
        b.connect(f);
        b.connect(g);
        f.connect(g);
        c.connect(f);
        c.connect(d);
        d.connect(e);
        course = a.traverse();
        iter = course.iterator();
        while (iter.hasNext()){
            Node<Integer> current = iter.next(); 
            String name = current.getName();
            Integer value = current.getValue();
            System.out.println("(" + name + "," + value + ")");
        }
    }
}
如果有人能给我一些提示或指出问题所在,将会很棒,谢谢!
英文:
Im a java beginner and I'm supposed to do an exercise for my class that involves creating a generic class that represents the Nodes of a graph. Either the method traverse or connect(or both) I implemented don't work properly. The graph is supposed to be an undirected graph, that means every connection between 2 points goes both ways.
Here is my code for the class Node:
import java.util.LinkedList;
public class Node <T>{
    private T value;
    private String name;
    private LinkedList<Node <T>> adjacent;
    
    public Node(String name, T value){
        this.value=value;
        this.name=name;
        this.adjacent= new LinkedList<Node <T>>();
    }
    
    public String getName(){
        return name;
    }
    
    public T getValue(){
        return value;
    }
   
    public void setValue(T newValue){
        this.value=newValue;
    } 
    
    public void connect(Node<T> connectableNode){
        this.adjacent.add(connectableNode);
        connectableNode.adjacent.add(this);
        //add() hängt das Argument ans Ende einer Liste
    }
    
    public void disconnect(Node<T> removableNode){
        if (this.adjacent.contains(removableNode)){
            this.adjacent.remove(removableNode);
            removableNode.adjacent.remove(this);
        }
    }
    
    public LinkedList<Node <T>> traverse(){
        //Variablen definieren für BFS
        LinkedList<Node <T>> graph = new LinkedList<Node <T>>();
        LinkedList<Node<T>> queue = new LinkedList<Node <T>>();
        Node <T> current;
        Node <T> next;
        int i;
        
        //Startknoten ist erstes Element
        graph.add(this);
        queue.add(this);
        while (queue.isEmpty()!=true){
            current = queue.removeFirst();
            //lasse for schleife durch alle Elemente der Adjazenzliste von current laufen
            for (i=0; i<current.adjacent.size(); i++ ){
                next=current.adjacent.removeFirst();
                if (graph.contains(next)!=true){
                    graph.add(next);
                    queue.add(next);
                }
            }
        }
        return graph;
        }
I'm supposed to create a graph that contains 7 nodes(named A to G) but I only get 4 connected Nodes as an Output:
(A,3)
(G,9)
(B,8)
(F,17)
Also the Nodes should be traversed using the breadth-first search algorithm so the 4 Nodes I have are not in the right order.
import java.util.*;
class TestNodeClass{   
    public static void main(String[] args){
//Definitionen
        LinkedList<Node <Integer>> course;
        Iterator<Node<Integer>> iter;
        Node <Integer> a = new Node <Integer>("A", 3);
        Node <Integer> b = new Node <Integer>("B", 8);
        Node <Integer> c = new Node <Integer>("C", 5);
        Node <Integer> d = new Node <Integer>("D", 1);
        Node <Integer> e = new Node <Integer>("E", 8);
        Node <Integer> f = new Node <Integer>("F", 17);
        Node <Integer> g = new Node <Integer>("G", 9);
        a.connect(g);
        a.connect(f);
        b.connect(f);
        b.connect(g);
        f.connect(g);
        c.connect(f);
        c.connect(d);
        d.connect(e);
        course = a.traverse();
        iter = course.iterator();
        while (iter.hasNext()){
            Node <Integer> current = iter.next(); 
            String name = current.getName();
            Integer value = current.getValue();
            System.out.println("(" + name +","+ value + ")");
        }
    }
}
Would be great if someone could give me a hint or point out what's wrong, thanks!
专注分享java语言的经验与见解,让所有开发者获益!

评论