Skip to the workspace
Pebellslearn by building
Balanced Trees  ›  Capstone Project

Capstone: Quarry's Index Layer

40 minShuffle a batch of ids into insert order, rebalance an AVL on every insert, then run a batch of searches — measured height, rotations, and search cost, not assumed ones

dsa / Capstone: Quarry's Index Layer

Explore each scene at your own pace.

01 · What you're building

What you're building

Capstone: Quarry's Index Layer

Definition
An index layer is a lookup structure that keeps ids in sorted order for fast search. Here it is an AVL tree, a binary search tree that rebalances with rotations, built from ids arriving in shuffled order and measured by height, rotations and search steps.
In plain words
It is the card catalogue of the engine, kept tidy as new cards arrive in no particular order. Rather than assume it stays quick, you count how tall it grew, how many fixes it needed, and how many steps an average search took.

In short: Every lesson in this section answered the same question a different way: what keeps a search tree short? Rotations are the one move that does it (lesson 1). AVL trees apply that move after every insert using a balance factor (lesson 2); B-trees sidestep the question by widening each node so a disk index stays shallow regardless (lesson 3); treaps and skip lists reach for randomness instead of bookkeeping (lesson 4); k-d trees split on alternating coordinates to prune a plane instead of a line (lesson 5). Quarry's index layer puts the AVL machinery to work on something closer to a real workload: ids that arrive in no particular order, and queries that follow.

What you're building

index_layer_report(n, seed) -> {
    "height": h, "rotations": r, "queries": n,
    "total_search_steps": t, "avg_search_steps": a,
    "balanced_bound": b,
}
index_layer_report(8, 42)
# {"height": 4, "rotations": 2, "queries": 8, "total_search_steps": 21,
#  "avg_search_steps": 2.62, "balanced_bound": 4}
  • height, rotations: the AVL tree's final height after inserting a shuffled batch of n ids, and how many rotations that took.
  • total_search_steps, avg_search_steps: run a search for every id, in a second shuffled order, and add up how many nodes each search visited.
  • balanced_bound: ceil(log2(n + 1)), the height a perfectly packed tree of n nodes would have — the number every other field is measured against.

Lesson 1 inserted ids in sorted order on purpose, to show the worst case. Real index layers rarely get keys that considerate. This capstone shuffles the insert order — and the query order — with a seeded random generator, so the numbers are realistic and exactly reproducible.

Scene 1 of 6
Project context and further resources
You're building a piece ofQuarry — the index layer
This piece — index_layer_report(): Runs a real insert/query mix and reports the index's height and how many rotations it took to stay that short.
Scenario Quarry's index layer is about to go live. Before it does, the team wants real numbers under a realistic insert/query mix — not the clean ascending order from lesson 1, and not a hand-picked best case.
Additional resources
index_layer_report.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
index_layer_report(n, seed) → dictThe AVL index's final height and rotation count after n shuffled inserts, plus the total and average search cost over n shuffled queries.
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.