Imagine you run a conference center. You have a calendar full of bookings — each one claiming a stretch of time. A new client calls and asks whether a particular hour is free. How do you find out?
The naive answer is to scan every booking. With ten reservations that is fine. With ten million — in a hospital bed scheduler, a stock exchange, or a cloud computing cluster — scanning is unacceptably slow.
The interval tree is the data structure built for exactly this question. It stores a set of intervals (pairs of start and end times, or any two numbers ) and answers stabbing queries: "which intervals contain the point ?" in time , where is the number of intervals actually hit. You pay only for what you find, plus a small logarithmic overhead — no matter how many millions of intervals you store.
The same idea also handles overlap queries: "which stored intervals overlap the range ?" A query interval stabs anything whose left end is and whose right end is .
The result is a beautifully simple idea hiding a careful invariant — and it appears quietly in everything from database engines to graphics renderers.
Comments
Loading comments...