Prashnikaप्रश्निका

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.

Build the tree

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

Separate the values with commas or spaces.

Try: BST: 50, 30, 70, 20, 40, 60, 80BST: MANGO, BANANA, PEAR, APPLE, ORANGEA, B, C, D, -, E, Finorder + preorderinorder + postorder

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

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

Practise on real ISC questions

More Computer Science tools

All study tools ›