Exercise 3: Why a Radix Tree's Cost Doesn't Grow With Route Count — Possible Solution ==================================================================== WHAT THE LINEAR-SCAN ROUTER ACTUALLY DOES ------------------------------ This chapter's own resolve() function loops over the full routes list, checking each pattern against the incoming path one at a time, in order, until one matches (or the list runs out). Every single registered route is a genuinely separate, independent check - the router has no way to know in advance that, say, /users/42 and /users/settings share the same "/users/" prefix, so it re-examines that shared prefix separately for every single route that happens to start with it. Adding a thousandth route means a thousandth full comparison might be needed before a match (or a 404) is found - the work done is directly proportional to n, the number of registered routes. WHAT A RADIX TREE DOES DIFFERENTLY ------------------------------ A radix tree stores routes as a real tree structure where routes sharing a common prefix literally share the same parent node in the tree, rather than being stored as n separate, independent entries. A tree holding /users/42, /users/settings, and /users/new would have one shared node for the common "/users/" segment, branching into separate children only where the paths actually diverge. Looking up a specific path means walking down the tree one segment (or character) at a time, following the branch that matches each part of the path being searched for - and at each step, the number of CANDIDATE branches at that node is small and bounded (however many real children that specific node happens to have), not "every route in the whole application." The number of steps taken is determined by how long the path being looked up is (k), not by how many total routes exist elsewhere in the tree. THE KEY DIFFERENCE, STATED DIRECTLY ------------------------------ The linear scanner re-examines the shared "/users/" prefix separately, once per route, for every route under it - duplicated work that grows with n. The radix tree examines that same shared prefix exactly ONCE, via the one shared parent node, no matter how many routes branch off beneath it. Adding a thousand more routes under /users/ costs the radix tree nothing extra when looking up a completely unrelated path like /posts/hello-world, because that lookup never even visits the /users/ portion of the tree at all - while the linear scanner would still have to skip past every one of those thousand extra entries one by one before reaching a route that actually matches. WHY THIS WORKS AS AN ANSWER ---------------------------- It explains concretely what each approach does with a shared path prefix (the linear scanner treats every route as fully independent and re-checks the prefix redundantly; the radix tree stores the shared prefix exactly once via a common parent node), and uses that concrete mechanical difference - not just the O(n) vs. O(k) notation alone - to explain why one genuinely grows with route count and the other genuinely doesn't.