Skip to content

Binary Tree Name

By the end of this lab you'll be able to:

  • Convert a letter to a number with ord() and pull out its binary bits with // and %
  • Trace a path down a binary tree — left for 0, right for 1
  • Draw the same structure at every level using nested loops

Every letter in your name is secretly a number, and every number is secretly a string of bits. This lab draws a five-level binary tree, then traces each letter of a name down the tree — branching left for every 0 bit and right for every 1 bit. Each letter lands on its own leaf!

Welcome to Binary Tree Name!

Monty waving welcome Computers store letters as numbers, and numbers as bits. Today you'll see those bits — as colorful paths through a tree. Let's code it together!

How It Works

ord('M') - 64 gives 13, because M is the 13th letter of the alphabet. Written in five bits, 13 is 01101. Starting at the root, the turtle reads the bits from left to right: 0 means "go down-left", 1 means "go down-right". After five moves every letter arrives at its own unique leaf — that's why five bits can label 32 different letters.

Sample Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import turtle
monty = turtle.Turtle()
monty.speed(0)
monty.hideturtle()

name = 'MONTY'
top = 160
drop = 50

pos = 0
for ch in name:
    n = ord(ch) - 64
    monty.penup()
    monty.goto(0, top)
    monty.pendown()
    x = 0
    y = top
    off = 88
    d = 16
    while d >= 1:
        bit = (n // d) % 2
        if bit == 1:
            x = x + off
        else:
            x = x - off
        y = y - drop
        monty.goto(x, y)
        off = off / 2
        d = d // 2
    pos = pos + 1

What Do You Think Will Happen?

Monty thinking MONTY has 5 letters, but the letter T appears once and Y once — all 5 letters are different. Will all 5 colored paths end on different leaves, or will any two paths land on the same leaf? Make your guess, then click Run!

Try It Now


  

Every letter lands on its own leaf — different letters always mean different bit patterns, so their paths must split apart somewhere. Were you right?

How It Works

The gray scaffold is drawn first: at each of the 5 levels, the loop visits every node (there are 2 ** level of them) and draws its two child branches. The branch spread off halves at every level (88, 44, 22, 11...) so branches never collide. Then each letter's number is peeled apart high-bit-first: dividing by 16, 8, 4, 2, 1 and taking % 2 yields the five bits in reading order, steering the colored pen down the tree.

Explanation Table

Line What it does
n = ord(ch) - 64 Letter position in the alphabet: A=1 ... Z=26
bit = (n // d) % 2 Extracts one bit; d counts down 16, 8, 4, 2, 1
off = off / 2 Halves the branch spread at each level down
monty.write(ch + ' = ' + bits, ...) Prints the letter's full bit string below the tree

Learning Check

Your Turn — Encode Your Initials

Monty thinking This smaller version traces just three letters — but it's set to 'ABC'. A is 1, which in five bits is 00001 — four moves left, then one right. Change name to your own initials and predict which side of the tree each letter will land on before you run it!


  

Letters early in the alphabet (small numbers) start with 0 bits, so they drift left; letters from Q onward start with a 1 bit and go right. Initials like 'AZ' split to opposite edges of the tree!

Experiments

  1. Trace the whole alphabet. Set name to 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' and use colors[pos % 5]. You'll know it worked when every leaf from the far left to position 26 has a path.

  2. Six-bit tree. Change top to 180, start d at 32, and add a sixth level to the scaffold loop (range(6)). You'll know it worked when the tree gains a whole extra row of tinier branches.

  3. Count the leaves. Add a loop that puts a tiny monty.dot(4, 'gray') at all 32 leaf positions of the bottom row. You'll know it worked when you see exactly 32 dots.

  4. Secret word decoder. Delete the letter labels (both write calls) and ask a friend to decode your word from the paths alone. You'll know it worked when they can read the bits right off the drawing.

You Speak Binary!

Monty celebrating You just drew what every computer does with text — turn letters into bits! ASCII, Unicode, files, and the whole internet rest on this one idea. Up next: Turtle Chase — a predator turtle hunts its prey with atan2().