Four traversals of binary trees (recursive and non recursive)

Posted by Eman on Thu, 07 Oct 2021 09:08:48 +0200

Preorder traversal and postorder traversal

First traverse the root node, then the left subtree, and then the right subtree.

Post order traversal first traverses the left subtree, then the right subtree, and then the root node.

Recursive implementation of preorder traversal:

public static void preOrderByRecursion(TreeNode root) {
    // Print node values
    System.out.println(root.value);
    preOrder(root.left);
    preOrder(root.right);
}

Non recursive implementation of preorder traversal:

Non recursive implementation needs the help of a data structure such as stack. In fact, recursive implementation also depends on stack, which is only implicit.

  1. First push the root node into the stack.
  2. Pop up the node in the stack, press the right sub node of the pop-up node into the stack, and then press the left sub tree of the pop-up node into the stack.
  3. Repeat step 2 until the stack is empty.
public static void preOrder(TreeNode root) {
    if (root == null) {
        return;
    }
    Stack<TreeNode> stack = new Stack<>();
    stack.push(root);
    while (!stack.empty()) {
        TreeNode node = stack.pop();
        // Print node values
        System.out.print(node.value + " ");
        if (node.right != null) {
            stack.push(node.right);
        }
        if (node.left != null) {
            stack.push(node.left);
        }
    }
}

Recursive implementation of post order traversal: the reverse of pre order traversal will not be repeated.

public static void postOrderByRecursion(TreeNode root) {
    postOrderByRecursion(root.left);
    postOrderByRecursion(root.right);
    System.out.println(root.value);
}

Non recursive implementation of post order traversal: post order traversal is the reverse of pre order traversal, so two stacks are required, and the extra stacks are used for reverse output.

public static void postOrder(TreeNode root) {
    if (root == null) {
        return;
    }
    Stack<TreeNode> s1 = new Stack<>();
    Stack<TreeNode> s2 = new Stack<>();
    s1.push(root);
    while (!s1.empty()) {
        TreeNode node = s1.pop();
        s2.push(node);
        if (node.left != null) {
            s1.push(node.left);
        }
        if (node.right != null) {
            s1.push(node.right);
        }
    }
    while (!s2.empty()) {
        System.out.println(s2.pop().value);
    }
}

Medium order traversal

Middle order traversal first traverses the left subtree, then the root node, and then the right subtree.

Recursive traversal:

public static void inOrderByRecursion(TreeNode root) {
    if (root == null) {
        return;
    }
    inOrderByRecursion(root.left);
    // Print node values
    System.out.println(root.value);
    inOrderByRecursion(root.right);
}

Non recursive traversal:

  1. Press the left "edge" of the binary tree into the stack from top to bottom.
  2. Pop node from stack
  3. Repeat step 1 for the subtree with the right child node of the pop-up node as the root node.
  4. Repeat steps 2 and 3 until the stack is empty.
public static void inOrder(TreeNode root) {
    if (root == null) {
        return;
    }
    Stack<TreeNode> stack = new Stack<>();
    TreeNode cur = root;

    while (cur != null) {
        stack.push(cur);
        cur = cur.left;
    }

    while (!stack.empty()) {
        TreeNode node = stack.pop();
        System.out.println(node.value);
        cur = node.right;
        while (cur != null) {
            stack.push(cur);
            cur = cur.left;
        }
    }
}

level traversal

As the name suggests, sequence traversal is a layer by layer, from left to right traversal binary tree. The queue data structure is required.

  1. Push the root node into the queue.

  2. Take a node from the queue.

  3. First push the left child node of the extracted node into the queue, and then push the right child node of the extracted node into the queue.

  4. Repeat steps 2 and 3 until there are no nodes in the queue.

public static void floorOrder(TreeNode root) {
    if (root == null) {
        return;
    }
    Queue<TreeNode> queue = new LinkedList<>();
    queue.add(root);
    while (!queue.isEmpty()) {
        TreeNode node = queue.poll();
        System.out.println(node.value);
        if (node.left != null) {
            queue.add(node.left);
        }
        if (node.right != null) {
            queue.add(node.right);
        }
    }
}

Topics: Algorithm data structure linked list