6.7: Iterative DFS

Think Data Structures - Algorithms and Information Retrieval in Java · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    From LibreTexts, under CC BY-NC-SA 3.0, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

    6.7: Iterative DFS

    Here is an iterative version of DFS that uses an ArrayDeque to represent a stack of Node objects:

    private static void iterativeDFS(Node root) {
        Deque<Node> stack = new ArrayDeque<Node>();
        stack.push(root);
        
        while (!stack.isEmpty()) {
            Node node = stack.pop();
            if (node instanceof TextNode) {
                System.out.print(node);
            }
            
            List<Node> nodes = new ArrayList<Node>(node.childNodes());
            Collections.reverse(nodes);
            
            for (Node child: nodes) {
                stack.push(child);
            }
        }
    }
    

    The parameter, root, is the root of the tree we want to traverse, so we start

    by creating the stack and pushing the root onto it.

    The loop continues until the stack is empty. Each time through, it pops a Node off the stack. If it gets a TextNode, it prints the contents. Then it pushes the children onto the stack. In order to process the children in the right order, we have to push them onto the stack in reverse order; we do that by copying the children into an ArrayList, reversing the elements in place, and then iterating through the reversed ArrayList.

    One advantage of the iterative version of DFS is that it is easier to implement as a Java Iterator; you’ll see how in the next chapter.

    But first, one last note about the Deque interface: in addition to ArrayDeque, Java provides another implementation of Deque, our old friend LinkedList. LinkedList implements both interfaces, List and Deque. Which interface you get depends on how you use it. For example, if you assign a LinkedList object to a Deque variable, like this:

    Deqeue<Node> deque = new LinkedList<Node>();
    

    you can use the methods in the Deque interface, but not all methods in the List interface. If you assign it to a List variable, like this:

    List<Node> deque = new LinkedList<Node>();
    

    you can use List methods but not all Deque methods. And if you assign it like this:

    LinkedList<Node> deque = new LinkedList<Node>();
    

    you can use all the methods. But if you combine methods from different interfaces, your code will be less readable and more error-prone.


    This page titled 6.7: Iterative DFS was last modified on Tue, 01 Dec 2020 03:50:42 GMT and is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Allen B. Downey (Green Tea Press) .