Lesson
The theory — Trie: The Prefix Tree, Step by Step
A trie, or prefix tree, stores a set of words as a tree with one node for every distinct prefix of them. The root stands for the empty prefix, each edge carries one letter, so the path from the root to a node spells that node's prefix, and a node is marked when its prefix is a whole stored word. Words that begin alike share the path for their common beginning. A lookup follows the word's letters from the root; a prefix search walks to the prefix's node and lists every marked node below it. Edward Fredkin named the structure in 1960, from retrieval, and meant it to be said like tree.
What each symbol means
W- the number of words in the list
L- the number of letters in all of them together
N- the number of nodes, one per distinct prefix, the root not counted
- Assumes
- Lowercase a to z, one byte per letter, 64-bit pointers, and no padding or allocator overhead in either structure. Binary search counts every pair of letters it compares and stops at the first pair that differ; reaching the end of a word costs it nothing. The hash table is given no collisions at all, its best case. The 18-byte node keeps its children in alphabet order, so a search along them stops as soon as it passes the letter it wants.
Problem solved in full
-
How much sharing would make the trie the smaller one? 4 steps
Press Long words: 85 letters in 21 nodes, three quarters of them shared, and the trie of 18-byte nodes still takes 396 bytes against the list's 157. Among lists whose words all have k letters, the trie does best against the list when the list holds every possible one. Under the page's byte counts, what is the shortest k for which that trie is smaller than the plain list?
-
On the page: 21 nodes and the root, 22 × 18 = 396 bytes. The list stores 85 letters and, for each of its 8 words, an end byte and an 8-byte pointer: 85 + 9 × 8 = 157.
-
Every string of k letters is 26k words of k letters each, so the list takes (k + 9) × 26k bytes.
-
The trie needs a node for every string of 1 to k letters: 26 + 26² + … + 26k, a little under 26/25 × 26k. Add the root, multiply by 18, and the trie takes just under 18.72 × 26k bytes.
-
So the list is the larger one once k + 9 passes 18.72, which first happens at k = 10. At k = 9 the trie is 4.0% larger than the list; at k = 10 it is 1.5% smaller.
Answer
Ten letters, with all 2610 of them in the list, about 1.4 × 1014 words. With 209-byte nodes the same sum asks for k + 9 to pass about 217, words of 209 letters.
-
Learning path
What a data structure makes cheap
References (3)
- The name, its origin in "retrieval", and Fredkin's intended pronunciation, quoted in the definition: Paul E. Black, "trie", in Dictionary of Algorithms and Data Structures [online], Paul E. Black, ed., NIST, entry modified 12 February 2019.
- The paper that named the structure: Edward Fredkin, "Trie memory," Communications of the ACM 3(9), September 1960, pp. 490–499.
- The earlier description of searching on letter-by-letter keys: René de la Briandais, "File searching using variable length keys," Papers presented at the Western Joint Computer Conference, San Francisco, 3–5 March 1959, pp. 295–298.