Skip to the workspace
Pebellslearn by building
Trees, Heaps & Tries  ›  Lesson

Binary Trees & the Genre Tree

Binary Trees 25 minNodes with two children, and the walks that visit them all

dsa / Binary Trees & the Genre Tree

Explore each scene at your own pace.

01 · Grow the genre tree from Tunebox's JSON

Grow the genre tree from Tunebox's JSON

Binary Trees & the Genre Tree

Definition
A binary tree is a structure of nodes where each node holds a value and at most two children, called left and right; walking it visits every node once, in depth-first (pre-, in-, post-order) or breadth-first order.
In plain words
It is a family tree where everyone has at most two children. You can explore one whole branch before the next, or visit everyone generation by generation, but either way you meet each person once.

In short: a binary tree is made of nodes, each holding a value and up to two children, left and right — a missing child is None. Recursion walks it naturally, because every child starts its own subtree that follows exactly the same rules. The three depth-first orders — pre-, in- and post-order — differ only in when a node is recorded relative to its two children; breadth-first (level order) visits every node at one depth before any node at the next, using a queue. Every walk touches each node once, so all of them cost O(n) time; the extra space is O(h) for recursion's call stack, or O(w) for a queue, where h is the height and w the widest level.

Two small five-node trees side by side: the left one numbers its nodes 1,2,4,5,3 as it dives down one branch before trying the other, the right one numbers them 1,2,3,4,5 as it empties each row before going deeper

Grow the genre tree from Tunebox's JSON

Tunebox's genres aren't a flat list. Music splits into Rock and Electronic; Rock splits into Punk and Metal. A list can hold those names, but it can't say that Punk sits under Rock — a tree can:

python

The top node is the root; a node with no children is a leaf. Tunebox actually ships the tree as JSON, level by level, left to right, with None for a missing child: ["Music", "Rock", "Electronic", "Punk", "Metal", None, "House"]. A queue rebuilds it — the first value is the root, and after that, values arrive in pairs, the left and right child of each real node in turn. The easy mistake is forgetting that a None still claims its pair slot even though it gets no children of its own:

python

Height is how tall a tree is — we count nodes on the longest root-to-leaf path, so an empty tree has height 0 and a lone root height 1. Height is recursion in its purest form: a tree is one node taller than its taller subtree.

What does this print?
class Node:
    def __init__(self, val, left=None, right=None):
        self.val, self.left, self.right = val, left, right

def height(node):
    if node is None:
        return 0
    return 1 + max(height(node.left), height(node.right))

jazz = Node("Jazz", Node("Bebop", None, Node("Hard Bop")), Node("Swing"))
print(height(jazz))
Scene 1 of 8
Project context and further resources
You're building a piece ofTunebox — discovery
This piece — genre_paths(): Walks the genre hierarchy from the top down to every sub-genre.
Scenario Tunebox's genre browser opens every branch of the genre tree. It needs each full path — Music > Rock > Punk — to label the shelves.
genre_paths.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
genre_paths(levels) → listEvery path from the top genre down to a sub-genre with nothing beneath it.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.