← CS Unplugged

CS Unplugged activity

Build a Tree

About 15 minutesSoloPencil

A binary search tree isn’t just found, it’s built one number at a time. The order you insert numbers in changes the tree’s shape, even when the numbers themselves are exactly the same. Here you’ll build the same five numbers two different ways and see why that matters.

How to insert a number

  1. Start at the root circle.
  2. Compare your new number to the number already there.
  3. Go left if it’s smaller, right if it’s larger.
  4. Keep going until you reach an empty circle, then write your number in.

What happened? Look at your two trees. One is short and wide, the other is tall and thin, almost a straight line. Which shape would let you find a number in fewer comparisons?

Check your answers

Adapted from CS Unplugged, by Tim Bell and the CS Unplugged team, University of Canterbury (csunplugged.org). Licensed under CC BY-SA 4.0.