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

20403060807050

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

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.

  1. 50: the first value is the root
  2. 30: 30 < 50: go left → left of 50
  3. 70: 70 > 50: go right → right of 50
  4. 20: 20 < 50: go left; 20 < 30: go left → left of 30
  5. 40: 40 < 50: go left; 40 > 30: go right → right of 30
  6. 60: 60 > 50: go right; 60 < 70: go left → left of 70
  7. 80: 80 > 50: go right; 80 > 70: go right → right of 70

Every node

NodeLevelParentLeft childRight childSiblingDegreeKind
500-3070-2root
301502040702internal
701506080302internal
20230--400leaf
40230--200leaf
60270--800leaf
80270--600leaf

Practise on real ISC questions

More Computer Science tools

All study tools ›