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

Binary Search Trees & the Release Catalog

Binary Search Trees 25 minA tree that keeps smaller keys left and bigger keys right

dsa / Binary Search Trees & the Release Catalog

Explore each scene at your own pace.

01 · Keep the release catalog ordered as it grows

Keep the release catalog ordered as it grows

Binary Search Trees & the Release Catalog

Definition
A binary search tree is a binary tree in which every key in a node's left subtree is smaller and every key in its right subtree is larger, so search and insert follow a single path from the root, costing O(h) for height h.
In plain words
It is a game of higher or lower built into the shape: at each node you go left for smaller and right for bigger, ruling out a whole side every time. If the tree grows lopsided, though, the path gets long.

In short: a binary search tree is a binary tree with one rule, holding at every node: every key in the left subtree is smaller, and every key in the right subtree is bigger (Tunebox sends ties right). That single rule turns search, insert and range queries into one path from the root — O(h), where h is the tree's height. The catch: h depends entirely on the order keys arrive in. The same keys can build a tree of height 3 or height 7, and only self-balancing trees (AVL, red-black) guarantee the short one.

Two trees of the same seven years: inserted middle-first they form a bushy triangle three levels tall; inserted oldest-first they form a straight diagonal chain seven levels tall

Keep the release catalog ordered as it grows

New releases land every day. Re-sorting a list after every insert costs O(n log n) each time; a BST takes a new release by walking a single path. Each new year walks down from the root, going left when it is earlier and right otherwise, so the rule holds for every ancestor, not just the parent:

watch it move · tree

Each new year walks down from the root, going left when it is earlier and right otherwise, so the rule holds for every ancestor and not just the parent.

1987 is earlier than 1999, so it lands in the left subtree. Binary tree, 2 nodes, height 2. At node 1987.

1 / 5

The rule covers whole subtrees, not just a node's two children — and that's exactly where a shallow check goes wrong:

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

def looks_ok(node):
    if node is None:
        return True
    if node.left and node.left.key >= node.key:
        return False
    if node.right and node.right.key < node.key:
        return False
    return looks_ok(node.left) and looks_ok(node.right)

root = Node(1999, Node(1987, None, Node(2003)), Node(2012))
print(looks_ok(root))

Insertion follows the path a search would: step left or right until you fall off onto None, and that empty slot is where the new key belongs.

step through it
1class Node:
2 def __init__(self, key):
3 self.key, self.left, self.right = key, None, None
4 
5def insert(root, key):
6 if root is None:
7 return Node(key)
8 if key < root.key:
9 root.left = insert(root.left, key)
10 else:
11 root.right = insert(root.right, key) # ties go right
12 return root
13 
14root = None
15for year in [1999, 1987, 2012, 1994]:
16 root = insert(root, year)
17print(root.left.right.key)

Returning root from every call is what lets one line — root.left = insert(...) — attach the new node, and it handles an empty tree for free. Step through four more inserts and watch the shape form:

your code, drawn · tree

Your BST insert, one comparison per level — the new key always ends up as a leaf.

Scene 1 of 9
Project context and further resources
You're building a piece ofTunebox — discovery
This piece — releases_between(): Keeps tracks ordered by release year so range queries stay fast.
Scenario Tunebox's catalog filter lets listeners pick a span of years. New releases are added every day, so the catalog lives in a binary search tree keyed by release year.
releases_between.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
releases_between(releases, start, end) → listTitles released from start to end inclusive, in year order.
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.