Capstone: Quarry's Index Layer
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 ofnids, 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 ofnnodes 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.