Java Visitor Pattern

  • + 1 comment

    @rshaghoulian..clear solution and easy to understand... I have a doubt that there is no need to remove the reverse edges if we skipped adding them...

    (i.e) if we skip this segment

            /* Edges are undirected: Add 2nd direction */
            HashSet<Integer> vNeighbors = map.get(v);
            if (vNeighbors == null) {
                vNeighbors = new HashSet<>();
                map.put(v, vNeighbors);
            }
            vNeighbors.add(u);
    

    we need not do this..

            map.get(treeNum).remove(parentNum); // removes the opposite arrow direction
    

    But if skip the above lines,it produces wrong output.Can u please explain this??

    and one more thing..

    boolean childHasChild = (grandChildren != null && !grandChildren.isEmpty());

    here, I can't figure out the difference between the two conditions "grandChildren != null" and "!grandChildren.isEmpty()" both appear similiar to me but if I OR them instead of AND,obviously it produces wrong output...can you please explain them??

    Thanks in advance!!