Binary tree - traversal of binary tree (recursion and iteration)

In the traversal of the binary tree, the recursive version uses the stack provided by the system to automatically push various information of the current node onto the stack

In the iterative version, you need to manually push the stack yourself, so you need a stack

In the recursive version, both pass through the node three times

In the iterative version, both pass through the node twice

 

preorder traversal

public class PreOrderRecursive {
    public static void preOrderRecursive(Tree tree){
        if(tree == null) return;
        System.out.print(tree.val + " ");
        preOrderRecursive( tree.left );
        preOrderRecursive( tree.right );
    }
}

  

Iterative version: a stack is used to store nodes. If there is a right, the right is pressed first, and if there is a left, the left is pressed.

 

Using the characteristics of the stack, start from the root, push the stack in turn, and then pop the stack in turn. While popping the stack, 
add its right and left children (note the order, if any) to the stack until there are no elements in the stack.

 

public class PreOrderNonRecursive {

    public static void preOrderNonRecursive(Tree tree){
        if(tree == null) return;
        Stack<Tree> stack = new Stack<>();

        stack.push(tree);
        while(!stack.empty()){
            Tree subTree = stack.pop();
            System.out.print(subTree.val + " ");
            if(subTree.right != null){
                stack.push(subTree.right);
            }
            if(subTree.left != null){
                stack.push(subTree.left);
            }
        }
    }
}

 

  

 

Inorder traversal

public class InOrderRecursive {
    public static void inOrderRecursive(Tree tree){
        if(tree == null) return;

        inOrderRecursive(tree.left);
        System.out.print(tree.val + " ");
        inOrderRecursive(tree.right);
    }
}

  

Iterative version:

The current node is not empty , the current node is pushed into the stack , and the current node goes left

Only when the current node is empty , a node is popped ( out of the stack ), printed, and then the current node goes to the right

public class InOrderNonRecursive {
    public static void inOrderNonRecursive(Tree tree){
        if(tree == null) return;
        Stack<Tree> stack = new Stack<>();
        while(!stack.empty() || tree != null){
            //If the current node is not empty, push a row of the left subtree of the current node in, (the current node is pushed into the stack, and the current node goes to the left)
            if(tree != null){
                stack.push(tree);
                tree = tree.left;
            } else{
                //If the current node is empty, pop a node in the stack and print it, and then the current node goes to the right
                tree = stack.pop();
                System.out.print(tree.val + " ");
                tree = tree.right;
            }
        }
    }
}

  

post-order traversal

public class PostOrderRecursive {
    public static void postOrderRecursive(Tree tree){
        if(tree == null) return;

        postOrderRecursive( tree.left );
        postOrderRecursive( tree.right );
        System.out.print(tree.val + " ");
    }
}

  

Iterative version:

Because the order of pre-order traversal is about middle , and the order of post-order traversal is about middle

Using the variant of pre-order traversal, first implement a middle-right-left traversal, then push it into the auxiliary stack , and finally pop the elements in the auxiliary stack in turn.

public class PostOrderNonRecursive {

    public static void postOrderNonRecursive(Tree tree){
        if(tree == null) return;
        Stack<Tree> stack = new Stack<>();
        Stack<Tree> stackHelp = new Stack<>();
        stack.push(tree);
        while(!stack.empty()){
            Tree subTree = stack.pop();
            stackHelp.push(subTree);
            if(subTree.left != null){
                stack.push(subTree.left);
            }
            if(subTree.right != null){
                stack.push(subTree.right);
            }
        }
        while(!stackHelp.empty()){
            System.out.print(stackHelp.pop().val + " ");
        }
    }
}

  

level-order traversal

There is no recursive version, only an iterative version, with the help of queues, first in first out

public class SequenceTranversal {
    public static void sequenceTranversal(Tree tree){
        if(tree == null) return;

        Queue<Tree> queue = new LinkedList<>();
        queue.offer(tree);

        while(!queue.isEmpty()){
            Tree subTree = queue.poll();
            System.out.print(subTree.val + " ");

            if(subTree.left != null){
                queue.offer(subTree.left);
            }
            if(subTree.right != null){
                queue.offer(subTree.right);
            }
        }
    }
}

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324834991&siteId=291194637