← Course home

Advanced applications · Lesson 48 of 52

Tries and text indexes

Compare prefix tries and text indexes by query, build cost and storage.

Full lessonAbout 10 min

You will learn to match text queries to indexes and include construction and output costs.

Recommended first: String search algorithms.

On this page 6 sections

The idea

After inserting cat and car, contains(ca) is false.

A trie stores strings along a path of characters. Trie.insert walks or creates a child for each character. contains walks the same path and succeeds only when the whole key was inserted. After inserting cat and car, contains car is true, contains cat is true, and contains ca is false. ca is a prefix of both keys, and this trie does not treat a prefix as a stored key. A suffix array is a different index. suffix_array_with_lcp on banana returns the suffix starts 5, 3, 1, 0, 4, 2 and an LCP array whose third entry is 3. The suffixes in that order are a, ana, anana, banana, na, nana. Equal prefixes sit together, which is why a pattern query can binary-search the array. The LCP says how long the shared prefix of neighbors is, so you do not recompare those characters.

A worked trace

StepStateWhat changed
Insertkey = catInsert cat. contains cat is true.
Insertkey = carInsert car. contains car is true.
Misskey = cacontains ca is false. A prefix is not a stored key.

Why it is correct

Trie lookup is correct because each step consumes one character and the path is unique. Missing a child means the key was never inserted. Stopping in the middle is not a hit unless insert marked that node as a key, and this contains requires the full key. The suffix array is the sorted order of every suffix. Binary search is valid because that order is alphabetical. The LCP of two adjacent suffixes is a lower bound on comparisons you can skip. Construction has to be counted separately from a single query.

A common mistake

A proper prefix is not a stored key unless insert wrote that key itself.

What it costs

Inserting or looking up a key in the trie is proportional to the key length. Not to the number of stored keys, unless you are scanning children linearly. The trie memory is proportional to the number of distinct prefixes. Building a suffix array by sorting suffixes naively is more expensive than one query, and the array itself is linear in the text length. Include that build when you compare a trie with a scan of the raw text. A query-only bound hides the index. A lookup is Θ(L) in the key length, and the trie uses Θ(P) extra memory.

Run it

python
from bitsandbytes.strings.algorithms import Trietrie = Trie()trie.insert('cat')trie.insert('car')print(trie.contains('car'), trie.contains('ca'))# prints: True False 

Practice and next step

Open Range-query structures next. Range queries keep a prefix sum useful after a point update.

Implementation and tests Optional reference

Library code 1

  • bitsandbytes.strings.algorithms

Use these references to compare the explanation with repository code and tests. A test link means it imports a lesson symbol; it does not by itself claim complete behavior coverage.

Step-by-step trace

A trie stores keys, a suffix array stores positions

After inserting cat and car, contains(car) is true and contains(ca) is false. The suffix array of banana starts 5, 3, 1, 0, 4, 2.

key
cat

State

  1. 0cchosen
  2. 1achosen
  3. 2tchosen

Insert cat. contains cat is true.

Practice with feedback

Check your understanding

Try each checkpoint before revealing the feedback. Ask for a hint when you need one. Answers and hints stay in this browser when local storage is available.

1cat and car are inserted. What does contains report for ca?
2You compare a suffix-array query with a scan of the raw text. What must the comparison include?

Progress is never sent to an account or synced to another device. If local storage is blocked, choices last only until you leave this page.