TL;DR
A radix tree is a trie with its single-child chains squeezed out. Where a trie has one node per letter, a radix tree lets an edge carry a whole string, so a run of letters with no branching becomes one edge. It answers the same questions as the trie, such as "is this word stored?" and "which words start with this prefix?", but with fewer nodes.
Builds on: Trie · Leads to: SGLang's prefix cache, and compact indexes in routers and databases.
Where it's used
URL routers in web frameworks. A web server must decide which handler serves a path such as /search/images. Routing libraries store the registered paths in a radix tree, so a path like /search that many routes share is kept as one edge, not one node per character.
Caches of shared prompts in language model servers. When many requests start with the same long system prompt, a server can reuse the stored attention (KV) cache for that shared beginning. SGLang keeps those cached token sequences in a radix tree, so a new request walks down the tree and reuses as much of the stored prefix as it can. When memory runs short, the least recently used branches are evicted, as in any cache eviction policy.
Compact indexes in systems software. Key-value stores and kernels use radix trees where keys are long and share long beginnings, such as route prefixes or memory addresses. Redis, for example, uses a radix tree to index the IDs in its stream data type. The job is the same as a Database Index: find a key without scanning every key.
How it works
Take four words: "sea", "search", "searcher" and "dog". First build the plain trie, then compress it.
In the plain trie, "sea", "search" and "searcher" share the path s, e, a, and then "search" continues r, c, h, while "searcher" continues r, c, h, e, r. Counting the non-root nodes gives 11 nodes for the four words.
Now compress. Walk down the trie and merge every node that has exactly one child and is not the end of a word into its child:
flowchart TD R["root"] -->|"sea"| A["sea (word)"] A -->|"rch"| B["search (word)"] B -->|"er"| C["searcher (word)"] R -->|"dog"| D["dog (word)"]
-
Start with the trie. Each edge holds one letter. Check: there is one non-root node for each distinct prefix of the four words: s, se, sea, sear, searc, search, searche, searcher, d, do, dog. That is 11.
-
Merge the chains from the root. The nodes "s" and "e" each have exactly one child and are not words, so they merge with "a" into one edge "sea". The node for "sea" is a word, so it stays. Below it, the nodes "r", "c", "h" each have exactly one child, and none is a word end until "h", so they merge into one edge "rch" that leads to "search". Check: "search" is still reached by spelling "sea" and then "rch".
-
Merge the chain after "search". The node for "search" has one child, "e", then "r". Replace those two edges with one edge labelled "er". Check: "searcher" is still reached by spelling "search" and then "er".
-
Keep the branch at the root. The root has two children, "sea" and "dog", so it stays a branching node. The "dog" branch is three letters with no branching, so it becomes one edge labelled "dog". Check: after compression there are 4 non-root nodes: "sea", "search", "searcher" and "dog".
-
Look up "searcher". Start at the root. The edge "sea" matches the first three letters. Remaining: "rcher". The next edge is "rch", which matches; remaining: "er". The edge "er" matches; remaining: nothing. The node reached is marked as a word, so "searcher" is stored. Check: three edge matches for an eight-letter word.
-
Look up "seek". Start at the root. The edge "sea" is compared with "see": the first two letters match, but the third differs, "a" against "e". No edge matches, so "seek" is not stored. Check: the walk stops at the first mismatch inside an edge, not after reading the whole word.
-
Look up "se". The edge "sea" is longer than what is left of "se", so the query ends partway along that edge. The point reached is not a node, so "se" is not stored as a word. Check: a prefix that ends inside an edge is a prefix, not a word.
The math
Let be a set of strings over an alphabet, and let be the total number of letters in .
A radix tree for is a rooted tree in which each edge carries a non-empty string label. Every path from the root spells a string, which is the concatenation of the labels along it. Two children of the same node never have labels that begin with the same letter. A node is marked as a word end exactly when the string spelled to it is in . Every node other than the root is either a word end or has at least two children.
The last condition is what "merge single-child chains" means. A node with one child and no word mark carries no information of its own, so it can be absorbed into its child's edge. The rule that siblings never start with the same letter is the same idea behind a prefix-free code: no label can be the start of another label at the same node, so a lookup always knows which single edge to follow.
Step 4 in symbols: the same language, fewer nodes
Each node other than the root is a word end or has at least two children. Count them. The number of word ends is at most . Each leaf is a word end, so the number of leaves is at most . A tree with leaves has at most nodes with two or more children. Adding the root gives a total of at most
nodes. For the four-word example, , so the bound is 8, and the real tree has 5 nodes including the root. A plain trie can need up to nodes. For short words that bound is not much smaller, but for long words that share few letters the radix tree stays at while the trie grows with .
Step 5 in symbols: the cost of a lookup
Let a query string have length . Each edge matched during a lookup consumes at least one letter of the query, so the walk takes at most edge matches. Each match compares the label letter by letter, so the total number of letter comparisons is at most as well. A lookup therefore costs (big-O notation), the same as in a trie, and it does not depend on .
Worked examples
Store "cat" and "car".
- The trie has nodes c, a, then branches into t and r. That is 4 non-root nodes: c, a, t, r.
- Node c has one child, a. Node a is not a word, and it has two children, t and r. So merge c and a into one edge "ca".
- The compressed tree is: root, then edge "ca", then two leaf edges "t" and "r". The non-root nodes are: the node after "ca", and the two leaves. That is 3 non-root nodes.
- Look up "car". Match the edge "ca", remaining "r". Match the edge "r", remaining nothing. That node is a word end, so "car" is stored.
Interpretation: the trie used 4 nodes, the radix tree used 3. The saving is one node, because the chain c, a was merged.
Suppose a dictionary has words, averaging 8 letters, so .
- A plain trie can have up to nodes.
- A radix tree has at most nodes, by the bound in the math section.
- A lookup for an 8-letter word takes at most 8 edge matches in either structure, because each edge consumes at least one letter.
- The radix tree therefore uses about a quarter of the node bound of the trie in this case, with the same lookup cost.
Interpretation: the number of nodes depends on how many words there are, not on how long they are, so compression matters most when words are long.
A small server registers three routes: "/search", "/search/images" and "/status". Find the route for "/search/images".
- The shared start of the three routes is "/s". Store it as the root's edge.
- After "/s" the routes split into "earch" (for "/search") and "tatus" (for "/status"). Those are the two children of the node after "/s".
- The "earch" node is a route, and it has one child, "/images". Store "/images" as its edge.
- Look up "/search/images". The root edge "/s" matches, remaining "earch/images". The edge "earch" matches, remaining "/images". The edge "/images" matches, remaining nothing. That node is a route, so the handler for "/search/images" is found.
- Count the work: 3 edge matches for a 14-character path.
Try it: a request arrives for "/searching". Which route, if any, matches, and why? Answer: none. The walk matches "/s" and then "earch", leaving "ing". The "earch" node has one child, "/images", which does not match "ing". The walk stops with a remainder, so no route matches.
Common mistakes and tradeoffs
It is tempting to think that a radix tree is a different kind of structure that answers different questions. In fact it answers exactly the questions a trie answers, because the compression only merges chains that carry no branching or word marks. The language stored is the same.
It is tempting to think that every edge still holds one letter. In fact an edge holds a whole string, and a lookup must compare that string, not just its first letter. A lookup that stops partway along an edge has found a prefix, not a word.
It is tempting to think that the radix tree is always faster. In fact a lookup is still and can be slower in practice, because each edge match compares several letters and the code is more complicated. Insertion is also harder: when a new word leaves a shared prefix partway along an existing edge, that edge must be split in two. A hash table stores exact keys with less code, and is usually the right choice when only exact lookups are needed. A B-tree is the usual choice for large sorted indexes on disk, and an Inverted Index is the usual choice for finding words inside documents.
When to use a radix tree: when the keys are long, share long beginnings, and you need prefix search, such as routing paths, IP prefixes or indexes of long strings. When not to: when the keys are short or share little, because then there are few chains to merge and a plain Trie is simpler. Alternatives: a hash table for exact lookups, and a plain trie when the code should stay simple.
History
The idea of skipping over single-branch parts of a binary tree is older than the name. In 1968, Donald R. Morrison described PATRICIA, a "Practical Algorithm To Retrieve Information Coded in Alphanumeric", which stores a binary digital tree with chains skipped. Its ideas were used for indexes and routing tables for decades. The general radix tree, with edges labelled by strings, follows the same plan and is the form used in the systems above.
Recap
- A trie has one node per letter. A radix tree merges every chain of single-child, non-word nodes into one edge labelled with a whole string.
- The two structures store the same set and answer the same queries. The radix tree has at most nodes, where is the number of words.
- A lookup takes at most one edge match per letter of the query, so it still costs .
- Insertion may split an edge when a new word shares a partial prefix with an existing edge.
- Next, read about the trie it compresses if you have not already, and see the radix tree at work in SGLang, which keeps its cached prompts in one.
Check yourself
QuestionA trie stores the words 'tea' and 'ten'. How many non-root nodes does the plain trie have, and how many does the radix tree have?Show answer
The plain trie has t, e, a and n: four non-root nodes, because "t" and "e" are shared. In the trie, the node for "t" has one child, "e", and is not a word, so it merges into its child. The result is one edge "te" from the root, leading to a node with two children, "a" and "n". Counting non-root nodes: the node reached by "te", the leaf for "tea", and the leaf for "ten", which is 3 nodes.
QuestionFor a set of 500 words, what is the largest number of nodes a radix tree can have, and why?Show answer
The bound is nodes. Each non-root node is either a word end or has at least two children. There are at most 500 word ends, and at most 499 nodes with two or more children, plus the root, so the total is at most 1000.
QuestionWhy does a lookup in a radix tree cost at most L edge matches for a query of length L, even though an edge can carry many letters?Show answer
Every edge in the path carries at least one letter, and each matched edge uses up its letters from the query. The query has only L letters, so the walk can match at most L edges. A walk that uses an edge with several letters uses up several query letters at once, so it takes fewer steps, not more.
QuestionA radix tree stores the routes '/api' and '/apiary'. A request for '/api/v2' arrives. Is there a route for it, and what does the lookup do?Show answer
The lookup matches the edge "/api", which is a route, then has the remaining text "/v2". The node for "/api" has one child, "ary", from "/apiary", and "/v2" does not match "ary". The walk stops with a remainder and no route matches. An exact-match router therefore returns no route for '/api/v2', even though '/api' is a route.
QuestionYou are building an autocomplete system for product names of about 50 characters each, with a million names that share many beginnings. Why might you choose a radix tree over a plain trie?Show answer
A plain trie would have up to about 50 million nodes, one per letter, while a radix tree has at most 2 million nodes, one bound per name. Product names share long beginnings, so many chains merge. Lookups cost the same order, , in both, so the saving in memory is the main reason to choose the radix tree.