easyBinary Trees 0 views

Binary Tree Traversals: Preorder, Inorder, Postorder

Add preorder, inorder, and postorder traversal methods to BinTree and see how each visits the same sample tree in a different order.

This exercise builds on Binary Tree Basics and adds the three classic ways to visit every node in a binary tree: preorder, inorder, and postorder traversal. All three reuse the same Node<T> / BinTree<T> classes and the same 12-node sample tree from that exercise.

Traversal orders

  • Preorder -- visit the node itself, then its left subtree, then its right subtree (root, left, right).
  • Inorder -- visit the left subtree, then the node itself, then the right subtree (left, root, right). For a binary search tree, this visits values in sorted order.
  • Postorder -- visit the left subtree, then the right subtree, then the node itself (left, right, root).

Each is naturally recursive: at every node, recurse into the children in the right order and visit ("print") the node itself at the right point relative to those two recursive calls.

private void preOrder(Node<T> node) {
    if (node != null) {
        System.out.printf(node.data + ", ");
        preOrder(node.left);
        preOrder(node.right);
    }
}

private void inOrder(Node<T> node) {
    if (node != null) {
        inOrder(node.left);
        System.out.printf(node.data + ", ");
        inOrder(node.right);
    }
}

private void postOrder(Node<T> node) {
    if (node != null) {
        postOrder(node.left);
        postOrder(node.right);
        System.out.printf(node.data + ", ");
    }
}

The sample tree

1
|- left: 2
|    |- left: 4
|    |    `- left: 8
|    `- right: 5
|         `- left: 9
`- right: 3
     |- left: 6
     `- right: 7
          |- left: 10
          `- right: 11
               `- right: 12

See the Solutions tab for the full Java implementation, including the sample tree's getTree() helper.

This is a foundational exercise -- there are no graded test cases for it, since the traversals print rather than return a value. The three outputs below (verified by hand against the tree above) serve as worked examples instead.

Example 1

Input: preOrder() on the sample tree

Output: 1, 2, 4, 8, 5, 9, 3, 6, 7, 10, 11, 12

Explanation: Root, then left subtree, then right subtree -- 1 comes first since it's the root.

Example 2

Input: inOrder() on the sample tree

Output: 8, 4, 2, 9, 5, 1, 6, 3, 10, 7, 11, 12

Explanation: Left subtree, then the node, then right subtree -- the whole left half of the tree is printed before 1.

Example 3

Input: postOrder() on the sample tree

Output: 8, 4, 9, 5, 2, 6, 10, 12, 11, 7, 3, 1

Explanation: Left subtree, then right subtree, then the node -- the root (1) is always printed last.

Constraints

  • Uses the same 12-node sample tree as Binary Tree Basics.
  • Each traversal visits every node exactly once.

Follow-up

Later exercises in the Binary Trees series (height/depth, balancing, search) will also operate on this same tree.

Hints

Companies

No companies reported yet.

Discussion

Sign in to join the discussion.

Loading discussion...

Test results

This question doesn't have a code exercise.