The Giving Trie

 Alternative Titles:

A Trie Grows in Palo Alto

The Trie of Life

The Best Time to Make a Trie is Twenty Years Ago, the Second Best Time is Now

The Trie of the Knowledge of 1 and 0

If a Trie Fails and No One Sees the Error, Does The Code Compile?


What is a Trie?

A trie (generally pronounced like "tree") is a searchable tree that has individual letters for nodes.  So in the example above, we have the following keys associated with their values:

"to": 7

"tea": 3

"ted": 4

"ten": 12

"A": 15

"i": 11

"in": 16

"inn": 25

Keys are only complete when you run into a value - so just "t" or "te" by themselves do not constitute keys for this trie.

Tries are sort of a niche data structure, but they do have some advantages: the lookup time for a trie will only ever be at most the length of the longest key.  In comparison to a binary search tree - which can at most have only 2 sub-trees coming off of each node - a trie can be very, very wide (as wide as the number of available letters for your key string), which can make for some crazy efficient searches.  In fact, you can view natural language, either spoken or written, as a trie, with the words of the language corresponding to keys and their meanings corresponding to the values.

While slower than hashes, tries have an advantage over them in that tries avoid collisions - where two values have the same key (due to a too-short input key size creating only a finite number of keys smaller than the number of values, or hashing function that incorrectly assigns two values to the same key).  On the other hand, tries often require an individual block of memory for each character in a key string, as opposed to a single block of memory for a hash key.

Tries are most often used in predictive text in things like phone autocompletes, web searches, etc. - as the user types in letters we transverse further down the trie and as the possible nodes thin out we can easily suss out what possible words users are going to write.

Tries are also used for IP routing as (see an example here) in the form of Patricia tries, a special type of trie in which nodes with only one child are merged with the parent node.  The ability of a Patricia trie to quickly deal with long prefixes makes them especially well-suited to this application. 

Examples

Here are some good implementations of Tries in my preferred languages:

JavaScript example

JavaScript example of a radix trie

Ruby example

In an exploit of writable or executable code



Comments

Popular posts from this blog

The Sorting Hat

Kadane's Algorithm

Loose Equality, the Null Operator, and Other Oddities