Binary tree and BST tool
Draw a binary tree and get what ISC Computer Science asks about it: the preorder, inorder and postorder traversals, the height and levels, leaves, internal nodes, and each node's parent, children and sibling. Build it by inserting values into a binary search tree, give it level by level, or rebuild it from two traversals.
How to use it
- 1Click to build: type the root, then fill in the child boxes that appear. Or choose another way to give the tree.
- 2Watch the preorder, inorder and postorder appear as you type, and tap one to number the nodes in that order.
- 3Press Open in visualiser to watch each walk step by step. For the drawing and the details of each node, choose Level by level and press Build the tree.
BST: 50, 30, 70, 20, 40, 60, 80
This is the kind of answer you get. Put these values in the boxes or type your own.
The tree
Traversals
- Preorder (root, left, right)
- 50 30 20 40 70 60 80
- Inorder (left, root, right)
- 20 30 40 50 60 70 80
- Postorder (left, right, root)
- 20 40 30 60 80 70 50
- Level order
- 50 30 70 20 40 60 80
Watch it step by step
The recursive calls of each traversal, on the call stack:
About the tree
- Root
- 50
- Number of nodes (size)
- 7
- Height = depth (edges from the root to the deepest leaf)
- 2
- Levels (the root is at level 0)
- 3
- Leaves (external nodes)
- 20, 40, 60, 80
- Internal nodes (not the root)
- 30, 70
- Full binary tree (every node has 0 or 2 children)
- Yes
- Complete binary tree
- Yes
- Binary search tree (inorder is in order)
- Yes
Inserting, one value at a time
Each value starts at the root and goes left if smaller, right if larger, until there is a free place.
- 50: the first value is the root
- 30: 30 < 50: go left → left of 50
- 70: 70 > 50: go right → right of 50
- 20: 20 < 50: go left; 20 < 30: go left → left of 30
- 40: 40 < 50: go left; 40 > 30: go right → right of 30
- 60: 60 > 50: go right; 60 < 70: go left → left of 70
- 80: 80 > 50: go right; 80 > 70: go right → right of 70
Every node
| Node | Level | Parent | Left child | Right child | Sibling | Degree | Kind |
|---|---|---|---|---|---|---|---|
| 50 | 0 | - | 30 | 70 | - | 2 | root |
| 30 | 1 | 50 | 20 | 40 | 70 | 2 | internal |
| 70 | 1 | 50 | 60 | 80 | 30 | 2 | internal |
| 20 | 2 | 30 | - | - | 40 | 0 | leaf |
| 40 | 2 | 30 | - | - | 20 | 0 | leaf |
| 60 | 2 | 70 | - | - | 80 | 0 | leaf |
| 80 | 2 | 70 | - | - | 60 | 0 | leaf |
About the Binary tree and BST tool
What it does
Draws a binary tree and answers the usual questions on it: the preorder, inorder and postorder traversals, the height and levels, the leaves and internal nodes, and each node’s parent, children and sibling. It also rebuilds a tree from an inorder traversal plus a preorder or postorder traversal.
Four ways to build the tree
- Click to build: type the root; two boxes for its children appear. Fill in the ones the tree has and leave the others empty: every box you fill gets two child boxes of its own. The three traversals update as you type, and a tap on one numbers the nodes in that order.
- Insert into a BST: the values are inserted one at a time; smaller goes left, larger goes right. Words go alphabetically.
- Level by level: list the nodes row by row from left to right, writing - for a missing child.
- From traversals: an inorder traversal together with a preorder or a postorder traversal gives one tree (when all the values are different).
The words it uses
The root is at level 0, and the height is counted in edges from the root down to the deepest leaf, so a tree of one node has height 0. Leaves are nodes with no child; every other node except the root is internal.
Common questions
How do I get the inorder traversal of a BST?
Visit the left subtree, then the root, then the right subtree. For a binary search tree this always lists the values in increasing order.
Why can’t preorder and postorder alone rebuild a tree?
Different trees can have the same preorder and postorder, so the inorder traversal is needed as well. That is why the tool asks for the inorder traversal plus one of the other two.
How do I leave out a child?
Leave its box empty. In Click to build, an empty box and everything below it is left out; clear a box you filled by mistake and its children are only hidden, so typing the value again brings them back.
What happens to a repeated value when inserting into a BST?
A value that is already in the tree is not inserted again. The tool lists the repeats it skipped.
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.
- 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.
- Binary tree traversal visualiserClick a binary tree together and watch preorder, inorder and postorder walk it, with the recursive calls on the call stack.
- 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.