0

I can track the last item by keeping the q.poll in a int but how can I track the second last item using this traversal?

    public Integer breadthFirstTraversal(Integer v) {
            Queue<Integer> q = new LinkedList<Integer>();
            VertexIDList adjList;

            q.add(v);
            getVertex(v).setMarked();

            while (!q.isEmpty()) {
                v = q.poll();
                adjList = getVertex(v).getAdjs();


                Iterator<Integer> vIt = adjList.iterator();
                while (vIt.hasNext()) {
                    Integer u = vIt.next();

                    if (!getVertex(u).isMarked()) {

                        q.add(u);
                        getVertex(u).setMarked();

                    }

                }

            }
// return second last item here

        }
  • If you can keep the last one pulled out of queue (by storing it in a variable) you can do the same to keep the previous one. – c0der May 26 '20 at 04:52

1 Answers1

1

Modify method signature and add a List to store the paths.(size()-2: last but 1)

public Integer breadthFirstTraversal(Integer v, List emptyList) {
    //with intermediary vars
    int last = 0;
    int last1 = 0;
    Queue<Integer> q = new LinkedList<Integer>();
    VertexIDList adjList;

    q.add(v);

    getVertex(v).setMarked();


    while (!q.isEmpty()) {
        v = q.poll();

        //store
        emptyList.add(getVertex(v));
        last1 = last;
        last = getVertex(v);
        //System.out.println(getVertex(v));

        adjList = getVertex(v).getAdjs();

        Iterator<Integer> vIt = adjList.iterator();
        while (vIt.hasNext()) {
            Integer u = vIt.next();
            if (!getVertex(u).isMarked()) {

                q.add(u);
                getVertex(u).setMarked();  
            }
        }
    }
 // return second last item here
 return last1;
}
Traian GEICU
  • 1,750
  • 3
  • 14
  • 26
  • What about `// return second last item here` ? There is no need to add the list to the signature when only one element of it should be returned. – c0der May 26 '20 at 04:47
  • @c0der This way you have all items, and grab whatever you wanted in case of changing the mind (size()-2 as asked). Without List one way is to use 2 intermediary vars(last, lastbutone) and return lastbutone ... – Traian GEICU May 26 '20 at 06:56
  • @c0der more over hardly to understand what the point of BFS with-out storing the paths?(or at least do some operation there ...) – Traian GEICU May 26 '20 at 07:09
  • @c0der The way of storing is the same, but did some errors when adding ... adjusted now (poll - give paths) . – Traian GEICU May 26 '20 at 07:28
  • 1
    OK. Two comments: emptyList is not needed at all to answer the question as asked. 2. If `while` runs only once last1 has the value of 0 which might be wrong. – c0der May 26 '20 at 11:44
  • @c0der 1-Yes, empty not needed, but let it just in case. 2.Yes, but assume at least 2 nodes (and if only 1 node still wrong value because there is no last but1t). Up to him here to put condition – Traian GEICU May 26 '20 at 11:48