Binary tree traversal visualiser
Build any binary tree by typing its root and then the children of each node: two boxes appear under every node you fill in. Pick preorder, inorder or postorder and press Play: the recursive calls pile up on the call stack, the node being visited lights up and the output grows, with the Java line that is running lit.
How to use it
- 1Type the root, then fill in the boxes that appear under each node (leave the ones the tree does not have empty), or tap an example.
- 2Pick preorder, inorder or postorder, press Play, then use Back and Next, the slider or the arrow keys to go at your own speed.
- 3Watch the recursive calls pile up on the call stack, the node being visited light up and the output grow, with the Java line that is running.
The animation needs JavaScript. Every step is written out below.
Result
- Result
- preorder: A B D C E F
Every step in words (39 steps)
- preorder(root) is called with the root, A.
- t is A, not null, so: the node, then left subtree, then right subtree.
- Print A.
- Go left: preorder(t.left) is called, and t.left is B.
- t is B, not null, so: the node, then left subtree, then right subtree.
- Print B.
- Go left: preorder(t.left) is called, and t.left is D.
- t is D, not null, so: the node, then left subtree, then right subtree.
- Print D.
- Go left: preorder(t.left) is called, and t.left is null.
- t is null, so there is nothing to do: the call returns at once.
- Go right: preorder(t.right) is called, and t.right is null.
- t is null, so there is nothing to do: the call returns at once.
- preorder(D) has done all three parts, so it returns.
- Go right: preorder(t.right) is called, and t.right is null.
- t is null, so there is nothing to do: the call returns at once.
- preorder(B) has done all three parts, so it returns.
- Go right: preorder(t.right) is called, and t.right is C.
- t is C, not null, so: the node, then left subtree, then right subtree.
- Print C.
- Go left: preorder(t.left) is called, and t.left is E.
- t is E, not null, so: the node, then left subtree, then right subtree.
- Print E.
- Go left: preorder(t.left) is called, and t.left is null.
- t is null, so there is nothing to do: the call returns at once.
- Go right: preorder(t.right) is called, and t.right is null.
- t is null, so there is nothing to do: the call returns at once.
- preorder(E) has done all three parts, so it returns.
- Go right: preorder(t.right) is called, and t.right is F.
- t is F, not null, so: the node, then left subtree, then right subtree.
- Print F.
- Go left: preorder(t.left) is called, and t.left is null.
- t is null, so there is nothing to do: the call returns at once.
- Go right: preorder(t.right) is called, and t.right is null.
- t is null, so there is nothing to do: the call returns at once.
- preorder(F) has done all three parts, so it returns.
- preorder(C) has done all three parts, so it returns.
- preorder(A) has done all three parts, so it returns.
- Done. The preorder traversal is A B D C E F.
About the Binary tree traversal visualiser
What it does
Walks a binary tree that you build yourself and shows what the recursive traversal method does at every call: it is called on a node, calls itself on the left and right children (a missing child is a call on null that returns at once) and visits the node at the moment its order says. The call stack, the node being worked on and the output so far are shown at every step.
The three orders
- Preorder: node, left subtree, right subtree.
- Inorder: left subtree, node, right subtree.
- Postorder: left subtree, right subtree, node.
For an expression tree (operators in the inner nodes, operands in the leaves) preorder gives the prefix form, inorder the infix form without brackets and postorder the postfix form.
The limits
Up to 15 nodes, each with its own value of up to 6 characters (so the picture can show which node was printed). To get the height, leaves and every node's parent and sibling, or to rebuild a tree from two traversals, use the binary tree tool.
Common questions
What is the preorder traversal of a binary tree?
Visit the node first, then the whole left subtree, then the whole right subtree. For the tree A, B, C, D, -, E, F (A with children B and C; B with the left child D; C with children E and F) it is A B D C E F.
How do the three traversals give prefix, infix and postfix?
In an expression tree the operators are the inner nodes. Preorder prints each operator before its operands (prefix), inorder between them (infix) and postorder after them (postfix).
Why must every node have a different value?
The picture marks a node as printed by its value, so two nodes with the same value could not be told apart. Give each node its own letter or number.
Can I see the code in Python, JavaScript or C++?
Yes. Choose the language in the menu above the code. Only the listing changes: the steps are always worked out from the Java program that ISC uses, and the highlighted line moves to the matching line of the language you chose.
Practise on real ISC questions
- Answer the following questions from the diagram of a Binary Tree given below: Name the root of the left sub…2025 Specimen
- Answer the following questions from the diagram of a Binary Tree given below: Write the post-order traversal…2026 Improvement
- State the difference between internal nodes and external nodes of a binary tree structure.2019
- Write the pre-order traversal of the above tree.2026
- Write the postorder traversal of the above tree structure.2017
- State the difference between a Binary Tree structure and a single Linked List.2020
- Answer the following questions from the diagram of a Binary Tree given below: Write the pre-order traversal…2023
- Write the inorder traversal of the above tree structure.2018
- Write the post order traversal of the tree.2020 Specimen
- Write the post-order traversal of the left subtree of the above structure.2020
More Computer Science tools
- Boolean algebra solverSimplify any expression: the laws step by step, truth table, K-map and the logic circuit.
- Infix to postfix converterInfix to postfix or prefix with the stack table, and postfix evaluation step by step.
- Binary tree and BST toolBuild a binary search tree, rebuild a tree from two traversals, and get every traversal and fact.
- Stack and queue simulatorRun push, pop, insert and delete on a stack, queue, circular queue or dequeue, and watch the pointers move.
- Recursion visualiserWatch factorial, Fibonacci, power, GCD and sum of digits run: the calls, the call stack and the returns.
- Tower of Hanoi visualiserWatch the discs move between the pegs while the recursive calls and the call stack build up.
- Binary search tree visualiserWatch values being inserted into a binary search tree, searched for, and walked inorder, preorder and postorder.
- Stack and queue visualiserWatch push, pop, insert and delete move top, front and rear along the array, with overflow and underflow.
- Linked list visualiserWatch nodes being added, deleted, searched, traversed and reversed, with start, p, prev and curr moving along the arrows.
- 2D array visualiserWatch row-wise, column-wise, diagonal, boundary and spiral walks number the cells of a 2D array in the order they print.
- Sorting visualiserWatch bubble, selection and insertion sort compare, swap and shift your own list, step by step.
- Linear and binary search visualiserWatch a linear or binary search look for a value in your list, and count the comparisons.