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

Write - for a missing child. After a -, leave out its children.

Separate the values with commas or spaces.

The tree

DBEFCA

Traversals

Preorder (root, left, right)
A B D C E F
Inorder (left, root, right)
D B A E C F
Postorder (left, right, root)
D B E F C A
Level order
A B C D E F

Watch it step by step

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

Preorder · Inorder · Postorder

About the tree

Root
A
Number of nodes (size)
6
Height = depth (edges from the root to the deepest leaf)
2
Levels (the root is at level 0)
3
Leaves (external nodes)
D, E, F
Internal nodes (not the root)
B, C
Full binary tree (every node has 0 or 2 children)
No
Complete binary tree
No
Binary search tree (inorder is in order)
No

Every node

NodeLevelParentLeft childRight childSiblingDegreeKind
A0-BC-2root
B1AD-C1internal
C1AEFB2internal
D2B---0leaf
E2C--F0leaf
F2C--E0leaf
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 →