Prashnikaप्रश्निका
Code
About

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

  1. 1Click to build: type the root, then fill in the child boxes that appear. Or choose another way to give the tree.
  2. 2Watch the preorder, inorder and postorder appear as you type, and tap one to number the nodes in that order.
  3. 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.
Build the tree

Numbers go by value, words alphabetically (MANGO, APPLE, PEAR ...).

Separate the values with commas or spaces.

The tree

APPLEBANANAORANGEPEARMANGO

Traversals

Preorder (root, left, right)
MANGO BANANA APPLE PEAR ORANGE
Inorder (left, root, right)
APPLE BANANA MANGO ORANGE PEAR
Postorder (left, right, root)
APPLE BANANA ORANGE PEAR MANGO
Level order
MANGO BANANA PEAR APPLE ORANGE

Watch it step by step

The recursive calls of each traversal, on the call stack:

Preorder · Inorder · Postorder

About the tree

Root
MANGO
Number of nodes (size)
5
Height = depth (edges from the root to the deepest leaf)
2
Levels (the root is at level 0)
3
Leaves (external nodes)
APPLE, ORANGE
Internal nodes (not the root)
BANANA, PEAR
Full binary tree (every node has 0 or 2 children)
No
Complete binary tree
No
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.

  1. MANGO: the first value is the root
  2. BANANA: BANANA < MANGO: go left → left of MANGO
  3. PEAR: PEAR > MANGO: go right → right of MANGO
  4. APPLE: APPLE < MANGO: go left; APPLE < BANANA: go left → left of BANANA
  5. ORANGE: ORANGE > MANGO: go right; ORANGE < PEAR: go left → left of PEAR

Every node

NodeLevelParentLeft childRight childSiblingDegreeKind
MANGO0-BANANAPEAR-2root
BANANA1MANGOAPPLE-PEAR1internal
PEAR1MANGOORANGE-BANANA1internal
APPLE2BANANA---0leaf
ORANGE2PEAR---0leaf
Start again

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

More Computer Science tools

All study tools →