通过深度优先遍历测试

问题描述

DepthFirstTraversal 方法有效。但是,它仅通过了 13 项测试中的 9 项。我已经将下面的测试类包含在不起作用的测试中。拜托,有人能帮我理解为什么它通过了除此测试之外的所有测试。

public class DepthFirstTraversal<T> extends AdjacencyGraph<T> implements Traversal<T> {

    private Queue<T> backTrack = new arraydeque<>();
    private List<T> traversal = new ArrayList<T>();

    @Override
    public List<T> traverse() throws GraphError {
        T node = getUnvisitednode();
        while (node !=null){
            traversal.add(node);
            backTrack.add(node);
            traverseBackTrackList();
            node=getUnvisitednode();
        }
        return traversal;
    }

    private boolean visited(T node) {
        return
                traversal.contains(node) || backTrack.contains(node);  // check if the node is 
                // in the traversal list
    }

    private T getUnvisitednode() {
        for (T node: getNodes()) {
            if (!visited(node)) { // if this node has not been "visited"
                return node; // then this is an unvisited node
            }
        }
        return null;
    }

    private void traverseBackTrackList() throws GraphError {
        while (backTrack.size() > 0) { // as long as there are still nodes in the back track 
                                       // list
            T node = backTrack.remove(); // get the next node from the backtrack list
            visitNode(node); // and visit that node
        }
    }

    private void visitNode(T node) throws GraphError {
        if (visited(node)) return; // if the node is already visited do nothing
        traversal.add(node); // add the node to the traversal
        for (T neighbour : getNeighbours(node)) { // for all this node's neighbours
            if (!visited(neighbour))
                backTrack.add(neighbour);
            traversal.add(neighbour);
        }
    }
}
abstract class TraversalTest<T extends Traversal<Integer>> {
    Random random = new Random(System.currentTimeMillis());

    private void testPairedGraph(T graph,List<Integer> traversal) throws GraphError {
        if (graph.getNoOfNodes() == 0) return;
        if (graph.getNoOfNodes()%2 != 0) fail("Graph does not contain an even number of nodes");
        for (int index = 0; index < traversal.size(); index += 2) {
            
        assertEquals(graph.getNoOfNodes(),traversal.get(index)+traversal.get(index+1),"Incorrect 
        neighbouring values in paired graph");
        }

    private void testPaired(int noOfPairs) throws GraphError {
        T graph = generatePairedGraph(noOfPairs);
        List<Integer> traversal = graph.traverse();
        testContents(graph,traversal);
        testPairedGraph(graph,traversal);
    }

    @Test
    void testPaired3() throws GraphError {
        testPaired(3);
    }

    @Test
    void testPaired10() throws GraphError {
        testPaired(10);
    }

    @Test
    void testPaired38() throws GraphError {
        testPaired(38);
    }

    @Test
    void testPairedRandom() throws GraphError {
        int size = random.nextInt(40)+10;
        testPaired(size);
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)